added support for .obj objects within the floorplan

include objects within navmesh calculation
include objects within 3d mesh generation
minor changes/fixes
This commit is contained in:
2018-02-17 17:20:43 +01:00
parent 42a3a47317
commit 8358f45674
15 changed files with 770 additions and 114 deletions

View File

@@ -9,8 +9,8 @@ class BBox2 {
protected:
static constexpr float MAX = +99999;
static constexpr float MIN = -99999;
static constexpr float MAX = +99999999;
static constexpr float MIN = -99999999;
/** minimum */
Point2 p1;
@@ -26,17 +26,28 @@ public:
/** ctor */
BBox2(const Point2& p1, const Point2& p2) : p1(p1), p2(p2) {;}
/** ctor */
BBox2(const float x1, const float y1, const float x2, const float y2) : p1(x1,y1), p2(x2,y2) {;}
/** adjust the bounding-box by adding this point */
void add(const Point2& p) {
add(p.x, p.y);
}
if (p.x > p2.x) {p2.x = p.x;}
if (p.y > p2.y) {p2.y = p.y;}
/** adjust the bounding-box by adding this point */
void add(const float x, const float y) {
if (p.x < p1.x) {p1.x = p.x;}
if (p.y < p1.y) {p1.y = p.y;}
if (x > p2.x) {p2.x = x;}
if (y > p2.y) {p2.y = y;}
if (x < p1.x) {p1.x = x;}
if (y < p1.y) {p1.y = y;}
}
/** the area spanned by the bbox */
float getArea() const {return getSize().x * getSize().y;}
/** returns true if the bbox is not yet configured */
bool isInvalid() const {
return p1.x == MAX || p1.y == MAX || p2.x == MIN || p2.y == MIN;
@@ -63,6 +74,44 @@ public:
(p2.y == o.p2.y);
}
bool intersects(const BBox2& o) const {
// TODO is this correct?
if (o.p2.x < p1.x) {return false;}
if (o.p1.x > p2.x) {return false;}
if (o.p2.y < p1.y) {return false;}
if (o.p1.y > p2.y) {return false;}
return true;
// return (p1.x <= o.p2.x) &&
// (p1.y <= o.p2.y) &&
// (p2.x >= o.p1.x) &&
// (p2.y >= o.p1.y);
}
BBox2 combine(const BBox2& o) {
// TODO is this correct?
const float x1 = std::min(p1.x, o.p1.x);
const float x2 = std::max(p2.x, o.p2.x);
const float y1 = std::min(p1.y, o.p1.y);
const float y2 = std::max(p2.y, o.p2.y);
return BBox2(x1,y1, x2,y2);
}
BBox2 intersection(const BBox2& o) {
// TODO is this correct?
const float x1 = std::max(p1.x, o.p1.x);
const float x2 = std::min(p2.x, o.p2.x);
const float y1 = std::max(p1.y, o.p1.y);
const float y2 = std::min(p2.y, o.p2.y);
return BBox2(x1,y1, x2,y2);
}
/** does the BBox intersect with the given line? */
bool intersects (const Line2& l) const {
const Line2 l1(p1.x, p1.y, p2.x, p1.y); // upper
@@ -87,10 +136,14 @@ public:
}
bool contains(const Point2& p) const {
if (p.x < p1.x) {return false;}
if (p.x > p2.x) {return false;}
if (p.y < p1.y) {return false;}
if (p.y > p2.y) {return false;}
return contains(p.x, p.y);
}
bool contains(const float x, const float y) const {
if (x < p1.x) {return false;}
if (x > p2.x) {return false;}
if (y < p1.y) {return false;}
if (y > p2.y) {return false;}
return true;
}

64
geo/ConvexHull2.h Normal file
View File

@@ -0,0 +1,64 @@
#ifndef GEO_CONVEXHULL2_H
#define GEO_CONVEXHULL2_H
#include "Point2.h"
#include <algorithm>
#include <vector>
/**
* get a convex-hull around a set of 2D points
* https://en.wikibooks.org/wiki/Algorithm_Implementation/Geometry/Convex_hull/Monotone_chain
*/
class ConvexHull2 {
public:
//using namespace std;
typedef double coord_t; // coordinate type
typedef double coord2_t; // must be big enough to hold 2*max(|coordinate|)^2
// 2D cross product of OA and OB vectors, i.e. z-component of their 3D cross product.
// Returns a positive value, if OAB makes a counter-clockwise turn,
// negative for clockwise turn, and zero if the points are collinear.
static inline coord2_t cross(const Point2 O, const Point2 A, const Point2 B) {
return (A.x - O.x) * (B.y - O.y) - (A.y - O.y) * (B.x - O.x);
}
// Returns a list of points on the convex hull in counter-clockwise order.
// Note: the last point in the returned list is the same as the first one.
static inline std::vector<Point2> get(std::vector<Point2> P) {
auto comp = [] (const Point2 p1, const Point2 p2) {
return p1.x < p2.x || (p1.x == p2.x && p1.y < p2.y);
};
int n = P.size(), k = 0;
if (n == 1) return P;
std::vector<Point2> H(2*n);
// Sort points lexicographically
std::sort(P.begin(), P.end(), comp);
// Build lower hull
for (int i = 0; i < n; ++i) {
while (k >= 2 && cross(H[k-2], H[k-1], P[i]) <= 0) k--;
H[k++] = P[i];
}
// Build upper hull
for (int i = n-2, t = k+1; i >= 0; i--) {
while (k >= t && cross(H[k-2], H[k-1], P[i]) <= 0) k--;
H[k++] = P[i];
}
H.resize(k-1);
return H;
}
};
#endif // GEO_CONVEXHULL2_H

View File

@@ -1,5 +1,5 @@
#ifndef GEO_POINT3_H
#define GEO_POINT3_H
#ifndef GEO_Point3_H
#define GEO_Point3_H
#include "../Assertions.h"
#include <cmath>
@@ -8,76 +8,76 @@
/**
* 3D Point
*/
struct Point3 {
template <typename Scalar> struct _Point3 {
float x;
float y;
float z;
Scalar x;
Scalar y;
Scalar z;
/** ctor */
Point3() : x(0), y(0), z(0) {;}
_Point3() : x(0), y(0), z(0) {;}
/** ctor */
Point3(const float x, const float y, const float z) : x(x), y(y), z(z) {;}
_Point3(const Scalar x, const Scalar y, const Scalar z) : x(x), y(y), z(z) {;}
Point3 operator - () const {return Point3(-x, -y, -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);}
_Point3 operator + (const _Point3& o) const {return _Point3(x+o.x, y+o.y, z+o.z);}
Point3 operator - (const Point3& o) const {return Point3(x-o.x, y-o.y, z-o.z);}
_Point3 operator - (const _Point3& o) const {return _Point3(x-o.x, y-o.y, z-o.z);}
Point3 operator * (const Point3& o) const {return Point3(x*o.x, y*o.y, z*o.z);}
_Point3 operator * (const _Point3& o) const {return _Point3(x*o.x, y*o.y, z*o.z);}
Point3 operator * (const float v) const {return Point3(v*x, v*y, v*z);}
_Point3 operator * (const Scalar v) const {return _Point3(v*x, v*y, v*z);}
Point3 operator / (const float v) const {return Point3(x/v, y/v, z/v);}
_Point3 operator / (const Scalar v) const {return _Point3(x/v, y/v, z/v);}
Point3& operator *= (const float v) {x*=v; y*=v; z*=v; return *this;}
_Point3& operator *= (const Scalar v) {x*=v; y*=v; z*=v; return *this;}
Point3& operator /= (const float v) {x/=v; y/=v; z/=v; return *this;}
_Point3& operator /= (const Scalar 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;}
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 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); }
bool eq (const _Point3& o, const Scalar 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 rotX(const Scalar 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 rotY(const Scalar 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 rotZ(const Scalar 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 {
_Point3 rot(const Scalar rx, const Scalar ry, const Scalar 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 {
Scalar operator [] (const int idx) const {
Assert::isBetween(idx, 0, 2, "index out of bounds");
if (0 == idx) {return x;}
if (1 == idx) {return y;}
@@ -85,19 +85,19 @@ struct Point3 {
}
/** get the distance between this point and the other one */
float getDistance(const Point3& o) const {
const float dx = x - o.x;
const float dy = y - o.y;
const float dz = z - o.z;
Scalar getDistance(const _Point3& o) const {
const Scalar dx = x - o.x;
const Scalar dy = y - o.y;
const Scalar dz = z - o.z;
return std::sqrt(dx*dx + dy*dy + dz*dz);
}
/** get a normalized copy */
Point3 normalized() const {return *this / this->length();}
_Point3 normalized() const {return *this / this->length();}
float length() const {return std::sqrt(x*x + y*y + z*z);}
Scalar length() const {return std::sqrt(x*x + y*y + z*z);}
float length(const float norm) const {
Scalar length(const Scalar norm) const {
return std::pow(
(std::pow(std::abs(x),norm) +
std::pow(std::abs(y),norm) +
@@ -111,12 +111,15 @@ struct Point3 {
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;}
static inline bool eq(const Scalar a, const Scalar b, const Scalar delta) {return std::abs(a-b) <= delta;}
static inline bool ne(const Scalar a, const Scalar b, const Scalar delta) {return std::abs(a-b) > delta;}
};
inline float dot(const Point3 p1, const Point3 p2) {
//using Point3 = _Point3<double>;
using Point3 = _Point3<float>;
inline double dot(const Point3 p1, const Point3 p2) {
return (p1.x*p2.x) + (p1.y*p2.y) + (p1.z*p2.z);
}
@@ -128,4 +131,4 @@ inline Point3 cross(const Point3 a, const Point3 b) {
);
}
#endif // GEO_POINT3_H
#endif // GEO__Point3_H

View File

@@ -34,6 +34,18 @@ public:
Triangle3 operator - (const Point3 o) const {
return Triangle3(p1-o, p2-o, p3-o);
}
Triangle3& operator += (const Point3 o) {
p1 += o;
p2 += o;
p3 += o;
return *this;
}
Triangle3& operator -= (const Point3 o) {
p1 -= o;
p2 -= o;
p3 -= o;
return *this;
}
Point3 getNormal() const {
return cross( p2-p1, p3-p1 ).normalized();
@@ -47,22 +59,22 @@ public:
const Point3 e2 = p3-p1;
const Point3 h = cross(ray.dir, e2);
const float a = dot(e1, h);
const double a = dot(e1, h);
if (a > -0.00001 && a < 0.00001) {return false;}
const float f = 1/a;
const double f = 1/a;
const Point3 s = ray.start - p1;
const float u = f * dot(s,h);
const double u = f * dot(s,h);
if (u < 0.0 || u > 1.0) {return false;}
const Point3 q = cross(s, e1);
const float v = f * dot(ray.dir, q);
const double v = f * dot(ray.dir, q);
if (v < 0.0 || u + v > 1.0) {return false;}
const float t = f * dot(e2,q);
const double t = f * dot(e2,q);
if (t > 0.00001) {