API reference
Every public method of Quaternion, grouped by purpose.
Every method returns a new Quaternion unless the return type says otherwise. All methods belong to PHPMath\Quaternion\Quaternion. A vector is array{float, float, float}. Angles are in radians.
Construction#
Named constructors that create a quaternion. The constructor itself is private.
of()#
public static function of(float $w, float $x, float $y, float $z): self
Creates a quaternion from its four components. The values are not normalized.
- Parameters:
$w,$x,$y,$zthe components - Returns:
Quaternion
identity()#
public static function identity(): self
Returns the identity quaternion (1, 0, 0, 0), which means no rotation.
- Returns:
Quaternion
fromAxisAngle()#
public static function fromAxisAngle(array $axis, float $angle): self
Creates a unit quaternion from a rotation axis and an angle. The axis is normalized.
- Parameters:
$axisthe axis as a list of three numbers[x, y, z];$anglethe angle in radians - Returns:
Quaternion - Throws:
InvalidAxisExceptionif the axis has zero length,InvalidVectorExceptionif the axis is not a list of exactly three numbers
fromAxisComponents()#
public static function fromAxisComponents(float $x, float $y, float $z, float $angle): self
Creates a unit quaternion from the three components of a rotation axis and an angle. The axis is normalized.
- Parameters:
$x,$y,$zthe components of the axis;$anglethe angle in radians - Returns:
Quaternion - Throws:
InvalidAxisExceptionif the axis has zero length
fromEuler()#
public static function fromEuler(float $yaw, float $pitch, float $roll): self
Creates a unit quaternion from intrinsic Z-Y'-X'' angles: yaw about Z, then pitch about the new Y, then roll about the new X.
- Parameters:
$yawthe rotation about Z;$pitchthe rotation about Y;$rollthe rotation about X - Returns:
Quaternion
Reading values#
Methods that return the components.
Arithmetic#
Component-wise arithmetic, the Hamilton product, conjugate and inverse.
add()#
public function add(Quaternion $other): self
Adds another quaternion component by component.
- Parameters:
$otherthe other operand - Returns:
Quaternion
subtract()#
public function subtract(Quaternion $other): self
Subtracts another quaternion component by component.
- Parameters:
$otherthe other operand - Returns:
Quaternion
multiply()#
public function multiply(Quaternion $other): self
Returns the Hamilton product $this * $other. It is not commutative. As a rotation it applies $other first, then $this.
- Parameters:
$otherthe right-hand operand - Returns:
Quaternion
scale()#
public function scale(float $factor): self
Multiplies all four components by a factor.
- Parameters:
$factorthe scale factor - Returns:
Quaternion
conjugate()#
public function conjugate(): self
Keeps w and flips the sign of x, y and z.
- Returns:
Quaternion
inverse()#
public function inverse(): self
Returns the quaternion that multiplies with this one to give the identity.
- Returns:
Quaternion - Throws:
ZeroQuaternionExceptionif the quaternion is zero
Length#
The norm, normalization and the dot product.
normalize()#
public function normalize(): self
Scales the quaternion to a norm of 1.
- Returns:
Quaternion - Throws:
ZeroQuaternionExceptionif the quaternion is zero
dot()#
public function dot(Quaternion $other): float
Returns the dot product with another quaternion.
- Parameters:
$otherthe other operand - Returns:
float
Rotation#
Rotate vectors, build matrices and interpolate. All three normalize the quaternion first, and each has its own message for the zero quaternion.
rotate()#
public function rotate(array $vector): array
Rotates a vector. The quaternion does not have to be a unit quaternion.
- Parameters:
$vectora list of three numbers[x, y, z] - Returns:
array{float, float, float} - Throws:
InvalidVectorExceptionif the vector is not a list of exactly three numbers,ZeroQuaternionExceptionif the quaternion is zero
toRotationMatrix()#
public function toRotationMatrix(): array
Returns the rotation as a 3x3 matrix of three rows, for use with column vectors.
- Returns:
array{array{float, float, float}, array{float, float, float}, array{float, float, float}} - Throws:
ZeroQuaternionExceptionif the quaternion is zero
slerp()#
public function slerp(Quaternion $to, float $t): self
Spherical linear interpolation along the shortest arc. A $t of 0 returns this rotation and 1 returns $to, possibly with all signs flipped (the same rotation, so compare with isSameRotation() and not equals()). Values outside 0 to 1 extrapolate.
- Parameters:
$tothe end rotation;$tthe interpolation factor - Returns:
Quaternion - Throws:
ZeroQuaternionExceptionif either quaternion is zero
Comparison and output#
Compare quaternions and convert them to text.
equals()#
public function equals(Quaternion $other, float $tolerance = 1e-9): bool
Compares the components with an absolute tolerance. q and -q are not equal, use isSameRotation() to compare rotations.
- Parameters:
$otherthe other quaternion;$tolerancethe largest allowed difference per component - Returns:
bool - Throws:
InvalidToleranceExceptionif the tolerance is negative,NANor infinite
isSameRotation()#
public function isSameRotation(Quaternion $other, float $tolerance = 1e-9): bool
Returns true when both quaternions describe the same rotation: their normalized forms are equal, or one is the negation of the other, within the tolerance per component.
- Parameters:
$otherthe other quaternion;$tolerancethe largest allowed difference per component - Returns:
bool - Throws:
ZeroQuaternionExceptionif either quaternion is zero,InvalidToleranceExceptionif the tolerance is negative,NANor infinite
__toString()#
public function __toString(): string
Formats the quaternion like 1 - 2i + 3j + 4k. Each component is a plain decimal with at most 14 decimals, without exponent or trailing zeros, and negative zero is written as 0. The precision ini setting has no effect.
- Returns:
string