mirror of https://github.com/mosra/magnum.git
Browse Source
This class now stores the tree in flat array, making it easier for user to query the contents, but the internals are much more complicated. This solution already reduces allocation count by count of nodes in the tree, future work might remove the per-shape allocation altogether by using large typeless array and placement-new etc.pull/278/head
11 changed files with 796 additions and 222 deletions
@ -0,0 +1,102 @@
|
||||
/*
|
||||
This file is part of Magnum. |
||||
|
||||
Copyright © 2010, 2011, 2012, 2013 Vladimír Vondruš <mosra@centrum.cz> |
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a |
||||
copy of this software and associated documentation files (the "Software"), |
||||
to deal in the Software without restriction, including without limitation |
||||
the rights to use, copy, modify, merge, publish, distribute, sublicense, |
||||
and/or sell copies of the Software, and to permit persons to whom the |
||||
Software is furnished to do so, subject to the following conditions: |
||||
|
||||
The above copyright notice and this permission notice shall be included |
||||
in all copies or substantial portions of the Software. |
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL |
||||
THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING |
||||
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER |
||||
DEALINGS IN THE SOFTWARE. |
||||
*/ |
||||
|
||||
#include "CollisionDispatch.h" |
||||
|
||||
#include "Physics/AxisAlignedBox.h" |
||||
#include "Physics/Box.h" |
||||
#include "Physics/Capsule.h" |
||||
#include "Physics/LineSegment.h" |
||||
#include "Physics/Plane.h" |
||||
#include "Physics/Point.h" |
||||
#include "Physics/Sphere.h" |
||||
#include "Physics/shapeImplementation.h" |
||||
|
||||
namespace Magnum { namespace Physics { namespace Implementation { |
||||
|
||||
namespace { |
||||
inline constexpr UnsignedInt operator*(ShapeDimensionTraits<2>::Type a, ShapeDimensionTraits<2>::Type b) { |
||||
return UnsignedInt(a)*UnsignedInt(b); |
||||
} |
||||
inline constexpr UnsignedInt operator*(ShapeDimensionTraits<3>::Type a, ShapeDimensionTraits<3>::Type b) { |
||||
return UnsignedInt(a)*UnsignedInt(b); |
||||
} |
||||
} |
||||
|
||||
template<> bool collides(const AbstractShape<2>* const a, const AbstractShape<2>* const b) { |
||||
if(a->type() < b->type()) return collides(b, a); |
||||
|
||||
switch(a->type()*b->type()) { |
||||
case ShapeDimensionTraits<2>::Type::Sphere*ShapeDimensionTraits<2>::Type::Point: |
||||
return static_cast<const Shape<Sphere2D>*>(a)->shape % static_cast<const Shape<Point2D>*>(b)->shape; |
||||
case ShapeDimensionTraits<2>::Type::Sphere*ShapeDimensionTraits<2>::Type::Line: |
||||
return static_cast<const Shape<Sphere2D>*>(a)->shape % static_cast<const Shape<Line2D>*>(b)->shape; |
||||
case ShapeDimensionTraits<2>::Type::Sphere*ShapeDimensionTraits<2>::Type::LineSegment: |
||||
return static_cast<const Shape<Sphere2D>*>(a)->shape % static_cast<const Shape<LineSegment2D>*>(b)->shape; |
||||
case ShapeDimensionTraits<2>::Type::Sphere*ShapeDimensionTraits<2>::Type::Sphere: |
||||
return static_cast<const Shape<Sphere2D>*>(a)->shape % static_cast<const Shape<Sphere2D>*>(b)->shape; |
||||
|
||||
case ShapeDimensionTraits<2>::Type::Capsule*ShapeDimensionTraits<2>::Type::Point: |
||||
return static_cast<const Shape<Capsule2D>*>(a)->shape % static_cast<const Shape<Point2D>*>(b)->shape; |
||||
case ShapeDimensionTraits<2>::Type::Capsule*ShapeDimensionTraits<2>::Type::Sphere: |
||||
return static_cast<const Shape<Capsule2D>*>(a)->shape % static_cast<const Shape<Sphere2D>*>(b)->shape; |
||||
|
||||
case ShapeDimensionTraits<2>::Type::AxisAlignedBox*ShapeDimensionTraits<2>::Type::Point: |
||||
return static_cast<const Shape<AxisAlignedBox2D>*>(a)->shape % static_cast<const Shape<Point2D>*>(b)->shape; |
||||
} |
||||
|
||||
return false; |
||||
} |
||||
|
||||
template<> bool collides(const AbstractShape<3>* const a, const AbstractShape<3>* const b) { |
||||
if(a->type() < b->type()) return collides(b, a); |
||||
|
||||
switch(a->type()*b->type()) { |
||||
case ShapeDimensionTraits<3>::Type::Sphere*ShapeDimensionTraits<3>::Type::Point: |
||||
return static_cast<const Shape<Sphere3D>*>(a)->shape % static_cast<const Shape<Point3D>*>(b)->shape; |
||||
case ShapeDimensionTraits<3>::Type::Sphere*ShapeDimensionTraits<3>::Type::Line: |
||||
return static_cast<const Shape<Sphere3D>*>(a)->shape % static_cast<const Shape<Line3D>*>(b)->shape; |
||||
case ShapeDimensionTraits<3>::Type::Sphere*ShapeDimensionTraits<3>::Type::LineSegment: |
||||
return static_cast<const Shape<Sphere3D>*>(a)->shape % static_cast<const Shape<LineSegment3D>*>(b)->shape; |
||||
case ShapeDimensionTraits<3>::Type::Sphere*ShapeDimensionTraits<3>::Type::Sphere: |
||||
return static_cast<const Shape<Sphere3D>*>(a)->shape % static_cast<const Shape<Sphere3D>*>(b)->shape; |
||||
|
||||
case ShapeDimensionTraits<3>::Type::Capsule*ShapeDimensionTraits<3>::Type::Point: |
||||
return static_cast<const Shape<Capsule3D>*>(a)->shape % static_cast<const Shape<Point3D>*>(b)->shape; |
||||
case ShapeDimensionTraits<3>::Type::Capsule*ShapeDimensionTraits<3>::Type::Sphere: |
||||
return static_cast<const Shape<Capsule3D>*>(a)->shape % static_cast<const Shape<Sphere3D>*>(b)->shape; |
||||
|
||||
case ShapeDimensionTraits<3>::Type::AxisAlignedBox*ShapeDimensionTraits<3>::Type::Point: |
||||
return static_cast<const Shape<AxisAlignedBox3D>*>(a)->shape % static_cast<const Shape<Point3D>*>(b)->shape; |
||||
|
||||
case ShapeDimensionTraits<3>::Type::Plane*ShapeDimensionTraits<3>::Type::Line: |
||||
return static_cast<const Shape<Plane>*>(a)->shape % static_cast<const Shape<Line3D>*>(b)->shape; |
||||
case ShapeDimensionTraits<3>::Type::Plane*ShapeDimensionTraits<3>::Type::LineSegment: |
||||
return static_cast<const Shape<Plane>*>(a)->shape % static_cast<const Shape<LineSegment3D>*>(b)->shape; |
||||
} |
||||
|
||||
return false; |
||||
} |
||||
|
||||
}}} |
||||
@ -0,0 +1,46 @@
|
||||
#ifndef Magnum_Physics_Implementation_CollisionDispatch_h |
||||
#define Magnum_Physics_Implementation_CollisionDispatch_h |
||||
/*
|
||||
This file is part of Magnum. |
||||
|
||||
Copyright © 2010, 2011, 2012, 2013 Vladimír Vondruš <mosra@centrum.cz> |
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a |
||||
copy of this software and associated documentation files (the "Software"), |
||||
to deal in the Software without restriction, including without limitation |
||||
the rights to use, copy, modify, merge, publish, distribute, sublicense, |
||||
and/or sell copies of the Software, and to permit persons to whom the |
||||
Software is furnished to do so, subject to the following conditions: |
||||
|
||||
The above copyright notice and this permission notice shall be included |
||||
in all copies or substantial portions of the Software. |
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL |
||||
THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING |
||||
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER |
||||
DEALINGS IN THE SOFTWARE. |
||||
*/ |
||||
|
||||
#include "Types.h" |
||||
|
||||
namespace Magnum { namespace Physics { namespace Implementation { |
||||
|
||||
template<UnsignedInt> struct AbstractShape; |
||||
|
||||
/*
|
||||
Shape collision double-dispatch: |
||||
|
||||
The collision is symmetric, i.e. it doesn't matter if we test Point vs. Sphere |
||||
or Sphere vs. Point. Each type is specified by unique prime number. Then we |
||||
multiply the two numbers together and switch() on the result. Because of |
||||
multiplying two prime numbers, there is no ambiguity (the result is unique for |
||||
each combination). |
||||
*/ |
||||
template<UnsignedInt dimensions> bool collides(const AbstractShape<dimensions>* a, const AbstractShape<dimensions>* b); |
||||
|
||||
}}} |
||||
|
||||
#endif |
||||
@ -0,0 +1,72 @@
|
||||
/*
|
||||
This file is part of Magnum. |
||||
|
||||
Copyright © 2010, 2011, 2012, 2013 Vladimír Vondruš <mosra@centrum.cz> |
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a |
||||
copy of this software and associated documentation files (the "Software"), |
||||
to deal in the Software without restriction, including without limitation |
||||
the rights to use, copy, modify, merge, publish, distribute, sublicense, |
||||
and/or sell copies of the Software, and to permit persons to whom the |
||||
Software is furnished to do so, subject to the following conditions: |
||||
|
||||
The above copyright notice and this permission notice shall be included |
||||
in all copies or substantial portions of the Software. |
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL |
||||
THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING |
||||
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER |
||||
DEALINGS IN THE SOFTWARE. |
||||
*/ |
||||
|
||||
#include "shapeImplementation.h" |
||||
|
||||
#include <Utility/Debug.h> |
||||
|
||||
namespace Magnum { namespace Physics { namespace Implementation { |
||||
|
||||
Debug operator<<(Debug debug, ShapeDimensionTraits<2>::Type value) { |
||||
switch(value) { |
||||
#define _val(value) case ShapeDimensionTraits<2>::Type::value: return debug << "Physics::Shape2D::Type::" #value; |
||||
_val(Point) |
||||
_val(Line) |
||||
_val(LineSegment) |
||||
_val(Sphere) |
||||
_val(Capsule) |
||||
_val(AxisAlignedBox) |
||||
_val(Box) |
||||
_val(ShapeGroup) |
||||
#undef _val |
||||
} |
||||
|
||||
return debug << "Physics::Shape2D::Type::(unknown)"; |
||||
} |
||||
|
||||
Debug operator<<(Debug debug, ShapeDimensionTraits<3>::Type value) { |
||||
switch(value) { |
||||
#define _val(value) case ShapeDimensionTraits<3>::Type::value: return debug << "Physics::Shape3D::Type::" #value; |
||||
_val(Point) |
||||
_val(Line) |
||||
_val(LineSegment) |
||||
_val(Sphere) |
||||
_val(Capsule) |
||||
_val(AxisAlignedBox) |
||||
_val(Box) |
||||
_val(Plane) |
||||
_val(ShapeGroup) |
||||
#undef _val |
||||
} |
||||
|
||||
return debug << "Physics::Shape3D::Type::(unknown)"; |
||||
} |
||||
|
||||
template<UnsignedInt dimensions> AbstractShape<dimensions>::~AbstractShape() = default; |
||||
template<UnsignedInt dimensions> AbstractShape<dimensions>::AbstractShape() = default; |
||||
|
||||
template struct AbstractShape<2>; |
||||
template struct AbstractShape<3>; |
||||
|
||||
}}} |
||||
@ -0,0 +1,155 @@
|
||||
#ifndef Magnum_Physics_shapeImplementation_h |
||||
#define Magnum_Physics_shapeImplementation_h |
||||
/*
|
||||
This file is part of Magnum. |
||||
|
||||
Copyright © 2010, 2011, 2012, 2013 Vladimír Vondruš <mosra@centrum.cz> |
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a |
||||
copy of this software and associated documentation files (the "Software"), |
||||
to deal in the Software without restriction, including without limitation |
||||
the rights to use, copy, modify, merge, publish, distribute, sublicense, |
||||
and/or sell copies of the Software, and to permit persons to whom the |
||||
Software is furnished to do so, subject to the following conditions: |
||||
|
||||
The above copyright notice and this permission notice shall be included |
||||
in all copies or substantial portions of the Software. |
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL |
||||
THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING |
||||
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER |
||||
DEALINGS IN THE SOFTWARE. |
||||
*/ |
||||
|
||||
#include <utility> |
||||
#include <Utility/Assert.h> |
||||
|
||||
#include "DimensionTraits.h" |
||||
#include "Magnum.h" |
||||
#include "Physics/Physics.h" |
||||
#include "Physics/magnumPhysicsVisibility.h" |
||||
|
||||
namespace Magnum { namespace Physics { namespace Implementation { |
||||
|
||||
/* Shape type for given dimension count */ |
||||
|
||||
template<UnsignedInt> struct ShapeDimensionTraits; |
||||
|
||||
template<> struct ShapeDimensionTraits<2> { |
||||
enum class Type: UnsignedByte { |
||||
Point = 1, |
||||
Line = 2, |
||||
LineSegment = 3, |
||||
Sphere = 5, |
||||
Capsule = 7, |
||||
AxisAlignedBox = 11, |
||||
Box = 13, |
||||
ShapeGroup = 17 |
||||
}; |
||||
}; |
||||
|
||||
template<> struct ShapeDimensionTraits<3> { |
||||
enum class Type: UnsignedByte { |
||||
Point = 1, |
||||
Line = 2, |
||||
LineSegment = 3, |
||||
Sphere = 5, |
||||
Capsule = 7, |
||||
AxisAlignedBox = 11, |
||||
Box = 13, |
||||
Plane = 17, |
||||
ShapeGroup = 19 |
||||
}; |
||||
}; |
||||
|
||||
Debug MAGNUM_PHYSICS_EXPORT operator<<(Debug debug, ShapeDimensionTraits<2>::Type value); |
||||
Debug MAGNUM_PHYSICS_EXPORT operator<<(Debug debug, ShapeDimensionTraits<3>::Type value); |
||||
|
||||
/* Enum value corresponding to given type */ |
||||
|
||||
template<class> struct TypeOf; |
||||
|
||||
template<UnsignedInt dimensions> struct TypeOf<Physics::Point<dimensions>> { |
||||
inline constexpr static typename ShapeDimensionTraits<dimensions>::Type type() { |
||||
return ShapeDimensionTraits<dimensions>::Type::Point; |
||||
} |
||||
}; |
||||
template<UnsignedInt dimensions> struct TypeOf<Physics::Line<dimensions>> { |
||||
inline constexpr static typename ShapeDimensionTraits<dimensions>::Type type() { |
||||
return ShapeDimensionTraits<dimensions>::Type::Line; |
||||
} |
||||
}; |
||||
template<UnsignedInt dimensions> struct TypeOf<Physics::LineSegment<dimensions>> { |
||||
inline constexpr static typename ShapeDimensionTraits<dimensions>::Type type() { |
||||
return ShapeDimensionTraits<dimensions>::Type::LineSegment; |
||||
} |
||||
}; |
||||
template<UnsignedInt dimensions> struct TypeOf<Physics::Sphere<dimensions>> { |
||||
inline constexpr static typename ShapeDimensionTraits<dimensions>::Type type() { |
||||
return ShapeDimensionTraits<dimensions>::Type::Sphere; |
||||
} |
||||
}; |
||||
template<UnsignedInt dimensions> struct TypeOf<Physics::Capsule<dimensions>> { |
||||
inline constexpr static typename ShapeDimensionTraits<dimensions>::Type type() { |
||||
return ShapeDimensionTraits<dimensions>::Type::Capsule; |
||||
} |
||||
}; |
||||
template<UnsignedInt dimensions> struct TypeOf<Physics::AxisAlignedBox<dimensions>> { |
||||
inline constexpr static typename ShapeDimensionTraits<dimensions>::Type type() { |
||||
return ShapeDimensionTraits<dimensions>::Type::AxisAlignedBox; |
||||
} |
||||
}; |
||||
template<UnsignedInt dimensions> struct TypeOf<Physics::Box<dimensions>> { |
||||
inline constexpr static typename ShapeDimensionTraits<dimensions>::Type type() { |
||||
return ShapeDimensionTraits<dimensions>::Type::Box; |
||||
} |
||||
}; |
||||
template<> struct TypeOf<Physics::Plane> { |
||||
inline constexpr static typename ShapeDimensionTraits<3>::Type type() { |
||||
return ShapeDimensionTraits<3>::Type::Plane; |
||||
} |
||||
}; |
||||
template<UnsignedInt dimensions> struct TypeOf<Physics::ShapeGroup<dimensions>> { |
||||
inline constexpr static typename ShapeDimensionTraits<dimensions>::Type type() { |
||||
return ShapeDimensionTraits<dimensions>::Type::ShapeGroup; |
||||
} |
||||
}; |
||||
|
||||
/* Polymorphic shape wrappers */ |
||||
|
||||
template<UnsignedInt dimensions> struct MAGNUM_PHYSICS_EXPORT AbstractShape { |
||||
explicit AbstractShape(); |
||||
virtual ~AbstractShape(); |
||||
|
||||
virtual typename ShapeDimensionTraits<dimensions>::Type MAGNUM_PHYSICS_LOCAL type() const = 0; |
||||
virtual AbstractShape<dimensions> MAGNUM_PHYSICS_LOCAL * clone() const = 0; |
||||
virtual void MAGNUM_PHYSICS_LOCAL transform(const typename DimensionTraits<dimensions>::MatrixType& matrix, AbstractShape<dimensions>* result) const = 0; |
||||
}; |
||||
|
||||
template<class T> struct Shape: AbstractShape<T::Dimensions> { |
||||
T shape; |
||||
|
||||
explicit Shape() = default; |
||||
explicit Shape(const T& shape): shape(shape) {} |
||||
explicit Shape(T&& shape): shape(std::move(shape)) {} |
||||
|
||||
typename ShapeDimensionTraits<T::Dimensions>::Type type() const override { |
||||
return TypeOf<T>::type(); |
||||
} |
||||
|
||||
AbstractShape<T::Dimensions>* clone() const override { |
||||
return new Shape<T>(shape); |
||||
} |
||||
|
||||
void transform(const typename DimensionTraits<T::Dimensions>::MatrixType& matrix, AbstractShape<T::Dimensions>* result) const override { |
||||
CORRADE_INTERNAL_ASSERT(result->type() == type()); |
||||
static_cast<Shape<T>*>(result)->shape = shape.transformed(matrix); |
||||
} |
||||
}; |
||||
|
||||
}}} |
||||
|
||||
#endif |
||||
Loading…
Reference in new issue