1 // Copyright 2010 Dolphin Emulator Project 2 // Licensed under GPLv2+ 3 // Refer to the license.txt file included. 4 5 #pragma once 6 7 #include <cmath> 8 #include <cstdlib> 9 10 class Vec3 11 { 12 public: 13 float x, y, z; 14 Vec3()15 Vec3() {} Vec3(float f)16 explicit Vec3(float f) { x = y = z = f; } Vec3(const float * f)17 explicit Vec3(const float* f) 18 { 19 x = f[0]; 20 y = f[1]; 21 z = f[2]; 22 } 23 Vec3(const float _x,const float _y,const float _z)24 Vec3(const float _x, const float _y, const float _z) 25 { 26 x = _x; 27 y = _y; 28 z = _z; 29 } 30 set(const float _x,const float _y,const float _z)31 void set(const float _x, const float _y, const float _z) 32 { 33 x = _x; 34 y = _y; 35 z = _z; 36 } 37 38 Vec3 operator+(const Vec3& other) const { return Vec3(x + other.x, y + other.y, z + other.z); } 39 void operator+=(const Vec3& other) 40 { 41 x += other.x; 42 y += other.y; 43 z += other.z; 44 } 45 46 Vec3 operator-(const Vec3& v) const { return Vec3(x - v.x, y - v.y, z - v.z); } 47 void operator-=(const Vec3& other) 48 { 49 x -= other.x; 50 y -= other.y; 51 z -= other.z; 52 } 53 54 Vec3 operator-() const { return Vec3(-x, -y, -z); } 55 Vec3 operator*(const float f) const { return Vec3(x * f, y * f, z * f); } 56 Vec3 operator/(const float f) const 57 { 58 float invf = (1.0f / f); 59 return Vec3(x * invf, y * invf, z * invf); 60 } 61 62 void operator/=(const float f) { *this = *this / f; } 63 float operator*(const Vec3& other) const { return (x * other.x) + (y * other.y) + (z * other.z); } 64 void operator*=(const float f) { *this = *this * f; } ScaledBy(const Vec3 & other)65 Vec3 ScaledBy(const Vec3& other) const { return Vec3(x * other.x, y * other.y, z * other.z); } 66 Vec3 operator%(const Vec3& v) const 67 { 68 return Vec3((y * v.z) - (z * v.y), (z * v.x) - (x * v.z), (x * v.y) - (y * v.x)); 69 } 70 Length2()71 float Length2() const { return (x * x) + (y * y) + (z * z); } Length()72 float Length() const { return sqrtf(Length2()); } Distance2To(Vec3 & other)73 float Distance2To(Vec3& other) { return (other - (*this)).Length2(); } Normalized()74 Vec3 Normalized() const { return (*this) / Length(); } Normalize()75 void Normalize() { (*this) /= Length(); } 76 float& operator[](int i) { return *((&x) + i); } 77 float operator[](const int i) const { return *((&x) + i); } 78 bool operator==(const Vec3& other) const { return x == other.x && y == other.y && z == other.z; } SetZero()79 void SetZero() 80 { 81 x = 0.0f; 82 y = 0.0f; 83 z = 0.0f; 84 } 85 }; 86