#ifndef Magnum_Shapes_Composition_h #define Magnum_Shapes_Composition_h /* This file is part of Magnum. Copyright © 2010, 2011, 2012, 2013, 2014, 2015 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 @ref Magnum::Shapes::Composition, typedef @ref Magnum::Shapes::Composition2D, @ref Magnum::Shapes::Composition3D, enum @ref Magnum::Shapes::CompositionOperation */ #include #include #include #include #include "Magnum/DimensionTraits.h" #include "Magnum/Shapes/Shapes.h" #include "Magnum/Shapes/shapeImplementation.h" #include "Magnum/Shapes/visibility.h" namespace Magnum { namespace Shapes { namespace Implementation { template struct ShapeHelper; template inline AbstractShape& getAbstractShape(Composition& group, std::size_t i) { return *group._shapes[i]; } template inline const AbstractShape& getAbstractShape(const Composition& group, std::size_t i) { return *group._shapes[i]; } } /** @brief Shape operation */ enum class CompositionOperation: UnsignedByte { Not, /**< Boolean NOT */ And, /**< Boolean AND */ Or /**< Boolean OR */ }; /** @brief Composition of shapes Result of logical operations on shapes. See @ref shapes for brief introduction. */ template class MAGNUM_SHAPES_EXPORT Composition { friend Implementation::AbstractShape& Implementation::getAbstractShape<>(Composition&, std::size_t); friend const Implementation::AbstractShape& Implementation::getAbstractShape<>(const Composition&, std::size_t); friend Implementation::ShapeHelper>; public: enum: UnsignedInt { Dimensions = dimensions /**< Dimension count */ }; /** @brief Shape type */ #ifdef DOXYGEN_GENERATING_OUTPUT enum class Type { Point, /**< Point */ Line, /**< Line */ LineSegment, /**< @ref LineSegment "Line segment" */ Sphere, /**< Sphere */ InvertedSphere, /**< @ref InvertedSphere "Inverted sphere" */ Cylinder, /**< @ref Cylinder */ Capsule, /**< Capsule */ AxisAlignedBox, /**< @ref AxisAlignedBox "Axis aligned box" */ Box, /**< Box */ Plane /**< Plane (3D only) */ }; #else typedef typename Implementation::ShapeDimensionTraits::Type Type; #endif /** * @brief Default constructor * * Creates empty composition. */ explicit Composition() {} /** * @brief Unary operation constructor * @param operation Unary operation * @param a Operand */ template explicit Composition(CompositionOperation operation, T&& a); /** * @brief Binary operation constructor * @param operation Binary operation * @param a Left operand * @param b Right operand */ template explicit Composition(CompositionOperation operation, T&& a, U&& b); /** @brief Copy constructor */ Composition(const Composition& other); /** @brief Move constructor */ Composition(Composition&& other); ~Composition(); /** @brief Assigment operator */ Composition& operator=(const Composition& other); /** @brief Move assignment operator */ Composition& operator=(Composition&& other); /** @brief Transformed shape */ Composition transformed(const MatrixTypeFor& matrix) const; /** @brief Count of shapes in the hierarchy */ std::size_t size() const { return _shapes.size(); } /** @brief Type of shape at given position */ Type type(std::size_t i) const { return _shapes[i]->type(); } /** @brief Shape at given position */ template const T& get(std::size_t i) const; /** @brief Collision with another shape */ #ifdef DOXYGEN_GENERATING_OUTPUT template bool operator%(const T& other) const { #else template auto operator%(const T& other) const -> typename std::enable_if::type()), typename Implementation::ShapeDimensionTraits::Type>::value, bool>::type { #endif return collides(Implementation::Shape(other)); } private: struct Node { std::size_t rightNode, rightShape; CompositionOperation operation; }; bool collides(const Implementation::AbstractShape& a) const { return collides(a, 0, 0, _shapes.size()); } bool collides(const Implementation::AbstractShape& a, std::size_t node, std::size_t shapeBegin, std::size_t shapeEnd) const; template constexpr static std::size_t shapeCount(const T&) { return 1; } constexpr static std::size_t shapeCount(const Composition& hierarchy) { return hierarchy._shapes.size(); } template constexpr static std::size_t nodeCount(const T&) { return 0; } constexpr static std::size_t nodeCount(const Composition& hierarchy) { return hierarchy._nodes.size(); } template void copyShapes(std::size_t offset, const T& shape) { _shapes[offset] = new Implementation::Shape(shape); } void copyShapes(std::size_t offset, Composition&& other); void copyShapes(std::size_t offset, const Composition& other); template void copyNodes(std::size_t, const T&) {} void copyNodes(std::size_t offset, const Composition& other); Containers::Array*> _shapes; Containers::Array _nodes; }; /** @brief Two-dimensional shape composition */ typedef Composition<2> Composition2D; /** @brief Three-dimensional shape composition */ typedef Composition<3> Composition3D; #ifdef DOXYGEN_GENERATING_OUTPUT /** @debugoperatorclassenum{Magnum::Shapes::Composition,Magnum::Shapes::Composition::Type} */ template Debug& operator<<(Debug& debug, typename Composition::Type value); #endif /** @relates Composition @brief Collision occurence of shape with Composition */ #ifdef DOXYGEN_GENERATING_OUTPUT template inline bool operator%(const T& a, const Composition& b) { #else template inline auto operator%(const T& a, const Composition& b) -> typename std::enable_if::type()), typename Implementation::ShapeDimensionTraits::Type>::value, bool>::type { #endif return b % a; } #ifdef DOXYGEN_GENERATING_OUTPUT /** @relates Composition @brief Logical NOT of shape */ template inline Composition operator!(T a); /** @relates Composition @brief Logical AND of two shapes [Short-circuit evaluation](http://en.wikipedia.org/wiki/Short-circuit_evaluation) is used here, so this operation can be used for providing simplified shape version, because collision with @p b is computed only if @p a collides. See @ref shapes-simplification for an example. */ template inline Composition operator&&(T a, T b); /** @relates Composition @brief Logical OR of two shapes [Short-circuit evaluation](http://en.wikipedia.org/wiki/Short-circuit_evaluation) is used, so if collision with @p a is detected, collision with @p b is not computed. */ template inline Composition operator||(T a, T b); #endif #ifndef DOXYGEN_GENERATING_OUTPUT #define enableIfIsShapeType typename std::enable_if< \ std::is_same::type()), typename Implementation::ShapeDimensionTraits::Type>::value, \ Composition>::type #define enableIfAreShapeType typename std::enable_if< \ std::is_same::type()), typename Implementation::ShapeDimensionTraits::Type>::value && \ std::is_same::type()), typename Implementation::ShapeDimensionTraits::Type>::value, \ Composition>::type template inline auto operator!(T&& a) -> enableIfIsShapeType { return Composition(CompositionOperation::Not, std::forward(a)); } template inline auto operator&&(T&& a, U&& b) -> enableIfAreShapeType { return Composition(CompositionOperation::And, std::forward(a), std::forward(b)); } template inline auto operator||(T&& a, U&& b) -> enableIfAreShapeType { return Composition(CompositionOperation::Or, std::forward(a), std::forward(b)); } #undef enableIfIsShapeType #undef enableIfAreShapeType #endif template template Composition::Composition(CompositionOperation operation, T&& a): _shapes(shapeCount(a)), _nodes(nodeCount(a)+1) { CORRADE_ASSERT(operation == CompositionOperation::Not, "Shapes::Composition::Composition(): unary operation expected", ); _nodes[0].operation = operation; /* 0 = no children, 1 = left child only */ _nodes[0].rightNode = (nodeCount(a) == 0 ? 0 : 1); _nodes[0].rightShape = shapeCount(a); copyNodes(1, a); copyShapes(0, std::forward(a)); } template template Composition::Composition(CompositionOperation operation, T&& a, U&& b): _shapes(shapeCount(a) + shapeCount(b)), _nodes(nodeCount(a) + nodeCount(b) + 1) { CORRADE_ASSERT(operation != CompositionOperation::Not, "Shapes::Composition::Composition(): binary operation expected", ); _nodes[0].operation = operation; /* 0 = no children, 1 = left child only, 2 = right child only, >2 = both */ if(nodeCount(a) == 0 && nodeCount(b) == 0) _nodes[0].rightNode = 0; else if(nodeCount(b) == 0) _nodes[0].rightNode = 1; else _nodes[0].rightNode = nodeCount(a) + 2; _nodes[0].rightShape = shapeCount(a); copyNodes(1, a); copyNodes(nodeCount(a) + 1, b); copyShapes(shapeCount(a), std::forward(b)); copyShapes(0, std::forward(a)); } template template inline const T& Composition::get(std::size_t i) const { CORRADE_ASSERT(_shapes[i]->type() == Implementation::TypeOf::type(), "Shapes::Composition::get(): given shape is not of type" << Implementation::TypeOf::type() << "but" << _shapes[i]->type(), *static_cast(nullptr)); return static_cast*>(_shapes[i])->shape; } }} #endif