2 * This program is free software: you can redistribute it and/or modify
3 * it under the terms of the GNU Lesser General Public License as
4 * published by the Free Software Foundation, either version 3 of the
5 * License, or (at your option) any later version.
7 * This program is distributed in the hope that it will be useful,
8 * but WITHOUT ANY WARRANTY; without even the implied warranty of
9 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10 * GNU General Public License for more details.
12 * You should have received a copy of the GNU General Public License
13 * along with this program. If not, see <http://www.gnu.org/licenses/>.
18 * @file NeighborhoodConvexityAnalyzer.ih
19 * @author Jacques-Olivier Lachaud (\c jacques-olivier.lachaud@univ-savoie.fr )
20 * Laboratory of Mathematics (CNRS, UMR 5127), University of Savoie, France
24 * Implementation of inline methods defined in NeighborhoodConvexityAnalyzer.h
26 * This file is part of the DGtal library.
30 //////////////////////////////////////////////////////////////////////////////
32 //////////////////////////////////////////////////////////////////////////////
34 //-----------------------------------------------------------------------------
35 template < typename TKSpace, int K >
36 template < typename PointPredicate >
38 DGtal::NeighborhoodConvexityAnalyzer<TKSpace,K>::
39 setCenter( Point c, const PointPredicate& X )
41 ASSERT( myDomain.isInside( c ) );
46 Domain local_domain( c - Point::diagonal( radius() ),
47 c + Point::diagonal( radius() ) );
50 for ( auto q : local_domain )
56 else if ( myDomain.isInside( q ) )
67 computeBasicConfigurations( myCfgX, myBasicCfgX );
70 //-----------------------------------------------------------------------------
71 template < typename TKSpace, int K >
73 DGtal::NeighborhoodConvexityAnalyzer<TKSpace,K>::
74 getLocalX( std::vector< Point >& localX, bool with_center ) const
76 Domain local_domain( myCenter - Point::diagonal( radius() ),
77 myCenter + Point::diagonal( radius() ) );
79 for ( auto q : local_domain )
81 if ( q == myCenter ) {
83 localX.push_back( myCenter );
84 } else if ( myDomain.isInside( q ) ) {
85 if ( myCfgX.test( bit ) )
86 localX.push_back( q );
91 //-----------------------------------------------------------------------------
92 template < typename TKSpace, int K >
94 DGtal::NeighborhoodConvexityAnalyzer<TKSpace,K>::
95 getLocalCompX( std::vector< Point >& localCompX, bool with_center ) const
97 Domain local_domain( myCenter - Point::diagonal( radius() ),
98 myCenter + Point::diagonal( radius() ) );
100 for ( auto q : local_domain )
102 if ( q == myCenter ) {
104 localCompX.push_back( myCenter );
105 } else if ( myDomain.isInside( q ) ) {
106 if ( ! myCfgX.test( bit ) )
107 localCompX.push_back( q );
113 //-----------------------------------------------------------------------------
114 template < typename TKSpace, int K >
116 DGtal::NeighborhoodConvexityAnalyzer<TKSpace,K>::
117 checkBasicConfigurationsFullConvexity
118 ( bool compX, bool with_center ) const
122 const Size mask_center = with_center ? 0x10 : 0;
123 for ( auto cfg : myBasicCfgX )
124 if ( ! myBasicFullConvexityTable[ cfg.to_ulong() | mask_center ] )
128 { // complementary of shape X
129 const Size mask_center = with_center ? 0x1ff : 0x1ef;
130 for ( auto cfg : myBasicCfgX )
131 if ( ! myBasicFullConvexityTable[ (~cfg.to_ulong()) & mask_center ] )
136 //-----------------------------------------------------------------------------
137 template < typename TKSpace, int K >
139 DGtal::NeighborhoodConvexityAnalyzer<TKSpace,K>::
140 checkBasicConfigurations0Convexity
141 ( bool compX, bool with_center ) const
145 const Size mask_center = with_center ? 0x10 : 0;
146 for ( auto cfg : myBasicCfgX )
147 if ( ! myBasic0ConvexityTable[ cfg.to_ulong() | mask_center ] )
151 { // complementary of shape X
152 const Size mask_center = with_center ? 0x1ff : 0x1ef;
153 for ( auto cfg : myBasicCfgX )
154 if ( ! myBasic0ConvexityTable[ (~cfg.to_ulong()) & mask_center ] )
159 //-----------------------------------------------------------------------------
160 template < typename TKSpace, int K >
162 DGtal::NeighborhoodConvexityAnalyzer<TKSpace,K>::
163 computeBasicConfigurations
164 ( Configuration cfg, std::vector< BasicConfiguration > & result ) const
166 if ( dimension == 2 ) result.push_back( cfg.to_ulong() );
169 for ( Dimension i = 0; i < dimension; i++ )
170 for ( Dimension j = i+1; j < dimension; j++ )
171 result.push_back( computeCentralBasicConfiguration( cfg, i, j ) );
175 //-----------------------------------------------------------------------------
176 template < typename TKSpace, int K >
177 typename DGtal::NeighborhoodConvexityAnalyzer<TKSpace,K>::BasicConfiguration
178 DGtal::NeighborhoodConvexityAnalyzer<TKSpace,K>::
179 computeCentralBasicConfiguration
180 ( Configuration cfg, Dimension i, Dimension j ) const
184 for ( Dimension d = 0; d < i; ++d ) { incr_i *= 2*K+1; }
185 for ( Dimension d = 0; d < j; ++d ) { incr_j *= 2*K+1; }
186 BasicConfiguration basic;
187 basic[ 0 ] = cfg[ middle - incr_i - incr_j ];
188 basic[ 1 ] = cfg[ middle - incr_j ];
189 basic[ 2 ] = cfg[ middle + incr_i - incr_j ];
190 basic[ 3 ] = cfg[ middle - incr_i ];
191 basic[ 4 ] = cfg[ middle ];
192 basic[ 5 ] = cfg[ middle + incr_i ];
193 basic[ 6 ] = cfg[ middle - incr_i + incr_j ];
194 basic[ 7 ] = cfg[ middle + incr_j ];
195 basic[ 8 ] = cfg[ middle + incr_i + incr_j ];
199 //-----------------------------------------------------------------------------
200 template < typename TKSpace, int K >
202 DGtal::NeighborhoodConvexityAnalyzer<TKSpace,K>::
203 computeBasicFullConvexityTable()
205 typedef KhalimskySpaceND< 2, int > K2;
206 typedef K2::Point Point2;
207 typedef K2::Space Z2;
208 typedef DigitalConvexity< K2 > DigConv2;
209 typedef HyperRectDomain< Z2 > Domain2;
210 DigConv2 dconv2( Point2::diagonal( -2 ), Point2::diagonal( 2 ) );
211 Point2 p1 = Point2::diagonal( -1 );
212 Point2 p2 = Point2::diagonal( 1 );
213 Point2 c = Point2::diagonal( 0 );
214 Domain2 domain( p1, p2 );
215 for ( unsigned int cfg = 0; cfg < 512; cfg++ )
217 // Building a configuration.
218 std::vector< Point2 > X;
219 unsigned int mask = 1;
220 for ( auto it = domain.begin(); it != domain.end(); ++it )
222 const Point2 p = *it;
223 if ( cfg & mask ) X.push_back( p );
226 myBasicFullConvexityTable[ cfg ] = dconv2.isFullyConvex( X );
227 myBasic0ConvexityTable [ cfg ] = dconv2.is0Convex ( X );
231 ///////////////////////////////////////////////////////////////////////////////
232 // Implementation of inline functions //
236 ///////////////////////////////////////////////////////////////////////////////