#ifndef Magnum_Shapes_Shape_h #define Magnum_Shapes_Shape_h /* This file is part of Magnum. Copyright © 2010, 2011, 2012, 2013 Vladimír Vondruš 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::Shape */ #include "Shapes/AbstractShape.h" #include "Shapes/Shapes.h" #include "magnumShapesVisibility.h" namespace Magnum { namespace Shapes { namespace Implementation { template struct ShapeHelper; } /** @brief Object shape Adds shape for collision detection to object. Each %Shape is part of some ShapeGroup, which essentially maintains a set of objects which can collide with each other. @section Shape-usage Usage Add the feature to the object and some shape group (you can also use ShapeGroup::add() and ShapeGroup::remove() later) and configure the shape. @code Shapes::ShapeGroup3D shapes; Object3D* object; auto shape = new Shapes::Shape(object, {{}, 0.75f}, &shapes); @endcode @see @ref scenegraph, ShapeGroup2D, ShapeGroup3D, DebugTools::ShapeRenderer */ template class MAGNUM_SHAPES_EXPORT Shape: public AbstractShape { friend struct Implementation::ShapeHelper; public: /** * @brief Constructor * @param object Object holding this feature * @param shape Shape * @param group Group this shape belongs to */ template explicit Shape(SceneGraph::AbstractObject* object, const T& shape, ShapeGroup* group = nullptr): AbstractShape(object, group) { Implementation::ShapeHelper::set(*this, shape); } /** @overload */ template explicit Shape(SceneGraph::AbstractObject* object, T&& shape, ShapeGroup* group = nullptr): AbstractShape(object, group) { Implementation::ShapeHelper::set(*this, std::move(shape)); } /** @overload */ template explicit Shape(SceneGraph::AbstractObject* object, ShapeGroup* group = nullptr): AbstractShape(object, group) {} /** @brief Shape */ inline const T& shape() const { return _shape.shape; } /** * @brief Set shape * @return Pointer to self (for method chaining) * * Marks the feature as dirty. */ Shape* setShape(const T& shape); /** * @brief Transformed shape * * Cleans the feature before returning the shape. */ const T& transformedShape(); protected: /** Applies transformation to associated shape. */ void clean(const typename DimensionTraits::MatrixType& absoluteTransformationMatrix) override; private: const Implementation::AbstractShape* abstractTransformedShape() const override { return &_transformedShape; } Implementation::Shape _shape, _transformedShape; }; template inline Shape* Shape::setShape(const T& shape) { Implementation::ShapeHelper::set(*this, shape); this->object()->setDirty(); return this; } template inline const T& Shape::transformedShape() { this->object()->setClean(); return _transformedShape.shape; } template void Shape::clean(const typename DimensionTraits::MatrixType& absoluteTransformationMatrix) { Implementation::ShapeHelper::transform(*this, absoluteTransformationMatrix); } namespace Implementation { template struct ShapeHelper { inline static void set(Shapes::Shape& shape, const T& s) { shape._shape.shape = s; } inline static void transform(Shapes::Shape& shape, const typename DimensionTraits::MatrixType& absoluteTransformationMatrix) { shape._transformedShape.shape = shape._shape.shape.transformed(absoluteTransformationMatrix); } }; template struct MAGNUM_SHAPES_EXPORT ShapeHelper> { static void set(Shapes::Shape>& shape, const Composition& composition); static void set(Shapes::Shape>& shape, Composition&& composition); static void transform(Shapes::Shape>& shape, const typename DimensionTraits::MatrixType& absoluteTransformationMatrix); }; } }} #endif