Surface.hpp

This file defines a templated class to represent a 2D suface capable of maintaining data about which 2D bodies are currently on that surface and rapidly identifying if they are overlapping.

BODY_TYPE is the class that represents the body geometry.

Developer Notes:

  • Should add enums to control boundary conditions (INFINITE, TOROIDAL, BOUNDED)

  • Incorporate physics? Can have various plug-in modules.

template<typename ...BODY_TYPES>
class Surface
#include <Surface.hpp>

Public Types

using body_types = TypePack<BODY_TYPES...>
using sector_t = vector<size_t>

Public Functions

inline Surface(Point _max)

Create a surface providing maxumum size.

inline ~Surface()
inline double GetWidth() const
inline double GetHeight() const
inline const Point &GetMaxPosition() const
inline const vector<BodyInfo> &GetBodySet() const
inline bool IsActive(size_t id) const

Determine if an id represents an active body on a surface.

template<typename ORIGINAL_T>
inline Ptr<ORIGINAL_T> GetPtr(size_t id) const
inline Point GetCenter(size_t id) const
inline double GetRadius(size_t id) const
inline size_t GetColor(size_t id) const
inline void SetCenter(size_t id, Point _in)
inline void Translate(size_t id, Point translation)
inline void TranslateWrap(size_t id, Point translation)
inline void SetRadius(size_t id, double _in)
inline void ScaleRadius(size_t id, double scale)
inline void SetColor(size_t id, size_t _in)
inline void RemoveBody(size_t id)
template<typename BODY_T>
inline size_t AddBody(Ptr<BODY_T> _body, Point _center, double _radius, size_t _color = 0)

Add a single body; return its unique ID.

template<typename BODY_T>
inline size_t AddBody(BODY_T *_body, Point _center, double _radius, size_t _color = 0)

Allow bodies to be provided as raw pointers as well.

inline Surface &Clear()

Remove all bodies from the surface.

template<typename BODY1_T, typename BODY2_T>
inline void AddOverlapFun(const std::function<void(BODY1_T&, BODY2_T&)> &overlap_fun)

Add a function to call in the case of overlaps (using an std::function object)

template<typename BODY1_T, typename BODY2_T>
inline void AddOverlapFun(void (*overlap_fun)(BODY1_T&, BODY2_T&))

Add a function to call in the case of overlaps (using a function pointer)

template<typename LAMBDA_T>
inline void AddOverlapFun(const LAMBDA_T &fun)

Add a to call a lambda function in the case of overlaps (using a function pointer)

inline void FindSectorOverlaps(BodyInfo &body1, size_t sector_id, size_t start_id = 0)
inline void FindOverlaps()
inline void FindOverlap(BodyInfo &body)

Determine if there are any overlaps with a single provided body (that may or may not be on surface).

inline void FindOverlap(size_t body_id)
inline void FindOverlap(Point center, double radius)

Public Static Functions

static inline bool TestOverlap(const BodyInfo &body1, const BodyInfo &body2)

Determine if two bodies overlap.

Protected Functions

inline void InitSectors()
inline void TestBodySize(BodyInfo &body)
inline void RefreshBodySize()
inline size_t FindSector(Point point)
inline void PlaceBody(BodyInfo &body)
inline void Activate()

Protected Attributes

TypeTracker<Ptr<BODY_TYPES>...> type_tracker

The TypeTracker manages pointers to arbitrary surface objects.

const Point max_pos

Lower-left corner of the surface.

vector<BodyInfo> body_set

Set of all bodies on surface.

vector<size_t> open_ids

Set of body_set positions ready for re-use.

bool data_active

Are we trying to keep data up-to-date?

double max_radius

Largest radius of any body.

size_t num_cols

How many cols of sectors are there?

size_t num_rows

How many rows of sectors are there?

size_t num_sectors

How many total sectors are there?

double sector_width

How wide is each sector?

double sector_height

How tall is each sector?

vector<sector_t> sectors

Which bodies are in each sector?

class BodyInfo
#include <Surface.hpp>

Public Functions

inline BodyInfo(TrackedVar &&_ptr, size_t _id, Point _center, double _radius, size_t _color = 0)
inline BodyInfo(size_t _id = (size_t)-1, Point _center = Point(), double _radius = 0.0)
BodyInfo(const BodyInfo&) = default
BodyInfo(BodyInfo&&) = default
inline TrackedVar &GetBodyPtr()
inline size_t GetID() const
inline Point GetCenter() const
inline double GetRadius() const
inline size_t GetColor() const
inline void SetCenter(Point _in)
inline void SetRadius(double _in)
inline void SetColor(size_t _in)
BodyInfo &operator=(const BodyInfo&) = default
BodyInfo &operator=(BodyInfo&&) = default
inline bool IsActive() const
inline void Deactivate()

Private Members

TrackedVar body_ptr

Pointer to the bodies.

size_t id

Index in body_set to find body info.

Point center

Center position of this body on surface.

double radius

Size of this body.

size_t color

Color of this body.