#ifndef _Polygon3_h__ #define _Polygon3_h__ //#include //#include //#include "common.h" #include "Containers.h" #include "Mesh.h" #include class Polygon3; class Plane3; class Face; class Intersectable; class AxisAlignedBox3; class Ray; //typedef Vertex3 Vector3; /** Class representing a planar convex polygon in 3d. */ class Polygon3 { public: Polygon3(); Polygon3(const VertexContainer &vertices); Polygon3(MeshInstance *parent); // creates an "infinite" polygon from this plane //Polygon3(Plane3 plane); /** Copies all the vertices of the face. */ Polygon3(Face *face, Mesh *parentMesh); /** Returns supporting plane of this polygon. */ Plane3 GetSupportingPlane() const; /** Splits polygon. @param partition the split plane @param front returns the front the front polygon @param back returns the back polygon @param splitPts returns the split points */ void Polygon3::Split(const Plane3 &partition, Polygon3 &front, Polygon3 &back, VertexContainer &splitPts); /** Returns the area of this polygon. */ float GetArea() const; enum {BACK_SIDE, FRONT_SIDE, SPLIT, COINCIDENT}; /** Classify polygon with respect to the plane. @returns one of BACK_SIDE, FRONT_SIDE, SPLIT, COINCIDENT */ int ClassifyPlane(const Plane3 &plane) const; /** Side of the polygon with respect to the plane. @returns 1 if on front side, -1 if on back side, 0 else. */ int Side(const Plane3 &plane) const; /** Scales the polygon about its center */ void Scale(const float scale); /** Computes the center of mass of the polygon */ Vector3 Center() const; /** Checks if the polygon is valid, i.e., not degenerated. @returns true if polygon is valid. */ bool Valid() const; /** Returns the surface normal. */ Vector3 GetNormal() const; /** Includes polygons to axis aligned box. */ static void IncludeInBox(const PolygonContainer &polys, AxisAlignedBox3 &box); /** Casts ray to polygon. */ int CastRay(const Ray &ray, float &t, const float nearestT); /// vertices are connected in counterclockwise order. VertexContainer mVertices; /// we can also store materials with polygons Material *mMaterial; /// pointer to the mesh instance this polygon is derived from MeshInstance *mParent; }; // Overload << operator for C++-style output inline ostream& operator<< (ostream &s, const Polygon3 &A) { VertexContainer::const_iterator it; //s << setprecision(6) << "Polygon:\n"; for (it = A.mVertices.begin(); it != A.mVertices.end(); ++it) s << *it << endl; return s; } #endif