import { Segment } from "./types" ; export namespace SegmentSelector { function select ( segments , selection ) { let result = [ ] ; segments . forEach ( ( seg ) => { let index = ( seg . fill . above ? 8 : 0 ) + ( seg . fill . below ? 4 : 0 ) + ( seg . otherFill && seg . otherFill . above ? 2 : 0 ) + ( seg . otherFill && seg . otherFill . below ? 1 : 0 ) ; if ( selection [ index ] !== 0 ) { result . push ( new Segment ( seg . start , seg . end , { above : selection [ index ] === 1 , below : selection [ index ] === 2 , } ) ) ; } } ) ; return result ; } export function union ( segments ) { return select ( segments , [ 0 , 2 , 1 , 0 , 2 , 2 , 0 , 0 , 1 , 0 , 1 , 0 , 0 , 0 , 0 , 0 , ] ) ; } export function difference ( segments ) { return select ( segments , [ 0 , 0 , 0 , 0 , 2 , 0 , 2 , 0 , 1 , 1 , 0 , 0 , 0 , 1 , 2 , 0 , ] ) ; } export function xor ( segments ) { return select ( segments , [ 0 , 2 , 1 , 0 , 2 , 0 , 0 , 1 , 1 , 0 , 0 , 2 , 0 , 1 , 2 , 0 , ] ) ; } }