forked from opengazer/OpenGazer
-
Notifications
You must be signed in to change notification settings - Fork 21
/
Copy pathPoint.cpp
executable file
·98 lines (78 loc) · 1.79 KB
/
Point.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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
#include "Point.h"
Point::Point():
x(0.0),
y(0.0)
{
}
Point::Point(double x, double y):
x(x),
y(y)
{
}
Point::Point(CvPoint2D32f const &point):
x(point.x),
y(point.y)
{
}
Point::Point(CvPoint const &point):
x(point.x),
y(point.y)
{
}
double Point::distance(Point other) const {
return fabs(other.x - x) + fabs(other.y - y);
}
double Point::distance2f(cv::Point2f other) const {
return fabs(other.x - x) + fabs(other.y - y);
}
int Point::closestPoint(const std::vector<Point> &points) const {
if (points.empty()) {
return -1;
}
double minDistance = 10000;
int minIndex = 0;
for(int i=0; i<points.size(); i++) {
double tempDistance = this->distance(points[i]);
if(tempDistance < minDistance) {
minDistance = tempDistance;
minIndex = 0;
}
}
return minIndex;
}
void Point::save(CvFileStorage *out, const char *name) const {
cvStartWriteStruct(out, name, CV_NODE_MAP);
cvWriteReal(out, "x", x);
cvWriteReal(out, "y", y);
cvEndWriteStruct(out);
}
void Point::load(CvFileStorage *in, CvFileNode *node) {
x = cvReadRealByName(in, node, "x");
y = cvReadRealByName(in, node, "y");
}
Point Point::operator+(const Point &other) const {
return Point(x + other.x, y + other.y);
}
Point Point::operator-(const Point &other) const {
return Point(x - other.x, y - other.y);
}
void Point::operator=(CvPoint2D32f const &point) {
x = point.x;
y = point.y;
}
void Point::operator=(CvPoint const &point) {
x = point.x;
y = point.y;
}
void Point::operator=(cv::Point2f const &point) {
x = point.x;
y = point.y;
}
std::ostream &operator<< (std::ostream &out, const Point &p) {
out << p.x << " " << p.y << std::endl;
return out;
}
std::istream &operator>> (std::istream &in, Point &p) {
in >> p.x >> p.y;
return in;
}