fix mesh size, decouple mesh topology from geometry, add polygon triangulation to make mass calculation O(n) in memory
This commit is contained in:
parent
4f18e46268
commit
d59b67c3c3
@ -22,6 +22,43 @@
|
|||||||
#include <bounce/common/geometry.h>
|
#include <bounce/common/geometry.h>
|
||||||
#include <bounce/collision/trees/static_tree.h>
|
#include <bounce/collision/trees/static_tree.h>
|
||||||
|
|
||||||
|
// A triangle in indexed form.
|
||||||
|
struct b3Triangle
|
||||||
|
{
|
||||||
|
// Does nothing for performance.
|
||||||
|
b3Triangle() { }
|
||||||
|
|
||||||
|
// Set this triangle from three vertices.
|
||||||
|
b3Triangle(u32 _v1, u32 _v2, u32 _v3)
|
||||||
|
{
|
||||||
|
v1 = _v1;
|
||||||
|
v2 = _v2;
|
||||||
|
v3 = _v3;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Set this triangle from three vertices.
|
||||||
|
void Set(u32 _v1, u32 _v2, u32 _v3)
|
||||||
|
{
|
||||||
|
v1 = _v1;
|
||||||
|
v2 = _v2;
|
||||||
|
v3 = _v3;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Test if this triangle contains a given vertex.
|
||||||
|
bool TestVertex(u32 v) const
|
||||||
|
{
|
||||||
|
return v == v1 || v == v2 || v == v3;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Test if this triangle contains two vertices.
|
||||||
|
bool TestEdge(u32 _v1, u32 _v2) const
|
||||||
|
{
|
||||||
|
return TestVertex(_v1) && TestVertex(_v2);
|
||||||
|
}
|
||||||
|
|
||||||
|
u32 v1, v2, v3;
|
||||||
|
};
|
||||||
|
|
||||||
struct b3Mesh
|
struct b3Mesh
|
||||||
{
|
{
|
||||||
u32 vertexCount;
|
u32 vertexCount;
|
||||||
@ -102,13 +139,12 @@ inline b3AABB3 b3Mesh::GetTriangleAABB(u32 index) const
|
|||||||
|
|
||||||
inline u32 b3Mesh::GetSize() const
|
inline u32 b3Mesh::GetSize() const
|
||||||
{
|
{
|
||||||
u32 memory = 0;
|
u32 size = 0;
|
||||||
memory += sizeof(b3Mesh);
|
size += sizeof(b3Mesh);
|
||||||
memory += sizeof(b3Vec3) * vertexCount;
|
size += sizeof(b3Vec3) * vertexCount;
|
||||||
memory += sizeof(b3Triangle) * triangleCount;
|
size += sizeof(b3Triangle) * triangleCount;
|
||||||
memory += sizeof(b3Plane) * triangleCount;
|
size += tree.GetSize();
|
||||||
memory += sizeof(b3StaticTree);
|
return size;
|
||||||
return memory;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
@ -57,6 +57,8 @@ public:
|
|||||||
|
|
||||||
// Draw this tree.
|
// Draw this tree.
|
||||||
void Draw(b3Draw* draw) const;
|
void Draw(b3Draw* draw) const;
|
||||||
|
|
||||||
|
u32 GetSize() const;
|
||||||
private :
|
private :
|
||||||
// A node in a static tree.
|
// A node in a static tree.
|
||||||
struct b3Node
|
struct b3Node
|
||||||
@ -198,4 +200,12 @@ inline void b3StaticTree::RayCast(T* callback, const b3RayCastInput& input) cons
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
inline u32 b3StaticTree::GetSize() const
|
||||||
|
{
|
||||||
|
u32 size = 0;
|
||||||
|
size += sizeof(b3StaticTree);
|
||||||
|
size += m_nodeCount * sizeof(b3Node);
|
||||||
|
return size;
|
||||||
|
}
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
@ -22,43 +22,6 @@
|
|||||||
#include <bounce/common/math/math.h>
|
#include <bounce/common/math/math.h>
|
||||||
#include <bounce/common/math/transform.h>
|
#include <bounce/common/math/transform.h>
|
||||||
|
|
||||||
// A triangle in indexed form.
|
|
||||||
struct b3Triangle
|
|
||||||
{
|
|
||||||
// Does nothing for performance.
|
|
||||||
b3Triangle() { }
|
|
||||||
|
|
||||||
// Set this triangle from three vertices.
|
|
||||||
b3Triangle(u32 _v1, u32 _v2, u32 _v3)
|
|
||||||
{
|
|
||||||
v1 = _v1;
|
|
||||||
v2 = _v2;
|
|
||||||
v3 = _v3;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Set this triangle from three vertices.
|
|
||||||
void Set(u32 _v1, u32 _v2, u32 _v3)
|
|
||||||
{
|
|
||||||
v1 = _v1;
|
|
||||||
v2 = _v2;
|
|
||||||
v3 = _v3;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Test if this triangle contains a given vertex.
|
|
||||||
bool TestVertex(u32 v) const
|
|
||||||
{
|
|
||||||
return v == v1 || v == v2 || v == v3;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Test if this triangle contains two vertices.
|
|
||||||
bool TestEdge(u32 _v1, u32 _v2) const
|
|
||||||
{
|
|
||||||
return TestVertex(_v1) && TestVertex(_v2);
|
|
||||||
}
|
|
||||||
|
|
||||||
u32 v1, v2, v3;
|
|
||||||
};
|
|
||||||
|
|
||||||
// A plane in constant normal form.
|
// A plane in constant normal form.
|
||||||
// dot(n, p) - d = 0.
|
// dot(n, p) - d = 0.
|
||||||
struct b3Plane
|
struct b3Plane
|
||||||
|
@ -39,41 +39,6 @@ void b3HullShape::Swap(const b3HullShape& other)
|
|||||||
|
|
||||||
void b3HullShape::ComputeMass(b3MassData* massData, float32 density) const
|
void b3HullShape::ComputeMass(b3MassData* massData, float32 density) const
|
||||||
{
|
{
|
||||||
// Build triangles for hull
|
|
||||||
b3StackArray<b3Triangle, 256> triangles;
|
|
||||||
|
|
||||||
// Use a small buffer for polygons.
|
|
||||||
u32 polygon[B3_MAX_HULL_FEATURES];
|
|
||||||
u32 vCount = 0;
|
|
||||||
|
|
||||||
// Convert polygons to triangles
|
|
||||||
for (u32 i = 0; i < m_hull->faceCount; ++i)
|
|
||||||
{
|
|
||||||
// Build convex polygon for loop
|
|
||||||
const b3Face* face = m_hull->GetFace(i);
|
|
||||||
const b3HalfEdge* begin = m_hull->GetEdge(face->edge);
|
|
||||||
const b3HalfEdge* edge = begin;
|
|
||||||
do
|
|
||||||
{
|
|
||||||
polygon[vCount++] = u32(edge->origin);
|
|
||||||
edge = m_hull->GetEdge(edge->next);
|
|
||||||
} while (edge != begin);
|
|
||||||
|
|
||||||
// Triangulate convex polygon
|
|
||||||
B3_ASSERT(vCount > 2);
|
|
||||||
for (u32 j = 1; j < vCount - 1; ++j)
|
|
||||||
{
|
|
||||||
b3Triangle triangle;
|
|
||||||
triangle.v1 = polygon[0];
|
|
||||||
triangle.v2 = polygon[j];
|
|
||||||
triangle.v3 = polygon[j + 1];
|
|
||||||
triangles.PushBack(triangle);
|
|
||||||
}
|
|
||||||
|
|
||||||
vCount = 0;
|
|
||||||
}
|
|
||||||
vCount = 0;
|
|
||||||
|
|
||||||
// Compute mass data
|
// Compute mass data
|
||||||
b3Vec3 center(0.0f, 0.0f, 0.0f);
|
b3Vec3 center(0.0f, 0.0f, 0.0f);
|
||||||
float32 volume = 0.0f;
|
float32 volume = 0.0f;
|
||||||
@ -92,13 +57,25 @@ void b3HullShape::ComputeMass(b3MassData* massData, float32 density) const
|
|||||||
b3Vec3 diag(0.0f, 0.0f, 0.0f);
|
b3Vec3 diag(0.0f, 0.0f, 0.0f);
|
||||||
b3Vec3 offDiag(0.0f, 0.0f, 0.0f);
|
b3Vec3 offDiag(0.0f, 0.0f, 0.0f);
|
||||||
|
|
||||||
for (u32 i = 0; i < triangles.Count(); ++i)
|
// Triangulate convex polygons
|
||||||
|
for (u32 i = 0; i < m_hull->faceCount; ++i)
|
||||||
{
|
{
|
||||||
const b3Triangle* triangle = triangles.Get(i);
|
const b3Face* face = m_hull->GetFace(i);
|
||||||
|
const b3HalfEdge* begin = m_hull->GetEdge(face->edge);
|
||||||
|
|
||||||
b3Vec3 v2 = m_hull->GetVertex(triangle->v1);
|
const b3HalfEdge* edge = m_hull->GetEdge(begin->next);
|
||||||
b3Vec3 v3 = m_hull->GetVertex(triangle->v2);
|
do
|
||||||
b3Vec3 v4 = m_hull->GetVertex(triangle->v3);
|
{
|
||||||
|
u32 i1 = begin->origin;
|
||||||
|
u32 i2 = edge->origin;
|
||||||
|
const b3HalfEdge* next = m_hull->GetEdge(edge->next);
|
||||||
|
u32 i3 = next->origin;
|
||||||
|
|
||||||
|
b3Vec3 v2 = m_hull->vertices[i1];
|
||||||
|
b3Vec3 v3 = m_hull->vertices[i2];
|
||||||
|
b3Vec3 v4 = m_hull->vertices[i3];
|
||||||
|
|
||||||
|
//
|
||||||
b3Vec3 tetraCenter = inv4 * (v1 + v2 + v3 + v4);
|
b3Vec3 tetraCenter = inv4 * (v1 + v2 + v3 + v4);
|
||||||
|
|
||||||
b3Vec3 e1 = v2 - v1;
|
b3Vec3 e1 = v2 - v1;
|
||||||
@ -127,6 +104,9 @@ void b3HullShape::ComputeMass(b3MassData* massData, float32 density) const
|
|||||||
e1[j1] * e3[j2] + e2[j1] * e1[j2] + e3[j1] * e2[j2] +
|
e1[j1] * e3[j2] + e2[j1] * e1[j2] + e3[j1] * e2[j2] +
|
||||||
e1[j1] * e1[j2] * 2.0f + e2[j1] * e2[j2] * 2.0f + e3[j1] * e3[j2] * 2.0f);
|
e1[j1] * e1[j2] * 2.0f + e2[j1] * e2[j2] * 2.0f + e3[j1] * e3[j2] * 2.0f);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
edge = next;
|
||||||
|
} while (m_hull->GetEdge(edge->next) != begin);
|
||||||
}
|
}
|
||||||
|
|
||||||
B3_ASSERT(volume > 0.0f);
|
B3_ASSERT(volume > 0.0f);
|
||||||
|
@ -467,39 +467,52 @@ struct DrawShapes
|
|||||||
void DrawHull(const b3HullShape* s, const b3Transform& xf)
|
void DrawHull(const b3HullShape* s, const b3Transform& xf)
|
||||||
{
|
{
|
||||||
const b3Hull* hull = s->m_hull;
|
const b3Hull* hull = s->m_hull;
|
||||||
|
|
||||||
for (u32 i = 0; i < hull->faceCount; ++i)
|
for (u32 i = 0; i < hull->faceCount; ++i)
|
||||||
{
|
{
|
||||||
b3Vec3 n = xf.rotation * hull->planes[i].normal;
|
|
||||||
|
|
||||||
glBegin(GL_POLYGON);
|
|
||||||
glNormal3f(n.x, n.y, n.z);
|
|
||||||
|
|
||||||
const b3Face* face = hull->GetFace(i);
|
const b3Face* face = hull->GetFace(i);
|
||||||
const b3HalfEdge* begin = hull->GetEdge(face->edge);
|
const b3HalfEdge* begin = hull->GetEdge(face->edge);
|
||||||
const b3HalfEdge* edge = begin;
|
|
||||||
|
b3Vec3 n = xf.rotation * hull->planes[i].normal;
|
||||||
|
|
||||||
|
const b3HalfEdge* edge = hull->GetEdge(begin->next);
|
||||||
do
|
do
|
||||||
{
|
{
|
||||||
b3Vec3 v = xf * hull->GetVertex(edge->origin);
|
u32 i1 = begin->origin;
|
||||||
|
u32 i2 = edge->origin;
|
||||||
|
const b3HalfEdge* next = hull->GetEdge(edge->next);
|
||||||
|
u32 i3 = next->origin;
|
||||||
|
|
||||||
glVertex3f(v.x, v.y, v.z);
|
b3Vec3 v1 = xf * hull->vertices[i1];
|
||||||
|
b3Vec3 v2 = xf * hull->vertices[i2];
|
||||||
|
b3Vec3 v3 = xf * hull->vertices[i3];
|
||||||
|
|
||||||
edge = hull->GetEdge(edge->next);
|
glBegin(GL_TRIANGLES);
|
||||||
} while (edge != begin);
|
glNormal3f(n.x, n.y, n.z);
|
||||||
|
|
||||||
|
glVertex3f(v1.x, v1.y, v1.z);
|
||||||
|
glVertex3f(v2.x, v2.y, v2.z);
|
||||||
|
glVertex3f(v3.x, v3.y, v3.z);
|
||||||
|
|
||||||
glEnd();
|
glEnd();
|
||||||
|
|
||||||
|
edge = next;
|
||||||
|
} while (hull->GetEdge(edge->next) != begin);
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void DrawMesh(const b3MeshShape* s, const b3Transform& xf)
|
void DrawMesh(const b3MeshShape* s, const b3Transform& xf)
|
||||||
{
|
{
|
||||||
const b3Mesh* mesh = s->m_mesh;
|
const b3Mesh* mesh = s->m_mesh;
|
||||||
|
|
||||||
for (u32 i = 0; i < mesh->triangleCount; ++i)
|
for (u32 i = 0; i < mesh->triangleCount; ++i)
|
||||||
{
|
{
|
||||||
const b3Triangle* triangle = mesh->triangles + i;
|
const b3Triangle* t = mesh->triangles + i;
|
||||||
|
|
||||||
b3Vec3 v1 = xf * mesh->vertices[triangle->v1];
|
b3Vec3 v1 = xf * mesh->vertices[t->v1];
|
||||||
b3Vec3 v2 = xf * mesh->vertices[triangle->v2];
|
b3Vec3 v2 = xf * mesh->vertices[t->v2];
|
||||||
b3Vec3 v3 = xf * mesh->vertices[triangle->v3];
|
b3Vec3 v3 = xf * mesh->vertices[t->v3];
|
||||||
|
|
||||||
b3Vec3 n = b3Cross(v2 - v1, v3 - v1);
|
b3Vec3 n = b3Cross(v2 - v1, v3 - v1);
|
||||||
n.Normalize();
|
n.Normalize();
|
||||||
|
Loading…
x
Reference in New Issue
Block a user