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 |
|
---|
12 | namespace GtpVisibilityPreprocessor {
|
---|
13 |
|
---|
14 | class Polygon3;
|
---|
15 | class Plane3;
|
---|
16 | struct Face;
|
---|
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 | ~Polygon3() {DEL_PTR(mPlane);}
|
---|
41 |
|
---|
42 | /** Copies all the vertices of the face.
|
---|
43 | */
|
---|
44 | Polygon3(Face *face, Mesh *parentMesh);
|
---|
45 |
|
---|
46 | /** Returns supporting plane of this polygon.
|
---|
47 | */
|
---|
48 | Plane3 GetSupportingPlane(); //const;
|
---|
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
|
---|
54 | @param epsilon epsilon where two points are considered equal
|
---|
55 | */
|
---|
56 | void Split(const Plane3 &partition,
|
---|
57 | Polygon3 &front,
|
---|
58 | Polygon3 &back,
|
---|
59 | const float epsilon);// = Limits::Small);
|
---|
60 |
|
---|
61 | /** Returns the area of this polygon.
|
---|
62 | */
|
---|
63 | float GetArea() const;
|
---|
64 |
|
---|
65 | /** Classify polygon with respect to the plane.
|
---|
66 | @param epsilon tolerance value
|
---|
67 | @returns one of BACK_SIDE, FRONT_SIDE, SPLIT, COINCIDENT
|
---|
68 |
|
---|
69 | */
|
---|
70 | int ClassifyPlane(const Plane3 &plane, const float epsilon) const;
|
---|
71 |
|
---|
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 | */
|
---|
75 | int Side(const Plane3 &plane, const float epsilon) const;
|
---|
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;
|
---|
84 |
|
---|
85 | /** Checks if the polygon is valid, i.e., not degenerated.
|
---|
86 | @returns true if polygon is valid.
|
---|
87 | */
|
---|
88 | bool Valid(const float epsilon) const;
|
---|
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 |
|
---|
98 | /** The polygon is converted to triangles.
|
---|
99 | */
|
---|
100 | void Triangulate(vector<Triangle3> &triangles) const;
|
---|
101 |
|
---|
102 | /**
|
---|
103 | Triangle strip indices are created from this polgon.
|
---|
104 | */
|
---|
105 | void Triangulate(VertexIndexContainer &indices) const;
|
---|
106 |
|
---|
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
|
---|
110 | */
|
---|
111 | void InheritRays(Polygon3 &front_piece,
|
---|
112 | Polygon3 &back_piece) const;
|
---|
113 |
|
---|
114 | /** Returns new polygon with reverse orientation.
|
---|
115 | */
|
---|
116 | Polygon3 *CreateReversePolygon() const;
|
---|
117 |
|
---|
118 | AxisAlignedBox3 GetBoundingBox() const;
|
---|
119 |
|
---|
120 | ////////////////////////////////////////////
|
---|
121 |
|
---|
122 | /** Classify polygons with respect to the plane.
|
---|
123 | @returns one of BACK_SIDE, FRONT_SIDE, SPLIT, COINCIDENT
|
---|
124 | */
|
---|
125 | static int ClassifyPlane(const PolygonContainer &polys,
|
---|
126 | const Plane3 &plane,
|
---|
127 | const float epsilon);
|
---|
128 |
|
---|
129 | /** Counts the number of different intersectables associated with the polygons.
|
---|
130 | */
|
---|
131 | static int ParentObjectsSize(const PolygonContainer &polys);
|
---|
132 |
|
---|
133 | /** Area of the accumulated polygons.
|
---|
134 | */
|
---|
135 | static float GetArea(const PolygonContainer &cell);
|
---|
136 |
|
---|
137 | //////////////////////////////////////////////////////
|
---|
138 |
|
---|
139 | /**
|
---|
140 | Adds polygon to mesh description as a face
|
---|
141 | */
|
---|
142 | friend void IncludePolyInMesh(const Polygon3 &poly, Mesh &mesh);
|
---|
143 |
|
---|
144 |
|
---|
145 | //////////////////////////////////////////
|
---|
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 |
|
---|
159 | protected:
|
---|
160 |
|
---|
161 | Plane3 *mPlane;
|
---|
162 | };
|
---|
163 |
|
---|
164 |
|
---|
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 |
|
---|
178 | }
|
---|
179 |
|
---|
180 | #endif
|
---|