graphene

changeset 4:d30e24132b6e

more gmath
author John Tsiombikas <nuclear@member.fsf.org>
date Sat, 25 Jul 2015 07:42:30 +0300
parents d71b4e899e08
children 2ce58d5309f0
files src/gmath/matrix.h src/gmath/vector.cc src/gmath/vector.h src/gmath/vector.inl
diffstat 4 files changed, 272 insertions(+), 156 deletions(-) [+]
line diff
     1.1 --- a/src/gmath/matrix.h	Sat Jul 25 05:52:39 2015 +0300
     1.2 +++ b/src/gmath/matrix.h	Sat Jul 25 07:42:30 2015 +0300
     1.3 @@ -2,7 +2,7 @@
     1.4  #define GMATH_MATRIX_H_
     1.5  
     1.6  #include <string.h>
     1.7 -#include "vec.h"
     1.8 +#include "vector.h"
     1.9  
    1.10  namespace gph {
    1.11  
     2.1 --- a/src/gmath/vector.cc	Sat Jul 25 05:52:39 2015 +0300
     2.2 +++ b/src/gmath/vector.cc	Sat Jul 25 07:42:30 2015 +0300
     2.3 @@ -1,4 +1,5 @@
     2.4 -#include "vec.h"
     2.5 +#include "vector.h"
     2.6 +#include "matrix.h"
     2.7  
     2.8  namespace gph {
     2.9  
    2.10 @@ -7,6 +8,22 @@
    2.11  {
    2.12  }
    2.13  
    2.14 +Vector3 operator *(const Vector3 &v, const Matrix4x4 &m)
    2.15 +{
    2.16 +	float x = v.x * m[0][0] + v.y * m[1][0] + v.z * m[2][0] + m[3][0];
    2.17 +	float y = v.x * m[0][1] + v.y * m[1][1] + v.z * m[2][1] + m[3][1];
    2.18 +	float z = v.x * m[0][2] + v.y * m[1][2] + v.z * m[2][2] + m[3][2];
    2.19 +	return Vector3(x, y, z);
    2.20 +}
    2.21 +
    2.22 +Vector3 operator *(const Matrix4x4 &m, const Vector3 &v)
    2.23 +{
    2.24 +	float x = m[0][0] * v.x + m[0][1] * v.y + m[0][2] * v.z + m[0][3];
    2.25 +	float y = m[1][0] * v.x + m[1][1] * v.y + m[1][2] * v.z + m[1][3];
    2.26 +	float z = m[2][0] * v.x + m[2][1] * v.y + m[2][2] * v.z + m[2][3];
    2.27 +	return Vector3(x, y, z);
    2.28 +}
    2.29 +
    2.30  Vector4::Vector4(const Vector3 &v)
    2.31  	: x(v.x), y(v.y), z(v.z), w(1.0f)
    2.32  {
     3.1 --- a/src/gmath/vector.h	Sat Jul 25 05:52:39 2015 +0300
     3.2 +++ b/src/gmath/vector.h	Sat Jul 25 07:42:30 2015 +0300
     3.3 @@ -6,6 +6,7 @@
     3.4  namespace gph {
     3.5  
     3.6  class Vector4;
     3.7 +class Matrix4x4;
     3.8  
     3.9  class Vector3 {
    3.10  public:
    3.11 @@ -15,25 +16,9 @@
    3.12  	Vector3(float x_, float y_, float z_) : x(x_), y(y_), z(z_) {}
    3.13  	Vector3(const Vector4 &v);
    3.14  
    3.15 -	inline void normalize()
    3.16 -	{
    3.17 -		float len = (float)sqrt(x * x + y * y + z * z);
    3.18 -		if(len != 0.0f) {
    3.19 -			x /= len;
    3.20 -			y /= len;
    3.21 -			z /= len;
    3.22 -		}
    3.23 -	}
    3.24 -
    3.25 -	inline float &operator[] (int idx)
    3.26 -	{
    3.27 -		return idx == 0 ? x : (idx == 1 ? y : z);
    3.28 -	}
    3.29 -
    3.30 -	inline const float &operator[] (int idx) const
    3.31 -	{
    3.32 -		return idx == 0 ? x : (idx == 1 ? y : z);
    3.33 -	}
    3.34 +	inline void normalize();
    3.35 +	inline float &operator[] (int idx);
    3.36 +	inline const float &operator[] (int idx) const;
    3.37  };
    3.38  
    3.39  
    3.40 @@ -45,149 +30,46 @@
    3.41  	Vector4(float x_, float y_, float z_, float w_) : x(x_), y(y_), z(z_), w(w_) {}
    3.42  	Vector4(const Vector3 &v);
    3.43  
    3.44 -	inline void normalize()
    3.45 -	{
    3.46 -		float len = (float)sqrt(x * x + y * y + z * z + w * w);
    3.47 -		if(len != 0.0f) {
    3.48 -			x /= len;
    3.49 -			y /= len;
    3.50 -			z /= len;
    3.51 -			w /= len;
    3.52 -		}
    3.53 -	}
    3.54 -
    3.55 -	inline float &operator[] (int idx)
    3.56 -	{
    3.57 -		return idx == 0 ? x : (idx == 1 ? y : (idx == 2 ? z : w));
    3.58 -	}
    3.59 -
    3.60 -	inline const float &operator[] (int idx) const
    3.61 -	{
    3.62 -		return idx == 0 ? x : (idx == 1 ? y : (idx == 2 ? z : w));
    3.63 -	}
    3.64 +	inline void normalize();
    3.65 +	inline float &operator[] (int idx);
    3.66 +	inline const float &operator[] (int idx) const;
    3.67  };
    3.68  
    3.69  // ---- Vector3 functions ----
    3.70 +inline Vector3 operator +(const Vector3 &a, const Vector3 &b);
    3.71 +inline Vector3 operator -(const Vector3 &a, const Vector3 &b);
    3.72 +inline Vector3 operator *(const Vector3 &a, const Vector3 &b);
    3.73 +inline Vector3 operator /(const Vector3 &a, const Vector3 &b);
    3.74 +inline Vector3 operator *(const Vector3 &v, float s);
    3.75 +inline Vector3 operator *(float s, const Vector3 &v);
    3.76 +inline Vector3 operator /(const Vector3 &v, float s);
    3.77 +inline Vector3 operator /(float s, const Vector3 &v);
    3.78 +inline Vector3 &operator +=(Vector3 &a, const Vector3 &b);
    3.79 +inline Vector3 &operator -=(Vector3 &a, const Vector3 &b);
    3.80 +inline Vector3 &operator *=(Vector3 &a, const Vector3 &b);
    3.81 +inline Vector3 &operator /=(Vector3 &a, const Vector3 &b);
    3.82 +inline Vector3 &operator *=(Vector3 &v, float s);
    3.83 +inline Vector3 &operator /=(Vector3 &v, float s);
    3.84  
    3.85 -inline Vector3 operator +(const Vector3 &a, const Vector3 &b)
    3.86 -{
    3.87 -	return Vector3(a.x + b.x, a.y + b.y, a.z + b.z);
    3.88 -}
    3.89 +Vector3 operator *(const Vector3 &v, const Matrix4x4 &m);
    3.90 +Vector3 operator *(const Matrix4x4 &m, const Vector3 &v);
    3.91  
    3.92 -inline Vector3 operator -(const Vector3 &a, const Vector3 &b)
    3.93 -{
    3.94 -	return Vector3(a.x - b.x, a.y - b.y, a.z - b.z);
    3.95 -}
    3.96 +inline bool operator ==(const Vector3 &a, const Vector3 &b);
    3.97 +inline bool operator !=(const Vector3 &a, const Vector3 &b);
    3.98  
    3.99 -inline Vector3 operator *(const Vector3 &a, const Vector3 &b)
   3.100 -{
   3.101 -	return Vector3(a.x * b.x, a.y * b.y, a.z * b.z);
   3.102 -}
   3.103 +inline float dot(const Vector3 &a, const Vector3 &b);
   3.104 +inline Vector3 cross(const Vector3 &a, const Vector3 &b);
   3.105 +inline float length(const Vector3 &v);
   3.106 +inline float length_sq(const Vector3 &v);
   3.107 +inline Vector3 normalize(const Vector3 &v);
   3.108  
   3.109 -inline Vector3 operator /(const Vector3 &a, const Vector3 &b)
   3.110 -{
   3.111 -	return Vector3(a.x / b.x, a.y / b.y, a.z / b.z);
   3.112 -}
   3.113 +inline Vector3 reflect(const Vector3 &v, const Vector3 &n);
   3.114 +inline Vector3 refract(const Vector3 &v, const Vector3 &n, float ior);
   3.115 +inline Vector3 refract(const Vector3 &v, const Vector3 &n, float from_ior, float to_ior);
   3.116  
   3.117 -inline Vector3 operator *(const Vector3 &v, float s)
   3.118 -{
   3.119 -	return Vector3(v.x * s, v.y * s, v.z * s);
   3.120 -}
   3.121 -
   3.122 -inline Vector3 operator *(float s, const Vector3 &v)
   3.123 -{
   3.124 -	return Vector3(s * v.x, s * v.y, s * v.z);
   3.125 -}
   3.126 -
   3.127 -inline Vector3 operator /(const Vector3 &v, float s)
   3.128 -{
   3.129 -	return Vector3(v.x / s, v.y / s, v.z / s);
   3.130 -}
   3.131 -
   3.132 -inline Vector3 operator /(float s, const Vector3 &v)
   3.133 -{
   3.134 -	return Vector3(s / v.x, s / v.y, s / v.z);
   3.135 -}
   3.136 -
   3.137 -inline Vector3 &operator +=(Vector3 &a, const Vector3 &b)
   3.138 -{
   3.139 -	a.x += b.x;
   3.140 -	a.y += b.y;
   3.141 -	a.z += b.z;
   3.142 -	return a;
   3.143 -}
   3.144 -
   3.145 -inline Vector3 &operator -=(Vector3 &a, const Vector3 &b)
   3.146 -{
   3.147 -	a.x -= b.x;
   3.148 -	a.y -= b.y;
   3.149 -	a.z -= b.z;
   3.150 -	return a;
   3.151 -}
   3.152 -
   3.153 -inline Vector3 &operator *=(Vector3 &a, const Vector3 &b)
   3.154 -{
   3.155 -	a.x *= b.x;
   3.156 -	a.y *= b.y;
   3.157 -	a.z *= b.z;
   3.158 -	return a;
   3.159 -}
   3.160 -
   3.161 -inline Vector3 &operator /=(Vector3 &a, const Vector3 &b)
   3.162 -{
   3.163 -	a.x /= b.x;
   3.164 -	a.y /= b.y;
   3.165 -	a.z /= b.z;
   3.166 -	return a;
   3.167 -}
   3.168 -
   3.169 -inline Vector3 &operator *=(Vector3 &v, float s)
   3.170 -{
   3.171 -	v.x *= s;
   3.172 -	v.y *= s;
   3.173 -	v.z *= s;
   3.174 -	return v;
   3.175 -}
   3.176 -
   3.177 -inline Vector3 &operator /=(Vector3 &v, float s)
   3.178 -{
   3.179 -	v.x /= s;
   3.180 -	v.y /= s;
   3.181 -	v.z /= s;
   3.182 -	return v;
   3.183 -}
   3.184 -
   3.185 -inline float dot(const Vector3 &a, const Vector3 &b)
   3.186 -{
   3.187 -	return a.x * b.x + a.y * b.y + a.z * b.z;
   3.188 -}
   3.189 -
   3.190 -inline Vector3 cross(const Vector3 &a, const Vector3 &b)
   3.191 -{
   3.192 -	return Vector3(a.y * b.z - a.z * b.y,
   3.193 -			a.z * b.x - a.x * b.z,
   3.194 -			a.x * b.y - a.y * b.x);
   3.195 -}
   3.196 -
   3.197 -inline float length(const Vector3 &v)
   3.198 -{
   3.199 -	return (float)sqrt(v.x * v.x + v.y * v.y + v.z * v.z);
   3.200 -}
   3.201 -
   3.202 -inline float length_sq(const Vector3 &v)
   3.203 -{
   3.204 -	return v.x * v.x + v.y * v.y + v.z * v.z;
   3.205 -}
   3.206 -
   3.207 -inline Vector3 normalize(const Vector3 &v)
   3.208 -{
   3.209 -	float len = length(v);
   3.210 -	if(len == 0.0f) {
   3.211 -		return v;
   3.212 -	}
   3.213 -
   3.214 -	return Vector3(v.x / len, v.y / len, v.z / len);
   3.215 -}
   3.216 +inline Vector3 distance(const Vector3 &a, const Vector3 &b);
   3.217 +inline Vector3 distance_sq(const Vector3 &a, const Vector3 &b);
   3.218 +inline Vector3 faceforward(const Vector3 &n, const Vector3 &vi, const Vector3 &ng);
   3.219  
   3.220  }
   3.221  
     4.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     4.2 +++ b/src/gmath/vector.inl	Sat Jul 25 07:42:30 2015 +0300
     4.3 @@ -0,0 +1,217 @@
     4.4 +#include <math.h>
     4.5 +
     4.6 +namespace gph {
     4.7 +
     4.8 +// ---- Vector3 ----
     4.9 +
    4.10 +inline void Vector3::normalize()
    4.11 +{
    4.12 +	float len = (float)sqrt(x * x + y * y + z * z);
    4.13 +	if(len != 0.0f) {
    4.14 +		x /= len;
    4.15 +		y /= len;
    4.16 +		z /= len;
    4.17 +	}
    4.18 +}
    4.19 +
    4.20 +inline float &Vector3::operator[] (int idx)
    4.21 +{
    4.22 +	return idx == 0 ? x : (idx == 1 ? y : z);
    4.23 +}
    4.24 +
    4.25 +inline const float &Vector3::operator[] (int idx) const
    4.26 +{
    4.27 +	return idx == 0 ? x : (idx == 1 ? y : z);
    4.28 +}
    4.29 +
    4.30 +inline Vector3 operator +(const Vector3 &a, const Vector3 &b)
    4.31 +{
    4.32 +	return Vector3(a.x + b.x, a.y + b.y, a.z + b.z);
    4.33 +}
    4.34 +
    4.35 +inline Vector3 operator -(const Vector3 &a, const Vector3 &b)
    4.36 +{
    4.37 +	return Vector3(a.x - b.x, a.y - b.y, a.z - b.z);
    4.38 +}
    4.39 +
    4.40 +inline Vector3 operator *(const Vector3 &a, const Vector3 &b)
    4.41 +{
    4.42 +	return Vector3(a.x * b.x, a.y * b.y, a.z * b.z);
    4.43 +}
    4.44 +
    4.45 +inline Vector3 operator /(const Vector3 &a, const Vector3 &b)
    4.46 +{
    4.47 +	return Vector3(a.x / b.x, a.y / b.y, a.z / b.z);
    4.48 +}
    4.49 +
    4.50 +inline Vector3 operator *(const Vector3 &v, float s)
    4.51 +{
    4.52 +	return Vector3(v.x * s, v.y * s, v.z * s);
    4.53 +}
    4.54 +
    4.55 +inline Vector3 operator *(float s, const Vector3 &v)
    4.56 +{
    4.57 +	return Vector3(s * v.x, s * v.y, s * v.z);
    4.58 +}
    4.59 +
    4.60 +inline Vector3 operator /(const Vector3 &v, float s)
    4.61 +{
    4.62 +	return Vector3(v.x / s, v.y / s, v.z / s);
    4.63 +}
    4.64 +
    4.65 +inline Vector3 operator /(float s, const Vector3 &v)
    4.66 +{
    4.67 +	return Vector3(s / v.x, s / v.y, s / v.z);
    4.68 +}
    4.69 +
    4.70 +inline Vector3 &operator +=(Vector3 &a, const Vector3 &b)
    4.71 +{
    4.72 +	a.x += b.x;
    4.73 +	a.y += b.y;
    4.74 +	a.z += b.z;
    4.75 +	return a;
    4.76 +}
    4.77 +
    4.78 +inline Vector3 &operator -=(Vector3 &a, const Vector3 &b)
    4.79 +{
    4.80 +	a.x -= b.x;
    4.81 +	a.y -= b.y;
    4.82 +	a.z -= b.z;
    4.83 +	return a;
    4.84 +}
    4.85 +
    4.86 +inline Vector3 &operator *=(Vector3 &a, const Vector3 &b)
    4.87 +{
    4.88 +	a.x *= b.x;
    4.89 +	a.y *= b.y;
    4.90 +	a.z *= b.z;
    4.91 +	return a;
    4.92 +}
    4.93 +
    4.94 +inline Vector3 &operator /=(Vector3 &a, const Vector3 &b)
    4.95 +{
    4.96 +	a.x /= b.x;
    4.97 +	a.y /= b.y;
    4.98 +	a.z /= b.z;
    4.99 +	return a;
   4.100 +}
   4.101 +
   4.102 +inline Vector3 &operator *=(Vector3 &v, float s)
   4.103 +{
   4.104 +	v.x *= s;
   4.105 +	v.y *= s;
   4.106 +	v.z *= s;
   4.107 +	return v;
   4.108 +}
   4.109 +
   4.110 +inline Vector3 &operator /=(Vector3 &v, float s)
   4.111 +{
   4.112 +	v.x /= s;
   4.113 +	v.y /= s;
   4.114 +	v.z /= s;
   4.115 +	return v;
   4.116 +}
   4.117 +
   4.118 +inline bool operator ==(const Vector3 &a, const Vector3 &b)
   4.119 +{
   4.120 +	return a.x == b.x && a.y == b.y && a.z == b.z;
   4.121 +}
   4.122 +
   4.123 +inline bool operator !=(const Vector3 &a, const Vector3 &b)
   4.124 +{
   4.125 +	return !(a == b);
   4.126 +}
   4.127 +
   4.128 +inline float dot(const Vector3 &a, const Vector3 &b)
   4.129 +{
   4.130 +	return a.x * b.x + a.y * b.y + a.z * b.z;
   4.131 +}
   4.132 +
   4.133 +inline Vector3 cross(const Vector3 &a, const Vector3 &b)
   4.134 +{
   4.135 +	return Vector3(a.y * b.z - a.z * b.y,
   4.136 +			a.z * b.x - a.x * b.z,
   4.137 +			a.x * b.y - a.y * b.x);
   4.138 +}
   4.139 +
   4.140 +inline float length(const Vector3 &v)
   4.141 +{
   4.142 +	return (float)sqrt(v.x * v.x + v.y * v.y + v.z * v.z);
   4.143 +}
   4.144 +
   4.145 +inline float length_sq(const Vector3 &v)
   4.146 +{
   4.147 +	return v.x * v.x + v.y * v.y + v.z * v.z;
   4.148 +}
   4.149 +
   4.150 +inline Vector3 normalize(const Vector3 &v)
   4.151 +{
   4.152 +	float len = length(v);
   4.153 +	if(len == 0.0f) {
   4.154 +		return v;
   4.155 +	}
   4.156 +
   4.157 +	return Vector3(v.x / len, v.y / len, v.z / len);
   4.158 +}
   4.159 +
   4.160 +inline Vector3 reflect(const Vector3 &v, const Vector3 &n)
   4.161 +{
   4.162 +	return v - n * dot(n, v) * 2.0;
   4.163 +}
   4.164 +
   4.165 +inline Vector3 refract(const Vector3 &v, const Vector3 &n, float ior)
   4.166 +{
   4.167 +	float ndotv = dot(n, v);
   4.168 +	float k = 1.0f - ior * ior * (1.0f - ndotv * ndotv);
   4.169 +	if(k < 0.0f) {
   4.170 +		return Vector3();
   4.171 +	}
   4.172 +	return ior * v - (ior * ndotv + sqrt(k)) * n;
   4.173 +}
   4.174 +
   4.175 +inline Vector3 refract(const Vector3 &v, const Vector3 &n, float from_ior, float to_ior)
   4.176 +{
   4.177 +	if(to_ior == 0.0f) to_ior = 1.0f;
   4.178 +	return refract(v, n, from_ior / to_ior);
   4.179 +}
   4.180 +
   4.181 +inline Vector3 distance(const Vector3 &a, const Vector3 &b)
   4.182 +{
   4.183 +	return length(a - b);
   4.184 +}
   4.185 +
   4.186 +inline Vector3 distance_sq(const Vector3 &a, const Vector3 &b)
   4.187 +{
   4.188 +	return length_sq(a - b);
   4.189 +}
   4.190 +
   4.191 +inline Vector3 faceforward(const Vector3 &n, const Vector3 &vi, const Vector3 &ng)
   4.192 +{
   4.193 +	return dot(ng, i) < 0.0f ? n : -n;
   4.194 +}
   4.195 +
   4.196 +// ---- Vector4 ----
   4.197 +
   4.198 +
   4.199 +inline void Vector4::normalize()
   4.200 +{
   4.201 +	float len = (float)sqrt(x * x + y * y + z * z + w * w);
   4.202 +	if(len != 0.0f) {
   4.203 +		x /= len;
   4.204 +		y /= len;
   4.205 +		z /= len;
   4.206 +		w /= len;
   4.207 +	}
   4.208 +}
   4.209 +
   4.210 +inline float &Vector4::operator[] (int idx)
   4.211 +{
   4.212 +	return idx == 0 ? x : (idx == 1 ? y : (idx == 2 ? z : w));
   4.213 +}
   4.214 +
   4.215 +inline const float &Vector4::operator[] (int idx) const
   4.216 +{
   4.217 +	return idx == 0 ? x : (idx == 1 ? y : (idx == 2 ? z : w));
   4.218 +}
   4.219 +
   4.220 +}	// namespace gph