diff --git a/Angle16.h b/Angle16.h new file mode 100644 index 0000000..fd62d1d --- /dev/null +++ b/Angle16.h @@ -0,0 +1,21 @@ +#include "AngleUsing.h" + +#include "Angle.h" +#include + +typedef AngleUsing Angle16; + +template <> Angle16::AngleUsing(float angle) { + if (!isfinite(angle)) { + value = 0; + return; + } + + // map float [-180..180) to integer [-32768..32767] + this->value = (signed short)((angle / 360.0F) * 65536.0F); +} + +template <> float Angle16::ToFloat() const { + float f = ((this->value * 180) / 32768.0F); + return f; +} diff --git a/Angle8.h b/Angle8.h new file mode 100644 index 0000000..22c8745 --- /dev/null +++ b/Angle8.h @@ -0,0 +1,22 @@ +#include "AngleUsing.h" + +#include "Angle.h" +#include + +typedef AngleUsing Angle8; + +template <> Angle8::AngleUsing(float angle) { + if (!isfinite(angle)) { + value = 0; + return; + } + + // map float [-180..180) to integer [-128..127] + float f = angle / 360.0F; + this->value = (signed char)(f * 256.0F); +} + +template <> float Angle8::ToFloat() const { + float f = (this->value * 180) / 128.0F; + return f; +} diff --git a/AngleUsing.h b/AngleUsing.h new file mode 100644 index 0000000..156fd6e --- /dev/null +++ b/AngleUsing.h @@ -0,0 +1,42 @@ +#ifndef DISCRETEANGLE_H +#define DISCRETEANGLE_H + +#include "Angle.h" +#include "Range.h" + +// A fixed angle between (-180..180] + +template class AngleUsing { +public: + AngleUsing(T sourceValue) { this->value = sourceValue; } + AngleUsing(float f); + float ToFloat() const; + inline T GetValue() const { return this->value; } + + AngleUsing operator+(const AngleUsing a) { + AngleUsing r = AngleUsing((float)this->value + a.value); + return r; + } + + inline AngleUsing operator+=(const AngleUsing a) { + return this->value + a.value; + } + + inline AngleUsing operator-(const AngleUsing a) { + return this->value - a.value; + } + + inline AngleUsing operator-() { + this->value = -this->value; + return *this; + } + + inline bool operator==(const AngleUsing a) { + return this->value == a.value; + } + + // protected: + T value; +}; + +#endif diff --git a/CMakeLists.txt b/CMakeLists.txt index 372b7c0..1591739 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -45,6 +45,7 @@ else() "test/Matrix_test.cc" "test/Polar_test.cc" "test/Spherical_test.cc" + "test/DiscreteAngle_test.cc" ) target_link_libraries( VectorAlgebraTest @@ -52,6 +53,13 @@ else() VectorAlgebra ) + if(MSVC) + target_compile_options(VectorAlgebraTest PRIVATE /W4 /WX) + else() + target_compile_options(VectorAlgebraTest PRIVATE -Wall -Wextra -Wpedantic -Werror) + endif() + + include(GoogleTest) gtest_discover_tests(VectorAlgebraTest) endif() diff --git a/DiscreteAngle.cpp b/DiscreteAngle.cpp deleted file mode 100644 index 0a0e4cd..0000000 --- a/DiscreteAngle.cpp +++ /dev/null @@ -1,46 +0,0 @@ -// 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 "Angle.h" -#include - -template <> AngleUsing::AngleUsing(unsigned char angle) { - if (!isfinite(angle)) { - value = 0; - return; - } - - // normalize to (-180..180] - angle = Angle::Normalize(angle); - - // map (-180..180] to (0..255], which is equivaluent to 1..255 - // This means that range value 0 is not used - this->value = (unsigned char)((angle + 180.0F) / 360.0F * 255.0F); -} - -template <> float AngleUsing::ToFloat() { - float f = ((float)this->value / 255.0F) * 360.0F - 180.0F; - return f; -} - -template <> AngleUsing::AngleUsing(unsigned short angle) { - if (!isfinite(angle)) { - value = 0; - return; - } - - // normalize to (-180..180] - angle = Angle::Normalize(angle); - - // map (-180..180] to (0..65535] which is equivalent to 1..65535 - // This means that range value 0 is not used - this->value = (unsigned short)((angle + 180.0F) / 360.0F * 65535.0F); -} - -template <> float AngleUsing::ToFloat() { - float f = ((float)this->value / 65535.0F) * 360.0F - 180.0F; - return f; -} \ No newline at end of file diff --git a/DiscreteAngle.h b/DiscreteAngle.h deleted file mode 100644 index 4297c20..0000000 --- a/DiscreteAngle.h +++ /dev/null @@ -1,24 +0,0 @@ -#ifndef DISCRETEANGLE_H -#define DISCRETEANGLE_H - -#include "Range.h" - -// A fixed angle between (-180..180] - -template class AngleUsing { -public: - AngleUsing(T sourceValue); - float ToFloat(); - inline T GetValue() { return this->value; } - - inline AngleUsing operator-(AngleUsing a) { - return this->value - a.value; - } - -protected: - T value; -}; - -#endif - -// template inline float AngleUsing::ToFloat() { return 0.0f; } diff --git a/Matrix.h b/Matrix.h index 5ef0588..7eb80a9 100644 --- a/Matrix.h +++ b/Matrix.h @@ -33,7 +33,7 @@ public: // Return a null matrix; // We dont set data to nullptr because it is allocated memory // Instead we write all zeros - for (int dataIx = 0; dataIx < resultSize; dataIx++) + for (unsigned int dataIx = 0; dataIx < resultSize; dataIx++) r->data[dataIx] = 0.0f; r->rows = 0; r->cols = 0; @@ -43,7 +43,7 @@ public: r->cols = this->rows; r->rows = this->cols; - for (int rDataIx = 0; rDataIx < matrixSize; rDataIx++) { + for (unsigned int rDataIx = 0; rDataIx < matrixSize; rDataIx++) { unsigned int rowIx = rDataIx / this->rows; unsigned int colIx = rDataIx % this->rows; unsigned int mDataIx = this->cols * colIx + rowIx; diff --git a/test/DiscreteAngle_test.cc b/test/DiscreteAngle_test.cc new file mode 100644 index 0000000..b0d59df --- /dev/null +++ b/test/DiscreteAngle_test.cc @@ -0,0 +1,64 @@ +#if GTEST +#include + +#include +#include + +#include "Angle16.h" +#include "Angle8.h" + +#define FLOAT_INFINITY std::numeric_limits::infinity() + +TEST(Angle8, Construct) { + float angle = 0.0F; + Angle8 a = Angle8(angle); + float f = a.ToFloat(); + EXPECT_FLOAT_EQ(f, angle); + + angle = -180.0F; + a = Angle8(angle); + f = a.ToFloat(); + EXPECT_FLOAT_EQ(f, angle); +} + +TEST(Angle8, Negate) { + float angle = 0; + Angle8 a = Angle8(angle); + a = -a; + float f = a.ToFloat(); + EXPECT_FLOAT_EQ(f, angle); + + angle = 90.0F; + a = Angle8(angle); + a = -a; + f = a.ToFloat(); + EXPECT_FLOAT_EQ(f, -angle); +} + +TEST(Angle8, Add) { + Angle8 a = Angle8(0.0F); + Angle8 b = Angle8(0.0F); + Angle8 r = a + b; + EXPECT_FLOAT_EQ(r.ToFloat(), 0); +} + +TEST(Angle16, Construct) { + Angle16 a = Angle16(0.0F); + float f = a.ToFloat(); + EXPECT_FLOAT_EQ(f, 0); +} + +TEST(Angle16, Negate) { + float angle = 0; + Angle16 a = Angle16(angle); + a = -a; + float f = a.ToFloat(); + EXPECT_FLOAT_EQ(f, angle); + + angle = 90.0F; + a = Angle16(angle); + a = -a; + f = a.ToFloat(); + EXPECT_FLOAT_EQ(f, -angle); +} +#endif \ No newline at end of file diff --git a/test/Matrix_test.cc b/test/Matrix_test.cc index dd358f9..a1267cf 100644 --- a/test/Matrix_test.cc +++ b/test/Matrix_test.cc @@ -18,7 +18,8 @@ TEST(MatrixSingle, Init) { MatrixOf m2 = MatrixOf(2, 2, data2); // negative - MatrixOf m_1 = MatrixOf(-1, -1); + // MatrixOf m_1 = MatrixOf(-1, -1); + // parameters are unsigned } TEST(MatrixSingle, Transpose) { diff --git a/test/Vector2_test.cc b/test/Vector2_test.cc index 88b8c97..ba16470 100644 --- a/test/Vector2_test.cc +++ b/test/Vector2_test.cc @@ -99,8 +99,6 @@ TEST(Vector2, Normalize) { } TEST(Vector2, Negate) { - bool r = false; - Vector2 v1 = Vector2(4, 5); Vector2 v = Vector2::zero; @@ -129,8 +127,6 @@ TEST(Vector2, Negate) { } TEST(Vector2, Subtract) { - bool r = false; - Vector2 v1 = Vector2(4, 5); Vector2 v2 = Vector2(1, 2); Vector2 v = Vector2::zero; @@ -164,11 +160,9 @@ TEST(Vector2, Subtract) { } TEST(Vector2, Addition) { - Vector2 v1 = Vector2(4, 5); Vector2 v2 = Vector2(1, 2); Vector2 v = Vector2::zero; - bool r = false; v = v1 + v2; EXPECT_TRUE(v == Vector2(5, 7)) << "4 5 + 1 2"; @@ -195,8 +189,6 @@ TEST(Vector2, Addition) { } TEST(Vector2, Scale) { - bool r = false; - Vector2 v1 = Vector2(4, 5); Vector2 v2 = Vector2(1, 2); Vector2 v = Vector2::zero; @@ -226,8 +218,6 @@ TEST(Vector2, Scale) { } TEST(Vector2, Multiply) { - bool r = false; - Vector2 v1 = Vector2(4, 5); float f = 3; Vector2 v = Vector2::zero; @@ -257,8 +247,6 @@ TEST(Vector2, Multiply) { } TEST(Vector2, Divide) { - bool r = false; - Vector2 v1 = Vector2(4, 5); float f = 2; Vector2 v = Vector2::zero; diff --git a/test/Vector3_test.cc b/test/Vector3_test.cc index e8f61f8..2148937 100644 --- a/test/Vector3_test.cc +++ b/test/Vector3_test.cc @@ -103,8 +103,6 @@ TEST(Vector3, Normalize) { } TEST(Vector3, Negate) { - bool r = false; - Vector3 v1 = Vector3(4, 5, 6); Vector3 v = Vector3::zero; @@ -133,8 +131,6 @@ TEST(Vector3, Negate) { } TEST(Vector3, Subtract) { - bool r = false; - Vector3 v1 = Vector3(4, 5, 6); Vector3 v2 = Vector3(1, 2, 3); Vector3 v = Vector3::zero; @@ -168,11 +164,9 @@ TEST(Vector3, Subtract) { } TEST(Vector3, Addition) { - Vector3 v1 = Vector3(4, 5, 6); Vector3 v2 = Vector3(1, 2, 3); Vector3 v = Vector3::zero; - bool r = false; v = v1 + v2; EXPECT_TRUE(v == Vector3(5, 7, 9)) << "4 5 6 + 1 2 3"; @@ -199,8 +193,6 @@ TEST(Vector3, Addition) { } TEST(Vector3, Scale) { - bool r = false; - Vector3 v1 = Vector3(4, 5, 6); Vector3 v2 = Vector3(1, 2, 3); Vector3 v = Vector3::zero; @@ -230,8 +222,6 @@ TEST(Vector3, Scale) { } TEST(Vector3, Multiply) { - bool r = false; - Vector3 v1 = Vector3(4, 5, 6); float f = 3; Vector3 v = Vector3::zero; @@ -261,8 +251,6 @@ TEST(Vector3, Multiply) { } TEST(Vector3, Divide) { - bool r = false; - Vector3 v1 = Vector3(4, 5, 6); float f = 2; Vector3 v = Vector3::zero;