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 *
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