home *** CD-ROM | disk | FTP | other *** search
/ Mega Top 1 / os2_top1.zip / os2_top1 / APPS / RAYTRACE / RT / PLANE.C < prev    next >
C/C++ Source or Header  |  1994-05-22  |  6KB  |  246 lines

  1. /*
  2.  
  3. PLANE.C  Half-plane logic
  4.  
  5. Defined solid for :- ax+by+cz+d<=0
  6.  
  7. */
  8.  
  9. /*...sincludes:0:*/
  10. #include <stdio.h>
  11. #include <stdlib.h>
  12. #include <stddef.h>
  13. #include <malloc.h>
  14. #include <memory.h>
  15. #include <math.h>
  16. #include "standard.h"
  17. #include "rt.h"
  18. #include "vector.h"
  19. #include "sil.h"
  20. #define    _PLANE_
  21. #include "plane.h"
  22.  
  23. /*...vrt\46\h:0:*/
  24. /*...vvector\46\h:0:*/
  25. /*...vsil\46\h:0:*/
  26. /*...vplane\46\h:0:*/
  27. /*...e*/
  28.  
  29. /*...screate_plane      \45\ create any general plane:0:*/
  30. PLANE    *create_plane(double a, double b, double c, double d)
  31.     {
  32.     PLANE    *plane;
  33.  
  34.     if ( (plane = malloc(sizeof(PLANE))) == NULL )
  35.         return NULL;
  36.  
  37.     plane->a = a;
  38.     plane->b = b;
  39.     plane->c = c;
  40.     plane->d = d;
  41.     return plane;
  42.     }
  43. /*...e*/
  44. /*...screate_x_lt_plane \45\ create plane solid for x \60\\61\ some value:0:*/
  45. PLANE    *create_x_lt_plane(double x)
  46.     {
  47.     return create_plane(1.0, 0.0, 0.0, -x);
  48.     }
  49. /*...e*/
  50. /*...screate_x_gt_plane \45\ create plane solid for x \62\\61\ some value:0:*/
  51. PLANE    *create_x_gt_plane(double x)
  52.     {
  53.     return create_plane(-1.0, 0.0, 0.0, x);
  54.     }
  55. /*...e*/
  56. /*...screate_y_lt_plane \45\ create plane solid for y \60\\61\ some value:0:*/
  57. PLANE    *create_y_lt_plane(double y)
  58.     {
  59.     return create_plane(0.0, 1.0, 0.0, -y);
  60.     }
  61. /*...e*/
  62. /*...screate_y_gt_plane \45\ create plane solid for y \62\\61\ some value:0:*/
  63. PLANE    *create_y_gt_plane(double y)
  64.     {
  65.     return create_plane(0.0, -1.0, 0.0, y);
  66.     }
  67. /*...e*/
  68. /*...screate_z_lt_plane \45\ create plane solid for z \60\\61\ some value:0:*/
  69. PLANE    *create_z_lt_plane(double z)
  70.     {
  71.     return create_plane(0.0, 0.0, 1.0, -z);
  72.     }
  73. /*...e*/
  74. /*...screate_z_gt_plane \45\ create plane solid for z \62\\61\ some value:0:*/
  75. PLANE    *create_z_gt_plane(double z)
  76.     {
  77.     return create_plane(0.0, 0.0, -1.0, z);
  78.     }
  79. /*...e*/
  80. /*...scopy_plane        \45\ make a copy of a plane:0:*/
  81. PLANE    *copy_plane(PLANE *plane)
  82.     {
  83.     PLANE    *copy;
  84.  
  85.     if ( (copy = malloc(sizeof(PLANE))) == NULL )
  86.         return NULL;
  87.  
  88.     memcpy(copy, plane, sizeof(PLANE));
  89.     return copy;
  90.     }
  91. /*...e*/
  92. /*...sdestroy_plane     \45\ destroy a plane:0:*/
  93. void    destroy_plane(PLANE *plane)
  94.     {
  95.     free(plane);
  96.     }
  97. /*...e*/
  98.  
  99. /*...strans_x_plane     \45\ translate by amount in x direction:0:*/
  100. void    trans_x_plane(PLANE *plane, double t)
  101.     {
  102.     plane->d -= plane->a * t;
  103.     }
  104. /*...e*/
  105. /*...strans_y_plane     \45\ translate by amount in y direction:0:*/
  106. void    trans_y_plane(PLANE *plane, double t)
  107.     {
  108.     plane->d -= plane->b * t;
  109.     }
  110. /*...e*/
  111. /*...strans_z_plane     \45\ translate by amount in z direction:0:*/
  112. void    trans_z_plane(PLANE *plane, double t)
  113.     {
  114.     plane->d -= plane->c * t;
  115.     }
  116. /*...e*/
  117. /*...sscale_x_plane     \45\ scale by factor in x direction:0:*/
  118. void    scale_x_plane(PLANE *plane, double factor)
  119.     {
  120.     plane->a /= factor;
  121.     }
  122. /*...e*/
  123. /*...sscale_y_plane     \45\ scale by factor in y direction:0:*/
  124. void    scale_y_plane(PLANE *plane, double factor)
  125.     {
  126.     plane->b /= factor;
  127.     }
  128. /*...e*/
  129. /*...sscale_z_plane     \45\ scale by factor in z direction:0:*/
  130. void    scale_z_plane(PLANE *plane, double factor)
  131.     {
  132.     plane->c /= factor;
  133.     }
  134. /*...e*/
  135. /*...srot_x_plane       \45\ rotate about x axis by given angle:0:*/
  136. void    rot_x_plane(PLANE *plane, double angle)
  137.     {
  138.     double    b  = plane->b;
  139.     double    c  = plane->c;
  140.     double    ca = cos(angle);
  141.     double    sa = sin(angle);
  142.  
  143.     plane->b = b * ca - c * sa;
  144.     plane->c = b * sa + c * ca;
  145.     }
  146. /*...e*/
  147. /*...srot_y_plane       \45\ rotate about y axis by given angle:0:*/
  148. void    rot_y_plane(PLANE *plane, double angle)
  149.     {
  150.     double    c  = plane->c;
  151.     double    a  = plane->a;
  152.     double    ca = cos(angle);
  153.     double    sa = sin(angle);
  154.  
  155.     plane->c = c * ca - a * sa;
  156.     plane->a = c * sa + a * ca;
  157.     }
  158. /*...e*/
  159. /*...srot_z_plane       \45\ rotate about z axis by given angle:0:*/
  160. void    rot_z_plane(PLANE *plane, double angle)
  161.     {
  162.     double    a  = plane->a;
  163.     double    b  = plane->b;
  164.     double    ca = cos(angle);
  165.     double    sa = sin(angle);
  166.  
  167.     plane->a = a * ca - b * sa;
  168.     plane->b = a * sa + b * ca;
  169.     }
  170. /*...e*/
  171.  
  172. /*...sisects_reqd_plane \45\ max number of isects we will generate:0:*/
  173. int    isects_reqd_plane(PLANE *plane)
  174.     {
  175.     plane=plane; /* Suppress 'unref arg' compiler warning */
  176.  
  177.     return 2;
  178.     }
  179. /*...e*/
  180. /*...sintersect_plane   \45\ determine intersection range of t for plane:0:*/
  181. /*
  182.  
  183. Any point along the line we are interested in is of the form p + tq.
  184.                                  ~    ~
  185. Given:    ax+by+cz+d=0
  186.  
  187. Subs:    x = x +tx        y = y +ty        z = z +tz
  188.          p   q             p   q             p   q
  189.  
  190. Gives:    (ax +by +cz )t + ax +by +cz +d = 0
  191.        q   q   q       p   p   p
  192.  
  193. */
  194.  
  195. /*...svalue_of_plane:0:*/
  196. static double value_of_plane(void *shapeinfo, VECTOR v)
  197.     {
  198.     PLANE    *plane = (PLANE *) shapeinfo;
  199.  
  200.     return plane->a * v.x +
  201.            plane->b * v.y +
  202.            plane->c * v.z +
  203.            plane->d;
  204.     }
  205. /*...e*/
  206.  
  207. void    intersect_plane(PLANE *plane, VECTOR p, VECTOR q, SIL *sil)
  208.     {
  209.     double    a = plane->a;
  210.     double    b = plane->b;
  211.     double    c = plane->c;
  212.     double    d = plane->d;
  213.     double    coeff_of_t    = a * q.x + b * q.y + c * q.z;
  214.     double    constant_term = a * p.x + b * p.y + c * p.z + d;
  215.  
  216.     intersect_linear_t(coeff_of_t, constant_term, p, q,
  217.                (void *) plane, value_of_plane, sil);
  218.     }
  219. /*...e*/
  220. /*...snormal_to_plane   \45\ find normal of surface at a given point:0:*/
  221. /*
  222.  
  223. Use partial derivatives to find normal at a given point.
  224.  
  225. Given:    ax+by+cz+d=0
  226.  
  227. d/dx:    a
  228.  
  229. d/dy:    b
  230.  
  231. d/dz:    c
  232.  
  233. */
  234.  
  235. VECTOR    normal_to_plane(PLANE *plane)
  236.     {
  237.     VECTOR    normal;
  238.  
  239.     normal.x = plane->a;
  240.     normal.y = plane->b;
  241.     normal.z = plane->c;
  242.  
  243.     return normal;
  244.     }
  245. /*...e*/
  246.