forked from opengazer/OpenGazer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Point.cpp
69 lines (54 loc) · 1.59 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
#include "utils.h"
void convert(const Point& point, CvPoint2D32f& p) {
p.x = point.x;
p.y = point.y;
}
ostream& operator<< (ostream& out, const Point& p) {
out << p.x << " " << p.y << endl;
return out;
}
istream& operator>> (istream& in, Point& p) {
in >> p.x >> p.y;
return in;
}
void Point::operator=(CvPoint2D32f const& point) {
x = point.x;
y = point.y;
}
void Point::operator=(CvPoint const& point) {
x = point.x;
y = point.y;
}
double Point::distance(Point other) const {
return fabs(other.x - x) + fabs(other.y - 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::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");
}
CvPoint Point::cvpoint(void) const {
return cvPoint(cvRound(x), cvRound(y));
}
CvPoint2D32f Point::cvpoint32(void) const {
return cvPoint2D32f(x, y);
}
int Point::closestPoint(const vector<Point> &points) const {
if (points.empty())
return -1;
vector<double> distances(points.size());
transform(points.begin(), points.end(), distances.begin(),
sigc::mem_fun(*this, &Point::distance));
return min_element(distances.begin(), distances.end()) - distances.begin();
}