update tests

This commit is contained in:
Irlan 2018-04-17 01:59:14 -03:00
parent 28367b8108
commit b85556a375
4 changed files with 14 additions and 364 deletions

View File

@ -26,20 +26,6 @@ class GyroMotion : public Test
public: public:
GyroMotion() GyroMotion()
{ {
{
b3StackArray<b3Vec3, 32> points;
ConstructCylinder(points, 0.95f, 4.0f);
const u32 size = qhGetMemorySize(points.Count());
void* p = b3Alloc(size);
qhHull hull;
hull.Construct(p, points);
m_cylinderHull = ConvertHull(hull);
b3Free(p);
}
{ {
b3BodyDef bd; b3BodyDef bd;
b3Body* ground = m_world.CreateBody(bd); b3Body* ground = m_world.CreateBody(bd);
@ -76,6 +62,8 @@ public:
} }
{ {
m_cylinderHull.SetAsCylinder(0.95f, 4.0f);
b3HullShape hull; b3HullShape hull;
hull.m_hull = &m_cylinderHull; hull.m_hull = &m_cylinderHull;
@ -92,12 +80,6 @@ public:
~GyroMotion() ~GyroMotion()
{ {
{
b3Free(m_cylinderHull.vertices);
b3Free(m_cylinderHull.edges);
b3Free(m_cylinderHull.faces);
b3Free(m_cylinderHull.planes);
}
} }
static Test* Create() static Test* Create()
@ -106,7 +88,7 @@ public:
} }
b3BoxHull m_rotorBox; b3BoxHull m_rotorBox;
b3Hull m_cylinderHull; b3QHull m_cylinderHull;
}; };
#endif #endif

View File

@ -19,8 +19,6 @@
#ifndef QUADRIC_SHAPES_H #ifndef QUADRIC_SHAPES_H
#define QUADRIC_SHAPES_H #define QUADRIC_SHAPES_H
#include <testbed/tests/quickhull_test.h>
class QuadricShapes : public Test class QuadricShapes : public Test
{ {
public: public:
@ -85,20 +83,8 @@ public:
b3Body* body = m_world.CreateBody(bdef); b3Body* body = m_world.CreateBody(bdef);
{ m_coneHull.SetAsCone();
b3StackArray<b3Vec3, 32> points;
ConstructCone(points);
u32 size = qhGetMemorySize(points.Count());
void* p = b3Alloc(size);
qhHull hull;
hull.Construct(p, points);
m_coneHull = ConvertHull(hull);
b3Free(p);
}
b3HullShape hull; b3HullShape hull;
hull.m_hull = &m_coneHull; hull.m_hull = &m_coneHull;
@ -117,19 +103,7 @@ public:
b3Body* body = m_world.CreateBody(bdef); b3Body* body = m_world.CreateBody(bdef);
{ m_cylinderHull.SetAsCylinder();
b3StackArray<b3Vec3, 32> points;
ConstructCylinder(points);
const u32 size = qhGetMemorySize(points.Count());
void* p = b3Alloc(size);
qhHull hull;
hull.Construct(p, points);
m_cylinderHull = ConvertHull(hull);
b3Free(p);
}
b3HullShape hull; b3HullShape hull;
hull.m_hull = &m_cylinderHull; hull.m_hull = &m_cylinderHull;
@ -145,19 +119,6 @@ public:
~QuadricShapes() ~QuadricShapes()
{ {
{
b3Free(m_coneHull.vertices);
b3Free(m_coneHull.edges);
b3Free(m_coneHull.faces);
b3Free(m_coneHull.planes);
}
{
b3Free(m_cylinderHull.vertices);
b3Free(m_cylinderHull.edges);
b3Free(m_cylinderHull.faces);
b3Free(m_cylinderHull.planes);
}
} }
static Test* Create() static Test* Create()
@ -165,8 +126,8 @@ public:
return new QuadricShapes(); return new QuadricShapes();
} }
b3Hull m_coneHull; b3QHull m_coneHull;
b3Hull m_cylinderHull; b3QHull m_cylinderHull;
}; };
#endif #endif

View File

@ -21,259 +21,6 @@
#include <bounce/quickhull/qh_hull.h> #include <bounce/quickhull/qh_hull.h>
struct Pair
{
void* key;
u8 value;
};
struct Map
{
void Add(const Pair& pair)
{
m_pairs.PushBack(pair);
}
Pair* Find(void* key)
{
for (u32 i = 0; i < m_pairs.Count(); ++i)
{
Pair* pair = m_pairs.Get(i);
if (pair->key == key)
{
return pair;
}
}
return NULL;
}
b3StackArray<Pair, 256> m_pairs;
};
#define NULL_FEATURE 0xFF
inline b3Hull ConvertHull(const qhHull& hull)
{
u8 V = 0;
u8 E = 0;
u8 F = 0;
qhFace* face = hull.m_faceList.head;
while (face)
{
qhHalfEdge* e = face->edge;
do
{
++E;
++V;
e = e->next;
} while (e != face->edge);
++F;
face = face->next;
}
u8 vertexCount = 0;
b3Vec3* vertices = (b3Vec3*)b3Alloc(V * sizeof(b3Vec3));
u8 edgeCount = 0;
b3HalfEdge* edges = (b3HalfEdge*)b3Alloc(E * sizeof(b3HalfEdge));
u8 faceCount = 0;
b3Face* faces = (b3Face*)b3Alloc(F * sizeof(b3Face));
b3Plane* planes = (b3Plane*)b3Alloc(F * sizeof(b3Plane));
Map vertexMap;
Map edgeMap;
face = hull.m_faceList.head;
while (face)
{
B3_ASSERT(faceCount < F);
u8 iface = faceCount;
b3Face* f = faces + faceCount;
b3Plane* plane = planes + faceCount;
++faceCount;
*plane = face->plane;
b3StackArray<u8, 32> faceEdges;
qhHalfEdge* edge = face->edge;
do
{
qhHalfEdge* twin = edge->twin;
qhVertex* v1 = edge->tail;
qhVertex* v2 = twin->tail;
Pair* mte = edgeMap.Find(edge);
Pair* mv1 = vertexMap.Find(v1);
Pair* mv2 = vertexMap.Find(v2);
u8 iv1;
if (mv1)
{
iv1 = mv1->value;
}
else
{
B3_ASSERT(vertexCount < V);
iv1 = vertexCount;
vertices[iv1] = v1->position;
vertexMap.Add({ v1, iv1 });
++vertexCount;
}
u8 iv2;
if (mv2)
{
iv2 = mv2->value;
}
else
{
B3_ASSERT(vertexCount < V);
iv2 = vertexCount;
vertices[iv2] = v2->position;
vertexMap.Add({ v2, iv2 });
++vertexCount;
}
if (mte)
{
u8 ie2 = mte->value;
b3HalfEdge* e2 = edges + ie2;
B3_ASSERT(e2->face == NULL_FEATURE);
e2->face = iface;
faceEdges.PushBack(ie2);
}
else
{
B3_ASSERT(edgeCount < E);
u8 ie1 = edgeCount;
b3HalfEdge* e1 = edges + edgeCount;
++edgeCount;
B3_ASSERT(edgeCount < E);
u8 ie2 = edgeCount;
b3HalfEdge* e2 = edges + edgeCount;
++edgeCount;
e1->face = iface;
e1->origin = iv1;
e1->twin = ie2;
e2->face = NULL_FEATURE;
e2->origin = iv2;
e2->twin = ie1;
faceEdges.PushBack(ie1);
edgeMap.Add({ edge, ie1 } );
edgeMap.Add({ twin, ie2 } );
}
edge = edge->next;
} while (edge != face->edge);
f->edge = faceEdges[0];
for (u32 i = 0; i < faceEdges.Count(); ++i)
{
u32 j = i < faceEdges.Count() - 1 ? i + 1 : 0;
edges[faceEdges[i]].next = faceEdges[j];
}
face = face->next;
}
b3Hull out;
out.vertexCount = vertexCount;
out.vertices = vertices;
out.edgeCount = edgeCount;
out.edges = edges;
out.faceCount = faceCount;
out.faces = faces;
out.planes = planes;
out.centroid = out.GetCentroid();
out.Validate();
return out;
}
inline void ConstructCylinder(b3Array<b3Vec3>& points, float32 radius = 1.0f, float32 height = 1.0f)
{
u32 kEdgeCount = 20;
float32 kAngleInc = 2.0f * B3_PI / float32(kEdgeCount);
b3Vec3 normal(0.0f, 1.0f, 0.0f);
b3Quat q(normal, kAngleInc);
points.Resize(4 * kEdgeCount);
u32 j = 0;
{
b3Vec3 center(0.0f, 0.0f, 0.0f);
b3Vec3 n1(1.0f, 0.0f, 0.0f);
b3Vec3 v1 = center + radius * n1;
for (u32 i = 0; i < kEdgeCount; ++i)
{
b3Vec3 n2 = b3Mul(q, n1);
b3Vec3 v2 = center + radius * n2;
points[j++] = v1;
points[j++] = v2;
n1 = n2;
v1 = v2;
}
}
{
b3Vec3 center(0.0f, height, 0.0f);
b3Vec3 n1(1.0f, 0.0f, 0.0f);
b3Vec3 v1 = center + radius * n1;
for (u32 i = 0; i < kEdgeCount; ++i)
{
b3Vec3 n2 = b3Mul(q, n1);
b3Vec3 v2 = center + radius * n2;
points[j++] = v1;
points[j++] = v2;
n1 = n2;
v1 = v2;
}
}
}
inline void ConstructCone(b3Array<b3Vec3>& points, float32 radius = 1.0f, float32 height = 1.0f)
{
u32 kEdgeCount = 20;
float32 kAngleInc = 2.0f * B3_PI / float32(kEdgeCount);
b3Vec3 normal(0.0f, 1.0f, 0.0f);
b3Quat q(normal, kAngleInc);
points.Resize(2 * kEdgeCount + 1);
u32 j = 0;
{
b3Vec3 center(0.0f, 0.0f, 0.0f);
b3Vec3 n1(1.0f, 0.0f, 0.0f);
b3Vec3 v1 = center + radius * n1;
for (u32 i = 0; i < kEdgeCount; ++i)
{
b3Vec3 n2 = b3Mul(q, n1);
b3Vec3 v2 = center + radius * n2;
points[j++] = v1;
points[j++] = v2;
n1 = n2;
v1 = v2;
}
}
b3Vec3 c(0.0f, height, 0.0f);
points[j++] = c;
}
class QuickhullTest : public Test class QuickhullTest : public Test
{ {
public: public:
@ -317,7 +64,8 @@ public:
void Step() void Step()
{ {
m_qhull.Draw(g_draw); g_draw->DrawString(b3Color_white, "Iterations = %d", m_qhull.GetIterations());
m_qhull.Draw();
} }
static Test* Create() static Test* Create()

View File

@ -163,52 +163,14 @@ public:
} }
} }
{ m_coneHull.SetAsCone();
b3StackArray<b3Vec3, 32> points; m_cylinderHull.SetAsCylinder();
ConstructCone(points);
u32 size = qhGetMemorySize(points.Count());
void* p = b3Alloc(size);
qhHull hull;
hull.Construct(p, points);
m_coneHull = ConvertHull(hull);
b3Free(p);
}
{
b3StackArray<b3Vec3, 32> points;
ConstructCylinder(points);
const u32 size = qhGetMemorySize(points.Count());
void* p = b3Alloc(size);
qhHull hull;
hull.Construct(p, points);
m_cylinderHull = ConvertHull(hull);
b3Free(p);
}
m_count = 0; m_count = 0;
} }
~Tumbler() ~Tumbler()
{ {
{
b3Free(m_coneHull.vertices);
b3Free(m_coneHull.edges);
b3Free(m_coneHull.faces);
b3Free(m_coneHull.planes);
}
{
b3Free(m_cylinderHull.vertices);
b3Free(m_cylinderHull.edges);
b3Free(m_cylinderHull.faces);
b3Free(m_cylinderHull.planes);
}
} }
void Step() void Step()
@ -263,11 +225,8 @@ public:
bdef.angularVelocity.Set(0.0f, 0.05f * B3_PI, 0.0f); bdef.angularVelocity.Set(0.0f, 0.05f * B3_PI, 0.0f);
b3Body* body = m_world.CreateBody(bdef); b3Body* body = m_world.CreateBody(bdef);
static b3BoxHull box;
box.SetIdentity();
b3HullShape hs; b3HullShape hs;
hs.m_hull = &box; hs.m_hull = &b3BoxHull_identity;
b3ShapeDef sd; b3ShapeDef sd;
sd.density = 0.05f; sd.density = 0.05f;
@ -322,8 +281,8 @@ public:
} }
u32 m_count; u32 m_count;
b3Hull m_coneHull; b3QHull m_coneHull;
b3Hull m_cylinderHull; b3QHull m_cylinderHull;
}; };
#endif #endif