Merge branch 'DiscreteAngle' into Experimental

This commit is contained in:
Pascal Serrarens 2024-04-05 10:43:58 +02:00
commit f190dd7df5
11 changed files with 161 additions and 97 deletions

21
Angle16.h Normal file
View File

@ -0,0 +1,21 @@
#include "AngleUsing.h"
#include "Angle.h"
#include <math.h>
typedef AngleUsing<signed short> 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;
}

22
Angle8.h Normal file
View File

@ -0,0 +1,22 @@
#include "AngleUsing.h"
#include "Angle.h"
#include <math.h>
typedef AngleUsing<signed char> 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;
}

42
AngleUsing.h Normal file
View File

@ -0,0 +1,42 @@
#ifndef DISCRETEANGLE_H
#define DISCRETEANGLE_H
#include "Angle.h"
#include "Range.h"
// A fixed angle between (-180..180]
template <typename T> class AngleUsing {
public:
AngleUsing(T sourceValue) { this->value = sourceValue; }
AngleUsing(float f);
float ToFloat() const;
inline T GetValue() const { return this->value; }
AngleUsing<T> operator+(const AngleUsing<T> a) {
AngleUsing<T> r = AngleUsing((float)this->value + a.value);
return r;
}
inline AngleUsing<T> operator+=(const AngleUsing<T> a) {
return this->value + a.value;
}
inline AngleUsing<T> operator-(const AngleUsing<T> a) {
return this->value - a.value;
}
inline AngleUsing<T> operator-() {
this->value = -this->value;
return *this;
}
inline bool operator==(const AngleUsing<T> a) {
return this->value == a.value;
}
// protected:
T value;
};
#endif

View File

@ -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()

View File

@ -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 <math.h>
template <> AngleUsing<unsigned char>::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<unsigned char>::ToFloat() {
float f = ((float)this->value / 255.0F) * 360.0F - 180.0F;
return f;
}
template <> AngleUsing<unsigned short>::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<unsigned short>::ToFloat() {
float f = ((float)this->value / 65535.0F) * 360.0F - 180.0F;
return f;
}

View File

@ -1,24 +0,0 @@
#ifndef DISCRETEANGLE_H
#define DISCRETEANGLE_H
#include "Range.h"
// A fixed angle between (-180..180]
template <typename T> class AngleUsing {
public:
AngleUsing(T sourceValue);
float ToFloat();
inline T GetValue() { return this->value; }
inline AngleUsing<T> operator-(AngleUsing<T> a) {
return this->value - a.value;
}
protected:
T value;
};
#endif
// template <typename T> inline float AngleUsing<T>::ToFloat() { return 0.0f; }

View File

@ -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;

View File

@ -0,0 +1,64 @@
#if GTEST
#include <gtest/gtest.h>
#include <limits>
#include <math.h>
#include "Angle16.h"
#include "Angle8.h"
#define FLOAT_INFINITY std::numeric_limits<float>::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

View File

@ -18,7 +18,8 @@ TEST(MatrixSingle, Init) {
MatrixOf<float> m2 = MatrixOf<float>(2, 2, data2);
// negative
MatrixOf<float> m_1 = MatrixOf<float>(-1, -1);
// MatrixOf<float> m_1 = MatrixOf<float>(-1, -1);
// parameters are unsigned
}
TEST(MatrixSingle, Transpose) {

View File

@ -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;

View File

@ -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;