|
|
|
|
#ifndef Magnum_Math_Frustum_h
|
|
|
|
|
#define Magnum_Math_Frustum_h
|
|
|
|
|
/*
|
|
|
|
|
This file is part of Magnum.
|
|
|
|
|
|
|
|
|
|
Copyright © 2010, 2011, 2012, 2013, 2014, 2015, 2016, 2017, 2018, 2019,
|
|
|
|
|
2020, 2021, 2022, 2023, 2024, 2025, 2026
|
|
|
|
|
Vladimír Vondruš <mosra@centrum.cz>
|
|
|
|
|
Copyright © 2016, 2020 Jonathan Hale <squareys@googlemail.com>
|
|
|
|
|
|
|
|
|
|
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::Frustum
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
/* std::declval() is said to be in <utility> but libstdc++, libc++ and MSVC STL
|
|
|
|
|
all have it directly in <type_traits> because it just makes sense */
|
|
|
|
|
#include <type_traits>
|
|
|
|
|
#include <Corrade/configure.h>
|
|
|
|
|
#ifndef CORRADE_SINGLES_NO_DEBUG
|
|
|
|
|
#include <Corrade/Utility/Debug.h>
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
#include "Magnum/Math/Matrix4.h"
|
|
|
|
|
#include "Magnum/Math/Vector4.h"
|
|
|
|
|
|
|
|
|
|
#ifdef CORRADE_TARGET_WINDOWS /* I so HATE windef.h */
|
|
|
|
|
/* far/near and FAR/NEAR are defined by minwindef.h, but the former are used by this file as variables.
|
|
|
|
|
While they are all empty defines, the Windows headers expect FAR/NEAR to be defined and so we redefine them here.
|
|
|
|
|
far/near are left undefined because they are not used directly in Windows headers.
|
|
|
|
|
*/
|
|
|
|
|
#undef near
|
|
|
|
|
#undef NEAR
|
|
|
|
|
#define NEAR
|
|
|
|
|
#undef far
|
|
|
|
|
#undef FAR
|
|
|
|
|
#define FAR
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
namespace Magnum { namespace Math {
|
|
|
|
|
|
|
|
|
|
namespace Implementation {
|
|
|
|
|
template<class, class> struct FrustumConverter;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
@brief Camera frustum
|
|
|
|
|
|
|
|
|
|
Stores camera frustum planes in order left (index `0`), right (index `1`),
|
|
|
|
|
bottom (index `2`), top (index `3`), near (index `4`) and far (index `5`).
|
|
|
|
|
@see @ref Magnum::Frustum, @ref Magnum::Frustumd,
|
|
|
|
|
@ref Intersection::pointFrustum(), @ref Intersection::rangeFrustum(),
|
|
|
|
|
@ref Intersection::aabbFrustum(), @ref Intersection::sphereFrustum()
|
|
|
|
|
*/
|
|
|
|
|
template<class T> class Frustum {
|
|
|
|
|
public:
|
|
|
|
|
/** @brief Create a frustum from a projection matrix */
|
|
|
|
|
static Frustum<T> fromMatrix(const Matrix4<T>& m) {
|
|
|
|
|
return {m.row(3) + m.row(0),
|
|
|
|
|
m.row(3) - m.row(0),
|
|
|
|
|
m.row(3) + m.row(1),
|
|
|
|
|
m.row(3) - m.row(1),
|
|
|
|
|
m.row(3) + m.row(2),
|
|
|
|
|
m.row(3) - m.row(2)};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @brief Default constructor
|
|
|
|
|
*
|
|
|
|
|
* Equivalent to @ref Frustum(IdentityInitT).
|
|
|
|
|
*/
|
|
|
|
|
constexpr /*implicit*/ Frustum() noexcept: Frustum<T>{IdentityInit} {}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @brief Identity constructor
|
|
|
|
|
*
|
|
|
|
|
* Equivalent to creating a frustum from an identity matrix.
|
|
|
|
|
* @see @ref fromMatrix()
|
|
|
|
|
*/
|
|
|
|
|
constexpr explicit Frustum(IdentityInitT) noexcept;
|
|
|
|
|
|
|
|
|
|
/** @brief Construct a frustum without initializing the contents */
|
|
|
|
|
explicit Frustum(Magnum::NoInitT) noexcept: _data{Vector4<T>{Magnum::NoInit}, Vector4<T>{Magnum::NoInit}, Vector4<T>{Magnum::NoInit}, Vector4<T>{Magnum::NoInit}, Vector4<T>{Magnum::NoInit}, Vector4<T>{Magnum::NoInit}} {}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @brief Construct a frustum from plane equations
|
|
|
|
|
*
|
|
|
|
|
* The equations are in a form @f$ ax + by + cz + d = 0 @f$. You can
|
|
|
|
|
* use @ref planeEquation() to calculate the coefficients from a normal
|
|
|
|
|
* and a point.
|
|
|
|
|
*/
|
|
|
|
|
constexpr /*implicit*/ Frustum(const Vector4<T>& left, const Vector4<T>& right, const Vector4<T>& bottom, const Vector4<T>& top, const Vector4<T>& near, const Vector4<T>& far) noexcept: _data{left, right, bottom, top, near, far} {}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @brief Construct a frustum from another of different type
|
|
|
|
|
*
|
|
|
|
|
* Performs only default casting on the values, no rounding or
|
|
|
|
|
* anything else.
|
|
|
|
|
*/
|
|
|
|
|
template<class U> constexpr explicit Frustum(const Frustum<U>& other) noexcept;
|
|
|
|
|
|
|
|
|
|
/** @brief Construct a frustum from external representation */
|
|
|
|
|
template<class U, class = decltype(Implementation::FrustumConverter<T, U>::from(std::declval<U>()))> constexpr explicit Frustum(const U& other) noexcept: Frustum<T>{Implementation::FrustumConverter<T, U>::from(other)} {}
|
|
|
|
|
|
|
|
|
|
/** @brief Convert the frustum to external representation */
|
|
|
|
|
template<class U, class = decltype(Implementation::FrustumConverter<T, U>::to(std::declval<Frustum<T>>()))> constexpr explicit operator U() const {
|
|
|
|
|
return Implementation::FrustumConverter<T, U>::to(*this);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @brief Equality comparison
|
|
|
|
|
*
|
|
|
|
|
* Done by comparing the underlying vectors, which internally uses
|
|
|
|
|
* @ref TypeTraits::equals(), i.e. a fuzzy compare.
|
|
|
|
|
*/
|
|
|
|
|
bool operator==(const Frustum<T>& other) const {
|
|
|
|
|
for(std::size_t i = 0; i != 6; ++i)
|
|
|
|
|
if(_data[i] != other._data[i]) return false;
|
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @brief Non-equality comparison
|
|
|
|
|
*
|
|
|
|
|
* Done by comparing the underlying vectors, which internally uses
|
|
|
|
|
* @ref TypeTraits::equals(), i.e. a fuzzy compare.
|
|
|
|
|
*/
|
|
|
|
|
bool operator!=(const Frustum<T>& other) const {
|
|
|
|
|
return !operator==(other);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @brief Raw data
|
|
|
|
|
*
|
|
|
|
|
* Contrary to what Doxygen shows, returns reference to a
|
|
|
|
|
* one-dimensional fixed-size array of 24 elements, i.e.
|
|
|
|
|
* @cpp T(&)[24] @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
|
|
|
|
|
T* data();
|
|
|
|
|
const T* data() const; /**< @overload */
|
|
|
|
|
#else
|
|
|
|
|
auto data() -> T(&)[24] {
|
|
|
|
|
return reinterpret_cast<T(&)[24]>(_data);
|
|
|
|
|
}
|
|
|
|
|
/* Can't really be constexpr anymore, the only other solution is having
|
|
|
|
|
an union with `T _rawData[24]` and return that, but in a constexpr
|
|
|
|
|
context it'd mean we'd have to initialize it instead of `_data` in
|
|
|
|
|
the constructor ... and then operator[]() could not be constexpr
|
|
|
|
|
anymore. I can't think of a practical use case where constexpr
|
|
|
|
|
data() would be needed and operator[]() could not be used instead.
|
|
|
|
|
Similarly to RectangularMatrix::data(), having a statically sized
|
|
|
|
|
array returned is a far more useful property than constexpr, so that
|
|
|
|
|
wins. */
|
|
|
|
|
auto data() const -> const T(&)[24] {
|
|
|
|
|
return reinterpret_cast<const T(&)[24]>(_data);
|
|
|
|
|
}
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @brief Plane at given index
|
|
|
|
|
* @m_since{2020,06}
|
|
|
|
|
*
|
|
|
|
|
* Expects that @p i is less than @cpp 6 @ce.
|
|
|
|
|
*/
|
|
|
|
|
CORRADE_CONSTEXPR14 Vector4<T>& operator[](std::size_t i) {
|
|
|
|
|
CORRADE_DEBUG_ASSERT(i < 6, "Math::Frustum::operator[](): index" << i << "out of range",
|
|
|
|
|
_data[i]);
|
|
|
|
|
return _data[i];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/** @overload */
|
|
|
|
|
/* returns const& so [][] operations are also constexpr */
|
|
|
|
|
constexpr const Vector4<T>& operator[](std::size_t i) const {
|
|
|
|
|
return CORRADE_CONSTEXPR_DEBUG_ASSERT(i < 6, "Math::Frustum::operator[](): index" << i << "out of range"), _data[i];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @brief First plane
|
|
|
|
|
* @m_since{2019,10}
|
|
|
|
|
*
|
|
|
|
|
* Together with @ref end() useful for range access, for example here
|
|
|
|
|
* to check for a point/frustum intersection, similarly to
|
|
|
|
|
* @ref Intersection::pointFrustum():
|
|
|
|
|
*
|
|
|
|
|
* @snippet Math.cpp Frustum-range
|
|
|
|
|
*/
|
|
|
|
|
CORRADE_CONSTEXPR14 Vector4<T>* begin() { return _data; }
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @overload
|
|
|
|
|
* @m_since{2019,10}
|
|
|
|
|
*/
|
|
|
|
|
constexpr const Vector4<T>* begin() const { return _data; }
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @overload
|
|
|
|
|
* @m_since{2019,10}
|
|
|
|
|
*/
|
|
|
|
|
constexpr const Vector4<T>* cbegin() const { return _data; }
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @brief (One after) last plane
|
|
|
|
|
* @m_since{2019,10}
|
|
|
|
|
*/
|
|
|
|
|
CORRADE_CONSTEXPR14 Vector4<T>* end() { return _data + 6; }
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @overload
|
|
|
|
|
* @m_since{2019,10}
|
|
|
|
|
*/
|
|
|
|
|
constexpr const Vector4<T>* end() const { return _data + 6; }
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @overload
|
|
|
|
|
* @m_since{2019,10}
|
|
|
|
|
*/
|
|
|
|
|
constexpr const Vector4<T>* cend() const { return _data + 6; }
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @brief Left plane
|
|
|
|
|
* @m_since{2020,06}
|
|
|
|
|
*/
|
|
|
|
|
CORRADE_CONSTEXPR14 Vector4<T>& left() { return _data[0]; }
|
|
|
|
|
constexpr Vector4<T> left() const { return _data[0]; } /**< @overload */
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @brief Right plane
|
|
|
|
|
* @m_since{2020,06}
|
|
|
|
|
*/
|
|
|
|
|
CORRADE_CONSTEXPR14 Vector4<T>& right() { return _data[1]; }
|
|
|
|
|
constexpr Vector4<T> right() const { return _data[1]; } /**< @overload */
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @brief Bottom plane
|
|
|
|
|
* @m_since{2020,06}
|
|
|
|
|
*/
|
|
|
|
|
CORRADE_CONSTEXPR14 Vector4<T>& bottom() { return _data[2]; }
|
|
|
|
|
constexpr Vector4<T> bottom() const { return _data[2]; } /**< @overload */
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @brief Top plane
|
|
|
|
|
* @m_since{2020,06}
|
|
|
|
|
*/
|
|
|
|
|
CORRADE_CONSTEXPR14 Vector4<T>& top() { return _data[3]; }
|
|
|
|
|
constexpr Vector4<T> top() const { return _data[3]; } /**< @overload */
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @brief Near plane
|
|
|
|
|
* @m_since{2020,06}
|
|
|
|
|
*/
|
|
|
|
|
CORRADE_CONSTEXPR14 Vector4<T>& near() { return _data[4]; }
|
|
|
|
|
constexpr Vector4<T> near() const { return _data[4]; } /**< @overload */
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @brief Far plane
|
|
|
|
|
* @m_since{2020,06}
|
|
|
|
|
*/
|
|
|
|
|
CORRADE_CONSTEXPR14 Vector4<T>& far() { return _data[5]; }
|
|
|
|
|
constexpr Vector4<T> far() const { return _data[5]; } /**< @overload */
|
|
|
|
|
|
|
|
|
|
private:
|
|
|
|
|
Vector4<T> _data[6];
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
#ifndef CORRADE_SINGLES_NO_DEBUG
|
|
|
|
|
/** @debugoperator{Frustum} */
|
|
|
|
|
template<class T> Debug& operator<<(Debug& debug, const Frustum<T>& value) {
|
|
|
|
|
debug << "Frustum({" << Debug::nospace;
|
|
|
|
|
for(std::size_t i = 0; i != 6; ++i) {
|
|
|
|
|
if(i != 0) debug << Debug::nospace << "},\n {" << Debug::nospace;
|
|
|
|
|
for(std::size_t j = 0; j != 4; ++j) {
|
|
|
|
|
if(j != 0) debug << Debug::nospace << ",";
|
|
|
|
|
debug << value[i][j];
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return debug << Debug::nospace << "})";
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* Explicit instantiation for commonly used types */
|
|
|
|
|
#ifndef DOXYGEN_GENERATING_OUTPUT
|
|
|
|
|
extern template MAGNUM_EXPORT Debug& operator<<(Debug&, const Frustum<Float>&);
|
|
|
|
|
extern template MAGNUM_EXPORT Debug& operator<<(Debug&, const Frustum<Double>&);
|
|
|
|
|
#endif
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
template<class T> constexpr Frustum<T>::Frustum(IdentityInitT) noexcept: _data{
|
|
|
|
|
{ 1.0f, 0.0f, 0.0f, 1.0f},
|
|
|
|
|
{-1.0f, 0.0f, 0.0f, 1.0f},
|
|
|
|
|
{ 0.0f, 1.0f, 0.0f, 1.0f},
|
|
|
|
|
{ 0.0f, -1.0f, 0.0f, 1.0f},
|
|
|
|
|
{ 0.0f, 0.0f, 1.0f, 1.0f},
|
|
|
|
|
{ 0.0f, 0.0f, -1.0f, 1.0f}} {}
|
|
|
|
|
|
|
|
|
|
template<class T> template<class U> constexpr Frustum<T>::Frustum(const Frustum<U>& other) noexcept: _data{
|
|
|
|
|
Vector4<T>{other[0]},
|
|
|
|
|
Vector4<T>{other[1]},
|
|
|
|
|
Vector4<T>{other[2]},
|
|
|
|
|
Vector4<T>{other[3]},
|
|
|
|
|
Vector4<T>{other[4]},
|
|
|
|
|
Vector4<T>{other[5]}} {}
|
|
|
|
|
|
|
|
|
|
#ifndef MAGNUM_NO_MATH_STRICT_WEAK_ORDERING
|
|
|
|
|
namespace Implementation {
|
|
|
|
|
|
|
|
|
|
template<class T> struct StrictWeakOrdering<Frustum<T>> {
|
|
|
|
|
bool operator()(const Frustum<T>& a, const Frustum<T>& b) const {
|
|
|
|
|
StrictWeakOrdering<Vector<4, T>> o;
|
|
|
|
|
for(std::size_t i = 0; i < 6; ++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
|