home *** CD-ROM | disk | FTP | other *** search
/ The Fred Fish Collection 1.5 / ffcollection-1-5-1992-11.iso / ff_disks / 300-399 / ff397.lzh / DKBTrace / DKBSource.LZH / planes.c < prev    next >
C/C++ Source or Header  |  1990-08-26  |  7KB  |  225 lines

  1. /*****************************************************************************
  2. *
  3. *                                    planes.c
  4. *
  5. *   from DKBTrace (c) 1990  David Buck
  6. *
  7. *  This module implements functions that manipulate planes.
  8. *
  9. * This software is freely distributable. The source and/or object code may be
  10. * copied or uploaded to communications services so long as this notice remains
  11. * at the top of each file.  If any changes are made to the program, you must
  12. * clearly indicate in the documentation and in the programs startup message
  13. * who it was who made the changes. The documentation should also describe what
  14. * those changes were. This software may not be included in whole or in
  15. * part into any commercial package without the express written consent of the
  16. * author.  It may, however, be included in other public domain or freely
  17. * distributed software so long as the proper credit for the software is given.
  18. *
  19. * This software is provided as is without any guarantees or warranty. Although
  20. * the author has attempted to find and correct any bugs in the software, he
  21. * is not responsible for any damage caused by the use of the software.  The
  22. * author is under no obligation to provide service, corrections, or upgrades
  23. * to this package.
  24. *
  25. * Despite all the legal stuff above, if you do find bugs, I would like to hear
  26. * about them.  Also, if you have any comments or questions, you may contact me
  27. * at the following address:
  28. *
  29. *     David Buck
  30. *     22C Sonnet Cres.
  31. *     Nepean Ontario
  32. *     Canada, K2H 8W7
  33. *
  34. *  I can also be reached on the following bulleton boards:
  35. *
  36. *     ATX              (613) 526-4141
  37. *     OMX              (613) 731-3419
  38. *     Mystic           (613) 731-0088 or (613) 731-6698
  39. *
  40. *  Fidonet:   1:163/109.9
  41. *  Internet:  David_Buck@Carleton.CA
  42. *
  43. *  IBM Port by Aaron A. Collins. Aaron may be reached on the following BBS'es:
  44. *
  45. *     Lattice BBS                      (708) 916-1200
  46. *     The Information Exchange BBS     (708) 945-5575
  47. *     Stillwaters BBS                  (708) 403-2826
  48. *
  49. *****************************************************************************/
  50.  
  51.  
  52. #include "frame.h"
  53. #include "vector.h"
  54. #include "dkbproto.h"
  55.  
  56. METHODS Plane_Methods =
  57.    { Object_Intersect, All_Plane_Intersections,
  58.      Inside_Plane, Plane_Normal,
  59.      Copy_Plane,
  60.      Translate_Plane, Rotate_Plane,
  61.      Scale_Plane, Invert_Plane};
  62.  
  63. #define Small_Tolerance 0.001
  64.  
  65. extern PLANE *Get_Plane_Shape();
  66.  
  67. extern RAY *VP_Ray;
  68. extern long Ray_Plane_Tests, Ray_Plane_Tests_Succeeded;
  69.  
  70. int All_Plane_Intersections (Object, Ray, Depth_Queue)
  71.    OBJECT *Object;
  72.    RAY *Ray;
  73.    PRIOQ *Depth_Queue;
  74.    {
  75.    PLANE *Shape = (PLANE *) Object;
  76.    DBL Depth;
  77.    VECTOR Intersection_Point;
  78.    INTERSECTION Local_Element;
  79.  
  80.    if (Intersect_Plane (Ray, Shape, &Depth))
  81.       if (Depth > Small_Tolerance)
  82.          {
  83.          Local_Element.Depth = Depth;
  84.          Local_Element.Object = Shape -> Parent_Object;
  85.          VScale (Intersection_Point, Ray -> Direction, Depth);
  86.          VAdd (Intersection_Point, Intersection_Point, Ray -> Initial);
  87.          Local_Element.Point = Intersection_Point;
  88.          Local_Element.Shape = (SHAPE *)Shape;
  89.          pq_add (Depth_Queue, &Local_Element);
  90.          return (TRUE);
  91.          }
  92.  
  93.    return (FALSE);
  94.    }
  95.  
  96. int Intersect_Plane (Ray, Plane, Depth)
  97.    RAY *Ray;
  98.    PLANE *Plane;
  99.    DBL *Depth;
  100.    {
  101.    DBL NormalDotOrigin, NormalDotDirection;
  102.  
  103.    Ray_Plane_Tests++;
  104.    if (Ray == VP_Ray) {
  105.       if (!Plane->VPCached) {
  106.          VDot (Plane->VPNormDotOrigin, Plane->Normal_Vector, Ray->Initial);
  107.          Plane->VPNormDotOrigin += Plane->Distance;
  108.          Plane->VPNormDotOrigin *= -1.0;
  109.          Plane->VPCached = TRUE;
  110.          }
  111.  
  112.       VDot (NormalDotDirection, Plane->Normal_Vector, Ray->Direction);
  113.       if ((NormalDotDirection < Small_Tolerance) &&
  114.           (NormalDotDirection > -Small_Tolerance))
  115.          return (FALSE);
  116.  
  117.       *Depth = Plane->VPNormDotOrigin / NormalDotDirection;
  118.       if ((*Depth >= Small_Tolerance) || (*Depth <= Max_Distance)) {
  119.          Ray_Plane_Tests_Succeeded++;
  120.          return (TRUE);
  121.          }
  122.       else
  123.          return (FALSE);
  124.       }
  125.    else {
  126.       VDot (NormalDotOrigin, Plane->Normal_Vector, Ray->Initial);
  127.       NormalDotOrigin += Plane->Distance;
  128.       NormalDotOrigin *= -1.0;
  129.  
  130.       VDot (NormalDotDirection, Plane->Normal_Vector, Ray->Direction);
  131.       if ((NormalDotDirection < Small_Tolerance) &&
  132.           (NormalDotDirection > -Small_Tolerance))
  133.             return (FALSE);
  134.  
  135.       *Depth = NormalDotOrigin / NormalDotDirection;
  136.       if ((*Depth >= Small_Tolerance) || (*Depth <= Max_Distance)) {
  137.          Ray_Plane_Tests_Succeeded++;
  138.          return (TRUE);
  139.          }
  140.       else
  141.          return (FALSE);
  142.       }
  143.    }
  144.  
  145. int Inside_Plane (Point, Object)
  146.    VECTOR *Point;
  147.    OBJECT *Object;
  148.    {
  149.    PLANE *Plane = (PLANE *) Object;
  150.    DBL Temp;
  151.  
  152.    VDot (Temp, *Point, Plane->Normal_Vector);
  153.    return ((Temp + Plane->Distance) <= Small_Tolerance);
  154.    }
  155.  
  156. void Plane_Normal (Result, Object, Intersection_Point)
  157.    OBJECT *Object;
  158.    VECTOR *Result, *Intersection_Point;
  159.    {
  160.    PLANE *Plane = (PLANE *) Object;
  161.  
  162.    *Result = Plane->Normal_Vector;
  163.    Perturb_Normal (Result, Plane->Parent_Object,
  164.                    Intersection_Point, Result);
  165.    }
  166.  
  167. void *Copy_Plane (Object)
  168.    OBJECT *Object;
  169.    {
  170.    PLANE *New_Shape;
  171.  
  172.    New_Shape = Get_Plane_Shape ();
  173.    *New_Shape = * ((PLANE *)Object);
  174.    New_Shape -> Next_Object = NULL;
  175.    return (New_Shape);
  176.    }
  177.  
  178. void Translate_Plane (Object, Vector)
  179.    OBJECT *Object;
  180.    VECTOR *Vector;
  181.    {
  182.    PLANE *Plane = (PLANE *) Object;
  183.    VECTOR Translation;
  184.  
  185.    VEvaluate (Translation, Plane->Normal_Vector, *Vector);
  186.    Plane->Distance -= Translation.x + Translation.y + Translation.z;
  187.    }
  188.  
  189. void Rotate_Plane (Object, Vector)
  190.    OBJECT *Object;
  191.    VECTOR *Vector;
  192.    {
  193.    TRANSFORMATION Transformation;
  194.  
  195.    Get_Rotation_Transformation (&Transformation, Vector);
  196.    MTransformVector (&((PLANE *) Object)->Normal_Vector,
  197.                      &((PLANE *) Object)->Normal_Vector, &Transformation);
  198.    }
  199.  
  200. void Scale_Plane (Object, Vector)
  201.    OBJECT *Object;
  202.    VECTOR *Vector;
  203.    {
  204.    DBL Length;
  205.    PLANE *Plane = (PLANE  *) Object;
  206.  
  207.    Plane->Normal_Vector.x = Plane->Normal_Vector.x / Vector->x;
  208.    Plane->Normal_Vector.y = Plane->Normal_Vector.y / Vector->y;
  209.    Plane->Normal_Vector.z = Plane->Normal_Vector.z / Vector->z;
  210.  
  211.    VLength(Length, Plane->Normal_Vector);
  212.    VScale (Plane->Normal_Vector, Plane->Normal_Vector, 1.0 / Length);
  213.    Plane->Distance /= Length;
  214.    }
  215.  
  216. void Invert_Plane (Object)
  217.    OBJECT *Object;
  218.    {
  219.    PLANE *Plane = (PLANE  *) Object;
  220.  
  221.    VScale (Plane->Normal_Vector, Plane->Normal_Vector, -1.0);
  222.    Plane->Distance *= -1.0;
  223.    }
  224.  
  225.