-
Notifications
You must be signed in to change notification settings - Fork 0
/
pointset.cpp
executable file
·180 lines (140 loc) · 4.68 KB
/
pointset.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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
#include "pointset.h"
using namespace std;
using namespace cimg_library;
/*****************************************
* DISPLAY FUNCTIONS *
******************************************/
void PointSet::display_in_console() const {
for (unsigned int i=0;i<this->size();i++){
cout << endl <<"Point #" << i+1 <<endl;
((*this)[i]).display_in_console();
}
}
void PointSet::draw_points(CImg<unsigned char> &img, const unsigned char color[]) const {
int radius = 5;
for (unsigned int i=0;i<this->size();i++){
img.draw_circle((*this)[i].x,(*this)[i].y,radius,color);
}
}
void PointSet::display(CImg<unsigned char> &img,const unsigned char color[]) const {
draw_points(img,color);
img.display("Nice PointSet, isn't it?");
}
/*****************************************
* DISTANCE FUNCTIONS *
******************************************/
double PointSet::get_dist_with(const PointSet &other) const {
/**
* Simple sum of the distances
*/
double dist=0;
if (this->size()==other.size()) {
for (unsigned int i=0;i<this->size();i++)
dist += (*this)[i].get_dist_with(other[i]);
}
else {
cout << "ERROR in PointSet: point sets must have the same sizes!" <<endl;
exit(2);
}
return dist;
}
double PointSet::get_dist_with(const PointSet &other,const vector<bool> &mask) const {
/**
* Sum of the distances where the value of the mask is true
*/
double dist=0;
if (this->size()==other.size() && this->size()==mask.size()) {
for (unsigned int i=0;i<this->size();i++) {
if (mask[i])
dist += (*this)[i].get_dist_with(other[i]);
}
}
else {
cout << "ERROR in PointSet: point set 1 and 2 and mask must have the same sizes!" <<endl;
exit(2);
}
return dist;
}
double PointSet::get_scaled_dist_with(const PointSet &other,const vector<bool> &mask) const {
/**
* Sum of the distances where the value of the mask is true
*/
double dist=0;
if (this->size()==other.size() && this->size()==mask.size()) {
double mean = 0, sigma = 0;
for (unsigned int i=0;i<this->size();i++) {
if (mask[i])
mean += (*this)[i].get_dist_with(other[i]);
}
mean = mean/this->size();
for (unsigned int i=0;i<this->size();i++) {
if (mask[i])
dist += (*this)[i].get_dist_with(other[i]);
sigma += pow((*this)[i].get_dist_with(other[i])-mean,2);
}
sigma = sqrt(sigma/this->size());
if (sigma==0) {
cout << "FATAL ERROR: division by 0 in PointSet::get_scaled_dist_with" << endl;
exit(2);
}
dist /= sigma;
}
else {
cout << "ERROR in PointSet: point set 1 and 2 and mask must have the same sizes!" <<endl;
exit(2);
}
return dist;
}
/*****************************************
* FILLING FUNCTIONS *
******************************************/
void PointSet::fill_with_random(int nb_points){
srand ( time(NULL) );
for (int i=0;i<nb_points;i++){
this->push_back(Point2D(rand()%1000,rand()%1000));
}
}
void PointSet::fill_with_set1(int nb_points){
srand ( time(NULL) );
for (int i=0;i<nb_points;i++){
this->push_back(Point2D(100+30*cos(i)*pow(1.02,i),20+22*i));
}
}
/*****************************************
* STAT FUNCTIONS *
******************************************/
double PointSet::get_x_mean(){
double x_mean = 0;
for (unsigned int i=0;i<this->size();i++)
x_mean+=(*this)[i].x;
x_mean/=this->size();
return x_mean;
}
double PointSet::get_y_mean(){
double y_mean = 0;
for (unsigned int i=0;i<this->size();i++)
y_mean+=(*this)[i].y;
y_mean/=this->size();
return y_mean;
}
/*****************************************
* BOUNDING BOX FUNCTIONS *
******************************************/
void PointSet::is_in_bounding_box(const BoundingBox &box,vector<bool> &mask_result) const {
mask_result.resize(this->size());
for (unsigned int i=0;i<this->size();i++){
mask_result[i] = (*this)[i].is_in_bounding_box(box);
}
}
/*****************************************
* MIN FUNCTIONS *
******************************************/
void PointSet::min_wrt_dist(const PointSet &other, double &min_dist, int &min_ind) const {
min_dist = INFINITY;
for (unsigned int i=0;i<this->size();i++) {
if (min_dist > (*this)[i].get_dist_with(other[i])) {
min_dist = (*this)[i].get_dist_with(other[i]);
min_ind = i;
}
}
}