home *** CD-ROM | disk | FTP | other *** search
/ Chestnut's Multimedia Mania / MM_MANIA.ISO / graphics / povsrc20 / planes.c < prev    next >
C/C++ Source or Header  |  1993-07-29  |  6KB  |  219 lines

  1. /****************************************************************************
  2. *                planes.c
  3. *
  4. *  This module implements functions that manipulate planes.
  5. *
  6. *  from Persistence of Vision Raytracer
  7. *  Copyright 1993 Persistence of Vision Team
  8. *---------------------------------------------------------------------------
  9. *  NOTICE: This source code file is provided so that users may experiment
  10. *  with enhancements to POV-Ray and to port the software to platforms other 
  11. *  than those supported by the POV-Ray Team.  There are strict rules under
  12. *  which you are permitted to use this file.  The rules are in the file
  13. *  named POVLEGAL.DOC which should be distributed with this file. If 
  14. *  POVLEGAL.DOC is not available or for more info please contact the POV-Ray
  15. *  Team Coordinator by leaving a message in CompuServe's Graphics Developer's
  16. *  Forum.  The latest version of POV-Ray may be found there as well.
  17. *
  18. * This program is based on the popular DKB raytracer version 2.12.
  19. * DKBTrace was originally written by David K. Buck.
  20. * DKBTrace Ver 2.0-2.12 were written by David K. Buck & Aaron A. Collins.
  21. *
  22. *****************************************************************************/
  23.  
  24. #include "frame.h"
  25. #include "vector.h"
  26. #include "povproto.h"
  27.  
  28. METHODS Plane_Methods =
  29.   { 
  30.   All_Plane_Intersections,
  31.   Inside_Plane, Plane_Normal,
  32.   Copy_Plane,
  33.   Translate_Plane, Rotate_Plane,
  34.   Scale_Plane, Transform_Plane, Invert_Plane, Destroy_Plane
  35. };
  36.  
  37. extern RAY *CM_Ray;
  38. extern long Ray_Plane_Tests, Ray_Plane_Tests_Succeeded;
  39.  
  40. #ifndef Plane_Tolerance
  41. #define Plane_Tolerance 1.0e-8
  42. #endif
  43.  
  44. int All_Plane_Intersections (Object, Ray, Depth_Stack)
  45. OBJECT *Object;
  46. RAY *Ray;
  47. ISTACK *Depth_Stack;
  48.   {
  49.   DBL Depth;
  50.   VECTOR IPoint;
  51.  
  52.   if (Intersect_Plane (Ray, (PLANE *)Object, &Depth))
  53.     if (Depth > Plane_Tolerance)
  54.       {
  55.       VScale (IPoint, Ray -> Direction, Depth);
  56.       VAddEq (IPoint, Ray -> Initial);
  57.       if (Point_In_Clip (&IPoint, Object->Clip))
  58.         {
  59.         push_entry(Depth,IPoint,Object,Depth_Stack);
  60.         return (TRUE);
  61.         }
  62.       }
  63.   return (FALSE);
  64.   }
  65.  
  66. int Intersect_Plane (Ray, Plane, Depth)
  67. RAY *Ray;
  68. PLANE *Plane;
  69. DBL *Depth;
  70.   {
  71.   DBL NormalDotOrigin, NormalDotDirection;
  72.  
  73.   Ray_Plane_Tests++;
  74.   if (Ray == CM_Ray) 
  75.     {
  76.     VDot (NormalDotDirection, Plane->Normal_Vector, Ray->Direction);
  77.     if ((NormalDotDirection < Plane_Tolerance) &&
  78.       (NormalDotDirection > -Plane_Tolerance))
  79.       return (FALSE);
  80.  
  81.     if (!Plane->CMCached) 
  82.       {
  83.       VDot (Plane->CMNormDotOrigin, Plane->Normal_Vector, Ray->Initial);
  84.       Plane->CMNormDotOrigin += Plane->Distance;
  85.       Plane->CMNormDotOrigin *= -1.0;
  86.       Plane->CMCached = TRUE;
  87.       }
  88.  
  89.     *Depth = Plane->CMNormDotOrigin / NormalDotDirection;
  90.     if ((*Depth >= Plane_Tolerance) && (*Depth <= Max_Distance)) 
  91.       {
  92.       Ray_Plane_Tests_Succeeded++;
  93.       return (TRUE);
  94.       }
  95.     else
  96.       return (FALSE);
  97.     }
  98.   else 
  99.     {
  100.     VDot (NormalDotDirection, Plane->Normal_Vector, Ray->Direction);
  101.     if ((NormalDotDirection < Plane_Tolerance) &&
  102.       (NormalDotDirection > -Plane_Tolerance))
  103.       return (FALSE);
  104.  
  105.     VDot (NormalDotOrigin, Plane->Normal_Vector, Ray->Initial);
  106.     NormalDotOrigin += Plane->Distance;
  107.     NormalDotOrigin *= -1.0;
  108.  
  109.     *Depth = NormalDotOrigin / NormalDotDirection;
  110.     if ((*Depth >= Plane_Tolerance) && (*Depth <= Max_Distance)) 
  111.       {
  112.       Ray_Plane_Tests_Succeeded++;
  113.       return (TRUE);
  114.       }
  115.     else
  116.       return (FALSE);
  117.     }
  118.   }
  119.  
  120. int Inside_Plane (IPoint, Object)
  121. VECTOR *IPoint;
  122. OBJECT *Object;
  123.   {
  124.   DBL Temp;
  125.  
  126.   VDot (Temp, *IPoint, ((PLANE *)Object)->Normal_Vector);
  127.   return ((Temp + ((PLANE *)Object)->Distance) <= Plane_Tolerance);
  128.   }
  129.  
  130. void Plane_Normal (Result, Object, IPoint)
  131. OBJECT *Object;
  132. VECTOR *Result, *IPoint;
  133.   {
  134.   *Result = ((PLANE *)Object)->Normal_Vector;
  135.   }
  136.  
  137. void Translate_Plane (Object, Vector)
  138. OBJECT *Object;
  139. VECTOR *Vector;
  140.   {
  141.   VECTOR Translation;
  142.  
  143.   VEvaluate (Translation, ((PLANE *)Object)->Normal_Vector, *Vector);
  144.   ((PLANE *)Object)->Distance -= Translation.x + Translation.y + Translation.z;
  145.   }
  146.  
  147. void Rotate_Plane (Object, Vector)
  148. OBJECT *Object;
  149. VECTOR *Vector;
  150.   {
  151.   TRANSFORM Trans;
  152.  
  153.   Compute_Rotation_Transform (&Trans, Vector);
  154.   Transform_Plane (Object, &Trans);
  155.   }
  156.  
  157. void Scale_Plane (Object, Vector)
  158. OBJECT *Object;
  159. VECTOR *Vector;
  160.   {
  161.   DBL Length;
  162.  
  163.   PLANE *Plane = (PLANE  *) Object;
  164.  
  165.   VDivEq(Plane->Normal_Vector, *Vector);
  166.  
  167.   VLength(Length, ((PLANE *)Object)->Normal_Vector);
  168.   VScaleEq (((PLANE *)Object)->Normal_Vector, 1.0 / Length);
  169.   ((PLANE *)Object)->Distance /= Length;
  170.   }
  171.  
  172. void Invert_Plane (Object)
  173. OBJECT *Object;
  174.   {
  175.   VScaleEq (((PLANE *) Object)->Normal_Vector, -1.0);
  176.   ((PLANE *) Object)->Distance *= -1.0;
  177.   }
  178.  
  179. PLANE *Create_Plane()
  180.   {
  181.   PLANE *New;
  182.  
  183.   if ((New = (PLANE *) malloc (sizeof (PLANE))) == NULL)
  184.     MAError ("plane");
  185.  
  186.   INIT_OBJECT_FIELDS(New,PLANE_OBJECT,&Plane_Methods)
  187.  
  188.     Make_Vector (&(New -> Normal_Vector), 0.0, 1.0, 0.0);
  189.   New -> Distance = 0.0;
  190.   New -> CMNormDotOrigin = 0.0;
  191.   New -> CMCached = 0;
  192.   return (New);
  193.   }
  194.  
  195. void *Copy_Plane (Object)
  196. OBJECT *Object;
  197.   {
  198.   PLANE *New;
  199.  
  200.   New = Create_Plane ();
  201.   *New = * ((PLANE *)Object);
  202.  
  203.   return (New);
  204.   }
  205.  
  206. void Transform_Plane (Object, Trans)
  207. OBJECT *Object;
  208. TRANSFORM *Trans;
  209.   {
  210.   MTransPoint (&((PLANE *) Object)->Normal_Vector,
  211.     &((PLANE *) Object)->Normal_Vector, Trans);
  212.   }
  213.  
  214. void Destroy_Plane (Object)
  215. OBJECT *Object;
  216.   {
  217.   free (Object);
  218.   }
  219.