Skip to content

Commit 95b6a25

Browse files
committed
household/refactor modm::Vector and co
1 parent 76794b2 commit 95b6a25

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

43 files changed

+1740
-5449
lines changed
+24-100
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
/*
22
* Copyright (c) 2009-2011, Fabian Greif
33
* Copyright (c) 2012, Niklas Hauser
4+
* Copyright (c) 2022, Thomas Sommer
45
*
56
* This file is part of the modm project.
67
*
@@ -10,12 +11,13 @@
1011
*/
1112
// ----------------------------------------------------------------------------
1213

13-
#ifndef MODM_GEOMETRIC_TRAITS_HPP
14-
#define MODM_GEOMETRIC_TRAITS_HPP
14+
#pragma once
1515

16+
#include <concepts>
1617
#include <cmath>
17-
#include <stdint.h>
18+
#include <limits>
1819
#include <modm/architecture/utils.hpp>
20+
#include <modm/math/utils/arithmetic_traits.hpp>
1921

2022
namespace modm
2123
{
@@ -24,120 +26,42 @@ namespace modm
2426
*
2527
* \ingroup modm_math_geometry
2628
* \author Fabian Greif
29+
* \author Thomas Sommer
2730
*/
2831
template <typename T>
29-
struct GeometricTraits
30-
{
31-
static const bool isValidType = false;
32+
struct GeometricTraits;
3233

33-
/**
34-
* \brief Round if converting from a floating point base to
35-
* a integer base.
36-
*
37-
* For T = \c float and \c double this method is specialized to return
38-
* the result directly without any rounding.
39-
*/
40-
static inline T
41-
round(float value)
42-
{
43-
return ::round(value);
44-
}
45-
};
46-
47-
template <>
48-
struct GeometricTraits<int8_t>
34+
template <std::integral T>
35+
struct GeometricTraits<T>
4936
{
50-
static const bool isValidType = true;
51-
52-
typedef float FloatType;
53-
typedef int16_t WideType;
54-
55-
static inline int8_t
56-
round(float value)
57-
{
58-
return ::round(value);
59-
}
60-
};
61-
62-
// TODO is this useful?
63-
template <>
64-
struct GeometricTraits<uint8_t>
65-
{
66-
static const bool isValidType = true;
67-
68-
typedef float FloatType;
69-
typedef int16_t WideType;
37+
[[deprecated("Use an appropriate C++ concept instead!")]]
38+
static const bool isValidType = false;
7039

71-
static inline uint8_t
72-
round(float value)
73-
{
74-
return ::round(value);
75-
}
40+
using FloatType = float;
41+
using WideType = modm::WideType<T>;
7642
};
7743

78-
template <>
79-
struct GeometricTraits<int16_t>
44+
template <std::floating_point T>
45+
struct GeometricTraits<T>
8046
{
47+
[[deprecated("Use an appropriate C++ concept instead!")]]
8148
static const bool isValidType = true;
8249

83-
typedef float FloatType;
84-
typedef int32_t WideType;
85-
86-
static inline int16_t
87-
round(float value)
88-
{
89-
return ::round(value);
90-
}
50+
using FloatType = T;
51+
using WideType = T;
9152
};
9253

54+
#ifdef __AVR__
9355
template <>
9456
struct GeometricTraits<int32_t>
9557
{
58+
[[deprecated("Use an appropriate C++ concept instead!")]]
9659
static const bool isValidType = true;
9760

98-
typedef float FloatType;
99-
100-
// Usually the range of a int32_t is big enough so that no
61+
using FloatType = float;
10162
// conversion to int64_t is required. This exception is made because
10263
// 64-bit operations are very, very slow on an AVR.
103-
typedef int32_t WideType;
104-
105-
static inline int32_t
106-
round(float value)
107-
{
108-
return ::round(value);
109-
}
110-
};
111-
112-
template <>
113-
struct GeometricTraits<float>
114-
{
115-
static const bool isValidType = true;
116-
117-
typedef float FloatType;
118-
typedef float WideType;
119-
120-
static inline float
121-
round(float value)
122-
{
123-
return value;
124-
}
64+
using WideType = int32_t;
12565
};
126-
127-
template <>
128-
struct GeometricTraits<double>
129-
{
130-
static const bool isValidType = true;
131-
132-
typedef double FloatType;
133-
typedef double WideType;
134-
135-
static inline double
136-
round(double value)
137-
{
138-
return value;
139-
}
140-
};
141-
}
142-
143-
#endif // MODM_GEOMETRIC_TRAITS_HPP
66+
#endif
67+
}

src/modm/math/geometry/line_2d.hpp

+4-5
Original file line numberDiff line numberDiff line change
@@ -38,18 +38,17 @@ namespace modm
3838
typedef typename GeometricTraits<T>::FloatType FloatType;
3939

4040
public:
41-
/**
42-
* \brief Default-Constructor
43-
*/
44-
Line2D();
41+
constexpr Line2D() = default;
4542

4643
/**
4744
* \brief Construct a line
4845
*
4946
* \param point a point on the line
5047
* \param directionVector direction vector, the length doesn't matter
5148
*/
52-
Line2D(const Vector<T, 2>& point, const Vector<T, 2>& directionVector);
49+
constexpr Line2D(const Vector<T, 2>& point, const Vector<T, 2>& directionVector)
50+
: point(point), directionVector(directionVector)
51+
{}
5352

5453

5554
inline void

src/modm/math/geometry/line_2d_impl.hpp

+1-14
Original file line numberDiff line numberDiff line change
@@ -15,19 +15,6 @@
1515
#error "Don't include this file directly, use 'line_2d.hpp' instead!"
1616
#endif
1717

18-
// ----------------------------------------------------------------------------
19-
template <typename T>
20-
modm::Line2D<T>::Line2D() :
21-
point(), directionVector()
22-
{
23-
}
24-
25-
template <typename T>
26-
modm::Line2D<T>::Line2D(const Vector<T, 2>& point, const Vector<T, 2>& direction) :
27-
point(point), directionVector(direction)
28-
{
29-
}
30-
3118
// ----------------------------------------------------------------------------
3219
template <typename T>
3320
inline void
@@ -79,7 +66,7 @@ modm::Line2D<T>::getDistanceTo(const Vector<T, 2>& point) const
7966
FloatType d = c1 / c2;
8067

8168
// calculate the closest point
82-
Vector<T, 2> closestPoint = this->point + d * this->directionVector;
69+
const Vector<T, 2> closestPoint = this->point + this->directionVector * d;
8370

8471
// return the length of the vector from the closest point on the line
8572
// to the given point

0 commit comments

Comments
 (0)