PageRenderTime 26ms CodeModel.GetById 21ms app.highlight 3ms RepoModel.GetById 1ms app.codeStats 0ms

/src/structuredlight/parseMap2.as

http://github.com/m0ose/ambient-pixel-action-script
ActionScript | 170 lines | 118 code | 36 blank | 16 comment | 33 complexity | a099751e8589a14e09e677cea188eb86 MD5 | raw file
  1package structuredlight
  2{
  3	import flash.geom.Point;
  4
  5	public class parseMap2
  6	{
  7	
  8		private var i:int//iterator for parseing
  9		private var txt:String
 10		private var width:int;
 11		private var height:int;
 12		
 13		
 14		
 15		public function parseMap2()
 16		{
 17		}
 18		
 19		
 20		public function camMap2XML( cm:CameraProjecterMap2):String
 21		{ 
 22			var xm:XML = <cammap>			</cammap>;
 23			//xm.appendChild(<scaleX> {cm._scaleX } </scaleX>);
 24			//xm.appendChild(<scaleY> {cm._scaleY } </scaleY>);
 25			xm.appendChild(<width> {cm.width() } </width> );
 26			xm.appendChild(<height> {cm.height() } </height> );
 27			xm.appendChild(<threshold> {cm._threshold} </threshold> );
 28			xm.appendChild(<gray_width> {cm._gray_width} </gray_width> );	
 29			xm.appendChild(<gray_height> {cm._gray_height} </gray_height> );			
 30			xm.appendChild(<cam_width> {cm._cam_width} </cam_width> );
 31			xm.appendChild(<cam_height> {cm._cam_height} </cam_height> );
 32			xm.appendChild(<screen_width> {cm._screen_width} </screen_width> );
 33			xm.appendChild(<screen_height> {cm._screen_height} </screen_height> );
 34			
 35			//var mp:XML = <graymap width={cm.width()} height={cm.height()} > {cm.map}  </graymap> ;
 36			var mp:XML = <graymap> {cm.map}  </graymap> ;
 37
 38			xm.appendChild(mp);
 39			
 40			return xm.toXMLString();
 41		}	
 42		
 43		/*public function projMap2XML( pm:ProjectorMap):String
 44		{ 
 45		//TODO
 46		}
 47		*/
 48		
 49		
 50		
 51		
 52		public function parse(sometext:String):CameraProjecterMap2
 53		{
 54			var xm:XML = XML(sometext);
 55			
 56			var cm:CameraProjecterMap2 = new CameraProjecterMap2();
 57			//if( xm.scaleX)
 58			//	cm._scaleX = int( xm.scaleX.toString() ); 
 59			//if( xm.scaleY)
 60			//	cm._scaleY = int( xm.scaleY.toString() ); 
 61			if( xm.threshold )
 62				cm._threshold = int( xm.threshold.toString() );
 63			if( xm.width)
 64				width = int(xm.width.toString() );
 65			if( xm.height)
 66				height = int( xm.height.toString() );
 67			if( xm.gray_width )
 68				cm._gray_width = int( xm.gray_width.toString() );
 69			if( xm.gray_height )
 70				cm._gray_height = int( xm.gray_height.toString() );
 71			if( xm.cam_width)
 72				cm._cam_width = int( xm.cam_width.toString() );
 73			if( xm.cam_height)
 74				cm._cam_height = int( xm.cam_height.toString() );
 75			if( xm.screen_width)
 76				cm._screen_width = int( xm.screen_width.toString() );
 77			if( xm.screen_height)
 78				cm._screen_height = int( xm.screen_height.toString() );
 79			
 80			
 81			if( xm.graymap )
 82			{
 83			}
 84			else //old style map
 85			{
 86				width = xm.@width;
 87				height = xm.@height;
 88			}
 89			
 90			txt = xm.graymap.toString();
 91			i=0
 92				
 93			cm.map = doArray();
 94			return cm;
 95		}
 96		
 97		
 98		public function doArray():Array
 99		{	
100			var xtmp:int;
101			var ytmp:int;
102			var newMap:Array = new Array( width);
103			
104			for(var x:int=0;x< width;x++)
105			{
106				
107				newMap[x] = new Array( height)
108				for( var y:int=0; y< height;y++)
109				{
110					//example : (x=341, y=330),(x=341, y=338),(x=333, y=341),(x=341, y=341),(x=342, y=309),(x=423, y=330),(x=150, y=330),(x=42, y=74),
111					eatUntill('(')
112					eatUntill('x')
113					xtmp = int(getWord())
114					
115					eatUntill('y')
116					ytmp= int(getWord())
117					//_output.text += " ("+xtmp+","+ytmp+") "+"{"+x+","+y+"}"
118					
119					newMap[x][y] = new Point(xtmp, ytmp)
120				}
121			}
122			//_output.text+="\n" + newMap.toString() + newMap.length.toString() +',' + newMap[0].length
123			
124			return newMap
125		}
126		
127		//string thigs
128		private function eatUntill( char:String):Boolean
129		{
130			while( txt.charAt(i) != char &&  i < txt.length)
131			{
132				i++
133			}
134			if (txt.charAt(i) == char)
135			{
136				i++
137				return true
138			}
139			return false
140			
141		}
142		private function eatWhite( ):void
143		{	
144			while( (txt.charAt(i) == ' ' || txt.charAt(i) == '='  )&&  i < txt.length)
145			{
146				i++
147			}	
148		}
149		private function getWord():String
150		{
151			eatWhite()
152			var res:String=new String()
153			var ch:String = txt.charAt(i)
154			while(ch != "=" && ch != "," && ch != ";" && ch !="\n" && ch !=" " && ch !='>' && ch != '<'  && ch != ')' && i < txt.length)
155			{
156				res += ch
157				i++
158					ch = txt.charAt(i)
159			}
160			return res.toLowerCase()
161		}
162		
163		
164		
165		
166		
167		
168		
169	}
170}