クオータニオンのメモ

クオータニオンのメモ

class Quaternion {
  constructor (w = 0, x = 0, y = 0, z = 0) {
    this.w = w; this.x = x; this.y = y; this.z = z;
  }

  add ({w, x, y, z}) {
    this.w += w; this.x += x; this.y += y; this.z += z;
    return this;
  }

  mult ({w, x, y, z}) {
    let {w:W, x:X, y:Y, z:Z} = this;
    this.w = W*w - X*x - Y*y - Z*z; this.x = W*x + X*w + Y*z - Z*y;
    this.y = W*y - X*z + Y*w + Z*x; this.z = W*z + X*y - Y*x + Z*w;
    return this;
  }
  
  norm () {
    return this.w**2 + this.x**2 + this.y**2 + this.z**2;
  }

  abs () {
    return Math.sqrt (this.norm ());
  }

  inverse () {
    let n = this.norm ();
    this.w /= n;
    this.x /= -n;
    this.y /= -n;
    this.z /= -n;
    return this;
  }
}