Cafu Engine
TraceSolid.hpp
1 /*
2 Cafu Engine, http://www.cafu.de/
3 Copyright (c) Carsten Fuchs and other contributors.
4 This project is licensed under the terms of the MIT license.
5 */
6 
7 #ifndef CAFU_CLIPSYS_TRACESOLID_HPP_INCLUDED
8 #define CAFU_CLIPSYS_TRACESOLID_HPP_INCLUDED
9 
10 #include "Math3D/BoundingBox.hpp"
11 #include "Math3D/Matrix3x3.hpp"
12 #include "Math3D/Plane3.hpp"
13 
14 
15 namespace cf
16 {
17  namespace ClipSys
18  {
19  /// This class represents a solid object that can be traced through collision worlds, models and shapes.
20  /// The shape of the object is defined as a convex polyhedron of arbitrary complexity.
21  /// However, polyhedrons of the least possible complexity (regarding the number of vertices, planes and edges)
22  /// are strongly preferable regarding performance considerations.
23  ///
24  /// Moreover, the solids must be "well defined" without degeneracies. In particular, this means:
25  /// - there must be no duplicate or colinear vertices,
26  /// - there must be no duplicate or redundant planes, and
27  /// - the solid must have a true positive volume.
28  /// The only exception to these rules is a trace solid with exactly one vertex, no planes and no edges.
29  /// Such trace solids are treated equivalently to point traces, so that special-case optimized code is employed.
31  {
32  public:
33 
34  /// This struct describes an edge of a TraceSolidT.
35  struct EdgeT
36  {
37  unsigned int A; ///< Index of the first vertex of this edge.
38  unsigned int B; ///< Index of the second vertex of this edge.
39  };
40 
41 
42  /// The constructor.
44 
45  /// The virtual destructor.
46  virtual ~TraceSolidT() { }
47 
48  /// Returns the bounding-box of (the vertices of) this solid.
50  {
51  BoundingBox3dT BB;
52 
53  for (unsigned int i = 0; i < GetNumVertices(); i++)
54  BB += GetVertices()[i];
55 
56  return BB;
57  }
58 
59  /// Returns the number of vertices of this solid.
60  virtual unsigned int GetNumVertices() const = 0;
61 
62  /// Returns the vertices of this solid.
63  virtual const Vector3dT* GetVertices() const = 0;
64 
65  /// Returns the number of planes of this solid.
66  virtual unsigned int GetNumPlanes() const = 0;
67 
68  /// Returns the planes of this solid.
69  virtual const Plane3dT* GetPlanes() const = 0;
70 
71  /// Returns the number of edges of this solid.
72  virtual unsigned int GetNumEdges() const = 0;
73 
74  /// Returns the edges of this solid.
75  virtual const EdgeT* GetEdges() const = 0;
76  };
77 
78 
79  /// This class represents a convex solid in the shape of a point.
80  /// Traces of TracePointT solids are effectively "ray" traces, for which the
81  /// collision model implementations apply special optimizations.
82  class TracePointT : public TraceSolidT
83  {
84  public:
85 
86  /// Creates a convex solid in the shape of a point.
88 
89  // Base class overrides.
90  unsigned int GetNumVertices() const override { return 1; }
91  const Vector3dT* GetVertices() const override { return &s_Vertex; }
92  unsigned int GetNumPlanes() const override { return 0; }
93  const Plane3dT* GetPlanes() const override { return NULL; }
94  unsigned int GetNumEdges() const override { return 0; }
95  const EdgeT* GetEdges() const override { return NULL; }
96 
97 
98  private:
99 
100  const static Vector3dT s_Vertex;
101  };
102 
103 
104  /// This class represents a convex solid in the shape of a (bounding-)box.
105  class TraceBoxT : public TraceSolidT
106  {
107  public:
108 
109  /// Creates a trace solid from (in the shape of) the given axis-aligned bounding-box.
110  TraceBoxT(const BoundingBox3dT& BB);
111 
112  // Base class overrides.
113  unsigned int GetNumVertices() const override { return 8; }
114  const Vector3dT* GetVertices() const override { return m_Vertices; }
115  unsigned int GetNumPlanes() const override { return 6; }
116  const Plane3dT* GetPlanes() const override { return m_Planes; }
117  unsigned int GetNumEdges() const override { return 12; }
118  const EdgeT* GetEdges() const override { return s_Edges; }
119 
120 
121  private:
122 
123  Vector3dT m_Vertices[8];
124  Plane3dT m_Planes[6];
125 
126  const static EdgeT s_Edges[12];
127  };
128 
129 
130  /// This class represents a generic convex solid of arbitrary shape.
131  class TraceGenericT : public TraceSolidT
132  {
133  public:
134 
135  /// Creates an empty (invalid) trace model.
136  TraceGenericT();
137 
138  /// Assigns the given solid to this one, transformed by the *transpose* of the given matrix.
139  /// Compared to creating a new trace solid, this can significantly reduce or even
140  /// completely eliminate the required memory (re-)allocations.
141  void AssignInvTransformed(const TraceSolidT& Other, const math::Matrix3x3dT& Mat);
142 
143  // Base class overrides.
144  unsigned int GetNumVertices() const override { return m_Vertices.Size(); }
145  const Vector3dT* GetVertices() const override { return &m_Vertices[0]; }
146  unsigned int GetNumPlanes() const override { return m_Planes.Size(); }
147  const Plane3dT* GetPlanes() const override { return &m_Planes[0]; }
148  unsigned int GetNumEdges() const override { return m_Edges.Size(); }
149  const EdgeT* GetEdges() const override { return &m_Edges[0]; }
150 
151 
152  private:
153 
154  ArrayT<Vector3dT> m_Vertices;
155  ArrayT<Plane3dT> m_Planes;
156  ArrayT<EdgeT> m_Edges;
157  };
158  }
159 }
160 
161 #endif
unsigned int GetNumVertices() const override
Returns the number of vertices of this solid.
Definition: TraceSolid.hpp:113
TraceGenericT()
Creates an empty (invalid) trace model.
Definition: TraceSolid.cpp:47
const Vector3dT * GetVertices() const override
Returns the vertices of this solid.
Definition: TraceSolid.hpp:145
const EdgeT * GetEdges() const override
Returns the edges of this solid.
Definition: TraceSolid.hpp:149
virtual unsigned int GetNumVertices() const =0
Returns the number of vertices of this solid.
unsigned int B
Index of the second vertex of this edge.
Definition: TraceSolid.hpp:38
void AssignInvTransformed(const TraceSolidT &Other, const math::Matrix3x3dT &Mat)
Assigns the given solid to this one, transformed by the transpose of the given matrix.
Definition: TraceSolid.cpp:52
TracePointT()
Creates a convex solid in the shape of a point.
Definition: TraceSolid.hpp:87
This struct describes an edge of a TraceSolidT.
Definition: TraceSolid.hpp:35
This class represents a solid object that can be traced through collision worlds, models and shapes...
Definition: TraceSolid.hpp:30
virtual const Plane3dT * GetPlanes() const =0
Returns the planes of this solid.
unsigned long Size() const
Get size of array.
Definition: Array.hpp:138
virtual const Vector3dT * GetVertices() const =0
Returns the vertices of this solid.
const Vector3dT * GetVertices() const override
Returns the vertices of this solid.
Definition: TraceSolid.hpp:114
const Plane3dT * GetPlanes() const override
Returns the planes of this solid.
Definition: TraceSolid.hpp:116
const Plane3dT * GetPlanes() const override
Returns the planes of this solid.
Definition: TraceSolid.hpp:147
This class represents a generic convex solid of arbitrary shape.
Definition: TraceSolid.hpp:131
unsigned int GetNumPlanes() const override
Returns the number of planes of this solid.
Definition: TraceSolid.hpp:92
const EdgeT * GetEdges() const override
Returns the edges of this solid.
Definition: TraceSolid.hpp:118
virtual unsigned int GetNumPlanes() const =0
Returns the number of planes of this solid.
TraceSolidT()
The constructor.
Definition: TraceSolid.hpp:43
unsigned int A
Index of the first vertex of this edge.
Definition: TraceSolid.hpp:37
TraceBoxT(const BoundingBox3dT &BB)
Creates a trace solid from (in the shape of) the given axis-aligned bounding-box. ...
Definition: TraceSolid.cpp:16
This class represents a convex solid in the shape of a (bounding-)box.
Definition: TraceSolid.hpp:105
const Plane3dT * GetPlanes() const override
Returns the planes of this solid.
Definition: TraceSolid.hpp:93
virtual unsigned int GetNumEdges() const =0
Returns the number of edges of this solid.
unsigned int GetNumVertices() const override
Returns the number of vertices of this solid.
Definition: TraceSolid.hpp:90
unsigned int GetNumPlanes() const override
Returns the number of planes of this solid.
Definition: TraceSolid.hpp:146
unsigned int GetNumEdges() const override
Returns the number of edges of this solid.
Definition: TraceSolid.hpp:148
const EdgeT * GetEdges() const override
Returns the edges of this solid.
Definition: TraceSolid.hpp:95
virtual const EdgeT * GetEdges() const =0
Returns the edges of this solid.
This class represents a convex solid in the shape of a point.
Definition: TraceSolid.hpp:82
virtual ~TraceSolidT()
The virtual destructor.
Definition: TraceSolid.hpp:46
unsigned int GetNumPlanes() const override
Returns the number of planes of this solid.
Definition: TraceSolid.hpp:115
unsigned int GetNumEdges() const override
Returns the number of edges of this solid.
Definition: TraceSolid.hpp:94
const Vector3dT * GetVertices() const override
Returns the vertices of this solid.
Definition: TraceSolid.hpp:91
Definition: Renderer.hpp:16
BoundingBox3dT GetBB() const
Returns the bounding-box of (the vertices of) this solid.
Definition: TraceSolid.hpp:49
unsigned int GetNumVertices() const override
Returns the number of vertices of this solid.
Definition: TraceSolid.hpp:144
unsigned int GetNumEdges() const override
Returns the number of edges of this solid.
Definition: TraceSolid.hpp:117