From 972846677d9efd082c73b4deb70ce91545dc6bd5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Vladim=C3=ADr=20Vondru=C5=A1?= Date: Fri, 5 Oct 2012 16:37:41 +0200 Subject: [PATCH] Using DimensionTraits for Shape implementation. Removed equivalent typedefs from AbstractShape, using DimensionTraits everywhere except for internal storage. It would possibly allow to remove #include for specialized types from Shape headers. Also removed Doxygen workarounds for applyTransformation(), as both the pure virtual function and implementations have now the same signature. --- src/Physics/AbstractShape.h | 20 ++------------------ src/Physics/AxisAlignedBox.cpp | 4 ++-- src/Physics/AxisAlignedBox.h | 22 +++++++++------------- src/Physics/Box.cpp | 2 +- src/Physics/Box.h | 16 ++++++---------- src/Physics/Capsule.cpp | 8 ++++---- src/Physics/Capsule.h | 22 +++++++++------------- src/Physics/Line.cpp | 6 +++--- src/Physics/Line.h | 22 +++++++++------------- src/Physics/LineSegment.h | 2 +- src/Physics/Plane.h | 2 +- src/Physics/Point.cpp | 4 ++-- src/Physics/Point.h | 16 ++++++---------- src/Physics/ShapeGroup.cpp | 2 +- src/Physics/ShapeGroup.h | 6 +----- src/Physics/Sphere.cpp | 6 +++--- src/Physics/Sphere.h | 16 ++++++---------- 17 files changed, 66 insertions(+), 110 deletions(-) diff --git a/src/Physics/AbstractShape.h b/src/Physics/AbstractShape.h index f4b8ea370..62b60acfc 100644 --- a/src/Physics/AbstractShape.h +++ b/src/Physics/AbstractShape.h @@ -20,6 +20,7 @@ */ #include "Magnum.h" +#include "DimensionTraits.h" #include "magnumPhysicsVisibility.h" @@ -30,10 +31,6 @@ namespace Implementation { template struct ShapeDimensionTraits {}; template<> struct ShapeDimensionTraits<2> { - typedef Vector2 VectorType; - typedef Point2D PointType; - typedef Matrix3 MatrixType; - enum class Type { Point, Line, @@ -47,10 +44,6 @@ namespace Implementation { }; template<> struct ShapeDimensionTraits<3> { - typedef Vector3 VectorType; - typedef Point3D PointType; - typedef Matrix4 MatrixType; - enum class Type { Point, Line, @@ -74,15 +67,6 @@ See @ref collision-detection for brief introduction. */ template class PHYSICS_EXPORT AbstractShape { public: - /** @brief %Vector type for given dimension count */ - typedef typename Implementation::ShapeDimensionTraits::VectorType VectorType; - - /** @brief %Point type for given dimension count */ - typedef typename Implementation::ShapeDimensionTraits::PointType PointType; - - /** @brief %Matrix type for given dimension count */ - typedef typename Implementation::ShapeDimensionTraits::MatrixType MatrixType; - /** @brief Dimension count */ static const size_t Dimensions = dimensions; @@ -121,7 +105,7 @@ template class PHYSICS_EXPORT AbstractShape { * Applies transformation to user-defined shape properties and caches * them for later usage in collision detection. */ - virtual void applyTransformation(const MatrixType& transformation) = 0; + virtual void applyTransformation(const typename DimensionTraits::MatrixType& transformation) = 0; /** * @brief Detect collision with other shape diff --git a/src/Physics/AxisAlignedBox.cpp b/src/Physics/AxisAlignedBox.cpp index 49a7c919f..64b832394 100644 --- a/src/Physics/AxisAlignedBox.cpp +++ b/src/Physics/AxisAlignedBox.cpp @@ -20,8 +20,8 @@ namespace Magnum { namespace Physics { -template void AxisAlignedBox::applyTransformation(const typename AbstractShape::MatrixType& transformation) { - _transformedPosition = (transformation*typename AxisAlignedBox::PointType(_position)).vector(); +template void AxisAlignedBox::applyTransformation(const typename DimensionTraits::MatrixType& transformation) { + _transformedPosition = (transformation*typename DimensionTraits::PointType(_position)).vector(); _transformedSize = transformation.rotationScaling()*_size; } diff --git a/src/Physics/AxisAlignedBox.h b/src/Physics/AxisAlignedBox.h index 45dab8f70..f6b9e87c7 100644 --- a/src/Physics/AxisAlignedBox.h +++ b/src/Physics/AxisAlignedBox.h @@ -32,39 +32,35 @@ namespace Magnum { namespace Physics { template class PHYSICS_EXPORT AxisAlignedBox: public AbstractShape { public: /** @brief Constructor */ - inline AxisAlignedBox(const typename AbstractShape::VectorType& position, const typename AbstractShape::VectorType& size): _position(position), _transformedPosition(position), _size(size), _transformedSize(size) {} + inline AxisAlignedBox(const typename DimensionTraits::VectorType& position, const typename DimensionTraits::VectorType& size): _position(position), _transformedPosition(position), _size(size), _transformedSize(size) {} - #ifndef DOXYGEN_GENERATING_OUTPUT - void applyTransformation(const typename AbstractShape::MatrixType& transformation); - #else - void applyTransformation(const MatrixType& transformation); - #endif + void applyTransformation(const typename DimensionTraits::MatrixType& transformation); /** @brief Position */ - inline typename AbstractShape::VectorType position() const { + inline typename DimensionTraits::VectorType position() const { return _position; } /** @brief Set position */ - inline void setPosition(const typename AbstractShape::VectorType& position) { + inline void setPosition(const typename DimensionTraits::VectorType& position) { _position = position; } /** @brief Size */ - inline typename AbstractShape::VectorType size() const { return _size; } + inline typename DimensionTraits::VectorType size() const { return _size; } /** @brief Set size */ - inline void setSize(const typename AbstractShape::VectorType& size) { + inline void setSize(const typename DimensionTraits::VectorType& size) { _size = size; } /** @brief Transformed position */ - inline typename AbstractShape::VectorType transformedPosition() const { + inline typename DimensionTraits::VectorType transformedPosition() const { return _transformedPosition; } /** @brief Transformed size */ - inline typename AbstractShape::VectorType transformedSize() const { + inline typename DimensionTraits::VectorType transformedSize() const { return _transformedSize; } @@ -74,7 +70,7 @@ template class PHYSICS_EXPORT AxisAlignedBox: public Abstract } private: - typename AbstractShape::VectorType _position, _transformedPosition, + Math::Vector _position, _transformedPosition, _size, _transformedSize; }; diff --git a/src/Physics/Box.cpp b/src/Physics/Box.cpp index cc5d292bd..8b63ad5dd 100644 --- a/src/Physics/Box.cpp +++ b/src/Physics/Box.cpp @@ -20,7 +20,7 @@ namespace Magnum { namespace Physics { -template void Box::applyTransformation(const typename AbstractShape::MatrixType& transformation) { +template void Box::applyTransformation(const typename DimensionTraits::MatrixType& transformation) { _transformedTransformation = (transformation*_transformation); } diff --git a/src/Physics/Box.h b/src/Physics/Box.h index 02515d976..f52b265d1 100644 --- a/src/Physics/Box.h +++ b/src/Physics/Box.h @@ -33,26 +33,22 @@ namespace Magnum { namespace Physics { template class PHYSICS_EXPORT Box: public AbstractShape { public: /** @brief Constructor */ - inline Box(const typename AbstractShape::MatrixType& transformation): _transformation(transformation), _transformedTransformation(transformation) {} + inline Box(const typename DimensionTraits::MatrixType& transformation): _transformation(transformation), _transformedTransformation(transformation) {} - #ifndef DOXYGEN_GENERATING_OUTPUT - void applyTransformation(const typename AbstractShape::MatrixType& transformation); - #else - void applyTransformation(const MatrixType& transformation); - #endif + void applyTransformation(const typename DimensionTraits::MatrixType& transformation); /** @brief Transformation */ - inline typename AbstractShape::MatrixType transformation() const { + inline typename DimensionTraits::MatrixType transformation() const { return _transformation; } /** @brief Set transformation */ - inline void setTransformation(const typename AbstractShape::MatrixType& transformation) { + inline void setTransformation(const typename DimensionTraits::MatrixType& transformation) { _transformation = transformation; } /** @brief Transformed transformation */ - inline typename AbstractShape::MatrixType transformedTransformation() const { + inline typename DimensionTraits::MatrixType transformedTransformation() const { return _transformedTransformation; } @@ -62,7 +58,7 @@ template class PHYSICS_EXPORT Box: public AbstractShape::MatrixType _transformation, + Math::Matrix _transformation, _transformedTransformation; }; diff --git a/src/Physics/Capsule.cpp b/src/Physics/Capsule.cpp index 9dd3715a4..f05b19858 100644 --- a/src/Physics/Capsule.cpp +++ b/src/Physics/Capsule.cpp @@ -27,10 +27,10 @@ using namespace Magnum::Math::Geometry; namespace Magnum { namespace Physics { -template void Capsule::applyTransformation(const typename AbstractShape::MatrixType& transformation) { - _transformedA = (transformation*typename AbstractShape::PointType(_a)).vector(); - _transformedB = (transformation*typename AbstractShape::PointType(_b)).vector(); - float scaling = (transformation.rotationScaling()*typename AbstractShape::VectorType(1/Math::Constants::sqrt3())).length(); +template void Capsule::applyTransformation(const typename DimensionTraits::MatrixType& transformation) { + _transformedA = (transformation*typename DimensionTraits::PointType(_a)).vector(); + _transformedB = (transformation*typename DimensionTraits::PointType(_b)).vector(); + float scaling = (transformation.rotationScaling()*typename DimensionTraits::VectorType(1/Math::Constants::sqrt3())).length(); _transformedRadius = scaling*_radius; } diff --git a/src/Physics/Capsule.h b/src/Physics/Capsule.h index 76bfc828f..93b950527 100644 --- a/src/Physics/Capsule.h +++ b/src/Physics/Capsule.h @@ -37,33 +37,29 @@ applying transformation, the scale factor is averaged from all axes. template class PHYSICS_EXPORT Capsule: public AbstractShape { public: /** @brief Constructor */ - inline Capsule(const typename AbstractShape::VectorType& a, const typename AbstractShape::VectorType& b, float radius): _a(a), _transformedA(a), _b(b), _transformedB(b), _radius(radius), _transformedRadius(radius) {} + inline Capsule(const typename DimensionTraits::VectorType& a, const typename DimensionTraits::VectorType& b, float radius): _a(a), _transformedA(a), _b(b), _transformedB(b), _radius(radius), _transformedRadius(radius) {} - #ifndef DOXYGEN_GENERATING_OUTPUT - void applyTransformation(const typename AbstractShape::MatrixType& transformation); - #else - void applyTransformation(const MatrixType& transformation); - #endif + void applyTransformation(const typename DimensionTraits::MatrixType& transformation); bool collides(const AbstractShape* other) const; /** @brief Start point */ - inline typename AbstractShape::VectorType a() const { + inline typename DimensionTraits::VectorType a() const { return _a; } /** @brief End point */ - inline typename AbstractShape::VectorType b() const { + inline typename DimensionTraits::VectorType b() const { return _a; } /** @brief Set start point */ - inline void setA(const typename AbstractShape::VectorType& a) { + inline void setA(const typename DimensionTraits::VectorType& a) { _a = a; } /** @brief Set end point */ - inline void setB(const typename AbstractShape::VectorType& b) { + inline void setB(const typename DimensionTraits::VectorType& b) { _b = b; } @@ -74,12 +70,12 @@ template class PHYSICS_EXPORT Capsule: public AbstractShape::VectorType transformedA() const { + inline typename DimensionTraits::VectorType transformedA() const { return _transformedA; } /** @brief Transformed second point */ - inline typename AbstractShape::VectorType transformedB() const { + inline typename DimensionTraits::VectorType transformedB() const { return _transformedB; } @@ -100,7 +96,7 @@ template class PHYSICS_EXPORT Capsule: public AbstractShape::VectorType _a, _transformedA, + Math::Vector _a, _transformedA, _b, _transformedB; float _radius, _transformedRadius; }; diff --git a/src/Physics/Line.cpp b/src/Physics/Line.cpp index 5adf4f61a..bfe5362ed 100644 --- a/src/Physics/Line.cpp +++ b/src/Physics/Line.cpp @@ -20,9 +20,9 @@ namespace Magnum { namespace Physics { -template void Line::applyTransformation(const typename AbstractShape::MatrixType& transformation) { - _transformedA = (transformation*typename AbstractShape::PointType(_a)).vector(); - _transformedB = (transformation*typename AbstractShape::PointType(_b)).vector(); +template void Line::applyTransformation(const typename DimensionTraits::MatrixType& transformation) { + _transformedA = (transformation*typename DimensionTraits::PointType(_a)).vector(); + _transformedB = (transformation*typename DimensionTraits::PointType(_b)).vector(); } /* Explicitly instantiate the templates */ diff --git a/src/Physics/Line.h b/src/Physics/Line.h index 4e43b98a9..90332cf9a 100644 --- a/src/Physics/Line.h +++ b/src/Physics/Line.h @@ -33,41 +33,37 @@ namespace Magnum { namespace Physics { template class PHYSICS_EXPORT Line: public AbstractShape { public: /** @brief Constructor */ - inline Line(const typename AbstractShape::VectorType& a, const typename AbstractShape::VectorType& b): _a(a), _transformedA(a), _b(b), _transformedB(b) {} + inline Line(const typename DimensionTraits::VectorType& a, const typename DimensionTraits::VectorType& b): _a(a), _transformedA(a), _b(b), _transformedB(b) {} - #ifndef DOXYGEN_GENERATING_OUTPUT - void applyTransformation(const typename AbstractShape::MatrixType& transformation); - #else - void applyTransformation(const MatrixType& transformation); - #endif + void applyTransformation(const typename DimensionTraits::MatrixType& transformation); /** @brief First point */ - inline typename AbstractShape::VectorType a() const { + inline typename DimensionTraits::VectorType a() const { return _a; } /** @brief Second point */ - inline typename AbstractShape::VectorType b() const { + inline typename DimensionTraits::VectorType b() const { return _a; } /** @brief Set first point */ - inline void setA(const typename AbstractShape::VectorType& a) { + inline void setA(const typename DimensionTraits::VectorType& a) { _a = a; } /** @brief Set second point */ - inline void setB(const typename AbstractShape::VectorType& b) { + inline void setB(const typename DimensionTraits::VectorType& b) { _b = b; } /** @brief Transformed first point */ - inline typename AbstractShape::VectorType transformedA() const { + inline typename DimensionTraits::VectorType transformedA() const { return _transformedA; } /** @brief Transformed second point */ - inline typename AbstractShape::VectorType transformedB() const { + inline typename DimensionTraits::VectorType transformedB() const { return _transformedB; } @@ -77,7 +73,7 @@ template class PHYSICS_EXPORT Line: public AbstractShape::VectorType _a, _transformedA, + Math::Vector _a, _transformedA, _b, _transformedB; }; diff --git a/src/Physics/LineSegment.h b/src/Physics/LineSegment.h index c7d59863d..407fdc382 100644 --- a/src/Physics/LineSegment.h +++ b/src/Physics/LineSegment.h @@ -31,7 +31,7 @@ namespace Magnum { namespace Physics { template class LineSegment: public Line { public: /** @brief Constructor */ - inline LineSegment(const typename AbstractShape::VectorType& a, const typename AbstractShape::VectorType& b): Line(a, b) {} + inline LineSegment(const typename DimensionTraits::VectorType& a, const typename DimensionTraits::VectorType& b): Line(a, b) {} protected: inline typename AbstractShape::Type type() const { return AbstractShape::Type::LineSegment; } diff --git a/src/Physics/Plane.h b/src/Physics/Plane.h index 64e7c116e..8c547dc8e 100644 --- a/src/Physics/Plane.h +++ b/src/Physics/Plane.h @@ -39,7 +39,7 @@ class PHYSICS_EXPORT Plane: public AbstractShape<3> { void applyTransformation(const Matrix4& transformation); bool collides(const AbstractShape<3>* other) const; #else - void applyTransformation(const MatrixType& transformation); + void applyTransformation(const typename DimensionTraits::MatrixType& transformation); bool collides(const AbstractShape* other) const; #endif diff --git a/src/Physics/Point.cpp b/src/Physics/Point.cpp index 67657450c..f2a15267f 100644 --- a/src/Physics/Point.cpp +++ b/src/Physics/Point.cpp @@ -20,8 +20,8 @@ namespace Magnum { namespace Physics { -template void Point::applyTransformation(const typename AbstractShape::MatrixType& transformation) { - _transformedPosition = (transformation*typename AbstractShape::PointType(_position)).vector(); +template void Point::applyTransformation(const typename DimensionTraits::MatrixType& transformation) { + _transformedPosition = (transformation*typename DimensionTraits::PointType(_position)).vector(); } template class Point<2>; diff --git a/src/Physics/Point.h b/src/Physics/Point.h index 05483d9b3..f390c4f02 100644 --- a/src/Physics/Point.h +++ b/src/Physics/Point.h @@ -32,26 +32,22 @@ namespace Magnum { namespace Physics { template class PHYSICS_EXPORT Point: public AbstractShape { public: /** @brief Constructor */ - inline Point(const typename AbstractShape::VectorType& position): _position(position), _transformedPosition(position) {} + inline Point(const typename DimensionTraits::VectorType& position): _position(position), _transformedPosition(position) {} - #ifndef DOXYGEN_GENERATING_OUTPUT - void applyTransformation(const typename AbstractShape::MatrixType& transformation); - #else - void applyTransformation(const MatrixType& transformation); - #endif + void applyTransformation(const typename DimensionTraits::MatrixType& transformation); /** @brief Position */ - inline typename AbstractShape::VectorType position() const { + inline typename DimensionTraits::VectorType position() const { return _position; } /** @brief Set position */ - inline void setPosition(const typename AbstractShape::VectorType& position) { + inline void setPosition(const typename DimensionTraits::VectorType& position) { _position = position; } /** @brief Transformed position */ - inline typename AbstractShape::VectorType transformedPosition() const { + inline typename DimensionTraits::VectorType transformedPosition() const { return _transformedPosition; } @@ -59,7 +55,7 @@ template class PHYSICS_EXPORT Point: public AbstractShape::Type type() const { return AbstractShape::Type::Point; } private: - typename AbstractShape::VectorType _position, _transformedPosition; + Math::Vector _position, _transformedPosition; }; #ifndef DOXYGEN_GENERATING_OUTPUT diff --git a/src/Physics/ShapeGroup.cpp b/src/Physics/ShapeGroup.cpp index 813baf7bd..be94a88f2 100644 --- a/src/Physics/ShapeGroup.cpp +++ b/src/Physics/ShapeGroup.cpp @@ -43,7 +43,7 @@ template ShapeGroup& ShapeGroup::oper return *this; } -template void ShapeGroup::applyTransformation(const typename AbstractShape::MatrixType& transformation) { +template void ShapeGroup::applyTransformation(const typename DimensionTraits::MatrixType& transformation) { if(a) a->applyTransformation(transformation); if(b) b->applyTransformation(transformation); } diff --git a/src/Physics/ShapeGroup.h b/src/Physics/ShapeGroup.h index 200898761..83d19b920 100644 --- a/src/Physics/ShapeGroup.h +++ b/src/Physics/ShapeGroup.h @@ -99,11 +99,7 @@ template class PHYSICS_EXPORT ShapeGroup: public AbstractShap /** @brief Move assignment */ ShapeGroup& operator=(ShapeGroup&& other); - #ifndef DOXYGEN_GENERATING_OUTPUT - void applyTransformation(const typename AbstractShape::MatrixType& transformation); - #else - void applyTransformation(const MatrixType& transformation); - #endif + void applyTransformation(const typename DimensionTraits::MatrixType& transformation); bool collides(const AbstractShape* other) const; diff --git a/src/Physics/Sphere.cpp b/src/Physics/Sphere.cpp index 6015f5950..af9a668a2 100644 --- a/src/Physics/Sphere.cpp +++ b/src/Physics/Sphere.cpp @@ -28,7 +28,7 @@ using namespace Magnum::Math::Geometry; namespace Magnum { namespace Physics { namespace { - template static typename AbstractShape::VectorType unitVector(); + template static typename DimensionTraits::VectorType unitVector(); template<> inline Vector2 unitVector<2>() { return Vector2(1/Math::Constants::sqrt2()); @@ -39,8 +39,8 @@ namespace { } } -template void Sphere::applyTransformation(const typename AbstractShape::MatrixType& transformation) { - _transformedPosition = (transformation*typename AbstractShape::PointType(_position)).vector(); +template void Sphere::applyTransformation(const typename DimensionTraits::MatrixType& transformation) { + _transformedPosition = (transformation*typename DimensionTraits::PointType(_position)).vector(); float scaling = (transformation.rotationScaling()*unitVector()).length(); _transformedRadius = scaling*_radius; } diff --git a/src/Physics/Sphere.h b/src/Physics/Sphere.h index 60639a37d..dda4e5161 100644 --- a/src/Physics/Sphere.h +++ b/src/Physics/Sphere.h @@ -38,23 +38,19 @@ applying transformation, the scale factor is averaged from all axes. template class PHYSICS_EXPORT Sphere: public AbstractShape { public: /** @brief Constructor */ - inline Sphere(const typename AbstractShape::VectorType& position, float radius): _position(position), _transformedPosition(position), _radius(radius), _transformedRadius(radius) {} + inline Sphere(const typename DimensionTraits::VectorType& position, float radius): _position(position), _transformedPosition(position), _radius(radius), _transformedRadius(radius) {} - #ifndef DOXYGEN_GENERATING_OUTPUT - void applyTransformation(const typename AbstractShape::MatrixType& transformation); - #else - void applyTransformation(const MatrixType& transformation); - #endif + void applyTransformation(const typename DimensionTraits::MatrixType& transformation); bool collides(const AbstractShape* other) const; /** @brief Position */ - inline typename AbstractShape::VectorType position() const { + inline typename DimensionTraits::VectorType position() const { return _position; } /** @brief Set position */ - inline void setPosition(const typename AbstractShape::VectorType& position) { + inline void setPosition(const typename DimensionTraits::VectorType& position) { _position = position; } @@ -65,7 +61,7 @@ template class PHYSICS_EXPORT Sphere: public AbstractShape::VectorType transformedPosition() const { + inline typename DimensionTraits::VectorType transformedPosition() const { return _transformedPosition; } @@ -92,7 +88,7 @@ template class PHYSICS_EXPORT Sphere: public AbstractShape::VectorType _position, + Math::Vector _position, _transformedPosition; float _radius, _transformedRadius; };