From dd8c20757e3ff24b38b94f8261134ab9476e9baf Mon Sep 17 00:00:00 2001 From: augustin64 Date: Wed, 12 Mar 2025 12:15:05 +0100 Subject: [PATCH] Add Laplacian smoothing --- .../5-LaplacianSmoothing/c++/laplacian.cpp | 33 +++++++++++++++++-- 1 file changed, 30 insertions(+), 3 deletions(-) diff --git a/geometry-processing/5-LaplacianSmoothing/c++/laplacian.cpp b/geometry-processing/5-LaplacianSmoothing/c++/laplacian.cpp index cdf83a0..cf0620b 100644 --- a/geometry-processing/5-LaplacianSmoothing/c++/laplacian.cpp +++ b/geometry-processing/5-LaplacianSmoothing/c++/laplacian.cpp @@ -1,3 +1,4 @@ +#include #include "geometrycentral/surface/manifold_surface_mesh.h" #include "geometrycentral/surface/meshio.h" @@ -15,11 +16,31 @@ std::unique_ptr mesh; std::unique_ptr geometry; int counter=0; +float lambda = 0.5; + +Vector3 vertAverage(std::vector vertexPositions) { + assert(vertexPositions.size() > 0); + + Vector3 out = {0, 0, 0}; + for (auto i=0; i < vertexPositions.size(); i++) { + out = out + vertexPositions.at(i); + } + return out/vertexPositions.size(); +} void oneStep() { - for(size_t i = 0; i < mesh->nVertices(); ++i) - geometry->vertexPositions[i] = geometry->vertexPositions[i] + Vector3::constant(0.01*sin(i*counter/(2.0*M_PI))); + for(Vertex v : mesh->vertices()) { + std::vector adjVert; + adjVert.push_back(geometry->vertexPositions[v]); + for (Vertex v_adj : v.adjacentVertices()) { + adjVert.push_back(geometry->vertexPositions[v_adj]); + } + Vector3 avgPos = vertAverage(adjVert); + avgPos -= geometry->vertexPositions[v]; + + geometry->vertexPositions[v] += lambda*avgPos; + } } bool go=false; @@ -41,12 +62,18 @@ void myCallback() int main(int argc, char **argv) { + CLI::App app{"laplacian"}; + std::string inputObj; + app.add_option("-i,--input", inputObj, "Source Obj")->required()->check(CLI::ExistingFile);; + app.add_option("-l,--lambda", lambda, "Lambda (speed of transformation)."); + CLI11_PARSE(app, argc, argv); + assert (lambda <= 1 && lambda > 0); // Initialize polyscope polyscope::init(); // Load mesh - std::tie(mesh, geometry) = readManifoldSurfaceMesh(argv[1]); + std::tie(mesh, geometry) = readManifoldSurfaceMesh(inputObj); // Register the mesh with polyscope polyscope::registerSurfaceMesh("Input obj",