Pyrogenesis  trunk
Brush.h
Go to the documentation of this file.
1 /* Copyright (C) 2021 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 <vector>
28 
30 class CFrustum;
31 class CPlane;
32 
33 
34 /**
35  * Class CBrush: Represents a convex object, supports some CSG operations.
36  */
37 class CBrush
38 {
39 public:
40  CBrush();
41 
42  /**
43  * CBrush: Construct a brush from a bounds object.
44  *
45  * @param bounds the CBoundingBoxAligned object to construct the brush from.
46  */
47  CBrush(const CBoundingBoxAligned& bounds);
48 
49  /**
50  * IsEmpty: Returns whether the brush is empty.
51  *
52  * @return @c true if the brush is empty, @c false otherwise
53  */
54  bool IsEmpty() const { return m_Vertices.size() == 0; }
55 
56  /**
57  * Bounds: Calculate the axis-aligned bounding box for this brush.
58  *
59  * @param result the resulting bounding box is stored here
60  */
61  void Bounds(CBoundingBoxAligned& result) const;
62 
63  /**
64  * Slice: Cut the object along the given plane, resulting in a smaller (or even empty) brush representing
65  * the part of the object that lies in front of the plane (as defined by the positive direction of its
66  * normal vector).
67  *
68  * @param plane the slicing plane
69  * @param result the resulting brush is stored here
70  */
71  void Slice(const CPlane& plane, CBrush& result) const;
72 
73  /**
74  * Intersect: Intersect the brush with the given frustum.
75  *
76  * @param frustum the frustum to intersect with
77  * @param result the resulting brush is stored here
78  */
79  void Intersect(const CFrustum& frustum, CBrush& result) const;
80 
81  /**
82  * Returns vertices in the brush. Intended for testing purposes; you should not need to use
83  * this method directly.
84  */
85  const std::vector<CVector3D>& GetVertices() const;
86 
87  /**
88  * Writes a vector of the faces in this brush to @p out. Each face is itself a vector, listing the vertex indices
89  * that make up the face, starting and ending with the same index. Intended for testing purposes; you should not
90  * need to use this method directly.
91  */
92  void GetFaces(std::vector<std::vector<size_t>>& out) const;
93 
94 private:
95  static const size_t NO_VERTEX = ~0u;
96 
97  typedef std::vector<CVector3D> Vertices;
98  typedef std::vector<size_t> FaceIndices;
99 
100  /// Collection of unique vertices that make up this shape.
101  Vertices m_Vertices;
102 
103  /**
104  * Holds the face definitions of this brush. Each face is a sequence of indices into m_Vertices that starts and ends with
105  * the same vertex index, completing a loop through all the vertices that make up the face. This vector holds all the face
106  * sequences back-to-back, thus looking something like 'x---xy--------yz--z' in the general case.
107  */
108  FaceIndices m_Faces;
109 
110  struct Helper;
111 };
112 
113 #endif // INCLUDED_BRUSH
static void out(const wchar_t *fmt,...)
Definition: wdbg_sym.cpp:421
Definition: Frustum.h:36
std::vector< CVector3D > Vertices
Definition: Brush.h:97
void GetFaces(std::vector< std::vector< size_t >> &out) const
Writes a vector of the faces in this brush to out.
Definition: Brush.cpp:382
static const size_t NO_VERTEX
Definition: Brush.h:95
Definition: Brush.cpp:115
Class CBrush: Represents a convex object, supports some CSG operations.
Definition: Brush.h:37
void Slice(const CPlane &plane, CBrush &result) const
Slice: Cut the object along the given plane, resulting in a smaller (or even empty) brush representin...
Definition: Brush.cpp:186
Vertices m_Vertices
Collection of unique vertices that make up this shape.
Definition: Brush.h:101
FaceIndices m_Faces
Holds the face definitions of this brush.
Definition: Brush.h:108
Definition: BoundingBoxAligned.h:33
Definition: Plane.h:38
void Bounds(CBoundingBoxAligned &result) const
Bounds: Calculate the axis-aligned bounding box for this brush.
Definition: Brush.cpp:57
std::vector< size_t > FaceIndices
Definition: Brush.h:98
bool IsEmpty() const
IsEmpty: Returns whether the brush is empty.
Definition: Brush.h:54
const std::vector< CVector3D > & GetVertices() const
Returns vertices in the brush.
Definition: Brush.cpp:377
void Intersect(const CFrustum &frustum, CBrush &result) const
Intersect: Intersect the brush with the given frustum.
Definition: Brush.cpp:342