From b85556a3752c7f9f6b6e91abc0576144fdd9ab9b Mon Sep 17 00:00:00 2001 From: Irlan <-> Date: Tue, 17 Apr 2018 01:59:14 -0300 Subject: [PATCH] update tests --- examples/testbed/tests/gyro_motion.h | 24 +-- examples/testbed/tests/quadric_shapes.h | 47 +---- examples/testbed/tests/quickhull_test.h | 256 +----------------------- examples/testbed/tests/tumbler.h | 51 +---- 4 files changed, 14 insertions(+), 364 deletions(-) diff --git a/examples/testbed/tests/gyro_motion.h b/examples/testbed/tests/gyro_motion.h index aebc21b..c81bd5f 100644 --- a/examples/testbed/tests/gyro_motion.h +++ b/examples/testbed/tests/gyro_motion.h @@ -26,20 +26,6 @@ class GyroMotion : public Test public: GyroMotion() { - { - b3StackArray 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; b3Body* ground = m_world.CreateBody(bd); @@ -76,6 +62,8 @@ public: } { + m_cylinderHull.SetAsCylinder(0.95f, 4.0f); + b3HullShape hull; hull.m_hull = &m_cylinderHull; @@ -92,12 +80,6 @@ public: ~GyroMotion() { - { - b3Free(m_cylinderHull.vertices); - b3Free(m_cylinderHull.edges); - b3Free(m_cylinderHull.faces); - b3Free(m_cylinderHull.planes); - } } static Test* Create() @@ -106,7 +88,7 @@ public: } b3BoxHull m_rotorBox; - b3Hull m_cylinderHull; + b3QHull m_cylinderHull; }; #endif \ No newline at end of file diff --git a/examples/testbed/tests/quadric_shapes.h b/examples/testbed/tests/quadric_shapes.h index 43de380..8bb1395 100644 --- a/examples/testbed/tests/quadric_shapes.h +++ b/examples/testbed/tests/quadric_shapes.h @@ -19,8 +19,6 @@ #ifndef QUADRIC_SHAPES_H #define QUADRIC_SHAPES_H -#include - class QuadricShapes : public Test { public: @@ -85,20 +83,8 @@ public: b3Body* body = m_world.CreateBody(bdef); - { - b3StackArray points; - ConstructCone(points); + m_coneHull.SetAsCone(); - u32 size = qhGetMemorySize(points.Count()); - void* p = b3Alloc(size); - - qhHull hull; - hull.Construct(p, points); - m_coneHull = ConvertHull(hull); - - b3Free(p); - } - b3HullShape hull; hull.m_hull = &m_coneHull; @@ -117,19 +103,7 @@ public: b3Body* body = m_world.CreateBody(bdef); - { - b3StackArray 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_cylinderHull.SetAsCylinder(); b3HullShape hull; hull.m_hull = &m_cylinderHull; @@ -145,19 +119,6 @@ public: ~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() @@ -165,8 +126,8 @@ public: return new QuadricShapes(); } - b3Hull m_coneHull; - b3Hull m_cylinderHull; + b3QHull m_coneHull; + b3QHull m_cylinderHull; }; #endif \ No newline at end of file diff --git a/examples/testbed/tests/quickhull_test.h b/examples/testbed/tests/quickhull_test.h index 4292591..fec246a 100644 --- a/examples/testbed/tests/quickhull_test.h +++ b/examples/testbed/tests/quickhull_test.h @@ -21,259 +21,6 @@ #include -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 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 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& 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& 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 { public: @@ -317,7 +64,8 @@ public: void Step() { - m_qhull.Draw(g_draw); + g_draw->DrawString(b3Color_white, "Iterations = %d", m_qhull.GetIterations()); + m_qhull.Draw(); } static Test* Create() diff --git a/examples/testbed/tests/tumbler.h b/examples/testbed/tests/tumbler.h index d1405a2..133fe3c 100644 --- a/examples/testbed/tests/tumbler.h +++ b/examples/testbed/tests/tumbler.h @@ -163,52 +163,14 @@ public: } } - { - b3StackArray points; - ConstructCone(points); + m_coneHull.SetAsCone(); + m_cylinderHull.SetAsCylinder(); - u32 size = qhGetMemorySize(points.Count()); - void* p = b3Alloc(size); - - qhHull hull; - hull.Construct(p, points); - m_coneHull = ConvertHull(hull); - - b3Free(p); - } - - { - b3StackArray 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; } ~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() @@ -263,11 +225,8 @@ public: bdef.angularVelocity.Set(0.0f, 0.05f * B3_PI, 0.0f); b3Body* body = m_world.CreateBody(bdef); - static b3BoxHull box; - box.SetIdentity(); - b3HullShape hs; - hs.m_hull = &box; + hs.m_hull = &b3BoxHull_identity; b3ShapeDef sd; sd.density = 0.05f; @@ -322,8 +281,8 @@ public: } u32 m_count; - b3Hull m_coneHull; - b3Hull m_cylinderHull; + b3QHull m_coneHull; + b3QHull m_cylinderHull; }; #endif \ No newline at end of file