[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 |
|
---|
| 12 | class Polygon3;
|
---|
| 13 | class Plane3;
|
---|
[752] | 14 | struct Face;
|
---|
[372] | 15 | class Intersectable;
|
---|
| 16 | class AxisAlignedBox3;
|
---|
| 17 | class Ray;
|
---|
| 18 | //typedef Vertex3 Vector3;
|
---|
| 19 |
|
---|
| 20 | /** Class representing a planar convex polygon in 3d.
|
---|
| 21 | */
|
---|
| 22 | class Polygon3
|
---|
| 23 | {
|
---|
| 24 | public:
|
---|
| 25 | enum {BACK_SIDE, FRONT_SIDE, SPLIT, COINCIDENT};
|
---|
| 26 |
|
---|
| 27 | /** Default constructor creating an empty polygon.
|
---|
| 28 | */
|
---|
| 29 | Polygon3();
|
---|
| 30 | /** Constructor creating a polygon from the vertices.
|
---|
| 31 | */
|
---|
| 32 | Polygon3(const VertexContainer &vertices);
|
---|
| 33 | /** Creates a polygon and stores pointer to parent mesh
|
---|
| 34 | instance.
|
---|
| 35 | */
|
---|
| 36 | Polygon3(MeshInstance *parent);
|
---|
| 37 |
|
---|
| 38 | // creates an "infinite" polygon from this plane
|
---|
| 39 | //Polygon3(Plane3 plane);
|
---|
| 40 | /** Copies all the vertices of the face.
|
---|
| 41 | */
|
---|
| 42 | Polygon3(Face *face, Mesh *parentMesh);
|
---|
| 43 |
|
---|
| 44 | /** Returns supporting plane of this polygon.
|
---|
| 45 | */
|
---|
| 46 | Plane3 GetSupportingPlane() const;
|
---|
| 47 |
|
---|
| 48 | /** Splits polygon.
|
---|
| 49 | @param partition the split plane
|
---|
| 50 | @param front returns the front the front polygon
|
---|
| 51 | @param back returns the back polygon
|
---|
[448] | 52 | @param epsilon epsilon where two points are considered equal
|
---|
[372] | 53 | */
|
---|
| 54 | void Split(const Plane3 &partition,
|
---|
| 55 | Polygon3 &front,
|
---|
[448] | 56 | Polygon3 &back,
|
---|
| 57 | const float epsilon);// = Limits::Small);
|
---|
[372] | 58 |
|
---|
| 59 | /** Returns the area of this polygon.
|
---|
| 60 | */
|
---|
| 61 | float GetArea() const;
|
---|
| 62 |
|
---|
| 63 | /** Classify polygon with respect to the plane.
|
---|
| 64 | @returns one of BACK_SIDE, FRONT_SIDE, SPLIT, COINCIDENT
|
---|
| 65 | */
|
---|
[448] | 66 | //int ClassifyPlane(const Plane3 &plane) const;
|
---|
[404] | 67 |
|
---|
| 68 | /** Classify polygon with respect to the plane.
|
---|
[448] | 69 | @param epsilon tolerance value
|
---|
| 70 | @returns one of BACK_SIDE, FRONT_SIDE, SPLIT, COINCIDENT
|
---|
| 71 |
|
---|
[404] | 72 | */
|
---|
[448] | 73 | int ClassifyPlane(const Plane3 &plane, const float epsilon) const;
|
---|
[404] | 74 |
|
---|
[372] | 75 | /** Side of the polygon with respect to the plane.
|
---|
| 76 | @returns 1 if on front side, -1 if on back side, 0 else.
|
---|
| 77 | */
|
---|
[448] | 78 | int Side(const Plane3 &plane, const float epsilon) const;
|
---|
[372] | 79 |
|
---|
| 80 | /** Scales the polygon about its center
|
---|
| 81 | */
|
---|
| 82 | void Scale(const float scale);
|
---|
| 83 |
|
---|
| 84 | /** Computes the center of mass of the polygon
|
---|
| 85 | */
|
---|
| 86 | Vector3 Center() const;
|
---|
| 87 | /** Checks if the polygon is valid, i.e., not degenerated.
|
---|
| 88 | @returns true if polygon is valid.
|
---|
| 89 | */
|
---|
[448] | 90 | bool Valid(const float epsilon) const;
|
---|
[372] | 91 |
|
---|
| 92 | /** Returns the surface normal.
|
---|
| 93 | */
|
---|
| 94 | Vector3 GetNormal() const;
|
---|
| 95 |
|
---|
| 96 | /** Casts ray to polygon.
|
---|
| 97 | */
|
---|
| 98 | int CastRay(const Ray &ray, float &t, const float nearestT);
|
---|
| 99 |
|
---|
[508] | 100 | /** The polygon is converted to triangles.
|
---|
[503] | 101 | */
|
---|
| 102 | void Triangulate(vector<Triangle3> &triangles);
|
---|
[508] | 103 | /**
|
---|
[750] | 104 | Adds polygon to mesh description as a face
|
---|
[508] | 105 | */
|
---|
| 106 | void AddToMesh(Mesh &mesh);
|
---|
| 107 | /**
|
---|
| 108 | Triangle strip indices are created from this polgon.
|
---|
| 109 | */
|
---|
| 110 | void Triangulate(VertexIndexContainer &indices);
|
---|
| 111 |
|
---|
[384] | 112 | /** The piercing rays of the polygon are inherited by the child fragments
|
---|
| 113 | @parm front_piece the front fragment inheriting the front rays
|
---|
| 114 | @param back_piece the back fragment inheriting the back rays
|
---|
[372] | 115 | */
|
---|
[384] | 116 | void InheritRays(Polygon3 &front_piece,
|
---|
| 117 | Polygon3 &back_piece) const;
|
---|
[372] | 118 |
|
---|
[396] | 119 | /** Returns new polygon with reverse orientation.
|
---|
| 120 | */
|
---|
| 121 | Polygon3 *CreateReversePolygon() const;
|
---|
[384] | 122 |
|
---|
[372] | 123 | /// vertices are connected in counterclockwise order.
|
---|
| 124 | VertexContainer mVertices;
|
---|
| 125 |
|
---|
| 126 | /// we can also store materials with polygons
|
---|
| 127 | Material *mMaterial;
|
---|
| 128 |
|
---|
| 129 | /// pointer to the mesh instance this polygon is derived from
|
---|
| 130 | MeshInstance *mParent;
|
---|
| 131 |
|
---|
| 132 | /// Rays piercing this polygon
|
---|
| 133 | RayContainer mPiercingRays;
|
---|
| 134 |
|
---|
[384] | 135 |
|
---|
| 136 | /** Includes polygons to axis aligned box.
|
---|
[372] | 137 | */
|
---|
[384] | 138 | static void IncludeInBox(const PolygonContainer &polys, AxisAlignedBox3 &box);
|
---|
[372] | 139 |
|
---|
[384] | 140 | /** Classify polygons with respect to the plane.
|
---|
| 141 | @returns one of BACK_SIDE, FRONT_SIDE, SPLIT, COINCIDENT
|
---|
| 142 | */
|
---|
[448] | 143 | static int ClassifyPlane(const PolygonContainer &polys,
|
---|
| 144 | const Plane3 &plane,
|
---|
| 145 | const float epsilon);
|
---|
[372] | 146 |
|
---|
[384] | 147 |
|
---|
[372] | 148 | /** Counts the number of different intersectables associated with the polygons.
|
---|
| 149 | */
|
---|
[375] | 150 | static int ParentObjectsSize(const PolygonContainer &polys);
|
---|
[384] | 151 |
|
---|
[574] | 152 | /** Area of the accumulated polygons.
|
---|
| 153 | */
|
---|
[384] | 154 | static float GetArea(const PolygonContainer &cell);
|
---|
[372] | 155 | };
|
---|
| 156 |
|
---|
| 157 | // Overload << operator for C++-style output
|
---|
| 158 | inline ostream&
|
---|
| 159 | operator<< (ostream &s, const Polygon3 &A)
|
---|
| 160 | {
|
---|
| 161 | VertexContainer::const_iterator it;
|
---|
| 162 |
|
---|
| 163 | //s << setprecision(6) << "Polygon:\n";
|
---|
| 164 | for (it = A.mVertices.begin(); it != A.mVertices.end(); ++it)
|
---|
| 165 | s << *it << endl;
|
---|
| 166 |
|
---|
| 167 | return s;
|
---|
| 168 | }
|
---|
| 169 |
|
---|
| 170 |
|
---|
| 171 | #endif
|
---|