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