AGX Dynamics 2.38.0.2
Loading...
Searching...
No Matches
BoxBoxOverlapTest.h
Go to the documentation of this file.
1/*
2Copyright 2007-2024. Algoryx Simulation AB.
3
4All AGX source code, intellectual property, documentation, sample code,
5tutorials, scene files and technical white papers, are copyrighted, proprietary
6and confidential material of Algoryx Simulation AB. You may not download, read,
7store, distribute, publish, copy or otherwise disseminate, use or expose this
8material unless having a written signed agreement with Algoryx Simulation AB, or having been
9advised so by Algoryx Simulation AB for a time limited evaluation, or having purchased a
10valid commercial license from Algoryx Simulation AB.
11
12Algoryx Simulation AB disclaims all responsibilities for loss or damage caused
13from using this software, unless otherwise stated in written agreements with
14Algoryx Simulation AB.
15*/
16
17#ifndef AGX_PHYSICS_HIERARCHICALGRID_BOXBOXOVERLAP_TEST_H
18#define AGX_PHYSICS_HIERARCHICALGRID_BOXBOXOVERLAP_TEST_H
19
20
21#include <agxCollide/Box.h>
24
25
26/*
27
28This file is pretty much a copy of the box-box collider, but with the actual
29contact generation removed, and instead returns a bool telling if any contacts
30would have been created by the full box-box collider. I have not been able to
31fully convince myself that it produces the correct result in all cases, see
32the \todo near the end of this file.
33
34*/
35
36
37namespace
38{
39
40 AGX_FORCE_INLINE bool calculateDepthEdgeProv ( const agx::Vec3& he1, const agx::Vec3& he2,
41 const agx::Matrix3x3& trans2To1Abs,
42 const agx::AffineMatrix4x4& trans2To1,
43 const agx::Vec3& midP2In1,
44 const int i, const int iIncr1, const int iIncr2,
45 const int j, const int jIncr1, const int jIncr2,
46 bool& testEdges, agx::Real& depth )
47 {
48 const agx::Real length2 = trans2To1Abs(j,iIncr1) * trans2To1Abs(j,iIncr1) + trans2To1Abs(j,iIncr2) * trans2To1Abs(j,iIncr2); // squared length, no square root yet, will do this later
49 if (agx::equalsZero( length2, agx::Real(1.0e-8) )) {
50 // should not get here, numerical issues? Drop this axis, since it's nearly parallel.
51 testEdges = false;
52 return true;
53 }
54 depth = (he1[iIncr1] * trans2To1Abs(j, iIncr2) + he1[iIncr2] * trans2To1Abs(j, iIncr1) +
55 he2[jIncr1] * trans2To1Abs(jIncr2, i) + he2[jIncr2] * trans2To1Abs(jIncr1, i) -
56 std::abs( midP2In1[iIncr2] * trans2To1(j, iIncr1) - midP2In1[iIncr1] * trans2To1(j, iIncr2)) );
57
58 return (depth >= 0); // return false if we found a separating axis
59 }
60
61
62
63 AGX_FORCE_INLINE void calculateDepthEdge ( const agx::AffineMatrix4x4& trans2To1,
64 const int i, const int iIncr1, const int iIncr2, const int j,
65 const agx::Real depth, int& depthIndex, agx::Real& minDepth)
66 {
67 // Now we calculate the true length.
68 // Delaying the square root until when it is needed.
69 const agx::Real length2 = trans2To1(j, iIncr1) * trans2To1(j, iIncr1) + trans2To1(j, iIncr2) * trans2To1(j, iIncr2);
70 // Avoiding the division by multiplying both sides of the inequality with a positive number.
71 if (depth * depth < minDepth * minDepth * length2) {
72 const int dim = 3 * i + j;
73 minDepth = depth / std::sqrt(length2);
74 depthIndex = 6 + dim;
75 }
76 }
77
78
79
80
81
82 bool boxBoxOverlapTest
83 (
84 const agx::Vec3& he1, const agx::Vec3& he2,
85 const agx::AffineMatrix4x4& trans1Inv, const agx::AffineMatrix4x4& trans2Inv
86 )
87 {
88 const agx::Real epsilon = 2 * (he1[0] + he1[1] + he1[2] + he2[0] + he2[1] + he2[2]) * agx::Real(agx::RealEpsilon); // epsilon for numerical comparisons, adapted to problem's size
89
90 const agx::AffineMatrix4x4 trans2To1 = trans2Inv.inverse() * trans1Inv;
91 const agx::Vec3 midP2In1 = trans2Inv.getInvTranslate() * trans1Inv;
92
93 agx::Matrix3x3 trans2To1Abs(trans2To1 );
94 for (int i = 0; i < 3; ++i)
95 for (int j = 0; j < 3; ++j)
96 trans2To1Abs( i, j ) = std::abs( trans2To1Abs( i, j ) );
97
98 // Looking for separating axes.
99
100 //
101 // First, test against box1's three coordinate axes.
102 //
103
104 agx::Real minDepth = agx::Real(agx::RealMax);
105 int depthIndex = 0;
107
108 // Test against first box
109 for (unsigned i = 0; i < 3; ++i) {
110 const agx::Real depth = he1[i] + he2 * agx::Vec3( trans2To1Abs( 0, i ), trans2To1Abs( 1, i ), trans2To1Abs( 2, i )) - std::abs( midP2In1[i] );
111 if (depth < 0) // found a separating axis, exit!
112 return false;
113 depthsFace.push_back(depth);
114 }
115
116
117 //
118 // Check if we are in a parallel case by looking at the rotation part of transformation matrix from shape2's frame to shape1's (trans2To1).
119 // We count the number of almost-1 diagonal elements std::abs values.
120 // If we have 1, we have 1 parallelism. This means we are in a 2D-case with additional testing required in the dimension they are parallel in.
121 // This means testing in 5 directions, but 6 won't hurt either.
122 // If we have 3 (since 2 are mathematically not possible, only due to numerical errors), the boxes have identical orientation (apart from 90 degree rotations).
123 // It is enough to test for the first box's three axes.
124 //
125
126 int numParallels = 0;
127 agx::Real maxRotError2 = 0;
128 for (int i = 0; i < 3; ++i) {
129 agx::Real length2[2] = {0, 0};
130 for (int j = 0; j < 3; ++j) {
131 length2[0] += trans2To1(i, j) * trans2To1(i, j);
132 length2[1] += trans2To1(j, i) * trans2To1(j, i);
133 }
134 for (int j = 0; j < 2; ++j) {
135 if (std::abs(length2[j] - 1) > maxRotError2)
136 maxRotError2 = std::abs(length2[j] - 1);
137 }
138 }
139
140 agx::Real rotationError = epsilon;
141 if (maxRotError2 > rotationError)
142 rotationError = std::sqrt( maxRotError2 );
143 for (int i = 0; i < 3; ++i) {
144 if (trans2To1Abs( i, i ) > 1.0 - 0.0001 + rotationError) { // almost parallel to coordinate axis
145 numParallels++;
146 }
147 }
148
149 //
150 // Then, test the up to 12 other axes for separation, and exit if found. If no separation is found, record the least overlap depth and the corresponding axis.
151 // The tests are taken from Christer Ericson, agx::Real Time Collision Detection, 2005, p.101-106.
152 //
153 const agx::Vec3 midP1In2 = trans1Inv.getInvTranslate() * trans2Inv;
154
155 if (numParallels < 2) {
156 // Test against second box
157 for (unsigned i = 0; i < 3; ++i) {
158 const agx::Real depth = he2[i] + he1 * agx::Vec3( trans2To1Abs( i, 0 ), trans2To1Abs( i, 1 ), trans2To1Abs( i, 2 )) - std::abs( midP1In2[i] );
159 if (depth < 0) // found a separating axis, exit!
160 return false;
161 depthsFace.push_back(depth);
162 }
163 }
164
165 for (unsigned int i = 0; i < depthsFace.size(); ++i) {
166 if (depthsFace[i] < minDepth) {
167 minDepth = depthsFace[i];
168 depthIndex = i;
169 }
170 }
171 // int depthIndexFace = depthIndex;
172 // agx::Real minDepthFace = minDepth;
173
174
175 if (numParallels < 1) {
176 // test against 9 cross products
177 // The test given in Christer Ericson's book does not give the true length, we have to normalize.
178 // Since the matrix has column-and row norm 1 in all columns and rows, we can use the same normalizing factor
179 // since the column and row defined by removing element i,j have the same norm.
180 bool testEdges = true;
181
182 agx::Real depths[3][3];
183 memset(depths, 0, sizeof(agx::Real)*9 );
184
185 if (!calculateDepthEdgeProv( he1, he2, trans2To1Abs, trans2To1, midP2In1, 0, 1, 2, 0, 1, 2, testEdges, depths[0][0] )) return false;
186 if (!calculateDepthEdgeProv( he1, he2, trans2To1Abs, trans2To1, midP2In1, 0, 1, 2, 1, 2, 0, testEdges, depths[0][1] )) return false;
187 if (!calculateDepthEdgeProv( he1, he2, trans2To1Abs, trans2To1, midP2In1, 0, 1, 2, 2, 0, 1, testEdges, depths[0][2] )) return false;
188 if (!calculateDepthEdgeProv( he1, he2, trans2To1Abs, trans2To1, midP2In1, 1, 2, 0, 0, 1, 2, testEdges, depths[1][0] )) return false;
189 if (!calculateDepthEdgeProv( he1, he2, trans2To1Abs, trans2To1, midP2In1, 1, 2, 0, 1, 2, 0, testEdges, depths[1][1] )) return false;
190 if (!calculateDepthEdgeProv( he1, he2, trans2To1Abs, trans2To1, midP2In1, 1, 2, 0, 2, 0, 1, testEdges, depths[1][2] )) return false;
191 if (!calculateDepthEdgeProv( he1, he2, trans2To1Abs, trans2To1, midP2In1, 2, 0, 1, 0, 1, 2, testEdges, depths[2][0] )) return false;
192 if (!calculateDepthEdgeProv( he1, he2, trans2To1Abs, trans2To1, midP2In1, 2, 0, 1, 1, 2, 0, testEdges, depths[2][1] )) return false;
193 if (!calculateDepthEdgeProv( he1, he2, trans2To1Abs, trans2To1, midP2In1, 2, 0, 1, 2, 0, 1, testEdges, depths[2][2] )) return false;
194
195
196 // Make normalization step only now, since overlap testing does not need a normalized depth calculation.
197 // This means that in case the cross products would yield a separating axis, the effort for exact depth computation
198 // (9 square roots and 9 divisions) would have been wasted.
199 // Do it now instead since we know now that we are in overlap.
200 // Also, since normalization can only increase the length of a vector (created from a vector with length one by setting one component to zero),
201 // do not normalize if the unnormalized length is already larger than the minimum depth.
202 if (testEdges) {
203 calculateDepthEdge( trans2To1, 0, 1, 2, 0, depths[0][0], depthIndex, minDepth );
204 calculateDepthEdge( trans2To1, 0, 1, 2, 1, depths[0][1], depthIndex, minDepth );
205 calculateDepthEdge( trans2To1, 0, 1, 2, 2, depths[0][2], depthIndex, minDepth );
206 calculateDepthEdge( trans2To1, 1, 2, 0, 0, depths[1][0], depthIndex, minDepth );
207 calculateDepthEdge( trans2To1, 1, 2, 0, 1, depths[1][1], depthIndex, minDepth );
208 calculateDepthEdge( trans2To1, 1, 2, 0, 2, depths[1][2], depthIndex, minDepth );
209 calculateDepthEdge( trans2To1, 2, 0, 1, 0, depths[2][0], depthIndex, minDepth );
210 calculateDepthEdge( trans2To1, 2, 0, 1, 1, depths[2][1], depthIndex, minDepth );
211 calculateDepthEdge( trans2To1, 2, 0, 1, 2, depths[2][2], depthIndex, minDepth );
212 }
213 }
214
216 return true;
217
218 #if 0
219 //
220 // Since the test has not exited yet, we must have an overlap.
221 // We have information about the depth (but will recalculate it for each created contact point individually),
222 // and know how to calculate the contact normal.
223 // For edge-edge contacts, we will create 1 contact point, for face contacts, 1 to 8.
224 //
225
226 if (depthIndex > 5) {
227 if (createEdgeContact) {
228 ShapeCollider::addContactPoint( result, (closestPoints.pointOn1 + closestPoints.pointOn2) * agx::Real(0.5), (trans1.transform3x3( normalIn1 )), minDepth );
229 }
230 else {
231 // create face contact
232 depthIndex = depthIndexFace;
233 if (depthIndex < 3)
234 solveFaceCaseAgX( depthIndex, -1, signIn1, trans1, trans2To1, trans1To2, he1, he2, epsilon, result );
235 else
236 solveFaceCaseAgX( depthIndex - 3, 1, signIn2, trans2, trans1To2, trans2To1, he2, he1, epsilon, result );
237 }
238 }
239 else {
240 // create face contact
241 depthIndex = depthIndexFace;
242 if (depthIndex < 3)
243 solveFaceCaseAgX( depthIndex, -1, sign(midP2In1[depthIndex]), trans1, trans2To1, trans1To2, he1, he2, epsilon, result );
244 else
245 solveFaceCaseAgX( depthIndex - 3, 1, sign(midP1In2[depthIndex - 3]), trans2, trans1To2, trans2To1, he2, he1, epsilon, result );
246 }
247 #endif
248 }
249
250
251} // Anonymous namespace.
252
253#endif
254
Vec3T< T > getInvTranslate() const
AffineMatrix4x4T< T > inverse() const
Quick inverse, transpose rotation part, and change sign of translation part.
Templated stack array class.
size_t size() const
Get the size of the array (number of filled slots).
void push_back(const T &value)
#define AGX_FORCE_INLINE
Definition macros.h:58
T sign(T v)
Definition Math.h:328
Vec3T< Real > Vec3
The object holding 3 dimensional vectors and providing basic arithmetic.
Definition agx/Vec3.h:36
AGXCORE_EXPORT const Real RealMax
AGXCORE_EXPORT const Real RealEpsilon
AGXPHYSICS_EXPORT agx::Bool equalsZero(const agx::AddedMassInteraction::Matrix6x6 &matrix, agx::Real eps=agx::RealEpsilon)
double Real
Definition Real.h:42