Skip to content

Commit

Permalink
Implemented constraint and collision filter API and visualization
Browse files Browse the repository at this point in the history
  • Loading branch information
jratcliff committed Aug 30, 2017
1 parent fc04ace commit a9222e6
Show file tree
Hide file tree
Showing 8 changed files with 341 additions and 79 deletions.
Binary file modified DebugView/ConvexDecomposition.exe
Binary file not shown.
182 changes: 171 additions & 11 deletions DebugView/TestHACD.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,30 +35,71 @@ class TestHACDImpl : public TestHACD, public VHACD::IVHACD::IUserCallback, publi
dest[2] = float(source[2] + diff[2] + center[2]);
}

virtual void render(float explodeViewScale,const float center[3],bool wireframe) final
void getExplodePosition(const float source[3], float dest[3], const double diff[3], const float center[3])
{
dest[0] = float(source[0] + diff[0] + center[0]);
dest[1] = float(source[1] + diff[1] + center[1]);
dest[2] = float(source[2] + diff[2] + center[2]);
}

void getExplodeViewPosition(const float center[3], const VHACD::IVHACD::ConvexHull &h, float explodeViewScale, const double sourcePos[3], float destPos[3])
{
double diff[3];

diff[0] = h.m_center[0] - center[0];
diff[1] = h.m_center[1] - center[1];
diff[2] = h.m_center[2] - center[2];

diff[0] *= explodeViewScale;
diff[1] *= explodeViewScale;
diff[2] *= explodeViewScale;

diff[0] -= h.m_center[0];
diff[1] -= h.m_center[1];
diff[2] -= h.m_center[2];

getExplodePosition(sourcePos, destPos, diff, center);
}

virtual void render(float explodeViewScale,
const float center[3],
bool wireframe,
bool showConstraints,
bool showSkeleton,
bool showCollisionPairs) final
{
uint32_t hullCount = mHACD->GetNConvexHulls();
if (hullCount)
{
if (!mHaveConstraints)
{
mHaveConstraints = true;
mHACD->ComputeConstraints();
}
mRenderDebug->pushRenderState();
float xform[16];
getTransform(xform);
mRenderDebug->setPose(xform);

// Render constraints here...
uint32_t constraintCount;
const VHACD::IVHACD::Constraint *constraints = mHACD->GetConstraints(constraintCount);

for (uint32_t j = 0; j < hullCount; j++)
{
VHACD::IVHACD::ConvexHull h;
mHACD->GetConvexHull(j, h);
{

if (wireframe)
{
mRenderDebug->removeFromCurrentState(RENDER_DEBUG::DebugRenderState::SolidShaded);
mRenderDebug->removeFromCurrentState(RENDER_DEBUG::DebugRenderState::SolidWireShaded);
}
else
{
mRenderDebug->addToCurrentState(RENDER_DEBUG::DebugRenderState::SolidWireShaded);
}
if (wireframe)
{
mRenderDebug->removeFromCurrentState(RENDER_DEBUG::DebugRenderState::SolidShaded);
mRenderDebug->removeFromCurrentState(RENDER_DEBUG::DebugRenderState::SolidWireShaded);
}
else
{
mRenderDebug->addToCurrentState(RENDER_DEBUG::DebugRenderState::SolidWireShaded);
}

uint32_t cindex = (j % 20) + RENDER_DEBUG::DebugColors::Red;

Expand Down Expand Up @@ -99,8 +140,100 @@ class TestHACDImpl : public TestHACD, public VHACD::IVHACD::IUserCallback, publi

mRenderDebug->debugTri(v1, v2, v3);
}

if (constraints && showConstraints)
{
mRenderDebug->pushRenderState();
for (uint32_t i = 0; i < constraintCount; i++)
{
const VHACD::IVHACD::Constraint &c = constraints[i];
if (c.mHullA == j)
{
float p1[3];
FLOAT_MATH::fm_doubleToFloat3(c.mConstraintPoint, p1);
float quat[4];
quat[0] = float(c.mConstraintOrientation[0]);
quat[1] = float(c.mConstraintOrientation[1]);
quat[2] = float(c.mConstraintOrientation[2]);
quat[3] = float(c.mConstraintOrientation[3]);
float transform[16];
FLOAT_MATH::fm_quatToMatrix(quat, transform);
transform[12] = p1[0];
transform[13] = p1[1];
transform[14] = p1[2];
float p2[3];
float basis[3] = { 1.5f, 0, 0, };
FLOAT_MATH::fm_transform(transform, basis, p2);
float v1[3];
float v2[3];
getExplodePosition(p1, v1, diff, center);
getExplodePosition(p2, v2, diff, center);
mRenderDebug->debugThickRay(v1, v2, 0.01f);
mRenderDebug->setCurrentColor(0xFF00FF);
mRenderDebug->addToCurrentState(RENDER_DEBUG::DebugRenderState::SolidShaded);
mRenderDebug->debugSphere(v1, 0.02f);
}
}
mRenderDebug->popRenderState();
}
}
}
if (showSkeleton)
{
for (uint32_t i = 0; i < constraintCount; i++)
{
const VHACD::IVHACD::Constraint &c = constraints[i];
VHACD::IVHACD::ConvexHull h1, h2;

mHACD->GetConvexHull(c.mHullA, h1);
mHACD->GetConvexHull(c.mHullB, h2);

float p1[3];
float p2[3];

getExplodeViewPosition(center, h1, explodeViewScale, h1.m_center, p1);
getExplodeViewPosition(center, h2, explodeViewScale, h2.m_center, p2);

mRenderDebug->pushRenderState();
mRenderDebug->setCurrentColor(0xFFFF00);
mRenderDebug->addToCurrentState(RENDER_DEBUG::DebugRenderState::SolidShaded);
mRenderDebug->debugSphere(p1, 0.1f);
mRenderDebug->debugSphere(p2, 0.1f);
mRenderDebug->debugThickRay(p1, p2, 0.02f);
mRenderDebug->popRenderState();
}
}
if (showCollisionPairs)
{
uint32_t collisionCount;
const uint32_t *pairs = mHACD->GetCollisionFilterPairs(collisionCount);
for (uint32_t i = 0; i < collisionCount; i++)
{
uint32_t hulla = pairs[i * 2];
uint32_t hullb = pairs[i * 2 + 1];

VHACD::IVHACD::ConvexHull h1, h2;

mHACD->GetConvexHull(hulla, h1);
mHACD->GetConvexHull(hullb, h2);

float p1[3];
float p2[3];

getExplodeViewPosition(center, h1, explodeViewScale, h1.m_center, p1);
getExplodeViewPosition(center, h2, explodeViewScale, h2.m_center, p2);

mRenderDebug->pushRenderState();
mRenderDebug->setCurrentColor(0xFF0000);
mRenderDebug->addToCurrentState(RENDER_DEBUG::DebugRenderState::SolidShaded);
mRenderDebug->debugSphere(p1, 0.1f);
mRenderDebug->debugSphere(p2, 0.1f);
mRenderDebug->debugThickRay(p1, p2, 0.02f);
mRenderDebug->popRenderState();

}
}

mRenderDebug->popRenderState();
}
else
Expand All @@ -121,6 +254,7 @@ class TestHACDImpl : public TestHACD, public VHACD::IVHACD::IUserCallback, publi
{
desc.m_callback = this;
desc.m_logger = this;
mHaveConstraints = false; // clear have constraints flag so they will be recomputed when the decomopsition is complete
mHACD->Compute(points, countPoints, triangles, countTriangles, desc);
}

Expand Down Expand Up @@ -159,6 +293,31 @@ class TestHACDImpl : public TestHACD, public VHACD::IVHACD::IUserCallback, publi
return mHACD ? mHACD->GetNConvexHulls() : 0;
}

virtual uint32_t getConstraintCount(void) const final
{
uint32_t ret = 0;

if (mHACD)
{
mHACD->GetConstraints(ret);
}

return ret;
}

virtual uint32_t getCollisionFilterCount(void) const final
{
uint32_t ret = 0;

if (mHACD)
{
mHACD->GetCollisionFilterPairs(ret);
}

return ret;
}


virtual void cancel(void)
{
if (mHACD)
Expand Down Expand Up @@ -296,7 +455,7 @@ class TestHACDImpl : public TestHACD, public VHACD::IVHACD::IUserCallback, publi
}
}

virtual void computeConstraints(void)
void computeConstraints(void)
{
if (mHACD)
{
Expand All @@ -316,6 +475,7 @@ class TestHACDImpl : public TestHACD, public VHACD::IVHACD::IUserCallback, publi
std::string mStage;
std::string mOperation;
float mCenterOfMass[3];
bool mHaveConstraints{ false };
};

TestHACD *TestHACD::create(RENDER_DEBUG::RenderDebug *renderDebug,NV_PHYSX_FRAMEWORK::PhysXFramework *pf)
Expand Down
11 changes: 8 additions & 3 deletions DebugView/TestHACD.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,16 +29,21 @@ class TestHACD
const uint32_t countTriangles,
VHACD::IVHACD::Parameters &desc) = 0;

virtual void render(float explodeViewScale,const float center[3],bool wireframe) = 0;
virtual void render(float explodeViewScale,
const float center[3],
bool wireframe,
bool showConstraints,
bool showSkeleton,
bool showCollisionPairs) = 0;

virtual void getTransform(float xform[16]) = 0;

virtual uint32_t getHullCount(void) const = 0;
virtual uint32_t getConstraintCount(void) const = 0;
virtual uint32_t getCollisionFilterCount(void) const = 0;

virtual void saveConvexDecomposition(const char *fname,const char *sourceMeshName) = 0;

virtual void computeConstraints(void) = 0;

virtual void cancel(void) = 0;


Expand Down
71 changes: 24 additions & 47 deletions DebugView/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,45 +37,6 @@ static std::string gSourceMeshName;
static VHACD::IVHACD::Parameters gDesc;


float fm_computePlane(const float *A,const float *B,const float *C,float *n) // returns D
{
float vx = (B[0] - C[0]);
float vy = (B[1] - C[1]);
float vz = (B[2] - C[2]);

float wx = (A[0] - B[0]);
float wy = (A[1] - B[1]);
float wz = (A[2] - B[2]);

float vw_x = vy * wz - vz * wy;
float vw_y = vz * wx - vx * wz;
float vw_z = vx * wy - vy * wx;

float mag = ::sqrtf((vw_x * vw_x) + (vw_y * vw_y) + (vw_z * vw_z));

if ( mag < 0.000001f )
{
mag = 0;
}
else
{
mag = 1.0f/mag;
}

float x = vw_x * mag;
float y = vw_y * mag;
float z = vw_z * mag;


float D = 0.0f - ((x*A[0])+(y*A[1])+(z*A[2]));

n[0] = x;
n[1] = y;
n[2] = z;

return D;
}

class MeshBuilder
{
public:
Expand Down Expand Up @@ -105,7 +66,7 @@ class MeshBuilder
void addTriangle(const float *p1,const float *p2,const float *p3)
{
float normal[3];
fm_computePlane(p3,p2,p1,normal);
FLOAT_MATH::fm_computePlane(p3,p2,p1,normal);

double nx = fabs(normal[0]);
double ny = fabs(normal[1]);
Expand Down Expand Up @@ -224,8 +185,10 @@ void createMenus(void)
gRenderDebug->sendRemoteCommand("EndGroup"); // End the group called 'HACD settings'

gRenderDebug->sendRemoteCommand("BeginGroup \"Simulation\""); // Mark the beginning of a group of controls.
gRenderDebug->sendRemoteCommand("CheckBox ShowConstraints true ShowConstraints");
gRenderDebug->sendRemoteCommand("CheckBox ShowSkeleton true ShowSkeleton");
gRenderDebug->sendRemoteCommand("CheckBox ShowCollisionPairs false ShowCollisionPairs");
gRenderDebug->sendRemoteCommand("Button ToggleSimulation ToggleSimulation");
gRenderDebug->sendRemoteCommand("Button ComputeConstraints ComputeConstraints");
gRenderDebug->sendRemoteCommand("EndGroup"); // End the group called 'controls'


Expand Down Expand Up @@ -289,6 +252,21 @@ class ConvexDecomposition : public NV_PHYSX_FRAMEWORK::PhysXFramework::CommandCa
const char *value = argv[1];
mShowPhysics = strcmp(value, "true") == 0;
}
else if (strcmp(cmd, "ShowConstraints") == 0 && argc == 2)
{
const char *value = argv[1];
mShowConstraints = strcmp(value, "true") == 0;
}
else if (strcmp(cmd, "ShowSkeleton") == 0 && argc == 2)
{
const char *value = argv[1];
mShowSkeleton = strcmp(value, "true") == 0;
}
else if (strcmp(cmd, "ShowCollisionPairs") == 0 && argc == 2)
{
const char *value = argv[1];
mShowCollisionPairs = strcmp(value, "true") == 0;
}
else if (strcmp(cmd, "SaveObj") == 0)
{
mWavefront.saveObj("wavefront.obj");
Expand All @@ -310,10 +288,6 @@ class ConvexDecomposition : public NV_PHYSX_FRAMEWORK::PhysXFramework::CommandCa
{
mTestHACD->toggleSimulation();
}
else if (strcmp(cmd, "ComputeConstraints") == 0 && mTestHACD)
{
mTestHACD->computeConstraints();
}
else if (strcmp(cmd, "raycast") == 0 && mTestHACD)
{
printf("Testing RaycastMesh\n");
Expand Down Expand Up @@ -452,7 +426,7 @@ class ConvexDecomposition : public NV_PHYSX_FRAMEWORK::PhysXFramework::CommandCa
gRenderDebug->debugText2D(0, 0.04f, 0.5f, 2.0f, false, 0xFFFF00, "%s", mMeshName.c_str());
if ( mTestHACD )
{
gRenderDebug->debugText2D(0, 0.08f, 0.5f, 2.0f, false, 0xFFFF00, "HullCount: %d", mTestHACD->getHullCount());
gRenderDebug->debugText2D(0, 0.08f, 0.5f, 2.0f, false, 0xFFFF00, "HullCount: %d ConstraintCount: %d CollisionFilterCount: %d", mTestHACD->getHullCount(), mTestHACD->getConstraintCount(), mTestHACD->getCollisionFilterCount());
}
gRenderDebug->addToCurrentState(RENDER_DEBUG::DebugRenderState::SolidWireShaded);
gRenderDebug->addToCurrentState(RENDER_DEBUG::DebugRenderState::CameraFacing);
Expand Down Expand Up @@ -517,7 +491,7 @@ class ConvexDecomposition : public NV_PHYSX_FRAMEWORK::PhysXFramework::CommandCa
}
if (mTestHACD && gShowConvexDecomposition)
{
mTestHACD->render(gExplodeViewScale, gCenter, mWireframeConvex);
mTestHACD->render(gExplodeViewScale, gCenter, mWireframeConvex, mShowConstraints, mShowSkeleton, mShowCollisionPairs);
}

gPhysXFramework->simulate(mShowPhysics);
Expand Down Expand Up @@ -569,6 +543,9 @@ class ConvexDecomposition : public NV_PHYSX_FRAMEWORK::PhysXFramework::CommandCa
}

uint32_t mMeshID{ 0 };
bool mShowConstraints{ true };
bool mShowCollisionPairs{ false };
bool mShowSkeleton{ true };
bool mShowPhysics{ true };
bool mSolid{ true };
bool mWireframeConvex{ false };
Expand Down
Loading

0 comments on commit a9222e6

Please sign in to comment.