ContourHelper.cpp
Go to the documentation of this file.
1 // ****************************************************************************
2 // This file is part of the Integrating Vision Toolkit (IVT).
3 //
4 // The IVT is maintained by the Karlsruhe Institute of Technology (KIT)
5 // (www.kit.edu) in cooperation with the company Keyetech (www.keyetech.de).
6 //
7 // Copyright (C) 2014 Karlsruhe Institute of Technology (KIT).
8 // All rights reserved.
9 //
10 // Redistribution and use in source and binary forms, with or without
11 // modification, are permitted provided that the following conditions are met:
12 //
13 // 1. Redistributions of source code must retain the above copyright
14 // notice, this list of conditions and the following disclaimer.
15 //
16 // 2. Redistributions in binary form must reproduce the above copyright
17 // notice, this list of conditions and the following disclaimer in the
18 // documentation and/or other materials provided with the distribution.
19 //
20 // 3. Neither the name of the KIT nor the names of its contributors may be
21 // used to endorse or promote products derived from this software
22 // without specific prior written permission.
23 //
24 // THIS SOFTWARE IS PROVIDED BY THE KIT AND CONTRIBUTORS “AS IS” AND ANY
25 // EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
26 // WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
27 // DISCLAIMED. IN NO EVENT SHALL THE KIT OR CONTRIBUTORS BE LIABLE FOR ANY
28 // DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
29 // (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
30 // LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
31 // ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
32 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
33 // THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34 // ****************************************************************************
35 // ****************************************************************************
36 // Filename: ContourHelper.cpp
37 // Authors: Derick Beng Yuh, Pedram Azad
38 // Date: 03.08.2010
39 // ****************************************************************************
40 
41 #include "ContourHelper.h"
42 #include "Math/Math2d.h"
43 
44 
45 
46 // ****************************************************************************
47 // Helper function used for testing if a middle point (p2) lies above, below or
48 // on the line between p1 and p3.
49 // Above : function returns negative value
50 // Below : function returns positive value
51 // On : function reurns 0
52 // ****************************************************************************
53 static inline float det(const Vec2d &p1, const Vec2d &p2, const Vec2d &p3)
54 {
55  return ((p1.x - p2.x) * (p3.y - p2.y)) - ((p3.x - p2.x) * (p1.y - p2.y));
56 }
57 
58 static void BuildLowerHalfHull(const CVec2dArray &input, CVec2dArray &output)
59 {
60  // Repeatedly add the leftmost point to the hull, then test to see if a
61  // convexity violation has occurred. If it has, fix things up by removing
62  // the next-to-last point in the output sequence until convexity is restored.
63  const int nPoints = input.GetSize();
64 
65  for (int i = 0; i < nPoints; i++)
66  {
67  output.AddElement(input[i]);
68 
69  while (output.GetSize() >= 3)
70  {
71  const int nLastIndex = output.GetSize() - 1;
72 
73  if (det(output[nLastIndex - 2], output[nLastIndex - 1], output[nLastIndex]) <= 0.0f)
74  output.DeleteElement(nLastIndex - 1);
75  else
76  break;
77  }
78  }
79 }
80 
81 static void BuildUpperHalfHull(const CVec2dArray &input, CVec2dArray &output)
82 {
83  // Repeatedly add the rightmost point to the hull, then test to see if a
84  // convexity violation has occurred. If it has, fix things up by removing
85  // the next-to-last point in the output sequence until convexity is restored.
86  const int nPoints = input.GetSize();
87 
88  for (int i = nPoints - 1; i >= 0; i--)
89  {
90  output.AddElement(input[i]);
91 
92  while (output.GetSize() >= 3)
93  {
94  const int nLastIndex = output.GetSize() - 1;
95 
96  if (det(output[nLastIndex - 2], output[nLastIndex - 1], output[nLastIndex]) <= 0.0f)
97  output.DeleteElement(nLastIndex - 1);
98  else
99  break;
100  }
101  }
102 }
103 
104 static void QuicksortX(CVec2dArray &pPoints, int nLow, int nHigh)
105 {
106  int i = nLow;
107  int j = nHigh;
108 
109  const float fX = pPoints[(nLow + nHigh) / 2].x;
110 
111  while (i <= j)
112  {
113  while (pPoints[i].x < fX) i++;
114  while (pPoints[j].x > fX) j--;
115 
116  if (i <= j)
117  {
118  Vec2d temp;
119  Math2d::SetVec(temp, pPoints[i]);
120  Math2d::SetVec(pPoints[i], pPoints[j]);
121  Math2d::SetVec(pPoints[j], temp);
122 
123  i++;
124  j--;
125  }
126  }
127 
128  if (nLow < j) QuicksortX(pPoints, nLow, j);
129  if (i < nHigh) QuicksortX(pPoints, i, nHigh);
130 }
131 
132 
133 void ContourHelper::ComputeConvexHull(const Vec2d *pPoints, int nPoints, Vec2d *pResultPoints, int &nResultPoints)
134 {
135  int i;
136 
137  CVec2dArray vertices(2 * nPoints);
138  for (i = 0; i < nPoints; i++)
139  vertices.AddElement(pPoints[i]);
140 
141  QuicksortX(vertices, 0, nPoints - 1);
142 
143  const int nLastPoint = vertices.GetSize() - 1;
144  const Vec2d &left = vertices[0];
145  const Vec2d &right = vertices[nLastPoint];
146 
147  // partition points on plane according to their position with
148  // with respect to the line between left and right
149  CVec2dArray upper(2 * nPoints), lower(2 * nPoints);
150  upper.AddElement(left); // must be added before loop
151  for (i = 1; i < nLastPoint; i++)
152  {
153  const Vec2d &point = vertices[i];
154 
155  if (det(left, point, right) < 0.0f)
156  {
157  // point is above the line between left and right
158  upper.AddElement(point);
159  }
160  else
161  {
162  // point is below the line between left and right
163  lower.AddElement(point);
164  }
165  }
166  lower.AddElement(right); // must be added after loop
167 
168  CVec2dArray hull;
169  hull.AddElement(left);
170 
171  // first build lower hull
172  BuildLowerHalfHull(lower, hull);
173 
174  // then build upper hull
175  BuildUpperHalfHull(upper, hull);
176 
177  // add points to result
178  nResultPoints = 0;
179  const int nHullSize = hull.GetSize() - 1;
180  for (i = 0; i < nHullSize; i++)
181  Math2d::SetVec(pResultPoints[nResultPoints++], hull[i]);
182 }
float y
Definition: Math2d.h:84
GLenum GLint x
Definition: glext.h:3125
static void BuildLowerHalfHull(const CVec2dArray &input, CVec2dArray &output)
float x
Definition: Math2d.h:84
static void BuildUpperHalfHull(const CVec2dArray &input, CVec2dArray &output)
static void QuicksortX(CVec2dArray &pPoints, int nLow, int nHigh)
GLenum GLenum GLenum input
Definition: glext.h:5307
void AddElement(const T &element)
Data structure for the representation of a 2D vector.
Definition: Math2d.h:82
void SetVec(Vec2d &vec, float x, float y)
Definition: Math2d.cpp:68
static float det(const Vec2d &p1, const Vec2d &p2, const Vec2d &p3)
bool DeleteElement(int nIndex)
void ComputeConvexHull(const Vec2d *pPoints, int nPoints, Vec2d *pResultPoints, int &nResultPoints)
Computes the convex hull for a set of contour points.


asr_ivt
Author(s): Allgeyer Tobias, Hutmacher Robin, Kleinert Daniel, Meißner Pascal, Scholz Jonas, Stöckle Patrick
autogenerated on Mon Dec 2 2019 03:47:27