Browse Source

MeshTools: reworked and updated transform*().

Also added just-to-be-sure unit test.
pull/7/head
Vladimír Vondruš 13 years ago
parent
commit
c00195c24b
  1. 1
      src/MeshTools/Test/CMakeLists.txt
  2. 109
      src/MeshTools/Test/TransformTest.cpp
  3. 109
      src/MeshTools/Transform.h

1
src/MeshTools/Test/CMakeLists.txt

@ -7,6 +7,7 @@ corrade_add_test(MeshToolsInterleaveTest InterleaveTest.cpp)
corrade_add_test(MeshToolsSubdivideTest SubdivideTest.cpp)
# corrade_add_test(MeshToolsSubdivideCleanBenchmark SubdivideCleanBenchmark.h SubdivideCleanBenchmark.cpp MagnumPrimitives)
corrade_add_test(MeshToolsTipsifyTest TipsifyTest.cpp LIBRARIES MagnumMeshTools)
corrade_add_test(MeshToolsTransformTest TransformTest.cpp LIBRARIES MagnumMeshTools)
# Graceful assert for testing
set_target_properties(MeshToolsCombineIndexedArraysTest MeshToolsInterleaveTest MeshToolsSubdivideTest PROPERTIES COMPILE_FLAGS -DCORRADE_GRACEFUL_ASSERT)

109
src/MeshTools/Test/TransformTest.cpp

@ -0,0 +1,109 @@
/*
Copyright © 2010, 2011, 2012 Vladimír Vondruš <mosra@centrum.cz>
This file is part of Magnum.
Magnum is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License version 3
only, as published by the Free Software Foundation.
Magnum is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Lesser General Public License version 3 for more details.
*/
#include <array>
#include <TestSuite/Tester.h>
#include "Math/Matrix3.h"
#include "Magnum.h"
#include "MeshTools/Transform.h"
namespace Magnum { namespace MeshTools { namespace Test {
class TransformTest: public Corrade::TestSuite::Tester {
public:
explicit TransformTest();
void transformVectors2D();
void transformVectors3D();
void transformPoints2D();
void transformPoints3D();
};
TransformTest::TransformTest() {
addTests(&TransformTest::transformVectors2D,
&TransformTest::transformVectors3D,
&TransformTest::transformPoints2D,
&TransformTest::transformPoints3D);
}
constexpr static std::array<Vector2, 2> points2D{{
{-3.0f, 4.0f},
{ 2.5f, -15.0f}
}};
constexpr static std::array<Vector2, 2> points2DRotated{{
{-4.0f, -3.0f},
{15.0f, 2.5f}
}};
constexpr static std::array<Vector2, 2> points2DRotatedTranslated{{
{-4.0f, -4.0f},
{15.0f, 1.5f}
}};
constexpr static std::array<Vector3, 2> points3D{{
{-3.0f, 4.0f, 34.0f},
{ 2.5f, -15.0f, 1.5f}
}};
constexpr static std::array<Vector3, 2> points3DRotated{{
{-4.0f, -3.0f, 34.0f},
{15.0f, 2.5f, 1.5f}
}};
constexpr static std::array<Vector3, 2> points3DRotatedTranslated{{
{-4.0f, -4.0f, 34.0f},
{15.0f, 1.5f, 1.5f}
}};
void TransformTest::transformVectors2D() {
auto matrix = MeshTools::transformVectors(Matrix3::rotation(Deg(90.0f)), points2D);
auto complex = MeshTools::transformVectors(Complex::rotation(Deg(90.0f)), points2D);
CORRADE_COMPARE(matrix, points2DRotated);
CORRADE_COMPARE(complex, points2DRotated);
}
void TransformTest::transformVectors3D() {
auto matrix = MeshTools::transformVectors(Matrix4::rotationZ(Deg(90.0f)), points3D);
auto complex = MeshTools::transformVectors(Quaternion::rotation(Deg(90.0f), Vector3::zAxis()), points3D);
CORRADE_COMPARE(matrix, points3DRotated);
CORRADE_COMPARE(complex, points3DRotated);
}
void TransformTest::transformPoints2D() {
auto matrix = MeshTools::transformPoints(
Matrix3::translation(Vector2::yAxis(-1.0f))*Matrix3::rotation(Deg(90.0f)), points2D);
CORRADE_COMPARE(matrix, points2DRotatedTranslated);
}
void TransformTest::transformPoints3D() {
auto matrix = MeshTools::transformPoints(
Matrix4::translation(Vector3::yAxis(-1.0f))*Matrix4::rotationZ(Deg(90.0f)), points3D);
auto complex = MeshTools::transformPoints(
DualQuaternion::translation(Vector3::yAxis(-1.0f))*DualQuaternion::rotation(Deg(90.0f), Vector3::zAxis()), points3D);
CORRADE_COMPARE(matrix, points3DRotatedTranslated);
CORRADE_COMPARE(complex, points3DRotatedTranslated);
}
}}}
CORRADE_TEST_MAIN(Magnum::MeshTools::Test::TransformTest)

109
src/MeshTools/Transform.h

@ -16,27 +16,116 @@
*/
/** @file
* @brief Function Magnum::MeshTools::transform()
* @brief Function Magnum::MeshTools::transformVectorsInPlace(), Magnum::MeshTools::transformVectors(), Magnum::MeshTools::transformPointsInPlace(), Magnum::MeshTools::transformPoints()
*/
#include "Math/Matrix.h"
#include "Math/Complex.h"
#include "Math/DualQuaternion.h"
#include "Math/Matrix3.h"
namespace Magnum { namespace MeshTools {
/**
@brief Transform vertices using given matrix
@brief Transform vectors in-place using given transformation
Usable for mesh transformations that would otherwise negatively affect
dependent objects, such as (uneven) scaling. Example usage:
Usable for one-time mesh transformations that would otherwise negatively affect
dependent objects, such as (uneven) scaling. Accepts any forward-iterable type
with compatible vector type as @p vectors. Expects that @ref Math::Quaternion "Quaternion"
or @ref Math::Complex "Complex" is normalized, no further requirements are for
transformation matrices.
Unlike in transformPointsInPlace(), the transformation does not involve
translation.
Example usage:
@code
std::vector<Vector3> vectors;
auto transformation = Quaternion::rotation(35.0_degf, Vector3::yAxis());
MeshTools::transformVectorsInPlace(rotation, vectors);
@endcode
@see transformVectors(), Matrix3::transformVector(), Matrix4::transformVector(),
Complex::transformVectorNormalized(), Quaternion::transformVectorNormalized()
@todo GPU transform feedback implementation (otherwise this is only bad joke)
*/
template<class T, class U> void transformVectorsInPlace(const Math::Quaternion<T>& normalizedQuaternion, U& vectors) {
for(auto& vector: vectors) vector = normalizedQuaternion.transformVectorNormalized(vector);
}
/** @overload */
template<class T, class U> void transformVectorsInPlace(const Math::Complex<T>& normalizedComplex, U& vectors) {
for(auto& vector: vectors) vector = normalizedComplex.transformVectorNormalized(vector);
}
/** @overload */
template<class T, class U> void transformVectorsInPlace(const Math::Matrix3<T>& matrix, U& vectors) {
for(auto& vector: vectors) vector = matrix.transformVector(vector);
}
/** @overload */
template<class T, class U> void transformVectorsInPlace(const Math::Matrix4<T>& matrix, U& vectors) {
for(auto& vector: vectors) vector = matrix.transformVector(vector);
}
/**
@brief Transform vectors using given transformation
Returns transformed vectors instead of modifying them in-place. See
transformVectorsInPlace() for more information.
*/
template<class T, class U> U transformVectors(const T& transformation, U vectors) {
U result(std::move(vectors));
transformVectorsInPlace(transformation, result);
return result;
}
/**
@brief Transform points in-place using given transformation
Usable for one-time mesh transformations that would otherwise negatively affect
dependent objects, such as (uneven) scaling. Accepts any forward-iterable type
with compatible vector type as @p vectors. Expects that
@ref Math::DualQuaternion "DualQuaternion" is normalized, no further
requirements are for transformation matrices.
Unlike in transformVectorsInPlace(), the transformation also involves
translation.
Example usage:
@code
std::vector<Point3D> vertices;
MeshTools::transform(Matrix4::scaling({2.0f, 0.5f, 0.0f}), vertices);
std::vector<Vector3> points;
auto transformation = DualQuaternion::rotation(35.0_degf, Vector3::yAxis())*
DualQuaternion::translation({0.5f, -1.0f, 3.0f});
MeshTools::transformPointsInPlace(rotation, points);
@endcode
@see transformPoints(), Matrix3::transformPoint(), Matrix4::transformPoint(),
DualQuaternion::transformPointNormalized()
*/
template<class T, class U> void transformPointsInPlace(const Math::DualQuaternion<T>& normalizedDualQuaternion, U& points) {
for(auto& point: points) point = normalizedDualQuaternion.transformPointNormalized(point);
}
/** @overload */
template<class T, class U> void transformPointsInPlace(const Math::Matrix3<T>& matrix, U& points) {
for(auto& point: points) point = matrix.transformPoint(point);
}
/** @overload */
template<class T, class U> void transformPointsInPlace(const Math::Matrix4<T>& matrix, U& points) {
for(auto& point: points) point = matrix.transformPoint(point);
}
/**
@brief Transform points using given transformation
Returns transformed points instead of modifying them in-place. See
transformPointsInPlace() for more information.
*/
template<std::size_t size, class T, class U> inline void transform(const Math::Matrix<size, T>& matrix, U& vertices) {
for(Math::Vector<size, T>& vertex: vertices)
vertex = matrix*vertex;
template<class T, class U> U transformPoints(const T& transformation, U vectors) {
U result(std::move(vectors));
transformPointsInPlace(transformation, result);
return result;
}
}}

Loading…
Cancel
Save