Line data Source code
1 : /* Copyright (C) 2025 Wildfire Games. 2 : * This file is part of 0 A.D. 3 : * 4 : * 0 A.D. is free software: you can redistribute it and/or modify 5 : * it under the terms of the GNU General Public License as published by 6 : * the Free Software Foundation, either version 2 of the License, or 7 : * (at your option) any later version. 8 : * 9 : * 0 A.D. is distributed in the hope that it will be useful, 10 : * but WITHOUT ANY WARRANTY; without even the implied warranty of 11 : * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 : * GNU General Public License for more details. 13 : * 14 : * You should have received a copy of the GNU General Public License 15 : * along with 0 A.D. If not, see <http://www.gnu.org/licenses/>. 16 : */ 17 : 18 : /* 19 : * CBrush, a class representing a convex object 20 : */ 21 : 22 : #ifndef INCLUDED_BRUSH 23 : #define INCLUDED_BRUSH 24 : 25 : #include "maths/Vector3D.h" 26 : 27 : #include <cstddef> 28 : #include <vector> 29 : 30 : class CBoundingBoxAligned; 31 : class CFrustum; 32 : class CPlane; 33 : 34 : 35 : /** 36 : * Class CBrush: Represents a convex object, supports some CSG operations. 37 : */ 38 0 : class CBrush 39 : { 40 : public: 41 : CBrush(); 42 : 43 : /** 44 : * CBrush: Construct a brush from a bounds object. 45 : * 46 : * @param bounds the CBoundingBoxAligned object to construct the brush from. 47 : */ 48 : CBrush(const CBoundingBoxAligned& bounds); 49 : 50 : /** 51 : * IsEmpty: Returns whether the brush is empty. 52 : * 53 : * @return @c true if the brush is empty, @c false otherwise 54 : */ 55 1 : bool IsEmpty() const { return m_Vertices.size() == 0; } 56 : 57 : /** 58 : * Bounds: Calculate the axis-aligned bounding box for this brush. 59 : * 60 : * @param result the resulting bounding box is stored here 61 : */ 62 : void Bounds(CBoundingBoxAligned& result) const; 63 : 64 : /** 65 : * Slice: Cut the object along the given plane, resulting in a smaller (or even empty) brush representing 66 : * the part of the object that lies in front of the plane (as defined by the positive direction of its 67 : * normal vector). 68 : * 69 : * @param plane the slicing plane 70 : * @param result the resulting brush is stored here 71 : */ 72 : void Slice(const CPlane& plane, CBrush& result) const; 73 : 74 : /** 75 : * Intersect: Intersect the brush with the given frustum. 76 : * 77 : * @param frustum the frustum to intersect with 78 : * @param result the resulting brush is stored here 79 : */ 80 : void Intersect(const CFrustum& frustum, CBrush& result) const; 81 : 82 : /** 83 : * Returns vertices in the brush. Intended for testing purposes; you should not need to use 84 : * this method directly. 85 : */ 86 : const std::vector<CVector3D>& GetVertices() const; 87 : 88 : /** 89 : * Writes a vector of the faces in this brush to @p out. Each face is itself a vector, listing the vertex indices 90 : * that make up the face, starting and ending with the same index. Intended for testing purposes; you should not 91 : * need to use this method directly. 92 : */ 93 : void GetFaces(std::vector<std::vector<size_t>>& out) const; 94 : 95 : private: 96 : static const size_t NO_VERTEX = ~0u; 97 : 98 : typedef std::vector<CVector3D> Vertices; 99 : typedef std::vector<size_t> FaceIndices; 100 : 101 : /// Collection of unique vertices that make up this shape. 102 : Vertices m_Vertices; 103 : 104 : /** 105 : * Holds the face definitions of this brush. Each face is a sequence of indices into m_Vertices that starts and ends with 106 : * the same vertex index, completing a loop through all the vertices that make up the face. This vector holds all the face 107 : * sequences back-to-back, thus looking something like 'x---xy--------yz--z' in the general case. 108 : */ 109 : FaceIndices m_Faces; 110 : 111 : struct Helper; 112 : }; 113 : 114 : #endif // INCLUDED_BRUSH