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