mirror of https://github.com/mosra/magnum.git
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
188 lines
6.7 KiB
188 lines
6.7 KiB
|
13 years ago
|
#ifndef Magnum_Shapes_shapeImplementation_h
|
||
|
|
#define Magnum_Shapes_shapeImplementation_h
|
||
|
13 years ago
|
/*
|
||
|
|
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 <utility>
|
||
|
|
#include <Utility/Assert.h>
|
||
|
13 years ago
|
#include <corradeCompatibility.h>
|
||
|
13 years ago
|
|
||
|
|
#include "DimensionTraits.h"
|
||
|
|
#include "Magnum.h"
|
||
|
13 years ago
|
#include "Shapes/Shapes.h"
|
||
|
|
#include "Shapes/magnumShapesVisibility.h"
|
||
|
13 years ago
|
|
||
|
13 years ago
|
namespace Magnum { namespace Shapes { namespace Implementation {
|
||
|
13 years ago
|
|
||
|
13 years ago
|
/*
|
||
|
|
Adding new collision type:
|
||
|
|
|
||
|
|
1. Add the type into the 2D/3D enums below, pick new prime number and
|
||
|
|
preserve complexity ordering
|
||
|
|
2. Update debug output operators for changed enums
|
||
|
|
3. Add TypeOf struct specialization (either for both 2D/3D or for only one
|
||
|
|
of them)
|
||
|
|
4. Add the enum value to (documentation-only) enum in Composition
|
||
|
|
5. Update doc/shapes.dox with new type
|
||
|
|
|
||
|
|
Adding new collision detection implementation:
|
||
|
|
|
||
|
|
1. Update Implementation/CollisionDispatch.cpp with newly implemented
|
||
|
|
2D/3D pair
|
||
|
|
*/
|
||
|
|
|
||
|
13 years ago
|
/* Shape type for given dimension count */
|
||
|
|
|
||
|
|
template<UnsignedInt> struct ShapeDimensionTraits;
|
||
|
|
|
||
|
|
template<> struct ShapeDimensionTraits<2> {
|
||
|
|
enum class Type: UnsignedByte {
|
||
|
|
Point = 1,
|
||
|
|
Line = 2,
|
||
|
|
LineSegment = 3,
|
||
|
|
Sphere = 5,
|
||
|
13 years ago
|
InvertedSphere = 7,
|
||
|
|
Cylinder = 11,
|
||
|
|
Capsule = 13,
|
||
|
|
AxisAlignedBox = 17,
|
||
|
|
Box = 19,
|
||
|
|
Composition = 23
|
||
|
13 years ago
|
};
|
||
|
|
};
|
||
|
|
|
||
|
|
template<> struct ShapeDimensionTraits<3> {
|
||
|
|
enum class Type: UnsignedByte {
|
||
|
|
Point = 1,
|
||
|
|
Line = 2,
|
||
|
|
LineSegment = 3,
|
||
|
|
Sphere = 5,
|
||
|
13 years ago
|
InvertedSphere = 7,
|
||
|
|
Cylinder = 11,
|
||
|
|
Capsule = 13,
|
||
|
|
AxisAlignedBox = 17,
|
||
|
|
Box = 19,
|
||
|
|
Plane = 23,
|
||
|
|
Composition = 29
|
||
|
13 years ago
|
};
|
||
|
|
};
|
||
|
|
|
||
|
13 years ago
|
Debug MAGNUM_SHAPES_EXPORT operator<<(Debug debug, ShapeDimensionTraits<2>::Type value);
|
||
|
|
Debug MAGNUM_SHAPES_EXPORT operator<<(Debug debug, ShapeDimensionTraits<3>::Type value);
|
||
|
13 years ago
|
|
||
|
|
/* Enum value corresponding to given type */
|
||
|
|
|
||
|
|
template<class> struct TypeOf;
|
||
|
|
|
||
|
13 years ago
|
template<UnsignedInt dimensions> struct TypeOf<Shapes::Point<dimensions>> {
|
||
|
13 years ago
|
constexpr static typename ShapeDimensionTraits<dimensions>::Type type() {
|
||
|
13 years ago
|
return ShapeDimensionTraits<dimensions>::Type::Point;
|
||
|
|
}
|
||
|
|
};
|
||
|
13 years ago
|
template<UnsignedInt dimensions> struct TypeOf<Shapes::Line<dimensions>> {
|
||
|
13 years ago
|
constexpr static typename ShapeDimensionTraits<dimensions>::Type type() {
|
||
|
13 years ago
|
return ShapeDimensionTraits<dimensions>::Type::Line;
|
||
|
|
}
|
||
|
|
};
|
||
|
13 years ago
|
template<UnsignedInt dimensions> struct TypeOf<Shapes::LineSegment<dimensions>> {
|
||
|
13 years ago
|
constexpr static typename ShapeDimensionTraits<dimensions>::Type type() {
|
||
|
13 years ago
|
return ShapeDimensionTraits<dimensions>::Type::LineSegment;
|
||
|
|
}
|
||
|
|
};
|
||
|
13 years ago
|
template<UnsignedInt dimensions> struct TypeOf<Shapes::Sphere<dimensions>> {
|
||
|
13 years ago
|
constexpr static typename ShapeDimensionTraits<dimensions>::Type type() {
|
||
|
13 years ago
|
return ShapeDimensionTraits<dimensions>::Type::Sphere;
|
||
|
|
}
|
||
|
|
};
|
||
|
13 years ago
|
template<UnsignedInt dimensions> struct TypeOf<Shapes::InvertedSphere<dimensions>> {
|
||
|
|
constexpr static typename ShapeDimensionTraits<dimensions>::Type type() {
|
||
|
|
return ShapeDimensionTraits<dimensions>::Type::InvertedSphere;
|
||
|
|
}
|
||
|
|
};
|
||
|
13 years ago
|
template<UnsignedInt dimensions> struct TypeOf<Shapes::Cylinder<dimensions>> {
|
||
|
|
constexpr static typename ShapeDimensionTraits<dimensions>::Type type() {
|
||
|
|
return ShapeDimensionTraits<dimensions>::Type::Cylinder;
|
||
|
|
}
|
||
|
|
};
|
||
|
13 years ago
|
template<UnsignedInt dimensions> struct TypeOf<Shapes::Capsule<dimensions>> {
|
||
|
13 years ago
|
constexpr static typename ShapeDimensionTraits<dimensions>::Type type() {
|
||
|
13 years ago
|
return ShapeDimensionTraits<dimensions>::Type::Capsule;
|
||
|
|
}
|
||
|
|
};
|
||
|
13 years ago
|
template<UnsignedInt dimensions> struct TypeOf<Shapes::AxisAlignedBox<dimensions>> {
|
||
|
13 years ago
|
constexpr static typename ShapeDimensionTraits<dimensions>::Type type() {
|
||
|
13 years ago
|
return ShapeDimensionTraits<dimensions>::Type::AxisAlignedBox;
|
||
|
|
}
|
||
|
|
};
|
||
|
13 years ago
|
template<UnsignedInt dimensions> struct TypeOf<Shapes::Box<dimensions>> {
|
||
|
13 years ago
|
constexpr static typename ShapeDimensionTraits<dimensions>::Type type() {
|
||
|
13 years ago
|
return ShapeDimensionTraits<dimensions>::Type::Box;
|
||
|
|
}
|
||
|
|
};
|
||
|
13 years ago
|
template<> struct TypeOf<Shapes::Plane> {
|
||
|
13 years ago
|
constexpr static ShapeDimensionTraits<3>::Type type() {
|
||
|
13 years ago
|
return ShapeDimensionTraits<3>::Type::Plane;
|
||
|
|
}
|
||
|
|
};
|
||
|
13 years ago
|
template<UnsignedInt dimensions> struct TypeOf<Shapes::Composition<dimensions>> {
|
||
|
13 years ago
|
constexpr static typename ShapeDimensionTraits<dimensions>::Type type() {
|
||
|
13 years ago
|
return ShapeDimensionTraits<dimensions>::Type::Composition;
|
||
|
13 years ago
|
}
|
||
|
|
};
|
||
|
|
|
||
|
|
/* Polymorphic shape wrappers */
|
||
|
|
|
||
|
13 years ago
|
template<UnsignedInt dimensions> struct MAGNUM_SHAPES_EXPORT AbstractShape {
|
||
|
13 years ago
|
explicit AbstractShape();
|
||
|
|
virtual ~AbstractShape();
|
||
|
|
|
||
|
13 years ago
|
virtual typename ShapeDimensionTraits<dimensions>::Type MAGNUM_SHAPES_LOCAL type() const = 0;
|
||
|
|
virtual AbstractShape<dimensions> MAGNUM_SHAPES_LOCAL * clone() const = 0;
|
||
|
13 years ago
|
virtual void MAGNUM_SHAPES_LOCAL transform(const typename DimensionTraits<dimensions, Float>::MatrixType& matrix, AbstractShape<dimensions>* result) const = 0;
|
||
|
13 years ago
|
};
|
||
|
|
|
||
|
|
template<class T> struct Shape: AbstractShape<T::Dimensions> {
|
||
|
|
T shape;
|
||
|
|
|
||
|
|
explicit Shape() = default;
|
||
|
|
explicit Shape(const T& shape): shape(shape) {}
|
||
|
|
explicit Shape(T&& shape): shape(std::move(shape)) {}
|
||
|
|
|
||
|
|
typename ShapeDimensionTraits<T::Dimensions>::Type type() const override {
|
||
|
|
return TypeOf<T>::type();
|
||
|
|
}
|
||
|
|
|
||
|
|
AbstractShape<T::Dimensions>* clone() const override {
|
||
|
|
return new Shape<T>(shape);
|
||
|
|
}
|
||
|
|
|
||
|
13 years ago
|
void transform(const typename DimensionTraits<T::Dimensions, Float>::MatrixType& matrix, AbstractShape<T::Dimensions>* result) const override {
|
||
|
13 years ago
|
CORRADE_INTERNAL_ASSERT(result->type() == type());
|
||
|
|
static_cast<Shape<T>*>(result)->shape = shape.transformed(matrix);
|
||
|
|
}
|
||
|
|
};
|
||
|
|
|
||
|
|
}}}
|
||
|
|
|
||
|
|
#endif
|