license param b64encoded returns export function stringToUint8array ( b64encoded ) { const u8 = new Uint8Array ( atob ( b64encoded ) . split ( "" ) . map ( ( c ) : number => { return c . charCodeAt ( 0 ) ; } ) ) ; return u8 ; } param b64encoded returns export function stringToArray ( b64encoded ) { const u = stringToUint8array ( b64encoded ) ; const result = new Int16Array ( u . buffer ) ; return result ; } let returnV = false ; let vVal = 0.0 ; export function gaussRandom ( ) { if ( returnV ) { returnV = false ; return vVal ; } const u = 2 * Math . random ( ) - 1 ; const v = 2 * Math . random ( ) - 1 ; const r = u * u + v * v ; if ( r === 0 || r > 1 ) { return gaussRandom ( ) ; } const c = Math . sqrt ( - 2 * Math . log ( r ) / r ) ; vVal = v * c ; returnV = true ; return u * c ; } export function randf ( a , b ) { return Math . random ( ) * ( b - a ) + a ; } export function randi ( a , b ) { return Math . floor ( Math . random ( ) * ( b - a ) + a ) ; } export function randn ( mu , std ) { return mu + gaussRandom ( ) * std ; } export function birandn ( mu1 , mu2 , std1 , std2 , rho ) { const z1 = randn ( 0 , 1 ) ; const z2 = randn ( 0 , 1 ) ; const x = Math . sqrt ( 1 - rho * rho ) * std1 * z1 + rho * std1 * z2 + mu1 ; const y = std2 * z2 + mu2 ; return [ x , y ] ; } /**
 * Sample from a categorial distribution
 */ export function sampleSoftmax ( zSample ) { const x = randf ( 0 , 1 ) ; const N = zSample . length ; let accumulate = 0 ; let i ; for ( i = 0 ; i < N ; i ++ ) { accumulate += zSample [ i ] ; if ( accumulate >= x ) { return i ; } } console . log ( 'error sampling pi index' ) ; return - 1 ; } export function simplifyLine ( V , tolerance = 2.0 ) { const tol = tolerance ; function diff ( u , v ) { return [ u [ 0 ] - v [ 0 ] , u [ 1 ] - v [ 1 ] ] ; } function dot ( u , v ) { return u [ 0 ] * v [ 0 ] + u [ 1 ] * v [ 1 ] ; } function norm2 ( v ) { return v [ 0 ] * v [ 0 ] + v [ 1 ] * v [ 1 ] ; } function d2 ( u , v ) { return norm2 ( diff ( u , v ) ) ; } function simplifyDP ( tol , v , j , k , mk ) { if ( k <= j + 1 ) { return ; } let maxi = j ; let maxd2 = 0 ; const tol2 = tol * tol ; const S = [ v [ j ] , v [ k ] ] ; const u = diff ( S [ 1 ] , S [ 0 ] ) ; const cu = norm2 ( u ) ; let w ; let pb ; let b , cw , dv2 ; for ( let i = j + 1 ; i < k ; i ++ ) { w = diff ( v [ i ] , S [ 0 ] ) ; cw = dot ( w , u ) ; if ( cw <= 0 ) { dv2 = d2 ( v [ i ] , S [ 0 ] ) ; } else if ( cu <= cw ) { dv2 = d2 ( v [ i ] , S [ 1 ] ) ; } else { b = cw / cu ; pb = [ S [ 0 ] [ 0 ] + b * u [ 0 ] , S [ 0 ] [ 1 ] + b * u [ 1 ] ] ; dv2 = d2 ( v [ i ] , pb ) ; } if ( dv2 <= maxd2 ) { continue ; } maxi = i ; maxd2 = dv2 ; } if ( maxd2 > tol2 ) { mk [ maxi ] = 1 ; simplifyDP ( tol , v , j , maxi , mk ) ; simplifyDP ( tol , v , maxi , k , mk ) ; } return ; } const n = V . length ; const sV = [ ] ; let i , k , m , pv ; const tol2 = tol * tol ; const vt = [ ] ; const mk = [ ] ; vt [ 0 ] = V [ 0 ] ; for ( i = k = 1 , pv = 0 ; i < n ; i ++ ) { if ( d2 ( V [ i ] , V [ pv ] ) < tol2 ) { continue ; } vt [ k ++ ] = V [ i ] ; pv = i ; } if ( pv < n - 1 ) { vt [ k ++ ] = V [ n - 1 ] ; } mk [ 0 ] = mk [ k - 1 ] = 1 ; simplifyDP ( tol , vt , 0 , k - 1 , mk ) ; for ( i = m = 0 ; i < k ; i ++ ) { if ( mk [ i ] ) { sV [ m ++ ] = vt [ i ] ; } } return sV ; } /**
 * Clean wrapper method to use RDP function.
 */ export function simplifyLines ( lines , tolerance ? ) { const result = [ ] ; let tol = 2.0 ; if ( tolerance ) { tol = tolerance ; } for ( let i = 0 ; i < lines . length ; i ++ ) { result . push ( simplifyLine ( lines [ i ] , tol ) ) ; } return result ; } /**
 * Convert from polylines to stroke-5 format that sketch-rnn uses
 */ export function linesToStrokes ( rawData ) { let x , y ; let px = 0 , py = 0 ; let dx , dy ; let pon , poff ; const stroke = [ ] ; let i , j ; let len ; let p ; for ( i = 0 ; i < rawData . length ; i ++ ) { len = rawData [ i ] . length ; if ( len > 1 ) { for ( j = 0 ; j < len ; j ++ ) { p = rawData [ i ] [ j ] ; x = p [ 0 ] ; y = p [ 1 ] ; if ( j === len - 1 ) { poff = 1 ; pon = 0 ; } else { poff = 0 ; pon = 1 ; } dx = x - px ; dy = y - py ; px = x ; py = y ; stroke . push ( [ dx , dy , pon , poff , 0 ] ) ; } } } stroke . push ( [ 0 , 0 , 0 , 0 , 1 ] ) ; return stroke . slice ( 1 ) ; } /**
 * Convert to stroke-5 format to polyline
 */ export function lineToStroke ( line , lastPoint ) { let pon , poff ; const stroke = [ ] ; let len ; let p ; let dx , dy ; let x , y ; let px , py ; let j ; px = lastPoint [ 0 ] ; py = lastPoint [ 1 ] ; len = line . length ; if ( len > 1 ) { for ( j = 0 ; j < len ; j ++ ) { p = line [ j ] ; x = p [ 0 ] ; y = p [ 1 ] ; if ( j === len - 1 ) { poff = 1 ; pon = 0 ; } else { poff = 0 ; pon = 1 ; } dx = x - px ; dy = y - py ; px = x ; py = y ; stroke . push ( [ dx , dy , pon , poff , 0 ] ) ; } } return stroke ; }