#ifndef _POLYHEDRON_H__ #define _POLYHEDRON_H__ #include "common.h" #include "AxisAlignedBox3.h" #include "Polygon3.h" #include namespace CHCDemoEngine { class Plane3; class AxisAlignedBox3; class Vector3; /** Class representing a convex polyhedron. */ class Polyhedron { public: Polyhedron(); /** Copy constructor making a deep copy of the polygons. */ Polyhedron(const Polyhedron &rhs); /** Initialises the Polyhedron with the given polygons. @NOTE The polygons are NOT duplicated, only a shallow copy is used. */ Polyhedron(const PolygonContainer &polys); ~Polyhedron(); /** Computes the intersection of the polyhedron with the given plane and returns it as another polyhedron @returns NULL if the geometry is not split by this plane */ Polyhedron *CalcIntersection(const Plane3 &splitPlane) const; /** Adds a polygon to the polyhedron. */ //void Add(const Polygon3 &poly); void Add(Polygon3 *poly); /** Computes bounding box of the geometry. */ AxisAlignedBox3 GetBoundingBox() const; /** Returns 1 if geometry in front of the plane 0 if plane intersects geometry, -1 if geometry is behind the plane */ int Side(const Plane3 &plane) const; /** Returns true if this geometry is well shaped. */ bool Valid() const; /** Number of polygons. */ int NumPolygons() const; /** Returns the polygons in a container. */ const PolygonContainer &GetPolygons() const; float GetArea() const; float GetVolume() const; Vector3 CenterOfMass() const; /** Returns all the vertices of this polyhedron */ void CollectVertices(VertexArray &vertices) const; /** Computes polyhedron from a couple of intersecting planes and a bounding box. */ static Polyhedron *CreatePolyhedron(const std::vector &planes, const AxisAlignedBox3 &box); inline friend std::ostream &operator<<(std::ostream &s, const Polyhedron &a); protected: /** Splits the given polygon with the polyhedron Returns the part of the polygon inside of the polyhedron. */ Polygon3 *SplitPolygon(Polygon3 *polygon) const; /////////////// /// the polygons PolygonContainer mPolygons; /// the bounding box AxisAlignedBox3 mBox; }; std::ostream &operator<<(std::ostream &s, const Polyhedron &a) { PolygonContainer::const_iterator it, it_end = a.mPolygons.end(); for (it = a.mPolygons.begin(); it != it_end; ++ it) s << *(*it) << std::endl; return s << std::endl; } } #endif