1 /*
2 ===========================================================================
3
4 Doom 3 GPL Source Code
5 Copyright (C) 1999-2011 id Software LLC, a ZeniMax Media company.
6
7 This file is part of the Doom 3 GPL Source Code ("Doom 3 Source Code").
8
9 Doom 3 Source Code is free software: you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation, either version 3 of the License, or
12 (at your option) any later version.
13
14 Doom 3 Source Code is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
18
19 You should have received a copy of the GNU General Public License
20 along with Doom 3 Source Code. If not, see <http://www.gnu.org/licenses/>.
21
22 In addition, the Doom 3 Source Code is also subject to certain additional terms. You should have received a copy of these additional terms immediately following the terms and conditions of the GNU General Public License which accompanied the Doom 3 Source Code. If not, please request a copy in writing from id Software at the address below.
23
24 If you have questions concerning this license or the applicable additional terms, you may contact in writing id Software LLC, c/o ZeniMax Media Inc., Suite 120, Rockville, Maryland 20850 USA.
25
26 ===========================================================================
27 */
28
29 #ifndef __TRACEMODEL_H__
30 #define __TRACEMODEL_H__
31
32 #include "idlib/geometry/Winding.h"
33 #include "idlib/math/Vector.h"
34 #include "idlib/bv/Bounds.h"
35
36 /*
37 ===============================================================================
38
39 A trace model is an arbitrary polygonal model which is used by the
40 collision detection system to find collisions, contacts or the contents
41 of a volume. For collision detection speed reasons the number of vertices
42 and edges are limited. The trace model can have any shape. However convex
43 models are usually preferred.
44
45 ===============================================================================
46 */
47
48 class idVec3;
49 class idMat3;
50 class idBounds;
51
52 // trace model type
53 typedef enum {
54 TRM_INVALID, // invalid trm
55 TRM_BOX, // box
56 TRM_OCTAHEDRON, // octahedron
57 TRM_DODECAHEDRON, // dodecahedron
58 TRM_CYLINDER, // cylinder approximation
59 TRM_CONE, // cone approximation
60 TRM_BONE, // two tetrahedrons attached to each other
61 TRM_POLYGON, // arbitrary convex polygon
62 TRM_POLYGONVOLUME, // volume for arbitrary convex polygon
63 TRM_CUSTOM // loaded from map model or ASE/LWO
64 } traceModel_t;
65
66 // these are bit cache limits
67 #define MAX_TRACEMODEL_VERTS 32
68 #define MAX_TRACEMODEL_EDGES 32
69 #define MAX_TRACEMODEL_POLYS 16
70 #define MAX_TRACEMODEL_POLYEDGES 16
71
72 typedef idVec3 traceModelVert_t;
73
74 typedef struct {
75 int v[2];
76 idVec3 normal;
77 } traceModelEdge_t;
78
79 typedef struct {
80 idVec3 normal;
81 float dist;
82 idBounds bounds;
83 int numEdges;
84 int edges[MAX_TRACEMODEL_POLYEDGES];
85 } traceModelPoly_t;
86
87 class idTraceModel {
88
89 public:
90 traceModel_t type;
91 int numVerts;
92 traceModelVert_t verts[MAX_TRACEMODEL_VERTS];
93 int numEdges;
94 traceModelEdge_t edges[MAX_TRACEMODEL_EDGES+1];
95 int numPolys;
96 traceModelPoly_t polys[MAX_TRACEMODEL_POLYS];
97 idVec3 offset; // offset to center of model
98 idBounds bounds; // bounds of model
99 bool isConvex; // true when model is convex
100
101 public:
102 idTraceModel( void );
103 // axial bounding box
104 idTraceModel( const idBounds &boxBounds );
105 // cylinder approximation
106 idTraceModel( const idBounds &cylBounds, const int numSides );
107 // bone
108 idTraceModel( const float length, const float width );
109
110 // axial box
111 void SetupBox( const idBounds &boxBounds );
112 void SetupBox( const float size );
113 // octahedron
114 void SetupOctahedron( const idBounds &octBounds );
115 void SetupOctahedron( const float size );
116 // dodecahedron
117 void SetupDodecahedron( const idBounds &dodBounds );
118 void SetupDodecahedron( const float size );
119 // cylinder approximation
120 void SetupCylinder( const idBounds &cylBounds, const int numSides );
121 void SetupCylinder( const float height, const float width, const int numSides );
122 // cone approximation
123 void SetupCone( const idBounds &coneBounds, const int numSides );
124 void SetupCone( const float height, const float width, const int numSides );
125 // two tetrahedrons attached to each other
126 void SetupBone( const float length, const float width );
127 // arbitrary convex polygon
128 void SetupPolygon( const idVec3 *v, const int count );
129 void SetupPolygon( const idWinding &w );
130 // generate edge normals
131 int GenerateEdgeNormals( void );
132 // translate the trm
133 void Translate( const idVec3 &translation );
134 // rotate the trm
135 void Rotate( const idMat3 &rotation );
136 // shrink the model m units on all sides
137 void Shrink( const float m );
138 // compare
139 bool Compare( const idTraceModel &trm ) const;
140 bool operator==( const idTraceModel &trm ) const;
141 bool operator!=( const idTraceModel &trm ) const;
142 // get the area of one of the polygons
143 float GetPolygonArea( int polyNum ) const;
144 // get the silhouette edges
145 int GetProjectionSilhouetteEdges( const idVec3 &projectionOrigin, int silEdges[MAX_TRACEMODEL_EDGES] ) const;
146 int GetParallelProjectionSilhouetteEdges( const idVec3 &projectionDir, int silEdges[MAX_TRACEMODEL_EDGES] ) const;
147 // calculate mass properties assuming an uniform density
148 void GetMassProperties( const float density, float &mass, idVec3 ¢erOfMass, idMat3 &inertiaTensor ) const;
149
150 private:
151 void InitBox( void );
152 void InitOctahedron( void );
153 void InitDodecahedron( void );
154 void InitBone( void );
155
156 void ProjectionIntegrals( int polyNum, int a, int b, struct projectionIntegrals_s &integrals ) const;
157 void PolygonIntegrals( int polyNum, int a, int b, int c, struct polygonIntegrals_s &integrals ) const;
158 void VolumeIntegrals( struct volumeIntegrals_s &integrals ) const;
159 void VolumeFromPolygon( idTraceModel &trm, float thickness ) const;
160 int GetOrderedSilhouetteEdges( const int edgeIsSilEdge[MAX_TRACEMODEL_EDGES+1], int silEdges[MAX_TRACEMODEL_EDGES] ) const;
161 };
162
163
idTraceModel(void)164 ID_INLINE idTraceModel::idTraceModel( void ) {
165 type = TRM_INVALID;
166 numVerts = numEdges = numPolys = 0;
167 bounds.Zero();
168 }
169
idTraceModel(const idBounds & boxBounds)170 ID_INLINE idTraceModel::idTraceModel( const idBounds &boxBounds ) {
171 InitBox();
172 SetupBox( boxBounds );
173 }
174
idTraceModel(const idBounds & cylBounds,const int numSides)175 ID_INLINE idTraceModel::idTraceModel( const idBounds &cylBounds, const int numSides ) {
176 SetupCylinder( cylBounds, numSides );
177 }
178
idTraceModel(const float length,const float width)179 ID_INLINE idTraceModel::idTraceModel( const float length, const float width ) {
180 InitBone();
181 SetupBone( length, width );
182 }
183
184 ID_INLINE bool idTraceModel::operator==( const idTraceModel &trm ) const {
185 return Compare( trm );
186 }
187
188 ID_INLINE bool idTraceModel::operator!=( const idTraceModel &trm ) const {
189 return !Compare( trm );
190 }
191
192 #endif /* !__TRACEMODEL_H__ */
193