forked from: forked from: Fast texture extraction

by makc3d forked from forked from: Fast texture extraction (diff: 58)
Testing projected shape behavior (still sucks).
♥0 | Line 143 | Modified 2010-12-06 16:36:03 | MIT License
play

ActionScript3 source code

/**
 * Copyright makc3d ( http://wonderfl.net/user/makc3d )
 * MIT License ( http://www.opensource.org/licenses/mit-license.php )
 * Downloaded from: http://wonderfl.net/c/mLnN
 */

package {
	import com.bit101.components.HSlider;
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.display.Loader;
	import flash.display.LoaderInfo;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.geom.Point;
	import flash.net.URLRequest;
	import flash.system.LoaderContext;
	
	/**
	 * Testing projected shape behavior (still sucks).
	 */
	[SWF(width=465,height=465,backgroundColor=0)]
	public class HTest extends Sprite {
		public var anchors:Vector.<Anchor>;
		public var homography:Homography;
		public var focal:HSlider;
		public function HTest () {
			anchors = new Vector.<Anchor> (4, true);
			addChild (anchors [0] = new Anchor (100, 257));
			addChild (anchors [1] = new Anchor (122, 218));
			addChild (anchors [2] = new Anchor (131, 251));
			addChild (anchors [3] = new Anchor (111, 296));
			addChild (homography = new Homography);
			homography.x = homography.y = 232;
			with (focal = new HSlider (this, 100, 300)) {
				minimum = 10; maximum = 10000; tick = 0.1; value = 400; width = 250;
			}
			stage.addEventListener (MouseEvent.MOUSE_MOVE, onMouseMove);
			onMouseMove (null);
		}
		public function onMouseMove (e:MouseEvent):void {
			if ((e == null) || e.buttonDown) {
				homography.update (focal.value,
					new Point (anchors [0].x, anchors [0].y),
					new Point (anchors [1].x, anchors [1].y),
					new Point (anchors [2].x, anchors [2].y),
					new Point (anchors [3].x, anchors [3].y)
				);
			}
		}
	}
}

import flash.display.Sprite;
import flash.events.MouseEvent;
class Anchor extends Sprite {
	public function Anchor (x0:int, y0:int) {
		x = x0; y = y0;
		graphics.beginFill (0xFF7F00, 1);
		graphics.drawRect (-4, -4, 8, 8);
		graphics.drawRect (-2, -2, 4, 4);
		graphics.beginFill (0xFF7F00, 0);
		graphics.drawRect (-2, -2, 4, 4);
		useHandCursor = buttonMode = true;
		addEventListener (MouseEvent.MOUSE_DOWN, startDragMe);
		addEventListener (MouseEvent.MOUSE_UP, stopDragMe);
	}
	public function startDragMe (e:MouseEvent):void {
		startDrag ();
	}
	public function stopDragMe (e:MouseEvent):void {
		stopDrag ();
	}
}

/**
 * Y. Hung, P. Yeh, and D. Harwood, "Passive Ranging to Known Planar Point Sets", 1985
 * (different flavour of same shit)
 */
import flash.display.BitmapData;
import flash.display.Graphics;
import flash.display.Shape;
import flash.geom.Matrix3D;
import flash.geom.Point;
import flash.geom.Vector3D;
class Homography extends Shape {

	public function update (focalLength:Number,
		p0:Point, p1:Point, p2:Point, p3:Point,
		halfWidth:int = 232, halfHeight:int = 232):void {

		// Find diagonals intersection point
		var pc:Point = new Point;

		var a1:Number = p2.y - p0.y;
		var b1:Number = p0.x - p2.x;
		var a2:Number = p3.y - p1.y;
		var b2:Number = p1.x - p3.x;

		var denom:Number = a1 * b2 - a2 * b1;
		if (denom == 0) {
			// something is better than nothing
			pc.x = 0.25 * (p0.x + p1.x + p2.x + p3.x);
			pc.y = 0.25 * (p0.y + p1.y + p2.y + p3.y);
		} else {
			var c1:Number = p2.x * p0.y - p0.x * p2.y;
			var c2:Number = p3.x * p1.y - p1.x * p3.y;
			pc.x = (b1 * c2 - b2 * c1) / denom;
			pc.y = (a2 * c1 - a1 * c2) / denom;
		}

		p0 = p0.subtract (pc);
		p1 = p1.subtract (pc);
		p2 = p2.subtract (pc);
		p3 = p3.subtract (pc);


		// -k0'*Q0 + k1'*Q1 + k2'*Q2 = Q3
		var m:Matrix3D = new Matrix3D;
		var r:Vector.<Number> = m.rawData;
		/* x */ r [0] = -p0.x;        r [4] = p1.x;        r [ 8] = p2.x;
		/* y */ r [1] = -p0.y;        r [5] = p1.y;        r [ 9] = p2.y;
		/* z */ r [2] = -focalLength; r [6] = focalLength; r [10] = focalLength;
		r [15] = 1;
		m.rawData = r;
		m.invert ();
		/*r = m.rawData;
		trace (r [0], r [4], r [ 8]);
		trace (r [1], r [5], r [ 9]);
		trace (r [2], r [6], r [10]);*/
		var ks:Vector3D = m.transformVector (new Vector3D (p3.x, p3.y, focalLength));

		var v0:Vector3D = new Vector3D (p0.x, p0.y, focalLength); v0.scaleBy (ks.x);
		var v1:Vector3D = new Vector3D (p1.x, p1.y, focalLength); v1.scaleBy (ks.y);
		var v2:Vector3D = new Vector3D (p2.x, p2.y, focalLength); v2.scaleBy (ks.z);
		var v3:Vector3D = new Vector3D (p3.x, p3.y, focalLength);

		// find any orthogonal basis in the plane
		var u:Vector3D = new Vector3D (v1.x - v0.x, v1.y - v0.y, v1.z - v0.z);
		var v:Vector3D = new Vector3D (v1.x - v2.x, v1.y - v2.y, v1.z - v2.z);
		var scale:Number = 200 / (u.length + v.length);
		var w:Vector3D = u.crossProduct (v); v = w.crossProduct (u);
		u.normalize (); v.normalize ();

		w.x = 0.25 * (v0.x + v1.x + v2.x + v3.x);
		w.y = 0.25 * (v0.y + v1.y + v2.y + v3.y);
		w.z = 0.25 * (v0.z + v1.z + v2.z + v3.z);
		v0 = v0.subtract (w);
		v1 = v1.subtract (w);
		v2 = v2.subtract (w);
		v3 = v3.subtract (w);

		graphics.clear ();
		graphics.beginFill (0xFF0000);
		graphics.drawCircle (
			u.dotProduct (v0) * scale,
			v.dotProduct (v0) * scale,
			5
		);
		1.
		graphics.drawCircle (
			u.dotProduct (v1) * scale,
			v.dotProduct (v1) * scale,
			5
		);
		graphics.drawCircle (
			u.dotProduct (v2) * scale,
			v.dotProduct (v2) * scale,
			5
		);
		graphics.drawCircle (
			u.dotProduct (v3) * scale,
			v.dotProduct (v3) * scale,
			5
		);
	}
}