From f7d002d06d4c3ba5b22b28689f07512bbbb3dec2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Vladim=C3=ADr=20Vondru=C5=A1?= Date: Sun, 11 Nov 2012 16:33:46 +0100 Subject: [PATCH] Debug operator for shape type. --- src/Physics/AbstractShape.cpp | 39 ++++++++++++++++++++++ src/Physics/AbstractShape.h | 8 +++++ src/Physics/Test/AbstractShapeTest.cpp | 41 +++++++++++++++++++++++ src/Physics/Test/AbstractShapeTest.h | 25 +++----------- src/Physics/Test/CMakeLists.txt | 1 + src/Physics/Test/CapsuleTest.h | 4 +-- src/Physics/Test/PlaneTest.h | 4 +-- src/Physics/Test/ShapeTestBase.h | 46 ++++++++++++++++++++++++++ src/Physics/Test/SphereTest.h | 4 +-- 9 files changed, 146 insertions(+), 26 deletions(-) create mode 100644 src/Physics/Test/AbstractShapeTest.cpp create mode 100644 src/Physics/Test/ShapeTestBase.h diff --git a/src/Physics/AbstractShape.cpp b/src/Physics/AbstractShape.cpp index 23a15ec0d..5ce4474bd 100644 --- a/src/Physics/AbstractShape.cpp +++ b/src/Physics/AbstractShape.cpp @@ -15,6 +15,8 @@ #include "AbstractShape.h" +#include + namespace Magnum { namespace Physics { template bool AbstractShape::collides(const AbstractShape* other) const { @@ -28,4 +30,41 @@ template bool AbstractShape::collides(const template class AbstractShape<2>; template class AbstractShape<3>; +#ifndef DOXYGEN_GENERATING_OUTPUT +Debug operator<<(Debug debug, AbstractShape2D::Type value) { + switch(value) { + #define _val(value) case AbstractShape2D::Type::value: return debug << "AbstractShape2D::Type::" #value; + _val(Point) + _val(Line) + _val(LineSegment) + _val(Sphere) + _val(Capsule) + _val(AxisAlignedBox) + _val(Box) + _val(ShapeGroup) + #undef _val + } + + return debug << "AbstractShape2D::Type::(unknown)"; +} + +Debug operator<<(Debug debug, AbstractShape3D::Type value) { + switch(value) { + #define _val(value) case AbstractShape3D::Type::value: return debug << "AbstractShape3D::Type::" #value; + _val(Point) + _val(Line) + _val(LineSegment) + _val(Sphere) + _val(Capsule) + _val(AxisAlignedBox) + _val(Box) + _val(ShapeGroup) + _val(Plane) + #undef _val + } + + return debug << "AbstractShape2D::Type::(unknown)"; +} +#endif + }} diff --git a/src/Physics/AbstractShape.h b/src/Physics/AbstractShape.h index 6ac3bdf25..a8320aa0d 100644 --- a/src/Physics/AbstractShape.h +++ b/src/Physics/AbstractShape.h @@ -124,6 +124,14 @@ typedef AbstractShape<2> AbstractShape2D; /** @brief Abstract three-dimensional shape */ typedef AbstractShape<3> AbstractShape3D; +/** @debugoperator{Magnum::Physics::AbstractShape} */ +#ifndef DOXYGEN_GENERATING_OUTPUT +Debug PHYSICS_EXPORT operator<<(Debug debug, AbstractShape2D::Type value); +Debug PHYSICS_EXPORT operator<<(Debug debug, AbstractShape3D::Type value); +#else +template Debug operator<<(Debug debug, typename AbstractShape::Type value); +#endif + }} #endif diff --git a/src/Physics/Test/AbstractShapeTest.cpp b/src/Physics/Test/AbstractShapeTest.cpp new file mode 100644 index 000000000..a7979c8ae --- /dev/null +++ b/src/Physics/Test/AbstractShapeTest.cpp @@ -0,0 +1,41 @@ +/* + Copyright © 2010, 2011, 2012 Vladimír Vondruš + + This file is part of Magnum. + + Magnum is free software: you can redistribute it and/or modify + it under the terms of the GNU Lesser General Public License version 3 + only, as published by the Free Software Foundation. + + Magnum is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU Lesser General Public License version 3 for more details. +*/ + +#include "AbstractShapeTest.h" + +#include +#include + +#include "Physics/AbstractShape.h" + +CORRADE_TEST_MAIN(Magnum::Physics::Test::AbstractShapeTest) + +namespace Magnum { namespace Physics { namespace Test { + +AbstractShapeTest::AbstractShapeTest() { + addTests(&AbstractShapeTest::debug); +} + +void AbstractShapeTest::debug() { + std::ostringstream o; + Debug(&o) << AbstractShape2D::Type::ShapeGroup; + CORRADE_COMPARE(o.str(), "AbstractShape2D::Type::ShapeGroup\n"); + + o.str(""); + Debug(&o) << AbstractShape3D::Type::Plane; + CORRADE_COMPARE(o.str(), "AbstractShape3D::Type::Plane\n"); +} + +}}} diff --git a/src/Physics/Test/AbstractShapeTest.h b/src/Physics/Test/AbstractShapeTest.h index ea8d1ff06..1fa4c4531 100644 --- a/src/Physics/Test/AbstractShapeTest.h +++ b/src/Physics/Test/AbstractShapeTest.h @@ -17,29 +17,14 @@ #include -#include "Math/Matrix4.h" -#include "Magnum.h" - namespace Magnum { namespace Physics { namespace Test { -class AbstractShapeTest { - protected: - template void randomTransformation(T& shape) { - shape.applyTransformation(Matrix4::translation({7.0f, 8.0f, -9.0f})); - } -}; +class AbstractShapeTest: public Corrade::TestSuite::Tester { + public: + AbstractShapeTest(); -#define VERIFY_COLLIDES(a, b) \ - CORRADE_VERIFY(a % b); \ - CORRADE_VERIFY(b % a); \ - CORRADE_VERIFY(a.collides(&b)); \ - CORRADE_VERIFY(b.collides(&a)); - -#define VERIFY_NOT_COLLIDES(a, b) \ - CORRADE_VERIFY(!(a % b)); \ - CORRADE_VERIFY(!(b % a)); \ - CORRADE_VERIFY(!(a.collides(&b))); \ - CORRADE_VERIFY(!(b.collides(&a))); + void debug(); +}; }}} diff --git a/src/Physics/Test/CMakeLists.txt b/src/Physics/Test/CMakeLists.txt index 4f8130a83..5bb96fb4a 100644 --- a/src/Physics/Test/CMakeLists.txt +++ b/src/Physics/Test/CMakeLists.txt @@ -1,3 +1,4 @@ +corrade_add_test2(PhysicsAbstractShapeTest AbstractShapeTest.cpp LIBRARIES MagnumPhysics) corrade_add_test2(PhysicsAxisAlignedBoxTest AxisAlignedBoxTest.cpp LIBRARIES MagnumPhysics) corrade_add_test2(PhysicsBoxTest BoxTest.cpp LIBRARIES MagnumPhysics) corrade_add_test2(PhysicsCapsuleTest CapsuleTest.cpp LIBRARIES MagnumPhysics) diff --git a/src/Physics/Test/CapsuleTest.h b/src/Physics/Test/CapsuleTest.h index 6cc79cfbb..88dec80df 100644 --- a/src/Physics/Test/CapsuleTest.h +++ b/src/Physics/Test/CapsuleTest.h @@ -15,11 +15,11 @@ GNU Lesser General Public License version 3 for more details. */ -#include "AbstractShapeTest.h" +#include "ShapeTestBase.h" namespace Magnum { namespace Physics { namespace Test { -class CapsuleTest: public Corrade::TestSuite::Tester, AbstractShapeTest { +class CapsuleTest: public Corrade::TestSuite::Tester, ShapeTestBase { public: CapsuleTest(); diff --git a/src/Physics/Test/PlaneTest.h b/src/Physics/Test/PlaneTest.h index 72baabe19..4381e3dc6 100644 --- a/src/Physics/Test/PlaneTest.h +++ b/src/Physics/Test/PlaneTest.h @@ -15,11 +15,11 @@ GNU Lesser General Public License version 3 for more details. */ -#include "AbstractShapeTest.h" +#include "ShapeTestBase.h" namespace Magnum { namespace Physics { namespace Test { -class PlaneTest: public Corrade::TestSuite::Tester, AbstractShapeTest { +class PlaneTest: public Corrade::TestSuite::Tester, ShapeTestBase { public: PlaneTest(); diff --git a/src/Physics/Test/ShapeTestBase.h b/src/Physics/Test/ShapeTestBase.h new file mode 100644 index 000000000..d5503bc65 --- /dev/null +++ b/src/Physics/Test/ShapeTestBase.h @@ -0,0 +1,46 @@ +#ifndef Magnum_Physics_Test_ShapeTestBase_h +#define Magnum_Physics_Test_ShapeTestBase_h +/* + Copyright © 2010, 2011, 2012 Vladimír Vondruš + + This file is part of Magnum. + + Magnum is free software: you can redistribute it and/or modify + it under the terms of the GNU Lesser General Public License version 3 + only, as published by the Free Software Foundation. + + Magnum is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU Lesser General Public License version 3 for more details. +*/ + +#include + +#include "Math/Matrix4.h" +#include "Magnum.h" + +namespace Magnum { namespace Physics { namespace Test { + +class ShapeTestBase { + protected: + template void randomTransformation(T& shape) { + shape.applyTransformation(Matrix4::translation({7.0f, 8.0f, -9.0f})); + } +}; + +#define VERIFY_COLLIDES(a, b) \ + CORRADE_VERIFY(a % b); \ + CORRADE_VERIFY(b % a); \ + CORRADE_VERIFY(a.collides(&b)); \ + CORRADE_VERIFY(b.collides(&a)); + +#define VERIFY_NOT_COLLIDES(a, b) \ + CORRADE_VERIFY(!(a % b)); \ + CORRADE_VERIFY(!(b % a)); \ + CORRADE_VERIFY(!(a.collides(&b))); \ + CORRADE_VERIFY(!(b.collides(&a))); + +}}} + +#endif diff --git a/src/Physics/Test/SphereTest.h b/src/Physics/Test/SphereTest.h index 6ff9c7bc9..48a2d2d3c 100644 --- a/src/Physics/Test/SphereTest.h +++ b/src/Physics/Test/SphereTest.h @@ -15,11 +15,11 @@ GNU Lesser General Public License version 3 for more details. */ -#include "AbstractShapeTest.h" +#include "ShapeTestBase.h" namespace Magnum { namespace Physics { namespace Test { -class SphereTest: public Corrade::TestSuite::Tester, AbstractShapeTest { +class SphereTest: public Corrade::TestSuite::Tester, ShapeTestBase { public: SphereTest();