DGtal  0.9.2
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 //-----------------------------------------------------------------------------
42 template <typename TSpace>
43 inline
44 DGtal::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 //-----------------------------------------------------------------------------
56 template <typename TSpace>
57 inline
58 DGtal::SpatialCubicalSubdivision<TSpace>::
59 SpatialCubicalSubdivision( 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 //-----------------------------------------------------------------------------
75 template <typename TSpace>
76 inline
77 DGtal::SpatialCubicalSubdivision<TSpace>::
78 SpatialCubicalSubdivision( 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 //-----------------------------------------------------------------------------
90 template <typename TSpace>
91 inline
92 const typename DGtal::SpatialCubicalSubdivision<TSpace>::Domain &
93 DGtal::SpatialCubicalSubdivision<TSpace>::
94 domain() const
95 {
96  return myDomain;
97 }
98 //-----------------------------------------------------------------------------
99 template <typename TSpace>
100 inline
101 const typename DGtal::SpatialCubicalSubdivision<TSpace>::Domain &
102 DGtal::SpatialCubicalSubdivision<TSpace>::
103 binDomain() const
104 {
105  return myArray.domain();
106 }
107 
108 //-----------------------------------------------------------------------------
109 template <typename TSpace>
110 inline
111 typename DGtal::SpatialCubicalSubdivision<TSpace>::Point
112 DGtal::SpatialCubicalSubdivision<TSpace>::
113 bin( Point p ) const
114 {
115  p -= myDomain.lowerBound();
116  return p / mySize;
117 }
118 
119 //-----------------------------------------------------------------------------
120 template <typename TSpace>
121 inline
122 typename DGtal::SpatialCubicalSubdivision<TSpace>::Point
123 DGtal::SpatialCubicalSubdivision<TSpace>::
124 lowest( Point b ) const
125 {
126  b *= mySize;
127  return b + myDomain.lowerBound();
128 }
129 
130 //-----------------------------------------------------------------------------
131 template <typename TSpace>
132 inline
133 typename DGtal::SpatialCubicalSubdivision<TSpace>::Point
134 DGtal::SpatialCubicalSubdivision<TSpace>::
135 uppermost( Point b ) const
136 {
137  b *= mySize;
138  return b + myDiag;
139 }
140 
141 //-----------------------------------------------------------------------------
142 template <typename TSpace>
143 inline
144 void
145 DGtal::SpatialCubicalSubdivision<TSpace>::
146 push( 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 //-----------------------------------------------------------------------------
159 template <typename TSpace>
160 template <typename PointConstIterator>
161 inline
162 void
163 DGtal::SpatialCubicalSubdivision<TSpace>::
164 push( PointConstIterator it, PointConstIterator itE )
165 {
166  for ( ; it != itE; ++it )
167  this->push( *it );
168 }
169 
170 //-----------------------------------------------------------------------------
171 template <typename TSpace>
172 template <typename PointPredicate>
173 inline
174 void
175 DGtal::SpatialCubicalSubdivision<TSpace>::
176 getPoints( 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 //-----------------------------------------------------------------------------
191 template <typename TSpace>
192 inline
193 void
194 DGtal::SpatialCubicalSubdivision<TSpace>::
195 getPoints( 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  */
218 template <typename TSpace>
219 inline
220 void
221 DGtal::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  */
233 template <typename TSpace>
234 inline
235 bool
236 DGtal::SpatialCubicalSubdivision<TSpace>::isValid() const
237 {
238  return true;
239 }
240 
241 
242 
243 ///////////////////////////////////////////////////////////////////////////////
244 // Implementation of inline functions //
245 
246 template <typename TSpace>
247 inline
248 std::ostream&
249 DGtal::operator<< ( std::ostream & out,
250  const SpatialCubicalSubdivision<TSpace> & object )
251 {
252  object.selfDisplay( out );
253  return out;
254 }
255 
256 // //
257 ///////////////////////////////////////////////////////////////////////////////
258 
259