Class Acts::AxisAlignedBoundingBox

template<typename entity_t, typename value_t, size_t DIM>
class AxisAlignedBoundingBox

Implementation of an Axis Aligned Bounding Box.

This type is compatible with 2D and 3D boxes

Public Types

using entity_type = entity_t

Type of stored entity.

using Size = NamedType<VertexType, struct SizeParameter>

Strong type to select the correct constructor.

using transform_type = Eigen::Transform<value_type, DIM, Eigen::Affine>

The transform type based on the value_type

using value_type = value_t

The value type used by this class.

using vertex_array_type = Eigen::Array<value_t, DIM, 1>

Associated array value to VertexType

using VertexType = Eigen::Matrix<value_t, DIM, 1>

Re-export vertex type based on value type given.

Public Functions

AxisAlignedBoundingBox(const self_t &other) = default

Copy constructor from other bounding box.

Parameters

other – The other AABB

AxisAlignedBoundingBox(const entity_t *entity, const VertexType &vmin, const VertexType &vmax)

Constructor from an entity pointer, and the min and max vertices.

Parameters
  • entity – The entity to store

  • vmin – The minimum vertex.

  • vmax – The maximum vertex.

AxisAlignedBoundingBox(const entity_t *entity, const VertexType &center, const Size &size)

Constructor from a center position, and a width and height.

Note

The special type size is required to disambiguate this constructor from the other one above. It is a wrapper around a simple Vector3.

Parameters
  • entity – The entity to store

  • center – The center position

  • size – The size (width and height) of the box.

AxisAlignedBoundingBox(const std::vector<self_t*> &boxes, vertex_array_type envelope = vertex_array_type::Zero())

Constructor from a list of child boxes.

This box will wrap around all boxes contained in boxes, and additional envelope can be given.

Parameters
  • boxes – Vector of child boxes to store in this bounding box.

  • envelope – Envelope that will be added/subtracted to the dimension.

const VertexType &center() const

Get the center position of this bounding box.

Returns

The center position

template<size_t D = DIM, std::enable_if_t<D == 3, int> = 0>
void draw(IVisualization3D &helper, std::array<int, 3> color = {120, 120, 120}, const transform_type &trf = transform_type::Identity()) const

Draw this bounding box using the given visualization helper.

This method is only available for the 3D case.

Template Parameters

D – (used for SFINAE)

Parameters
  • helper – The visualization helper to write to

  • color – The color to use for drawing

  • trf – An optional transform to apply first.

const entity_t *entity() const

Return the entity associated with this box.

This might be nullptr if there is no entity attached.

Returns

The entity pointer, might be nullptr

const self_t *getLeftChild() const

Get the left child (i.e.

the first of the children that are inside this bounding box).

Returns

The lest most child.

const self_t *getSkip() const

Get the skip node for this box.

Returns

The skip node pointer

bool hasEntity() const

Check whether this node as an associated entity.

If it does not have one, this is a purely abstract container box.

Returns

Whether the box has an entity attached.

bool intersect(const VertexType &point) const

Calculate whether a point is inside this box.

Parameters

point – The point to test.

Returns

Whether the point is inside or not.

bool intersect(const Ray<value_type, DIM> &ray) const

Implements the slab method for Ray/AABB intersections.

See https://tavianator.com/fast-branchless-raybounding-box-intersections/, https://tavianator.com/fast-branchless-raybounding-box-intersections-part-2-nans/, https://medium.com/@bromanz/another-view-on-the-classic-ray-aabb-intersection-algorithm-for-bvh-traversal-41125138b525 The original algorithms is described in “Graphics Gems (1990)” 1

Note

This implementation may treat parallel rays on any of the slabs as outside due to how NaNs are handled by Eigen. See http://eigen.tuxfamily.org/bz/show_bug.cgi?id=564

Parameters

ray – The ray to intersect with

Returns

Whether the ray intersects this AABB

template<size_t sides>
bool intersect(const Frustum<value_type, DIM, sides> &fr) const

Check if a frustum intersects with this bounding box.

This method implements an algorithm similar to the one described in “Optimized View Frustum Culling Algorithms for Bounding Boxes (2012)” 2, but drops some of the more sophisticated optimization.

Parameters

fr – The frustum

Returns

Whether the frustum intersects this AABB

const VertexType &max() const

Get the maximum vertex.

Returns

The maximum vertex

const VertexType &min() const

Get the minimum vertex.

Returns

The minimum vertex

AxisAlignedBoundingBox &operator=(const self_t &other) = default

Copy assignment operator from other bounding box.

Parameters

other – The other AABB

void setEntity(const entity_t *entity)

Set the entity associated with with this box.

Parameters

entity – The entity

void setSkip(self_t *skip)

Set the skip node (bounding box)

Parameters

skip – The target skip node pointer

template<size_t D = DIM, std::enable_if_t<D == 2, int> = 0>
std::ostream &svg(std::ostream &os, value_type w, value_type h, value_type unit = 10, std::string label = "", std::string fillcolor = "grey") const

Draw this bounding box as SVG.

This method is only available for the 2D case.

Template Parameters

D – (used for SFINAE)

Parameters
  • os – The output stream to write to

  • w – The width of the output SVG.

  • h – The height of the output SVG.

  • unit – A scale factor to apply before drawing

  • label – A label to put next to the box.

  • fillcolor – Color to fill the box with.

Returns

The outstream given in os.

std::ostream &toStream(std::ostream &os) const

Write information about this bounding box to a stream.

Parameters

os – The output stream.

Returns

The stream given as an argument.

void transform(const transform_type &trf)

Transforms this bounding box using the given transform.

This method modifies the box it is called on.

Parameters

trf – The transform

self_t transformed(const transform_type &trf) const

Transforms this bounding box using the given transfom.

This method returns a copy of this box, with the transformation applied, and leaves this instance unchanged.

Parameters

trf – The transform

Returns

The transformed bounding box

Public Static Functions

static std::pair<VertexType, VertexType> wrap(const std::vector<const self_t*> &boxes, vertex_array_type envelope = vertex_array_type::Zero())

Helper function to calculate the size of a bounding box enclosing boxes.

Parameters
  • boxes – The boxes to wrap (const pointers)

  • envelope – Optional envelop to add/subtract to dimension.

Returns

Pair of vertixes: min and max.

static std::pair<VertexType, VertexType> wrap(const std::vector<self_t*> &boxes, vertex_array_type envelope = vertex_array_type::Zero())

Helper function to calculate the size of a bounding box enclosing boxes.

Overload which accepts non-const boxes in boxes.

Parameters
  • boxes – The boxes to wrap (non-const pointers)

  • envelope – Optional envelop to add/subtract to dimension.

Returns

Pair of vertixes: min and max.

static std::pair<VertexType, VertexType> wrap(const std::vector<self_t> &boxes, vertex_array_type envelope = vertex_array_type::Zero())

Helper function to calculate the size of a bounding box enclosing boxes.

Overload which accepts a vector in boxes which owns the instances

Parameters
  • boxes – The boxes to wrap (by-value vector)

  • envelope – Optional envelop to add/subtract to dimension.

Returns

Pair of vertixes: min and max.

Public Static Attributes

static const size_t dim = DIM

Re-export dimension from template parameter.