-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCurveRDP.cpp
67 lines (56 loc) · 1.72 KB
/
CurveRDP.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
/**
*
* Reza Adhitya Saputra (reza.adhitya.saputra@gmail.com)
* Version: 2015
*
*/
#include "CurveRDP.h"
#include <limits>
/**
* Reza Adhitya Saputra
* radhitya@uwaterloo.ca
*/
CurveRDP::CurveRDP()
{
}
float CurveRDP::PerpendicularDistance(AVector p, AVector p1, AVector p2)
{
float result;
if (abs(p1.x - p2.x) < std::numeric_limits<float>::epsilon()) { result = abs(p.x - p1.x); }
else
{
float slope = (p2.y - p1.y) / (p2.x - p1.x);
float intercept = p1.y - (slope * p1.x);
result = abs(slope * p.x - p.y + intercept) / sqrt(pow(slope, 2) + 1);
}
return result;
}
void CurveRDP::SimplifyRDP(std::vector<AVector>& oldCurves, std::vector<AVector>& newCurves, float epsilon)
{
newCurves.clear();
newCurves.push_back(oldCurves[0]);
SimplifyRDPRecursive(oldCurves, newCurves, epsilon, 0, oldCurves.size() - 1);
newCurves.push_back(oldCurves[oldCurves.size() - 1]);
}
void CurveRDP::SimplifyRDPRecursive(std::vector<AVector>& oldCurves, std::vector<AVector>& newCurves, float epsilon, int startIndex, int endIndex)
{
AVector firstPoint = oldCurves[startIndex];
AVector lastPoint = oldCurves[endIndex];
int index = -1;
float dist = std::numeric_limits<float>::min();
for (int i = startIndex + 1; i < endIndex; i++)
{
float cDist = PerpendicularDistance(oldCurves[i], firstPoint, lastPoint);
if (cDist > dist)
{
dist = cDist;
index = i;
}
}
if (index != -1 && dist > epsilon)
{
SimplifyRDPRecursive(oldCurves, newCurves, epsilon, startIndex, index);
newCurves.push_back(oldCurves[index]);
SimplifyRDPRecursive(oldCurves, newCurves, epsilon, index, endIndex);
}
}