[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: |
---|
| 25 | Polygon3(); |
---|
[333] | 26 | |
---|
[236] | 27 | Polygon3(const VertexContainer &vertices); |
---|
[308] | 28 | Polygon3(MeshInstance *parent); |
---|
[333] | 29 | |
---|
| 30 | ~Polygon3(); |
---|
| 31 | |
---|
[317] | 32 | // creates an "infinite" polygon from this plane |
---|
| 33 | //Polygon3(Plane3 plane); |
---|
[235] | 34 | /** Copies all the vertices of the face. |
---|
| 35 | */ |
---|
[286] | 36 | Polygon3(Face *face, Mesh *parentMesh); |
---|
[235] | 37 | |
---|
| 38 | /** Returns supporting plane of this polygon. |
---|
| 39 | */ |
---|
[238] | 40 | Plane3 GetSupportingPlane() const; |
---|
[235] | 41 | |
---|
| 42 | /** Splits polygon. |
---|
| 43 | @param partition the split plane |
---|
[312] | 44 | @param front returns the front the front polygon |
---|
| 45 | @param back returns the back polygon |
---|
| 46 | @param splitPts returns the split points |
---|
[235] | 47 | */ |
---|
[327] | 48 | void Split(const Plane3 &partition, |
---|
| 49 | Polygon3 &front, |
---|
| 50 | Polygon3 &back, |
---|
| 51 | VertexContainer &splitPts); |
---|
[235] | 52 | |
---|
[298] | 53 | /** Returns the area of this polygon. |
---|
| 54 | */ |
---|
| 55 | float GetArea() const; |
---|
| 56 | |
---|
[238] | 57 | /** Classify polygon with respect to the plane. |
---|
[235] | 58 | @returns one of BACK_SIDE, FRONT_SIDE, SPLIT, COINCIDENT |
---|
| 59 | */ |
---|
[238] | 60 | int ClassifyPlane(const Plane3 &plane) const; |
---|
[235] | 61 | |
---|
[238] | 62 | /** Side of the polygon with respect to the plane. |
---|
| 63 | @returns 1 if on front side, -1 if on back side, 0 else. |
---|
| 64 | */ |
---|
| 65 | int Side(const Plane3 &plane) const; |
---|
| 66 | |
---|
[256] | 67 | /** Scales the polygon about its center |
---|
| 68 | */ |
---|
| 69 | void Scale(const float scale); |
---|
| 70 | |
---|
| 71 | /** Computes the center of mass of the polygon |
---|
| 72 | */ |
---|
| 73 | Vector3 Center() const; |
---|
[289] | 74 | /** Checks if the polygon is valid, i.e., not degenerated. |
---|
| 75 | @returns true if polygon is valid. |
---|
| 76 | */ |
---|
[299] | 77 | bool Valid() const; |
---|
[256] | 78 | |
---|
[312] | 79 | /** Returns the surface normal. |
---|
| 80 | */ |
---|
[333] | 81 | Vector3 GetNormal() const; |
---|
[306] | 82 | |
---|
[295] | 83 | /** Includes polygons to axis aligned box. |
---|
| 84 | */ |
---|
| 85 | static void IncludeInBox(const PolygonContainer &polys, AxisAlignedBox3 &box); |
---|
| 86 | |
---|
[314] | 87 | /** Casts ray to polygon. |
---|
| 88 | */ |
---|
| 89 | int CastRay(const Ray &ray, float &t, const float nearestT); |
---|
| 90 | |
---|
[319] | 91 | /** Returns piercing rays container. |
---|
| 92 | */ |
---|
| 93 | RayContainer *GetPiercingRays(); |
---|
| 94 | |
---|
| 95 | /** Adds a ray to the ray container. |
---|
| 96 | */ |
---|
| 97 | void AddPiercingRay(Ray *ray); |
---|
| 98 | |
---|
[235] | 99 | /// vertices are connected in counterclockwise order. |
---|
| 100 | VertexContainer mVertices; |
---|
[242] | 101 | |
---|
| 102 | /// we can also store materials with polygons |
---|
| 103 | Material *mMaterial; |
---|
[286] | 104 | |
---|
[308] | 105 | /// pointer to the mesh instance this polygon is derived from |
---|
| 106 | MeshInstance *mParent; |
---|
[319] | 107 | |
---|
| 108 | /// Rays piercing this polygon |
---|
| 109 | RayContainer *mPiercingRays; |
---|
[329] | 110 | |
---|
| 111 | static float sSideTolerance; |
---|
| 112 | static float sSideToleranceSqrt; |
---|
[235] | 113 | }; |
---|
| 114 | |
---|
[237] | 115 | // Overload << operator for C++-style output |
---|
| 116 | inline ostream& |
---|
| 117 | operator<< (ostream &s, const Polygon3 &A) |
---|
| 118 | { |
---|
| 119 | VertexContainer::const_iterator it; |
---|
[235] | 120 | |
---|
[289] | 121 | //s << setprecision(6) << "Polygon:\n"; |
---|
[237] | 122 | for (it = A.mVertices.begin(); it != A.mVertices.end(); ++it) |
---|
| 123 | s << *it << endl; |
---|
| 124 | |
---|
| 125 | return s; |
---|
| 126 | } |
---|
[235] | 127 | |
---|
[237] | 128 | |
---|
[235] | 129 | #endif |
---|