Welcome to mirror list, hosted at ThFree Co, Russian Federation.

btGImpactShape.cpp « Gimpact « BulletCollision « src « bullet2 « extern - git.blender.org/blender.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 34c229a3abab5b556b83b4098c9d5616e76a74a4 (plain)
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
/*
This source file is part of GIMPACT Library.

For the latest info, see http://gimpact.sourceforge.net/

Copyright (c) 2007 Francisco Leon Najera. C.C. 80087371.
email: projectileman@yahoo.com


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.
*/

#include "btGImpactShape.h"
#include "btGImpactMassUtil.h"

btGImpactMeshShapePart::btGImpactMeshShapePart(btStridingMeshInterface* meshInterface, int part)
{
	// moved from .h to .cpp because of conditional compilation
	// (The setting of BT_THREADSAFE may differ between various cpp files, so it is best to
	// avoid using it in h files)
	m_primitive_manager.m_meshInterface = meshInterface;
	m_primitive_manager.m_part = part;
	m_box_set.setPrimitiveManager(&m_primitive_manager);
#if BT_THREADSAFE
	// If threadsafe is requested, this object uses a different lock/unlock
	//  model with the btStridingMeshInterface -- lock once when the object is constructed
	//  and unlock once in the destructor.
	// The other way of locking and unlocking for each collision check in the narrowphase
	// is not threadsafe.  Note these are not thread-locks, they are calls to the meshInterface's
	// getLockedReadOnlyVertexIndexBase virtual function, which by default just returns a couple of
	// pointers.  In theory a client could override the lock function to do all sorts of
	// things like reading data from GPU memory, or decompressing data on the fly, but such things
	// do not seem all that likely or useful, given the performance cost.
	m_primitive_manager.lock();
#endif
}

btGImpactMeshShapePart::~btGImpactMeshShapePart()
{
	// moved from .h to .cpp because of conditional compilation
#if BT_THREADSAFE
	m_primitive_manager.unlock();
#endif
}

void btGImpactMeshShapePart::lockChildShapes() const
{
	// moved from .h to .cpp because of conditional compilation
#if !BT_THREADSAFE
	// called in the narrowphase -- not threadsafe!
	void* dummy = (void*)(m_box_set.getPrimitiveManager());
	TrimeshPrimitiveManager* dummymanager = static_cast<TrimeshPrimitiveManager*>(dummy);
	dummymanager->lock();
#endif
}

void btGImpactMeshShapePart::unlockChildShapes() const
{
	// moved from .h to .cpp because of conditional compilation
#if !BT_THREADSAFE
	// called in the narrowphase -- not threadsafe!
	void* dummy = (void*)(m_box_set.getPrimitiveManager());
	TrimeshPrimitiveManager* dummymanager = static_cast<TrimeshPrimitiveManager*>(dummy);
	dummymanager->unlock();
#endif
}

#define CALC_EXACT_INERTIA 1

void btGImpactCompoundShape::calculateLocalInertia(btScalar mass, btVector3& inertia) const
{
	lockChildShapes();
#ifdef CALC_EXACT_INERTIA
	inertia.setValue(0.f, 0.f, 0.f);

	int i = this->getNumChildShapes();
	btScalar shapemass = mass / btScalar(i);

	while (i--)
	{
		btVector3 temp_inertia;
		m_childShapes[i]->calculateLocalInertia(shapemass, temp_inertia);
		if (childrenHasTransform())
		{
			inertia = gim_inertia_add_transformed(inertia, temp_inertia, m_childTransforms[i]);
		}
		else
		{
			inertia = gim_inertia_add_transformed(inertia, temp_inertia, btTransform::getIdentity());
		}
	}

#else

	// Calc box inertia

	btScalar lx = m_localAABB.m_max[0] - m_localAABB.m_min[0];
	btScalar ly = m_localAABB.m_max[1] - m_localAABB.m_min[1];
	btScalar lz = m_localAABB.m_max[2] - m_localAABB.m_min[2];
	const btScalar x2 = lx * lx;
	const btScalar y2 = ly * ly;
	const btScalar z2 = lz * lz;
	const btScalar scaledmass = mass * btScalar(0.08333333);

	inertia = scaledmass * (btVector3(y2 + z2, x2 + z2, x2 + y2));

#endif
	unlockChildShapes();
}

void btGImpactMeshShapePart::calculateLocalInertia(btScalar mass, btVector3& inertia) const
{
	lockChildShapes();

#ifdef CALC_EXACT_INERTIA
	inertia.setValue(0.f, 0.f, 0.f);

	int i = this->getVertexCount();
	btScalar pointmass = mass / btScalar(i);

	while (i--)
	{
		btVector3 pointintertia;
		this->getVertex(i, pointintertia);
		pointintertia = gim_get_point_inertia(pointintertia, pointmass);
		inertia += pointintertia;
	}

#else

	// Calc box inertia

	btScalar lx = m_localAABB.m_max[0] - m_localAABB.m_min[0];
	btScalar ly = m_localAABB.m_max[1] - m_localAABB.m_min[1];
	btScalar lz = m_localAABB.m_max[2] - m_localAABB.m_min[2];
	const btScalar x2 = lx * lx;
	const btScalar y2 = ly * ly;
	const btScalar z2 = lz * lz;
	const btScalar scaledmass = mass * btScalar(0.08333333);

	inertia = scaledmass * (btVector3(y2 + z2, x2 + z2, x2 + y2));

#endif

	unlockChildShapes();
}

void btGImpactMeshShape::calculateLocalInertia(btScalar mass, btVector3& inertia) const
{
#ifdef CALC_EXACT_INERTIA
	inertia.setValue(0.f, 0.f, 0.f);

	int i = this->getMeshPartCount();
	btScalar partmass = mass / btScalar(i);

	while (i--)
	{
		btVector3 partinertia;
		getMeshPart(i)->calculateLocalInertia(partmass, partinertia);
		inertia += partinertia;
	}

#else

	// Calc box inertia

	btScalar lx = m_localAABB.m_max[0] - m_localAABB.m_min[0];
	btScalar ly = m_localAABB.m_max[1] - m_localAABB.m_min[1];
	btScalar lz = m_localAABB.m_max[2] - m_localAABB.m_min[2];
	const btScalar x2 = lx * lx;
	const btScalar y2 = ly * ly;
	const btScalar z2 = lz * lz;
	const btScalar scaledmass = mass * btScalar(0.08333333);

	inertia = scaledmass * (btVector3(y2 + z2, x2 + z2, x2 + y2));

#endif
}

void btGImpactMeshShape::rayTest(const btVector3& rayFrom, const btVector3& rayTo, btCollisionWorld::RayResultCallback& resultCallback) const
{
}

void btGImpactMeshShapePart::processAllTrianglesRay(btTriangleCallback* callback, const btVector3& rayFrom, const btVector3& rayTo) const
{
	lockChildShapes();

	btAlignedObjectArray<int> collided;
	btVector3 rayDir(rayTo - rayFrom);
	rayDir.normalize();
	m_box_set.rayQuery(rayDir, rayFrom, collided);

	if (collided.size() == 0)
	{
		unlockChildShapes();
		return;
	}

	int part = (int)getPart();
	btPrimitiveTriangle triangle;
	int i = collided.size();
	while (i--)
	{
		getPrimitiveTriangle(collided[i], triangle);
		callback->processTriangle(triangle.m_vertices, part, collided[i]);
	}
	unlockChildShapes();
}

void btGImpactMeshShapePart::processAllTriangles(btTriangleCallback* callback, const btVector3& aabbMin, const btVector3& aabbMax) const
{
	lockChildShapes();
	btAABB box;
	box.m_min = aabbMin;
	box.m_max = aabbMax;

	btAlignedObjectArray<int> collided;
	m_box_set.boxQuery(box, collided);

	if (collided.size() == 0)
	{
		unlockChildShapes();
		return;
	}

	int part = (int)getPart();
	btPrimitiveTriangle triangle;
	int i = collided.size();
	while (i--)
	{
		this->getPrimitiveTriangle(collided[i], triangle);
		callback->processTriangle(triangle.m_vertices, part, collided[i]);
	}
	unlockChildShapes();
}

void btGImpactMeshShape::processAllTriangles(btTriangleCallback* callback, const btVector3& aabbMin, const btVector3& aabbMax) const
{
	int i = m_mesh_parts.size();
	while (i--)
	{
		m_mesh_parts[i]->processAllTriangles(callback, aabbMin, aabbMax);
	}
}

void btGImpactMeshShape::processAllTrianglesRay(btTriangleCallback* callback, const btVector3& rayFrom, const btVector3& rayTo) const
{
	int i = m_mesh_parts.size();
	while (i--)
	{
		m_mesh_parts[i]->processAllTrianglesRay(callback, rayFrom, rayTo);
	}
}

///fills the dataBuffer and returns the struct name (and 0 on failure)
const char* btGImpactMeshShape::serialize(void* dataBuffer, btSerializer* serializer) const
{
	btGImpactMeshShapeData* trimeshData = (btGImpactMeshShapeData*)dataBuffer;

	btCollisionShape::serialize(&trimeshData->m_collisionShapeData, serializer);

	m_meshInterface->serialize(&trimeshData->m_meshInterface, serializer);

	trimeshData->m_collisionMargin = float(m_collisionMargin);

	localScaling.serializeFloat(trimeshData->m_localScaling);

	trimeshData->m_gimpactSubType = int(getGImpactShapeType());

	return "btGImpactMeshShapeData";
}