-
Notifications
You must be signed in to change notification settings - Fork 1
/
superpix_helper_func.cpp
212 lines (178 loc) · 5.16 KB
/
superpix_helper_func.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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
#include "superpix_helper_func.h"
#include <iostream>
#include <vector>
#include <math.h>
#include "my_point.h"
#include "path.h"
#include "curve.h"
#include "bound_box_shifter.h"
#include "splitter.h"
#include "shifter.h"
#include "polygonalizer.h"
#include "approximator.h"
using namespace std;
//Converts points to pixels (with alpha value)
// TODO this shouldn't be needed because we already have trunkated every
// double in "convert_path_to_pixels"...
vector<my_point> points_to_pixels(vector<my_point> pixel_pos){
//uint8_t alpha = 255; // 0-255
for (size_t i = 0; i < pixel_pos.size(); i++){
// if pos in not in int format, use math.round to convert x,y
}
return pixel_pos;
}
vector<my_point> convert_path_to_pixels(vector<my_point> corners){
vector<my_point> points;
for (size_t i=1; i < corners.size(); i++) {
my_point p1 = corners.at(i-1), p2 = corners.at(i);
if ( p1.x == p2.x || p1.y == p2.y){
continue;
}
int x1, y1, x2, y2;
if (p1.x < p2.x) {
x1 = (int) round(p1.x);
x2 = (int) round(p2.x) - 1;
} else {
x1 = (int) round(p1.x) - 1;
x2 = (int) round(p2.x);
}
if (p1.y < p2.y) {
y1 = (int) round(p1.y);
y2 = (int) round(p2.y) - 1;
} else {
y1 = (int) round(p1.y) - 1;
y2 = (int) round(p2.y);
}
if(x1==x2 && y1==y2){
my_point new_p;
new_p.x = x1;
new_p.y = y1;
points.push_back(new_p);
} else if(x1==x2) {
int sy = (y1 < y2) ? 1 : -1;
for(int y = y1; in_range(y, y1, y2); y += sy) {
my_point new_p;
new_p.x = x1;
new_p.y = y;
points.push_back(new_p);
}
} else if(y1==y2) {
int sx = (x1 < x2) ? 1 : -1;
for(int x = x1; in_range(x, x1, x2); x += sx) {
my_point new_p;
new_p.x = x;
new_p.y = y1;
points.push_back(new_p);
}
} else {
//assert(false);
int sx = (x1 < x2) ? 1 : -1;
int sy = (y1 < y2) ? 1 : -1;
for(int x = x1; in_range(x, x1, x2); x += sx) {
for(int y = y1; in_range(y, y1, y2); y += sy) {
my_point new_p;
new_p.x = x;
new_p.y = y;
points.push_back(new_p);
}
}
}
}
return points;
}
vector<my_point> get_pixelated_path_wrt_center(Path path, bool shifted, bool sorted, my_point center){
vector<Path> step1 = split_by_monotonicity(path);
//vector<my_point> pixels_trans;
//for (auto sub_path : step1 ) {
// vector<my_point> points = get_sample_points(sub_path, 100);
// pixels_trans.insert(pixels_trans.end(), points.begin(), points.end());
//}
//return pixels_trans;
if (step1.empty()) {
cout << "Step1 empty\n";
}
vector<Path> step2 = shift_endpoints_to_pixel_centres_WRT_center(step1, shifted, true, center);
if (step2.empty()) {
cout << "Step2 empty\n";
}
vector<my_point> step3 = get_pixelated_path_WRT_center(step2, sorted, center);
if (step3.empty()) {
cout << "Step3 empty\n";
}
return step3;
}
Path merge_straight_lines(Path path){
Path new_path = path;
for(size_t i = new_path.get_num_segments()-2; i >= 1; i--) {
Curve curve_before = new_path.get_curve_before_seg_idx(i);
Curve curve_after = new_path.get_curve_after_seg_idx(i);
my_point vec1 = curve_before.get_pN(3) - curve_before.get_pN(0);
my_point vec2 = curve_after.get_pN(3) - curve_after.get_pN(0);
bool between_lines = false;
//Is vec2 in the origin (zero)
if(is_zero(vec2)){
between_lines = true;
} else {
double angle = get_angle_to(vec1, vec2);
between_lines = curve_before.is_straight() && curve_after.is_straight()
&& abs(angle) < 0.01;
}
if(between_lines) {
new_path.remove_segment(i);
}
}
return new_path;
}
Path remove_degenerate_segements(Path path){
Path new_path;
// Get rid of degenerate curves
for (size_t i = 0; i < path.get_num_curves(); i++){
Curve curve = path.get_curve(i);
if (!curve.is_degenerate()){
new_path.add_curve(curve);
}
}
return new_path;
}
Path preprocess(Path path, bool &pixelated){
Path clean_path1 = remove_degenerate_segements(path);
if (clean_path1.empty()){
pixelated = true;
return clean_path1;
}
Path clean_path2 = merge_straight_lines(clean_path1);
return clean_path2;
}
vector<my_point> get_pixelated_path(Path path, bool shifted, bool sorted){
vector<my_point> pixels0;
bool pixelated = false;
Path step0 = preprocess(path, pixelated);
if (pixelated) {
cout << "Pixelated return\n";
return pixels0;
}
if (step0.empty()) {
vector<my_point> empty_vec;
cout << "Step0 return\n";
return empty_vec;
}
my_point center;
//TODO does this really have to be double and not int?
vector<double> bb_coeffs_without_AA = {10000, 40, 1, 50, 100};
step0 = snap_shape_by_optimal_bounding_box(step0, center, bb_coeffs_without_AA);
vector<my_point> pixels_trans = get_pixelated_path_wrt_center(step0, shifted, sorted, center);
return pixels_trans;
}
vector<my_point> superpix_helper_func(Path path){
bool shifted, sorted;
shifted = sorted = true;
vector<my_point> pixelated_path = get_pixelated_path(path, shifted, sorted);
if (pixelated_path.empty()){
cout << "Pixelated path is does not exists!\n";
return pixelated_path;
}
vector<my_point> pixel_pos = convert_path_to_pixels(pixelated_path);
//Convert doubles to ints (might be superfluous, look into this)
//vector<my_point> pixels = points_to_pixels(pixel_pos);
return pixel_pos;
}