#ifndef Magnum_Shapes_shapeImplementation_h #define Magnum_Shapes_shapeImplementation_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. */ #include #include #include "DimensionTraits.h" #include "Magnum.h" #include "Shapes/Shapes.h" #include "Shapes/magnumShapesVisibility.h" namespace Magnum { namespace Shapes { namespace Implementation { /* Shape type for given dimension count */ template struct ShapeDimensionTraits; template<> struct ShapeDimensionTraits<2> { enum class Type: UnsignedByte { Point = 1, Line = 2, LineSegment = 3, Sphere = 5, Capsule = 7, AxisAlignedBox = 11, Box = 13, Composition = 17 }; }; template<> struct ShapeDimensionTraits<3> { enum class Type: UnsignedByte { Point = 1, Line = 2, LineSegment = 3, Sphere = 5, Capsule = 7, AxisAlignedBox = 11, Box = 13, Plane = 17, Composition = 19 }; }; Debug MAGNUM_SHAPES_EXPORT operator<<(Debug debug, ShapeDimensionTraits<2>::Type value); Debug MAGNUM_SHAPES_EXPORT operator<<(Debug debug, ShapeDimensionTraits<3>::Type value); /* Enum value corresponding to given type */ template struct TypeOf; template struct TypeOf> { constexpr static typename ShapeDimensionTraits::Type type() { return ShapeDimensionTraits::Type::Point; } }; template struct TypeOf> { constexpr static typename ShapeDimensionTraits::Type type() { return ShapeDimensionTraits::Type::Line; } }; template struct TypeOf> { constexpr static typename ShapeDimensionTraits::Type type() { return ShapeDimensionTraits::Type::LineSegment; } }; template struct TypeOf> { constexpr static typename ShapeDimensionTraits::Type type() { return ShapeDimensionTraits::Type::Sphere; } }; template struct TypeOf> { constexpr static typename ShapeDimensionTraits::Type type() { return ShapeDimensionTraits::Type::Capsule; } }; template struct TypeOf> { constexpr static typename ShapeDimensionTraits::Type type() { return ShapeDimensionTraits::Type::AxisAlignedBox; } }; template struct TypeOf> { constexpr static typename ShapeDimensionTraits::Type type() { return ShapeDimensionTraits::Type::Box; } }; template<> struct TypeOf { constexpr static ShapeDimensionTraits<3>::Type type() { return ShapeDimensionTraits<3>::Type::Plane; } }; template struct TypeOf> { constexpr static typename ShapeDimensionTraits::Type type() { return ShapeDimensionTraits::Type::Composition; } }; /* Polymorphic shape wrappers */ template struct MAGNUM_SHAPES_EXPORT AbstractShape { explicit AbstractShape(); virtual ~AbstractShape(); virtual typename ShapeDimensionTraits::Type MAGNUM_SHAPES_LOCAL type() const = 0; virtual AbstractShape MAGNUM_SHAPES_LOCAL * clone() const = 0; virtual void MAGNUM_SHAPES_LOCAL transform(const typename DimensionTraits::MatrixType& matrix, AbstractShape* result) const = 0; }; template struct Shape: AbstractShape { T shape; explicit Shape() = default; explicit Shape(const T& shape): shape(shape) {} explicit Shape(T&& shape): shape(std::move(shape)) {} typename ShapeDimensionTraits::Type type() const override { return TypeOf::type(); } AbstractShape* clone() const override { return new Shape(shape); } void transform(const typename DimensionTraits::MatrixType& matrix, AbstractShape* result) const override { CORRADE_INTERNAL_ASSERT(result->type() == type()); static_cast*>(result)->shape = shape.transformed(matrix); } }; }}} #endif