DGtal  1.4.beta
ParallelStrip.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 ParallelStrip.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 2013/09/26
23  *
24  * Implementation of inline methods defined in ParallelStrip.h
25  *
26  * This file is part of the DGtal library.
27  */
28 
29 
30 //////////////////////////////////////////////////////////////////////////////
31 #include <cstdlib>
32 #include <cmath>
33 //////////////////////////////////////////////////////////////////////////////
34 
35 ///////////////////////////////////////////////////////////////////////////////
36 // IMPLEMENTATION of inline methods.
37 ///////////////////////////////////////////////////////////////////////////////
38 
39 ///////////////////////////////////////////////////////////////////////////////
40 // ----------------------- Standard services ------------------------------
41 
42 //-----------------------------------------------------------------------------
43 template <typename TSpace, bool muIncluded, bool muPlusNuIncluded>
44 inline
45 DGtal::ParallelStrip<TSpace, muIncluded, muPlusNuIncluded>::~ParallelStrip()
46 {}
47 //-----------------------------------------------------------------------------
48 template <typename TSpace, bool muIncluded, bool muPlusNuIncluded>
49 inline
50 DGtal::ParallelStrip<TSpace, muIncluded, muPlusNuIncluded>::ParallelStrip()
51 {}
52 //-----------------------------------------------------------------------------
53 template <typename TSpace, bool muIncluded, bool muPlusNuIncluded>
54 inline
55 DGtal::ParallelStrip<TSpace, muIncluded, muPlusNuIncluded>::ParallelStrip
56 ( Scalar aMu, const RealVector & N, Scalar aNu )
57  : myMu( aMu ), myN( N ), myNu( aNu )
58 {
59  Scalar l = myN.norm();
60  if ( l != 1.0 ) {
61  myMu /= l;
62  myN /= l;
63  myNu /= l;
64  }
65 }
66 //-----------------------------------------------------------------------------
67 template <typename TSpace, bool muIncluded, bool muPlusNuIncluded>
68 inline
69 DGtal::ParallelStrip<TSpace, muIncluded, muPlusNuIncluded>::ParallelStrip
70 ( const ParallelStrip& other )
71  : myMu( other.myMu ), myN( other.myN ), myNu( other.myNu )
72 {
73  ASSERT( myNu != NumberTraits<Scalar>::ZERO );
74  ASSERT( myN.norm1() != NumberTraits<Scalar>::ZERO );
75 }
76 //-----------------------------------------------------------------------------
77 template <typename TSpace, bool muIncluded, bool muPlusNuIncluded>
78 inline
79 DGtal::ParallelStrip<TSpace, muIncluded, muPlusNuIncluded>&
80 DGtal::ParallelStrip<TSpace, muIncluded, muPlusNuIncluded>::operator=( const ParallelStrip & other )
81 {
82  if ( this != &other )
83  {
84  myMu = other.myMu;
85  myN = other.myN;
86  myNu = other.myNu;
87  }
88  return *this;
89 }
90 //-----------------------------------------------------------------------------
91 template <typename TSpace, bool muIncluded, bool muPlusNuIncluded>
92 inline
93 typename DGtal::ParallelStrip<TSpace, muIncluded, muPlusNuIncluded>::Scalar
94 DGtal::ParallelStrip<TSpace, muIncluded, muPlusNuIncluded>::mu() const
95 {
96  return myMu;
97 }
98 //-----------------------------------------------------------------------------
99 template <typename TSpace, bool muIncluded, bool muPlusNuIncluded>
100 inline
101 const typename DGtal::ParallelStrip<TSpace, muIncluded, muPlusNuIncluded>::RealVector&
102 DGtal::ParallelStrip<TSpace, muIncluded, muPlusNuIncluded>::normal() const
103 {
104  return myN;
105 }
106 //-----------------------------------------------------------------------------
107 template <typename TSpace, bool muIncluded, bool muPlusNuIncluded>
108 inline
109 typename DGtal::ParallelStrip<TSpace, muIncluded, muPlusNuIncluded>::Scalar
110 DGtal::ParallelStrip<TSpace, muIncluded, muPlusNuIncluded>::nu() const
111 {
112  return myNu;
113 }
114 //-----------------------------------------------------------------------------
115 template <typename TSpace, bool muIncluded, bool muPlusNuIncluded>
116 inline
117 typename DGtal::ParallelStrip<TSpace, muIncluded, muPlusNuIncluded>::Scalar
118 DGtal::ParallelStrip<TSpace, muIncluded, muPlusNuIncluded>::width() const
119 {
120  return nu();
121 }
122 //-----------------------------------------------------------------------------
123 template <typename TSpace, bool muIncluded, bool muPlusNuIncluded>
124 inline
125 DGtal::Dimension
126 DGtal::ParallelStrip<TSpace, muIncluded, muPlusNuIncluded>::mainAxis() const
127 {
128  Dimension i = 0;
129  for ( Dimension k = 1; k < Space::dimension; ++k )
130  if ( std::abs( myN[ k ] ) > std::abs( myN[ i ] ) )
131  i = k;
132  return i;
133 }
134 //-----------------------------------------------------------------------------
135 template <typename TSpace, bool muIncluded, bool muPlusNuIncluded>
136 inline
137 typename DGtal::ParallelStrip<TSpace, muIncluded, muPlusNuIncluded>::Scalar
138 DGtal::ParallelStrip<TSpace, muIncluded, muPlusNuIncluded>::axisWidth() const
139 {
140  Dimension i = mainAxis();
141  return std::abs( nu() / myN[ i ] );
142 }
143 //-----------------------------------------------------------------------------
144 template <typename TSpace, bool muIncluded, bool muPlusNuIncluded>
145 inline
146 DGtal::Dimension
147 DGtal::ParallelStrip<TSpace, muIncluded, muPlusNuIncluded>::mainDiagonal() const
148 {
149  Dimension d = 0;
150  Dimension i = 1;
151  for ( Dimension k = 0; k < Space::dimension; ++k )
152  {
153  if ( myN[ k ] < NumberTraits<Scalar>::ZERO )
154  d += i;
155  i <<= 1;
156  }
157  return d;
158 }
159 //-----------------------------------------------------------------------------
160 template <typename TSpace, bool muIncluded, bool muPlusNuIncluded>
161 inline
162 typename DGtal::ParallelStrip<TSpace, muIncluded, muPlusNuIncluded>::Scalar
163 DGtal::ParallelStrip<TSpace, muIncluded, muPlusNuIncluded>::diagonalWidth() const
164 {
165  return nu() * sqrt( Space::dimension ) / myN.norm1();
166 }
167 //-----------------------------------------------------------------------------
168 template <typename TSpace, bool muIncluded, bool muPlusNuIncluded>
169 inline
170 void
171 DGtal::ParallelStrip<TSpace, muIncluded, muPlusNuIncluded>::getBounds( Scalar & aMu, Scalar & mu_plus_nu ) const
172 {
173  aMu = myMu;
174  mu_plus_nu = myMu + myNu;
175 }
176 //-----------------------------------------------------------------------------
177 template <typename TSpace, bool muIncluded, bool muPlusNuIncluded>
178 inline
179 bool
180 DGtal::ParallelStrip<TSpace, muIncluded, muPlusNuIncluded>::operator()( const Point & p ) const
181 {
182  Scalar s = NumberTraits<Scalar>::ZERO;
183  for ( Dimension i = 0; i < Space::dimension; ++i )
184  s += myN[ i ] * NumberTraits<typename Point::Coordinate>::castToDouble( p[ i ] );
185  return ( muIncluded ? ( mu() <= s ) : ( mu() < s ) )
186  && ( muPlusNuIncluded ? ( s <= mu() + nu() ) : ( s < mu() + nu() ) );
187 }
188 //-----------------------------------------------------------------------------
189 template <typename TSpace, bool muIncluded, bool muPlusNuIncluded>
190 inline
191 bool
192 DGtal::ParallelStrip<TSpace, muIncluded, muPlusNuIncluded>::operator()( const RealPoint & p ) const
193 {
194  Scalar s = myN.dot( p );
195  return ( muIncluded ? ( mu() <= s ) : ( mu() < s ) )
196  && ( muPlusNuIncluded ? ( s <= mu() + nu() ) : ( s < mu() + nu() ) );
197 }
198 
199 
200 
201 ///////////////////////////////////////////////////////////////////////////////
202 // Interface - public :
203 
204 /**
205  * Writes/Displays the object on an output stream.
206  * @param out the output stream where the object is written.
207  */
208 template <typename TSpace, bool muIncluded, bool muPlusNuIncluded>
209 inline
210 void
211 DGtal::ParallelStrip<TSpace, muIncluded, muPlusNuIncluded>::selfDisplay ( std::ostream & out ) const
212 {
213  out << "[ParallelStrip " << mu()
214  << ( muIncluded ? " <= " : " < " )
215  << "( " << myN[ 0 ];
216  for ( Dimension i = 1; i < Space::dimension; ++i )
217  out << ", " << myN[ i ];
218  out << " ).X"
219  << ( muPlusNuIncluded ? " <= " : " < " )
220  << (mu()+nu()) << "]";
221 }
222 
223 /**
224  * Checks the validity/consistency of the object.
225  * @return 'true' if the object is valid, 'false' otherwise.
226  */
227 template <typename TSpace, bool muIncluded, bool muPlusNuIncluded>
228 inline
229 bool
230 DGtal::ParallelStrip<TSpace, muIncluded, muPlusNuIncluded>::isValid() const
231 {
232  return ( nu() >= NumberTraits<Scalar>::ZERO )
233  && ( normal().norm1() > NumberTraits<Scalar>::ZERO );
234 }
235 
236 
237 
238 ///////////////////////////////////////////////////////////////////////////////
239 // Implementation of inline functions //
240 
241 template <typename TSpace, bool muIncluded, bool muPlusNuIncluded>
242 inline
243 std::ostream&
244 DGtal::operator<< ( std::ostream & out,
245  const ParallelStrip<TSpace, muIncluded, muPlusNuIncluded> & object )
246 {
247  object.selfDisplay( out );
248  return out;
249 }
250 
251 // //
252 ///////////////////////////////////////////////////////////////////////////////