2018-02-26 15:24:21 -08:00
|
|
|
//
|
|
|
|
|
// Vector3.cpp
|
2018-04-22 23:11:50 -07:00
|
|
|
// Kraken Engine / Hydra
|
2018-02-26 15:24:21 -08:00
|
|
|
//
|
2026-05-17 12:22:00 -07:00
|
|
|
// Copyright 2026 Kearwood Gilbert. All rights reserved.
|
2018-02-26 15:24:21 -08:00
|
|
|
//
|
|
|
|
|
// Redistribution and use in source and binary forms, with or without modification, are
|
|
|
|
|
// permitted provided that the following conditions are met:
|
|
|
|
|
//
|
|
|
|
|
// 1. Redistributions of source code must retain the above copyright notice, this list of
|
|
|
|
|
// conditions and the following disclaimer.
|
|
|
|
|
//
|
|
|
|
|
// 2. Redistributions in binary form must reproduce the above copyright notice, this list
|
|
|
|
|
// of conditions and the following disclaimer in the documentation and/or other materials
|
|
|
|
|
// provided with the distribution.
|
|
|
|
|
//
|
|
|
|
|
// THIS SOFTWARE IS PROVIDED BY KEARWOOD GILBERT ''AS IS'' AND ANY EXPRESS OR IMPLIED
|
|
|
|
|
// WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
|
|
|
|
|
// FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL KEARWOOD GILBERT OR
|
|
|
|
|
// CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
|
|
|
|
|
// CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
|
|
|
|
|
// SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
|
|
|
|
|
// ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
|
|
|
|
|
// NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
|
|
|
|
|
// ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
|
//
|
|
|
|
|
// The views and conclusions contained in the software and documentation are those of the
|
|
|
|
|
// authors and should not be interpreted as representing official policies, either expressed
|
|
|
|
|
// or implied, of Kearwood Gilbert.
|
|
|
|
|
//
|
|
|
|
|
|
2018-04-22 23:11:50 -07:00
|
|
|
#include "../include/hydra.h"
|
2018-03-28 12:59:33 -07:00
|
|
|
#include "krhelpers.h"
|
2018-02-26 15:24:21 -08:00
|
|
|
|
2023-08-05 21:00:37 -07:00
|
|
|
namespace hydra {
|
2018-02-26 15:24:21 -08:00
|
|
|
|
|
|
|
|
//default constructor
|
|
|
|
|
void Vector3::init()
|
|
|
|
|
{
|
2022-08-08 00:20:45 -07:00
|
|
|
x = 0.0f;
|
|
|
|
|
y = 0.0f;
|
|
|
|
|
z = 0.0f;
|
2018-02-26 15:24:21 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Vector3 Vector3::Create()
|
|
|
|
|
{
|
2022-08-08 00:20:45 -07:00
|
|
|
Vector3 r;
|
|
|
|
|
r.init();
|
|
|
|
|
return r;
|
2018-02-26 15:24:21 -08:00
|
|
|
}
|
|
|
|
|
|
2022-08-08 00:20:45 -07:00
|
|
|
void Vector3::init(const Vector3& v)
|
|
|
|
|
{
|
|
|
|
|
x = v.x;
|
|
|
|
|
y = v.y;
|
|
|
|
|
z = v.z;
|
2018-02-26 15:24:21 -08:00
|
|
|
}
|
|
|
|
|
|
2022-08-08 00:20:45 -07:00
|
|
|
Vector3 Vector3::Create(const Vector3& v)
|
2018-02-26 15:24:21 -08:00
|
|
|
{
|
2022-08-08 00:20:45 -07:00
|
|
|
Vector3 r;
|
|
|
|
|
r.init(v);
|
|
|
|
|
return r;
|
2018-02-26 15:24:21 -08:00
|
|
|
}
|
|
|
|
|
|
2022-08-08 00:20:45 -07:00
|
|
|
void Vector3::init(const Vector4& v)
|
|
|
|
|
{
|
|
|
|
|
x = v.x;
|
|
|
|
|
y = v.y;
|
|
|
|
|
z = v.z;
|
2018-02-26 15:24:21 -08:00
|
|
|
}
|
|
|
|
|
|
2022-08-08 00:20:45 -07:00
|
|
|
Vector3 Vector3::Create(const Vector4& v)
|
2018-02-26 15:24:21 -08:00
|
|
|
{
|
2022-08-08 00:20:45 -07:00
|
|
|
Vector3 r;
|
|
|
|
|
r.init(v);
|
|
|
|
|
return r;
|
2018-02-26 15:24:21 -08:00
|
|
|
}
|
|
|
|
|
|
2022-08-08 00:20:45 -07:00
|
|
|
void Vector3::init(float* v)
|
|
|
|
|
{
|
|
|
|
|
x = v[0];
|
|
|
|
|
y = v[1];
|
|
|
|
|
z = v[2];
|
2018-02-26 15:24:21 -08:00
|
|
|
}
|
|
|
|
|
|
2022-08-08 00:20:45 -07:00
|
|
|
Vector3 Vector3::Create(float* v)
|
2018-02-26 15:24:21 -08:00
|
|
|
{
|
2022-08-08 00:20:45 -07:00
|
|
|
Vector3 r;
|
|
|
|
|
r.init(v);
|
|
|
|
|
return r;
|
2018-02-26 15:24:21 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2022-08-08 00:20:45 -07:00
|
|
|
void Vector3::init(double* v)
|
|
|
|
|
{
|
|
|
|
|
x = (float)v[0];
|
|
|
|
|
y = (float)v[1];
|
|
|
|
|
z = (float)v[2];
|
2018-02-26 15:24:21 -08:00
|
|
|
}
|
|
|
|
|
|
2022-08-08 00:20:45 -07:00
|
|
|
Vector3 Vector3::Create(double* v)
|
2018-02-26 15:24:21 -08:00
|
|
|
{
|
2022-08-08 00:20:45 -07:00
|
|
|
Vector3 r;
|
|
|
|
|
r.init(v);
|
|
|
|
|
return r;
|
2018-02-26 15:24:21 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2022-08-08 00:20:45 -07:00
|
|
|
void Vector3::init(float v)
|
|
|
|
|
{
|
|
|
|
|
x = v;
|
|
|
|
|
y = v;
|
|
|
|
|
z = v;
|
2018-02-26 15:24:21 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Vector3 Vector3::Create(float v)
|
|
|
|
|
{
|
2022-08-08 00:20:45 -07:00
|
|
|
Vector3 r;
|
|
|
|
|
r.init(v);
|
|
|
|
|
return r;
|
2018-02-26 15:24:21 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void Vector3::init(float X, float Y, float Z)
|
|
|
|
|
{
|
2022-08-08 00:20:45 -07:00
|
|
|
x = X;
|
|
|
|
|
y = Y;
|
|
|
|
|
z = Z;
|
2018-02-26 15:24:21 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Vector3 Vector3::Create(float X, float Y, float Z)
|
|
|
|
|
{
|
2022-08-08 00:20:45 -07:00
|
|
|
Vector3 r;
|
|
|
|
|
r.init(X, Y, Z);
|
|
|
|
|
return r;
|
2018-02-26 15:24:21 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Vector2 Vector3::xx() const
|
|
|
|
|
{
|
2022-08-08 00:20:45 -07:00
|
|
|
return Vector2::Create(x, x);
|
2018-02-26 15:24:21 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Vector2 Vector3::xy() const
|
|
|
|
|
{
|
2022-08-08 00:20:45 -07:00
|
|
|
return Vector2::Create(x, y);
|
2018-02-26 15:24:21 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Vector2 Vector3::xz() const
|
|
|
|
|
{
|
2022-08-08 00:20:45 -07:00
|
|
|
return Vector2::Create(x, z);
|
2018-02-26 15:24:21 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Vector2 Vector3::yx() const
|
|
|
|
|
{
|
2022-08-08 00:20:45 -07:00
|
|
|
return Vector2::Create(y, x);
|
2018-02-26 15:24:21 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Vector2 Vector3::yy() const
|
|
|
|
|
{
|
2022-08-08 00:20:45 -07:00
|
|
|
return Vector2::Create(y, y);
|
2018-02-26 15:24:21 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Vector2 Vector3::yz() const
|
|
|
|
|
{
|
2022-08-08 00:20:45 -07:00
|
|
|
return Vector2::Create(y, z);
|
2018-02-26 15:24:21 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Vector2 Vector3::zx() const
|
|
|
|
|
{
|
2022-08-08 00:20:45 -07:00
|
|
|
return Vector2::Create(z, x);
|
2018-02-26 15:24:21 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Vector2 Vector3::zy() const
|
|
|
|
|
{
|
2022-08-08 00:20:45 -07:00
|
|
|
return Vector2::Create(z, y);
|
2018-02-26 15:24:21 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Vector2 Vector3::zz() const
|
|
|
|
|
{
|
2022-08-08 00:20:45 -07:00
|
|
|
return Vector2::Create(z, z);
|
2018-02-26 15:24:21 -08:00
|
|
|
}
|
|
|
|
|
|
2022-08-08 00:20:45 -07:00
|
|
|
void Vector3::xy(const Vector2& v)
|
2018-02-26 15:24:21 -08:00
|
|
|
{
|
2022-08-08 00:20:45 -07:00
|
|
|
x = v.x;
|
|
|
|
|
y = v.y;
|
2018-02-26 15:24:21 -08:00
|
|
|
}
|
|
|
|
|
|
2022-08-08 00:20:45 -07:00
|
|
|
void Vector3::xz(const Vector2& v)
|
2018-02-26 15:24:21 -08:00
|
|
|
{
|
2022-08-08 00:20:45 -07:00
|
|
|
x = v.x;
|
|
|
|
|
z = v.y;
|
2018-02-26 15:24:21 -08:00
|
|
|
}
|
|
|
|
|
|
2022-08-08 00:20:45 -07:00
|
|
|
void Vector3::yx(const Vector2& v)
|
2018-02-26 15:24:21 -08:00
|
|
|
{
|
2022-08-08 00:20:45 -07:00
|
|
|
y = v.x;
|
|
|
|
|
x = v.y;
|
2018-02-26 15:24:21 -08:00
|
|
|
}
|
|
|
|
|
|
2022-08-08 00:20:45 -07:00
|
|
|
void Vector3::yz(const Vector2& v)
|
2018-02-26 15:24:21 -08:00
|
|
|
{
|
2022-08-08 00:20:45 -07:00
|
|
|
y = v.x;
|
|
|
|
|
z = v.y;
|
2018-02-26 15:24:21 -08:00
|
|
|
}
|
|
|
|
|
|
2022-08-08 00:20:45 -07:00
|
|
|
void Vector3::zx(const Vector2& v)
|
2018-02-26 15:24:21 -08:00
|
|
|
{
|
2022-08-08 00:20:45 -07:00
|
|
|
z = v.x;
|
|
|
|
|
x = v.y;
|
2018-02-26 15:24:21 -08:00
|
|
|
}
|
|
|
|
|
|
2022-08-08 00:20:45 -07:00
|
|
|
void Vector3::zy(const Vector2& v)
|
2018-02-26 15:24:21 -08:00
|
|
|
{
|
2022-08-08 00:20:45 -07:00
|
|
|
z = v.x;
|
|
|
|
|
y = v.y;
|
2018-02-26 15:24:21 -08:00
|
|
|
}
|
|
|
|
|
|
2022-08-08 00:20:45 -07:00
|
|
|
Vector3 Vector3::Min()
|
|
|
|
|
{
|
|
|
|
|
return Vector3::Create(-std::numeric_limits<float>::max());
|
2018-02-26 15:24:21 -08:00
|
|
|
}
|
|
|
|
|
|
2022-08-08 00:20:45 -07:00
|
|
|
Vector3 Vector3::Max()
|
|
|
|
|
{
|
|
|
|
|
return Vector3::Create(std::numeric_limits<float>::max());
|
2018-02-26 15:24:21 -08:00
|
|
|
}
|
|
|
|
|
|
2022-08-08 00:20:45 -07:00
|
|
|
Vector3 Vector3::Zero()
|
|
|
|
|
{
|
|
|
|
|
return Vector3::Create();
|
2018-02-26 15:24:21 -08:00
|
|
|
}
|
|
|
|
|
|
2022-08-08 00:20:45 -07:00
|
|
|
Vector3 Vector3::One()
|
|
|
|
|
{
|
|
|
|
|
return Vector3::Create(1.0f, 1.0f, 1.0f);
|
2018-02-26 15:24:21 -08:00
|
|
|
}
|
|
|
|
|
|
2022-08-08 00:20:45 -07:00
|
|
|
Vector3 Vector3::Forward()
|
|
|
|
|
{
|
|
|
|
|
return Vector3::Create(0.0f, 0.0f, 1.0f);
|
2018-02-26 15:24:21 -08:00
|
|
|
}
|
|
|
|
|
|
2022-08-08 00:20:45 -07:00
|
|
|
Vector3 Vector3::Backward()
|
|
|
|
|
{
|
|
|
|
|
return Vector3::Create(0.0f, 0.0f, -1.0f);
|
2018-02-26 15:24:21 -08:00
|
|
|
}
|
|
|
|
|
|
2022-08-08 00:20:45 -07:00
|
|
|
Vector3 Vector3::Up()
|
|
|
|
|
{
|
|
|
|
|
return Vector3::Create(0.0f, 1.0f, 0.0f);
|
2018-02-26 15:24:21 -08:00
|
|
|
}
|
|
|
|
|
|
2022-08-08 00:20:45 -07:00
|
|
|
Vector3 Vector3::Down()
|
|
|
|
|
{
|
|
|
|
|
return Vector3::Create(0.0f, -1.0f, 0.0f);
|
2018-02-26 15:24:21 -08:00
|
|
|
}
|
|
|
|
|
|
2022-08-08 00:20:45 -07:00
|
|
|
Vector3 Vector3::Left()
|
|
|
|
|
{
|
|
|
|
|
return Vector3::Create(-1.0f, 0.0f, 0.0f);
|
2018-02-26 15:24:21 -08:00
|
|
|
}
|
|
|
|
|
|
2022-08-08 00:20:45 -07:00
|
|
|
Vector3 Vector3::Right()
|
|
|
|
|
{
|
|
|
|
|
return Vector3::Create(1.0f, 0.0f, 0.0f);
|
2018-02-26 15:24:21 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2022-08-08 00:20:45 -07:00
|
|
|
void Vector3::scale(const Vector3& v)
|
2018-02-26 15:24:21 -08:00
|
|
|
{
|
2022-08-08 00:20:45 -07:00
|
|
|
x *= v.x;
|
|
|
|
|
y *= v.y;
|
|
|
|
|
z *= v.z;
|
2018-02-26 15:24:21 -08:00
|
|
|
}
|
|
|
|
|
|
2022-08-08 00:20:45 -07:00
|
|
|
Vector3 Vector3::Scale(const Vector3& v1, const Vector3& v2)
|
2018-02-26 15:24:21 -08:00
|
|
|
{
|
2022-08-08 00:20:45 -07:00
|
|
|
return Vector3::Create(v1.x * v2.x, v1.y * v2.y, v1.z * v2.z);
|
2018-02-26 15:24:21 -08:00
|
|
|
}
|
|
|
|
|
|
2022-08-08 00:20:45 -07:00
|
|
|
Vector3 Vector3::Lerp(const Vector3& v1, const Vector3& v2, float d)
|
|
|
|
|
{
|
|
|
|
|
return v1 + (v2 - v1) * d;
|
2018-02-26 15:24:21 -08:00
|
|
|
}
|
|
|
|
|
|
2022-08-08 00:20:45 -07:00
|
|
|
Vector3 Vector3::Slerp(const Vector3& v1, const Vector3& v2, float d)
|
|
|
|
|
{
|
|
|
|
|
// From: http://keithmaggio.wordpress.com/2011/02/15/math-magician-lerp-slerp-and-nlerp/
|
|
|
|
|
// Dot product - the cosine of the angle between 2 vectors.
|
|
|
|
|
float dot = Vector3::Dot(v1, v2);
|
|
|
|
|
// Clamp it to be in the range of Acos()
|
|
|
|
|
if (dot < -1.0f) dot = -1.0f;
|
|
|
|
|
if (dot > 1.0f) dot = 1.0f;
|
|
|
|
|
// Acos(dot) returns the angle between start and end,
|
|
|
|
|
// And multiplying that by percent returns the angle between
|
|
|
|
|
// start and the final result.
|
|
|
|
|
float theta = acosf(dot) * d;
|
|
|
|
|
Vector3 RelativeVec = v2 - v1 * dot;
|
|
|
|
|
RelativeVec.normalize(); // Orthonormal basis
|
|
|
|
|
// The final result.
|
|
|
|
|
return ((v1 * cosf(theta)) + (RelativeVec * sinf(theta)));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void Vector3::OrthoNormalize(Vector3& normal, Vector3& tangent)
|
|
|
|
|
{
|
|
|
|
|
// Gram-Schmidt Orthonormalization
|
|
|
|
|
normal.normalize();
|
|
|
|
|
Vector3 proj = normal * Dot(tangent, normal);
|
|
|
|
|
tangent = tangent - proj;
|
|
|
|
|
tangent.normalize();
|
2018-02-26 15:24:21 -08:00
|
|
|
}
|
|
|
|
|
|
2022-08-08 00:20:45 -07:00
|
|
|
Vector3& Vector3::operator =(const Vector4& b)
|
|
|
|
|
{
|
|
|
|
|
x = b.x;
|
|
|
|
|
y = b.y;
|
|
|
|
|
z = b.z;
|
|
|
|
|
return *this;
|
2018-02-26 15:24:21 -08:00
|
|
|
}
|
|
|
|
|
|
2022-08-08 00:20:45 -07:00
|
|
|
Vector3 Vector3::operator +(const Vector3& b) const
|
|
|
|
|
{
|
|
|
|
|
return Vector3::Create(x + b.x, y + b.y, z + b.z);
|
2018-02-26 15:24:21 -08:00
|
|
|
}
|
2022-08-08 00:20:45 -07:00
|
|
|
Vector3 Vector3::operator -(const Vector3& b) const
|
|
|
|
|
{
|
|
|
|
|
return Vector3::Create(x - b.x, y - b.y, z - b.z);
|
2018-02-26 15:24:21 -08:00
|
|
|
}
|
2022-08-08 00:20:45 -07:00
|
|
|
Vector3 Vector3::operator +() const
|
|
|
|
|
{
|
|
|
|
|
return *this;
|
2018-02-26 15:24:21 -08:00
|
|
|
}
|
2022-08-08 00:20:45 -07:00
|
|
|
Vector3 Vector3::operator -() const
|
|
|
|
|
{
|
|
|
|
|
return Vector3::Create(-x, -y, -z);
|
2018-02-26 15:24:21 -08:00
|
|
|
}
|
|
|
|
|
|
2022-08-08 00:20:45 -07:00
|
|
|
Vector3 Vector3::operator *(const float v) const
|
|
|
|
|
{
|
|
|
|
|
return Vector3::Create(x * v, y * v, z * v);
|
2018-02-26 15:24:21 -08:00
|
|
|
}
|
|
|
|
|
|
2022-08-08 00:20:45 -07:00
|
|
|
Vector3 Vector3::operator /(const float v) const
|
|
|
|
|
{
|
|
|
|
|
float inv_v = 1.0f / v;
|
|
|
|
|
return Vector3::Create(x * inv_v, y * inv_v, z * inv_v);
|
2018-02-26 15:24:21 -08:00
|
|
|
}
|
|
|
|
|
|
2022-08-08 00:20:45 -07:00
|
|
|
Vector3& Vector3::operator +=(const Vector3& b)
|
|
|
|
|
{
|
|
|
|
|
x += b.x;
|
|
|
|
|
y += b.y;
|
|
|
|
|
z += b.z;
|
2018-02-26 15:24:21 -08:00
|
|
|
|
2022-08-08 00:20:45 -07:00
|
|
|
return *this;
|
2018-02-26 15:24:21 -08:00
|
|
|
}
|
|
|
|
|
|
2022-08-08 00:20:45 -07:00
|
|
|
Vector3& Vector3::operator -=(const Vector3& b)
|
|
|
|
|
{
|
|
|
|
|
x -= b.x;
|
|
|
|
|
y -= b.y;
|
|
|
|
|
z -= b.z;
|
2018-02-26 15:24:21 -08:00
|
|
|
|
2022-08-08 00:20:45 -07:00
|
|
|
return *this;
|
2018-02-26 15:24:21 -08:00
|
|
|
}
|
|
|
|
|
|
2022-08-08 00:20:45 -07:00
|
|
|
Vector3& Vector3::operator *=(const float v)
|
|
|
|
|
{
|
|
|
|
|
x *= v;
|
|
|
|
|
y *= v;
|
|
|
|
|
z *= v;
|
|
|
|
|
|
|
|
|
|
return *this;
|
2018-02-26 15:24:21 -08:00
|
|
|
}
|
|
|
|
|
|
2022-08-08 00:20:45 -07:00
|
|
|
Vector3& Vector3::operator /=(const float v)
|
|
|
|
|
{
|
|
|
|
|
float inv_v = 1.0f / v;
|
|
|
|
|
x *= inv_v;
|
|
|
|
|
y *= inv_v;
|
|
|
|
|
z *= inv_v;
|
|
|
|
|
|
|
|
|
|
return *this;
|
2018-02-26 15:24:21 -08:00
|
|
|
}
|
|
|
|
|
|
2022-08-08 00:20:45 -07:00
|
|
|
bool Vector3::operator ==(const Vector3& b) const
|
|
|
|
|
{
|
|
|
|
|
return x == b.x && y == b.y && z == b.z;
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
bool Vector3::operator !=(const Vector3& b) const
|
|
|
|
|
{
|
|
|
|
|
return x != b.x || y != b.y || z != b.z;
|
2018-02-26 15:24:21 -08:00
|
|
|
}
|
|
|
|
|
|
2022-08-08 00:20:45 -07:00
|
|
|
float& Vector3::operator[](unsigned i)
|
|
|
|
|
{
|
|
|
|
|
switch (i) {
|
|
|
|
|
case 0:
|
|
|
|
|
return x;
|
|
|
|
|
case 1:
|
|
|
|
|
return y;
|
|
|
|
|
default:
|
|
|
|
|
case 2:
|
|
|
|
|
return z;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
float Vector3::operator[](unsigned i) const
|
|
|
|
|
{
|
|
|
|
|
switch (i) {
|
|
|
|
|
case 0:
|
|
|
|
|
return x;
|
|
|
|
|
case 1:
|
|
|
|
|
return y;
|
|
|
|
|
case 2:
|
|
|
|
|
default:
|
|
|
|
|
return z;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
float Vector3::sqrMagnitude() const
|
|
|
|
|
{
|
|
|
|
|
// calculate the square of the magnitude (useful for comparison of magnitudes without the cost of a sqrt() function)
|
|
|
|
|
return x * x + y * y + z * z;
|
2018-02-26 15:24:21 -08:00
|
|
|
}
|
|
|
|
|
|
2022-08-08 00:20:45 -07:00
|
|
|
float Vector3::magnitude() const
|
|
|
|
|
{
|
|
|
|
|
return sqrtf(x * x + y * y + z * z);
|
2018-02-26 15:24:21 -08:00
|
|
|
}
|
|
|
|
|
|
2022-08-08 00:20:45 -07:00
|
|
|
void Vector3::normalize()
|
|
|
|
|
{
|
|
|
|
|
float inv_magnitude = 1.0f / sqrtf(x * x + y * y + z * z);
|
|
|
|
|
x *= inv_magnitude;
|
|
|
|
|
y *= inv_magnitude;
|
|
|
|
|
z *= inv_magnitude;
|
2018-02-26 15:24:21 -08:00
|
|
|
}
|
2022-08-08 00:20:45 -07:00
|
|
|
Vector3 Vector3::Normalize(const Vector3& v)
|
|
|
|
|
{
|
|
|
|
|
float inv_magnitude = 1.0f / sqrtf(v.x * v.x + v.y * v.y + v.z * v.z);
|
|
|
|
|
return Vector3::Create(v.x * inv_magnitude, v.y * inv_magnitude, v.z * inv_magnitude);
|
2018-02-26 15:24:21 -08:00
|
|
|
}
|
|
|
|
|
|
2022-08-08 00:20:45 -07:00
|
|
|
Vector3 Vector3::Cross(const Vector3& v1, const Vector3& v2)
|
|
|
|
|
{
|
|
|
|
|
return Vector3::Create(v1.y * v2.z - v1.z * v2.y,
|
|
|
|
|
v1.z * v2.x - v1.x * v2.z,
|
|
|
|
|
v1.x * v2.y - v1.y * v2.x);
|
2018-02-26 15:24:21 -08:00
|
|
|
}
|
|
|
|
|
|
2022-08-08 00:20:45 -07:00
|
|
|
float Vector3::Dot(const Vector3& v1, const Vector3& v2)
|
|
|
|
|
{
|
|
|
|
|
return v1.x * v2.x + v1.y * v2.y + v1.z * v2.z;
|
2018-02-26 15:24:21 -08:00
|
|
|
}
|
|
|
|
|
|
2022-08-08 00:20:45 -07:00
|
|
|
Vector3 Vector3::Min(const Vector3& v1, const Vector3& v2)
|
|
|
|
|
{
|
2026-05-17 14:23:26 -07:00
|
|
|
return Vector3::Create(std::min(v1.x, v2.x), std::min(v1.y, v2.y), std::min(v1.z, v2.z));
|
2018-03-28 12:59:33 -07:00
|
|
|
}
|
|
|
|
|
|
2022-08-08 00:20:45 -07:00
|
|
|
Vector3 Vector3::Max(const Vector3& v1, const Vector3& v2)
|
|
|
|
|
{
|
2026-05-17 14:23:26 -07:00
|
|
|
return Vector3::Create(std::max(v1.x, v2.x), std::max(v1.y, v2.y), std::max(v1.z, v2.z));
|
2018-03-28 12:59:33 -07:00
|
|
|
}
|
|
|
|
|
|
2018-02-26 15:24:21 -08:00
|
|
|
bool Vector3::operator >(const Vector3& b) const
|
|
|
|
|
{
|
2022-08-08 00:20:45 -07:00
|
|
|
// Comparison operators are implemented to allow insertion into sorted containers such as std::set
|
|
|
|
|
if (x > b.x) {
|
|
|
|
|
return true;
|
|
|
|
|
} else if (x < b.x) {
|
|
|
|
|
return false;
|
|
|
|
|
} else if (y > b.y) {
|
|
|
|
|
return true;
|
|
|
|
|
} else if (y < b.y) {
|
|
|
|
|
return false;
|
|
|
|
|
} else if (z > b.z) {
|
|
|
|
|
return true;
|
|
|
|
|
} else {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
2018-02-26 15:24:21 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool Vector3::operator <(const Vector3& b) const
|
|
|
|
|
{
|
2022-08-08 00:20:45 -07:00
|
|
|
// Comparison operators are implemented to allow insertion into sorted containers such as std::set
|
|
|
|
|
if (x < b.x) {
|
|
|
|
|
return true;
|
|
|
|
|
} else if (x > b.x) {
|
|
|
|
|
return false;
|
|
|
|
|
} else if (y < b.y) {
|
|
|
|
|
return true;
|
|
|
|
|
} else if (y > b.y) {
|
|
|
|
|
return false;
|
|
|
|
|
} else if (z < b.z) {
|
|
|
|
|
return true;
|
|
|
|
|
} else {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
2018-02-26 15:24:21 -08:00
|
|
|
}
|
|
|
|
|
|
2023-08-05 21:00:37 -07:00
|
|
|
} // namespace hydra
|
2018-02-26 15:24:21 -08:00
|
|
|
|