Pogosim
Loading...
Searching...
No Matches
geometry.h
Go to the documentation of this file.
1#ifndef GEOMETRY_H
2#define GEOMETRY_H
3
4#include <SDL2/SDL.h>
5#include <SDL2/SDL_image.h>
6#include <box2d/box2d.h>
7#include <vector>
8#include <string>
9#include <tuple>
10#include <utility>
11#include <cstdint>
12
13typedef std::vector<std::vector<b2Vec2>> arena_polygons_t;
14
15float const VISUALIZATION_SCALE = 100.0f; // 1 Box2D unit = 100 pixels
16
18extern float mm_to_pixels;
20extern float visualization_x;
22extern float visualization_y;
23
32void adjust_mm_to_pixels(float delta);
33
44b2Vec2 visualization_position(float x, float y);
45
55b2Vec2 visualization_position(b2Vec2 pos);
56
76std::vector<std::vector<b2Vec2>> read_poly_from_csv(const std::string& filename, float total_surface);
77
89
90b2Vec2 generate_random_point_within_polygon(const std::vector<b2Vec2>& polygon);
91
103bool is_point_within_polygon(const std::vector<b2Vec2>& polygon, float x, float y);
104
117std::vector<b2Vec2> offset_polygon(const std::vector<b2Vec2>& polygon, float offset);
118
119
139 const std::vector<std::vector<b2Vec2>> &polygons,
140 const std::vector<float> &reserve_radii,
141 float max_neighbor_distance = std::numeric_limits<float>::infinity(),
142 std::uint32_t attempts_per_point = 100U,
143 std::uint32_t max_restarts = 100U);
144
169std::vector<b2Vec2> generate_points_voronoi_lloyd(const std::vector<std::vector<b2Vec2>> &polygons,
170 std::size_t k,
171 std::size_t n_samples = 20'000,
172 std::size_t kmeans_iterations = 20,
173 std::uint32_t max_restarts = 3);
174
175
207std::vector<b2Vec2> generate_random_points_power_lloyd(
208 const std::vector<std::vector<b2Vec2>> &polygons,
209 const std::vector<float> &reserve_radii,
210 std::size_t n_samples = 25'000,
211 std::size_t kmeans_iterations = 25,
212 float convergence_eps = 1e-3f,
213 std::uint32_t max_restarts = 3);
214
215
227std::vector<b2Vec2> generate_random_points_layered(
228 const std::vector<std::vector<b2Vec2>> &polygons,
229 const std::vector<float> &reserve_radii,
230 std::uint32_t attempts_per_point = 1'000U,
231 std::uint32_t max_restarts = 25U);
232
233
261std::vector<b2Vec2> generate_chessboard_points(
262 const std::vector<std::vector<b2Vec2>> &polygons,
263 std::size_t n_points,
264 float pitch,
265 bool cluster_center = false);
266
278std::pair<float, float> compute_polygon_dimensions(const std::vector<b2Vec2>& polygon);
279
290float compute_polygon_area(const std::vector<b2Vec2>& poly);
291
300b2Vec2 polygon_centroid(const std::vector<b2Vec2>& polygon);
301
312float point_to_line_segment_distance(const b2Vec2& p, const b2Vec2& a, const b2Vec2& b);
313
326std::vector<b2Vec2> generate_regular_disk_points_in_polygon( const std::vector<std::vector<b2Vec2>>& polygons, const std::vector<float>& reserve_radii);
327
328
356std::tuple<std::vector<b2Vec2>, std::vector<float>>
357import_points_from_file(const arena_polygons_t& scaled_arena_polygons,
358 const size_t nb_objects,
359 const std::string& formation_filename,
360 const std::pair<float, float>& imported_formation_min_coords,
361 const std::pair<float, float>& imported_formation_max_coords);
362
363
364#endif // GEOMETRY_H
365
366// MODELINE "{{{1
367// vim:expandtab:softtabstop=4:shiftwidth=4:fileencoding=utf-8
368// vim:foldmethod=marker
float visualization_y
Global visualization offset for the y-coordinate.
Definition geometry.cpp:27
float mm_to_pixels
Global scaling factor from millimeters to pixels.
Definition geometry.cpp:25
float visualization_x
Global visualization offset for the x-coordinate.
Definition geometry.cpp:26
std::tuple< std::vector< b2Vec2 >, std::vector< float > > import_points_from_file(const arena_polygons_t &scaled_arena_polygons, const size_t nb_objects, const std::string &formation_filename, const std::pair< float, float > &imported_formation_min_coords, const std::pair< float, float > &imported_formation_max_coords)
Import and rescale robot formation points.
Definition geometry.cpp:1622
float compute_polygon_area(const std::vector< b2Vec2 > &poly)
Computes the area of a polygon using the shoelace formula.
Definition geometry.cpp:1140
std::vector< b2Vec2 > generate_random_points_layered(const std::vector< std::vector< b2Vec2 > > &polygons, const std::vector< float > &reserve_radii, std::uint32_t attempts_per_point=1 '000U, std::uint32_t max_restarts=25U)
Definition geometry.cpp:441
b2Vec2 visualization_position(float x, float y)
Calculates the visualization position for given x and y coordinates.
Definition geometry.cpp:39
b2Vec2 polygon_centroid(const std::vector< b2Vec2 > &polygon)
Computes the centroid of a polygon.
Definition geometry.cpp:1153
std::vector< std::vector< b2Vec2 > > arena_polygons_t
Definition geometry.h:13
float point_to_line_segment_distance(const b2Vec2 &p, const b2Vec2 &a, const b2Vec2 &b)
Calculates the distance from a point to a line segment.
Definition geometry.cpp:1172
std::vector< b2Vec2 > generate_points_voronoi_lloyd(const std::vector< std::vector< b2Vec2 > > &polygons, std::size_t k, std::size_t n_samples=20 '000, std::size_t kmeans_iterations=20, std::uint32_t max_restarts=3)
Definition geometry.cpp:611
std::vector< std::vector< b2Vec2 > > read_poly_from_csv(const std::string &filename, float total_surface)
Reads polygons from a CSV file and scales them to match a specified surface area.
Definition geometry.cpp:50
void adjust_mm_to_pixels(float delta)
Adjusts the global mm_to_pixels scaling factor.
Definition geometry.cpp:30
std::pair< float, float > compute_polygon_dimensions(const std::vector< b2Vec2 > &polygon)
Computes the width and height of a polygon.
Definition geometry.cpp:1121
std::vector< b2Vec2 > generate_regular_disk_points_in_polygon(const std::vector< std::vector< b2Vec2 > > &polygons, const std::vector< float > &reserve_radii)
Place points on (approximate) concentric rings inside polygons[0] so that: • point i stays ≥ reserve_...
Definition geometry.cpp:1198
std::vector< b2Vec2 > generate_chessboard_points(const std::vector< std::vector< b2Vec2 > > &polygons, std::size_t n_points, float pitch, bool cluster_center=false)
Generate a square-grid ("checkerboard") layout with **exactly n_points** nodes inside a (possibly hol...
Definition geometry.cpp:903
b2Vec2 generate_random_point_within_polygon(const std::vector< b2Vec2 > &polygon)
Generates a random point within the specified polygon.
Definition geometry.cpp:140
std::vector< b2Vec2 > generate_random_points_within_polygon_safe(const std::vector< std::vector< b2Vec2 > > &polygons, const std::vector< float > &reserve_radii, float max_neighbor_distance=std::numeric_limits< float >::infinity(), std::uint32_t attempts_per_point=100U, std::uint32_t max_restarts=100U)
Definition geometry.cpp:278
std::vector< b2Vec2 > offset_polygon(const std::vector< b2Vec2 > &polygon, float offset)
Computes an offset polygon.
Definition geometry.cpp:196
bool is_point_within_polygon(const std::vector< b2Vec2 > &polygon, float x, float y)
Determines whether a point is within a polygon.
Definition geometry.cpp:173
std::vector< b2Vec2 > generate_random_points_power_lloyd(const std::vector< std::vector< b2Vec2 > > &polygons, const std::vector< float > &reserve_radii, std::size_t n_samples=25 '000, std::size_t kmeans_iterations=25, float convergence_eps=1e-3f, std::uint32_t max_restarts=3)
Uniformly sample approximately equi-spaced points in a (possibly holed) polygonal domain,...
Definition geometry.cpp:739
float const VISUALIZATION_SCALE
Definition geometry.h:15