[148] | 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-2013 Torus Knot Software Ltd |
---|
| 8 | Copyright (c) 2006 Matthias Fink, netAllied GmbH <matthias.fink@web.de> |
---|
| 9 | |
---|
| 10 | Permission is hereby granted, free of charge, to any person obtaining a copy |
---|
| 11 | of this software and associated documentation files (the "Software"), to deal |
---|
| 12 | in the Software without restriction, including without limitation the rights |
---|
| 13 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
---|
| 14 | copies of the Software, and to permit persons to whom the Software is |
---|
| 15 | furnished to do so, subject to the following conditions: |
---|
| 16 | |
---|
| 17 | The above copyright notice and this permission notice shall be included in |
---|
| 18 | all copies or substantial portions of the Software. |
---|
| 19 | |
---|
| 20 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
---|
| 21 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
---|
| 22 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
---|
| 23 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
---|
| 24 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
---|
| 25 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
---|
| 26 | THE SOFTWARE. |
---|
| 27 | ----------------------------------------------------------------------------- |
---|
| 28 | */ |
---|
| 29 | #ifndef __ConvexBody_H__ |
---|
| 30 | #define __ConvexBody_H__ |
---|
| 31 | |
---|
| 32 | #include "OgrePrerequisites.h" |
---|
| 33 | #include "OgrePolygon.h" |
---|
| 34 | #if OGRE_THREAD_SUPPORT |
---|
| 35 | #include "Threading/OgreThreadHeaders.h" |
---|
| 36 | #endif |
---|
| 37 | #include "OgreHeaderPrefix.h" |
---|
| 38 | |
---|
| 39 | |
---|
| 40 | namespace Ogre |
---|
| 41 | { |
---|
| 42 | |
---|
| 43 | /** \addtogroup Core |
---|
| 44 | * @{ |
---|
| 45 | */ |
---|
| 46 | /** \addtogroup Math |
---|
| 47 | * @{ |
---|
| 48 | */ |
---|
| 49 | /** Holds a solid representation of a convex body. |
---|
| 50 | @remarks |
---|
| 51 | Administers a convex body. All polygons of the body are convex and |
---|
| 52 | planar. Several operations may be applied, ranging from intersection |
---|
| 53 | to join where each result it itself a convex body. |
---|
| 54 | */ |
---|
| 55 | class _OgreExport ConvexBody |
---|
| 56 | { |
---|
| 57 | public: |
---|
| 58 | typedef vector< Polygon* >::type PolygonList; |
---|
| 59 | |
---|
| 60 | protected: |
---|
| 61 | PolygonList mPolygons; |
---|
| 62 | |
---|
| 63 | // Static 'free list' of polygons to save reallocation, shared between all bodies |
---|
| 64 | static PolygonList msFreePolygons; |
---|
| 65 | #if OGRE_THREAD_SUPPORT |
---|
| 66 | OGRE_STATIC_MUTEX(msFreePolygonsMutex); |
---|
| 67 | #endif |
---|
| 68 | |
---|
| 69 | public: |
---|
| 70 | ConvexBody(); |
---|
| 71 | ~ConvexBody(); |
---|
| 72 | ConvexBody( const ConvexBody& cpy ); |
---|
| 73 | |
---|
| 74 | /** Build a new polygon representation from a frustum. |
---|
| 75 | */ |
---|
| 76 | void define(const Frustum& frustum); |
---|
| 77 | |
---|
| 78 | /** Build a new polygon representation from an AAB. |
---|
| 79 | */ |
---|
| 80 | void define(const AxisAlignedBox& aab); |
---|
| 81 | |
---|
| 82 | /** Clips the body with a frustum. The resulting holes |
---|
| 83 | are filled with new polygons. |
---|
| 84 | */ |
---|
| 85 | void clip( const Frustum& frustum ); |
---|
| 86 | |
---|
| 87 | /** Clips the body with an AAB. The resulting holes |
---|
| 88 | are filled with new polygons. |
---|
| 89 | */ |
---|
| 90 | void clip( const AxisAlignedBox& aab ); |
---|
| 91 | |
---|
| 92 | /** Clips the body with another body. |
---|
| 93 | */ |
---|
| 94 | void clip(const ConvexBody& body); |
---|
| 95 | |
---|
| 96 | /** Clips the object by the positive half space of a plane |
---|
| 97 | */ |
---|
| 98 | void clip(const Plane& pl, bool keepNegative = true); |
---|
| 99 | |
---|
| 100 | /** Extends the existing body to incorporate the passed in point as a |
---|
| 101 | convex hull. |
---|
| 102 | @remarks |
---|
| 103 | You must already have constructed a basic body using a 'construct' |
---|
| 104 | method. |
---|
| 105 | */ |
---|
| 106 | void extend(const Vector3& pt); |
---|
| 107 | |
---|
| 108 | /** Resets the object. |
---|
| 109 | */ |
---|
| 110 | void reset( void ); |
---|
| 111 | |
---|
| 112 | /** Returns the current number of polygons. |
---|
| 113 | */ |
---|
| 114 | size_t getPolygonCount( void ) const; |
---|
| 115 | |
---|
| 116 | /** Returns the number of vertices for a polygon |
---|
| 117 | */ |
---|
| 118 | size_t getVertexCount( size_t poly ) const; |
---|
| 119 | |
---|
| 120 | /** Returns a polygon. |
---|
| 121 | */ |
---|
| 122 | const Polygon& getPolygon( size_t poly ) const; |
---|
| 123 | |
---|
| 124 | /** Returns a specific vertex of a polygon. |
---|
| 125 | */ |
---|
| 126 | const Vector3& getVertex( size_t poly, size_t vertex ) const; |
---|
| 127 | |
---|
| 128 | /** Returns the normal of a specified polygon. |
---|
| 129 | */ |
---|
| 130 | const Vector3& getNormal( size_t poly ); |
---|
| 131 | |
---|
| 132 | /** Returns an AABB representation. |
---|
| 133 | */ |
---|
| 134 | AxisAlignedBox getAABB( void ) const; |
---|
| 135 | |
---|
| 136 | /** Checks if the body has a closed hull. |
---|
| 137 | */ |
---|
| 138 | bool hasClosedHull( void ) const; |
---|
| 139 | |
---|
| 140 | /** Merges all neighboring polygons into one single polygon if they are |
---|
| 141 | lay in the same plane. |
---|
| 142 | */ |
---|
| 143 | void mergePolygons( void ); |
---|
| 144 | |
---|
| 145 | /** Determines if the current object is equal to the compared one. |
---|
| 146 | */ |
---|
| 147 | bool operator == ( const ConvexBody& rhs ) const; |
---|
| 148 | |
---|
| 149 | /** Determines if the current object is not equal to the compared one. |
---|
| 150 | */ |
---|
| 151 | bool operator != ( const ConvexBody& rhs ) const |
---|
| 152 | { return !( *this == rhs ); } |
---|
| 153 | |
---|
| 154 | /** Prints out the body with all its polygons. |
---|
| 155 | */ |
---|
| 156 | _OgreExport friend std::ostream& operator<< ( std::ostream& strm, const ConvexBody& body ); |
---|
| 157 | |
---|
| 158 | /** Log details of this body */ |
---|
| 159 | void logInfo() const; |
---|
| 160 | |
---|
| 161 | /// Initialise the internal polygon pool used to minimise allocations |
---|
| 162 | static void _initialisePool(); |
---|
| 163 | /// Tear down the internal polygon pool used to minimise allocations |
---|
| 164 | static void _destroyPool(); |
---|
| 165 | |
---|
| 166 | |
---|
| 167 | protected: |
---|
| 168 | /** Get a new polygon from the pool. |
---|
| 169 | */ |
---|
| 170 | static Polygon* allocatePolygon(); |
---|
| 171 | /** Release a polygon back tot he pool. */ |
---|
| 172 | static void freePolygon(Polygon* poly); |
---|
| 173 | /** Inserts a polygon at a particular point in the body. |
---|
| 174 | @note |
---|
| 175 | After this method is called, the ConvexBody 'owns' this Polygon |
---|
| 176 | and will be responsible for deleting it. |
---|
| 177 | */ |
---|
| 178 | void insertPolygon(Polygon* pdata, size_t poly); |
---|
| 179 | /** Inserts a polygon at the end. |
---|
| 180 | @note |
---|
| 181 | After this method is called, the ConvexBody 'owns' this Polygon |
---|
| 182 | and will be responsible for deleting it. |
---|
| 183 | */ |
---|
| 184 | void insertPolygon(Polygon* pdata); |
---|
| 185 | |
---|
| 186 | /** Inserts a vertex for a polygon at a particular point. |
---|
| 187 | @note |
---|
| 188 | No checks are done whether the assembled polygon is (still) planar, |
---|
| 189 | the caller must ensure that this is the case. |
---|
| 190 | */ |
---|
| 191 | void insertVertex(size_t poly, const Vector3& vdata, size_t vertex); |
---|
| 192 | /** Inserts a vertex for a polygon at the end. |
---|
| 193 | @note |
---|
| 194 | No checks are done whether the assembled polygon is (still) planar, |
---|
| 195 | the caller must ensure that this is the case. |
---|
| 196 | */ |
---|
| 197 | void insertVertex(size_t poly, const Vector3& vdata); |
---|
| 198 | /** Deletes a specific polygon. |
---|
| 199 | */ |
---|
| 200 | void deletePolygon(size_t poly); |
---|
| 201 | |
---|
| 202 | /** Removes a specific polygon from the body without deleting it. |
---|
| 203 | @note |
---|
| 204 | The retrieved polygon needs to be deleted later by the caller. |
---|
| 205 | */ |
---|
| 206 | Polygon* unlinkPolygon(size_t poly); |
---|
| 207 | |
---|
| 208 | /** Moves all polygons from the parameter body to this instance. |
---|
| 209 | @note Both the passed in object and this instance are modified |
---|
| 210 | */ |
---|
| 211 | void moveDataFromBody(ConvexBody& body); |
---|
| 212 | |
---|
| 213 | /** Deletes a specific vertex of a specific polygon. |
---|
| 214 | */ |
---|
| 215 | void deleteVertex(size_t poly, size_t vertex); |
---|
| 216 | |
---|
| 217 | /** Replace a polygon at a particular index. |
---|
| 218 | @note Again, the passed in polygon is owned by this object after this |
---|
| 219 | call returns, and this object is resonsible for deleting it. |
---|
| 220 | */ |
---|
| 221 | void setPolygon(Polygon* pdata, size_t poly ); |
---|
| 222 | |
---|
| 223 | /** Replace a specific vertex of a polygon. |
---|
| 224 | @note |
---|
| 225 | No checks are done whether the assembled polygon is (still) planar, |
---|
| 226 | the caller must ensure that this is the case. |
---|
| 227 | */ |
---|
| 228 | void setVertex( size_t poly, const Vector3& vdata, size_t vertex ); |
---|
| 229 | |
---|
| 230 | /** Returns the single edges in an EdgeMap (= edges where one side is a vertex and the |
---|
| 231 | other is empty space (a hole in the body)). |
---|
| 232 | */ |
---|
| 233 | Polygon::EdgeMap getSingleEdges() const; |
---|
| 234 | |
---|
| 235 | /** Stores the edges of a specific polygon in a passed in structure. |
---|
| 236 | */ |
---|
| 237 | void storeEdgesOfPolygon(size_t poly, Polygon::EdgeMap *edgeMap) const; |
---|
| 238 | |
---|
| 239 | /** Allocates space for an specified amount of polygons with |
---|
| 240 | each of them having a specified number of vertices. |
---|
| 241 | @note |
---|
| 242 | Old data (if available) will be erased. |
---|
| 243 | */ |
---|
| 244 | void allocateSpace(size_t numPolygons, size_t numVertices); |
---|
| 245 | |
---|
| 246 | /** Searches for a pair (an edge) in the intersectionList with an entry |
---|
| 247 | that equals vec, and removes it from the passed in list. |
---|
| 248 | @param vec The vertex to search for in intersectionEdges |
---|
| 249 | @param intersectionEdges A list of edges, which is updated if a match is found |
---|
| 250 | @param vNext A reference to a vector which will be filled with the other |
---|
| 251 | vertex at the matching edge, if found. |
---|
| 252 | @return True if a match was found |
---|
| 253 | */ |
---|
| 254 | bool findAndEraseEdgePair(const Vector3& vec, |
---|
| 255 | Polygon::EdgeMap& intersectionEdges, Vector3& vNext ) const; |
---|
| 256 | |
---|
| 257 | }; |
---|
| 258 | /** @} */ |
---|
| 259 | /** @} */ |
---|
| 260 | |
---|
| 261 | } |
---|
| 262 | |
---|
| 263 | #include "OgreHeaderSuffix.h" |
---|
| 264 | |
---|
| 265 | #endif |
---|
| 266 | |
---|