DGtal 1.3.0
Loading...
Searching...
No Matches
SpatialCubicalSubdivision.ih
1/**
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.
6 *
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.
11 *
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/>.
14 *
15 **/
16
17/**
18 * @file SpatialCubicalSubdivision.ih
19 * @author Jacques-Olivier Lachaud (\c jacques-olivier.lachaud@univ-savoie.fr )
20 * Laboratory of Mathematics (CNRS, UMR 5127), University of Savoie, France
21 *
22 * @date 2014/02/11
23 *
24 * Implementation of inline methods defined in SpatialCubicalSubdivision.h
25 *
26 * This file is part of the DGtal library.
27 */
28
29
30//////////////////////////////////////////////////////////////////////////////
31#include <cstdlib>
32//////////////////////////////////////////////////////////////////////////////
33
34///////////////////////////////////////////////////////////////////////////////
35// IMPLEMENTATION of inline methods.
36///////////////////////////////////////////////////////////////////////////////
37
38///////////////////////////////////////////////////////////////////////////////
39// ----------------------- Standard services ------------------------------
40
41//-----------------------------------------------------------------------------
42template <typename TSpace>
43inline
44DGtal::SpatialCubicalSubdivision<TSpace>::
45~SpatialCubicalSubdivision()
46{
47 BinConstRange range = myArray.constRange();
48 for ( typename BinConstRange::ConstIterator it = range.begin(), itE = range.end();
49 it != itE; ++it )
50 {
51 Storage* ptr = *it;
52 if ( ptr ) delete ptr;
53 }
54}
55//-----------------------------------------------------------------------------
56template <typename TSpace>
57inline
58DGtal::SpatialCubicalSubdivision<TSpace>::
59SpatialCubicalSubdivision( const SpatialCubicalSubdivision& other )
60 : myDomain( other.myDomain ), mySize( other.mySize ),
61 myArray( other.binDomain() ), myDiag( other.myDiag )
62{
63 BinConstRange range = myArray.constRange();
64 BinConstRange range_src = other.myArray.constRange();
65 for ( typename BinConstRange::ConstIterator it = range.begin(), itE = range.end(),
66 itSrc = other.myArray.constRange();
67 it != itE; ++it, ++itSrc )
68 {
69 Storage* ptr = *itSrc;
70 if ( ptr != 0 ) *it = new Storage( *ptr );
71 }
72}
73
74//-----------------------------------------------------------------------------
75template <typename TSpace>
76inline
77DGtal::SpatialCubicalSubdivision<TSpace>::
78SpatialCubicalSubdivision( Point lo, Point up, Coordinate size )
79 : myDomain( lo, up ), mySize( size ), myArray( myDomain )
80{
81 Point dimensions = myDomain.upperBound() - myDomain.lowerBound();
82 dimensions /= mySize;
83 // the domain for the bins defines the image domain.
84 myArray = StorageArray( Domain( Point::zero, dimensions ) );
85 // all elements of myArray are initialized with 0.
86 myDiag = myDomain.lowerBound() + Point::diagonal(mySize-1); // used in uppermost
87}
88
89//-----------------------------------------------------------------------------
90template <typename TSpace>
91inline
92const typename DGtal::SpatialCubicalSubdivision<TSpace>::Domain &
93DGtal::SpatialCubicalSubdivision<TSpace>::
94domain() const
95{
96 return myDomain;
97}
98//-----------------------------------------------------------------------------
99template <typename TSpace>
100inline
101const typename DGtal::SpatialCubicalSubdivision<TSpace>::Domain &
102DGtal::SpatialCubicalSubdivision<TSpace>::
103binDomain() const
104{
105 return myArray.domain();
106}
107
108//-----------------------------------------------------------------------------
109template <typename TSpace>
110inline
111typename DGtal::SpatialCubicalSubdivision<TSpace>::Point
112DGtal::SpatialCubicalSubdivision<TSpace>::
113bin( Point p ) const
114{
115 p -= myDomain.lowerBound();
116 return p / mySize;
117}
118
119//-----------------------------------------------------------------------------
120template <typename TSpace>
121inline
122typename DGtal::SpatialCubicalSubdivision<TSpace>::Point
123DGtal::SpatialCubicalSubdivision<TSpace>::
124lowest( Point b ) const
125{
126 b *= mySize;
127 return b + myDomain.lowerBound();
128}
129
130//-----------------------------------------------------------------------------
131template <typename TSpace>
132inline
133typename DGtal::SpatialCubicalSubdivision<TSpace>::Point
134DGtal::SpatialCubicalSubdivision<TSpace>::
135uppermost( Point b ) const
136{
137 b *= mySize;
138 return b + myDiag;
139}
140
141//-----------------------------------------------------------------------------
142template <typename TSpace>
143inline
144void
145DGtal::SpatialCubicalSubdivision<TSpace>::
146push( const Point& p )
147{
148 Point b = bin( p );
149 Storage* pts = myArray( b );
150 if ( pts == 0 )
151 {
152 pts = new Storage;
153 myArray.setValue( b, pts );
154 }
155 pts->push_back( p );
156}
157
158//-----------------------------------------------------------------------------
159template <typename TSpace>
160template <typename PointConstIterator>
161inline
162void
163DGtal::SpatialCubicalSubdivision<TSpace>::
164push( PointConstIterator it, PointConstIterator itE )
165{
166 for ( ; it != itE; ++it )
167 this->push( *it );
168}
169
170//-----------------------------------------------------------------------------
171template <typename TSpace>
172template <typename PointPredicate>
173inline
174void
175DGtal::SpatialCubicalSubdivision<TSpace>::
176getPoints( std::vector<Point> & pts,
177 Point bin_lo, Point bin_up, const PointPredicate & pred ) const
178{
179 Domain local( bin_lo.sup( binDomain().lowerBound() ),
180 bin_up.inf( binDomain().upperBound() ) );
181 for ( typename Domain::ConstIterator it = local.begin(), itE = local.end(); it != itE; ++it )
182 {
183 const Storage* storage = myArray( *it );
184 if ( storage )
185 for ( typename Storage::const_iterator its = storage->begin(), itsE = storage->end();
186 its != itsE; ++its )
187 if ( pred( *its ) ) pts.push_back( *its );
188 }
189}
190//-----------------------------------------------------------------------------
191template <typename TSpace>
192inline
193void
194DGtal::SpatialCubicalSubdivision<TSpace>::
195getPoints( std::vector<Point> & pts,
196 Point bin_lo, Point bin_up ) const
197{
198 Domain local( bin_lo.sup( binDomain().lowerBound() ),
199 bin_up.inf( binDomain().upperBound() ) );
200 for ( typename Domain::ConstIterator it = local.begin(), itE = local.end(); it != itE; ++it )
201 {
202 const Storage* storage = myArray( *it );
203 if ( storage )
204 for ( typename Storage::const_iterator its = storage->begin(), itsE = storage->end();
205 its != itsE; ++its )
206 pts.push_back( *its );
207 }
208}
209
210
211///////////////////////////////////////////////////////////////////////////////
212// Interface - public :
213
214/**
215 * Writes/Displays the object on an output stream.
216 * @param out the output stream where the object is written.
217 */
218template <typename TSpace>
219inline
220void
221DGtal::SpatialCubicalSubdivision<TSpace>::selfDisplay ( std::ostream & out ) const
222{
223 out << "[SpatialCubicalSubdivision domain=" << domain()
224 << " binDomain=" << binDomain()
225 << " binSize=" << mySize
226 << "]";
227}
228
229/**
230 * Checks the validity/consistency of the object.
231 * @return 'true' if the object is valid, 'false' otherwise.
232 */
233template <typename TSpace>
234inline
235bool
236DGtal::SpatialCubicalSubdivision<TSpace>::isValid() const
237{
238 return true;
239}
240
241
242
243///////////////////////////////////////////////////////////////////////////////
244// Implementation of inline functions //
245
246template <typename TSpace>
247inline
248std::ostream&
249DGtal::operator<< ( std::ostream & out,
250 const SpatialCubicalSubdivision<TSpace> & object )
251{
252 object.selfDisplay( out );
253 return out;
254}
255
256// //
257///////////////////////////////////////////////////////////////////////////////
258
259