graphene

diff src/gmath/vector.inl @ 4:d30e24132b6e

more gmath
author John Tsiombikas <nuclear@member.fsf.org>
date Sat, 25 Jul 2015 07:42:30 +0300
parents
children 2ce58d5309f0
line diff
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/src/gmath/vector.inl	Sat Jul 25 07:42:30 2015 +0300
     1.3 @@ -0,0 +1,217 @@
     1.4 +#include <math.h>
     1.5 +
     1.6 +namespace gph {
     1.7 +
     1.8 +// ---- Vector3 ----
     1.9 +
    1.10 +inline void Vector3::normalize()
    1.11 +{
    1.12 +	float len = (float)sqrt(x * x + y * y + z * z);
    1.13 +	if(len != 0.0f) {
    1.14 +		x /= len;
    1.15 +		y /= len;
    1.16 +		z /= len;
    1.17 +	}
    1.18 +}
    1.19 +
    1.20 +inline float &Vector3::operator[] (int idx)
    1.21 +{
    1.22 +	return idx == 0 ? x : (idx == 1 ? y : z);
    1.23 +}
    1.24 +
    1.25 +inline const float &Vector3::operator[] (int idx) const
    1.26 +{
    1.27 +	return idx == 0 ? x : (idx == 1 ? y : z);
    1.28 +}
    1.29 +
    1.30 +inline Vector3 operator +(const Vector3 &a, const Vector3 &b)
    1.31 +{
    1.32 +	return Vector3(a.x + b.x, a.y + b.y, a.z + b.z);
    1.33 +}
    1.34 +
    1.35 +inline Vector3 operator -(const Vector3 &a, const Vector3 &b)
    1.36 +{
    1.37 +	return Vector3(a.x - b.x, a.y - b.y, a.z - b.z);
    1.38 +}
    1.39 +
    1.40 +inline Vector3 operator *(const Vector3 &a, const Vector3 &b)
    1.41 +{
    1.42 +	return Vector3(a.x * b.x, a.y * b.y, a.z * b.z);
    1.43 +}
    1.44 +
    1.45 +inline Vector3 operator /(const Vector3 &a, const Vector3 &b)
    1.46 +{
    1.47 +	return Vector3(a.x / b.x, a.y / b.y, a.z / b.z);
    1.48 +}
    1.49 +
    1.50 +inline Vector3 operator *(const Vector3 &v, float s)
    1.51 +{
    1.52 +	return Vector3(v.x * s, v.y * s, v.z * s);
    1.53 +}
    1.54 +
    1.55 +inline Vector3 operator *(float s, const Vector3 &v)
    1.56 +{
    1.57 +	return Vector3(s * v.x, s * v.y, s * v.z);
    1.58 +}
    1.59 +
    1.60 +inline Vector3 operator /(const Vector3 &v, float s)
    1.61 +{
    1.62 +	return Vector3(v.x / s, v.y / s, v.z / s);
    1.63 +}
    1.64 +
    1.65 +inline Vector3 operator /(float s, const Vector3 &v)
    1.66 +{
    1.67 +	return Vector3(s / v.x, s / v.y, s / v.z);
    1.68 +}
    1.69 +
    1.70 +inline Vector3 &operator +=(Vector3 &a, const Vector3 &b)
    1.71 +{
    1.72 +	a.x += b.x;
    1.73 +	a.y += b.y;
    1.74 +	a.z += b.z;
    1.75 +	return a;
    1.76 +}
    1.77 +
    1.78 +inline Vector3 &operator -=(Vector3 &a, const Vector3 &b)
    1.79 +{
    1.80 +	a.x -= b.x;
    1.81 +	a.y -= b.y;
    1.82 +	a.z -= b.z;
    1.83 +	return a;
    1.84 +}
    1.85 +
    1.86 +inline Vector3 &operator *=(Vector3 &a, const Vector3 &b)
    1.87 +{
    1.88 +	a.x *= b.x;
    1.89 +	a.y *= b.y;
    1.90 +	a.z *= b.z;
    1.91 +	return a;
    1.92 +}
    1.93 +
    1.94 +inline Vector3 &operator /=(Vector3 &a, const Vector3 &b)
    1.95 +{
    1.96 +	a.x /= b.x;
    1.97 +	a.y /= b.y;
    1.98 +	a.z /= b.z;
    1.99 +	return a;
   1.100 +}
   1.101 +
   1.102 +inline Vector3 &operator *=(Vector3 &v, float s)
   1.103 +{
   1.104 +	v.x *= s;
   1.105 +	v.y *= s;
   1.106 +	v.z *= s;
   1.107 +	return v;
   1.108 +}
   1.109 +
   1.110 +inline Vector3 &operator /=(Vector3 &v, float s)
   1.111 +{
   1.112 +	v.x /= s;
   1.113 +	v.y /= s;
   1.114 +	v.z /= s;
   1.115 +	return v;
   1.116 +}
   1.117 +
   1.118 +inline bool operator ==(const Vector3 &a, const Vector3 &b)
   1.119 +{
   1.120 +	return a.x == b.x && a.y == b.y && a.z == b.z;
   1.121 +}
   1.122 +
   1.123 +inline bool operator !=(const Vector3 &a, const Vector3 &b)
   1.124 +{
   1.125 +	return !(a == b);
   1.126 +}
   1.127 +
   1.128 +inline float dot(const Vector3 &a, const Vector3 &b)
   1.129 +{
   1.130 +	return a.x * b.x + a.y * b.y + a.z * b.z;
   1.131 +}
   1.132 +
   1.133 +inline Vector3 cross(const Vector3 &a, const Vector3 &b)
   1.134 +{
   1.135 +	return Vector3(a.y * b.z - a.z * b.y,
   1.136 +			a.z * b.x - a.x * b.z,
   1.137 +			a.x * b.y - a.y * b.x);
   1.138 +}
   1.139 +
   1.140 +inline float length(const Vector3 &v)
   1.141 +{
   1.142 +	return (float)sqrt(v.x * v.x + v.y * v.y + v.z * v.z);
   1.143 +}
   1.144 +
   1.145 +inline float length_sq(const Vector3 &v)
   1.146 +{
   1.147 +	return v.x * v.x + v.y * v.y + v.z * v.z;
   1.148 +}
   1.149 +
   1.150 +inline Vector3 normalize(const Vector3 &v)
   1.151 +{
   1.152 +	float len = length(v);
   1.153 +	if(len == 0.0f) {
   1.154 +		return v;
   1.155 +	}
   1.156 +
   1.157 +	return Vector3(v.x / len, v.y / len, v.z / len);
   1.158 +}
   1.159 +
   1.160 +inline Vector3 reflect(const Vector3 &v, const Vector3 &n)
   1.161 +{
   1.162 +	return v - n * dot(n, v) * 2.0;
   1.163 +}
   1.164 +
   1.165 +inline Vector3 refract(const Vector3 &v, const Vector3 &n, float ior)
   1.166 +{
   1.167 +	float ndotv = dot(n, v);
   1.168 +	float k = 1.0f - ior * ior * (1.0f - ndotv * ndotv);
   1.169 +	if(k < 0.0f) {
   1.170 +		return Vector3();
   1.171 +	}
   1.172 +	return ior * v - (ior * ndotv + sqrt(k)) * n;
   1.173 +}
   1.174 +
   1.175 +inline Vector3 refract(const Vector3 &v, const Vector3 &n, float from_ior, float to_ior)
   1.176 +{
   1.177 +	if(to_ior == 0.0f) to_ior = 1.0f;
   1.178 +	return refract(v, n, from_ior / to_ior);
   1.179 +}
   1.180 +
   1.181 +inline Vector3 distance(const Vector3 &a, const Vector3 &b)
   1.182 +{
   1.183 +	return length(a - b);
   1.184 +}
   1.185 +
   1.186 +inline Vector3 distance_sq(const Vector3 &a, const Vector3 &b)
   1.187 +{
   1.188 +	return length_sq(a - b);
   1.189 +}
   1.190 +
   1.191 +inline Vector3 faceforward(const Vector3 &n, const Vector3 &vi, const Vector3 &ng)
   1.192 +{
   1.193 +	return dot(ng, i) < 0.0f ? n : -n;
   1.194 +}
   1.195 +
   1.196 +// ---- Vector4 ----
   1.197 +
   1.198 +
   1.199 +inline void Vector4::normalize()
   1.200 +{
   1.201 +	float len = (float)sqrt(x * x + y * y + z * z + w * w);
   1.202 +	if(len != 0.0f) {
   1.203 +		x /= len;
   1.204 +		y /= len;
   1.205 +		z /= len;
   1.206 +		w /= len;
   1.207 +	}
   1.208 +}
   1.209 +
   1.210 +inline float &Vector4::operator[] (int idx)
   1.211 +{
   1.212 +	return idx == 0 ? x : (idx == 1 ? y : (idx == 2 ? z : w));
   1.213 +}
   1.214 +
   1.215 +inline const float &Vector4::operator[] (int idx) const
   1.216 +{
   1.217 +	return idx == 0 ? x : (idx == 1 ? y : (idx == 2 ? z : w));
   1.218 +}
   1.219 +
   1.220 +}	// namespace gph