summaryrefslogtreecommitdiffstats
path: root/libdimension/dimension
diff options
context:
space:
mode:
authorTavian Barnes <tavianator@gmail.com>2009-06-30 15:53:41 +0000
committerTavian Barnes <tavianator@gmail.com>2009-06-30 15:53:41 +0000
commit5dcc62e9f198094e3fd49be1847871853c1449bc (patch)
treeb8efb924424c0160e77782ce40e8ddb6c85c9e2f /libdimension/dimension
parenta50808b77e7b7f9c5239d6416bb8cb9741794670 (diff)
downloaddimension-5dcc62e9f198094e3fd49be1847871853c1449bc.tar.xz
New inline function framework.
Diffstat (limited to 'libdimension/dimension')
-rw-r--r--libdimension/dimension/array.h10
-rw-r--r--libdimension/dimension/geometry.h105
2 files changed, 95 insertions, 20 deletions
diff --git a/libdimension/dimension/array.h b/libdimension/dimension/array.h
index 5d5e50e..036d6c8 100644
--- a/libdimension/dimension/array.h
+++ b/libdimension/dimension/array.h
@@ -45,7 +45,13 @@ void dmnsn_array_get(const dmnsn_array *array, size_t i, void *obj);
void dmnsn_array_set(dmnsn_array *array, size_t i, const void *obj);
void *dmnsn_array_at(dmnsn_array *array, size_t i);
-size_t dmnsn_array_size(const dmnsn_array *array);
-void dmnsn_array_resize(dmnsn_array *array, size_t length);
+/* Inline so for-loops calling this are fast */
+DMNSN_INLINE size_t
+dmnsn_array_size(const dmnsn_array *array)
+{
+ return array->length;
+}
+
+void dmnsn_array_resize(dmnsn_array *array, size_t length);
#endif /* DIMENSION_ARRAY_H */
diff --git a/libdimension/dimension/geometry.h b/libdimension/dimension/geometry.h
index 9ddfbd9..2f08012 100644
--- a/libdimension/dimension/geometry.h
+++ b/libdimension/dimension/geometry.h
@@ -25,6 +25,8 @@
#ifndef DIMENSION_GEOMETRY_H
#define DIMENSION_GEOMETRY_H
+#include <math.h>
+
/* Vector and matrix types. */
typedef struct { double x, y, z; } dmnsn_vector;
@@ -39,12 +41,25 @@ typedef struct {
/* Shorthand for vector/matrix construction */
-dmnsn_vector dmnsn_vector_construct(double x, double y, double z);
-
-dmnsn_matrix dmnsn_matrix_construct(double a0, double a1, double a2, double a3,
- double b0, double b1, double b2, double b3,
- double c0, double c1, double c2, double c3,
- double d0, double d1, double d2, double d3);
+DMNSN_INLINE dmnsn_vector
+dmnsn_vector_construct(double x, double y, double z)
+{
+ dmnsn_vector v = { x, y, z };
+ return v;
+}
+
+DMNSN_INLINE dmnsn_matrix
+dmnsn_matrix_construct(double a0, double a1, double a2, double a3,
+ double b0, double b1, double b2, double b3,
+ double c0, double c1, double c2, double c3,
+ double d0, double d1, double d2, double d3)
+{
+ dmnsn_matrix m = { { { a0, a1, a2, a3 },
+ { b0, b1, b2, b3 },
+ { c0, c1, c2, c3 },
+ { d0, d1, d2, d3 } } };
+ return m;
+}
dmnsn_matrix dmnsn_identity_matrix();
dmnsn_matrix dmnsn_scale_matrix(dmnsn_vector s);
@@ -52,20 +67,69 @@ dmnsn_matrix dmnsn_translation_matrix(dmnsn_vector d);
/* Left-handed rotation; theta/|theta| = axis, |theta| = angle */
dmnsn_matrix dmnsn_rotation_matrix(dmnsn_vector theta);
-dmnsn_line dmnsn_line_construct(dmnsn_vector x0, dmnsn_vector n);
+DMNSN_INLINE dmnsn_line
+dmnsn_line_construct(dmnsn_vector x0, dmnsn_vector n)
+{
+ dmnsn_line l = { x0, n };
+ return l;
+}
/* Vector and matrix arithmetic */
-dmnsn_vector dmnsn_vector_add(dmnsn_vector lhs, dmnsn_vector rhs);
-dmnsn_vector dmnsn_vector_sub(dmnsn_vector lhs, dmnsn_vector rhs);
-dmnsn_vector dmnsn_vector_mul(double lhs, dmnsn_vector rhs);
-dmnsn_vector dmnsn_vector_div(dmnsn_vector lhs, double rhs);
-
-double dmnsn_vector_dot(dmnsn_vector lhs, dmnsn_vector rhs);
-dmnsn_vector dmnsn_vector_cross(dmnsn_vector lhs, dmnsn_vector rhs);
-
-double dmnsn_vector_norm(dmnsn_vector n);
-dmnsn_vector dmnsn_vector_normalize(dmnsn_vector n);
+DMNSN_INLINE dmnsn_vector
+dmnsn_vector_add(dmnsn_vector lhs, dmnsn_vector rhs)
+{
+ dmnsn_vector v = { lhs.x + rhs.x, lhs.y + rhs.y, lhs.z + rhs.z };
+ return v;
+}
+
+DMNSN_INLINE dmnsn_vector
+dmnsn_vector_sub(dmnsn_vector lhs, dmnsn_vector rhs)
+{
+ dmnsn_vector v = { lhs.x - rhs.x, lhs.y - rhs.y, lhs.z - rhs.z };
+ return v;
+}
+
+DMNSN_INLINE dmnsn_vector
+dmnsn_vector_mul(double lhs, dmnsn_vector rhs)
+{
+ dmnsn_vector v = { lhs*rhs.x, lhs*rhs.y, lhs*rhs.z };
+ return v;
+}
+
+DMNSN_INLINE dmnsn_vector
+dmnsn_vector_div(dmnsn_vector lhs, double rhs)
+{
+ dmnsn_vector v = { lhs.x/rhs, lhs.y/rhs, lhs.z/rhs };
+ return v;
+}
+
+DMNSN_INLINE double
+dmnsn_vector_dot(dmnsn_vector lhs, dmnsn_vector rhs)
+{
+ return lhs.x*rhs.x + lhs.y*rhs.y + lhs.z*rhs.z;
+}
+
+DMNSN_INLINE dmnsn_vector
+dmnsn_vector_cross(dmnsn_vector lhs, dmnsn_vector rhs)
+{
+ dmnsn_vector v = { lhs.y*rhs.z - lhs.z*rhs.y,
+ lhs.z*rhs.x - lhs.x*rhs.z,
+ lhs.x*rhs.y - lhs.y*rhs.x };
+ return v;
+}
+
+DMNSN_INLINE double
+dmnsn_vector_norm(dmnsn_vector n)
+{
+ return sqrt(dmnsn_vector_dot(n, n));
+}
+
+DMNSN_INLINE dmnsn_vector
+dmnsn_vector_normalize(dmnsn_vector n)
+{
+ return dmnsn_vector_div(n, dmnsn_vector_norm(n));
+}
dmnsn_matrix dmnsn_matrix_inverse(dmnsn_matrix A);
dmnsn_matrix dmnsn_matrix_mul(dmnsn_matrix lhs, dmnsn_matrix rhs);
@@ -73,7 +137,12 @@ dmnsn_vector dmnsn_matrix_vector_mul(dmnsn_matrix lhs, dmnsn_vector rhs);
dmnsn_line dmnsn_matrix_line_mul(dmnsn_matrix lhs, dmnsn_line rhs);
/* A point on a line, defined by x0 + t*n */
-dmnsn_vector dmnsn_line_point(dmnsn_line l, double t);
+DMNSN_INLINE dmnsn_vector
+dmnsn_line_point(dmnsn_line l, double t)
+{
+ return dmnsn_vector_add(l.x0, dmnsn_vector_mul(t, l.n));
+}
+
/* Solve for the t value such that x0 + t*n = x */
double dmnsn_line_index(dmnsn_line l, dmnsn_vector x);