11 const float LP_PI = 3.14159265f;
12 const float kEpsilon = 0.00001f;
33 inline Vector3(
float _x,
float _y,
float _z)
40 inline Vector3(
float _x,
float _y)
123 operator/(
const float &d)
132 inline Vector3 operator*(
const float &d)
154 (lhs.x * rhs.x) + (lhs.y * rhs.y) + (lhs.z * rhs.z);
161 float result = (x * rhs.x) + (y * rhs.y) + (z * rhs.z);
168 Vector3 dir0 = v0.Normalized();
169 Vector3 dir1 = v1.Normalized();
171 float dot = dir0.Dot(dir1);
172 dot = (dot < -1.f ? -1.f : (dot > 1.f ? 1.f : dot));
174 float angle = acos(dot);
182 return x * x + y * y + z * z;
188 return sqrt(x * x + y * y + z * z);
214 return inNormal * -2.F * Dot(inNormal, inDirection) +
221 float mag = Magnitude();
222 if (mag > kEpsilon) {
237 float mag = Magnitude();
238 if (mag > kEpsilon) {
252 rotate(
Vector3 axis,
float radians)
254 float cos_theta = cosf(radians);
255 float sin_theta = sinf(radians);
257 x = (x * cos_theta) + (Vector3::Cross(axis) * sin_theta).x +
258 (axis.x * Vector3::Dot(axis, *
this)) * (1 - cos_theta);
260 y = (y * cos_theta) + (Vector3::Cross(axis) * sin_theta).y +
261 (axis.y * Vector3::Dot(axis, *
this)) * (1 - cos_theta);
263 z = (z * cos_theta) + (Vector3::Cross(axis) * sin_theta).z +
264 (axis.z * Vector3::Dot(axis, *
this)) * (1 - cos_theta);
271 ret.x = y * in.z - z * in.y;
272 ret.y = z * in.x - x * in.z;
273 ret.z = x * in.y - y * in.x;
298 inline Vector2(
float _x,
float _y)
331 operator/(
const float &d)
339 inline Vector2 operator*(
const float &d)
495 const Vector3 v = fromDir.Cross(toDir);
496 const float c = fromDir.Dot(toDir);
497 const float k = 1.0f / (1.0f + c);
499 return Matrix4x4(v.x * v.x * k + c, v.y * v.x * k - v.z,
500 v.z * v.x * k + v.y, 0.f, v.x * v.y * k + v.z,
501 v.y * v.y * k + c, v.z * v.y * k - v.x, 0.f,
502 v.x * v.z * k - v.y, v.y * v.z * k + v.x,
503 v.z * v.z * k + c, 0.f, 0.f, 0.f, 0.f, 1.f);
509 ret.m00 = (m00 * _in.m00) + (m01 * _in.m10) + (m02 * _in.m20) +
511 ret.m01 = (m00 * _in.m01) + (m01 * _in.m11) + (m02 * _in.m21) +
513 ret.m02 = (m00 * _in.m02) + (m01 * _in.m12) + (m02 * _in.m22) +
515 ret.m03 = (m00 * _in.m03) + (m01 * _in.m13) + (m02 * _in.m23) +
519 ret.m10 = (m10 * _in.m00) + (m11 * _in.m10) + (m12 * _in.m20) +
521 ret.m11 = (m10 * _in.m01) + (m11 * _in.m11) + (m12 * _in.m21) +
523 ret.m12 = (m10 * _in.m02) + (m11 * _in.m12) + (m12 * _in.m22) +
525 ret.m13 = (m10 * _in.m03) + (m11 * _in.m13) + (m12 * _in.m23) +
529 ret.m20 = (m20 * _in.m00) + (m21 * _in.m10) + (m22 * _in.m20) +
531 ret.m21 = (m20 * _in.m01) + (m21 * _in.m11) + (m22 * _in.m21) +
533 ret.m22 = (m20 * _in.m02) + (m21 * _in.m12) + (m22 * _in.m22) +
535 ret.m23 = (m20 * _in.m03) + (m21 * _in.m13) + (m22 * _in.m23) +
538 ret.m30 = (m30 * _in.m00) + (m31 * _in.m10) + (m32 * _in.m20) +
540 ret.m31 = (m30 * _in.m01) + (m31 * _in.m11) + (m32 * _in.m21) +
542 ret.m32 = (m30 * _in.m02) + (m31 * _in.m12) + (m32 * _in.m22) +
544 ret.m33 = (m30 * _in.m03) + (m31 * _in.m13) + (m32 * _in.m23) +
555 res.x = m00 * point.x + m01 * point.y + m02 * point.z + m03;
556 res.y = m10 * point.x + m11 * point.y + m12 * point.z + m13;
557 res.z = m20 * point.x + m21 * point.y + m22 * point.z + m23;
559 w = m30 * point.x + m31 * point.y + m32 * point.z + m33;
595 res.x = m00 * vector.x + m01 * vector.y + m02 * vector.z;
596 res.y = m10 * vector.x + m11 * vector.y + m12 * vector.z;
597 res.z = m20 * vector.x + m21 * vector.y + m22 * vector.z;
602 MultiplyPoint3x4(
Vector3 point)
605 res.x = m00 * point.x + m01 * point.y + m02 * point.z + m03;
606 res.y = m10 * point.x + m11 * point.y + m12 * point.z + m13;
607 res.z = m20 * point.x + m21 * point.y + m22 * point.z + m23;
637 float A2323 = m22 * m33 - m23 * m32;
638 float A1323 = m21 * m33 - m23 * m31;
639 float A1223 = m21 * m32 - m22 * m31;
640 float A0323 = m20 * m33 - m23 * m30;
641 float A0223 = m20 * m32 - m22 * m30;
642 float A0123 = m20 * m31 - m21 * m30;
643 float A2313 = m12 * m33 - m13 * m32;
644 float A1313 = m11 * m33 - m13 * m31;
645 float A1213 = m11 * m32 - m12 * m31;
646 float A2312 = m12 * m23 - m13 * m22;
647 float A1312 = m11 * m23 - m13 * m21;
648 float A1212 = m11 * m22 - m12 * m21;
649 float A0313 = m10 * m33 - m13 * m30;
650 float A0213 = m10 * m32 - m12 * m30;
651 float A0312 = m10 * m23 - m13 * m20;
652 float A0212 = m10 * m22 - m12 * m20;
653 float A0113 = m10 * m31 - m11 * m30;
654 float A0112 = m10 * m21 - m11 * m20;
656 float det = m00 * (m11 * A2323 - m12 * A1323 + m13 * A1223) -
657 m01 * (m10 * A2323 - m12 * A0323 + m13 * A0223) +
658 m02 * (m10 * A1323 - m11 * A0323 + m13 * A0123) -
659 m03 * (m10 * A1223 - m11 * A0223 + m12 * A0123);
663 det * (m11 * A2323 - m12 * A1323 + m13 * A1223),
664 det * -(m01 * A2323 - m02 * A1323 + m03 * A1223),
665 det * (m01 * A2313 - m02 * A1313 + m03 * A1213),
666 det * -(m01 * A2312 - m02 * A1312 + m03 * A1212),
667 det * -(m10 * A2323 - m12 * A0323 + m13 * A0223),
668 det * (m00 * A2323 - m02 * A0323 + m03 * A0223),
669 det * -(m00 * A2313 - m02 * A0313 + m03 * A0213),
670 det * (m00 * A2312 - m02 * A0312 + m03 * A0212),
671 det * (m10 * A1323 - m11 * A0323 + m13 * A0123),
672 det * -(m00 * A1323 - m01 * A0323 + m03 * A0123),
673 det * (m00 * A1313 - m01 * A0313 + m03 * A0113),
674 det * -(m00 * A1312 - m01 * A0312 + m03 * A0112),
675 det * -(m10 * A1223 - m11 * A0223 + m12 * A0123),
676 det * (m00 * A1223 - m01 * A0223 + m02 * A0123),
677 det * -(m00 * A1213 - m01 * A0213 + m02 * A0113),
678 det * (m00 * A1212 - m01 * A0212 + m02 * A0112));
735 const float zNear = 0.07f;
736 const float zFar = 1000.f;
743 float idx = 1.0f / (fRight - fLeft);
744 float idy = 1.0f / (fBottom - fTop);
746 float sx = fRight + fLeft;
747 float sy = fBottom + fTop;
749 float c = -(zFar + zNear) / (zFar - zNear);
750 float d = -(2.0F * zFar * zNear) / (zFar - zNear);
785 direction.Normalize();
786 m_Direction.x = direction.x;
787 m_Direction.y = direction.y;
788 m_Direction.z = direction.z;
792 GetPoint(
float distance)
794 return m_Origin + m_Direction * distance;
812 inline Quaternion(
float _x,
float _y,
float _z,
float _w)
853 return sqrt((x * x) + (y * y) + (z * z) + (w * w));
859 return Quaternion(w * s, x * s, y * s, z * s);
865 return conjugate() / norm();
871 return *
this * Vector3::Right();
877 return *
this * Vector3::Up();
883 return *
this * Vector3::Forward();
887 ToEulerAngle(
const Quaternion &q,
float &roll,
float &pitch,
float &yaw)
889 float sinr = +2.0f * (q.w * q.x + q.y * q.z);
890 float cosr = +1.0f - 2.0f * (q.x * q.x + q.y * q.y);
891 roll = atan2f(sinr, cosr);
893 float sinp = +2.0f * (q.w * q.y - q.z * q.x);
894 if (fabs(sinp) >= 1.f) {
895 pitch = copysignf(LP_PI / 2.f, sinp);
900 float siny = +2.0f * (q.w * q.z + q.x * q.y);
901 float cosy = +1.0f - 2.0f * (q.y * q.y + q.z * q.z);
902 yaw = atan2f(siny, cosy);
909 const static float PI_OVER_2 = LP_PI * 0.5f;
910 const static float EPSILON = 1e-10f;
911 float sqw, sqx, sqy, sqz;
919 euler.y = asinf(2.0f * (in.w * in.y - in.x * in.z));
920 if (PI_OVER_2 - fabs(euler.y) > EPSILON) {
921 euler.z = atan2f(2.0f * (in.x * in.y + in.w * in.z),
922 sqx - sqy - sqz + sqw);
923 euler.x = atan2f(2.0f * (in.w * in.x + in.y * in.z),
924 sqw - sqx - sqy + sqz);
927 euler.z = atan2f(2.f * in.y * in.z - 2.f * in.x * in.w,
928 2.f * in.x * in.z + 2.f * in.y * in.w);
933 euler.z = LP_PI - euler.z;
942 float num2 = y * 2.f;
943 float num3 = z * 2.f;
944 float num4 = x * num;
945 float num5 = y * num2;
946 float num6 = z * num3;
947 float num7 = x * num2;
948 float num8 = x * num3;
949 float num9 = y * num3;
950 float num10 = w * num;
951 float num11 = w * num2;
952 float num12 = w * num3;
954 result.x = (1.f - (num5 + num6)) * vec.x +
955 (num7 - num12) * vec.y + (num8 + num11) * vec.z;
956 result.y = (num7 + num12) * vec.x +
957 (1.f - (num4 + num6)) * vec.y +
958 (num9 - num10) * vec.z;
959 result.z = (num8 - num11) * vec.x + (num9 + num10) * vec.y +
960 (1.f - (num4 + num5)) * vec.z;
967 float c1 = cos(euler.z * 0.5f);
968 float c2 = cos(euler.y * 0.5f);
969 float c3 = cos(euler.x * 0.5f);
970 float s1 = sin(euler.z * 0.5f);
971 float s2 = sin(euler.y * 0.5f);
972 float s3 = sin(euler.x * 0.5f);
975 ret.x = c1 * c2 * s3 - s1 * s2 * c3;
976 ret.y = c1 * s2 * c3 + s1 * c2 * s3;
977 ret.z = s1 * c2 * c3 - c1 * s2 * s3;
978 ret.w = c1 * c2 * c3 + s1 * s2 * s3;
993 1.0f / sqrt(qx * qx + qy * qy + qz * qz + qw * qw);
999 return Matrix4x4(1.0f - 2.0f * qy * qy - 2.0f * qz * qz,
1000 2.0f * qx * qy - 2.0f * qz * qw,
1001 2.0f * qx * qz + 2.0f * qy * qw, 0.0f,
1002 2.0f * qx * qy + 2.0f * qz * qw,
1003 1.0f - 2.0f * qx * qx - 2.0f * qz * qz,
1004 2.0f * qy * qz - 2.0f * qx * qw, 0.0f,
1005 2.0f * qx * qz - 2.0f * qy * qw,
1006 2.0f * qy * qz + 2.0f * qx * qw,
1007 1.0f - 2.0f * qx * qx - 2.0f * qy * qy, 0.0f,
1008 0.0f, 0.0f, 0.0f, 1.0f);
1013 return Quaternion(y * q.z - z * q.y + x * q.w + w * q.x,
1014 z * q.x - x * q.z + y * q.w + w * q.y,
1015 x * q.y - y * q.x + z * q.w + w * q.z,
1016 w * q.w - x * q.x - y * q.y - z * q.z);
1020 operator/(
const float div)
1022 return Quaternion(x / div, y / div, z / div, w / div);
1026 AxisAngle(
Vector3 axis,
float angle)
1028 float halfAngle = angle * .5f;
1029 float s = (float)sin(halfAngle);
1034 q.w = (float)cos(halfAngle);
1041 Vector3 forwardVector = (destPoint - sourcePoint).Normalized();
1043 float dot = Vector3::Dot(Vector3::Forward(), forwardVector);
1045 if (fabs(dot - (-1.0f)) < 0.000001f) {
1046 return Quaternion(Vector3::Up().x, Vector3::Up().y,
1047 Vector3::Up().z, 3.1415926535897932f);
1049 if (fabs(dot - (1.0f)) < 0.000001f) {
1053 float rotAngle = cos(dot);
1054 Vector3 rotAxis = Vector3::Forward().Cross(forwardVector);
1055 rotAxis = rotAxis.Normalized();
1056 return AxisAngle(rotAxis, rotAngle);
1063 Vector3 vector1 = forward.Normalized();
1064 Vector3 vector2 = (Up.Cross(vector1)).Normalized();
1065 Vector3 vector3 = vector1.Cross(vector2);
1067 float m00 = vector2.x;
1068 float m01 = vector2.y;
1069 float m02 = vector2.z;
1070 float m10 = vector3.x;
1071 float m11 = vector3.y;
1072 float m12 = vector3.z;
1073 float m20 = vector1.x;
1074 float m21 = vector1.y;
1075 float m22 = vector1.z;
1077 float num8 = (m00 + m11) + m22;
1080 float num = (float)sqrtf(num8 + 1.f);
1081 quaternion.w = num * 0.5f;
1083 quaternion.x = (m12 - m21) * num;
1084 quaternion.y = (m20 - m02) * num;
1085 quaternion.z = (m01 - m10) * num;
1089 if ((m00 >= m11) && (m00 >= m22)) {
1090 float num7 = (float)sqrtf(((1.f + m00) - m11) - m22);
1091 float num4 = 0.5f / num7;
1092 quaternion.x = 0.5f * num7;
1093 quaternion.y = (m01 + m10) * num4;
1094 quaternion.z = (m02 + m20) * num4;
1095 quaternion.w = (m12 - m21) * num4;
1100 float num6 = (float)sqrtf(((1.f + m11) - m00) - m22);
1101 float num3 = 0.5f / num6;
1102 quaternion.x = (m10 + m01) * num3;
1103 quaternion.y = 0.5f * num6;
1104 quaternion.z = (m21 + m12) * num3;
1105 quaternion.w = (m20 - m02) * num3;
1109 float num5 = (float)sqrtf(((1.f + m22) - m00) - m11);
1110 float num2 = 0.5f / num5;
1111 quaternion.x = (m20 + m02) * num2;
1112 quaternion.y = (m21 + m12) * num2;
1113 quaternion.z = 0.5f * num5;
1114 quaternion.w = (m01 - m10) * num2;
1122 return (x >= 0.0f) ? +1.0f : -1.0f;
1126 NORM(
float a,
float b,
float c,
float d)
1128 return sqrt(a * a + b * b + c * c + d * d);
1134 float tr = m.m00 + m.m11 + m.m22;
1135 float qx, qy, qz, qw;
1138 float S = sqrtf(tr + 1.f) * 2.f;
1140 qx = (m.m21 - m.m12) / S;
1141 qy = (m.m02 - m.m20) / S;
1142 qz = (m.m10 - m.m01) / S;
1143 }
else if ((m.m00 > m.m11) && (m.m00 > m.m22)) {
1144 float S = sqrtf(1.f + m.m00 - m.m11 - m.m22) * 2.f;
1145 qw = (m.m21 - m.m12) / S;
1147 qy = (m.m01 + m.m10) / S;
1148 qz = (m.m02 + m.m20) / S;
1149 }
else if (m.m11 > m.m22) {
1150 float S = sqrtf(1.f + m.m11 - m.m00 - m.m22) * 2.f;
1151 qw = (m.m02 - m.m20) / S;
1152 qx = (m.m01 + m.m10) / S;
1154 qz = (m.m12 + m.m21) / S;
1156 float S = sqrtf(1.f + m.m22 - m.m00 - m.m11) * 2.f;
1157 qw = (m.m10 - m.m01) / S;
1158 qx = (m.m02 + m.m20) / S;
1159 qy = (m.m12 + m.m21) / S;
1234 Vector3 axis = dir0.Cross(dir1).Normalized();
1235 float angle = Vector3::Angle(dir0, dir1);
1236 return Quaternion::AxisAngle(axis, angle);
1256 rotation = Quaternion::Identity();
1260 position = Vector3::Zero();
1272 return Pose(Vector3::Zero(), Quaternion::Identity());
1279 return Pose(invQ * -position, invQ);
1285 return Matrix4x4::Translate(position) * rotation.ToMatrix4x4();
1290 return Pose(position + (rotation * rhs.position),
1291 rotation * rhs.rotation);
1296 return Pose(position + rotation * rhs, rotation);
1303 Quaternion::FromMatrix(m));
Definition: utility_northstar.h:801
Definition: utility_northstar.h:368
Definition: utility_northstar.h:283
Definition: utility_northstar.h:779
Definition: utility_northstar.h:703
Definition: utility_northstar.h:1247
Definition: utility_northstar.h:16