File: main.cpp

package info (click to toggle)
bullet 3.24%2Bdfsg-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 15,164 kB
  • sloc: cpp: 246,331; lisp: 12,017; ansic: 11,175; python: 630; makefile: 136; sh: 75
file content (307 lines) | stat: -rw-r--r-- 9,699 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
/*
Bullet Continuous Collision Detection and Physics Library
Copyright (c) 2014 Google Inc. http://bulletphysics.org

This software is provided 'as-is', without any express or implied warranty.
In no event will the authors be held liable for any damages arising from the use of this software.
Permission is granted to anyone to use this software for any purpose, 
including commercial applications, and to alter it and redistribute it freely, 
subject to the following restrictions:

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.
2. Altered source versions must be plainly marked as such, and must not be misrepresented as being the original software.
3. This notice may not be removed or altered from any source distribution.
*/

///Original author: Erwin Coumans, October 2014
///Initial version of this low-level GJK/EPA/MPR convex-convex collision test
///You can provide your own support function in combination with the template functions
///See btComputeGjkEpaSphereSphereCollision below for an example
///Todo: the test needs proper coverage and using a convex hull point cloud
///Also the GJK, EPA and MPR should be improved, both quality and performance

#include <vector>

#include <gtest/gtest.h>

#include "SphereSphereCollision.h"
#include "BulletCollision/CollisionShapes/btHeightfieldTerrainShape.h"
#include "BulletCollision/CollisionShapes/btSphereShape.h"
#include "BulletCollision/CollisionShapes/btMultiSphereShape.h"

#include "BulletCollision/NarrowPhaseCollision/btComputeGjkEpaPenetration.h"
#include "BulletCollision/NarrowPhaseCollision/btGjkEpa3.h"
#include "BulletCollision/NarrowPhaseCollision/btMprPenetration.h"

namespace {

btVector3 MyBulletShapeSupportFunc(const void* shapeAptr, const btVector3& dir, bool includeMargin)
{
	btConvexShape* shape = (btConvexShape*)shapeAptr;
	if (includeMargin)
	{
		return shape->localGetSupportingVertex(dir);
	}

	return shape->localGetSupportingVertexWithoutMargin(dir);
}

btVector3 MyBulletShapeCenterFunc(const void* shapeAptr)
{
	return btVector3(0, 0, 0);
}

enum SphereSphereTestMethod
{
	SSTM_ANALYTIC,
	SSTM_GJKEPA,
	SSTM_GJKEPA_RADIUS_NOT_FULL_MARGIN,
	SSTM_GJKMPR
};

struct ConvexWrap
{
	btConvexShape* m_convex;
	btTransform m_worldTrans;
	inline btScalar getMargin() const
	{
		return m_convex->getMargin();
	}
	inline btVector3 getObjectCenterInWorld() const
	{
		return m_worldTrans.getOrigin();
	}
	inline const btTransform& getWorldTransform() const
	{
		return m_worldTrans;
	}
	inline btVector3 getLocalSupportWithMargin(const btVector3& dir) const
	{
		return m_convex->localGetSupportingVertex(dir);
	}
	inline btVector3 getLocalSupportWithoutMargin(const btVector3& dir) const
	{
		return m_convex->localGetSupportingVertexWithoutMargin(dir);
	}
};

inline int btComputeGjkEpaSphereSphereCollision(const btSphereSphereCollisionDescription& input, btDistanceInfo* distInfo, SphereSphereTestMethod method)
{
	///for spheres it is best to use a 'point' and set the margin to the radius (which is what btSphereShape does)
	btSphereShape singleSphereA(input.m_radiusA);
	btSphereShape singleSphereB(input.m_radiusB);
	btVector3 org(0, 0, 0);
	btScalar radA = input.m_radiusA;
	btScalar radB = input.m_radiusB;

	ConvexWrap a, b;
	a.m_worldTrans = input.m_sphereTransformA;
	b.m_worldTrans = input.m_sphereTransformB;
	;

	btMultiSphereShape multiSphereA(&org, &radA, 1);
	btMultiSphereShape multiSphereB(&org, &radB, 1);

	btGjkCollisionDescription colDesc;
	switch (method)
	{
		case SSTM_GJKEPA_RADIUS_NOT_FULL_MARGIN:
		{
			a.m_convex = &multiSphereA;
			b.m_convex = &multiSphereB;
			break;
		}
		default:
		{
			a.m_convex = &singleSphereA;
			b.m_convex = &singleSphereB;
		}
	};

	btVoronoiSimplexSolver simplexSolver;
	simplexSolver.reset();

	int res = -1;
	///todo(erwincoumans): improve convex-convex quality and performance
	///also compare with https://code.google.com/p/bullet/source/browse/branches/PhysicsEffects/src/base_level/collision/pfx_gjk_solver.cpp
	switch (method)
	{
		case SSTM_GJKEPA_RADIUS_NOT_FULL_MARGIN:
		case SSTM_GJKEPA:
		{
			res = btComputeGjkEpaPenetration(a, b, colDesc, simplexSolver, distInfo);
			break;
		}
		case SSTM_GJKMPR:
		{
			res = btComputeGjkDistance(a, b, colDesc, distInfo);
			if (res == 0)
			{
				//   printf("use GJK results in distance %f\n",distInfo->m_distance);
				return res;
			}
			else
			{
				btMprCollisionDescription mprDesc;
				res = btComputeMprPenetration(a, b, mprDesc, distInfo);

				//                if (res==0)
				//                {
				//                    printf("use MPR results in distance %f\n",distInfo->m_distance);
				//                }
			}
			break;
		}
		default:
		{
			btAssert(0);
		}
	}
	return res;
}

void testSphereSphereDistance(SphereSphereTestMethod method, btScalar abs_error)
{
	{
		btSphereSphereCollisionDescription ssd;
		ssd.m_sphereTransformA.setIdentity();
		ssd.m_sphereTransformB.setIdentity();
		ssd.m_radiusA = 0.f;
		ssd.m_radiusB = 0.f;
		btDistanceInfo distInfo;
		int result = btComputeSphereSphereCollision(ssd, &distInfo);
		ASSERT_EQ(0, result);
		ASSERT_EQ(btScalar(0), distInfo.m_distance);
	}

	for (int rb = 1; rb < 5; rb++)
		for (int z = -5; z < 5; z++)
		{
			for (int j = 1; j < 5; j++)
			{
				for (int i = -5; i < 5; i++)
				{
					if (i != z)  //skip co-centric spheres for now (todo(erwincoumans) fix this)
					{
						btSphereSphereCollisionDescription ssd;
						ssd.m_sphereTransformA.setIdentity();
						ssd.m_sphereTransformA.setOrigin(btVector3(0, btScalar(i), 0));
						ssd.m_sphereTransformB.setIdentity();
						ssd.m_sphereTransformB.setOrigin(btVector3(0, btScalar(z), 0));
						ssd.m_radiusA = btScalar(j);
						ssd.m_radiusB = btScalar(rb) * btScalar(0.1);
						btDistanceInfo distInfo;
						int result = -1;
						switch (method)
						{
							case SSTM_ANALYTIC:
							{
								result = btComputeSphereSphereCollision(ssd, &distInfo);
								break;
							}
							case SSTM_GJKMPR:
							case SSTM_GJKEPA:
							case SSTM_GJKEPA_RADIUS_NOT_FULL_MARGIN:
							{
								result = btComputeGjkEpaSphereSphereCollision(ssd, &distInfo, method);
								break;
							}
							default:
							{
								ASSERT_EQ(0, 1);
								btAssert(0);
								break;
							}
						}
							//  int result = btComputeSphereSphereCollision(ssd,&distInfo);
#if 0
                        printf("sphereA(pos=[%f,%f,%f],r=%f)-sphereB(pos=[%f,%f,%f],r=%f) Dist=%f,normalOnB[%f,%f,%f],pA=[%f,%f,%f],pB[%f,%f,%f]\n",
                               ssd.m_sphereTransformA.getOrigin()[0],ssd.m_sphereTransformA.getOrigin()[1],ssd.m_sphereTransformA.getOrigin()[2],ssd.m_radiusA,
                               ssd.m_sphereTransformB.getOrigin()[0],ssd.m_sphereTransformB.getOrigin()[1],ssd.m_sphereTransformB.getOrigin()[2],ssd.m_radiusB,
                               distInfo.m_distance,distInfo.m_normalBtoA[0],distInfo.m_normalBtoA[1],distInfo.m_normalBtoA[2],
                               distInfo.m_pointOnA[0],distInfo.m_pointOnA[1],distInfo.m_pointOnA[2],
                               distInfo.m_pointOnB[0],distInfo.m_pointOnB[1],distInfo.m_pointOnB[2]);
#endif
						ASSERT_EQ(0, result);
						ASSERT_NEAR(btFabs(btScalar(i - z)) - btScalar(j) - ssd.m_radiusB, distInfo.m_distance, abs_error);
						btVector3 computedA = distInfo.m_pointOnB + distInfo.m_distance * distInfo.m_normalBtoA;
						ASSERT_NEAR(computedA.x(), distInfo.m_pointOnA.x(), abs_error);
						ASSERT_NEAR(computedA.y(), distInfo.m_pointOnA.y(), abs_error);
						ASSERT_NEAR(computedA.z(), distInfo.m_pointOnA.z(), abs_error);
					}
				}
			}
		}
}

TEST(BulletCollisionTest, GjkMPRSphereSphereDistance)
{
	testSphereSphereDistance(SSTM_GJKMPR, 0.0001);
}

TEST(BulletCollisionTest, GjkEpaSphereSphereDistance)
{
	testSphereSphereDistance(SSTM_GJKEPA, 0.00001);
}

TEST(BulletCollisionTest, GjkEpaSphereSphereRadiusNotFullMarginDistance)
{
	testSphereSphereDistance(SSTM_GJKEPA_RADIUS_NOT_FULL_MARGIN, 0.1);
}

TEST(BulletCollisionTest, AnalyticSphereSphereDistance)
{
	testSphereSphereDistance(SSTM_ANALYTIC, 0.00001);
}

class TriangleCollector : public btTriangleCallback
{
public:
	std::vector<btVector3> *triangles;

	explicit TriangleCollector(std::vector<btVector3>* triangles) : triangles(triangles) {}
	virtual ~TriangleCollector() {}

	virtual void processTriangle(btVector3* triangle, int partId, int triangleIndex)
	{
		triangles->push_back(*triangle);
	}
};

TEST(BulletCollisionTest, Heightfield_ProcessAllTriangles_FiltersByUpAxis)
{
	// A flat 2x2 heightfield.
	const btScalar heightFieldData[] = {
		10.0, 10.0,
		10.0, 10.0,
	};
	btHeightfieldTerrainShape shape(
		/*heightStickWidth=*/2, /*heightStickLength=*/2,
		&heightFieldData[0], /*heightScale=*/1,
		/*minHeight=*/-10.0, /*maxHeight=*/10.0,
		/*upAxis=*/2, PHY_FLOAT, /*flipQuadEdges=*/false);

	std::vector<btVector3> triangles;
	TriangleCollector collector(&triangles);

	// AABB overlaps with the heightfield on upAxis.
	shape.processAllTriangles(&collector, btVector3(0, 0, 0), btVector3(20, 20, 20));
	EXPECT_EQ(triangles.size(), 2);

	// AABB does not overlap with the heightfield on upAxis.
	triangles.clear();
	shape.processAllTriangles(&collector, btVector3(0, 0, 0), btVector3(20, 20, 5));
	EXPECT_EQ(triangles.size(), 0);
}

}  // namespace

int main(int argc, char** argv)
{
#if _MSC_VER
	_CrtSetDbgFlag(_CRTDBG_ALLOC_MEM_DF | _CRTDBG_LEAK_CHECK_DF);
	//void *testWhetherMemoryLeakDetectionWorks = malloc(1);
#endif
	::testing::InitGoogleTest(&argc, argv);
	return RUN_ALL_TESTS();
}