[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;
|
---|
| 14 | class Face;
|
---|
| 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
|
---|
| 52 | */
|
---|
| 53 | void Split(const Plane3 &partition,
|
---|
| 54 | Polygon3 &front,
|
---|
[436] | 55 | Polygon3 &back);
|
---|
[372] | 56 |
|
---|
| 57 | /** Returns the area of this polygon.
|
---|
| 58 | */
|
---|
| 59 | float GetArea() const;
|
---|
| 60 |
|
---|
| 61 | /** Classify polygon with respect to the plane.
|
---|
| 62 | @returns one of BACK_SIDE, FRONT_SIDE, SPLIT, COINCIDENT
|
---|
| 63 | */
|
---|
| 64 | int ClassifyPlane(const Plane3 &plane) const;
|
---|
| 65 |
|
---|
[404] | 66 |
|
---|
| 67 | /** Classify polygon with respect to the plane.
|
---|
| 68 | @returns one of BACK_SIDE, FRONT_SIDE, SPLIT, COINCIDENT
|
---|
| 69 | */
|
---|
| 70 | int ClassifyPlane(const Plane3 &plane, const float tolerance) const;
|
---|
| 71 |
|
---|
[372] | 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;
|
---|
| 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 | /** Checks if the polygon is valid, i.e., not degenerated.
|
---|
| 85 | @returns true if polygon is valid.
|
---|
| 86 | */
|
---|
| 87 | bool Valid() const;
|
---|
| 88 |
|
---|
| 89 | /** Returns the surface normal.
|
---|
| 90 | */
|
---|
| 91 | Vector3 GetNormal() const;
|
---|
| 92 |
|
---|
| 93 | /** Casts ray to polygon.
|
---|
| 94 | */
|
---|
| 95 | int CastRay(const Ray &ray, float &t, const float nearestT);
|
---|
| 96 |
|
---|
[384] | 97 |
|
---|
| 98 | /** The piercing rays of the polygon are inherited by the child fragments
|
---|
| 99 | @parm front_piece the front fragment inheriting the front rays
|
---|
| 100 | @param back_piece the back fragment inheriting the back rays
|
---|
[372] | 101 | */
|
---|
[384] | 102 | void InheritRays(Polygon3 &front_piece,
|
---|
| 103 | Polygon3 &back_piece) const;
|
---|
[372] | 104 |
|
---|
[396] | 105 | /** Returns new polygon with reverse orientation.
|
---|
| 106 | */
|
---|
| 107 | Polygon3 *CreateReversePolygon() const;
|
---|
[384] | 108 |
|
---|
[372] | 109 | /// vertices are connected in counterclockwise order.
|
---|
| 110 | VertexContainer mVertices;
|
---|
| 111 |
|
---|
| 112 | /// we can also store materials with polygons
|
---|
| 113 | Material *mMaterial;
|
---|
| 114 |
|
---|
| 115 | /// pointer to the mesh instance this polygon is derived from
|
---|
| 116 | MeshInstance *mParent;
|
---|
| 117 |
|
---|
| 118 | /// Rays piercing this polygon
|
---|
| 119 | RayContainer mPiercingRays;
|
---|
| 120 |
|
---|
[384] | 121 |
|
---|
| 122 | /** Includes polygons to axis aligned box.
|
---|
[372] | 123 | */
|
---|
[384] | 124 | static void IncludeInBox(const PolygonContainer &polys, AxisAlignedBox3 &box);
|
---|
[372] | 125 |
|
---|
[384] | 126 | /** Classify polygons with respect to the plane.
|
---|
| 127 | @returns one of BACK_SIDE, FRONT_SIDE, SPLIT, COINCIDENT
|
---|
| 128 | */
|
---|
| 129 | static int ClassifyPlane(const PolygonContainer &polys, const Plane3 &plane);
|
---|
[372] | 130 |
|
---|
[384] | 131 |
|
---|
[372] | 132 | /** Counts the number of different intersectables associated with the polygons.
|
---|
| 133 | */
|
---|
[375] | 134 | static int ParentObjectsSize(const PolygonContainer &polys);
|
---|
[384] | 135 |
|
---|
| 136 | static float GetArea(const PolygonContainer &cell);
|
---|
[372] | 137 | };
|
---|
| 138 |
|
---|
| 139 | // Overload << operator for C++-style output
|
---|
| 140 | inline ostream&
|
---|
| 141 | operator<< (ostream &s, const Polygon3 &A)
|
---|
| 142 | {
|
---|
| 143 | VertexContainer::const_iterator it;
|
---|
| 144 |
|
---|
| 145 | //s << setprecision(6) << "Polygon:\n";
|
---|
| 146 | for (it = A.mVertices.begin(); it != A.mVertices.end(); ++it)
|
---|
| 147 | s << *it << endl;
|
---|
| 148 |
|
---|
| 149 | return s;
|
---|
| 150 | }
|
---|
| 151 |
|
---|
| 152 |
|
---|
| 153 | #endif
|
---|