DGtal  0.9.2
ContourHelper.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 ContourHelper.ih
19  * @author Bertrand Kerautret (\c kerautre@loria.fr )
20  * LORIA (CNRS, UMR 7503), University of Nancy, France
21  *
22  * @date 2011/05/02
23  *
24  * Implementation of inline methods defined in ContourHelper.h
25  *
26  * This file is part of the DGtal library.
27  */
28 
29 ///////////////////////////////////////////////////////////////////////////////
30 // IMPLEMENTATION of inline methods.
31 ///////////////////////////////////////////////////////////////////////////////
32 
33 //////////////////////////////////////////////////////////////////////////////
34 #include <cstdlib>
35 //////////////////////////////////////////////////////////////////////////////
36 
37 
38 
39 ///////////////////////////////////////////////////////////////////////////////
40 // Implementation of inline methods //
41 
42 
43 
44 template <typename TPoint>
45 inline
46 TPoint
47 DGtal::ContourHelper::getMeanPoint(const std::vector<TPoint> & aContour){
48  TPoint ptMean;
49  for(unsigned int i =0; i<aContour.size(); i++){
50  ptMean[0]+=aContour.at(i)[0];
51  ptMean[1]+=aContour.at(i)[1];
52  }
53  ptMean[0] /= aContour.size();
54  ptMean[1] /= aContour.size();
55  return ptMean;
56 }
57 
58 
59 template <typename TPoint>
60 inline
61 bool
62 DGtal::ContourHelper::isCounterClockWise(const std::vector<TPoint> & aContour){
63  double sum=0.0;
64  TPoint p0 = *(aContour.begin());
65  typename std::vector<TPoint>::const_iterator itContour = aContour.begin();
66  itContour++;
67  TPoint p=*itContour;
68  itContour++;
69  for( ; itContour!=aContour.end(); itContour++){
70  TPoint pSuiv = *itContour;
71  typename TPoint::Coordinate v1x = p[0] - p0[0];
72  typename TPoint::Coordinate v1y = p[1] - p0[1];
73  typename TPoint::Coordinate v2x = pSuiv[0] - p[0];
74  typename TPoint::Coordinate v2y = pSuiv[1] - p[1];
75  sum+=(v1x*v2y)-(v2x*v1y);
76  p=pSuiv;
77  }
78  return (sum>0.0);
79 }
80 
81 
82 
83 
84 
85 ///////////////////////////////////////////////////////////////////////////////
86 // Implementation of inline functions and external operators //
87 
88 /**
89  * Overloads 'operator<<' for displaying objects of class 'ContourHelper'.
90  * @param out the output stream where the object is written.
91  * @param object the object of class 'ContourHelper' to write.
92  * @return the output stream after the writing.
93  */
94 inline
95 std::ostream&
96 DGtal::operator<< ( std::ostream & out,
97  const ContourHelper & object )
98 {
99  object.selfDisplay ( out );
100  return out;
101 }
102 
103 // //
104 ///////////////////////////////////////////////////////////////////////////////
105 
106