/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

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