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 VoronoiCovarianceMeasure.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 VoronoiCovarianceMeasure.h
26 * This file is part of the DGtal library.
30 //////////////////////////////////////////////////////////////////////////////
32 //////////////////////////////////////////////////////////////////////////////
34 ///////////////////////////////////////////////////////////////////////////////
35 // IMPLEMENTATION of inline methods.
36 ///////////////////////////////////////////////////////////////////////////////
38 ///////////////////////////////////////////////////////////////////////////////
39 // ----------------------- Standard services ------------------------------
41 //-----------------------------------------------------------------------------
42 template <typename TSpace, typename TSeparableMetric>
44 DGtal::VoronoiCovarianceMeasure<TSpace,TSeparableMetric>::
45 ~VoronoiCovarianceMeasure()
49 //-----------------------------------------------------------------------------
50 template <typename TSpace, typename TSeparableMetric>
52 DGtal::VoronoiCovarianceMeasure<TSpace,TSeparableMetric>::
53 VoronoiCovarianceMeasure( double _R, double _r, Metric aMetric, bool verbose )
54 : myBigR( _R ), myMetric( aMetric ), myVerbose( verbose ),
55 myDomain( Point::diagonal(0), Point::diagonal(0) ), // dummy domain
58 myProximityStructure( 0 )
60 mySmallR = (_r >= 2.0) ? _r : 2.0;
62 //-----------------------------------------------------------------------------
63 template <typename TSpace, typename TSeparableMetric>
65 DGtal::VoronoiCovarianceMeasure<TSpace,TSeparableMetric>::
66 VoronoiCovarianceMeasure( const VoronoiCovarianceMeasure& other )
67 : myBigR( other.myBigR ), mySmallR( other.mySmallR ),
68 myMetric( other.myMetric ), myVerbose( other.myVerbose ),
69 myDomain( other.myDomain )
71 if ( other.myCharSet ) myCharSet = new CharacteristicSet( *other.myCharSet );
73 if ( other.myVoronoi ) myVoronoi = new Voronoi( *other.myVoronoi );
75 if ( other.myProximityStructure )
76 myProximityStructure = new ProximityStructure( *other.myVoronoi );
77 else myProximityStructure = 0;
79 //-----------------------------------------------------------------------------
80 template <typename TSpace, typename TSeparableMetric>
82 DGtal::VoronoiCovarianceMeasure<TSpace,TSeparableMetric>&
83 DGtal::VoronoiCovarianceMeasure<TSpace,TSeparableMetric>::
84 operator=( const VoronoiCovarianceMeasure& other )
88 myBigR = other.myBigR;
89 mySmallR = other.mySmallR;
90 myMetric = other.myMetric;
91 myVerbose = other.myVerbose;
92 myDomain = other.myDomain;
94 if ( other.myCharSet ) myCharSet = new CharacteristicSet( *other.myCharSet );
95 if ( other.myVoronoi ) myVoronoi = new Voronoi( *other.myVoronoi );
96 if ( other.myProximityStructure )
97 myProximityStructure = new ProximityStructure( *other.myVoronoi );
101 //-----------------------------------------------------------------------------
102 template <typename TSpace, typename TSeparableMetric>
104 typename DGtal::VoronoiCovarianceMeasure<TSpace,TSeparableMetric>::Scalar
105 DGtal::VoronoiCovarianceMeasure<TSpace,TSeparableMetric>::
110 //-----------------------------------------------------------------------------
111 template <typename TSpace, typename TSeparableMetric>
113 typename DGtal::VoronoiCovarianceMeasure<TSpace,TSeparableMetric>::Scalar
114 DGtal::VoronoiCovarianceMeasure<TSpace,TSeparableMetric>::
119 //-----------------------------------------------------------------------------
120 template <typename TSpace, typename TSeparableMetric>
123 DGtal::VoronoiCovarianceMeasure<TSpace,TSeparableMetric>::
126 if ( myCharSet ) { delete myCharSet; myCharSet = 0; }
127 if ( myVoronoi ) { delete myVoronoi; myVoronoi = 0; }
128 if ( myProximityStructure )
129 { delete myProximityStructure; myProximityStructure = 0; }
132 //-----------------------------------------------------------------------------
133 template <typename TSpace, typename TSeparableMetric>
135 const typename DGtal::VoronoiCovarianceMeasure<TSpace,TSeparableMetric>::Domain&
136 DGtal::VoronoiCovarianceMeasure<TSpace,TSeparableMetric>::
141 //-----------------------------------------------------------------------------
142 template <typename TSpace, typename TSeparableMetric>
144 const typename DGtal::VoronoiCovarianceMeasure<TSpace,TSeparableMetric>::Voronoi&
145 DGtal::VoronoiCovarianceMeasure<TSpace,TSeparableMetric>::
148 ASSERT( myVoronoi != 0 );
152 //-----------------------------------------------------------------------------
153 template <typename TSpace, typename TSeparableMetric>
154 template <typename PointInputIterator>
157 DGtal::VoronoiCovarianceMeasure<TSpace,TSeparableMetric>::
158 init( PointInputIterator itb, PointInputIterator ite )
160 BOOST_CONCEPT_ASSERT(( boost::InputIterator< PointInputIterator > ));
161 // PointInputIterator must be an iterator on points.
162 BOOST_STATIC_ASSERT ((boost::is_same< Point, typename PointInputIterator::value_type >::value ));
163 ASSERT( itb != ite );
169 // Start computations
170 if ( myVerbose ) trace.beginBlock( "Computing Voronoi Covariance Measure." );
172 // First pass to get domain.
173 if ( myVerbose ) trace.beginBlock( "Determining computation domain." );
178 for ( PointInputIterator it = itb; it != ite; ++it, ++nbPts )
181 lower = lower.inf( p );
182 upper = upper.sup( p );
183 myVCM[ p ] = matrixZero;
185 Integer intR = (Integer) ceil( myBigR );
186 lower -= Point::diagonal( intR );
187 upper += Point::diagonal( intR );
188 myDomain = Domain( lower, upper );
189 if ( myVerbose ) trace.endBlock();
191 // Second pass to compute characteristic set.
192 if ( myVerbose ) trace.beginBlock( "Computing characteristic set and building proximity structure." );
193 myCharSet = new CharacteristicSet( myDomain );
194 myProximityStructure = new ProximityStructure( lower, upper, (Integer) ceil( mySmallR ) );
195 for ( ; itb != ite; ++itb )
198 myCharSet->setValue( p, true );
199 myProximityStructure->push( p );
201 if ( myVerbose ) trace.endBlock();
203 // Third pass to compute voronoi map.
204 if ( myVerbose ) trace.beginBlock( "Computing voronoi map." );
205 // Voronoi diagram is computed onto complement of K.
206 CharacteristicSetPredicate inCharSet( *myCharSet );
207 NotPredicate notSetPred( inCharSet );
208 myVoronoi = new Voronoi( myDomain, notSetPred, myMetric );
209 if ( myVerbose ) trace.endBlock();
211 // On parcourt le domaine pour calculer le VCM.
212 if ( myVerbose ) trace.beginBlock( "Computing VCM with R-offset." );
213 Size domain_size = myDomain.size();
216 for ( typename Domain::ConstIterator itDomain = myDomain.begin(), itDomainEnd = myDomain.end();
217 itDomain != itDomainEnd; ++itDomain )
219 if ( myVerbose ) trace.progressBar(++di,domain_size);
221 Point q = (*myVoronoi)( p ); // closest site to p
224 double d = myMetric( q, p );
225 if ( d <= myBigR ) // We restrict computation to the R offset of K.
228 // Computes tensor product V^t x V
229 for ( Dimension i = 0; i < Space::dimension; ++i )
230 for ( Dimension j = 0; j < Space::dimension; ++j )
231 m.setComponent( i, j, v[ i ] * v[ j ] );
236 if ( myVerbose ) trace.endBlock();
238 if ( myVerbose ) trace.endBlock();
241 //-----------------------------------------------------------------------------
242 template <typename TSpace, typename TSeparableMetric>
243 template <typename Point2ScalarFunction>
245 typename DGtal::VoronoiCovarianceMeasure<TSpace,TSeparableMetric>::MatrixNN
246 DGtal::VoronoiCovarianceMeasure<TSpace,TSeparableMetric>::
247 measure( Point2ScalarFunction chi_r, Point p ) const
249 ASSERT( myProximityStructure != 0 );
250 std::vector<Point> neighbors;
251 Point b = myProximityStructure->bin( p );
252 myProximityStructure->getPoints( neighbors,
253 b - Point::diagonal(1),
254 b + Point::diagonal(1) );
256 // std::cout << *it << " has " << neighbors.size() << " neighbors." << std::endl;
257 for ( typename std::vector<Point>::const_iterator it_neighbors = neighbors.begin(),
258 it_neighbors_end = neighbors.end(); it_neighbors != it_neighbors_end; ++it_neighbors )
260 Point q = *it_neighbors;
261 Scalar coef = chi_r( q - p );
264 typename std::map<Point,MatrixNN>::const_iterator it = myVCM.find( q );
265 ASSERT( it != myVCM.end() );
266 MatrixNN vcm_q = it->second;
274 //-----------------------------------------------------------------------------
275 template <typename TSpace, typename TSeparableMetric>
277 const typename DGtal::VoronoiCovarianceMeasure<TSpace,TSeparableMetric>::Point2MatrixNN&
278 DGtal::VoronoiCovarianceMeasure<TSpace,TSeparableMetric>::
284 ///////////////////////////////////////////////////////////////////////////////
285 // Interface - public :
288 * Writes/Displays the object on an output stream.
289 * @param out the output stream where the object is written.
291 template <typename TSpace, typename TSeparableMetric>
294 DGtal::VoronoiCovarianceMeasure<TSpace, TSeparableMetric>::
295 selfDisplay ( std::ostream & out ) const
297 out << "[VoronoiCovarianceMeasure]";
301 * Checks the validity/consistency of the object.
302 * @return 'true' if the object is valid, 'false' otherwise.
304 template <typename TSpace, typename TSeparableMetric>
307 DGtal::VoronoiCovarianceMeasure<TSpace, TSeparableMetric>::
315 ///////////////////////////////////////////////////////////////////////////////
316 // Implementation of inline functions //
318 template <typename TSpace, typename TSeparableMetric>
321 DGtal::operator<< ( std::ostream & out,
322 const VoronoiCovarianceMeasure<TSpace, TSeparableMetric> & object )
324 object.selfDisplay( out );
329 ///////////////////////////////////////////////////////////////////////////////