Skip to content

Commit 409c3a3

Browse files
committed
DirectXMath 3.02
1 parent 1fa826b commit 409c3a3

9 files changed

+10593
-7389
lines changed

Inc/DirectXCollision.h

+184-5
Original file line numberDiff line numberDiff line change
@@ -33,14 +33,16 @@ enum PlaneIntersectionType
3333
};
3434

3535
struct BoundingBox;
36+
struct BoundingOrientedBox;
37+
struct BoundingFrustum;
3638

3739
#pragma warning(push)
38-
#pragma warning(disable:4324)
40+
#pragma warning(disable:4324 4820)
3941

4042
//-------------------------------------------------------------------------------------
4143
// Bounding sphere
4244
//-------------------------------------------------------------------------------------
43-
__declspec(align(16)) struct BoundingSphere
45+
struct BoundingSphere
4446
{
4547
XMFLOAT3 Center; // Center of the sphere.
4648
float Radius; // Radius of the sphere.
@@ -63,10 +65,14 @@ __declspec(align(16)) struct BoundingSphere
6365
ContainmentType Contains( _In_ FXMVECTOR V0, _In_ FXMVECTOR V1, _In_ FXMVECTOR V2 ) const;
6466
ContainmentType Contains( _In_ const BoundingSphere& sh ) const;
6567
ContainmentType Contains( _In_ const BoundingBox& box ) const;
68+
ContainmentType Contains( _In_ const BoundingOrientedBox& box ) const;
69+
ContainmentType Contains( _In_ const BoundingFrustum& fr ) const;
6670

6771
bool Intersects( _In_ const BoundingSphere& sh ) const;
6872
bool Intersects( _In_ const BoundingBox& box ) const;
69-
73+
bool Intersects( _In_ const BoundingOrientedBox& box ) const;
74+
bool Intersects( _In_ const BoundingFrustum& fr ) const;
75+
7076
bool Intersects( _In_ FXMVECTOR V0, _In_ FXMVECTOR V1, _In_ FXMVECTOR V2 ) const;
7177
// Triangle-sphere test
7278

@@ -76,19 +82,26 @@ __declspec(align(16)) struct BoundingSphere
7682
bool Intersects( _In_ FXMVECTOR Origin, _In_ FXMVECTOR Direction, _Out_ float& Dist ) const;
7783
// Ray-sphere test
7884

85+
ContainmentType ContainedBy( _In_ FXMVECTOR Plane0, _In_ FXMVECTOR Plane1, _In_ FXMVECTOR Plane2,
86+
_In_ GXMVECTOR Plane3, _In_ CXMVECTOR Plane4, _In_ CXMVECTOR Plane5 ) const;
87+
// Test sphere against six planes (see BoundingFrustum::GetPlanes)
88+
7989
// Static methods
8090
static void CreateMerged( _Out_ BoundingSphere& Out, _In_ const BoundingSphere& S1, _In_ const BoundingSphere& S2 );
8191

8292
static void CreateFromBoundingBox( _Out_ BoundingSphere& Out, _In_ const BoundingBox& box );
93+
static void CreateFromBoundingBox( _Out_ BoundingSphere& Out, _In_ const BoundingOrientedBox& box );
8394

8495
static void CreateFromPoints( _Out_ BoundingSphere& Out, _In_ size_t Count,
8596
_In_reads_bytes_(sizeof(XMFLOAT3)+Stride*(Count-1)) const XMFLOAT3* pPoints, _In_ size_t Stride );
97+
98+
static void CreateFromFrustum( _Out_ BoundingSphere& Out, _In_ const BoundingFrustum& fr );
8699
};
87100

88101
//-------------------------------------------------------------------------------------
89102
// Axis-aligned bounding box
90103
//-------------------------------------------------------------------------------------
91-
__declspec(align(16)) struct BoundingBox
104+
struct BoundingBox
92105
{
93106
static const size_t CORNER_COUNT = 8;
94107

@@ -114,9 +127,13 @@ __declspec(align(16)) struct BoundingBox
114127
ContainmentType Contains( _In_ FXMVECTOR V0, _In_ FXMVECTOR V1, _In_ FXMVECTOR V2 ) const;
115128
ContainmentType Contains( _In_ const BoundingSphere& sh ) const;
116129
ContainmentType Contains( _In_ const BoundingBox& box ) const;
130+
ContainmentType Contains( _In_ const BoundingOrientedBox& box ) const;
131+
ContainmentType Contains( _In_ const BoundingFrustum& fr ) const;
117132

118133
bool Intersects( _In_ const BoundingSphere& sh ) const;
119134
bool Intersects( _In_ const BoundingBox& box ) const;
135+
bool Intersects( _In_ const BoundingOrientedBox& box ) const;
136+
bool Intersects( _In_ const BoundingFrustum& fr ) const;
120137

121138
bool Intersects( _In_ FXMVECTOR V0, _In_ FXMVECTOR V1, _In_ FXMVECTOR V2 ) const;
122139
// Triangle-Box test
@@ -127,6 +144,10 @@ __declspec(align(16)) struct BoundingBox
127144
bool Intersects( _In_ FXMVECTOR Origin, _In_ FXMVECTOR Direction, _Out_ float& Dist ) const;
128145
// Ray-Box test
129146

147+
ContainmentType ContainedBy( _In_ FXMVECTOR Plane0, _In_ FXMVECTOR Plane1, _In_ FXMVECTOR Plane2,
148+
_In_ GXMVECTOR Plane3, _In_ CXMVECTOR Plane4, _In_ CXMVECTOR Plane5 ) const;
149+
// Test box against six planes (see BoundingFrustum::GetPlanes)
150+
130151
// Static methods
131152
static void CreateMerged( _Out_ BoundingBox& Out, _In_ const BoundingBox& b1, _In_ const BoundingBox& b2 );
132153

@@ -137,6 +158,164 @@ __declspec(align(16)) struct BoundingBox
137158
_In_reads_bytes_(sizeof(XMFLOAT3)+Stride*(Count-1)) const XMFLOAT3* pPoints, _In_ size_t Stride );
138159
};
139160

161+
//-------------------------------------------------------------------------------------
162+
// Oriented bounding box
163+
//-------------------------------------------------------------------------------------
164+
struct BoundingOrientedBox
165+
{
166+
static const size_t CORNER_COUNT = 8;
167+
168+
XMFLOAT3 Center; // Center of the box.
169+
XMFLOAT3 Extents; // Distance from the center to each side.
170+
XMFLOAT4 Orientation; // Unit quaternion representing rotation (box -> world).
171+
172+
// Creators
173+
BoundingOrientedBox() : Center(0,0,0), Extents( 1.f, 1.f, 1.f ), Orientation(0,0,0, 1.f ) {}
174+
BoundingOrientedBox( _In_ const XMFLOAT3& _Center, _In_ const XMFLOAT3& _Extents, _In_ const XMFLOAT4& _Orientation )
175+
: Center(_Center), Extents(_Extents), Orientation(_Orientation)
176+
{
177+
assert(_Extents.x >= 0 && _Extents.y >= 0 && _Extents.z >= 0);
178+
}
179+
BoundingOrientedBox( _In_ const BoundingOrientedBox& box )
180+
: Center(box.Center), Extents(box.Extents), Orientation(box.Orientation) {}
181+
182+
// Methods
183+
BoundingOrientedBox& operator=( _In_ const BoundingOrientedBox& box ) { Center = box.Center; Extents = box.Extents; Orientation = box.Orientation; return *this; }
184+
185+
void Transform( _Out_ BoundingOrientedBox& Out, _In_ CXMMATRIX M ) const;
186+
void Transform( _Out_ BoundingOrientedBox& Out, _In_ float Scale, _In_ FXMVECTOR Rotation, _In_ FXMVECTOR Translation ) const;
187+
188+
void GetCorners( _Out_writes_(8) XMFLOAT3* Corners ) const;
189+
// Gets the 8 corners of the box
190+
191+
ContainmentType Contains( _In_ FXMVECTOR Point ) const;
192+
ContainmentType Contains( _In_ FXMVECTOR V0, _In_ FXMVECTOR V1, _In_ FXMVECTOR V2 ) const;
193+
ContainmentType Contains( _In_ const BoundingSphere& sh ) const;
194+
ContainmentType Contains( _In_ const BoundingBox& box ) const;
195+
ContainmentType Contains( _In_ const BoundingOrientedBox& box ) const;
196+
ContainmentType Contains( _In_ const BoundingFrustum& fr ) const;
197+
198+
bool Intersects( _In_ const BoundingSphere& sh ) const;
199+
bool Intersects( _In_ const BoundingBox& box ) const;
200+
bool Intersects( _In_ const BoundingOrientedBox& box ) const;
201+
bool Intersects( _In_ const BoundingFrustum& fr ) const;
202+
203+
bool Intersects( _In_ FXMVECTOR V0, _In_ FXMVECTOR V1, _In_ FXMVECTOR V2 ) const;
204+
// Triangle-OrientedBox test
205+
206+
PlaneIntersectionType Intersects( _In_ FXMVECTOR Plane ) const;
207+
// Plane-OrientedBox test
208+
209+
bool Intersects( _In_ FXMVECTOR Origin, _In_ FXMVECTOR Direction, _Out_ float& Dist ) const;
210+
// Ray-OrientedBox test
211+
212+
ContainmentType ContainedBy( _In_ FXMVECTOR Plane0, _In_ FXMVECTOR Plane1, _In_ FXMVECTOR Plane2,
213+
_In_ GXMVECTOR Plane3, _In_ CXMVECTOR Plane4, _In_ CXMVECTOR Plane5 ) const;
214+
// Test OrientedBox against six planes (see BoundingFrustum::GetPlanes)
215+
216+
// Static methods
217+
static void CreateFromBoundingBox( _Out_ BoundingOrientedBox& Out, _In_ const BoundingBox& box );
218+
219+
static void CreateFromPoints( _Out_ BoundingOrientedBox& Out, _In_ size_t Count,
220+
_In_reads_bytes_(sizeof(XMFLOAT3)+Stride*(Count-1)) const XMFLOAT3* pPoints, _In_ size_t Stride );
221+
};
222+
223+
//-------------------------------------------------------------------------------------
224+
// Bounding frustum
225+
//-------------------------------------------------------------------------------------
226+
struct BoundingFrustum
227+
{
228+
static const size_t CORNER_COUNT = 8;
229+
230+
XMFLOAT3 Origin; // Origin of the frustum (and projection).
231+
XMFLOAT4 Orientation; // Quaternion representing rotation.
232+
233+
float RightSlope; // Positive X slope (X/Z).
234+
float LeftSlope; // Negative X slope.
235+
float TopSlope; // Positive Y slope (Y/Z).
236+
float BottomSlope; // Negative Y slope.
237+
float Near, Far; // Z of the near plane and far plane.
238+
239+
// Creators
240+
BoundingFrustum() : Origin(0,0,0), Orientation(0,0,0, 1.f), RightSlope( 1.f ), LeftSlope( -1.f ),
241+
TopSlope( 1.f ), BottomSlope( -1.f ), Near(0), Far( 1.f ) {}
242+
BoundingFrustum( _In_ const XMFLOAT3& _Origin, _In_ const XMFLOAT4& _Orientation,
243+
_In_ float _RightSlope, _In_ float _LeftSlope, _In_ float _TopSlope, _In_ float _BottomSlope,
244+
_In_ float _Near, _In_ float _Far )
245+
: Origin(_Origin), Orientation(_Orientation),
246+
RightSlope(_RightSlope), LeftSlope(_LeftSlope), TopSlope(_TopSlope), BottomSlope(_BottomSlope),
247+
Near(_Near), Far(_Far) { assert( _Near <= _Far ); }
248+
BoundingFrustum( _In_ const BoundingFrustum& fr )
249+
: Origin(fr.Origin), Orientation(fr.Orientation), RightSlope(fr.RightSlope), LeftSlope(fr.LeftSlope),
250+
TopSlope(fr.TopSlope), BottomSlope(fr.BottomSlope), Near(fr.Near), Far(fr.Far) {}
251+
BoundingFrustum( _In_ CXMMATRIX Projection ) { CreateFromMatrix( *this, Projection ); }
252+
253+
// Methods
254+
BoundingFrustum& operator=( _In_ const BoundingFrustum& fr ) { Origin=fr.Origin; Orientation=fr.Orientation;
255+
RightSlope=fr.RightSlope; LeftSlope=fr.LeftSlope;
256+
TopSlope=fr.TopSlope; BottomSlope=fr.BottomSlope;
257+
Near=fr.Near; Far=fr.Far; return *this; }
258+
259+
void Transform( _Out_ BoundingFrustum& Out, _In_ CXMMATRIX M ) const;
260+
void Transform( _Out_ BoundingFrustum& Out, _In_ float Scale, _In_ FXMVECTOR Rotation, _In_ FXMVECTOR Translation ) const;
261+
262+
void GetCorners( _Out_writes_(8) XMFLOAT3* Corners ) const;
263+
// Gets the 8 corners of the frustum
264+
265+
ContainmentType Contains( _In_ FXMVECTOR Point ) const;
266+
ContainmentType Contains( _In_ FXMVECTOR V0, _In_ FXMVECTOR V1, _In_ FXMVECTOR V2 ) const;
267+
ContainmentType Contains( _In_ const BoundingSphere& sp ) const;
268+
ContainmentType Contains( _In_ const BoundingBox& box ) const;
269+
ContainmentType Contains( _In_ const BoundingOrientedBox& box ) const;
270+
ContainmentType Contains( _In_ const BoundingFrustum& fr ) const;
271+
// Frustum-Frustum test
272+
273+
bool Intersects( _In_ const BoundingSphere& sh ) const;
274+
bool Intersects( _In_ const BoundingBox& box ) const;
275+
bool Intersects( _In_ const BoundingOrientedBox& box ) const;
276+
bool Intersects( _In_ const BoundingFrustum& fr ) const;
277+
278+
bool Intersects( _In_ FXMVECTOR V0, _In_ FXMVECTOR V1, _In_ FXMVECTOR V2 ) const;
279+
// Triangle-Frustum test
280+
281+
PlaneIntersectionType Intersects( _In_ FXMVECTOR Plane ) const;
282+
// Plane-Frustum test
283+
284+
bool Intersects( _In_ FXMVECTOR Origin, _In_ FXMVECTOR Direction, _Out_ float& Dist ) const;
285+
// Ray-Frustum test
286+
287+
ContainmentType ContainedBy( _In_ FXMVECTOR Plane0, _In_ FXMVECTOR Plane1, _In_ FXMVECTOR Plane2,
288+
_In_ GXMVECTOR Plane3, _In_ CXMVECTOR Plane4, _In_ CXMVECTOR Plane5 ) const;
289+
// Test frustum against six planes (see BoundingFrustum::GetPlanes)
290+
291+
void GetPlanes( _Out_opt_ XMVECTOR* NearPlane, _Out_opt_ XMVECTOR* FarPlane, _Out_opt_ XMVECTOR* RightPlane,
292+
_Out_opt_ XMVECTOR* LeftPlane, _Out_opt_ XMVECTOR* TopPlane, _Out_opt_ XMVECTOR* BottomPlane ) const;
293+
// Create 6 Planes representation of Frustum
294+
295+
// Static methods
296+
static void CreateFromMatrix( _Out_ BoundingFrustum& Out, _In_ CXMMATRIX Projection );
297+
};
298+
299+
//-----------------------------------------------------------------------------
300+
// Triangle intersection testing routines.
301+
//-----------------------------------------------------------------------------
302+
namespace TriangleTests
303+
{
304+
bool Intersects( _In_ FXMVECTOR Origin, _In_ FXMVECTOR Direction, _In_ FXMVECTOR V0, _In_ GXMVECTOR V1, _In_ CXMVECTOR V2, _Out_ float& Dist );
305+
// Ray-Triangle
306+
307+
bool Intersects( _In_ FXMVECTOR A0, _In_ FXMVECTOR A1, _In_ FXMVECTOR A2, _In_ GXMVECTOR B0, _In_ CXMVECTOR B1, _In_ CXMVECTOR B2 );
308+
// Triangle-Triangle
309+
310+
PlaneIntersectionType Intersects( _In_ FXMVECTOR V0, _In_ FXMVECTOR V1, _In_ FXMVECTOR V2, _In_ GXMVECTOR Plane );
311+
// Plane-Triangle
312+
313+
ContainmentType ContainedBy( _In_ FXMVECTOR V0, _In_ FXMVECTOR V1, _In_ FXMVECTOR V2,
314+
_In_ GXMVECTOR Plane0, _In_ CXMVECTOR Plane1, _In_ CXMVECTOR Plane2,
315+
_In_ CXMVECTOR Plane3, _In_ CXMVECTOR Plane4, _In_ CXMVECTOR Plane5 );
316+
// Test a triangle against six planes at once (see BoundingFrustum::GetPlanes)
317+
};
318+
140319
#pragma warning(pop)
141320

142321
/****************************************************************************
@@ -146,7 +325,7 @@ __declspec(align(16)) struct BoundingBox
146325
****************************************************************************/
147326

148327
#pragma warning(push)
149-
#pragma warning(disable:4068)
328+
#pragma warning(disable : 4068 4616 6001)
150329

151330
#pragma prefast(push)
152331
#pragma prefast(disable : 25000, "FXMVECTOR is 16 bytes")

0 commit comments

Comments
 (0)