setFromEuler method

Quaternion setFromEuler(
  1. double x,
  2. double y,
  3. double z, [
  4. Order order = Order.xyz,
])

Set the quaternion components given Euler angle representation.

@param order The order to apply angles: 'XYZ' or 'YXZ' or any other combination.

See {@link https://www.mathworks.com/matlabcentral/fileexchange/20696-function-to-convert-between-dcm-euler-angles-quaternions-and-euler-vectors mathWorks} reference

Implementation

Quaternion setFromEuler(double x,double y,double z, [Order order = Order.xyz]){
  final c1 = math.cos(x / 2);
  final c2 = math.cos(y / 2);
  final c3 = math.cos(z / 2);
  final s1 = math.sin(x / 2);
  final s2 = math.sin(y / 2);
  final s3 = math.sin(z / 2);

  if (order == Order.xyz) {
    this.x = s1 * c2 * c3 + c1 * s2 * s3;
    this.y = c1 * s2 * c3 - s1 * c2 * s3;
    this.z = c1 * c2 * s3 + s1 * s2 * c3;
    w = c1 * c2 * c3 - s1 * s2 * s3;
  } else if (order == Order.yxz) {
    this.x = s1 * c2 * c3 + c1 * s2 * s3;
    this.y = c1 * s2 * c3 - s1 * c2 * s3;
    this.z = c1 * c2 * s3 - s1 * s2 * c3;
    w = c1 * c2 * c3 + s1 * s2 * s3;
  } else if (order == Order.zxy) {
    this.x = s1 * c2 * c3 - c1 * s2 * s3;
    this.y = c1 * s2 * c3 + s1 * c2 * s3;
    this.z = c1 * c2 * s3 + s1 * s2 * c3;
    w = c1 * c2 * c3 - s1 * s2 * s3;
  } else if (order == Order.zyx) {
    this.x = s1 * c2 * c3 - c1 * s2 * s3;
    this.y = c1 * s2 * c3 + s1 * c2 * s3;
    this.z = c1 * c2 * s3 - s1 * s2 * c3;
    w = c1 * c2 * c3 + s1 * s2 * s3;
  } else if (order == Order.yzx) {
    this.x = s1 * c2 * c3 + c1 * s2 * s3;
    this.y = c1 * s2 * c3 + s1 * c2 * s3;
    this.z = c1 * c2 * s3 - s1 * s2 * c3;
    w = c1 * c2 * c3 - s1 * s2 * s3;
  } else if (order == Order.xzy) {
    this.x = s1 * c2 * c3 - c1 * s2 * s3;
    this.y = c1 * s2 * c3 - s1 * c2 * s3;
    this.z = c1 * c2 * s3 + s1 * s2 * c3;
    w = c1 * c2 * c3 + s1 * s2 * s3;
  }

  return this;
}