MCPcopy Create free account
hub / github.com/TombEngine/TombEngine / ConvertDirectionToQuat

Function ConvertDirectionToQuat

TombEngine/Math/Geometry.cpp:295–321  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

293 }
294
295 Quaternion ConvertDirectionToQuat(const Vector3& dir)
296 {
297 constexpr auto SINGULARITY_THRESHOLD = 1.0f - EPSILON;
298
299 static const auto REF_DIR = Vector3::UnitZ;
300
301 // Vectors are nearly opposite; return orientation 180 degrees around arbitrary axis.
302 float dot = REF_DIR.Dot(dir);
303 if (dot < -SINGULARITY_THRESHOLD)
304 {
305 auto axis = Vector3::UnitX.Cross(REF_DIR);
306 if (axis.LengthSquared() < EPSILON)
307 axis = Vector3::UnitY.Cross(REF_DIR);
308 axis.Normalize();
309
310 auto axisAngle = AxisAngle(axis, FROM_RAD(PI));
311 return axisAngle.ToQuaternion();
312 }
313
314 // Vectors are nearly identical; return identity quaternion.
315 if (dot > SINGULARITY_THRESHOLD)
316 return Quaternion::Identity;
317
318 // Calculate axis-angle and return converted quaternion.
319 auto axisAngle = AxisAngle(REF_DIR.Cross(dir), FROM_RAD(acos(dot)));
320 return axisAngle.ToQuaternion();
321 }
322
323 Vector3 ConvertQuatToDirection(const Quaternion& quat)
324 {

Callers 2

UpdateMethod · 0.85
GetSegmentOrientationMethod · 0.85

Calls 6

FROM_RADFunction · 0.85
AxisAngleClass · 0.70
DotMethod · 0.45
CrossMethod · 0.45
NormalizeMethod · 0.45
ToQuaternionMethod · 0.45

Tested by

no test coverage detected