17 #ifndef _VECTORMATH_VEC_AOS_CPP_H
18 #define _VECTORMATH_VEC_AOS_CPP_H
23 #define _VECTORMATH_SLERP_TOL 0.999f
28 #ifndef _VECTORMATH_INTERNAL_FUNCTIONS
29 #define _VECTORMATH_INTERNAL_FUNCTIONS
33 namespace Vectormath {
66 return Vector3( 1.0f, 0.0f, 0.0f );
71 return Vector3( 0.0f, 1.0f, 0.0f );
76 return Vector3( 0.0f, 0.0f, 1.0f );
81 return ( vec0 + ( ( vec1 - vec0 ) * t ) );
86 float recipSinAngle, scale0, scale1, cosAngle, angle;
87 cosAngle =
dot( unitVec0, unitVec1 );
89 angle = acosf( cosAngle );
90 recipSinAngle = ( 1.0f / sinf( angle ) );
91 scale0 = ( sinf( ( ( 1.0f - t ) * angle ) ) * recipSinAngle );
92 scale1 = ( sinf( ( t * angle ) ) * recipSinAngle );
94 scale0 = ( 1.0f - t );
97 return ( ( unitVec0 * scale0 ) + ( unitVec1 * scale1 ) );
102 vec =
Vector3( fptr[0], fptr[1], fptr[2] );
107 fptr[0] = vec.
getX();
108 fptr[1] = vec.
getY();
109 fptr[2] = vec.
getZ();
119 for (
int i = 0; i < 3; i++) {
120 unsigned short fp16 = hfptr[i];
121 unsigned int sign = fp16 >> 15;
122 unsigned int exponent = (fp16 >> 10) & ((1 << 5) - 1);
123 unsigned int mantissa = fp16 & ((1 << 10) - 1);
129 }
else if (exponent == 31) {
135 exponent += 127 - 15;
140 d.u32 = (sign << 31) | (exponent << 23) | mantissa;
152 for (
int i = 0; i < 3; i++) {
156 unsigned int sign = d.u32 >> 31;
157 unsigned int exponent = (d.u32 >> 23) & ((1 << 8) - 1);
158 unsigned int mantissa = d.u32 & ((1 << 23) - 1);;
164 }
else if (exponent == 255 && mantissa != 0) {
169 }
else if (exponent >= 127 - 15 + 31) {
174 }
else if (exponent <= 127 - 15) {
180 exponent -= 127 - 15;
184 hfptr[i] = (
unsigned short)((sign << 15) | (exponent << 10) | mantissa);
231 *(&
mX + idx) = value;
300 *
this = *
this * scalar;
315 *
this = *
this / scalar;
354 ( 1.0f / vec.
getX() ),
355 ( 1.0f / vec.
getY() ),
356 ( 1.0f / vec.
getZ() )
372 ( 1.0f / sqrtf( vec.
getX() ) ),
373 ( 1.0f / sqrtf( vec.
getY() ) ),
374 ( 1.0f / sqrtf( vec.
getZ() ) )
390 ( vec1.
getX() < 0.0f )? -fabsf( vec0.
getX() ) : fabsf( vec0.
getX() ),
391 ( vec1.
getY() < 0.0f )? -fabsf( vec0.
getY() ) : fabsf( vec0.
getY() ),
392 ( vec1.
getZ() < 0.0f )? -fabsf( vec0.
getZ() ) : fabsf( vec0.
getZ() )
409 result = (vec.
getZ() > result)? vec.
getZ() : result;
426 result = (vec.
getZ() < result)? vec.
getZ() : result;
433 result = ( vec.
getX() + vec.
getY() );
434 result = ( result + vec.
getZ() );
441 result = ( vec0.
getX() * vec1.
getX() );
442 result = ( result + ( vec0.
getY() * vec1.
getY() ) );
443 result = ( result + ( vec0.
getZ() * vec1.
getZ() ) );
450 result = ( vec.
getX() * vec.
getX() );
451 result = ( result + ( vec.
getY() * vec.
getY() ) );
452 result = ( result + ( vec.
getZ() * vec.
getZ() ) );
463 float lenSqr, lenInv;
465 lenInv = ( 1.0f / sqrtf( lenSqr ) );
467 ( vec.
getX() * lenInv ),
468 ( vec.
getY() * lenInv ),
469 ( vec.
getZ() * lenInv )
485 ( select1 )? vec1.
getX() : vec0.
getX(),
486 ( select1 )? vec1.
getY() : vec0.
getY(),
487 ( select1 )? vec1.
getZ() : vec0.
getZ()
491 #ifdef _VECTORMATH_DEBUG
493 inline void print(
const Vector3 & vec )
495 printf(
"( %f %f %f )\n", vec.getX(), vec.getY(), vec.getZ() );
498 inline void print(
const Vector3 & vec,
const char * name )
500 printf(
"%s: ( %f %f %f )\n", name, vec.getX(), vec.getY(), vec.getZ() );
561 return Vector4( 1.0f, 0.0f, 0.0f, 0.0f );
566 return Vector4( 0.0f, 1.0f, 0.0f, 0.0f );
571 return Vector4( 0.0f, 0.0f, 1.0f, 0.0f );
576 return Vector4( 0.0f, 0.0f, 0.0f, 1.0f );
581 return ( vec0 + ( ( vec1 - vec0 ) * t ) );
586 float recipSinAngle, scale0, scale1, cosAngle, angle;
587 cosAngle =
dot( unitVec0, unitVec1 );
589 angle = acosf( cosAngle );
590 recipSinAngle = ( 1.0f / sinf( angle ) );
591 scale0 = ( sinf( ( ( 1.0f - t ) * angle ) ) * recipSinAngle );
592 scale1 = ( sinf( ( t * angle ) ) * recipSinAngle );
594 scale0 = ( 1.0f - t );
597 return ( ( unitVec0 * scale0 ) + ( unitVec1 * scale1 ) );
602 vec =
Vector4( fptr[0], fptr[1], fptr[2], fptr[3] );
607 fptr[0] = vec.
getX();
608 fptr[1] = vec.
getY();
609 fptr[2] = vec.
getZ();
610 fptr[3] = vec.
getW();
620 for (
int i = 0; i < 4; i++) {
621 unsigned short fp16 = hfptr[i];
622 unsigned int sign = fp16 >> 15;
623 unsigned int exponent = (fp16 >> 10) & ((1 << 5) - 1);
624 unsigned int mantissa = fp16 & ((1 << 10) - 1);
630 }
else if (exponent == 31) {
636 exponent += 127 - 15;
641 d.u32 = (sign << 31) | (exponent << 23) | mantissa;
653 for (
int i = 0; i < 4; i++) {
657 unsigned int sign = d.u32 >> 31;
658 unsigned int exponent = (d.u32 >> 23) & ((1 << 8) - 1);
659 unsigned int mantissa = d.u32 & ((1 << 23) - 1);;
665 }
else if (exponent == 255 && mantissa != 0) {
670 }
else if (exponent >= 127 - 15 + 31) {
675 }
else if (exponent <= 127 - 15) {
681 exponent -= 127 - 15;
685 hfptr[i] = (
unsigned short)((sign << 15) | (exponent << 10) | mantissa);
757 *(&
mX + idx) = value;
820 *
this = *
this * scalar;
836 *
this = *
this / scalar;
878 ( 1.0f / vec.
getX() ),
879 ( 1.0f / vec.
getY() ),
880 ( 1.0f / vec.
getZ() ),
881 ( 1.0f / vec.
getW() )
898 ( 1.0f / sqrtf( vec.
getX() ) ),
899 ( 1.0f / sqrtf( vec.
getY() ) ),
900 ( 1.0f / sqrtf( vec.
getZ() ) ),
901 ( 1.0f / sqrtf( vec.
getW() ) )
918 ( vec1.
getX() < 0.0f )? -fabsf( vec0.
getX() ) : fabsf( vec0.
getX() ),
919 ( vec1.
getY() < 0.0f )? -fabsf( vec0.
getY() ) : fabsf( vec0.
getY() ),
920 ( vec1.
getZ() < 0.0f )? -fabsf( vec0.
getZ() ) : fabsf( vec0.
getZ() ),
921 ( vec1.
getW() < 0.0f )? -fabsf( vec0.
getW() ) : fabsf( vec0.
getW() )
939 result = (vec.
getZ() > result)? vec.
getZ() : result;
940 result = (vec.
getW() > result)? vec.
getW() : result;
958 result = (vec.
getZ() < result)? vec.
getZ() : result;
959 result = (vec.
getW() < result)? vec.
getW() : result;
966 result = ( vec.
getX() + vec.
getY() );
967 result = ( result + vec.
getZ() );
968 result = ( result + vec.
getW() );
975 result = ( vec0.
getX() * vec1.
getX() );
976 result = ( result + ( vec0.
getY() * vec1.
getY() ) );
977 result = ( result + ( vec0.
getZ() * vec1.
getZ() ) );
978 result = ( result + ( vec0.
getW() * vec1.
getW() ) );
985 result = ( vec.
getX() * vec.
getX() );
986 result = ( result + ( vec.
getY() * vec.
getY() ) );
987 result = ( result + ( vec.
getZ() * vec.
getZ() ) );
988 result = ( result + ( vec.
getW() * vec.
getW() ) );
999 float lenSqr, lenInv;
1001 lenInv = ( 1.0f / sqrtf( lenSqr ) );
1003 ( vec.
getX() * lenInv ),
1004 ( vec.
getY() * lenInv ),
1005 ( vec.
getZ() * lenInv ),
1006 ( vec.
getW() * lenInv )
1013 ( select1 )? vec1.
getX() : vec0.
getX(),
1014 ( select1 )? vec1.
getY() : vec0.
getY(),
1015 ( select1 )? vec1.
getZ() : vec0.
getZ(),
1016 ( select1 )? vec1.
getW() : vec0.
getW()
1020 #ifdef _VECTORMATH_DEBUG
1022 inline void print(
const Vector4 & vec )
1024 printf(
"( %f %f %f %f )\n", vec.getX(), vec.getY(), vec.getZ(), vec.getW() );
1027 inline void print(
const Vector4 & vec,
const char * name )
1029 printf(
"%s: ( %f %f %f %f )\n", name, vec.getX(), vec.getY(), vec.getZ(), vec.getW() );
1064 return ( pnt0 + ( ( pnt1 - pnt0 ) * t ) );
1069 pnt =
Point3( fptr[0], fptr[1], fptr[2] );
1074 fptr[0] = pnt.
getX();
1075 fptr[1] = pnt.
getY();
1076 fptr[2] = pnt.
getZ();
1086 for (
int i = 0; i < 3; i++) {
1087 unsigned short fp16 = hfptr[i];
1088 unsigned int sign = fp16 >> 15;
1089 unsigned int exponent = (fp16 >> 10) & ((1 << 5) - 1);
1090 unsigned int mantissa = fp16 & ((1 << 10) - 1);
1092 if (exponent == 0) {
1096 }
else if (exponent == 31) {
1102 exponent += 127 - 15;
1107 d.u32 = (sign << 31) | (exponent << 23) | mantissa;
1119 for (
int i = 0; i < 3; i++) {
1123 unsigned int sign = d.u32 >> 31;
1124 unsigned int exponent = (d.u32 >> 23) & ((1 << 8) - 1);
1125 unsigned int mantissa = d.u32 & ((1 << 23) - 1);;
1127 if (exponent == 0) {
1131 }
else if (exponent == 255 && mantissa != 0) {
1136 }
else if (exponent >= 127 - 15 + 31) {
1141 }
else if (exponent <= 127 - 15) {
1147 exponent -= 127 - 15;
1151 hfptr[i] = (
unsigned short)((sign << 15) | (exponent << 10) | mantissa);
1198 *(&
mX + idx) = value;
1204 return *(&
mX + idx);
1209 return *(&
mX + idx);
1214 return *(&
mX + idx);
1246 *
this = *
this + vec;
1252 *
this = *
this - vec;
1277 ( 1.0f / pnt.
getX() ),
1278 ( 1.0f / pnt.
getY() ),
1279 ( 1.0f / pnt.
getZ() )
1286 sqrtf( pnt.
getX() ),
1287 sqrtf( pnt.
getY() ),
1295 ( 1.0f / sqrtf( pnt.
getX() ) ),
1296 ( 1.0f / sqrtf( pnt.
getY() ) ),
1297 ( 1.0f / sqrtf( pnt.
getZ() ) )
1304 fabsf( pnt.
getX() ),
1305 fabsf( pnt.
getY() ),
1313 ( pnt1.
getX() < 0.0f )? -fabsf( pnt0.
getX() ) : fabsf( pnt0.
getX() ),
1314 ( pnt1.
getY() < 0.0f )? -fabsf( pnt0.
getY() ) : fabsf( pnt0.
getY() ),
1315 ( pnt1.
getZ() < 0.0f )? -fabsf( pnt0.
getZ() ) : fabsf( pnt0.
getZ() )
1332 result = (pnt.
getZ() > result)? pnt.
getZ() : result;
1349 result = (pnt.
getZ() < result)? pnt.
getZ() : result;
1356 result = ( pnt.
getX() + pnt.
getY() );
1357 result = ( result + pnt.
getZ() );
1374 result = ( pnt.
getX() * unitVec.
getX() );
1375 result = ( result + ( pnt.
getY() * unitVec.
getY() ) );
1376 result = ( result + ( pnt.
getZ() * unitVec.
getZ() ) );
1397 return length( ( pnt1 - pnt0 ) );
1403 ( select1 )? pnt1.
getX() : pnt0.
getX(),
1404 ( select1 )? pnt1.
getY() : pnt0.
getY(),
1405 ( select1 )? pnt1.
getZ() : pnt0.
getZ()
1409 #ifdef _VECTORMATH_DEBUG
1411 inline void print(
const Point3 & pnt )
1413 printf(
"( %f %f %f )\n", pnt.getX(), pnt.getY(), pnt.getZ() );
1416 inline void print(
const Point3 & pnt,
const char * name )
1418 printf(
"%s: ( %f %f %f )\n", name, pnt.getX(), pnt.getY(), pnt.getZ() );
const Vector4 operator*(float scalar) const
const Quat normalize(const Quat &quat)
float & operator[](int idx)
const Vector3 getXYZ() const
const Vector3 recipPerElem(const Vector3 &vec)
static const Vector4 yAxis()
float minElem(const Vector3 &vec)
void loadHalfFloats(Vector3 &vec, const unsigned short *hfptr)
Vector4 & setXYZ(const Vector3 &vec)
Vector4 & operator+=(const Vector4 &vec)
static const Vector3 xAxis()
static const Vector3 zAxis()
void storeHalfFloats(const Vector3 &vec, unsigned short *hfptr)
float dist(const Point3 &pnt0, const Point3 &pnt1)
const Vector3 minPerElem(const Vector3 &vec0, const Vector3 &vec1)
Point3 & operator=(const Point3 &pnt)
Point3 & setElem(int idx, float value)
static const Vector4 xAxis()
Vector3 & operator=(const Vector3 &vec)
Point3 & operator-=(const Vector3 &vec)
float distFromOrigin(const Point3 &pnt)
#define _VECTORMATH_SLERP_TOL
const Vector4 operator-() const
Vector3 & operator/=(float scalar)
const Vector3 sqrtPerElem(const Vector3 &vec)
static const Vector3 yAxis()
Vector3 & operator-=(const Vector3 &vec)
static const Vector4 wAxis()
const Vector3 maxPerElem(const Vector3 &vec0, const Vector3 &vec1)
float lengthSqr(const Vector3 &vec)
float distSqrFromOrigin(const Point3 &pnt)
const Point3 operator+(const Vector3 &vec) const
float & operator[](int idx)
const Point3 scale(const Point3 &pnt, float scaleVal)
const Vector3 operator-(const Point3 &pnt) const
float projection(const Point3 &pnt, const Vector3 &unitVec)
const Vector3 copySignPerElem(const Vector3 &vec0, const Vector3 &vec1)
const Vector3 operator-() const
const Vector3 operator+(const Vector3 &vec) const
Vector3 & setElem(int idx, float value)
const Vector3 operator/(float scalar) const
Point3 & operator+=(const Vector3 &vec)
void loadXYZW(Quat &quat, const float *fptr)
const Vector4 operator+(const Vector4 &vec) const
const Vector3 rsqrtPerElem(const Vector3 &vec)
float distSqr(const Point3 &pnt0, const Point3 &pnt1)
float getElem(int idx) const
void storeXYZW(const Quat &quat, float *fptr)
const Quat lerp(float t, const Quat &quat0, const Quat &quat1)
const Matrix3 select(const Matrix3 &mat0, const Matrix3 &mat1, bool select1)
const Vector4 operator/(float scalar) const
Vector3 & operator*=(float scalar)
float getElem(int idx) const
Vector3 & operator+=(const Vector3 &vec)
float getElem(int idx) const
float maxElem(const Vector3 &vec)
void storeXYZ(const Vector3 &vec, float *fptr)
float & operator[](int idx)
float sum(const Vector3 &vec)
static const Vector4 zAxis()
float dot(const Quat &quat0, const Quat &quat1)
const Vector3 cross(const Vector3 &vec0, const Vector3 &vec1)
const Matrix3 mulPerElem(const Matrix3 &mat0, const Matrix3 &mat1)
Vector4 & operator/=(float scalar)
const Vector3 operator*(float scalar) const
float length(const Quat &quat)
Vector4 & operator*=(float scalar)
const Vector3 divPerElem(const Vector3 &vec0, const Vector3 &vec1)
const Matrix3 absPerElem(const Matrix3 &mat)
Vector4 & setElem(int idx, float value)
const Matrix3 operator*(float scalar, const Matrix3 &mat)
Vector4 & operator=(const Vector4 &vec)
Vector4 & operator-=(const Vector4 &vec)
void loadXYZ(Vector3 &vec, const float *fptr)
const Quat slerp(float t, const Quat &unitQuat0, const Quat &unitQuat1)