$extrastylesheet
Olena  User documentation 2.1
An Image Processing Platform
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
simple_point3d_lut_tbb.hh
1 // Copyright (C) 2011, 2013 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_TBB_HH
30 # define TOOLS_SIMPLE_POINT3D_LUT_TBB_HH
31 
32 #include <tbb/blocked_range.h>
33 #include <tbb/parallel_for.h>
34 
35 #include "simple_point3d_lut.hh"
36 
37 
38 /*-------------------------------------------------------------------.
39 | Parallel computation of a look-up table (LUT) showing whether a 3D |
40 | point is simple or not. |
41 `-------------------------------------------------------------------*/
42 
43 // The core of the loop within connectivity_numbers_3d_tbb below.
44 template <typename FG, typename BG>
46 {
47  typedef mln::neighb3d N;
48 
49  simple_point3d_lut_tbb_range(FG fg_fun, BG bg_fun, const N& nbh,
50  simple_point_lut_t& simple_point_lut)
51  : fg_fun_(fg_fun), bg_fun_(bg_fun), nbh_(nbh),
52  simple_point_lut_(simple_point_lut)
53  {
54  }
55 
56  void operator()(const tbb::blocked_range<config_3d_t>& r) const
57  {
58  using namespace mln;
59 
60  typedef image3d<bool> I;
61  // B must be a model of mln::Box.
62  typedef mln_domain(I) B;
63  typedef mln_psite(I) P;
64 
65  B b = make::box3d(-1,-1,-1, 1,1,1);
66  I fg_ima(b, 0);
67  I bg_ima(b, 0);
68  P p(0, 0, 0);
69 
70  for (config_3d_t i = r.begin(); i != r.end(); ++i)
71  {
72  /* Create the local i-th configuration around P.
73 
74  Note that the value corresponding to P is always `false',
75  to prevent the connection of two components through P. */
76  data::fill(fg_ima, false);
77  data::fill(bg_ima, false);
78  config_3d_t tmp = i;
79  mln_fwd_niter_(N) n(nbh_, p);
80  for_all(n)
81  {
82  if (tmp % 2)
83  fg_ima(n) = true;
84  else
85  bg_ima(n) = true;
86  tmp = tmp >> 1;
87  }
88  simple_point_lut_[i] = ( fg_fun_(fg_ima) == 1
89  && bg_fun_(bg_ima) == 1);
90  }
91  }
92 
93 private:
94  FG fg_fun_;
95  BG bg_fun_;
96  // NBH is copied (not taken as a reference) to avoid thread-unsafe
97  // accesses to this neighborhood (see the long explanation in
98  // connectivity_numbers_3d.hh).
99  N nbh_;
100  simple_point_lut_t& simple_point_lut_;
101 };
102 
112 template <typename FG, typename BG>
113 simple_point_lut_t
114 simple_point3d_lut_tbb(FG fg_fun, BG bg_fun)
115 {
116  // FIXME: Code duplicated from simple_point3d_lut_tbb_range.
117  const unsigned dim = 3;
118  const unsigned max_nneighbs = mlc_pow_int(3, dim) - 1;
119  const unsigned nconfigs = mlc_pow_int(2, max_nneighbs);
120 
121  typedef mln::neighb3d N;
122  N nbh = mln::c26();
123  simple_point_lut_t simple_point_lut(nconfigs, false);
124 
125  tbb::parallel_for(tbb::blocked_range<config_3d_t>(0, nconfigs),
127  nbh,
128  simple_point_lut));
129 
130  return simple_point_lut;
131 }
132 
133 
134 /*-------------------------------------------------------------.
135 | (6, 26) configurations: 6-connected foreground, 26-connected |
136 | background. |
137 `-------------------------------------------------------------*/
138 
139 simple_point_lut_t
140 simple_point3d_lut_tbb__6_26()
141 {
142  return simple_point3d_lut_tbb(connectivity_number_3d__6_26_one,
143  connectivity_number_3d__26_6_one);
144 }
145 
146 
147 /*-------------------------------------------------------------.
148 | (26, 6) configurations: 26-connected foreground, 6-connected |
149 | background. |
150 `-------------------------------------------------------------*/
151 
152 simple_point_lut_t
153 simple_point3d_lut_tbb__26_6()
154 {
155  return simple_point3d_lut_tbb(connectivity_number_3d__26_6_one,
156  connectivity_number_3d__6_26_one);
157 }
158 
159 
160 /*---------------------------------------------------------------.
161 | (6+, 18) configurations: 6+-connected foreground, 18-connected |
162 | background. |
163 `---------------------------------------------------------------*/
164 
165 simple_point_lut_t
166 simple_point3d_lut_tbb__6p_18()
167 {
168  return simple_point3d_lut_tbb(connectivity_number_3d__6p_18_one,
169  connectivity_number_3d__18_6p_one);
170 }
171 
172 
173 /*---------------------------------------------------------------.
174 | (18, 6+) configurations: 18-connected foreground, 6+-connected |
175 | background. |
176 `---------------------------------------------------------------*/
177 
178 simple_point_lut_t
179 simple_point3d_lut_tbb__18_6p()
180 {
181  return simple_point3d_lut_tbb(connectivity_number_3d__18_6p_one,
182  connectivity_number_3d__6p_18_one);
183 }
184 
185 #endif // ! TOOLS_SIMPLE_POINT3D_LUT_TBB_HH