Skip to content

Commit

Permalink
Mesh: add overloaded mesh decimation to set target number of triangles
Browse files Browse the repository at this point in the history
  • Loading branch information
wwmayer committed May 3, 2020
1 parent 3ad1534 commit 2ac739e
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 0 deletions.
49 changes: 49 additions & 0 deletions src/Mod/Mesh/App/Core/Decimation.cpp
Expand Up @@ -95,3 +95,52 @@ void MeshSimplify::simplify(float tolerance, float reduction)

myKernel.Adopt(new_points, new_facets, true);
}

void MeshSimplify::simplify(int targetSize)
{
Simplify alg;

const MeshPointArray& points = myKernel.GetPoints();
for (std::size_t i = 0; i < points.size(); i++) {
Simplify::Vertex v;
v.p = points[i];
alg.vertices.push_back(v);
}

const MeshFacetArray& facets = myKernel.GetFacets();
for (std::size_t i = 0; i < facets.size(); i++) {
Simplify::Triangle t;
for (int j = 0; j < 3; j++)
t.v[j] = facets[i]._aulPoints[j];
alg.triangles.push_back(t);
}

// Simplification starts
alg.simplify_mesh(targetSize, FLT_MAX);

// Simplification done
MeshPointArray new_points;
new_points.reserve(alg.vertices.size());
for (std::size_t i = 0; i < alg.vertices.size(); i++) {
new_points.push_back(alg.vertices[i].p);
}

std::size_t numFacets = 0;
for (std::size_t i = 0; i < alg.triangles.size(); i++) {
if (!alg.triangles[i].deleted)
numFacets++;
}
MeshFacetArray new_facets;
new_facets.reserve(numFacets);
for (std::size_t i = 0; i < alg.triangles.size(); i++) {
if (!alg.triangles[i].deleted) {
MeshFacet face;
face._aulPoints[0] = alg.triangles[i].v[0];
face._aulPoints[1] = alg.triangles[i].v[1];
face._aulPoints[2] = alg.triangles[i].v[2];
new_facets.push_back(face);
}
}

myKernel.Adopt(new_points, new_facets, true);
}
1 change: 1 addition & 0 deletions src/Mod/Mesh/App/Core/Decimation.h
Expand Up @@ -35,6 +35,7 @@ class MeshExport MeshSimplify
MeshSimplify(MeshKernel&);
~MeshSimplify();
void simplify(float tolerance, float reduction);
void simplify(int targetSize);

private:
MeshKernel& myKernel;
Expand Down
6 changes: 6 additions & 0 deletions src/Mod/Mesh/App/Mesh.cpp
Expand Up @@ -989,6 +989,12 @@ void MeshObject::decimate(float fTolerance, float fReduction)
dm.simplify(fTolerance, fReduction);
}

void MeshObject::decimate(int targetSize)
{
MeshCore::MeshSimplify dm(this->_kernel);
dm.simplify(targetSize);
}

Base::Vector3d MeshObject::getPointNormal(unsigned long index) const
{
std::vector<Base::Vector3f> temp = _kernel.CalcVertexNormals();
Expand Down
1 change: 1 addition & 0 deletions src/Mod/Mesh/App/Mesh.h
Expand Up @@ -217,6 +217,7 @@ class MeshExport MeshObject : public Data::ComplexGeoData
void setPoint(unsigned long, const Base::Vector3d& v);
void smooth(int iterations, float d_max);
void decimate(float fTolerance, float fReduction);
void decimate(int targetSize);
Base::Vector3d getPointNormal(unsigned long) const;
std::vector<Base::Vector3d> getPointNormals() const;
void crossSections(const std::vector<TPlane>&, std::vector<TPolylines> &sections,
Expand Down

0 comments on commit 2ac739e

Please sign in to comment.