Newton Dynamics  4.00
ndIntersections.h
1 /* Copyright (c) <2003-2022> <Julio Jerez, Newton Game Dynamics>
2 *
3 * This software is provided 'as-is', without any express or implied
4 * warranty. In no event will the authors be held liable for any damages
5 * arising from the use of this software.
6 *
7 * Permission is granted to anyone to use this software for any purpose,
8 * including commercial applications, and to alter it and redistribute it
9 * freely, subject to the following restrictions:
10 *
11 * 1. The origin of this software must not be misrepresented; you must not
12 * claim that you wrote the original software. If you use this software
13 * in a product, an acknowledgment in the product documentation would be
14 * appreciated but is not required.
15 *
16 * 2. Altered source versions must be plainly marked as such, and must not be
17 * misrepresented as being the original software.
18 *
19 * 3. This notice may not be removed or altered from any source distribution.
20 */
21 
22 #ifndef __ND_INTERSECTION_H__
23 #define __ND_INTERSECTION_H__
24 
25 #include "ndCoreStdafx.h"
26 #include "ndDebug.h"
27 #include "ndVector.h"
28 #include "ndMatrix.h"
29 
30 class ndPlane;
31 class ndFastRay;
32 
33 D_CORE_API ndBigVector ndPointToRayDistance(const ndBigVector& point, const ndBigVector& ray_p0, const ndBigVector& ray_p1);
34 D_CORE_API ndBigVector ndPointToTriangleDistance(const ndBigVector& point, const ndBigVector& p0, const ndBigVector& p1, const ndBigVector& p2);
35 D_CORE_API ndBigVector ndPointToTetrahedrumDistance(const ndBigVector& point, const ndBigVector& p0, const ndBigVector& p1, const ndBigVector& p2, const ndBigVector& p3);
36 D_CORE_API ndBigVector ndPointToPolygonDistance(const ndBigVector& point, const ndBigVector* const points, ndInt32 vertexCount);
37 
38 D_CORE_API void ndRayToRayDistance(const ndBigVector& ray_p0, const ndBigVector& ray_p1, const ndBigVector& ray_q0, const ndBigVector& ray_q1, ndBigVector& p0Out, ndBigVector& p1Out);
39 D_CORE_API void ndRayToPolygonDistance(const ndBigVector& ray_p0, const ndBigVector& ray_p1, const ndBigVector* const points, ndInt32 vertexCount, ndBigVector& p0Out, ndBigVector& p1Out);
40 
41 D_CORE_API bool ndRayBoxClip (ndVector& ray_p0, ndVector& ray_p1, const ndVector& boxP0, const ndVector& boxP1);
42 D_CORE_API ndFloat32 ndRayCastBox (const ndVector& p0, const ndVector& p1, const ndVector& boxP0, const ndVector& boxP1, ndVector& normalOut);
43 D_CORE_API ndFloat32 ndRayCastSphere (const ndVector& p0, const ndVector& p1, const ndVector& origin, ndFloat32 radius);
44 
45 inline ndInt32 ndOverlapTest (const ndVector& p0, const ndVector& p1, const ndVector& q0, const ndVector& q1)
46 {
47  ndVector r0(p0 - q1);
48  ndVector r1(p1 - q0);
49  ndVector val(r0 * r1);
50  ndInt32 mask = val.GetSignMask() & 0x07;
51  return (mask == 0x07);
52 }
53 
54 inline ndInt32 ndBoxInclusionTest (const ndVector& p0, const ndVector& p1, const ndVector& q0, const ndVector& q1)
55 {
56  ndVector val(ndVector::m_negOne & ((p0 >= q0) & (p1 <= q1)));
57  ndInt32 mask = val.GetSignMask() & 0x07;
58  return (mask == 0x07);
59 }
60 
61 inline ndInt32 ndCompareBox (const ndVector& p0, const ndVector& p1, const ndVector& q0, const ndVector& q1)
62 {
63  ndAssert(0);
64  return (p0.m_x != q0.m_x) || (p0.m_y != q0.m_y) || (p0.m_z != q0.m_z) || (p1.m_x != q1.m_x) || (p1.m_y != q1.m_y) || (p1.m_z != q1.m_z);
65 }
66 
67 inline void ndMovingAABB (ndVector& p0, ndVector& p1, const ndVector& veloc, const ndVector& omega, ndFloat32 timestep, ndFloat32 maxRadius, ndFloat32 minRadius)
68 {
69  ndVector linearStep (veloc.Scale (timestep));
70 
71  // estimate the maximum effect of the angular velocity and enlarge that box by that value (use 45 degrees as max angle not 90)
72  ndAssert (omega.m_w == ndFloat32 (0.0f));
73  ndFloat32 maxAngle = ndMin (ndSqrt (omega.DotProduct(omega).GetScalar() * timestep * timestep), ndFloat32 (45.0f * ndDegreeToRad));
74 
75  ndFloat32 angularTravel = (maxRadius - minRadius) * maxAngle;
76  ndVector angularStep (angularTravel, angularTravel, angularTravel, ndFloat32 (0.0f));
77 
78  ndVector r0 (p0 - angularStep);
79  ndVector r1 (p1 + angularStep);
80  ndVector q0 (r0 + linearStep);
81  ndVector q1 (r1 + linearStep);
82  p0 = r0.GetMin (q0) & ndVector::m_triplexMask;
83  p1 = r1.GetMax (q1) & ndVector::m_triplexMask;
84 }
85 
86 //inline ndFloat32 ndBoxPenetration (const ndVector& minBox, const ndVector& maxBox)
87 //{
88 // ndAssert(maxBox.m_x >= minBox.m_x);
89 // ndAssert(maxBox.m_y >= minBox.m_y);
90 // ndAssert(maxBox.m_z >= minBox.m_z);
91 //
92 // ndVector mask ((minBox * maxBox) < ndVector::m_zero);
93 // ndVector dist (maxBox.GetMin (minBox.Abs()) & mask);
94 // dist = dist.GetMin(dist.ShiftTripleRight());
95 // dist = dist.GetMin(dist.ShiftTripleRight());
96 // return dist.GetScalar();
97 //}
98 
99 inline ndFloat32 ndBoxDistanceToOrigin2 (const ndVector& minBox, const ndVector& maxBox)
100 {
101  ndAssert(maxBox.m_x >= minBox.m_x);
102  ndAssert(maxBox.m_y >= minBox.m_y);
103  ndAssert(maxBox.m_z >= minBox.m_z);
104  const ndVector mask (((minBox * maxBox) > ndVector::m_zero) & ndVector::m_triplexMask);
105  const ndVector dist (maxBox.Abs().GetMin (minBox.Abs()) & mask);
106  return dist.DotProduct(dist).GetScalar();
107 }
108 
109 #endif
110 
ndPlane
Definition: ndPlane.h:35
ndFastRay
Definition: ndFastRay.h:48
ndBigVector
Definition: ndVectorArmNeon.h:463
ndVector
Definition: ndVectorArmNeon.h:41