Skip to content

Commit

Permalink
allow setting epsilon
Browse files Browse the repository at this point in the history
  • Loading branch information
pca006132 committed Dec 9, 2024
1 parent f4c6cc3 commit b152de0
Show file tree
Hide file tree
Showing 6 changed files with 209 additions and 1,212 deletions.
8 changes: 8 additions & 0 deletions include/manifold/manifold.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@
#include <functional>
#include <memory>

#ifdef MANIFOLD_EXPORT
#include <iostream>
#endif

#include "manifold/common.h"
#include "manifold/vec_view.h"

Expand Down Expand Up @@ -376,6 +380,10 @@ class Manifold {

struct Impl;

#ifdef MANIFOLD_EXPORT
static Manifold ImportMeshGL64(std::istream& stream);
#endif

private:
Manifold(std::shared_ptr<CsgNode> pNode_);
Manifold(std::shared_ptr<Impl> pImpl_);
Expand Down
31 changes: 23 additions & 8 deletions src/impl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
#include <algorithm>
#include <atomic>
#include <map>
#include <optional>

#include "./hashtable.h"
#include "./mesh_fixes.h"
Expand Down Expand Up @@ -697,22 +698,28 @@ std::ostream& operator<<(std::ostream& stream, const Manifold::Impl& impl) {
stream << std::setprecision(17); // for double precision
stream << "# ======= begin mesh ======" << std::endl;
stream << "# tolerance = " << impl.tolerance_ << std::endl;
stream << "# epsilon = " << impl.epsilon_ << std::endl;
// TODO: Mesh relation, vertex normal and face normal

for (const vec3& v : impl.vertPos_)
stream << "v " << v.x << " " << v.y << " " << v.z << std::endl;
std::vector<ivec3> triangles;
triangles.reserve(impl.halfedge_.size() / 3);
for (size_t i = 0; i < impl.halfedge_.size(); i += 3)
stream << "f " << impl.halfedge_[i].startVert + 1 << " "
<< impl.halfedge_[i + 1].startVert + 1 << " "
<< impl.halfedge_[i + 2].startVert + 1 << std::endl;
triangles.emplace_back(impl.halfedge_[i].startVert + 1,
impl.halfedge_[i + 1].startVert + 1,
impl.halfedge_[i + 2].startVert + 1);
sort(triangles.begin(), triangles.end());
for (const auto& tri : triangles)
stream << "f " << tri.x << " " << tri.y << " " << tri.z << std::endl;
stream << "# ======== end mesh =======" << std::endl;
return stream;
}
#endif

#ifdef MANIFOLD_EXPORT
MeshGL64 ImportMeshGL64(std::istream& stream) {
Manifold Manifold::ImportMeshGL64(std::istream& stream) {
MeshGL64 mesh;
std::optional<double> epsilon;
stream.precision(17);
while (true) {
char c = stream.get();
Expand All @@ -721,11 +728,17 @@ MeshGL64 ImportMeshGL64(std::istream& stream) {
case '#': {
char c = stream.get();
if (c == ' ') {
constexpr int SIZE = 13;
constexpr int SIZE = 10;
std::array<char, SIZE> tmp;
stream.get(tmp.data(), SIZE, '\n');
if (strncmp(tmp.data(), "tolerance = ", SIZE) == 0) {
if (strncmp(tmp.data(), "tolerance", SIZE) == 0) {
// skip 3 letters
for (int i : {0, 1, 2}) stream.get();
stream >> mesh.tolerance;
} else if (strncmp(tmp.data(), "epsilon =", SIZE) == 0) {
double tmp;
stream >> tmp;
epsilon = {tmp};
} else {
// add it back because it is not what we want
int end = 0;
Expand Down Expand Up @@ -760,7 +773,9 @@ MeshGL64 ImportMeshGL64(std::istream& stream) {
DEBUG_ASSERT(false, userErr, "unexpected character in MeshGL64 import");
}
}
return mesh;
auto m = std::make_shared<Manifold::Impl>(mesh);
if (epsilon) m->SetEpsilon(*epsilon);
return Manifold(m);
}
#endif

Expand Down
6 changes: 1 addition & 5 deletions src/impl.h
Original file line number Diff line number Diff line change
Expand Up @@ -202,7 +202,7 @@ struct Manifold::Impl {
MarkFailure(Error::NonFiniteVertex);
return;
}
SetEpsilon(-1, std::is_same<Precision, float>::value);
SetEpsilon(meshGL.tolerance, std::is_same<Precision, float>::value);

SplitPinchedVerts();

Expand Down Expand Up @@ -355,8 +355,4 @@ struct Manifold::Impl {
extern std::mutex dump_lock;
std::ostream& operator<<(std::ostream& stream, const Manifold::Impl& impl);
#endif

#ifdef MANIFOLD_EXPORT
MeshGL64 ImportMeshGL64(std::istream& stream);
#endif
} // namespace manifold
24 changes: 11 additions & 13 deletions test/boolean_complex_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
#include <fstream>
#include <iostream>

#include "../src/impl.h"
#include "manifold/manifold.h"
#include "manifold/polygon.h"
#include "test.h"
Expand Down Expand Up @@ -1025,22 +1024,20 @@ TEST(BooleanComplex, DISABLED_OffsetTriangulationFailure) {
ManifoldParams().intermediateChecks = true;
std::string file = __FILE__;
std::string dir = file.substr(0, file.rfind('/'));
MeshGL64 a, b;
Manifold a, b;
std::ifstream f;
try {
f.open(dir + "/models/Offset1.obj");
a = ImportMeshGL64(f);
a = Manifold::ImportMeshGL64(f);
f.close();
f.open(dir + "/models/Offset2.obj");
b = ImportMeshGL64(f);
b = Manifold::ImportMeshGL64(f);
f.close();
} catch (std::exception& err) {
std::cout << err.what() << std::endl;
FAIL();
}
Manifold x(a);
Manifold y(b);
Manifold result = x + y;
Manifold result = a + b;
EXPECT_EQ(result.Status(), Manifold::Error::NoError);
ManifoldParams().intermediateChecks = intermediateChecks;
}
Expand All @@ -1050,24 +1047,25 @@ TEST(BooleanComplex, DISABLED_OffsetSelfIntersect) {
ManifoldParams().intermediateChecks = true;
std::string file = __FILE__;
std::string dir = file.substr(0, file.rfind('/'));
MeshGL64 a, b;
Manifold a, b;
std::ifstream f;
try {
f.open(dir + "/models/Offset3.obj");
a = ImportMeshGL64(f);
a = Manifold::ImportMeshGL64(f);
f.close();
f.open(dir + "/models/Offset4.obj");
b = ImportMeshGL64(f);
b = Manifold::ImportMeshGL64(f);
f.close();
} catch (std::exception& err) {
std::cout << err.what() << std::endl;
FAIL();
}
Manifold x(a);
Manifold y(b);
Manifold result = x + y;

Manifold result = a + b;
EXPECT_EQ(result.Status(), Manifold::Error::NoError);
ManifoldParams().intermediateChecks = intermediateChecks;

ManifoldParams().intermediateChecks = intermediateChecks;
}

#endif
Loading

0 comments on commit b152de0

Please sign in to comment.