Created
January 28, 2019 14:26
-
-
Save Sciss/ef82ed81339fcb92a4f7bd5d043c64dd to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// quick and dirty from | |
// https://math.stackexchange.com/questions/40164/how-do-you-rotate-a-vector-by-a-unit-quaternion | |
def quaternion_mult(q: Vector[Double],r: Vector[Double]) = | |
Vector(r(0)*q(0)-r(1)*q(1)-r(2)*q(2)-r(3)*q(3), | |
r(0)*q(1)+r(1)*q(0)-r(2)*q(3)+r(3)*q(2), | |
r(0)*q(2)+r(1)*q(3)+r(2)*q(0)-r(3)*q(1), | |
r(0)*q(3)-r(1)*q(2)+r(2)*q(1)+r(3)*q(0)) | |
def point_rotation_by_quaternion(point: Vector[Double],q: Vector[Double]) = { | |
val r = 0.0 +: point | |
val q_conj = Vector(q(0),-q(1),-q(2),-q(3)) | |
quaternion_mult(quaternion_mult(q,r),q_conj).tail | |
} | |
println(point_rotation_by_quaternion(Vector(1, 0, 0),Vector(0.5.sqrt, 0.0, 0.5.sqrt, 0.0))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment