YODAU 1.0
YEAR OF THE DEPEND ADULT UNDERGARMENT
Loading...
Searching...
No Matches
geometry.hpp
Go to the documentation of this file.
1#ifndef YODAU_BACKEND_GEOMETRY_HPP
2#define YODAU_BACKEND_GEOMETRY_HPP
3
4#include <memory>
5#include <ostream>
6#include <string>
7#include <string_view>
8#include <vector>
9
10namespace yodau::backend {
11
12/**
13 * @brief Point in percentage-based image coordinates.
14 *
15 * Coordinates are expressed in the range [0.0; 100.0], where:
16 * - x = 0 is the left edge, x = 100 is the right edge
17 * - y = 0 is the top edge, y = 100 is the bottom edge
18 */
19struct point {
20 /**
21 * @brief Horizontal coordinate (percentage of width).
22 */
23 float x {}; // percentage [0.0; 100.0]
24
25 /**
26 * @brief Vertical coordinate (percentage of height).
27 */
28 float y {}; // percentage [0.0; 100.0]
29
30 /**
31 * @brief Tolerance used for fuzzy point comparisons.
32 */
33 static constexpr float epsilon { 0.001f };
34
35 /**
36 * @brief Compute Euclidean distance to another point.
37 *
38 * Distance is computed in percentage units.
39 *
40 * @param other Target point.
41 * @return Euclidean distance between this and @p other.
42 */
43 float distance_to(const point& other) const;
44
45 /**
46 * @brief Compare two points with tolerance @ref epsilon.
47 *
48 * The comparison is component-wise:
49 * @code
50 * abs(x - other.x) < epsilon && abs(y - other.y) < epsilon
51 * @endcode
52 *
53 * @param other Point to compare to.
54 * @return true if points are equal within tolerance.
55 */
56 bool compare(const point& other) const;
57};
58
59/**
60 * @brief Allowed crossing direction for a tripwire.
61 */
62enum class tripwire_dir {
63 /** Direction is not constrained; any crossing counts. */
65 /** Crossing from negative side to positive side counts. */
67 /** Crossing from positive side to negative side counts. */
69};
70
71/**
72 * @brief Polyline / polygon described in percentage coordinates.
73 *
74 * A line can represent:
75 * - an open polyline (when @ref closed == false),
76 * - or a closed polygon-like chain (when @ref closed == true).
77 *
78 * The points may be reordered by @ref normalize() to provide a canonical
79 * representation for equality checks and stable processing.
80 */
81struct line {
82 /**
83 * @brief Logical name of the line (e.g., "entrance_tripwire").
84 */
85 std::string name;
86
87 /**
88 * @brief Vertex list in percentage coordinates.
89 */
91
92 /**
93 * @brief Whether the chain is closed.
94 *
95 * If true, the first point is considered connected to the last point.
96 */
97 bool closed { false };
98
99 /**
100 * @brief Optional tripwire direction constraint.
101 *
102 * Used by line-crossing logic. Defaults to @ref tripwire_dir::any.
103 */
105
106 /**
107 * @brief Print a human-readable representation of the line.
108 *
109 * @param out Output stream to write to.
110 */
111 void dump(std::ostream& out) const;
112
113 /**
114 * @brief Canonicalize point order.
115 *
116 * Behavior (as per implementation):
117 * - If @ref closed is true, rotates points so the vertex closest to (0,0)
118 * becomes the first element.
119 * - Then ensures a consistent direction by comparing distances of
120 * first/last vertices to a reference point and reversing if needed.
121 *
122 * @note This operation may reorder @ref points but does not change
123 * their values.
124 */
125 void normalize();
126
127 /**
128 * @brief Equality check using canonical point comparison.
129 *
130 * Two lines are equal if:
131 * - their @ref closed flags match,
132 * - they have the same number of points,
133 * - and all points compare equal via @ref point::compare.
134 *
135 * @note The @ref name and @ref dir fields are intentionally NOT compared.
136 *
137 * @param other Line to compare with.
138 * @return true if geometrically equal within tolerance.
139 */
140 bool operator==(const line& other) const;
141};
142
143/**
144 * @brief Shared, immutable line pointer.
145 */
146using line_ptr = std::shared_ptr<line const>;
147
148/**
149 * @brief Create and normalize a line.
150 *
151 * Allocates a new @ref line, moves in @p points and @p name, sets @p closed,
152 * and calls @ref line::normalize().
153 *
154 * @param points Vertex list to move into the line.
155 * @param name Logical name to move into the line.
156 * @param closed Whether the line should be treated as closed.
157 * @return A shared pointer to an immutable line instance.
158 */
160make_line(std::vector<point> points, std::string name, bool closed = false);
161
162/**
163 * @brief Parse points from a textual representation.
164 *
165 * Input format:
166 * - Points are separated by semicolons ';'
167 * - Each point is "x,y"
168 * - Whitespace and parentheses are ignored.
169 *
170 * Examples:
171 * - "(10, 20); (30,40)"
172 * - "10,20;30,40; 50, 60"
173 *
174 * @param points_str Input string containing points.
175 * @return Parsed list of points.
176 * @throws std::runtime_error if parsing fails or no valid points are found.
177 */
178std::vector<point> parse_points(const std::string& points_str);
179
180/**
181 * @brief Remove whitespace and parentheses from a string.
182 *
183 * Used internally to simplify parsing of point lists.
184 *
185 * @param str Input string view.
186 * @return Normalized string with spaces and '(' / ')' removed.
187 */
188std::string normalize_str(std::string_view str);
189
190/**
191 * @brief Parse a float from a string view.
192 *
193 * Uses std::from_chars for locale-independent parsing.
194 *
195 * @param num_str Number representation.
196 * @return Parsed float value.
197 * @throws std::runtime_error if the input is not a valid float.
198 */
199float parse_float(std::string_view num_str);
200
201} // namespace yodau::backend
202
203#endif // YODAU_BACKEND_GEOMETRY_HPP
Simple interactive CLI (REPL) for controlling a stream_manager.
void cmd_start_stream(const std::vector< std::string > &args) const
Handler for start-stream.
void dispatch_command(const std::string &cmd, const std::vector< std::string > &args) const
Dispatch a command to its handler.
void cmd_set_line(const std::vector< std::string > &args) const
Handler for set-line.
int run() const
Run the interactive command loop.
void cmd_add_line(const std::vector< std::string > &args) const
Handler for add-line.
static cxxopts::ParseResult parse_with_cxxopts(const std::string &cmd, const std::vector< std::string > &args, cxxopts::Options &options)
Parse command arguments using cxxopts.
backend::stream_manager & stream_mgr
Stream manager controlled by this CLI.
void cmd_stop_stream(const std::vector< std::string > &args) const
Handler for stop-stream.
void cmd_list_streams(const std::vector< std::string > &args) const
Handler for list-streams.
void cmd_add_stream(const std::vector< std::string > &args) const
Handler for add-stream.
void cmd_list_lines(const std::vector< std::string > &args) const
Handler for list-lines.
cli_client(backend::stream_manager &mgr)
Construct a CLI client operating on an existing manager.
Definition cli_client.cpp:5
static std::vector< std::string > tokenize(const std::string &line)
Split a line into whitespace-separated tokens.
Central coordinator for streams, geometry, frame processing and events.
std::function< void(const std::string &stream_name, frame &&f)> manual_push_fn
Hook for manual frame pushing.
Represents a single video stream and its analytic connections.
Definition stream.hpp:64
static yodau::backend::tripwire_dir parse_tripwire_dir(const std::string &s)
line_ptr make_line(std::vector< point > points, std::string name, bool closed=false)
Create and normalize a line.
Definition geometry.cpp:82
std::string normalize_str(std::string_view str)
Remove whitespace and parentheses from a string.
Definition geometry.cpp:133
std::vector< point > parse_points(const std::string &points_str)
Parse points from a textual representation.
Definition geometry.cpp:94
float parse_float(std::string_view num_str)
Parse a float from a string view.
Definition geometry.cpp:145
std::shared_ptr< line const > line_ptr
Shared, immutable line pointer.
Definition geometry.hpp:146
event_kind
High-level classification of backend events.
Definition event.hpp:19
tripwire_dir
Allowed crossing direction for a tripwire.
Definition geometry.hpp:62
Generic event produced by the backend.
Definition event.hpp:44
std::chrono::steady_clock::time_point ts
Monotonic timestamp when the event was generated.
Definition event.hpp:70
event_kind kind
Type of the event.
Definition event.hpp:50
std::string stream_name
Name/identifier of the stream that produced the event.
Definition event.hpp:57
std::optional< point > pos_pct
Optional position associated with the event in percentage coordinates.
Definition event.hpp:79
std::string line_name
Name of the line / ROI / rule responsible for this event.
Definition event.hpp:86
std::string message
Human-readable event description or payload.
Definition event.hpp:65
Video frame container.
Definition frame.hpp:44
Polyline / polygon described in percentage coordinates.
Definition geometry.hpp:81
std::string name
Logical name of the line (e.g., "entrance_tripwire").
Definition geometry.hpp:85
tripwire_dir dir
Optional tripwire direction constraint.
Definition geometry.hpp:104
bool closed
Whether the chain is closed.
Definition geometry.hpp:97
void dump(std::ostream &out) const
Print a human-readable representation of the line.
Definition geometry.cpp:17
std::vector< point > points
Vertex list in percentage coordinates.
Definition geometry.hpp:90
void normalize()
Canonicalize point order.
Definition geometry.cpp:29
bool operator==(const line &other) const
Equality check using canonical point comparison.
Definition geometry.cpp:70
Point in percentage-based image coordinates.
Definition geometry.hpp:19
float distance_to(const point &other) const
Compute Euclidean distance to another point.
Definition geometry.cpp:7
float x
Horizontal coordinate (percentage of width).
Definition geometry.hpp:23
float y
Vertical coordinate (percentage of height).
Definition geometry.hpp:28
static constexpr float epsilon
Tolerance used for fuzzy point comparisons.
Definition geometry.hpp:33
bool compare(const point &other) const
Compare two points with tolerance epsilon.
Definition geometry.cpp:13