$extrastylesheet
Olena  User documentation 2.1
An Image Processing Platform
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
apps/graph-morpho/convert.hh
1 // Copyright (C) 2009, 2010 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 
26 #ifndef APPS_GRAPH_MORPHO_CONVERT_HH
27 # define APPS_GRAPH_MORPHO_CONVERT_HH
28 
31 
32 # include <mln/core/alias/complex_image.hh>
33 # include <mln/core/image/image2d.hh>
34 
35 # include <mln/math/abs.hh>
36 
37 # include "apps/graph-morpho/make.hh"
38 
39 
40 namespace convert
41 {
42 
44  inline
47  {
48  using namespace mln;
49 
50  const unsigned dim = 1;
52 
53  // Compute the bounding box of the domain of FROM.
56  for_all(v)
57  {
58  mln_site_(geom_t) s(v);
59  // Site S is point2d multi-site and should be a singleton (since V
60  // iterates on vertices).
61  mln_invariant(s.size() == 1);
62  point2d p = s.front();
63  bbox.take(p);
64  }
65  mln::box2d support = bbox;
66 
67  point2d box_p_min(support.pmin().row() * 2, support.pmin().col() * 2);
68  point2d box_p_max(support.pmax().row() * 2, support.pmax().col() * 2);
69  image2d<bool> output(box2d(box_p_min, box_p_max));
70  data::fill(output, false);
71 
72  // Iterate on vertices.
73  for_all(v)
74  {
75  mln_site_(geom_t) s(v);
76  // Site S is point2d multi-site and should be a singleton (since V
77  // iterates on vertices).
78  mln_invariant(s.size() == 1);
79  point2d p_in = s.front();
80  point2d p_out(p_in.row() * 2, p_in.col() * 2);
81  output(p_out) = input(v);
82  }
83 
84  // Iterate on edges.
86  for_all(e)
87  {
88  mln_site_(geom_t) s(e);
89  // Site S is point2d multi-site and should be a pair (since E
90  // iterates on vertices).
91  mln_invariant(s.size() == 2);
92  point2d p1 = s[0];
93  point2d p2 = s[1];
94  if (p1.row() == p2.row())
95  {
96  // Horizontal edge.
97  point2d p_out(p1.row() * 2,
98  p1.col() + p2.col());
99  output(p_out) = input(e);
100  }
101  else if (p1.col() == p2.col())
102  {
103  // Vertical edge.
104  point2d p_out(p1.row() + p2.row(),
105  p1.col() * 2);
106  output(p_out) = input(e);
107  }
108  else
109  {
110  // Edge not fitting in the 2D regular grid canvas, aborting.
111  abort();
112  }
113  }
114  return output;
115  }
116 
117 
119  inline
122  {
123  using namespace mln;
124 
125  const box2d& input_box = input.domain();
126  // The input image must have an odd number of rows and columns.
127  mln_precondition(input_box.nrows() % 2 == 1);
128  mln_precondition(input_box.ncols() % 2 == 1);
129 
130  // The domain of the graph image is twice as small, since we
131  // consider only vertices (edges are set ``between'' vertices).
132  box2d output_box(input_box.nrows() / 2 + 1,
133  input_box.ncols() / 2 + 1);
134  bin_1complex_image2d output = ::make::complex1d_image<bool>(output_box);
135 
136  const unsigned dim = 1;
138 
139  // Add values on vertices.
140  p_n_faces_fwd_piter<dim, geom_t> v(output.domain(), 0);
141  for_all(v)
142  {
143  mln_site_(geom_t) s(v);
144  // Site S is point2d multi-site and should be a singleton (since V
145  // iterates on vertices).
146  mln_invariant(s.size() == 1);
147  point2d p = s.front();
148  point2d q(p.row() * 2, p.col() * 2);
149  output(v) = input(q);
150  }
151 
152  // Add values on edges.
153  p_n_faces_fwd_piter<dim, geom_t> e(output.domain(), 1);
154  for_all(e)
155  {
156  mln_site_(geom_t) s(e);
157  // Site S is point2d multi-site and should be a pair (since E
158  // iterates on vertices).
159  mln_invariant(s.size() == 2);
160  point2d p1 = s[0];
161  point2d p2 = s[1];
162  mln_invariant(math::abs(p1.row() - p2.row()) == 1
163  || math::abs(p1.col() - p2.col()) == 1);
164  point2d q (p1.row() + p2.row(), p1.col() + p2.col());
165  output(e) = input(q);
166  }
167 
168  return output;
169  }
170 
171 } // end of namespace convert
172 
173 
174 #endif // ! APPS_GRAPH_MORPHO_CONVERT_HH