Formula Student Autonomous Systems
The code for the main driverless system
Loading...
Searching...
No Matches
grid_geometry.cpp
Go to the documentation of this file.
2
3GridGeometry::GridGeometry(double angle_, double radius_, double start_aug_, double radius_aug_,
4 double fov_)
5 : angle(angle_),
6 radius(radius_),
7 start_augmentation(start_aug_),
8 radius_augmentation(radius_aug_),
9 fov(fov_) {}
10
11int GridGeometry::get_slice_index(double x, double y) const {
12 double angle_deg = std::atan2(y, x) * 180.0 / M_PI; // [-90, 90]
13 angle_deg += fov / 2; // Convert to [0, fov]
14
15 if (angle_deg < 0.0 || angle_deg >= fov) {
16 return -1; // Out of FOV
17 }
18
19 return static_cast<int>(std::floor(angle_deg / angle));
20}
21
22int GridGeometry::get_bin_index(double x, double y) const {
23 double distance = std::sqrt(x * x + y * y);
24 int bin_idx = 0;
25
26 if (distance < start_augmentation || radius_augmentation == 0.0) {
27 bin_idx = static_cast<int>(std::floor(distance / radius));
28 } else {
29 const double d = distance - start_augmentation;
30 const double a = radius_augmentation / 2.0;
31 const double b = radius + radius_augmentation / 2.0;
32 const double disc = b * b + 4.0 * a * d;
33 const double n_pos = (-b + std::sqrt(disc)) / (2.0 * a);
34 const int n = static_cast<int>(std::floor(n_pos)) + 1;
35 bin_idx = static_cast<int>(std::floor(start_augmentation / radius)) + (n - 1);
36 }
37
38 return std::max(bin_idx, 0);
39}
40
42 return std::max(1, static_cast<int>(std::ceil(fov / angle)));
43}
44
45int GridGeometry::get_num_bins(double range) const {
46 int num_bins = 0;
47 if (radius_augmentation == 0.0 || range <= start_augmentation) {
48 num_bins = static_cast<int>(std::ceil(range / radius));
49 } else {
50 int base_bins = static_cast<int>(std::floor(start_augmentation / radius));
51 num_bins = base_bins;
52
53 double dist_remaining = range - start_augmentation;
54 double current_bin_size = radius;
55
56 while (dist_remaining > 0) {
57 dist_remaining -= current_bin_size;
58 if (dist_remaining >= 0) {
59 num_bins++;
60 }
61 current_bin_size += radius_augmentation;
62 }
63 }
64 return num_bins;
65}
double radius_augmentation
int get_bin_index(double x, double y) const
Compute bin index for a given (x, y)
int get_slice_index(double x, double y) const
Compute slice index for a given (x, y)
double start_augmentation
GridGeometry(double angle_, double radius_, double start_aug_, double radius_aug_, double fov_)
Constructor to initialize grid geometry parameters.
int get_num_slices() const
Get the number of slices in the grid.
int get_num_bins(double range) const
Get the number of bins for a given range.