-
Notifications
You must be signed in to change notification settings - Fork 0
/
world.h
128 lines (104 loc) · 4.34 KB
/
world.h
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
#ifndef WORLD_H
#define WORLD_H
#include <stdbool.h>
#include <math.h>
#define WORLD_PADDING
/// The maximum amount of border points.
#define MAX_BORDER_POINTS 256
/// The amount of clear area points to use.
#define WORLD_CLEAR_POINTS 1024
/// The amount of occupied area points to use.
#define WORLD_OCCUPIED_POINTS 1024
/// The max amount of targets to track.
#define WORLD_TARGET_POINTS_MAX 32
#define WORLD_IR_SENSOR_POINTS 5
/// The amount of rows in which distance is checked between objects to reduce computation time.
#define ADD_EVICT_ROWS 512
#define MIN_INTERSECTION_DENOM 0.000001f
#define CLOSE_EVICT_OCCUPIED 0.01f
#define CLOSE_EVICT_CLEAR 0.01f
typedef struct {
float x, y;
} AbsolutePoint;
/// Used for the world to produce output points with different variances.
typedef struct {
AbsolutePoint p;
/// Variance of the absolute point.
float v;
char pad[4];
} VariancePoint;
/// Used for the world to produce output points with different variances.
typedef struct {
VariancePoint vp;
float angle, av;
} OrientPoint;
/// Used for the border of any world line.
typedef struct {
AbsolutePoint p0, p1;
} WorldBorder;
static inline float speedy_square(float n) {
return n * n;
}
/// Compute the distance squared between two AbsolutePoint objects.
static inline float point_distance_squared(AbsolutePoint p0, AbsolutePoint p1) {
return speedy_square(p0.x - p1.x) + speedy_square(p0.y - p1.y);
}
/// Compute the distance squared between two AbsolutePoint objects.
static inline float delta_distance_squared(AbsolutePoint delta) {
return speedy_square(delta.x) + speedy_square(delta.y);
}
/// Compute the delta vector between two AbsolutePoint objects.
static inline AbsolutePoint point_delta(AbsolutePoint *from, AbsolutePoint *to) {
AbsolutePoint p = {to->x - from->x, to->y - from->y};
return p;
}
/// Get the delta from an angle and a magnitude.
static inline AbsolutePoint angle_delta(float angle, float mag) {
AbsolutePoint p = {mag * cosf(angle), mag * sinf(angle)};
return p;
}
static inline bool between(float start, float end, float p) {
if (start < end) {
return p > start && p < end;
} else {
return p > end && p < start;
}
return false;
}
static inline float vector_dot(AbsolutePoint a, AbsolutePoint b) {
return a.x * b.x + a.y * b.y;
}
/// Get the point at which two lines (specified by their endpoints) intersect.
/// If null is passed only checks for intersection.
bool intersection_point(AbsolutePoint a0, AbsolutePoint a1, AbsolutePoint b0,
AbsolutePoint b1, AbsolutePoint *intersection);
/// Returns the closest point on the line.
AbsolutePoint point_projection(AbsolutePoint p, AbsolutePoint l0, AbsolutePoint l1);
/// Choose which point to evict from an array of points when it gets full.
void add_evict(VariancePoint *points, unsigned *current, unsigned max, unsigned *evict_row, VariancePoint npoint);
/// Run to init the world model.
void world_init(OrientPoint rover_a, unsigned num_targets, float initial_target_spawn_radius,
AbsolutePoint *borders, unsigned total_border_points);
/// Add an ir sensor reading from the front-right-facing IR sensor on rover A.
void world_add_front_right_ir_sensor_reading(float distance);
/// Add an ir sensor reading from the front-left-facing IR sensor on rover A.
void world_add_front_left_ir_sensor_reading(float distance);
/// Add an ir sensor reading from the left-facing IR sensor on rover A.
void world_add_left_ir_sensor_reading(float distance);
/// Add movement from rover A's movement module.
void world_update_movement(OrientPoint movement);
/// Signify that the rover is aligned.
void world_rover_aligned();
/// Cycle the world.
void world_update(void);
/// Returns the number of points and assigns to *points the array of points.
unsigned world_retrieve_arena_border_points(AbsolutePoint **points);
/// Returns the number of points and assigns to *points the array of points.
unsigned world_retrieve_clear_points(VariancePoint **points);
/// Returns the number of points and assigns to *points the array of points.
unsigned world_retrieve_occupied_points(VariancePoint **points);
/// Always succeeds since rover A is always on the world.
OrientPoint* world_retrieve_rover(void);
/// Returns the number of targets retrieved.
unsigned world_retrieve_targets(VariancePoint **targets);
#endif // WORLD_H