PageRenderTime 26ms CodeModel.GetById 17ms app.highlight 6ms RepoModel.GetById 1ms app.codeStats 0ms

/src/away3d/extrusions/utils/PathUtils.as

http://github.com/away3d/away3d-core-fp11
ActionScript | 104 lines | 76 code | 25 blank | 3 comment | 8 complexity | 54e280dabdd6ea21dd7785ba7e28d5c9 MD5 | raw file
  1package away3d.extrusions.utils
  2{
  3	import flash.geom.Vector3D;
  4
  5	/**
  6	 * Geometry handlers for classes using Path objects
  7	 */
  8	public class PathUtils{
  9    	 
 10		 public static function step( startVal:Vector3D, endVal:Vector3D, subdivision:int):Vector.<Vector3D> {
 11			var vTween:Vector.<Vector3D> = new Vector.<Vector3D>();
 12			
 13			var stepx:Number =  (endVal.x-startVal.x) / subdivision;
 14			var stepy:Number =  (endVal.y-startVal.y) / subdivision;
 15			var stepz:Number =  (endVal.z-startVal.z) / subdivision;
 16			
 17			var step:int = 1;
 18			var scalestep:Vector3D;
 19			
 20			while (step < subdivision) { 
 21				scalestep = new Vector3D();
 22				scalestep.x = startVal.x+(stepx*step);
 23				scalestep.y = startVal.y+(stepy*step);
 24				scalestep.z = startVal.z+(stepz*step);
 25				vTween.push(scalestep);
 26				
 27				step ++;
 28			}
 29			
 30			vTween.push(endVal);
 31			
 32			return vTween;
 33		}
 34		 
 35		public static function rotatePoint(aPoint:Vector3D, rotation:Vector3D):Vector3D
 36		{
 37			if(rotation.x !=0 || rotation.y != 0 || rotation.z != 0)
 38			{
 39				var x1:Number;
 40				var y1:Number;
 41				
 42				var rad:Number = Math.PI / 180;
 43				var rotx:Number = rotation.x * rad;
 44				var roty:Number = rotation.y * rad;
 45				var rotz:Number = rotation.z * rad;
 46				var sinx:Number = Math.sin(rotx);
 47				var cosx:Number = Math.cos(rotx);
 48				var siny:Number = Math.sin(roty);
 49				var cosy:Number = Math.cos(roty);
 50				var sinz:Number = Math.sin(rotz);
 51				var cosz:Number = Math.cos(rotz);
 52	
 53				var x:Number = aPoint.x;
 54				var y:Number = aPoint.y;
 55				var z:Number = aPoint.z;
 56	
 57				y1 = y;
 58				y = y1*cosx+z*-sinx;
 59				z = y1*sinx+z*cosx;
 60				
 61				x1 = x;
 62				x = x1*cosy+z*siny;
 63				z = x1*-siny+z*cosy;
 64			
 65				x1 = x;
 66				x = x1*cosz+y*-sinz;
 67				y = x1*sinz+y*cosz;
 68	
 69				aPoint.x = x;
 70				aPoint.y = y;
 71				aPoint.z = z;
 72			}
 73			
 74			return aPoint;
 75		}
 76		
 77		public static function getPointsOnCurve(path:Path, subdivision:uint):Vector.<Vector.<Vector3D>> 
 78		{	
 79			var segPts:Vector.<Vector.<Vector3D>> = new Vector.<Vector.<Vector3D>>();
 80			 
 81			for (var i:uint = 0; i < path.length; ++i)
 82				segPts[i] = PathUtils.getSegmentPoints(path.segments[i].pStart, path.segments[i].pControl, path.segments[i].pEnd, subdivision, (i ==path.length-1));
 83			
 84			return segPts;
 85		}
 86		
 87		public static function getSegmentPoints(pStart:Vector3D, pControl:Vector3D, pEnd:Vector3D, n:uint, last:Boolean):Vector.<Vector3D>
 88		{
 89			var aPts:Vector.<Vector3D> = new Vector.<Vector3D>();
 90			
 91			for (var i:uint = 0; i < n+((last)? 1 : 0); ++i)
 92				aPts[i] = PathUtils.getNewPoint(pStart.x, pStart.y, pStart.z, pControl.x, pControl.y, pControl.z, pEnd.x, pEnd.y, pEnd.z, i / n);
 93			
 94			return aPts;
 95		}
 96		
 97		public static function getNewPoint(x0:Number = 0, y0:Number = 0, z0:Number=0, aX:Number = 0, aY:Number = 0, aZ:Number=0, x1:Number = 0, y1:Number = 0, z1:Number=0, t:Number = 0):Vector3D 
 98		{
 99			return new Vector3D(x0 + t * (2 * (1 - t) * (aX - x0) + t * (x1 - x0)), y0 + t * (2 * (1 - t) * (aY - y0) + t * (y1 - y0)), z0 + t * (2 * (1 - t) * (aZ - z0) + t * (z1 - z0)));
100		}
101      
102		
103    }
104}