mirror of
https://github.com/nillerusr/source-engine.git
synced 2026-08-10 10:49:35 +00:00
1
This commit is contained in:
Vendored
+97
@@ -0,0 +1,97 @@
|
||||
//===== Copyright 1996-2010, Valve Corporation, All rights reserved. ======//
|
||||
//
|
||||
// Purpose: - defines the type fltx4 - Avoid cyclic includion.
|
||||
//
|
||||
//===========================================================================//
|
||||
|
||||
#ifndef FLTX4_H
|
||||
#define FLTX4_H
|
||||
|
||||
#if defined(GNUC)
|
||||
#define USE_STDC_FOR_SIMD 0
|
||||
#else
|
||||
#define USE_STDC_FOR_SIMD 0
|
||||
#endif
|
||||
|
||||
#if (!defined(PLATFORM_PPC) && (USE_STDC_FOR_SIMD == 0))
|
||||
#define _SSE1 1
|
||||
#endif
|
||||
|
||||
// I thought about defining a class/union for the SIMD packed floats instead of using fltx4,
|
||||
// but decided against it because (a) the nature of SIMD code which includes comparisons is to blur
|
||||
// the relationship between packed floats and packed integer types and (b) not sure that the
|
||||
// compiler would handle generating good code for the intrinsics.
|
||||
|
||||
#if USE_STDC_FOR_SIMD
|
||||
|
||||
#error "hello"
|
||||
typedef union
|
||||
{
|
||||
float m128_f32[4];
|
||||
uint32 m128_u32[4];
|
||||
} fltx4;
|
||||
|
||||
typedef fltx4 i32x4;
|
||||
typedef fltx4 u32x4;
|
||||
|
||||
#ifdef _PS3
|
||||
typedef fltx4 u32x4;
|
||||
typedef fltx4 i32x4;
|
||||
#endif
|
||||
typedef fltx4 bi32x4;
|
||||
|
||||
#elif ( defined( _PS3 ) )
|
||||
|
||||
typedef union
|
||||
{
|
||||
// This union allows float/int access (which generally shouldn't be done in inner loops)
|
||||
|
||||
vec_float4 vmxf;
|
||||
vec_int4 vmxi;
|
||||
vec_uint4 vmxui;
|
||||
__vector bool vmxbi;
|
||||
|
||||
struct
|
||||
{
|
||||
float x;
|
||||
float y;
|
||||
float z;
|
||||
float w;
|
||||
};
|
||||
|
||||
float m128_f32[4];
|
||||
uint32 m128_u32[4];
|
||||
int32 m128_i32[4];
|
||||
|
||||
} fltx4_union;
|
||||
|
||||
typedef vec_float4 fltx4;
|
||||
typedef vec_uint4 u32x4;
|
||||
typedef vec_int4 i32x4;
|
||||
typedef __vector bool bi32x4;
|
||||
#define DIFFERENT_NATIVE_VECTOR_TYPES // true if the compiler has different types for float4, uint4, int4, etc
|
||||
|
||||
#elif ( defined( _X360 ) )
|
||||
|
||||
typedef union
|
||||
{
|
||||
// This union allows float/int access (which generally shouldn't be done in inner loops)
|
||||
__vector4 vmx;
|
||||
float m128_f32[4];
|
||||
uint32 m128_u32[4];
|
||||
} fltx4_union;
|
||||
|
||||
typedef __vector4 fltx4;
|
||||
typedef __vector4 i32x4; // a VMX register; just a way of making it explicit that we're doing integer ops.
|
||||
typedef __vector4 u32x4; // a VMX register; just a way of making it explicit that we're doing unsigned integer ops.
|
||||
typedef fltx4 bi32x4;
|
||||
#else
|
||||
|
||||
typedef __m128 fltx4;
|
||||
typedef __m128 i32x4;
|
||||
typedef __m128 u32x4;
|
||||
typedef fltx4 bi32x4;
|
||||
|
||||
#endif
|
||||
|
||||
#endif
|
||||
+283
@@ -0,0 +1,283 @@
|
||||
//========= Copyright © 1996-2005, Valve Corporation, All rights reserved. ============//
|
||||
//
|
||||
// Purpose:
|
||||
//
|
||||
//=====================================================================================//
|
||||
|
||||
#ifndef _MATH_PFNS_H_
|
||||
#define _MATH_PFNS_H_
|
||||
|
||||
#include <limits>
|
||||
|
||||
#if defined( _X360 )
|
||||
#include <xboxmath.h>
|
||||
#elif defined(_PS3)
|
||||
|
||||
#ifndef SPU
|
||||
#include <ppu_asm_intrinsics.h>
|
||||
#endif
|
||||
|
||||
// Note that similar defines exist in ssemath.h
|
||||
// Maybe we should consolidate in one place for all platforms.
|
||||
|
||||
#define _VEC_0x7ff (vec_int4){0x7ff,0x7ff,0x7ff,0x7ff}
|
||||
#define _VEC_0x3ff (vec_int4){0x3ff,0x3ff,0x3ff,0x3ff}
|
||||
#define _VEC_22L (vector unsigned int){22,22,22,22}
|
||||
#define _VEC_11L (vector unsigned int){11,11,11,11}
|
||||
#define _VEC_0L (vector unsigned int){0,0,0,0}
|
||||
#define _VEC_255F (vector float){255.0f,255.0f,255.0f,255.0f}
|
||||
#define _VEC_NEGONEF (vector float){-1.0f,-1.0f,-1.0f,-1.0f}
|
||||
#define _VEC_ONEF (vector float){1.0f,1.0f,1.0f,1.0f}
|
||||
#define _VEC_ZEROF (vector float){0.0f,0.0f,0.0f,0.0f}
|
||||
#define _VEC_ZEROxyzONEwF (vector float){0.0f,0.0f,0.0f,1.0f}
|
||||
#define _VEC_HALFF (vector float){0.5f,0.5f,0.5f,0.5f}
|
||||
#define _VEC_HALFxyzZEROwF (vector float){0.5f,0.5f,0.5f,0.0f}
|
||||
#define _VEC_PERMUTE_XYZ0W1 (vector unsigned char){0x00,0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,0x09,0x0a,0x0b,0x1c,0x1d,0x1e,0x1f}
|
||||
|
||||
#define _VEC_IEEEHACK (vector float){(float)(1 << 23),(float)(1 << 23),(float)(1 << 23),(float)(1 << 23)}
|
||||
#define _VEC_PERMUTE_FASTFTOC (vector unsigned char){0,0,0,0,0,0,0,0,0,0,0,0,0x03,0x07,0x0b,0x0f}
|
||||
|
||||
// AngleQuaternion
|
||||
#define _VEC_PERMUTE_AQsxsxcxcx (vector unsigned char) {0x00,0x01,0x02,0x03,0x00,0x01,0x02,0x03,0x10,0x11,0x12,0x13,0x10,0x11,0x12,0x13}
|
||||
#define _VEC_PERMUTE_AQczszszcz (vector unsigned char) {0x18,0x19,0x1a,0x1b,0x08,0x09,0x0a,0x0b,0x08,0x09,0x0a,0x0b,0x18,0x19,0x1a,0x1b}
|
||||
#define _VEC_PERMUTE_AQcxcxsxsx (vector unsigned char) {0x10,0x11,0x12,0x13,0x10,0x11,0x12,0x13,0x00,0x01,0x02,0x03,0x00,0x01,0x02,0x03}
|
||||
#define _VEC_PERMUTE_AQszczczsz (vector unsigned char) {0x08,0x09,0x0a,0x0b,0x18,0x19,0x1a,0x1b,0x18,0x19,0x1a,0x1b,0x08,0x09,0x0a,0x0b}
|
||||
#define _VEC_PERMUTE_ANGLEQUAT (vector unsigned char) {0x10,0x11,0x12,0x13,0x04,0x05,0x06,0x07,0x18,0x19,0x1a,0x1b,0x0c,0x0d,0x0e,0x0f}
|
||||
|
||||
#define _VEC_EPSILONF (__vector float) {FLT_EPSILON,FLT_EPSILON,FLT_EPSILON,FLT_EPSILON}
|
||||
|
||||
#endif
|
||||
|
||||
#if !(defined( PLATFORM_PPC ) || defined(SPU))
|
||||
// If we are not PPC based or SPU based, then assumes it is SSE2. We should make this code cleaner.
|
||||
|
||||
#include <xmmintrin.h>
|
||||
|
||||
// These globals are initialized by mathlib and redirected based on available fpu features
|
||||
|
||||
// The following are not declared as macros because they are often used in limiting situations,
|
||||
// and sometimes the compiler simply refuses to inline them for some reason
|
||||
FORCEINLINE float FastSqrt( float x )
|
||||
{
|
||||
__m128 root = _mm_sqrt_ss( _mm_load_ss( &x ) );
|
||||
return *( reinterpret_cast<float *>( &root ) );
|
||||
}
|
||||
|
||||
FORCEINLINE float FastRSqrtFast( float x )
|
||||
{
|
||||
// use intrinsics
|
||||
__m128 rroot = _mm_rsqrt_ss( _mm_load_ss( &x ) );
|
||||
return *( reinterpret_cast<float *>( &rroot ) );
|
||||
}
|
||||
// Single iteration NewtonRaphson reciprocal square root:
|
||||
// 0.5 * rsqrtps * (3 - x * rsqrtps(x) * rsqrtps(x))
|
||||
// Very low error, and fine to use in place of 1.f / sqrtf(x).
|
||||
FORCEINLINE float FastRSqrt( float x )
|
||||
{
|
||||
float rroot = FastRSqrtFast( x );
|
||||
return (0.5f * rroot) * (3.f - (x * rroot) * rroot);
|
||||
}
|
||||
|
||||
void FastSinCos( float x, float* s, float* c ); // any x
|
||||
float FastCos( float x );
|
||||
|
||||
|
||||
|
||||
inline float FastRecip(float x) {return 1.0f / x;}
|
||||
// Simple SSE rsqrt. Usually accurate to around 6 (relative) decimal places
|
||||
// or so, so ok for closed transforms. (ie, computing lighting normals)
|
||||
inline float FastSqrtEst(float x) { return FastRSqrtFast(x) * x; }
|
||||
|
||||
|
||||
#else // !defined( PLATFORM_PPC ) && !defined(_SPU)
|
||||
|
||||
#ifndef SPU
|
||||
// We may not need this for SPU, so let's not bother for now
|
||||
|
||||
FORCEINLINE float _VMX_Sqrt( float x )
|
||||
{
|
||||
return __fsqrts( x );
|
||||
}
|
||||
|
||||
FORCEINLINE double _VMX_RSqrt( double x )
|
||||
{
|
||||
double rroot = __frsqrte( x );
|
||||
|
||||
// Single iteration NewtonRaphson on reciprocal square root estimate
|
||||
return (0.5f * rroot) * (3.0f - (x * rroot) * rroot);
|
||||
}
|
||||
|
||||
FORCEINLINE double _VMX_RSqrtFast( double x )
|
||||
{
|
||||
return __frsqrte( x );
|
||||
}
|
||||
|
||||
#ifdef _X360
|
||||
FORCEINLINE void _VMX_SinCos( float a, float *pS, float *pC )
|
||||
{
|
||||
XMScalarSinCos( pS, pC, a );
|
||||
}
|
||||
|
||||
FORCEINLINE float _VMX_Cos( float a )
|
||||
{
|
||||
return XMScalarCos( a );
|
||||
}
|
||||
#endif
|
||||
|
||||
// the 360 has fixed hw and calls directly
|
||||
#define FastSqrt(x) _VMX_Sqrt(x)
|
||||
#define FastRSqrt(x) _VMX_RSqrt(x)
|
||||
#define FastRSqrtFast(x) _VMX_RSqrtFast(x)
|
||||
#define FastSinCos(x,s,c) _VMX_SinCos(x,s,c)
|
||||
#define FastCos(x) _VMX_Cos(x)
|
||||
|
||||
inline double FastRecip(double x) {return __fres(x);}
|
||||
inline double FastSqrtEst(double x) { return __frsqrte(x) * x; }
|
||||
|
||||
#endif // !defined( PLATFORM_PPC ) && !defined(_SPU)
|
||||
|
||||
// if x is infinite, return FLT_MAX
|
||||
inline float FastClampInfinity( float x )
|
||||
{
|
||||
#ifdef PLATFORM_PPC
|
||||
return fsel( std::numeric_limits<float>::infinity() - x, x, FLT_MAX );
|
||||
#else
|
||||
return ( x > FLT_MAX ? FLT_MAX : x );
|
||||
#endif
|
||||
}
|
||||
|
||||
#if defined (_PS3) && !defined(SPU)
|
||||
|
||||
// extern float cosvf(float); /* single precision cosine */
|
||||
// extern float sinvf(float); /* single precision sine */
|
||||
// TODO: need a faster single precision equivalent
|
||||
#define cosvf cosf
|
||||
#define sinvf sinf
|
||||
|
||||
inline int _rotl( int x, int c )
|
||||
{
|
||||
return __rlwimi(x,x,c,0,31);
|
||||
}
|
||||
|
||||
inline int64 _rotl64( int64 x, int c )
|
||||
{
|
||||
return __rldicl( x, c, 0 );
|
||||
}
|
||||
|
||||
//-----------------------------------------------------------------
|
||||
// Vector Unions
|
||||
//-----------------------------------------------------------------
|
||||
|
||||
//-----------------------------------------------------------------
|
||||
// Floats
|
||||
//-----------------------------------------------------------------
|
||||
typedef union
|
||||
{
|
||||
vector float vf;
|
||||
float f[4];
|
||||
} vector_float_union;
|
||||
|
||||
//-----------------------------------------------------------------
|
||||
// Ints
|
||||
//-----------------------------------------------------------------
|
||||
typedef union
|
||||
{
|
||||
vector int vi;
|
||||
int i[4];
|
||||
} vector_int4_union;
|
||||
|
||||
typedef union
|
||||
{
|
||||
vector unsigned int vui;
|
||||
unsigned int ui[4];
|
||||
} vector_uint4_union;
|
||||
|
||||
//-----------------------------------------------------------------
|
||||
// Shorts
|
||||
//-----------------------------------------------------------------
|
||||
typedef union
|
||||
{
|
||||
vector signed short vs;
|
||||
signed short s[8];
|
||||
} vector_short8_union;
|
||||
|
||||
typedef union
|
||||
{
|
||||
vector unsigned short vus;
|
||||
unsigned short us[8];
|
||||
} vector_ushort8_union;
|
||||
|
||||
//-----------------------------------------------------------------
|
||||
// Chars
|
||||
//-----------------------------------------------------------------
|
||||
typedef union
|
||||
{
|
||||
vector signed char vc;
|
||||
signed char c[16];
|
||||
} vector_char16_union;
|
||||
|
||||
typedef union
|
||||
{
|
||||
vector unsigned char vuc;
|
||||
unsigned char uc[16];
|
||||
} vector_uchar16_union;
|
||||
|
||||
/*
|
||||
FORCEINLINE float _VMX_Sqrt( float x )
|
||||
{
|
||||
vector_float_union vIn, vOut;
|
||||
|
||||
vIn.f[0] = x;
|
||||
|
||||
vOut.vf = sqrtf4(vIn.vf);
|
||||
|
||||
return vOut.f[0];
|
||||
}
|
||||
|
||||
FORCEINLINE float _VMX_RSqrt( float x )
|
||||
{
|
||||
vector_float_union vIn, vOut;
|
||||
|
||||
vIn.f[0] = x;
|
||||
|
||||
vOut.vf = rsqrtf4(vIn.vf);
|
||||
|
||||
return vOut.f[0];
|
||||
}
|
||||
|
||||
FORCEINLINE float _VMX_RSqrtFast( float x )
|
||||
{
|
||||
vector_float_union vIn, vOut;
|
||||
|
||||
vIn.f[0] = x;
|
||||
|
||||
vOut.vf = rsqrtf4fast(vIn.vf);
|
||||
|
||||
return vOut.f[0];
|
||||
}
|
||||
*/
|
||||
|
||||
FORCEINLINE void _VMX_SinCos( float a, float *pS, float *pC )
|
||||
{
|
||||
*pS=sinvf(a);
|
||||
*pC=cosvf(a);
|
||||
}
|
||||
|
||||
FORCEINLINE float _VMX_Cos( float a )
|
||||
{
|
||||
return cosvf(a);
|
||||
}
|
||||
|
||||
|
||||
// the 360 has fixed hw and calls directly
|
||||
/*
|
||||
#define FastSqrt(x) _VMX_Sqrt(x)
|
||||
#define FastRSqrt(x) _VMX_RSqrt(x)
|
||||
#define FastRSqrtFast(x) _VMX_RSqrtFast(x)
|
||||
#define FastSinCos(x,s,c) _VMX_SinCos(x,s,c)
|
||||
#define FastCos(x) _VMX_Cos(x)
|
||||
*/
|
||||
#endif // _PS3
|
||||
#endif // #ifndef SPU
|
||||
|
||||
#endif // _MATH_PFNS_H_
|
||||
+2425
File diff suppressed because it is too large
Load Diff
Vendored
+2633
File diff suppressed because it is too large
Load Diff
+670
@@ -0,0 +1,670 @@
|
||||
//========= Copyright © 1996-2005, Valve Corporation, All rights reserved. ============//
|
||||
//
|
||||
// Purpose:
|
||||
//
|
||||
// $NoKeywords: $
|
||||
//
|
||||
//=============================================================================//
|
||||
|
||||
#ifndef VECTOR2D_H
|
||||
#define VECTOR2D_H
|
||||
|
||||
#ifdef _WIN32
|
||||
#pragma once
|
||||
#endif
|
||||
|
||||
#include <math.h>
|
||||
#include <float.h>
|
||||
|
||||
// For vec_t, put this somewhere else?
|
||||
#include "tier0/basetypes.h"
|
||||
|
||||
// For RandomFloat()
|
||||
#include "vstdlib/random.h"
|
||||
|
||||
#include "tier0/dbg.h"
|
||||
#include "mathlib/math_pfns.h"
|
||||
|
||||
//=========================================================
|
||||
// 2D Vector2D
|
||||
//=========================================================
|
||||
|
||||
class Vector2D
|
||||
{
|
||||
public:
|
||||
// Members
|
||||
vec_t x, y;
|
||||
|
||||
// Construction/destruction
|
||||
Vector2D(void);
|
||||
Vector2D(vec_t X, vec_t Y);
|
||||
Vector2D(const float *pFloat);
|
||||
|
||||
// Initialization
|
||||
void Init(vec_t ix=0.0f, vec_t iy=0.0f);
|
||||
|
||||
// Got any nasty NAN's?
|
||||
bool IsValid() const;
|
||||
|
||||
// array access...
|
||||
vec_t operator[](int i) const;
|
||||
vec_t& operator[](int i);
|
||||
|
||||
// Base address...
|
||||
vec_t* Base();
|
||||
vec_t const* Base() const;
|
||||
|
||||
// Initialization methods
|
||||
void Random( float minVal, float maxVal );
|
||||
|
||||
// equality
|
||||
bool operator==(const Vector2D& v) const;
|
||||
bool operator!=(const Vector2D& v) const;
|
||||
|
||||
// arithmetic operations
|
||||
Vector2D& operator+=(const Vector2D &v);
|
||||
Vector2D& operator-=(const Vector2D &v);
|
||||
Vector2D& operator*=(const Vector2D &v);
|
||||
Vector2D& operator*=(float s);
|
||||
Vector2D& operator/=(const Vector2D &v);
|
||||
Vector2D& operator/=(float s);
|
||||
|
||||
// negate the Vector2D components
|
||||
void Negate();
|
||||
|
||||
// Get the Vector2D's magnitude.
|
||||
vec_t Length() const;
|
||||
|
||||
// Get the Vector2D's magnitude squared.
|
||||
vec_t LengthSqr(void) const;
|
||||
|
||||
// return true if this vector is (0,0) within tolerance
|
||||
bool IsZero( float tolerance = 0.01f ) const
|
||||
{
|
||||
return (x > -tolerance && x < tolerance &&
|
||||
y > -tolerance && y < tolerance);
|
||||
}
|
||||
|
||||
// Normalize in place and return the old length.
|
||||
vec_t NormalizeInPlace();
|
||||
|
||||
// Compare length.
|
||||
bool IsLengthGreaterThan( float val ) const;
|
||||
bool IsLengthLessThan( float val ) const;
|
||||
|
||||
// Get the distance from this Vector2D to the other one.
|
||||
vec_t DistTo(const Vector2D &vOther) const;
|
||||
|
||||
// Get the distance from this Vector2D to the other one squared.
|
||||
vec_t DistToSqr(const Vector2D &vOther) const;
|
||||
|
||||
// Copy
|
||||
void CopyToArray(float* rgfl) const;
|
||||
|
||||
// Multiply, add, and assign to this (ie: *this = a + b * scalar). This
|
||||
// is about 12% faster than the actual Vector2D equation (because it's done per-component
|
||||
// rather than per-Vector2D).
|
||||
void MulAdd(const Vector2D& a, const Vector2D& b, float scalar);
|
||||
|
||||
// Dot product.
|
||||
vec_t Dot(const Vector2D& vOther) const;
|
||||
|
||||
// assignment
|
||||
Vector2D& operator=(const Vector2D &vOther);
|
||||
|
||||
#ifndef VECTOR_NO_SLOW_OPERATIONS
|
||||
// copy constructors
|
||||
Vector2D(const Vector2D &vOther);
|
||||
|
||||
// arithmetic operations
|
||||
Vector2D operator-(void) const;
|
||||
|
||||
Vector2D operator+(const Vector2D& v) const;
|
||||
Vector2D operator-(const Vector2D& v) const;
|
||||
Vector2D operator*(const Vector2D& v) const;
|
||||
Vector2D operator/(const Vector2D& v) const;
|
||||
Vector2D operator*(float fl) const;
|
||||
Vector2D operator/(float fl) const;
|
||||
|
||||
// Cross product between two vectors.
|
||||
Vector2D Cross(const Vector2D &vOther) const;
|
||||
|
||||
// Returns a Vector2D with the min or max in X, Y, and Z.
|
||||
Vector2D Min(const Vector2D &vOther) const;
|
||||
Vector2D Max(const Vector2D &vOther) const;
|
||||
|
||||
#else
|
||||
|
||||
private:
|
||||
// No copy constructors allowed if we're in optimal mode
|
||||
Vector2D(const Vector2D& vOther);
|
||||
#endif
|
||||
};
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
|
||||
const Vector2D vec2_origin(0,0);
|
||||
const Vector2D vec2_invalid( FLT_MAX, FLT_MAX );
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
// Vector2D related operations
|
||||
//-----------------------------------------------------------------------------
|
||||
|
||||
// Vector2D clear
|
||||
void Vector2DClear( Vector2D& a );
|
||||
|
||||
// Copy
|
||||
void Vector2DCopy( const Vector2D& src, Vector2D& dst );
|
||||
|
||||
// Vector2D arithmetic
|
||||
void Vector2DAdd( const Vector2D& a, const Vector2D& b, Vector2D& result );
|
||||
void Vector2DSubtract( const Vector2D& a, const Vector2D& b, Vector2D& result );
|
||||
void Vector2DMultiply( const Vector2D& a, vec_t b, Vector2D& result );
|
||||
void Vector2DMultiply( const Vector2D& a, const Vector2D& b, Vector2D& result );
|
||||
void Vector2DDivide( const Vector2D& a, vec_t b, Vector2D& result );
|
||||
void Vector2DDivide( const Vector2D& a, const Vector2D& b, Vector2D& result );
|
||||
void Vector2DMA( const Vector2D& start, float s, const Vector2D& dir, Vector2D& result );
|
||||
|
||||
// Store the min or max of each of x, y, and z into the result.
|
||||
void Vector2DMin( const Vector2D &a, const Vector2D &b, Vector2D &result );
|
||||
void Vector2DMax( const Vector2D &a, const Vector2D &b, Vector2D &result );
|
||||
|
||||
#define Vector2DExpand( v ) (v).x, (v).y
|
||||
|
||||
// Normalization
|
||||
vec_t Vector2DNormalize( Vector2D& v );
|
||||
|
||||
// Length
|
||||
vec_t Vector2DLength( const Vector2D& v );
|
||||
|
||||
// Dot Product
|
||||
vec_t DotProduct2D(const Vector2D& a, const Vector2D& b);
|
||||
|
||||
// Linearly interpolate between two vectors
|
||||
void Vector2DLerp(const Vector2D& src1, const Vector2D& src2, vec_t t, Vector2D& dest );
|
||||
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
//
|
||||
// Inlined Vector2D methods
|
||||
//
|
||||
//-----------------------------------------------------------------------------
|
||||
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
// constructors
|
||||
//-----------------------------------------------------------------------------
|
||||
|
||||
inline Vector2D::Vector2D(void)
|
||||
{
|
||||
#ifdef _DEBUG
|
||||
// Initialize to NAN to catch errors
|
||||
x = y = VEC_T_NAN;
|
||||
#endif
|
||||
}
|
||||
|
||||
inline Vector2D::Vector2D(vec_t X, vec_t Y)
|
||||
{
|
||||
x = X; y = Y;
|
||||
Assert( IsValid() );
|
||||
}
|
||||
|
||||
inline Vector2D::Vector2D(const float *pFloat)
|
||||
{
|
||||
Assert( pFloat );
|
||||
x = pFloat[0]; y = pFloat[1];
|
||||
Assert( IsValid() );
|
||||
}
|
||||
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
// copy constructor
|
||||
//-----------------------------------------------------------------------------
|
||||
|
||||
inline Vector2D::Vector2D(const Vector2D &vOther)
|
||||
{
|
||||
Assert( vOther.IsValid() );
|
||||
x = vOther.x; y = vOther.y;
|
||||
}
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
// initialization
|
||||
//-----------------------------------------------------------------------------
|
||||
|
||||
inline void Vector2D::Init( vec_t ix, vec_t iy )
|
||||
{
|
||||
x = ix; y = iy;
|
||||
Assert( IsValid() );
|
||||
}
|
||||
|
||||
inline void Vector2D::Random( float minVal, float maxVal )
|
||||
{
|
||||
x = RandomFloat( minVal , maxVal );
|
||||
y = RandomFloat( minVal , maxVal );
|
||||
}
|
||||
|
||||
inline void Vector2DClear( Vector2D& a )
|
||||
{
|
||||
a.x = a.y = 0.0f;
|
||||
}
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
// assignment
|
||||
//-----------------------------------------------------------------------------
|
||||
|
||||
inline Vector2D& Vector2D::operator=(const Vector2D &vOther)
|
||||
{
|
||||
Assert( vOther.IsValid() );
|
||||
x=vOther.x; y=vOther.y;
|
||||
return *this;
|
||||
}
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
// Array access
|
||||
//-----------------------------------------------------------------------------
|
||||
|
||||
inline vec_t& Vector2D::operator[](int i)
|
||||
{
|
||||
Assert( (i >= 0) && (i < 2) );
|
||||
return ((vec_t*)this)[i];
|
||||
}
|
||||
|
||||
inline vec_t Vector2D::operator[](int i) const
|
||||
{
|
||||
Assert( (i >= 0) && (i < 2) );
|
||||
return ((vec_t*)this)[i];
|
||||
}
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
// Base address...
|
||||
//-----------------------------------------------------------------------------
|
||||
|
||||
inline vec_t* Vector2D::Base()
|
||||
{
|
||||
return (vec_t*)this;
|
||||
}
|
||||
|
||||
inline vec_t const* Vector2D::Base() const
|
||||
{
|
||||
return (vec_t const*)this;
|
||||
}
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
// IsValid?
|
||||
//-----------------------------------------------------------------------------
|
||||
|
||||
inline bool Vector2D::IsValid() const
|
||||
{
|
||||
return IsFinite(x) && IsFinite(y);
|
||||
}
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
// comparison
|
||||
//-----------------------------------------------------------------------------
|
||||
|
||||
inline bool Vector2D::operator==( const Vector2D& src ) const
|
||||
{
|
||||
Assert( src.IsValid() && IsValid() );
|
||||
return (src.x == x) && (src.y == y);
|
||||
}
|
||||
|
||||
inline bool Vector2D::operator!=( const Vector2D& src ) const
|
||||
{
|
||||
Assert( src.IsValid() && IsValid() );
|
||||
return (src.x != x) || (src.y != y);
|
||||
}
|
||||
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
// Copy
|
||||
//-----------------------------------------------------------------------------
|
||||
|
||||
inline void Vector2DCopy( const Vector2D& src, Vector2D& dst )
|
||||
{
|
||||
Assert( src.IsValid() );
|
||||
dst.x = src.x;
|
||||
dst.y = src.y;
|
||||
}
|
||||
|
||||
inline void Vector2D::CopyToArray(float* rgfl) const
|
||||
{
|
||||
Assert( IsValid() );
|
||||
Assert( rgfl );
|
||||
rgfl[0] = x; rgfl[1] = y;
|
||||
}
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
// standard math operations
|
||||
//-----------------------------------------------------------------------------
|
||||
|
||||
inline void Vector2D::Negate()
|
||||
{
|
||||
Assert( IsValid() );
|
||||
x = -x; y = -y;
|
||||
}
|
||||
|
||||
inline Vector2D& Vector2D::operator+=(const Vector2D& v)
|
||||
{
|
||||
Assert( IsValid() && v.IsValid() );
|
||||
x+=v.x; y+=v.y;
|
||||
return *this;
|
||||
}
|
||||
|
||||
inline Vector2D& Vector2D::operator-=(const Vector2D& v)
|
||||
{
|
||||
Assert( IsValid() && v.IsValid() );
|
||||
x-=v.x; y-=v.y;
|
||||
return *this;
|
||||
}
|
||||
|
||||
inline Vector2D& Vector2D::operator*=(float fl)
|
||||
{
|
||||
x *= fl;
|
||||
y *= fl;
|
||||
Assert( IsValid() );
|
||||
return *this;
|
||||
}
|
||||
|
||||
inline Vector2D& Vector2D::operator*=(const Vector2D& v)
|
||||
{
|
||||
x *= v.x;
|
||||
y *= v.y;
|
||||
Assert( IsValid() );
|
||||
return *this;
|
||||
}
|
||||
|
||||
inline Vector2D& Vector2D::operator/=(float fl)
|
||||
{
|
||||
Assert( fl != 0.0f );
|
||||
float oofl = 1.0f / fl;
|
||||
x *= oofl;
|
||||
y *= oofl;
|
||||
Assert( IsValid() );
|
||||
return *this;
|
||||
}
|
||||
|
||||
inline Vector2D& Vector2D::operator/=(const Vector2D& v)
|
||||
{
|
||||
Assert( v.x != 0.0f && v.y != 0.0f );
|
||||
x /= v.x;
|
||||
y /= v.y;
|
||||
Assert( IsValid() );
|
||||
return *this;
|
||||
}
|
||||
|
||||
inline void Vector2DAdd( const Vector2D& a, const Vector2D& b, Vector2D& c )
|
||||
{
|
||||
Assert( a.IsValid() && b.IsValid() );
|
||||
c.x = a.x + b.x;
|
||||
c.y = a.y + b.y;
|
||||
}
|
||||
|
||||
inline void Vector2DSubtract( const Vector2D& a, const Vector2D& b, Vector2D& c )
|
||||
{
|
||||
Assert( a.IsValid() && b.IsValid() );
|
||||
c.x = a.x - b.x;
|
||||
c.y = a.y - b.y;
|
||||
}
|
||||
|
||||
inline void Vector2DMultiply( const Vector2D& a, vec_t b, Vector2D& c )
|
||||
{
|
||||
Assert( a.IsValid() && IsFinite(b) );
|
||||
c.x = a.x * b;
|
||||
c.y = a.y * b;
|
||||
}
|
||||
|
||||
inline void Vector2DMultiply( const Vector2D& a, const Vector2D& b, Vector2D& c )
|
||||
{
|
||||
Assert( a.IsValid() && b.IsValid() );
|
||||
c.x = a.x * b.x;
|
||||
c.y = a.y * b.y;
|
||||
}
|
||||
|
||||
|
||||
inline void Vector2DDivide( const Vector2D& a, vec_t b, Vector2D& c )
|
||||
{
|
||||
Assert( a.IsValid() );
|
||||
Assert( b != 0.0f );
|
||||
vec_t oob = 1.0f / b;
|
||||
c.x = a.x * oob;
|
||||
c.y = a.y * oob;
|
||||
}
|
||||
|
||||
inline void Vector2DDivide( const Vector2D& a, const Vector2D& b, Vector2D& c )
|
||||
{
|
||||
Assert( a.IsValid() );
|
||||
Assert( (b.x != 0.0f) && (b.y != 0.0f) );
|
||||
c.x = a.x / b.x;
|
||||
c.y = a.y / b.y;
|
||||
}
|
||||
|
||||
inline void Vector2DMA( const Vector2D& start, float s, const Vector2D& dir, Vector2D& result )
|
||||
{
|
||||
Assert( start.IsValid() && IsFinite(s) && dir.IsValid() );
|
||||
result.x = start.x + s*dir.x;
|
||||
result.y = start.y + s*dir.y;
|
||||
}
|
||||
|
||||
// FIXME: Remove
|
||||
// For backwards compatability
|
||||
inline void Vector2D::MulAdd(const Vector2D& a, const Vector2D& b, float scalar)
|
||||
{
|
||||
x = a.x + b.x * scalar;
|
||||
y = a.y + b.y * scalar;
|
||||
}
|
||||
|
||||
inline void Vector2DLerp(const Vector2D& src1, const Vector2D& src2, vec_t t, Vector2D& dest )
|
||||
{
|
||||
dest[0] = src1[0] + (src2[0] - src1[0]) * t;
|
||||
dest[1] = src1[1] + (src2[1] - src1[1]) * t;
|
||||
}
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
// dot, cross
|
||||
//-----------------------------------------------------------------------------
|
||||
inline vec_t DotProduct2D(const Vector2D& a, const Vector2D& b)
|
||||
{
|
||||
Assert( a.IsValid() && b.IsValid() );
|
||||
return( a.x*b.x + a.y*b.y );
|
||||
}
|
||||
|
||||
// for backwards compatability
|
||||
inline vec_t Vector2D::Dot( const Vector2D& vOther ) const
|
||||
{
|
||||
return DotProduct2D( *this, vOther );
|
||||
}
|
||||
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
// length
|
||||
//-----------------------------------------------------------------------------
|
||||
inline vec_t Vector2DLength( const Vector2D& v )
|
||||
{
|
||||
Assert( v.IsValid() );
|
||||
return (vec_t)FastSqrt(v.x*v.x + v.y*v.y);
|
||||
}
|
||||
|
||||
inline vec_t Vector2D::LengthSqr(void) const
|
||||
{
|
||||
Assert( IsValid() );
|
||||
return (x*x + y*y);
|
||||
}
|
||||
|
||||
inline vec_t Vector2D::NormalizeInPlace()
|
||||
{
|
||||
return Vector2DNormalize( *this );
|
||||
}
|
||||
|
||||
inline bool Vector2D::IsLengthGreaterThan( float val ) const
|
||||
{
|
||||
return LengthSqr() > val*val;
|
||||
}
|
||||
|
||||
inline bool Vector2D::IsLengthLessThan( float val ) const
|
||||
{
|
||||
return LengthSqr() < val*val;
|
||||
}
|
||||
|
||||
inline vec_t Vector2D::Length(void) const
|
||||
{
|
||||
return Vector2DLength( *this );
|
||||
}
|
||||
|
||||
|
||||
inline void Vector2DMin( const Vector2D &a, const Vector2D &b, Vector2D &result )
|
||||
{
|
||||
result.x = (a.x < b.x) ? a.x : b.x;
|
||||
result.y = (a.y < b.y) ? a.y : b.y;
|
||||
}
|
||||
|
||||
|
||||
inline void Vector2DMax( const Vector2D &a, const Vector2D &b, Vector2D &result )
|
||||
{
|
||||
result.x = (a.x > b.x) ? a.x : b.x;
|
||||
result.y = (a.y > b.y) ? a.y : b.y;
|
||||
}
|
||||
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
// Normalization
|
||||
//-----------------------------------------------------------------------------
|
||||
inline vec_t Vector2DNormalize( Vector2D& v )
|
||||
{
|
||||
Assert( v.IsValid() );
|
||||
vec_t l = v.Length();
|
||||
if (l != 0.0f)
|
||||
{
|
||||
v /= l;
|
||||
}
|
||||
else
|
||||
{
|
||||
v.x = v.y = 0.0f;
|
||||
}
|
||||
return l;
|
||||
}
|
||||
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
// Get the distance from this Vector2D to the other one
|
||||
//-----------------------------------------------------------------------------
|
||||
inline vec_t Vector2D::DistTo(const Vector2D &vOther) const
|
||||
{
|
||||
Vector2D delta;
|
||||
Vector2DSubtract( *this, vOther, delta );
|
||||
return delta.Length();
|
||||
}
|
||||
|
||||
inline vec_t Vector2D::DistToSqr(const Vector2D &vOther) const
|
||||
{
|
||||
Vector2D delta;
|
||||
Vector2DSubtract( *this, vOther, delta );
|
||||
return delta.LengthSqr();
|
||||
}
|
||||
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
// Computes the closest point to vecTarget no farther than flMaxDist from vecStart
|
||||
//-----------------------------------------------------------------------------
|
||||
inline void ComputeClosestPoint2D( const Vector2D& vecStart, float flMaxDist, const Vector2D& vecTarget, Vector2D *pResult )
|
||||
{
|
||||
Vector2D vecDelta;
|
||||
Vector2DSubtract( vecTarget, vecStart, vecDelta );
|
||||
float flDistSqr = vecDelta.LengthSqr();
|
||||
if ( flDistSqr <= flMaxDist * flMaxDist )
|
||||
{
|
||||
*pResult = vecTarget;
|
||||
}
|
||||
else
|
||||
{
|
||||
vecDelta /= FastSqrt( flDistSqr );
|
||||
Vector2DMA( vecStart, flMaxDist, vecDelta, *pResult );
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
//
|
||||
// Slow methods
|
||||
//
|
||||
//-----------------------------------------------------------------------------
|
||||
|
||||
#ifndef VECTOR_NO_SLOW_OPERATIONS
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
// Returns a Vector2D with the min or max in X, Y, and Z.
|
||||
//-----------------------------------------------------------------------------
|
||||
|
||||
inline Vector2D Vector2D::Min(const Vector2D &vOther) const
|
||||
{
|
||||
return Vector2D(x < vOther.x ? x : vOther.x,
|
||||
y < vOther.y ? y : vOther.y);
|
||||
}
|
||||
|
||||
inline Vector2D Vector2D::Max(const Vector2D &vOther) const
|
||||
{
|
||||
return Vector2D(x > vOther.x ? x : vOther.x,
|
||||
y > vOther.y ? y : vOther.y);
|
||||
}
|
||||
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
// arithmetic operations
|
||||
//-----------------------------------------------------------------------------
|
||||
|
||||
inline Vector2D Vector2D::operator-(void) const
|
||||
{
|
||||
return Vector2D(-x,-y);
|
||||
}
|
||||
|
||||
inline Vector2D Vector2D::operator+(const Vector2D& v) const
|
||||
{
|
||||
Vector2D res;
|
||||
Vector2DAdd( *this, v, res );
|
||||
return res;
|
||||
}
|
||||
|
||||
inline Vector2D Vector2D::operator-(const Vector2D& v) const
|
||||
{
|
||||
Vector2D res;
|
||||
Vector2DSubtract( *this, v, res );
|
||||
return res;
|
||||
}
|
||||
|
||||
inline Vector2D Vector2D::operator*(float fl) const
|
||||
{
|
||||
Vector2D res;
|
||||
Vector2DMultiply( *this, fl, res );
|
||||
return res;
|
||||
}
|
||||
|
||||
inline Vector2D Vector2D::operator*(const Vector2D& v) const
|
||||
{
|
||||
Vector2D res;
|
||||
Vector2DMultiply( *this, v, res );
|
||||
return res;
|
||||
}
|
||||
|
||||
inline Vector2D Vector2D::operator/(float fl) const
|
||||
{
|
||||
Vector2D res;
|
||||
Vector2DDivide( *this, fl, res );
|
||||
return res;
|
||||
}
|
||||
|
||||
inline Vector2D Vector2D::operator/(const Vector2D& v) const
|
||||
{
|
||||
Vector2D res;
|
||||
Vector2DDivide( *this, v, res );
|
||||
return res;
|
||||
}
|
||||
|
||||
inline Vector2D operator*(float fl, const Vector2D& v)
|
||||
{
|
||||
return v * fl;
|
||||
}
|
||||
|
||||
#endif //slow
|
||||
|
||||
#endif // VECTOR2D_H
|
||||
|
||||
Reference in New Issue
Block a user