DGtal  0.9.2
ImplicitPolynomial3Shape.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 ImplicitPolynomial3Shape.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 2012/02/14
23  *
24  * Implementation of inline methods defined in ImplicitPolynomial3Shape.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::ImplicitPolynomial3Shape<TSpace>::~ImplicitPolynomial3Shape()
45 {
46 }
47 //-----------------------------------------------------------------------------
48 template <typename TSpace>
49 inline
50 DGtal::ImplicitPolynomial3Shape<TSpace>::
51 ImplicitPolynomial3Shape( const Polynomial3 & poly )
52 {
53  init( poly );
54 }
55 //-----------------------------------------------------------------------------
56 template <typename TSpace>
57 inline
58 DGtal::ImplicitPolynomial3Shape<TSpace> &
59 DGtal::ImplicitPolynomial3Shape<TSpace>::
60 operator=( const ImplicitPolynomial3Shape & other )
61 {
62  if ( this != &other )
63  {
64  myPolynomial = other.myPolynomial;
65 
66  myFx= other.myFx;
67  myFy= other.myFy;
68  myFz= other.myFz;
69 
70  myFxx= other.myFxx;
71  myFxy= other.myFxy;
72  myFxz= other.myFxz;
73 
74  myFyx= other.myFyx;
75  myFyy= other.myFyy;
76  myFyz= other.myFyz;
77 
78  myFzx= other.myFzx;
79  myFzy= other.myFzy;
80  myFzz= other.myFzz;
81 
82  myUpPolynome = other.myUpPolynome;
83  myLowPolynome = other.myLowPolynome;
84  }
85  return *this;
86 }
87 //-----------------------------------------------------------------------------
88 template <typename TSpace>
89 inline
90 void
91 DGtal::ImplicitPolynomial3Shape<TSpace>::
92 init( const Polynomial3 & poly )
93 {
94  myPolynomial = poly;
95 
96  myFx= derivative<0>( poly );
97  myFy= derivative<1>( poly );
98  myFz= derivative<2>( poly );
99 
100  myFxx= derivative<0>( myFx );
101  myFxy= derivative<1>( myFx );
102  myFxz= derivative<2>( myFx);
103 
104  myFyx= derivative<0>( myFy );
105  myFyy= derivative<1>( myFy );
106  myFyz= derivative<2>( myFy );
107 
108  myFzx= derivative<0>( myFz );
109  myFzy= derivative<1>( myFz );
110  myFzz= derivative<2>( myFz );
111 
112  myUpPolynome = myFx*(myFx*myFxx+myFy*myFyx+myFz*myFzx)+
113  myFy*(myFx*myFxy+myFy*myFyy+myFz*myFzy)+
114  myFz*(myFx*myFxz+myFy*myFyz+myFz*myFzz)-
115  ( myFx*myFx +myFy*myFy+myFz*myFz )*(myFxx+myFyy+myFzz);
116 
117  myLowPolynome = myFx*myFx +myFy*myFy+myFz*myFz;
118 }
119 //-----------------------------------------------------------------------------
120 template <typename TSpace>
121 inline
122 double
123 DGtal::ImplicitPolynomial3Shape<TSpace>::
124 operator()(const RealPoint &aPoint) const
125 {
126  return myPolynomial( aPoint[ 0 ] )( aPoint[ 1 ] )( aPoint[ 2 ] );
127 }
128 //-----------------------------------------------------------------------------
129 template <typename TSpace>
130 inline
131 bool
132 DGtal::ImplicitPolynomial3Shape<TSpace>::
133 isInside(const RealPoint &aPoint) const
134 {
135  return orientation( aPoint ) == INSIDE;
136 }
137 //-----------------------------------------------------------------------------
138 template <typename TSpace>
139 inline
140 DGtal::Orientation
141 DGtal::ImplicitPolynomial3Shape<TSpace>::
142 orientation(const RealPoint &aPoint) const
143 {
144  Ring v = this->operator()(aPoint);
145  if ( v < (Ring)0 )
146  return INSIDE;
147  else if ( v > (Ring)0 )
148  return OUTSIDE;
149  else
150  return ON;
151 }
152 //-----------------------------------------------------------------------------
153 template <typename TSpace>
154 inline
155 typename DGtal::ImplicitPolynomial3Shape<TSpace>::RealVector
156 DGtal::ImplicitPolynomial3Shape<TSpace>::
157 gradient( const RealPoint &aPoint ) const
158 {
159  // ISO C++ tells that an object created at return time will not be
160  // copied into the caller context, but will be already defined in
161  // the correct context.
162  return RealVector
163  ( myFx ( aPoint[ 0 ] )( aPoint[ 1 ] )( aPoint[ 2 ] ),
164  myFy ( aPoint[ 0 ] )( aPoint[ 1 ] )( aPoint[ 2 ] ),
165  myFz ( aPoint[ 0 ] )( aPoint[ 1 ] )( aPoint[ 2 ] ) );
166 
167 }
168 
169 
170 // ------------------------------------------------------------ Added by Anis Benyoub
171 //-----------------------------------------------------------------------------
172 
173 /**
174  * @param aPoint any point in the Euclidean space.
175  * This computation is based on the hessian formula of the mean curvature
176  * k=-(∇F ∗ H (F ) ∗ ∇F T − |∇F |^2 *Trace(H (F ))/2|∇F |^3
177  * we define it as positive for a sphere
178  * @return the mean curvature value of the polynomial at \a aPoint.
179  *
180 */
181 template <typename TSpace>
182 inline
183 double
184 DGtal::ImplicitPolynomial3Shape<TSpace>::
185 meanCurvature( const RealPoint &aPoint ) const
186 {
187  double temp= myLowPolynome( aPoint[ 0 ] )( aPoint[ 1 ] )( aPoint[ 2 ] );
188  temp = sqrt(temp);
189  double downValue = 2.0*(temp*temp*temp);
190  double upValue = myUpPolynome( aPoint[ 0 ] )( aPoint[ 1 ] )( aPoint[ 2 ] );
191 
192 
193  return -(upValue/downValue);
194 }
195 
196 
197 
198 //-----------------------------------------------------------------------------
199 template <typename TSpace>
200 inline
201 double
202 DGtal::ImplicitPolynomial3Shape<TSpace>::
203 gaussianCurvature( const RealPoint &aPoint ) const
204 {
205 
206  double vFx= myFx( aPoint[ 0 ] )( aPoint[ 1 ] )( aPoint[ 2 ] );
207  double vFy= myFy( aPoint[ 0 ] )( aPoint[ 1 ] )( aPoint[ 2 ] );
208  double vFz= myFz( aPoint[ 0 ] )( aPoint[ 1 ] )( aPoint[ 2 ] );
209 
210  double vFxx= myFxx( aPoint[ 0 ] )( aPoint[ 1 ] )( aPoint[ 2 ] );
211  double vFxy= myFxy( aPoint[ 0 ] )( aPoint[ 1 ] )( aPoint[ 2 ] );
212  double vFxz= myFxz( aPoint[ 0 ] )( aPoint[ 1 ] )( aPoint[ 2 ] );
213 
214  //double vFyx= myFyx( aPoint[ 0 ] )( aPoint[ 1 ] )( aPoint[ 2 ] );
215  double vFyy= myFyy( aPoint[ 0 ] )( aPoint[ 1 ] )( aPoint[ 2 ] );
216  double vFyz= myFyz( aPoint[ 0 ] )( aPoint[ 1 ] )( aPoint[ 2 ] );
217 
218 
219  /*double vFzx = myFzx( aPoint[ 0 ] )( aPoint[ 1 ] )( aPoint[ 2 ] );
220  double vFzy = myFzy( aPoint[ 0 ] )( aPoint[ 1 ] )( aPoint[ 2 ] );
221  */
222  double vFzz = myFzz( aPoint[ 0 ] )( aPoint[ 1 ] )( aPoint[ 2 ] );
223 
224 
225  double A = vFz*(vFxx*vFz-2.0*vFx*vFxz)+vFx*vFx*vFzz;
226 
227  double B = vFz*(vFyy*vFz-2.0*vFy*vFyz)+vFy*vFy*vFzz;
228 
229  double C = vFz*(-vFx*vFyz+vFxy*vFz-vFxz*vFy)+vFx*vFy*vFzz;
230 
231  double D = vFz*(vFx*vFx+vFy*vFy+vFz*vFz);
232 
233  //ASSERT ( D != 0.0 );
234 
235  double G= (A*B-C*C)/(D*D);
236 
237  return G;
238 
239 }
240 
241 template< typename TSpace >
242 inline
243 void
244 DGtal::ImplicitPolynomial3Shape<TSpace>::principalCurvatures
245 ( const RealPoint & aPoint,
246  double & k1,
247  double & k2 ) const
248 {
249  double H = meanCurvature( aPoint );
250  double G = gaussianCurvature( aPoint );
251  double tmp = std::sqrt( fabs( H * H - G ));
252  k2 = H + tmp;
253  k1 = H - tmp;
254 }
255 
256 
257 /**
258  *@param aPoint any point in the Euclidean space.
259  *@param accuracy refers to the precision
260  *@param maxIter refers to the maximum iterations the fonction user authorises
261  *@param gamma refers to the step
262  * This function is very useful for mean and gaussian curvature computation (If the set step is big) .For a small one ( <0.5) it's less
263  * usefull
264  *@return the nearest point on the surface to the one given in parameter.
265  */
266 template <typename TSpace>
267 inline
268 typename DGtal::ImplicitPolynomial3Shape<TSpace>::RealPoint
269 DGtal::ImplicitPolynomial3Shape<TSpace>::nearestPoint( const RealPoint &aPoint, const double accuracy,
270  const int maxIter, const double gamma ) const
271 {
272  RealPoint agradient= (*this).gradient(aPoint);
273  RealPoint X=aPoint;
274  int numberIter=0;
275  while((fabs((*this)(X))>=accuracy) && (numberIter<maxIter))
276  {
277  double norm=agradient.norm();
278  RealPoint normalizedGradient= RealPoint(agradient[0]/norm,agradient[1]/norm,agradient[2]/norm);
279  double alpha =gamma;
280  if((*this)(X)>0)
281  {
282  alpha=-alpha;
283  }
284  else
285  {
286 
287  }
288 
289  X=X+normalizedGradient*alpha;
290  agradient= (*this).gradient(X);
291  numberIter++;
292  }
293  return X;
294 }
295 
296 ///////////////////////////////////////////////////////////////////////////////
297 // Interface - public :
298 
299 /**
300  * Writes/Displays the object on an output stream.
301  * @param out the output stream where the object is written.
302  */
303 template <typename TSpace>
304 inline
305 void
306 DGtal::ImplicitPolynomial3Shape<TSpace>::selfDisplay ( std::ostream & out ) const
307 {
308  out << "[ImplicitPolynomial3Shape] P(x,y,z) = " << myPolynomial;
309 }
310 
311 /**
312  * Checks the validity/consistency of the object.
313  * @return 'true' if the object is valid, 'false' otherwise.
314  */
315 template <typename TSpace>
316 inline
317 bool
318 DGtal::ImplicitPolynomial3Shape<TSpace>::isValid() const
319 {
320  return true;
321 }
322 
323 
324 
325 ///////////////////////////////////////////////////////////////////////////////
326 // Implementation of inline functions //
327 
328 template <typename TSpace>
329 inline
330 std::ostream&
331 DGtal::operator<< ( std::ostream & out,
332  const ImplicitPolynomial3Shape<TSpace> & object )
333 {
334  object.selfDisplay( out );
335  return out;
336 }
337 
338 // //
339 ///////////////////////////////////////////////////////////////////////////////
340 
341