Browse Source

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.
vectorfields
Vladimír Vondruš 14 years ago
parent
commit
972846677d
  1. 20
      src/Physics/AbstractShape.h
  2. 4
      src/Physics/AxisAlignedBox.cpp
  3. 22
      src/Physics/AxisAlignedBox.h
  4. 2
      src/Physics/Box.cpp
  5. 16
      src/Physics/Box.h
  6. 8
      src/Physics/Capsule.cpp
  7. 22
      src/Physics/Capsule.h
  8. 6
      src/Physics/Line.cpp
  9. 22
      src/Physics/Line.h
  10. 2
      src/Physics/LineSegment.h
  11. 2
      src/Physics/Plane.h
  12. 4
      src/Physics/Point.cpp
  13. 16
      src/Physics/Point.h
  14. 2
      src/Physics/ShapeGroup.cpp
  15. 6
      src/Physics/ShapeGroup.h
  16. 6
      src/Physics/Sphere.cpp
  17. 16
      src/Physics/Sphere.h

20
src/Physics/AbstractShape.h

@ -20,6 +20,7 @@
*/
#include "Magnum.h"
#include "DimensionTraits.h"
#include "magnumPhysicsVisibility.h"
@ -30,10 +31,6 @@ namespace Implementation {
template<size_t dimensions> 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<size_t dimensions> class PHYSICS_EXPORT AbstractShape {
public:
/** @brief %Vector type for given dimension count */
typedef typename Implementation::ShapeDimensionTraits<dimensions>::VectorType VectorType;
/** @brief %Point type for given dimension count */
typedef typename Implementation::ShapeDimensionTraits<dimensions>::PointType PointType;
/** @brief %Matrix type for given dimension count */
typedef typename Implementation::ShapeDimensionTraits<dimensions>::MatrixType MatrixType;
/** @brief Dimension count */
static const size_t Dimensions = dimensions;
@ -121,7 +105,7 @@ template<size_t dimensions> 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<dimensions, GLfloat>::MatrixType& transformation) = 0;
/**
* @brief Detect collision with other shape

4
src/Physics/AxisAlignedBox.cpp

@ -20,8 +20,8 @@
namespace Magnum { namespace Physics {
template<size_t dimensions> void AxisAlignedBox<dimensions>::applyTransformation(const typename AbstractShape<dimensions>::MatrixType& transformation) {
_transformedPosition = (transformation*typename AxisAlignedBox<dimensions>::PointType(_position)).vector();
template<size_t dimensions> void AxisAlignedBox<dimensions>::applyTransformation(const typename DimensionTraits<dimensions, GLfloat>::MatrixType& transformation) {
_transformedPosition = (transformation*typename DimensionTraits<dimensions, GLfloat>::PointType(_position)).vector();
_transformedSize = transformation.rotationScaling()*_size;
}

22
src/Physics/AxisAlignedBox.h

@ -32,39 +32,35 @@ namespace Magnum { namespace Physics {
template<size_t dimensions> class PHYSICS_EXPORT AxisAlignedBox: public AbstractShape<dimensions> {
public:
/** @brief Constructor */
inline AxisAlignedBox(const typename AbstractShape<dimensions>::VectorType& position, const typename AbstractShape<dimensions>::VectorType& size): _position(position), _transformedPosition(position), _size(size), _transformedSize(size) {}
inline AxisAlignedBox(const typename DimensionTraits<dimensions, GLfloat>::VectorType& position, const typename DimensionTraits<dimensions, GLfloat>::VectorType& size): _position(position), _transformedPosition(position), _size(size), _transformedSize(size) {}
#ifndef DOXYGEN_GENERATING_OUTPUT
void applyTransformation(const typename AbstractShape<dimensions>::MatrixType& transformation);
#else
void applyTransformation(const MatrixType& transformation);
#endif
void applyTransformation(const typename DimensionTraits<dimensions, GLfloat>::MatrixType& transformation);
/** @brief Position */
inline typename AbstractShape<dimensions>::VectorType position() const {
inline typename DimensionTraits<dimensions, GLfloat>::VectorType position() const {
return _position;
}
/** @brief Set position */
inline void setPosition(const typename AbstractShape<dimensions>::VectorType& position) {
inline void setPosition(const typename DimensionTraits<dimensions, GLfloat>::VectorType& position) {
_position = position;
}
/** @brief Size */
inline typename AbstractShape<dimensions>::VectorType size() const { return _size; }
inline typename DimensionTraits<dimensions, GLfloat>::VectorType size() const { return _size; }
/** @brief Set size */
inline void setSize(const typename AbstractShape<dimensions>::VectorType& size) {
inline void setSize(const typename DimensionTraits<dimensions, GLfloat>::VectorType& size) {
_size = size;
}
/** @brief Transformed position */
inline typename AbstractShape<dimensions>::VectorType transformedPosition() const {
inline typename DimensionTraits<dimensions, GLfloat>::VectorType transformedPosition() const {
return _transformedPosition;
}
/** @brief Transformed size */
inline typename AbstractShape<dimensions>::VectorType transformedSize() const {
inline typename DimensionTraits<dimensions, GLfloat>::VectorType transformedSize() const {
return _transformedSize;
}
@ -74,7 +70,7 @@ template<size_t dimensions> class PHYSICS_EXPORT AxisAlignedBox: public Abstract
}
private:
typename AbstractShape<dimensions>::VectorType _position, _transformedPosition,
Math::Vector<dimensions, GLfloat> _position, _transformedPosition,
_size, _transformedSize;
};

2
src/Physics/Box.cpp

@ -20,7 +20,7 @@
namespace Magnum { namespace Physics {
template<size_t dimensions> void Box<dimensions>::applyTransformation(const typename AbstractShape<dimensions>::MatrixType& transformation) {
template<size_t dimensions> void Box<dimensions>::applyTransformation(const typename DimensionTraits<dimensions, GLfloat>::MatrixType& transformation) {
_transformedTransformation = (transformation*_transformation);
}

16
src/Physics/Box.h

@ -33,26 +33,22 @@ namespace Magnum { namespace Physics {
template<size_t dimensions> class PHYSICS_EXPORT Box: public AbstractShape<dimensions> {
public:
/** @brief Constructor */
inline Box(const typename AbstractShape<dimensions>::MatrixType& transformation): _transformation(transformation), _transformedTransformation(transformation) {}
inline Box(const typename DimensionTraits<dimensions, GLfloat>::MatrixType& transformation): _transformation(transformation), _transformedTransformation(transformation) {}
#ifndef DOXYGEN_GENERATING_OUTPUT
void applyTransformation(const typename AbstractShape<dimensions>::MatrixType& transformation);
#else
void applyTransformation(const MatrixType& transformation);
#endif
void applyTransformation(const typename DimensionTraits<dimensions, GLfloat>::MatrixType& transformation);
/** @brief Transformation */
inline typename AbstractShape<dimensions>::MatrixType transformation() const {
inline typename DimensionTraits<dimensions, GLfloat>::MatrixType transformation() const {
return _transformation;
}
/** @brief Set transformation */
inline void setTransformation(const typename AbstractShape<dimensions>::MatrixType& transformation) {
inline void setTransformation(const typename DimensionTraits<dimensions, GLfloat>::MatrixType& transformation) {
_transformation = transformation;
}
/** @brief Transformed transformation */
inline typename AbstractShape<dimensions>::MatrixType transformedTransformation() const {
inline typename DimensionTraits<dimensions, GLfloat>::MatrixType transformedTransformation() const {
return _transformedTransformation;
}
@ -62,7 +58,7 @@ template<size_t dimensions> class PHYSICS_EXPORT Box: public AbstractShape<dimen
}
private:
typename AbstractShape<dimensions>::MatrixType _transformation,
Math::Matrix<dimensions+1, GLfloat> _transformation,
_transformedTransformation;
};

8
src/Physics/Capsule.cpp

@ -27,10 +27,10 @@ using namespace Magnum::Math::Geometry;
namespace Magnum { namespace Physics {
template<size_t dimensions> void Capsule<dimensions>::applyTransformation(const typename AbstractShape<dimensions>::MatrixType& transformation) {
_transformedA = (transformation*typename AbstractShape<dimensions>::PointType(_a)).vector();
_transformedB = (transformation*typename AbstractShape<dimensions>::PointType(_b)).vector();
float scaling = (transformation.rotationScaling()*typename AbstractShape<dimensions>::VectorType(1/Math::Constants<float>::sqrt3())).length();
template<size_t dimensions> void Capsule<dimensions>::applyTransformation(const typename DimensionTraits<dimensions, GLfloat>::MatrixType& transformation) {
_transformedA = (transformation*typename DimensionTraits<dimensions, GLfloat>::PointType(_a)).vector();
_transformedB = (transformation*typename DimensionTraits<dimensions, GLfloat>::PointType(_b)).vector();
float scaling = (transformation.rotationScaling()*typename DimensionTraits<dimensions, GLfloat>::VectorType(1/Math::Constants<float>::sqrt3())).length();
_transformedRadius = scaling*_radius;
}

22
src/Physics/Capsule.h

@ -37,33 +37,29 @@ applying transformation, the scale factor is averaged from all axes.
template<size_t dimensions> class PHYSICS_EXPORT Capsule: public AbstractShape<dimensions> {
public:
/** @brief Constructor */
inline Capsule(const typename AbstractShape<dimensions>::VectorType& a, const typename AbstractShape<dimensions>::VectorType& b, float radius): _a(a), _transformedA(a), _b(b), _transformedB(b), _radius(radius), _transformedRadius(radius) {}
inline Capsule(const typename DimensionTraits<dimensions, GLfloat>::VectorType& a, const typename DimensionTraits<dimensions, GLfloat>::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<dimensions>::MatrixType& transformation);
#else
void applyTransformation(const MatrixType& transformation);
#endif
void applyTransformation(const typename DimensionTraits<dimensions, GLfloat>::MatrixType& transformation);
bool collides(const AbstractShape<dimensions>* other) const;
/** @brief Start point */
inline typename AbstractShape<dimensions>::VectorType a() const {
inline typename DimensionTraits<dimensions, GLfloat>::VectorType a() const {
return _a;
}
/** @brief End point */
inline typename AbstractShape<dimensions>::VectorType b() const {
inline typename DimensionTraits<dimensions, GLfloat>::VectorType b() const {
return _a;
}
/** @brief Set start point */
inline void setA(const typename AbstractShape<dimensions>::VectorType& a) {
inline void setA(const typename DimensionTraits<dimensions, GLfloat>::VectorType& a) {
_a = a;
}
/** @brief Set end point */
inline void setB(const typename AbstractShape<dimensions>::VectorType& b) {
inline void setB(const typename DimensionTraits<dimensions, GLfloat>::VectorType& b) {
_b = b;
}
@ -74,12 +70,12 @@ template<size_t dimensions> class PHYSICS_EXPORT Capsule: public AbstractShape<d
inline void setRadius(float radius) { _radius = radius; }
/** @brief Transformed first point */
inline typename AbstractShape<dimensions>::VectorType transformedA() const {
inline typename DimensionTraits<dimensions, GLfloat>::VectorType transformedA() const {
return _transformedA;
}
/** @brief Transformed second point */
inline typename AbstractShape<dimensions>::VectorType transformedB() const {
inline typename DimensionTraits<dimensions, GLfloat>::VectorType transformedB() const {
return _transformedB;
}
@ -100,7 +96,7 @@ template<size_t dimensions> class PHYSICS_EXPORT Capsule: public AbstractShape<d
}
private:
typename AbstractShape<dimensions>::VectorType _a, _transformedA,
Math::Vector<dimensions, GLfloat> _a, _transformedA,
_b, _transformedB;
float _radius, _transformedRadius;
};

6
src/Physics/Line.cpp

@ -20,9 +20,9 @@
namespace Magnum { namespace Physics {
template<size_t dimensions> void Line<dimensions>::applyTransformation(const typename AbstractShape<dimensions>::MatrixType& transformation) {
_transformedA = (transformation*typename AbstractShape<dimensions>::PointType(_a)).vector();
_transformedB = (transformation*typename AbstractShape<dimensions>::PointType(_b)).vector();
template<size_t dimensions> void Line<dimensions>::applyTransformation(const typename DimensionTraits<dimensions, GLfloat>::MatrixType& transformation) {
_transformedA = (transformation*typename DimensionTraits<dimensions, GLfloat>::PointType(_a)).vector();
_transformedB = (transformation*typename DimensionTraits<dimensions, GLfloat>::PointType(_b)).vector();
}
/* Explicitly instantiate the templates */

22
src/Physics/Line.h

@ -33,41 +33,37 @@ namespace Magnum { namespace Physics {
template<size_t dimensions> class PHYSICS_EXPORT Line: public AbstractShape<dimensions> {
public:
/** @brief Constructor */
inline Line(const typename AbstractShape<dimensions>::VectorType& a, const typename AbstractShape<dimensions>::VectorType& b): _a(a), _transformedA(a), _b(b), _transformedB(b) {}
inline Line(const typename DimensionTraits<dimensions, GLfloat>::VectorType& a, const typename DimensionTraits<dimensions, GLfloat>::VectorType& b): _a(a), _transformedA(a), _b(b), _transformedB(b) {}
#ifndef DOXYGEN_GENERATING_OUTPUT
void applyTransformation(const typename AbstractShape<dimensions>::MatrixType& transformation);
#else
void applyTransformation(const MatrixType& transformation);
#endif
void applyTransformation(const typename DimensionTraits<dimensions, GLfloat>::MatrixType& transformation);
/** @brief First point */
inline typename AbstractShape<dimensions>::VectorType a() const {
inline typename DimensionTraits<dimensions, GLfloat>::VectorType a() const {
return _a;
}
/** @brief Second point */
inline typename AbstractShape<dimensions>::VectorType b() const {
inline typename DimensionTraits<dimensions, GLfloat>::VectorType b() const {
return _a;
}
/** @brief Set first point */
inline void setA(const typename AbstractShape<dimensions>::VectorType& a) {
inline void setA(const typename DimensionTraits<dimensions, GLfloat>::VectorType& a) {
_a = a;
}
/** @brief Set second point */
inline void setB(const typename AbstractShape<dimensions>::VectorType& b) {
inline void setB(const typename DimensionTraits<dimensions, GLfloat>::VectorType& b) {
_b = b;
}
/** @brief Transformed first point */
inline typename AbstractShape<dimensions>::VectorType transformedA() const {
inline typename DimensionTraits<dimensions, GLfloat>::VectorType transformedA() const {
return _transformedA;
}
/** @brief Transformed second point */
inline typename AbstractShape<dimensions>::VectorType transformedB() const {
inline typename DimensionTraits<dimensions, GLfloat>::VectorType transformedB() const {
return _transformedB;
}
@ -77,7 +73,7 @@ template<size_t dimensions> class PHYSICS_EXPORT Line: public AbstractShape<dime
}
private:
typename AbstractShape<dimensions>::VectorType _a, _transformedA,
Math::Vector<dimensions, GLfloat> _a, _transformedA,
_b, _transformedB;
};

2
src/Physics/LineSegment.h

@ -31,7 +31,7 @@ namespace Magnum { namespace Physics {
template<size_t dimensions> class LineSegment: public Line<dimensions> {
public:
/** @brief Constructor */
inline LineSegment(const typename AbstractShape<dimensions>::VectorType& a, const typename AbstractShape<dimensions>::VectorType& b): Line<dimensions>(a, b) {}
inline LineSegment(const typename DimensionTraits<dimensions, GLfloat>::VectorType& a, const typename DimensionTraits<dimensions, GLfloat>::VectorType& b): Line<dimensions>(a, b) {}
protected:
inline typename AbstractShape<dimensions>::Type type() const { return AbstractShape<dimensions>::Type::LineSegment; }

2
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<dimensions, GLfloat>::MatrixType& transformation);
bool collides(const AbstractShape* other) const;
#endif

4
src/Physics/Point.cpp

@ -20,8 +20,8 @@
namespace Magnum { namespace Physics {
template<size_t dimensions> void Point<dimensions>::applyTransformation(const typename AbstractShape<dimensions>::MatrixType& transformation) {
_transformedPosition = (transformation*typename AbstractShape<dimensions>::PointType(_position)).vector();
template<size_t dimensions> void Point<dimensions>::applyTransformation(const typename DimensionTraits<dimensions, GLfloat>::MatrixType& transformation) {
_transformedPosition = (transformation*typename DimensionTraits<dimensions, GLfloat>::PointType(_position)).vector();
}
template class Point<2>;

16
src/Physics/Point.h

@ -32,26 +32,22 @@ namespace Magnum { namespace Physics {
template<size_t dimensions> class PHYSICS_EXPORT Point: public AbstractShape<dimensions> {
public:
/** @brief Constructor */
inline Point(const typename AbstractShape<dimensions>::VectorType& position): _position(position), _transformedPosition(position) {}
inline Point(const typename DimensionTraits<dimensions, GLfloat>::VectorType& position): _position(position), _transformedPosition(position) {}
#ifndef DOXYGEN_GENERATING_OUTPUT
void applyTransformation(const typename AbstractShape<dimensions>::MatrixType& transformation);
#else
void applyTransformation(const MatrixType& transformation);
#endif
void applyTransformation(const typename DimensionTraits<dimensions, GLfloat>::MatrixType& transformation);
/** @brief Position */
inline typename AbstractShape<dimensions>::VectorType position() const {
inline typename DimensionTraits<dimensions, GLfloat>::VectorType position() const {
return _position;
}
/** @brief Set position */
inline void setPosition(const typename AbstractShape<dimensions>::VectorType& position) {
inline void setPosition(const typename DimensionTraits<dimensions, GLfloat>::VectorType& position) {
_position = position;
}
/** @brief Transformed position */
inline typename AbstractShape<dimensions>::VectorType transformedPosition() const {
inline typename DimensionTraits<dimensions, GLfloat>::VectorType transformedPosition() const {
return _transformedPosition;
}
@ -59,7 +55,7 @@ template<size_t dimensions> class PHYSICS_EXPORT Point: public AbstractShape<dim
inline typename AbstractShape<dimensions>::Type type() const { return AbstractShape<dimensions>::Type::Point; }
private:
typename AbstractShape<dimensions>::VectorType _position, _transformedPosition;
Math::Vector<dimensions, GLfloat> _position, _transformedPosition;
};
#ifndef DOXYGEN_GENERATING_OUTPUT

2
src/Physics/ShapeGroup.cpp

@ -43,7 +43,7 @@ template<size_t dimensions> ShapeGroup<dimensions>& ShapeGroup<dimensions>::oper
return *this;
}
template<size_t dimensions> void ShapeGroup<dimensions>::applyTransformation(const typename AbstractShape<dimensions>::MatrixType& transformation) {
template<size_t dimensions> void ShapeGroup<dimensions>::applyTransformation(const typename DimensionTraits<dimensions, GLfloat>::MatrixType& transformation) {
if(a) a->applyTransformation(transformation);
if(b) b->applyTransformation(transformation);
}

6
src/Physics/ShapeGroup.h

@ -99,11 +99,7 @@ template<size_t dimensions> class PHYSICS_EXPORT ShapeGroup: public AbstractShap
/** @brief Move assignment */
ShapeGroup& operator=(ShapeGroup&& other);
#ifndef DOXYGEN_GENERATING_OUTPUT
void applyTransformation(const typename AbstractShape<dimensions>::MatrixType& transformation);
#else
void applyTransformation(const MatrixType& transformation);
#endif
void applyTransformation(const typename DimensionTraits<dimensions, GLfloat>::MatrixType& transformation);
bool collides(const AbstractShape<dimensions>* other) const;

6
src/Physics/Sphere.cpp

@ -28,7 +28,7 @@ using namespace Magnum::Math::Geometry;
namespace Magnum { namespace Physics {
namespace {
template<size_t dimensions> static typename AbstractShape<dimensions>::VectorType unitVector();
template<size_t dimensions> static typename DimensionTraits<dimensions, GLfloat>::VectorType unitVector();
template<> inline Vector2 unitVector<2>() {
return Vector2(1/Math::Constants<float>::sqrt2());
@ -39,8 +39,8 @@ namespace {
}
}
template<size_t dimensions> void Sphere<dimensions>::applyTransformation(const typename AbstractShape<dimensions>::MatrixType& transformation) {
_transformedPosition = (transformation*typename AbstractShape<dimensions>::PointType(_position)).vector();
template<size_t dimensions> void Sphere<dimensions>::applyTransformation(const typename DimensionTraits<dimensions, GLfloat>::MatrixType& transformation) {
_transformedPosition = (transformation*typename DimensionTraits<dimensions, GLfloat>::PointType(_position)).vector();
float scaling = (transformation.rotationScaling()*unitVector<dimensions>()).length();
_transformedRadius = scaling*_radius;
}

16
src/Physics/Sphere.h

@ -38,23 +38,19 @@ applying transformation, the scale factor is averaged from all axes.
template<size_t dimensions> class PHYSICS_EXPORT Sphere: public AbstractShape<dimensions> {
public:
/** @brief Constructor */
inline Sphere(const typename AbstractShape<dimensions>::VectorType& position, float radius): _position(position), _transformedPosition(position), _radius(radius), _transformedRadius(radius) {}
inline Sphere(const typename DimensionTraits<dimensions, GLfloat>::VectorType& position, float radius): _position(position), _transformedPosition(position), _radius(radius), _transformedRadius(radius) {}
#ifndef DOXYGEN_GENERATING_OUTPUT
void applyTransformation(const typename AbstractShape<dimensions>::MatrixType& transformation);
#else
void applyTransformation(const MatrixType& transformation);
#endif
void applyTransformation(const typename DimensionTraits<dimensions, GLfloat>::MatrixType& transformation);
bool collides(const AbstractShape<dimensions>* other) const;
/** @brief Position */
inline typename AbstractShape<dimensions>::VectorType position() const {
inline typename DimensionTraits<dimensions, GLfloat>::VectorType position() const {
return _position;
}
/** @brief Set position */
inline void setPosition(const typename AbstractShape<dimensions>::VectorType& position) {
inline void setPosition(const typename DimensionTraits<dimensions, GLfloat>::VectorType& position) {
_position = position;
}
@ -65,7 +61,7 @@ template<size_t dimensions> class PHYSICS_EXPORT Sphere: public AbstractShape<di
inline void setRadius(float radius) { _radius = radius; }
/** @brief Transformed position */
inline typename AbstractShape<dimensions>::VectorType transformedPosition() const {
inline typename DimensionTraits<dimensions, GLfloat>::VectorType transformedPosition() const {
return _transformedPosition;
}
@ -92,7 +88,7 @@ template<size_t dimensions> class PHYSICS_EXPORT Sphere: public AbstractShape<di
}
private:
typename AbstractShape<dimensions>::VectorType _position,
Math::Vector<dimensions, GLfloat> _position,
_transformedPosition;
float _radius, _transformedRadius;
};

Loading…
Cancel
Save