Skip to main content

Shared types and results

ROCK/Abi.h holds the values every interface shares; each interface's own records and enums live in its header. ROCK/TransformMath.h holds optional transform helpers.

Common values​

  • Hand: None 0, Right 1, Left 2. Do not use these numbers as array indices.
  • Transform: rotate[9] (rows of the 3x3 rotation), translate[3] in game units, scale. A value-initialized rotation is all zeros, not an identity matrix. Game space unless a field name says otherwise.
  • Point3, Bounds3 (min, max, valid).
  • Sample: frameIndex, publicationSequence, worldGeneration, skeletonGeneration, providerGeneration, collisionGeneration. Every interface's getSample returns it.
  • ReferenceKey: referenceFormId, referenceNativeHandle and the three generations; identifies a live reference for requests that act on one.
  • PowerArmorPoint: None, LeftArmorHand, RightArmorHand.
  • OwnerToken: the 64-bit owner handle from Core registerConsumer. Pass it first to every call.
  • kMaxWeaponSlots (2): a weapon slot is the native equipment index, 0 the first equipped weapon and 1 the second. Which hand fires or supports a weapon is data in its slot's state, never implied by the slot. See the slot table.
  • InterfaceId: the ids in the API index. 10 (References) and 12 (Diagnostics) are retired and never reused.

Fields named ...Game use game units, ...Havok Havok units; Collision getEnvironment gives the scale. Body ids and source keys are opaque, never engine pointers.

Status values​

Handle the status a call actually returns; a declared status is not a promise that every function emits it.

ValueMeaning
Ok 0Done. Validity flags in the output still decide which fields are usable.
NotReady 1ROCK has nothing to report or act on yet (provider, skeleton or first publication missing); retry later.
InvalidArgument 2A bad argument: null pointer, out-of-range value, unknown flag.
InvalidSize 3, UnsupportedVersion 4A record's size or version is wrong.
CapacityFull 5A limit is reached; nothing was evicted.
OwnerNotRegistered 6The owner token is unknown.
OwnerConflict 7Another owner holds that resource.
PermissionDenied 8The interface is not bound with the needed permission.
WorldNotReady 9No live physics world.
TargetInvalid 10, TargetUnavailable 11The target does not match, or is not available now.
HandUnavailable 12, HandBusy 13The hand cannot take the request, or is doing something else.
ObjectAlreadyOwned 14Another owner or hand already holds the object.
RequestQueued 15Accepted for a later frame; poll for the result. Admission, not success.
RequestNotFound 16No such command or request.
WrongThread 17Called outside the thread or callback phase the function requires.
AlreadyCommitted 18Too late to cancel.
FeatureDisabled 19The player has turned off the ROCK feature the request needs. Nothing was queued; do it the game's own way.
UnknownInterface 100Discovery: no such interface, including the retired ids.
UnsupportedMajor 101Discovery: the interface exists at another major. Mods built for provider API 1 get this.
UnsupportedMinor 102Discovery: ROCK's minor is below the requested minimum.
TableTooSmall 103Discovery: ROCK's table is smaller than requested.
OwnerRevoked 104ROCK revoked the owner after one of its callbacks faulted. Everything it held is released; reads still work, writes and callbacks are refused.
InternalError 105ROCK failed internally; details are in ROCK.log.
Busy 106Try again later: another operation is in progress, or the call came from inside a callback that forbids it.
Ambiguous 107A selector matched more than one target.
GenerationMismatch 108The request's generations are stale.

Transform helpers​

rock::transform_math holds header-only templates over any matrix, vector and transform type with ROCK's field layout (CommonLib's NiMatrix3, NiPoint3 and NiTransform included): compose and invert transforms, convert points and vectors between local and world space, orthonormalize rotations, Euler angles, and the conversions between Ni rows, Havok columns and Havok quaternions. They follow FO4VR's native compose convention (child-local vectors applied through the stored basis as Transpose() * v) and compute in double precision.

Declarations​

Complete public declarations: ROCK/Abi.h
ROCK/Abi.h
#pragma once
#include <cstdint>
#include <cstddef>
#include <type_traits>
#define ROCK_CALL __cdecl

// Frozen common value vocabulary. Feature records belong to their own headers.
#pragma pack(push, 8)
namespace rock::api {
enum class Hand : std::uint32_t
{
None = 0,
Right = 1,
Left = 2,
};

// A weapon slot is the native equipment index: 0 is the first equipped
// weapon, 1 the second. Which hand fires or supports a weapon is data in
// its slot's state, never implied by the slot.
inline constexpr std::uint32_t kMaxWeaponSlots = 2;

enum class Status : std::uint32_t
{
Ok = 0,
NotReady = 1,
InvalidArgument = 2,
InvalidSize = 3,
UnsupportedVersion = 4,
CapacityFull = 5,
OwnerNotRegistered = 6,
OwnerConflict = 7,
PermissionDenied = 8,
WorldNotReady = 9,
TargetInvalid = 10,
TargetUnavailable = 11,
HandUnavailable = 12,
HandBusy = 13,
ObjectAlreadyOwned = 14,
RequestQueued = 15,
RequestNotFound = 16,
WrongThread = 17,
AlreadyCommitted = 18,
// The player has turned off the ROCK feature the request needs.
// Nothing was queued; do it the game's own way.
FeatureDisabled = 19,
UnknownInterface = 100,
UnsupportedMajor = 101,
UnsupportedMinor = 102,
TableTooSmall = 103,
OwnerRevoked = 104,
InternalError = 105,
Busy = 106,
Ambiguous = 107,
GenerationMismatch = 108,
};

struct Transform
{
float rotate[9]{};
float translate[3]{};
float scale{ 1.0f };
};

struct Point3
{
float x{ 0.0f };
float y{ 0.0f };
float z{ 0.0f };
};

enum class PowerArmorPoint : std::uint32_t
{
None = 0,
LeftArmorHand = 1,
RightArmorHand = 2,
};

struct Bounds3
{
Point3 min{};
Point3 max{};
std::uint32_t valid{ 0 };
std::uint32_t reserved{ 0 };
};

using OwnerToken = std::uint64_t;
enum class InterfaceId : std::uint32_t {
Core=1, Hands=2, Collision=3, Grab=4, Touch=5, Weapon=6,
WeaponParts=7, Animation=8, Input=9,
// 10 (References) is retired: its queries are part of Grab. Never reuse.
References=10,
// 12 (Diagnostics) is retired: other mods draw through RPS UI Framework. Never reuse.
// 13 (Configuration) is retired: ROCK's settings are edited in Mods Config FO4 VR. Never reuse.
PlayerController=11, Diagnostics=12, Configuration=13, NativeInteraction=14, Storage=15
};
struct ReferenceKey {
std::uint32_t referenceFormId{};
std::uint32_t referenceNativeHandle{};
std::uint32_t worldGeneration{};
std::uint32_t skeletonGeneration{};
std::uint32_t providerGeneration{};
};
struct Sample {
std::uint64_t frameIndex{};
std::uint64_t publicationSequence{};
std::uint32_t worldGeneration{};
std::uint32_t skeletonGeneration{};
std::uint32_t providerGeneration{};
std::uint32_t collisionGeneration{};
};
static_assert(sizeof(void*) == 8, "ROCK requires a 64-bit consumer");
static_assert(sizeof(Hand) == 4 && sizeof(Status) == 4);

}

#pragma pack(pop)
Complete public declarations: ROCK/TransformMath.h
ROCK/TransformMath.h
#pragma once

#include <algorithm>
#include <cmath>
#include <type_traits>

namespace rock::transform_math
{
/*
* Shared transform convention:
* ROCK keeps grab and collision frames as NiTransform relationships. FO4VR's
* compose helper at 0x1401A8D60 confirms that child-local vectors are applied
* through the stored Ni basis as Transpose()*v. Centralizing that here
* prevents hand grabs, debug markers, two-handed grip, and mesh weapon
* bodies from drifting back into the older row-vector/wand math.
*
* Intermediate arithmetic intentionally promotes to double before writing
* back to Ni's float storage, preserving the established native transform
* convention without changing engine types.
*/

namespace detail
{
template <class Value>
using StoredValue = std::remove_cv_t<std::remove_reference_t<Value>>;

template <class Value>
inline StoredValue<Value> narrowDouble(double value)
{
return static_cast<StoredValue<Value>>(value);
}

template <class Vector>
inline Vector makeVector(double x, double y, double z)
{
Vector result{};
result.x = narrowDouble<decltype(result.x)>(x);
result.y = narrowDouble<decltype(result.y)>(y);
result.z = narrowDouble<decltype(result.z)>(z);
return result;
}

template <class Matrix>
inline void setMatrixEntry(Matrix& matrix, int row, int column, double value)
{
matrix.entry[row][column] = narrowDouble<decltype(matrix.entry[row][column])>(value);
}

template <class Matrix>
inline double multiplyStoredRotationEntry(const Matrix& lhs, const Matrix& rhs, int row, int column)
{
return static_cast<double>(lhs.entry[row][0]) * static_cast<double>(rhs.entry[0][column]) +
static_cast<double>(lhs.entry[row][1]) * static_cast<double>(rhs.entry[1][column]) +
static_cast<double>(lhs.entry[row][2]) * static_cast<double>(rhs.entry[2][column]);
}

template <class Matrix, class Vector>
inline double rotateLocalVectorToWorldAxis(const Matrix& matrix, const Vector& vector, int column)
{
return static_cast<double>(matrix.entry[0][column]) * static_cast<double>(vector.x) +
static_cast<double>(matrix.entry[1][column]) * static_cast<double>(vector.y) +
static_cast<double>(matrix.entry[2][column]) * static_cast<double>(vector.z);
}

template <class Matrix, class Vector>
inline double rotateWorldVectorToLocalAxis(const Matrix& matrix, const Vector& vector, int row)
{
return static_cast<double>(matrix.entry[row][0]) * static_cast<double>(vector.x) +
static_cast<double>(matrix.entry[row][1]) * static_cast<double>(vector.y) +
static_cast<double>(matrix.entry[row][2]) * static_cast<double>(vector.z);
}

template <class Matrix>
inline double rotateWorldOffsetToLocalAxis(const Matrix& matrix, double offsetX, double offsetY, double offsetZ, int row)
{
return static_cast<double>(matrix.entry[row][0]) * offsetX + static_cast<double>(matrix.entry[row][1]) * offsetY +
static_cast<double>(matrix.entry[row][2]) * offsetZ;
}
}

template <class Matrix>
inline Matrix makeIdentityRotation()
{
Matrix result{};
result.entry[0][0] = 1.0f;
result.entry[1][1] = 1.0f;
result.entry[2][2] = 1.0f;
return result;
}

template <class Transform>
inline Transform makeIdentityTransform()
{
Transform result{};
result.rotate = makeIdentityRotation<decltype(result.rotate)>();
result.scale = 1.0f;
return result;
}

template <class Matrix>
inline Matrix transposeRotation(const Matrix& matrix)
{
Matrix result{};
for (int row = 0; row < 3; ++row) {
for (int column = 0; column < 3; ++column) {
result.entry[row][column] = matrix.entry[column][row];
}
}
return result;
}

/*
* Largest deviation of a stored rotation from an orthonormal basis: the
* worst |row.row - 1| or |row_i.row_j| over its rows. Scene bone worlds
* drift ~1e-4 from orthonormal after long float chains.
*/
template <class Matrix>
inline double storedRotationOrthonormalityError(const Matrix& matrix)
{
double worst = 0.0;
for (int a = 0; a < 3; ++a) {
for (int b = a; b < 3; ++b) {
double dot = 0.0;
for (int k = 0; k < 3; ++k) {
dot += static_cast<double>(matrix.entry[a][k]) * static_cast<double>(matrix.entry[b][k]);
}
worst = (std::max)(worst, std::abs(dot - (a == b ? 1.0 : 0.0)));
}
}
return worst;
}

/*
* Gram-Schmidt on the stored rows in double precision, keeping the
* input's handedness. Required before any transform is inverted as a
* transpose or fed back through a renderer: a basis with scale drift
* does not cancel against its transpose, and a product that returns
* one frame later as an input squares the drift every frame.
*/
template <class Matrix>
inline Matrix orthonormalizeStoredRotation(const Matrix& matrix)
{
double rows[3][3]{};
for (int row = 0; row < 3; ++row) {
for (int column = 0; column < 3; ++column) {
rows[row][column] = static_cast<double>(matrix.entry[row][column]);
}
}
const auto dot = [&rows](int a, int b) {
return rows[a][0] * rows[b][0] + rows[a][1] * rows[b][1] + rows[a][2] * rows[b][2];
};
const auto normalize = [&rows, &dot](int row) {
const double length = std::sqrt(dot(row, row));
if (!(length > 1e-9)) {
return false;
}
rows[row][0] /= length;
rows[row][1] /= length;
rows[row][2] /= length;
return true;
};
const auto subtractProjection = [&rows, &dot](int row, int onto) {
const double amount = dot(row, onto);
rows[row][0] -= amount * rows[onto][0];
rows[row][1] -= amount * rows[onto][1];
rows[row][2] -= amount * rows[onto][2];
};
if (!normalize(0)) {
return matrix;
}
subtractProjection(1, 0);
if (!normalize(1)) {
return matrix;
}
subtractProjection(2, 0);
subtractProjection(2, 1);
if (!normalize(2)) {
return matrix;
}
Matrix result{};
for (int row = 0; row < 3; ++row) {
for (int column = 0; column < 3; ++column) {
detail::setMatrixEntry(result, row, column, rows[row][column]);
}
}
return result;
}

template <class Transform>
inline Transform orthonormalizedTransform(const Transform& transform)
{
Transform result = transform;
result.rotate = orthonormalizeStoredRotation(transform.rotate);
return result;
}

template <class Matrix>
inline Matrix multiplyStoredRotations(const Matrix& lhs, const Matrix& rhs)
{
Matrix result{};
for (int row = 0; row < 3; ++row) {
for (int column = 0; column < 3; ++column) {
detail::setMatrixEntry(result, row, column, detail::multiplyStoredRotationEntry(lhs, rhs, row, column));
}
}
return result;
}

template <class Matrix, class Vector>
inline Vector rotateLocalVectorToWorld(const Matrix& matrix, const Vector& vector)
{
return detail::makeVector<Vector>(
detail::rotateLocalVectorToWorldAxis(matrix, vector, 0),
detail::rotateLocalVectorToWorldAxis(matrix, vector, 1),
detail::rotateLocalVectorToWorldAxis(matrix, vector, 2));
}

template <class Matrix, class Vector>
inline Vector rotateWorldVectorToLocal(const Matrix& matrix, const Vector& vector)
{
return detail::makeVector<Vector>(
detail::rotateWorldVectorToLocalAxis(matrix, vector, 0),
detail::rotateWorldVectorToLocalAxis(matrix, vector, 1),
detail::rotateWorldVectorToLocalAxis(matrix, vector, 2));
}

template <class Transform, class Vector>
inline Vector localVectorToWorld(const Transform& transform, const Vector& vector)
{
const double scale = static_cast<double>(transform.scale);
return detail::makeVector<Vector>(
detail::rotateLocalVectorToWorldAxis(transform.rotate, vector, 0) * scale,
detail::rotateLocalVectorToWorldAxis(transform.rotate, vector, 1) * scale,
detail::rotateLocalVectorToWorldAxis(transform.rotate, vector, 2) * scale);
}

template <class Transform, class Vector>
inline Vector worldVectorToLocal(const Transform& transform, const Vector& vector)
{
const double scale = static_cast<double>(transform.scale);
const double inverseScale = (std::abs(scale) > 0.0001) ? (1.0 / scale) : 1.0;
return detail::makeVector<Vector>(
detail::rotateWorldVectorToLocalAxis(transform.rotate, vector, 0) * inverseScale,
detail::rotateWorldVectorToLocalAxis(transform.rotate, vector, 1) * inverseScale,
detail::rotateWorldVectorToLocalAxis(transform.rotate, vector, 2) * inverseScale);
}

template <class Transform, class Vector>
inline Vector localPointToWorld(const Transform& transform, const Vector& point)
{
const double scale = static_cast<double>(transform.scale);
return detail::makeVector<Vector>(
static_cast<double>(transform.translate.x) + detail::rotateLocalVectorToWorldAxis(transform.rotate, point, 0) * scale,
static_cast<double>(transform.translate.y) + detail::rotateLocalVectorToWorldAxis(transform.rotate, point, 1) * scale,
static_cast<double>(transform.translate.z) + detail::rotateLocalVectorToWorldAxis(transform.rotate, point, 2) * scale);
}

template <class Transform, class Vector>
inline Vector worldPointToLocal(const Transform& transform, const Vector& point)
{
const double scale = static_cast<double>(transform.scale);
const double inverseScale = (std::abs(scale) > 0.0001) ? (1.0 / scale) : 1.0;
const double offsetX = static_cast<double>(point.x) - static_cast<double>(transform.translate.x);
const double offsetY = static_cast<double>(point.y) - static_cast<double>(transform.translate.y);
const double offsetZ = static_cast<double>(point.z) - static_cast<double>(transform.translate.z);

return detail::makeVector<Vector>(
detail::rotateWorldOffsetToLocalAxis(transform.rotate, offsetX, offsetY, offsetZ, 0) * inverseScale,
detail::rotateWorldOffsetToLocalAxis(transform.rotate, offsetX, offsetY, offsetZ, 1) * inverseScale,
detail::rotateWorldOffsetToLocalAxis(transform.rotate, offsetX, offsetY, offsetZ, 2) * inverseScale);
}

template <class Transform>
inline Transform composeTransforms(const Transform& parent, const Transform& child)
{
Transform result = makeIdentityTransform<Transform>();
result.rotate = multiplyStoredRotations(child.rotate, parent.rotate);
result.translate = localPointToWorld(parent, child.translate);
result.scale = detail::narrowDouble<decltype(result.scale)>(static_cast<double>(parent.scale) * static_cast<double>(child.scale));
return result;
}

template <class Transform>
inline Transform invertTransform(const Transform& transform)
{
Transform result = makeIdentityTransform<Transform>();
result.rotate = transposeRotation(transform.rotate);
const double scale = static_cast<double>(transform.scale);
result.scale = detail::narrowDouble<decltype(result.scale)>((std::abs(scale) > 0.0001) ? (1.0 / scale) : 1.0);
const decltype(transform.translate) origin{};
result.translate = worldPointToLocal(transform, origin);
return result;
}

template <class Matrix, class Vector>
inline Vector matrixToEulerRadians(const Matrix& matrix)
{
Vector result{};
const float m02 = std::clamp(matrix.entry[0][2], -1.0f, 1.0f);

if (m02 < 1.0f) {
if (m02 > -1.0f) {
result.x = std::atan2(-matrix.entry[1][2], matrix.entry[2][2]);
result.y = std::asin(m02);
result.z = std::atan2(-matrix.entry[0][1], matrix.entry[0][0]);
} else {
result.x = -std::atan2(-matrix.entry[1][0], matrix.entry[1][1]);
result.y = -1.5707963267948966f;
result.z = 0.0f;
}
} else {
result.x = std::atan2(matrix.entry[1][0], matrix.entry[1][1]);
result.y = 1.5707963267948966f;
result.z = 0.0f;
}

const auto sanitizeZero = [](float value) {
return std::abs(value) < 0.00001f ? 0.0f : value;
};
result.x = sanitizeZero(result.x);
result.y = sanitizeZero(result.y);
result.z = sanitizeZero(result.z);
return result;
}

// Native reference Euler radians reconstruct the stored matrix as Rz*Ry*Rx.
// The XYZ extractor above gives a different pose for combined rotations.
// Confirmed against equipped-drop request/loose-root telemetry; keep this
// conversion at the reference-spawn boundary, outside Ni/body composition.
template <class Matrix, class Vector>
inline Vector matrixToReferenceEulerRadians(const Matrix& matrix)
{
const double horizontal = std::hypot(
static_cast<double>(matrix.entry[0][0]),
static_cast<double>(matrix.entry[1][0]));
const double pitch = std::atan2(-static_cast<double>(matrix.entry[2][0]), horizontal);
if (horizontal > 0.000001) {
return detail::makeVector<Vector>(
std::atan2(static_cast<double>(matrix.entry[2][1]), static_cast<double>(matrix.entry[2][2])),
pitch,
std::atan2(static_cast<double>(matrix.entry[1][0]), static_cast<double>(matrix.entry[0][0])));
}
// At either vertical pole, choose zero roll and retain the combined
// yaw/roll orientation. atan2 avoids asin losing near-pole precision.
return detail::makeVector<Vector>(0.0, pitch,
std::atan2(-static_cast<double>(matrix.entry[0][1]), static_cast<double>(matrix.entry[1][1])));
}

/*
* FO4VR hknp BODY slots store the three local axes as 4-float blocks:
* [0,1,2], [4,5,6], [8,9,10]. ROCK's grab-space NiTransform helpers also
* store local axes as rows, because localVectorToWorld applies x*row0 +
* y*row1 + z*row2. BODY readback and deferred BODY writes must therefore
* copy those axis blocks directly. The older column transpose helpers below
* remain available for native helpers that intentionally build a column-axis
* NiMatrix before passing it across the hkTransform boundary.
*/
template <class Matrix>
inline Matrix hknpBodyColumnsToNiStoredAxes(const float* bodyFloats)
{
Matrix result{};
result.entry[0][0] = bodyFloats[0];
result.entry[0][1] = bodyFloats[1];
result.entry[0][2] = bodyFloats[2];
result.entry[0][3] = 0.0f;

result.entry[1][0] = bodyFloats[4];
result.entry[1][1] = bodyFloats[5];
result.entry[1][2] = bodyFloats[6];
result.entry[1][3] = 0.0f;

result.entry[2][0] = bodyFloats[8];
result.entry[2][1] = bodyFloats[9];
result.entry[2][2] = bodyFloats[10];
result.entry[2][3] = 0.0f;
return result;
}

template <class Matrix>
inline Matrix niStoredAxesToHknpBodyColumns(const Matrix& matrix)
{
Matrix result{};
result.entry[0][0] = matrix.entry[0][0];
result.entry[0][1] = matrix.entry[0][1];
result.entry[0][2] = matrix.entry[0][2];
result.entry[0][3] = 0.0f;

result.entry[1][0] = matrix.entry[1][0];
result.entry[1][1] = matrix.entry[1][1];
result.entry[1][2] = matrix.entry[1][2];
result.entry[1][3] = 0.0f;

result.entry[2][0] = matrix.entry[2][0];
result.entry[2][1] = matrix.entry[2][1];
result.entry[2][2] = matrix.entry[2][2];
result.entry[2][3] = 0.0f;
return result;
}

// Transpose between ROCK stored rows and native helpers that deliberately
// use NiMatrix columns as physical axes before building an hkTransformf.
template <class Matrix>
inline Matrix niRowsToHavokColumns(const Matrix& matrix)
{
Matrix result{};
result.entry[0][0] = matrix.entry[0][0];
result.entry[0][1] = matrix.entry[1][0];
result.entry[0][2] = matrix.entry[2][0];
result.entry[0][3] = 0.0f;

result.entry[1][0] = matrix.entry[0][1];
result.entry[1][1] = matrix.entry[1][1];
result.entry[1][2] = matrix.entry[2][1];
result.entry[1][3] = 0.0f;

result.entry[2][0] = matrix.entry[0][2];
result.entry[2][1] = matrix.entry[1][2];
result.entry[2][2] = matrix.entry[2][2];
result.entry[2][3] = 0.0f;
return result;
}

template <class Matrix>
inline Matrix havokQuaternionToNiRows(const float quaternion[4])
{
Matrix matrix = makeIdentityRotation<Matrix>();
float x = quaternion[0];
float y = quaternion[1];
float z = quaternion[2];
float w = quaternion[3];

const float length = std::sqrt(x * x + y * y + z * z + w * w);
if (length <= 0.000001f) {
return matrix;
}

const float invLength = 1.0f / length;
x *= invLength;
y *= invLength;
z *= invLength;
w *= invLength;

matrix.entry[0][0] = 1.0f - 2.0f * (y * y + z * z);
matrix.entry[0][1] = 2.0f * (x * y - w * z);
matrix.entry[0][2] = 2.0f * (x * z + w * y);
matrix.entry[0][3] = 0.0f;
matrix.entry[1][0] = 2.0f * (x * y + w * z);
matrix.entry[1][1] = 1.0f - 2.0f * (x * x + z * z);
matrix.entry[1][2] = 2.0f * (y * z - w * x);
matrix.entry[1][3] = 0.0f;
matrix.entry[2][0] = 2.0f * (x * z - w * y);
matrix.entry[2][1] = 2.0f * (y * z + w * x);
matrix.entry[2][2] = 1.0f - 2.0f * (x * x + y * y);
matrix.entry[2][3] = 0.0f;
return matrix;
}

template <class Matrix>
inline void niRowsToHavokQuaternion(const Matrix& matrix, float outQuat[4])
{
const float trace = matrix.entry[0][0] + matrix.entry[1][1] + matrix.entry[2][2];
float x = 0.0f;
float y = 0.0f;
float z = 0.0f;
float w = 1.0f;

if (trace > 0.0f) {
const float root = std::sqrt(trace + 1.0f);
const float inv = 0.5f / root;
w = root * 0.5f;
x = (matrix.entry[2][1] - matrix.entry[1][2]) * inv;
y = (matrix.entry[0][2] - matrix.entry[2][0]) * inv;
z = (matrix.entry[1][0] - matrix.entry[0][1]) * inv;
} else if (matrix.entry[0][0] > matrix.entry[1][1] && matrix.entry[0][0] > matrix.entry[2][2]) {
const float root = std::sqrt((matrix.entry[0][0] - (matrix.entry[1][1] + matrix.entry[2][2])) + 1.0f);
const float inv = 0.5f / root;
x = root * 0.5f;
y = (matrix.entry[1][0] + matrix.entry[0][1]) * inv;
z = (matrix.entry[0][2] + matrix.entry[2][0]) * inv;
w = (matrix.entry[2][1] - matrix.entry[1][2]) * inv;
} else if (matrix.entry[1][1] > matrix.entry[2][2]) {
const float root = std::sqrt((matrix.entry[1][1] - (matrix.entry[2][2] + matrix.entry[0][0])) + 1.0f);
const float inv = 0.5f / root;
x = (matrix.entry[1][0] + matrix.entry[0][1]) * inv;
y = root * 0.5f;
z = (matrix.entry[2][1] + matrix.entry[1][2]) * inv;
w = (matrix.entry[0][2] - matrix.entry[2][0]) * inv;
} else {
const float root = std::sqrt((matrix.entry[2][2] - (matrix.entry[0][0] + matrix.entry[1][1])) + 1.0f);
const float inv = 0.5f / root;
x = (matrix.entry[0][2] + matrix.entry[2][0]) * inv;
y = (matrix.entry[2][1] + matrix.entry[1][2]) * inv;
z = root * 0.5f;
w = (matrix.entry[1][0] - matrix.entry[0][1]) * inv;
}

const float length = std::sqrt(x * x + y * y + z * z + w * w);
if (length > 0.0f) {
const float invLength = 1.0f / length;
x *= invLength;
y *= invLength;
z *= invLength;
w *= invLength;
}

outQuat[0] = x;
outQuat[1] = y;
outQuat[2] = z;
outQuat[3] = w;
}
}