DGtal  0.9.2
FPLengthEstimator.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 FPLengthEstimator.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 FPLengthEstimator.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::FPLengthEstimator<T>::FPLengthEstimator()
50 {
51  myIsInitBefore = false;
52 }
53 
54 /**
55  * Destructor.
56  */
57 template <typename T>
58 inline
59 DGtal::FPLengthEstimator<T>::~FPLengthEstimator()
60 {
61 }
62 
63 
64 
65 ///////////////////////////////////////////////////////////////////////////////
66 // Interface - public :
67 
68 
69 
70 template <typename T>
71 inline
72 void
73 DGtal::FPLengthEstimator<T>::init(const double h,
74  const ConstIterator& itb, const ConstIterator& ite, const bool& isClosed)
75 {
76  myH = h;
77  myIsInitBefore = true;
78  myRep.clear();
79 
80  //TODO deal with circulators
81  FaithfulPolygon fp( itb, ite );
82  myRep.resize( fp.size() );
83  fp.copyFP( myRep.begin() );
84 
85  if ( (isClosed)&&(myRep.size()!=0) )
86  myRep.push_back(myRep.front());
87 
88 }
89 
90 template <typename T>
91 inline
92 typename DGtal::FPLengthEstimator<T>::Quantity
93 DGtal::FPLengthEstimator<T>::eval() const
94 {
95  ASSERT(myH > 0);
96  ASSERT(myIsInitBefore);
97 
98  Quantity val = 0;
99 
100  if (myRep.size() > 1) {
101 
102  typename std::vector<Point>::const_iterator i = myRep.begin();
103  typename std::vector<Point>::const_iterator j = i;
104  ++j;
105  for ( ; j != myRep.end(); ++i, ++j) {
106  Vector v( *j - *i );
107  val += v.norm(Vector::L_2);
108  }
109 
110  }
111 
112  return val*myH;
113 }
114 
115 
116 
117 
118 
119 /**
120  * Writes/Displays the object on an output stream.
121  * @param out the output stream where the object is written.
122  */
123 template <typename T>
124 inline
125 void
126 DGtal::FPLengthEstimator<T>::selfDisplay ( std::ostream & out ) const
127 {
128  out << "[FPLengthEstimator]";
129  if (myIsInitBefore)
130  out <<" myH="<<myH;
131  else
132  out<< " not initialized";
133 }
134 
135 /**
136  * Checks the validity/consistency of the object.
137  * @return 'true' if the object is valid, 'false' otherwise.
138  */
139 template <typename T>
140 inline
141 bool
142 DGtal::FPLengthEstimator<T>::isValid() const
143 {
144  return myIsInitBefore;
145 }
146 
147 
148 
149 ///////////////////////////////////////////////////////////////////////////////
150 // Implementation of inline functions //
151 
152 template <typename T>
153 inline
154 std::ostream&
155 DGtal::operator<< ( std::ostream & out,
156  const FPLengthEstimator<T> & object )
157 {
158  object.selfDisplay( out );
159  return out;
160 }
161 
162 // //
163 ///////////////////////////////////////////////////////////////////////////////
164 
165