Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 11a5c8a

Browse files
committedNov 18, 2021
replaced Vector<T, 2>::convert() with conceptual conversion constructor
1 parent a802a93 commit 11a5c8a

10 files changed

+31
-96
lines changed
 

‎src/modm/math/geometry/location_2d.hpp

+5-5
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,9 @@ namespace modm
4343

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

46+
template<typename U>
47+
constexpr Location2D(const Location2D<U> &l) : position(l.position), orientation(l.orientation) {}
48+
4649
inline const Vector<T, 2>&
4750
getPosition() const;
4851

@@ -97,11 +100,6 @@ namespace modm
97100
Vector<T, 2>
98101
translated(const Vector<T, 2>& vector) const;
99102

100-
/// Convert between Location-objects with different base-types
101-
template <typename U>
102-
Location2D<U>
103-
convert() const;
104-
105103
bool
106104
operator == (const Location2D &other) const;
107105

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

114+
// FIXME make friends
115+
public:
116116
Vector<T, 2> position;
117117
float orientation;
118118
};

‎src/modm/math/geometry/location_2d_impl.hpp

-8
Original file line numberDiff line numberDiff line change
@@ -135,14 +135,6 @@ modm::Location2D<T>::translated(const Vector<T, 2>& vector) const
135135
return result;
136136
}
137137

138-
// ----------------------------------------------------------------------------
139-
template<typename T> template<typename U>
140-
modm::Location2D<U>
141-
modm::Location2D<T>::convert() const
142-
{
143-
return Location2D<U>(this->position.template convert<U>(), this->orientation);
144-
}
145-
146138
// ----------------------------------------------------------------------------
147139
template<typename T>
148140
bool

‎src/modm/math/geometry/vector1.hpp

+7
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,13 @@ namespace modm
4343
constexpr Vector(T x) : x(x) {}
4444
constexpr Vector(const Matrix<T, 1, 1> &rhs);
4545

46+
template<typename U>
47+
constexpr Vector(const Vector<U, 1> &v) : x(v.x) {}
48+
// Use round() when constructing from float
49+
// TODO This may be extended to all other constuctors as well
50+
template<std::floating_point U>
51+
constexpr Vector(const Vector<U, 1> &v) : x(round(v.x)) {}
52+
4653
// getters and setters
4754
void set(T x) { this->x = x; }
4855
void setX(T x) { this->x = x; }

‎src/modm/math/geometry/vector2.cpp

-36
This file was deleted.

‎src/modm/math/geometry/vector2.hpp

+5-34
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,11 @@ namespace modm
6565

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

6974
constexpr Vector(Vector<T, 1> vx, Vector<T, 1> vy) : x(vx.x), y(vy.x) {}
7075
constexpr Vector(T x, Vector<T, 1> vy) : x(x), y(vy.x) {}
@@ -162,13 +167,6 @@ namespace modm
162167
WideType
163168
cross(const Vector& other) const;
164169

165-
/**
166-
* \brief Convert between Point-objects with different base-types
167-
*/
168-
template<typename U>
169-
Vector<U, 2>
170-
convert() const;
171-
172170
/**
173171
* \brief Returns a perpendicular copy of the vector
174172
*
@@ -297,33 +295,6 @@ namespace modm
297295
template<typename U>
298296
Vector<U, 2>
299297
operator / (float scale, const Vector<U, 2> &vector);
300-
301-
// ------------------------------------------------------------------------
302-
// Declaration of specialized methods
303-
// ------------------------------------------------------------------------
304-
305-
template<> template<>
306-
Vector<double, 2>
307-
Vector<float, 2>::convert() const;
308-
309-
template<> template<>
310-
Vector<float, 2>
311-
Vector<double, 2>::convert() const;
312-
313-
// round for everything that's not float => double or double => float
314-
template<> template<typename U>
315-
Vector<U, 2>
316-
Vector<float, 2>::convert() const
317-
{
318-
return Vector<U, 2>(round(this->x), round(this->y));
319-
}
320-
321-
template<> template<typename U>
322-
Vector<U, 2>
323-
Vector<double, 2>::convert() const
324-
{
325-
return Vector<U, 2>(round(this->x), round(this->y));
326-
}
327298
}
328299

329300
#include "vector2_impl.hpp"

‎src/modm/math/geometry/vector2_impl.hpp

-8
Original file line numberDiff line numberDiff line change
@@ -140,14 +140,6 @@ modm::Vector<T, 2>::cross(const modm::Vector<T, 2>& other) const
140140
static_cast<WideType>(y) * static_cast<WideType>(other.x));
141141
}
142142

143-
// ----------------------------------------------------------------------------
144-
template<typename T> template<typename U>
145-
modm::Vector<U, 2>
146-
modm::Vector<T, 2>::convert() const
147-
{
148-
return Vector<U, 2>(static_cast<U>(x), static_cast<U>(y));
149-
}
150-
151143
// ----------------------------------------------------------------------------
152144
template<typename T>
153145
modm::Vector<T, 2>

‎src/modm/math/geometry/vector3.hpp

+4
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,10 @@ namespace modm
4848

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

5256
constexpr Vector(Vector<T, 1> vx, T y, T z) : x(vx.x), y(y), z(z) {}
5357
constexpr Vector(T x, Vector<T, 1> vy, T z) : x(x), y(vy.x), z(z) {}

‎src/modm/math/geometry/vector4.hpp

+7
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,13 @@ namespace modm
4545
constexpr explicit Vector(T xyzw) : x(xyzw), y(xyzw), z(xyzw), w(xyzw) {}
4646
constexpr Vector(T x, T y, T z, T w) : x(x), y(y), z(z), w(w) {}
4747

48+
template<typename U>
49+
constexpr Vector(const Vector<U, 4> &v) : x(v.x), y(v.y), z(v.z), w(v.w) {}
50+
// Use round() when constructing from float
51+
// TODO This may be extended to all other constuctors as well
52+
template<std::floating_point U>
53+
constexpr Vector(const Vector<U, 4> &v) : x(round(v.x)), y(round(v.y)), z(round(v.z)), w(round(v.w)) {}
54+
4855
constexpr Vector(Vector<T, 1> vx, Vector<T, 1> vy, Vector<T, 1> vz, Vector<T, 1> vw)
4956
: x(vx.x), y(vy.x), z(vz.x), w(vw.x) {}
5057
constexpr Vector(Vector<T, 1> vx, Vector<T, 1> vy, Vector<T, 1> vz, T w)

‎test/modm/math/geometry/location_2d_test.cpp

+2-4
Original file line numberDiff line numberDiff line change
@@ -110,11 +110,9 @@ Location2DTest::testMove()
110110
void
111111
Location2DTest::testConvert()
112112
{
113-
modm::Location2D<float> a(
114-
modm::Vector<float, 2>(-10.65, 20.31),
115-
M_PI);
113+
modm::Location2D<float> a({-10.65, 20.31}, M_PI);
116114

117-
modm::Location2D<int16_t> b = a.convert<int16_t>();
115+
modm::Location2D<int16_t> b(a);
118116

119117
TEST_ASSERT_EQUALS(b.getX(), -11);
120118
TEST_ASSERT_EQUALS(b.getY(), 20);

‎test/modm/math/geometry/vector2_test.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -322,7 +322,7 @@ Vector2Test::testConversion()
322322
TEST_ASSERT_EQUALS(a.getX(), 12.763f);
323323
TEST_ASSERT_EQUALS(a.getY(), -13.3123f);
324324

325-
modm::Vector2i b = a.convert<int16_t>();
325+
modm::Vector2i b(a);
326326

327327
TEST_ASSERT_EQUALS(b.getX(), 13);
328328
TEST_ASSERT_EQUALS(b.getY(), -13);

0 commit comments

Comments
 (0)
Please sign in to comment.