Browse Source

Expect radians for angle functions

Kajetan Johannes Hammerle 2 months ago
parent
commit
3da553b4e9

+ 3 - 3
include/core/Matrix.h

@@ -23,9 +23,9 @@ Matrix* translateMatrixX(Matrix* m, float tx);
 Matrix* translateMatrixY(Matrix* m, float ty);
 Matrix* translateMatrixZ(Matrix* m, float tz);
 Matrix* translateMatrixTo(Matrix* m, const Vector3* v);
-Matrix* rotateMatrixX(Matrix* m, float degrees);
-Matrix* rotateMatrixY(Matrix* m, float degrees);
-Matrix* rotateMatrixZ(Matrix* m, float degrees);
+Matrix* rotateMatrixX(Matrix* m, float radians);
+Matrix* rotateMatrixY(Matrix* m, float radians);
+Matrix* rotateMatrixZ(Matrix* m, float radians);
 Matrix* rotateMatrix(Matrix* m, const Quaternion* q);
 size_t toStringMatrix(const Matrix* m, char* buffer, size_t n);
 

+ 1 - 2
src/Frustum.c

@@ -3,10 +3,9 @@
 #include <math.h>
 
 #include "core/Generic.h"
-#include "core/Utility.h"
 
 void initFrustum(Frustum* f, float fieldOfView, float nearClip, float farClip) {
-    f->tan = tanf(degreeToRadian(fieldOfView) * 0.5f);
+    f->tan = tanf(fieldOfView * 0.5f);
     f->nearClip = nearClip;
     f->farClip = farClip;
 

+ 9 - 10
src/Matrix.c

@@ -86,26 +86,25 @@ Matrix* translateMatrixTo(Matrix* m, const Vector3* v) {
     return m;
 }
 
-static Matrix* rotate(Matrix* m, float degrees, int a, int b) {
-    degrees = degreeToRadian(degrees);
-    float sin = sinf(degrees);
-    float cos = cosf(degrees);
+static Matrix* rotate(Matrix* m, float radians, int a, int b) {
+    float sin = sinf(radians);
+    float cos = cosf(radians);
     Vector4 v = m->data[a];
     sub(m->data + a, mul(&v, cos), mul(m->data + b, sin));
     add(m->data + b, mul(&v, sin), mul(m->data + b, cos));
     return m;
 }
 
-Matrix* rotateMatrixX(Matrix* m, float degrees) {
-    return rotate(m, degrees, 1, 2);
+Matrix* rotateMatrixX(Matrix* m, float radians) {
+    return rotate(m, radians, 1, 2);
 }
 
-Matrix* rotateMatrixY(Matrix* m, float degrees) {
-    return rotate(m, -degrees, 0, 2);
+Matrix* rotateMatrixY(Matrix* m, float radians) {
+    return rotate(m, -radians, 0, 2);
 }
 
-Matrix* rotateMatrixZ(Matrix* m, float degrees) {
-    return rotate(m, degrees, 0, 1);
+Matrix* rotateMatrixZ(Matrix* m, float radians) {
+    return rotate(m, radians, 0, 1);
 }
 
 Matrix* rotateMatrix(Matrix* m, const Quaternion* q) {

+ 1 - 2
src/Quaternion.c

@@ -2,12 +2,11 @@
 
 #include "core/Generic.h"
 #include "core/ToString.h"
-#include "core/Utility.h"
 
 Quaternion* axisAngleQ(Quaternion* q, const Vector3* axis, float angle) {
     q->xyz = *axis;
     normalize(&q->xyz);
-    angle = degreeToRadian(angle) * 0.5f;
+    angle *= 0.5f;
     q->w = cosf(angle);
     mulSet(&q->xyz, sinf(angle));
     return q;

+ 0 - 4
src/Vector.c

@@ -3,7 +3,6 @@
 #include <math.h>
 
 #include "core/ToString.h"
-#include "core/Utility.h"
 
 typedef Vector2 V2;
 typedef Vector3 V3;
@@ -13,9 +12,6 @@ typedef IntVector3 IV3;
 typedef IntVector4 IV4;
 
 V3* angles(V3* r, float lengthAngle, float widthAngle) {
-    lengthAngle = degreeToRadian(lengthAngle);
-    widthAngle = degreeToRadian(widthAngle);
-
     float sWidth = sinf(widthAngle);
     float cWidth = cosf(widthAngle);
     float sLength = sinf(lengthAngle);

+ 7 - 5
test/modules/FrustumTests.c

@@ -1,10 +1,12 @@
 #include "../Tests.h"
 #include "core/Frustum.h"
-#include "core/ToString.h"
+#include "core/Utility.h"
+
+#define R60 degreeToRadian(60.0f)
 
 static void testToString() {
     Frustum f;
-    initFrustum(&f, 60.0f, 0.1f, 1000.0f);
+    initFrustum(&f, R60, 0.1f, 1000.0f);
     TEST_FLOAT(0.577f, f.tan, 0.01f);
     TEST_FLOAT(0.100f, f.nearClip, 0.01f);
     TEST_FLOAT(1000.0f, f.farClip, 0.01f);
@@ -12,7 +14,7 @@ static void testToString() {
 
 static void testPointIsInside() {
     Frustum f;
-    initFrustum(&f, 60.0f, 0.1f, 1000.0f);
+    initFrustum(&f, R60, 0.1f, 1000.0f);
     updateFrustumPlanes(&f, &V(0, 0, 0), &V(1, 0, 0), &V(0, 1, 0), &V(0, 0, 1),
                         &IV(200, 100));
 
@@ -28,7 +30,7 @@ static void testPointIsInside() {
 static void testSphereIsInside() {
     IntVector2 size = {{200, 100}};
     Frustum f;
-    initFrustum(&f, 60.0f, 0.1f, 1000.0f);
+    initFrustum(&f, R60, 0.1f, 1000.0f);
     updateFrustumPlanes(&f, &V(0, 0, 0), &V(1, 0, 0), &V(0, 1, 0), &V(0, 0, 1),
                         &size);
 
@@ -51,7 +53,7 @@ static void testSphereIsInside() {
 
 static void testUpdateProjection() {
     Frustum f;
-    initFrustum(&f, 60.0f, 0.1f, 1000.0f);
+    initFrustum(&f, R60, 0.1f, 1000.0f);
     const Matrix* m = updateProjection(&f, &IV(400, 300));
     char buffer[128];
     toStringMatrix(m, buffer, sizeof(buffer));

+ 10 - 10
test/modules/MatrixTests.c

@@ -1,6 +1,6 @@
 #include "../Tests.h"
 #include "core/Generic.h"
-#include "core/ToString.h"
+#include "core/Utility.h"
 
 static void testInit() {
     Matrix m = UNIT_MATRIX;
@@ -116,7 +116,7 @@ static void testMatrixCombination() {
 
 static void testRotateX() {
     Matrix m = UNIT_MATRIX;
-    rotateMatrixX(&m, 90.0f);
+    rotateMatrixX(&m, degreeToRadian(90.0f));
     TEST_V3(&V(1.0f, 0.0f, 0.0f), mul(&m, &V(1.0f, 0.0f, 0.0f)));
     TEST_V3(&V(0.0f, 0.0f, 1.0f), mul(&m, &V(0.0f, 1.0f, 0.0f)));
     TEST_V3(&V(0.0f, -1.0f, 0.0f), mul(&m, &V(0.0f, 0.0f, 1.0f)));
@@ -124,7 +124,7 @@ static void testRotateX() {
 
 static void testRotateY() {
     Matrix m = UNIT_MATRIX;
-    rotateMatrixY(&m, 90.0f);
+    rotateMatrixY(&m, degreeToRadian(90.0f));
     TEST_V3(&V(0.0f, 0.0f, -1.0f), mul(&m, &V(1.0f, 0.0f, 0.0f)));
     TEST_V3(&V(0.0f, 1.0f, 0.0f), mul(&m, &V(0.0f, 1.0f, 0.0f)));
     TEST_V3(&V(1.0f, 0.0f, 0.0f), mul(&m, &V(0.0f, 0.0f, 1.0f)));
@@ -132,7 +132,7 @@ static void testRotateY() {
 
 static void testRotateZ() {
     Matrix m = UNIT_MATRIX;
-    rotateMatrixZ(&m, 90.0f);
+    rotateMatrixZ(&m, degreeToRadian(90.0f));
     TEST_V3(&V(0.0f, 1.0f, 0.0f), mul(&m, &V(1.0f, 0.0f, 0.0f)));
     TEST_V3(&V(-1.0f, 0.0f, 0.0f), mul(&m, &V(0.0f, 1.0f, 0.0f)));
     TEST_V3(&V(0.0f, 0.0f, 1.0f), mul(&m, &V(0.0f, 0.0f, 1.0f)));
@@ -140,11 +140,11 @@ static void testRotateZ() {
 
 static void testQuaternionMatrix() {
     Quaternion q1 = UNIT_QUATERNION;
-    axisAngleQ(&q1, &V(1.0f, 0.0f, 0.0f), 48.0f);
+    axisAngleQ(&q1, &V(1.0f, 0.0f, 0.0f), degreeToRadian(48.0f));
     Quaternion q2 = UNIT_QUATERNION;
-    axisAngleQ(&q2, &V(0.0f, 1.0f, 0.0f), 52.0f);
+    axisAngleQ(&q2, &V(0.0f, 1.0f, 0.0f), degreeToRadian(52.0f));
     Quaternion q3 = UNIT_QUATERNION;
-    axisAngleQ(&q3, &V(0.0f, 0.0f, 1.0f), 60.0f);
+    axisAngleQ(&q3, &V(0.0f, 0.0f, 1.0f), degreeToRadian(60.0f));
 
     Matrix m = UNIT_MATRIX;
     translateMatrix(&m, &V(1.0f, 2.0f, 3.0f));
@@ -155,9 +155,9 @@ static void testQuaternionMatrix() {
 
     Matrix check = UNIT_MATRIX;
     translateMatrix(&check, &V(1.0f, 2.0f, 3.0f));
-    rotateMatrixX(&check, 48.0f);
-    rotateMatrixY(&check, 52.0f);
-    rotateMatrixZ(&check, 60.0f);
+    rotateMatrixX(&check, degreeToRadian(48.0f));
+    rotateMatrixY(&check, degreeToRadian(52.0f));
+    rotateMatrixZ(&check, degreeToRadian(60.0f));
     translateMatrix(&check, &V(1.0f, 2.0f, 3.0f));
 
     for(int i = 0; i < 16; i++) {

+ 13 - 12
test/modules/QuaternionTests.c

@@ -1,5 +1,6 @@
 #include "../Tests.h"
 #include "core/Generic.h"
+#include "core/Utility.h"
 
 #define CV3(a, b, c) (&(Vector3){{a, b, c}})
 #define CV30 CV3(0.0f, 0.0f, 0.0f)
@@ -13,7 +14,7 @@ static void testInit() {
 
 static void testAxisAndDegreesInit() {
     Quaternion q = UNIT_QUATERNION;
-    axisAngleQ(&q, CV3(1.0f, 2.0f, 3.0f), 142.0f);
+    axisAngleQ(&q, CV3(1.0f, 2.0f, 3.0f), degreeToRadian(142.0f));
     char buffer[128];
     toStringQ(&q, buffer, sizeof(buffer));
     TEST_STRING("(0.253 i + 0.505 j + 0.758 k + 0.326)", buffer);
@@ -21,9 +22,9 @@ static void testAxisAndDegreesInit() {
 
 static void testLerp() {
     Quaternion q1 = UNIT_QUATERNION;
-    axisAngleQ(&q1, CV3(2.0f, 5.0f, 7.0f), 130.0f);
+    axisAngleQ(&q1, CV3(2.0f, 5.0f, 7.0f), degreeToRadian(130.0f));
     Quaternion q2 = UNIT_QUATERNION;
-    axisAngleQ(&q2, CV3(1.0f, 2.0f, 4.0f), 260.0f);
+    axisAngleQ(&q2, CV3(1.0f, 2.0f, 4.0f), degreeToRadian(260.0f));
     Quaternion q3;
     lerpQ(&q3, &q1, 0.3f, &q2);
 
@@ -34,9 +35,9 @@ static void testLerp() {
 
 static void testMulSet() {
     Quaternion q1 = UNIT_QUATERNION;
-    axisAngleQ(&q1, CV3(2.0f, 5.0f, 7.0f), 50.0f);
+    axisAngleQ(&q1, CV3(2.0f, 5.0f, 7.0f), degreeToRadian(50.0f));
     Quaternion q2 = UNIT_QUATERNION;
-    axisAngleQ(&q2, CV3(2.0f, 5.0f, 7.0f), 60.0f);
+    axisAngleQ(&q2, CV3(2.0f, 5.0f, 7.0f), degreeToRadian(60.0f));
     Quaternion q3 = UNIT_QUATERNION;
     mulSet(&q3, &q1);
 
@@ -49,7 +50,7 @@ static void testMulSet() {
     mulSet(&q3, &q2);
     toStringQ(&q3, bufferQ3, sizeof(bufferQ3));
 
-    axisAngleQ(&q1, CV3(2.0f, 5.0f, 7.0f), 110.0f);
+    axisAngleQ(&q1, CV3(2.0f, 5.0f, 7.0f), degreeToRadian(110.0f));
     toStringQ(&q1, bufferQ1, sizeof(bufferQ1));
 
     TEST_STRING(bufferQ1, bufferQ3);
@@ -57,9 +58,9 @@ static void testMulSet() {
 
 static void testMul() {
     Quaternion q1 = UNIT_QUATERNION;
-    axisAngleQ(&q1, CV3(2.0f, 5.0f, 7.0f), 50.0f);
+    axisAngleQ(&q1, CV3(2.0f, 5.0f, 7.0f), degreeToRadian(50.0f));
     Quaternion q2 = UNIT_QUATERNION;
-    axisAngleQ(&q2, CV3(2.0f, 5.0f, 7.0f), 60.0f);
+    axisAngleQ(&q2, CV3(2.0f, 5.0f, 7.0f), degreeToRadian(60.0f));
     Quaternion q3 = UNIT_QUATERNION;
     mul(&q3, &q1, &q2);
 
@@ -67,7 +68,7 @@ static void testMul() {
     toStringQ(&q3, bufferQ3, sizeof(bufferQ3));
 
     Quaternion q = UNIT_QUATERNION;
-    axisAngleQ(&q, CV3(2.0f, 5.0f, 7.0f), 110.0f);
+    axisAngleQ(&q, CV3(2.0f, 5.0f, 7.0f), degreeToRadian(110.0f));
     char bufferQ[128];
     toStringQ(&q, bufferQ, sizeof(bufferQ));
 
@@ -76,11 +77,11 @@ static void testMul() {
 
 static void testMulVector() {
     Quaternion q1 = UNIT_QUATERNION;
-    axisAngleQ(&q1, CV3(1.0f, 0.0f, 0.0f), 90.0f);
+    axisAngleQ(&q1, CV3(1.0f, 0.0f, 0.0f), degreeToRadian(90.0f));
     Quaternion q2 = UNIT_QUATERNION;
-    axisAngleQ(&q2, CV3(0.0f, 1.0f, 0.0f), 90.0f);
+    axisAngleQ(&q2, CV3(0.0f, 1.0f, 0.0f), degreeToRadian(90.0f));
     Quaternion q3 = UNIT_QUATERNION;
-    axisAngleQ(&q3, CV3(0.0f, 0.0f, 1.0f), 90.0f);
+    axisAngleQ(&q3, CV3(0.0f, 0.0f, 1.0f), degreeToRadian(90.0f));
 
     Vector3 v1 = {{1.0f, 0.0f, 0.0f}};
     Vector3 v2 = {{0.0f, 1.0f, 0.0f}};

+ 38 - 29
test/modules/VectorTests.c

@@ -2,6 +2,7 @@
 
 #include "../Tests.h"
 #include "core/Generic.h"
+#include "core/Utility.h"
 
 static const float eps = 0.0001f;
 
@@ -35,38 +36,46 @@ static const float eps = 0.0001f;
 
 #define TESTS X(V2) X(V3) X(V4) X(IV2) X(IV3) X(IV4)
 
+#define R45 degreeToRadian(45.0f)
+#define R90 degreeToRadian(90.0f)
+#define R135 degreeToRadian(135.0f)
+#define R180 degreeToRadian(180.0f)
+#define R225 degreeToRadian(225.0f)
+#define R270 degreeToRadian(270.0f)
+#define R315 degreeToRadian(315.0f)
+
 static void testSetAngles() {
     float root = sqrtf(2) * 0.5f;
     TEST_V3(FV3(1, 0, 0), angles(CV30, 0, 0));
-    TEST_V3(FV3(root, 0, -root), angles(CV30, 45, 0));
-    TEST_V3(FV3(0, 0, -1), angles(CV30, 90, 0));
-    TEST_V3(FV3(-root, 0, -root), angles(CV30, 135, 0));
-    TEST_V3(FV3(-1, 0, 0), angles(CV30, 180, 0));
-    TEST_V3(FV3(-root, 0, root), angles(CV30, 225, 0));
-    TEST_V3(FV3(0, 0, 1), angles(CV30, 270, 0));
-    TEST_V3(FV3(root, 0, root), angles(CV30, 315, 0));
-
-    TEST_V3(FV3(0, 1, 0), angles(CV30, 0, 90));
-    TEST_V3(FV3(0, 1, 0), angles(CV30, 90, 90));
-    TEST_V3(FV3(0, 1, 0), angles(CV30, 180, 90));
-    TEST_V3(FV3(0, 1, 0), angles(CV30, 270, 90));
-
-    TEST_V3(FV3(0, -1, 0), angles(CV30, 0, -90));
-    TEST_V3(FV3(0, -1, 0), angles(CV30, 90, -90));
-    TEST_V3(FV3(0, -1, 0), angles(CV30, 180, -90));
-    TEST_V3(FV3(0, -1, 0), angles(CV30, 270, -90));
-
-    TEST_V3(FV3(root, root, 0), angles(CV30, 0, 45));
-    TEST_V3(FV3(0, root, -root), angles(CV30, 90, 45));
-    TEST_V3(FV3(-root, root, 0), angles(CV30, 180, 45));
-    TEST_V3(FV3(0, root, root), angles(CV30, 270, 45));
-
-    TEST_V3(FV3(root, -root, 0), angles(CV30, 0, -45));
-    TEST_V3(FV3(0, -root, -root), angles(CV30, 90, -45));
-    TEST_V3(FV3(-root, -root, 0), angles(CV30, 180, -45));
-    TEST_V3(FV3(0, -root, root), angles(CV30, 270, -45));
-
-    TEST_V3(FV3(0.5f, root, -0.5f), angles(CV30, 45, 45));
+    TEST_V3(FV3(root, 0, -root), angles(CV30, R45, 0));
+    TEST_V3(FV3(0, 0, -1), angles(CV30, R90, 0));
+    TEST_V3(FV3(-root, 0, -root), angles(CV30, R135, 0));
+    TEST_V3(FV3(-1, 0, 0), angles(CV30, R180, 0));
+    TEST_V3(FV3(-root, 0, root), angles(CV30, R225, 0));
+    TEST_V3(FV3(0, 0, 1), angles(CV30, R270, 0));
+    TEST_V3(FV3(root, 0, root), angles(CV30, R315, 0));
+
+    TEST_V3(FV3(0, 1, 0), angles(CV30, 0, R90));
+    TEST_V3(FV3(0, 1, 0), angles(CV30, R90, R90));
+    TEST_V3(FV3(0, 1, 0), angles(CV30, R180, R90));
+    TEST_V3(FV3(0, 1, 0), angles(CV30, R270, R90));
+
+    TEST_V3(FV3(0, -1, 0), angles(CV30, 0, -R90));
+    TEST_V3(FV3(0, -1, 0), angles(CV30, R90, -R90));
+    TEST_V3(FV3(0, -1, 0), angles(CV30, R180, -R90));
+    TEST_V3(FV3(0, -1, 0), angles(CV30, R270, -R90));
+
+    TEST_V3(FV3(root, root, 0), angles(CV30, 0, R45));
+    TEST_V3(FV3(0, root, -root), angles(CV30, R90, R45));
+    TEST_V3(FV3(-root, root, 0), angles(CV30, R180, R45));
+    TEST_V3(FV3(0, root, root), angles(CV30, R270, R45));
+
+    TEST_V3(FV3(root, -root, 0), angles(CV30, 0, -R45));
+    TEST_V3(FV3(0, -root, -root), angles(CV30, R90, -R45));
+    TEST_V3(FV3(-root, -root, 0), angles(CV30, R180, -R45));
+    TEST_V3(FV3(0, -root, root), angles(CV30, R270, -R45));
+
+    TEST_V3(FV3(0.5f, root, -0.5f), angles(CV30, R45, R45));
 }
 
 static void testCross() {