libvmath4

changeset 0:4d6383605d64 tip

initial commit
author John Tsiombikas <nuclear@member.fsf.org>
date Sun, 05 Oct 2014 04:00:05 +0300
parents
children
files .hgignore Makefile examples/simple/.clang_complete examples/simple/main.cc src/matrix.h src/matrix.inl src/quat.cc src/quat.h src/quat.inl src/vector.h src/vector.inl src/vmath.h
diffstat 11 files changed, 444 insertions(+), 0 deletions(-) [+]
line diff
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/.hgignore	Sun Oct 05 04:00:05 2014 +0300
     1.3 @@ -0,0 +1,3 @@
     1.4 +\.o$
     1.5 +\.d$
     1.6 +\.swp$
     2.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     2.2 +++ b/Makefile	Sun Oct 05 04:00:05 2014 +0300
     2.3 @@ -0,0 +1,52 @@
     2.4 +PREFIX = /usr/local
     2.5 +
     2.6 +src = $(wildcard src/*.cc)
     2.7 +obj = $(src:.cc=.o)
     2.8 +dep = $(obj:.o=.d)
     2.9 +lib_a = libvmath4.a
    2.10 +
    2.11 +so_major = 4
    2.12 +so_minor = 0
    2.13 +
    2.14 +warn = -Wall
    2.15 +dbg = -g
    2.16 +
    2.17 +CXXFLAGS = -pedantic $(warn) $(dbg) $(pic)
    2.18 +LDFLAGS = -lm
    2.19 +
    2.20 +ifeq ($(shell uname -s), Darwin)
    2.21 +	lib_so = libvmath4.dylib
    2.22 +	shared = -dynamiclib
    2.23 +else
    2.24 +	ldname = libvmath4.so
    2.25 +	soname = libvmath4.so.$(so_major)
    2.26 +	lib_so = libvmath4.so.$(so_major).$(so_minor)
    2.27 +	shared = -shared -Wl,-soname=$(soname)
    2.28 +	pic = -fPIC
    2.29 +endif
    2.30 +
    2.31 +.PHONY: all
    2.32 +all: $(lib_so) $(lib_a)
    2.33 +
    2.34 +$(lib_a): $(obj)
    2.35 +	$(AR) rcs $@ $(obj)
    2.36 +
    2.37 +$(lib_so): $(obj)
    2.38 +	$(CXX) $(shared) -o $@ $(obj) $(LDFLAGS)
    2.39 +
    2.40 +$(soname): $(lib_so)
    2.41 +	rm -f $(soname)
    2.42 +	ln -s $(lib_so) $(soname)
    2.43 +
    2.44 +$(ldname): $(soname)
    2.45 +	rm -f $(ldname)
    2.46 +	ln -s $(soname) $(ldname)
    2.47 +
    2.48 +-include $(dep)
    2.49 +
    2.50 +%.d: %.cc
    2.51 +	@$(CPP) $(CXXFLAGS) $< -MM -MT $(@:.d=.o) >$@
    2.52 +
    2.53 +.PHONY: clean
    2.54 +clean:
    2.55 +	rm -f $(obj) $(bin) $(dep)
     3.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     3.2 +++ b/examples/simple/.clang_complete	Sun Oct 05 04:00:05 2014 +0300
     3.3 @@ -0,0 +1,1 @@
     3.4 +-I../../src
     4.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     4.2 +++ b/examples/simple/main.cc	Sun Oct 05 04:00:05 2014 +0300
     4.3 @@ -0,0 +1,7 @@
     4.4 +#include <stdio.h>
     4.5 +#include <vmath.h>
     4.6 +
     4.7 +int main()
     4.8 +{
     4.9 +
    4.10 +}
     5.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     5.2 +++ b/src/matrix.h	Sun Oct 05 04:00:05 2014 +0300
     5.3 @@ -0,0 +1,39 @@
     5.4 +#ifndef MATRIX_H_
     5.5 +#define MATRIX_H_
     5.6 +
     5.7 +namespace vmath {
     5.8 +
     5.9 +class Matrix3x3 {
    5.10 +private:
    5.11 +	float m[3][3];
    5.12 +
    5.13 +public:
    5.14 +	static Matrix3x3 id;
    5.15 +
    5.16 +	Matrix3x3();
    5.17 +	Matrix3x3(const float *m);
    5.18 +	Matrix3x3(float m00, float m01, float m02,
    5.19 +			float m10, float m11, float m12,
    5.20 +			float m20, float m21, float m22);
    5.21 +};
    5.22 +
    5.23 +class Matrix4x4 {
    5.24 +private:
    5.25 +	float m[4][4];
    5.26 +
    5.27 +public:
    5.28 +	static Matrix4x4 id;
    5.29 +
    5.30 +	Matrix4x4();
    5.31 +	Matrix4x4(const float *m);
    5.32 +	Matrix4x4(float m00, float m01, float m02, float m03,
    5.33 +			float m10, float m11, float m12, float m13,
    5.34 +			float m20, float m21, float m22, float m23,
    5.35 +			float m30, float m31, float m32, float m33);
    5.36 +};
    5.37 +
    5.38 +#include "matrix.inl"
    5.39 +
    5.40 +}	// namespace vmath
    5.41 +
    5.42 +#endif	// MATRIX_H_
     6.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     6.2 +++ b/src/matrix.inl	Sun Oct 05 04:00:05 2014 +0300
     6.3 @@ -0,0 +1,45 @@
     6.4 +#include <string.h>
     6.5 +
     6.6 +Matrix3x3 Matrix3x3::id = Matrix3x3(1, 0, 0, 0, 1, 0, 0, 0, 1);
     6.7 +Matrix4x4 Matrix4x4::id = Matrix4x4(1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1);
     6.8 +
     6.9 +Matrix3x3::Matrix3x3()
    6.10 +{
    6.11 +	memcpy(m, Matrix3x3::id.m, sizeof m);
    6.12 +}
    6.13 +
    6.14 +Matrix3x3::Matrix3x3(const float *m)
    6.15 +{
    6.16 +	memcpy(this->m, m, sizeof this->m);
    6.17 +}
    6.18 +
    6.19 +Matrix3x3::Matrix3x3(float m00, float m01, float m02,
    6.20 +					float m10, float m11, float m12,
    6.21 +					float m20, float m21, float m22)
    6.22 +{
    6.23 +	m[0][0] = m00; m[0][1] = m01; m[0][2] = m02;
    6.24 +	m[1][0] = m10; m[1][1] = m11; m[1][2] = m12;
    6.25 +	m[2][0] = m20; m[2][1] = m21; m[2][2] = m22;
    6.26 +}
    6.27 +
    6.28 +
    6.29 +Matrix4x4::Matrix4x4()
    6.30 +{
    6.31 +	memcpy(m, Matrix4x4::id.m, sizeof m);
    6.32 +}
    6.33 +
    6.34 +Matrix4x4::Matrix4x4(const float *m)
    6.35 +{
    6.36 +	memcpy(this->m, m, sizeof this->m);
    6.37 +}
    6.38 +
    6.39 +Matrix4x4::Matrix4x4(float m00, float m01, float m02, float m03,
    6.40 +					float m10, float m11, float m12, float m13,
    6.41 +					float m20, float m21, float m22, float m23,
    6.42 +					float m30, float m31, float m32, float m33)
    6.43 +{
    6.44 +	m[0][0] = m00; m[0][1] = m01; m[0][2] = m02; m[0][3] = m03;
    6.45 +	m[1][0] = m10; m[1][1] = m11; m[1][2] = m12; m[1][3] = m13;
    6.46 +	m[2][0] = m20; m[2][1] = m21; m[2][2] = m22; m[2][3] = m23;
    6.47 +	m[3][0] = m20; m[3][1] = m21; m[3][2] = m22; m[3][3] = m23;
    6.48 +}
     7.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     7.2 +++ b/src/quat.cc	Sun Oct 05 04:00:05 2014 +0300
     7.3 @@ -0,0 +1,12 @@
     7.4 +#include "quat.h"
     7.5 +#include "vector.h"
     7.6 +
     7.7 +using namespace vmath;
     7.8 +
     7.9 +Quat::Quat(const Vector4 &v)
    7.10 +{
    7.11 +	x = v.x;
    7.12 +	y = v.y;
    7.13 +	z = v.z;
    7.14 +	w = v.w;
    7.15 +}
     8.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     8.2 +++ b/src/quat.h	Sun Oct 05 04:00:05 2014 +0300
     8.3 @@ -0,0 +1,25 @@
     8.4 +#ifndef QUAT_H_
     8.5 +#define QUAT_H_
     8.6 +
     8.7 +namespace vmath {
     8.8 +
     8.9 +class Vector4;
    8.10 +
    8.11 +class Quat {
    8.12 +public:
    8.13 +	float x, y, z, w;
    8.14 +
    8.15 +	Quat();
    8.16 +	Quat(float x, float y, float z, float w);
    8.17 +	Quat(const float *v);
    8.18 +	Quat(const Vector4 &v);
    8.19 +
    8.20 +	float &operator [](int idx);
    8.21 +	const float &operator [](int idx) const;
    8.22 +};
    8.23 +
    8.24 +#include "quat.inl"
    8.25 +
    8.26 +}	// namespace vmath
    8.27 +
    8.28 +#endif	// QUAT_H_
     9.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     9.2 +++ b/src/quat.inl	Sun Oct 05 04:00:05 2014 +0300
     9.3 @@ -0,0 +1,51 @@
     9.4 +Quat::Quat()
     9.5 +{
     9.6 +	x = y = z = 0.0f;
     9.7 +	w = 1.0f;
     9.8 +}
     9.9 +
    9.10 +Quat::Quat(float x, float y, float z, float w)
    9.11 +{
    9.12 +	this->x = x;
    9.13 +	this->y = y;
    9.14 +	this->z = z;
    9.15 +	this->w = w;
    9.16 +}
    9.17 +
    9.18 +Quat::Quat(const float *v)
    9.19 +{
    9.20 +	x = v[0];
    9.21 +	y = v[1];
    9.22 +	z = v[2];
    9.23 +	w = v[3];
    9.24 +}
    9.25 +
    9.26 +float &Quat::operator [](int idx)
    9.27 +{
    9.28 +	switch(idx) {
    9.29 +	case 1:
    9.30 +		return y;
    9.31 +	case 2:
    9.32 +		return z;
    9.33 +	case 3:
    9.34 +		return w;
    9.35 +	case 0:
    9.36 +	default:
    9.37 +		return x;
    9.38 +	}
    9.39 +}
    9.40 +
    9.41 +const float &Quat::operator [](int idx) const
    9.42 +{
    9.43 +	switch(idx) {
    9.44 +	case 1:
    9.45 +		return y;
    9.46 +	case 2:
    9.47 +		return z;
    9.48 +	case 3:
    9.49 +		return w;
    9.50 +	case 0:
    9.51 +	default:
    9.52 +		return x;
    9.53 +	}
    9.54 +}
    10.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
    10.2 +++ b/src/vector.h	Sun Oct 05 04:00:05 2014 +0300
    10.3 @@ -0,0 +1,199 @@
    10.4 +#ifndef VMATH4_VECTOR_H_
    10.5 +#define VMATH4_VECTOR_H_
    10.6 +
    10.7 +#include "matrix.h"
    10.8 +#include "quat.h"
    10.9 +
   10.10 +namespace vmath {
   10.11 +
   10.12 +class Vector3;
   10.13 +class Vector4;
   10.14 +
   10.15 +class Vector2 {
   10.16 +public:
   10.17 +	float x, y;
   10.18 +
   10.19 +	inline Vector2();
   10.20 +	inline Vector2(float x, float y);
   10.21 +	explicit inline Vector2(float *v);
   10.22 +	explicit inline Vector2(float scalar);
   10.23 +	explicit inline Vector2(const Vector3 &v);
   10.24 +	explicit inline Vector2(const Vector4 &v);
   10.25 +
   10.26 +	inline float &operator [](int idx);
   10.27 +	inline const float &operator [](int idx) const;
   10.28 +
   10.29 +	operator float* ();
   10.30 +	operator const float* () const;
   10.31 +};
   10.32 +
   10.33 +inline Vector2 operator -(const Vector2 &v);
   10.34 +
   10.35 +inline Vector2 operator +(const Vector2 &a, const Vector2 &b);
   10.36 +inline Vector2 operator -(const Vector2 &a, const Vector2 &b);
   10.37 +
   10.38 +inline Vector2 operator *(const Vector2 &a, const Vector2 &b);
   10.39 +inline Vector2 operator *(const Vector2 &v, float s);
   10.40 +inline Vector2 operator *(float s, const Vector2 &v);
   10.41 +
   10.42 +inline Vector2 operator /(const Vector2 &a, const Vector2 &b);
   10.43 +inline Vector2 operator /(const Vector2 &v, float s);
   10.44 +inline Vector2 operator /(float s, const Vector2 &v);
   10.45 +
   10.46 +inline Vector2 operator +=(const Vector2 &a, const Vector2 &b);
   10.47 +inline Vector2 operator -=(const Vector2 &a, const Vector2 &b);
   10.48 +inline Vector2 operator *=(const Vector2 &a, const Vector2 &b);
   10.49 +inline Vector2 operator *=(const Vector2 &v, float s);
   10.50 +inline Vector2 operator /=(const Vector2 &a, const Vector2 &b);
   10.51 +inline Vector2 operator /=(const Vector2 &v, float s);
   10.52 +
   10.53 +inline float dot(const Vector2 &a, const Vector2 &b);
   10.54 +
   10.55 +inline float length(const Vector2 &v);
   10.56 +inline float length_sq(const Vector2 &v);
   10.57 +
   10.58 +inline Vector2 normalize(const Vector2 &v);
   10.59 +
   10.60 +
   10.61 +class Vector3 {
   10.62 +public:
   10.63 +	float x, y, z;
   10.64 +
   10.65 +	inline Vector3();
   10.66 +	inline Vector3(float x, float y, float z);
   10.67 +	explicit inline Vector3(float *v);
   10.68 +	explicit inline Vector3(float scalar);
   10.69 +	explicit inline Vector3(const Vector2 &v);
   10.70 +	explicit inline Vector3(const Vector4 &v);
   10.71 +
   10.72 +	inline float &operator [](int idx);
   10.73 +	inline const float &operator [](int idx) const;
   10.74 +
   10.75 +	operator float* ();
   10.76 +	operator const float* () const;
   10.77 +};
   10.78 +
   10.79 +inline Vector3 operator -(const Vector3 &v);
   10.80 +
   10.81 +// Vector op Vector
   10.82 +inline Vector3 operator +(const Vector3 &a, const Vector3 &b);
   10.83 +inline Vector3 operator -(const Vector3 &a, const Vector3 &b);
   10.84 +inline Vector3 operator *(const Vector3 &a, const Vector3 &b);
   10.85 +inline Vector3 operator /(const Vector3 &a, const Vector3 &b);
   10.86 +
   10.87 +// binary op between Vector and scalar
   10.88 +inline Vector3 operator +(const Vector3 &v, float s);
   10.89 +inline Vector3 operator +(float s, const Vector3 &v);
   10.90 +inline Vector3 operator -(const Vector3 &v, float s);
   10.91 +inline Vector3 operator -(float s, const Vector3 &v);
   10.92 +inline Vector3 operator *(const Vector3 &v, float s);
   10.93 +inline Vector3 operator *(float s, const Vector3 &v);
   10.94 +inline Vector3 operator /(const Vector3 &v, float s);
   10.95 +inline Vector3 operator /(float s, const Vector3 &v);
   10.96 +
   10.97 +// binary op between Vector and Matrix
   10.98 +inline Vector3 operator *(const Vector3 &v, const Matrix3x3 &m);
   10.99 +inline Vector3 operator *(const Vector3 &v, const Matrix4x4 &m);
  10.100 +inline Vector3 operator *(const Matrix3x3 &m, const Vector3 &v);
  10.101 +inline Vector3 operator *(const Matrix4x4 &m, const Vector3 &v);
  10.102 +
  10.103 +inline Vector3 operator +=(const Vector3 &a, const Vector3 &b);
  10.104 +inline Vector3 operator -=(const Vector3 &a, const Vector3 &b);
  10.105 +inline Vector3 operator *=(const Vector3 &a, const Vector3 &b);
  10.106 +inline Vector3 operator /=(const Vector3 &a, const Vector3 &b);
  10.107 +
  10.108 +inline Vector3 operator +=(const Vector3 &v, float s);
  10.109 +inline Vector3 operator -=(const Vector3 &v, float s);
  10.110 +inline Vector3 operator *=(const Vector3 &v, float s);
  10.111 +inline Vector3 operator /=(const Vector3 &v, float s);
  10.112 +
  10.113 +inline Vector3 operator *=(const Vector3 &v, const Matrix3x3 &m);
  10.114 +inline Vector3 operator *=(const Vector3 &v, const Matrix4x4 &m);
  10.115 +
  10.116 +inline float dot(const Vector3 &a, const Vector3 &b);
  10.117 +inline Vector3 cross(const Vector3 &a, const Vector3 &b);
  10.118 +
  10.119 +inline float length(const Vector3 &v);
  10.120 +inline float length_sq(const Vector3 &v);
  10.121 +
  10.122 +inline Vector3 normalize(const Vector3 &v);
  10.123 +
  10.124 +// equivalent to mat * vec
  10.125 +inline Vector3 transform(const Vector3 &v, const Matrix3x3 &m);
  10.126 +inline Vector3 transform(const Vector3 &v, const Matrix4x4 &m);
  10.127 +inline Vector3 transform(const Vector3 &v, const Quat &q);
  10.128 +
  10.129 +
  10.130 +class Vector4 {
  10.131 +public:
  10.132 +	float x, y, z, w;
  10.133 +
  10.134 +	inline Vector4();
  10.135 +	inline Vector4(float x, float y, float z, float w = 1.0f);
  10.136 +	explicit inline Vector4(float *v);
  10.137 +	explicit inline Vector4(float scalar);
  10.138 +	explicit inline Vector4(const Vector2 &v);
  10.139 +	explicit inline Vector4(const Vector3 &v);
  10.140 +
  10.141 +	inline float &operator [](int idx);
  10.142 +	inline const float &operator [](int idx) const;
  10.143 +
  10.144 +	operator float* ();
  10.145 +	operator const float* () const;
  10.146 +};
  10.147 +
  10.148 +inline Vector4 operator -(const Vector4 &v);
  10.149 +
  10.150 +// Vector op Vector
  10.151 +inline Vector4 operator +(const Vector4 &a, const Vector4 &b);
  10.152 +inline Vector4 operator -(const Vector4 &a, const Vector4 &b);
  10.153 +inline Vector4 operator *(const Vector4 &a, const Vector4 &b);
  10.154 +inline Vector4 operator /(const Vector4 &a, const Vector4 &b);
  10.155 +
  10.156 +// binary op between Vector and scalar
  10.157 +inline Vector4 operator +(const Vector4 &v, float s);
  10.158 +inline Vector4 operator +(float s, const Vector4 &v);
  10.159 +inline Vector4 operator -(const Vector4 &v, float s);
  10.160 +inline Vector4 operator -(float s, const Vector4 &v);
  10.161 +inline Vector4 operator *(const Vector4 &v, float s);
  10.162 +inline Vector4 operator *(float s, const Vector4 &v);
  10.163 +inline Vector4 operator /(const Vector4 &v, float s);
  10.164 +inline Vector4 operator /(float s, const Vector4 &v);
  10.165 +
  10.166 +// binary op between Vector and Matrix
  10.167 +inline Vector4 operator *(const Vector4 &v, const Matrix3x3 &m);
  10.168 +inline Vector4 operator *(const Vector4 &v, const Matrix4x4 &m);
  10.169 +inline Vector4 operator *(const Matrix3x3 &m, const Vector4 &v);
  10.170 +inline Vector4 operator *(const Matrix4x4 &m, const Vector4 &v);
  10.171 +
  10.172 +inline Vector4 operator +=(const Vector4 &a, const Vector4 &b);
  10.173 +inline Vector4 operator -=(const Vector4 &a, const Vector4 &b);
  10.174 +inline Vector4 operator *=(const Vector4 &a, const Vector4 &b);
  10.175 +inline Vector4 operator /=(const Vector4 &a, const Vector4 &b);
  10.176 +
  10.177 +inline Vector4 operator +=(const Vector4 &v, float s);
  10.178 +inline Vector4 operator -=(const Vector4 &v, float s);
  10.179 +inline Vector4 operator *=(const Vector4 &v, float s);
  10.180 +inline Vector4 operator /=(const Vector4 &v, float s);
  10.181 +
  10.182 +inline Vector4 operator *=(const Vector4 &v, const Matrix3x3 &m);
  10.183 +inline Vector4 operator *=(const Vector4 &v, const Matrix4x4 &m);
  10.184 +
  10.185 +inline float dot(const Vector4 &a, const Vector4 &b);
  10.186 +inline Vector4 cross(const Vector4 &a, const Vector4 &b, const Vector4 &c);
  10.187 +
  10.188 +inline float length(const Vector4 &v);
  10.189 +inline float length_sq(const Vector4 &v);
  10.190 +
  10.191 +inline Vector4 normalize(const Vector4 &v);
  10.192 +
  10.193 +// equivalent to mat * vec
  10.194 +inline Vector4 transform(const Vector4 &v, const Matrix3x3 &m);
  10.195 +inline Vector4 transform(const Vector4 &v, const Matrix4x4 &m);
  10.196 +inline Vector4 transform(const Vector4 &v, const Quat &q);
  10.197 +
  10.198 +#include "vector.inl"
  10.199 +
  10.200 +}	// namespace vmath
  10.201 +
  10.202 +#endif	// VMATH4_VECTOR_H_
    11.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
    11.2 +++ b/src/vmath.h	Sun Oct 05 04:00:05 2014 +0300
    11.3 @@ -0,0 +1,10 @@
    11.4 +#ifndef VMATH4_H_
    11.5 +#define VMATH4_H_
    11.6 +
    11.7 +#include "vector.h"
    11.8 +#include "matrix.h"
    11.9 +#include "quat.h"
   11.10 +//#include "numalg.h"
   11.11 +//#include "geom.h"
   11.12 +
   11.13 +#endif	/* VMATH4_H_ */