Skip to content

Commit

Permalink
replaced Vector<T, 2>::convert() with conceptual conversion constructor
Browse files Browse the repository at this point in the history
  • Loading branch information
TomSaw committed Nov 18, 2021
1 parent a802a93 commit 5760b8b
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 65 deletions.
5 changes: 5 additions & 0 deletions src/modm/math/geometry/vector1.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,11 @@ namespace modm
constexpr Vector(T x) : x(x) {}
constexpr Vector(const Matrix<T, 1, 1> &rhs);

// Use round() when constructing from float
// TODO This may be extended to all other constuctors as well
template<std::floating_point U>
constexpr Vector(const Vector<U, 1> &v) : x(round(v.x)) {}

// getters and setters
void set(T x) { this->x = x; }
void setX(T x) { this->x = x; }
Expand Down
36 changes: 0 additions & 36 deletions src/modm/math/geometry/vector2.cpp

This file was deleted.

33 changes: 6 additions & 27 deletions src/modm/math/geometry/vector2.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,12 @@ namespace modm
template<typename U>
constexpr Vector(const Vector<U, 2> &v) : x(v.x), y(v.y) {}

// Use round() when constructing from float
// TODO This may be extended to all other constuctors as well
// DEPRICATED Replaces previously defined, specialized convert() methods below
template<std::floating_point U>
constexpr Vector(const Vector<U, 2> &v) : x(round(v.x)), y(round(v.y)) {}

constexpr Vector(Vector<T, 1> vx, Vector<T, 1> vy) : x(vx.x), y(vy.x) {}
constexpr Vector(T x, Vector<T, 1> vy) : x(x), y(vy.x) {}
constexpr Vector(Vector<T, 1> vx, T y) : x(vx.x), y(y) {}
Expand Down Expand Up @@ -297,33 +303,6 @@ namespace modm
template<typename U>
Vector<U, 2>
operator / (float scale, const Vector<U, 2> &vector);

// ------------------------------------------------------------------------
// Declaration of specialized methods
// ------------------------------------------------------------------------

template<> template<>
Vector<double, 2>
Vector<float, 2>::convert() const;

template<> template<>
Vector<float, 2>
Vector<double, 2>::convert() const;

// round for everything that's not float => double or double => float
template<> template<typename U>
Vector<U, 2>
Vector<float, 2>::convert() const
{
return Vector<U, 2>(round(this->x), round(this->y));
}

template<> template<typename U>
Vector<U, 2>
Vector<double, 2>::convert() const
{
return Vector<U, 2>(round(this->x), round(this->y));
}
}

#include "vector2_impl.hpp"
6 changes: 4 additions & 2 deletions src/modm/math/geometry/vector3.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,10 @@ namespace modm
constexpr explicit Vector(T xyz) : x(xyz), y(xyz), z(xyz) {}
constexpr Vector(T x, T y, T z) : x(x), y(y), z(z) {}

template<typename U>
constexpr Vector(const Vector<U, 3> &v) : x(v.x), y(v.y), z(v.z) {}
// Use round() when constructing from float
// TODO This may be extended to all other constuctors as well
template<std::floating_point U>
constexpr Vector(const Vector<U, 3> &v) : x(round(v.x)), y(round(v.y)), z(round(v.z)) {}

constexpr Vector(Vector<T, 1> vx, T y, T z) : x(vx.x), y(y), z(z) {}
constexpr Vector(T x, Vector<T, 1> vy, T z) : x(x), y(vy.x), z(z) {}
Expand Down
5 changes: 5 additions & 0 deletions src/modm/math/geometry/vector4.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,11 @@ namespace modm
constexpr explicit Vector(T xyzw) : x(xyzw), y(xyzw), z(xyzw), w(xyzw) {}
constexpr Vector(T x, T y, T z, T w) : x(x), y(y), z(z), w(w) {}

// Use round() when constructing from float
// TODO This may be extended to all other constuctors as well
template<std::floating_point U>
constexpr Vector(const Vector<U, 4> &v) : x(round(v.x)), y(round(v.y)), z(round(v.z)), w(round(v.w)) {}

constexpr Vector(Vector<T, 1> vx, Vector<T, 1> vy, Vector<T, 1> vz, Vector<T, 1> vw)
: x(vx.x), y(vy.x), z(vz.x), w(vw.x) {}
constexpr Vector(Vector<T, 1> vx, Vector<T, 1> vy, Vector<T, 1> vz, T w)
Expand Down

0 comments on commit 5760b8b

Please sign in to comment.