/src/away3d/tools/Align.as

http://github.com/away3d/away3d-core-fp11 · ActionScript · 132 lines · 90 code · 20 blank · 22 comment · 14 complexity · c58cbe5ff3432e196e2be1550466af73 MD5 · raw file

  1. package away3d.tools
  2. {
  3. import away3d.arcane;
  4. use namespace arcane;
  5. /**
  6. * Class Aligns an arrays of Object3Ds, Vector3D's or Vertexes compaired to each other.<code>Align</code>
  7. */
  8. public class Align {
  9. private static var _axis:String;
  10. private static var _condition:String;
  11. /**
  12. * Applies to array elements the alignment according to axis, x, y or z and a condition.
  13. * each element must have public x,y and z properties
  14. * String condition:
  15. * "+" align to highest value on a given axis
  16. * "-" align to lowest value on a given axis
  17. * "" align to a given axis on 0; This is the default.
  18. * "av" align to average of all values on a given axis
  19. *
  20. * @param aObjs Array. An array with elements with x,y and z public properties such as Mesh, Object3D, ObjectContainer3D,Vector3D or Vertex
  21. * @param axis String. Represent the axis to align on.
  22. * @param condition [optional]. String. Can be '+", "-", "av" or "", Default is "", aligns to given axis at 0.
  23. */
  24. public static function align(aObjs:Array, axis:String, condition:String = ""):void
  25. {
  26. checkAxis(axis);
  27. checkCondition(condition);
  28. var base:Number;
  29. switch(_condition){
  30. case "+":
  31. base = getMax(aObjs, _axis);
  32. break;
  33. case "-":
  34. base = getMin(aObjs, _axis);
  35. break;
  36. case "av":
  37. base = getAverage(aObjs, _axis);
  38. break;
  39. case "":
  40. base = 0;
  41. }
  42. for(var i:uint = 0;i<aObjs.length;++i){
  43. aObjs[i][_axis] = base;
  44. }
  45. }
  46. /**
  47. * Applies to array elements a distributed alignment according to axis, x,y or z.
  48. * each element must have public x,y and z properties
  49. * @param aObjs Array. An array with elements with x,y and z public properties such as Mesh, Object3D, ObjectContainer3D,Vector3D or Vertex
  50. * @param axis String. Represent the axis to align on.
  51. */
  52. public static function distribute(aObjs:Array, axis:String):void
  53. {
  54. checkAxis(axis);
  55. var max:Number = getMax(aObjs, _axis);
  56. var min:Number = getMin(aObjs, _axis);
  57. var unit:Number = (max - min) / aObjs.length;
  58. aObjs.sortOn(axis, 16);
  59. var step:Number = 0;
  60. for(var i:uint = 0;i<aObjs.length;++i){
  61. aObjs[i][_axis] = min+step;
  62. step+=unit;
  63. }
  64. }
  65. private static function checkAxis(axis:String):void
  66. {
  67. axis = axis.substring(0, 1).toLowerCase();
  68. if(axis == "x" || axis == "y" || axis == "z"){
  69. _axis = axis;
  70. return;
  71. }
  72. throw new Error("Invalid axis: string value must be 'x', 'y' or 'z'");
  73. }
  74. private static function checkCondition(condition:String):void
  75. {
  76. condition = condition.toLowerCase();
  77. var aConds:Array = ["+", "-", "", "av"];
  78. for(var i:uint = 0;i<aConds.length;++i){
  79. if(aConds[i] == condition){
  80. _condition = condition;
  81. return;
  82. }
  83. }
  84. throw new Error("Invalid condition: possible string value are '+', '-', 'av' or '' ");
  85. }
  86. private static function getMin(a:Array, prop:String):Number
  87. {
  88. var min:Number = Infinity;
  89. for(var i:uint = 0;i<a.length;++i){
  90. min = Math.min(a[i][prop], min);
  91. }
  92. return min;
  93. }
  94. private static function getMax(a:Array, prop:String):Number
  95. {
  96. var max:Number = -Infinity;
  97. for(var i:uint = 0;i<a.length;++i){
  98. max = Math.max(a[i][prop], max);
  99. }
  100. return max;
  101. }
  102. private static function getAverage(a:Array, prop:String):Number
  103. {
  104. var av:Number = 0;
  105. var loop:int = a.length;
  106. for(var i:uint = 0;i<loop;++i){
  107. av += a[i][prop];
  108. }
  109. return av/loop;
  110. }
  111. }
  112. }