3
3
* Copyright (c) 2009-2011, Fabian Greif
4
4
* Copyright (c) 2012, Niklas Hauser
5
5
* Copyright (c) 2012, Sascha Schade
6
+ * Copyright (c) 2013, Kevin Läufer
7
+ * Copyright (c) 2022, Thomas Sommer
6
8
*
7
9
* This file is part of the modm project.
8
10
*
12
14
*/
13
15
// ----------------------------------------------------------------------------
14
16
15
- #ifndef MODM_LOCATION_2D_HPP
16
- #define MODM_LOCATION_2D_HPP
17
-
18
- #include < cmath>
17
+ #pragma once
19
18
20
19
#include < modm/io/iostream.hpp>
21
20
@@ -37,39 +36,60 @@ namespace modm
37
36
class Location2D
38
37
{
39
38
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
+ }
60
77
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);
63
82
64
- void
65
- setOrientation (const float & phi);
83
+ position.translate (movement);
84
+ orientation = Angle::normalize (orientation + diff.orientation );
85
+ }
66
86
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 );
70
90
71
- void
72
- move ( const Vector<T, 2 >& diff);
91
+ position. translate (movement);
92
+ }
73
93
74
94
/* *
75
95
* \brief Add a increment only in x-direction
@@ -85,62 +105,38 @@ namespace modm
85
105
* movement over time.
86
106
* Because the y-component will always be zero, we created this
87
107
* 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.
89
109
*
90
110
* \param x movement in x-direction
91
111
* \param phi rotation
92
112
*/
93
113
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);
95
117
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
+ }
104
120
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);
107
126
108
- bool
109
- operator != ( const Location2D &other) const ;
127
+ return result;
128
+ }
110
129
111
130
private:
112
131
template <typename U>
113
132
friend IOStream&
114
133
operator <<( IOStream&, const Location2D<U>&);
115
-
116
- Vector<T, 2 > position;
117
- float orientation;
118
134
};
119
135
120
- // ------------------------------------------------------------------------
121
- // Global functions
122
- // ------------------------------------------------------------------------
123
- /* *
124
- * \brief Stream operator to \b modm::Location<T>
125
- *
126
- * \ingroup modm_math_geometry
127
- */
128
136
template <typename T>
129
137
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