PageRenderTime 29ms CodeModel.GetById 20ms app.highlight 5ms RepoModel.GetById 1ms app.codeStats 1ms

/src/structuredlight/projMapToString.as

http://github.com/m0ose/ambient-pixel-action-script
ActionScript | 183 lines | 71 code | 14 blank | 98 comment | 24 complexity | 6a82fcc750f019051b143823829ae820 MD5 | raw file
  1package structuredlight
  2{
  3	import flash.display.BitmapData;
  4	import flash.display.Shape;
  5	import flash.net.FileFilter;
  6	import flash.net.FileReference;
  7
  8	public class projMapToString
  9	{
 10		public var proj_map:ProjectorMap
 11		//private var fr:FileReference
 12		public function projMapToString(pm:ProjectorMap)
 13		{
 14			proj_map = pm;
 15		}
 16
 17		/*
 18		This makes a vertex mesh for use with the PbMesh plugin for Quartz composer by Paul Bourke and Christopher Wright
 19		For detailed information on the quartz patch goto   http://local.wasp.uwa.edu.au/~pbourke/miscellaneous/domemirror/warppatch/
 20		, and information on the mesh format goto   http://local.wasp.uwa.edu.au/~pbourke/dataformats/meshwarp/	
 21		
 22		This function makes a string that represents a rectangular mesh(open GL style regular mesh)
 23		first line: is the type of mesh .  1=planar, 2=fisheye, 3=cylindrical panorama, 4=spherical panorama( for this, put it at two)
 24		second line : Dimensions of mesh. I use 64 x 48 usually( 64 48)
 25		next lines : Represent a node index in the mesh. Go from left to right and up to down. EG. for y in 48 { for x in 64 { ...} }
 26		For Each Line:
 27		vertex coordinates (x,y), texture coordinates (u,v) and an intensity mapping (i).
 28		if there is no coord for that point put u = -1001 v = -1001 and i = -1 
 29		
 30		
 31		here is quick example of what it looks like:
 32		2 
 33		65 49
 34		0.96875 1 1002 1002 -1
 35		1 1 1002 1002 -1
 36		-1 0.9583333333333334 1002 1002 -1
 37		-0.96875 0.9583333333333334 0.7359375 0.55125 1
 38		-0.9375 0.9583333333333334 0.72890625 0.5525 1
 39		-0.90625 0.9583333333333334 0.72265625 0.55375 1
 40		-0.875 0.9583333333333334 0.71640625 0.55375 1
 41		-0.84375 0.9583333333333334 0.7109375 0.55625 1
 42		... and so and and so forth ...
 43		*/ 
 44		/*public function MakeUVMap4Quartz( xdivisions:int = 64, ydivisions:int = 48):String
 45		{
 46			var result:String = "2 \n";
 47			// todo figure out resolution automatically
 48			//var xdivisions:int = 64 ; 
 49			//var ydivisions:int = 48 ;
 50			
 51			result += int(xdivisions + 1) + " " + int(ydivisions + 1) + "\n";
 52			
 53			for( var yn:Number = 0 ; yn <= ydivisions; yn++)
 54			{
 55				for( var xn:Number = 0; xn <= xdivisions ; xn ++  ) 
 56				{
 57					var x:Number = 2 * ( xn / xdivisions ) - 1 ;
 58					var y:Number = 2 * ( yn / ydivisions ) - 1;
 59					var xm:int = Math.floor( proj_map.width()  * (x+1)/2 );
 60					var ym:int = Math.floor( proj_map.height() * (y+1)/2 );
 61					var u:Number = proj_map.getProjXY( xm, ym ).x;
 62					var v:Number = proj_map.getProjXY( xm, ym ).y;
 63					var i:Number = 1 ; 
 64					if( u >=0 && v >=0 )
 65					{
 66						u =  u / proj_map.cam_map._screen_width ;
 67						v =  v / proj_map.cam_map._screen_height ;
 68					}
 69					else
 70					{
 71						u = v = -1000;
 72						i = -1 ;
 73					}
 74					
 75					//
 76					// need to put opacity for boarders
 77					//
 78				
 79					
 80					
 81					//the output for the quartz PBMesh plugin is inverted. 
 82					//result += x + " " + y + " " + u + " " + v + " " + i + "\n";
 83					// the repaired version is below
 84					//result += x + " " + -1 * y + " " + ( u).toString() + " " + (1-v).toString() + " " + i + "\n";
 85					result += x + " " + -1 * y + " " + ( u).toString() + " " + (1-v).toString() + " " + i + "\n";
 86
 87					
 88					
 89				}
 90			}
 91			return result;
 92		}
 93		*/
 94	
 95		//
 96		//
 97		//  this version of the functions should handle the borders better
 98		//
 99			
100		public function MakeUVMap4Quartz( xdivisions:int = 64, ydivisions:int = 48):String
101		{
102			var result:String = "2 \n";
103			// todo figure out resolution automatically
104			//var xdivisions:int = 64 ; 
105			//var ydivisions:int = 48 ;
106			
107			
108			//
109			//  put all nodes into an array 
110			//
111			result += int(xdivisions + 1) + " " + int(ydivisions + 1) + "\n";
112			var nodes:Array = new Array( ydivisions +1);
113			for( var yn:Number = 0 ; yn <= ydivisions; yn++)
114			{
115				nodes[ yn] = new Array( xdivisions + 1);
116			}
117			
118			for( var yn:Number = 0 ; yn <= ydivisions; yn++)
119			{
120				for( var xn:Number = 0; xn <= xdivisions ; xn ++  ) 
121				{
122					var x:Number = 2 * ( xn / xdivisions ) - 1 ;
123					var y:Number = 2 * ( yn / ydivisions ) - 1;
124					var xm:int = Math.floor( proj_map.width()  * (x+1)/2 );
125					var ym:int = Math.floor( proj_map.height() * (y+1)/2 );
126					var u:Number = proj_map.getProjXY( xm, ym ).x;
127					var v:Number = proj_map.getProjXY( xm, ym ).y;
128					var i:Number = 1 ; 
129					if( u >=0 && v >=0 )
130					{
131						u =  u / proj_map.cam_map._screen_width ;
132						v =  v / proj_map.cam_map._screen_height ;
133					}
134					else
135					{
136						u = v = -1000;
137						i = -1 ;
138					}
139					//push an object on
140					nodes[yn][xn] = { x:x , y:y , u:u , v:v ,i:i}; 
141
142					
143				}
144			}
145			
146			for( var yn:Number = 0 ; yn <= ydivisions; yn++)
147			{
148				for( var xn:Number = 0; xn <= xdivisions ; xn ++  ) 
149				{
150					var tmpObj:Object = nodes[yn][xn];
151					//
152					//
153					//  handle the border issues 
154					//       make all borders translucent
155					//       
156					//    
157					var i = Number(tmpObj.i);
158					if( xn == 0 || yn == 0)
159						i = 0;
160					else if( xn == xdivisions || yn == ydivisions)
161						i = 0;
162					else if( nodes[yn][xn-1].i <= 0 )
163						i = 0;
164					else if( nodes[yn][xn+1].i <= 0 )
165						i = 0;
166					else if( nodes[yn-1][xn].i <= 0 )
167						i = 0;
168					else if( nodes[yn+1][xn].i <= 0 )
169						i = 0;
170					
171					
172					//
173					//  finally outputto the final string
174					//
175					result += tmpObj.x + " " + -1 * tmpObj.y + " " + ( tmpObj.u ).toString() + " " + (1- tmpObj.v ).toString() + " " + i + "\n";					
176				}
177			}
178			return result;
179		}
180		
181
182	}
183}