cgmath initial commit
authorJohn Tsiombikas <nuclear@mutantstargoat.com>
Wed, 30 Aug 2017 02:43:03 +0000 (05:43 +0300)
committerJohn Tsiombikas <nuclear@mutantstargoat.com>
Wed, 30 Aug 2017 02:43:03 +0000 (05:43 +0300)
CMakeLists.txt [new file with mode: 0644]
src/cgmath.h [new file with mode: 0644]
src/cgmath.inl [new file with mode: 0644]

diff --git a/CMakeLists.txt b/CMakeLists.txt
new file mode 100644 (file)
index 0000000..fd714d1
--- /dev/null
@@ -0,0 +1,44 @@
+cmake_minimum_required(VERSION 2.8.8)
+project(gph-cmath)
+
+include(GNUInstallDirs)
+
+set(SO_MAJOR 0)
+set(SO_MINOR 1)
+
+file(GLOB hdr "src/*.h")
+file(GLOB inl "src/*.inl")
+list(APPEND hdr ${inl})
+file(GLOB src "src/*.c")
+
+add_library(cgmath SHARED ${src} ${hdr})
+add_library(cgmath-static STATIC ${src} ${hdr})
+
+set_target_properties(cgmath PROPERTIES VERSION ${SO_MAJOR}.${SO_MINOR})
+set_target_properties(cgmath PROPERTIES SOVERSION ${SO_MAJOR})
+
+if(MSVC)
+       set_target_properties(cgmath PROPERTIES PREFIX "lib")
+       set_target_properties(cgmath PROPERTIES IMPORT_PREFIX "lib")
+       set_target_properties(cgmath-static PROPERTIES PREFIX "lib")
+
+       set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /wd4244 /wd4996")
+else()
+       set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -pedantic -Wall")
+endif()
+
+if(NOT WIN32)
+       set_target_properties(cgmath-static PROPERTIES OUTPUT_NAME cgmath)
+endif()
+
+install(TARGETS cgmath
+       RUNTIME DESTINATION bin
+       LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}
+       ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR})
+
+install(TARGETS cgmath-static
+       RUNTIME DESTINATION bin
+       LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}
+       ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR})
+
+install(FILES ${hdr} DESTINATION include/cgmath)
diff --git a/src/cgmath.h b/src/cgmath.h
new file mode 100644 (file)
index 0000000..c033a22
--- /dev/null
@@ -0,0 +1,30 @@
+/* C version of the graphene math library */
+#ifndef CGMATH_H_
+#define CGMATH_H_
+
+#include <math.h>
+
+typedef struct {
+       float x, y, z;
+} cgm_vec3;
+
+typedef struct {
+       float x, y, z, w;
+} cgm_quat;
+
+static inline void cgm_vadd(cgm_vec3 *a, const cgm_vec3 *b);
+static inline void cgm_vsub(cgm_vec3 *a, const cgm_vec3 *b);
+static inline void cgm_vmul(cgm_vec3 *a, const cgm_vec3 *b);
+static inline void cgm_vscale(cgm_vec3 *v, float s);
+
+static inline float cgm_vdot(const cgm_vec3 *a, const cgm_vec3 *b);
+static inline void cgm_vcross(cgm_vec3 *res, const cgm_vec3 *a, const cgm_vec3 *b);
+static inline float cgm_vlength(const cgm_vec3 *v);
+static inline float cgm_vlength_sq(const cgm_vec3 *v);
+static inline float cgm_vdist(const cgm_vec3 *a, const cgm_vec3 *b);
+static inline float cgm_vdist_sq(const cgm_vec3 *a, const cgm_vec3 *b);
+static inline void cgm_vnormalize(cgm_vec3 *v);
+
+#include "cgmath.inl"
+
+#endif /* CGMATH_H_ */
diff --git a/src/cgmath.inl b/src/cgmath.inl
new file mode 100644 (file)
index 0000000..081452c
--- /dev/null
@@ -0,0 +1,76 @@
+static inline void cgm_vadd(cgm_vec3 *a, const cgm_vec3 *b)
+{
+       a->x += b->x;
+       a->y += b->y;
+       a->z += b->z;
+}
+
+static inline void cgm_vsub(cgm_vec3 *a, const cgm_vec3 *b)
+{
+       a->x -= b->x;
+       a->y -= b->y;
+       a->z -= b->z;
+}
+
+static inline void cgm_vmul(cgm_vec3 *a, const cgm_vec3 *b)
+{
+       a->x *= b->x;
+       a->y *= b->y;
+       a->z *= b->z;
+}
+
+static inline void cgm_vscale(cgm_vec3 *v, float s)
+{
+       v->x *= s;
+       v->y *= s;
+       v->z *= s;
+}
+
+static inline float cgm_vdot(const cgm_vec3 *a, const cgm_vec3 *b)
+{
+       return a->x * b->x + a->y * b->y + a->z * b->z;
+}
+
+static inline void cgm_vcross(cgm_vec3 *res, const cgm_vec3 *a, const cgm_vec3 *b)
+{
+       res->x = a->y * b->z - a->z * b->y;
+       res->y = a->z * b->x - a->x * b->z;
+       res->z = a->x * b->y - a->y * b->x;
+}
+
+static inline float cgm_vlength(const cgm_vec3 *v)
+{
+       return sqrt(v->x * v->x + v->y * v->y + v->z * v->z);
+}
+
+static inline float cgm_vlength_sq(const cgm_vec3 *v)
+{
+       return v->x * v->x + v->y * v->y + v->z * v->z;
+}
+
+static inline float cgm_vdist(const cgm_vec3 *a, const cgm_vec3 *b)
+{
+       float dx = a->x - b->x;
+       float dy = a->y - b->y;
+       float dz = a->z - b->z;
+       return sqrt(dx * dx + dy * dy + dz * dz);
+}
+
+static inline float cgm_vdist_sq(const cgm_vec3 *a, const cgm_vec3 *b)
+{
+       float dx = a->x - b->x;
+       float dy = a->y - b->y;
+       float dz = a->z - b->z;
+       return dx * dx + dy * dy + dz * dz;
+}
+
+static inline void cgm_vnormalize(cgm_vec3 *v)
+{
+       float len = cgm_vlength(v);
+       if(len != 0.0f) {
+               float s = 1.0f / len;
+               v->x *= s;
+               v->y *= s;
+               v->z *= s;
+       }
+}