/* * PROJECT: FLARManager * http://transmote.com/flar * Copyright 2009, Eric Socolofsky * -------------------------------------------------------------------------------- * This work complements FLARToolkit, developed by Saqoosha as part of the Libspark project. * http://www.libspark.org/wiki/saqoosha/FLARToolKit * FLARToolkit is Copyright (C)2008 Saqoosha, * and is ported from NYARToolkit, which is ported from ARToolkit. * * This program is free software; you can redistribute it and/or * modify it under the terms of the GNU General Public License * as published by the Free Software Foundation; either version 2 * of the License, or (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this framework; if not, write to the Free Software * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA * * For further information please contact: * * http://transmote.com/flar * */ package com.transmote.flar.utils { import __AS3__.vec.Vector; import com.transmote.flar.marker.FLARMarker; import com.transmote.flar.marker.FLARMarkerEvent; import com.transmote.flar.marker.FLARMarkerOutline; import com.transmote.flar.pattern.FLARPattern; import com.transmote.flar.source.IFLARSource; import flash.display.BitmapData; import flash.display.Stage; import flash.events.Event; import flash.events.EventDispatcher; import flash.events.IOErrorEvent; import flash.events.KeyboardEvent; import flash.events.MouseEvent; import flash.events.SecurityErrorEvent; import flash.geom.Rectangle; import flash.net.URLLoader; import flash.net.URLLoaderDataFormat; import flash.net.URLRequest; import flash.utils.ByteArray; import org.libspark.flartoolkit.core.FLARSquare; import org.libspark.flartoolkit.core.param.FLARParam; import org.libspark.flartoolkit.core.transmat.FLARTransMatResult; import org.libspark.flartoolkit.core.types.FLARDoublePoint2d; import org.libspark.flartoolkit.detector.FLARMultiMarkerDetectorResult; /** * FLARProxy provides a way to test FLARToolkit applications with a mouse and keyboard. * to use, instantiate a FLARProxy instance and call FLARProxy.activate(). * set up handlers for FLARMarkerEvents in the same manner as with FLARManager. * * clicking the mouse sends a MARKER_ADDED event, * dragging the mouse sends a MARKER_UPDATED event, * and releasing the mouse sends a MARKER_REMOVED event. * these events can be handled exactly as if they were dispatched by FLARManager. * * press keys 0-9 to specify a patternId. * FLARProxy only supports patternIds 0 through 9. */ public class FLARProxy extends EventDispatcher implements IFLARSource { private static const MAX_NUMBER_MARKERS:uint = 10; private static const DEFAULT_MARKER_Z:Number = 400; private var _resultsToDisplayRatio:Number; private var stage:Stage; private var markerCornerVal:Number; private var mouseIsDown:Boolean; private var activePatternId:uint = 0; private var activeMarkers:Vector.; private var bLoadingCameraParams:Boolean = false; private var bActivatePending:Boolean = false; private var _cameraParams:FLARParam; /** * constructor. * @param resultsToDisplayRatio similar to IFLARSource, resultsToDisplayRatio is the ratio of the screen size * as reported to FLARToolkit to the actual screen size. defaults to 0.5, * to match FLARCameraSource and FLARLoaderSource. */ public function FLARProxy (resultsToDisplayRatio:Number=0.5, cameraParamsPath:String="") { this._resultsToDisplayRatio = resultsToDisplayRatio; this.markerCornerVal = 0.5 * this._resultsToDisplayRatio * FLARPattern.DEFAULT_UNSCALED_MARKER_WIDTH; this.activeMarkers = new Vector.(MAX_NUMBER_MARKERS, true); if (cameraParamsPath != "") { this.loadCameraParams(cameraParamsPath); } } /** * FLARParam used by this FLARProxy, for use with Papervision. */ public function get cameraParams () :FLARParam { return this._cameraParams; } /** * begin marker simulation. * @param stage a reference to the application Stage. */ public function activate (stage:Stage) :void { this.stage = stage; if (this.bLoadingCameraParams) { this.bActivatePending = true; return; } this.stage.addEventListener(MouseEvent.MOUSE_DOWN, this.onMouseDown); this.stage.addEventListener(MouseEvent.MOUSE_UP, this.onMouseUp); this.stage.addEventListener(KeyboardEvent.KEY_DOWN, this.onKeyDown); this.dispatchEvent(new Event(Event.INIT)); } /** * stop marker simulation. */ public function deactivate () :void { this.stage.removeEventListener(MouseEvent.MOUSE_DOWN, this.onMouseDown); this.stage.removeEventListener(MouseEvent.MOUSE_UP, this.onMouseUp); this.stage.removeEventListener(KeyboardEvent.KEY_DOWN, this.onKeyDown); } /** * load camera params file; needed to generate transform matrices. * called automatically if cameraParamsPath passed into constructor. * @param cameraParamsPath path to camera parameters binary. */ public function loadCameraParams (cameraParamsPath:String) :void { if (this.bLoadingCameraParams) { return; } this.bLoadingCameraParams = true; var loader:URLLoader = new URLLoader(); loader.dataFormat = URLLoaderDataFormat.BINARY; loader.addEventListener(IOErrorEvent.IO_ERROR, this.onCameraParamsLoadError); loader.addEventListener(SecurityErrorEvent.SECURITY_ERROR, this.onCameraParamsLoadError); loader.addEventListener(Event.COMPLETE, this.onCameraParamsLoaded); loader.load(new URLRequest(cameraParamsPath)); } /** * FLARProxy updates on mouse interaction; * method is here only for compliance with IFLARSource. */ public function update () :void {} /** * FLARProxy has no BitmapData source; * method is here only for compliance with IFLARSource. */ public function get source () :BitmapData { return null; } /** * size of BitmapData source used for analysis. */ public function get sourceSize () :Rectangle { return new Rectangle(0, 0, this.stage.stageWidth, this.stage.stageHeight); } /** * ratio of area of reported results to display size. * use to scale (multiply) results of FLARToolkit analysis to correctly fit display area. */ public function get resultsToDisplayRatio () :Number { return this._resultsToDisplayRatio; } /** * FLARProxy cannot be mirrored; * method is here only for compliance with IFLARSource. */ public function get mirrored () :Boolean { return false; } public function set mirrored (val:Boolean) :void {} private function addMarker (patternId:uint, x:Number, y:Number) :void { var proxyMarker:FLARMarker = new FLARMarker(this.createProxyResult(patternId, x, y), this.createProxyMatrix(x, y), this, this.createProxyOutline(x, y)); proxyMarker.setSessionId(); this.activeMarkers[patternId] = proxyMarker; this.dispatchEvent(new FLARMarkerEvent(FLARMarkerEvent.MARKER_ADDED, proxyMarker)); } private function updateMarker (patternId:uint, x:Number, y:Number) :void { var proxyMarker:FLARMarker = this.activeMarkers[patternId]; if (!proxyMarker) { return; } var updatedMarker:FLARMarker = new FLARMarker(this.createProxyResult(patternId, x, y), this.createProxyMatrix(x, y), this, this.createProxyOutline(x, y)); proxyMarker.copy(updatedMarker); this.dispatchEvent(new FLARMarkerEvent(FLARMarkerEvent.MARKER_UPDATED, proxyMarker)); } private function removeMarker (patternId:uint, x:Number, y:Number) :void { var proxyMarker:FLARMarker = this.activeMarkers[patternId]; if (!proxyMarker) { return; } var removedMarker:FLARMarker = new FLARMarker(this.createProxyResult(patternId, x, y), this.createProxyMatrix(x, y), this, this.createProxyOutline(x, y)); proxyMarker.copy(removedMarker); this.activeMarkers[patternId] = null; this.dispatchEvent(new FLARMarkerEvent(FLARMarkerEvent.MARKER_REMOVED, proxyMarker)); } private function onMouseDown (evt:MouseEvent) :void { this.stage.addEventListener(MouseEvent.MOUSE_MOVE, this.onMouseMove); this.mouseIsDown = true; this.addMarker(this.activePatternId, evt.stageX, evt.stageY); } private function onMouseMove (evt:MouseEvent) :void { this.updateMarker(this.activePatternId, evt.stageX, evt.stageY); } private function onMouseUp (evt:MouseEvent) :void { this.stage.removeEventListener(MouseEvent.MOUSE_MOVE, this.onMouseMove); this.mouseIsDown = false; this.removeMarker(this.activePatternId, evt.stageX, evt.stageY); } private function onKeyDown (evt:KeyboardEvent) :void { var newPatternId:uint = evt.keyCode - 48; if (newPatternId == this.activePatternId || newPatternId < 0 || newPatternId > 9) { return; } trace("FLARProxy active patternId: "+ newPatternId); var lastPatternId:uint = this.activePatternId; this.activePatternId = newPatternId; if (this.mouseIsDown) { this.removeMarker(lastPatternId, this.stage.mouseX, this.stage.mouseY); this.addMarker(this.activePatternId, this.stage.mouseX, this.stage.mouseY); } } private function createProxyResult (patternId:int, x:Number, y:Number) :FLARMultiMarkerDetectorResult { return new FLARMultiMarkerDetectorResult(patternId, 0, 1.0, this.createProxySquare(x, y)); } private function createProxySquare (x:Number, y:Number) :FLARSquare { x *= this._resultsToDisplayRatio; y *= this._resultsToDisplayRatio; var sqvertex:Array = new Array( new FLARDoublePoint2d(x-this.markerCornerVal, y-this.markerCornerVal), new FLARDoublePoint2d(x+this.markerCornerVal, y-this.markerCornerVal), new FLARDoublePoint2d(x+this.markerCornerVal, y+this.markerCornerVal), new FLARDoublePoint2d(x-this.markerCornerVal, y+this.markerCornerVal)); var flarSquare:FLARSquare = new FLARSquare(); flarSquare.sqvertex = sqvertex; return flarSquare; } private function createProxyOutline (x:Number, y:Number) :FLARMarkerOutline { trace("FLARMarkerOutline is deprecated; access its methods and properties via FLARMarker."); return new FLARMarkerOutline(this.createProxySquare(x, y), null, this._resultsToDisplayRatio); } private function createProxyMatrix (x:Number, y:Number) :FLARTransMatResult { var matrix:FLARTransMatResult = FLARGeomUtils.createFLARIdentityTransMat(); matrix.m03 = (x - 0.5*this.stage.stageWidth) * this._resultsToDisplayRatio; matrix.m13 = (y - 0.5*this.stage.stageHeight) * this._resultsToDisplayRatio; matrix.m23 = DEFAULT_MARKER_Z; return matrix; } private function onCameraParamsLoadError (evt:Event) :void { var errorText:String = "Camera params load error."; if (evt is IOErrorEvent) { errorText = IOErrorEvent(evt).text; } else if (evt is SecurityErrorEvent) { errorText = SecurityErrorEvent(evt).text; } this.onCameraParamsLoaded(evt, new Error(errorText)); } private function onCameraParamsLoaded (evt:Event, error:Error=null) :void { var loader:URLLoader = evt.target as URLLoader; loader.removeEventListener(IOErrorEvent.IO_ERROR, this.onCameraParamsLoadError); loader.removeEventListener(SecurityErrorEvent.SECURITY_ERROR, this.onCameraParamsLoadError); loader.removeEventListener(Event.COMPLETE, this.onCameraParamsLoaded); this.bLoadingCameraParams = false; if (error) { throw error; } this._cameraParams = new FLARParam(); this._cameraParams.loadARParam(ByteArray(loader.data)); this._cameraParams.changeScreenSize(this._resultsToDisplayRatio*this.stage.stageWidth, this._resultsToDisplayRatio*this.stage.stageHeight); if (this.bActivatePending) { this.activate(this.stage); this.bActivatePending = false; } } } }