Milena (Olena)  User documentation 2.0a Id
 All Classes Namespaces Functions Variables Typedefs Enumerator Groups Pages
tree_to_image.hh
1 // Copyright (C) 2007, 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 
26 #ifndef MLN_UTIL_TREE_TO_IMAGE_HH
27 # define MLN_UTIL_TREE_TO_IMAGE_HH
28 
33 
34 # include <mln/core/concept/image.hh>
35 # include <mln/core/image/image2d.hh>
36 # include <mln/util/tree.hh>
37 # include <mln/core/site_set/p_set.hh>
38 # include <mln/data/fill.hh>
39 
40 namespace mln
41 {
42 
43  namespace data
44  {
45  template <typename I, typename D>
46  void fill(Image<I>& ima, const D& data);
47 
48  }
49 
50  namespace util
51  {
52 
58  template <typename T, typename I>
59  void
60  tree_to_image(tree<T>& tree, Image<I>& output_);
61 
67  template <typename I, typename J>
68  void
69  display_tree(const Image<J>& ima_, tree<I>& tree);
70 
71 
77  template <typename I, typename J>
78  void
79  display_branch(const Image<J>& ima_, tree_node<I>* tree_node);
80 
81 # ifndef MLN_INCLUDE_ONLY
82 
83  namespace impl
84  {
85 
86  template <typename T, typename I>
87  inline
88  void
89  tree_to_image_rec(tree_node<T>* tree_node, Image<I>& output_)
90  {
91  trace::entering("util::impl::tree_to_image_rec");
92 
93  I& output = exact(output_);
94 
95  mln_piter(p_set<point2d>) p(tree_node->elt().points);
96 
97  for_all(p)
98  output(p) = tree_node->elt().value;
99 
100  typename std::vector< util::tree_node<T>* >::const_iterator it = tree_node->children().begin();
101 
102  for (int i = 0;
103  it != tree_node->children().end();
104  ++it, ++i)
105  {
106  if (*it)
107  tree_to_image_rec((*it), output);
108  }
109  trace::exiting("util::impl::tree_to_image_rec");
110  }
111 
112  template <typename T, typename J>
113  inline
114  void
115  display_tree_rec(const Image<J>& ima_, tree_node<T>* tree_node, int level)
116  {
117  trace::entering("util::impl::display_tree_rec");
118 
119  const J& ima = exact(ima_);
120  display_set(ima, tree_node->elt().points);
121  typename mln::util::tree_node<T>::children_t::iterator it = tree_node->children().begin();
122  for (;
123  it != tree_node->children().end(); ++it)
124  display_tree_rec(ima, (*it), level + 1);
125 
126  trace::exiting("util::impl::display_tree_rec");
127  }
128 
129 
130  template <typename T, typename J, typename K>
131  inline
132  void
133  display_branch_rec(const Image<J>& ima_, tree_node<T>* tree_node, Image<K>& output_)
134  {
135  trace::entering("util::impl::display_branch_rec");
136 
137  K& output = exact(output_);
138  const J& ima = exact(ima_);
139 
140  mln_piter(p_set<point2d>) p(tree_node->elt().points);
141  for_all(p)
142  output(p) = true;
143  typename mln::util::tree_node<T>::children_t::iterator it = tree_node->children().begin();
144  for (;
145  it != tree_node->children().end(); ++it)
146  display_branch_rec(ima, (*it), output);
147 
148  trace::exiting("util::impl::display_branch_rec");
149  }
150 
151 
152  template <typename P, typename J>
153  inline
154  void
155  display_set(const Image<J>& ima_, p_set<P>& s)
156  {
157  trace::entering("util::impl::display_set");
158 
159  const J& ima = exact(ima_);
160  image2d<bool> out(ima.bbox());
161 
162  data::fill(out, false);
163  mln_piter(p_set<P>) p(s);
164  for_all(p)
165  out(p) = true;
166 
167  trace::exiting("util::impl::display_set");
168  }
169 
170 
171  } // end of namespace mln::util::impl
172 
173 
174 
175  template <typename T, typename I>
176  inline
177  void
178  tree_to_image(tree<T>& tree, Image<I>& output_)
179  {
180  trace::entering("util::tree_to_image");
181 
182  I& output = exact(output_);
183  impl::tree_to_image_rec(tree.root(), output);
184 
185  trace::exiting("util::tree_to_image");
186  }
187 
188 
189  template <typename I, typename J>
190  inline
191  void
193  {
194  trace::entering("util::display_tree");
195 
196  mln_precondition(tree.root());
197 
198  const J& ima = exact(ima_);
199  int level = 0;
200 
201  impl::display_tree_rec(ima, tree.root(), level);
202 
203  trace::exiting("util::display_tree");
204  }
205 
206 
207  template <typename I, typename J>
208  inline
209  void
210  display_branch(const Image<J>& ima_, tree_node<I>* tree_node)
211  {
212  trace::entering("util::display_branch");
213 
214  mln_assertion(tree_node);
215 
216  const J& ima = exact(ima_);
217 
218  image2d<bool> output(ima.domain());
219  data::fill(output, false);
220  impl::display_branch_rec(ima, tree_node, output);
221 
222  trace::exiting("util::display_branch");
223  }
224 
225 
226 # endif // ! MLN_INCLUDE_ONLY
227 
228  } // end of namespace mln::util
229 
230 } // end of namespace mln
231 
232 #endif // ! MLN_UTIL_TREE_TO_IMAGE_HH