[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 |
|
---|
[1328] | 40 | ~Polygon3() {DEL_PTR(mPlane);}
|
---|
| 41 |
|
---|
[372] | 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;
|
---|
[404] | 64 |
|
---|
| 65 | /** Classify polygon with respect to the plane.
|
---|
[448] | 66 | @param epsilon tolerance value
|
---|
| 67 | @returns one of BACK_SIDE, FRONT_SIDE, SPLIT, COINCIDENT
|
---|
| 68 |
|
---|
[404] | 69 | */
|
---|
[448] | 70 | int ClassifyPlane(const Plane3 &plane, const float epsilon) const;
|
---|
[404] | 71 |
|
---|
[372] | 72 | /** Side of the polygon with respect to the plane.
|
---|
| 73 | @returns 1 if on front side, -1 if on back side, 0 else.
|
---|
| 74 | */
|
---|
[448] | 75 | int Side(const Plane3 &plane, const float epsilon) const;
|
---|
[372] | 76 |
|
---|
| 77 | /** Scales the polygon about its center
|
---|
| 78 | */
|
---|
| 79 | void Scale(const float scale);
|
---|
| 80 |
|
---|
| 81 | /** Computes the center of mass of the polygon
|
---|
| 82 | */
|
---|
| 83 | Vector3 Center() const;
|
---|
[1328] | 84 |
|
---|
[372] | 85 | /** Checks if the polygon is valid, i.e., not degenerated.
|
---|
| 86 | @returns true if polygon is valid.
|
---|
| 87 | */
|
---|
[448] | 88 | bool Valid(const float epsilon) const;
|
---|
[372] | 89 |
|
---|
| 90 | /** Returns the surface normal.
|
---|
| 91 | */
|
---|
| 92 | Vector3 GetNormal() const;
|
---|
| 93 |
|
---|
| 94 | /** Casts ray to polygon.
|
---|
| 95 | */
|
---|
| 96 | int CastRay(const Ray &ray, float &t, const float nearestT);
|
---|
| 97 |
|
---|
[508] | 98 | /** The polygon is converted to triangles.
|
---|
[503] | 99 | */
|
---|
[840] | 100 | void Triangulate(vector<Triangle3> &triangles) const;
|
---|
| 101 |
|
---|
[508] | 102 | /**
|
---|
| 103 | Triangle strip indices are created from this polgon.
|
---|
| 104 | */
|
---|
[840] | 105 | void Triangulate(VertexIndexContainer &indices) const;
|
---|
[508] | 106 |
|
---|
[384] | 107 | /** The piercing rays of the polygon are inherited by the child fragments
|
---|
| 108 | @parm front_piece the front fragment inheriting the front rays
|
---|
| 109 | @param back_piece the back fragment inheriting the back rays
|
---|
[372] | 110 | */
|
---|
[384] | 111 | void InheritRays(Polygon3 &front_piece,
|
---|
| 112 | Polygon3 &back_piece) const;
|
---|
[372] | 113 |
|
---|
[396] | 114 | /** Returns new polygon with reverse orientation.
|
---|
| 115 | */
|
---|
| 116 | Polygon3 *CreateReversePolygon() const;
|
---|
[384] | 117 |
|
---|
[1328] | 118 | AxisAlignedBox3 GetBoundingBox() const;
|
---|
[372] | 119 |
|
---|
[1328] | 120 | ////////////////////////////////////////////
|
---|
| 121 |
|
---|
[384] | 122 | /** Classify polygons with respect to the plane.
|
---|
| 123 | @returns one of BACK_SIDE, FRONT_SIDE, SPLIT, COINCIDENT
|
---|
| 124 | */
|
---|
[448] | 125 | static int ClassifyPlane(const PolygonContainer &polys,
|
---|
| 126 | const Plane3 &plane,
|
---|
| 127 | const float epsilon);
|
---|
[372] | 128 |
|
---|
| 129 | /** Counts the number of different intersectables associated with the polygons.
|
---|
| 130 | */
|
---|
[375] | 131 | static int ParentObjectsSize(const PolygonContainer &polys);
|
---|
[384] | 132 |
|
---|
[574] | 133 | /** Area of the accumulated polygons.
|
---|
| 134 | */
|
---|
[384] | 135 | static float GetArea(const PolygonContainer &cell);
|
---|
[840] | 136 |
|
---|
[1328] | 137 | //////////////////////////////////////////////////////
|
---|
| 138 |
|
---|
[840] | 139 | /**
|
---|
| 140 | Adds polygon to mesh description as a face
|
---|
| 141 | */
|
---|
| 142 | friend void IncludePolyInMesh(const Polygon3 &poly, Mesh &mesh);
|
---|
[860] | 143 |
|
---|
| 144 |
|
---|
[1328] | 145 | //////////////////////////////////////////
|
---|
[860] | 146 |
|
---|
| 147 | /// vertices are connected in counterclockwise order.
|
---|
| 148 | VertexContainer mVertices;
|
---|
| 149 |
|
---|
| 150 | /// we can also store materials with polygons
|
---|
| 151 | Material *mMaterial;
|
---|
| 152 |
|
---|
| 153 | /// pointer to the mesh instance this polygon is derived from
|
---|
| 154 | MeshInstance *mParent;
|
---|
| 155 |
|
---|
| 156 | /// Rays piercing this polygon
|
---|
| 157 | RayContainer mPiercingRays;
|
---|
| 158 |
|
---|
[1328] | 159 | protected:
|
---|
| 160 |
|
---|
[1076] | 161 | Plane3 *mPlane;
|
---|
[372] | 162 | };
|
---|
| 163 |
|
---|
[1328] | 164 |
|
---|
[372] | 165 | // Overload << operator for C++-style output
|
---|
| 166 | inline ostream&
|
---|
| 167 | operator<< (ostream &s, const Polygon3 &A)
|
---|
| 168 | {
|
---|
| 169 | VertexContainer::const_iterator it;
|
---|
| 170 |
|
---|
| 171 | //s << setprecision(6) << "Polygon:\n";
|
---|
| 172 | for (it = A.mVertices.begin(); it != A.mVertices.end(); ++it)
|
---|
| 173 | s << *it << endl;
|
---|
| 174 |
|
---|
| 175 | return s;
|
---|
| 176 | }
|
---|
| 177 |
|
---|
[860] | 178 | }
|
---|
[372] | 179 |
|
---|
| 180 | #endif
|
---|