1 | /* |
---|
2 | ----------------------------------------------------------------------------- |
---|
3 | This source file is part of OGRE |
---|
4 | (Object-oriented Graphics Rendering Engine) |
---|
5 | For the latest info, see http://www.ogre3d.org/ |
---|
6 | |
---|
7 | Copyright (c) 2000-2006 Torus Knot Software Ltd |
---|
8 | Copyright (c) 2006 Matthias Fink, netAllied GmbH <matthias.fink@web.de> |
---|
9 | Also see acknowledgements in Readme.html |
---|
10 | |
---|
11 | This program is free software; you can redistribute it and/or modify it under |
---|
12 | the terms of the GNU Lesser General Public License as published by the Free Software |
---|
13 | Foundation; either version 2 of the License, or (at your option) any later |
---|
14 | version. |
---|
15 | |
---|
16 | This program is distributed in the hope that it will be useful, but WITHOUT |
---|
17 | ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS |
---|
18 | FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. |
---|
19 | |
---|
20 | You should have received a copy of the GNU Lesser General Public License along with |
---|
21 | this program; if not, write to the Free Software Foundation, Inc., 59 Temple |
---|
22 | Place - Suite 330, Boston, MA 02111-1307, USA, or go to |
---|
23 | http://www.gnu.org/copyleft/lesser.txt. |
---|
24 | |
---|
25 | You may alternatively use this source under the terms of a specific version of |
---|
26 | the OGRE Unrestricted License provided you have obtained such a license from |
---|
27 | Torus Knot Software Ltd. |
---|
28 | ----------------------------------------------------------------------------- |
---|
29 | */ |
---|
30 | #ifndef __ConvexBody_H__ |
---|
31 | #define __ConvexBody_H__ |
---|
32 | |
---|
33 | #include "OgrePrerequisites.h" |
---|
34 | #include "OgrePolygon.h" |
---|
35 | |
---|
36 | |
---|
37 | namespace Ogre |
---|
38 | { |
---|
39 | |
---|
40 | /** Holds a solid representation of a convex body. |
---|
41 | @remarks |
---|
42 | Administers a convex body. All polygons of the body are convex and |
---|
43 | planar. Several operations may be applied, ranging from intersection |
---|
44 | to join where each result it itself a convex body. |
---|
45 | */ |
---|
46 | class _OgreExport ConvexBody |
---|
47 | { |
---|
48 | public: |
---|
49 | typedef std::vector< Polygon* > PolygonList; |
---|
50 | |
---|
51 | protected: |
---|
52 | PolygonList mPolygons; |
---|
53 | |
---|
54 | // Static 'free list' of polygons to save reallocation, shared between all bodies |
---|
55 | static PolygonList msFreePolygons; |
---|
56 | #if OGRE_THREAD_SUPPORT |
---|
57 | static boost::recursive_mutex msFreePolygonsMutex; |
---|
58 | #endif |
---|
59 | |
---|
60 | public: |
---|
61 | ConvexBody(); |
---|
62 | ~ConvexBody(); |
---|
63 | ConvexBody( const ConvexBody& cpy ); |
---|
64 | |
---|
65 | /** Build a new polygon representation from a frustum. |
---|
66 | */ |
---|
67 | void define(const Frustum& frustum); |
---|
68 | |
---|
69 | /** Build a new polygon representation from an AAB. |
---|
70 | */ |
---|
71 | void define(const AxisAlignedBox& aab); |
---|
72 | |
---|
73 | /** Clips the body with a frustum. The resulting holes |
---|
74 | are filled with new polygons. |
---|
75 | */ |
---|
76 | void clip( const Frustum& frustum ); |
---|
77 | |
---|
78 | /** Clips the body with an AAB. The resulting holes |
---|
79 | are filled with new polygons. |
---|
80 | */ |
---|
81 | void clip( const AxisAlignedBox& aab ); |
---|
82 | |
---|
83 | /** Clips the body with another body. |
---|
84 | */ |
---|
85 | void clip(const ConvexBody& body); |
---|
86 | |
---|
87 | /** Clips the object by the positive half space of a plane |
---|
88 | */ |
---|
89 | void clip(const Plane& pl, bool keepNegative = true); |
---|
90 | |
---|
91 | /** Extends the existing body to incorporate the passed in point as a |
---|
92 | convex hull. |
---|
93 | @remarks |
---|
94 | You must already have constructed a basic body using a 'construct' |
---|
95 | method. |
---|
96 | */ |
---|
97 | void extend(const Vector3& pt); |
---|
98 | |
---|
99 | /** Resets the object. |
---|
100 | */ |
---|
101 | void reset( void ); |
---|
102 | |
---|
103 | /** Returns the current number of polygons. |
---|
104 | */ |
---|
105 | size_t getPolygonCount( void ) const; |
---|
106 | |
---|
107 | /** Returns the number of vertices for a polygon |
---|
108 | */ |
---|
109 | size_t getVertexCount( size_t poly ) const; |
---|
110 | |
---|
111 | /** Returns a polygon. |
---|
112 | */ |
---|
113 | const Polygon& getPolygon( size_t poly ) const; |
---|
114 | |
---|
115 | /** Returns a specific vertex of a polygon. |
---|
116 | */ |
---|
117 | const Vector3& getVertex( size_t poly, size_t vertex ) const; |
---|
118 | |
---|
119 | /** Returns the normal of a specified polygon. |
---|
120 | */ |
---|
121 | const Vector3& getNormal( size_t poly ); |
---|
122 | |
---|
123 | /** Returns an AABB representation. |
---|
124 | */ |
---|
125 | AxisAlignedBox getAABB( void ) const; |
---|
126 | |
---|
127 | /** Checks if the body has a closed hull. |
---|
128 | */ |
---|
129 | bool hasClosedHull( void ) const; |
---|
130 | |
---|
131 | /** Merges all neighboring polygons into one single polygon if they are |
---|
132 | lay in the same plane. |
---|
133 | */ |
---|
134 | void mergePolygons( void ); |
---|
135 | |
---|
136 | /** Determines if the current object is equal to the compared one. |
---|
137 | */ |
---|
138 | bool operator == ( const ConvexBody& rhs ) const; |
---|
139 | |
---|
140 | /** Determines if the current object is not equal to the compared one. |
---|
141 | */ |
---|
142 | bool operator != ( const ConvexBody& rhs ) const |
---|
143 | { return !( *this == rhs ); } |
---|
144 | |
---|
145 | /** Prints out the body with all its polygons. |
---|
146 | */ |
---|
147 | _OgreExport friend std::ostream& operator<< ( std::ostream& strm, const ConvexBody& body ); |
---|
148 | |
---|
149 | /** Log details of this body */ |
---|
150 | void logInfo() const; |
---|
151 | |
---|
152 | /// Initialise the internal polygon pool used to minimise allocations |
---|
153 | static void _initialisePool(); |
---|
154 | /// Tear down the internal polygon pool used to minimise allocations |
---|
155 | static void _destroyPool(); |
---|
156 | |
---|
157 | |
---|
158 | protected: |
---|
159 | /** Get a new polygon from the pool. |
---|
160 | */ |
---|
161 | static Polygon* allocatePolygon(); |
---|
162 | /** Release a polygon back tot he pool. */ |
---|
163 | static void freePolygon(Polygon* poly); |
---|
164 | /** Inserts a polygon at a partcular point in the body. |
---|
165 | @note |
---|
166 | After this method is called, the ConvexBody 'owns' this Polygon |
---|
167 | and will be responsible for deleting it. |
---|
168 | */ |
---|
169 | void insertPolygon(Polygon* pdata, size_t poly); |
---|
170 | /** Inserts a polygon at the end. |
---|
171 | @note |
---|
172 | After this method is called, the ConvexBody 'owns' this Polygon |
---|
173 | and will be responsible for deleting it. |
---|
174 | */ |
---|
175 | void insertPolygon(Polygon* pdata); |
---|
176 | |
---|
177 | /** Inserts a vertex for a polygon at a particular point. |
---|
178 | @note |
---|
179 | No checks are done whether the assembled polygon is (still) planar, |
---|
180 | the caller must ensure that this is the case. |
---|
181 | */ |
---|
182 | void insertVertex(size_t poly, const Vector3& vdata, size_t vertex); |
---|
183 | /** Inserts a vertex for a polygon at the end. |
---|
184 | @note |
---|
185 | No checks are done whether the assembled polygon is (still) planar, |
---|
186 | the caller must ensure that this is the case. |
---|
187 | */ |
---|
188 | void insertVertex(size_t poly, const Vector3& vdata); |
---|
189 | /** Deletes a specific polygon. |
---|
190 | */ |
---|
191 | void deletePolygon(size_t poly); |
---|
192 | |
---|
193 | /** Removes a specific polygon from the body without deleting it. |
---|
194 | @note |
---|
195 | The retrieved polygon needs to be deleted later by the caller. |
---|
196 | */ |
---|
197 | Polygon* unlinkPolygon(size_t poly); |
---|
198 | |
---|
199 | /** Moves all polygons from the parameter body to this instance. |
---|
200 | @note Both the passed in object and this instance are modified |
---|
201 | */ |
---|
202 | void moveDataFromBody(ConvexBody& body); |
---|
203 | |
---|
204 | /** Deletes a specific vertex of a specific polygon. |
---|
205 | */ |
---|
206 | void deleteVertex(size_t poly, size_t vertex); |
---|
207 | |
---|
208 | /** Replace a polygon at a particular index. |
---|
209 | @note Again, the passed in polygon is owned by this object after this |
---|
210 | call returns, and this object is resonsible for deleting it. |
---|
211 | */ |
---|
212 | void setPolygon(Polygon* pdata, size_t poly ); |
---|
213 | |
---|
214 | /** Replace a specific vertex of a polygon. |
---|
215 | @note |
---|
216 | No checks are done whether the assembled polygon is (still) planar, |
---|
217 | the caller must ensure that this is the case. |
---|
218 | */ |
---|
219 | void setVertex( size_t poly, const Vector3& vdata, size_t vertex ); |
---|
220 | |
---|
221 | /** Returns the single edges in an EdgeMap (= edges where one side is a vertex and the |
---|
222 | other is empty space (a hole in the body)). |
---|
223 | */ |
---|
224 | Polygon::EdgeMap getSingleEdges() const; |
---|
225 | |
---|
226 | /** Stores the edges of a specific polygon in a passed in structure. |
---|
227 | */ |
---|
228 | void storeEdgesOfPolygon(size_t poly, Polygon::EdgeMap *edgeMap) const; |
---|
229 | |
---|
230 | /** Allocates space for an specified amount of polygons with |
---|
231 | each of them having a specified number of vertices. |
---|
232 | @note |
---|
233 | Old data (if available) will be erased. |
---|
234 | */ |
---|
235 | void allocateSpace(size_t numPolygons, size_t numVertices); |
---|
236 | |
---|
237 | /** Searches for a pair (an edge) in the intersectionList with an entry |
---|
238 | that equals vec, and removes it from the passed in list. |
---|
239 | @param vec The vertex to search for in intersectionEdges |
---|
240 | @param intersectionEdges A list of edges, which is updated if a match is found |
---|
241 | @param vNext A reference to a vector which will be filled with the other |
---|
242 | vertex at the matching edge, if found. |
---|
243 | @returns True if a match was found |
---|
244 | */ |
---|
245 | bool findAndEraseEdgePair(const Vector3& vec, |
---|
246 | Polygon::EdgeMap& intersectionEdges, Vector3& vNext ) const; |
---|
247 | |
---|
248 | }; |
---|
249 | } |
---|
250 | |
---|
251 | #endif |
---|
252 | |
---|