DGtal  0.9.2
DGtal/shapes/Shapes.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 Shapes.ih
19  * @author Jacques-Olivier Lachaud (\c jacques-olivier.lachaud@univ-savoie.fr )
20  * Laboratory of Mathematics (CNRS, UMR 5807), University of Savoie, France
21  *
22  * @date 2010/10/28
23  *
24  * Implementation of inline methods defined in Shapes.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  * Destructor.
43  */
44 template <typename TDomain>
45 inline
46 DGtal::Shapes<TDomain>::~Shapes()
47 {
48 }
49 
50 
51 /**
52  * Removes the discrete ball (norm-1) of center [aCenter] and radius
53  * [aRadius] to the (perhaps non empty) set [aSet].
54  *
55  * @tparam TDigitalSet the type chosen for the digital set.
56  * @param aSet the set (modified) which will contain the discrete ball.
57  * @param aCenter the center of the ball.
58  * @param aRadius the radius of the ball.
59  */
60 template <typename TDomain>
61 template <typename TDigitalSet>
62 inline
63 void
64 DGtal::Shapes<TDomain>::removeNorm1Ball
65 ( TDigitalSet & aSet,
66  const Point & aCenter,
67  UnsignedInteger aRadius )
68 {
69  Point v1( aCenter.diagonal( aRadius ) );
70  Point p1( aCenter );
71  Point p2( p1 );
72  p1 -= v1;
73  p2 += v1;
74  const Domain & domain = aSet.domain();
75  typedef DGtal::HyperRectDomain<Space> LocalSpace;
76  LocalSpace ballDomain( p1, p2 );
77  for ( typename LocalSpace::ConstIterator it = ballDomain.begin();
78  it != ballDomain.end();
79  ++it )
80  {
81  if ( domain.isInside( *it )
82  && ( (*it - aCenter ).norm1() <= aRadius ) )
83  aSet.erase( *it );
84  }
85 }
86 /**
87  * Adds the discrete ball (norm-1) of center [aCenter] and radius
88  * [aRadius] to the (perhaps non empty) set [aSet].
89  *
90  * @tparam TDigitalSet the type chosen for the digital set.
91  * @param aSet the set (modified) which will contain the discrete ball.
92  * @param aCenter the center of the ball.
93  * @param aRadius the radius of the ball.
94  */
95 template <typename TDomain>
96 template <typename TDigitalSet>
97 inline
98 void
99 DGtal::Shapes<TDomain>::addNorm1Ball
100 ( TDigitalSet & aSet,
101  const Point & aCenter,
102  UnsignedInteger aRadius )
103 {
104  Point v1( aCenter.diagonal( aRadius ) );
105  Point p1( aCenter );
106  Point p2( p1 );
107  p1 -= v1;
108  p2 += v1;
109  const Domain & domain = aSet.domain();
110  typedef DGtal::HyperRectDomain<Space> LocalSpace;
111  LocalSpace ballDomain( p1, p2 );
112  for ( typename LocalSpace::ConstIterator it = ballDomain.begin();
113  it != ballDomain.end();
114  ++it )
115  {
116  if ( domain.isInside( *it )
117  && ( (*it - aCenter ).norm1() <= aRadius ) )
118  aSet.insert( *it );
119  }
120 }
121 
122 
123 /**
124  * Removes the discrete ball (norm-2) of center [aCenter] and radius
125  * [aRadius] to the (perhaps non empty) set [aSet].
126  *
127  * @tparam TDigitalSet the type chosen for the digital set.
128  * @param aSet the set (modified) which will contain the discrete ball.
129  * @param aCenter the center of the ball.
130  * @param aRadius the radius of the ball.
131  */
132 template <typename TDomain>
133 template <typename TDigitalSet>
134 inline
135 void
136 DGtal::Shapes<TDomain>::removeNorm2Ball
137 ( TDigitalSet & aSet,
138  const Point & aCenter,
139  UnsignedInteger aRadius )
140 {
141  Point v1( aCenter.diagonal( aRadius ) );
142  Point p1( aCenter );
143  Point p2( p1 );
144  p1 -= v1;
145  p2 += v1;
146  const Domain & domain = aSet.domain();
147  typedef DGtal::HyperRectDomain<Space> LocalSpace;
148  LocalSpace ballDomain( p1, p2 );
149  for ( typename LocalSpace::ConstIterator it = ballDomain.begin();
150  it != ballDomain.end();
151  ++it )
152  {
153  if ( domain.isInside( *it )
154  && ( (*it - aCenter ).norm() <= aRadius ) )
155  aSet.erase( *it );
156  }
157 }
158 
159 
160 /**
161  * Adds the discrete ball (norm-2) of center [aCenter] and radius
162  * [aRadius] to the (perhaps non empty) set [aSet].
163  *
164  * @tparam TDigitalSet the type chosen for the digital set.
165  * @param aSet the set (modified) which will contain the discrete ball.
166  * @param aCenter the center of the ball.
167  * @param aRadius the radius of the ball.
168  */
169 template <typename TDomain>
170 template <typename TDigitalSet>
171 inline
172 void
173 DGtal::Shapes<TDomain>::addNorm2Ball
174 ( TDigitalSet & aSet,
175  const Point & aCenter,
176  UnsignedInteger aRadius )
177 {
178  Point v1( aCenter.diagonal( aRadius ) );
179  Point p1( aCenter );
180  Point p2( p1 );
181  p1 -= v1;
182  p2 += v1;
183  const Domain & domain = aSet.domain();
184  typedef DGtal::HyperRectDomain<Space> LocalSpace;
185  LocalSpace ballDomain( p1, p2 );
186  for ( typename LocalSpace::ConstIterator it = ballDomain.begin();
187  it != ballDomain.end();
188  ++it )
189  {
190  if ( domain.isInside( *it )
191  && ( (*it - aCenter ).norm() <= aRadius ) )
192  aSet.insert( *it );
193  }
194 }
195 
196 template <typename TDomain>
197 template <typename TDigitalSet, typename ShapeFunctor>
198 void
199 DGtal::Shapes<TDomain>::digitalShaper( TDigitalSet & aSet,
200  const ShapeFunctor & aFunctor)
201 {
202  typedef DGtal::HyperRectDomain<Space> LocalSpace;
203 
204  BOOST_CONCEPT_ASSERT((concepts::CDigitalBoundedShape<ShapeFunctor>));
205  BOOST_CONCEPT_ASSERT((concepts::CDigitalOrientedShape<ShapeFunctor>));
206 
207 
208  Point pLow = aFunctor.getLowerBound();
209  Point pUpp = aFunctor.getUpperBound();
210 
211  LocalSpace implicitDomain( pLow, pUpp );
212  for ( typename LocalSpace::ConstIterator it = implicitDomain.begin();
213  it != implicitDomain.end();
214  ++it )
215  {
216  if ( aFunctor.orientation( *it ) == INSIDE || aFunctor.orientation( *it ) == ON )
217  aSet.insert( *it );
218  }
219 }
220 
221 
222 template <typename TDomain>
223 template <typename TDigitalSet, typename ShapeFunctor>
224 void
225 DGtal::Shapes<TDomain>::euclideanShaper( TDigitalSet & aSet,
226  const ShapeFunctor & aFunctor,
227  const double h)
228 {
229 
230  BOOST_CONCEPT_ASSERT((concepts::CEuclideanBoundedShape<ShapeFunctor>));
231  BOOST_CONCEPT_ASSERT((concepts::CEuclideanOrientedShape<ShapeFunctor>));
232 
233  RealPoint pLow = aFunctor.getLowerBound();
234  RealPoint pUpp = aFunctor.getUpperBound();
235  GaussDigitizer<Space,ShapeFunctor> dig;
236  dig.attach( aFunctor ); // attaches the shape.
237  dig.init( pLow, pUpp, h);
238 
239  // Creates a set from the digitizer.
240  Shapes<Domain>::digitalShaper( aSet, dig );
241 }
242 
243 template <typename TDomain>
244 template <typename DigitalSet, typename PointPredicate>
245 void
246 DGtal::Shapes<TDomain>::makeSetFromPointPredicate
247 ( DigitalSet & aSet,
248  const PointPredicate & aPP )
249 {
250 
251  BOOST_CONCEPT_ASSERT(( concepts::CDigitalSet< DigitalSet > ));
252  BOOST_CONCEPT_ASSERT(( concepts::CPointPredicate< PointPredicate > ));
253  BOOST_STATIC_ASSERT
254  (( concepts::ConceptUtils::SameType< Domain, typename DigitalSet::Domain >::value ));
255  BOOST_STATIC_ASSERT
256  (( concepts::ConceptUtils::SameType< Point, typename PointPredicate::Point >::value ));
257 
258  typedef typename Domain::ConstIterator ConstIterator;
259  for ( ConstIterator it = aSet.domain().begin(),
260  it_end = aSet.domain().end(); it != it_end; ++it )
261  if ( aPP( *it ) ) aSet.insert( *it );
262 }
263 
264 
265 
266 
267 ///////////////////////////////////////////////////////////////////////////////
268 // Interface - public :
269 
270 /**
271  * Writes/Displays the object on an output stream.
272  * @param out the output stream where the object is written.
273  */
274 template <typename TDomain>
275 inline
276 void
277 DGtal::Shapes<TDomain>::selfDisplay ( std::ostream & out ) const
278 {
279  out << "[Shapes]";
280 }
281 
282 /**
283  * Checks the validity/consistency of the object.
284  * @return 'true' if the object is valid, 'false' otherwise.
285  */
286 template <typename TDomain>
287 inline
288 bool
289 DGtal::Shapes<TDomain>::isValid() const
290 {
291  return true;
292 }
293 
294 
295 
296 ///////////////////////////////////////////////////////////////////////////////
297 // Implementation of inline functions //
298 
299 template <typename TDomain>
300 inline
301 std::ostream&
302 DGtal::operator<< ( std::ostream & out,
303  const Shapes<TDomain> & object )
304 {
305  object.selfDisplay( out );
306  return out;
307 }
308 
309 // //
310 ///////////////////////////////////////////////////////////////////////////////
311 
312