| 
									
										
										
										
											2015-05-24 06:55:12 +02:00
										 |  |  | // Copyright 2010 Dolphin Emulator Project
 | 
					
						
							| 
									
										
										
										
											2015-05-18 01:08:10 +02:00
										 |  |  | // Licensed under GPLv2+
 | 
					
						
							| 
									
										
										
										
											2013-04-17 23:29:41 -04:00
										 |  |  | // Refer to the license.txt file included.
 | 
					
						
							| 
									
										
										
										
											2010-07-19 12:34:11 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-02-10 13:54:46 -05:00
										 |  |  | #pragma once
 | 
					
						
							| 
									
										
										
										
											2010-07-19 12:34:11 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-02-17 05:18:15 -05:00
										 |  |  | #include <cmath>
 | 
					
						
							|  |  |  | #include <cstdlib>
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #include "Common/ChunkFile.h"
 | 
					
						
							| 
									
										
										
										
											2010-07-19 12:34:11 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | class Vec3 | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | public: | 
					
						
							| 
									
										
										
										
											2014-08-10 21:34:34 -04:00
										 |  |  | 	float x, y, z; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	Vec3() | 
					
						
							|  |  |  | 	{ | 
					
						
							| 
									
										
										
										
											2010-07-19 12:34:11 +00:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2014-08-10 21:34:34 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	explicit Vec3(float f) | 
					
						
							|  |  |  | 	{ | 
					
						
							|  |  |  | 		x = y = z = f; | 
					
						
							| 
									
										
										
										
											2010-07-19 12:34:11 +00:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2014-08-10 21:34:34 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	explicit Vec3(const float *f) | 
					
						
							|  |  |  | 	{ | 
					
						
							|  |  |  | 		x = f[0]; | 
					
						
							|  |  |  | 		y = f[1]; | 
					
						
							|  |  |  | 		z = f[2]; | 
					
						
							| 
									
										
										
										
											2010-07-19 12:34:11 +00:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2014-08-10 21:34:34 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	Vec3(const float _x, const float _y, const float _z) | 
					
						
							|  |  |  | 	{ | 
					
						
							|  |  |  | 		x = _x; | 
					
						
							|  |  |  | 		y = _y; | 
					
						
							|  |  |  | 		z = _z; | 
					
						
							| 
									
										
										
										
											2010-07-19 12:34:11 +00:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2014-08-10 21:34:34 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	void set(const float _x, const float _y, const float _z) | 
					
						
							|  |  |  | 	{ | 
					
						
							|  |  |  | 		x = _x; | 
					
						
							|  |  |  | 		y = _y; | 
					
						
							|  |  |  | 		z = _z; | 
					
						
							| 
									
										
										
										
											2010-07-19 12:34:11 +00:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2014-08-10 21:34:34 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	Vec3 operator+(const Vec3 &other) const | 
					
						
							| 
									
										
										
										
											2010-07-19 12:34:11 +00:00
										 |  |  | 	{ | 
					
						
							| 
									
										
										
										
											2014-08-10 21:34:34 -04:00
										 |  |  | 		return Vec3(x + other.x, y + other.y, z + other.z); | 
					
						
							| 
									
										
										
										
											2010-07-19 12:34:11 +00:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2014-08-10 21:34:34 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	void operator+=(const Vec3 &other) | 
					
						
							|  |  |  | 	{ | 
					
						
							|  |  |  | 		x += other.x; | 
					
						
							|  |  |  | 		y += other.y; | 
					
						
							|  |  |  | 		z += other.z; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	Vec3 operator-(const Vec3 &v) const | 
					
						
							|  |  |  | 	{ | 
					
						
							|  |  |  | 		return Vec3(x - v.x, y - v.y, z - v.z); | 
					
						
							| 
									
										
										
										
											2010-07-19 12:34:11 +00:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-08-10 21:34:34 -04:00
										 |  |  | 	void operator-=(const Vec3 &other) | 
					
						
							|  |  |  | 	{ | 
					
						
							|  |  |  | 		x -= other.x; | 
					
						
							|  |  |  | 		y -= other.y; | 
					
						
							|  |  |  | 		z -= other.z; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	Vec3 operator-() const | 
					
						
							|  |  |  | 	{ | 
					
						
							|  |  |  | 		return Vec3(-x, -y, -z); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	Vec3 operator*(const float f) const | 
					
						
							|  |  |  | 	{ | 
					
						
							|  |  |  | 		return Vec3(x * f, y * f, z * f); | 
					
						
							| 
									
										
										
										
											2010-07-19 12:34:11 +00:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2014-08-10 21:34:34 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	Vec3 operator/(const float f) const | 
					
						
							|  |  |  | 	{ | 
					
						
							|  |  |  | 		float invf = (1.0f / f); | 
					
						
							|  |  |  | 		return Vec3(x * invf, y * invf, z * invf); | 
					
						
							| 
									
										
										
										
											2010-07-19 12:34:11 +00:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2014-08-10 21:34:34 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	void operator/=(const float f) | 
					
						
							| 
									
										
										
										
											2010-07-19 12:34:11 +00:00
										 |  |  | 	{ | 
					
						
							|  |  |  | 		*this = *this / f; | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2014-08-10 21:34:34 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	float operator*(const Vec3 &other) const | 
					
						
							|  |  |  | 	{ | 
					
						
							|  |  |  | 		return (x * other.x) + | 
					
						
							|  |  |  | 		       (y * other.y) + | 
					
						
							|  |  |  | 		       (z * other.z); | 
					
						
							| 
									
										
										
										
											2010-07-19 12:34:11 +00:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2014-08-10 21:34:34 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	void operator*=(const float f) | 
					
						
							|  |  |  | 	{ | 
					
						
							| 
									
										
										
										
											2010-07-19 12:34:11 +00:00
										 |  |  | 		*this = *this * f; | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2014-08-10 21:34:34 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	Vec3 ScaledBy(const Vec3 &other) const | 
					
						
							|  |  |  | 	{ | 
					
						
							|  |  |  | 		return Vec3(x * other.x, y * other.y, z * other.z); | 
					
						
							| 
									
										
										
										
											2010-07-19 12:34:11 +00:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-08-10 21:34:34 -04:00
										 |  |  | 	Vec3 operator%(const Vec3 &v) const | 
					
						
							|  |  |  | 	{ | 
					
						
							|  |  |  | 		return Vec3((y * v.z) - (z * v.y), | 
					
						
							|  |  |  | 		            (z * v.x) - (x * v.z), | 
					
						
							|  |  |  | 		            (x * v.y) - (y * v.x)); | 
					
						
							| 
									
										
										
										
											2013-10-29 01:23:17 -04:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2014-08-10 21:34:34 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	float Length2() const | 
					
						
							|  |  |  | 	{ | 
					
						
							|  |  |  | 		return (x * x) + (y * y) + (z * z); | 
					
						
							| 
									
										
										
										
											2010-07-19 12:34:11 +00:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2014-08-10 21:34:34 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	float Length() const | 
					
						
							|  |  |  | 	{ | 
					
						
							|  |  |  | 		return sqrtf(Length2()); | 
					
						
							| 
									
										
										
										
											2010-07-19 12:34:11 +00:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2014-08-10 21:34:34 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	float Distance2To(Vec3 &other) | 
					
						
							| 
									
										
										
										
											2010-07-19 12:34:11 +00:00
										 |  |  | 	{ | 
					
						
							| 
									
										
										
										
											2014-08-10 21:34:34 -04:00
										 |  |  | 		return (other - (*this)).Length2(); | 
					
						
							| 
									
										
										
										
											2010-07-19 12:34:11 +00:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-08-10 21:34:34 -04:00
										 |  |  | 	Vec3 Normalized() const | 
					
						
							|  |  |  | 	{ | 
					
						
							|  |  |  | 		return (*this) / Length(); | 
					
						
							| 
									
										
										
										
											2010-07-19 12:34:11 +00:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2014-08-10 21:34:34 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	void Normalize() | 
					
						
							|  |  |  | 	{ | 
					
						
							|  |  |  | 		(*this) /= Length(); | 
					
						
							| 
									
										
										
										
											2010-07-19 12:34:11 +00:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2014-08-10 21:34:34 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	float &operator[](int i) | 
					
						
							| 
									
										
										
										
											2010-07-19 12:34:11 +00:00
										 |  |  | 	{ | 
					
						
							|  |  |  | 		return *((&x) + i); | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2014-08-10 21:34:34 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	float operator[](const int i) const | 
					
						
							| 
									
										
										
										
											2010-07-19 12:34:11 +00:00
										 |  |  | 	{ | 
					
						
							|  |  |  | 		return *((&x) + i); | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2014-08-10 21:34:34 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	bool operator==(const Vec3 &other) const | 
					
						
							| 
									
										
										
										
											2010-07-19 12:34:11 +00:00
										 |  |  | 	{ | 
					
						
							| 
									
										
										
										
											2015-08-28 14:12:14 -04:00
										 |  |  | 		return x == other.x && y == other.y && z == other.z; | 
					
						
							| 
									
										
										
										
											2010-07-19 12:34:11 +00:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2014-08-10 21:34:34 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	void SetZero() | 
					
						
							| 
									
										
										
										
											2010-07-19 12:34:11 +00:00
										 |  |  | 	{ | 
					
						
							| 
									
										
										
										
											2015-08-28 14:10:07 -04:00
										 |  |  | 		x = 0.0f; | 
					
						
							|  |  |  | 		y = 0.0f; | 
					
						
							|  |  |  | 		z = 0.0f; | 
					
						
							| 
									
										
										
										
											2010-07-19 12:34:11 +00:00
										 |  |  | 	} | 
					
						
							|  |  |  | }; |