Browse Source

Math: explicit one-parameter Point*D constructor.

Was causing improper implicit conversions, such as here (example
directly from unit tests, where it was unintentionally used):

    Vector3 normal;
    Matrix4 transformation;
    auto transformedNormal = transformation*normal;

Not only that it was possible to multiply 4x4 matrix with 3-component
vector, but the resulting type was Point3D which was absolutely
confusing. Currently it must be explicitly converted:

    transformedNormal = transformation*Point3D(normal);
pull/7/head
Vladimír Vondruš 13 years ago
parent
commit
0274ad7d56
  1. 9
      src/Math/Point2D.h
  2. 9
      src/Math/Point3D.h
  3. 3
      src/Math/Test/Matrix3Test.cpp
  4. 3
      src/Math/Test/Matrix4Test.cpp
  5. 3
      src/Primitives/Icosphere.cpp

9
src/Math/Point2D.h

@ -54,7 +54,14 @@ template<class T> class Point2D: public Vector3<T> {
* @param xy Two-component vector
* @param z Z component
*/
inline constexpr /*implicit*/ Point2D(const Vector2<T>& xy, T z = T(1)): Vector3<T>(xy, z) {}
inline constexpr /*implicit*/ Point2D(const Vector2<T>& xy, T z): Vector3<T>(xy, z) {}
/**
* @brief Construct 2D point from 2D vector
*
* Z component is set to `1`.
*/
inline constexpr explicit Point2D(const Vector2<T>& xy): Vector3<T>(xy, T(1)) {}
/** @copydoc Vector::Vector(const Vector<size, U>&) */
template<class U> inline constexpr explicit Point2D(const Vector<3, U>& other): Vector3<T>(other) {}

9
src/Math/Point3D.h

@ -55,7 +55,14 @@ template<class T> class Point3D: public Vector4<T> {
* @param xyz Three-component vector
* @param w W component
*/
inline constexpr /*implicit*/ Point3D(const Vector3<T>& xyz, T w = T(1)): Vector4<T>(xyz, w) {}
inline constexpr /*implicit*/ Point3D(const Vector3<T>& xyz, T w): Vector4<T>(xyz, w) {}
/**
* @brief Construct 3D point from 3D vector
*
* W component is set to `1`.
*/
inline constexpr explicit Point3D(const Vector3<T>& xyz): Vector4<T>(xyz, T(1)) {}
/** @copydoc Vector::Vector(const Vector<size, U>&) */
template<class U> inline constexpr explicit Point3D(const Vector<4, U>& other): Vector4<T>(other) {}

3
src/Math/Test/Matrix3Test.cpp

@ -48,6 +48,7 @@ class Matrix3Test: public Corrade::TestSuite::Tester {
typedef Math::Matrix3<float> Matrix3;
typedef Math::Matrix<2, float> Matrix2;
typedef Math::Vector2<float> Vector2;
typedef Math::Point2D<float> Point2D;
Matrix3Test::Matrix3Test() {
addTests(&Matrix3Test::constructIdentity,
@ -122,7 +123,7 @@ void Matrix3Test::reflection() {
{0.0f, 0.0f, 1.0f});
CORRADE_COMPARE(actual*actual, Matrix3());
CORRADE_COMPARE(actual*normal, -normal);
CORRADE_COMPARE((actual*Point2D(normal)).vector(), -normal);
CORRADE_COMPARE(actual, expected);
}

3
src/Math/Test/Matrix4Test.cpp

@ -53,6 +53,7 @@ class Matrix4Test: public Corrade::TestSuite::Tester {
typedef Math::Matrix4<float> Matrix4;
typedef Math::Matrix<3, float> Matrix3;
typedef Math::Vector3<float> Vector3;
typedef Math::Point3D<float> Point3D;
Matrix4Test::Matrix4Test() {
addTests(&Matrix4Test::constructIdentity,
@ -170,7 +171,7 @@ void Matrix4Test::reflection() {
{ 0.0f, 0.0f, 0.0f, 1.0f});
CORRADE_COMPARE(actual*actual, Matrix4());
CORRADE_COMPARE(actual*normal, -normal);
CORRADE_COMPARE((actual*Point3D(normal)).vector(), -normal);
CORRADE_COMPARE(actual, expected);
}

3
src/Primitives/Icosphere.cpp

@ -54,7 +54,8 @@ Icosphere<0>::Icosphere(): MeshData3D(Mesh::Primitive::Triangles, new std::vecto
{0.0f, 0.525731f, -0.850651f},
{0.0f, 0.525731f, 0.850651f}
}}, {}) {
positions(0)->assign(normals(0)->begin(), normals(0)->end());
positions(0)->reserve(normals(0)->size());
for(auto i: *normals(0)) positions(0)->push_back(Point3D(i));
}
}}

Loading…
Cancel
Save