[774] | 1 | #ifndef GFXGEOM_3D_INCLUDED // -*- C++ -*-
|
---|
| 2 | #define GFXGEOM_3D_INCLUDED
|
---|
| 3 |
|
---|
| 4 | #include <gfx/math/Vec3.h>
|
---|
| 5 | #include <gfx/math/Vec4.h>
|
---|
| 6 | #include <gfx/tools/Array.h>
|
---|
| 7 |
|
---|
| 8 | //
|
---|
| 9 | // Generally useful geometric functions
|
---|
| 10 | //
|
---|
| 11 |
|
---|
[1025] | 12 | namespace simplif
|
---|
[774] | 13 | {
|
---|
| 14 | extern Vec3 randomPoint(const Vec3&, const Vec3&); // on segment
|
---|
| 15 | extern Vec3 randomPoint(const Vec3&, const Vec3&, const Vec3&); // in triangle
|
---|
| 16 |
|
---|
| 17 | extern real triangleArea(const Vec3&, const Vec3&, const Vec3&);
|
---|
| 18 | extern real triangleCompactness(const Vec3&, const Vec3&, const Vec3&);
|
---|
| 19 |
|
---|
| 20 | class Bounds
|
---|
| 21 | {
|
---|
| 22 | public:
|
---|
| 23 |
|
---|
| 24 | Vec3 min, max;
|
---|
| 25 | Vec3 center;
|
---|
| 26 | real radius;
|
---|
| 27 | unsigned int points;
|
---|
| 28 |
|
---|
| 29 | Bounds() { reset(); }
|
---|
| 30 |
|
---|
| 31 | void reset();
|
---|
| 32 | void addPoint(const Vec3&);
|
---|
| 33 | void complete();
|
---|
| 34 | };
|
---|
| 35 |
|
---|
| 36 | class Plane
|
---|
| 37 | {
|
---|
| 38 | //
|
---|
| 39 | // A plane is defined by the equation: n*p + d = 0
|
---|
| 40 | Vec3 n;
|
---|
| 41 | real d;
|
---|
| 42 |
|
---|
| 43 | public:
|
---|
| 44 |
|
---|
| 45 | Plane() : n(0,0,1) { d=0; } // -- this will define the XY plane
|
---|
| 46 | Plane(const Vec3& p, const Vec3& q, const Vec3& r) { calcFrom(p,q,r); }
|
---|
| 47 | Plane(const array<Vec3>& verts) { calcFrom(verts); }
|
---|
| 48 | Plane(const Plane& p) { n=p.n; d=p.d; }
|
---|
| 49 |
|
---|
| 50 | void calcFrom(const Vec3& p, const Vec3& q, const Vec3& r);
|
---|
| 51 | void calcFrom(const array<Vec3>&);
|
---|
| 52 |
|
---|
| 53 | bool isValid() const { return n[X]!=0.0 || n[Y]!=0.0 || n[Z]!= 0.0; }
|
---|
| 54 | void markInvalid() { n[X] = n[Y] = n[Z] = 0.0; }
|
---|
| 55 |
|
---|
| 56 | real distTo(const Vec3& p) const { return n*p + d; }
|
---|
| 57 | const Vec3& normal() const { return n; }
|
---|
| 58 |
|
---|
| 59 | void coeffs(real *a, real *b, real *c, real *dd) const {
|
---|
| 60 | *a=n[X]; *b=n[Y]; *c=n[Z]; *dd=d;
|
---|
| 61 | }
|
---|
| 62 | Vec4 coeffs() const { return Vec4(n,d); }
|
---|
| 63 | };
|
---|
| 64 |
|
---|
| 65 |
|
---|
| 66 | //
|
---|
| 67 | // A triangular face in 3D (ie. a 2-simplex in E3)
|
---|
| 68 | //
|
---|
| 69 | class Face3
|
---|
| 70 | {
|
---|
| 71 | protected:
|
---|
| 72 | Plane P;
|
---|
| 73 |
|
---|
| 74 | private:
|
---|
| 75 | void recalcPlane() { P.calcFrom(vertexPos(0),vertexPos(1),vertexPos(2)); }
|
---|
| 76 | void recalcPlane(const Vec3& a,const Vec3& b,const Vec3& c)
|
---|
| 77 | { P.calcFrom(a,b,c); }
|
---|
| 78 |
|
---|
| 79 | public:
|
---|
| 80 | Face3(const Vec3& a,const Vec3& b,const Vec3& c)
|
---|
| 81 | : P(a,b,c)
|
---|
| 82 | { }
|
---|
| 83 |
|
---|
| 84 | //
|
---|
| 85 | // Basic primitive operations on faces
|
---|
| 86 | virtual const Vec3& vertexPos(int i) const = 0;
|
---|
| 87 | virtual void vertexPos(int i, const Vec3&) = 0;
|
---|
| 88 | const Plane& plane() { if(!P.isValid()) recalcPlane(); return P;}
|
---|
| 89 | void invalidatePlane() { P.markInvalid(); }
|
---|
| 90 |
|
---|
| 91 | real distTo(const Vec3& p) const;
|
---|
| 92 | real area();
|
---|
| 93 | };
|
---|
| 94 | }
|
---|
| 95 |
|
---|
| 96 |
|
---|
| 97 |
|
---|
| 98 | // GFXGEOM_3D_INCLUDED
|
---|
| 99 | #endif
|
---|