PlayRho  1.1.0
An Interactive Real-Time-Oriented C++ Physics Engine & Library
playrho::d2::Shape Class Reference

Shape. More...

#include <Shape.hpp>

Collaboration diagram for playrho::d2::Shape:
[legend]

Classes

struct  Concept
 Internal configuration concept. More...
 
struct  Model
 Internal model configuration concept. More...
 

Public Member Functions

 Shape () noexcept=default
 Default constructor. More...
 
 Shape (const Shape &other)=default
 Copy constructor.
 
 Shape (Shape &&other) noexcept=default
 Move constructor.
 
template<typename T , typename Tp = DecayedTypeIfNotSelf<T>, typename = std::enable_if_t<std::is_copy_constructible<Tp>::value>>
 Shape (T &&arg)
 Initializing constructor. More...
 
Shapeoperator= (const Shape &other)=default
 Copy assignment operator.
 
Shapeoperator= (Shape &&other)=default
 Move assignment operator.
 
template<typename T , typename Tp = DecayedTypeIfNotSelf<T>, typename = std::enable_if_t<std::is_copy_constructible<Tp>::value>>
Shapeoperator= (T &&other)
 Move assignment operator. More...
 
void swap (Shape &other) noexcept
 Swap support.
 
bool has_value () const noexcept
 Checks whether this instance contains a value.
 

Private Types

template<typename Type , typename DecayedType = std::decay_t<Type>>
using DecayedTypeIfNotSelf = std::enable_if_t<!std::is_same_v< DecayedType, Shape >, DecayedType >
 Decayed type if not same as this class. More...
 

Private Attributes

std::shared_ptr< const Conceptm_self
 Self pointer.
 

Friends

ChildCounter GetChildCount (const Shape &shape) noexcept
 Gets the number of child primitives of the shape. More...
 
DistanceProxy GetChild (const Shape &shape, ChildCounter index)
 Gets the "child" for the given index. More...
 
MassData GetMassData (const Shape &shape) noexcept
 Gets the mass properties of this shape using its dimensions and density. More...
 
NonNegative< LengthGetVertexRadius (const Shape &shape, ChildCounter idx)
 Gets the vertex radius of the indexed child of the given shape. More...
 
Real GetFriction (const Shape &shape) noexcept
 Gets the coefficient of friction. More...
 
Real GetRestitution (const Shape &shape) noexcept
 Gets the coefficient of restitution value of the given shape.
 
NonNegative< AreaDensityGetDensity (const Shape &shape) noexcept
 Gets the density of the given shape. More...
 
void Transform (Shape &shape, const Mat22 &m)
 Transforms all of the given shape's vertices by the given transformation matrix. More...
 
const void * GetData (const Shape &shape) noexcept
 Gets a pointer to the underlying data. More...
 
TypeID GetType (const Shape &shape) noexcept
 Gets the type info of the use of the given shape. More...
 
template<typename T >
std::add_pointer_t< std::add_const_t< T > > TypeCast (const Shape *value) noexcept
 Converts the given shape into its current configuration value. More...
 
bool operator== (const Shape &lhs, const Shape &rhs) noexcept
 Equality operator for shape to shape comparisons.
 
bool operator!= (const Shape &lhs, const Shape &rhs) noexcept
 Inequality operator for shape to shape comparisons.
 

Related Functions

(Note that these are not member functions.)

AABB ComputeAABB (const Shape &shape, const Transformation &xf) noexcept
 Computes the AABB for the given shape with the given transformation.
 
RayCastOutput RayCast (const Shape &shape, ChildCounter childIndex, const RayCastInput &input, const Transformation &transform) noexcept
 Cast a ray against the child of the given shape. More...
 
bool TestPoint (const Shape &shape, Length2 point) noexcept
 Test a point for containment in the given shape. More...
 
template<typename T >
TypeCast (const Shape &value)
 Casts the specified instance into the template specified type. More...
 

Detailed Description

Shape.

A shape is used for collision detection. You can create a shape from any supporting type. Shapes are conceptually made up of zero or more convex child shapes where each child shape is made up of zero or more vertices and an associated radius called its "vertex radius".

Note
This class's design provides a "polymorphic value type" offering polymorphism without public inheritance. This is based on a technique that's described by Sean Parent in his January 2017 Norwegian Developers Conference London talk "Better Code: Runtime Polymorphism". With this implementation, different shapes types can be had by constructing instances of this class with the different types that provide the required support. Different shapes of a given type meanwhile are had by providing different values for the type.
A shape can be constructor from or have its value set to any value whose type T has at least the following function definitions available for it:
  • bool operator==(const T& lhs, const T& rhs) noexcept;
  • ChildCounter GetChildCount(const T&) noexcept;
  • DistanceProxy GetChild(const T&, ChildCounter index);
  • MassData GetMassData(const T&) noexcept;
  • NonNegative<Length> GetVertexRadius(const T&, ChildCounter idx);
  • NonNegative<AreaDensity> GetDensity(const T&) noexcept;
  • Real GetFriction(const T&) noexcept;
  • Real GetRestitution(const T&) noexcept;
  • void Transform(T&, const Mat22& value);
See also
FixtureConf
https://youtu.be/QGcVXgEVMJg
https://en.wikibooks.org/wiki/More_C%2B%2B_Idioms/Polymorphic_Value_Types
Examples
DistanceJoint.cpp, FrictionJoint.cpp, GearJoint.cpp, HelloWorld.cpp, MotorJoint.cpp, PrismaticJoint.cpp, RevoluteJoint.cpp, RopeJoint.cpp, Shape.cpp, WeldJoint.cpp, WheelJoint.cpp, World.cpp, WorldBody.cpp, WorldContact.cpp, and WorldFixture.cpp.

Member Typedef Documentation

◆ DecayedTypeIfNotSelf

template<typename Type , typename DecayedType = std::decay_t<Type>>
using playrho::d2::Shape::DecayedTypeIfNotSelf = std::enable_if_t<!std::is_same_v<DecayedType, Shape>, DecayedType>
private

Decayed type if not same as this class.

Note
This is done separately from other checks to ensure order of compiler's SFINAE processing and to ensure elimination of self class before attempting to process other checks like is_copy_constructible. This prevents a compiler error that started showing up in gcc-9.

Constructor & Destructor Documentation

◆ Shape() [1/2]

playrho::d2::Shape::Shape ( )
defaultnoexcept

Default constructor.

Postcondition
has_value() returns false.

Referenced by operator=().

◆ Shape() [2/2]

template<typename T , typename Tp = DecayedTypeIfNotSelf<T>, typename = std::enable_if_t<std::is_copy_constructible<Tp>::value>>
playrho::d2::Shape::Shape ( T &&  arg)
inlineexplicit

Initializing constructor.

Parameters
argConfiguration value to construct a shape instance for.
Note
See the class notes section for an explanation of requirements on a type T for its values to be valid candidates for this function.
Postcondition
has_value() returns true.
Exceptions
std::bad_allocif there's a failure allocating storage.

Member Function Documentation

◆ operator=()

template<typename T , typename Tp = DecayedTypeIfNotSelf<T>, typename = std::enable_if_t<std::is_copy_constructible<Tp>::value>>
Shape& playrho::d2::Shape::operator= ( T &&  other)
inline

Move assignment operator.

Postcondition
has_value() returns true.

Friends And Related Function Documentation

◆ GetChild

DistanceProxy GetChild ( const Shape shape,
ChildCounter  index 
)
friend

Gets the "child" for the given index.

Parameters
shapeShape to get "child" shape of.
indexIndex to a child element of the shape. Value must be less than the number of child primitives of the shape.
Note
The shape must remain in scope while the proxy is in use.
Exceptions
InvalidArgumentif the given index is out of range.
See also
GetChildCount

Referenced by playrho::d2::Shape::Model< T >::GetChild_().

◆ GetChildCount

ChildCounter GetChildCount ( const Shape shape)
friend

Gets the number of child primitives of the shape.

Returns
Non-negative count.

Referenced by playrho::d2::Shape::Model< T >::GetChildCount_().

◆ GetData

const void* GetData ( const Shape shape)
friend

Gets a pointer to the underlying data.

Note
Provided for introspective purposes like visitation.

◆ GetDensity

NonNegative<AreaDensity> GetDensity ( const Shape shape)
friend

Gets the density of the given shape.

Returns
Non-negative density (in mass per area).

Referenced by playrho::d2::Shape::Model< T >::GetDensity_().

◆ GetFriction

Real GetFriction ( const Shape shape)
friend

Gets the coefficient of friction.

Returns
Value of 0 or higher.

Referenced by playrho::d2::Shape::Model< T >::GetFriction_().

◆ GetMassData

MassData GetMassData ( const Shape shape)
friend

Gets the mass properties of this shape using its dimensions and density.

Returns
Mass data for this shape.

Referenced by playrho::d2::Shape::Model< T >::GetMassData_().

◆ GetType

TypeID GetType ( const Shape shape)
friend

Gets the type info of the use of the given shape.

Note
This is not the same as calling GetTypeID<Shape>().
Returns
Type info of the underlying value's type.

◆ GetVertexRadius

NonNegative<Length> GetVertexRadius ( const Shape shape,
ChildCounter  idx 
)
friend

Gets the vertex radius of the indexed child of the given shape.

This gets the radius from the vertex that the shape's "skin" should extend outward by. While any edges - line segments between multiple vertices - are straight, corners between them (the vertices) are rounded and treated as rounded. Shapes with larger vertex radiuses compared to edge lengths therefore will be more prone to rolling or having other shapes more prone to roll off of them. Here's an image of a shape configured via a PolygonShapeConf with it's skin drawn:

Parameters
shapeShape to get child's vertex radius for.
idxChild index to get vertex radius for.
Note
This must be a non-negative value.
See also
UseVertexRadius
Exceptions
InvalidArgumentif the child index is not less than the child count.

Referenced by playrho::d2::Shape::Model< T >::GetVertexRadius_().

◆ Transform

void Transform ( Shape shape,
const Mat22 m 
)
friend

Transforms all of the given shape's vertices by the given transformation matrix.

See also
https://en.wikipedia.org/wiki/Transformation_matrix
Note
This may throw std::bad_alloc or any exception that's thrown by the constructor for the model's underlying data type.
Exceptions
std::bad_allocif there's a failure allocating storage.

Referenced by playrho::d2::Shape::Model< T >::Transform_().

◆ TypeCast() [1/2]

template<typename T >
T TypeCast ( const Shape value)
related

Casts the specified instance into the template specified type.

Exceptions
std::bad_castIf the template specified type is not the type of data underlying the given instance.
See also
GetType(const Shape&)

◆ TypeCast [2/2]

template<typename T >
std::add_pointer_t<std::add_const_t<T> > TypeCast ( const Shape value)
friend

Converts the given shape into its current configuration value.

Note
The design for this was based off the design of the C++17 std::any class and its associated std::any_cast function. The code for this is based off of the std::any code from the LLVM Project.
See also
https://llvm.org/
GetType(const Shape&)

The documentation for this class was generated from the following files: