$extrastylesheet
Olena  User documentation 2.1
An Image Processing Platform
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
mesh-complex-2-collapse.cc
1 // Copyright (C) 2008, 2009, 2011 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 
30 
31 #include <iostream>
32 
33 #include <mln/core/image/complex_image.hh>
34 #include <mln/core/image/complex_neighborhoods.hh>
35 
36 #include <mln/core/image/dmorph/image_if.hh>
37 #include <mln/core/image/dmorph/sub_image.hh>
38 #include <mln/core/image/dmorph/mutable_extension_ima.hh>
39 #include <mln/core/routine/mutable_extend.hh>
40 #include <mln/data/paste.hh>
41 
42 #include <mln/topo/is_n_face.hh>
43 #include <mln/topo/is_simple_pair.hh>
44 #include <mln/topo/detach_pair.hh>
45 #include <mln/topo/skeleton/breadth_first_thinning.hh>
46 
47 #include <mln/io/vtk/load.hh>
48 #include <mln/io/vtk/save.hh>
49 
50 
51 int
52 main(int argc, char* argv[])
53 {
54  if (argc != 3)
55  {
56  std::cerr << "usage: " << argv[0] << " input.vtk output.vtk"
57  << std::endl;
58  std::exit(1);
59  }
60 
61  std::string input_filename = argv[1];
62  std::string output_filename = argv[2];
63 
64  /*----------------------.
65  | Complex input image. |
66  `----------------------*/
67 
68  // Image type.
69  typedef mln::bin_2complex_image3df ima_t;
70  // Dimension of the image (and thus of the complex).
71  static const unsigned D = ima_t::dim;
72  // Geometry of the image.
73  typedef mln_geom_(ima_t) G;
74 
75  ima_t ima;
76  mln::io::vtk::load(ima, input_filename);
77 
78  /*-------------.
79  | 2-collapse. |
80  `-------------*/
81 
82  // ------------------------------- //
83  // Image restricted to triangles. //
84  // ------------------------------- //
85 
86  // Predicate type: is a face a triangle (2-face)?
87  typedef mln::topo::is_n_face<mln_psite_(ima_t), D> is_a_triangle_t;
88  is_a_triangle_t is_a_triangle;
89  // Surface image type, of which domain is restricted to triangles.
90  typedef mln::image_if<ima_t, is_a_triangle_t> triangle_only_ima_t;
91  // Surface image type, of which iteration (not domain) is restricted
92  // to triangles.
93  typedef mln::mutable_extension_ima<triangle_only_ima_t, ima_t>
94  triangle_ima_t;
95 
96  // ------------------------ //
97  // Simple point predicate. //
98  // ------------------------ //
99 
100  // Neighborhood type returning the set of (n-1)-faces adjacent to a
101  // an n-face.
102  typedef mln::complex_lower_neighborhood<D, G> lower_adj_nbh_t;
103  lower_adj_nbh_t lower_adj_nbh;
104  // Neighborhood type returning the set of (n+1)-faces adjacent to a
105  // an n-face.
106  typedef mln::complex_higher_neighborhood<D, G> higher_adj_nbh_t;
107  higher_adj_nbh_t higher_adj_nbh;
108  // Predicate type: is a triangle (2-face) simple?
109  typedef mln::topo::is_simple_pair< triangle_ima_t,
110  lower_adj_nbh_t,
111  higher_adj_nbh_t >
112  is_simple_triangle_t;
113  is_simple_triangle_t is_simple_triangle(lower_adj_nbh, higher_adj_nbh);
114 
115  // ------------------------------- //
116  // Simple point detach procedure. //
117  // ------------------------------- //
118 
119  // Functor detaching a cell.
120  typedef mln::topo::detach_pair< triangle_ima_t,
121  lower_adj_nbh_t,
122  higher_adj_nbh_t > detach_t;
123  detach_t detach(lower_adj_nbh, higher_adj_nbh);
124 
125  // ------------------------ //
126  // Thinning by 2-collapse. //
127  // ------------------------ //
128 
130  typedef mln::complex_lower_dim_connected_n_face_neighborhood<D, G> nbh_t;
131  nbh_t nbh;
132 
133  mln_concrete_(ima_t) skel;
134  mln::initialize(skel, ima);
135  mln::data::paste
136  (mln::topo::skeleton::breadth_first_thinning
137  (mln::mutable_extend((ima | is_a_triangle).rw(), ima),
138  nbh,
139  is_simple_triangle,
140  detach)
141  /* Before pasting the result of the computation into SKEL,
142  re-expand its domain to the initial site set, to ensure data
143  from all faces (i.e., both the 2-faces, directly processed;
144  and the 1-faces from the extension, undirectly processed). */
145  | ima.domain(),
146  skel);
147 
148  /*---------.
149  | Output. |
150  `---------*/
151 
152  mln::io::vtk::save(skel, output_filename);
153 }