set hull from vertex format

The user can create a convex hull from a vertex format, not directly from a list of vertices. That's interesting because in some 3D engines convex hulls are typically created from a render mesh that might structure a vertex in different ways in the memory.
This commit is contained in:
Irlan 2018-09-04 17:52:17 -03:00
parent 4bef8b11d2
commit 6486770fc1
5 changed files with 34 additions and 27 deletions

View File

@ -81,7 +81,7 @@ public:
m_points[m_count++] = p;
}
m_hull.Set(m_points, m_count);
m_hull.Set(sizeof(b3Vec3), m_points, m_count);
assert(m_hull.faceCount > 0);
m_selection = m_hull.GetSupportFace(b3Vec3_z);
}

View File

@ -85,14 +85,14 @@ public:
void Step()
{
b3QHull hull1;
hull1.Set(m_points1, e_count);
hull1.Set(sizeof(b3Vec3), m_points1, e_count);
b3HullShape sA;
sA.m_hull = &hull1;
m_shapeA = &sA;
b3QHull hull2;
hull2.Set(m_points2, e_count);
hull2.Set(sizeof(b3Vec3), m_points2, e_count);
b3HullShape sB;
sB.m_hull = &hull2;

View File

@ -66,7 +66,7 @@ public:
points[j] = p;
}
hull->Set(points, count);
hull->Set(sizeof(b3Vec3), points, count);
}
{

View File

@ -42,11 +42,13 @@ struct b3QHull : public b3Hull
centroid.SetZero();
}
// Create a convex hull from an array of points.
// Create a convex hull from vertex data.
// If the creation has failed then this convex hull is not modified.
// If the flag simplify is set to true then the convex hull is simplified after
// initial construction.
void Set(const b3Vec3* points, u32 count, bool simplify = true);
// vertexStride - size of bytes between vertices
// vertexBase - pointer to the first vertex
// vertexCount - number of vertices
// simplify - if set to true the convex hull is simplified after initial construction
void Set(u32 vertexStride, const void* vertexBase, u32 vertexCount, bool simplify = true);
// Set this hull as a sphere located at the origin
// given the radius.

View File

@ -98,22 +98,27 @@ static b3Vec3 b3ComputeCentroid(b3QHull* hull)
return centroid;
}
void b3QHull::Set(const b3Vec3* points, u32 count, bool simplify)
void b3QHull::Set(u32 vtxStride, const void* vtxBase, u32 vtxCount, bool simplify)
{
B3_ASSERT(count >= 4);
B3_ASSERT(vtxStride >= sizeof(b3Vec3));
B3_ASSERT(vtxCount >= 4);
// Copy points into local buffer, perform welding.
u32 psCount = 0;
b3Vec3* ps = (b3Vec3*)b3Alloc(count * sizeof(b3Vec3));
for (u32 i = 0; i < count; ++i)
// Copy vertices into local buffer, perform welding.
u32 vs0Count = 0;
b3Vec3* vs0 = (b3Vec3*)b3Alloc(vtxCount * sizeof(b3Vec3));
for (u32 i = 0; i < vtxCount; ++i)
{
b3Vec3 p = points[i];
b3Vec3 v = *(b3Vec3*)((u8*)vtxBase + vtxStride * i);
B3_ASSERT(b3IsValid(v.x));
B3_ASSERT(b3IsValid(v.y));
B3_ASSERT(b3IsValid(v.z));
bool unique = true;
for (u32 j = 0; j < psCount; ++j)
for (u32 j = 0; j < vs0Count; ++j)
{
if (b3DistanceSquared(p, ps[j]) <= B3_LINEAR_SLOP * B3_LINEAR_SLOP)
if (b3DistanceSquared(v, vs0[j]) <= B3_LINEAR_SLOP * B3_LINEAR_SLOP)
{
unique = false;
break;
@ -122,14 +127,14 @@ void b3QHull::Set(const b3Vec3* points, u32 count, bool simplify)
if (unique)
{
ps[psCount++] = p;
vs0[vs0Count++] = v;
}
}
if (psCount < 4)
if (vs0Count < 4)
{
// Polyhedron is degenerate.
b3Free(ps);
b3Free(vs0);
return;
}
@ -139,8 +144,8 @@ void b3QHull::Set(const b3Vec3* points, u32 count, bool simplify)
if (simplify == true)
{
qhHull primary;
primary.Construct(ps, psCount);
b3Free(ps);
primary.Construct(vs0, vs0Count);
b3Free(vs0);
// Simplify the constructed hull.
@ -251,8 +256,8 @@ void b3QHull::Set(const b3Vec3* points, u32 count, bool simplify)
}
else
{
hull.Construct(ps, psCount);
b3Free(ps);
hull.Construct(vs0, vs0Count);
b3Free(vs0);
}
// Convert the constructed hull into a run-time hull.
@ -371,7 +376,7 @@ void b3QHull::SetAsSphere(float32 radius)
}
// Set
Set(vs, e_vertexCount, false);
Set(sizeof(b3Vec3), vs, e_vertexCount, false);
}
void b3QHull::SetAsCylinder(float32 radius, float32 ey)
@ -423,7 +428,7 @@ void b3QHull::SetAsCylinder(float32 radius, float32 ey)
}
// Set
Set(vs, count, false);
Set(sizeof(b3Vec3), vs, count, false);
}
void b3QHull::SetAsCone(float32 radius, float32 ey)
@ -458,5 +463,5 @@ void b3QHull::SetAsCone(float32 radius, float32 ey)
vs[count++].Set(0.0f, ey, 0.0f);
// Set
Set(vs, count, false);
Set(sizeof(b3Vec3), vs, count, false);
}