[235] | 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" |
---|
[236] | 9 | #include "Mesh.h" |
---|
[270] | 10 | #include <iomanip> |
---|
[235] | 11 | |
---|
[236] | 12 | class Polygon3; |
---|
[235] | 13 | class Plane3; |
---|
| 14 | class Face; |
---|
[286] | 15 | class Intersectable; |
---|
[295] | 16 | class AxisAlignedBox3; |
---|
[314] | 17 | class Ray; |
---|
[289] | 18 | //typedef Vertex3 Vector3; |
---|
| 19 | |
---|
| 20 | /** Class representing a planar convex polygon in 3d. |
---|
[235] | 21 | */ |
---|
| 22 | class Polygon3 |
---|
| 23 | { |
---|
| 24 | public: |
---|
[349] | 25 | enum {BACK_SIDE, FRONT_SIDE, SPLIT, COINCIDENT}; |
---|
| 26 | |
---|
| 27 | /** Default constructor creating an empty polygon. |
---|
| 28 | */ |
---|
[235] | 29 | Polygon3(); |
---|
[349] | 30 | /** Constructor creating a polygon from the vertices. |
---|
| 31 | */ |
---|
[236] | 32 | Polygon3(const VertexContainer &vertices); |
---|
[349] | 33 | /** Creates a polygon and stores pointer to parent mesh |
---|
| 34 | instance. |
---|
| 35 | */ |
---|
[308] | 36 | Polygon3(MeshInstance *parent); |
---|
[333] | 37 | |
---|
[317] | 38 | // creates an "infinite" polygon from this plane |
---|
| 39 | //Polygon3(Plane3 plane); |
---|
[235] | 40 | /** Copies all the vertices of the face. |
---|
| 41 | */ |
---|
[286] | 42 | Polygon3(Face *face, Mesh *parentMesh); |
---|
[235] | 43 | |
---|
| 44 | /** Returns supporting plane of this polygon. |
---|
| 45 | */ |
---|
[238] | 46 | Plane3 GetSupportingPlane() const; |
---|
[235] | 47 | |
---|
| 48 | /** Splits polygon. |
---|
| 49 | @param partition the split plane |
---|
[312] | 50 | @param front returns the front the front polygon |
---|
| 51 | @param back returns the back polygon |
---|
| 52 | @param splitPts returns the split points |
---|
[235] | 53 | */ |
---|
[327] | 54 | void Split(const Plane3 &partition, |
---|
| 55 | Polygon3 &front, |
---|
| 56 | Polygon3 &back, |
---|
| 57 | VertexContainer &splitPts); |
---|
[235] | 58 | |
---|
[298] | 59 | /** Returns the area of this polygon. |
---|
| 60 | */ |
---|
| 61 | float GetArea() const; |
---|
| 62 | |
---|
[238] | 63 | /** Classify polygon with respect to the plane. |
---|
[235] | 64 | @returns one of BACK_SIDE, FRONT_SIDE, SPLIT, COINCIDENT |
---|
| 65 | */ |
---|
[238] | 66 | int ClassifyPlane(const Plane3 &plane) const; |
---|
[235] | 67 | |
---|
[238] | 68 | /** Side of the polygon with respect to the plane. |
---|
| 69 | @returns 1 if on front side, -1 if on back side, 0 else. |
---|
| 70 | */ |
---|
| 71 | int Side(const Plane3 &plane) const; |
---|
| 72 | |
---|
[256] | 73 | /** Scales the polygon about its center |
---|
| 74 | */ |
---|
| 75 | void Scale(const float scale); |
---|
| 76 | |
---|
| 77 | /** Computes the center of mass of the polygon |
---|
| 78 | */ |
---|
| 79 | Vector3 Center() const; |
---|
[289] | 80 | /** Checks if the polygon is valid, i.e., not degenerated. |
---|
| 81 | @returns true if polygon is valid. |
---|
| 82 | */ |
---|
[299] | 83 | bool Valid() const; |
---|
[256] | 84 | |
---|
[312] | 85 | /** Returns the surface normal. |
---|
| 86 | */ |
---|
[333] | 87 | Vector3 GetNormal() const; |
---|
[306] | 88 | |
---|
[295] | 89 | /** Includes polygons to axis aligned box. |
---|
| 90 | */ |
---|
| 91 | static void IncludeInBox(const PolygonContainer &polys, AxisAlignedBox3 &box); |
---|
| 92 | |
---|
[314] | 93 | /** Casts ray to polygon. |
---|
| 94 | */ |
---|
| 95 | int CastRay(const Ray &ray, float &t, const float nearestT); |
---|
| 96 | |
---|
[360] | 97 | /** Classify polygons with respect to the plane. |
---|
| 98 | @returns one of BACK_SIDE, FRONT_SIDE, SPLIT, COINCIDENT |
---|
| 99 | */ |
---|
| 100 | static int ClassifyPlane(const PolygonContainer &polys, const Plane3 &plane); |
---|
| 101 | |
---|
[235] | 102 | /// vertices are connected in counterclockwise order. |
---|
| 103 | VertexContainer mVertices; |
---|
[242] | 104 | |
---|
| 105 | /// we can also store materials with polygons |
---|
| 106 | Material *mMaterial; |
---|
[286] | 107 | |
---|
[308] | 108 | /// pointer to the mesh instance this polygon is derived from |
---|
| 109 | MeshInstance *mParent; |
---|
[319] | 110 | |
---|
| 111 | /// Rays piercing this polygon |
---|
[349] | 112 | RayContainer mPiercingRays; |
---|
[329] | 113 | |
---|
[349] | 114 | /** The piercing rays of the polygon are inherited by the child fragments |
---|
| 115 | @parm front_piece the front fragment inheriting the front rays |
---|
| 116 | @param back_piece the back fragment inheriting the back rays |
---|
| 117 | */ |
---|
| 118 | void InheritRays(Polygon3 &front_piece, |
---|
| 119 | Polygon3 &back_piece) const; |
---|
[362] | 120 | |
---|
| 121 | |
---|
| 122 | /** Counts the number of different intersectables associated with the polygons. |
---|
| 123 | */ |
---|
| 124 | static int CountIntersectables(const PolygonContainer &polys); |
---|
[235] | 125 | }; |
---|
| 126 | |
---|
[237] | 127 | // Overload << operator for C++-style output |
---|
| 128 | inline ostream& |
---|
| 129 | operator<< (ostream &s, const Polygon3 &A) |
---|
| 130 | { |
---|
| 131 | VertexContainer::const_iterator it; |
---|
[235] | 132 | |
---|
[289] | 133 | //s << setprecision(6) << "Polygon:\n"; |
---|
[237] | 134 | for (it = A.mVertices.begin(); it != A.mVertices.end(); ++it) |
---|
| 135 | s << *it << endl; |
---|
| 136 | |
---|
| 137 | return s; |
---|
| 138 | } |
---|
[235] | 139 | |
---|
[237] | 140 | |
---|
[235] | 141 | #endif |
---|