All Projects → infusion → Quaternion.js

infusion / Quaternion.js

Licence: MIT License
A JavaScript Quaternion library

Programming Languages

javascript
184084 projects - #8 most used programming language

Projects that are alternatives of or similar to Quaternion.js

exponential-moving-average
Calculate an exponential moving average from an array of numbers.
Stars: ✭ 41 (-52.33%)
Mutual labels:  math, numbers
SCNMathExtensions
Math extensions for SCNVector3, SCNQuaternion, SCNMatrix4
Stars: ✭ 32 (-62.79%)
Mutual labels:  math, quaternion
versor
a home for Mike Bostock's versor.js
Stars: ✭ 26 (-69.77%)
Mutual labels:  quaternion, rotation
abacus
📐 C# cross precision 3D maths library.
Stars: ✭ 35 (-59.3%)
Mutual labels:  math, quaternion
Quadcopter SimCon
Quadcopter Simulation and Control. Dynamics generated with PyDy.
Stars: ✭ 84 (-2.33%)
Mutual labels:  quaternion, rotation
spatialmath-matlab
Create, manipulate and convert representations of position and orientation in 2D or 3D using Python
Stars: ✭ 142 (+65.12%)
Mutual labels:  math, quaternion
python-baseconv
Python module to convert numbers from base 10 integers to base X strings and back again.
Stars: ✭ 40 (-53.49%)
Mutual labels:  math, numbers
SurrealNumbers.jl
Implementation of Conway's Surreal Numbers
Stars: ✭ 30 (-65.12%)
Mutual labels:  math, numbers
zalgebra
Linear algebra library for games and real-time graphics.
Stars: ✭ 129 (+50%)
Mutual labels:  math, quaternion
rotation master
Provide conversion between the major representations of 3D rotation and visualize the orientation of a rigid body
Stars: ✭ 157 (+82.56%)
Mutual labels:  quaternion, rotation
hlml
vectorized high-level math library
Stars: ✭ 42 (-51.16%)
Mutual labels:  math, quaternion
yt-channels-DS-AI-ML-CS
A comprehensive list of 180+ YouTube Channels for Data Science, Data Engineering, Machine Learning, Deep learning, Computer Science, programming, software engineering, etc.
Stars: ✭ 1,038 (+1106.98%)
Mutual labels:  math
Computer-Vision
implemented some computer vision problems
Stars: ✭ 25 (-70.93%)
Mutual labels:  math
MROGeometry
♊️ Mirror of http://purl.mro.name/ios/MROGeometry | √ C and Objective C math and geometry helpers
Stars: ✭ 58 (-32.56%)
Mutual labels:  math
launchpad
Resources to get started in Quantum Computing!
Stars: ✭ 21 (-75.58%)
Mutual labels:  math
mathlion
Mathlion is an advanced math plugin for Kibana's Timelion
Stars: ✭ 77 (-10.47%)
Mutual labels:  math
calculus-notes
微积分学笔记,包含极限论、微分、积分、级数理论.
Stars: ✭ 67 (-22.09%)
Mutual labels:  math
card-shuffling
The Math of Card Shufflig
Stars: ✭ 33 (-61.63%)
Mutual labels:  math
minisketch-rs
Rust bindings to @sipa's minisketch library
Stars: ✭ 16 (-81.4%)
Mutual labels:  math
Algorithm-Math
算法 & 数学知识 & 重拾基础知识系列文章编写和收集
Stars: ✭ 19 (-77.91%)
Mutual labels:  math

Quaternion.js - ℍ in JavaScript

NPM Package Build Status MIT license

Quaternion.js is a well tested JavaScript library for 3D rotations. Quaternions can be used everywhere, from the rotation calculation of your mobile phone over computer games to the rotation of satellites and all by avoiding the Gimbal lock. The library comes with examples to make you get started much quicker without worrying about the math behind.

Examples

var Quaternion = require('quaternion');

var q = new Quaternion("99.3+8i");
c.mul(1,2,3,4).div([3,4,1]).sub(7, [1, 2, 3]);

HTML5 Device Orientation

In order to create a HTML element, which always rotates in 3D with your mobile device, all you need is the following snippet. Look at the examples folder for a complete version.

var rad = Math.PI / 180;
window.addEventListener("deviceorientation", function(ev) {

  // Update the rotation object
  var q = Quaternion.fromEuler(ev.alpha * rad, ev.beta * rad, ev.gamma * rad, 'ZXY');

  // Set the CSS style to the element you want to rotate
  elm.style.transform = "matrix3d(" + q.conjugate().toMatrix4() + ")";

}, true);

Parser

Any function (see below) as well as the constructor of the Quaternion class parses its input like this.

You can pass either Objects, Doubles or Strings.

Arguments

Calling the constructor will create a quaternion 1-element.

new Quaternion() // 1 + 0i + 0j + 0k

The typical use case contains all quaternion parameters

new Quaternion(w, x, y, z)

Objects

Quaternion as angle and vector. Note: This is not equivalent to Quaternion.fromAxisAngle()!

new Quaternion(w, [x, y, z])

Quaternion as an object (it's ok to leave components out)

new Quaternion({w: w, x: x, y: y, z: z})

Quaternion out of a complex number, e.g. Complex.js.

new Quaternion({re: real, im: imaginary})

Quaternion out of a 4 elements vector

new Quaternion([w, x, y, z])

Augmented Quaternion out of a 3 elements vector

new Quaternion([x, y, z])

Doubles

new Quaternion(55.4);

Strings

new Quaternion('1 - 2i - 3j - 4k')
new Quaternion("123.45");
new Quaternion("15+3i");
new Quaternion("i");

Functions

Every stated parameter n in the following list of functions behaves in the same way as the constructor examples above

Note: Calling a method like add() without parameters passes a quaternion with all elements zero, not one!

Quaternion add(n)

Adds two quaternions Q1 and Q2

Quaternion sub(n)

Subtracts a quaternions Q2 from Q1

Quaternion neg()

Calculates the additive inverse, or simply it negates the quaternion

Quaternion norm()

Calculates the length/modulus/magnitude or the norm of a quaternion

Quaternion normSq()

Calculates the squared length/modulus/magnitude or the norm of a quaternion

Quaternion normalize()

Normalizes the quaternion to have |Q| = 1 as long as the norm is not zero. Alternative names are the signum, unit or versor

Quaternion mul(n)

Calculates the Hamilton product of two quaternions. Leaving out the imaginary part results in just scaling the quat.

Note: This function is not commutative, i.e. order matters!

Quaternion scale(s)

Scales a quaternion by a scalar, faster than using multiplication

Quaternion dot()

Calculates the dot product of two quaternions

Quaternion inverse()

Calculates the inverse of a quat for non-normalized quats such that Q^-1 * Q = 1 and Q * Q^-1 = 1

Quaternion div(n)

Multiplies a quaternion with the inverse of a second quaternion

Quaternion conjugate()

Calculates the conjugate of a quaternion. If the quaternion is normalized, the conjugate is the inverse of the quaternion - but faster.

Quaternion pow(n)

Calculates the power of a quaternion raised to the quaternion n

Quaternion exp()

Calculates the natural exponentiation of the quaternion

Quaternion log()

Calculates the natural logarithm of the quaternion

double real()

Returns the real w part of the quaternion

Quaternion imag()

Returns the imaginary part [x, y, z] of the quaternion as a 3D vector / array

boolean equals(n)

Checks if two quats are the same

boolean isFinite

Checks if all parts of a quaternion are finite

boolean isNaN

Checks if any of the parts of the quaternion is not a number

String toString()

Gets the Quaternion as a well formatted string

Array toVector()

Gets the actual quaternion as a 4D vector / array

Array toMatrix(2d=false)

Calculates the 3x3 rotation matrix for the current quat as a 9 element array or alternatively as a 2d array

Array toMatrix4(2d=false)

Calculates the homogeneous 4x4 rotation matrix for the current quat as a 16 element array or alternatively as a 2d array

Quaternion clone()

Clones the actual object

Array rotateVector(v)

Rotates a 3 component vector, represented as an array by the current quaternion

Quaternion slerp(q)(pct)

Returns a function to spherically interpolate between two quaternions. Called with a percentage [0-1], the function returns the interpolated Quaternion.

Quaternion.fromAxisAngle(axis, angle)

Gets a quaternion by a rotation given as an axis and angle

Quaternion.fromEuler(Φ, θ, ψ[, order="ZXY"])

Gets a quaternion given three Euler angles. The angles are applied from right to left.

So, order ZXY for example means first rotate around Y by ψ then around X by θ and then around Z by Φ (RotZ(Φ)RotX(θ)RotY(ψ)). The order can take the string value ZXY, XYZ or RPY, YXZ, ZYX or YPR, YZX, XZY.

Relations

  • axisAngle([0, 1, 0], x)axisAngle([0, 0, 1], y)axisAngle([1, 0, 0], z) = fromEuler(x, y, z, 'YZX')
  • Mathematica RollPitchYawMatrix[{α,β,γ}] = fromEuler(γ, β, α, 'RPY')

Quaternion.fromBetweenVectors(u, v)

Calculates the quaternion to rotate one vector onto the other

Quaternion.random()

Gets a spherical random number

Constants

Quaternion.ZERO

A quaternion zero instance (additive identity)

Quaternion.ONE

A quaternion one instance (multiplicative identity)

Quaternion.I

An imaginary number i instance

Quaternion.J

An imaginary number j instance

Quaternion.K

An imaginary number k instance

Quaternion.EPSILON

A small epsilon value used for equals() comparison in order to circumvent double imprecision.

Installation

Installing Quaternion.js is as easy as cloning this repo or use one of the following commands:

bower install quaternion

or

npm install quaternion

Using Quaternion.js with the browser

<script src="quaternion.js"></script>
<script>
    console.log(Quaternion("1 + 2i - 3j + 4k"));
</script>

Using Quaternion.js with require.js

<script src="require.js"></script>
<script>
requirejs(['quaternion.js'],
function(Quaternion) {
    console.log(Quaternion("1 + 2i - 3j + 4k"));
});
</script>

Coding Style

As every library I publish, Quaternion.js is also built to be as small as possible after compressing it with Google Closure Compiler in advanced mode. Thus the coding style orientates a little on maxing-out the compression rate. Please make sure you keep this style if you plan to extend the library.

Testing

If you plan to enhance the library, make sure you add test cases and all the previous tests are passing. You can test the library with

npm test

Copyright and licensing

Copyright (c) 2017, Robert Eisele Dual licensed under the MIT or GPL Version 2 licenses.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].