partially fix MeshReconstruction

This commit is contained in:
papush! 2017-05-12 03:04:43 +02:00
parent a9e0f9612b
commit 6cb0dbc21e
6 changed files with 14 additions and 25 deletions

Binary file not shown.

View File

@ -1,7 +1,5 @@
add_library(MeshReconstruction MeshReconstruction.h MeshReconstruction.cpp Cube.h Cube.cpp DataStructs.h IO.h IO.cpp Triangulation.h Triangulation.cpp) add_library(MeshReconstruction MeshReconstruction.h MeshReconstruction.cpp Cube.h Cube.cpp DataStructs.h IO.h IO.cpp Triangulation.h Triangulation.cpp)
target_link_libraries(MeshReconstruction m)
install(TARGETS MeshReconstruction install(TARGETS MeshReconstruction
RUNTIME DESTINATION bin RUNTIME DESTINATION bin
LIBRARY DESTINATION lib LIBRARY DESTINATION lib

View File

@ -1,5 +1,4 @@
#include "Cube.h" #include "Cube.h"
#include <cmath>
using namespace MeshReconstruction; using namespace MeshReconstruction;
@ -44,7 +43,7 @@ namespace
struct Edge struct Edge
{ {
int edgeFlag : 12; // flag: 1, 2, 4, ... 2048 unsigned edgeFlag : 12; // flag: 1, 2, 4, ... 2048
int vert0; // 0-7 int vert0; // 0-7
int vert1; // 0-7 int vert1; // 0-7
}; };
@ -74,9 +73,9 @@ Vec3 Cube::LerpVertex(double isoLevel, int i1, int i2) const
auto const& p1 = pos[i1]; auto const& p1 = pos[i1];
auto const& p2 = pos[i2]; auto const& p2 = pos[i2];
if (abs(isoLevel - v1) < Eps) return p1; if (std::abs(isoLevel - v1) < Eps) return p1;
if (abs(isoLevel - v2) < Eps) return p2; if (std::abs(isoLevel - v2) < Eps) return p2;
if (abs(v1 - v2) < Eps) return p1; if (std::abs(v1 - v2) < Eps) return p1;
auto mu = (isoLevel - v1) / (v2 - v1); auto mu = (isoLevel - v1) / (v2 - v1);
return p1 + (p2 - p1)*mu; return p1 + (p2 - p1)*mu;
@ -117,7 +116,7 @@ int Cube::SignConfig(double isoLevel) const
{ {
if (sdf[i] < isoLevel) if (sdf[i] < isoLevel)
{ {
edgeIndex |= 1 << i; edgeIndex |= (1 << i);
} }
} }
@ -133,10 +132,10 @@ IntersectInfo Cube::Intersect(double iso) const
IntersectInfo intersect; IntersectInfo intersect;
intersect.signConfig = SignConfig(iso); intersect.signConfig = SignConfig(iso);
auto intersectedEdges = signConfigToIntersectedEdges[intersect.signConfig];
for (auto e = 0; e<12; ++e) for (auto e = 0; e<12; ++e)
{ {
if (signConfigToIntersectedEdges[intersect.signConfig] & edges[e].edgeFlag) if (intersectedEdges & edges[e].edgeFlag)
{ {
auto v0 = edges[e].vert0; auto v0 = edges[e].vert0;
auto v1 = edges[e].vert1; auto v1 = edges[e].vert1;

View File

@ -54,4 +54,4 @@ namespace MeshReconstruction
using Fun3s = std::function<double(Vec3 const&)>; using Fun3s = std::function<double(Vec3 const&)>;
using Fun3v = std::function<Vec3(Vec3 const&)>; using Fun3v = std::function<Vec3(Vec3 const&)>;
} }

View File

@ -6,13 +6,7 @@ void MeshReconstruction::WriteObjFile(Mesh const& mesh, string const& fileName)
{ {
// FILE faster than streams. // FILE faster than streams.
FILE* file; FILE* file = fopen(fileName.c_str(), "w");
//auto status = fopen_s(&file, fileName.c_str(), "w");
//if (status != 0)
//{
// throw runtime_error("Could not write obj file.");
//}
file = fopen(fileName.c_str(), "w");
if (file == NULL) if (file == NULL)
{ {
throw runtime_error("Could not write obj file."); throw runtime_error("Could not write obj file.");
@ -24,7 +18,7 @@ void MeshReconstruction::WriteObjFile(Mesh const& mesh, string const& fileName)
static_cast<int>(mesh.triangles.size())); static_cast<int>(mesh.triangles.size()));
// vertices // vertices
for (auto vi = 0; vi < mesh.vertices.size(); ++vi) for (size_t vi = 0; vi < mesh.vertices.size(); ++vi)
{ {
auto const& v = mesh.vertices.at(vi); auto const& v = mesh.vertices.at(vi);
fprintf(file, "v %f %f %f\n", v.x, v.y, v.z); fprintf(file, "v %f %f %f\n", v.x, v.y, v.z);
@ -32,7 +26,7 @@ void MeshReconstruction::WriteObjFile(Mesh const& mesh, string const& fileName)
// vertex normals // vertex normals
fprintf(file, "\n"); fprintf(file, "\n");
for (auto ni = 0; ni < mesh.vertices.size(); ++ni) for (size_t ni = 0; ni < mesh.vertices.size(); ++ni)
{ {
auto const& vn = mesh.vertexNormals.at(ni); auto const& vn = mesh.vertexNormals.at(ni);
fprintf(file, "vn %f %f %f\n", vn.x, vn.y, vn.z); fprintf(file, "vn %f %f %f\n", vn.x, vn.y, vn.z);
@ -40,7 +34,7 @@ void MeshReconstruction::WriteObjFile(Mesh const& mesh, string const& fileName)
// triangles (1-based) // triangles (1-based)
fprintf(file, "\n"); fprintf(file, "\n");
for (auto ti = 0; ti < mesh.triangles.size(); ++ti) for (size_t ti = 0; ti < mesh.triangles.size(); ++ti)
{ {
auto const& t = mesh.triangles.at(ti); auto const& t = mesh.triangles.at(ti);
fprintf(file, "f %d//%d %d//%d %d//%d\n", fprintf(file, "f %d//%d %d//%d %d//%d\n",
@ -50,4 +44,4 @@ void MeshReconstruction::WriteObjFile(Mesh const& mesh, string const& fileName)
} }
fclose(file); fclose(file);
} }

View File

@ -52,11 +52,9 @@ Mesh MeshReconstruction::MarchCube(
for (auto ix = 0; ix < NumX; ++ix) for (auto ix = 0; ix < NumX; ++ix)
{ {
auto x = domain.min.x + ix * cubeSize.x; auto x = domain.min.x + ix * cubeSize.x;
for (auto iy = 0; iy < NumY; ++iy) for (auto iy = 0; iy < NumY; ++iy)
{ {
auto y = domain.min.y + iy * cubeSize.y; auto y = domain.min.y + iy * cubeSize.y;
for (auto iz = 0; iz < NumZ; ++iz) for (auto iz = 0; iz < NumZ; ++iz)
{ {
auto z = domain.min.z + iz * cubeSize.z; auto z = domain.min.z + iz * cubeSize.z;
@ -64,7 +62,7 @@ Mesh MeshReconstruction::MarchCube(
// Process only if cube lies within narrow band around surface. // Process only if cube lies within narrow band around surface.
auto cubeCenter = min + HalfCubeSize; auto cubeCenter = min + HalfCubeSize;
auto dist = abs(sdf(cubeCenter) - isoLevel); double dist = std::fabs(sdf(cubeCenter) - isoLevel);
if (dist > HalfCubeDiag) continue; if (dist > HalfCubeDiag) continue;
Cube cube({ min, cubeSize }, sdf); Cube cube({ min, cubeSize }, sdf);