35 #ifndef __INCLUDE_MIST_BOUNDARY__
36 #define __INCLUDE_MIST_BOUNDARY__
38 #ifndef __INCLUDE_MIST_H__
47 namespace __boundary_controller__
52 template <
class Array >
53 static inline typename Array::size_type neighbor( Array &in,
const typename Array::size_type i[ 4 ],
const typename Array::size_type j[ 4 ],
const typename Array::size_type k[ 4 ] )
60 struct neighbors< 26 >
62 template <
class Array >
63 static inline typename Array::size_type neighbor( Array &in,
const typename Array::size_type i[ 4 ],
const typename Array::size_type j[ 4 ],
const typename Array::size_type k[ 4 ] )
65 typedef typename Array::size_type size_type;
69 count = in( i[ 0 ], j[ 0 ], k[ 0 ] );
70 count *= in( i[ 1 ], j[ 0 ], k[ 0 ] );
71 count *= in( i[ 2 ], j[ 0 ], k[ 0 ] );
72 count *= in( i[ 0 ], j[ 1 ], k[ 0 ] );
73 count *= in( i[ 1 ], j[ 1 ], k[ 0 ] );
74 count *= in( i[ 2 ], j[ 1 ], k[ 0 ] );
75 count *= in( i[ 0 ], j[ 2 ], k[ 0 ] );
76 count *= in( i[ 1 ], j[ 2 ], k[ 0 ] );
77 count *= in( i[ 2 ], j[ 2 ], k[ 0 ] );
79 count *= in( i[ 0 ], j[ 0 ], k[ 1 ] );
80 count *= in( i[ 1 ], j[ 0 ], k[ 1 ] );
81 count *= in( i[ 2 ], j[ 0 ], k[ 1 ] );
82 count *= in( i[ 0 ], j[ 1 ], k[ 1 ] );
83 count *= in( i[ 2 ], j[ 1 ], k[ 1 ] );
84 count *= in( i[ 0 ], j[ 2 ], k[ 1 ] );
85 count *= in( i[ 1 ], j[ 2 ], k[ 1 ] );
86 count *= in( i[ 2 ], j[ 2 ], k[ 1 ] );
88 count *= in( i[ 0 ], j[ 0 ], k[ 2 ] );
89 count *= in( i[ 1 ], j[ 0 ], k[ 2 ] );
90 count *= in( i[ 2 ], j[ 0 ], k[ 2 ] );
91 count *= in( i[ 0 ], j[ 1 ], k[ 2 ] );
92 count *= in( i[ 1 ], j[ 1 ], k[ 2 ] );
93 count *= in( i[ 2 ], j[ 1 ], k[ 2 ] );
94 count *= in( i[ 0 ], j[ 2 ], k[ 2 ] );
95 count *= in( i[ 1 ], j[ 2 ], k[ 2 ] );
96 count *= in( i[ 2 ], j[ 2 ], k[ 2 ] );
98 count *= i[ 3 ] * j[ 3 ] * k[ 3 ];
105 struct neighbors< 18 >
107 template <
class Array >
108 static inline typename Array::size_type neighbor( Array &in,
const typename Array::size_type i[ 4 ],
const typename Array::size_type j[ 4 ],
const typename Array::size_type k[ 4 ] )
110 typedef typename Array::size_type size_type;
114 count = in( i[ 1 ], j[ 0 ], k[ 0 ] );
115 count *= in( i[ 0 ], j[ 1 ], k[ 0 ] );
116 count *= in( i[ 1 ], j[ 1 ], k[ 0 ] );
117 count *= in( i[ 2 ], j[ 1 ], k[ 0 ] );
118 count *= in( i[ 1 ], j[ 2 ], k[ 0 ] );
120 count *= in( i[ 0 ], j[ 0 ], k[ 1 ] );
121 count *= in( i[ 1 ], j[ 0 ], k[ 1 ] );
122 count *= in( i[ 2 ], j[ 0 ], k[ 1 ] );
123 count *= in( i[ 0 ], j[ 1 ], k[ 1 ] );
124 count *= in( i[ 2 ], j[ 1 ], k[ 1 ] );
125 count *= in( i[ 0 ], j[ 2 ], k[ 1 ] );
126 count *= in( i[ 1 ], j[ 2 ], k[ 1 ] );
127 count *= in( i[ 2 ], j[ 2 ], k[ 1 ] );
129 count *= in( i[ 1 ], j[ 0 ], k[ 2 ] );
130 count *= in( i[ 0 ], j[ 1 ], k[ 2 ] );
131 count *= in( i[ 1 ], j[ 1 ], k[ 2 ] );
132 count *= in( i[ 2 ], j[ 1 ], k[ 2 ] );
133 count *= in( i[ 1 ], j[ 2 ], k[ 2 ] );
135 count *= i[ 3 ] * j[ 3 ] * k[ 3 ];
142 struct neighbors< 6 >
144 template <
class Array >
145 static inline typename Array::size_type neighbor( Array &in,
const typename Array::size_type i[ 4 ],
const typename Array::size_type j[ 4 ],
const typename Array::size_type k[ 4 ] )
147 typedef typename Array::size_type size_type;
151 count = in( i[ 1 ], j[ 1 ], k[ 0 ] );
152 count *= in( i[ 1 ], j[ 0 ], k[ 1 ] );
153 count *= in( i[ 0 ], j[ 1 ], k[ 1 ] );
154 count *= in( i[ 2 ], j[ 1 ], k[ 1 ] );
155 count *= in( i[ 1 ], j[ 2 ], k[ 1 ] );
156 count *= in( i[ 1 ], j[ 1 ], k[ 2 ] );
158 count *= i[ 3 ] * j[ 3 ] * k[ 3 ];
165 struct neighbors< 8 >
167 template <
class Array >
168 static inline typename Array::size_type neighbor( Array &in,
const typename Array::size_type i[ 4 ],
const typename Array::size_type j[ 4 ],
const typename Array::size_type k[ 4 ] )
170 typedef typename Array::size_type size_type;
174 count = in( i[ 0 ], j[ 0 ], k[ 1 ] );
175 count *= in( i[ 1 ], j[ 0 ], k[ 1 ] );
176 count *= in( i[ 2 ], j[ 0 ], k[ 1 ] );
177 count *= in( i[ 0 ], j[ 1 ], k[ 1 ] );
178 count *= in( i[ 2 ], j[ 1 ], k[ 1 ] );
179 count *= in( i[ 0 ], j[ 2 ], k[ 1 ] );
180 count *= in( i[ 1 ], j[ 2 ], k[ 1 ] );
181 count *= in( i[ 2 ], j[ 2 ], k[ 1 ] );
183 count *= i[ 3 ] * j[ 3 ];
190 struct neighbors< 4 >
192 template <
class Array >
193 static inline typename Array::size_type neighbor( Array &in,
const typename Array::size_type i[ 4 ],
const typename Array::size_type j[ 4 ],
const typename Array::size_type k[ 4 ] )
195 typedef typename Array::size_type size_type;
199 count = in( i[ 1 ], j[ 0 ], k[ 1 ] );
200 count *= in( i[ 0 ], j[ 1 ], k[ 1 ] );
201 count *= in( i[ 2 ], j[ 1 ], k[ 1 ] );
202 count *= in( i[ 1 ], j[ 2 ], k[ 1 ] );
204 count *= i[ 3 ] * j[ 3 ];
211 template <
class Array,
class neighbor,
class Functor >
212 typename Array::size_type boundary( Array &in,
typename Array::value_type border,
typename Array::value_type inside,
bool boundary_is_border,
const neighbor , Functor f )
214 typedef typename Array::size_type size_type;
215 typedef typename Array::value_type value_type;
218 size_type ii[ 4 ], jj[ 4 ], kk[ 4 ];
220 const size_type width = in.width( );
221 const size_type height = in.height( );
222 const size_type depth = in.depth( );
224 const bool bprogress1 = depth == 1;
225 const bool bprogress2 = depth > 1;
230 value_type boundary = boundary_is_border ? 0 : 1;
232 for( i = 0 ; i < in.size( ) ; i++ )
234 in[ i ] = in[ i ] == 0 ? 0 : 1;
237 for( k = 0 ; k < depth ; k++ )
239 kk[ 0 ] = k == 0 ? k : k - 1;
241 kk[ 2 ] = k == depth - 1 ? k : k + 1;
242 kk[ 3 ] = 1 < k && k < depth - 1 ? 1 : boundary;
244 for( j = 0 ; j < height ; j++ )
246 jj[ 0 ] = j == 0 ? j : j - 1;
248 jj[ 2 ] = j == height - 1 ? j : j + 1;
249 jj[ 3 ] = 1 < j && j < height - 1 ? 1 : boundary;
251 for( i = 0 ; i < width ; i++ )
253 ii[ 0 ] = i == 0 ? i : i - 1;
255 ii[ 2 ] = i == width - 1 ? i : i + 1;
256 ii[ 3 ] = 1 < i && i < width - 1 ? 1 : boundary;
258 if( in( i, j, k ) != 0 )
260 if( neighbor::neighbor( in, ii, jj, kk ) == 0 )
270 f( static_cast< double >( j + 1 ) / static_cast< double >( height ) * 100.0 );
276 f( static_cast< double >( k + 1 ) / static_cast< double >( depth ) * 100.0 );
280 for( i = 0 ; i < in.size( ) ; i++ )
284 in[ i ] = in[ i ] == 2 ? border : inside;
320 template <
class T,
class Allocator,
class Functor >
323 return( __boundary_controller__::boundary( in, border, inside, boundary_is_border, __boundary_controller__::neighbors< 4 >( ), f ) );
339 template <
class T,
class Allocator >
360 template <
class T,
class Allocator,
class Functor >
363 return( __boundary_controller__::boundary( in, border, inside, boundary_is_border, __boundary_controller__::neighbors< 8 >( ), f ) );
379 template <
class T,
class Allocator >
399 template <
class T,
class Allocator,
class Functor >
402 return( __boundary_controller__::boundary( in, border, inside, boundary_is_border, __boundary_controller__::neighbors< 6 >( ), f ) );
418 template <
class T,
class Allocator >
438 template <
class T,
class Allocator,
class Functor >
441 return( __boundary_controller__::boundary( in, border, inside, boundary_is_border, __boundary_controller__::neighbors< 18 >( ), f ) );
457 template <
class T,
class Allocator >
477 template <
class T,
class Allocator,
class Functor >
480 return( __boundary_controller__::boundary( in, border, inside, boundary_is_border, __boundary_controller__::neighbors< 26 >( ), f ) );
496 template <
class T,
class Allocator >
512 #endif // __INCLUDE_MIST_BOUNDARY__