Kaynağa Gözat

Use anonymous union/struct

Kajetan Johannes Hammerle 2 ay önce
ebeveyn
işleme
68de7fc295
8 değiştirilmiş dosya ile 62 ekleme ve 58 silme
  1. 14 1
      include/core/Vector.h
  2. 5 9
      src/Box.c
  3. 2 2
      src/Frustum.c
  4. 20 20
      src/Matrix.c
  5. 2 2
      src/Plane.c
  6. 2 2
      src/Quaternion.c
  7. 14 17
      src/Vector.c
  8. 3 5
      src/View.c

+ 14 - 1
include/core/Vector.h

@@ -6,9 +6,22 @@
 #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)                                    \
-    typedef struct {                                                           \
+    typedef union {                                                            \
         type data[N];                                                          \
+        struct {                                                               \
+            type x;                                                            \
+            type y;                                                            \
+            VECTOR_ARG_Z##N(type) VECTOR_ARG_W##N(type)                        \
+        };                                                                     \
     } name;                                                                    \
     name* addSet##sname(VECTOR_OP2(name));                                     \
     name* add##sname(VECTOR_OP3(name));                                        \

+ 5 - 9
src/Box.c

@@ -21,12 +21,9 @@ void offsetBox(Box* box, const Vector3* offset) {
 }
 
 bool collidesWithBox(const Box* box, const Box* other) {
-    return box->max.data[0] > other->min.data[0] &&
-           box->min.data[0] < other->max.data[0] &&
-           box->max.data[1] > other->min.data[1] &&
-           box->min.data[1] < other->max.data[1] &&
-           box->max.data[2] > other->min.data[2] &&
-           box->min.data[2] < other->max.data[2];
+    return box->max.x > other->min.x && box->min.x < other->max.x &&
+           box->max.y > other->min.y && box->min.y < other->max.y &&
+           box->max.z > other->min.z && box->min.z < other->max.z;
 }
 
 void expandBox(Box* box, const Vector3* offset) {
@@ -58,7 +55,6 @@ void growBox(Box* box, const Vector3* growth) {
 
 size_t toStringBox(const Box* box, char* buffer, size_t n) {
     return toString(buffer, n, "Box([%.3f, %.3f, %.3f], [%.3f, %.3f, %.3f])",
-                    (double)box->min.data[0], (double)box->min.data[1],
-                    (double)box->min.data[2], (double)box->max.data[0],
-                    (double)box->max.data[1], (double)box->max.data[2]);
+                    (double)box->min.x, (double)box->min.y, (double)box->min.z,
+                    (double)box->max.x, (double)box->max.y, (double)box->max.z);
 }

+ 2 - 2
src/Frustum.c

@@ -19,7 +19,7 @@ void initFrustum(Frustum* f, float fieldOfView, float nearClip, float farClip) {
 }
 
 const Matrix* updateProjection(Frustum* f, const IntVector2* size) {
-    float x = (float)size->data[1] / (f->tan * (float)size->data[0]);
+    float x = (float)size->y / (f->tan * (float)size->x);
     f->projection.data[0] = V(x, 0.0f, 0.0f, 0.0f);
     return &f->projection;
 }
@@ -27,7 +27,7 @@ const Matrix* updateProjection(Frustum* f, const IntVector2* size) {
 void updateFrustumPlanes(Frustum* f, const Vector3* pos, const Vector3* right,
                          const Vector3* up, const Vector3* front,
                          const IntVector2* size) {
-    float aspect = (float)size->data[0] / (float)size->data[1];
+    float aspect = (float)size->x / (float)size->y;
 
     float hNearHeight = f->tan * f->nearClip;
     float hNearWidth = hNearHeight * aspect;

+ 20 - 20
src/Matrix.c

@@ -21,10 +21,10 @@ Matrix* transposeMatrix(Matrix* m) {
 Matrix* mulSetMatrix(Matrix* m, const Matrix* a) {
     for(int i = 0; i < 4; i++) {
         Vector4 d = VECTOR4;
-        addSet(&d, mul(a->data + 0, m->data[i].data[0]));
-        addSet(&d, mul(a->data + 1, m->data[i].data[1]));
-        addSet(&d, mul(a->data + 2, m->data[i].data[2]));
-        addSet(&d, mul(a->data + 3, m->data[i].data[3]));
+        addSet(&d, mul(a->data + 0, m->data[i].x));
+        addSet(&d, mul(a->data + 1, m->data[i].y));
+        addSet(&d, mul(a->data + 2, m->data[i].z));
+        addSet(&d, mul(a->data + 3, m->data[i].w));
         m->data[i] = d;
     }
     return m;
@@ -37,18 +37,18 @@ Matrix* mulMatrix(Matrix* m, const Matrix* a, const Matrix* b) {
 }
 
 Vector3* mulMatrixV3(Vector3* v, const Matrix* m, const Vector3* a) {
-    Vector4 v4 = V(a->data[0], a->data[1], a->data[2], 1.0f);
-    v->data[0] = dot(m->data + 0, &v4);
-    v->data[1] = dot(m->data + 1, &v4);
-    v->data[2] = dot(m->data + 2, &v4);
+    Vector4 v4 = V(a->x, a->y, a->z, 1.0f);
+    v->x = dot(m->data + 0, &v4);
+    v->y = dot(m->data + 1, &v4);
+    v->z = dot(m->data + 2, &v4);
     mulSet(v, 1.0f / dot(m->data + 3, &v4));
     return v;
 }
 
 Matrix* scaleMatrix(Matrix* m, const Vector3* v) {
-    mulSet(m->data + 0, v->data[0]);
-    mulSet(m->data + 1, v->data[1]);
-    mulSet(m->data + 2, v->data[2]);
+    mulSet(m->data + 0, v->x);
+    mulSet(m->data + 1, v->y);
+    mulSet(m->data + 2, v->z);
     return m;
 }
 
@@ -57,9 +57,9 @@ Matrix* scaleMatrixF(Matrix* m, float f) {
 }
 
 Matrix* translateMatrix(Matrix* m, const Vector3* v) {
-    translateMatrixX(m, v->data[0]);
-    translateMatrixY(m, v->data[1]);
-    translateMatrixZ(m, v->data[2]);
+    translateMatrixX(m, v->x);
+    translateMatrixY(m, v->y);
+    translateMatrixZ(m, v->z);
     return m;
 }
 
@@ -79,9 +79,9 @@ Matrix* translateMatrixZ(Matrix* m, float tz) {
 }
 
 Matrix* translateMatrixTo(Matrix* m, const Vector3* v) {
-    m->data[0] = V(1.0f, 0.0f, 0.0f, v->data[0]);
-    m->data[1] = V(0.0f, 1.0f, 0.0f, v->data[1]);
-    m->data[2] = V(0.0f, 0.0f, 1.0f, v->data[2]);
+    m->data[0] = V(1.0f, 0.0f, 0.0f, v->x);
+    m->data[1] = V(0.0f, 1.0f, 0.0f, v->y);
+    m->data[2] = V(0.0f, 0.0f, 1.0f, v->z);
     m->data[3] = V(0.0f, 0.0f, 0.0f, 1.0f);
     return m;
 }
@@ -117,9 +117,9 @@ Matrix* rotateMatrix(Matrix* m, const Quaternion* q) {
     mul(&b, q, &V(M(m, 0, 1), M(m, 1, 1), M(m, 2, 1)));
     mul(&c, q, &V(M(m, 0, 2), M(m, 1, 2), M(m, 2, 2)));
     mul(&d, q, &V(M(m, 0, 3), M(m, 1, 3), M(m, 2, 3)));
-    m->data[0] = V(a.data[0], b.data[0], c.data[0], d.data[0]);
-    m->data[1] = V(a.data[1], b.data[1], c.data[1], d.data[1]);
-    m->data[2] = V(a.data[2], b.data[2], c.data[2], d.data[2]);
+    m->data[0] = V(a.x, b.x, c.x, d.x);
+    m->data[1] = V(a.y, b.y, c.y, d.y);
+    m->data[2] = V(a.z, b.z, c.z, d.z);
     return m;
 }
 

+ 2 - 2
src/Plane.c

@@ -15,6 +15,6 @@ float signedDistance(const Plane* p, const Vector3* v) {
 
 size_t toStringPlane(const Plane* p, char* buffer, size_t n) {
     return toString(buffer, n, "(%.3f x + %.3f y + %.3f z + %.3f)",
-                    (double)p->abc.data[0], (double)p->abc.data[1],
-                    (double)p->abc.data[2], (double)p->d);
+                    (double)p->abc.x, (double)p->abc.y, (double)p->abc.z,
+                    (double)p->d);
 }

+ 2 - 2
src/Quaternion.c

@@ -47,6 +47,6 @@ Vector3* mulQV3(Vector3* r, const Quaternion* q, const Vector3* v) {
 
 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.data[0], (double)q->xyz.data[1],
-                    (double)q->xyz.data[2], (double)q->w);
+                    (double)q->xyz.x, (double)q->xyz.y, (double)q->xyz.z,
+                    (double)q->w);
 }

+ 14 - 17
src/Vector.c

@@ -21,16 +21,16 @@ V3* angles(V3* r, float lengthAngle, float widthAngle) {
     float sLength = sinf(lengthAngle);
     float cLength = cosf(lengthAngle);
 
-    r->data[0] = cWidth * cLength;
-    r->data[1] = sWidth;
-    r->data[2] = -sLength * cWidth;
+    r->x = cWidth * cLength;
+    r->y = sWidth;
+    r->z = -sLength * cWidth;
     return r;
 }
 
 V3* cross(V3* r, const V3* a, const V3* b) {
-    r->data[0] = a->data[1] * b->data[2] - a->data[2] * b->data[1];
-    r->data[1] = a->data[2] * b->data[0] - a->data[0] * b->data[2];
-    r->data[2] = a->data[0] * b->data[1] - a->data[1] * b->data[0];
+    r->x = a->y * b->z - a->z * b->y;
+    r->y = a->z * b->x - a->x * b->z;
+    r->z = a->x * b->y - a->y * b->x;
     return r;
 }
 
@@ -158,30 +158,27 @@ VECTOR_IMPL_FLOAT(V3, 3, IV3)
 VECTOR_IMPL_FLOAT(V4, 4, IV4)
 
 size_t toStringV2(const V2* a, char* buffer, size_t n) {
-    return toString(buffer, n, "[%.3f, %.3f]", (double)a->data[0],
-                    (double)a->data[1]);
+    return toString(buffer, n, "[%.3f, %.3f]", (double)a->x, (double)a->y);
 }
 
 size_t toStringV3(const V3* a, char* buffer, size_t n) {
-    return toString(buffer, n, "[%.3f, %.3f, %.3f]", (double)a->data[0],
-                    (double)a->data[1], (double)a->data[2]);
+    return toString(buffer, n, "[%.3f, %.3f, %.3f]", (double)a->x, (double)a->y,
+                    (double)a->z);
 }
 
 size_t toStringV4(const V4* a, char* buffer, size_t n) {
-    return toString(buffer, n, "[%.3f, %.3f, %.3f, %.3f]", (double)a->data[0],
-                    (double)a->data[1], (double)a->data[2], (double)a->data[3]);
+    return toString(buffer, n, "[%.3f, %.3f, %.3f, %.3f]", (double)a->x,
+                    (double)a->y, (double)a->z, (double)a->w);
 }
 
 size_t toStringIV2(const IV2* a, char* buffer, size_t n) {
-    return toString(buffer, n, "[%d, %d]", a->data[0], a->data[1]);
+    return toString(buffer, n, "[%d, %d]", a->x, a->y);
 }
 
 size_t toStringIV3(const IV3* a, char* buffer, size_t n) {
-    return toString(buffer, n, "[%d, %d, %d]", a->data[0], a->data[1],
-                    a->data[2]);
+    return toString(buffer, n, "[%d, %d, %d]", a->x, a->y, a->z);
 }
 
 size_t toStringIV4(const IV4* a, char* buffer, size_t n) {
-    return toString(buffer, n, "[%d, %d, %d, %d]", a->data[0], a->data[1],
-                    a->data[2], a->data[3]);
+    return toString(buffer, n, "[%d, %d, %d, %d]", a->x, a->y, a->z, a->w);
 }

+ 3 - 5
src/View.c

@@ -36,11 +36,9 @@ void updateDirectionsQ(View* v, const Quaternion* q) {
 
 Matrix* updateMatrix(View* v, const Vector3* pos) {
     Vector4* d = v->view.data;
-    d[0] = V(v->right.data[0], v->right.data[1], v->right.data[2],
-             -dot(&v->right, pos));
-    d[1] = V(v->up.data[0], v->up.data[1], v->up.data[2], -dot(&v->up, pos));
-    d[2] = V(v->back.data[0], v->back.data[1], v->back.data[2],
-             -dot(&v->back, pos));
+    d[0] = V(v->right.x, v->right.y, v->right.z, -dot(&v->right, pos));
+    d[1] = V(v->up.x, v->up.y, v->up.z, -dot(&v->up, pos));
+    d[2] = V(v->back.x, v->back.y, v->back.z, -dot(&v->back, pos));
     d[3] = V(0.0f, 0.0f, 0.0f, 1.0f);
     return &v->view;
 }