smooth.cc 1.94 KB
Newer Older
Max Lyon's avatar
Max Lyon committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44

#include <OpenMesh/Core/IO/MeshIO.hh>
#include <OpenMesh/Core/Mesh/DefaultTriMesh.hh>
#include <OpenMesh/Core/Utils/PropertyManager.hh>

#include <iostream>
#include <vector>

using MyMesh = OpenMesh::TriMesh;

int main(int argc, char** argv)
{
  // Read command line options
  MyMesh mesh;
  if (argc != 4) {
    std::cerr << "Usage: " << argv[0] << " #iterations infile outfile" << std::endl;
    return 1;
  }
  const int iterations = argv[1];
  const std::string infile = argv[2];
  const std::string outfile = argv[3];
  
  // Read mesh file
  if (!OpenMesh::IO::read_mesh(mesh, infile)) {
    std::cerr << "Error: Cannot read mesh from " << infile << std::endl;
    return 1;
  }
  
  {
    // Add a vertex property storing the laplace vector
    auto laplace = OpenMesh::VProp<MyMesh::Point>(mesh);
    
    // Add a vertex property storing the laplace of the laplace
    auto bi_laplace = OpenMesh::VProp<MyMesh::Point>(mesh);
    
    // Get a propertymanager of the points property of the mesh to use as functor
    auto points = OpenMesh::getPointsProperty(mesh);
    
    // Smooth the mesh several times
    for (int i = 0; i < iterations; ++i) {
      // Iterate over all vertices to compute laplace vector
      for (const auto& vh : mesh.vertices())
        laplace(vh) = vh.vertices().avg(points) - points(vh);
      
Max Lyon's avatar
Max Lyon committed
45
      // Iterate over all vertices to compute the laplace vector of the laplace vectors
Max Lyon's avatar
Max Lyon committed
46 47 48
      for (const auto& vh : mesh.vertices())
        bi_laplace(vh) =  (vh.vertices().avg(laplace) - laplace(vh));
      
Max Lyon's avatar
Max Lyon committed
49
      // update points by substracting the bi-laplacian damped by a factor of 0.5
Max Lyon's avatar
Max Lyon committed
50 51 52
      for (const auto& vh : mesh.vertices())
        points(vh) += -0.5 * bi_laplace(vh);
    }
Max Lyon's avatar
Max Lyon committed
53
  } // The laplace and update properties are removed from the mesh at the end of this scope.
Max Lyon's avatar
Max Lyon committed
54 55 56 57 58 59 60 61 62
  
  
  // Write mesh file
  if (!OpenMesh::IO::read_mesh(mesh, outfile)) {
    std::cerr << "Error: Cannot write mesh to " << outfile << std::endl;
    return 1;
  }
}