class export default class VolumeMaker { param { number } vx param { number } vy param { number } vz param { function } sdFunc static createVolume ( vx , vy , vz , sdFunc ) { const data = new Uint8Array ( vx * vy * vz ) . fill ( 0 ) ; const cx = vx / 2 ; const cy = vy / 2 ; const cz = vz / 2 ; let offset , px , py , pz ; for ( let i = 0 ; i < vz ; ++ i ) { for ( let j = 0 ; j < vy ; ++ j ) { for ( let k = 0 ; k < vx ; ++ k ) { offset = i * ( vx * vy ) + j * vx + k ; px = k - cx ; py = j - cy ; pz = i - cz ; if ( sdFunc ( px , py , pz ) < 0 ) { data [ offset ] = 255 ; } else { data [ offset ] = 0 ; } } } } return data ; } param { number } vx param { number } vy param { number } vz param { number } radius static createSphere ( vx , vy , vz , radius ) { return VolumeMaker . createVolume ( vx , vy , vz , ( px , py , pz ) => { return Math . sqrt ( px * px + py * py + pz * pz ) - radius ; } ) ; } param { number } vx param { number } vy param { number } vz param { number } hx param { number } hy static createCylinder ( vx , vy , vz , hx , hy ) { let dx , dy , mdx , mdy ; return VolumeMaker . createVolume ( vx , vy , vz , ( px , py , pz ) => { dx = Math . abs ( Math . sqrt ( px * px + pz * pz ) ) - hx ; dy = Math . abs ( py ) - hy ; mdx = Math . max ( dx , 0.0 ) ; mdy = Math . max ( dy , 0.0 ) ; return Math . min ( Math . max ( dx , dy ) , 0.0 ) + Math . sqrt ( mdx * mdx + mdy * mdy ) ; } ) ; } param { number } vx param { number } vy param { number } vz param { number } tx param { number } ty static createTorus ( vx , vy , vz , tx , ty ) { let qx , qy ; return VolumeMaker . createVolume ( vx , vy , vz , ( px , py , pz ) => { qx = Math . sqrt ( px * px + pz * pz ) - tx ; qy = py ; return Math . sqrt ( qx * qx + qy * qy ) - ty ; } ) ; } param { number } vx param { number } vy param { number } vz param { number } cx param { number } cy static createCone ( vx , vy , vz , cx , cy ) { let q ; return VolumeMaker . createVolume ( vx , vy , vz , ( px , py , pz ) => { q = Math . sqrt ( px * px + py * py ) ; return cx * q + cy * pz ; } ) ; } }