Skip to content

Commit 05212e2

Browse files
committed
constexpr constructors, Arguemnts changed uint8_t -> std::size_t
1 parent d46c09d commit 05212e2

16 files changed

+462
-960
lines changed

src/modm/math/geometry/quaternion.hpp

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,10 @@
2020
namespace modm
2121
{
2222
// forward declaration
23-
template<class T, uint8_t N>
23+
template<class T, std::size_t N>
2424
class Vector;
2525

26-
template<class T, uint8_t ROWS, uint8_t COLUMNS>
26+
template<class T, std::size_t ROWS, std::size_t COLUMNS>
2727
class Matrix;
2828

2929
/**
@@ -111,5 +111,3 @@ namespace modm
111111
}
112112

113113
#include "quaternion_impl.hpp"
114-
115-
#endif // MODM_QUATERNION_HPP

src/modm/math/geometry/vector.hpp

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,8 @@
2424
namespace modm
2525
{
2626
// forward declaration
27-
template<typename T, uint8_t W, uint8_t H> class Matrix;
27+
template<typename T, std::size_t, std::size_t>
28+
class Matrix;
2829

2930
/**
3031
* \brief Class for handling common point operations
@@ -54,11 +55,11 @@ namespace modm
5455
* \ingroup modm_math_geometry
5556
* \author Niklas Hauser
5657
*/
57-
template<typename T, uint8_t N>
58+
template<typename T, std::size_t N>
5859
class Vector
5960
{
6061
public:
61-
Vector();
62+
Vector() = default;
6263
Vector(const T *ptData);
6364

6465
Vector(const Matrix<T, N, 1> &rhs);
@@ -71,8 +72,8 @@ namespace modm
7172
bool operator > (const Vector &rhs) const;
7273
bool operator >= (const Vector &rhs) const;
7374

74-
const T& operator [] (uint8_t index) const;
75-
T& operator [] (uint8_t index);
75+
const T& operator [] (std::size_t index) const;
76+
T& operator [] (std::size_t index);
7677

7778
T* ptr();
7879
const T* ptr() const;
@@ -104,21 +105,21 @@ namespace modm
104105
asTransposedMatrix() const;
105106

106107
public:
107-
static inline uint8_t
108+
static inline std::size_t
108109
getSize();
109110

110-
T coords[N];
111+
T coords[N] = {0};
111112
};
112113

113-
template< typename T, uint8_t N >
114+
template< typename T, std::size_t N >
114115
struct detail::MakeSigned< Vector<T, N> >
115116
{ using type = Vector< SignedType<T>, N >; };
116117

117-
template< typename T, uint8_t N >
118+
template< typename T, std::size_t N >
118119
struct detail::MakeUnsigned< Vector<T, N> >
119120
{ using type = Vector< UnsignedType<T>, N >; };
120121

121-
template< typename T, uint8_t N >
122+
template< typename T, std::size_t N >
122123
struct detail::WideType< Vector<T, N> >
123124
{ using type = Vector< WideType<T>, N >; };
124125
}

src/modm/math/geometry/vector1.hpp

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -36,9 +36,9 @@ namespace modm
3636
class Vector<T, 1>
3737
{
3838
public:
39-
Vector();
40-
Vector(T inX);
41-
Vector(const Matrix<T, 1, 1> &rhs);
39+
constexpr Vector() = default;
40+
constexpr Vector(T inX);
41+
constexpr Vector(const Matrix<T, 1, 1> &rhs);
4242

4343
inline void
4444
set(const T& x);
@@ -58,8 +58,8 @@ namespace modm
5858
bool operator > (const Vector &rhs) const;
5959
bool operator >= (const Vector &rhs) const;
6060

61-
const T& operator [] (uint8_t index) const;
62-
T& operator [] (uint8_t index);
61+
const T& operator [] (std::size_t index) const;
62+
T& operator [] (std::size_t index);
6363
T* ptr();
6464
const T* ptr() const;
6565

@@ -88,7 +88,7 @@ namespace modm
8888
bool hasInf() const;
8989

9090
public:
91-
T x;
91+
T x = 0;
9292

9393
public:
9494
#ifndef __DOXYGEN__

src/modm/math/geometry/vector1_impl.hpp

Lines changed: 8 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -15,40 +15,27 @@
1515
#error "Don't include this file directly, use 'vector1.hpp' instead!"
1616
#endif
1717

18-
// ----------------------------------------------------------------------------
1918
template<typename T>
20-
modm::Vector<T, 1>::Vector() :
21-
x()
22-
{
23-
}
24-
25-
template<typename T>
26-
modm::Vector<T, 1>::Vector(T inX) :
19+
constexpr modm::Vector<T, 1>::Vector(T inX) :
2720
x(inX)
28-
{
29-
}
21+
{}
3022

3123
template<typename T>
32-
modm::Vector<T, 1>::Vector(const modm::Matrix<T, 1, 1> &rhs) :
24+
constexpr modm::Vector<T, 1>::Vector(const modm::Matrix<T, 1, 1> &rhs) :
3325
x(*reinterpret_cast<const T*>(&rhs))
34-
{
35-
}
26+
{}
3627

3728
// ----------------------------------------------------------------------------
3829
template<typename T>
3930
void
4031
modm::Vector<T, 1>::set(const T& value)
41-
{
42-
this->x = value;
43-
}
32+
{ this->x = value; }
4433

4534
// ----------------------------------------------------------------------------
4635
template<typename T>
4736
void
4837
modm::Vector<T, 1>::setX(const T& value)
49-
{
50-
this->x = value;
51-
}
38+
{ this->x = value; }
5239

5340
// ----------------------------------------------------------------------------
5441
template<typename T>
@@ -118,14 +105,14 @@ modm::Vector<T, 1>::operator >= (const modm::Vector<T, 1> &rhs) const
118105
// ----------------------------------------------------------------------------
119106
template<typename T>
120107
const T&
121-
modm::Vector<T, 1>::operator [] (uint8_t index) const
108+
modm::Vector<T, 1>::operator [] (std::size_t index) const
122109
{
123110
return reinterpret_cast<const T*>(this)[index];
124111
}
125112

126113
template<typename T>
127114
T&
128-
modm::Vector<T, 1>::operator [] (uint8_t index)
115+
modm::Vector<T, 1>::operator [] (std::size_t index)
129116
{
130117
return reinterpret_cast<T*>(this)[index];
131118
}

src/modm/math/geometry/vector2.hpp

Lines changed: 13 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -62,20 +62,13 @@ namespace modm
6262
typedef typename GeometricTraits<T>::FloatType FloatType;
6363

6464
public:
65-
/**
66-
* \brief Default-Constructor
67-
*
68-
* Creates a Vector with coordinates (0, 0).
69-
*/
70-
Vector();
71-
72-
Vector(const T& inX, const T& inY);
73-
74-
Vector(const Vector<T, 1> &inX, const Vector<T, 1> &inY);
75-
Vector(const T &inX, const Vector<T, 1> &inY);
76-
Vector(const Vector<T, 1> &inX, const T &inY);
77-
explicit Vector(T inVal);
78-
Vector(const Matrix<T, 2, 1> &rhs);
65+
constexpr Vector() = default;
66+
constexpr Vector(const T& inX, const T& inY);
67+
constexpr Vector(const Vector<T, 1> &inX, const Vector<T, 1> &inY);
68+
constexpr Vector(const T &inX, const Vector<T, 1> &inY);
69+
constexpr Vector(const Vector<T, 1> &inX, const T &inY);
70+
constexpr explicit Vector(T inVal);
71+
constexpr Vector(const Matrix<T, 2, 1> &rhs);
7972

8073
inline void
8174
setX(const T& value);
@@ -239,15 +232,15 @@ namespace modm
239232
bool operator > (const Vector &rhs) const;
240233
bool operator >= (const Vector &rhs) const;
241234

242-
const T& operator [] (uint8_t index) const;
243-
T& operator [] (uint8_t index);
235+
const T& operator [] (std::size_t index) const;
236+
T& operator [] (std::size_t index);
244237

245238
T* ptr();
246239
const T* ptr() const;
247240

248241
Vector operator - () const;
249-
Vector operator - (const Vector &rhs) const;
250-
Vector operator + (const Vector &rhs) const;
242+
constexpr Vector operator - (const Vector &rhs) const;
243+
constexpr Vector operator + (const Vector &rhs) const;
251244
T operator * (const Vector &rhs) const;
252245
T operator ^ (const Vector &rhs) const;
253246
Vector operator * (float rhs) const;
@@ -295,8 +288,8 @@ namespace modm
295288
#endif
296289

297290
public:
298-
T x;
299-
T y;
291+
T x = 0;
292+
T y = 0;
300293

301294
protected:
302295
template<typename U>

src/modm/math/geometry/vector2_impl.hpp

Lines changed: 20 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -18,61 +18,35 @@
1818

1919
// ----------------------------------------------------------------------------
2020
template<typename T>
21-
modm::Vector<T, 2>::Vector() :
22-
x(),
23-
y()
24-
{
25-
}
21+
constexpr modm::Vector<T, 2>::Vector(const T& inX, const T& inY)
22+
: x(inX), y(inY) {}
2623

2724
// ----------------------------------------------------------------------------
2825
template<typename T>
29-
modm::Vector<T, 2>::Vector(const T& inX, const T& inY) :
30-
x(inX),
31-
y(inY)
32-
{
33-
}
26+
constexpr modm::Vector<T, 2>::Vector(const modm::Vector<T, 1> &inX, const modm::Vector<T, 1> &inY)
27+
: x(inX.x), y(inY.x) {}
3428

3529
// ----------------------------------------------------------------------------
3630
template<typename T>
37-
modm::Vector<T, 2>::Vector(
38-
const modm::Vector<T, 1> &inX,
39-
const modm::Vector<T, 1> &inY) :
40-
x(inX.x),
41-
y(inY.x)
42-
{
43-
}
31+
constexpr modm::Vector<T, 2>::Vector(const T &inX, const modm::Vector<T, 1> &inY)
32+
: x(inX), y(inY.x) {}
4433

4534
// ----------------------------------------------------------------------------
4635
template<typename T>
47-
modm::Vector<T, 2>::Vector(const T &inX, const modm::Vector<T, 1> &inY) :
48-
x(inX),
49-
y(inY.x)
50-
{
51-
}
36+
constexpr modm::Vector<T, 2>::Vector(const modm::Vector<T, 1> &inX, const T &inY)
37+
: x(inX.x), y(inY) {}
5238

5339
// ----------------------------------------------------------------------------
5440
template<typename T>
55-
modm::Vector<T, 2>::Vector(const modm::Vector<T, 1> &inX, const T &inY) :
56-
x(inX.x),
57-
y(inY)
58-
{
59-
}
41+
constexpr modm::Vector<T, 2>::Vector(T inVal) :
42+
x(inVal), y(inVal) {}
6043

6144
// ----------------------------------------------------------------------------
6245
template<typename T>
63-
modm::Vector<T, 2>::Vector(T inVal) :
64-
x(inVal),
65-
y(inVal)
66-
{
67-
}
68-
69-
// ----------------------------------------------------------------------------
70-
template<typename T>
71-
modm::Vector<T, 2>::Vector(const modm::Matrix<T, 2, 1> &rhs) :
46+
constexpr modm::Vector<T, 2>::Vector(const modm::Matrix<T, 2, 1> &rhs) :
7247
x(reinterpret_cast<const T*>(&rhs)[0]),
7348
y(reinterpret_cast<const T*>(&rhs)[1])
74-
{
75-
}
49+
{}
7650

7751
// ----------------------------------------------------------------------------
7852
template<typename T>
@@ -288,26 +262,16 @@ modm::Vector<T, 2>::ccw(const Vector& a, const Vector& b,
288262
WideType d1 = dx1 * dy2;
289263
WideType d2 = dy1 * dx2;
290264

291-
if (d1 > d2) {
265+
if (d1 > d2)
292266
return 1;
293-
}
294-
else if (d1 < d2) {
267+
else if (d1 < d2)
295268
return -1;
296-
}
297269
else
298270
{
299-
if ((dx1 * dx2 < 0) || (dy1 * dy2 < 0)) {
271+
if ((dx1 * dx2 < 0) || (dy1 * dy2 < 0))
300272
return -1;
301-
}
302273
else
303-
{
304-
if ((dx1 * dx1 + dy1 * dy1) >= (dx2 * dx2 + dy2 * dy2)) {
305-
return 0;
306-
}
307-
else {
308-
return 1;
309-
}
310-
}
274+
return ((dx1 * dx1 + dy1 * dy1) >= (dx2 * dx2 + dy2 * dy2)) ? 0 : 1;
311275
}
312276
}
313277

@@ -367,15 +331,15 @@ modm::Vector<T, 2>::operator >= (const modm::Vector<T, 2> &rhs) const
367331
// ----------------------------------------------------------------------------
368332
template<typename T>
369333
const T&
370-
modm::Vector<T, 2>::operator [] (uint8_t index) const
334+
modm::Vector<T, 2>::operator [] (std::size_t index) const
371335
{
372336
return reinterpret_cast<const T*>(this)[index];
373337
}
374338

375339
// ----------------------------------------------------------------------------
376340
template<typename T>
377341
T&
378-
modm::Vector<T, 2>::operator [] (uint8_t index)
342+
modm::Vector<T, 2>::operator [] (std::size_t index)
379343
{
380344
return reinterpret_cast<T*>(this)[index];
381345
}
@@ -406,15 +370,15 @@ modm::Vector<T, 2>::operator - () const
406370

407371
// ----------------------------------------------------------------------------
408372
template<typename T>
409-
modm::Vector<T, 2>
373+
constexpr modm::Vector<T, 2>
410374
modm::Vector<T, 2>::operator - (const modm::Vector<T, 2> &rhs) const
411375
{
412376
return modm::Vector<T, 2>(x - rhs.x, y - rhs.y);
413377
}
414378

415379
// ----------------------------------------------------------------------------
416380
template<typename T>
417-
modm::Vector<T, 2>
381+
constexpr modm::Vector<T, 2>
418382
modm::Vector<T, 2>::operator + (const modm::Vector<T, 2> &rhs) const
419383
{
420384
return modm::Vector<T, 2>(x + rhs.x, y + rhs.y);

0 commit comments

Comments
 (0)