MIRA
RasterTriangle.h
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2012 by
3  * MetraLabs GmbH (MLAB), GERMANY
4  * and
5  * Neuroinformatics and Cognitive Robotics Labs (NICR) at TU Ilmenau, GERMANY
6  * All rights reserved.
7  *
8  * Contact: info@mira-project.org
9  *
10  * Commercial Usage:
11  * Licensees holding valid commercial licenses may use this file in
12  * accordance with the commercial license agreement provided with the
13  * software or, alternatively, in accordance with the terms contained in
14  * a written agreement between you and MLAB or NICR.
15  *
16  * GNU General Public License Usage:
17  * Alternatively, this file may be used under the terms of the GNU
18  * General Public License version 3.0 as published by the Free Software
19  * Foundation and appearing in the file LICENSE.GPL3 included in the
20  * packaging of this file. Please review the following information to
21  * ensure the GNU General Public License version 3.0 requirements will be
22  * met: http://www.gnu.org/copyleft/gpl.html.
23  * Alternatively you may (at your option) use any later version of the GNU
24  * General Public License if such license has been publicly approved by
25  * MLAB and NICR (or its successors, if any).
26  *
27  * IN NO EVENT SHALL "MLAB" OR "NICR" BE LIABLE TO ANY PARTY FOR DIRECT,
28  * INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES ARISING OUT OF
29  * THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF "MLAB" OR
30  * "NICR" HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31  *
32  * "MLAB" AND "NICR" SPECIFICALLY DISCLAIM ANY WARRANTIES, INCLUDING,
33  * BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
34  * FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE PROVIDED HEREUNDER IS
35  * ON AN "AS IS" BASIS, AND "MLAB" AND "NICR" HAVE NO OBLIGATION TO
36  * PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS OR MODIFICATIONS.
37  */
38 
47 #ifndef _MIRA_RASTERTIRANGLE_H_
48 #define _MIRA_RASTERTIRANGLE_H_
49 
50 #include <geometry/Bresenham.h>
51 #include <geometry/Polygon.h>
52 #include <error/Exceptions.h>
53 
54 namespace mira {
55 
57 
59 namespace Private {
60 
62 
63 struct LeftVisitor {
64  std::vector<int>& mP;
65  LeftVisitor(std::vector<int>& p) : mP(p) {}
66  void operator()(int x, int y, int length) {
67  mP[y] = x;
68  }
69 };
70 
71 struct RightVisitor {
72  std::vector<int>& mP;
73  RightVisitor(std::vector<int>& p) : mP(p) {}
74  void operator()(int x, int y, int length) {
75  mP[y] = x+length-1;
76  }
77 };
78 
79 template<typename Visitor>
80 struct AdaptVisitor {
81  Visitor&& v; // this can be an lvalue reference or rvalue reference depending on
82  // how the constructor is called (assuming type Visitor is deduced)
83  // in both cases, keeping a (l/rvalue) reference to visitor is ok as
84  // long as its lifetime does not end before ours!
85  // (e.g. we are created and directly used as param to a function call)
86  AdaptVisitor(Visitor&& visitor) : v(std::forward<Visitor>(visitor)) {}
87  void operator()(int x, int y, int length) {
88  v(x, x+length-1, y);
89  }
90 };
91 
93 
94 } // namespace Private {
96 
98 
125 template<typename Visitor>
126 inline void rasterTriangle(Point2i p0, Point2i p1, Point2i p2,
127  Visitor&& visitor)
128 {
129  if(p1[1]<p0[1] || ((p1[1]==p0[1]) && (p1[0]<p0[0]))) {
130  std::swap(p1[0], p0[0]);
131  std::swap(p1[1], p0[1]);
132  }
133 
134  if(p2[1]<p1[1] || ((p2[1]==p1[1]) && (p2[0]<p1[0]))) {
135  std::swap(p2[0], p1[0]);
136  std::swap(p2[1], p1[1]);
137  }
138 
139  if(p1[1]<p0[1] || ((p1[1]==p0[1]) && (p1[0]<p0[0]))) {
140  std::swap(p1[0], p0[0]);
141  std::swap(p1[1], p0[1]);
142  }
143 
144  // possible cases for the triangle
145 
146  // CASE 1:
147  // 0
148  // |\.
149  // | \.
150  // p * 1
151  // | /
152  // |/
153  // 2
154 
155  // CASE 2:
156  // 0
157  // /|
158  // / |
159  // 1 * p
160  // \ |
161  // \|
162  // 2
163 
164  // compute the x-coordinate of p:
165  int x0(p0[0]), x1(p1[0]), x2(p2[0]);
166  int y0(p0[1]), y1(p1[1]), y2(p2[1]);
167 
168  const int dx01 = x1-x0;
169  const int dy01 = y1-y0;
170 
171  const int dx02 = x2-x0;
172  const int dy02 = y2-y0;
173 
174  // just raster a single line if all 3 points are on a straight line (for efficiency)
175  if (dx01*dy02 == dx02*dy01) {
176  bresenhamRunSlice(x0, y0, x2, y2, Private::AdaptVisitor<Visitor>(std::forward<Visitor>(visitor)));
177  return;
178  }
179 
180  assert(dy02!=0); // dy02 == 0 --> dy01 == 0 (because of sorting on y) --> dx01*dy02 == dx02*dy01 (case above)
181 
182  float p = float(dx02 * dy01) / dy02 + x0;
183 
184  std::vector<int> left(dy02+1);
185  std::vector<int> right(dy02+1);
186 
187  Private::LeftVisitor leftVisitor(left);
188  Private::RightVisitor rightVisitor(right);
189  if(p <= x1) { // CASE 1:
190  // left edge 0->2
191  bresenhamRunSlice(x0, y0-y0, x2, y2-y0, leftVisitor);
192  // right edge 0->1
193  bresenhamRunSlice(x0, y0-y0, x1, y1-y0, rightVisitor);
194  // right edge 1->2
195  bresenhamRunSlice(x1, y1-y0, x2, y2-y0, rightVisitor);
196  } else { // CASE 2:
197  // left edge 0->1
198  bresenhamRunSlice(x0, y0-y0, x1, y1-y0, leftVisitor);
199  // left edge 1->2
200  bresenhamRunSlice(x1, y1-y0, x2, y2-y0, leftVisitor);
201  // right edge 0->2
202  bresenhamRunSlice(x0, y0-y0, x2, y2-y0, rightVisitor);
203  }
204 
205  for(int i=0; i<=dy02; ++i)
206  visitor(left[i], right[i], i+y0);
207 }
208 
218 template<typename Visitor>
219 inline void rasterTriangle(const Polygon2i& polygon, Visitor&& visitor)
220 {
221  // only polygons with 3 points or 4 points are valid
222  if((polygon.size() < 3)||(polygon.size() > 4))
223  {
224  throw XRuntime("Only polygons with 3 points and no inner rings are triangles");
225  }
226 
227  auto pointIter = polygon.begin();
228 
229  Point2i A = *pointIter; pointIter++;
230  Point2i B = *pointIter; pointIter++;
231  Point2i C = *pointIter;
232 
233  rasterTriangle(A, B, C, visitor);
234 }
235 
237 
238 }
239 
240 #endif
General point class template.
Definition: Point.h:133
specialize cv::DataType for our ImgPixel and inherit from cv::DataType<Vec>
Definition: IOService.h:67
STL namespace.
void bresenhamRunSlice(int x0, int y0, int x1, int y1, Visitor &&visitor)
Rasterizes a Bresenham line starting at coordinate (x0,y0) and ending in (x1,y1). ...
Definition: Bresenham.h:572
void rasterTriangle(Point2i p0, Point2i p1, Point2i p2, Visitor &&visitor)
Rasters a triangle scanline by scanline.
Definition: RasterTriangle.h:126
Commonly used exception classes.
boost::geometry::model::ring< Point2i > Polygon2i
A 2D polygon with integer precision.
Definition: Polygon.h:128
This header provides a set of functions to iterate over lines with the Bresenham or Bresenham Run-Sli...
Simple Wrapper for Boost::geometry polygon.