PER Firmware
Loading...
Searching...
No Matches
geometry.h
Go to the documentation of this file.
1#ifndef GEOMETRY_H
2#define GEOMETRY_H
3
15#include <stdbool.h>
16
17#include "utils/abs.h"
18#include "utils/vector2.h"
19#include "utils/max.h"
20#include "utils/min.h"
21
22static constexpr float GEOMETRY_EPSILON = 1e-6f;
23
28
34
43static inline winding_t vector2_winding(const vector2_t a, const vector2_t b, const vector2_t c) {
44 float cross = vector2_cross(vector2_sub(b, a), vector2_sub(c, a));
45
46 if (ABS(cross) <= GEOMETRY_EPSILON) {
47 return WINDING_COLLINEAR;
48 }
49 return (cross > 0.0f) ? WINDING_COUNTERCLOCKWISE : WINDING_CLOCKWISE;
50}
51
58static inline float segment2_length(const segment2_t segment) {
59 return vector2_distance(segment.start, segment.end);
60}
61
72static inline bool segment2_contains_bounds(const segment2_t segment, const vector2_t point) {
73 return (point.x <= MAXOF(segment.start.x, segment.end.x) + GEOMETRY_EPSILON)
74 && (point.x >= MINOF(segment.start.x, segment.end.x) - GEOMETRY_EPSILON)
75 && (point.y <= MAXOF(segment.start.y, segment.end.y) + GEOMETRY_EPSILON)
76 && (point.y >= MINOF(segment.start.y, segment.end.y) - GEOMETRY_EPSILON);
77}
78
86static inline bool segment2_intersects(const segment2_t a, const segment2_t b) {
87 winding_t b_start_vs_a = vector2_winding(a.start, a.end, b.start);
88 winding_t b_end_vs_a = vector2_winding(a.start, a.end, b.end);
89 winding_t a_start_vs_b = vector2_winding(b.start, b.end, a.start);
90 winding_t a_end_vs_b = vector2_winding(b.start, b.end, a.end);
91
92 // collinear endpoints only touch if they land within the other segment
93 if (b_start_vs_a == WINDING_COLLINEAR && segment2_contains_bounds(a, b.start)) {
94 return true;
95 }
96 if (b_end_vs_a == WINDING_COLLINEAR && segment2_contains_bounds(a, b.end)) {
97 return true;
98 }
99 if (a_start_vs_b == WINDING_COLLINEAR && segment2_contains_bounds(b, a.start)) {
100 return true;
101 }
102 if (a_end_vs_b == WINDING_COLLINEAR && segment2_contains_bounds(b, a.end)) {
103 return true;
104 }
105
106 // otherwise they cross only if each segment straddles the other
107 return (b_start_vs_a != b_end_vs_a) && (a_start_vs_b != a_end_vs_b);
108}
109
118 const segment2_t segment,
119 const float half_length
120) {
121 vector2_t direction = vector2_sub(segment.end, segment.start);
122 vector2_t offset = vector2_scale(
124 half_length
125 );
126 vector2_t midpoint = vector2_midpoint(segment.start, segment.end);
127
128 return (segment2_t){
129 .start = vector2_sub(midpoint, offset),
130 .end = vector2_add(midpoint, offset),
131 };
132}
133
134#endif // GEOMETRY_H
Absolute value functions utility header.
#define ABS(x)
Definition abs.h:24
static float segment2_length(const segment2_t segment)
Calculate the length of a segment.
Definition geometry.h:58
static winding_t vector2_winding(const vector2_t a, const vector2_t b, const vector2_t c)
Determine which way the path a -> b -> c turns.
Definition geometry.h:43
static bool segment2_contains_bounds(const segment2_t segment, const vector2_t point)
Test whether a point falls inside a segment's bounding box.
Definition geometry.h:72
winding_t
Definition geometry.h:29
@ WINDING_COUNTERCLOCKWISE
Definition geometry.h:32
@ WINDING_COLLINEAR
Definition geometry.h:30
@ WINDING_CLOCKWISE
Definition geometry.h:31
static bool segment2_intersects(const segment2_t a, const segment2_t b)
Test whether two segments intersect, including collinear overlap.
Definition geometry.h:86
static constexpr float GEOMETRY_EPSILON
Definition geometry.h:22
static segment2_t segment2_perpendicular_bisector(const segment2_t segment, const float half_length)
Build the segment perpendicular to a segment through its midpoint.
Definition geometry.h:117
Maximum value functions utility header.
#define MAXOF(...)
Definition max.h:42
Minimum value functions utility header.
#define MINOF(...)
Definition min.h:42
Definition geometry.h:24
vector2_t end
Definition geometry.h:26
vector2_t start
Definition geometry.h:25
Definition vector2.h:15
float x
Definition vector2.h:16
float y
Definition vector2.h:17
2D vector types and operations.
static vector2_t vector2_perpendicular(const vector2_t vec)
Rotate a 2D vector 90 degrees counterclockwise.
Definition vector2.h:139
static vector2_t vector2_sub(const vector2_t a, const vector2_t b)
Subtract one 2D vector from another.
Definition vector2.h:39
static float vector2_cross(const vector2_t a, const vector2_t b)
Cross product of two 2D vectors.
Definition vector2.h:77
static vector2_t vector2_scale(const vector2_t vec, const float scalar)
Scale a 2D vector by a scalar.
Definition vector2.h:51
static vector2_t vector2_normalize(const vector2_t vec)
Normalize a 2D vector.
Definition vector2.h:97
static float vector2_distance(const vector2_t a, const vector2_t b)
Distance between two 2D points.
Definition vector2.h:118
static vector2_t vector2_midpoint(const vector2_t a, const vector2_t b)
Midpoint of two 2D points.
Definition vector2.h:129
static vector2_t vector2_add(const vector2_t a, const vector2_t b)
Add two 2D vectors.
Definition vector2.h:27