3 Achegas 921a0d44ad ... 02b8906525

Autor SHA1 Mensaxe Data
  Kajetan Johannes Hammerle 02b8906525 Allow box array access and use it hai 1 mes
  Kajetan Johannes Hammerle 333425e6ca Use Vector4 for quaternion hai 1 mes
  Kajetan Johannes Hammerle 1bd8ab5ca0 Add more vector sub overloads hai 1 mes
Modificáronse 5 ficheiros con 51 adicións e 43 borrados
  1. 6 3
      include/core/Box.h
  2. 2 3
      include/core/Quaternion.h
  3. 26 14
      include/core/Vector.h
  4. 1 5
      src/Box.c
  5. 16 18
      src/Quaternion.c

+ 6 - 3
include/core/Box.h

@@ -3,9 +3,12 @@
 
 #include "core/Vector.h"
 
-typedef struct {
-    Vector3 min;
-    Vector3 max;
+typedef union {
+    struct {
+        Vector3 min;
+        Vector3 max;
+    };
+    Vector3 v[2];
 } Box;
 
 #define BOX ((Box){0})

+ 2 - 3
include/core/Quaternion.h

@@ -4,11 +4,10 @@
 #include "core/Vector.h"
 
 typedef struct {
-    Vector3 xyz;
-    float w;
+    Vector4 v;
 } Quaternion;
 
-#define UNIT_QUATERNION ((Quaternion){{{0.0f, 0.0f, 0.0f}}, 1.0f})
+#define UNIT_QUATERNION ((Quaternion){{{0.0f, 0.0f, 0.0f, 1.0f}}})
 
 Quaternion* axisAngleQ(Quaternion* q, const Vector3* axis, float angle);
 Quaternion* lerpQ(Quaternion* q, const Quaternion* a, float f,

+ 26 - 14
include/core/Vector.h

@@ -6,23 +6,35 @@
 #define VECTOR_OP2(name) name *r, const name *a
 #define VECTOR_OP3(name) VECTOR_OP2(name), const name* b
 
-#define VECTOR_ARG_Z2(type)
-#define VECTOR_ARG_Z3(type) type z;
-#define VECTOR_ARG_Z4(type) type z;
-
-#define VECTOR_ARG_W2(type)
-#define VECTOR_ARG_W3(type)
-#define VECTOR_ARG_W4(type) type w;
-
-#define DEFINE_VECTOR(N, name, sname, type)                                    \
+#define VECTOR_TYPE(type, Type)                                                \
+    typedef union {                                                            \
+        type data[2];                                                          \
+        struct {                                                               \
+            type x, y;                                                         \
+        };                                                                     \
+    } Type##2;                                                                 \
+    typedef union {                                                            \
+        type data[3];                                                          \
+        struct {                                                               \
+            type x, y, z;                                                      \
+        };                                                                     \
+        Type##2 xy;                                                            \
+    } Type##3;                                                                 \
     typedef union {                                                            \
-        type data[N];                                                          \
+        type data[4];                                                          \
+        struct {                                                               \
+            type x, y, z, w;                                                   \
+        };                                                                     \
         struct {                                                               \
-            type x;                                                            \
-            type y;                                                            \
-            VECTOR_ARG_Z##N(type) VECTOR_ARG_W##N(type)                        \
+            Type##2 xy, zw;                                                    \
         };                                                                     \
-    } name;                                                                    \
+        Type##3 xyz;                                                           \
+    } Type##4
+
+VECTOR_TYPE(float, Vector);
+VECTOR_TYPE(int, IntVector);
+
+#define DEFINE_VECTOR(N, name, sname, type)                                    \
     name* addSet##sname(VECTOR_OP2(name));                                     \
     name* add##sname(VECTOR_OP3(name));                                        \
     name* subSet##sname(VECTOR_OP2(name));                                     \

+ 1 - 5
src/Box.c

@@ -28,11 +28,7 @@ bool collidesWithBox(const Box* box, const Box* other) {
 
 void expandBox(Box* box, const Vector3* offset) {
     for(size_t i = 0; i < 3; i++) {
-        if(offset->data[i] > 0.0f) {
-            box->max.data[i] += offset->data[i];
-        } else {
-            box->min.data[i] += offset->data[i];
-        }
+        box->v[offset->data[i] > 0.0f].data[i] += offset->data[i];
     }
 }
 

+ 16 - 18
src/Quaternion.c

@@ -4,29 +4,27 @@
 #include "core/ToString.h"
 
 Quaternion* axisAngleQ(Quaternion* q, const Vector3* axis, float angle) {
-    q->xyz = *axis;
-    normalize(&q->xyz);
+    q->v.xyz = *axis;
+    normalize(&q->v.xyz);
     angle *= 0.5f;
-    q->w = cosf(angle);
-    mulSet(&q->xyz, sinf(angle));
+    q->v.w = cosf(angle);
+    mulSet(&q->v.xyz, sinf(angle));
     return q;
 }
 
 Quaternion* lerpQ(Quaternion* q, const Quaternion* a, float f,
                   const Quaternion* b) {
-    add(&q->xyz, mul(&a->xyz, 1.0f - f), mul(&b->xyz, f));
-    q->w = a->w * (1.0f - f) + b->w * f;
-    float iLength = 1.0f / sqrtf(squareLength(&q->xyz) + q->w * q->w);
-    mulSet(&q->xyz, iLength);
-    q->w *= iLength;
+    add(&q->v, mul(&a->v, 1.0f - f), mul(&b->v, f));
+    float iLength = 1.0f / length(&q->v);
+    mulSet(&q->v, iLength);
     return q;
 }
 
 Quaternion* mulSetQ(Quaternion* q, const Quaternion* o) {
-    float dot = dot(&q->xyz, &o->xyz);
-    add(&q->xyz, mul(&o->xyz, q->w),
-        add(mul(&q->xyz, o->w), cross(&q->xyz, &o->xyz)));
-    q->w = q->w * o->w - dot;
+    float dot = dot(&q->v.xyz, &o->v.xyz);
+    add(&q->v.xyz, mul(&o->v.xyz, q->v.w),
+        add(mul(&q->v.xyz, o->v.w), cross(&q->v.xyz, &o->v.xyz)));
+    q->v.w = q->v.w * o->v.w - dot;
     return q;
 }
 
@@ -38,14 +36,14 @@ Quaternion* mulQ(Quaternion* q, const Quaternion* a, const Quaternion* b) {
 
 Vector3* mulQV3(Vector3* r, const Quaternion* q, const Vector3* v) {
     Vector3 qv;
-    add(&qv, mul(v, q->w), cross(&q->xyz, v));
-    add(r, mul(&q->xyz, dot(&q->xyz, v)),
-        sub(mul(&qv, q->w), cross(&qv, &q->xyz)));
+    add(&qv, mul(v, q->v.w), cross(&q->v.xyz, v));
+    add(r, mul(&q->v.xyz, dot(&q->v.xyz, v)),
+        sub(mul(&qv, q->v.w), cross(&qv, &q->v.xyz)));
     return r;
 }
 
 size_t toStringQ(const Quaternion* q, char* buffer, size_t n) {
     return toString(buffer, n, "(%.3f i + %.3f j + %.3f k + %.3f)",
-                    (double)q->xyz.x, (double)q->xyz.y, (double)q->xyz.z,
-                    (double)q->w);
+                    (double)q->v.x, (double)q->v.y, (double)q->v.z,
+                    (double)q->v.w);
 }