1 | #ifndef _POLYHEDRON_H__
|
---|
2 | #define _POLYHEDRON_H__
|
---|
3 |
|
---|
4 |
|
---|
5 | #include "common.h"
|
---|
6 | #include "AxisAlignedBox3.h"
|
---|
7 | #include "Polygon3.h"
|
---|
8 |
|
---|
9 | #include <iostream>
|
---|
10 |
|
---|
11 |
|
---|
12 |
|
---|
13 | namespace CHCDemoEngine
|
---|
14 | {
|
---|
15 |
|
---|
16 | class Plane3;
|
---|
17 | class AxisAlignedBox3;
|
---|
18 | class Vector3;
|
---|
19 |
|
---|
20 |
|
---|
21 | /** Class representing a convex polyhedron.
|
---|
22 | */
|
---|
23 | class Polyhedron
|
---|
24 | {
|
---|
25 | public:
|
---|
26 |
|
---|
27 | Polyhedron();
|
---|
28 | /** Copy constructor making a deep copy of the polygons.
|
---|
29 | */
|
---|
30 | Polyhedron(const Polyhedron &rhs);
|
---|
31 | /** Initialises the Polyhedron with the given polygons.
|
---|
32 | @NOTE The polygons are NOT duplicated, only
|
---|
33 | a shallow copy is used.
|
---|
34 | */
|
---|
35 | Polyhedron(const PolygonContainer &polys);
|
---|
36 |
|
---|
37 | ~Polyhedron();
|
---|
38 | /** Computes the intersection of the polyhedron with the given plane
|
---|
39 | @returns NULL if the geometry is not split by this plane
|
---|
40 | */
|
---|
41 | Polyhedron *CalcIntersection(const Plane3 &splitPlane) const;
|
---|
42 | /** Adds a polygon to the polyhedron.
|
---|
43 | */
|
---|
44 | void Add(Polygon3 *poly);
|
---|
45 | /** Computes bounding box of the geometry.
|
---|
46 | */
|
---|
47 | AxisAlignedBox3 GetBoundingBox() const;
|
---|
48 | /** Returns
|
---|
49 | 1 if geometry in front of the plane
|
---|
50 | 0 if plane intersects geometry,
|
---|
51 | -1 if geometry is behind the plane
|
---|
52 | */
|
---|
53 | int Side(const Plane3 &plane) const;
|
---|
54 | /** Returns true if this geometry is well shaped.
|
---|
55 | */
|
---|
56 | bool Valid() const;
|
---|
57 | /** Number of polygons.
|
---|
58 | */
|
---|
59 | int NumPolygons() const;
|
---|
60 | /** Returns the polygons in a container.
|
---|
61 | */
|
---|
62 | const PolygonContainer &GetPolygons() const;
|
---|
63 |
|
---|
64 | float GetArea() const;
|
---|
65 |
|
---|
66 | float GetVolume() const;
|
---|
67 |
|
---|
68 | Vector3 CenterOfMass() const;
|
---|
69 | /** Returns all the vertices of this polyhedron
|
---|
70 | */
|
---|
71 | void CollectVertices(VertexArray &vertices) const;
|
---|
72 |
|
---|
73 | inline friend std::ostream &operator<<(std::ostream &s, const Polyhedron &a);
|
---|
74 |
|
---|
75 | /** Computes polyhedron from a couple of intersecting planes and a bounding box.
|
---|
76 | */
|
---|
77 | static Polyhedron *CreatePolyhedron(const std::vector<Plane3> &planes, const AxisAlignedBox3 &box);
|
---|
78 |
|
---|
79 | protected:
|
---|
80 |
|
---|
81 | /** Splits the given polygon with the polyhedron
|
---|
82 | Returns the part of the polygon inside of the polyhedron.
|
---|
83 | */
|
---|
84 | Polygon3 *SplitPolygon(Polygon3 *polygon) const;
|
---|
85 |
|
---|
86 |
|
---|
87 |
|
---|
88 | ///////////////
|
---|
89 |
|
---|
90 | /// the polygons
|
---|
91 | PolygonContainer mPolygons;
|
---|
92 | /// the bounding box
|
---|
93 | AxisAlignedBox3 mBox;
|
---|
94 | };
|
---|
95 |
|
---|
96 |
|
---|
97 | std::ostream &operator<<(std::ostream &s, const Polyhedron &a)
|
---|
98 | {
|
---|
99 | PolygonContainer::const_iterator it, it_end = a.mPolygons.end();
|
---|
100 |
|
---|
101 | for (it = a.mPolygons.begin(); it != it_end; ++ it)
|
---|
102 | s << *(*it) << std::endl;
|
---|
103 | return s << std::endl;
|
---|
104 | }
|
---|
105 |
|
---|
106 |
|
---|
107 | }
|
---|
108 |
|
---|
109 | #endif |
---|