import { IClone } from "./IClone" ; import { MathUtil } from "./MathUtil" ; import { Matrix } from "./Matrix" ; import { Quaternion } from "./Quaternion" ; /**
 * Describes a 4D-vector.
 */ export class Vector4 implements IClone { internal static readonly _zero = new Vector4 ( 0.0 , 0.0 , 0.0 , 0.0 ) ; internal static readonly _one = new Vector4 ( 1.0 , 1.0 , 1.0 , 1.0 ) ; param left param right param out static add ( left , right , out ) { out . x = left . x + right . x ; out . y = left . y + right . y ; out . z = left . z + right . z ; out . w = left . w + right . w ; } param left param right param out static subtract ( left , right , out ) { out . x = left . x - right . x ; out . y = left . y - right . y ; out . z = left . z - right . z ; out . w = left . w - right . w ; } param left param right param out static multiply ( left , right , out ) { out . x = left . x * right . x ; out . y = left . y * right . y ; out . z = left . z * right . z ; out . w = left . w * right . w ; } param left param right param out static divide ( left , right , out ) { out . x = left . x / right . x ; out . y = left . y / right . y ; out . z = left . z / right . z ; out . w = left . w / right . w ; } param left param right returns static dot ( left , right ) { return left . x * right . x + left . y * right . y + left . z * right . z + left . w * right . w ; } param a param b returns static distance ( a , b ) { const x = b . x - a . x ; const y = b . y - a . y ; const z = b . z - a . z ; const w = b . w - a . w ; return Math . sqrt ( x * x + y * y + z * z + w * w ) ; } param a param b returns static distanceSquared ( a , b ) { const x = b . x - a . x ; const y = b . y - a . y ; const z = b . z - a . z ; const w = b . w - a . w ; return x * x + y * y + z * z + w * w ; } param left param right returns static equals ( left , right ) { return ( MathUtil . equals ( left . x , right . x ) && MathUtil . equals ( left . y , right . y ) && MathUtil . equals ( left . z , right . z ) && MathUtil . equals ( left . w , right . w ) ) ; } param start param end param t param out static lerp ( start , end , t , out ) { const { x , y , z , w } = start ; out . x = x + ( end . x - x ) * t ; out . y = y + ( end . y - y ) * t ; out . z = z + ( end . z - z ) * t ; out . w = w + ( end . w - w ) * t ; } param left param right param out static max ( left , right , out ) { out . x = Math . max ( left . x , right . x ) ; out . y = Math . max ( left . y , right . y ) ; out . z = Math . max ( left . z , right . z ) ; out . w = Math . max ( left . w , right . w ) ; } param left param right param out static min ( left , right , out ) { out . x = Math . min ( left . x , right . x ) ; out . y = Math . min ( left . y , right . y ) ; out . z = Math . min ( left . z , right . z ) ; out . w = Math . min ( left . w , right . w ) ; } param a param out static negate ( a , out ) { out . x = - a . x ; out . y = - a . y ; out . z = - a . z ; out . w = - a . w ; } param a param out static normalize ( a , out ) { const { x , y , z , w } = a ; let len = Math . sqrt ( x * x + y * y + z * z + w * w ) ; if ( len > MathUtil . zeroTolerance ) { len = 1 / len ; out . x = x * len ; out . y = y * len ; out . z = z * len ; out . w = w * len ; } } param a param s param out static scale ( a , s , out ) { out . x = a . x * s ; out . y = a . y * s ; out . z = a . z * s ; out . w = a . w * s ; } param v param m param out static transform ( v , m , out ) { const { x , y , z , w } = v ; const e = m . elements ; out . x = x * e [ 0 ] + y * e [ 4 ] + z * e [ 8 ] + w * e [ 12 ] ; out . y = x * e [ 1 ] + y * e [ 5 ] + z * e [ 9 ] + w * e [ 13 ] ; out . z = x * e [ 2 ] + y * e [ 6 ] + z * e [ 10 ] + w * e [ 14 ] ; out . w = x * e [ 3 ] + y * e [ 7 ] + z * e [ 11 ] + w * e [ 15 ] ; } param v param q param out static transformByQuat ( v , q , out ) { const { x , y , z , w } = v ; const qx = q . x ; const qy = q . y ; const qz = q . z ; const qw = q . w ; const ix = qw * x + qy * z - qz * y ; const iy = qw * y + qz * x - qx * z ; const iz = qw * z + qx * y - qy * x ; const iw = - qx * x - qy * y - qz * z ; out . x = ix * qw - iw * qx - iy * qz + iz * qy ; out . y = iy * qw - iw * qy - iz * qx + ix * qz ; out . z = iz * qw - iw * qz - ix * qy + iy * qx ; out . w = w ; } /** The x component of the vector. */ x : number ; /** The y component of the vector. */ y : number ; /** The z component of the vector. */ z : number ; /** The w component of the vector. */ w : number ; param x param y param z param w constructor ( x = 0 , y = 0 , z = 0 , w = 0 ) { this . x = x ; this . y = y ; this . z = z ; this . w = w ; } param x param y param z param w returns setValue ( x , y , z , w ) { this . x = x ; this . y = y ; this . z = z ; this . w = w ; return this ; } param array param offset returns setValueByArray ( array , offset = 0 ) { this . x = array [ offset ] ; this . y = array [ offset + 1 ] ; this . z = array [ offset + 2 ] ; this . w = array [ offset + 3 ] ; return this ; } param right returns add ( right ) { this . x += right . x ; this . y += right . y ; this . z += right . z ; this . w += right . w ; return this ; } param right returns subtract ( right ) { this . x -= right . x ; this . y -= right . y ; this . z -= right . z ; this . w -= right . w ; return this ; } param right returns multiply ( right ) { this . x *= right . x ; this . y *= right . y ; this . z *= right . z ; this . w *= right . w ; return this ; } param right returns divide ( right ) { this . x /= right . x ; this . y /= right . y ; this . z /= right . z ; this . w /= right . w ; return this ; } returns length ( ) { const { x , y , z , w } = this ; return Math . sqrt ( x * x + y * y + z * z + w * w ) ; } returns lengthSquared ( ) { const { x , y , z , w } = this ; return x * x + y * y + z * z + w * w ; } returns negate ( ) { this . x = - this . x ; this . y = - this . y ; this . z = - this . z ; this . w = - this . w ; return this ; } returns normalize ( ) { Vector4 . normalize ( this , this ) ; return this ; } param s returns scale ( s ) { this . x *= s ; this . y *= s ; this . z *= s ; this . w *= s ; return this ; } param out param outOffset toArray ( out , outOffset = 0 ) { out [ outOffset ] = this . x ; out [ outOffset + 1 ] = this . y ; out [ outOffset + 2 ] = this . z ; out [ outOffset + 3 ] = this . w ; } returns clone ( ) { let ret = new Vector4 ( this . x , this . y , this . z , this . w ) ; return ret ; } param out returns cloneTo ( out ) { out . x = this . x ; out . y = this . y ; out . z = this . z ; out . w = this . w ; return out ; } }