worked on floorplan (v2)
worked on grid-generation (v2) new helper methods for geometry new test cases
This commit is contained in:
29
geo/BBox2.h
29
geo/BBox2.h
@@ -42,6 +42,10 @@ public:
|
||||
/** get the bbox's maximum */
|
||||
const Point2& getMax() const {return p2;}
|
||||
|
||||
/** get the bbox's center point */
|
||||
Point2 getCenter() const { return (p1+p2) / 2; }
|
||||
|
||||
|
||||
/** equal? */
|
||||
bool operator == (const BBox2& o) const {
|
||||
return (p1.x == o.p1.x) &&
|
||||
@@ -52,16 +56,27 @@ public:
|
||||
|
||||
/** does the BBox intersect with the given line? */
|
||||
bool intersects (const Line2& l) const {
|
||||
Line2 l1(p1.x, p1.y, p2.x, p1.y); // upper
|
||||
Line2 l2(p1.x, p2.y, p2.x, p2.y); // lower
|
||||
Line2 l3(p1.x, p1.y, p1.x, p2.y); // left
|
||||
Line2 l4(p2.x, p1.y, p2.x, p2.y); // right
|
||||
const Line2 l1(p1.x, p1.y, p2.x, p1.y); // upper
|
||||
const Line2 l2(p1.x, p2.y, p2.x, p2.y); // lower
|
||||
const Line2 l3(p1.x, p1.y, p1.x, p2.y); // left
|
||||
const Line2 l4(p2.x, p1.y, p2.x, p2.y); // right
|
||||
return l.getSegmentIntersection(l1) ||
|
||||
l.getSegmentIntersection(l2) ||
|
||||
l.getSegmentIntersection(l3) ||
|
||||
l.getSegmentIntersection(l4);
|
||||
}
|
||||
|
||||
std::vector<Line2> lines() const {
|
||||
return std::vector<Line2>(
|
||||
{
|
||||
Line2(p1.x, p1.y, p2.x, p1.y),
|
||||
Line2(p1.x, p2.y, p2.x, p2.y),
|
||||
Line2(p1.x, p1.y, p1.x, p2.y),
|
||||
Line2(p2.x, p1.y, p2.x, p2.y)
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
bool contains(const Point2& p) const {
|
||||
if (p.x < p1.x) {return false;}
|
||||
if (p.x > p2.x) {return false;}
|
||||
@@ -76,6 +91,12 @@ public:
|
||||
p2 += p; // increase maximum
|
||||
}
|
||||
|
||||
/** grow the bbox by the amount given for each dimension */
|
||||
void grow(const float val) {
|
||||
p1 -= Point2(val, val); // decrease minimum
|
||||
p2 += Point2(val, val); // increase maximum
|
||||
}
|
||||
|
||||
};
|
||||
|
||||
#endif // BBOX2_H
|
||||
|
||||
66
geo/Line2.h
66
geo/Line2.h
@@ -55,6 +55,35 @@ public:
|
||||
|
||||
bool getSegmentIntersection(const Line2& other) const {
|
||||
|
||||
const double delta = 0.0000001;
|
||||
|
||||
const double bx = p2.x - p1.x;
|
||||
const double by = p2.y - p1.y;
|
||||
|
||||
const double dx = other.p2.x - other.p1.x;
|
||||
const double dy = other.p2.y - other.p1.y;
|
||||
|
||||
const double b_dot_d_perp = bx*dy - by*dx;
|
||||
|
||||
if (std::abs(b_dot_d_perp) == 0) {return false;}
|
||||
|
||||
const double cx = other.p1.x - p1.x;
|
||||
const double cy = other.p1.y - p1.y;
|
||||
|
||||
const double t = (cx * dy - cy * dx) / b_dot_d_perp;
|
||||
if(t < 0+delta || t > 1-delta) {return false;}
|
||||
|
||||
const double u = (cx * by - cy * bx) / b_dot_d_perp;
|
||||
if(u < 0+delta || u > 1-delta) {return false;}
|
||||
|
||||
return true;
|
||||
|
||||
}
|
||||
|
||||
bool getSegmentIntersection(const Line2& other, Point2& result) const {
|
||||
|
||||
const float delta = 0.000001;
|
||||
|
||||
const float bx = p2.x - p1.x;
|
||||
const float by = p2.y - p1.y;
|
||||
|
||||
@@ -63,49 +92,24 @@ public:
|
||||
|
||||
const float b_dot_d_perp = bx*dy - by*dx;
|
||||
|
||||
if (b_dot_d_perp == 0) {return false;}
|
||||
if(b_dot_d_perp == 0) {return false;}
|
||||
|
||||
const float cx = other.p1.x - p1.x;
|
||||
const float cy = other.p1.y - p1.y;
|
||||
|
||||
const float t = (cx * dy - cy * dx) / b_dot_d_perp;
|
||||
if(t < 0 || t > 1) {return false;}
|
||||
if(t < 0+delta || t > 1-delta) {return false;}
|
||||
|
||||
const float u = (cx * by - cy * bx) / b_dot_d_perp;
|
||||
if(u < 0 || u > 1) {return false;}
|
||||
if(u < 0+delta || u > 1-delta) {return false;}
|
||||
|
||||
result.x = p1.x + t * bx;
|
||||
result.y = p1.y + t * by;
|
||||
|
||||
return true;
|
||||
|
||||
}
|
||||
|
||||
bool getSegmentIntersection(const Line2& other, Point2& result) const {
|
||||
|
||||
const double bx = p2.x - p1.x;
|
||||
const double by = p2.y - p1.y;
|
||||
|
||||
const double dx = other.p2.x - other.p1.x;
|
||||
const double dy = other.p2.y - other.p1.y;
|
||||
|
||||
const double b_dot_d_perp = bx*dy - by*dx;
|
||||
|
||||
if(b_dot_d_perp == 0) {return false;}
|
||||
|
||||
const double cx = other.p1.x - p1.x;
|
||||
const double cy = other.p1.y - p1.y;
|
||||
|
||||
const double t = (cx * dy - cy * dx) / b_dot_d_perp;
|
||||
if(t < 0 || t > 1) {return false;}
|
||||
|
||||
const double u = (cx * by - cy * bx) / b_dot_d_perp;
|
||||
if(u < 0 || u > 1) {return false;}
|
||||
|
||||
result.x = p1.x + t * bx;
|
||||
result.y = p1.y + t * by;
|
||||
|
||||
return true;
|
||||
|
||||
}
|
||||
|
||||
};
|
||||
|
||||
#endif // LINE2D_H
|
||||
|
||||
@@ -38,10 +38,14 @@ struct Point2 {
|
||||
|
||||
bool operator == (const Point2& o) const {return x==o.x && y==o.y;}
|
||||
|
||||
bool operator != (const Point2& o) const {return x!=o.x || y!=o.y;}
|
||||
bool operator != (const Point2& o) const {return x!=o.x || y!=o.y;}
|
||||
|
||||
|
||||
Point2 perpendicular() const {return Point2(-y, x);}
|
||||
Point2 perpendicular() const {return Point2(-y, x);}
|
||||
|
||||
float length() const {return std::sqrt(x*x + y*y);}
|
||||
|
||||
Point2 normalized() const {return (*this) / length();}
|
||||
|
||||
/** get the distance between this point and the other one */
|
||||
float getDistance(const Point2& o) const {
|
||||
|
||||
33
geo/Point3.h
33
geo/Point3.h
@@ -21,6 +21,8 @@ struct Point3 {
|
||||
Point3(const float x, const float y, const float z) : x(x), y(y), z(z) {;}
|
||||
|
||||
|
||||
Point3 operator - () const {return Point3(-x, -y, -z);}
|
||||
|
||||
|
||||
Point3 operator + (const Point3& o) const {return Point3(x+o.x, y+o.y, z+o.z);}
|
||||
|
||||
@@ -37,19 +39,43 @@ struct Point3 {
|
||||
|
||||
Point3& operator /= (const float v) {x/=v; y/=v; z/=v; return *this;}
|
||||
|
||||
|
||||
Point3& operator += (const Point3& o) {x+=o.x; y+=o.y; z+=o.z; return *this;}
|
||||
|
||||
Point3& operator -= (const Point3& o) {x-=o.x; y-=o.y; z-=o.z; return *this;}
|
||||
|
||||
Point3& operator *= (const Point3& o) {x*=o.x; y*=o.y; z*=o.z; return *this;}
|
||||
|
||||
Point3& operator /= (const Point3& o) {x/=o.x; y/=o.y; z/=o.z; return *this;}
|
||||
|
||||
|
||||
bool operator < (const Point3& o) const {return x<o.x && y<o.y && z<o.z;}
|
||||
|
||||
bool operator == (const Point3& o) const {return x==o.x && y==o.y && z==o.z;}
|
||||
|
||||
bool operator != (const Point3& o) const {return x!=o.x || y!=o.y || z!=o.z;}
|
||||
bool operator != (const Point3& o) const {return x!=o.x || y!=o.y || z!=o.z;}
|
||||
|
||||
bool eq (const Point3& o, const float delta) const { return eq(x,o.x,delta) && eq(y,o.y,delta) && eq(z,o.z,delta); }
|
||||
|
||||
|
||||
Point2 xy() const {return Point2(x,y);}
|
||||
|
||||
|
||||
Point3 rotX(const float r) const {
|
||||
return Point3(x, y*cos(r) - z*sin(r), y*sin(r) + z*cos(r));
|
||||
}
|
||||
Point3 rotY(const float r) const {
|
||||
return Point3(z*sin(r) + x*cos(r), y, z*cos(r) - x*sin(r));
|
||||
}
|
||||
Point3 rotZ(const float r) const {
|
||||
return Point3(x*cos(r) - y*sin(r), x*sin(r) + y*cos(r), z);
|
||||
}
|
||||
Point3 rot(const float rx, const float ry, const float rz) const {
|
||||
return rotX(rx).rotY(ry).rotZ(rz);
|
||||
//return rotZ(rz).rotY(ry).rotX(rx);
|
||||
}
|
||||
|
||||
|
||||
/** read-only array access */
|
||||
float operator [] (const int idx) const {
|
||||
Assert::isBetween(idx, 0, 2, "index out of bounds");
|
||||
@@ -76,6 +102,11 @@ struct Point3 {
|
||||
), 1.0f/norm);
|
||||
}
|
||||
|
||||
private:
|
||||
|
||||
static inline bool eq(const float a, const float b, const float delta) {return std::abs(a-b) <= delta;}
|
||||
static inline bool ne(const float a, const float b, const float delta) {return std::abs(a-b) > delta;}
|
||||
|
||||
};
|
||||
|
||||
#endif // POINT3_H
|
||||
|
||||
Reference in New Issue
Block a user