11const float kEpsilon = 0.00001f;
32 inline Vector3(
float _x,
float _y,
float _z)
39 inline Vector3(
float _x,
float _y)
87 operator-(
const Vector3 &rhs)
const
103 operator+(
const Vector3 &rhs)
const
122 operator/(
const float &d)
const
132 operator*(
const float &d)
const
149 float result = (lhs.x * rhs.x) + (lhs.y * rhs.y) + (lhs.z * rhs.z);
156 float result = (x * rhs.x) + (y * rhs.y) + (z * rhs.z);
163 Vector3 dir0 = v0.Normalized();
164 Vector3 dir1 = v1.Normalized();
166 float dot = dir0.Dot(dir1);
167 dot = (dot < -1.f ? -1.f : (dot > 1.f ? 1.f : dot));
169 float angle = acos(dot);
177 return x * x + y * y + z * z;
183 return sqrt(x * x + y * y + z * z);
189 return {0.f, 0.f, 0.f};
195 return {1.f, 1.f, 1.f};
201 return inNormal * -2.F * Dot(inNormal, inDirection) + inDirection;
207 float mag = Magnitude();
208 if (mag > kEpsilon) {
223 float mag = Magnitude();
224 if (mag > kEpsilon) {
238 rotate(
const Vector3 &axis,
float radians)
240 float cos_theta = cosf(radians);
241 float sin_theta = sinf(radians);
243 x = (x * cos_theta) + (Vector3::Cross(axis) * sin_theta).x +
244 (axis.x * Vector3::Dot(axis, *
this)) * (1 - cos_theta);
246 y = (y * cos_theta) + (Vector3::Cross(axis) * sin_theta).y +
247 (axis.y * Vector3::Dot(axis, *
this)) * (1 - cos_theta);
249 z = (z * cos_theta) + (Vector3::Cross(axis) * sin_theta).z +
250 (axis.z * Vector3::Dot(axis, *
this)) * (1 - cos_theta);
257 ret.x = y * in.z - z * in.y;
258 ret.y = z * in.x - x * in.z;
259 ret.z = x * in.y - y * in.x;
284 inline Vector2(
float _x,
float _y)
317 operator/(
float d)
const
326 operator*(
float d)
const
334 operator-(
const Vector2 &rhs)
const
343 operator+(
const Vector2 &rhs)
const
482 const Vector3 v = fromDir.Cross(toDir);
483 const float c = fromDir.Dot(toDir);
484 const float k = 1.0f / (1.0f + c);
486 return Matrix4x4(v.x * v.x * k + c, v.y * v.x * k - v.z, v.z * v.x * k + v.y, 0.f, v.x * v.y * k + v.z,
487 v.y * v.y * k + c, v.z * v.y * k - v.x, 0.f, v.x * v.z * k - v.y, v.y * v.z * k + v.x,
488 v.z * v.z * k + c, 0.f, 0.f, 0.f, 0.f, 1.f);
495 ret.m00 = (m00 * _in.m00) + (m01 * _in.m10) + (m02 * _in.m20) + (m03 * _in.m30);
496 ret.m01 = (m00 * _in.m01) + (m01 * _in.m11) + (m02 * _in.m21) + (m03 * _in.m31);
497 ret.m02 = (m00 * _in.m02) + (m01 * _in.m12) + (m02 * _in.m22) + (m03 * _in.m32);
498 ret.m03 = (m00 * _in.m03) + (m01 * _in.m13) + (m02 * _in.m23) + (m03 * _in.m33);
501 ret.m10 = (m10 * _in.m00) + (m11 * _in.m10) + (m12 * _in.m20) + (m13 * _in.m30);
502 ret.m11 = (m10 * _in.m01) + (m11 * _in.m11) + (m12 * _in.m21) + (m13 * _in.m31);
503 ret.m12 = (m10 * _in.m02) + (m11 * _in.m12) + (m12 * _in.m22) + (m13 * _in.m32);
504 ret.m13 = (m10 * _in.m03) + (m11 * _in.m13) + (m12 * _in.m23) + (m13 * _in.m33);
507 ret.m20 = (m20 * _in.m00) + (m21 * _in.m10) + (m22 * _in.m20) + (m23 * _in.m30);
508 ret.m21 = (m20 * _in.m01) + (m21 * _in.m11) + (m22 * _in.m21) + (m23 * _in.m31);
509 ret.m22 = (m20 * _in.m02) + (m21 * _in.m12) + (m22 * _in.m22) + (m23 * _in.m32);
510 ret.m23 = (m20 * _in.m03) + (m21 * _in.m13) + (m22 * _in.m23) + (m23 * _in.m33);
512 ret.m30 = (m30 * _in.m00) + (m31 * _in.m10) + (m32 * _in.m20) + (m33 * _in.m30);
513 ret.m31 = (m30 * _in.m01) + (m31 * _in.m11) + (m32 * _in.m21) + (m33 * _in.m31);
514 ret.m32 = (m30 * _in.m02) + (m31 * _in.m12) + (m32 * _in.m22) + (m33 * _in.m32);
515 ret.m33 = (m30 * _in.m03) + (m31 * _in.m13) + (m32 * _in.m23) + (m33 * _in.m33);
521 MultiplyPoint(
Vector3 const &point)
const
525 res.x = m00 * point.x + m01 * point.y + m02 * point.z + m03;
526 res.y = m10 * point.x + m11 * point.y + m12 * point.z + m13;
527 res.z = m20 * point.x + m21 * point.y + m22 * point.z + m23;
529 w = m30 * point.x + m31 * point.y + m32 * point.z + m33;
539 Translate(
const Vector3 &vector)
562 MultiplyVector(
const Vector3 &vector)
const
565 res.x = m00 * vector.x + m01 * vector.y + m02 * vector.z;
566 res.y = m10 * vector.x + m11 * vector.y + m12 * vector.z;
567 res.z = m20 * vector.x + m21 * vector.y + m22 * vector.z;
572 MultiplyPoint3x4(
const Vector3 &point)
const
575 res.x = m00 * point.x + m01 * point.y + m02 * point.z + m03;
576 res.y = m10 * point.x + m11 * point.y + m12 * point.z + m13;
577 res.z = m20 * point.x + m21 * point.y + m22 * point.z + m23;
607 float A2323 = m22 * m33 - m23 * m32;
608 float A1323 = m21 * m33 - m23 * m31;
609 float A1223 = m21 * m32 - m22 * m31;
610 float A0323 = m20 * m33 - m23 * m30;
611 float A0223 = m20 * m32 - m22 * m30;
612 float A0123 = m20 * m31 - m21 * m30;
613 float A2313 = m12 * m33 - m13 * m32;
614 float A1313 = m11 * m33 - m13 * m31;
615 float A1213 = m11 * m32 - m12 * m31;
616 float A2312 = m12 * m23 - m13 * m22;
617 float A1312 = m11 * m23 - m13 * m21;
618 float A1212 = m11 * m22 - m12 * m21;
619 float A0313 = m10 * m33 - m13 * m30;
620 float A0213 = m10 * m32 - m12 * m30;
621 float A0312 = m10 * m23 - m13 * m20;
622 float A0212 = m10 * m22 - m12 * m20;
623 float A0113 = m10 * m31 - m11 * m30;
624 float A0112 = m10 * m21 - m11 * m20;
627 m00 * (m11 * A2323 - m12 * A1323 + m13 * A1223) - m01 * (m10 * A2323 - m12 * A0323 + m13 * A0223) +
628 m02 * (m10 * A1323 - m11 * A0323 + m13 * A0123) - m03 * (m10 * A1223 - m11 * A0223 + m12 * A0123);
632 det * (m11 * A2323 - m12 * A1323 + m13 * A1223), det * -(m01 * A2323 - m02 * A1323 + m03 * A1223),
633 det * (m01 * A2313 - m02 * A1313 + m03 * A1213), det * -(m01 * A2312 - m02 * A1312 + m03 * A1212),
634 det * -(m10 * A2323 - m12 * A0323 + m13 * A0223), det * (m00 * A2323 - m02 * A0323 + m03 * A0223),
635 det * -(m00 * A2313 - m02 * A0313 + m03 * A0213), det * (m00 * A2312 - m02 * A0312 + m03 * A0212),
636 det * (m10 * A1323 - m11 * A0323 + m13 * A0123), det * -(m00 * A1323 - m01 * A0323 + m03 * A0123),
637 det * (m00 * A1313 - m01 * A0313 + m03 * A0113), det * -(m00 * A1312 - m01 * A0312 + m03 * A0112),
638 det * -(m10 * A1223 - m11 * A0223 + m12 * A0123), det * (m00 * A1223 - m01 * A0223 + m02 * A0123),
639 det * -(m00 * A1213 - m01 * A0213 + m02 * A0113), det * (m00 * A1212 - m01 * A0212 + m02 * A0112));
698 ComposeProjection()
const
700 const float zNear = 0.07f;
701 const float zFar = 1000.f;
708 float idx = 1.0f / (fRight - fLeft);
709 float idy = 1.0f / (fBottom - fTop);
711 float sx = fRight + fLeft;
712 float sy = fBottom + fTop;
714 float c = -(zFar + zNear) / (zFar - zNear);
715 float d = -(2.0F * zFar * zNear) / (zFar - zNear);
750 direction.Normalize();
751 m_Direction.x = direction.x;
752 m_Direction.y = direction.y;
753 m_Direction.z = direction.z;
757 GetPoint(
float distance)
const
759 return m_Origin + m_Direction * distance;
769 return (x >= 0.0f) ? +1.0f : -1.0f;
773NORM(
float a,
float b,
float c,
float d)
775 return sqrt(a * a + b * b + c * c + d * d);
789 inline Quaternion(
float _x,
float _y,
float _z,
float _w)
817 return sqrt((x * x) + (y * y) + (z * z) + (w * w));
823 return Quaternion(w * s, x * s, y * s, z * s);
829 return conjugate() / norm();
835 return *
this * Vector3::Right();
841 return *
this * Vector3::Up();
847 return *
this * Vector3::Forward();
851 ToEulerAngle(
const Quaternion &q,
float &roll,
float &pitch,
float &yaw)
853 float sinr = +2.0f * (q.w * q.x + q.y * q.z);
854 float cosr = +1.0f - 2.0f * (q.x * q.x + q.y * q.y);
855 roll = atan2f(sinr, cosr);
857 float sinp = +2.0f * (q.w * q.y - q.z * q.x);
858 if (fabs(sinp) >= 1.f) {
859 pitch = copysignf(M_PI / 2.f, sinp);
864 float siny = +2.0f * (q.w * q.z + q.x * q.y);
865 float cosy = +1.0f - 2.0f * (q.y * q.y + q.z * q.z);
866 yaw = atan2f(siny, cosy);
873 const static float PI_OVER_2 = M_PI * 0.5f;
874 const static float EPSILON = 1e-10f;
886 euler.y = asinf(2.0f * (in.w * in.y - in.x * in.z));
887 if (PI_OVER_2 - fabs(euler.y) > EPSILON) {
888 euler.z = atan2f(2.0f * (in.x * in.y + in.w * in.z), sqx - sqy - sqz + sqw);
889 euler.x = atan2f(2.0f * (in.w * in.x + in.y * in.z), sqw - sqx - sqy + sqz);
892 euler.z = atan2f(2.f * in.y * in.z - 2.f * in.x * in.w, 2.f * in.x * in.z + 2.f * in.y * in.w);
897 euler.z = M_PI - euler.z;
904 operator*(
Vector3 const &vec)
const
907 float num2 = y * 2.f;
908 float num3 = z * 2.f;
909 float num4 = x * num;
910 float num5 = y * num2;
911 float num6 = z * num3;
912 float num7 = x * num2;
913 float num8 = x * num3;
914 float num9 = y * num3;
915 float num10 = w * num;
916 float num11 = w * num2;
917 float num12 = w * num3;
919 result.x = (1.f - (num5 + num6)) * vec.x + (num7 - num12) * vec.y + (num8 + num11) * vec.z;
920 result.y = (num7 + num12) * vec.x + (1.f - (num4 + num6)) * vec.y + (num9 - num10) * vec.z;
921 result.z = (num8 - num11) * vec.x + (num9 + num10) * vec.y + (1.f - (num4 + num5)) * vec.z;
928 float c1 = cos(euler.z * 0.5f);
929 float c2 = cos(euler.y * 0.5f);
930 float c3 = cos(euler.x * 0.5f);
931 float s1 = sin(euler.z * 0.5f);
932 float s2 = sin(euler.y * 0.5f);
933 float s3 = sin(euler.x * 0.5f);
936 ret.x = c1 * c2 * s3 - s1 * s2 * c3;
937 ret.y = c1 * s2 * c3 + s1 * c2 * s3;
938 ret.z = s1 * c2 * c3 - c1 * s2 * s3;
939 ret.w = c1 * c2 * c3 + s1 * s2 * s3;
953 const float n = 1.0f / sqrt(qx * qx + qy * qy + qz * qz + qw * qw);
959 return Matrix4x4(1.0f - 2.0f * qy * qy - 2.0f * qz * qz, 2.0f * qx * qy - 2.0f * qz * qw,
960 2.0f * qx * qz + 2.0f * qy * qw, 0.0f, 2.0f * qx * qy + 2.0f * qz * qw,
961 1.0f - 2.0f * qx * qx - 2.0f * qz * qz, 2.0f * qy * qz - 2.0f * qx * qw, 0.0f,
962 2.0f * qx * qz - 2.0f * qy * qw, 2.0f * qy * qz + 2.0f * qx * qw,
963 1.0f - 2.0f * qx * qx - 2.0f * qy * qy, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f);
969 return Quaternion(y * q.z - z * q.y + x * q.w + w * q.x, z * q.x - x * q.z + y * q.w + w * q.y,
970 x * q.y - y * q.x + z * q.w + w * q.z, w * q.w - x * q.x - y * q.y - z * q.z);
974 operator/(
const float div)
const
976 return Quaternion(x / div, y / div, z / div, w / div);
980 AxisAngle(
const Vector3 &axis,
float angle)
982 float halfAngle = angle * .5f;
983 float s = (float)sin(halfAngle);
988 q.w = (float)cos(halfAngle);
995 Vector3 forwardVector = (destPoint - sourcePoint).Normalized();
997 float dot = Vector3::Dot(Vector3::Forward(), forwardVector);
999 if (fabs(dot - (-1.0f)) < 0.000001f) {
1000 return Quaternion(Vector3::Up().x, Vector3::Up().y, Vector3::Up().z, 3.1415926535897932f);
1002 if (fabs(dot - (1.0f)) < 0.000001f) {
1006 float rotAngle = cos(dot);
1007 Vector3 rotAxis = Vector3::Forward().Cross(forwardVector);
1008 rotAxis = rotAxis.Normalized();
1009 return AxisAngle(rotAxis, rotAngle);
1013 QuaternionLookRotation(
const Vector3 &forward,
const Vector3 &Up)
1016 Vector3 vector1 = forward.Normalized();
1017 Vector3 vector2 = (Up.Cross(vector1)).Normalized();
1018 Vector3 vector3 = vector1.Cross(vector2);
1020 float m00 = vector2.x;
1021 float m01 = vector2.y;
1022 float m02 = vector2.z;
1023 float m10 = vector3.x;
1024 float m11 = vector3.y;
1025 float m12 = vector3.z;
1026 float m20 = vector1.x;
1027 float m21 = vector1.y;
1028 float m22 = vector1.z;
1030 float num8 = (m00 + m11) + m22;
1033 float num = (float)sqrtf(num8 + 1.f);
1034 quaternion.w = num * 0.5f;
1036 quaternion.x = (m12 - m21) * num;
1037 quaternion.y = (m20 - m02) * num;
1038 quaternion.z = (m01 - m10) * num;
1042 if ((m00 >= m11) && (m00 >= m22)) {
1043 float num7 = (float)sqrtf(((1.f + m00) - m11) - m22);
1044 float num4 = 0.5f / num7;
1045 quaternion.x = 0.5f * num7;
1046 quaternion.y = (m01 + m10) * num4;
1047 quaternion.z = (m02 + m20) * num4;
1048 quaternion.w = (m12 - m21) * num4;
1053 float num6 = (float)sqrtf(((1.f + m11) - m00) - m22);
1054 float num3 = 0.5f / num6;
1055 quaternion.x = (m10 + m01) * num3;
1056 quaternion.y = 0.5f * num6;
1057 quaternion.z = (m21 + m12) * num3;
1058 quaternion.w = (m20 - m02) * num3;
1062 float num5 = (float)sqrtf(((1.f + m22) - m00) - m11);
1063 float num2 = 0.5f / num5;
1064 quaternion.x = (m20 + m02) * num2;
1065 quaternion.y = (m21 + m12) * num2;
1066 quaternion.z = 0.5f * num5;
1067 quaternion.w = (m01 - m10) * num2;
1074 float tr = m.m00 + m.m11 + m.m22;
1081 float S = sqrtf(tr + 1.f) * 2.f;
1083 qx = (m.m21 - m.m12) / S;
1084 qy = (m.m02 - m.m20) / S;
1085 qz = (m.m10 - m.m01) / S;
1086 }
else if ((m.m00 > m.m11) && (m.m00 > m.m22)) {
1087 float S = sqrtf(1.f + m.m00 - m.m11 - m.m22) * 2.f;
1088 qw = (m.m21 - m.m12) / S;
1090 qy = (m.m01 + m.m10) / S;
1091 qz = (m.m02 + m.m20) / S;
1092 }
else if (m.m11 > m.m22) {
1093 float S = sqrtf(1.f + m.m11 - m.m00 - m.m22) * 2.f;
1094 qw = (m.m02 - m.m20) / S;
1095 qx = (m.m01 + m.m10) / S;
1097 qz = (m.m12 + m.m21) / S;
1099 float S = sqrtf(1.f + m.m22 - m.m00 - m.m11) * 2.f;
1100 qw = (m.m10 - m.m01) / S;
1101 qx = (m.m02 + m.m20) / S;
1102 qy = (m.m12 + m.m21) / S;
1177 Vector3 axis = dir0.Cross(dir1).Normalized();
1178 float angle = Vector3::Angle(dir0, dir1);
1179 return Quaternion::AxisAngle(axis, angle);
Definition: utility_northstar.h:356
Definition: utility_northstar.h:779
Definition: utility_northstar.h:745
Definition: utility_northstar.h:270
Definition: utility_northstar.h:16
Definition: utility_northstar.h:665
Wrapper header for <math.h> to ensure pi-related math constants are defined.