GJK_nD
 All Classes Files Functions Typedefs Friends Pages
GreedyDecomposition.hpp
1 /*
2  * Copyright (c) 2012 Laurent Provot <provot.research@gmail.com>,
3  * Yan Gerard <yan.gerard@free.fr> and Fabien Feschet <research@feschet.fr>
4  * All rights reserved.
5  *
6  * This is free software: you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation, either version 3 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program. If not, see <http://www.gnu.org/licenses/>.
18  */
19 
20 #ifndef UTILS_GREEDY_DECOMPOSITION_HPP
21 #define UTILS_GREEDY_DECOMPOSITION_HPP
22 
23 #include <vector>
24 #include <utility>
25 
26 namespace Utils {
27 
73 template <typename Segment>
75 {
76 public:
78  typedef typename Segment::Coordinates Coordinates;
80  typedef typename Segment::Curve Curve;
81 
82 public:
87  GreedyDecomposition(const Curve * curve = 0) : myCurve(curve) {}
89 
93  void setCurve(const Curve * curve) { myCurve = curve; }
94 
99  std::vector<Segment> decomposeCurve();
100 
105  std::vector<Segment> decomposeCurve(const Curve & curve)
106  {
107  std::vector<Segment> result;
108  const Curve * myCurveBak = myCurve;
109  setCurve(&curve);
110  result = decomposeCurve();
111  myCurve = myCurveBak;
112  return result;
113  }
114 
115 private:
117  GreedyDecomposition & operator=(const GreedyDecomposition &);
118 
119 private:
120  const Curve * myCurve;
121 };
122 
123 
124 template <typename Segment>
126 {
127  std::vector<Segment> result;
128 
129  Segment segment;
130  typename Curve::const_iterator pixelItor = myCurve->begin();
131  while (pixelItor != myCurve->end()) {
132  if (!segment.addPoint(*pixelItor)) {
133  result.push_back(segment);
134  segment = Segment();
135  continue;
136  }
137  ++pixelItor;
138  }
139  if (segment.isValid())
140  result.push_back(segment);
141 
142  return result;
143 }
144 
145 } // namespace Utils
146 
147 #endif // UTILS_GREEDY_DECOMPOSITION_HPP