Dart Documentationvector_math

vector_math library

Functions

bool pickRay(mat4 cameraMatrix, num viewportX, num viewportWidth, num viewportY, num viewportHeight, num pickX, num pickY, vec3 rayNear, vec3 rayFar) #

On success, rayNear and rayFar are the points where the screen space pickX, pickY intersect with the near and far planes respectively.

The viewport is specified by ( viewportX, viewportWidth) and ( viewportY, viewportHeight).

cameraMatrix includes both the projection and view transforms.

Returns false on error, for example, the mouse is not in the viewport

bool pickRay(mat4 cameraMatrix, num viewportX, num viewportWidth,
              num viewportY, num viewportHeight,
              num pickX, num pickY,
              vec3 rayNear, vec3 rayFar) {

 bool r;

 r = unproject(cameraMatrix, viewportX, viewportWidth, viewportY,
               viewportHeight, pickX, viewportHeight-pickY, 0.0, rayNear);
 if (!r) {
   return false;
 }

 r = unproject(cameraMatrix, viewportX, viewportWidth, viewportY,
               viewportHeight, pickX, viewportHeight-pickY, 1.0, rayFar);

 return r;
}

bool unproject(mat4 cameraMatrix, num viewportX, num viewportWidth, num viewportY, num viewportHeight, num pickX, num pickY, num pickZ, vec3 pickWorld) #

On success, Sets pickWorld to be the world space position of the screen space pickX, pickY, and pickZ.

The viewport is specified by ( viewportX, viewportWidth) and ( viewportY, viewportHeight).

cameraMatrix includes both the projection and view transforms.

pickZ is typically either 0.0 (near plane) or 1.0 (far plane).

Returns false on error, for example, the mouse is not in the viewport

bool unproject(mat4 cameraMatrix, num viewportX, num viewportWidth,
              num viewportY, num viewportHeight,
              num pickX, num pickY, num pickZ,
              vec3 pickWorld) {
 viewportX = viewportX.toDouble();
 viewportWidth = viewportWidth.toDouble();
 viewportY = viewportY.toDouble();
 viewportHeight = viewportHeight.toDouble();
 pickX = pickX.toDouble();
 pickY = pickY.toDouble();
 pickX = (pickX - viewportX);
 pickY = (pickY - viewportY);
 pickX = (2.0 * pickX / viewportWidth) - 1.0;
 pickY = (2.0 * pickY / viewportHeight) - 1.0;
 pickZ = (2.0 * pickZ) - 1.0;

 // Check if pick point is inside unit cube
 if (pickX < -1.0 || pickY < -1.0 || pickX > 1.0 || pickY > 1.0 ||
     pickZ < -1.0 || pickZ > 1.0) {
   return false;
 }

 // Copy camera matrix.
 mat4 invertedCameraMatrix = new mat4.copy(cameraMatrix);
 // Invert the camera matrix.
 invertedCameraMatrix.invert();
 // Determine intersection point.
 vec4 v = new vec4.raw(pickX, pickY, pickZ, 1.0);
 invertedCameraMatrix.transform(v);
 if (v.w == 0.0) {
   return false;
 }
 double invW = 1.0 / v.w;
 pickWorld.x = v.x * invW;
 pickWorld.y = v.y * invW;
 pickWorld.z = v.z * invW;

 return true;
}

mat4 makePlaneReflection(vec3 planeNormal, vec3 planePoint) #

Returns a transformation matrix that transforms points by reflecting them through the plane specified with planeNormal and planePoint

mat4 makePlaneReflection(vec3 planeNormal, vec3 planePoint) {
 vec4 v = new vec4(planeNormal, 0.0);
 mat4 outer = new mat4.outer(v,v);
 outer.scale(2.0);
 mat4 r = new mat4();
 r = r - outer;
 double scale = 2.0 * dot(planePoint, planeNormal);
 vec3 scaledNormal = (planeNormal.scaled(scale));
 vec4 T = new vec4(scaledNormal, 1.0);
 r.col3 = T;
 return r;
}

mat4 makePlaneProjection(vec3 planeNormal, vec3 planePoint) #

Returns a transformation matrix that transforms points onto the plane specified with planeNormal and planePoint

mat4 makePlaneProjection(vec3 planeNormal, vec3 planePoint) {
 vec4 v = new vec4(planeNormal, 0.0);
 mat4 outer = new mat4.outer(v, v);
 mat4 r = new mat4();
 r = r - outer;
 vec3 scaledNormal = (planeNormal.scaled(dot(planePoint, planeNormal)));
 vec4 T = new vec4(scaledNormal, 1.0);
 r.col3 = T;
 return r;
}

mat4 makeOrthographic(num left, num right, num bottom, num top, num znear, num zfar) #

Returns an OpenGL orthographic camera projection matrix

mat4 makeOrthographic(num left, num right, num bottom, num top, num znear,
                     num zfar) {
 left = left.toDouble();
 right = right.toDouble();
 bottom = bottom.toDouble();
 top = top.toDouble();
 znear = znear.toDouble();
 zfar = zfar.toDouble();
 double rml = right - left;
 double rpl = right + left;
 double tmb = top - bottom;
 double tpb = top + bottom;
 double fmn = zfar - znear;
 double fpn = zfar + znear;

 mat4 r = new mat4.zero();
 r[0].x = 2.0/rml;
 r[1].y = 2.0/tmb;
 r[2].z = -2.0/fmn;
 r[3].x = -rpl/rml;
 r[3].y = -tpb/tmb;
 r[3].z = -fpn/fmn;
 r[3].w = 1.0;

 return r;
}

mat4 makeFrustum(num left, num right, num bottom, num top, num near, num far) #

Returns an OpenGL frustum camera projection matrix

mat4 makeFrustum(num left, num right, num bottom, num top, num near, num far) {
 left = left.toDouble();
 right = right.toDouble();
 bottom = bottom.toDouble();
 top = top.toDouble();
 near = near.toDouble();
 far = far.toDouble();
 double two_near = 2.0 * near;
 double right_minus_left = right - left;
 double top_minus_bottom = top - bottom;
 double far_minus_near = far - near;

 mat4 view = new mat4.zero();
 view[0].x = two_near / right_minus_left;

 view[1].y = two_near / top_minus_bottom;

 view[2].x = (right + left) / right_minus_left;
 view[2].y = (top + bottom) / top_minus_bottom;
 view[2].z = -(far + near) / far_minus_near;
 view[2].w = -1.0;

 view[3].z = -(two_near * far) / far_minus_near;
 view[3].w = 0.0;

 return view;
}

mat4 makePerspective(num fov_y_radians, num aspect_ratio, num znear, num zfar) #

Returns an OpenGL perspective camera projection matrix

mat4 makePerspective(num fov_y_radians, num aspect_ratio, num znear, num zfar) {
 double height = tan(fov_y_radians.toDouble() * 0.5) * znear.toDouble();
 double width = height.toDouble() * aspect_ratio.toDouble();
 return makeFrustum(-width, width, -height, height, znear, zfar);
}

mat4 makeLookAt(vec3 cameraPosition, vec3 cameraFocusPosition, vec3 upDirection) #

Returns an OpenGL look at matrix. The camera is located at cameraPosition and is focused on cameraFocusPostion.

The upDirection is almost always (0, 1, 0).

mat4 makeLookAt(vec3 cameraPosition, vec3 cameraFocusPosition,
               vec3 upDirection) {
 vec3 z = cameraPosition - cameraFocusPosition;
 z.normalize();

 vec3 x = upDirection.cross(z);
 x.normalize();

 vec3 y = z.cross(x);
 y.normalize();

 mat4 r = new mat4.zero();
 r[0].xyz = x;
 r[1].xyz = y;
 r[2].xyz = z;
 r[3].w = 1.0;
 r = r.transposed();
 vec3 rotatedEye = r * -cameraPosition;
 r[3].xyz = rotatedEye;

 return r;
}

double absoluteError(calculated, correct) #

Returns absolute error between calculated and correct. The type of calculated and correct must match and can be any vector, matrix, or quaternion.

double absoluteError(dynamic calculated, dynamic correct) {
 if (calculated is double && correct is double) {
   double diff = (calculated - correct).abs();
   return diff;
 }
 return calculated.absoluteError(correct);
}

double relativeError(calculated, correct) #

Returns relative error between calculated and correct. The type of calculated and correct must match and can be any vector, matrix, or quaternion.

double relativeError(dynamic calculated, dynamic correct) {
 if (calculated is double && correct is double) {
   double diff = (calculated - correct).abs();
   return diff/correct;
 }
 return calculated.relativeError(correct);
}

atan(arg, [arg2]) #

Returns atan( arg) or atan( arg/ arg2)

Arguments can be of type double, vec2, vec3 or vec4 Return type matches input argument type

dynamic atan(dynamic arg, [dynamic arg2]) {
 if (arg2 == null) {
   if (arg is num) {
     return Math.atan(arg.toDouble());
   }
   if (arg is vec2) {
     return new vec2(Math.atan(arg.x), Math.atan(arg.y));
   }
   if (arg is vec3) {
     return new vec3(Math.atan(arg.x), Math.atan(arg.y), Math.atan(arg.z));
   }
   if (arg is vec4) {
     return new vec4(Math.atan(arg.x), Math.atan(arg.y), Math.atan(arg.z), Math.atan(arg.w));
   }
 } else {
   if (arg is num) {
     return Math.atan2(arg.toDouble(), arg2);
   }
   if (arg is vec2) {
     return new vec2(Math.atan2(arg.x, arg2.x), Math.atan2(arg.y, arg2.y));
   }
   if (arg is vec3) {
     return new vec3(Math.atan2(arg.x, arg2.x), Math.atan2(arg.y, arg2.y), Math.atan2(arg.z, arg2.z));
   }
   if (arg is vec4) {
     return new vec4(Math.atan2(arg.x, arg2.x), Math.atan2(arg.y, arg2.y), Math.atan2(arg.z, arg2.z), Math.atan2(arg.w, arg2.w));
   }
 }
 throw new ArgumentError('');
}

smoothstep(edge0, edge1, x, [out = null]) #

Hermite intpolation between edge0 and edge1. edge0 < x < edge1.

dynamic smoothstep(dynamic edge0, dynamic edge1, dynamic x, [dynamic out=null]) {
 if (x is double) {
   return _ScalerHelpers.smoothstep(edge0, edge1, x);
 }
 if (x is vec2) {
   if (out == null) {
     out = new vec2.zero();
   }
   (out as vec2).x = _ScalerHelpers.smoothstep(edge0.x, edge1.x, x.x);
   (out as vec2).y = _ScalerHelpers.smoothstep(edge0.y, edge1.y, x.y);
   return out;
 }
 if (x is vec3) {
   if (out == null) {
     out = new vec3.zero();
   }
   (out as vec3).x = _ScalerHelpers.smoothstep(edge0.x, edge1.x, x.x);
   (out as vec3).y = _ScalerHelpers.smoothstep(edge0.y, edge1.y, x.y);
   (out as vec3).z = _ScalerHelpers.smoothstep(edge0.z, edge1.z, x.z);
   return out;
 }
 if (x is vec4) {
   if (out == null) {
     out = new vec4.zero();
   }
   (out as vec4).x = _ScalerHelpers.smoothstep(edge0.x, edge1.x, x.x);
   (out as vec4).y = _ScalerHelpers.smoothstep(edge0.y, edge1.y, x.y);
   (out as vec4).z = _ScalerHelpers.smoothstep(edge0.z, edge1.z, x.z);
   (out as vec4).w = _ScalerHelpers.smoothstep(edge0.w, edge1.w, x.w);
   return out;
 }
 throw new ArgumentError(x);
}

step(x, y, [out = null]) #

Returns 0.0 if x < y and 1.0 otherwise.

dynamic step(dynamic x, dynamic y, [dynamic out=null]) {
 if (x is double) {
   return _ScalerHelpers.step(x, y);
 }
 if (x is vec2) {
   if (out == null) {
     out = new vec2.zero();
   }
   (out as vec2).x = _ScalerHelpers.step(x.x, y.x);
   (out as vec2).y = _ScalerHelpers.step(x.y, y.y);
   return out;
 }
 if (x is vec3) {
   if (out == null) {
     out = new vec3.zero();
   }
   (out as vec3).x = _ScalerHelpers.step(x.x, y.x);
   (out as vec3).y = _ScalerHelpers.step(x.y, y.y);
   (out as vec3).z = _ScalerHelpers.step(x.z, y.z);
   return out;
 }
 if (x is vec4) {
   if (out == null) {
     out = new vec4.zero();
   }
   (out as vec4).x = _ScalerHelpers.step(x.x, y.x);
   (out as vec4).y = _ScalerHelpers.step(x.y, y.y);
   (out as vec4).z = _ScalerHelpers.step(x.z, y.z);
   (out as vec4).w = _ScalerHelpers.step(x.w, y.w);
   return out;
 }
 throw new ArgumentError(x);
}

mix(x, y, t) #

Linear interpolation between x and y with t. t must be between 0.0 and 1.0.

dynamic mix(dynamic x, dynamic y, dynamic t) {
 if (t is num) {
     if (x is num) {
       return _ScalerHelpers.mix(x, y, t);
     }
     if (x is vec2) {
       x = x as vec2;
       return new vec2(_ScalerHelpers.mix(x.x, y.x, t), _ScalerHelpers.mix(x.y, y.y, t));
     }
     if (x is vec3) {
       x = x as vec3;
       return new vec3(_ScalerHelpers.mix(x.x, y.x, t), _ScalerHelpers.mix(x.y, y.y, t), _ScalerHelpers.mix(x.z, y.z, t));
     }
     if (x is vec4) {
       x = x as vec4;
       return new vec4(_ScalerHelpers.mix(x.x, y.x, t), _ScalerHelpers.mix(x.y, y.y, t), _ScalerHelpers.mix(x.z, y.z, t), _ScalerHelpers.mix(x.w, y.w, t));
     }
     throw new ArgumentError(x);

 } else {
     if (x is num) {
       return _ScalerHelpers.mix(x, y, t);
     }
     if (x is vec2) {
       x = x as vec2;
       return new vec2(_ScalerHelpers.mix(x.x, y.x, t.x), _ScalerHelpers.mix(x.y, y.y, t.y));
     }
     if (x is vec3) {
       x = x as vec3;
       return new vec3(_ScalerHelpers.mix(x.x, y.x, t.x), _ScalerHelpers.mix(x.y, y.y, t.y), _ScalerHelpers.mix(x.z, y.z, t.z));
     }
     if (x is vec4) {
       x = x as vec4;
       return new vec4(_ScalerHelpers.mix(x.x, y.x, t.x), _ScalerHelpers.mix(x.y, y.y, t.y), _ScalerHelpers.mix(x.z, y.z, t.z), _ScalerHelpers.mix(x.w, y.w, t.w));
     }
     throw new ArgumentError(x);

 }
}

clamp(x, min_, max_, [out = null]) #

Component wise clamp of x between min_ and max_

dynamic clamp(dynamic x, dynamic min_, dynamic max_, [dynamic out=null]) {
 if (x is double) {
   return _ScalerHelpers.clamp(x, min_, max_);
 }
 if (x is vec2) {
   if (out == null) {
     out = new vec2.zero();
   }
   (out as vec2).x = _ScalerHelpers.clamp(x.x, min_.x, max_.x);
   (out as vec2).y = _ScalerHelpers.clamp(x.y, min_.y, max_.y);
   return out;
 }
 if (x is vec3) {
   if (out == null) {
     out = new vec3.zero();
   }
   (out as vec3).x = _ScalerHelpers.clamp(x.x, min_.x, max_.x);
   (out as vec3).y = _ScalerHelpers.clamp(x.y, min_.y, max_.y);
   (out as vec3).z = _ScalerHelpers.clamp(x.z, min_.z, max_.z);
   return out;
 }
 if (x is vec4) {
   if (out == null) {
     out = new vec4.zero();
   }
   (out as vec4).x = _ScalerHelpers.clamp(x.x, min_.x, max_.x);
   (out as vec4).y = _ScalerHelpers.clamp(x.y, min_.y, max_.y);
   (out as vec4).z = _ScalerHelpers.clamp(x.z, min_.z, max_.z);
   (out as vec4).w = _ScalerHelpers.clamp(x.w, min_.w, max_.w);
   return out;
 }
 throw new ArgumentError(x);
}

max(x, y, [out = null]) #

Returns component wise maximum of x and y

dynamic max(dynamic x, dynamic y, [dynamic out=null]) {
 if (x is double) {
   return Math.max(x, y);
 }
 if (x is vec2) {
   if (out == null) {
     out = new vec2.zero();
   }
   (out as vec2).x = Math.max(x.x, y.x);
   (out as vec2).y = Math.max(x.y, y.y);
   return out;
 }
 if (x is vec3) {
   if (out == null) {
     out = new vec3.zero();
   }
   (out as vec3).x = Math.max(x.x, y.x);
   (out as vec3).y = Math.max(x.y, y.y);
   (out as vec3).z = Math.max(x.z, y.z);
   return out;
 }
 if (x is vec4) {
   if (out == null) {
     out = new vec4.zero();
   }
   (out as vec4).x = Math.max(x.x, y.x);
   (out as vec4).y = Math.max(x.y, y.y);
   (out as vec4).z = Math.max(x.z, y.z);
   (out as vec4).w = Math.max(x.w, y.w);
   return out;
 }
 throw new ArgumentError(x);
}

min(x, y, [out = null]) #

Returns component wise minimum of x and y

dynamic min(dynamic x, dynamic y, [dynamic out=null]) {
 if (x is double) {
   return Math.min(x, y);
 }
 if (x is vec2) {
   if (out == null) {
     out = new vec2.zero();
   }
   (out as vec2).x = Math.min(x.x, y.x);
   (out as vec2).y = Math.min(x.y, y.y);
   return out;
 }
 if (x is vec3) {
   if (out == null) {
     out = new vec3.zero();
   }
   (out as vec3).x = Math.min(x.x, y.x);
   (out as vec3).y = Math.min(x.y, y.y);
   (out as vec3).z = Math.min(x.z, y.z);
   return out;
 }
 if (x is vec4) {
   if (out == null) {
     out = new vec4.zero();
   }
   (out as vec4).x = Math.min(x.x, y.x);
   (out as vec4).y = Math.min(x.y, y.y);
   (out as vec4).z = Math.min(x.z, y.z);
   (out as vec4).w = Math.min(x.w, y.w);
   return out;
 }
 throw new ArgumentError(x);
}

mod(x, y, [out = null]) #

Returns x mod y

dynamic mod(dynamic x, dynamic y, [dynamic out=null]) {
 if (x is double) {
   return _ScalerHelpers.mod(x, y);
 }
 if (x is vec2) {
   if (out == null) {
     out = new vec2.zero();
   }
   (out as vec2).x = _ScalerHelpers.mod(x.x, y.x);
   (out as vec2).y = _ScalerHelpers.mod(x.y, y.y);
   return out;
 }
 if (x is vec3) {
   if (out == null) {
     out = new vec3.zero();
   }
   (out as vec3).x = _ScalerHelpers.mod(x.x, y.x);
   (out as vec3).y = _ScalerHelpers.mod(x.y, y.y);
   (out as vec3).z = _ScalerHelpers.mod(x.z, y.z);
   return out;
 }
 if (x is vec4) {
   if (out == null) {
     out = new vec4.zero();
   }
   (out as vec4).x = _ScalerHelpers.mod(x.x, y.x);
   (out as vec4).y = _ScalerHelpers.mod(x.y, y.y);
   (out as vec4).z = _ScalerHelpers.mod(x.z, y.z);
   (out as vec4).w = _ScalerHelpers.mod(x.w, y.w);
   return out;
 }
 throw new ArgumentError(x);
}

fract(arg, [out = null]) #

Returns fraction of arg

dynamic fract(dynamic arg, [dynamic out=null]) {
 if (arg is double) {
   return _ScalerHelpers.fract(arg);
 }
 if (arg is vec2) {
   if (out == null) {
     out = new vec2.zero();
   }
   (out as vec2).x = _ScalerHelpers.fract(arg.x);
   (out as vec2).y = _ScalerHelpers.fract(arg.y);
   return out;
 }
 if (arg is vec3) {
   if (out == null) {
     out = new vec3.zero();
   }
   (out as vec3).x = _ScalerHelpers.fract(arg.x);
   (out as vec3).y = _ScalerHelpers.fract(arg.y);
   (out as vec3).z = _ScalerHelpers.fract(arg.z);
   return out;
 }
 if (arg is vec4) {
   if (out == null) {
     out = new vec4.zero();
   }
   (out as vec4).x = _ScalerHelpers.fract(arg.x);
   (out as vec4).y = _ScalerHelpers.fract(arg.y);
   (out as vec4).z = _ScalerHelpers.fract(arg.z);
   (out as vec4).w = _ScalerHelpers.fract(arg.w);
   return out;
 }
 throw new ArgumentError(arg);
}

ceil(arg, [out = null]) #

Returns ceiling of arg

dynamic ceil(dynamic arg, [dynamic out=null]) {
 if (arg is double) {
   return _ScalerHelpers.ceil(arg);
 }
 if (arg is vec2) {
   if (out == null) {
     out = new vec2.zero();
   }
   (out as vec2).x = _ScalerHelpers.ceil(arg.x);
   (out as vec2).y = _ScalerHelpers.ceil(arg.y);
   return out;
 }
 if (arg is vec3) {
   if (out == null) {
     out = new vec3.zero();
   }
   (out as vec3).x = _ScalerHelpers.ceil(arg.x);
   (out as vec3).y = _ScalerHelpers.ceil(arg.y);
   (out as vec3).z = _ScalerHelpers.ceil(arg.z);
   return out;
 }
 if (arg is vec4) {
   if (out == null) {
     out = new vec4.zero();
   }
   (out as vec4).x = _ScalerHelpers.ceil(arg.x);
   (out as vec4).y = _ScalerHelpers.ceil(arg.y);
   (out as vec4).z = _ScalerHelpers.ceil(arg.z);
   (out as vec4).w = _ScalerHelpers.ceil(arg.w);
   return out;
 }
 throw new ArgumentError(arg);
}

roundEven(arg, [out = null]) #

Returns arg rounded to nearest even integer.

dynamic roundEven(dynamic arg, [dynamic out=null]) {
 if (arg is double) {
   return _ScalerHelpers.roundEven(arg);
 }
 if (arg is vec2) {
   if (out == null) {
     out = new vec2.zero();
   }
   (out as vec2).x = _ScalerHelpers.roundEven(arg.x);
   (out as vec2).y = _ScalerHelpers.roundEven(arg.y);
   return out;
 }
 if (arg is vec3) {
   if (out == null) {
     out = new vec3.zero();
   }
   (out as vec3).x = _ScalerHelpers.roundEven(arg.x);
   (out as vec3).y = _ScalerHelpers.roundEven(arg.y);
   (out as vec3).z = _ScalerHelpers.roundEven(arg.z);
   return out;
 }
 if (arg is vec4) {
   if (out == null) {
     out = new vec4.zero();
   }
   (out as vec4).x = _ScalerHelpers.roundEven(arg.x);
   (out as vec4).y = _ScalerHelpers.roundEven(arg.y);
   (out as vec4).z = _ScalerHelpers.roundEven(arg.z);
   (out as vec4).w = _ScalerHelpers.roundEven(arg.w);
   return out;
 }
 throw new ArgumentError(arg);
}

round(arg, [out = null]) #

Returns arg rounded to nearest integer.

dynamic round(dynamic arg, [dynamic out=null]) {
 if (arg is double) {
   return _ScalerHelpers.round(arg);
 }
 if (arg is vec2) {
   if (out == null) {
     out = new vec2.zero();
   }
   (out as vec2).x = _ScalerHelpers.round(arg.x);
   (out as vec2).y = _ScalerHelpers.round(arg.y);
   return out;
 }
 if (arg is vec3) {
   if (out == null) {
     out = new vec3.zero();
   }
   (out as vec3).x = _ScalerHelpers.round(arg.x);
   (out as vec3).y = _ScalerHelpers.round(arg.y);
   (out as vec3).z = _ScalerHelpers.round(arg.z);
   return out;
 }
 if (arg is vec4) {
   if (out == null) {
     out = new vec4.zero();
   }
   (out as vec4).x = _ScalerHelpers.round(arg.x);
   (out as vec4).y = _ScalerHelpers.round(arg.y);
   (out as vec4).z = _ScalerHelpers.round(arg.z);
   (out as vec4).w = _ScalerHelpers.round(arg.w);
   return out;
 }
 throw new ArgumentError(arg);
}

trunc(arg, [out = null]) #

Returns arg truncated.

dynamic trunc(dynamic arg, [dynamic out=null]) {
 if (arg is double) {
   return _ScalerHelpers.truncate(arg);
 }
 if (arg is vec2) {
   if (out == null) {
     out = new vec2.zero();
   }
   (out as vec2).x = _ScalerHelpers.truncate(arg.x);
   (out as vec2).y = _ScalerHelpers.truncate(arg.y);
   return out;
 }
 if (arg is vec3) {
   if (out == null) {
     out = new vec3.zero();
   }
   (out as vec3).x = _ScalerHelpers.truncate(arg.x);
   (out as vec3).y = _ScalerHelpers.truncate(arg.y);
   (out as vec3).z = _ScalerHelpers.truncate(arg.z);
   return out;
 }
 if (arg is vec4) {
   if (out == null) {
     out = new vec4.zero();
   }
   (out as vec4).x = _ScalerHelpers.truncate(arg.x);
   (out as vec4).y = _ScalerHelpers.truncate(arg.y);
   (out as vec4).z = _ScalerHelpers.truncate(arg.z);
   (out as vec4).w = _ScalerHelpers.truncate(arg.w);
   return out;
 }
 throw new ArgumentError(arg);
}

floor(arg, [out = null]) #

Returns floor value of arg.

dynamic floor(dynamic arg, [dynamic out=null]) {
 if (arg is double) {
   return _ScalerHelpers.floor(arg);
 }
 if (arg is vec2) {
   if (out == null) {
     out = new vec2.zero();
   }
   (out as vec2).x = _ScalerHelpers.floor(arg.x);
   (out as vec2).y = _ScalerHelpers.floor(arg.y);
   return out;
 }
 if (arg is vec3) {
   if (out == null) {
     out = new vec3.zero();
   }
   (out as vec3).x = _ScalerHelpers.floor(arg.x);
   (out as vec3).y = _ScalerHelpers.floor(arg.y);
   (out as vec3).z = _ScalerHelpers.floor(arg.z);
   return out;
 }
 if (arg is vec4) {
   if (out == null) {
     out = new vec4.zero();
   }
   (out as vec4).x = _ScalerHelpers.floor(arg.x);
   (out as vec4).y = _ScalerHelpers.floor(arg.y);
   (out as vec4).z = _ScalerHelpers.floor(arg.z);
   (out as vec4).w = _ScalerHelpers.floor(arg.w);
   return out;
 }
 throw new ArgumentError(arg);
}

sign(arg, [out = null]) #

Returns 1.0 or 0.0 or -1.0 depending on sign of arg.

dynamic sign(dynamic arg, [dynamic out=null]) {
 if (arg is double) {
   return _ScalerHelpers.sign(arg);
 }
 if (arg is vec2) {
   if (out == null) {
     out = new vec2.zero();
   }
   (out as vec2).x = _ScalerHelpers.sign(arg.x);
   (out as vec2).y = _ScalerHelpers.sign(arg.y);
   return out;
 }
 if (arg is vec3) {
   if (out == null) {
     out = new vec3.zero();
   }
   (out as vec3).x = _ScalerHelpers.sign(arg.x);
   (out as vec3).y = _ScalerHelpers.sign(arg.y);
   (out as vec3).z = _ScalerHelpers.sign(arg.z);
   return out;
 }
 if (arg is vec4) {
   if (out == null) {
     out = new vec4.zero();
   }
   (out as vec4).x = _ScalerHelpers.sign(arg.x);
   (out as vec4).y = _ScalerHelpers.sign(arg.y);
   (out as vec4).z = _ScalerHelpers.sign(arg.z);
   (out as vec4).w = _ScalerHelpers.sign(arg.w);
   return out;
 }
 throw new ArgumentError(arg);
}

abs(arg, [out = null]) #

Returns absolute value of arg.

dynamic abs(dynamic arg, [dynamic out=null]) {
 if (arg is double) {
   return _ScalerHelpers.abs(arg);
 }
 if (arg is vec2) {
   if (out == null) {
     out = new vec2.zero();
   }
   (out as vec2).x = _ScalerHelpers.abs(arg.x);
   (out as vec2).y = _ScalerHelpers.abs(arg.y);
   return out;
 }
 if (arg is vec3) {
   if (out == null) {
     out = new vec3.zero();
   }
   (out as vec3).x = _ScalerHelpers.abs(arg.x);
   (out as vec3).y = _ScalerHelpers.abs(arg.y);
   (out as vec3).z = _ScalerHelpers.abs(arg.z);
   return out;
 }
 if (arg is vec4) {
   if (out == null) {
     out = new vec4.zero();
   }
   (out as vec4).x = _ScalerHelpers.abs(arg.x);
   (out as vec4).y = _ScalerHelpers.abs(arg.y);
   (out as vec4).z = _ScalerHelpers.abs(arg.z);
   (out as vec4).w = _ScalerHelpers.abs(arg.w);
   return out;
 }
 throw new ArgumentError(arg);
}

inversesqrt(arg, [out = null]) #

Returns the inverse square root of arg. Supports vectors and numbers.

dynamic inversesqrt(dynamic arg, [dynamic out=null]) {
 if (arg is double) {
   return _ScalerHelpers.inversesqrt(arg);
 }
 if (arg is vec2) {
   if (out == null) {
     out = new vec2.zero();
   }
   (out as vec2).x = _ScalerHelpers.inversesqrt(arg.x);
   (out as vec2).y = _ScalerHelpers.inversesqrt(arg.y);
   return out;
 }
 if (arg is vec3) {
   if (out == null) {
     out = new vec3.zero();
   }
   (out as vec3).x = _ScalerHelpers.inversesqrt(arg.x);
   (out as vec3).y = _ScalerHelpers.inversesqrt(arg.y);
   (out as vec3).z = _ScalerHelpers.inversesqrt(arg.z);
   return out;
 }
 if (arg is vec4) {
   if (out == null) {
     out = new vec4.zero();
   }
   (out as vec4).x = _ScalerHelpers.inversesqrt(arg.x);
   (out as vec4).y = _ScalerHelpers.inversesqrt(arg.y);
   (out as vec4).z = _ScalerHelpers.inversesqrt(arg.z);
   (out as vec4).w = _ScalerHelpers.inversesqrt(arg.w);
   return out;
 }
 throw new ArgumentError(arg);
}

sqrt(arg, [out = null]) #

Returns the square root of arg.

dynamic sqrt(dynamic arg, [dynamic out=null]) {
 if (arg is double) {
   return Math.sqrt(arg);
 }
 if (arg is vec2) {
   if (out == null) {
     out = new vec2.zero();
   }
   (out as vec2).x = Math.sqrt(arg.x);
   (out as vec2).y = Math.sqrt(arg.y);
   return out;
 }
 if (arg is vec3) {
   if (out == null) {
     out = new vec3.zero();
   }
   (out as vec3).x = Math.sqrt(arg.x);
   (out as vec3).y = Math.sqrt(arg.y);
   (out as vec3).z = Math.sqrt(arg.z);
   return out;
 }
 if (arg is vec4) {
   if (out == null) {
     out = new vec4.zero();
   }
   (out as vec4).x = Math.sqrt(arg.x);
   (out as vec4).y = Math.sqrt(arg.y);
   (out as vec4).z = Math.sqrt(arg.z);
   (out as vec4).w = Math.sqrt(arg.w);
   return out;
 }
 throw new ArgumentError(arg);
}

log2(arg, [out = null]) #

Returns the logarithm of arg base 2. Supports vectors and numbers.

dynamic log2(dynamic arg, [dynamic out=null]) {
 if (arg is double) {
   return _ScalerHelpers.log2(arg);
 }
 if (arg is vec2) {
   if (out == null) {
     out = new vec2.zero();
   }
   (out as vec2).x = _ScalerHelpers.log2(arg.x);
   (out as vec2).y = _ScalerHelpers.log2(arg.y);
   return out;
 }
 if (arg is vec3) {
   if (out == null) {
     out = new vec3.zero();
   }
   (out as vec3).x = _ScalerHelpers.log2(arg.x);
   (out as vec3).y = _ScalerHelpers.log2(arg.y);
   (out as vec3).z = _ScalerHelpers.log2(arg.z);
   return out;
 }
 if (arg is vec4) {
   if (out == null) {
     out = new vec4.zero();
   }
   (out as vec4).x = _ScalerHelpers.log2(arg.x);
   (out as vec4).y = _ScalerHelpers.log2(arg.y);
   (out as vec4).z = _ScalerHelpers.log2(arg.z);
   (out as vec4).w = _ScalerHelpers.log2(arg.w);
   return out;
 }
 throw new ArgumentError(arg);
}

exp2(arg, [out = null]) #

Returns 2 raised to the exponent arg. Supports vectors and numbers.

dynamic exp2(dynamic arg, [dynamic out=null]) {
 if (arg is double) {
   return _ScalerHelpers.exp2(arg);
 }
 if (arg is vec2) {
   if (out == null) {
     out = new vec2.zero();
   }
   (out as vec2).x = _ScalerHelpers.exp2(arg.x);
   (out as vec2).y = _ScalerHelpers.exp2(arg.y);
   return out;
 }
 if (arg is vec3) {
   if (out == null) {
     out = new vec3.zero();
   }
   (out as vec3).x = _ScalerHelpers.exp2(arg.x);
   (out as vec3).y = _ScalerHelpers.exp2(arg.y);
   (out as vec3).z = _ScalerHelpers.exp2(arg.z);
   return out;
 }
 if (arg is vec4) {
   if (out == null) {
     out = new vec4.zero();
   }
   (out as vec4).x = _ScalerHelpers.exp2(arg.x);
   (out as vec4).y = _ScalerHelpers.exp2(arg.y);
   (out as vec4).z = _ScalerHelpers.exp2(arg.z);
   (out as vec4).w = _ScalerHelpers.exp2(arg.w);
   return out;
 }
 throw new ArgumentError(arg);
}

log(arg, [out = null]) #

Returns the logarithm of arg base e. Supports vectors and numbers.

dynamic log(dynamic arg, [dynamic out=null]) {
 if (arg is double) {
   return Math.log(arg);
 }
 if (arg is vec2) {
   if (out == null) {
     out = new vec2.zero();
   }
   (out as vec2).x = Math.log(arg.x);
   (out as vec2).y = Math.log(arg.y);
   return out;
 }
 if (arg is vec3) {
   if (out == null) {
     out = new vec3.zero();
   }
   (out as vec3).x = Math.log(arg.x);
   (out as vec3).y = Math.log(arg.y);
   (out as vec3).z = Math.log(arg.z);
   return out;
 }
 if (arg is vec4) {
   if (out == null) {
     out = new vec4.zero();
   }
   (out as vec4).x = Math.log(arg.x);
   (out as vec4).y = Math.log(arg.y);
   (out as vec4).z = Math.log(arg.z);
   (out as vec4).w = Math.log(arg.w);
   return out;
 }
 throw new ArgumentError(arg);
}

exp(arg, [out = null]) #

Returns e raised to the exponent arg. Supports vectors and numbers.

dynamic exp(dynamic arg, [dynamic out=null]) {
 if (arg is double) {
   return Math.exp(arg);
 }
 if (arg is vec2) {
   if (out == null) {
     out = new vec2.zero();
   }
   (out as vec2).x = Math.exp(arg.x);
   (out as vec2).y = Math.exp(arg.y);
   return out;
 }
 if (arg is vec3) {
   if (out == null) {
     out = new vec3.zero();
   }
   (out as vec3).x = Math.exp(arg.x);
   (out as vec3).y = Math.exp(arg.y);
   (out as vec3).z = Math.exp(arg.z);
   return out;
 }
 if (arg is vec4) {
   if (out == null) {
     out = new vec4.zero();
   }
   (out as vec4).x = Math.exp(arg.x);
   (out as vec4).y = Math.exp(arg.y);
   (out as vec4).z = Math.exp(arg.z);
   (out as vec4).w = Math.exp(arg.w);
   return out;
 }
 throw new ArgumentError(arg);
}

pow(x, y, [out = null]) #

Returns x raised to the exponent y. Supports vectors and numbers.

dynamic pow(dynamic x, dynamic y, [dynamic out=null]) {
 if (x is double) {
   return Math.pow(x, y);
 }
 if (x is vec2) {
   if (out == null) {
     out = new vec2.zero();
   }
   (out as vec2).x = Math.pow(x.x, y.x);
   (out as vec2).y = Math.pow(x.y, y.y);
   return out;
 }
 if (x is vec3) {
   if (out == null) {
     out = new vec3.zero();
   }
   (out as vec3).x = Math.pow(x.x, y.x);
   (out as vec3).y = Math.pow(x.y, y.y);
   (out as vec3).z = Math.pow(x.z, y.z);
   return out;
 }
 if (x is vec4) {
   if (out == null) {
     out = new vec4.zero();
   }
   (out as vec4).x = Math.pow(x.x, y.x);
   (out as vec4).y = Math.pow(x.y, y.y);
   (out as vec4).z = Math.pow(x.z, y.z);
   (out as vec4).w = Math.pow(x.w, y.w);
   return out;
 }
 throw new ArgumentError(x);
}

degrees(arg, [out = null]) #

Returns arg converted from radians to degrees. Return types matches the type of arg

dynamic degrees(dynamic arg, [dynamic out=null]) {
 if (arg is double) {
   return _ScalerHelpers.degrees(arg);
 }
 if (arg is vec2) {
   if (out == null) {
     out = new vec2.zero();
   }
   (out as vec2).x = _ScalerHelpers.degrees(arg.x);
   (out as vec2).y = _ScalerHelpers.degrees(arg.y);
   return out;
 }
 if (arg is vec3) {
   if (out == null) {
     out = new vec3.zero();
   }
   (out as vec3).x = _ScalerHelpers.degrees(arg.x);
   (out as vec3).y = _ScalerHelpers.degrees(arg.y);
   (out as vec3).z = _ScalerHelpers.degrees(arg.z);
   return out;
 }
 if (arg is vec4) {
   if (out == null) {
     out = new vec4.zero();
   }
   (out as vec4).x = _ScalerHelpers.degrees(arg.x);
   (out as vec4).y = _ScalerHelpers.degrees(arg.y);
   (out as vec4).z = _ScalerHelpers.degrees(arg.z);
   (out as vec4).w = _ScalerHelpers.degrees(arg.w);
   return out;
 }
 throw new ArgumentError(arg);
}

radians(arg, [out = null]) #

Returns arg converted from degrees to radians. Return types matches the type of arg

dynamic radians(dynamic arg, [dynamic out=null]) {
 if (arg is double) {
   return _ScalerHelpers.radians(arg);
 }
 if (arg is vec2) {
   if (out == null) {
     out = new vec2.zero();
   }
   (out as vec2).x = _ScalerHelpers.radians(arg.x);
   (out as vec2).y = _ScalerHelpers.radians(arg.y);
   return out;
 }
 if (arg is vec3) {
   if (out == null) {
     out = new vec3.zero();
   }
   (out as vec3).x = _ScalerHelpers.radians(arg.x);
   (out as vec3).y = _ScalerHelpers.radians(arg.y);
   (out as vec3).z = _ScalerHelpers.radians(arg.z);
   return out;
 }
 if (arg is vec4) {
   if (out == null) {
     out = new vec4.zero();
   }
   (out as vec4).x = _ScalerHelpers.radians(arg.x);
   (out as vec4).y = _ScalerHelpers.radians(arg.y);
   (out as vec4).z = _ScalerHelpers.radians(arg.z);
   (out as vec4).w = _ScalerHelpers.radians(arg.w);
   return out;
 }
 throw new ArgumentError(arg);
}

acos(arg, [out = null]) #

Returns arc cosine of arg. Return type matches the type of arg

dynamic acos(dynamic arg, [dynamic out=null]) {
 if (arg is double) {
   return Math.acos(arg);
 }
 if (arg is vec2) {
   if (out == null) {
     out = new vec2.zero();
   }
   (out as vec2).x = Math.acos(arg.x);
   (out as vec2).y = Math.acos(arg.y);
   return out;
 }
 if (arg is vec3) {
   if (out == null) {
     out = new vec3.zero();
   }
   (out as vec3).x = Math.acos(arg.x);
   (out as vec3).y = Math.acos(arg.y);
   (out as vec3).z = Math.acos(arg.z);
   return out;
 }
 if (arg is vec4) {
   if (out == null) {
     out = new vec4.zero();
   }
   (out as vec4).x = Math.acos(arg.x);
   (out as vec4).y = Math.acos(arg.y);
   (out as vec4).z = Math.acos(arg.z);
   (out as vec4).w = Math.acos(arg.w);
   return out;
 }
 throw new ArgumentError(arg);
}

asin(arg, [out = null]) #

Returns arc sine of arg. Return type matches the type of arg

dynamic asin(dynamic arg, [dynamic out=null]) {
 if (arg is double) {
   return Math.asin(arg);
 }
 if (arg is vec2) {
   if (out == null) {
     out = new vec2.zero();
   }
   (out as vec2).x = Math.asin(arg.x);
   (out as vec2).y = Math.asin(arg.y);
   return out;
 }
 if (arg is vec3) {
   if (out == null) {
     out = new vec3.zero();
   }
   (out as vec3).x = Math.asin(arg.x);
   (out as vec3).y = Math.asin(arg.y);
   (out as vec3).z = Math.asin(arg.z);
   return out;
 }
 if (arg is vec4) {
   if (out == null) {
     out = new vec4.zero();
   }
   (out as vec4).x = Math.asin(arg.x);
   (out as vec4).y = Math.asin(arg.y);
   (out as vec4).z = Math.asin(arg.z);
   (out as vec4).w = Math.asin(arg.w);
   return out;
 }
 throw new ArgumentError(arg);
}

tan(arg, [out = null]) #

Returns tangent of arg. Return type matches the type of arg

dynamic tan(dynamic arg, [dynamic out=null]) {
 if (arg is double) {
   return Math.tan(arg);
 }
 if (arg is vec2) {
   if (out == null) {
     out = new vec2.zero();
   }
   (out as vec2).x = Math.tan(arg.x);
   (out as vec2).y = Math.tan(arg.y);
   return out;
 }
 if (arg is vec3) {
   if (out == null) {
     out = new vec3.zero();
   }
   (out as vec3).x = Math.tan(arg.x);
   (out as vec3).y = Math.tan(arg.y);
   (out as vec3).z = Math.tan(arg.z);
   return out;
 }
 if (arg is vec4) {
   if (out == null) {
     out = new vec4.zero();
   }
   (out as vec4).x = Math.tan(arg.x);
   (out as vec4).y = Math.tan(arg.y);
   (out as vec4).z = Math.tan(arg.z);
   (out as vec4).w = Math.tan(arg.w);
   return out;
 }
 throw new ArgumentError(arg);
}

cos(arg, [out = null]) #

Returns cosine of arg. Return type matches the type of arg

dynamic cos(dynamic arg, [dynamic out=null]) {
 if (arg is double) {
   return Math.cos(arg);
 }
 if (arg is vec2) {
   if (out == null) {
     out = new vec2.zero();
   }
   (out as vec2).x = Math.cos(arg.x);
   (out as vec2).y = Math.cos(arg.y);
   return out;
 }
 if (arg is vec3) {
   if (out == null) {
     out = new vec3.zero();
   }
   (out as vec3).x = Math.cos(arg.x);
   (out as vec3).y = Math.cos(arg.y);
   (out as vec3).z = Math.cos(arg.z);
   return out;
 }
 if (arg is vec4) {
   if (out == null) {
     out = new vec4.zero();
   }
   (out as vec4).x = Math.cos(arg.x);
   (out as vec4).y = Math.cos(arg.y);
   (out as vec4).z = Math.cos(arg.z);
   (out as vec4).w = Math.cos(arg.w);
   return out;
 }
 throw new ArgumentError(arg);
}

sin(arg, [out = null]) #

Returns sine of arg. Return type matches the type of arg

dynamic sin(dynamic arg, [dynamic out=null]) {
 if (arg is double) {
   return Math.sin(arg);
 }
 if (arg is vec2) {
   if (out == null) {
     out = new vec2.zero();
   }
   (out as vec2).x = Math.sin(arg.x);
   (out as vec2).y = Math.sin(arg.y);
   return out;
 }
 if (arg is vec3) {
   if (out == null) {
     out = new vec3.zero();
   }
   (out as vec3).x = Math.sin(arg.x);
   (out as vec3).y = Math.sin(arg.y);
   (out as vec3).z = Math.sin(arg.z);
   return out;
 }
 if (arg is vec4) {
   if (out == null) {
     out = new vec4.zero();
   }
   (out as vec4).x = Math.sin(arg.x);
   (out as vec4).y = Math.sin(arg.y);
   (out as vec4).z = Math.sin(arg.z);
   (out as vec4).w = Math.sin(arg.w);
   return out;
 }
 throw new ArgumentError(arg);
}

void buildPlaneVectors(vec3 planeNormal, vec3 u, vec3 v) #

Sets u and v to be two vectors orthogonal to each other and planeNormal

void buildPlaneVectors(final vec3 planeNormal, vec3 u, vec3 v) {
 if (planeNormal.z.abs() > _ScalerHelpers._sqrtOneHalf) {
   // choose u in y-z plane
   double a = planeNormal.y*planeNormal.y + planeNormal.z*planeNormal.z;
   double k = 1.0/Math.sqrt(a);
   u.x = 0.0;
   u.y = -planeNormal.z*k;
   u.z = planeNormal.y*k;
   v.x = a*k;
   v.y = -planeNormal[0]*(planeNormal[1]*k);
   v.z = planeNormal[0]*(-planeNormal[2]*k);
 } else {
   // choose u in x-y plane
   double a = planeNormal.x*planeNormal.x + planeNormal.y*planeNormal.y;
   double k = 1.0/Math.sqrt(a);
   u.x = -planeNormal[1]*k;
   u.y = planeNormal[0]*k;
   u.z = 0.0;
   v.x = -planeNormal[2]*(planeNormal[0]*k);
   v.y = planeNormal[2]*(-planeNormal[1]*k);
   v.z = a*k;
 }
}

normalize(x, [out = null]) #

Returns x normalized. Supports num, vec2, vec3, and vec4 input types. The return type will match the type of x

dynamic normalize(dynamic x, [dynamic out=null]) {
 if (x is num) {
   return 1.0 * sign(x.toDouble());
 }
 if (x is vec2) {
   if (out == null) {
     out = new vec2.copy(x);
   }
   (x as vec2).normalize();
   return out;
 }
 if (x is vec3) {
   if (out == null) {
     out = new vec3.copy(x);
   }
   (x as vec3).normalize();
   return out;
 }
 if (x is vec4) {
   if (out == null) {
     out = new vec4.copy(x);
   }
   (x as vec4).normalize();
   return out;
 }
 return null;
}

cross(x, y, [out = null]) #

Returns the cross product between x and y. x and y can be vec2, vec3 or double, but not all combinations are supported.

dynamic cross(dynamic x, dynamic y, [dynamic out=null]) {
 if (x is vec3 && y is vec3) {
   return x.cross(y, out);
 } else if (x is vec2 && y is vec2) {
   return x.cross(y);
 } else if (x is num && y is vec2) {
   x = x.toDouble();
   if (out == null) {
     out = new vec2.zero();
   }
   out.x = -x * y.y;
   out.y = x * y.x;
   return out;
 } else if (x is vec2 && y is num) {
   y = y.toDouble();
   if (out == null) {
     out = new vec2.zero();
   }
   out.x = y * x.y;
   out.y = -y * x.x;
   return out;
 } else {
   assert(false);
 }
 return null;
}

double distance2(x, y) #

Returns the distance squared between vectors x and y.

double distance2(dynamic x, dynamic y) {
 return length2(x - y);
}

double distance(x, y) #

Returns the distance between vectors x and y. The dimension of x and y must match.

double distance(dynamic x, dynamic y) {
 return length(x - y);
}

double length2(x) #

Returns the length squared of vector x

double length2(dynamic x) {
 return x.length2;
}

double length(x) #

Returns the length of vector x

double length(dynamic x) {
 return x.length;
}

double dot(x, y) #

Returns the dot product between vectors x and y. The dimension of x and y must match.

double dot(dynamic x, dynamic y) {
 return x.dot(y);
}

Classes