Milena (Olena)  User documentation 2.0a Id
 All Classes Namespaces Functions Variables Typedefs Enumerator Groups Pages
mesh-max-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 
32 #include <algorithm>
33 #include <vector>
34 #include <iostream>
35 #include <utility>
36 
37 #include <TriMesh.h>
38 
39 #include <mln/math/max.hh>
40 #include <mln/math/sqr.hh>
41 #include <mln/accu/stat/min_max.hh>
42 #include <mln/fun/v2v/linear.hh>
43 
44 #include "io.hh"
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 
73  std::vector<float> vertex_m(mesh.vertices.size(), 0.f);
74  for (unsigned v = 0; v < mesh.vertices.size(); ++v)
75  // Max curvature.
76  vertex_m[v] = mln::math::max(mln::math::sqr(mesh.curv1[v]),
77  mln::math::sqr(mesh.curv2[v]));
78 
79  // For each face of the mesh, computean an average curvature value
80  // from the mean curvature at its vertices.
81  std::vector<float> face_m(mesh.faces.size(), 0.f);
83  for (unsigned f = 0; f < mesh.faces.size(); ++f)
84  {
85  float m = (vertex_m[mesh.faces[f][0]] +
86  vertex_m[mesh.faces[f][1]] +
87  vertex_m[mesh.faces[f][2]]) / 3;
88  face_m[f] = m;
89  acc.take(m);
90  }
91 
92  /* Shrink the values of FACE_M into the range 0..1, as these are
93  the only values accepted a an RGB floating-point component in the
94  OFF file format. */
95  std::vector<float> normalized_face_m(face_m.size(), 0.0f);
96  std::pair<float, float> min_max(acc);
97  // FIXME: Taken from mln/data/stretch.hh (this should be factored).
98  float min = min_max.first;
99  float max = min_max.second;
100  // Don't normalize actually if the curvature is constant (i.e.,
101  // if min == max).
102  if (min != max)
103  {
104  float m = 0.0f;
105  float M = 1.0f;
106  float a = (M - m) / (max - min);
107  float b = (m * max - M * min) / (max - min);
109  std::transform(face_m.begin(), face_m.end(),
110  normalized_face_m.begin(), f);
111  }
112 
113  // Taken and adapted from TriMesh_io.cc
114  FILE* f_out = fopen(output_filename.c_str(), "wb");
115  if (!f_out)
116  {
117  std::cerr << "Error opening " << output_filename.c_str()
118  << " for writing." << std::endl;
119  std::exit(2);
120  }
121  write_off_float(mesh_ptr, normalized_face_m, f_out);
122  fclose(f_out);
123 
124  delete mesh_ptr;
125 }