[2912] | 1 | #ifndef _Polygon3_h__
|
---|
| 2 | #define _Polygon3_h__
|
---|
| 3 |
|
---|
| 4 |
|
---|
| 5 | #include "common.h"
|
---|
| 6 | #include "Vector3.h"
|
---|
| 7 | #include <iomanip>
|
---|
| 8 | #include <iostream>
|
---|
| 9 |
|
---|
| 10 |
|
---|
| 11 | namespace CHCDemoEngine
|
---|
| 12 | {
|
---|
| 13 |
|
---|
| 14 |
|
---|
| 15 | class Polygon3;
|
---|
| 16 | class Plane3;
|
---|
| 17 | class AxisAlignedBox3;
|
---|
| 18 | struct Triangle3;
|
---|
| 19 |
|
---|
| 20 |
|
---|
| 21 |
|
---|
| 22 | /** Class representing a planar convex polygon in 3d.
|
---|
| 23 | */
|
---|
| 24 | class Polygon3
|
---|
| 25 | {
|
---|
| 26 | public:
|
---|
| 27 |
|
---|
| 28 | enum {BACK_SIDE, FRONT_SIDE, SPLIT, COINCIDENT};
|
---|
| 29 |
|
---|
| 30 | /** Default constructor creating an empty polygon.
|
---|
| 31 | */
|
---|
| 32 | Polygon3();
|
---|
| 33 | /** Constructor creating a polygon from the vertices.
|
---|
| 34 | */
|
---|
| 35 | Polygon3(const VertexArray &vertices);
|
---|
| 36 |
|
---|
| 37 | ~Polygon3();
|
---|
| 38 | /** Returns supporting plane of this polygon.
|
---|
| 39 | */
|
---|
| 40 | Plane3 GetSupportingPlane() const;
|
---|
| 41 | /** Splits polygon.
|
---|
| 42 | @param partition the split plane
|
---|
| 43 | @param front returns the front polygon
|
---|
| 44 | @param back returns the back polygon
|
---|
| 45 | @param epsilon epsilon where two points are considered equal
|
---|
| 46 | */
|
---|
| 47 | void Split(const Plane3 &partition, Polygon3 &front, Polygon3 &back, float eps);
|
---|
| 48 |
|
---|
| 49 | /** Returns the area of this polygon.
|
---|
| 50 | */
|
---|
| 51 | float GetArea() const;
|
---|
| 52 |
|
---|
| 53 | /** Classify polygon with respect to the plane.
|
---|
| 54 | @param epsilon tolerance value
|
---|
| 55 | @returns one of BACK_SIDE, FRONT_SIDE, SPLIT, COINCIDENT
|
---|
| 56 |
|
---|
| 57 | */
|
---|
| 58 | int ClassifyPlane(const Plane3 &plane, const float eps) const;
|
---|
| 59 | /** Side of the polygon with respect to the plane.
|
---|
| 60 | @returns 1 if on front side, -1 if on back side, 0 else.
|
---|
| 61 | */
|
---|
| 62 | int Side(const Plane3 &plane, float eps) const;
|
---|
| 63 | /** Computes the center of mass of the polygon
|
---|
| 64 | */
|
---|
| 65 | Vector3 Center() const;
|
---|
| 66 | /** Checks if the polygon is valid, i.e., not degenerated.
|
---|
| 67 | @returns true if polygon is valid.
|
---|
| 68 | */
|
---|
[2913] | 69 | bool Valid(float epsilon) const;
|
---|
[2912] | 70 | /** Returns the surface normal.
|
---|
| 71 | */
|
---|
| 72 | Vector3 GetNormal() const;
|
---|
| 73 | /** Returns new polygon with reverse orientation.
|
---|
| 74 | */
|
---|
| 75 | Polygon3 *CreateReversePolygon() const;
|
---|
| 76 |
|
---|
| 77 | AxisAlignedBox3 GetBoundingBox() const;
|
---|
| 78 |
|
---|
| 79 |
|
---|
| 80 | //////////////////////////////////////////
|
---|
| 81 |
|
---|
| 82 | /// vertices are connected in counterclockwise order.
|
---|
| 83 | VertexArray mVertices;
|
---|
| 84 |
|
---|
| 85 | };
|
---|
| 86 |
|
---|
| 87 |
|
---|
| 88 | // Overload << operator for C++-style output
|
---|
| 89 | inline std::ostream&
|
---|
| 90 | operator<< (std::ostream &s, const Polygon3 &A)
|
---|
| 91 | {
|
---|
| 92 | VertexArray::const_iterator it;
|
---|
| 93 |
|
---|
| 94 | //s << setprecision(6) << "Polygon:\n";
|
---|
| 95 | for (it = A.mVertices.begin(); it != A.mVertices.end(); ++ it)
|
---|
| 96 | {
|
---|
| 97 | s << *it << " ";
|
---|
| 98 | }
|
---|
| 99 |
|
---|
| 100 | return s;
|
---|
| 101 | }
|
---|
| 102 |
|
---|
| 103 | }
|
---|
| 104 |
|
---|
| 105 | #endif
|
---|