2022-12-17 13:32:43 +01:00
|
|
|
/***
|
2013-08-30 13:34:05 -07:00
|
|
|
*
|
|
|
|
* Copyright (c) 1996-2002, Valve LLC. All rights reserved.
|
|
|
|
*
|
|
|
|
* This product contains software technology licensed from Id
|
|
|
|
* Software, Inc. ("Id Technology"). Id Technology (c) 1996 Id Software, Inc.
|
|
|
|
* All Rights Reserved.
|
|
|
|
*
|
|
|
|
* Use, distribution, and modification of this source code and/or resulting
|
|
|
|
* object code is restricted to non-commercial enhancements to products from
|
|
|
|
* Valve LLC. All other use, distribution, or modification is prohibited
|
|
|
|
* without written permission from Valve LLC.
|
|
|
|
*
|
|
|
|
****/
|
2021-11-18 19:17:53 +01:00
|
|
|
|
|
|
|
#pragma once
|
2013-08-30 13:34:05 -07:00
|
|
|
|
|
|
|
//=========================================================
|
2021-11-28 16:54:48 +01:00
|
|
|
// 2DVector - used for many pathfinding and many other
|
2013-08-30 13:34:05 -07:00
|
|
|
// operations that are treated as planar rather than 3d.
|
|
|
|
//=========================================================
|
|
|
|
class Vector2D
|
|
|
|
{
|
|
|
|
public:
|
2021-11-28 22:01:47 +01:00
|
|
|
constexpr Vector2D() = default;
|
|
|
|
constexpr Vector2D(const Vector2D&) = default;
|
|
|
|
constexpr Vector2D& operator=(const Vector2D&) = default;
|
|
|
|
|
|
|
|
constexpr Vector2D(float X, float Y)
|
2021-11-29 20:55:01 +01:00
|
|
|
: x(X), y(Y)
|
2021-11-28 16:54:48 +01:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2021-11-28 22:01:47 +01:00
|
|
|
[[nodiscard]] constexpr Vector2D operator+(const Vector2D& v) const { return Vector2D(x + v.x, y + v.y); }
|
|
|
|
[[nodiscard]] constexpr Vector2D operator-(const Vector2D& v) const { return Vector2D(x - v.x, y - v.y); }
|
|
|
|
[[nodiscard]] constexpr Vector2D operator*(float fl) const { return Vector2D(x * fl, y * fl); }
|
|
|
|
[[nodiscard]] constexpr Vector2D operator/(float fl) const { return Vector2D(x / fl, y / fl); }
|
2021-11-28 16:54:48 +01:00
|
|
|
|
2021-11-28 22:01:47 +01:00
|
|
|
[[nodiscard]] float Length() const { return static_cast<float>(sqrt(x * x + y * y)); }
|
2013-08-30 13:34:05 -07:00
|
|
|
|
2021-11-28 22:01:47 +01:00
|
|
|
[[nodiscard]] Vector2D Normalize() const
|
|
|
|
{
|
2013-08-30 13:34:05 -07:00
|
|
|
float flLen = Length();
|
2021-11-28 16:54:48 +01:00
|
|
|
if (flLen == 0)
|
2013-08-30 13:34:05 -07:00
|
|
|
{
|
2021-11-28 16:54:48 +01:00
|
|
|
return Vector2D(0, 0);
|
2013-08-30 13:34:05 -07:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
flLen = 1 / flLen;
|
2021-11-28 16:54:48 +01:00
|
|
|
return Vector2D(x * flLen, y * flLen);
|
2013-08-30 13:34:05 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-11-28 22:01:47 +01:00
|
|
|
vec_t x = 0, y = 0;
|
2013-08-30 13:34:05 -07:00
|
|
|
};
|
|
|
|
|
2021-11-28 22:01:47 +01:00
|
|
|
[[nodiscard]] constexpr float DotProduct(const Vector2D& a, const Vector2D& b)
|
|
|
|
{
|
|
|
|
return (a.x * b.x + a.y * b.y);
|
|
|
|
}
|
|
|
|
|
|
|
|
[[nodiscard]] constexpr Vector2D operator*(float fl, const Vector2D& v)
|
|
|
|
{
|
|
|
|
return v * fl;
|
|
|
|
}
|
2013-08-30 13:34:05 -07:00
|
|
|
|
|
|
|
//=========================================================
|
|
|
|
// 3D Vector
|
|
|
|
//=========================================================
|
2021-11-28 16:54:48 +01:00
|
|
|
class Vector // same data-layout as engine's vec3_t,
|
|
|
|
{ // which is a vec_t[3]
|
2013-08-30 13:34:05 -07:00
|
|
|
public:
|
|
|
|
// Construction/destruction
|
2021-11-28 22:01:47 +01:00
|
|
|
constexpr Vector() = default;
|
|
|
|
constexpr Vector(const Vector&) = default;
|
|
|
|
constexpr Vector& operator=(const Vector&) = default;
|
2021-03-16 20:24:00 +01:00
|
|
|
|
2021-11-28 22:01:47 +01:00
|
|
|
constexpr Vector(float X, float Y, float Z)
|
2021-11-28 16:54:48 +01:00
|
|
|
: x(X), y(Y), z(Z)
|
2021-03-16 20:24:00 +01:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2021-11-28 22:01:47 +01:00
|
|
|
constexpr Vector(float rgfl[3])
|
2021-11-28 16:54:48 +01:00
|
|
|
: x(rgfl[0]), y(rgfl[1]), z(rgfl[2])
|
2021-03-16 20:24:00 +01:00
|
|
|
{
|
|
|
|
}
|
2013-08-30 13:34:05 -07:00
|
|
|
|
|
|
|
// Operators
|
2021-11-28 22:01:47 +01:00
|
|
|
[[nodiscard]] constexpr Vector operator-() const { return Vector(-x, -y, -z); }
|
|
|
|
[[nodiscard]] constexpr bool operator==(const Vector& v) const { return x == v.x && y == v.y && z == v.z; }
|
|
|
|
[[nodiscard]] constexpr bool operator!=(const Vector& v) const { return !(*this == v); }
|
|
|
|
[[nodiscard]] constexpr Vector operator+(const Vector& v) const { return Vector(x + v.x, y + v.y, z + v.z); }
|
|
|
|
[[nodiscard]] constexpr Vector operator-(const Vector& v) const { return Vector(x - v.x, y - v.y, z - v.z); }
|
|
|
|
[[nodiscard]] constexpr Vector operator*(float fl) const { return Vector(x * fl, y * fl, z * fl); }
|
|
|
|
[[nodiscard]] constexpr Vector operator/(float fl) const { return Vector(x / fl, y / fl, z / fl); }
|
2021-11-28 16:54:48 +01:00
|
|
|
|
2013-08-30 13:34:05 -07:00
|
|
|
// Methods
|
2021-11-28 22:01:47 +01:00
|
|
|
constexpr void CopyToArray(float* rgfl) const { rgfl[0] = x, rgfl[1] = y, rgfl[2] = z; }
|
2021-12-24 16:22:25 +01:00
|
|
|
|
|
|
|
[[nodiscard]] constexpr float LengthSquared() const { return x * x + y * y + z * z; }
|
|
|
|
[[nodiscard]] float Length() const { return static_cast<float>(sqrt(LengthSquared())); }
|
2021-11-28 22:01:47 +01:00
|
|
|
[[nodiscard]] constexpr operator float*() { return &x; } // Vectors will now automatically convert to float * when needed
|
|
|
|
[[nodiscard]] constexpr operator const float*() const { return &x; } // Vectors will now automatically convert to float * when needed
|
|
|
|
|
|
|
|
[[nodiscard]] Vector Normalize() const
|
2013-08-30 13:34:05 -07:00
|
|
|
{
|
|
|
|
float flLen = Length();
|
2021-11-28 16:54:48 +01:00
|
|
|
if (flLen == 0)
|
|
|
|
return Vector(0, 0, 1); // ????
|
2013-08-30 13:34:05 -07:00
|
|
|
flLen = 1 / flLen;
|
|
|
|
return Vector(x * flLen, y * flLen, z * flLen);
|
|
|
|
}
|
|
|
|
|
2021-11-28 22:01:47 +01:00
|
|
|
[[nodiscard]] constexpr Vector2D Make2D() const
|
2013-08-30 13:34:05 -07:00
|
|
|
{
|
2021-11-28 22:01:47 +01:00
|
|
|
return {x, y};
|
2013-08-30 13:34:05 -07:00
|
|
|
}
|
2021-11-28 22:01:47 +01:00
|
|
|
|
|
|
|
[[nodiscard]] float Length2D() const { return static_cast<float>(sqrt(x * x + y * y)); }
|
2013-08-30 13:34:05 -07:00
|
|
|
|
|
|
|
// Members
|
2021-11-28 22:01:47 +01:00
|
|
|
vec_t x = 0, y = 0, z = 0;
|
2013-08-30 13:34:05 -07:00
|
|
|
};
|
2021-11-28 22:01:47 +01:00
|
|
|
|
|
|
|
[[nodiscard]] constexpr Vector operator*(float fl, const Vector& v)
|
|
|
|
{
|
|
|
|
return v * fl;
|
|
|
|
}
|
|
|
|
|
|
|
|
[[nodiscard]] constexpr float DotProduct(const Vector& a, const Vector& b)
|
|
|
|
{
|
|
|
|
return (a.x * b.x + a.y * b.y + a.z * b.z);
|
|
|
|
}
|
|
|
|
|
|
|
|
[[nodiscard]] constexpr Vector CrossProduct(const Vector& a, const Vector& b)
|
|
|
|
{
|
|
|
|
return Vector(a.y * b.z - a.z * b.y, a.z * b.x - a.x * b.z, a.x * b.y - a.y * b.x);
|
|
|
|
}
|