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