$extrastylesheet
Olena  User documentation 2.1
An Image Processing Platform
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
graph_elt_window_if.cc
1 // Copyright (C) 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 #include <mln/core/image/edge_image.hh>
27 #include <mln/core/image/vertex_image.hh>
28 
29 #include <mln/core/image/graph_elt_window_if.hh>
30 
31 #include <mln/make/dummy_p_vertices.hh>
32 #include <mln/make/p_vertices_with_mass_centers.hh>
33 #include <mln/make/dummy_p_edges.hh>
34 #include <mln/make/p_edges_with_mass_centers.hh>
35 
36 #include <mln/data/fill.hh>
37 
38 #include <mln/util/graph.hh>
39 #include <mln/util/site_pair.hh>
40 
41 int main()
42 {
43  using namespace mln;
44 
45  typedef util::graph G;
46 
47  /*--------.
48  | Graph. |
49  `--------*/
50 
51  /* The graph is as follows:
52 
53  _____
54  / \
55  0 1 2 - 3
56  \ / \ /
57  4 - 5
58 
59  */
60 
61  util::graph gr;
62  gr.add_vertices(6);
63  gr.add_edge(1,3);
64  gr.add_edge(1,4);
65  gr.add_edge(2,3);
66  gr.add_edge(2,4);
67  gr.add_edge(2,5);
68  gr.add_edge(3,5);
69  gr.add_edge(4,5);
70 
72  {
73  static const unsigned result[] = { 3, 3, 5, 1, 5, 1, 5, 3 };
74 
75  // Create a vertex image.
76  typedef p_vertices<G> pv_t;
77  pv_t pv = make::dummy_p_vertices(gr);
78  typedef vertex_image<void,unsigned> v_ima_t;
79  v_ima_t v_ima(pv);
80  data::fill(v_ima, 4);
81 
82  // Create a mask on vertices.
83  typedef mln_ch_value_(v_ima_t, bool) v_mask_t;
84  v_mask_t v_mask(pv);
85  mln_piter_(v_mask_t) vm(v_mask.domain());
86  for_all(vm)
87  v_mask(vm) = vm.id()%2;
88 
89  mln_piter_(v_ima_t) v(v_ima.domain());
90  typedef graph_elt_window_if<util::graph, pv_t, v_mask_t> win_t;
91  win_t win(v_mask);
92  mln_qiter_(win_t) q(win, v);
93  unsigned i = 0;
94  for_all(v)
95  for_all(q)
96  {
97  mln_assertion(result[i++] == q.id());
98  mln_assertion(q.id()%2);
99  }
100  }
101 
103  {
104  // Create an edge image.
105  typedef p_edges<G> pe_t;
106  pe_t pe = make::dummy_p_edges(gr);
107  typedef edge_image<void,unsigned> e_ima_t;
108  e_ima_t e_ima(pe);
109  data::fill(e_ima, 3);
110 
111  // Create a mask on edges.
112  typedef mln_ch_value_(e_ima_t, bool) e_mask_t;
113  e_mask_t e_mask(pe);
114  mln_piter_(e_mask_t) em(e_mask.domain());
115  for_all(em)
116  e_mask(em) = em.id()%2;
117 
118  // Iterate on edges neighborhood according to the given mask.
119  mln_piter_(e_ima_t) e(e_ima.domain());
120  typedef graph_elt_window_if<util::graph, pe_t, e_mask_t> win_t;
121  win_t win(e_mask);
122  mln_qiter_(win_t) q(win, e);
123  for_all(e)
124  for_all(q)
125  mln_assertion(q.id()%2);
126  }
127 
128 
130  {
131  // Create an edge image.
132  typedef p_edges<G> pe_t;
133  pe_t pe = make::dummy_p_edges(gr);
134  typedef edge_image<void,unsigned> e_ima_t;
135  e_ima_t e_ima(pe);
136  data::fill(e_ima, 3);
137 
138 
139  // Create a mask on vertices.
140  typedef p_vertices<G> pv_t;
141  pv_t pv = make::dummy_p_vertices(gr);
142  typedef vertex_image<void,unsigned> v_ima_t;
143  v_ima_t v_ima(pv);
144  typedef mln_ch_value_(v_ima_t, bool) v_mask_t;
145  v_mask_t v_mask(pv);
146  mln_piter_(v_mask_t) vm(v_mask.domain());
147  for_all(vm)
148  v_mask(vm) = vm.id()%2;
149 
150  mln_piter_(e_ima_t) e(e_ima.domain());
151  typedef graph_elt_window_if<util::graph, pe_t, v_mask_t> win_t;
152  win_t win(v_mask);
153  mln_qiter_(win_t) q(win, e);
154  for_all(e)
155  for_all(q)
156  mln_assertion(v_mask(q.v1()) || v_mask(q.v2()));
157  }
158 
159 
161  {
162  static const unsigned result[] = { 4, 4, 5, 1, 2, 3 };
163 
164  // Create a vertex image.
165  typedef p_vertices<G> pv_t;
166  pv_t pv = make::dummy_p_vertices(gr);
167  typedef vertex_image<void,unsigned> v_ima_t;
168  v_ima_t v_ima(pv);
169  data::fill(v_ima, 4);
170 
171  // Create a mask on edges.
172  typedef p_edges<G> pe_t;
173  pe_t pe = make::dummy_p_edges(gr);
174  typedef edge_image<void,bool> e_mask_t;
175  e_mask_t e_mask(pe);
176  mln_piter_(e_mask_t) em(e_mask.domain());
177  for_all(em)
178  e_mask(em) = em.id()%2;
179 
180  mln_piter_(v_ima_t) v(v_ima.domain());
181  typedef graph_elt_window_if<util::graph, pv_t, e_mask_t> win_t;
182  win_t win(e_mask);
183  mln_qiter_(win_t) q(win, v);
184  unsigned i = 0;
185  for_all(v)
186  for_all(q)
187  {
188  mln_assertion(result[i++] == q.id());
189  mln_assertion(e_mask(v.edge_with(q).id()));
190  }
191  }
192 }