DGtal  0.9.2
DSSLengthEstimator.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 DSSLengthEstimator.ih
19  * @author Tristan Roussillon (\c
20  * tristan.roussillon@liris.cnrs.fr ) Laboratoire d'InfoRmatique en
21  * Image et Systèmes d'information - LIRIS (CNRS, UMR 5205), CNRS,
22  * France
23  *
24  *
25  * @date 2011/07/07
26  *
27  * Implementation of inline methods defined in DSSLengthEstimator.h
28  *
29  * This file is part of the DGtal library.
30  */
31 
32 
33 //////////////////////////////////////////////////////////////////////////////
34 #include <cstdlib>
35 //////////////////////////////////////////////////////////////////////////////
36 
37 ///////////////////////////////////////////////////////////////////////////////
38 // IMPLEMENTATION of inline methods.
39 ///////////////////////////////////////////////////////////////////////////////
40 
41 ///////////////////////////////////////////////////////////////////////////////
42 // ----------------------- Standard services ------------------------------
43 
44 /**
45  * Constructor.
46  */
47 template <typename T>
48 inline
49 DGtal::DSSLengthEstimator<T>::DSSLengthEstimator()
50 {
51  myIsInitBefore = false;
52 }
53 
54 /**
55  * Destructor.
56  */
57 template <typename T>
58 inline
59 DGtal::DSSLengthEstimator<T>::~DSSLengthEstimator()
60 {
61 }
62 
63 
64 
65 ///////////////////////////////////////////////////////////////////////////////
66 // Interface - public :
67 
68 
69 
70 template <typename T>
71 inline
72 void
73 DGtal::DSSLengthEstimator<T>::init(const double h,
74  const ConstIterator& itb, const ConstIterator& ite)
75 {
76  myH = h;
77  myIsInitBefore = true;
78  myRep.clear();
79 
80  //segments into DSSs
81  DSSComputer computer;
82  GreedySegmentation<DSSComputer> decomposition ( itb, ite, computer );
83 
84  //computes the resulting polygonal representation
85  typename GreedySegmentation<DSSComputer>::SegmentComputerIterator segIt = decomposition.begin();
86  typename GreedySegmentation<DSSComputer>::SegmentComputerIterator segItEnd = decomposition.end();
87  for ( ; segIt != segItEnd; ++segIt )
88  {
89  myRep.push_back( segIt->back() );
90  }
91  //last point
92  myRep.push_back(lastPoint(ite));
93 }
94 
95 template <typename T>
96 inline
97 typename DGtal::DSSLengthEstimator<T>::Point
98 DGtal::DSSLengthEstimator<T>::lastPoint(const ConstIterator& ite)
99 {
100  return lastPoint(ite, typename DGtal::IteratorCirculatorTraits<ConstIterator>::Type());
101 }
102 
103 template <typename T>
104 inline
105 typename DGtal::DSSLengthEstimator<T>::Point
106 DGtal::DSSLengthEstimator<T>::lastPoint(const ConstIterator& ite, IteratorType)
107 {
108  ConstIterator itcopy = ite;
109  return *--itcopy;
110 }
111 
112 template <typename T>
113 inline
114 typename DGtal::DSSLengthEstimator<T>::Point
115 DGtal::DSSLengthEstimator<T>::lastPoint(const ConstIterator& c, CirculatorType)
116 {
117  return *c;
118 }
119 
120 template <typename T>
121 inline
122 typename DGtal::DSSLengthEstimator<T>::Quantity
123 DGtal::DSSLengthEstimator<T>::eval() const
124 {
125  ASSERT(myH > 0);
126  ASSERT(myIsInitBefore);
127 
128  Quantity val = 0;
129 
130  if (myRep.size() > 1) {
131 
132  typename std::vector<Point>::const_iterator i = myRep.begin();
133  typename std::vector<Point>::const_iterator j = i;
134  ++j;
135  for ( ; j != myRep.end(); ++i, ++j) {
136  Vector v( *j - *i );
137  val += v.norm(Vector::L_2);
138  }
139 
140  }
141 
142  return val*myH;
143 }
144 
145 
146 
147 
148 
149 /**
150  * Writes/Displays the object on an output stream.
151  * @param out the output stream where the object is written.
152  */
153 template <typename T>
154 inline
155 void
156 DGtal::DSSLengthEstimator<T>::selfDisplay ( std::ostream & out ) const
157 {
158  out << "[DSSLengthEstimator]";
159  if (myIsInitBefore)
160  out <<" myH="<<myH;
161  else
162  out<< " not initialized";
163 }
164 
165 /**
166  * Checks the validity/consistency of the object.
167  * @return 'true' if the object is valid, 'false' otherwise.
168  */
169 template <typename T>
170 inline
171 bool
172 DGtal::DSSLengthEstimator<T>::isValid() const
173 {
174  return myIsInitBefore;
175 }
176 
177 
178 
179 ///////////////////////////////////////////////////////////////////////////////
180 // Implementation of inline functions //
181 
182 template <typename T>
183 inline
184 std::ostream&
185 DGtal::operator<< ( std::ostream & out,
186  const DSSLengthEstimator<T> & object )
187 {
188  object.selfDisplay( out );
189  return out;
190 }
191 
192 // //
193 ///////////////////////////////////////////////////////////////////////////////
194 
195