Skip to content

Commit c9c1de5

Browse files
committed
household/refactor modm::Vector and co
1 parent 57e7fe9 commit c9c1de5

26 files changed

+1864
-4431
lines changed
Lines changed: 24 additions & 100 deletions
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/location_2d.hpp

Lines changed: 71 additions & 75 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
* Copyright (c) 2009-2011, Fabian Greif
44
* Copyright (c) 2012, Niklas Hauser
55
* Copyright (c) 2012, Sascha Schade
6+
* Copyright (c) 2013, Kevin Läufer
7+
* Copyright (c) 2022, Thomas Sommer
68
*
79
* This file is part of the modm project.
810
*
@@ -12,10 +14,7 @@
1214
*/
1315
// ----------------------------------------------------------------------------
1416

15-
#ifndef MODM_LOCATION_2D_HPP
16-
#define MODM_LOCATION_2D_HPP
17-
18-
#include <cmath>
17+
#pragma once
1918

2019
#include <modm/io/iostream.hpp>
2120

@@ -37,39 +36,60 @@ namespace modm
3736
class Location2D
3837
{
3938
public:
40-
Location2D();
41-
42-
Location2D(const Vector<T, 2>& position, const float& orientation);
43-
44-
Location2D(const T& x, const T& y, const float& orientation);
45-
46-
inline const Vector<T, 2>&
47-
getPosition() const;
48-
49-
inline const T&
50-
getX() const;
51-
52-
inline const T&
53-
getY() const;
54-
55-
void
56-
setPosition(const Vector<T, 2>& point);
57-
58-
void
59-
setPosition(const T& x, const T& y);
39+
Vector<T, 2> position;
40+
float orientation = 0;
41+
42+
constexpr Location2D() = default;
43+
constexpr Location2D(const Vector<T, 2>& position, const float& orientation)
44+
: position(position), orientation(orientation) {}
45+
46+
[[deprecated("Use 'setPosition({x, y}, orientation)' instead!")]]
47+
constexpr Location2D(const T& x, const T& y, const float& orientation)
48+
: position(x, y), orientation(orientation) {}
49+
50+
template<typename U>
51+
constexpr Location2D(const Location2D<U> &l) : position(l.position), orientation(l.orientation) {}
52+
53+
// getters and setters
54+
void setPosition(const Vector<T, 2>& position) { this->position = position; }
55+
56+
[[deprecated("Use 'setPosition({x, y}' instead!")]]
57+
void setPosition(T x, T y) { this->position.x = x; this->position.y = y; }
58+
void setOrientation(const float orientation) { this->orientation = orientation; }
59+
60+
Vector<T, 2> getPosition() const { return position; }
61+
inline float getOrientation() const { return orientation; }
62+
T getX() const { return position.x; }
63+
T getY() const { return position.y; }
64+
65+
bool operator== (const Location2D &other) const {
66+
return (
67+
position == other.position and
68+
std::abs(orientation - other.orientation) < __FLT_EPSILON__
69+
);
70+
}
71+
bool operator!= (const Location2D &other) const {
72+
return (
73+
position != other.position or
74+
std::abs(orientation - other.orientation) > __FLT_EPSILON__
75+
);
76+
}
6077

61-
inline float
62-
getOrientation() const;
78+
/// Add a position increment
79+
void move(const Location2D& diff) {
80+
Vector<T, 2> movement = diff.position;
81+
movement.rotate(orientation);
6382

64-
void
65-
setOrientation(const float& phi);
83+
position.translate(movement);
84+
orientation = Angle::normalize(orientation + diff.orientation);
85+
}
6686

67-
/// Add a position increment
68-
void
69-
move(const Location2D& diff);
87+
void move(const Vector<T, 2>& diff) {
88+
Vector<T, 2> movement(diff);
89+
movement.rotate(orientation);
7090

71-
void
72-
move(const Vector<T, 2>& diff);
91+
position.translate(movement);
92+
}
7393

7494
/**
7595
* \brief Add a increment only in x-direction
@@ -85,62 +105,38 @@ namespace modm
85105
* movement over time.
86106
* Because the y-component will always be zero, we created this
87107
* method, which avoids unnecessary computations for the y-component
88-
* and is therefore faster the the universal move-method.
108+
* and is therefore faster than the universal move-method.
89109
*
90110
* \param x movement in x-direction
91111
* \param phi rotation
92112
*/
93113
void
94-
move(T x, float phi);
114+
move(T x, float phi) {
115+
Vector<T, 2> vector(Vector<float, 2>(x * std::cos(orientation), x * std::sin(orientation)));
116+
position.translate(vector);
95117

96-
/// TODO
97-
Vector<T, 2>
98-
translated(const Vector<T, 2>& vector) const;
99-
100-
/// Convert between Location-objects with different base-types
101-
template <typename U>
102-
Location2D<U>
103-
convert() const;
118+
orientation = Angle::normalize(orientation + phi);
119+
}
104120

105-
bool
106-
operator == (const Location2D &other) const;
121+
// TODO
122+
Vector<T, 2> translated(const Vector<T, 2>& vector) const {
123+
Vector<T, 2> result(vector);
124+
result.rotate(orientation);
125+
result.translate(position);
107126

108-
bool
109-
operator != (const Location2D &other) const;
127+
return result;
128+
}
110129

111130
private:
112131
template <typename U>
113132
friend IOStream&
114133
operator <<( IOStream&, const Location2D<U>&);
115-
116-
Vector<T, 2> position;
117-
float orientation;
118134
};
119135

120-
// ------------------------------------------------------------------------
121-
// Global functions
122-
// ------------------------------------------------------------------------
123-
/**
124-
* \brief Stream operator to \b modm::Location<T>
125-
*
126-
* \ingroup modm_math_geometry
127-
*/
128136
template<typename T>
129137
IOStream&
130-
operator << (IOStream& os, const Location2D<T>& l);
131-
132-
// ------------------------------------------------------------------------
133-
// Declaration of specialized methods
134-
// ------------------------------------------------------------------------
135-
/*template<>
136-
bool
137-
Location2D<float>::operator == (const Location2D &other) const;
138-
139-
template<>
140-
bool
141-
Location2D<double>::operator == (const Location2D &other) const;*/
142-
}
143-
144-
#include "location_2d_impl.hpp"
145-
146-
#endif // MODM_LOCATION_2D_HPP
138+
operator<< (IOStream& os, const Location2D<T>& location) {
139+
os << location.position << ", phi=" << location.orientation;
140+
return os;
141+
}
142+
}

0 commit comments

Comments
 (0)