Browse Source

Shapes: added infinite cylinder shape.

Seems like an essential part of the toolbox -- we have Point, LineSegment
and Line and now we have also all "swept sphere" alternatives to them --
Sphere (swept "along" the point), Capsule (swept along line segment) and
Cylinder (swept along infinite line).

"Implemented" collision detection with point and sphere, which is just
simplified version of collision detection implemented for Capsule (i.e.
no need to check for cap cases).
pull/23/head
Vladimír Vondruš 13 years ago
parent
commit
d8a5695a28
  1. 1
      doc/shapes.dox
  2. 2
      src/Shapes/CMakeLists.txt
  3. 2
      src/Shapes/Capsule.h
  4. 1
      src/Shapes/Composition.h
  5. 58
      src/Shapes/Cylinder.cpp
  6. 117
      src/Shapes/Cylinder.h
  7. 7
      src/Shapes/Implementation/CollisionDispatch.cpp
  8. 4
      src/Shapes/Shapes.h
  9. 1
      src/Shapes/Test/CMakeLists.txt
  10. 85
      src/Shapes/Test/CylinderTest.cpp
  11. 2
      src/Shapes/shapeImplementation.cpp
  12. 25
      src/Shapes/shapeImplementation.h

1
doc/shapes.dox

@ -51,6 +51,7 @@ line and point. Collision of two lines can be detected only in 2D.
@subsection shapes-3D Three-dimensional shapes
- @ref Shapes::Sphere "Shapes::Sphere*D" -- @copybrief Shapes::Sphere
- @ref Shapes::Cylinder "Shapes::Cylinder*D" -- @copybrief Shapes::Cylinder
- @ref Shapes::Capsule "Shapes::Capsule*D" -- @copybrief Shapes::Capsule
- @ref Shapes::AxisAlignedBox "Shapes::AxisAlignedBox*D" -- @copybrief Shapes::AxisAlignedBox
- @ref Shapes::Box "Shapes::Box*D" -- @copybrief Shapes::Box

2
src/Shapes/CMakeLists.txt

@ -27,6 +27,7 @@ set(MagnumShapes_SRCS
AxisAlignedBox.cpp
Box.cpp
Capsule.cpp
Cylinder.cpp
Composition.cpp
Line.cpp
Plane.cpp
@ -44,6 +45,7 @@ set(MagnumShapes_HEADERS
AxisAlignedBox.h
Box.h
Capsule.h
Cylinder.h
Composition.h
Line.h
LineSegment.h

2
src/Shapes/Capsule.h

@ -40,7 +40,7 @@ namespace Magnum { namespace Shapes {
Unlike other elements the capsule expects uniform scaling. See @ref shapes for
brief introduction.
@see Capsule2D, Capsule3D
@see Capsule2D, Capsule3D, Cylinder
@todo Store the radius as squared value to avoid sqrt/pow? Will complicate
collision detection with sphere.
*/

1
src/Shapes/Composition.h

@ -79,6 +79,7 @@ template<UnsignedInt dimensions> class MAGNUM_SHAPES_EXPORT Composition {
Line, /**< Line */
LineSegment, /**< @ref LineSegment "Line segment" */
Sphere, /**< Sphere */
Cylinder, /**< @ref Cylinder */
Capsule, /**< Capsule */
AxisAlignedBox, /**< @ref AxisAlignedBox "Axis aligned box" */
Box, /**< Box */

58
src/Shapes/Cylinder.cpp

@ -0,0 +1,58 @@
/*
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 "Cylinder.h"
#include "Math/Functions.h"
#include "Math/Matrix3.h"
#include "Math/Matrix4.h"
#include "Math/Geometry/Distance.h"
#include "Magnum.h"
#include "Shapes/Point.h"
#include "Shapes/Sphere.h"
using namespace Magnum::Math::Geometry;
namespace Magnum { namespace Shapes {
template<UnsignedInt dimensions> Cylinder<dimensions> Cylinder<dimensions>::transformed(const typename DimensionTraits<dimensions, Float>::MatrixType& matrix) const {
return Cylinder<dimensions>(matrix.transformPoint(_a), matrix.transformPoint(_b), matrix.uniformScaling()*_radius);
}
template<UnsignedInt dimensions> bool Cylinder<dimensions>::operator%(const Point<dimensions>& other) const {
return Distance::linePointSquared(_a, _b, other.position()) <
Math::pow<2>(_radius);
}
template<UnsignedInt dimensions> bool Cylinder<dimensions>::operator%(const Sphere<dimensions>& other) const {
return Distance::linePointSquared(_a, _b, other.position()) <
Math::pow<2>(_radius+other.radius());
}
#ifndef DOXYGEN_GENERATING_OUTPUT
template class MAGNUM_SHAPES_EXPORT Cylinder<2>;
template class MAGNUM_SHAPES_EXPORT Cylinder<3>;
#endif
}}

117
src/Shapes/Cylinder.h

@ -0,0 +1,117 @@
#ifndef Magnum_Shapes_Cylinder_h
#define Magnum_Shapes_Cylinder_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.
*/
/** @file
* @brief Class Magnum::Shapes::Cylinder, typedef Magnum::Shapes::Cylinder2D, Magnum::Shapes::Cylinder3D
*/
#include "Math/Vector3.h"
#include "DimensionTraits.h"
#include "Shapes/Shapes.h"
#include "Shapes/magnumShapesVisibility.h"
namespace Magnum { namespace Shapes {
/**
@brief Infinite cylinder defined by line and radius
Unlike other elements the cylinder expects uniform scaling. See @ref shapes for
brief introduction.
@see @ref Cylinder2D, @ref Cylinder3D, @ref Capsule
@todo Store the radius as squared value to avoid sqrt/pow? Will complicate
collision detection with sphere.
*/
template<UnsignedInt dimensions> class MAGNUM_SHAPES_EXPORT Cylinder {
public:
enum: UnsignedInt {
Dimensions = dimensions /**< Dimension count */
};
/**
* @brief Constructor
*
* Creates zero-sized cylinder at origin.
*/
constexpr /*implicit*/ Cylinder(): _radius(0.0f) {}
/** @brief Constructor */
constexpr /*implicit*/ Cylinder(const typename DimensionTraits<dimensions, Float>::VectorType& a, const typename DimensionTraits<dimensions, Float>::VectorType& b, Float radius): _a(a), _b(b), _radius(radius) {}
/** @brief Transformed shape */
Cylinder<dimensions> transformed(const typename DimensionTraits<dimensions, Float>::MatrixType& matrix) const;
/** @brief First point */
constexpr typename DimensionTraits<dimensions, Float>::VectorType a() const {
return _a;
}
/** @brief Set first point */
void setA(const typename DimensionTraits<dimensions, Float>::VectorType& a) {
_a = a;
}
/** @brief Second point */
constexpr typename DimensionTraits<dimensions, Float>::VectorType b() const {
return _b;
}
/** @brief Set second point */
void setB(const typename DimensionTraits<dimensions, Float>::VectorType& b) {
_b = b;
}
/** @brief Radius */
constexpr Float radius() const { return _radius; }
/** @brief Set radius */
void setRadius(Float radius) { _radius = radius; }
/** @brief Collision with point */
bool operator%(const Point<dimensions>& other) const;
/** @brief Collision with sphere */
bool operator%(const Sphere<dimensions>& other) const;
private:
typename DimensionTraits<dimensions, Float>::VectorType _a, _b;
Float _radius;
};
/** @brief Infinite two-dimensional cylinder */
typedef Cylinder<2> Cylinder2D;
/** @brief Infinite three-dimensional cylinder */
typedef Cylinder<3> Cylinder3D;
/** @collisionoperator{Point,Cylinder} */
template<UnsignedInt dimensions> inline bool operator%(const Point<dimensions>& a, const Cylinder<dimensions>& b) { return b % a; }
/** @collisionoperator{Sphere,Cylinder} */
template<UnsignedInt dimensions> inline bool operator%(const Sphere<dimensions>& a, const Cylinder<dimensions>& b) { return b % a; }
}}
#endif

7
src/Shapes/Implementation/CollisionDispatch.cpp

@ -27,6 +27,7 @@
#include "Shapes/AxisAlignedBox.h"
#include "Shapes/Box.h"
#include "Shapes/Capsule.h"
#include "Shapes/Cylinder.h"
#include "Shapes/LineSegment.h"
#include "Shapes/Plane.h"
#include "Shapes/Point.h"
@ -47,6 +48,9 @@ template<> bool collides(const AbstractShape<2>& a, const AbstractShape<2>& b) {
_c(Sphere, Sphere2D, LineSegment, LineSegment2D)
_c(Sphere, Sphere2D, Sphere, Sphere2D)
_c(Cylinder, Cylinder2D, Point, Point2D)
_c(Cylinder, Cylinder2D, Sphere, Sphere2D)
_c(Capsule, Capsule2D, Point, Point2D)
_c(Capsule, Capsule2D, Sphere, Sphere2D)
@ -69,6 +73,9 @@ template<> bool collides(const AbstractShape<3>& a, const AbstractShape<3>& b) {
_c(Sphere, Sphere3D, LineSegment, LineSegment3D)
_c(Sphere, Sphere3D, Sphere, Sphere3D)
_c(Cylinder, Cylinder3D, Point, Point3D)
_c(Cylinder, Cylinder3D, Sphere, Sphere3D)
_c(Capsule, Capsule3D, Point, Point3D)
_c(Capsule, Capsule3D, Sphere, Sphere3D)

4
src/Shapes/Shapes.h

@ -54,6 +54,10 @@ template<UnsignedInt> class Composition;
typedef Composition<2> Composition2D;
typedef Composition<3> Composition3D;
template<UnsignedInt> class Cylinder;
typedef Cylinder<2> Cylinder2D;
typedef Cylinder<3> Cylinder3D;
template<UnsignedInt> class Line;
typedef Line<2> Line2D;
typedef Line<3> Line3D;

1
src/Shapes/Test/CMakeLists.txt

@ -26,6 +26,7 @@ corrade_add_test(ShapesShapeImplementationTest ShapeImplementationTest.cpp LIBRA
corrade_add_test(ShapesAxisAlignedBoxTest AxisAlignedBoxTest.cpp LIBRARIES MagnumShapes)
corrade_add_test(ShapesBoxTest BoxTest.cpp LIBRARIES MagnumShapes)
corrade_add_test(ShapesCapsuleTest CapsuleTest.cpp LIBRARIES MagnumShapes)
corrade_add_test(ShapesCylinderTest CylinderTest.cpp LIBRARIES MagnumShapes)
corrade_add_test(ShapesLineTest LineTest.cpp LIBRARIES MagnumShapes)
corrade_add_test(ShapesPlaneTest PlaneTest.cpp LIBRARIES MagnumShapes)
corrade_add_test(ShapesPointTest PointTest.cpp LIBRARIES MagnumShapes)

85
src/Shapes/Test/CylinderTest.cpp

@ -0,0 +1,85 @@
/*
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 "Math/Matrix3.h"
#include "Math/Matrix4.h"
#include "Magnum.h"
#include "Shapes/Cylinder.h"
#include "Shapes/Point.h"
#include "Shapes/Sphere.h"
#include "ShapeTestBase.h"
namespace Magnum { namespace Shapes { namespace Test {
class CylinderTest: public TestSuite::Tester {
public:
CylinderTest();
void transformed();
void transformedAverageScaling();
void collisionPoint();
void collisionSphere();
};
CylinderTest::CylinderTest() {
addTests({&CylinderTest::transformed,
&CylinderTest::collisionPoint,
&CylinderTest::collisionSphere});
}
void CylinderTest::transformed() {
const Shapes::Cylinder3D cylinder({1.0f, 2.0f, 3.0f}, {-1.0f, -2.0f, -3.0f}, 7.0f);
const auto transformed = cylinder.transformed(Matrix4::scaling(Vector3(2.0f))*Matrix4::rotation(Deg(90.0f), Vector3::zAxis()));
CORRADE_COMPARE(transformed.a(), Vector3(-4.0f, 2.0f, 6.0f));
CORRADE_COMPARE(transformed.b(), Vector3(4.0f, -2.0f, -6.0f));
CORRADE_COMPARE(transformed.radius(), 14.0f);
}
void CylinderTest::collisionPoint() {
Shapes::Cylinder3D cylinder({-1.0f, -1.0f, 0.0f}, {1.0f, 1.0f, 0.0f}, 2.0f);
Shapes::Point3D point({2.0f, 0.0f, 0.0f});
Shapes::Point3D point1({1.0f, 3.1f, 0.0f});
Shapes::Point3D point2({2.9f, -1.0f, 0.0f});
VERIFY_COLLIDES(cylinder, point);
VERIFY_COLLIDES(cylinder, point1);
VERIFY_NOT_COLLIDES(cylinder, point2);
}
void CylinderTest::collisionSphere() {
Shapes::Cylinder3D cylinder({-1.0f, -1.0f, 0.0f}, {1.0f, 1.0f, 0.0f}, 2.0f);
Shapes::Sphere3D sphere({3.0f, 0.0f, 0.0f}, 0.9f);
Shapes::Sphere3D sphere1({1.0f, 4.1f, 0.0f}, 1.0f);
Shapes::Sphere3D sphere2({3.5f, -1.0f, 0.0f}, 0.6f);
VERIFY_COLLIDES(cylinder, sphere);
VERIFY_COLLIDES(cylinder, sphere1);
VERIFY_NOT_COLLIDES(cylinder, sphere2);
}
}}}
CORRADE_TEST_MAIN(Magnum::Shapes::Test::CylinderTest)

2
src/Shapes/shapeImplementation.cpp

@ -36,6 +36,7 @@ Debug operator<<(Debug debug, ShapeDimensionTraits<2>::Type value) {
_val(LineSegment)
_val(Sphere)
_val(Capsule)
_val(Cylinder)
_val(AxisAlignedBox)
_val(Box)
_val(Composition)
@ -53,6 +54,7 @@ Debug operator<<(Debug debug, ShapeDimensionTraits<3>::Type value) {
_val(LineSegment)
_val(Sphere)
_val(Capsule)
_val(Cylinder)
_val(AxisAlignedBox)
_val(Box)
_val(Plane)

25
src/Shapes/shapeImplementation.h

@ -44,10 +44,11 @@ template<> struct ShapeDimensionTraits<2> {
Line = 2,
LineSegment = 3,
Sphere = 5,
Capsule = 7,
AxisAlignedBox = 11,
Box = 13,
Composition = 17
Cylinder = 7,
Capsule = 11,
AxisAlignedBox = 13,
Box = 17,
Composition = 19
};
};
@ -57,11 +58,12 @@ template<> struct ShapeDimensionTraits<3> {
Line = 2,
LineSegment = 3,
Sphere = 5,
Capsule = 7,
AxisAlignedBox = 11,
Box = 13,
Plane = 17,
Composition = 19
Cylinder = 7,
Capsule = 11,
AxisAlignedBox = 13,
Box = 17,
Plane = 19,
Composition = 23
};
};
@ -92,6 +94,11 @@ template<UnsignedInt dimensions> struct TypeOf<Shapes::Sphere<dimensions>> {
return ShapeDimensionTraits<dimensions>::Type::Sphere;
}
};
template<UnsignedInt dimensions> struct TypeOf<Shapes::Cylinder<dimensions>> {
constexpr static typename ShapeDimensionTraits<dimensions>::Type type() {
return ShapeDimensionTraits<dimensions>::Type::Cylinder;
}
};
template<UnsignedInt dimensions> struct TypeOf<Shapes::Capsule<dimensions>> {
constexpr static typename ShapeDimensionTraits<dimensions>::Type type() {
return ShapeDimensionTraits<dimensions>::Type::Capsule;

Loading…
Cancel
Save