import ARToolKit from "./ARToolKit" ; exports constructor param { number } width param { number } height param { ARCameraParam | string } camera export class ARToolKitController { public width : any ; public height : any ; public orientation : string ; private framepointer : any ; private id : number ; private listeners : any ; private imageData : any ; private patternMarkers : any ; private barcodeMarkers : any ; private transformMat : any ; private defaultMarkerWidth : number ; private cameraParam : any ; private markerTransformMat : any ; private _bwpointer : any ; private framesize : any ; private dataHeap : any ; private cameraMat : any ; private onload : any ; constructor ( width , height , camera ) { this . orientation = "portrait" ; this . listeners = { } ; this . defaultMarkerWidth = 1 ; this . patternMarkers = { } ; this . barcodeMarkers = { } ; this . transformMat = new Float32Array ( 16 ) ; this . width = width ; this . height = height ; this . id = - 1 ; this . cameraParam = camera ; this . _initialize ( ) ; } /**
     * Destroys the ARController instance and frees all associated resources.
     * After calling dispose, the ARController can't be used any longer. Make a new one if you need one.
     * Calling this avoids leaking Emscripten memory, which may be important if you"re
     * using multiple ARControllers.
     */ public dispose ( ) { ARToolKit . teardown ( this . id ) ; for ( const key in this ) { if ( key ) { delete this [ key ] ; } } } /**
     * Detects markers in the given image. The process method dispatches marker detection events during its run.
     *
     * The marker detection process proceeds by first dispatching a markerNum event that tells you how many
     * markers were found in the image. Next, a getMarker event is dispatched for each found marker square.
     * finally, getMultiMarker is dispatched for every found multimarker
     * followed by getMultiMarkerSub events
     * dispatched for each of the markers in the multimarker.
     * arController.addEventListener("markerNum", function(ev) {
     * console.log("Detected " + ev.data + " markers.")
     * });
     * arController.addEventListener("getMarker", function(ev) {
     * console.log("Detected marker with ids:", ev.data.marker.id, ev.data.marker.idPatt, ev.data.marker.idMatrix);
     * console.log("Marker data", ev.data.marker);
     * console.log("Marker transform matrix:", [].join.call(ev.data.matrix, ", "));
     * });
     * arController.addEventListener("getMultiMarker", function(ev) {
     * console.log("Detected multimarker with id:", ev.data.multiMarkerId);
     * });
     * arController.addEventListener("getMultiMarkerSub", function(ev) {
     * console.log("Submarker for " + ev.data.multiMarkerId, ev.data.markerIndex, ev.data.marker);
     * });
     * arController.process(image);
     * If no image is given, defaults to this.image.
     * If the debugSetup has been called, draws debug markers on the debug canvas.
     */ public process ( imageData ) { this . detectMarker ( imageData ) ; const markerNum = this . getMarkerNum ( ) ; for ( const k in this . patternMarkers ) { if ( k !== undefined ) { const o = this . patternMarkers [ k ] ; o . inPrevious = o . inCurrent ; o . inCurrent = false ; } } for ( const k in this . barcodeMarkers ) { if ( k !== undefined ) { const o = this . barcodeMarkers [ k ] ; o . inPrevious = o . inCurrent ; o . inCurrent = false ; } } let visible ; let multiEachMarkerInfo ; for ( let i = 0 ; i < markerNum ; i ++ ) { const markerInfo = this . getMarker ( i ) ; let markerType = ARToolKit . UNKNOWN_MARKER ; visible = this . trackPatternMarkerId ( - 1 ) ; if ( markerInfo . idPatt > - 1 && ( markerInfo . id === markerInfo . idPatt || markerInfo . idMatrix === - 1 ) ) { visible = this . trackPatternMarkerId ( markerInfo . idPatt ) ; markerType = ARToolKit . PATTERN_MARKER ; if ( markerInfo . dir !== markerInfo . dirPatt ) { this . setMarkerInfoDir ( i , markerInfo . dirPatt ) ; } } else if ( markerInfo . idMatrix > - 1 ) { visible = this . trackBarcodeMarkerId ( markerInfo . idMatrix ) ; markerType = ARToolKit . BARCODE_MARKER ; if ( markerInfo . dir !== markerInfo . dirMatrix ) { this . setMarkerInfoDir ( i , markerInfo . dirMatrix ) ; } } if ( markerType !== ARToolKit . UNKNOWN_MARKER && visible . inPrevious ) { this . getTransMatSquareCont ( i , visible . markerWidth , visible . matrix , visible . matrix ) ; } else { this . getTransMatSquare ( i , visible . markerWidth , visible . matrix ) ; } visible . inCurrent = true ; this . transMatToGLMat ( visible . matrix , this . transformMat ) ; this . dispatchEvent ( { name : "getMarker" , target : this , data : { index : i , type : markerType , marker : markerInfo , matrix : this . transformMat } } ) ; } const multiMarkerCount = this . getMultiMarkerCount ( ) ; for ( let i = 0 ; i < multiMarkerCount ; i ++ ) { const subMarkerCount = this . getMultiMarkerPatternCount ( i ) ; visible = false ; ARToolKit . getTransMatMultiSquareRobust ( this . id , i ) ; this . transMatToGLMat ( this . markerTransformMat , this . transformMat ) ; for ( let j = 0 ; j < subMarkerCount ; j ++ ) { multiEachMarkerInfo = this . getMultiEachMarker ( i , j ) ; if ( multiEachMarkerInfo . visible >= 0 ) { visible = true ; this . dispatchEvent ( { name : "getMultiMarker" , target : this , data : { multiMarkerId : i , matrix : this . transformMat } } ) ; break ; } } if ( visible ) { for ( let j = 0 ; j < subMarkerCount ; j ++ ) { multiEachMarkerInfo = this . getMultiEachMarker ( i , j ) ; this . transMatToGLMat ( this . markerTransformMat , this . transformMat ) ; this . dispatchEvent ( { name : "getMultiMarkerSub" , target : this , data : { multiMarkerId : i , markerIndex : j , marker : multiEachMarkerInfo , matrix : this . transformMat } } ) ; } } } if ( this . _bwpointer ) { this . debugDraw ( ) ; } } param { number } id param { number } markerWidth return { Object } public trackPatternMarkerId ( id , markerWidth ? ) { let obj = this . patternMarkers [ id ] ; if ( ! obj ) { this . patternMarkers [ id ] = obj = { inPrevious : false , inCurrent : false , matrix : new Float32Array ( 12 ) , markerWidth : markerWidth || this . defaultMarkerWidth } ; } if ( markerWidth ) { obj . markerWidth = markerWidth ; } return obj ; } param { number } id param { number } markerWidth return { Object } public trackBarcodeMarkerId ( id , markerWidth ? ) { let obj = this . barcodeMarkers [ id ] ; if ( ! obj ) { this . barcodeMarkers [ id ] = obj = { inPrevious : false , inCurrent : false , matrix : new Float32Array ( 12 ) , markerWidth : markerWidth || this . defaultMarkerWidth } ; } if ( markerWidth ) { obj . markerWidth = markerWidth ; } return obj ; } return { number } public getMultiMarkerCount ( ) { return ARToolKit . getMultiMarkerCount ( this . id ) ; } param { number } multiMarkerId return { number } public getMultiMarkerPatternCount ( multiMarkerId ) { return ARToolKit . getMultiMarkerNum ( this . id , multiMarkerId ) ; } param { string } name param { function } callback public addEventListener ( name , callback ) { if ( ! this . listeners [ name ] ) { this . listeners [ name ] = [ ] ; } this . listeners [ name ] . push ( callback ) ; } param { string } name param { function } callback public removeEventListener ( name , callback ) { if ( this . listeners [ name ] ) { const index = this . listeners [ name ] . indexOf ( callback ) ; if ( index > - 1 ) { this . listeners [ name ] . splice ( index , 1 ) ; } } } param { Object } event public dispatchEvent ( event ) { const listeners = this . listeners [ event . name ] ; if ( listeners ) { for ( let i = 0 ; i < listeners . length ; i ++ ) { listeners [ i ] . call ( this , event ) ; } } } param { string } markerURL param { function } onSuccess param { function } onError public loadMarker ( markerURL , onSuccess , onError ) { return ARToolKit . addMarker ( this . id , markerURL , onSuccess , onError ) ; } param { string } markerURL param { function } onSuccess param { function } onError public loadMultiMarker ( markerURL , onSuccess , onError ) { return ARToolKit . addMultiMarker ( this . id , markerURL , onSuccess , onError ) ; } param { number } markerUID param { number } markerWidth param { Float64Array } dst return { Float64Array } public getTransMatSquare ( markerUID , markerWidth , dst ) { ARToolKit . getTransMatSquare ( this . id , markerUID , markerWidth ) ; dst . set ( this . markerTransformMat ) ; return dst ; } param { number } markerUID param { number } markerWidth param { Float64Array } previousMarkerTransform param { Float64Array } dst return { Float64Array } public getTransMatSquareCont ( markerUID , markerWidth , previousMarkerTransform , dst ) { this . markerTransformMat . set ( previousMarkerTransform ) ; ARToolKit . getTransMatSquareCont ( this . id , markerUID , markerWidth ) ; dst . set ( this . markerTransformMat ) ; return dst ; } param { number } markerUID param { Float64Array } dst return { Float64Array } public getTransMatMultiSquare ( markerUID , dst ) { ARToolKit . getTransMatMultiSquare ( this . id , markerUID ) ; dst . set ( this . markerTransformMat ) ; return dst ; } param { number } markerUID param { Float64Array } dst return { Float64Array } public getTransMatMultiSquareRobust ( markerUID , dst ) { ARToolKit . getTransMatMultiSquare ( this . id , markerUID ) ; dst . set ( this . markerTransformMat ) ; return dst ; } param { Float64Array } transMat param { Float64Array } glMat param { number } scale public transMatToGLMat ( transMat , glMat , scale ? ) { glMat [ 0 + 0 * 4 ] = transMat [ 0 ] ; glMat [ 0 + 1 * 4 ] = transMat [ 1 ] ; glMat [ 0 + 2 * 4 ] = transMat [ 2 ] ; glMat [ 0 + 3 * 4 ] = transMat [ 3 ] ; glMat [ 1 + 0 * 4 ] = transMat [ 4 ] ; glMat [ 1 + 1 * 4 ] = transMat [ 5 ] ; glMat [ 1 + 2 * 4 ] = transMat [ 6 ] ; glMat [ 1 + 3 * 4 ] = transMat [ 7 ] ; glMat [ 2 + 0 * 4 ] = transMat [ 8 ] ; glMat [ 2 + 1 * 4 ] = transMat [ 9 ] ; glMat [ 2 + 2 * 4 ] = transMat [ 10 ] ; glMat [ 2 + 3 * 4 ] = transMat [ 11 ] ; glMat [ 3 + 0 * 4 ] = 0.0 ; glMat [ 3 + 1 * 4 ] = 0.0 ; glMat [ 3 + 2 * 4 ] = 0.0 ; glMat [ 3 + 3 * 4 ] = 1.0 ; if ( scale !== undefined && scale !== 0.0 ) { glMat [ 12 ] *= scale ; glMat [ 13 ] *= scale ; glMat [ 14 ] *= scale ; } return glMat ; } param { HTMLImageElement | HTMLVideoElement } image return { number } public detectMarker ( imageData ) { if ( this . _copyImageToHeap ( imageData ) ) { return ARToolKit . detectMarker ( this . id ) ; } return - 99 ; } return { number } public getMarkerNum ( ) { return ARToolKit . getMarkerNum ( this . id ) ; } field field field field field field field field field field field field field param { number } markerIndex returns { Object } public getMarker ( markerIndex ) { if ( 0 === ARToolKit . getMarker ( this . id , markerIndex ) ) { return ARToolKit . getMarkerInfo ( ) ; } } param { number } markerIndex param { * } vertexData public setMarkerInfoVertex ( markerIndex , vertexData ) { for ( let i = 0 ; i < vertexData . length ; i ++ ) { this . markerTransformMat [ i * 2 + 0 ] = vertexData [ i ] [ 0 ] ; this . markerTransformMat [ i * 2 + 1 ] = vertexData [ i ] [ 1 ] ; } return ARToolKit . setMarkerInfoVertex ( this . id , markerIndex ) ; } param { Object } markerInfo return { Object } public cloneMarkerInfo ( markerInfo ) { return JSON . parse ( JSON . stringify ( markerInfo ) ) ; } field field field field param { number } multiMarkerId param { number } markerIndex returns { Object } public getMultiEachMarker ( multiMarkerId , markerIndex ) { if ( 0 === ARToolKit . getMultiEachMarker ( this . id , multiMarkerId , markerIndex ) ) { return ARToolKit . multiEachMarkerInfo ; } } return { Float64Array } public getTransformationMatrix ( ) { return this . transformMat ; } return { Float64Array } public getCameraMatrix ( ) { return this . cameraMat ; } return { Float64Array } public getMarkerTransformationMatrix ( ) { return this . markerTransformMat ; } param { boolean } mode see getDebugMode public setDebugMode ( mode ) { return ARToolKit . setDebugMode ( this . id , mode ) ; } return { boolean } see setDebugMode public getDebugMode ( ) { return ARToolKit . getDebugMode ( this . id ) ; } return { number } public getProcessingImage ( ) { return ARToolKit . getProcessingImage ( this . id ) ; } param mode public setLogLevel ( mode ) { return ARToolKit . setLogLevel ( mode ) ; } returns { * } public getLogLevel ( ) { return ARToolKit . getLogLevel ( ) ; } param markerIndex param dir returns { * } public setMarkerInfoDir ( markerIndex , dir ) { return ARToolKit . setMarkerInfoDir ( this . id , markerIndex , dir ) ; } param value returns { * } public setProjectionNearPlane ( value ) { return ARToolKit . setProjectionNearPlane ( this . id , value ) ; } returns { * } public getProjectionNearPlane ( ) { return ARToolKit . getProjectionNearPlane ( this . id ) ; } param value returns { * } public setProjectionFarPlane ( value ) { return ARToolKit . setProjectionFarPlane ( this . id , value ) ; } returns { * } public getProjectionFarPlane ( ) { return ARToolKit . getProjectionFarPlane ( this . id ) ; } param { number } mode public setThresholdMode ( mode ) { return ARToolKit . setThresholdMode ( this . id , mode ) ; } return { number } see getVideoThresholdMode public getThresholdMode ( ) { return ARToolKit . getThresholdMode ( this . id ) ; } param { number } threshold public setThreshold ( threshold ) { return ARToolKit . setThreshold ( this . id , threshold ) ; } return { number } public getThreshold ( ) { return ARToolKit . getThreshold ( this . id ) ; } param { number } mode public setPatternDetectionMode ( mode ) { return ARToolKit . setPatternDetectionMode ( this . id , mode ) ; } return { number } public getPatternDetectionMode ( ) { return ARToolKit . getPatternDetectionMode ( this . id ) ; } param type public setMatrixCodeType ( type ) { return ARToolKit . setMatrixCodeType ( this . id , type ) ; } return { number } public getMatrixCodeType ( ) { return ARToolKit . getMatrixCodeType ( this . id ) ; } param { number } mode public setLabelingMode ( mode ) { return ARToolKit . setLabelingMode ( this . id , mode ) ; } result public getLabelingMode ( ) { return ARToolKit . getLabelingMode ( this . id ) ; } param { number } pattRatio public setPattRatio ( pattRatio ) { return ARToolKit . setPattRatio ( this . id , pattRatio ) ; } return { number } public getPattRatio ( ) { return ARToolKit . getPattRatio ( this . id ) ; } param { number } mode public setImageProcMode ( mode ) { return ARToolKit . setImageProcMode ( this . id , mode ) ; } return { number } public getImageProcMode ( ) { return ARToolKit . getImageProcMode ( this . id ) ; } /**
     * Draw the black and white image and debug markers to the ARController canvas.
     * See setDebugMode.
     */ public debugDraw ( ) { } private _initialize ( ) { this . id = ARToolKit . setup ( this . width , this . height , this . cameraParam . id ) ; const params = ARToolKit . getFrameMalloc ( ) ; if ( params ) { this . framepointer = params . framepointer ; this . framesize = params . framesize ; this . dataHeap = new Uint8Array ( ARToolKit . HEAPU8 . buffer , this . framepointer , this . framesize ) ; this . cameraMat = new Float64Array ( ARToolKit . HEAPU8 . buffer , params . camera , 16 ) ; this . markerTransformMat = new Float64Array ( ARToolKit . HEAPU8 . buffer , params . transform , 12 ) ; } else { throw Error ( "frameMalloc not set" ) ; } this . setProjectionNearPlane ( 0.1 ) ; this . setProjectionFarPlane ( 1000 ) ; setTimeout ( ( ) => { if ( this . onload ) { this . onload ( ) ; } this . dispatchEvent ( { name : "load" , target : this } ) ; } , 0 ) ; } param { HTMLImageElement | HTMLVideoElement } image returns { boolean } private private _copyImageToHeap ( imageData ) { if ( ! imageData ) { imageData = this . imageData ; } if ( this . dataHeap ) { this . dataHeap . set ( imageData ) ; return true ; } return false ; } } export default ARToolKitController ;