ReGameDLL_CS/regamedll/dlls/vector.h

463 lines
9.8 KiB
C
Raw Normal View History

2015-06-30 12:46:07 +03:00
/*
*
* This program is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License as published by the
* Free Software Foundation; either version 2 of the License, or (at
* your option) any later version.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software Foundation,
* Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*
* In addition, as a special exception, the author gives permission to
* link the code of this program with the Half-Life Game Engine ("HL
* Engine") and Modified Game Libraries ("MODs") developed by Valve,
* L.L.C ("Valve"). You must obey the GNU General Public License in all
* respects for all of the code used other than the HL Engine and MODs
* from Valve. If you modify this file, you may extend this exception
* to your version of the file, but you are not obligated to do so. If
* you do not wish to do so, delete this exception statement from your
* version.
*
*/
#ifndef VECTOR_H
#define VECTOR_H
#ifdef _WIN32
#pragma once
#endif
/* <5d3ffa> ../cstrike/dlls/vector.h:26 */
class Vector2D
{
public:
vec_t x, y;
Vector2D(void) : x(0.0), y(0.0) {}
Vector2D(float X, float Y) : x(0.0), y(0.0)
2015-06-30 12:46:07 +03:00
{
x = X;
y = Y;
}
Vector2D operator+(const Vector2D &v) const
2015-06-30 12:46:07 +03:00
{
return Vector2D(x + v.x, y + v.y);
}
Vector2D operator-(const Vector2D &v) const
2015-06-30 12:46:07 +03:00
{
return Vector2D(x - v.x, y - v.y);
}
#ifdef HOOK_GAMEDLL
Vector2D operator*(float_precision fl) const
{
return Vector2D((vec_t)(x * fl), (vec_t)(y * fl));
}
Vector2D operator/(float_precision fl) const
{
return Vector2D((vec_t)(x / fl), (vec_t)(y / fl));
}
Vector2D operator/=(float_precision fl) const
{
return Vector2D((vec_t)(x / fl), (vec_t)(y / fl));
}
#else
Vector2D operator*(float fl) const
2015-06-30 12:46:07 +03:00
{
return Vector2D(x * fl, y * fl);
}
Vector2D operator/(float fl) const
2015-06-30 12:46:07 +03:00
{
return Vector2D(x / fl, y / fl);
}
Vector2D operator/=(float fl) const
2015-06-30 12:46:07 +03:00
{
return Vector2D(x / fl, y / fl);
}
#endif // HOOK_GAMEDLL
float_precision Length(void) const
2015-06-30 12:46:07 +03:00
{
return sqrt((float_precision)(x * x + y * y));
2015-06-30 12:46:07 +03:00
}
float LengthSquared(void) const
2015-06-30 12:46:07 +03:00
{
return (x * x + y * y);
}
operator float*()
{
return &x;
}
operator const float*() const
{
return &x;
}
Vector2D Normalize(void) const
2015-06-30 12:46:07 +03:00
{
2015-08-20 13:35:01 +03:00
float_precision flLen = Length();
if (!flLen)
2015-06-30 12:46:07 +03:00
return Vector2D(0, 0);
2015-08-20 13:35:01 +03:00
flLen = 1 / flLen;
2015-09-16 23:19:21 +03:00
#ifdef HOOK_GAMEDLL
2015-08-20 13:35:01 +03:00
return Vector2D((vec_t)(x * flLen), (vec_t)(y * flLen));
2015-06-30 12:46:07 +03:00
#else
2015-08-20 13:35:01 +03:00
return Vector2D(x * flLen, y * flLen);
2015-06-30 12:46:07 +03:00
#endif // HOOK_GAMEDLL
}
bool IsLengthLessThan(float length) const
2015-06-30 12:46:07 +03:00
{
return (LengthSquared() < length * length);
}
bool IsLengthGreaterThan(float length) const
2015-06-30 12:46:07 +03:00
{
return (LengthSquared() > length * length);
}
float_precision NormalizeInPlace(void)
2015-06-30 12:46:07 +03:00
{
2015-08-20 13:35:01 +03:00
float_precision flLen = Length();
if (flLen > 0.0)
2015-06-30 12:46:07 +03:00
{
2015-12-09 01:39:54 +03:00
x = (vec_t)(1 / flLen * x);
y = (vec_t)(1 / flLen * y);
2015-06-30 12:46:07 +03:00
}
else
{
x = 1.0;
y = 0.0;
}
2015-08-20 13:35:01 +03:00
return flLen;
2015-06-30 12:46:07 +03:00
}
bool IsZero(float tolerance = 0.01f) const
2015-06-30 12:46:07 +03:00
{
return (x > -tolerance && x < tolerance &&
y > -tolerance && y < tolerance);
}
2015-09-16 23:19:21 +03:00
2015-06-30 12:46:07 +03:00
};/* size: 8, cachelines: 1, members: 2 */
inline float DotProduct(const Vector2D &a, const Vector2D &b)
{
return (a.x * b.x + a.y * b.y);
}
2015-06-30 12:46:07 +03:00
inline Vector2D operator*(float fl, const Vector2D &v)
{
return v * fl;
}
/* <5e2e91> ../cstrike/dlls/vector.h:104 */
class Vector
{
public:
vec_t x, y, z;
Vector(void) : x(0.0), y(0.0), z(0.0) {}
Vector(float X, float Y, float Z) : x(0.0), y(0.0), z(0.0)
2015-06-30 12:46:07 +03:00
{
x = X;
y = Y;
z = Z;
}
Vector(const Vector &v) : x(0.0), y(0.0), z(0.0)
2015-06-30 12:46:07 +03:00
{
x = v.x;
y = v.y;
z = v.z;
}
Vector(const float rgfl[3]) : x(0.0), y(0.0), z(0.0)
2015-06-30 12:46:07 +03:00
{
x = rgfl[0];
y = rgfl[1];
z = rgfl[2];
}
Vector operator-(void) const
2015-06-30 12:46:07 +03:00
{
return Vector(-x, -y, -z);
}
int operator==(const Vector &v) const
2015-06-30 12:46:07 +03:00
{
return x == v.x && y == v.y && z == v.z;
}
int operator!=(const Vector &v) const
2015-06-30 12:46:07 +03:00
{
return !(*this == v);
}
Vector operator+(const Vector &v) const
2015-06-30 12:46:07 +03:00
{
return Vector(x + v.x, y + v.y, z + v.z);
}
Vector operator-(const Vector &v) const
2015-06-30 12:46:07 +03:00
{
return Vector(x - v.x, y - v.y, z - v.z);
}
#ifdef HOOK_GAMEDLL
Vector operator*(float_precision fl) const
{
return Vector((vec_t)(x * fl), (vec_t)(y * fl), (vec_t)(z * fl));
}
Vector operator/(float_precision fl) const
{
return Vector((vec_t)(x / fl), (vec_t)(y / fl), (vec_t)(z / fl));
}
Vector operator/=(float_precision fl) const
{
return Vector((vec_t)(x / fl), (vec_t)(y / fl), (vec_t)(z / fl));
}
#else
Vector operator*(float fl) const
2015-06-30 12:46:07 +03:00
{
return Vector(x * fl, y * fl, z * fl);
}
Vector operator/(float fl) const
2015-06-30 12:46:07 +03:00
{
return Vector(x / fl, y / fl, z / fl);
}
Vector operator/=(float fl) const
2015-06-30 12:46:07 +03:00
{
return Vector(x / fl, y / fl, z / fl);
}
#endif // HOOK_GAMEDLL
void CopyToArray(float *rgfl) const
2015-06-30 12:46:07 +03:00
{
rgfl[0] = x;
rgfl[1] = y;
rgfl[2] = z;
}
float_precision Length(void) const
2015-06-30 12:46:07 +03:00
{
2015-08-20 13:35:01 +03:00
float_precision x1 = (float_precision)x;
float_precision y1 = (float_precision)y;
float_precision z1 = (float_precision)z;
return sqrt(x1 * x1 + y1 * y1 + z1 * z1);
//return sqrt((float_precision)(x * x + y * y + z * z));
2015-06-30 12:46:07 +03:00
}
float LengthSquared(void) const
2015-06-30 12:46:07 +03:00
{
return (x * x + y * y + z * z);
}
operator float*()
{
return &x;
}
operator const float*() const
{
return &x;
}
2015-08-20 13:35:01 +03:00
#ifndef HOOK_GAMEDLL
Vector Normalize(void)
2015-06-30 12:46:07 +03:00
{
2015-08-20 13:35:01 +03:00
float flLen = Length();
if (flLen == 0)
2015-06-30 12:46:07 +03:00
return Vector(0, 0, 1);
2015-09-16 23:19:21 +03:00
flLen = 1 / flLen;
2015-08-20 13:35:01 +03:00
return Vector(x * flLen, y * flLen, z * flLen);
}
#else
Vector Normalize(void)
2015-08-20 13:35:01 +03:00
{
float_precision flLen = Length();
if (flLen == 0)
return Vector(0, 0, 1);
2015-09-16 23:19:21 +03:00
vec_t fTemp = (vec_t)(1 / flLen);
2015-08-20 13:35:01 +03:00
return Vector(x * fTemp, y * fTemp, z * fTemp);
}
#endif // HOOK_GAMEDLL
2015-08-20 13:35:01 +03:00
// for out precision normalize
Vector NormalizePrecision(void)
2015-08-20 13:35:01 +03:00
{
#ifndef HOOK_GAMEDLL
return Normalize();
#else
float_precision flLen = Length();
if (flLen == 0)
return Vector(0, 0, 1);
2015-09-16 23:19:21 +03:00
flLen = 1 / flLen;
2015-08-20 13:35:01 +03:00
return Vector((vec_t)(x * flLen), (vec_t)(y * flLen), (vec_t)(z * flLen));
#endif // HOOK_GAMEDLL
2015-06-30 12:46:07 +03:00
}
Vector2D Make2D(void) const
2015-06-30 12:46:07 +03:00
{
Vector2D Vec2;
Vec2.x = x;
Vec2.y = y;
return Vec2;
}
float_precision Length2D(void) const
2015-06-30 12:46:07 +03:00
{
return sqrt((float_precision)(x * x + y * y));
2015-06-30 12:46:07 +03:00
}
bool IsLengthLessThan(float length) const
2015-06-30 12:46:07 +03:00
{
return (LengthSquared() < length * length);
}
bool IsLengthGreaterThan(float length) const
2015-06-30 12:46:07 +03:00
{
return (LengthSquared() > length * length);
}
#ifdef HOOK_GAMEDLL
float_precision NormalizeInPlace(void)
2015-06-30 12:46:07 +03:00
{
2015-08-20 13:35:01 +03:00
float_precision flLen = Length();
if (flLen > 0)
2015-06-30 12:46:07 +03:00
{
x = (vec_t)(1 / flLen * x);
y = (vec_t)(1 / flLen * y);
z = (vec_t)(1 / flLen * z);
2015-06-30 12:46:07 +03:00
}
else
{
x = 0;
y = 0;
z = 1;
}
return flLen;
}
#else // HOOK_GAMEDLL
float NormalizeInPlace(void)
{
float flLen = Length();
if (flLen > 0)
{
x /= flLen;
y /= flLen;
z /= flLen;
}
else
{
x = 0;
y = 0;
z = 1;
2015-06-30 12:46:07 +03:00
}
2015-08-20 13:35:01 +03:00
return flLen;
2015-06-30 12:46:07 +03:00
}
#endif // HOOK_GAMEDLL
bool IsZero(float tolerance = 0.01f) const
2015-06-30 12:46:07 +03:00
{
return (x > -tolerance && x < tolerance &&
y > -tolerance && y < tolerance &&
z > -tolerance && z < tolerance);
}
2015-06-30 12:46:07 +03:00
};/* size: 12, cachelines: 1, members: 3 */
/* <1c0d1> ../cstrike/dlls/vector.h:184 */
inline Vector operator*(float fl, const Vector &v)
{
return v * fl;
}
/* <5d9a4> ../cstrike/dlls/vector.h:185 */
2015-08-20 13:35:01 +03:00
inline float_precision DotProduct(const Vector &a, const Vector &b)
2015-06-30 12:46:07 +03:00
{
return (a.x * b.x + a.y * b.y + a.z * b.z);
}
/* <1ba548> ../cstrike/dlls/vector.h:186 */
inline 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);
}
template<class T>
inline void SWAP(T &first, T &second)
{
T temp = first;
first = second;
second = temp;
}
2015-08-20 13:35:01 +03:00
template<
typename X,
typename Y,
typename Z,
typename LenType
>
2015-09-16 23:19:21 +03:00
inline LenType LengthSubtract(Vector vecStart, Vector vecDest)
2015-08-20 13:35:01 +03:00
{
X floatX = (vecDest.x - vecStart.x);
Y floatY = (vecDest.y - vecStart.y);
Z floatZ = (vecDest.z - vecStart.z);
return sqrt((float_precision)(floatX * floatX + floatY * floatY + floatZ * floatZ));
}
template<
typename X,
typename Y,
typename Z,
typename LenType
>
inline Vector NormalizeSubtract(Vector vecStart, Vector vecDest)
{
Vector dir;
#ifdef HOOK_GAMEDLL
X floatX = (vecDest.x - vecStart.x);
Y floatY = (vecDest.y - vecStart.y);
Z floatZ = (vecDest.z - vecStart.z);
LenType flLen = sqrt((float_precision)(floatX * floatX + floatY * floatY + floatZ * floatZ));
if (flLen == 0.0)
{
dir = Vector(0, 0, 1);
}
else
{
2015-09-16 23:19:21 +03:00
flLen = 1.0 / flLen;
dir.x = (vec_t)(floatX * flLen);
dir.y = (vec_t)(floatY * flLen);
2015-08-20 13:35:01 +03:00
dir.z = (vec_t)(floatZ * flLen);
}
#else
dir = (vecDest - vecStart).Normalize();
#endif // HOOK_GAMEDLL
return dir;
}
#ifdef HOOK_GAMEDLL
template<
typename X,
typename Y,
typename LenType
>
inline Vector NormalizeMulScalar(Vector2D vec, float scalar)
{
LenType flLen;
X floatX;
Y floatY;
flLen = (LenType)vec.Length();
if (flLen <= 0.0)
{
floatX = 1;
floatY = 0;
}
else
{
flLen = 1 / flLen;
floatX = vec.x * flLen;
floatY = vec.y * flLen;
}
return Vector((vec_t)(floatX * scalar), (vec_t)(floatY * scalar), 0);
}
#endif // HOOK_GAMEDLL
2015-06-30 12:46:07 +03:00
#endif // VECTOR_H