ClarkeRubber

3D imaging - warping

Nov 11th, 2011
108
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 3.53 KB | None | 0 0
  1. <?php
  2. function minAngle($line, $p){
  3.     //$l1 and $l2 are the start and end points of a line segment, formatted as (x, y, z). $p is the points that is being tested.
  4.     /*I'm thinking screw it. Instead of finding the minimum distance between the ray and the point. I think that I should focus on whether or not the angle between the ray and the point is satisfactory. For starters, I'll make the angular limit 1 degree.*/
  5.     $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
  6.     $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
  7.     $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
  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.     if($angle < 2){
  10.         return true;
  11.     }else{
  12.         return false;
  13.     }
  14. }
  15.  
  16. $screen_dimensions = array('height' => 700, 'width' => 700);
  17. $screen_position = array('x' => 0, 'y' => 0, 'z' => 0);
  18. $screen_viewingAngle = 150;
  19. /* Do not use these yet.
  20. $screen_yaw = 0;
  21. $screen_pitch = 0;
  22. $screen_roll = 0;
  23. */
  24. /* BEGIN MAIN PROGRAM */
  25. //Build screen rays
  26. $offset['y'] = (0.5*$screen_dimensions['width'])/tan(deg2rad(0.5*$screen_viewingAngle));
  27. $offset['x'] = 0.5*$screen_dimensions['width'];
  28. $offset['z'] = 0.5*$screen_dimensions['height'];
  29.  
  30. $current['z'] = 0.5*$screen_dimensions['height'];
  31. $current['x'] = -0.5*$screen_dimensions['width'];
  32. for($i = 0; $i < 36; $i++){
  33.     if($i > 0){
  34.         $current['x'] += $screen_dimensions['width']/5;
  35.     }
  36.     if($current['x'] > $offset['x']){
  37.         $current['x'] = -$offset['x'];
  38.         $current['z'] -= $screen_dimensions['height']/5;
  39.     }
  40.     print "Z = ".$current['z'] ." X = ".$current['x'] ."</br>\n";
  41.     $points[$i]['x'] = $current['x'];
  42.     $points[$i]['y'] = $offset['y']+10;
  43.     $points[$i]['z'] = $current['z'];
  44. }
  45. $current['z'] = 0.5*$screen_dimensions['height'];
  46. $current['x'] = -0.5*$screen_dimensions['width'];
  47.  
  48. unlink("image.ppm"); //file that we will be editing, we are are deleting the file so we have a clean file to work on
  49. $fh = fopen("image.ppm", 'a+'); //creates the image file that we will be editting and opens the file for appending
  50.  
  51. $print_height = 1+$screen_dimensions['height'];
  52. $print_width = 1+$screen_dimensions['width'];
  53.  
  54. $startWrite = "P3\n# image.ppm\n$print_width $print_height\n255"; //fomatting the ppm file
  55. fwrite($fh, $startWrite);
  56.  
  57. for($i = 0; $i < ($print_height*$print_width); $i++){
  58.     //Origin points
  59.     $lines['x'][1] = $screen_position['x'];
  60.     $lines['y'][1] = $screen_position['y'];
  61.     $lines['z'][1] = $screen_position['z'];
  62.     // \Origin points
  63.     //To points
  64.     if($i > 0){
  65.         $current['x']++;
  66.     }
  67.     if($current['x'] > $offset['x']){
  68.         $current['x'] = -$offset['x'];
  69.         $current['z']--;
  70.     }
  71.     $lines['x'][2] = $current['x'];
  72.     $lines['y'][2] = $offset['y'];
  73.     $lines['z'][2] = $current['z'];
  74.  
  75.     echo $lines['x'][2].", ".$lines['z'][2].", ".$lines['y'][2]."</br> \n";
  76.     if($i%5 == 0){ //basic formatting decides when a new line must be placed
  77.         $Nl = " \n";
  78.     }else{
  79.         $Nl = " ";
  80.     }
  81.     foreach($points as $value){
  82.         if(minAngle($lines, $value)){
  83.             $colour = "255";
  84.             echo "white </br>\n";
  85.             break 1;
  86.         }else{
  87.             $colour = "000";
  88.         }
  89.     }
  90.     $write = "$Nl$colour $colour $colour"; //the line that is to be outputted
  91.     fwrite($fh, $write); //write the pixel to the file.
  92.    
  93. }
  94. fclose($fh);
  95. ?>
  96.  
Advertisement
Add Comment
Please, Sign In to add comment