import { vec3 , quat } from 'gl-matrix' ; class Transform { rot = quat . create ( ) ; pos = vec3 . create ( ) ; scl = vec3 . fromValues ( 1 , 1 , 1 ) ; constructor ( ) ; constructor ( tran ) ; constructor ( rot , pos , scl ) ; constructor ( rot ? , pos ? , scl ? ) { if ( rot instanceof Transform ) { this . copy ( rot ) ; } else if ( rot && pos && scl ) { this . set ( rot , pos , scl ) ; } } reset ( ) { quat . set ( this . rot , 0 , 0 , 0 , 1 ) ; vec3 . set ( this . pos , 0 , 0 , 0 ) ; vec3 . set ( this . scl , 1 , 1 , 1 ) ; return this ; } copy ( t ) { quat . copy ( this . rot , t . rot ) ; vec3 . copy ( this . pos , t . pos ) ; vec3 . copy ( this . scl , t . scl ) ; return this ; } set ( r ? , p ? , s ? ) { if ( r ) quat . copy ( this . rot , r ) ; if ( p ) vec3 . copy ( this . pos , p ) ; if ( s ) vec3 . copy ( this . scl , s ) ; return this ; } setPos ( v ) { vec3 . copy ( this . pos , v ) ; return this ; } setRot ( v ) { quat . copy ( this . rot , v ) ; return this ; } setScl ( v ) { vec3 . copy ( this . scl , v ) ; return this ; } clone ( ) { return new Transform ( this ) ; } mul ( tran ) ; mul ( cr , cp , cs ? ) ; mul ( cr , cp ? , cs ? ) { if ( cr instanceof Transform ) { cp = cr . pos ; cs = cr . scl ; cr = cr . rot ; } if ( cr && cp ) { const tmp = [ 0 , 0 , 0 ] ; vec3 . mul ( tmp , this . scl , cp ) ; vec3 . transformQuat ( tmp , tmp , this . rot ) ; vec3 . add ( this . pos , this . pos , tmp ) ; if ( cs ) vec3 . mul ( this . scl , this . scl , cs ) ; quat . mul ( this . rot , this . rot , cr ) ; } return this ; } pmul ( tran ) ; pmul ( pr , pp , ps ) ; pmul ( pr , pp ? , ps ? ) { if ( pr instanceof Transform ) { pp = pr . pos ; ps = pr . scl ; pr = pr . rot ; } if ( ! pr || ! pp || ! ps ) return this ; const tmp = [ 0 , 0 , 0 ] ; vec3 . mul ( tmp , this . pos , ps ) ; vec3 . transformQuat ( tmp , tmp , pr ) ; vec3 . add ( this . pos , tmp , pp ) ; if ( ps ) vec3 . mul ( this . scl , this . scl , ps ) ; quat . mul ( this . rot , pr , this . rot ) ; return this ; } addPos ( cp , ignoreScl = false ) { if ( ignoreScl ) { vec3 . add ( this . pos , this . pos , vec3 . transformQuat ( [ 0 , 0 , 0 ] , cp , this . rot ) ) ; } else { const tmp = [ 0 , 0 , 0 ] ; vec3 . mul ( tmp , cp , this . scl ) ; vec3 . add ( this . pos , this . pos , vec3 . transformQuat ( tmp , tmp , this . rot ) ) ; } return this ; } fromMul ( tp , tc ) { const tmp = [ 0 , 0 , 0 ] ; vec3 . mul ( tmp , tp . scl , tc . pos ) ; vec3 . transformQuat ( tmp , tmp , tp . rot ) ; vec3 . add ( this . pos , tp . pos , tmp ) ; vec3 . mul ( this . scl , tp . scl , tc . scl ) ; quat . mul ( this . rot , tp . rot , tc . rot ) ; return this ; } fromInvert ( t ) { quat . invert ( this . rot , t . rot ) ; vec3 . inverse ( this . scl , t . scl ) ; const tmp = [ 0 , 0 , 0 ] ; vec3 . negate ( tmp , t . pos ) ; vec3 . mul ( tmp , tmp , this . scl ) ; vec3 . transformQuat ( this . pos , tmp , this . rot ) ; return this ; } transformVec3 ( v , out ? ) { const tmp = [ 0 , 0 , 0 ] ; vec3 . mul ( tmp , v , this . scl ) ; vec3 . transformQuat ( tmp , tmp , this . rot ) ; return vec3 . add ( ( out || v ) , tmp , this . pos ) ; } static mul ( tp , tc ) { return new Transform ( ) . fromMul ( tp , tc ) ; } static invert ( t ) { return new Transform ( ) . fromInvert ( t ) ; } static fromPos ( v ) ; static fromPos ( x , y , z ) ; static fromPos ( x , y ? , z ? ) { const t = new Transform ( ) ; if ( x instanceof Float32Array || x instanceof Array ) { vec3 . copy ( t . pos , x ) ; } else if ( x != undefined && y != undefined && z != undefined ) { vec3 . set ( t . pos , x , y , z ) ; } return t ; } } export default Transform ;