Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save ynagatomo/17c092e6c5357e44c5d5d0cdf85a06f0 to your computer and use it in GitHub Desktop.
Save ynagatomo/17c092e6c5357e44c5d5d0cdf85a06f0 to your computer and use it in GitHub Desktop.
import simd
// Initializers
simd_quatf()
simd_quatf(vector: simd_float4(0, 0, 0, 1)) // [0, 1, 2]: img, [3]: real
simd_quatf(simd_float3x3(SIMD3<Float>(cos(Float.pi/2), -sin(Float.pi/2), 0), SIMD3<Float>(sin(Float.pi/2), cos(Float.pi/2), 0), SIMD3<Float>(0, 0, 1))) // from 3x3 rotation matrix, which must be orthogonal and have a determinant of 1
simd_float3x3(SIMD3<Float>(cos(Float.pi/2), -sin(Float.pi/2), 0), SIMD3<Float>(sin(Float.pi/2), cos(Float.pi/2), 0), SIMD3<Float>(0, 0, 1)).determinant
simd_quatf(simd_float4x4(SIMD4<Float>(cos(Float.pi/2), -sin(Float.pi/2), 0, 0), SIMD4<Float>(sin(Float.pi/2), cos(Float.pi/2), 0, 0), SIMD4<Float>(0, 0, 1, 0), SIMD4<Float>(0, 0, 0, 1))) // from 4x4 rotation matrix, whose 3x3 part must be orthogonal and have a determinant of 1
simd_quatf(angle: Float.pi/2, axis: SIMD3<Float>(0, 0, -1)) // rotation about an axis
simd_quatf(from: SIMD3<Float>(0, 1, 0), to: SIMD3<Float>(1, 0, 0)) // rotation between two vectors. They should be normalized.
simd_quatf(ix: 0, iy: 0, iz: 0, r: 1) // from scalar values
simd_quatf(real: 1, imag: SIMD3<Float>.zero) // from a scalar value and a vector
// Quaternion Properties
simd_quatf(simd_float3x3(SIMD3<Float>(cos(Float.pi/2), -sin(Float.pi/2), 0), SIMD3<Float>(sin(Float.pi/2), cos(Float.pi/2), 0), SIMD3<Float>(0, 0, 1))).angle
simd_quatf(simd_float3x3(SIMD3<Float>(cos(Float.pi/2), -sin(Float.pi/2), 0), SIMD3<Float>(sin(Float.pi/2), cos(Float.pi/2), 0), SIMD3<Float>(0, 0, 1))).axis
simd_quatf(ix: 1, iy: 2, iz: 3, r: 4).conjugate // conjugate
simd_quatf(ix: 1, iy: 2, iz: 3, r: 4).imag
simd_quatf(ix: 1, iy: 2, iz: 3, r: 4).real
simd_quatf(simd_float3x3(SIMD3<Float>(cos(Float.pi/2), -sin(Float.pi/2), 0), SIMD3<Float>(sin(Float.pi/2), cos(Float.pi/2), 0), SIMD3<Float>(0, 0, 1))).inverse
simd_quatf(ix: 1, iy: 2, iz: 3, r: 4).length
simd_quatf(ix: 1, iy: 2, iz: 3, r: 4).normalized
simd_quatf(ix: 1, iy: 2, iz: 3, r: 4).normalized.length
simd_quatf(ix: 1, iy: 2, iz: 3, r: 4).vector
// Math Functions
simd_add(simd_quatf(ix: 1, iy: 2, iz: 3, r: 4), simd_quatf(ix: 1, iy: 2, iz: 3, r: 4))
simd_mul(simd_quatf(ix: 1, iy: 2, iz: 3, r: 4), simd_quatf(ix: 5, iy: 6, iz: 7, r: 8))
simd_mul(simd_quatf(ix: 5, iy: 6, iz: 7, r: 8), simd_quatf(ix: 1, iy: 2, iz: 3, r: 4))
simd_mul(2, simd_quatf(ix: 1, iy: 2, iz: 3, r: 4))
simd_mul(simd_quatf(ix: 1, iy: 2, iz: 3, r: 4), 2)
simd_sub(simd_quatf(ix: 1, iy: 1, iz: 1, r: 1), simd_quatf(ix: 3, iy: 4, iz: 5, r: 2))
// Quaternion Functions
simd_act(simd_quatf(angle: Float.pi/2, axis: SIMD3<Float>(0, 0, -1)), simd_float3(1, 0, 0))
simd_angle(simd_quatf(angle: Float.pi/2, axis: SIMD3<Float>(0, 0, -1)))
simd_axis(simd_quatf(angle: Float.pi/2, axis: SIMD3<Float>(0, 0, -1)))
simd_negate(simd_quatf(ix: 1, iy: 2, iz: 3, r: 4))
simd_slerp(simd_quatf(angle: Float.pi/2, axis: SIMD3<Float>(1, 0, 0)) ,simd_quatf(angle: Float.pi/2, axis: SIMD3<Float>(0, 1, 0)), 0) // (q0, q1, t)
simd_slerp(simd_quatf(angle: Float.pi/2, axis: SIMD3<Float>(1, 0, 0)) ,simd_quatf(angle: Float.pi/2, axis: SIMD3<Float>(0, 1, 0)), 0.5) // (q0, q1, t)
simd_slerp(simd_quatf(angle: Float.pi/2, axis: SIMD3<Float>(1, 0, 0)) ,simd_quatf(angle: Float.pi/2, axis: SIMD3<Float>(0, 1, 0)), 1.0) // (q0, q1, t)
simd_slerp_longest(simd_quatf(angle: Float.pi/2, axis: SIMD3<Float>(1, 0, 0)) ,simd_quatf(angle: Float.pi/2, axis: SIMD3<Float>(0, 1, 0)), 0.5) // (q0, q1, t)
// Geometry Functions
simd_dot(simd_quatf(ix: 1, iy: 2, iz: 3, r: 4), simd_quatf(ix: 5, iy: 6, iz: 7, r: 8))
dot(simd_quatf(ix: 1, iy: 2, iz: 3, r: 4), simd_quatf(ix: 5, iy: 6, iz: 7, r: 8))
exp(simd_quatf(ix: 1, iy: 1, iz: 1, r: 1))
// Instance Methods
simd_quatf(angle: Float.pi/2, axis: SIMD3<Float>(0, 0, -1)).act(SIMD3<Float>(1, 0, 0))
// Operators
2 * simd_quatf(ix: 1, iy: 2, iz: 3, r: 4)
simd_quatf(ix: 1, iy: 2, iz: 3, r: 4) * 2
simd_quatf(ix: 1, iy: 2, iz: 3, r: 4) * simd_quatf(ix: 5, iy: 6, iz: 7, r: 8)
simd_quatf(ix: 5, iy: 6, iz: 7, r: 8) * simd_quatf(ix: 1, iy: 2, iz: 3, r: 4)
simd_quatf(ix: 1, iy: 2, iz: 3, r: 4) + simd_quatf(ix: 1, iy: 2, iz: 3, r: 4)
simd_quatf(ix: 1, iy: 2, iz: 3, r: 4) - simd_quatf(ix: 1, iy: 1, iz: 1, r: 1)
simd_quatf(ix: 1, iy: 2, iz: 3, r: 4) / 2
simd_quatf(ix: 1, iy: 2, iz: 3, r: 4) / simd_quatf(ix: 1, iy: 2, iz: 3, r: 4)
simd_quatf(ix: 1, iy: 2, iz: 3, r: 4) == simd_quatf(ix: 1, iy: 2, iz: 3, r: 4)
simd_quatf(ix: 1, iy: 2, iz: 3, r: 4) != simd_quatf(ix: 1, iy: 2, iz: 3, r: 4)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment