Repository navigation
Expand file tree
/
Copy pathgeometry.cc
More file actions
153 lines (139 loc) · 5.55 KB
/
Copy pathgeometry.cc
File metadata and controls
153 lines (139 loc) · 5.55 KB
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
#include "geometry.h"
#include <math.h>
#include <stdio.h>
#include <algorithm>
#include <chrono>
#include <cmath>
#include <fstream>
#include <functional>
#include <iostream>
#include <random>
namespace primitives {
namespace geometry {
const float kWidthRange = 1;
const float kLengthRange = 1;
Eigen::Vector2d GeneratePoint() {
Eigen::Vector2d p(
static_cast<float>(rand()) / (static_cast<float>(RAND_MAX / kWidthRange)),
static_cast<float>(rand()) /
(static_cast<float>(RAND_MAX / kLengthRange)));
return p;
}
std::vector<float> GenerateRandomVec(const float size, const float min_range,
const float max_range) {
std::random_device random_device;
std::mt19937 mersenne_engine{random_device()};
std::uniform_real_distribution<float> distribution{min_range, max_range};
auto gen = [&distribution, &mersenne_engine]() {
return distribution(mersenne_engine);
};
std::vector<float> vec(size);
std::generate(std::begin(vec), std::end(vec), gen);
return vec;
}
std::vector<double> ComputeDirectionalComponents(const size_t num_sides,
const size_t range) {
std::vector<float> coordinates = GenerateRandomVec(num_sides, 0, range);
std::sort(coordinates.begin(), coordinates.end());
// Generate binary mask
std::vector<uint32_t> binary_mask(num_sides - 2, 0);
size_t split_size = std::ceil(binary_mask.size() / 2);
for (size_t i = 0; i < split_size; ++i) {
binary_mask[i] = 1;
}
// Shuffle the array.
std::shuffle(
binary_mask.begin(), binary_mask.end(),
std::default_random_engine(
std::chrono::system_clock::now().time_since_epoch().count()));
std::vector<double> directional_vectors;
directional_vectors.reserve(num_sides);
size_t previous_index_up = 0;
size_t previous_index_down = 0;
double sum_up = 0;
double sum_down = 0;
for (size_t i = 0; i < binary_mask.size(); ++i) {
if (binary_mask[i]) {
directional_vectors.push_back(
-1 * (coordinates[i + 1] - coordinates[previous_index_up]));
sum_up += std::abs((coordinates[i + 1] - coordinates[previous_index_up]));
previous_index_up = i + 1;
} else {
directional_vectors.push_back(coordinates[i + 1] -
coordinates[previous_index_down]);
sum_down += (coordinates[i + 1] - coordinates[previous_index_down]);
previous_index_down = i + 1;
}
}
// Add two vectors connecting to the last coordinate, one going up and the
// other down.
size_t index = coordinates.size() - 1;
directional_vectors.push_back(coordinates[index] -
coordinates[previous_index_down]);
directional_vectors.push_back(
-1 * (coordinates[index] - coordinates[previous_index_up]));
sum_up += (coordinates[index] - coordinates[previous_index_up]);
sum_down += (coordinates[index] - coordinates[previous_index_down]);
float coord_range = coordinates[index] - coordinates[0];
float kErr = 0.00001;
assert(std::abs(sum_up - coord_range) < kErr);
assert(std::abs(sum_down - coord_range) < kErr);
assert(directional_vectors.size() == num_sides);
return directional_vectors;
}
Polygon ConstructConvexPolygon(const size_t num_sides) {
std::vector<double> x_components =
ComputeDirectionalComponents(num_sides, kWidthRange);
std::vector<double> y_components =
ComputeDirectionalComponents(num_sides, kLengthRange);
assert(x_components.size() == num_sides);
assert(y_components.size() == num_sides);
int current_int = 0;
auto increment_int = [¤t_int]() { return current_int++; };
std::vector<size_t> x_sequence(x_components.size());
std::generate_n(x_sequence.begin(), x_sequence.size(), increment_int);
std::vector<size_t> y_sequence = x_sequence;
std::shuffle(
x_sequence.begin(), x_sequence.end(),
std::default_random_engine(
std::chrono::system_clock::now().time_since_epoch().count()));
std::shuffle(
y_sequence.begin(), y_sequence.end(),
std::default_random_engine(
std::chrono::system_clock::now().time_since_epoch().count()));
assert(x_sequence.size() == num_sides);
assert(x_components.size() == num_sides);
// Randomly pair each directional component and compute vectors.
std::vector<Eigen::Vector2d> polygon_sides;
polygon_sides.reserve(x_sequence.size());
for (size_t i = 0; i < num_sides; ++i) {
polygon_sides.emplace_back(x_components[x_sequence[i]],
y_components[y_sequence[i]]);
}
// Compute the angle of each vector with respect to the +y-axis.
std::vector<std::pair<size_t, float>> index_angles;
for (size_t i = 0; i < polygon_sides.size(); ++i) {
float angle = std::atan2(polygon_sides[i].y(), polygon_sides[i].x());
if (angle < 0) {
angle += 2 * M_PI;
}
index_angles.push_back(std::make_pair(i, angle));
}
auto compare_angles = [](const std::pair<size_t, float>& a,
const std::pair<size_t, float>& b) {
return a.second < b.second;
};
std::sort(index_angles.begin(), index_angles.end(), compare_angles);
Polygon polygon(num_sides);
Eigen::Vector2d current_start = Eigen::Vector2d::Zero();
for (size_t i = 0; i < index_angles.size(); ++i) {
polygon[i].first = current_start;
polygon[i].second = current_start + polygon_sides[index_angles[i].first];
current_start = polygon[i].second;
std::cout << polygon[i].first.transpose() << " --> "
<< polygon[i].second.transpose() << std::endl;
}
return polygon;
}
} // namespace geometry
} // namespace primitives