From 2bb0009861558e5546ac87d9527d97043cbb6eb5 Mon Sep 17 00:00:00 2001 From: Pascal Serrarens Date: Thu, 7 Dec 2023 10:43:07 +0100 Subject: [PATCH 1/6] Initial implementation --- include/DiscreteAngle.h | 12 ++++++++++++ include/Range.h | 28 ++++++++++++++++++++++++++++ src/DiscreteAngle.cpp | 24 ++++++++++++++++++++++++ src/Range.cpp | 38 ++++++++++++++++++++++++++++++++++++++ 4 files changed, 102 insertions(+) create mode 100644 include/DiscreteAngle.h create mode 100644 include/Range.h create mode 100644 src/DiscreteAngle.cpp create mode 100644 src/Range.cpp diff --git a/include/DiscreteAngle.h b/include/DiscreteAngle.h new file mode 100644 index 0000000..ee2ca31 --- /dev/null +++ b/include/DiscreteAngle.h @@ -0,0 +1,12 @@ +#ifndef DISCRETEANGLE_H +#define DISCRETEANGLE_H + +#include "Range.h" + +// A fixed point 16-bit signed angle between (-180..180] +class DiscreteAngle16 : Range16 { +public: + DiscreteAngle16(float angle); +}; + +#endif \ No newline at end of file diff --git a/include/Range.h b/include/Range.h new file mode 100644 index 0000000..09f7ffc --- /dev/null +++ b/include/Range.h @@ -0,0 +1,28 @@ +#ifndef RANGE_H +#define RANGE_H + +/// @brief Signed range. May be renamed to SignedRange later +class Range16 { +public: + Range16(); + Range16(float range); // range -1..1 + Range16(short range); // range -32768..32767 + + inline Range16 operator-(Range16 a); + inline Range16 operator+(Range16 a); + + inline bool operator==(Range16 a); + inline bool operator!=(Range16 a); + + inline bool operator<(Range16 a); + inline bool operator>(Range16 a); + inline bool operator<=(Range16 a); + inline bool operator>=(Range16 a); + +protected: + // How do we make sure we have 16 bit range on every platform? + // uint16_t range; // 16-bit range + unsigned short range; // 16-bit range +}; + +#endif \ No newline at end of file diff --git a/src/DiscreteAngle.cpp b/src/DiscreteAngle.cpp new file mode 100644 index 0000000..7ab700f --- /dev/null +++ b/src/DiscreteAngle.cpp @@ -0,0 +1,24 @@ +// This Source Code Form is subject to the terms of the Mozilla Public +// License, v. 2.0.If a copy of the MPL was not distributed with this +// file, You can obtain one at https ://mozilla.org/MPL/2.0/. + +#include "DiscreteAngle.h" + +#include + +DiscreteAngle16::DiscreteAngle16(float angle) : Range16() { + if (!isfinite(angle)) { + range = 0; + return; + } + + // clamp the float range to -1..1 + while (angle <= -180.0F) + angle += 360.0F; + while (angle > 180.0F) + angle -= 360.0F; + + // map (-180..180] to 1..65535 + // This means that range value 0 is not used + this->range = (unsigned short)((angle + 180.0F) * 65535.0F); +} diff --git a/src/Range.cpp b/src/Range.cpp new file mode 100644 index 0000000..340f165 --- /dev/null +++ b/src/Range.cpp @@ -0,0 +1,38 @@ +#include "Range.h" + +Range16::Range16() { this->range = 0; } + +Range16::Range16(float f) { + // clamp the float range to -1..1 + if (f < -1.0F) + f = -1.0F; + else if (f > 1.0F) + f = 1.0F; + + // map -1..1 to 0..65535 + this->range = (unsigned short)((f + 1.0F) * 65535.0F); +} + +Range16::Range16(short s) { + // clamp the range to -32767..32767 + // This is needed because the representation can support +/-32768 (like 2s + // complement) + if (s < -32767) + s = -32767; + else if (s > 32767) + s = 32767; + + this->range = (unsigned short)(s + 32767); +} + +Range16 Range16::operator-(Range16 a) { this->range - a.range; }; + +Range16 Range16::operator+(Range16 a) { this->range + a.range; }; + +bool Range16::operator==(Range16 a) { return (this->range == a.range); } +bool Range16::operator!=(Range16 a) { return (this->range != a.range); } + +bool Range16::operator<(Range16 a) { return (this->range < a.range); } +bool Range16::operator>(Range16 a) { return (this->range > a.range); } +bool Range16::operator<=(Range16 a) { return (this->range <= a.range); } +bool Range16::operator>=(Range16 a) { return (this->range >= a.range); } \ No newline at end of file From bd04285d48f1a45bc33264c0cc80b6c4e58325e6 Mon Sep 17 00:00:00 2001 From: Pascal Serrarens Date: Thu, 7 Dec 2023 10:48:47 +0100 Subject: [PATCH 2/6] Make superclass accessable --- include/DiscreteAngle.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/include/DiscreteAngle.h b/include/DiscreteAngle.h index ee2ca31..d5e0c95 100644 --- a/include/DiscreteAngle.h +++ b/include/DiscreteAngle.h @@ -4,7 +4,7 @@ #include "Range.h" // A fixed point 16-bit signed angle between (-180..180] -class DiscreteAngle16 : Range16 { +class DiscreteAngle16 : public Range16 { public: DiscreteAngle16(float angle); }; From 254bb27f53acc26c41080ac5a6d32f3d5d5f3b19 Mon Sep 17 00:00:00 2001 From: Pascal Serrarens Date: Thu, 7 Dec 2023 10:51:20 +0100 Subject: [PATCH 3/6] Added negation --- include/Range.h | 1 + src/Range.cpp | 5 +++-- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/include/Range.h b/include/Range.h index 09f7ffc..8f8834e 100644 --- a/include/Range.h +++ b/include/Range.h @@ -10,6 +10,7 @@ public: inline Range16 operator-(Range16 a); inline Range16 operator+(Range16 a); + inline Range16 operator-(); inline bool operator==(Range16 a); inline bool operator!=(Range16 a); diff --git a/src/Range.cpp b/src/Range.cpp index 340f165..3e3661e 100644 --- a/src/Range.cpp +++ b/src/Range.cpp @@ -26,10 +26,11 @@ Range16::Range16(short s) { } Range16 Range16::operator-(Range16 a) { this->range - a.range; }; - Range16 Range16::operator+(Range16 a) { this->range + a.range; }; -bool Range16::operator==(Range16 a) { return (this->range == a.range); } +Range16 Range16::operator-() { -this->range; } + +bool Range16::operator==(Range16 a) { this->range == a.range; } bool Range16::operator!=(Range16 a) { return (this->range != a.range); } bool Range16::operator<(Range16 a) { return (this->range < a.range); } From de3a6479cf0985362ca0b28acd62189dd2bd75ba Mon Sep 17 00:00:00 2001 From: Pascal Serrarens Date: Thu, 7 Dec 2023 10:56:49 +0100 Subject: [PATCH 4/6] Updated negation --- include/Range.h | 2 +- src/Range.cpp | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/include/Range.h b/include/Range.h index 8f8834e..f66bcf0 100644 --- a/include/Range.h +++ b/include/Range.h @@ -10,7 +10,7 @@ public: inline Range16 operator-(Range16 a); inline Range16 operator+(Range16 a); - inline Range16 operator-(); + inline Range16 &operator-(); inline bool operator==(Range16 a); inline bool operator!=(Range16 a); diff --git a/src/Range.cpp b/src/Range.cpp index 3e3661e..af23906 100644 --- a/src/Range.cpp +++ b/src/Range.cpp @@ -28,7 +28,7 @@ Range16::Range16(short s) { Range16 Range16::operator-(Range16 a) { this->range - a.range; }; Range16 Range16::operator+(Range16 a) { this->range + a.range; }; -Range16 Range16::operator-() { -this->range; } +Range16 &Range16::operator-() { -this->range; } bool Range16::operator==(Range16 a) { this->range == a.range; } bool Range16::operator!=(Range16 a) { return (this->range != a.range); } From 2b50babcf12a5d30e27cdc1ba0469012dbca3dcc Mon Sep 17 00:00:00 2001 From: Pascal Serrarens Date: Thu, 7 Dec 2023 11:03:06 +0100 Subject: [PATCH 5/6] Add negation for DiscreteAngle --- include/DiscreteAngle.h | 2 ++ src/DiscreteAngle.cpp | 2 ++ 2 files changed, 4 insertions(+) diff --git a/include/DiscreteAngle.h b/include/DiscreteAngle.h index d5e0c95..17c4616 100644 --- a/include/DiscreteAngle.h +++ b/include/DiscreteAngle.h @@ -7,6 +7,8 @@ class DiscreteAngle16 : public Range16 { public: DiscreteAngle16(float angle); + + inline DiscreteAngle16 &operator-(); }; #endif \ No newline at end of file diff --git a/src/DiscreteAngle.cpp b/src/DiscreteAngle.cpp index 7ab700f..9544da1 100644 --- a/src/DiscreteAngle.cpp +++ b/src/DiscreteAngle.cpp @@ -22,3 +22,5 @@ DiscreteAngle16::DiscreteAngle16(float angle) : Range16() { // This means that range value 0 is not used this->range = (unsigned short)((angle + 180.0F) * 65535.0F); } + +DiscreteAngle16 &DiscreteAngle16::operator-() { -this->range; } \ No newline at end of file From 2cbf2594eb7ea5a23962b7e9b465dfaf9993ef3a Mon Sep 17 00:00:00 2001 From: Pascal Serrarens Date: Thu, 7 Dec 2023 11:04:03 +0100 Subject: [PATCH 6/6] Make negation virtual override --- include/DiscreteAngle.h | 2 +- include/Range.h | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/include/DiscreteAngle.h b/include/DiscreteAngle.h index 17c4616..5a2171b 100644 --- a/include/DiscreteAngle.h +++ b/include/DiscreteAngle.h @@ -8,7 +8,7 @@ class DiscreteAngle16 : public Range16 { public: DiscreteAngle16(float angle); - inline DiscreteAngle16 &operator-(); + inline DiscreteAngle16 &operator-() override; }; #endif \ No newline at end of file diff --git a/include/Range.h b/include/Range.h index f66bcf0..e73cde7 100644 --- a/include/Range.h +++ b/include/Range.h @@ -10,7 +10,7 @@ public: inline Range16 operator-(Range16 a); inline Range16 operator+(Range16 a); - inline Range16 &operator-(); + inline virtual Range16 &operator-(); inline bool operator==(Range16 a); inline bool operator!=(Range16 a);