Milena (Olena)  User documentation 2.0a Id
 All Classes Namespaces Functions Variables Typedefs Enumerator Groups Pages
mesh-segm.cc
1 // Copyright (C) 2008, 2009 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 // FIXME: Factor commons parts between mesh-segm and mesh-skel.
31 
32 #include <cstdlib>
33 #include <cmath>
34 
35 #include <utility>
36 #include <iostream>
37 
38 #include <TriMesh.h>
39 
40 #include <mln/core/alias/point3d.hh>
41 #include <mln/core/alias/point3d.hh>
42 
43 #include <mln/util/graph.hh>
44 #include <mln/core/image/line_graph_image.hh>
45 #include <mln/core/image/line_graph_elt_neighborhood.hh>
46 
47 #include <mln/morpho/closing/area.hh>
48 #include <mln/morpho/meyer_wst.hh>
49 
50 #include "io.hh"
51 
52 
53 // Doesn't C++ have a better way to express Pi ?
54 const float pi = 4 * atanf(1);
55 
56 
57 int main(int argc, char* argv[])
58 {
59  if (argc != 4)
60  {
61  std::cerr << "usage: " << argv[0] << " input.off lambda output.off"
62  << std::endl;
63  std::exit(1);
64  }
65 
66  std::string input_filename = argv[1];
67  unsigned lambda = atoi(argv[2]);
68  std::string output_filename = argv[3];
69 
70 
71  /*-------.
72  | Mesh. |
73  `-------*/
74 
75  // TriMesh is a pain: it systematically allocates on the heap.
76  // Introduce another name to manipulate the mesh as a (non-pointer)
77  // object.
78  TriMesh* mesh_ptr = TriMesh::read(input_filename.c_str());
79  if (!mesh_ptr)
80  std::exit(2);
81  TriMesh& mesh = *mesh_ptr;
82 
83  // Computes faces (triangles).
84  mesh.need_faces();
85  // Computation of the mean curvature on each vertex of the mesh.
86  mesh.need_curvatures();
87  /* FIXME: Our implementation of the WST doesn't work well with
88  floats (yet). Convert floating point values to a proportional
89  integer value for the moment. */
90  typedef int curv_t;
91  std::vector<curv_t> vertex_h_inv(mesh.vertices.size(), 0.f);
92  for (unsigned v = 0; v < mesh.vertices.size(); ++v)
93  {
94  float h = (mesh.curv1[v] + mesh.curv2[v]) / 2;
95  float h_inv = 1 / pi * atan(-h) + pi / 2;
96  /* FIXME: This coefficient is used to distinguish small
97  curvature values. We should get rid of it as soon as
98  morpho::meyer_wst works correctly on images of float
99  values. */
100  vertex_h_inv[v] = 1000 * h_inv;
101  }
102 
103  /*--------.
104  | Graph. |
105  `--------*/
106 
107  /* Build a graph whose vertices correspond to the faces of the mesh,
108  whose edges (between two vertices) correspond to edges (between
109  two faces) of the mesh. */
110 
111  // Values associated to the graph sites.
112  // (Reminder: currently, values associated to vertices in a
113  // line_graph_image are dummy.)
114  std::vector<curv_t> vertex_values (mesh.faces.size(), 0.f);
115  std::vector<curv_t> edge_values;
116 
117  /* FIXME: We don't have the required site type yet. Simulate with a
118  dummy type (point3d). */
120  // Populate the graph with vertices.
121  for (unsigned i = 0; i < mesh.faces.size(); ++i)
122  g.add_vertex (mln::point3d(i, i, i));
123 
124  // Populate the graph with edges.
125  mesh.need_across_edge();
126  for (unsigned f = 0; f < mesh.faces.size(); ++f)
127  for (unsigned e = 0; e < 3; ++e)
128  {
129  int f_adj = mesh.across_edge[f][e];
130  if (f_adj != -1)
131  {
132  // Add an edge into the graph.
133  if (g.add_edge(f, f_adj) != mln_max(mln::util::edge_id::equiv))
134  {
135  // Find the edge (i.e., the two vertices) common to faces
136  // F and F_ADJ.
137  /* FIXME: We lack a proper interface from the TriMesh
138  structure to do this elegantly. */
139  std::vector<int> adj_vertices;
140  adj_vertices.reserve(2);
141  for (unsigned i = 0; i < 3; ++i)
142  for (unsigned j = 0; j < 3; ++j)
143  if (mesh.faces[f][i] == mesh.faces[f_adj][j])
144  adj_vertices.push_back(mesh.faces[f][i]);
145  mln_assertion(adj_vertices.size() == 2);
146 
147  // Compute the mean curvature on the edge.
148  edge_values.push_back((vertex_h_inv[adj_vertices[0]] +
149  vertex_h_inv[adj_vertices[1]])
150  / 2);
151  }
152 
153  // Check the consistency of the two arrays.
154  mln_assertion(g.edges().size() == edge_values.size());
155  }
156  }
157 
158  /*-------------------.
159  | Line graph image. |
160  `-------------------*/
161 
162  mln::p_line_graph<mln::point3d> plg(g);
163 
164  typedef mln::line_graph_image<mln::point3d, curv_t> ima_t;
165  ima_t lg_ima(plg, vertex_values, edge_values);
166 
167  /*-----------------.
168  | Simplification. |
169  `-----------------*/
170 
171  typedef mln::line_graph_elt_neighborhood<mln::point3d> nbh_t;
172  nbh_t nbh;
173 
174  ima_t closed_lg_ima = mln::morpho::closing::area(lg_ima, nbh, lambda);
175 
176  /*------.
177  | WST. |
178  `------*/
179 
180  typedef unsigned wst_val_t;
181  wst_val_t nbasins;
182  typedef mln::line_graph_image<mln::point3d, wst_val_t> wst_ima_t;
183  wst_ima_t wshed = mln::morpho::meyer_wst(closed_lg_ima, nbh, nbasins);
184  std::cout << "nbasins = " << nbasins << std::endl;
185 
186  /*------------------------------------------.
187  | Label graph vertices (i.e., mesh faces). |
188  `------------------------------------------*/
189 
190  /* FIXME: We should be using wshed.vertex_values_ if
191  mln::line_graph_image were fully functional... */
192  std::vector<unsigned> vertex_label(wshed.domain().nvertices(), 0);
193  mln_piter_(wst_ima_t) pw(wshed.domain());
194  for_all(pw)
195  if (wshed(pw) != 0)
196  {
197  mln_psite_(wst_ima_t) pp(pw);
198  vertex_label[pp.first_id().to_equiv()] = wshed(pw);
199  vertex_label[pp.second_id().to_equiv()] = wshed(pw);
200  }
201 
202  /*---------.
203  | Output. |
204  `---------*/
205 
206  // Choose random colors for each basin number.
207  std::vector<mln::value::rgb8> basin_color (nbasins + 1);
208  for (unsigned i = 0; i <= nbasins; ++i)
209  basin_color[i] = mln::value::rgb8(random() % 256,
210  random() % 256,
211  random() % 256);
212 
213  // Assign colors to graph vertices (mesh faces).
214  std::vector<mln::value::rgb8> face_color (vertex_label.size());
215  for (unsigned i = 0; i < vertex_label.size() ; ++i)
216  face_color[i] = basin_color[vertex_label[i]];
217 
218  // Taken and adapted from TriMesh_io.cc
219  FILE* f_out = fopen(output_filename.c_str(), "wb");
220  if (!f_out)
221  {
222  std::cerr << "Error opening " << output_filename.c_str()
223  << " for writing." << std::endl;
224  std::exit(2);
225  }
226  write_off_colored(mesh_ptr, face_color, f_out);
227  fclose(f_out);
228 
229  delete mesh_ptr;
230 }