Browse Source

Math: rename Quaternion::fromRotation() to rotation().

Now it is similar in usage to Matrix4 functions and is now also
crossreferenced in documentation. Also updated the test to also check
assertion.
pull/7/head
Vladimír Vondruš 13 years ago
parent
commit
a0b8dcc067
  1. 13
      src/Math/Matrix4.h
  2. 9
      src/Math/Quaternion.h
  3. 25
      src/Math/Test/QuaternionTest.cpp

13
src/Math/Matrix4.h

@ -70,8 +70,8 @@ template<class T> class Matrix4: public Matrix<4, T> {
*
* Expects that the rotation axis is normalized. If possible, use
* faster alternatives like rotationX(), rotationY() and rotationZ().
* @see rotation() const, Matrix3::rotation(T), Vector3::xAxis(),
* Vector3::yAxis(), Vector3::zAxis(), deg(), rad()
* @see rotation() const, Quaternion::rotation(), Matrix3::rotation(T),
* Vector3::xAxis(), Vector3::yAxis(), Vector3::zAxis(), deg(), rad()
*/
static Matrix4<T> rotation(T angle, const Vector3<T>& normalizedAxis) {
CORRADE_ASSERT(MathTypeTraits<T>::equals(normalizedAxis.dot(), T(1)),
@ -111,7 +111,8 @@ template<class T> class Matrix4: public Matrix<4, T> {
*
* Faster than calling `Matrix4::rotation(angle, Vector3::xAxis())`.
* @see rotation(T, const Vector3&), rotationY(), rotationZ(),
* rotation() const, Matrix3::rotation(T), deg(), rad()
* rotation() const, Quaternion::rotation(), Matrix3::rotation(T),
* deg(), rad()
*/
static Matrix4<T> rotationX(T angle) {
T sine = std::sin(angle);
@ -129,7 +130,8 @@ template<class T> class Matrix4: public Matrix<4, T> {
*
* Faster than calling `Matrix4::rotation(angle, Vector3::yAxis())`.
* @see rotation(T, const Vector3&), rotationX(), rotationZ(),
* rotation() const, Matrix3::rotation(T), deg(), rad()
* rotation() const, Quaternion::rotation(), Matrix3::rotation(T),
* deg(), rad()
*/
static Matrix4<T> rotationY(T angle) {
T sine = std::sin(angle);
@ -147,7 +149,8 @@ template<class T> class Matrix4: public Matrix<4, T> {
*
* Faster than calling `Matrix4::rotation(angle, Vector3::zAxis())`.
* @see rotation(T, const Vector3&), rotationX(), rotationY(),
* rotation() const, Matrix3::rotation(T), deg(), rad()
* rotation() const, Quaternion::rotation(), Matrix3::rotation(T),
* deg(), rad()
*/
static Matrix4<T> rotationZ(T angle) {
T sine = std::sin(angle);

9
src/Math/Quaternion.h

@ -110,10 +110,11 @@ template<class T> class Quaternion {
* Expects that the rotation axis is normalized. @f[
* q = [\boldsymbol a \cdot sin \frac \theta 2, cos \frac \theta 2]
* @f]
* @see Matrix4::rotation()
*/
inline static Quaternion<T> fromRotation(T angle, const Vector3<T>& normalizedAxis) {
inline static Quaternion<T> rotation(T angle, const Vector3<T>& normalizedAxis) {
CORRADE_ASSERT(MathTypeTraits<T>::equals(normalizedAxis.dot(), T(1)),
"Math::Quaternion::fromRotation(): axis must be normalized", {});
"Math::Quaternion::rotation(): axis must be normalized", {});
return {normalizedAxis*std::sin(angle/2), std::cos(angle/2)};
}
@ -153,7 +154,7 @@ template<class T> class Quaternion {
* Expects that the quaternion is normalized. @f[
* \theta = 2 \cdot acos q_S
* @f]
* @see rotationAxis(), fromRotation()
* @see rotationAxis(), rotation()
*/
inline T rotationAngle() const {
CORRADE_ASSERT(MathTypeTraits<T>::equals(dot(), T(1)),
@ -168,7 +169,7 @@ template<class T> class Quaternion {
* Expects that the quaternion is normalized. @f[
* \boldsymbol a = \frac{\boldsymbol q_V}{\sqrt{1 - q_S^2}}
* @f]
* @see rotationAngle(), fromRotation()
* @see rotationAngle(), rotation()
*/
inline Vector3<T> rotationAxis() const {
CORRADE_ASSERT(MathTypeTraits<T>::equals(dot(), T(1)),

25
src/Math/Test/QuaternionTest.cpp

@ -175,16 +175,23 @@ void QuaternionTest::invertedNormalized() {
}
void QuaternionTest::rotation() {
std::ostringstream o;
Error::setOutput(&o);
float angle = deg(120.0f);
Vector3 axis(1.0f/Constants<float>::sqrt3());
Quaternion q = Quaternion::fromRotation(angle, axis);
CORRADE_COMPARE(Quaternion::rotation(deg(-74.0f), {-1.0f, 2.0f, 2.0f}), Quaternion());
CORRADE_COMPARE(o.str(), "Math::Quaternion::rotation(): axis must be normalized\n");
Quaternion q = Quaternion::rotation(angle, axis);
CORRADE_COMPARE(q, Quaternion(Vector3(0.5f, 0.5f, 0.5f), 0.5f));
CORRADE_COMPARE(q.rotationAngle(), angle);
CORRADE_COMPARE(q.rotationAxis(), axis);
CORRADE_COMPARE(q.rotationAxis().length(), 1.0f);
/* Verify negative angle */
Quaternion q2 = Quaternion::fromRotation(deg(-120.0f), axis);
Quaternion q2 = Quaternion::rotation(deg(-120.0f), axis);
CORRADE_COMPARE(q2, Quaternion(Vector3(-0.5f, -0.5f, -0.5f), 0.5f));
CORRADE_COMPARE(q2.rotationAngle(), deg(120.0f));
CORRADE_COMPARE(q2.rotationAxis(), -axis);
@ -209,7 +216,7 @@ void QuaternionTest::angle() {
void QuaternionTest::matrix() {
float angle = deg(37.0f);
Vector3 axis(1.0f/Constants<float>::sqrt3());
Quaternion q = Quaternion::fromRotation(angle, axis);
Quaternion q = Quaternion::rotation(angle, axis);
Matrix<3, float> expected = Matrix4<float>::rotation(angle, axis).rotationScaling();
CORRADE_COMPARE(q.matrix(), expected);
@ -218,8 +225,8 @@ void QuaternionTest::matrix() {
}
void QuaternionTest::lerp() {
Quaternion a = Quaternion::fromRotation(deg(15.0f), Vector3(1.0f/Constants<float>::sqrt3()));
Quaternion b = Quaternion::fromRotation(deg(23.0f), Vector3::xAxis());
Quaternion a = Quaternion::rotation(deg(15.0f), Vector3(1.0f/Constants<float>::sqrt3()));
Quaternion b = Quaternion::rotation(deg(23.0f), Vector3::xAxis());
std::ostringstream o;
Corrade::Utility::Error::setOutput(&o);
@ -240,8 +247,8 @@ void QuaternionTest::lerp() {
}
void QuaternionTest::slerp() {
Quaternion a = Quaternion::fromRotation(deg(15.0f), Vector3(1.0f/Constants<float>::sqrt3()));
Quaternion b = Quaternion::fromRotation(deg(23.0f), Vector3::xAxis());
Quaternion a = Quaternion::rotation(deg(15.0f), Vector3(1.0f/Constants<float>::sqrt3()));
Quaternion b = Quaternion::rotation(deg(23.0f), Vector3::xAxis());
std::ostringstream o;
Corrade::Utility::Error::setOutput(&o);
@ -262,7 +269,7 @@ void QuaternionTest::slerp() {
}
void QuaternionTest::rotateVector() {
Quaternion a = Quaternion::fromRotation(deg(23.0f), Vector3::xAxis());
Quaternion a = Quaternion::rotation(deg(23.0f), Vector3::xAxis());
Vector3 v(0.0f, -3.6f, 0.7f);
Vector3 rotated = a.rotateVector(v);
@ -272,7 +279,7 @@ void QuaternionTest::rotateVector() {
}
void QuaternionTest::rotateVectorNormalized() {
Quaternion a = Quaternion::fromRotation(deg(23.0f), Vector3::xAxis());
Quaternion a = Quaternion::rotation(deg(23.0f), Vector3::xAxis());
Vector3 v(0.0f, -3.6f, 0.7f);
std::ostringstream o;

Loading…
Cancel
Save