Permalink
Browse files

Now using LEDA's rational number type

  • Loading branch information...
spratt committed Jul 24, 2012
1 parent 0c6571b commit fd8b6fedfdaf6be51b314ec993115539b900476e
View
@@ -114,6 +114,7 @@ namespace geometry {
}
bool LineSegment::operator<(const LineSegment& other) const {
+ clog << *this << " < " << other << endl;
bool yasc_flag, ydesc_flag, xasc_flag, xdesc_flag;
if(this->getLeftEndPoint().x < other.getLeftEndPoint().x) {
ydesc_flag = ydesc(*this,other);
@@ -129,40 +130,26 @@ namespace geometry {
xasc_flag = xdesc(other,*this);
xdesc_flag = xasc(other,*this);
}
- if(ydesc_flag)
+ if(ydesc_flag) {
+ clog << "ydesc flag" << endl;
return true;
- if(yasc_flag)
+ } else if(yasc_flag) {
+ clog << "yasc flag" << endl;
return false;
- if(xdesc_flag)
+ } else if(xdesc_flag) {
+ clog << "xdesc flag" << endl;
return true;
+ } else if(xasc_flag) {
+ clog << "xasc flag" << endl;
+ return false;
+ }
+ clog << "equal?" << endl;
// xasc or equal
return false;
}
bool LineSegment::operator>(const LineSegment& other) const {
- bool yasc_flag, ydesc_flag, xasc_flag, xdesc_flag;
- if(this->getLeftEndPoint().x < other.getLeftEndPoint().x) {
- ydesc_flag = ydesc(*this,other);
- yasc_flag = yasc(*this,other);
- } else {
- yasc_flag = ydesc(other,*this);
- ydesc_flag = yasc(other,*this);
- }
- if(this->getBottomEndPoint().y < other.getBottomEndPoint().y) {
- xdesc_flag = xdesc(*this,other);
- xasc_flag = xasc(*this,other);
- } else {
- xasc_flag = xdesc(other,*this);
- xdesc_flag = xasc(other,*this);
- }
- if(yasc_flag)
- return true;
- if(ydesc_flag)
- return false;
- if(xasc_flag)
- return true;
- // xdesc or equal
- return false;
+ return !operator==(other) && !operator<(other);
}
bool LineSegment::operator<=(const LineSegment& other) const {
@@ -176,18 +163,31 @@ namespace geometry {
// returns true if left belongs above right in descending order
bool LineSegment::ydesc(const LineSegment& left, const LineSegment& right) {
// both vertical
- if(left.isVertical() && right.isVertical())
- return left.getBottomEndPoint().y > right.getBottomEndPoint().y;
+ if(left.isVertical() && right.isVertical()) {
+ if(left.getBottomEndPoint().y > right.getBottomEndPoint().y)
+ return true;
+ if(left.getBottomEndPoint().y < right.getBottomEndPoint().y)
+ return false;
+ return left.getTopEndPoint().y > right.getTopEndPoint().y;
+ }
// left is vertical
if(left.isVertical())
return left.getBottomEndPoint().y > right.getLeftEndPoint().y;
// left.left, left.right, right.left colinear
if(Point2D::colinear(left.getLeftEndPoint(),
left.getRightEndPoint(),
- right.getLeftEndPoint()))
+ right.getLeftEndPoint())) {
+ // all points colinear
+ if(Point2D::colinear(left.getLeftEndPoint(),
+ left.getRightEndPoint(),
+ right.getRightEndPoint())) {
+ return left.getBottomEndPoint().y > right.getBottomEndPoint().y;
+ }
+ // only 3 colinear
return Point2D::rightTurn(left.getLeftEndPoint(),
left.getRightEndPoint(),
right.getRightEndPoint());
+ }
// normal case
return Point2D::rightTurn(left.getLeftEndPoint(),
left.getRightEndPoint(),
@@ -197,18 +197,31 @@ namespace geometry {
// returns true if left belongs above right in ascending order
bool LineSegment::yasc(const LineSegment& left, const LineSegment& right) {
// both vertical
- if(left.isVertical() && right.isVertical())
- return left.getBottomEndPoint().y < right.getBottomEndPoint().y;
+ if(left.isVertical() && right.isVertical()) {
+ if(left.getBottomEndPoint().y < right.getBottomEndPoint().y)
+ return true;
+ if(left.getBottomEndPoint().y > right.getBottomEndPoint().y)
+ return false;
+ return left.getTopEndPoint().y < right.getTopEndPoint().y;
+ }
// left is vertical
if(left.isVertical())
return left.getBottomEndPoint().y < right.getLeftEndPoint().y;
// left.left, left.right, right.left colinear
if(Point2D::colinear(left.getLeftEndPoint(),
left.getRightEndPoint(),
- right.getLeftEndPoint()))
+ right.getLeftEndPoint())) {
+ // all points colinear
+ if(Point2D::colinear(left.getLeftEndPoint(),
+ left.getRightEndPoint(),
+ right.getRightEndPoint())) {
+ return left.getBottomEndPoint().y < right.getBottomEndPoint().y;
+ }
+ // only 3 colinear
return Point2D::leftTurn(left.getLeftEndPoint(),
left.getRightEndPoint(),
right.getRightEndPoint());
+ }
// normal case
return Point2D::leftTurn(left.getLeftEndPoint(),
left.getRightEndPoint(),
View
@@ -31,12 +31,15 @@ CXX = g++
VALGRIND = valgrind
VGOPS = --leak-check=full -v --show-reachable=yes
+LEDAROOT = /home2bak/spratt/Projects/PrioritySearchTree/LEDA/6.3-x64
+LEDAFLAGS= -L$(LEDAROOT) -I$(LEDAROOT)/incl -lleda -lX11 -lm
+
# if mode is release, don't include debug info
ifeq ($(mode),release)
- CXXFLAGS=-O2 -Wall -DNDEBUG
+ CXXFLAGS=-O2 -Wall -DNDEBUG $(LEDAFLAGS)
else
mode = debug
- CXXFLAGS=-g -std=c++98 -pedantic-errors -Wall -Werror
+ CXXFLAGS=-g -std=c++98 -pedantic-errors -Wall -Werror $(LEDAFLAGS)
endif
BAR = "======================================================================"
@@ -64,11 +67,13 @@ TESTS = ${TEST_LS}
.SILENT: run_tests run_tests_mac
#begin actual makefile stuff
+tests: ${TESTS} ${TEST_PS}
+
+all: get_libs tests
+
get_libs:
cd lib;./get_libs.sh
-all: get_libs ${TESTS} ${TEST_PS}
-
run: run_tests_mac run_test_ps
test: run_tests_mac run_tests
View
@@ -69,24 +69,4 @@ namespace geometry {
return is;
}
}
-namespace std {
- /////////////////////////////////////////////////////////////////////////////
- // Define limits for points //
- /////////////////////////////////////////////////////////////////////////////
- geometry::Point2D numeric_limits<geometry::Point2D>::infinity() {
- assert(numeric_limits<geometry::Point2D>::has_infinity);
- return geometry::Point2D(numeric_limits<geometry::coord_t>::infinity(),
- numeric_limits<geometry::coord_t>::infinity());
- }
- geometry::Point2D numeric_limits<geometry::Point2D>::max() {
- assert(! numeric_limits<geometry::Point2D>::has_infinity);
- return geometry::Point2D(numeric_limits<geometry::coord_t>::max(),
- numeric_limits<geometry::coord_t>::max());
- }
- geometry::Point2D numeric_limits<geometry::Point2D>::min() {
- assert(! numeric_limits<geometry::Point2D>::has_infinity);
- return geometry::Point2D(numeric_limits<geometry::coord_t>::min(),
- numeric_limits<geometry::coord_t>::min());
- }
-}
View
@@ -29,9 +29,13 @@
using namespace std;
+#include <LEDA/numbers/rational.h>
+
+using leda::rational;
+
namespace geometry {
// Defines the implementation and precision of coordinates
- typedef int coord_t;
+ typedef rational coord_t;
/////////////////////////////////////////////////////////////////////////////
// Point2D interface //
@@ -12,6 +12,7 @@
#include <algorithm>
#include "PolygonalSubdivision.hpp"
#include <iostream>
+#include <sstream>
namespace geometry {
@@ -61,6 +62,8 @@ namespace geometry {
if(_locked)
return;
+ clog << "There are " << line_segments_left.size() << " segments." << endl;
+
// lock the division
_locked = true;
@@ -84,12 +87,16 @@ namespace geometry {
for(set< coord_t >::iterator coord = x_coords.begin();
coord != x_coords.end();
++coord) {
+ clog << "Considering x=" << *coord << endl;
+ int present = psl.getPresent();
// add points whose left end points are on the sweep line
{
LineSegment& line = line_segments_left.back();
while(line_segments_left.size() > 0 &&
line.getLeftEndPoint().x <= (*coord)) {
+ clog << "Considering segment for insertion: " << line << endl;
if(line.isVertical()) {
+ clog << "Too vertical." << endl;
if(vertical_lines.count(line.getFirstEndPoint().x) == 0)
vertical_lines[line.getFirstEndPoint().x] =
vector<LineSegment>();
@@ -99,10 +106,16 @@ namespace geometry {
continue;
}
try {
+ // DEBUG
+ clog << "Inserting..." << endl;
+ // END DEBUG
psl.insert(line);
} catch(char const* exception) {
psl.drawPresent();
- throw "Error while trying to insert line";
+ stringstream ss;
+ ss << "Error while trying to insert line: " << line << endl
+ << "Found: " << *(psl.find(line,present)) << endl;
+ throw ss.str();
}
if(line_segments_right.count(line.getRightEndPoint().x) == 0) {
line_segments_right[line.getRightEndPoint().x] =
@@ -113,28 +126,44 @@ namespace geometry {
line = line_segments_left.back();
}
}
+ clog << "Done insertions" <<endl;
// remove points whose right end points are at most the sweep line
- int present = psl.getPresent();
{
vector<LineSegment>::iterator it =
line_segments_right[*coord].begin();
vector<LineSegment>::iterator end =
line_segments_right[*coord].end();
while(it != end) {
+ clog << "Deleting segment: " << *it << endl;
PSLIterator<LineSegment> toRemove = psl.find((*it),present);
if((*it) != (*toRemove)) {
clog << "=== ERROR ===" << endl
<< "Deletion mismatch" << endl
<< "Sought: " << (*it) << endl
<< "Found: " << (*toRemove) << endl;
+#ifndef NDEBUG
+ clog << "Search path: ";
+ for(vector<LineSegment>::iterator path_item = psl.lastSearchPath.begin();
+ path_item != psl.lastSearchPath.end();
+ ++path_item)
+ clog << *path_item << ", ";
+ clog << endl;
+#endif
+ clog << "Printing contents of psl: " << endl;
+ for(PSLIterator<LineSegment> psl_it = psl.begin(present);
+ psl_it != psl.end(present);
+ ++psl_it) {
+ clog << *psl_it << ", ";
+ }
+ clog << endl;
}
assert((*it) == (*toRemove));
toRemove.remove();
++it;
}
line_segments_right.erase(*coord);
}
-
+ psl.drawPresent();
psl.incTime();
sweep_points.push_back(*coord);
}
Oops, something went wrong.

0 comments on commit fd8b6fe

Please sign in to comment.