$extrastylesheet
Olena  User documentation 2.1
An Image Processing Platform
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
mesh-complex-max-curv.cc
1 // Copyright (C) 2008, 2009, 2010, 2013 EPITA Research and Development
2 // Laboratory (LRDE)
3 //
4 // This file is part of Olena.
5 //
6 // Olena is free software: you can redistribute it and/or modify it under
7 // the terms of the GNU General Public License as published by the Free
8 // Software Foundation, version 2 of the License.
9 //
10 // Olena is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 // General Public License for more details.
14 //
15 // You should have received a copy of the GNU General Public License
16 // along with Olena. If not, see <http://www.gnu.org/licenses/>.
17 //
18 // As a special exception, you may use this file as part of a free
19 // software project without restriction. Specifically, if other files
20 // instantiate templates or use macros or inline functions from this
21 // file, or you compile this file and link it with other files to produce
22 // an executable, this file does not by itself cause the resulting
23 // executable to be covered by the GNU General Public License. This
24 // exception does not however invalidate any other reasons why the
25 // executable file might be covered by the GNU General Public License.
26 
31 
32 #include <cstdlib>
33 #include <cmath>
34 
35 #include <utility>
36 #include <iostream>
37 
38 #include <mln/core/image/complex_image.hh>
39 #include <mln/core/image/complex_neighborhoods.hh>
40 
41 #include <mln/data/fill.hh>
42 #include <mln/literal/zero.hh>
43 
44 #include <mln/math/max.hh>
45 #include <mln/math/sqr.hh>
46 #include <mln/accu/stat/min_max.hh>
47 #include <mln/fun/v2v/linear.hh>
48 #include <mln/data/transform.hh>
49 
50 #include <mln/literal/white.hh>
51 
52 #include <mln/io/off/load.hh>
53 #include <mln/io/off/save.hh>
54 
55 #include "misc.hh"
56 
57 
58 int main(int argc, char* argv[])
59 {
60  if (argc != 3)
61  {
62  std::cerr << "usage: " << argv[0] << " input.off output.off"
63  << std::endl;
64  std::exit(1);
65  }
66 
67  std::string input_filename = argv[1];
68  std::string output_filename = argv[2];
69 
70  /*----------------.
71  | Complex image. |
72  `----------------*/
73 
74  // Image type.
75  typedef mln::float_2complex_image3df ima_t;
76  // Dimension of the image (and therefore of the complex).
77  static const unsigned D = ima_t::dim;
78  // Geometry of the image.
79  typedef mln_geom_(ima_t) G;
80 
81  mln::bin_2complex_image3df input;
82  mln::io::off::load(input, input_filename);
83 
84  std::pair<ima_t, ima_t> curv = mln::geom::mesh_curvature(input.domain());
85 
86  // Compute the max curvature at each vertex.
87  ima_t max_curv(input.domain());
88  mln::data::fill(max_curv, mln::literal::zero);
89  mln::p_n_faces_fwd_piter<D, G> v(max_curv.domain(), 0);
90  for_all(v)
91  max_curv(v) = mln::math::max(mln::math::sqr(curv.first(v)),
92  mln::math::sqr(curv.second(v)));
93 
94  // Propagate these values on triangles.
95  mln::p_n_faces_fwd_piter<D, G> t(max_curv.domain(), 2);
96  typedef mln::complex_m_face_neighborhood<D, G> adj_vertices_nbh_t;
97  adj_vertices_nbh_t adj_vertices_nbh;
98  mln_niter_(adj_vertices_nbh_t) adj_v(adj_vertices_nbh, t);
99  /* FIXME: Not really user friendly! The `m' value should pass at
100  the construction of ADJ_V. */
101  adj_v.iter().set_m(0);
102  mln::accu::stat::min_max<float> acc;
103  // Iterate on triangles (2-faces).
104  for_all(t)
105  {
106  float s = 0.0f;
107  unsigned n = 0;
108  // Iterate on vertices (0-faces).
109  for_all(adj_v)
110  {
111  s += max_curv(adj_v);
112  ++n;
113  }
114  float m = s / n;
115  max_curv(t) = m;
116  acc.take(m);
117  // A triangle should be adjacent to exactly three vertices.
118  mln_invariant(n <= 3);
119  }
120 
121  // Normalize values between 0 and 1.
122  /* Shrink the values of FACE_M into the range 0..1, as these are
123  the only values accepted a an RGB floating-point component in the
124  OFF file format. */
125  ima_t output(max_curv.domain());
127  std::pair<float, float> min_max(acc);
128  // FIXME: Taken from mln/data/stretch.hh (this should be factored).
129  float min = min_max.first;
130  float max = min_max.second;
131  std::cout << min << std::endl;
132  std::cout << max << std::endl;
133  // Don't normalize actually if the curvature is constant (i.e.,
134  // if min == max).
135  if (min != max)
136  {
137  float m = 0.0f;
138  float M = 1.0f;
139  float a = (M - m) / (max - min);
140  float b = (m * max - M * min) / (max - min);
142  output = mln::data::transform(max_curv, f);
143  }
144 
145  // Output.
146  mln::io::off::save(output, output_filename);
147 }