Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implemented hull point projection; optional setting to project hull vertices back onto the original source mesh #40

Merged
merged 7 commits into from
Aug 13, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -56,3 +56,10 @@ build/
/DebugView/compiler/vc14win64/x64/ConvexDecomposition/debug/wavefront.obj
/DebugView/compiler/vc14win64/x64/ConvexDecomposition/debug/ConvexDecompositionDEBUG.Build.CppClean.log
/DebugView/compiler/xpj/ConvexDecomposition.bak
/DebugView/compiler/vc14win64/ConvexDecomposition.obj
/DebugView/compiler/vc14win64/x64/ConvexDecomposition/debug/vhacdNearestPoint.obj
/DebugView/compiler/vc14win64/x64/ConvexDecomposition/release/vhacdNearestPoint.obj
/DebugView/compiler/vc14win64/x64/ConvexDecomposition/debug/RaycastMesh.obj
/DebugView/compiler/vc14win64/x64/ConvexDecomposition/debug/TestRaycast.obj
/DebugView/compiler/vc14win64/x64/ConvexDecomposition/release/RaycastMesh.obj
/DebugView/compiler/vc14win64/x64/ConvexDecomposition/release/TestRaycast.obj
Binary file modified DebugView/ConvexDecomposition.exe
Binary file not shown.
64 changes: 61 additions & 3 deletions DebugView/TestHACD.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,8 @@ 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]) final
virtual void render(float explodeViewScale,const float center[3],bool wireframe) final
{


uint32_t hullCount = mHACD->GetNConvexHulls();
if (hullCount)
{
Expand All @@ -39,6 +37,16 @@ class TestHACDImpl : public TestHACD, public VHACD::IVHACD::IUserCallback, publi
{
mRenderDebug->pushRenderState();

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;

uint32_t color = mRenderDebug->getDebugColor((RENDER_DEBUG::DebugColors::Enum)cindex);
Expand Down Expand Up @@ -146,6 +154,56 @@ class TestHACDImpl : public TestHACD, public VHACD::IVHACD::IUserCallback, publi
}
}

virtual void saveConvexDecomposition(const char *fname,const char *sourceMeshName)
{
FILE *fph = fopen(fname, "wb");
if (fph)
{
printf("Saving ConvexDecomposition to '%s\n", fname);
uint32_t hcount = mHACD->GetNConvexHulls();
printf("Saving %d convex hulls.\n", hcount);
fprintf(fph, "# ConvexDecomposition of %s contains %d convex hull parts.\n", sourceMeshName, hcount);
uint32_t vertIndex = 0;
uint32_t *offsets = new uint32_t[hcount];
for (uint32_t i = 0; i < hcount; i++)
{
VHACD::IVHACD::ConvexHull ch;
mHACD->GetConvexHull(i, ch);
printf("Hull %d contains %d vertices and %d triangles.\n", i + 1, ch.m_nPoints, ch.m_nTriangles);
fprintf(fph, "##########################################################################################\n");
fprintf(fph, "# Convex Hull %d contains %d vertices.\n", i + 1, ch.m_nPoints);
fprintf(fph, "##########################################################################################\n");
for (uint32_t j = 0; j < ch.m_nPoints; j++)
{
const double *pos = &ch.m_points[j * 3];
fprintf(fph, "v %0.9f %0.9f %0.9f\n", pos[0], pos[1], pos[2]);
}
offsets[i] = vertIndex;
vertIndex += ch.m_nPoints;
}
for (uint32_t i = 0; i < hcount; i++)
{
VHACD::IVHACD::ConvexHull ch;
mHACD->GetConvexHull(i, ch);
fprintf(fph, "##########################################################################################\n");
fprintf(fph, "# Convex Hull %d contains %d triangles.\n", i + 1, ch.m_nPoints);
fprintf(fph, "##########################################################################################\n");
vertIndex = offsets[i] + 1;
for (uint32_t j = 0; j < ch.m_nTriangles; j++)
{
const int *indices = &ch.m_triangles[j * 3];
fprintf(fph, "f %d %d %d\n", indices[0] + vertIndex, indices[1] + vertIndex, indices[2] + vertIndex);
}
}
fclose(fph);
delete[]offsets;
}
else
{
printf("Failed to open output file '%s for write access\n", fname);
}
}

RENDER_DEBUG::RenderDebug *mRenderDebug;
VHACD::IVHACD *mHACD;

Expand Down
4 changes: 3 additions & 1 deletion DebugView/TestHACD.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,12 @@ class TestHACD
const unsigned int countTriangles,
VHACD::IVHACD::Parameters &desc) = 0;

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

virtual uint32_t getHullCount(void) const = 0;

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

virtual void cancel(void) = 0;

virtual void release(void) = 0;
Expand Down
162 changes: 162 additions & 0 deletions DebugView/TestRaycast.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,162 @@
#include "TestRaycast.h"
#include "NvRenderDebug.h"
#include "vhacdRaycastMesh.h"
#include <float.h>
#include <math.h>

const double FM_PI = 3.1415926535897932384626433832795028841971693993751f;
const double FM_DEG_TO_RAD = ((2.0f * FM_PI) / 360.0f);
const double FM_RAD_TO_DEG = (360.0f / (2.0f * FM_PI));

static void fm_getAABB(uint32_t vcount, const double *p,double *bmin, double *bmax)
{
bmin[0] = p[0];
bmin[1] = p[1];
bmin[2] = p[2];

bmax[0] = p[0];
bmax[1] = p[1];
bmax[2] = p[2];

p += 3;

for (uint32_t i = 1; i < vcount; i++)
{
if (p[0] < bmin[0]) bmin[0] = p[0];
if (p[1] < bmin[1]) bmin[1] = p[1];
if (p[2] < bmin[2]) bmin[2] = p[2];

if (p[0] > bmax[0]) bmax[0] = p[0];
if (p[1] > bmax[1]) bmax[1] = p[1];
if (p[2] > bmax[2]) bmax[2] = p[2];
p += 3;

}
}

// Little helper class to test the raycast mesh code and display the results
class TestRaycastImpl : public TestRaycast
{
public:
TestRaycastImpl(void)
{

}
virtual ~TestRaycastImpl(void)
{

}

void doubleToFloatVert(const double *source, float *dest)
{
dest[0] = float(source[0]);
dest[1] = float(source[1]);
dest[2] = float(source[2]);
}

void floatToDoubleVert(const float *source, double *dest)
{
dest[0] = double(source[0]);
dest[1] = double(source[1]);
dest[2] = double(source[2]);
}


virtual void testRaycast(uint32_t vcount, // number of vertices
uint32_t tcount, // number of triangles
const double *vertices, // Vertices in the mesh
const uint32_t *indices, // triangle indices
RENDER_DEBUG::RenderDebug *renderDebug) // rendering interface
{
if (vcount == 0 || tcount == 0) return;

double bmin[3];
double bmax[3];
float center[3];
fm_getAABB(vcount, vertices, bmin, bmax);
center[0] = float((bmin[0] + bmax[0])*0.5f);
center[1] = float((bmin[1] + bmax[1])*0.5f);
center[2] = float((bmin[2] + bmax[2])*0.5f);

double dx = bmax[0] - bmin[0];
double dy = bmax[1] - bmin[1];
double dz = bmax[2] - bmin[2];

double distance = sqrt(dx*dx + dy*dy + dz*dz)*0.5;

renderDebug->pushRenderState();
renderDebug->setCurrentDisplayTime(5.0f); // 5 seconds
renderDebug->removeFromCurrentState(RENDER_DEBUG::DebugRenderState::SolidShaded);
renderDebug->removeFromCurrentState(RENDER_DEBUG::DebugRenderState::SolidWireShaded);
renderDebug->setCurrentArrowSize(float(distance*0.01));

VHACD::RaycastMesh *rm = VHACD::RaycastMesh::createRaycastMesh(vcount,3, vertices, tcount, 3, indices);

float bmn[3];
float bmx[3];
doubleToFloatVert(bmin, bmn);
doubleToFloatVert(bmax, bmx);

renderDebug->setCurrentColor(0xFFFFFF);
renderDebug->debugBound(bmn, bmx);

uint32_t stepSize = 15;
//
for (uint32_t theta = 0; theta < 360; theta += stepSize)
{
for (uint32_t phi = 0; phi < 360; phi += stepSize)
{
double t = theta*FM_DEG_TO_RAD;
double p = phi*FM_DEG_TO_RAD;
float point[3];

point[0] = float(cos(t)*sin(p)*distance) + center[0];
point[1] = float(cos(p)*distance) + center[1];
point[2] = float(sin(t)*sin(p)*distance) + center[2];


{
double from[3];
double to[3];
floatToDoubleVert(center, from);
floatToDoubleVert(point, to);
double hitLocation[3];
double hitDistance;
if (rm->raycast(from,to,from,hitLocation,&hitDistance))
{
float hit[3];
doubleToFloatVert(hitLocation, hit);
renderDebug->setCurrentColor(0xFFFF00);
renderDebug->debugLine(center, hit);
renderDebug->debugSphere(hit, float(distance)*0.01f);
}
else
{
renderDebug->setCurrentColor(0xFFFFFF);
renderDebug->debugRay(center, point);
}
}

}
}
//

rm->release();

renderDebug->popRenderState();
}

virtual void release(void)
{
delete this;
}

protected:
};

TestRaycast *TestRaycast::create(void)
{
TestRaycastImpl *ret = new TestRaycastImpl;
return static_cast<TestRaycast *>(ret);
}

32 changes: 32 additions & 0 deletions DebugView/TestRaycast.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
#ifndef TEST_RAYCAST_H
#define TEST_RAYCAST_H

#include <stdint.h>

namespace RENDER_DEBUG
{
class RenderDebug;
}


// Little helper class to test the raycast mesh code and display the results
class TestRaycast
{
public:
static TestRaycast *create(void);

virtual void testRaycast(uint32_t vcount, // number of vertices
uint32_t tcount, // number of triangles
const double *vertices, // Vertices in the mesh
const uint32_t *indices, // triangle indices
RENDER_DEBUG::RenderDebug *renderDebug) = 0; // rendering interface

virtual void release(void) = 0;

protected:
virtual ~TestRaycast(void)
{
}
};

#endif
8 changes: 8 additions & 0 deletions DebugView/compiler/vc14win32/ConvexDecomposition.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -121,10 +121,14 @@
</ClCompile>
<ClCompile Include="..\..\TestHACD.cpp">
</ClCompile>
<ClCompile Include="..\..\TestRaycast.cpp">
</ClCompile>
<ClCompile Include="..\..\wavefront.cpp">
</ClCompile>
<ClInclude Include="..\..\TestHACD.h">
</ClInclude>
<ClInclude Include="..\..\TestRaycast.h">
</ClInclude>
<ClInclude Include="..\..\wavefront.h">
</ClInclude>
</ItemGroup>
Expand All @@ -151,6 +155,8 @@
</ClInclude>
<ClInclude Include="..\..\..\src\VHACD_Lib\inc\vhacdMutex.h">
</ClInclude>
<ClInclude Include="..\..\..\src\VHACD_Lib\inc\vhacdRaycastMesh.h">
</ClInclude>
<ClInclude Include="..\..\..\src\VHACD_Lib\inc\vhacdSArray.h">
</ClInclude>
<ClInclude Include="..\..\..\src\VHACD_Lib\inc\vhacdTimer.h">
Expand Down Expand Up @@ -181,6 +187,8 @@
</ClCompile>
<ClCompile Include="..\..\..\src\VHACD_Lib\src\vhacdMesh.cpp">
</ClCompile>
<ClCompile Include="..\..\..\src\VHACD_Lib\src\vhacdRaycastMesh.cpp">
</ClCompile>
<ClCompile Include="..\..\..\src\VHACD_Lib\src\vhacdVolume.cpp">
</ClCompile>
</ItemGroup>
Expand Down
12 changes: 12 additions & 0 deletions DebugView/compiler/vc14win32/ConvexDecomposition.vcxproj.filters
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,18 @@
<ClCompile Include="..\..\TestHACD.cpp">
<Filter>ConvexDecomposition</Filter>
</ClCompile>
<ClCompile Include="..\..\TestRaycast.cpp">
<Filter>ConvexDecomposition</Filter>
</ClCompile>
<ClCompile Include="..\..\wavefront.cpp">
<Filter>ConvexDecomposition</Filter>
</ClCompile>
<ClInclude Include="..\..\TestHACD.h">
<Filter>ConvexDecomposition</Filter>
</ClInclude>
<ClInclude Include="..\..\TestRaycast.h">
<Filter>ConvexDecomposition</Filter>
</ClInclude>
<ClInclude Include="..\..\wavefront.h">
<Filter>ConvexDecomposition</Filter>
</ClInclude>
Expand Down Expand Up @@ -78,6 +84,9 @@
<ClInclude Include="..\..\..\src\VHACD_Lib\inc\vhacdMutex.h">
<Filter>VHADC_lib\inc</Filter>
</ClInclude>
<ClInclude Include="..\..\..\src\VHACD_Lib\inc\vhacdRaycastMesh.h">
<Filter>VHADC_lib\inc</Filter>
</ClInclude>
<ClInclude Include="..\..\..\src\VHACD_Lib\inc\vhacdSArray.h">
<Filter>VHADC_lib\inc</Filter>
</ClInclude>
Expand Down Expand Up @@ -129,6 +138,9 @@
<ClCompile Include="..\..\..\src\VHACD_Lib\src\vhacdMesh.cpp">
<Filter>VHADC_lib\src</Filter>
</ClCompile>
<ClCompile Include="..\..\..\src\VHACD_Lib\src\vhacdRaycastMesh.cpp">
<Filter>VHADC_lib\src</Filter>
</ClCompile>
<ClCompile Include="..\..\..\src\VHACD_Lib\src\vhacdVolume.cpp">
<Filter>VHADC_lib\src</Filter>
</ClCompile>
Expand Down
Loading