Coverage Report

Created: 2025-08-28 06:22

/src/ogre/OgreMain/include/OgreConvexBody.h
Line
Count
Source (jump to first uncovered line)
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-2014 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
#include "OgreHeaderPrefix.h"
35
#include "Threading/OgreThreadHeaders.h"
36
37
namespace Ogre
38
{
39
40
    /** \addtogroup Core
41
    *  @{
42
    */
43
    /** \addtogroup Math
44
    *  @{
45
    */
46
    /** Holds a solid representation of a convex body.
47
48
        Administers a convex body. All polygons of the body are convex and
49
        planar. Several operations may be applied, ranging from intersection
50
        to join where each result it itself a convex body.
51
    */
52
    class _OgreExport ConvexBody
53
    {
54
    public:
55
        typedef std::vector< Polygon* >    PolygonList;
56
57
    private:
58
        PolygonList mPolygons;
59
60
        // Static 'free list' of polygons to save reallocation, shared between all bodies
61
        static PolygonList msFreePolygons;
62
        OGRE_STATIC_MUTEX(msFreePolygonsMutex);
63
64
    public:
65
        ConvexBody();
66
        ~ConvexBody();
67
        ConvexBody( const ConvexBody& cpy );
68
69
        /** Build a new polygon representation from a frustum.
70
        */
71
        void define(const Frustum& frustum);
72
73
        /** Build a new polygon representation from an AAB.
74
        */
75
        void define(const AxisAlignedBox& aab);
76
77
        /** Clips the body with a frustum. The resulting holes
78
            are filled with new polygons.
79
        */
80
        void clip( const Frustum& frustum );
81
82
        /** Clips the body with an AAB. The resulting holes
83
            are filled with new polygons.
84
        */
85
        void clip( const AxisAlignedBox& aab );
86
87
        /** Clips the body with another body.
88
        */
89
        void clip(const ConvexBody& body);
90
91
        /** Clips the object by the positive half space of a plane
92
        */
93
        void clip(const Plane& pl, bool keepNegative = true);
94
95
        /** Extends the existing body to incorporate the passed in point as a
96
            convex hull.
97
98
            You must already have constructed a basic body using a 'construct' 
99
            method.
100
        */
101
        void extend(const Vector3& pt);
102
103
        /** Resets the object.
104
        */
105
        void reset( void );
106
107
        /** Returns the current number of polygons.
108
        */
109
        size_t getPolygonCount( void ) const;
110
111
        /** Returns the number of vertices for a polygon
112
        */
113
        size_t getVertexCount( size_t poly ) const;
114
115
        /** Returns a polygon.
116
        */
117
        const Polygon& getPolygon( size_t poly ) const;
118
119
        /** Returns a specific vertex of a polygon.
120
        */
121
        const Vector3& getVertex( size_t poly, size_t vertex ) const;
122
123
        /** Returns the normal of a specified polygon.
124
        */
125
        const Vector3& getNormal( size_t poly );
126
127
        /** Returns an AABB representation.
128
        */
129
        AxisAlignedBox getAABB( void ) const;
130
131
        /** Checks if the body has a closed hull.
132
        */
133
        bool hasClosedHull( void ) const;
134
135
        /** Merges all neighboring polygons into one single polygon if they are
136
            lay in the same plane.
137
        */
138
        void mergePolygons( void );
139
140
        /** Determines if the current object is equal to the compared one.
141
        */
142
        bool operator == ( const ConvexBody& rhs ) const;
143
144
        /** Determines if the current object is not equal to the compared one.
145
        */
146
        bool operator != ( const ConvexBody& rhs ) const
147
0
        { return !( *this == rhs ); }
148
149
        /** Prints out the body with all its polygons.
150
        */
151
        _OgreExport friend std::ostream& operator<< ( std::ostream& strm, const ConvexBody& body );
152
153
        /// Initialise the internal polygon pool used to minimise allocations
154
        static void _initialisePool();
155
        /// Tear down the internal polygon pool used to minimise allocations
156
        static void _destroyPool();
157
158
159
    private:
160
        /** Get a new polygon from the pool.
161
        */
162
        static Polygon* allocatePolygon();
163
        /** Release a polygon back tot he pool. */
164
        static void freePolygon(Polygon* poly);
165
        /** Inserts a polygon at a particular point in the body.
166
        @note
167
            After this method is called, the ConvexBody 'owns' this Polygon
168
            and will be responsible for deleting it.
169
        */
170
        void insertPolygon(Polygon* pdata, size_t poly);
171
        /** Inserts a polygon at the end.
172
        @note
173
            After this method is called, the ConvexBody 'owns' this Polygon
174
            and will be responsible for deleting it.
175
        */
176
        void insertPolygon(Polygon* pdata);
177
178
        /** Inserts a vertex for a polygon at a particular point.
179
        @note
180
            No checks are done whether the assembled polygon is (still) planar, 
181
            the caller must ensure that this is the case.
182
        */
183
        void insertVertex(size_t poly, const Vector3& vdata, size_t vertex);
184
        /** Inserts a vertex for a polygon at the end.
185
        @note
186
            No checks are done whether the assembled polygon is (still) planar, 
187
            the caller must ensure that this is the case.
188
        */
189
        void insertVertex(size_t poly, const Vector3& vdata);
190
        /** Deletes a specific polygon.
191
        */
192
        void deletePolygon(size_t poly);
193
194
        /** Removes a specific polygon from the body without deleting it.
195
        @note
196
            The retrieved polygon needs to be deleted later by the caller.
197
        */
198
        Polygon* unlinkPolygon(size_t poly);
199
200
        /** Moves all polygons from the parameter body to this instance.
201
        @note Both the passed in object and this instance are modified
202
        */
203
        void moveDataFromBody(ConvexBody& body);
204
205
        /** Deletes a specific vertex of a specific polygon.
206
        */
207
        void deleteVertex(size_t poly, size_t vertex);
208
209
        /** Replace a polygon at a particular index.
210
        @note Again, the passed in polygon is owned by this object after this
211
            call returns, and this object is resonsible for deleting it.
212
        */
213
        void setPolygon(Polygon* pdata, size_t poly );
214
215
        /** Replace a specific vertex of a polygon.
216
        @note
217
            No checks are done whether the assembled polygon is (still) planar, 
218
            the caller must ensure that this is the case.
219
        */
220
        void setVertex( size_t poly, const Vector3& vdata, size_t vertex );
221
222
        /** Returns the single edges in an EdgeMap (= edges where one side is a vertex and the
223
            other is empty space (a hole in the body)).
224
        */
225
        Polygon::EdgeMap getSingleEdges() const;
226
227
        /** Stores the edges of a specific polygon in a passed in structure.
228
        */
229
        void storeEdgesOfPolygon(size_t poly, Polygon::EdgeMap *edgeMap) const;
230
            
231
        /** Allocates space for an specified amount of polygons with
232
            each of them having a specified number of vertices.
233
            @note
234
                Old data (if available) will be erased.
235
        */
236
        void allocateSpace(size_t numPolygons, size_t numVertices);
237
238
        /** Searches for a pair (an edge) in the intersectionList with an entry
239
            that equals vec, and removes it from the passed in list.
240
        @param vec The vertex to search for in intersectionEdges
241
        @param intersectionEdges A list of edges, which is updated if a match is found
242
        @param vNext A reference to a vector which will be filled with the other
243
            vertex at the matching edge, if found.
244
        @return True if a match was found
245
        */
246
        bool findAndEraseEdgePair(const Vector3& vec, 
247
            Polygon::EdgeMap& intersectionEdges, Vector3& vNext ) const;
248
249
    };
250
    /** @} */
251
    /** @} */
252
253
}
254
255
#include "OgreHeaderSuffix.h"
256
257
#endif 
258