DGtal  1.5.beta
ImplicitFunctionDiff1LinearCellEmbedder.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 ImplicitFunctionDiff1LinearCellEmbedder.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 ImplicitFunctionDiff1LinearCellEmbedder.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 TKSpace, typename TImplicitFunctionDiff1, typename TEmbedder >
43 inline
44 DGtal::ImplicitFunctionDiff1LinearCellEmbedder<TKSpace, TImplicitFunctionDiff1, TEmbedder>::
45 ~ImplicitFunctionDiff1LinearCellEmbedder()
46 {}
47 //-----------------------------------------------------------------------------
48 template < typename TKSpace, typename TImplicitFunctionDiff1, typename TEmbedder >
49 inline
50 DGtal::ImplicitFunctionDiff1LinearCellEmbedder<TKSpace, TImplicitFunctionDiff1, TEmbedder>::
51 ImplicitFunctionDiff1LinearCellEmbedder()
52  : myPtrK( 0 ), myPtrFct( 0 ), myPtrEmbedder( 0 )
53 {}
54 //-----------------------------------------------------------------------------
55 template < typename TKSpace, typename TImplicitFunctionDiff1, typename TEmbedder >
56 inline
57 DGtal::ImplicitFunctionDiff1LinearCellEmbedder<TKSpace, TImplicitFunctionDiff1, TEmbedder>::
58 ImplicitFunctionDiff1LinearCellEmbedder( const ImplicitFunctionDiff1LinearCellEmbedder & other )
59  : myPtrK( other.myPtrK ),
60  myPtrFct( other.myPtrFct ),
61  myPtrEmbedder( other.myPtrEmbedder )
62 {}
63 //-----------------------------------------------------------------------------
64 template < typename TKSpace, typename TImplicitFunctionDiff1, typename TEmbedder >
65 inline
66 DGtal::ImplicitFunctionDiff1LinearCellEmbedder<TKSpace, TImplicitFunctionDiff1, TEmbedder> &
67 DGtal::ImplicitFunctionDiff1LinearCellEmbedder<TKSpace, TImplicitFunctionDiff1, TEmbedder>::
68 operator=( const ImplicitFunctionDiff1LinearCellEmbedder & other )
69 {
70  if ( this != &other )
71  {
72  myPtrK = other.myPtrK;
73  myPtrFct = other.myPtrFct;
74  myPtrEmbedder = other.myPtrEmbedder;
75  }
76  return *this;
77 }
78 //-----------------------------------------------------------------------------
79 template < typename TKSpace, typename TImplicitFunctionDiff1, typename TEmbedder >
80 inline
81 void
82 DGtal::ImplicitFunctionDiff1LinearCellEmbedder<TKSpace, TImplicitFunctionDiff1, TEmbedder>::
83 init( ConstAlias<KSpace> K, ConstAlias<ImplicitFunctionDiff1> f, ConstAlias<Embedder> e )
84 {
85  myPtrK = &K;
86  myPtrFct = &f;
87  myPtrEmbedder = &e;
88 }
89 //-----------------------------------------------------------------------------
90 template < typename TKSpace, typename TImplicitFunctionDiff1, typename TEmbedder >
91 inline
92 typename DGtal::ImplicitFunctionDiff1LinearCellEmbedder<TKSpace, TImplicitFunctionDiff1, TEmbedder>::GradientMap
93 DGtal::ImplicitFunctionDiff1LinearCellEmbedder<TKSpace, TImplicitFunctionDiff1, TEmbedder>::
94 gradientMap() const
95 {
96  return GradientMap( *this );
97 }
98 //-----------------------------------------------------------------------------
99 template < typename TKSpace, typename TImplicitFunctionDiff1, typename TEmbedder >
100 inline
101 typename DGtal::ImplicitFunctionDiff1LinearCellEmbedder<TKSpace, TImplicitFunctionDiff1, TEmbedder>::RealPoint
102 DGtal::ImplicitFunctionDiff1LinearCellEmbedder<TKSpace, TImplicitFunctionDiff1, TEmbedder>::
103 embed( const Point & p ) const
104 {
105  ASSERT( myPtrEmbedder != 0 );
106  return myPtrEmbedder->embed( p );
107 }
108 //-----------------------------------------------------------------------------
109 template < typename TKSpace, typename TImplicitFunctionDiff1, typename TEmbedder >
110 inline
111 typename DGtal::ImplicitFunctionDiff1LinearCellEmbedder<TKSpace, TImplicitFunctionDiff1, TEmbedder>::RealPoint
112 DGtal::ImplicitFunctionDiff1LinearCellEmbedder<TKSpace, TImplicitFunctionDiff1, TEmbedder>::
113 embedCell( const Cell & cell ) const
114 {
115  return this->operator()( cell );
116 }
117 //-----------------------------------------------------------------------------
118 template < typename TKSpace, typename TImplicitFunctionDiff1, typename TEmbedder >
119 inline
120 typename DGtal::ImplicitFunctionDiff1LinearCellEmbedder<TKSpace, TImplicitFunctionDiff1, TEmbedder>::RealPoint
121 DGtal::ImplicitFunctionDiff1LinearCellEmbedder<TKSpace, TImplicitFunctionDiff1, TEmbedder>::
122 operator()( const Cell & cell ) const
123 {
124  ASSERT( this->isValid() );
125  // embed first the spel related to the cell.
126  Point p1( myPtrK->uCoords( cell ) );
127  RealPoint x1( embed( p1 ) );
128  ImplicitFctValue y1 = (*myPtrFct)( x1 );
129  for ( typename KSpace::DirIterator qit = myPtrK->uOrthDirs( cell );
130  qit != 0; ++qit )
131  { // cell is closed along this dimension.
132  // estimate coordinate by interpolation, looking for f(...)=0.
133  Dimension k = *qit;
134  Point p2( p1 ); --p2[ k ];
135  RealPoint x2( embed( p2 ) );
136  ImplicitFctValue y2 = (*myPtrFct)( x2 );
137  x1[ k ] -= y1 * ( x2[ k ] - x1[ k ] ) / ( y2 - y1 );
138  }
139  return x1;
140 }
141 //-----------------------------------------------------------------------------
142 template < typename TKSpace, typename TImplicitFunctionDiff1, typename TEmbedder >
143 inline
144 typename DGtal::ImplicitFunctionDiff1LinearCellEmbedder<TKSpace, TImplicitFunctionDiff1, TEmbedder>::RealPoint
145 DGtal::ImplicitFunctionDiff1LinearCellEmbedder<TKSpace, TImplicitFunctionDiff1, TEmbedder>::
146 embedSCell( const SCell & scell ) const
147 {
148  ASSERT( this->isValid() );
149  // embed first the spel related to the cell.
150  Point p1( myPtrK->sCoords( scell ) );
151  RealPoint x1( embed( p1 ) );
152  ImplicitFctValue y1 = (*myPtrFct)( x1 );
153  for ( typename KSpace::DirIterator qit = myPtrK->sOrthDirs( scell );
154  qit != 0; ++qit )
155  { // cell is closed along this dimension.
156  // estimate coordinate by interpolation, looking for f(...)=0.
157  Dimension k = *qit;
158  Point p2( p1 ); --p2[ k ];
159  RealPoint x2( embed( p2 ) );
160  ImplicitFctValue y2 = (*myPtrFct)( x2 );
161  x1[ k ] -= y1 * ( x2[ k ] - x1[ k ] ) / ( y2 - y1 );
162  }
163  return x1;
164 }
165 //-----------------------------------------------------------------------------
166 template < typename TKSpace, typename TImplicitFunctionDiff1, typename TEmbedder >
167 inline
168 void
169 DGtal::ImplicitFunctionDiff1LinearCellEmbedder<TKSpace, TImplicitFunctionDiff1, TEmbedder>::
170 embed( const Point & p, RealPoint & x, RealVector & grad ) const
171 {
172  x = embed( p );
173  grad = myPtrFct->gradient( x );
174 }
175 //-----------------------------------------------------------------------------
176 template < typename TKSpace, typename TImplicitFunctionDiff1, typename TEmbedder >
177 inline
178 void
179 DGtal::ImplicitFunctionDiff1LinearCellEmbedder<TKSpace, TImplicitFunctionDiff1, TEmbedder>::
180 embedCell( const Cell & cell, RealPoint & x, RealVector & grad ) const
181 {
182  x = embedCell( cell );
183  grad = myPtrFct->gradient( x );
184 }
185 //-----------------------------------------------------------------------------
186 template < typename TKSpace, typename TImplicitFunctionDiff1, typename TEmbedder >
187 inline
188 void
189 DGtal::ImplicitFunctionDiff1LinearCellEmbedder<TKSpace, TImplicitFunctionDiff1, TEmbedder>::
190 embedSCell( const SCell & scell, RealPoint & x, RealVector & grad ) const
191 {
192  x = embedSCell( scell );
193  grad = myPtrFct->gradient( x );
194 }
195 
196 
197 
198 ///////////////////////////////////////////////////////////////////////////////
199 // Interface - public :
200 
201 /**
202  * Writes/Displays the object on an output stream.
203  * @param out the output stream where the object is written.
204  */
205 template < typename TKSpace, typename TImplicitFunctionDiff1, typename TEmbedder >
206 inline
207 void
208 DGtal::ImplicitFunctionDiff1LinearCellEmbedder<TKSpace, TImplicitFunctionDiff1, TEmbedder>::
209 selfDisplay ( std::ostream & out ) const
210 {
211  out << "[ImplicitFunctionDiff1LinearCellEmbedder]";
212 }
213 
214 /**
215  * Checks the validity/consistency of the object.
216  * @return 'true' if the object is valid, 'false' otherwise.
217  */
218 template < typename TKSpace, typename TImplicitFunctionDiff1, typename TEmbedder >
219 inline
220 bool
221 DGtal::ImplicitFunctionDiff1LinearCellEmbedder<TKSpace, TImplicitFunctionDiff1, TEmbedder>::
222 isValid() const
223 {
224  return ( myPtrK != 0 ) && ( myPtrFct != 0 ) && ( myPtrEmbedder != 0 );
225 }
226 
227 
228 
229 ///////////////////////////////////////////////////////////////////////////////
230 // Implementation of inline functions //
231 
232 template < typename TKSpace, typename TImplicitFunctionDiff1, typename TEmbedder >
233 inline
234 std::ostream&
235 DGtal::operator<< ( std::ostream & out,
236  const ImplicitFunctionDiff1LinearCellEmbedder<TKSpace, TImplicitFunctionDiff1, TEmbedder> & object )
237 {
238  object.selfDisplay( out );
239  return out;
240 }
241 
242 // //
243 ///////////////////////////////////////////////////////////////////////////////
244 
245