Initial commit of cloth modifier from branch rev 13453
[blender.git] / extern / bullet2 / src / BulletDynamics / Dynamics / Bullet-C-API.cpp
1 /*
2 Bullet Continuous Collision Detection and Physics Library
3 Copyright (c) 2003-2006 Erwin Coumans  http://continuousphysics.com/Bullet/
4
5 This software is provided 'as-is', without any express or implied warranty.
6 In no event will the authors be held liable for any damages arising from the use of this software.
7 Permission is granted to anyone to use this software for any purpose, 
8 including commercial applications, and to alter it and redistribute it freely, 
9 subject to the following restrictions:
10
11 1. The origin of this software must not be misrepresented; you must not claim that you wrote the original software. If you use this software in a product, an acknowledgment in the product documentation would be appreciated but is not required.
12 2. Altered source versions must be plainly marked as such, and must not be misrepresented as being the original software.
13 3. This notice may not be removed or altered from any source distribution.
14 */
15
16 /*
17         Draft high-level generic physics C-API. For low-level access, use the physics SDK native API's.
18         Work in progress, functionality will be added on demand.
19
20         If possible, use the richer Bullet C++ API, by including <src/btBulletDynamicsCommon.h>
21 */
22
23 #include "Bullet-C-Api.h"
24 #include "btBulletDynamicsCommon.h"
25 #include "LinearMath/btAlignedAllocator.h"
26
27
28 #include "LinearMath/btVector3.h"
29 #include "LinearMath/btScalar.h"        
30 #include "LinearMath/btMatrix3x3.h"
31 #include "LinearMath/btTransform.h"
32 #include "BulletCollision/NarrowPhaseCollision/btVoronoiSimplexSolver.h"
33 #include "BulletCollision/CollisionShapes/btTriangleShape.h"
34
35 #include "BulletCollision/NarrowPhaseCollision/btGjkPairDetector.h"
36 #include "BulletCollision/NarrowPhaseCollision/btPointCollector.h"
37 #include "BulletCollision/NarrowPhaseCollision/btVoronoiSimplexSolver.h"
38 #include "BulletCollision/NarrowPhaseCollision/btSubSimplexConvexCast.h"
39 #include "BulletCollision/NarrowPhaseCollision/btGjkEpaPenetrationDepthSolver.h"
40 #include "BulletCollision/NarrowPhaseCollision/btGjkEpa.h"
41 #include "BulletCollision/CollisionShapes/btMinkowskiSumShape.h"
42 #include "BulletCollision/NarrowPhaseCollision/btSubSimplexConvexCast.h"
43 #include "BulletCollision/NarrowPhaseCollision/btVoronoiSimplexSolver.h"
44 #include "BulletCollision/NarrowPhaseCollision/btGjkPairDetector.h"
45
46 #include "BulletCollision/NarrowPhaseCollision/btDiscreteCollisionDetectorInterface.h"
47 #include "BulletCollision/NarrowPhaseCollision/btSimplexSolverInterface.h"
48 #include "BulletCollision/NarrowPhaseCollision/btMinkowskiPenetrationDepthSolver.h"
49 #include "BulletCollision/NarrowPhaseCollision/btGjkPairDetector.h"
50 #include "BulletCollision/NarrowPhaseCollision/btGjkEpaPenetrationDepthSolver.h"
51 #include "LinearMath/btStackAlloc.h"
52
53 extern "C"
54 double plNearestPoints(float p1[3], float p2[3], float p3[3], float q1[3], float q2[3], float q3[3], float *pa, float *pb, float normal[3])
55 {
56         btTriangleShape trishapeA(btVector3(p1[0], p1[1], p1[2]), btVector3(p2[0], p2[1], p2[2]), btVector3(p3[0], p3[1], p3[2]));
57         trishapeA.setMargin(0.000001f);
58         
59         btTriangleShape trishapeB(btVector3(q1[0], q1[1], q1[2]), btVector3(q2[0], q2[1], q2[2]), btVector3(q3[0], q3[1], q3[2]));
60         trishapeB.setMargin(0.000001f);
61         
62         // btVoronoiSimplexSolver sGjkSimplexSolver;
63         // btGjkEpaPenetrationDepthSolver penSolverPtr; 
64         
65         static btSimplexSolverInterface sGjkSimplexSolver;
66         sGjkSimplexSolver.reset();
67         
68         static btGjkEpaPenetrationDepthSolver Solver0;
69         static btMinkowskiPenetrationDepthSolver Solver1;
70                 
71         btConvexPenetrationDepthSolver* Solver = NULL;
72         
73         Solver = &Solver1;      
74                 
75         btGjkPairDetector convexConvex(&trishapeA ,&trishapeB,&sGjkSimplexSolver,Solver);
76         
77         convexConvex.m_catchDegeneracies = 1;
78         
79         // btGjkPairDetector convexConvex(&trishapeA ,&trishapeB,&sGjkSimplexSolver,0);
80         
81         btPointCollector gjkOutput;
82         btGjkPairDetector::ClosestPointInput input;
83         
84         btStackAlloc gStackAlloc(1024*1024*2);
85  
86         input.m_stackAlloc = &gStackAlloc;
87         
88         btTransform tr;
89         tr.setIdentity();
90         
91         input.m_transformA = tr;
92         input.m_transformB = tr;
93         
94         convexConvex.getClosestPoints(input, gjkOutput, 0);
95         
96         
97         if (gjkOutput.m_hasResult)
98         {
99                 
100                 pb[0] = pa[0] = gjkOutput.m_pointInWorld[0];
101                 pb[1] = pa[1] = gjkOutput.m_pointInWorld[1];
102                 pb[2] = pa[2] = gjkOutput.m_pointInWorld[2];
103
104                 pb[0]+= gjkOutput.m_normalOnBInWorld[0] * gjkOutput.m_distance;
105                 pb[1]+= gjkOutput.m_normalOnBInWorld[1] * gjkOutput.m_distance;
106                 pb[2]+= gjkOutput.m_normalOnBInWorld[2] * gjkOutput.m_distance;
107                 
108                 normal[0] = gjkOutput.m_normalOnBInWorld[0];
109                 normal[1] = gjkOutput.m_normalOnBInWorld[1];
110                 normal[2] = gjkOutput.m_normalOnBInWorld[2];
111
112                 return gjkOutput.m_distance;
113         }
114         return -1.0f;   
115 }