Pogosim
Loading...
Searching...
No Matches
distances.h File Reference
#include <box2d/box2d.h>
#include <unordered_map>
#include <vector>
#include <cmath>
#include "utils.h"
#include "spogobot.h"
#include "robot.h"

Go to the source code of this file.

Classes

struct  GridCell
 Represents a cell in a spatial grid. More...
struct  GridCellHash
 Hash functor for GridCell. More...
struct  Candidate
struct  angles::Interval

Namespaces

namespace  angles

Functions

float euclidean_distance (const b2Vec2 &a, const b2Vec2 &b)
 Computes the Euclidean distance between two 2D points.
GridCell get_grid_cell (float x, float y, float cell_size)
 Converts a 2D position to a grid cell index.
float angles::wrap (float a)
void angles::add_interval (float a, float b, std::vector< Interval > &ivs)
bool angles::fully_covered (float a, float b, const std::vector< Interval > &ivs)
bool angles::in_fov (float bearing, float center, float half_ap)
std::unordered_map< GridCell, std::vector< std::size_t >, GridCellHashbuild_spatial_hash (span_t< float > xs, span_t< float > ys, float cell_size)
std::vector< Candidatecollect_candidates (std::size_t i, span_t< float > xs, span_t< float > ys, span_t< float > cx, span_t< float > cy, span_t< float > body_rad, span_t< float > comm_rad, span_t< float > led_dir, const std::unordered_map< GridCell, std::vector< std::size_t >, GridCellHash > &hash, float cell_size, bool clip_fov)
std::vector< std::size_t > filter_visible (const std::vector< Candidate > &cand)
void find_neighbors (ir_direction dir, std::vector< std::shared_ptr< PogobotObject > > &robots, float maxDistance, bool enable_occlusion=true)
 Finds neighboring robots within a specified maximum distance.
std::vector< float > compute_wall_distances (ir_direction dir, const std::vector< std::shared_ptr< PogobotObject > > &robots, const arena_polygons_t &arena_polygons)
 Compute, for every robot, the distance from its IR–emitter dir to the nearest arena wall.
void find_neighbors_to_pogowalls (std::vector< std::shared_ptr< Pogowall > > &pogowalls, ir_direction dir, std::vector< std::shared_ptr< PogobotObject > > &robots)
 Finds robots that are close to given pogowalls.
float wrap01 (float x, float L)
float delta_periodic (float d, float L)
b2Vec2 torus_delta (b2Vec2 a, b2Vec2 b, b2Vec2 dom_min, float Lx, float Ly)
void find_neighbors_periodic (ir_direction dir, std::vector< std::shared_ptr< PogobotObject > > &robots, float max_distance, b2Vec2 domain_min, float domain_w, float domain_h, bool enable_occlusion=true)
std::unordered_map< GridCell, std::vector< std::size_t >, GridCellHashbuild_spatial_hash_periodic (span_t< float > xs, span_t< float > ys, float cell_size, b2Vec2 domain_min, float domain_w, float domain_h)

Variables

constexpr std::array< GridCell, 9 > precomputed_neighbor_cells
 Precomputed offsets for neighbor grid cells.

Function Documentation

◆ build_spatial_hash()

std::unordered_map< GridCell, std::vector< std::size_t >, GridCellHash > build_spatial_hash ( span_t< float > xs,
span_t< float > ys,
float cell_size )

◆ build_spatial_hash_periodic()

std::unordered_map< GridCell, std::vector< std::size_t >, GridCellHash > build_spatial_hash_periodic ( span_t< float > xs,
span_t< float > ys,
float cell_size,
b2Vec2 domain_min,
float domain_w,
float domain_h )

◆ collect_candidates()

std::vector< Candidate > collect_candidates ( std::size_t i,
span_t< float > xs,
span_t< float > ys,
span_t< float > cx,
span_t< float > cy,
span_t< float > body_rad,
span_t< float > comm_rad,
span_t< float > led_dir,
const std::unordered_map< GridCell, std::vector< std::size_t >, GridCellHash > & hash,
float cell_size,
bool clip_fov )

◆ compute_wall_distances()

std::vector< float > compute_wall_distances ( ir_direction dir,
const std::vector< std::shared_ptr< PogobotObject > > & robots,
const arena_polygons_t & arena_polygons )

Compute, for every robot, the distance from its IR–emitter dir to the nearest arena wall.

The function returns a vector<double> whose i‑th element is the distance for robots[i]. The caller decides what to do with the result (store it in the robot, feed a sensor model, etc.).

Parameters
dirWhich IR emitter of the robot is queried.
robotsRobots for which the distance is required.
arena_polygonsArena walls described as polygons (see arena_polygons_t).
Returns
std::vector<float> A vector of distances (same ordering as robots).

◆ delta_periodic()

float delta_periodic ( float d,
float L )
inline

◆ euclidean_distance()

float euclidean_distance ( const b2Vec2 & a,
const b2Vec2 & b )

Computes the Euclidean distance between two 2D points.

This function calculates the Euclidean distance between the points represented by the Box2D vectors a and b.

Parameters
aThe first point as a b2Vec2.
bThe second point as a b2Vec2.
Returns
float The Euclidean distance between a and b.

◆ filter_visible()

std::vector< std::size_t > filter_visible ( const std::vector< Candidate > & cand)

◆ find_neighbors()

void find_neighbors ( ir_direction dir,
std::vector< std::shared_ptr< PogobotObject > > & robots,
float maxDistance,
bool enable_occlusion = true )

Finds neighboring robots within a specified maximum distance.

This function uses spatial hashing to efficiently determine the neighbors for each robot. It partitions the 2D space into grid cells of size maxDistance and assigns each robot to a cell. For every robot, the function then checks the same cell and all adjacent cells for other robots, and updates the robot's neighbor list if the Euclidean distance (squared) is within the allowed maximum distance.

Parameters
dirDirection in which messages are sent (i.e. the ID number of the IR emitter)
robotsA vector of Robot objects. Each Robot must implement a method get_position() returning a b2Vec2, and contain a public member "neighbors" (e.g., a vector) that can store pointers to neighboring Robot objects.
maxDistanceThe maximum distance within which two robots are considered neighbors.
enable_occlusionWhen true, use the angular-shadow-map LOS filter; when false, accept every range-culled candidate.

◆ find_neighbors_periodic()

void find_neighbors_periodic ( ir_direction dir,
std::vector< std::shared_ptr< PogobotObject > > & robots,
float max_distance,
b2Vec2 domain_min,
float domain_w,
float domain_h,
bool enable_occlusion = true )

◆ find_neighbors_to_pogowalls()

void find_neighbors_to_pogowalls ( std::vector< std::shared_ptr< Pogowall > > & pogowalls,
ir_direction dir,
std::vector< std::shared_ptr< PogobotObject > > & robots )

Finds robots that are close to given pogowalls.

Parameters
pogowallsA vector of Pogowall objects.
dirDirection in which messages are sent (i.e. the ID number of the IR emitter)
robotsA vector of Robot objects.
max_distanceThe maximum distance within which a robot and a pogowalls are considered neighbors.

◆ get_grid_cell()

GridCell get_grid_cell ( float x,
float y,
float cell_size )
inline

Converts a 2D position to a grid cell index.

This inline function maps the provided (x, y) coordinates into a GridCell based on the specified cell size. It uses std::floor to determine the appropriate cell index.

Parameters
xThe x-coordinate of the position.
yThe y-coordinate of the position.
cellSizeThe size of a single grid cell.
Returns
GridCell The corresponding grid cell for the given position.

◆ torus_delta()

b2Vec2 torus_delta ( b2Vec2 a,
b2Vec2 b,
b2Vec2 dom_min,
float Lx,
float Ly )
inline

◆ wrap01()

float wrap01 ( float x,
float L )
inline

Variable Documentation

◆ precomputed_neighbor_cells

std::array<GridCell,9> precomputed_neighbor_cells
constexpr
Initial value:
{
GridCell{-1,-1}, GridCell{-1,0}, GridCell{-1,1},
GridCell{ 0,-1}, GridCell{ 0,0}, GridCell{ 0,1},
GridCell{ 1,-1}, GridCell{ 1,0}, GridCell{ 1,1}
}
Represents a cell in a spatial grid.
Definition distances.h:31

Precomputed offsets for neighbor grid cells.

This constant array contains the relative offsets for a cell's neighbors in a 3x3 grid, including the cell itself. It is used to quickly access adjacent cells during spatial queries.