$extrastylesheet
Olena  User documentation 2.1
An Image Processing Platform
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
connectivity_numbers_3d_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_CONNECTIVITY_NUMBERS_3D_TBB_HH
30 # define TOOLS_CONNECTIVITY_NUMBERS_3D_TBB_HH
31 
32 #include <tbb/blocked_range.h>
33 #include <tbb/parallel_for.h>
34 
35 #include "connectivity_numbers_3d.hh"
36 
37 
38 /*--------------------------------------------------.
39 | Parallel computation of 3D connectivity numbers. |
40 `--------------------------------------------------*/
41 
42 // The core of the loop within connectivity_numbers_3d_tbb below.
43 template <typename F>
45 {
46  typedef mln::neighb3d N;
47 
48  connectivity_numbers_3d_tbb_range(F f, const N& nbh, conn_numbers_t& numbers)
49  : f_(f), nbh_(nbh), numbers_(numbers)
50  {
51  }
52 
53  void operator()(const tbb::blocked_range<config_3d_t>& r) const
54  {
55  using namespace mln;
56 
57  typedef image3d<bool> I;
58  // B must be a model of mln::Box.
59  typedef mln_domain(I) B;
60  typedef mln_psite(I) P;
61 
62  B b = make::box3d(-1,-1,-1, 1,1,1);
63  I ima(b, 0);
64  P p(0, 0, 0);
65 
66  for (config_3d_t i = r.begin(); i != r.end(); ++i)
67  {
68  /* Create the local i-th configuration around P.
69 
70  Note that the value corresponding to P is always `false',
71  to prevent the connection of two components through P. */
72  data::fill(ima, false);
73  config_3d_t tmp = i;
74  mln_fwd_niter_(N) n(nbh_, p);
75  for_all(n)
76  {
77  if (tmp % 2)
78  ima(n) = true;
79  tmp = tmp >> 1;
80  }
81  numbers_[i] = f_(ima);
82  }
83  }
84 
85 private:
86  F f_;
87  // NBH is copied (not taken as a reference) to avoid thread-unsafe
88  // accesses to this neighborhood (see the long explanation in
89  // connectivity_numbers_3d.hh).
90  N nbh_;
91  conn_numbers_t& numbers_;
92 };
93 
98 template <typename F>
99 conn_numbers_t
100 connectivity_numbers_3d_tbb(F f)
101 {
102  // FIXME: Code duplicated from connectivity_numbers_3d_tbb_range.
103  const unsigned dim = 3;
104  const unsigned max_nneighbs = mlc_pow_int(3, dim) - 1;
105  const unsigned nconfigs = mlc_pow_int(2, max_nneighbs);
106 
107  typedef mln::neighb3d N;
108  N nbh = mln::c26();
109  conn_numbers_t numbers(nconfigs, 0);
110  tbb::parallel_for(tbb::blocked_range<config_3d_t>(0, nconfigs),
111  connectivity_numbers_3d_tbb_range<F>(f, nbh, numbers));
112  return numbers;
113 }
114 
115 
116 /*-------------------------------------------------------------.
117 | (6, 26) configurations: 6-connected foreground, 26-connected |
118 | background. |
119 `-------------------------------------------------------------*/
120 
121 conn_numbers_t
122 connectivity_numbers_3d_tbb__6_26()
123 {
124  return connectivity_numbers_3d_tbb(connectivity_number_3d__6_26_one);
125 }
126 
127 
128 /*-------------------------------------------------------------.
129 | (26, 6) configurations: 26-connected foreground, 6-connected |
130 | background. |
131 `-------------------------------------------------------------*/
132 
133 conn_numbers_t
134 connectivity_numbers_3d_tbb__26_6()
135 {
136  return connectivity_numbers_3d_tbb(connectivity_number_3d__26_6_one);
137 }
138 
139 
140 /*---------------------------------------------------------------.
141 | (6+, 18) configurations: 6+-connected foreground, 18-connected |
142 | background. |
143 `---------------------------------------------------------------*/
144 
145 conn_numbers_t
146 connectivity_numbers_3d_tbb__6p_18()
147 {
148  return connectivity_numbers_3d_tbb(connectivity_number_3d__6p_18_one);
149 }
150 
151 
152 /*---------------------------------------------------------------.
153 | (18, 6+) configurations: 18-connected foreground, 6+-connected |
154 | background. |
155 `---------------------------------------------------------------*/
156 
157 conn_numbers_t
158 connectivity_numbers_3d_tbb__18_6p()
159 {
160  return connectivity_numbers_3d_tbb(connectivity_number_3d__18_6p_one);
161 }
162 
163 #endif // ! TOOLS_CONNECTIVITY_NUMBERS_3D_TBB_HH