$extrastylesheet
Olena  User documentation 2.1
An Image Processing Platform
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
simple_point3d_lut.hh
1 // Copyright (C) 2011 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 
28 
29 #ifndef TOOLS_SIMPLE_POINT3D_LUT_HH
30 # define TOOLS_SIMPLE_POINT3D_LUT_HH
31 
32 #include "connectivity_numbers_3d.hh"
33 
34 
36 typedef std::vector<bool> simple_point_lut_t;
37 
38 
39 /*------------------------------------------------------------------.
40 | Sequential computation of a look-up table (LUT) showing whether a |
41 | 3D point is simple or not. |
42 `------------------------------------------------------------------*/
43 
53 template <typename FG, typename BG>
54 simple_point_lut_t
55 simple_point3d_lut(FG fg_fun, BG bg_fun)
56 {
57  using namespace mln;
58 
59  typedef image3d<bool> I;
60  // B must be a model of mln::Box.
61  typedef mln_domain(I) B;
62  typedef mln_psite(I) P;
63 
64  B b = make::box3d(-1,-1,-1, 1,1,1);
65  I fg_ima(b, 0);
66  I bg_ima(b, 0);
67  P p(0, 0, 0);
68 
69  const unsigned dim = 3;
70  const unsigned max_nneighbs = mlc_pow_int(3, dim) - 1;
71  const unsigned nconfigs = mlc_pow_int(2, max_nneighbs);
72 
73  simple_point_lut_t simple_point_lut(nconfigs, false);
74 
75  typedef neighb3d N;
76  N nbh = c26();
77 
78  for (config_3d_t i = 0; i < nconfigs; ++i)
79  {
80  /* Create the local i-th configuration around P.
81 
82  Note that the value corresponding to P is always `false',
83  to prevent the connection of two components through P. */
84  data::fill(fg_ima, false);
85  data::fill(bg_ima, false);
86  config_3d_t tmp = i;
87  mln_fwd_niter_(N) n(nbh, p);
88  for_all(n)
89  {
90  if (tmp % 2)
91  fg_ima(n) = true;
92  else
93  bg_ima(n) = true;
94  tmp = tmp >> 1;
95  }
96  simple_point_lut[i] = ( fg_fun(fg_ima) == 1
97  && bg_fun(bg_ima) == 1);
98  }
99  return simple_point_lut;
100 }
101 
102 
103 /*-------------------------------------------------------------.
104 | (6, 26) configurations: 6-connected foreground, 26-connected |
105 | background. |
106 `-------------------------------------------------------------*/
107 
108 simple_point_lut_t
109 simple_point3d_lut__6_26()
110 {
111  return simple_point3d_lut(connectivity_number_3d__6_26_one,
112  connectivity_number_3d__26_6_one);
113 }
114 
115 
116 /*-------------------------------------------------------------.
117 | (26, 6) configurations: 26-connected foreground, 6-connected |
118 | background. |
119 `-------------------------------------------------------------*/
120 
121 simple_point_lut_t
122 simple_point3d_lut__26_6()
123 {
124  return simple_point3d_lut(connectivity_number_3d__26_6_one,
125  connectivity_number_3d__6_26_one);
126 }
127 
128 
129 /*---------------------------------------------------------------.
130 | (6+, 18) configurations: 6+-connected foreground, 18-connected |
131 | background. |
132 `---------------------------------------------------------------*/
133 
134 simple_point_lut_t
135 simple_point3d_lut__6p_18()
136 {
137  return simple_point3d_lut(connectivity_number_3d__6p_18_one,
138  connectivity_number_3d__18_6p_one);
139 }
140 
141 
142 /*---------------------------------------------------------------.
143 | (18, 6+) configurations: 18-connected foreground, 6+-connected |
144 | background. |
145 `---------------------------------------------------------------*/
146 
147 simple_point_lut_t
148 simple_point3d_lut__18_6p()
149 {
150  return simple_point3d_lut(connectivity_number_3d__18_6p_one,
151  connectivity_number_3d__6p_18_one);
152 }
153 
154 /*----------------------.
155 | Helpers for drivers. |
156 `----------------------*/
157 
158 void
159 display_simple_point_lut(const simple_point_lut_t& simple_point_lut)
160 {
161  const unsigned bits_per_byte = 8;
162 
163  // Set up the output format.
164  std::cout << std::hex << std::setfill ('0');
165  std::string prefix = "0x";
166 
167  size_t i = 0;
168  while (i < simple_point_lut.size())
169  {
171  mln::value::int_u8 mask = 1;
172  for (unsigned j = 0; i < simple_point_lut.size() && j < bits_per_byte;
173  ++j, ++i)
174  {
175  if (simple_point_lut[i])
176  pack = pack | mask;
177  mask = mask << 1;
178  }
179 
180  /* Display.
181 
182  Note that except maybe after for the last iteration of the
183  outer loop, I is always a multiple of 8. */
184  std::cout << prefix << std::setw(2) << pack << ", ";
185  if (i % 64 == 0) std::cout << std::endl;
186  if (i % 256 == 0) std::cout << std::endl;
187  }
188 }
189 
190 #endif // ! TOOLS_SIMPLE_POINT3D_LUT_HH