DGtal  0.9.4.1
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 #include "DGtal/geometry/curves/FreemanChain.h"
36 //////////////////////////////////////////////////////////////////////////////
37 
38 
39 
40 ///////////////////////////////////////////////////////////////////////////////
41 // Implementation of inline methods //
42 
43 
44 
45 template <typename TPoint>
46 inline
47 DGtal::PointVector<TPoint::dimension, double>
48 DGtal::ContourHelper::getBarycenter(const std::vector<TPoint> & aSet)
49 {
50  DGtal::PointVector<TPoint::dimension, double> ptMean;
51  for(unsigned int i =0; i<aSet.size(); i++)
52  {
53  for(typename TPoint::Dimension j=0; j < TPoint::dimension; j++)
54  {
55  ptMean[j] += static_cast<double>(aSet.at(i)[j]);
56  }
57  }
58  for(unsigned int j=0; j < TPoint::dimension; j++)
59  {
60  ptMean[j] /= aSet.size();
61  }
62  return ptMean;
63 }
64 
65 
66 
67 
68 
69 
70 template <typename TIterator, typename TOutputIterator>
71 inline
72 void
73 DGtal::ContourHelper::pixels2pixels8C(const TIterator &itb, const TIterator &ite,
74  TOutputIterator out)
75 {
76  TIterator it = itb;
77  *out++ = *it;
78  unsigned int size = std::distance(itb, ite);
79  int i=0;
80  while(i<(int)size-1)
81  {
82 
83  short code = FreemanChain<int>::freemanCode4C((*(it+1))[0]-(*it)[0],(*(it+1))[1]-(*it)[1]);
84  short codeNext = FreemanChain<int>::freemanCode4C((*(it+2))[0]-(*(it+1))[0],(*(it+2))[1]-(*(it+1))[1]);
85  if((code != 8 && codeNext != 8) && (((4+(int)codeNext-(int)code)%4)==1 || ((4+(int)codeNext-(int)code)%4)==3))
86  {
87  it++;
88  i++;
89  }
90  it++;
91  *out++ = *it;
92  i++;
93  }
94 }
95 
96 
97 
98 
99 template <typename TPoint>
100 inline
101 bool
102 DGtal::ContourHelper::isCounterClockWise(const std::vector<TPoint> & aCurve)
103 {
104  double sum=0.0;
105  TPoint p0 = *(aCurve.begin());
106  typename std::vector<TPoint>::const_iterator itContour = aCurve.begin();
107  itContour++;
108  TPoint p=*itContour;
109  itContour++;
110  for( ; itContour!=aCurve.end(); itContour++)
111  {
112  TPoint pSuiv = *itContour;
113  typename TPoint::Coordinate v1x = p[0] - p0[0];
114  typename TPoint::Coordinate v1y = p[1] - p0[1];
115  typename TPoint::Coordinate v2x = pSuiv[0] - p[0];
116  typename TPoint::Coordinate v2y = pSuiv[1] - p[1];
117  sum+=(v1x*v2y)-(v2x*v1y);
118  p=pSuiv;
119  }
120  return (sum>0.0);
121 }
122 
123 
124 
125 
126 
127 ///////////////////////////////////////////////////////////////////////////////
128 // Implementation of inline functions and external operators //
129 
130 /**
131  * Overloads 'operator<<' for displaying objects of class 'ContourHelper'.
132  * @param out the output stream where the object is written.
133  * @param object the object of class 'ContourHelper' to write.
134  * @return the output stream after the writing.
135  */
136 inline
137 std::ostream&
138 DGtal::operator<< ( std::ostream & out,
139  const ContourHelper & object )
140 {
141  object.selfDisplay ( out );
142  return out;
143 }
144 
145 // //
146 ///////////////////////////////////////////////////////////////////////////////