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.
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.
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/>.
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
24 * Implementation of inline methods defined in ImplicitPolynomial3Shape.h
26 * This file is part of the DGtal library.
30 //////////////////////////////////////////////////////////////////////////////
32 //////////////////////////////////////////////////////////////////////////////
34 ///////////////////////////////////////////////////////////////////////////////
35 // IMPLEMENTATION of inline methods.
36 ///////////////////////////////////////////////////////////////////////////////
38 ///////////////////////////////////////////////////////////////////////////////
39 // ----------------------- Standard services ------------------------------
41 //-----------------------------------------------------------------------------
42 template <typename TSpace>
44 DGtal::ImplicitPolynomial3Shape<TSpace>::~ImplicitPolynomial3Shape()
47 //-----------------------------------------------------------------------------
48 template <typename TSpace>
50 DGtal::ImplicitPolynomial3Shape<TSpace>::
51 ImplicitPolynomial3Shape( const Polynomial3 & poly )
55 //-----------------------------------------------------------------------------
56 template <typename TSpace>
58 DGtal::ImplicitPolynomial3Shape<TSpace> &
59 DGtal::ImplicitPolynomial3Shape<TSpace>::
60 operator=( const ImplicitPolynomial3Shape & other )
64 myPolynomial = other.myPolynomial;
82 myUpPolynome = other.myUpPolynome;
83 myLowPolynome = other.myLowPolynome;
87 //-----------------------------------------------------------------------------
88 template <typename TSpace>
91 DGtal::ImplicitPolynomial3Shape<TSpace>::
92 init( const Polynomial3 & poly )
96 myFx= derivative<0>( poly );
97 myFy= derivative<1>( poly );
98 myFz= derivative<2>( poly );
100 myFxx= derivative<0>( myFx );
101 myFxy= derivative<1>( myFx );
102 myFxz= derivative<2>( myFx);
104 myFyx= derivative<0>( myFy );
105 myFyy= derivative<1>( myFy );
106 myFyz= derivative<2>( myFy );
108 myFzx= derivative<0>( myFz );
109 myFzy= derivative<1>( myFz );
110 myFzz= derivative<2>( myFz );
112 // These two polynomials are used for mean curvature estimation.
113 myUpPolynome = myFx*(myFx*myFxx+myFy*myFyx+myFz*myFzx)+
114 myFy*(myFx*myFxy+myFy*myFyy+myFz*myFzy)+
115 myFz*(myFx*myFxz+myFy*myFyz+myFz*myFzz)-
116 ( myFx*myFx +myFy*myFy+myFz*myFz )*(myFxx+myFyy+myFzz);
118 myLowPolynome = myFx*myFx +myFy*myFy+myFz*myFz;
120 //-----------------------------------------------------------------------------
121 template <typename TSpace>
124 DGtal::ImplicitPolynomial3Shape<TSpace>::
125 operator()(const RealPoint &aPoint) const
127 return myPolynomial( aPoint[ 0 ] )( aPoint[ 1 ] )( aPoint[ 2 ] );
129 //-----------------------------------------------------------------------------
130 template <typename TSpace>
133 DGtal::ImplicitPolynomial3Shape<TSpace>::
134 isInside(const RealPoint &aPoint) const
136 return orientation( aPoint ) == INSIDE;
138 //-----------------------------------------------------------------------------
139 template <typename TSpace>
142 DGtal::ImplicitPolynomial3Shape<TSpace>::
143 orientation(const RealPoint &aPoint) const
145 Ring v = this->operator()(aPoint);
148 else if ( v > (Ring)0 )
153 //-----------------------------------------------------------------------------
154 template <typename TSpace>
156 typename DGtal::ImplicitPolynomial3Shape<TSpace>::RealVector
157 DGtal::ImplicitPolynomial3Shape<TSpace>::
158 gradient( const RealPoint &aPoint ) const
160 // ISO C++ tells that an object created at return time will not be
161 // copied into the caller context, but will be already defined in
162 // the correct context.
164 ( myFx ( aPoint[ 0 ] )( aPoint[ 1 ] )( aPoint[ 2 ] ),
165 myFy ( aPoint[ 0 ] )( aPoint[ 1 ] )( aPoint[ 2 ] ),
166 myFz ( aPoint[ 0 ] )( aPoint[ 1 ] )( aPoint[ 2 ] ) );
171 // ------------------------------------------------------------ Added by Anis Benyoub
172 //-----------------------------------------------------------------------------
175 * @param aPoint any point in the Euclidean space.
176 * This computation is based on the hessian formula of the mean curvature
177 * k=-(∇F ∗ H (F ) ∗ ∇F T − |∇F |^2 *Trace(H (F ))/2|∇F |^3
178 * we define it as positive for a sphere
179 * @return the mean curvature value of the polynomial at \a aPoint.
182 template <typename TSpace>
185 DGtal::ImplicitPolynomial3Shape<TSpace>::
186 meanCurvature( const RealPoint &aPoint ) const
188 double temp= myLowPolynome( aPoint[ 0 ] )( aPoint[ 1 ] )( aPoint[ 2 ] );
190 double downValue = 2.0*(temp*temp*temp);
191 double upValue = myUpPolynome( aPoint[ 0 ] )( aPoint[ 1 ] )( aPoint[ 2 ] );
194 return -(upValue/downValue);
199 //-----------------------------------------------------------------------------
200 template <typename TSpace>
203 DGtal::ImplicitPolynomial3Shape<TSpace>::
204 gaussianCurvature( const RealPoint &aPoint ) const
207 JOL: new Gaussian curvature formula (in sage)
208 var('Fx','Fy','Fz','Fxx','Fxy','Fxz','Fyy','Fyz','Fzz')
209 M=Matrix(4,4,[[Fxx,Fxy,Fxz,Fx],[Fxy,Fyy,Fyz,Fy],[Fxz,Fyz,Fzz,Fz],[Fx,Fy,Fz,0]])
211 # Fxz^2*Fy^2 - 2*Fx*Fxz*Fy*Fyz + Fx^2*Fyz^2 - 2*Fxy*Fxz*Fy*Fz + 2*Fx*Fxz*Fyy*Fz - 2*Fx*Fxy*Fyz*Fz + 2*Fxx*Fy*Fyz*Fz + Fxy^2*Fz^2 - Fxx*Fyy*Fz^2 + 2*Fx*Fxy*Fy*Fzz - Fxx*Fy^2*Fzz - Fx^2*Fyy*Fzz
212 G = -det(M) / ( Fx^2 + Fy^2 + Fz^2 )^2
214 const double x = aPoint[ 0 ];
215 const double y = aPoint[ 1 ];
216 const double z = aPoint[ 2 ];
217 const double Fx = myFx( x )( y )( z );
218 const double Fy = myFy( x )( y )( z );
219 const double Fz = myFz( x )( y )( z );
220 const double Fx2 = Fx * Fx;
221 const double Fy2 = Fy * Fy;
222 const double Fz2 = Fz * Fz;
223 const double G2 = Fx2 + Fy2 + Fz2;
224 const double Fxx = myFxx( x )( y )( z );
225 const double Fxy = myFxy( x )( y )( z );
226 const double Fxz = myFxz( x )( y )( z );
227 const double Fyy = myFyy( x )( y )( z );
228 const double Fyz = myFyz( x )( y )( z );
229 const double Fzz = myFzz( x )( y )( z );
230 const double Ax2 = ( Fyz * Fyz - Fyy * Fzz ) * Fx2;
231 const double Ay2 = ( Fxz * Fxz - Fxx * Fzz ) * Fy2;
232 const double Az2 = ( Fxy * Fxy - Fxx * Fyy ) * Fz2;
233 const double Axy = ( Fxy * Fzz - Fxz * Fyz ) * Fx * Fy;
234 const double Axz = ( Fxz * Fyy - Fxy * Fyz ) * Fx * Fz;
235 const double Ayz = ( Fxx * Fyz - Fxy * Fxz ) * Fy * Fz;
236 const double det = Ax2 + Ay2 + Az2 + 2 * ( Axy + Axz + Ayz );
237 return - det / ( G2*G2 );
240 template< typename TSpace >
243 DGtal::ImplicitPolynomial3Shape<TSpace>::principalCurvatures
244 ( const RealPoint & aPoint,
248 double H = meanCurvature( aPoint );
249 double G = gaussianCurvature( aPoint );
250 double tmp = std::sqrt( fabs( H * H - G ));
257 *@param aPoint any point in the Euclidean space.
258 *@param accuracy refers to the precision
259 *@param maxIter refers to the maximum iterations the fonction user authorises
260 *@param gamma refers to the step
261 *@return the nearest point on the surface to the one given in parameter.
263 template <typename TSpace>
265 typename DGtal::ImplicitPolynomial3Shape<TSpace>::RealPoint
266 DGtal::ImplicitPolynomial3Shape<TSpace>::nearestPoint
267 ( const RealPoint &aPoint, const double accuracy,
268 const int maxIter, const double gamma ) const
270 RealPoint X = aPoint;
271 for ( int numberIter = 0; numberIter < maxIter; numberIter++ )
273 double val_X = (*this)( X );
274 if ( fabs( val_X ) < accuracy ) break;
275 RealVector grad_X = (*this).gradient( X );
276 double n2_grad_X = grad_X.dot( grad_X );
277 if ( n2_grad_X > 0.000001 ) grad_X /= n2_grad_X;
278 X -= val_X * gamma * grad_X ;
283 ///////////////////////////////////////////////////////////////////////////////
284 // Interface - public :
287 * Writes/Displays the object on an output stream.
288 * @param out the output stream where the object is written.
290 template <typename TSpace>
293 DGtal::ImplicitPolynomial3Shape<TSpace>::selfDisplay ( std::ostream & out ) const
295 out << "[ImplicitPolynomial3Shape] P(x,y,z) = " << myPolynomial;
299 * Checks the validity/consistency of the object.
300 * @return 'true' if the object is valid, 'false' otherwise.
302 template <typename TSpace>
305 DGtal::ImplicitPolynomial3Shape<TSpace>::isValid() const
312 ///////////////////////////////////////////////////////////////////////////////
313 // Implementation of inline functions //
315 template <typename TSpace>
318 DGtal::operator<< ( std::ostream & out,
319 const ImplicitPolynomial3Shape<TSpace> & object )
321 object.selfDisplay( out );
326 ///////////////////////////////////////////////////////////////////////////////