1 //this file is autogenerated using stringify.bat (premake --stringify) in the build folder of this project
2 static const char* mprKernelsCL =
4 " * ---------------------------------\n"
5 " * Copyright (c)2012 Daniel Fiser <danfis@danfis.cz>\n"
7 " * This file was ported from mpr.c file, part of libccd.\n"
8 " * The Minkoski Portal Refinement implementation was ported \n"
9 " * to OpenCL by Erwin Coumans for the Bullet 3 Physics library.\n"
10 " * at http://github.com/erwincoumans/bullet3\n"
12 " * Distributed under the OSI-approved BSD License (the \"License\");\n"
13 " * see <http://www.opensource.org/licenses/bsd-license.php>.\n"
14 " * This software is distributed WITHOUT ANY WARRANTY; without even the\n"
15 " * implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.\n"
16 " * See the License for more information.\n"
18 "#ifndef B3_MPR_PENETRATION_H\n"
19 "#define B3_MPR_PENETRATION_H\n"
20 "#ifndef B3_PLATFORM_DEFINITIONS_H\n"
21 "#define B3_PLATFORM_DEFINITIONS_H\n"
26 "#ifdef __cplusplus\n"
28 "//keep B3_LARGE_FLOAT*B3_LARGE_FLOAT < FLT_MAX\n"
29 "#define B3_LARGE_FLOAT 1e18f\n"
30 "#define B3_INFINITY 1e18f\n"
31 "#define b3Assert(a)\n"
32 "#define b3ConstArray(a) __global const a*\n"
33 "#define b3AtomicInc atomic_inc\n"
34 "#define b3AtomicAdd atomic_add\n"
35 "#define b3Fabs fabs\n"
36 "#define b3Sqrt native_sqrt\n"
37 "#define b3Sin native_sin\n"
38 "#define b3Cos native_cos\n"
42 "#ifndef B3_FLOAT4_H\n"
43 "#define B3_FLOAT4_H\n"
44 "#ifndef B3_PLATFORM_DEFINITIONS_H\n"
45 "#ifdef __cplusplus\n"
49 "#ifdef __cplusplus\n"
51 " typedef float4 b3Float4;\n"
52 " #define b3Float4ConstArg const b3Float4\n"
53 " #define b3MakeFloat4 (float4)\n"
54 " float b3Dot3F4(b3Float4ConstArg v0,b3Float4ConstArg v1)\n"
56 " float4 a1 = b3MakeFloat4(v0.xyz,0.f);\n"
57 " float4 b1 = b3MakeFloat4(v1.xyz,0.f);\n"
58 " return dot(a1, b1);\n"
60 " b3Float4 b3Cross3(b3Float4ConstArg v0,b3Float4ConstArg v1)\n"
62 " float4 a1 = b3MakeFloat4(v0.xyz,0.f);\n"
63 " float4 b1 = b3MakeFloat4(v1.xyz,0.f);\n"
64 " return cross(a1, b1);\n"
66 " #define b3MinFloat4 min\n"
67 " #define b3MaxFloat4 max\n"
68 " #define b3Normalized(a) normalize(a)\n"
71 "inline bool b3IsAlmostZero(b3Float4ConstArg v)\n"
73 " if(b3Fabs(v.x)>1e-6 || b3Fabs(v.y)>1e-6 || b3Fabs(v.z)>1e-6) \n"
77 "inline int b3MaxDot( b3Float4ConstArg vec, __global const b3Float4* vecArray, int vecLen, float* dotOut )\n"
79 " float maxDot = -B3_INFINITY;\n"
81 " int ptIndex = -1;\n"
82 " for( i = 0; i < vecLen; i++ )\n"
84 " float dot = b3Dot3F4(vecArray[i],vec);\n"
86 " if( dot > maxDot )\n"
92 " b3Assert(ptIndex>=0);\n"
97 " *dotOut = maxDot;\n"
100 "#endif //B3_FLOAT4_H\n"
101 "#ifndef B3_RIGIDBODY_DATA_H\n"
102 "#define B3_RIGIDBODY_DATA_H\n"
103 "#ifndef B3_FLOAT4_H\n"
104 "#ifdef __cplusplus\n"
107 "#endif //B3_FLOAT4_H\n"
108 "#ifndef B3_QUAT_H\n"
109 "#define B3_QUAT_H\n"
110 "#ifndef B3_PLATFORM_DEFINITIONS_H\n"
111 "#ifdef __cplusplus\n"
115 "#ifndef B3_FLOAT4_H\n"
116 "#ifdef __cplusplus\n"
119 "#endif //B3_FLOAT4_H\n"
120 "#ifdef __cplusplus\n"
122 " typedef float4 b3Quat;\n"
123 " #define b3QuatConstArg const b3Quat\n"
126 "inline float4 b3FastNormalize4(float4 v)\n"
128 " v = (float4)(v.xyz,0.f);\n"
129 " return fast_normalize(v);\n"
132 "inline b3Quat b3QuatMul(b3Quat a, b3Quat b);\n"
133 "inline b3Quat b3QuatNormalized(b3QuatConstArg in);\n"
134 "inline b3Quat b3QuatRotate(b3QuatConstArg q, b3QuatConstArg vec);\n"
135 "inline b3Quat b3QuatInvert(b3QuatConstArg q);\n"
136 "inline b3Quat b3QuatInverse(b3QuatConstArg q);\n"
137 "inline b3Quat b3QuatMul(b3QuatConstArg a, b3QuatConstArg b)\n"
140 " ans = b3Cross3( a, b );\n"
141 " ans += a.w*b+b.w*a;\n"
142 "// ans.w = a.w*b.w - (a.x*b.x+a.y*b.y+a.z*b.z);\n"
143 " ans.w = a.w*b.w - b3Dot3F4(a, b);\n"
146 "inline b3Quat b3QuatNormalized(b3QuatConstArg in)\n"
150 " //return b3FastNormalize4(in);\n"
151 " float len = native_sqrt(dot(q, q));\n"
158 " q.x = q.y = q.z = 0.f;\n"
163 "inline float4 b3QuatRotate(b3QuatConstArg q, b3QuatConstArg vec)\n"
165 " b3Quat qInv = b3QuatInvert( q );\n"
166 " float4 vcpy = vec;\n"
168 " float4 out = b3QuatMul(b3QuatMul(q,vcpy),qInv);\n"
171 "inline b3Quat b3QuatInverse(b3QuatConstArg q)\n"
173 " return (b3Quat)(-q.xyz, q.w);\n"
175 "inline b3Quat b3QuatInvert(b3QuatConstArg q)\n"
177 " return (b3Quat)(-q.xyz, q.w);\n"
179 "inline float4 b3QuatInvRotate(b3QuatConstArg q, b3QuatConstArg vec)\n"
181 " return b3QuatRotate( b3QuatInvert( q ), vec );\n"
183 "inline b3Float4 b3TransformPoint(b3Float4ConstArg point, b3Float4ConstArg translation, b3QuatConstArg orientation)\n"
185 " return b3QuatRotate( orientation, point ) + (translation);\n"
189 "#endif //B3_QUAT_H\n"
190 "#ifndef B3_MAT3x3_H\n"
191 "#define B3_MAT3x3_H\n"
192 "#ifndef B3_QUAT_H\n"
193 "#ifdef __cplusplus\n"
196 "#endif //B3_QUAT_H\n"
197 "#ifdef __cplusplus\n"
201 " b3Float4 m_row[3];\n"
203 "#define b3Mat3x3ConstArg const b3Mat3x3\n"
204 "#define b3GetRow(m,row) (m.m_row[row])\n"
205 "inline b3Mat3x3 b3QuatGetRotationMatrix(b3Quat quat)\n"
207 " b3Float4 quat2 = (b3Float4)(quat.x*quat.x, quat.y*quat.y, quat.z*quat.z, 0.f);\n"
209 " out.m_row[0].x=1-2*quat2.y-2*quat2.z;\n"
210 " out.m_row[0].y=2*quat.x*quat.y-2*quat.w*quat.z;\n"
211 " out.m_row[0].z=2*quat.x*quat.z+2*quat.w*quat.y;\n"
212 " out.m_row[0].w = 0.f;\n"
213 " out.m_row[1].x=2*quat.x*quat.y+2*quat.w*quat.z;\n"
214 " out.m_row[1].y=1-2*quat2.x-2*quat2.z;\n"
215 " out.m_row[1].z=2*quat.y*quat.z-2*quat.w*quat.x;\n"
216 " out.m_row[1].w = 0.f;\n"
217 " out.m_row[2].x=2*quat.x*quat.z-2*quat.w*quat.y;\n"
218 " out.m_row[2].y=2*quat.y*quat.z+2*quat.w*quat.x;\n"
219 " out.m_row[2].z=1-2*quat2.x-2*quat2.y;\n"
220 " out.m_row[2].w = 0.f;\n"
223 "inline b3Mat3x3 b3AbsoluteMat3x3(b3Mat3x3ConstArg matIn)\n"
226 " out.m_row[0] = fabs(matIn.m_row[0]);\n"
227 " out.m_row[1] = fabs(matIn.m_row[1]);\n"
228 " out.m_row[2] = fabs(matIn.m_row[2]);\n"
232 "b3Mat3x3 mtZero();\n"
234 "b3Mat3x3 mtIdentity();\n"
236 "b3Mat3x3 mtTranspose(b3Mat3x3 m);\n"
238 "b3Mat3x3 mtMul(b3Mat3x3 a, b3Mat3x3 b);\n"
240 "b3Float4 mtMul1(b3Mat3x3 a, b3Float4 b);\n"
242 "b3Float4 mtMul3(b3Float4 a, b3Mat3x3 b);\n"
244 "b3Mat3x3 mtZero()\n"
247 " m.m_row[0] = (b3Float4)(0.f);\n"
248 " m.m_row[1] = (b3Float4)(0.f);\n"
249 " m.m_row[2] = (b3Float4)(0.f);\n"
253 "b3Mat3x3 mtIdentity()\n"
256 " m.m_row[0] = (b3Float4)(1,0,0,0);\n"
257 " m.m_row[1] = (b3Float4)(0,1,0,0);\n"
258 " m.m_row[2] = (b3Float4)(0,0,1,0);\n"
262 "b3Mat3x3 mtTranspose(b3Mat3x3 m)\n"
265 " out.m_row[0] = (b3Float4)(m.m_row[0].x, m.m_row[1].x, m.m_row[2].x, 0.f);\n"
266 " out.m_row[1] = (b3Float4)(m.m_row[0].y, m.m_row[1].y, m.m_row[2].y, 0.f);\n"
267 " out.m_row[2] = (b3Float4)(m.m_row[0].z, m.m_row[1].z, m.m_row[2].z, 0.f);\n"
271 "b3Mat3x3 mtMul(b3Mat3x3 a, b3Mat3x3 b)\n"
273 " b3Mat3x3 transB;\n"
274 " transB = mtTranspose( b );\n"
276 " // why this doesn't run when 0ing in the for{}\n"
277 " a.m_row[0].w = 0.f;\n"
278 " a.m_row[1].w = 0.f;\n"
279 " a.m_row[2].w = 0.f;\n"
280 " for(int i=0; i<3; i++)\n"
282 "// a.m_row[i].w = 0.f;\n"
283 " ans.m_row[i].x = b3Dot3F4(a.m_row[i],transB.m_row[0]);\n"
284 " ans.m_row[i].y = b3Dot3F4(a.m_row[i],transB.m_row[1]);\n"
285 " ans.m_row[i].z = b3Dot3F4(a.m_row[i],transB.m_row[2]);\n"
286 " ans.m_row[i].w = 0.f;\n"
291 "b3Float4 mtMul1(b3Mat3x3 a, b3Float4 b)\n"
294 " ans.x = b3Dot3F4( a.m_row[0], b );\n"
295 " ans.y = b3Dot3F4( a.m_row[1], b );\n"
296 " ans.z = b3Dot3F4( a.m_row[2], b );\n"
301 "b3Float4 mtMul3(b3Float4 a, b3Mat3x3 b)\n"
303 " b3Float4 colx = b3MakeFloat4(b.m_row[0].x, b.m_row[1].x, b.m_row[2].x, 0);\n"
304 " b3Float4 coly = b3MakeFloat4(b.m_row[0].y, b.m_row[1].y, b.m_row[2].y, 0);\n"
305 " b3Float4 colz = b3MakeFloat4(b.m_row[0].z, b.m_row[1].z, b.m_row[2].z, 0);\n"
307 " ans.x = b3Dot3F4( a, colx );\n"
308 " ans.y = b3Dot3F4( a, coly );\n"
309 " ans.z = b3Dot3F4( a, colz );\n"
313 "#endif //B3_MAT3x3_H\n"
314 "typedef struct b3RigidBodyData b3RigidBodyData_t;\n"
315 "struct b3RigidBodyData\n"
319 " b3Float4 m_linVel;\n"
320 " b3Float4 m_angVel;\n"
321 " int m_collidableIdx;\n"
322 " float m_invMass;\n"
323 " float m_restituitionCoeff;\n"
324 " float m_frictionCoeff;\n"
326 "typedef struct b3InertiaData b3InertiaData_t;\n"
327 "struct b3InertiaData\n"
329 " b3Mat3x3 m_invInertiaWorld;\n"
330 " b3Mat3x3 m_initInvInertia;\n"
332 "#endif //B3_RIGIDBODY_DATA_H\n"
334 "#ifndef B3_CONVEX_POLYHEDRON_DATA_H\n"
335 "#define B3_CONVEX_POLYHEDRON_DATA_H\n"
336 "#ifndef B3_FLOAT4_H\n"
337 "#ifdef __cplusplus\n"
340 "#endif //B3_FLOAT4_H\n"
341 "#ifndef B3_QUAT_H\n"
342 "#ifdef __cplusplus\n"
345 "#endif //B3_QUAT_H\n"
346 "typedef struct b3GpuFace b3GpuFace_t;\n"
349 " b3Float4 m_plane;\n"
350 " int m_indexOffset;\n"
351 " int m_numIndices;\n"
352 " int m_unusedPadding1;\n"
353 " int m_unusedPadding2;\n"
355 "typedef struct b3ConvexPolyhedronData b3ConvexPolyhedronData_t;\n"
356 "struct b3ConvexPolyhedronData\n"
358 " b3Float4 m_localCenter;\n"
359 " b3Float4 m_extents;\n"
363 " int m_faceOffset;\n"
365 " int m_numVertices;\n"
366 " int m_vertexOffset;\n"
367 " int m_uniqueEdgesOffset;\n"
368 " int m_numUniqueEdges;\n"
371 "#endif //B3_CONVEX_POLYHEDRON_DATA_H\n"
372 "#ifndef B3_COLLIDABLE_H\n"
373 "#define B3_COLLIDABLE_H\n"
374 "#ifndef B3_FLOAT4_H\n"
375 "#ifdef __cplusplus\n"
378 "#endif //B3_FLOAT4_H\n"
379 "#ifndef B3_QUAT_H\n"
380 "#ifdef __cplusplus\n"
383 "#endif //B3_QUAT_H\n"
384 "enum b3ShapeTypes\n"
386 " SHAPE_HEIGHT_FIELD=1,\n"
387 " SHAPE_CONVEX_HULL=3,\n"
389 " SHAPE_CONCAVE_TRIMESH=5,\n"
390 " SHAPE_COMPOUND_OF_CONVEX_HULLS=6,\n"
392 " MAX_NUM_SHAPE_TYPES,\n"
394 "typedef struct b3Collidable b3Collidable_t;\n"
395 "struct b3Collidable\n"
398 " int m_numChildShapes;\n"
404 " int m_compoundBvhIndex;\n"
406 " int m_shapeType;\n"
407 " int m_shapeIndex;\n"
409 "typedef struct b3GpuChildShape b3GpuChildShape_t;\n"
410 "struct b3GpuChildShape\n"
412 " b3Float4 m_childPosition;\n"
413 " b3Quat m_childOrientation;\n"
414 " int m_shapeIndex;\n"
419 "struct b3CompoundOverlappingPair\n"
421 " int m_bodyIndexA;\n"
422 " int m_bodyIndexB;\n"
423 "// int m_pairType;\n"
424 " int m_childShapeIndexA;\n"
425 " int m_childShapeIndexB;\n"
427 "#endif //B3_COLLIDABLE_H\n"
428 "#ifdef __cplusplus\n"
430 "#define B3_MPR_SQRT sqrt\n"
432 "#define B3_MPR_FMIN(x, y) ((x) < (y) ? (x) : (y))\n"
433 "#define B3_MPR_FABS fabs\n"
434 "#define B3_MPR_TOLERANCE 1E-6f\n"
435 "#define B3_MPR_MAX_ITERATIONS 1000\n"
436 "struct _b3MprSupport_t \n"
438 " b3Float4 v; //!< Support point in minkowski sum\n"
439 " b3Float4 v1; //!< Support point in obj1\n"
440 " b3Float4 v2; //!< Support point in obj2\n"
442 "typedef struct _b3MprSupport_t b3MprSupport_t;\n"
443 "struct _b3MprSimplex_t \n"
445 " b3MprSupport_t ps[4];\n"
446 " int last; //!< index of last added point\n"
448 "typedef struct _b3MprSimplex_t b3MprSimplex_t;\n"
449 "inline b3MprSupport_t* b3MprSimplexPointW(b3MprSimplex_t *s, int idx)\n"
451 " return &s->ps[idx];\n"
453 "inline void b3MprSimplexSetSize(b3MprSimplex_t *s, int size)\n"
455 " s->last = size - 1;\n"
457 "inline int b3MprSimplexSize(const b3MprSimplex_t *s)\n"
459 " return s->last + 1;\n"
461 "inline const b3MprSupport_t* b3MprSimplexPoint(const b3MprSimplex_t* s, int idx)\n"
463 " // here is no check on boundaries\n"
464 " return &s->ps[idx];\n"
466 "inline void b3MprSupportCopy(b3MprSupport_t *d, const b3MprSupport_t *s)\n"
470 "inline void b3MprSimplexSet(b3MprSimplex_t *s, size_t pos, const b3MprSupport_t *a)\n"
472 " b3MprSupportCopy(s->ps + pos, a);\n"
474 "inline void b3MprSimplexSwap(b3MprSimplex_t *s, size_t pos1, size_t pos2)\n"
476 " b3MprSupport_t supp;\n"
477 " b3MprSupportCopy(&supp, &s->ps[pos1]);\n"
478 " b3MprSupportCopy(&s->ps[pos1], &s->ps[pos2]);\n"
479 " b3MprSupportCopy(&s->ps[pos2], &supp);\n"
481 "inline int b3MprIsZero(float val)\n"
483 " return B3_MPR_FABS(val) < FLT_EPSILON;\n"
485 "inline int b3MprEq(float _a, float _b)\n"
489 " ab = B3_MPR_FABS(_a - _b);\n"
490 " if (B3_MPR_FABS(ab) < FLT_EPSILON)\n"
492 " a = B3_MPR_FABS(_a);\n"
493 " b = B3_MPR_FABS(_b);\n"
495 " return ab < FLT_EPSILON * b;\n"
497 " return ab < FLT_EPSILON * a;\n"
500 "inline int b3MprVec3Eq(const b3Float4* a, const b3Float4 *b)\n"
502 " return b3MprEq((*a).x, (*b).x)\n"
503 " && b3MprEq((*a).y, (*b).y)\n"
504 " && b3MprEq((*a).z, (*b).z);\n"
506 "inline b3Float4 b3LocalGetSupportVertex(b3Float4ConstArg supportVec,__global const b3ConvexPolyhedronData_t* hull, b3ConstArray(b3Float4) verticesA)\n"
508 " b3Float4 supVec = b3MakeFloat4(0,0,0,0);\n"
509 " float maxDot = -B3_LARGE_FLOAT;\n"
510 " if( 0 < hull->m_numVertices )\n"
512 " const b3Float4 scaled = supportVec;\n"
513 " int index = b3MaxDot(scaled, &verticesA[hull->m_vertexOffset], hull->m_numVertices, &maxDot);\n"
514 " return verticesA[hull->m_vertexOffset+index];\n"
518 "B3_STATIC void b3MprConvexSupport(int pairIndex,int bodyIndex, b3ConstArray(b3RigidBodyData_t) cpuBodyBuf, \n"
519 " b3ConstArray(b3ConvexPolyhedronData_t) cpuConvexData, \n"
520 " b3ConstArray(b3Collidable_t) cpuCollidables,\n"
521 " b3ConstArray(b3Float4) cpuVertices,\n"
522 " __global b3Float4* sepAxis,\n"
523 " const b3Float4* _dir, b3Float4* outp, int logme)\n"
525 " //dir is in worldspace, move to local space\n"
527 " b3Float4 pos = cpuBodyBuf[bodyIndex].m_pos;\n"
528 " b3Quat orn = cpuBodyBuf[bodyIndex].m_quat;\n"
530 " b3Float4 dir = b3MakeFloat4((*_dir).x,(*_dir).y,(*_dir).z,0.f);\n"
532 " const b3Float4 localDir = b3QuatRotate(b3QuatInverse(orn),dir);\n"
534 " //find local support vertex\n"
535 " int colIndex = cpuBodyBuf[bodyIndex].m_collidableIdx;\n"
537 " b3Assert(cpuCollidables[colIndex].m_shapeType==SHAPE_CONVEX_HULL);\n"
538 " __global const b3ConvexPolyhedronData_t* hull = &cpuConvexData[cpuCollidables[colIndex].m_shapeIndex];\n"
543 " b3Float4 supVec = b3MakeFloat4(0,0,0,0);\n"
544 " float maxDot = -B3_LARGE_FLOAT;\n"
545 " if( 0 < hull->m_numVertices )\n"
547 " const b3Float4 scaled = localDir;\n"
548 " int index = b3MaxDot(scaled, &cpuVertices[hull->m_vertexOffset], hull->m_numVertices, &maxDot);\n"
549 " pInA = cpuVertices[hull->m_vertexOffset+index];\n"
554 " pInA = b3LocalGetSupportVertex(localDir,hull,cpuVertices);\n"
556 " //move vertex to world space\n"
557 " *outp = b3TransformPoint(pInA,pos,orn);\n"
560 "inline void b3MprSupport(int pairIndex,int bodyIndexA, int bodyIndexB, b3ConstArray(b3RigidBodyData_t) cpuBodyBuf, \n"
561 " b3ConstArray(b3ConvexPolyhedronData_t) cpuConvexData, \n"
562 " b3ConstArray(b3Collidable_t) cpuCollidables,\n"
563 " b3ConstArray(b3Float4) cpuVertices,\n"
564 " __global b3Float4* sepAxis,\n"
565 " const b3Float4* _dir, b3MprSupport_t *supp)\n"
569 " b3MprConvexSupport(pairIndex,bodyIndexA,cpuBodyBuf,cpuConvexData,cpuCollidables,cpuVertices,sepAxis,&dir, &supp->v1,0);\n"
570 " dir = *_dir*-1.f;\n"
571 " b3MprConvexSupport(pairIndex,bodyIndexB,cpuBodyBuf,cpuConvexData,cpuCollidables,cpuVertices,sepAxis,&dir, &supp->v2,0);\n"
572 " supp->v = supp->v1 - supp->v2;\n"
574 "inline void b3FindOrigin(int bodyIndexA, int bodyIndexB, b3ConstArray(b3RigidBodyData_t) cpuBodyBuf, b3MprSupport_t *center)\n"
576 " center->v1 = cpuBodyBuf[bodyIndexA].m_pos;\n"
577 " center->v2 = cpuBodyBuf[bodyIndexB].m_pos;\n"
578 " center->v = center->v1 - center->v2;\n"
580 "inline void b3MprVec3Set(b3Float4 *v, float x, float y, float z)\n"
587 "inline void b3MprVec3Add(b3Float4 *v, const b3Float4 *w)\n"
589 " (*v).x += (*w).x;\n"
590 " (*v).y += (*w).y;\n"
591 " (*v).z += (*w).z;\n"
593 "inline void b3MprVec3Copy(b3Float4 *v, const b3Float4 *w)\n"
597 "inline void b3MprVec3Scale(b3Float4 *d, float k)\n"
601 "inline float b3MprVec3Dot(const b3Float4 *a, const b3Float4 *b)\n"
604 " dot = b3Dot3F4(*a,*b);\n"
607 "inline float b3MprVec3Len2(const b3Float4 *v)\n"
609 " return b3MprVec3Dot(v, v);\n"
611 "inline void b3MprVec3Normalize(b3Float4 *d)\n"
613 " float k = 1.f / B3_MPR_SQRT(b3MprVec3Len2(d));\n"
614 " b3MprVec3Scale(d, k);\n"
616 "inline void b3MprVec3Cross(b3Float4 *d, const b3Float4 *a, const b3Float4 *b)\n"
618 " *d = b3Cross3(*a,*b);\n"
621 "inline void b3MprVec3Sub2(b3Float4 *d, const b3Float4 *v, const b3Float4 *w)\n"
625 "inline void b3PortalDir(const b3MprSimplex_t *portal, b3Float4 *dir)\n"
627 " b3Float4 v2v1, v3v1;\n"
628 " b3MprVec3Sub2(&v2v1, &b3MprSimplexPoint(portal, 2)->v,\n"
629 " &b3MprSimplexPoint(portal, 1)->v);\n"
630 " b3MprVec3Sub2(&v3v1, &b3MprSimplexPoint(portal, 3)->v,\n"
631 " &b3MprSimplexPoint(portal, 1)->v);\n"
632 " b3MprVec3Cross(dir, &v2v1, &v3v1);\n"
633 " b3MprVec3Normalize(dir);\n"
635 "inline int portalEncapsulesOrigin(const b3MprSimplex_t *portal,\n"
636 " const b3Float4 *dir)\n"
639 " dot = b3MprVec3Dot(dir, &b3MprSimplexPoint(portal, 1)->v);\n"
640 " return b3MprIsZero(dot) || dot > 0.f;\n"
642 "inline int portalReachTolerance(const b3MprSimplex_t *portal,\n"
643 " const b3MprSupport_t *v4,\n"
644 " const b3Float4 *dir)\n"
646 " float dv1, dv2, dv3, dv4;\n"
647 " float dot1, dot2, dot3;\n"
648 " // find the smallest dot product of dir and {v1-v4, v2-v4, v3-v4}\n"
649 " dv1 = b3MprVec3Dot(&b3MprSimplexPoint(portal, 1)->v, dir);\n"
650 " dv2 = b3MprVec3Dot(&b3MprSimplexPoint(portal, 2)->v, dir);\n"
651 " dv3 = b3MprVec3Dot(&b3MprSimplexPoint(portal, 3)->v, dir);\n"
652 " dv4 = b3MprVec3Dot(&v4->v, dir);\n"
653 " dot1 = dv4 - dv1;\n"
654 " dot2 = dv4 - dv2;\n"
655 " dot3 = dv4 - dv3;\n"
656 " dot1 = B3_MPR_FMIN(dot1, dot2);\n"
657 " dot1 = B3_MPR_FMIN(dot1, dot3);\n"
658 " return b3MprEq(dot1, B3_MPR_TOLERANCE) || dot1 < B3_MPR_TOLERANCE;\n"
660 "inline int portalCanEncapsuleOrigin(const b3MprSimplex_t *portal, \n"
661 " const b3MprSupport_t *v4,\n"
662 " const b3Float4 *dir)\n"
665 " dot = b3MprVec3Dot(&v4->v, dir);\n"
666 " return b3MprIsZero(dot) || dot > 0.f;\n"
668 "inline void b3ExpandPortal(b3MprSimplex_t *portal,\n"
669 " const b3MprSupport_t *v4)\n"
673 " b3MprVec3Cross(&v4v0, &v4->v, &b3MprSimplexPoint(portal, 0)->v);\n"
674 " dot = b3MprVec3Dot(&b3MprSimplexPoint(portal, 1)->v, &v4v0);\n"
676 " dot = b3MprVec3Dot(&b3MprSimplexPoint(portal, 2)->v, &v4v0);\n"
678 " b3MprSimplexSet(portal, 1, v4);\n"
680 " b3MprSimplexSet(portal, 3, v4);\n"
683 " dot = b3MprVec3Dot(&b3MprSimplexPoint(portal, 3)->v, &v4v0);\n"
685 " b3MprSimplexSet(portal, 2, v4);\n"
687 " b3MprSimplexSet(portal, 1, v4);\n"
691 "B3_STATIC int b3DiscoverPortal(int pairIndex, int bodyIndexA, int bodyIndexB, b3ConstArray(b3RigidBodyData_t) cpuBodyBuf, \n"
692 " b3ConstArray(b3ConvexPolyhedronData_t) cpuConvexData, \n"
693 " b3ConstArray(b3Collidable_t) cpuCollidables,\n"
694 " b3ConstArray(b3Float4) cpuVertices,\n"
695 " __global b3Float4* sepAxis,\n"
696 " __global int* hasSepAxis,\n"
697 " b3MprSimplex_t *portal)\n"
699 " b3Float4 dir, va, vb;\n"
704 " // vertex 0 is center of portal\n"
705 " b3FindOrigin(bodyIndexA,bodyIndexB,cpuBodyBuf, b3MprSimplexPointW(portal, 0));\n"
706 " // vertex 0 is center of portal\n"
707 " b3MprSimplexSetSize(portal, 1);\n"
709 " b3Float4 zero = b3MakeFloat4(0,0,0,0);\n"
710 " b3Float4* b3mpr_vec3_origin = &zero;\n"
711 " if (b3MprVec3Eq(&b3MprSimplexPoint(portal, 0)->v, b3mpr_vec3_origin)){\n"
712 " // Portal's center lies on origin (0,0,0) => we know that objects\n"
713 " // intersect but we would need to know penetration info.\n"
714 " // So move center little bit...\n"
715 " b3MprVec3Set(&va, FLT_EPSILON * 10.f, 0.f, 0.f);\n"
716 " b3MprVec3Add(&b3MprSimplexPointW(portal, 0)->v, &va);\n"
718 " // vertex 1 = support in direction of origin\n"
719 " b3MprVec3Copy(&dir, &b3MprSimplexPoint(portal, 0)->v);\n"
720 " b3MprVec3Scale(&dir, -1.f);\n"
721 " b3MprVec3Normalize(&dir);\n"
722 " b3MprSupport(pairIndex,bodyIndexA,bodyIndexB,cpuBodyBuf,cpuConvexData,cpuCollidables,cpuVertices, sepAxis,&dir, b3MprSimplexPointW(portal, 1));\n"
723 " b3MprSimplexSetSize(portal, 2);\n"
724 " // test if origin isn't outside of v1\n"
725 " dot = b3MprVec3Dot(&b3MprSimplexPoint(portal, 1)->v, &dir);\n"
727 " if (b3MprIsZero(dot) || dot < 0.f)\n"
730 " b3MprVec3Cross(&dir, &b3MprSimplexPoint(portal, 0)->v,\n"
731 " &b3MprSimplexPoint(portal, 1)->v);\n"
732 " if (b3MprIsZero(b3MprVec3Len2(&dir))){\n"
733 " if (b3MprVec3Eq(&b3MprSimplexPoint(portal, 1)->v, b3mpr_vec3_origin)){\n"
734 " // origin lies on v1\n"
737 " // origin lies on v0-v1 segment\n"
741 " b3MprVec3Normalize(&dir);\n"
742 " b3MprSupport(pairIndex,bodyIndexA,bodyIndexB,cpuBodyBuf,cpuConvexData,cpuCollidables,cpuVertices, sepAxis,&dir, b3MprSimplexPointW(portal, 2));\n"
744 " dot = b3MprVec3Dot(&b3MprSimplexPoint(portal, 2)->v, &dir);\n"
745 " if (b3MprIsZero(dot) || dot < 0.f)\n"
747 " b3MprSimplexSetSize(portal, 3);\n"
748 " // vertex 3 direction\n"
749 " b3MprVec3Sub2(&va, &b3MprSimplexPoint(portal, 1)->v,\n"
750 " &b3MprSimplexPoint(portal, 0)->v);\n"
751 " b3MprVec3Sub2(&vb, &b3MprSimplexPoint(portal, 2)->v,\n"
752 " &b3MprSimplexPoint(portal, 0)->v);\n"
753 " b3MprVec3Cross(&dir, &va, &vb);\n"
754 " b3MprVec3Normalize(&dir);\n"
755 " // it is better to form portal faces to be oriented \"outside\" origin\n"
756 " dot = b3MprVec3Dot(&dir, &b3MprSimplexPoint(portal, 0)->v);\n"
758 " b3MprSimplexSwap(portal, 1, 2);\n"
759 " b3MprVec3Scale(&dir, -1.f);\n"
761 " while (b3MprSimplexSize(portal) < 4){\n"
762 " b3MprSupport(pairIndex,bodyIndexA,bodyIndexB,cpuBodyBuf,cpuConvexData,cpuCollidables,cpuVertices, sepAxis,&dir, b3MprSimplexPointW(portal, 3));\n"
764 " dot = b3MprVec3Dot(&b3MprSimplexPoint(portal, 3)->v, &dir);\n"
765 " if (b3MprIsZero(dot) || dot < 0.f)\n"
768 " // test if origin is outside (v1, v0, v3) - set v2 as v3 and\n"
770 " b3MprVec3Cross(&va, &b3MprSimplexPoint(portal, 1)->v,\n"
771 " &b3MprSimplexPoint(portal, 3)->v);\n"
772 " dot = b3MprVec3Dot(&va, &b3MprSimplexPoint(portal, 0)->v);\n"
773 " if (dot < 0.f && !b3MprIsZero(dot)){\n"
774 " b3MprSimplexSet(portal, 2, b3MprSimplexPoint(portal, 3));\n"
778 " // test if origin is outside (v3, v0, v2) - set v1 as v3 and\n"
780 " b3MprVec3Cross(&va, &b3MprSimplexPoint(portal, 3)->v,\n"
781 " &b3MprSimplexPoint(portal, 2)->v);\n"
782 " dot = b3MprVec3Dot(&va, &b3MprSimplexPoint(portal, 0)->v);\n"
783 " if (dot < 0.f && !b3MprIsZero(dot)){\n"
784 " b3MprSimplexSet(portal, 1, b3MprSimplexPoint(portal, 3));\n"
789 " b3MprVec3Sub2(&va, &b3MprSimplexPoint(portal, 1)->v,\n"
790 " &b3MprSimplexPoint(portal, 0)->v);\n"
791 " b3MprVec3Sub2(&vb, &b3MprSimplexPoint(portal, 2)->v,\n"
792 " &b3MprSimplexPoint(portal, 0)->v);\n"
793 " b3MprVec3Cross(&dir, &va, &vb);\n"
794 " b3MprVec3Normalize(&dir);\n"
796 " b3MprSimplexSetSize(portal, 4);\n"
801 "B3_STATIC int b3RefinePortal(int pairIndex,int bodyIndexA, int bodyIndexB, b3ConstArray(b3RigidBodyData_t) cpuBodyBuf, \n"
802 " b3ConstArray(b3ConvexPolyhedronData_t) cpuConvexData, \n"
803 " b3ConstArray(b3Collidable_t) cpuCollidables,\n"
804 " b3ConstArray(b3Float4) cpuVertices,\n"
805 " __global b3Float4* sepAxis,\n"
806 " b3MprSimplex_t *portal)\n"
809 " b3MprSupport_t v4;\n"
810 " for (int i=0;i<B3_MPR_MAX_ITERATIONS;i++)\n"
813 " // compute direction outside the portal (from v0 throught v1,v2,v3\n"
815 " b3PortalDir(portal, &dir);\n"
816 " // test if origin is inside the portal\n"
817 " if (portalEncapsulesOrigin(portal, &dir))\n"
819 " // get next support point\n"
821 " b3MprSupport(pairIndex,bodyIndexA,bodyIndexB,cpuBodyBuf,cpuConvexData,cpuCollidables,cpuVertices, sepAxis,&dir, &v4);\n"
822 " // test if v4 can expand portal to contain origin and if portal\n"
823 " // expanding doesn't reach given tolerance\n"
824 " if (!portalCanEncapsuleOrigin(portal, &v4, &dir)\n"
825 " || portalReachTolerance(portal, &v4, &dir))\n"
829 " // v1-v2-v3 triangle must be rearranged to face outside Minkowski\n"
830 " // difference (direction from v0).\n"
831 " b3ExpandPortal(portal, &v4);\n"
835 "B3_STATIC void b3FindPos(const b3MprSimplex_t *portal, b3Float4 *pos)\n"
837 " b3Float4 zero = b3MakeFloat4(0,0,0,0);\n"
838 " b3Float4* b3mpr_vec3_origin = &zero;\n"
841 " float b[4], sum, inv;\n"
842 " b3Float4 vec, p1, p2;\n"
843 " b3PortalDir(portal, &dir);\n"
844 " // use barycentric coordinates of tetrahedron to find origin\n"
845 " b3MprVec3Cross(&vec, &b3MprSimplexPoint(portal, 1)->v,\n"
846 " &b3MprSimplexPoint(portal, 2)->v);\n"
847 " b[0] = b3MprVec3Dot(&vec, &b3MprSimplexPoint(portal, 3)->v);\n"
848 " b3MprVec3Cross(&vec, &b3MprSimplexPoint(portal, 3)->v,\n"
849 " &b3MprSimplexPoint(portal, 2)->v);\n"
850 " b[1] = b3MprVec3Dot(&vec, &b3MprSimplexPoint(portal, 0)->v);\n"
851 " b3MprVec3Cross(&vec, &b3MprSimplexPoint(portal, 0)->v,\n"
852 " &b3MprSimplexPoint(portal, 1)->v);\n"
853 " b[2] = b3MprVec3Dot(&vec, &b3MprSimplexPoint(portal, 3)->v);\n"
854 " b3MprVec3Cross(&vec, &b3MprSimplexPoint(portal, 2)->v,\n"
855 " &b3MprSimplexPoint(portal, 1)->v);\n"
856 " b[3] = b3MprVec3Dot(&vec, &b3MprSimplexPoint(portal, 0)->v);\n"
857 " sum = b[0] + b[1] + b[2] + b[3];\n"
858 " if (b3MprIsZero(sum) || sum < 0.f){\n"
860 " b3MprVec3Cross(&vec, &b3MprSimplexPoint(portal, 2)->v,\n"
861 " &b3MprSimplexPoint(portal, 3)->v);\n"
862 " b[1] = b3MprVec3Dot(&vec, &dir);\n"
863 " b3MprVec3Cross(&vec, &b3MprSimplexPoint(portal, 3)->v,\n"
864 " &b3MprSimplexPoint(portal, 1)->v);\n"
865 " b[2] = b3MprVec3Dot(&vec, &dir);\n"
866 " b3MprVec3Cross(&vec, &b3MprSimplexPoint(portal, 1)->v,\n"
867 " &b3MprSimplexPoint(portal, 2)->v);\n"
868 " b[3] = b3MprVec3Dot(&vec, &dir);\n"
869 " sum = b[1] + b[2] + b[3];\n"
871 " inv = 1.f / sum;\n"
872 " b3MprVec3Copy(&p1, b3mpr_vec3_origin);\n"
873 " b3MprVec3Copy(&p2, b3mpr_vec3_origin);\n"
874 " for (i = 0; i < 4; i++){\n"
875 " b3MprVec3Copy(&vec, &b3MprSimplexPoint(portal, i)->v1);\n"
876 " b3MprVec3Scale(&vec, b[i]);\n"
877 " b3MprVec3Add(&p1, &vec);\n"
878 " b3MprVec3Copy(&vec, &b3MprSimplexPoint(portal, i)->v2);\n"
879 " b3MprVec3Scale(&vec, b[i]);\n"
880 " b3MprVec3Add(&p2, &vec);\n"
882 " b3MprVec3Scale(&p1, inv);\n"
883 " b3MprVec3Scale(&p2, inv);\n"
884 " b3MprVec3Copy(pos, &p1);\n"
885 " b3MprVec3Add(pos, &p2);\n"
886 " b3MprVec3Scale(pos, 0.5);\n"
888 "inline float b3MprVec3Dist2(const b3Float4 *a, const b3Float4 *b)\n"
891 " b3MprVec3Sub2(&ab, a, b);\n"
892 " return b3MprVec3Len2(&ab);\n"
894 "inline float _b3MprVec3PointSegmentDist2(const b3Float4 *P,\n"
895 " const b3Float4 *x0,\n"
896 " const b3Float4 *b,\n"
897 " b3Float4 *witness)\n"
899 " // The computation comes from solving equation of segment:\n"
900 " // S(t) = x0 + t.d\n"
901 " // where - x0 is initial point of segment\n"
902 " // - d is direction of segment from x0 (|d| > 0)\n"
903 " // - t belongs to <0, 1> interval\n"
905 " // Than, distance from a segment to some point P can be expressed:\n"
906 " // D(t) = |x0 + t.d - P|^2\n"
907 " // which is distance from any point on segment. Minimization\n"
908 " // of this function brings distance from P to segment.\n"
909 " // Minimization of D(t) leads to simple quadratic equation that's\n"
910 " // solving is straightforward.\n"
912 " // Bonus of this method is witness point for free.\n"
915 " // direction of segment\n"
916 " b3MprVec3Sub2(&d, b, x0);\n"
917 " // precompute vector from P to x0\n"
918 " b3MprVec3Sub2(&a, x0, P);\n"
919 " t = -1.f * b3MprVec3Dot(&a, &d);\n"
920 " t /= b3MprVec3Len2(&d);\n"
921 " if (t < 0.f || b3MprIsZero(t)){\n"
922 " dist = b3MprVec3Dist2(x0, P);\n"
924 " b3MprVec3Copy(witness, x0);\n"
925 " }else if (t > 1.f || b3MprEq(t, 1.f)){\n"
926 " dist = b3MprVec3Dist2(b, P);\n"
928 " b3MprVec3Copy(witness, b);\n"
931 " b3MprVec3Copy(witness, &d);\n"
932 " b3MprVec3Scale(witness, t);\n"
933 " b3MprVec3Add(witness, x0);\n"
934 " dist = b3MprVec3Dist2(witness, P);\n"
936 " // recycling variables\n"
937 " b3MprVec3Scale(&d, t);\n"
938 " b3MprVec3Add(&d, &a);\n"
939 " dist = b3MprVec3Len2(&d);\n"
944 "inline float b3MprVec3PointTriDist2(const b3Float4 *P,\n"
945 " const b3Float4 *x0, const b3Float4 *B,\n"
946 " const b3Float4 *C,\n"
947 " b3Float4 *witness)\n"
949 " // Computation comes from analytic expression for triangle (x0, B, C)\n"
950 " // T(s, t) = x0 + s.d1 + t.d2, where d1 = B - x0 and d2 = C - x0 and\n"
951 " // Then equation for distance is:\n"
952 " // D(s, t) = | T(s, t) - P |^2\n"
953 " // This leads to minimization of quadratic function of two variables.\n"
954 " // The solution from is taken only if s is between 0 and 1, t is\n"
955 " // between 0 and 1 and t + s < 1, otherwise distance from segment is\n"
957 " b3Float4 d1, d2, a;\n"
958 " float u, v, w, p, q, r;\n"
959 " float s, t, dist, dist2;\n"
960 " b3Float4 witness2;\n"
961 " b3MprVec3Sub2(&d1, B, x0);\n"
962 " b3MprVec3Sub2(&d2, C, x0);\n"
963 " b3MprVec3Sub2(&a, x0, P);\n"
964 " u = b3MprVec3Dot(&a, &a);\n"
965 " v = b3MprVec3Dot(&d1, &d1);\n"
966 " w = b3MprVec3Dot(&d2, &d2);\n"
967 " p = b3MprVec3Dot(&a, &d1);\n"
968 " q = b3MprVec3Dot(&a, &d2);\n"
969 " r = b3MprVec3Dot(&d1, &d2);\n"
970 " s = (q * r - w * p) / (w * v - r * r);\n"
971 " t = (-s * r - q) / w;\n"
972 " if ((b3MprIsZero(s) || s > 0.f)\n"
973 " && (b3MprEq(s, 1.f) || s < 1.f)\n"
974 " && (b3MprIsZero(t) || t > 0.f)\n"
975 " && (b3MprEq(t, 1.f) || t < 1.f)\n"
976 " && (b3MprEq(t + s, 1.f) || t + s < 1.f)){\n"
978 " b3MprVec3Scale(&d1, s);\n"
979 " b3MprVec3Scale(&d2, t);\n"
980 " b3MprVec3Copy(witness, x0);\n"
981 " b3MprVec3Add(witness, &d1);\n"
982 " b3MprVec3Add(witness, &d2);\n"
983 " dist = b3MprVec3Dist2(witness, P);\n"
985 " dist = s * s * v;\n"
986 " dist += t * t * w;\n"
987 " dist += 2.f * s * t * r;\n"
988 " dist += 2.f * s * p;\n"
989 " dist += 2.f * t * q;\n"
993 " dist = _b3MprVec3PointSegmentDist2(P, x0, B, witness);\n"
994 " dist2 = _b3MprVec3PointSegmentDist2(P, x0, C, &witness2);\n"
995 " if (dist2 < dist){\n"
998 " b3MprVec3Copy(witness, &witness2);\n"
1000 " dist2 = _b3MprVec3PointSegmentDist2(P, B, C, &witness2);\n"
1001 " if (dist2 < dist){\n"
1004 " b3MprVec3Copy(witness, &witness2);\n"
1009 "B3_STATIC void b3FindPenetr(int pairIndex,int bodyIndexA, int bodyIndexB, b3ConstArray(b3RigidBodyData_t) cpuBodyBuf, \n"
1010 " b3ConstArray(b3ConvexPolyhedronData_t) cpuConvexData, \n"
1011 " b3ConstArray(b3Collidable_t) cpuCollidables,\n"
1012 " b3ConstArray(b3Float4) cpuVertices,\n"
1013 " __global b3Float4* sepAxis,\n"
1014 " b3MprSimplex_t *portal,\n"
1015 " float *depth, b3Float4 *pdir, b3Float4 *pos)\n"
1018 " b3MprSupport_t v4;\n"
1019 " unsigned long iterations;\n"
1020 " b3Float4 zero = b3MakeFloat4(0,0,0,0);\n"
1021 " b3Float4* b3mpr_vec3_origin = &zero;\n"
1022 " iterations = 1UL;\n"
1023 " for (int i=0;i<B3_MPR_MAX_ITERATIONS;i++)\n"
1026 " // compute portal direction and obtain next support point\n"
1027 " b3PortalDir(portal, &dir);\n"
1029 " b3MprSupport(pairIndex,bodyIndexA,bodyIndexB,cpuBodyBuf,cpuConvexData,cpuCollidables,cpuVertices, sepAxis,&dir, &v4);\n"
1030 " // reached tolerance -> find penetration info\n"
1031 " if (portalReachTolerance(portal, &v4, &dir)\n"
1032 " || iterations ==B3_MPR_MAX_ITERATIONS)\n"
1034 " *depth = b3MprVec3PointTriDist2(b3mpr_vec3_origin,&b3MprSimplexPoint(portal, 1)->v,&b3MprSimplexPoint(portal, 2)->v,&b3MprSimplexPoint(portal, 3)->v,pdir);\n"
1035 " *depth = B3_MPR_SQRT(*depth);\n"
1037 " if (b3MprIsZero((*pdir).x) && b3MprIsZero((*pdir).y) && b3MprIsZero((*pdir).z))\n"
1042 " b3MprVec3Normalize(pdir);\n"
1044 " // barycentric coordinates:\n"
1045 " b3FindPos(portal, pos);\n"
1048 " b3ExpandPortal(portal, &v4);\n"
1052 "B3_STATIC void b3FindPenetrTouch(b3MprSimplex_t *portal,float *depth, b3Float4 *dir, b3Float4 *pos)\n"
1054 " // Touching contact on portal's v1 - so depth is zero and direction\n"
1055 " // is unimportant and pos can be guessed\n"
1057 " b3Float4 zero = b3MakeFloat4(0,0,0,0);\n"
1058 " b3Float4* b3mpr_vec3_origin = &zero;\n"
1059 " b3MprVec3Copy(dir, b3mpr_vec3_origin);\n"
1060 " b3MprVec3Copy(pos, &b3MprSimplexPoint(portal, 1)->v1);\n"
1061 " b3MprVec3Add(pos, &b3MprSimplexPoint(portal, 1)->v2);\n"
1062 " b3MprVec3Scale(pos, 0.5);\n"
1064 "B3_STATIC void b3FindPenetrSegment(b3MprSimplex_t *portal,\n"
1065 " float *depth, b3Float4 *dir, b3Float4 *pos)\n"
1068 " // Origin lies on v0-v1 segment.\n"
1069 " // Depth is distance to v1, direction also and position must be\n"
1071 " b3MprVec3Copy(pos, &b3MprSimplexPoint(portal, 1)->v1);\n"
1072 " b3MprVec3Add(pos, &b3MprSimplexPoint(portal, 1)->v2);\n"
1073 " b3MprVec3Scale(pos, 0.5f);\n"
1075 " b3MprVec3Copy(dir, &b3MprSimplexPoint(portal, 1)->v);\n"
1076 " *depth = B3_MPR_SQRT(b3MprVec3Len2(dir));\n"
1077 " b3MprVec3Normalize(dir);\n"
1079 "inline int b3MprPenetration(int pairIndex, int bodyIndexA, int bodyIndexB,\n"
1080 " b3ConstArray(b3RigidBodyData_t) cpuBodyBuf,\n"
1081 " b3ConstArray(b3ConvexPolyhedronData_t) cpuConvexData, \n"
1082 " b3ConstArray(b3Collidable_t) cpuCollidables,\n"
1083 " b3ConstArray(b3Float4) cpuVertices,\n"
1084 " __global b3Float4* sepAxis,\n"
1085 " __global int* hasSepAxis,\n"
1086 " float *depthOut, b3Float4* dirOut, b3Float4* posOut)\n"
1089 " b3MprSimplex_t portal;\n"
1091 "// if (!hasSepAxis[pairIndex])\n"
1094 " hasSepAxis[pairIndex] = 0;\n"
1096 " // Phase 1: Portal discovery\n"
1097 " res = b3DiscoverPortal(pairIndex,bodyIndexA,bodyIndexB,cpuBodyBuf,cpuConvexData,cpuCollidables,cpuVertices,sepAxis,hasSepAxis, &portal);\n"
1100 " //sepAxis[pairIndex] = *pdir;//or -dir?\n"
1105 " // Phase 2: Portal refinement\n"
1107 " res = b3RefinePortal(pairIndex,bodyIndexA,bodyIndexB,cpuBodyBuf,cpuConvexData,cpuCollidables,cpuVertices, sepAxis,&portal);\n"
1110 " // Phase 3. Penetration info\n"
1111 " b3FindPenetr(pairIndex,bodyIndexA,bodyIndexB,cpuBodyBuf,cpuConvexData,cpuCollidables,cpuVertices, sepAxis,&portal, depthOut, dirOut, posOut);\n"
1112 " hasSepAxis[pairIndex] = 1;\n"
1113 " sepAxis[pairIndex] = -*dirOut;\n"
1118 " // Touching contact on portal's v1.\n"
1119 " b3FindPenetrTouch(&portal, depthOut, dirOut, posOut);\n"
1125 " b3FindPenetrSegment( &portal, depthOut, dirOut, posOut);\n"
1130 " hasSepAxis[pairIndex]=0;\n"
1133 " // Origin isn't inside portal - no collision.\n"
1141 "#endif //B3_MPR_PENETRATION_H\n"
1142 "#ifndef B3_CONTACT4DATA_H\n"
1143 "#define B3_CONTACT4DATA_H\n"
1144 "#ifndef B3_FLOAT4_H\n"
1145 "#ifdef __cplusplus\n"
1148 "#endif //B3_FLOAT4_H\n"
1149 "typedef struct b3Contact4Data b3Contact4Data_t;\n"
1150 "struct b3Contact4Data\n"
1152 " b3Float4 m_worldPosB[4];\n"
1153 "// b3Float4 m_localPosA[4];\n"
1154 "// b3Float4 m_localPosB[4];\n"
1155 " b3Float4 m_worldNormalOnB; // w: m_nPoints\n"
1156 " unsigned short m_restituitionCoeffCmp;\n"
1157 " unsigned short m_frictionCoeffCmp;\n"
1158 " int m_batchIdx;\n"
1159 " int m_bodyAPtrAndSignBit;//x:m_bodyAPtr, y:m_bodyBPtr\n"
1160 " int m_bodyBPtrAndSignBit;\n"
1161 " int m_childIndexA;\n"
1162 " int m_childIndexB;\n"
1166 "inline int b3Contact4Data_getNumPoints(const struct b3Contact4Data* contact)\n"
1168 " return (int)contact->m_worldNormalOnB.w;\n"
1170 "inline void b3Contact4Data_setNumPoints(struct b3Contact4Data* contact, int numPoints)\n"
1172 " contact->m_worldNormalOnB.w = (float)numPoints;\n"
1174 "#endif //B3_CONTACT4DATA_H\n"
1175 "#define AppendInc(x, out) out = atomic_inc(x)\n"
1176 "#define GET_NPOINTS(x) (x).m_worldNormalOnB.w\n"
1177 "#ifdef cl_ext_atomic_counters_32\n"
1178 " #pragma OPENCL EXTENSION cl_ext_atomic_counters_32 : enable\n"
1180 " #define counter32_t volatile __global int*\n"
1182 "__kernel void mprPenetrationKernel( __global int4* pairs,\n"
1183 " __global const b3RigidBodyData_t* rigidBodies, \n"
1184 " __global const b3Collidable_t* collidables,\n"
1185 " __global const b3ConvexPolyhedronData_t* convexShapes, \n"
1186 " __global const float4* vertices,\n"
1187 " __global float4* separatingNormals,\n"
1188 " __global int* hasSeparatingAxis,\n"
1189 " __global struct b3Contact4Data* restrict globalContactsOut,\n"
1190 " counter32_t nGlobalContactsOut,\n"
1191 " int contactCapacity,\n"
1194 " int i = get_global_id(0);\n"
1195 " int pairIndex = i;\n"
1196 " if (i<numPairs)\n"
1198 " int bodyIndexA = pairs[i].x;\n"
1199 " int bodyIndexB = pairs[i].y;\n"
1200 " int collidableIndexA = rigidBodies[bodyIndexA].m_collidableIdx;\n"
1201 " int collidableIndexB = rigidBodies[bodyIndexB].m_collidableIdx;\n"
1203 " int shapeIndexA = collidables[collidableIndexA].m_shapeIndex;\n"
1204 " int shapeIndexB = collidables[collidableIndexB].m_shapeIndex;\n"
1207 " //once the broadphase avoids static-static pairs, we can remove this test\n"
1208 " if ((rigidBodies[bodyIndexA].m_invMass==0) &&(rigidBodies[bodyIndexB].m_invMass==0))\n"
1213 " if ((collidables[collidableIndexA].m_shapeType!=SHAPE_CONVEX_HULL) ||(collidables[collidableIndexB].m_shapeType!=SHAPE_CONVEX_HULL))\n"
1217 " float depthOut;\n"
1218 " b3Float4 dirOut;\n"
1219 " b3Float4 posOut;\n"
1220 " int res = b3MprPenetration(pairIndex, bodyIndexA, bodyIndexB,rigidBodies,convexShapes,collidables,vertices,separatingNormals,hasSeparatingAxis,&depthOut, &dirOut, &posOut);\n"
1227 " //add a contact\n"
1229 " AppendInc( nGlobalContactsOut, dstIdx );\n"
1230 " if (dstIdx<contactCapacity)\n"
1232 " pairs[pairIndex].z = dstIdx;\n"
1233 " __global struct b3Contact4Data* c = globalContactsOut + dstIdx;\n"
1234 " c->m_worldNormalOnB = -dirOut;//normal;\n"
1235 " c->m_restituitionCoeffCmp = (0.f*0xffff);c->m_frictionCoeffCmp = (0.7f*0xffff);\n"
1236 " c->m_batchIdx = pairIndex;\n"
1237 " int bodyA = pairs[pairIndex].x;\n"
1238 " int bodyB = pairs[pairIndex].y;\n"
1239 " c->m_bodyAPtrAndSignBit = rigidBodies[bodyA].m_invMass==0 ? -bodyA:bodyA;\n"
1240 " c->m_bodyBPtrAndSignBit = rigidBodies[bodyB].m_invMass==0 ? -bodyB:bodyB;\n"
1241 " c->m_childIndexA = -1;\n"
1242 " c->m_childIndexB = -1;\n"
1243 " //for (int i=0;i<nContacts;i++)\n"
1244 " posOut.w = -depthOut;\n"
1245 " c->m_worldPosB[0] = posOut;//localPoints[contactIdx[i]];\n"
1246 " GET_NPOINTS(*c) = 1;//nContacts;\n"
1251 "typedef float4 Quaternion;\n"
1252 "#define make_float4 (float4)\n"
1254 "float dot3F4(float4 a, float4 b)\n"
1256 " float4 a1 = make_float4(a.xyz,0.f);\n"
1257 " float4 b1 = make_float4(b.xyz,0.f);\n"
1258 " return dot(a1, b1);\n"
1261 "float4 cross3(float4 a, float4 b)\n"
1263 " return cross(a,b);\n"
1266 "Quaternion qtMul(Quaternion a, Quaternion b)\n"
1268 " Quaternion ans;\n"
1269 " ans = cross3( a, b );\n"
1270 " ans += a.w*b+b.w*a;\n"
1271 "// ans.w = a.w*b.w - (a.x*b.x+a.y*b.y+a.z*b.z);\n"
1272 " ans.w = a.w*b.w - dot3F4(a, b);\n"
1276 "Quaternion qtInvert(Quaternion q)\n"
1278 " return (Quaternion)(-q.xyz, q.w);\n"
1281 "float4 qtRotate(Quaternion q, float4 vec)\n"
1283 " Quaternion qInv = qtInvert( q );\n"
1284 " float4 vcpy = vec;\n"
1286 " float4 out = qtMul(qtMul(q,vcpy),qInv);\n"
1290 "float4 transform(const float4* p, const float4* translation, const Quaternion* orientation)\n"
1292 " return qtRotate( *orientation, *p ) + (*translation);\n"
1295 "float4 qtInvRotate(const Quaternion q, float4 vec)\n"
1297 " return qtRotate( qtInvert( q ), vec );\n"
1299 "inline void project(__global const b3ConvexPolyhedronData_t* hull, const float4 pos, const float4 orn, \n"
1300 "const float4* dir, __global const float4* vertices, float* min, float* max)\n"
1302 " min[0] = FLT_MAX;\n"
1303 " max[0] = -FLT_MAX;\n"
1304 " int numVerts = hull->m_numVertices;\n"
1305 " const float4 localDir = qtInvRotate(orn,*dir);\n"
1306 " float offset = dot(pos,*dir);\n"
1307 " for(int i=0;i<numVerts;i++)\n"
1309 " float dp = dot(vertices[hull->m_vertexOffset+i],localDir);\n"
1310 " if(dp < min[0]) \n"
1312 " if(dp > max[0]) \n"
1315 " if(min[0]>max[0])\n"
1317 " float tmp = min[0];\n"
1318 " min[0] = max[0];\n"
1321 " min[0] += offset;\n"
1322 " max[0] += offset;\n"
1324 "bool findSeparatingAxisUnitSphere( __global const b3ConvexPolyhedronData_t* hullA, __global const b3ConvexPolyhedronData_t* hullB, \n"
1325 " const float4 posA1,\n"
1326 " const float4 ornA,\n"
1327 " const float4 posB1,\n"
1328 " const float4 ornB,\n"
1329 " const float4 DeltaC2,\n"
1330 " __global const float4* vertices,\n"
1331 " __global const float4* unitSphereDirections,\n"
1332 " int numUnitSphereDirections,\n"
1337 " float4 posA = posA1;\n"
1339 " float4 posB = posB1;\n"
1341 " int curPlaneTests=0;\n"
1342 " int curEdgeEdge = 0;\n"
1343 " // Test unit sphere directions\n"
1344 " for (int i=0;i<numUnitSphereDirections;i++)\n"
1346 " float4 crossje;\n"
1347 " crossje = unitSphereDirections[i]; \n"
1348 " if (dot3F4(DeltaC2,crossje)>0)\n"
1349 " crossje *= -1.f;\n"
1352 " bool result = true;\n"
1353 " float Min0,Max0;\n"
1354 " float Min1,Max1;\n"
1355 " project(hullA,posA,ornA,&crossje,vertices, &Min0, &Max0);\n"
1356 " project(hullB,posB,ornB,&crossje,vertices, &Min1, &Max1);\n"
1358 " if(Max0<Min1 || Max1<Min0)\n"
1361 " float d0 = Max0 - Min1;\n"
1362 " float d1 = Max1 - Min0;\n"
1363 " dist = d0<d1 ? d0:d1;\n"
1369 " *sep = crossje;\n"
1374 " if((dot3F4(-DeltaC2,*sep))>0.0f)\n"
1376 " *sep = -(*sep);\n"
1380 "__kernel void findSeparatingAxisUnitSphereKernel( __global const int4* pairs, \n"
1381 " __global const b3RigidBodyData_t* rigidBodies, \n"
1382 " __global const b3Collidable_t* collidables,\n"
1383 " __global const b3ConvexPolyhedronData_t* convexShapes, \n"
1384 " __global const float4* vertices,\n"
1385 " __global const float4* unitSphereDirections,\n"
1386 " __global float4* separatingNormals,\n"
1387 " __global int* hasSeparatingAxis,\n"
1388 " __global float* dmins,\n"
1389 " int numUnitSphereDirections,\n"
1393 " int i = get_global_id(0);\n"
1395 " if (i<numPairs)\n"
1397 " if (hasSeparatingAxis[i])\n"
1400 " int bodyIndexA = pairs[i].x;\n"
1401 " int bodyIndexB = pairs[i].y;\n"
1403 " int collidableIndexA = rigidBodies[bodyIndexA].m_collidableIdx;\n"
1404 " int collidableIndexB = rigidBodies[bodyIndexB].m_collidableIdx;\n"
1406 " int shapeIndexA = collidables[collidableIndexA].m_shapeIndex;\n"
1407 " int shapeIndexB = collidables[collidableIndexB].m_shapeIndex;\n"
1410 " int numFacesA = convexShapes[shapeIndexA].m_numFaces;\n"
1412 " float dmin = dmins[i];\n"
1414 " float4 posA = rigidBodies[bodyIndexA].m_pos;\n"
1416 " float4 posB = rigidBodies[bodyIndexB].m_pos;\n"
1418 " float4 c0local = convexShapes[shapeIndexA].m_localCenter;\n"
1419 " float4 ornA = rigidBodies[bodyIndexA].m_quat;\n"
1420 " float4 c0 = transform(&c0local, &posA, &ornA);\n"
1421 " float4 c1local = convexShapes[shapeIndexB].m_localCenter;\n"
1422 " float4 ornB =rigidBodies[bodyIndexB].m_quat;\n"
1423 " float4 c1 = transform(&c1local,&posB,&ornB);\n"
1424 " const float4 DeltaC2 = c0 - c1;\n"
1425 " float4 sepNormal = separatingNormals[i];\n"
1427 " int numEdgeEdgeDirections = convexShapes[shapeIndexA].m_numUniqueEdges*convexShapes[shapeIndexB].m_numUniqueEdges;\n"
1428 " if (numEdgeEdgeDirections>numUnitSphereDirections)\n"
1430 " bool sepEE = findSeparatingAxisUnitSphere( &convexShapes[shapeIndexA], &convexShapes[shapeIndexB],posA,ornA,\n"
1433 " vertices,unitSphereDirections,numUnitSphereDirections,&sepNormal,&dmin);\n"
1436 " hasSeparatingAxis[i] = 0;\n"
1439 " hasSeparatingAxis[i] = 1;\n"
1440 " separatingNormals[i] = sepNormal;\n"
1443 " } //if (hasSeparatingAxis[i])\n"
1444 " }//(i<numPairs)\n"