[372] | 1 | #ifndef _Polygon3_h__
|
---|
| 2 | #define _Polygon3_h__
|
---|
| 3 |
|
---|
| 4 |
|
---|
| 5 | //#include <iostream>
|
---|
| 6 | //#include <math.h>
|
---|
| 7 | //#include "common.h"
|
---|
| 8 | #include "Containers.h"
|
---|
| 9 | #include "Mesh.h"
|
---|
| 10 | #include <iomanip>
|
---|
| 11 |
|
---|
[860] | 12 | namespace GtpVisibilityPreprocessor {
|
---|
| 13 |
|
---|
[372] | 14 | class Polygon3;
|
---|
| 15 | class Plane3;
|
---|
[752] | 16 | struct Face;
|
---|
[372] | 17 | class Intersectable;
|
---|
| 18 | class AxisAlignedBox3;
|
---|
| 19 | class Ray;
|
---|
| 20 | //typedef Vertex3 Vector3;
|
---|
| 21 |
|
---|
| 22 | /** Class representing a planar convex polygon in 3d.
|
---|
| 23 | */
|
---|
| 24 | class Polygon3
|
---|
| 25 | {
|
---|
| 26 | public:
|
---|
| 27 | enum {BACK_SIDE, FRONT_SIDE, SPLIT, COINCIDENT};
|
---|
| 28 |
|
---|
| 29 | /** Default constructor creating an empty polygon.
|
---|
| 30 | */
|
---|
| 31 | Polygon3();
|
---|
| 32 | /** Constructor creating a polygon from the vertices.
|
---|
| 33 | */
|
---|
| 34 | Polygon3(const VertexContainer &vertices);
|
---|
| 35 | /** Creates a polygon and stores pointer to parent mesh
|
---|
| 36 | instance.
|
---|
| 37 | */
|
---|
| 38 | Polygon3(MeshInstance *parent);
|
---|
| 39 |
|
---|
| 40 | // creates an "infinite" polygon from this plane
|
---|
| 41 | //Polygon3(Plane3 plane);
|
---|
| 42 | /** Copies all the vertices of the face.
|
---|
| 43 | */
|
---|
| 44 | Polygon3(Face *face, Mesh *parentMesh);
|
---|
| 45 |
|
---|
| 46 | /** Returns supporting plane of this polygon.
|
---|
| 47 | */
|
---|
[1076] | 48 | Plane3 GetSupportingPlane(); //const;
|
---|
[372] | 49 |
|
---|
| 50 | /** Splits polygon.
|
---|
| 51 | @param partition the split plane
|
---|
| 52 | @param front returns the front the front polygon
|
---|
| 53 | @param back returns the back polygon
|
---|
[448] | 54 | @param epsilon epsilon where two points are considered equal
|
---|
[372] | 55 | */
|
---|
| 56 | void Split(const Plane3 &partition,
|
---|
| 57 | Polygon3 &front,
|
---|
[448] | 58 | Polygon3 &back,
|
---|
| 59 | const float epsilon);// = Limits::Small);
|
---|
[372] | 60 |
|
---|
| 61 | /** Returns the area of this polygon.
|
---|
| 62 | */
|
---|
| 63 | float GetArea() const;
|
---|
| 64 |
|
---|
| 65 | /** Classify polygon with respect to the plane.
|
---|
| 66 | @returns one of BACK_SIDE, FRONT_SIDE, SPLIT, COINCIDENT
|
---|
| 67 | */
|
---|
[448] | 68 | //int ClassifyPlane(const Plane3 &plane) const;
|
---|
[404] | 69 |
|
---|
| 70 | /** Classify polygon with respect to the plane.
|
---|
[448] | 71 | @param epsilon tolerance value
|
---|
| 72 | @returns one of BACK_SIDE, FRONT_SIDE, SPLIT, COINCIDENT
|
---|
| 73 |
|
---|
[404] | 74 | */
|
---|
[448] | 75 | int ClassifyPlane(const Plane3 &plane, const float epsilon) const;
|
---|
[404] | 76 |
|
---|
[372] | 77 | /** Side of the polygon with respect to the plane.
|
---|
| 78 | @returns 1 if on front side, -1 if on back side, 0 else.
|
---|
| 79 | */
|
---|
[448] | 80 | int Side(const Plane3 &plane, const float epsilon) const;
|
---|
[372] | 81 |
|
---|
| 82 | /** Scales the polygon about its center
|
---|
| 83 | */
|
---|
| 84 | void Scale(const float scale);
|
---|
| 85 |
|
---|
| 86 | /** Computes the center of mass of the polygon
|
---|
| 87 | */
|
---|
| 88 | Vector3 Center() const;
|
---|
| 89 | /** Checks if the polygon is valid, i.e., not degenerated.
|
---|
| 90 | @returns true if polygon is valid.
|
---|
| 91 | */
|
---|
[448] | 92 | bool Valid(const float epsilon) const;
|
---|
[372] | 93 |
|
---|
| 94 | /** Returns the surface normal.
|
---|
| 95 | */
|
---|
| 96 | Vector3 GetNormal() const;
|
---|
| 97 |
|
---|
| 98 | /** Casts ray to polygon.
|
---|
| 99 | */
|
---|
| 100 | int CastRay(const Ray &ray, float &t, const float nearestT);
|
---|
| 101 |
|
---|
[508] | 102 | /** The polygon is converted to triangles.
|
---|
[503] | 103 | */
|
---|
[840] | 104 | void Triangulate(vector<Triangle3> &triangles) const;
|
---|
| 105 |
|
---|
[508] | 106 | /**
|
---|
| 107 | Triangle strip indices are created from this polgon.
|
---|
| 108 | */
|
---|
[840] | 109 | void Triangulate(VertexIndexContainer &indices) const;
|
---|
[508] | 110 |
|
---|
[384] | 111 | /** The piercing rays of the polygon are inherited by the child fragments
|
---|
| 112 | @parm front_piece the front fragment inheriting the front rays
|
---|
| 113 | @param back_piece the back fragment inheriting the back rays
|
---|
[372] | 114 | */
|
---|
[384] | 115 | void InheritRays(Polygon3 &front_piece,
|
---|
| 116 | Polygon3 &back_piece) const;
|
---|
[372] | 117 |
|
---|
[396] | 118 | /** Returns new polygon with reverse orientation.
|
---|
| 119 | */
|
---|
| 120 | Polygon3 *CreateReversePolygon() const;
|
---|
[384] | 121 |
|
---|
[372] | 122 |
|
---|
[384] | 123 | /** Classify polygons with respect to the plane.
|
---|
| 124 | @returns one of BACK_SIDE, FRONT_SIDE, SPLIT, COINCIDENT
|
---|
| 125 | */
|
---|
[448] | 126 | static int ClassifyPlane(const PolygonContainer &polys,
|
---|
| 127 | const Plane3 &plane,
|
---|
| 128 | const float epsilon);
|
---|
[372] | 129 |
|
---|
[384] | 130 |
|
---|
[372] | 131 | /** Counts the number of different intersectables associated with the polygons.
|
---|
| 132 | */
|
---|
[375] | 133 | static int ParentObjectsSize(const PolygonContainer &polys);
|
---|
[384] | 134 |
|
---|
[574] | 135 | /** Area of the accumulated polygons.
|
---|
| 136 | */
|
---|
[384] | 137 | static float GetArea(const PolygonContainer &cell);
|
---|
[840] | 138 |
|
---|
| 139 | /**
|
---|
| 140 | Adds polygon to mesh description as a face
|
---|
| 141 | */
|
---|
| 142 | friend void IncludePolyInMesh(const Polygon3 &poly, Mesh &mesh);
|
---|
[860] | 143 |
|
---|
| 144 |
|
---|
| 145 |
|
---|
| 146 | /// vertices are connected in counterclockwise order.
|
---|
| 147 | VertexContainer mVertices;
|
---|
| 148 |
|
---|
| 149 | /// we can also store materials with polygons
|
---|
| 150 | Material *mMaterial;
|
---|
| 151 |
|
---|
| 152 | /// pointer to the mesh instance this polygon is derived from
|
---|
| 153 | MeshInstance *mParent;
|
---|
| 154 |
|
---|
| 155 | /// Rays piercing this polygon
|
---|
| 156 | RayContainer mPiercingRays;
|
---|
| 157 |
|
---|
[1076] | 158 | ~Polygon3() {DEL_PTR(mPlane);}
|
---|
| 159 | Plane3 *mPlane;
|
---|
| 160 |
|
---|
[372] | 161 | };
|
---|
| 162 |
|
---|
| 163 | // Overload << operator for C++-style output
|
---|
| 164 | inline ostream&
|
---|
| 165 | operator<< (ostream &s, const Polygon3 &A)
|
---|
| 166 | {
|
---|
| 167 | VertexContainer::const_iterator it;
|
---|
| 168 |
|
---|
| 169 | //s << setprecision(6) << "Polygon:\n";
|
---|
| 170 | for (it = A.mVertices.begin(); it != A.mVertices.end(); ++it)
|
---|
| 171 | s << *it << endl;
|
---|
| 172 |
|
---|
| 173 | return s;
|
---|
| 174 | }
|
---|
| 175 |
|
---|
[860] | 176 | }
|
---|
[372] | 177 |
|
---|
| 178 | #endif
|
---|