载入中...
搜索中...
未找到
BakeRigidTransform.cpp
浏览该文件的文档.
1// eve-hot-path
3#include <cmath>
4#include <limits>
6namespace eve::physics {
7namespace {
8Diagnostic transformError(const char* message) {
9 return Diagnostic::error(DiagnosticCode::InvalidArgument, message, "bakeRigidTransform");
10}
11} // namespace
15 if (!std::isfinite(value)) return Result<void>::failure(transformError("nonfinite playback placement"));
17 const double norm = std::sqrt(x * x + y * y + z * z + w * w);
18 if (norm == 0) return Result<void>::failure(transformError("zero placement quaternion"));
19 placement.rotationX = static_cast<float>(x / norm);
20 placement.rotationY = static_cast<float>(y / norm);
21 placement.rotationZ = static_cast<float>(z / norm);
22 placement.rotationW = static_cast<float>(w / norm);
23 placement_ = placement;
24 return Result<void>::success();
25}
26BakeRigidPlacement BakeRigidTransform::placement() const noexcept { return placement_; }
28 for (float value : {pose.x, pose.y, pose.z, pose.qx, pose.qy, pose.qz, pose.qw})
29 if (!std::isfinite(value)) return Result<RigidTrackPose>::failure(transformError("nonfinite pose"));
30 // Double intermediates avoid overflow before the final representability check.
31 const double qx = placement_.rotationX, qy = placement_.rotationY, qz = placement_.rotationZ,
32 qw = placement_.rotationW;
33 const double norm = qx * qx + qy * qy + qz * qz + qw * qw;
34 const double tx = 2 * (qy * pose.z - qz * pose.y) / norm;
35 const double ty = 2 * (qz * pose.x - qx * pose.z) / norm;
36 const double tz = 2 * (qx * pose.y - qy * pose.x) / norm;
37 const double px = pose.x + qw * tx + qy * tz - qz * ty + placement_.x;
38 const double py = pose.y + qw * ty + qz * tx - qx * tz + placement_.y;
39 const double pz = pose.z + qw * tz + qx * ty - qy * tx + placement_.z;
40 for (double value : {px, py, pz})
41 if (!std::isfinite(value) || std::abs(value) > std::numeric_limits<float>::max())
42 return Result<RigidTrackPose>::failure(transformError("placed sample position overflow"));
43 const double bx = pose.qx, by = pose.qy, bz = pose.qz, bw = pose.qw;
44 const double rx = qw * bx + qx * bw + qy * bz - qz * by;
45 const double ry = qw * by - qx * bz + qy * bw + qz * bx;
46 const double rz = qw * bz + qx * by - qy * bx + qz * bw;
47 const double rw = qw * bw - qx * bx - qy * by - qz * bz;
48 const double length = std::sqrt(rx * rx + ry * ry + rz * rz + rw * rw);
49 if (length == 0) return Result<RigidTrackPose>::failure(transformError("zero pose quaternion"));
50 pose.x = static_cast<float>(px);
51 pose.y = static_cast<float>(py);
52 pose.z = static_cast<float>(pz);
53 pose.qx = static_cast<float>(rx / length);
54 pose.qy = static_cast<float>(ry / length);
55 pose.qz = static_cast<float>(rz / length);
56 pose.qw = static_cast<float>(rw / length);
58}
59} // namespace eve::physics
double z
double y
double x
float w
Definition AnimClip.cpp:738
eve::EntitySpatialPose pose
std::string message
std::vector< Colorf > px
float length
Placement placement
Object::Ptr value
static Diagnostic error(DiagnosticCode code, std::string message, std::string path={})
Construct an error diagnostic with the standard error severity.
Move-only operation result carrying either a value or Status.
Definition Result.h:176
static Result success(T value)
Construct a successful result owning value.
Definition Result.h:185
static Result failure(Status status)
Construct a failed result from a structured status.
Definition Result.h:196
Result< void > set(BakeRigidPlacement placement)
Validate finite translation and a nonzero XYZW quaternion; failures preserve prior placement.
BakeRigidPlacement placement() const noexcept
Return the normalized placement by value.
Result< RigidTrackPose > apply(RigidTrackPose pose) const
Rotate and translate a finite pose, preserving inputs on failure.
eve::Diagnostic Diagnostic
Optional physics backend for vehicle mobility and body attach.
Definition Climbing.h:36
Rotation about the baked origin followed by translation, in the track's explicit units; no scaling.
Owning pose; XYZW quaternion, pixels or metres selected by the track. Planar poses require z=qx=qy=0.