Browse Source

Math: Add FrustumTest

Signed-off-by: Squareys <Squareys@googlemail.com>
pull/185/head
Squareys 10 years ago
parent
commit
0797e4a7d0
  1. 19
      src/Magnum/Math/Frustum.h
  2. 1
      src/Magnum/Math/Test/CMakeLists.txt
  3. 89
      src/Magnum/Math/Test/FrustumTest.cpp

19
src/Magnum/Math/Frustum.h

@ -35,6 +35,7 @@
#include <Corrade/Utility/Debug.h>
#include "Magnum/Math/Matrix4.h"
#include "Magnum/Math/Vector4.h"
namespace Magnum { namespace Math {
@ -50,19 +51,19 @@ template<class T> class Frustum {
*/
static Frustum<T> fromMatrix(const Matrix4<T>& m) {
return Frustum{
mvp.row(3) + mvp.row(0),
mvp.row(3) - mvp.row(0),
mvp.row(3) + mvp.row(1),
mvp.row(3) - mvp.row(1),
mvp.row(3) + mvp.row(2),
mvp.row(3) - mvp.row(2)
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 Construct frustum from frustum planes
*/
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& far): _data{left, right, bottom, top, near, far} {}
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): _data{left, right, bottom, top, near, far} {}
/**
* @brief Raw data
@ -79,7 +80,9 @@ template<class T> class Frustum {
* In order left (index `0`), right (index `1`), bottom (index `1`),
* top (index `3`), near (index `4`), far (index `5`).
*/
constexpr Corrade::Containers::StaticArrayView<6, Vector4<T>> planes() const { return _data; }
constexpr Corrade::Containers::StaticArrayView<6, const Vector4<T>> planes() const {
return Corrade::Containers::StaticArrayView<6, const Vector4<T>>{_data};
}
/**
* @brief Plane at given index

1
src/Magnum/Math/Test/CMakeLists.txt

@ -52,6 +52,7 @@ corrade_add_test(MathQuaternionTest QuaternionTest.cpp LIBRARIES MagnumMathTestL
corrade_add_test(MathDualQuaternionTest DualQuaternionTest.cpp LIBRARIES MagnumMathTestLib)
corrade_add_test(MathBezierTest BezierTest.cpp LIBRARIES MagnumMathTestLib)
corrade_add_test(MathFrustumTest FrustumTest.cpp LIBRARIES MagnumMathTestLib)
set_property(TARGET
MathVectorTest

89
src/Magnum/Math/Test/FrustumTest.cpp

@ -0,0 +1,89 @@
/*
This file is part of Magnum.
Copyright © 2010, 2011, 2012, 2013, 2014, 2015, 2016
Vladimír Vondruš <mosra@centrum.cz>
Copyright © 2016 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.
*/
#include <cmath>
#include <Corrade/TestSuite/Tester.h>
#include <Corrade/TestSuite/Compare/Container.h>
#include "Magnum/Math/Frustum.h"
using namespace Corrade;
namespace Magnum { namespace Math { namespace Test {
struct FrustumTest: TestSuite::Tester {
explicit FrustumTest();
void construct();
void constructFromMatrix();
};
typedef Vector4<Float> Vector4;
typedef Matrix4<Float> Matrix4;
typedef Frustum<Float> Frustum;
typedef Deg<Float> Degf;
FrustumTest::FrustumTest() {
addTests({&FrustumTest::construct,
&FrustumTest::constructFromMatrix});
}
void FrustumTest::construct() {
Vector4 planes[6]{
{-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}};
Frustum frustum{
planes[0], planes[1],
planes[2], planes[3],
planes[4], planes[5],
};
CORRADE_COMPARE_AS(frustum.planes(), Containers::ArrayView<const Vector4>(planes), TestSuite::Compare::Container);
}
void FrustumTest::constructFromMatrix() {
Vector4 planes[6]{
{ 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, 0.0f},
{ 0.0f, 0.0f,-2.22222f,-2.22222f},
{ 0.0f, 0.0f, 0.22222f, 2.22222f}};
const Frustum frustum = Frustum::fromMatrix(
Matrix4::perspectiveProjection(Degf(90.0f), 1.0f, 1.0f, 10.0f));
CORRADE_COMPARE_AS(frustum.planes(), Containers::ArrayView<const Vector4>(planes), TestSuite::Compare::Container);
}
}}}
CORRADE_TEST_MAIN(Magnum::Math::Test::FrustumTest)
Loading…
Cancel
Save