Milena (Olena)  User documentation 2.0a Id
 All Classes Namespaces Functions Variables Typedefs Enumerator Groups Pages
mesh-pinv-curv.cc
1 // Copyright (C) 2008 EPITA Research and Development Laboratory (LRDE)
2 //
3 // This file is part of Olena.
4 //
5 // Olena is free software: you can redistribute it and/or modify it under
6 // the terms of the GNU General Public License as published by the Free
7 // Software Foundation, version 2 of the License.
8 //
9 // Olena is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 // General Public License for more details.
13 //
14 // You should have received a copy of the GNU General Public License
15 // along with Olena. If not, see <http://www.gnu.org/licenses/>.
16 //
17 // As a special exception, you may use this file as part of a free
18 // software project without restriction. Specifically, if other files
19 // instantiate templates or use macros or inline functions from this
20 // file, or you compile this file and link it with other files to produce
21 // an executable, this file does not by itself cause the resulting
22 // executable to be covered by the GNU General Public License. This
23 // exception does not however invalidate any other reasons why the
24 // executable file might be covered by the GNU General Public License.
25 
29 
30 #include <cstdlib>
31 #include <cmath>
32 
33 #include <algorithm>
34 #include <vector>
35 #include <iostream>
36 #include <utility>
37 
38 #include <TriMesh.h>
39 
40 #include "io.hh"
41 
42 
43 // Doesn't C++ have a better way to express Pi?
44 static const float pi = 4 * atanf(1);
45 
46 
47 int main(int argc, char* argv[])
48 {
49  if (argc != 3)
50  {
51  std::cerr << "usage: " << argv[0] << " input.off output.off"
52  << std::endl;
53  std::exit(1);
54  }
55 
56  std::string input_filename = argv[1];
57  std::string output_filename = argv[2];
58 
59 
60  // TriMesh is a pain: it systematically allocates on the heap.
61  // Introduce another name to manipulate the mesh as a (non-pointer)
62  // object.
63  TriMesh* mesh_ptr = TriMesh::read(input_filename.c_str());
64  if (!mesh_ptr)
65  std::exit(2);
66  TriMesh& mesh = *mesh_ptr;
67 
68  // Computes faces (triangles).
69  mesh.need_faces();
70  // Computation of the curvature on each vertex of the mesh.
71  mesh.need_curvatures();
72  std::vector<float> vertex_h_inv(mesh.vertices.size(), 0.f);
73  for (unsigned v = 0; v < mesh.vertices.size(); ++v)
74  {
75  float h = (mesh.curv1[v] + mesh.curv2[v]) / 2;
76  // Pseudo-inverse curvature.
77  float h_inv = 1 / pi * (atan(-h) + pi / 2);
78  vertex_h_inv[v] = h_inv;
79  }
80  // For each face of the mesh, computean an average curvature value
81  // from the mean curvature at its vertices.
82  std::vector<float> face_h_inv(mesh.faces.size(), 42.f);
83  for (unsigned f = 0; f < mesh.faces.size(); ++f)
84  {
85  float h_inv = (vertex_h_inv[mesh.faces[f][0]] +
86  vertex_h_inv[mesh.faces[f][1]] +
87  vertex_h_inv[mesh.faces[f][2]]) / 3;
88  mln_invariant(0.f <= h_inv);
89  mln_invariant(h_inv <= 1.f);
90  face_h_inv[f] = h_inv;
91  }
92 
93  // Taken and adapted from TriMesh_io.cc
94  FILE* f_out = fopen(output_filename.c_str(), "wb");
95  if (!f_out)
96  {
97  std::cerr << "Error opening " << output_filename.c_str()
98  << " for writing." << std::endl;
99  std::exit(2);
100  }
101  write_off_float(mesh_ptr, face_h_inv, f_out);
102  fclose(f_out);
103 
104  delete mesh_ptr;
105 }