DGtal  0.6.devel
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
ImplicitFunctionDiff1LinearCellEmbedder.ih
1 
30 
31 #include <cstdlib>
33 
35 // IMPLEMENTATION of inline methods.
37 
39 // ----------------------- Standard services ------------------------------
40 
41 //-----------------------------------------------------------------------------
42 template < typename TKSpace, typename TImplicitFunctionDiff1, typename TEmbedder >
43 inline
46 {}
47 //-----------------------------------------------------------------------------
48 template < typename TKSpace, typename TImplicitFunctionDiff1, typename TEmbedder >
49 inline
52  : myPtrK( 0 ), myPtrFct( 0 ), myPtrEmbedder( 0 )
53 {}
54 //-----------------------------------------------------------------------------
55 template < typename TKSpace, typename TImplicitFunctionDiff1, typename TEmbedder >
56 inline
59  : myPtrK( other.myPtrK ),
60  myPtrFct( other.myPtrFct ),
61  myPtrEmbedder( other.myPtrEmbedder )
62 {}
63 //-----------------------------------------------------------------------------
64 template < typename TKSpace, typename TImplicitFunctionDiff1, typename TEmbedder >
65 inline
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
83 init( const KSpace & K, const ImplicitFunctionDiff1 & f, const Embedder & e )
84 {
85  myPtrK = &K;
86  myPtrFct = &f;
87  myPtrEmbedder = &e;
88 }
89 //-----------------------------------------------------------------------------
90 template < typename TKSpace, typename TImplicitFunctionDiff1, typename TEmbedder >
91 inline
94 gradientMap() const
95 {
96  return GradientMap( *this );
97 }
98 //-----------------------------------------------------------------------------
99 template < typename TKSpace, typename TImplicitFunctionDiff1, typename TEmbedder >
100 inline
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
113 embedCell( const Cell & cell ) const
114 {
115  return this->operator()( cell );
116 }
117 //-----------------------------------------------------------------------------
118 template < typename TKSpace, typename TImplicitFunctionDiff1, typename TEmbedder >
119 inline
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
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
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
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
190 embedSCell( const SCell & scell, RealPoint & x, RealVector & grad ) const
191 {
192  x = embedSCell( scell );
193  grad = myPtrFct->gradient( x );
194 }
195 
196 
197 
199 // Interface - public :
200 
205 template < typename TKSpace, typename TImplicitFunctionDiff1, typename TEmbedder >
206 inline
207 void
209 selfDisplay ( std::ostream & out ) const
210 {
211  out << "[ImplicitFunctionDiff1LinearCellEmbedder]";
212 }
213 
218 template < typename TKSpace, typename TImplicitFunctionDiff1, typename TEmbedder >
219 inline
220 bool
222 isValid() const
223 {
224  return ( myPtrK != 0 ) && ( myPtrFct != 0 ) && ( myPtrEmbedder != 0 );
225 }
226 
227 
228 
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,
237 {
238  object.selfDisplay( out );
239  return out;
240 }
241 
242 // //
244 
245