DGtal  1.5.beta
ImageLinearCellEmbedder.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 ImageLinearCellEmbedder.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 ImageLinearCellEmbedder.h
25  *
26  * This file is part of the DGtal library.
27  */
28 
29 
30 //////////////////////////////////////////////////////////////////////////////
31 #include <cstdlib>
32 
33 //self include
34 #include "DGtal/images/ImageLinearCellEmbedder.h"
35 
36 
37 ///////////////////////////////////////////////////////////////////////////////
38 // IMPLEMENTATION of inline methods.
39 ///////////////////////////////////////////////////////////////////////////////
40 
41 ///////////////////////////////////////////////////////////////////////////////
42 // ----------------------- Standard services ------------------------------
43 
44 //-----------------------------------------------------------------------------
45 template < typename TKSpace, typename TImage, typename TEmbedder >
46 inline
47 DGtal::ImageLinearCellEmbedder<TKSpace, TImage, TEmbedder>::
48 ~ImageLinearCellEmbedder()
49 {}
50 //-----------------------------------------------------------------------------
51 template < typename TKSpace, typename TImage, typename TEmbedder >
52 inline
53 DGtal::ImageLinearCellEmbedder<TKSpace, TImage, TEmbedder>::
54 ImageLinearCellEmbedder()
55  : myPtrK( 0 ), myPtrImage( 0 ), myPtrEmbedder( 0 )
56 {}
57 //-----------------------------------------------------------------------------
58 template < typename TKSpace, typename TImage, typename TEmbedder >
59 inline
60 DGtal::ImageLinearCellEmbedder<TKSpace, TImage, TEmbedder>::
61 ImageLinearCellEmbedder( const ImageLinearCellEmbedder & other )
62  : myPtrK( other.myPtrK ),
63  myPtrImage( other.myPtrImage ),
64  myPtrEmbedder( other.myPtrEmbedder ),
65  myIsoValue( other.myIsoValue )
66 {}
67 //-----------------------------------------------------------------------------
68 template < typename TKSpace, typename TImage, typename TEmbedder >
69 inline
70 DGtal::ImageLinearCellEmbedder<TKSpace, TImage, TEmbedder> &
71 DGtal::ImageLinearCellEmbedder<TKSpace, TImage, TEmbedder>::
72 operator=( const ImageLinearCellEmbedder & other )
73 {
74  if ( this != &other )
75  {
76  myPtrK = other.myPtrK;
77  myPtrImage = other.myPtrImage;
78  myPtrEmbedder = other.myPtrEmbedder;
79  myIsoValue = other.myIsoValue;
80  }
81  return *this;
82 }
83 //-----------------------------------------------------------------------------
84 template < typename TKSpace, typename TImage, typename TEmbedder >
85 inline
86 void
87 DGtal::ImageLinearCellEmbedder<TKSpace, TImage, TEmbedder>::
88 init( ConstAlias<KSpace> K, ConstAlias<Image> f, ConstAlias<Embedder> e,
89  double iso_value )
90 {
91  myPtrK = &K;
92  myPtrImage = &f;
93  myPtrEmbedder = &e;
94  myIsoValue = iso_value;
95 }
96 //-----------------------------------------------------------------------------
97 template < typename TKSpace, typename TImage, typename TEmbedder >
98 inline
99 typename DGtal::ImageLinearCellEmbedder<TKSpace, TImage, TEmbedder>::RealPoint
100 DGtal::ImageLinearCellEmbedder<TKSpace, TImage, TEmbedder>::
101 embed( const Point & p ) const
102 {
103  ASSERT( myPtrEmbedder != 0 );
104  return myPtrEmbedder->embed( p );
105 }
106 //-----------------------------------------------------------------------------
107 template < typename TKSpace, typename TImage, typename TEmbedder >
108 inline
109 typename DGtal::ImageLinearCellEmbedder<TKSpace, TImage, TEmbedder>::RealPoint
110 DGtal::ImageLinearCellEmbedder<TKSpace, TImage, TEmbedder>::
111 embedCell( const Cell & cell ) const
112 {
113  return this->operator()( cell );
114 }
115 //-----------------------------------------------------------------------------
116 template < typename TKSpace, typename TImage, typename TEmbedder >
117 inline
118 typename DGtal::ImageLinearCellEmbedder<TKSpace, TImage, TEmbedder>::RealPoint
119 DGtal::ImageLinearCellEmbedder<TKSpace, TImage, TEmbedder>::
120 operator()( const Cell & cell ) const
121 {
122  ASSERT( this->isValid() );
123  // embed first the spel related to the cell.
124  Point p1( myPtrK->uCoords( cell ) );
125  RealPoint x1( embed( p1 ) );
126  ImageValue y1 = (*myPtrImage)( p1 );
127  double v1 = NumberTraits<ImageValue>::castToDouble( y1 );
128  for ( typename KSpace::DirIterator qit = myPtrK->uOrthDirs( cell );
129  qit != 0; ++qit )
130  { // cell is closed along this dimension.
131  // estimate coordinate by interpolation, looking for f(...)=0.
132  Dimension k = *qit;
133  Point p2( p1 ); --p2[ k ];
134  RealPoint x2( embed( p2 ) );
135  ImageValue y2 = (*myPtrImage)( p2 );
136  double v2 = NumberTraits<ImageValue>::castToDouble( y2 );
137  x1[ k ] -= ( v1 - myIsoValue ) * ( x2[ k ] - x1[ k ] ) / ( v2 - v1 );
138  }
139  return x1;
140 }
141 //-----------------------------------------------------------------------------
142 template < typename TKSpace, typename TImage, typename TEmbedder >
143 inline
144 typename DGtal::ImageLinearCellEmbedder<TKSpace, TImage, TEmbedder>::RealPoint
145 DGtal::ImageLinearCellEmbedder<TKSpace, TImage, 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  ImageValue y1 = (*myPtrImage)( p1 );
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  ImageValue y2 = (*myPtrImage)( p2 );
161  x1[ k ] -= ( NumberTraits<ImageValue>::castToDouble( y1 ) - myIsoValue )
162  * ( x2[ k ] - x1[ k ] )
163  / NumberTraits<ImageValue>::castToDouble( y2 - y1 );
164  }
165  return x1;
166 }
167 
168 
169 ///////////////////////////////////////////////////////////////////////////////
170 // Interface - public :
171 
172 /**
173  * Writes/Displays the object on an output stream.
174  * @param out the output stream where the object is written.
175  */
176 template < typename TKSpace, typename TImage, typename TEmbedder >
177 inline
178 void
179 DGtal::ImageLinearCellEmbedder<TKSpace, TImage, TEmbedder>::
180 selfDisplay ( std::ostream & out ) const
181 {
182  out << "[ImageLinearCellEmbedder]";
183 }
184 
185 /**
186  * Checks the validity/consistency of the object.
187  * @return 'true' if the object is valid, 'false' otherwise.
188  */
189 template < typename TKSpace, typename TImage, typename TEmbedder >
190 inline
191 bool
192 DGtal::ImageLinearCellEmbedder<TKSpace, TImage, TEmbedder>::
193 isValid() const
194 {
195  return ( myPtrK != 0 ) && ( myPtrImage != 0 ) && ( myPtrEmbedder != 0 );
196 }
197 
198 
199 
200 ///////////////////////////////////////////////////////////////////////////////
201 // Implementation of inline functions //
202 
203 template < typename TKSpace, typename TImage, typename TEmbedder >
204 inline
205 std::ostream&
206 DGtal::operator<< ( std::ostream & out,
207  const ImageLinearCellEmbedder<TKSpace, TImage, TEmbedder> & object )
208 {
209  object.selfDisplay( out );
210  return out;
211 }
212 
213 // //
214 ///////////////////////////////////////////////////////////////////////////////
215 
216