#ifndef Magnum_Math_Bezier_h #define Magnum_Math_Bezier_h /* This file is part of Magnum. Copyright © 2010, 2011, 2012, 2013, 2014, 2015, 2016, 2017, 2018, 2019, 2020, 2021, 2022, 2023 Vladimír Vondruš Copyright © 2016 Ashwin Ravichandran 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::Math::Bezier, alias @ref Magnum::Math::QuadraticBezier, @ref Magnum::Math::QuadraticBezier2D, @ref Magnum::Math::QuadraticBezier3D, @ref Magnum::Math::CubicBezier, @ref Magnum::Math::CubicBezier2D, @ref Magnum::Math::CubicBezier3D */ /* std::declval() is said to be in but libstdc++, libc++ and MSVC STL all have it directly in because it just makes sense */ #include #include "Magnum/Math/Vector.h" #ifdef MAGNUM_BUILD_DEPRECATED /* Some APIs returned std::pair before */ #include #endif namespace Magnum { namespace Math { namespace Implementation { template struct BezierConverter; } /** @brief Bézier curve @tparam order Order of Bézier curve @tparam dimensions Dimensions of control points @tparam T Underlying data type Represents a M-order N-dimensional [Bézier Curve](https://en.wikipedia.org/wiki/B%C3%A9zier_curve) segment. Cubic Bézier curves are fully interchangeable with cubic Hermite splines, use @ref fromCubicHermite() and @ref CubicHermite::fromBezier() for the conversion. @see @ref QuadraticBezier, @ref CubicBezier, @ref QuadraticBezier2D, @ref QuadraticBezier3D, @ref CubicBezier2D, @ref CubicBezier3D */ template class Bezier { static_assert(order != 0, "Bezier cannot have zero order"); template friend class Bezier; public: typedef T Type; /**< @brief Underlying data type */ enum: UnsignedInt { Order = order, /**< Order of Bézier curve */ Dimensions = dimensions /**< Dimensions of control points */ }; /** * @brief Create cubic Hermite spline point from adjacent Bézier curve segments * * Given two cubic Hermite spline points defined by points * @f$ \boldsymbol{p}_i @f$, in-tangents @f$ \boldsymbol{m}_i @f$ and * out-tangents @f$ \boldsymbol{n}_i @f$, the corresponding cubic * Bezier curve segment with points @f$ \boldsymbol{c}_0 @f$, * @f$ \boldsymbol{c}_1 @f$, @f$ \boldsymbol{c}_2 @f$ and * @f$ \boldsymbol{c}_3 @f$ is defined as: @f[ * \begin{array}{rcl} * \boldsymbol{c}_0 & = & \boldsymbol{p}_a \\ * \boldsymbol{c}_1 & = & \frac{1}{3} \boldsymbol{n}_a - \boldsymbol{p}_a \\ * \boldsymbol{c}_2 & = & \boldsymbol{p}_b - \frac{1}{3} \boldsymbol{m}_b \\ * \boldsymbol{c}_3 & = & \boldsymbol{p}_b * \end{array} * @f] * * Enabled only on @ref CubicBezier for @ref CubicHermite with vector * underlying types. See @ref CubicHermite::fromBezier() for the * inverse operation. */ template static #ifndef DOXYGEN_GENERATING_OUTPUT typename std::enable_if, VectorType>::value && order == 3, Bezier>::type #else Bezier #endif fromCubicHermite(const CubicHermite& a, const CubicHermite& b) { return {a.point(), a.outTangent()/T(3) - a.point(), b.point() - b.inTangent()/T(3), b.point()}; } /** * @brief Default constructor * * Equivalent to @ref Bezier(ZeroInitT). */ constexpr /*implicit*/ Bezier() noexcept: Bezier{typename Containers::Implementation::GenerateSequence::Type{}, ZeroInit} {} /** * @brief Construct a zero curve * * All control points are zero vectors. */ constexpr explicit Bezier(ZeroInitT) noexcept: Bezier{typename Containers::Implementation::GenerateSequence::Type{}, ZeroInit} {} /** @brief Construct a Bézier without initializing the contents */ explicit Bezier(Magnum::NoInitT) noexcept: Bezier{typename Containers::Implementation::GenerateSequence::Type{}, Magnum::NoInit} {} /** @brief Construct a Bézier curve with given array of control points */ template constexpr /*implicit*/ Bezier(const Vector& first, U... next) noexcept: _data{first, next...} { static_assert(sizeof...(U) + 1 == order + 1, "Wrong number of arguments"); } /** * @brief Construct a Bézier curve from another of different type * * Performs only default casting on the values, no rounding or * anything else. */ template constexpr explicit Bezier(const Bezier& other) noexcept: Bezier{typename Containers::Implementation::GenerateSequence::Type{}, other} {} /** @brief Construct a Bézier curve from external representation */ template::from(std::declval()))> constexpr explicit Bezier(const U& other) noexcept: Bezier{Implementation::BezierConverter::from(other)} {} /** @brief Convert the Bézier curve to external representation */ template::to(std::declval>()))> constexpr explicit operator U() const { return Implementation::BezierConverter::to(*this); } /** * @brief Raw data * * Contrary to what Doxygen shows, returns reference to an * one-dimensional fixed-size array of @cpp order + 1 @ce elements, * i.e. @cpp T(&)[size] @ce. * @see @ref operator[]() * @todoc Fix once there's a possibility to patch the signature in a * post-processing step (https://github.com/mosra/m.css/issues/56) */ #ifdef DOXYGEN_GENERATING_OUTPUT Vector* data(); constexpr const Vector* data() const; /**< @overload */ #else auto data() -> Vector(&)[order + 1] { return _data; } constexpr auto data() const -> const Vector(&)[order + 1] { return _data; } #endif /** @brief Equality comparison */ bool operator==(const Bezier& other) const { for(std::size_t i = 0; i != order + 1; ++i) if(_data[i] != other._data[i]) return false; return true; } /** @brief Non-equality comparison */ bool operator!=(const Bezier& other) const { return !operator==(other); } /** * @brief Control point access * * @p i should not be larger than @ref Order. */ Vector& operator[](std::size_t i) { return _data[i]; } /* returns const& so [][] operations are also constexpr */ constexpr const Vector& operator[](std::size_t i) const { return _data[i]; } /**< @overload */ /** * @brief Interpolate the curve at given position * * Returns point on the curve for given interpolation factor. Uses * the [De Casteljau's algorithm](https://en.wikipedia.org/wiki/De_Casteljau%27s_algorithm). * @see @ref subdivide() */ Vector value(T t) const { Bezier iPoints[order + 1]; calculateIntermediatePoints(iPoints, t); return iPoints[0][order]; } /** * @brief Subdivide the curve at given position * * Returns two Bézier curves following the original curve, split at * given interpolation factor. Uses the [De Casteljau's algorithm](https://en.wikipedia.org/wiki/De_Casteljau%27s_algorithm). * @see @ref value() */ Containers::Pair, Bezier> subdivide(T t) const { Bezier iPoints[order + 1]; calculateIntermediatePoints(iPoints, t); Bezier left, right; for(std::size_t i = 0; i <= order; ++i) left[i] = iPoints[0][i]; for(std::size_t i = 0, j = order; i <= order; --j, ++i) right[i] = iPoints[i][j]; return {left, right}; } private: /* Implementation for Bezier::Bezier(const Bezier&) */ template constexpr explicit Bezier(Containers::Implementation::Sequence, const Bezier& other) noexcept: _data{Vector(other._data[sequence])...} {} /* Implementation for Bezier::Bezier(ZeroInitT) and Bezier::Bezier(NoInitT) */ /* MSVC 2015 can't handle {} here */ template constexpr explicit Bezier(Containers::Implementation::Sequence, U): _data{Vector((static_cast(sequence), U{typename U::Init{}}))...} {} /* Calculates and returns all intermediate points generated when using De Casteljau's algorithm */ void calculateIntermediatePoints(Bezier(&iPoints)[order + 1], T t) const { for(std::size_t i = 0; i <= order; ++i) { iPoints[i][0] = _data[i]; } for(std::size_t r = 1; r <= order; ++r) { for(std::size_t i = 0; i <= order - r; ++i) { iPoints[i][r] = (1 - t)*iPoints[i][r - 1] + t*iPoints[i + 1][r - 1]; } } } Vector _data[order + 1]; }; /** @brief Quadratic Bézier curve Convenience alternative to @cpp Bezier<2, dimensions, T> @ce. See @ref Bezier for more information. @see @ref QuadraticBezier2D, @ref QuadraticBezier3D */ #ifndef CORRADE_MSVC2015_COMPATIBILITY /* Multiple definitions still broken */ template using QuadraticBezier = Bezier<2, dimensions, T>; #endif /** @brief Two-dimensional quadratic Bézier curve Convenience alternative to @cpp QuadraticBezier<2, T> @ce. See @ref QuadraticBezier and @ref Bezier for more information. @see @ref QuadraticBezier3D, @ref Magnum::QuadraticBezier2D, @ref Magnum::QuadraticBezier2Dd */ #ifndef CORRADE_MSVC2015_COMPATIBILITY /* Multiple definitions still broken */ template using QuadraticBezier2D = QuadraticBezier<2, T>; #endif /** @brief Three-dimensional quadratic Bézier curve Convenience alternative to @cpp QuadraticBezier<3, T> @ce. See @ref QuadraticBezier and @ref Bezier for more information. @see @ref QuadraticBezier2D, @ref Magnum::QuadraticBezier3D, @ref Magnum::QuadraticBezier3Dd */ #ifndef CORRADE_MSVC2015_COMPATIBILITY /* Multiple definitions still broken */ template using QuadraticBezier3D = QuadraticBezier<3, T>; #endif /** @brief Cubic Bézier curve Convenience alternative to @cpp Bezier<3, dimensions, T> @ce. See @ref Bezier for more information. @see @ref CubicBezier2D, @ref CubicBezier3D */ #ifndef CORRADE_MSVC2015_COMPATIBILITY /* Multiple definitions still broken */ template using CubicBezier = Bezier<3, dimensions, T>; #endif /** @brief Two-dimensional cubic Bézier curve Convenience alternative to @cpp CubicBezier<2, T> @ce. See @ref CubicBezier and @ref Bezier for more information. @see @ref CubicBezier3D, @ref Magnum::CubicBezier2D, @ref Magnum::CubicBezier2Dd */ #ifndef CORRADE_MSVC2015_COMPATIBILITY /* Multiple definitions still broken */ template using CubicBezier2D = CubicBezier<2, T>; #endif /** @brief Three-dimensional cubic Bézier curve Convenience alternative to @cpp CubicBezier<3, T> @ce. See @ref CubicBezier and @ref Bezier for more information. @see @ref CubicBezier2D, @ref Magnum::CubicBezier3D, @ref Magnum::CubicBezier3Dd */ #ifndef CORRADE_MSVC2015_COMPATIBILITY /* Multiple definitions still broken */ template using CubicBezier3D = CubicBezier<3, T>; #endif #ifndef CORRADE_SINGLES_NO_DEBUG /** @debugoperator{Bezier} */ template Utility::Debug& operator<<(Debug& debug, const Bezier& value) { debug << "Bezier(" << Debug::nospace; for(UnsignedInt o = 0; o != order + 1; ++o) { debug << (o ? ", {" : "{") << Debug::nospace << value[o][0] << Debug::nospace; for(UnsignedInt i = 1; i != dimensions; ++i) debug << "," << value[o][i] << Debug::nospace; debug << "}" << Debug::nospace; } return debug << ")"; } /* Explicit instantiation for commonly used types */ #ifndef DOXYGEN_GENERATING_OUTPUT extern template MAGNUM_EXPORT Debug& operator<<(Debug&, const Bezier<2, 2, Float>&); extern template MAGNUM_EXPORT Debug& operator<<(Debug&, const Bezier<2, 3, Float>&); extern template MAGNUM_EXPORT Debug& operator<<(Debug&, const Bezier<3, 2, Float>&); extern template MAGNUM_EXPORT Debug& operator<<(Debug&, const Bezier<3, 3, Float>&); extern template MAGNUM_EXPORT Debug& operator<<(Debug&, const Bezier<2, 2, Double>&); extern template MAGNUM_EXPORT Debug& operator<<(Debug&, const Bezier<2, 3, Double>&); extern template MAGNUM_EXPORT Debug& operator<<(Debug&, const Bezier<3, 2, Double>&); extern template MAGNUM_EXPORT Debug& operator<<(Debug&, const Bezier<3, 3, Double>&); #endif #endif #ifndef MAGNUM_NO_MATH_STRICT_WEAK_ORDERING namespace Implementation { template struct StrictWeakOrdering> { bool operator()(const Bezier& a, const Bezier& b) const { StrictWeakOrdering> o; for(std::size_t i = 0; i < order + 1; ++i) { if(o(a[i], b[i])) return true; if(o(b[i], a[i])) return false; } return false; /* a and b are equivalent */ } }; } #endif }} #endif