ClarkeRubber

3D imaging - depth of field

Nov 12th, 2011
115
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 5.04 KB | None | 0 0
  1. <?php
  2. function minAngle($line, $p, $threshold, $focus, $depth){
  3.     //---Find the side lengths of the triangle created by the point and the line
  4.     $b = pow($line['x'][1]-$line['x'][2], 2)+pow($line['y'][1]-$line['y'][2], 2)+pow($line['z'][1]-$line['z'][2], 2); //Difference between the two points of the line
  5.     $c = pow($line['x'][1]-$p['x'], 2)+pow($line['y'][1]-$p['y'], 2)+pow($line['z'][1]-$p['z'], 2); //Difference between the start of the line and the point
  6.     $a = pow($p['x']-$line['x'][2], 2)+pow($p['y']-$line['y'][2], 2)+pow($p['z']-$line['z'][2], 2); //difference between the end of the line and the point
  7.     //---Find the angle formed between the ray and the point
  8.     $angle = rad2deg(acos(($b+$c-$a)/(2*sqrt($b)*sqrt($c)))); //cos^(-1)((b^2 + c^2 - a^2) / 2bc), will give the angle between the point an the ray.
  9.    
  10.     //---Find out how out of focus the point is
  11.     $dif = intval(abs($p['y']-$focus)/$depth);
  12.     if($dif == 0){
  13.         $dif = 1;
  14.     }
  15.     if($angle < $threshold*$dif){
  16.         //---Depending on the angle formed and the level of focus, return the brightness of the ray/pixel.
  17.         $return = str_pad(intval((($threshold*$dif-$angle)/($threshold*$dif))*255), 3, "0", STR_PAD_LEFT);
  18.         echo "test = $return</br>\n";
  19.         return $return;
  20.     }else{
  21.         return '000';
  22.     }
  23. }
  24.  
  25. //---Variables
  26. $screen_dimensions = array('height' => 300, 'width' => 700); //Do not make this very large as too much memory/time will be used and the program will fail.
  27. $screen_position = array('x' => 0, 'y' => 0, 'z' => 0); //I spose this could be changed, but I haven't bothered with it.
  28.  
  29. $screen_viewingAngle = 140; //In degrees
  30. $focus = (0.5*$screen_dimensions['width'])/tan(deg2rad(0.5*$screen_viewingAngle)); //The Y frame which will be in focus.
  31. $depth_of_field = 30; //This is kinda like the F-stop on a lens.
  32. $angular_threshold = 2; //This is kinda different, but it's basically the accuracy of the imaging. 0 would be perfect, but would not work.
  33. /* Do not use these yet.
  34. $screen_yaw = 0;
  35. $screen_pitch = 0;
  36. $screen_roll = 0;
  37. */
  38.  
  39. /* BEGIN MAIN PROGRAM */
  40.  
  41. //---Pre-processing variables (inititators of a sort)
  42. $offset['y'] = (0.5*$screen_dimensions['width'])/tan(deg2rad(0.5*$screen_viewingAngle));
  43. $offset['x'] = 0.5*$screen_dimensions['width'];
  44. $offset['z'] = 0.5*$screen_dimensions['height'];
  45.  
  46. $current['z'] = $offset['z'];
  47. $current['x'] = -$offset['x'];
  48. $cycle = 0;
  49.  
  50. //---Build points to be displayed
  51. for($i = 0; $i < 36; $i++){
  52.     if($i > 0){
  53.         $current['x'] += $screen_dimensions['width']/5;
  54.     }
  55.     if($current['x'] > $offset['x']){
  56.         $current['x'] = -$offset['x'];
  57.         $current['z'] -= $screen_dimensions['height']/5;
  58.     }
  59.    
  60.     $points[$i]['x'] = $current['x'];
  61.     $points[$i]['y'] = $offset['y'];
  62.     $points[$i]['z'] = $current['z'];
  63.     print "Z = ".$points[$i]['z'] ." Y = ".$points[$i]['y'] ." X = ".$points[$i]['x'] ."</br>\n";
  64. }
  65. //---Copy these points back a few times
  66. foreach($points as $key => $value){
  67.     $points[$key+36]['x'] = $value['x'];
  68.     $points[$key+36]['z'] = $value['z'];
  69.     $points[$key+36]['y'] = $value['y']+40;
  70.     $points[$key+72]['x'] = $value['x'];
  71.     $points[$key+72]['z'] = $value['z'];
  72.     $points[$key+72]['y'] = $value['y']+80;
  73.     $points[$key+108]['x'] = $value['x'];
  74.     $points[$key+108]['z'] = $value['z'];
  75.     $points[$key+108]['y'] = $value['y']+120;
  76. }
  77.  
  78. //---Reset '$current' variables
  79. $current['z'] = 0.5*$screen_dimensions['height'];
  80. $current['x'] = -0.5*$screen_dimensions['width'];
  81.  
  82. //---Begin writing file
  83. unlink("image.ppm"); //file that we will be editing, we are are deleting the file so we have a clean file to work on
  84. $fh = fopen("image.ppm", 'a+'); //creates the image file that we will be editting and opens the file for appending
  85.  
  86. $print_height = 1+$screen_dimensions['height'];
  87. $print_width = 1+$screen_dimensions['width'];
  88.  
  89. $startWrite = "P3\n# image.ppm\n$print_width $print_height\n255"; //fomatting the ppm file
  90. fwrite($fh, $startWrite);
  91.  
  92. //---Begin building rays and printing the file
  93. //These two steps are done at the same time for efficiency, and memory
  94. for($i = 0; $i < ($print_height*$print_width); $i++){
  95.     //---Origin points
  96.     $lines['x'][1] = $screen_position['x'];
  97.     $lines['y'][1] = $screen_position['y'];
  98.     $lines['z'][1] = $screen_position['z'];
  99.  
  100.     //---Resolve when to move to the next row of pixels
  101.     if($i > 0){
  102.         $current['x']++;
  103.     }
  104.     if($current['x'] > $offset['x']){
  105.         $current['x'] = -$offset['x'];
  106.         $current['z']--;
  107.     }
  108.     //---To points
  109.     $lines['x'][2] = $current['x'];
  110.     $lines['y'][2] = $offset['y'];
  111.     $lines['z'][2] = $current['z'];
  112.  
  113.     echo $lines['x'][2].", ".$lines['z'][2].", ".$lines['y'][2]."</br> \n";
  114.     if($i%5 == 0){ //basic formatting decides when a new line must be placed
  115.         $Nl = " \n";
  116.     }else{
  117.         $Nl = " ";
  118.     }
  119.     $out = 000;
  120.     foreach($points as $value){
  121.         $colour = minAngle($lines, $value, $angular_threshold, $focus, $depth_of_field);
  122.         if($colour == 255){
  123.             $out = $colour;
  124.             echo "white </br>\n";
  125.             break 1;
  126.         }elseif($colour >= 0 && $colour >= $out){
  127.             $out = $colour;
  128.         }
  129.     }
  130.     $write = "$Nl$out $out $out"; //the line that is to be outputted
  131.     fwrite($fh, $write); //write the pixel to the file.
  132. }
  133. fclose($fh);
  134. ?>
  135.  
Advertisement
Add Comment
Please, Sign In to add comment