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