Pogosim
Loading...
Searching...
No Matches
objects_geometry.h
Go to the documentation of this file.
1#ifndef OBJECTS_GEOMETRY_H
2#define OBJECTS_GEOMETRY_H
3
4#include <functional>
5#include <array>
6
7#include "utils.h"
8#include "configuration.h"
9#include "render.h"
10#include "colormaps.h"
11
12
13class Simulation;
14
19 float center_x;
20 float center_y;
21 float radius;
22};
23
28 float x;
29 float y;
30 float width;
31 float height;
32};
33
34
35
41public:
42
47
51 virtual ~ObjectGeometry();
52
56 virtual void create_box2d_shape(b2BodyId body_id, b2ShapeDef& shape_def) = 0;
57
61 b2ShapeId get_shape_id() const { return shape_id; }
62
74 virtual std::vector<std::vector<bool>> export_geometry_grid(size_t num_bins_x,
75 size_t num_bins_y,
76 float bin_width,
77 float bin_height,
78 float obj_x,
79 float obj_y) const = 0;
80
93 virtual void render(SDL_Renderer* renderer, b2WorldId world_id, float x, float y, float theta,
94 uint8_t r, uint8_t g, uint8_t b, uint8_t alpha = 255) const = 0;
95
102
109
113 virtual float get_distance_to(b2Vec2 orig, b2Vec2 point) const;
114
127 virtual arena_polygons_t generate_contours(std::size_t points_per_contour = 0, b2Vec2 position = {0.0f, 0.0f}) const = 0;
128
129protected:
130 bool shape_created = false;
131 b2ShapeId shape_id;
132};
133
139public:
140
144 DiskGeometry(float _radius) : radius(_radius) {}
145
149 virtual void create_box2d_shape(b2BodyId body_id, b2ShapeDef& shape_def) override;
150
154 float get_radius() const { return radius; }
155
167 virtual std::vector<std::vector<bool>> export_geometry_grid(size_t num_bins_x,
168 size_t num_bins_y,
169 float bin_width,
170 float bin_height,
171 float obj_x,
172 float obj_y) const override;
173
186 virtual void render(SDL_Renderer* renderer, b2WorldId world_id, float x, float y, float theta,
187 uint8_t r, uint8_t g, uint8_t b, uint8_t alpha = 255) const override;
188
194 virtual BoundingDisk compute_bounding_disk() const override;
195
201 virtual BoundingBox compute_bounding_box() const override;
202
215 virtual arena_polygons_t generate_contours(std::size_t points_per_contour = 0, b2Vec2 position = {0.0f, 0.0f}) const override;
216
217protected:
218 float radius;
219};
220
222public:
228 RectangleGeometry(float _width, float _height) : width(_width), height(_height) {}
229
233 virtual void create_box2d_shape(b2BodyId body_id, b2ShapeDef& shape_def) override;
234
246 virtual std::vector<std::vector<bool>> export_geometry_grid(size_t num_bins_x,
247 size_t num_bins_y,
248 float bin_width,
249 float bin_height,
250 float obj_x,
251 float obj_y) const override;
252
265 virtual void render(SDL_Renderer* renderer, b2WorldId world_id, float x, float y, float theta,
266 uint8_t r, uint8_t g, uint8_t b, uint8_t alpha = 255) const override;
267
271 float get_width() const { return width; }
272
276 float get_height() const { return height; }
277
283 virtual BoundingDisk compute_bounding_disk() const override;
284
290 virtual BoundingBox compute_bounding_box() const override;
291
304 virtual arena_polygons_t generate_contours(std::size_t points_per_contour = 0, b2Vec2 position = {0.0f, 0.0f}) const override;
305
306protected:
307 float width;
308 float height;
309};
310
311
313public:
321 explicit TriangleGeometry(float _side_length) : side_length(_side_length) {}
322
326 void create_box2d_shape(b2BodyId body_id, b2ShapeDef& shape_def) override;
327
331 std::vector<std::vector<bool>> export_geometry_grid(size_t num_bins_x,
332 size_t num_bins_y,
333 float bin_width,
334 float bin_height,
335 float obj_x,
336 float obj_y) const override;
337
341 void render(SDL_Renderer* renderer, b2WorldId world_id, float x, float y, float theta,
342 uint8_t r, uint8_t g, uint8_t b, uint8_t alpha = 255) const override;
343
347 float get_side_length() const { return side_length; }
348
352 BoundingDisk compute_bounding_disk() const override;
353
357 BoundingBox compute_bounding_box() const override;
358
363 arena_polygons_t generate_contours(std::size_t points_per_contour = 0, b2Vec2 position = {0.0f, 0.0f}) const override;
364
365private:
370 std::array<b2Vec2, 3> get_local_vertices() const;
371
372 float side_length;
373};
374
375
380class GlobalGeometry final : public ObjectGeometry {
381public:
386
390 virtual void create_box2d_shape(b2BodyId, b2ShapeDef&) override {};
391
403 virtual std::vector<std::vector<bool>> export_geometry_grid(size_t num_bins_x,
404 size_t num_bins_y,
405 float bin_width,
406 float bin_height,
407 float obj_x,
408 float obj_y) const override;
409
422 virtual void render(SDL_Renderer*, b2WorldId, float, float, float, uint8_t, uint8_t, uint8_t, uint8_t = 255) const override {}
423
429 virtual BoundingDisk compute_bounding_disk() const override;
430
436 virtual BoundingBox compute_bounding_box() const override;
437
441 virtual float get_distance_to([[maybe_unused]] b2Vec2 orig, [[maybe_unused]] b2Vec2 point) const override { return 0.0f; }
442
455 virtual arena_polygons_t generate_contours([[maybe_unused]] std::size_t points_per_contour = 0, [[maybe_unused]] b2Vec2 position = {0.0f, 0.0f}) const override { return {}; }
456};
457
458
464class ArenaGeometry final : public ObjectGeometry {
465public:
467 explicit ArenaGeometry(arena_polygons_t const& arena_polygons) noexcept
468 : arena_polygons_{arena_polygons} {}
469
470 /* ---------- ObjectGeometry interface ---------- */
471
472 void create_box2d_shape([[maybe_unused]] b2BodyId body_id, [[maybe_unused]] b2ShapeDef& shape_def) override { }
473
474 std::vector<std::vector<bool>>
475 export_geometry_grid(std::size_t num_bins_x,
476 std::size_t num_bins_y,
477 float bin_width,
478 float bin_height,
479 float obj_x,
480 float obj_y) const override;
481
482 void render(SDL_Renderer*, b2WorldId, float, float, float,
483 uint8_t, uint8_t, uint8_t, uint8_t = 255) const override { }
484
485 BoundingDisk compute_bounding_disk() const override;
486 BoundingBox compute_bounding_box() const override;
487
494 float get_distance_to(b2Vec2 /*orig*/, b2Vec2 point) const override;
495
496 arena_polygons_t const& get_arena_polygons() { return arena_polygons_; }
497
510 virtual arena_polygons_t generate_contours(std::size_t points_per_contour = 0, b2Vec2 position = {0.0f, 0.0f}) const override;
511
512private:
513 static float distance_point_segment(b2Vec2 p, b2Vec2 a, b2Vec2 b) noexcept;
514 static bool point_inside_polygon(b2Vec2 p, const std::vector<b2Vec2>& poly) noexcept;
515
516 const arena_polygons_t& arena_polygons_;
517};
518
519
527
528
529#endif
530
531// MODELINE "{{{1
532// vim:expandtab:softtabstop=4:shiftwidth=4:fileencoding=utf-8
533// vim:foldmethod=marker
void render(SDL_Renderer *, b2WorldId, float, float, float, uint8_t, uint8_t, uint8_t, uint8_t=255) const override
Renders the object on the given SDL renderer.
Definition objects_geometry.h:482
arena_polygons_t const & get_arena_polygons()
Definition objects_geometry.h:496
float get_distance_to(b2Vec2, b2Vec2 point) const override
Return the shortest Euclidean distance between point and all arena walls.
Definition objects_geometry.cpp:536
void create_box2d_shape(b2BodyId body_id, b2ShapeDef &shape_def) override
Create Box2D shape based on this geometry.
Definition objects_geometry.h:472
virtual arena_polygons_t generate_contours(std::size_t points_per_contour=0, b2Vec2 position={0.0f, 0.0f}) const override
Return one or more polygonal contours that approximate / represent this geometry.
Definition objects_geometry.cpp:552
BoundingBox compute_bounding_box() const override
Computes the axis-aligned bounding box that completely encloses the geometry.
Definition objects_geometry.cpp:511
ArenaGeometry(arena_polygons_t const &arena_polygons) noexcept
Ctor – keeps a ref to the polygon container that lives elsewhere.
Definition objects_geometry.h:467
BoundingDisk compute_bounding_disk() const override
Computes the bounding disk that completely encloses the geometry.
Definition objects_geometry.cpp:528
std::vector< std::vector< bool > > export_geometry_grid(std::size_t num_bins_x, std::size_t num_bins_y, float bin_width, float bin_height, float obj_x, float obj_y) const override
Definition objects_geometry.cpp:485
Class for managing hierarchical configuration parameters.
Definition configuration.h:64
virtual BoundingDisk compute_bounding_disk() const override
Computes the bounding disk that completely encloses the geometry.
Definition objects_geometry.cpp:69
float get_radius() const
Return radius of the disk.
Definition objects_geometry.h:154
DiskGeometry(float _radius)
Construct an ObjectGeometry.
Definition objects_geometry.h:144
virtual std::vector< std::vector< bool > > export_geometry_grid(size_t num_bins_x, size_t num_bins_y, float bin_width, float bin_height, float obj_x, float obj_y) const override
Exports a boolean 2D grid showing which bins are covered by the geometry.
Definition objects_geometry.cpp:40
virtual void render(SDL_Renderer *renderer, b2WorldId world_id, float x, float y, float theta, uint8_t r, uint8_t g, uint8_t b, uint8_t alpha=255) const override
Renders the object on the given SDL renderer.
Definition objects_geometry.cpp:65
virtual arena_polygons_t generate_contours(std::size_t points_per_contour=0, b2Vec2 position={0.0f, 0.0f}) const override
Return one or more polygonal contours that approximate / represent this geometry.
Definition objects_geometry.cpp:80
virtual BoundingBox compute_bounding_box() const override
Computes the axis-aligned bounding box that completely encloses the geometry.
Definition objects_geometry.cpp:74
float radius
Radius of the disk.
Definition objects_geometry.h:218
virtual void create_box2d_shape(b2BodyId body_id, b2ShapeDef &shape_def) override
Create Box2D shape based on this geometry.
Definition objects_geometry.cpp:32
virtual void create_box2d_shape(b2BodyId, b2ShapeDef &) override
Create Box2D shape based on this geometry.
Definition objects_geometry.h:390
virtual float get_distance_to(b2Vec2 orig, b2Vec2 point) const override
Compute the distance from a given point to the geometry.
Definition objects_geometry.h:441
virtual BoundingDisk compute_bounding_disk() const override
Computes the bounding disk that completely encloses the geometry.
Definition objects_geometry.cpp:438
GlobalGeometry()
Construct an ObjectGeometry.
Definition objects_geometry.h:385
virtual void render(SDL_Renderer *, b2WorldId, float, float, float, uint8_t, uint8_t, uint8_t, uint8_t=255) const override
Renders the object on the given SDL renderer.
Definition objects_geometry.h:422
virtual BoundingBox compute_bounding_box() const override
Computes the axis-aligned bounding box that completely encloses the geometry.
Definition objects_geometry.cpp:442
virtual std::vector< std::vector< bool > > export_geometry_grid(size_t num_bins_x, size_t num_bins_y, float bin_width, float bin_height, float obj_x, float obj_y) const override
Exports a boolean 2D grid showing which bins are covered by the geometry.
Definition objects_geometry.cpp:429
virtual arena_polygons_t generate_contours(std::size_t points_per_contour=0, b2Vec2 position={0.0f, 0.0f}) const override
Return one or more polygonal contours that approximate / represent this geometry.
Definition objects_geometry.h:455
Geometry of an object.
Definition objects_geometry.h:40
virtual BoundingDisk compute_bounding_disk() const =0
Computes the bounding disk that completely encloses the geometry.
virtual std::vector< std::vector< bool > > export_geometry_grid(size_t num_bins_x, size_t num_bins_y, float bin_width, float bin_height, float obj_x, float obj_y) const =0
Exports a boolean 2D grid showing which bins are covered by the geometry.
virtual void render(SDL_Renderer *renderer, b2WorldId world_id, float x, float y, float theta, uint8_t r, uint8_t g, uint8_t b, uint8_t alpha=255) const =0
Renders the object on the given SDL renderer.
b2ShapeId get_shape_id() const
Return Box2D shape_id.
Definition objects_geometry.h:61
ObjectGeometry()
Construct an ObjectGeometry.
Definition objects_geometry.h:46
b2ShapeId shape_id
Box2D shape identifier.
Definition objects_geometry.h:131
virtual ~ObjectGeometry()
Destructor.
Definition objects_geometry.cpp:21
virtual float get_distance_to(b2Vec2 orig, b2Vec2 point) const
Compute the distance from a given point to the geometry.
Definition objects_geometry.cpp:26
bool shape_created
Definition objects_geometry.h:130
virtual BoundingBox compute_bounding_box() const =0
Computes the axis-aligned bounding box that completely encloses the geometry.
virtual arena_polygons_t generate_contours(std::size_t points_per_contour=0, b2Vec2 position={0.0f, 0.0f}) const =0
Return one or more polygonal contours that approximate / represent this geometry.
virtual void create_box2d_shape(b2BodyId body_id, b2ShapeDef &shape_def)=0
Create Box2D shape based on this geometry.
float width
Width of the rectangle.
Definition objects_geometry.h:307
virtual BoundingBox compute_bounding_box() const override
Computes the axis-aligned bounding box that completely encloses the geometry.
Definition objects_geometry.cpp:194
virtual arena_polygons_t generate_contours(std::size_t points_per_contour=0, b2Vec2 position={0.0f, 0.0f}) const override
Return one or more polygonal contours that approximate / represent this geometry.
Definition objects_geometry.cpp:199
virtual BoundingDisk compute_bounding_disk() const override
Computes the bounding disk that completely encloses the geometry.
Definition objects_geometry.cpp:185
RectangleGeometry(float _width, float _height)
Construct a RectangleGeometry.
Definition objects_geometry.h:228
virtual std::vector< std::vector< bool > > export_geometry_grid(size_t num_bins_x, size_t num_bins_y, float bin_width, float bin_height, float obj_x, float obj_y) const override
Exports a boolean 2D grid showing which bins are covered by the rectangle.
Definition objects_geometry.cpp:113
float get_width() const
Returns the width of the rectangle.
Definition objects_geometry.h:271
virtual void render(SDL_Renderer *renderer, b2WorldId world_id, float x, float y, float theta, uint8_t r, uint8_t g, uint8_t b, uint8_t alpha=255) const override
Renders the rectangle on the given SDL renderer.
Definition objects_geometry.cpp:147
float get_height() const
Returns the height of the rectangle.
Definition objects_geometry.h:276
virtual void create_box2d_shape(b2BodyId body_id, b2ShapeDef &shape_def) override
Create a Box2D shape based on this geometry.
Definition objects_geometry.cpp:102
float height
Height of the rectangle.
Definition objects_geometry.h:308
Class representing the simulation environment.
Definition simulator.h:43
BoundingDisk compute_bounding_disk() const override
Computes the bounding disk that completely encloses the geometry.
Definition objects_geometry.cpp:364
std::vector< std::vector< bool > > export_geometry_grid(size_t num_bins_x, size_t num_bins_y, float bin_width, float bin_height, float obj_x, float obj_y) const override
Exports a boolean 2D grid showing which bins are covered by the triangle.
Definition objects_geometry.cpp:317
void create_box2d_shape(b2BodyId body_id, b2ShapeDef &shape_def) override
Create a Box2D shape based on this geometry.
Definition objects_geometry.cpp:258
void render(SDL_Renderer *renderer, b2WorldId world_id, float x, float y, float theta, uint8_t r, uint8_t g, uint8_t b, uint8_t alpha=255) const override
Renders the triangle on the given SDL renderer.
Definition objects_geometry.cpp:345
BoundingBox compute_bounding_box() const override
Computes the axis-aligned bounding box that completely encloses the geometry.
Definition objects_geometry.cpp:369
TriangleGeometry(float _side_length)
Construct an equilateral TriangleGeometry.
Definition objects_geometry.h:321
float get_side_length() const
Returns the side length of the equilateral triangle.
Definition objects_geometry.h:347
arena_polygons_t generate_contours(std::size_t points_per_contour=0, b2Vec2 position={0.0f, 0.0f}) const override
Return one or more polygonal contours that approximate / represent this geometry.
Definition objects_geometry.cpp:383
std::vector< std::vector< b2Vec2 > > arena_polygons_t
Definition geometry.h:13
ObjectGeometry * object_geometry_factory(Configuration const &config, Simulation *simulation)
Factory of ObjectGeometries.
Definition objects_geometry.cpp:611
std::unique_ptr< Simulation > simulation
Global simulation instance.
Definition simulator.cpp:74
Represents an axis-aligned bounding box with top-left corner (x, y) and dimensions width and height.
Definition objects_geometry.h:27
float x
Definition objects_geometry.h:28
float width
Definition objects_geometry.h:30
float height
Definition objects_geometry.h:31
float y
Definition objects_geometry.h:29
Represents a disk with center (x, y) and radius.
Definition objects_geometry.h:18
float center_y
Definition objects_geometry.h:20
float center_x
Definition objects_geometry.h:19
float radius
Definition objects_geometry.h:21