Skip to content

Commit

Permalink
update tolerance defaults
Browse files Browse the repository at this point in the history
  • Loading branch information
elalish committed Oct 22, 2024
1 parent 5ba3c24 commit a7060ab
Show file tree
Hide file tree
Showing 7 changed files with 36 additions and 25 deletions.
6 changes: 4 additions & 2 deletions src/impl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -571,9 +571,11 @@ Manifold::Impl Manifold::Impl::Transform(const mat3x4& transform_) const {
* Sets the precision based on the bounding box, and limits its minimum value
* by the optional input.
*/
void Manifold::Impl::SetEpsilon(double minEpsilon) {
void Manifold::Impl::SetEpsilon(double minEpsilon, bool useSingle) {
epsilon_ = MaxEpsilon(minEpsilon, bBox_);
tolerance_ = std::max(tolerance_, epsilon_);
double minTol = epsilon_;
if (useSingle) minTol = std::max(minTol, kFloatTolerance * bBox_.Scale());
tolerance_ = std::max(tolerance_, minTol);
}

/**
Expand Down
4 changes: 2 additions & 2 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();
SetEpsilon(-1, std::is_same<Precision, float>::value);

SplitPinchedVerts();

Expand Down Expand Up @@ -277,7 +277,7 @@ struct Manifold::Impl {
void CalculateBBox();
bool IsFinite() const;
bool IsIndexInBounds(VecView<const ivec3> triVerts) const;
void SetEpsilon(double minEpsilon = -1);
void SetEpsilon(double minEpsilon = -1, bool useSingle = false);
bool IsManifold() const;
bool Is2Manifold() const;
bool MatchesTriNormals() const;
Expand Down
8 changes: 6 additions & 2 deletions src/manifold.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -71,8 +71,12 @@ MeshGLP<Precision, I> GetMeshGLImpl(const manifold::Manifold::Impl& impl,

MeshGLP<Precision, I> out;
out.numProp = 3 + numProp;
out.tolerance = std::max(std::numeric_limits<Precision>::epsilon(),
static_cast<Precision>(impl.tolerance_));
out.tolerance = impl.tolerance_;
if (std::is_same<Precision, float>::value)
out.tolerance =
std::max(out.tolerance,
static_cast<Precision>(std::numeric_limits<float>::epsilon() *
impl.bBox_.Scale()));
out.triVerts.resize(3 * numTri);

const int numHalfedge = impl.halfedgeTangent_.size();
Expand Down
2 changes: 1 addition & 1 deletion src/quickhull.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -847,7 +847,7 @@ void Manifold::Impl::Hull(VecView<vec3> vertPos) {
QuickHull qh(vertPos);
std::tie(halfedge_, vertPos_) = qh.buildMesh();
CalculateBBox();
SetEpsilon(bBox_.Scale() * kTolerance);
SetEpsilon();
CalculateNormals();
InitializeOriginal();
Finish();
Expand Down
29 changes: 17 additions & 12 deletions src/sort.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -143,25 +143,30 @@ bool MergeMeshGLP(MeshGLP<Precision, I>& mesh) {
bBox.min[i] = minMax.first;
bBox.max[i] = minMax.second;
}
auto epsilon = MaxEpsilon(0, bBox);

const double tolerance = std::max(
static_cast<double>(mesh.tolerance),
(std::is_same<Precision, float>::value ? kFloatTolerance : kTolerance) *
bBox.Scale());

auto policy = autoPolicy(numOpenVert, 1e5);
Vec<Box> vertBox(numOpenVert);
Vec<uint32_t> vertMorton(numOpenVert);

for_each_n(
policy, countAt(0), numOpenVert,
[&vertMorton, &vertBox, &openVerts, &bBox, &mesh, epsilon](const int i) {
int vert = openVerts[i];
for_each_n(policy, countAt(0), numOpenVert,
[&vertMorton, &vertBox, &openVerts, &bBox, &mesh,
tolerance](const int i) {
int vert = openVerts[i];

const vec3 center(mesh.vertProperties[mesh.numProp * vert],
mesh.vertProperties[mesh.numProp * vert + 1],
mesh.vertProperties[mesh.numProp * vert + 2]);
const vec3 center(mesh.vertProperties[mesh.numProp * vert],
mesh.vertProperties[mesh.numProp * vert + 1],
mesh.vertProperties[mesh.numProp * vert + 2]);

vertBox[i].min = center - epsilon / 2.0;
vertBox[i].max = center + epsilon / 2.0;
vertBox[i].min = center - tolerance / 2.0;
vertBox[i].max = center + tolerance / 2.0;

vertMorton[i] = MortonCode(center, bBox);
});
vertMorton[i] = MortonCode(center, bBox);
});

Vec<int> vertNew2Old(numOpenVert);
sequence(vertNew2Old.begin(), vertNew2Old.end());
Expand Down
1 change: 1 addition & 0 deletions src/utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ namespace manifold {
}

constexpr double kTolerance = 1e-12;
constexpr double kFloatTolerance = 1e-6;

inline int Next3(int i) {
constexpr ivec3 next3(1, 2, 0);
Expand Down
11 changes: 5 additions & 6 deletions test/test_main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -300,10 +300,6 @@ void RelatedGL(const Manifold& out, const std::vector<MeshGL>& originals,
const ivec3 normalIdx = updateNormals ? ivec3(3, 4, 5) : ivec3(0);
MeshGL output = out.GetMeshGL(normalIdx);

float epsilon = std::max(static_cast<float>(out.GetEpsilon()),
std::numeric_limits<float>::epsilon() *
static_cast<float>(out.BoundingBox().Scale()));

for (size_t run = 0; run < output.runOriginalID.size(); ++run) {
const float* m = output.runTransform.data() + 12 * run;
const mat3x4 transform =
Expand All @@ -318,6 +314,9 @@ void RelatedGL(const Manifold& out, const std::vector<MeshGL>& originals,
}
ASSERT_LT(i, originals.size());
const MeshGL& inMesh = originals[i];
const float tolerance =
std::max(static_cast<float>(out.GetTolerance()), inMesh.tolerance);

for (uint32_t tri = output.runIndex[run] / 3;
tri < output.runIndex[run + 1] / 3; ++tri) {
if (!output.faceID.empty()) {
Expand Down Expand Up @@ -355,7 +354,7 @@ void RelatedGL(const Manifold& out, const std::vector<MeshGL>& originals,
vec3 edges[3];
for (int k : {0, 1, 2}) edges[k] = inTriPos[k] - outTriPos[j];
const double volume = la::dot(edges[0], la::cross(edges[1], edges[2]));
ASSERT_LE(volume, area * epsilon);
ASSERT_LE(volume, area * tolerance);

if (checkNormals) {
vec3 normal;
Expand All @@ -378,7 +377,7 @@ void RelatedGL(const Manifold& out, const std::vector<MeshGL>& originals,
const double volumeP =
la::dot(edgesP[0], la::cross(edgesP[1], edgesP[2]));

ASSERT_LE(volumeP, area * epsilon);
ASSERT_LE(volumeP, area * tolerance);
}
}
}
Expand Down

0 comments on commit a7060ab

Please sign in to comment.