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 11a5c8a
Show file tree
Hide file tree
Showing 10 changed files with 31 additions and 96 deletions.
10 changes: 5 additions & 5 deletions src/modm/math/geometry/location_2d.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,9 @@ namespace modm

Location2D(const T& x, const T& y, const float& orientation);

template<typename U>
constexpr Location2D(const Location2D<U> &l) : position(l.position), orientation(l.orientation) {}

inline const Vector<T, 2>&
getPosition() const;

Expand Down Expand Up @@ -97,11 +100,6 @@ namespace modm
Vector<T, 2>
translated(const Vector<T, 2>& vector) const;

/// Convert between Location-objects with different base-types
template <typename U>
Location2D<U>
convert() const;

bool
operator == (const Location2D &other) const;

Expand All @@ -113,6 +111,8 @@ namespace modm
friend IOStream&
operator <<( IOStream&, const Location2D<U>&);

// FIXME make friends
public:
Vector<T, 2> position;
float orientation;
};
Expand Down
8 changes: 0 additions & 8 deletions src/modm/math/geometry/location_2d_impl.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -135,14 +135,6 @@ modm::Location2D<T>::translated(const Vector<T, 2>& vector) const
return result;
}

// ----------------------------------------------------------------------------
template<typename T> template<typename U>
modm::Location2D<U>
modm::Location2D<T>::convert() const
{
return Location2D<U>(this->position.template convert<U>(), this->orientation);
}

// ----------------------------------------------------------------------------
template<typename T>
bool
Expand Down
7 changes: 7 additions & 0 deletions src/modm/math/geometry/vector1.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,13 @@ namespace modm
constexpr Vector(T x) : x(x) {}
constexpr Vector(const Matrix<T, 1, 1> &rhs);

template<typename U>
constexpr Vector(const Vector<U, 1> &v) : x(v.x) {}
// 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.

39 changes: 5 additions & 34 deletions src/modm/math/geometry/vector2.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,11 @@ 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) {}
Expand Down Expand Up @@ -162,13 +167,6 @@ namespace modm
WideType
cross(const Vector& other) const;

/**
* \brief Convert between Point-objects with different base-types
*/
template<typename U>
Vector<U, 2>
convert() const;

/**
* \brief Returns a perpendicular copy of the vector
*
Expand Down Expand Up @@ -297,33 +295,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"
8 changes: 0 additions & 8 deletions src/modm/math/geometry/vector2_impl.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -140,14 +140,6 @@ modm::Vector<T, 2>::cross(const modm::Vector<T, 2>& other) const
static_cast<WideType>(y) * static_cast<WideType>(other.x));
}

// ----------------------------------------------------------------------------
template<typename T> template<typename U>
modm::Vector<U, 2>
modm::Vector<T, 2>::convert() const
{
return Vector<U, 2>(static_cast<U>(x), static_cast<U>(y));
}

// ----------------------------------------------------------------------------
template<typename T>
modm::Vector<T, 2>
Expand Down
4 changes: 4 additions & 0 deletions src/modm/math/geometry/vector3.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,10 @@ namespace modm

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
7 changes: 7 additions & 0 deletions src/modm/math/geometry/vector4.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,13 @@ 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) {}

template<typename U>
constexpr Vector(const Vector<U, 4> &v) : x(v.x), y(v.y), z(v.z), w(v.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
6 changes: 2 additions & 4 deletions test/modm/math/geometry/location_2d_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -110,11 +110,9 @@ Location2DTest::testMove()
void
Location2DTest::testConvert()
{
modm::Location2D<float> a(
modm::Vector<float, 2>(-10.65, 20.31),
M_PI);
modm::Location2D<float> a({-10.65, 20.31}, M_PI);

modm::Location2D<int16_t> b = a.convert<int16_t>();
modm::Location2D<int16_t> b(a);

TEST_ASSERT_EQUALS(b.getX(), -11);
TEST_ASSERT_EQUALS(b.getY(), 20);
Expand Down
2 changes: 1 addition & 1 deletion test/modm/math/geometry/vector2_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -322,7 +322,7 @@ Vector2Test::testConversion()
TEST_ASSERT_EQUALS(a.getX(), 12.763f);
TEST_ASSERT_EQUALS(a.getY(), -13.3123f);

modm::Vector2i b = a.convert<int16_t>();
modm::Vector2i b(a);

TEST_ASSERT_EQUALS(b.getX(), 13);
TEST_ASSERT_EQUALS(b.getY(), -13);
Expand Down

0 comments on commit 11a5c8a

Please sign in to comment.