graphene

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

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