home *** CD-ROM | disk | FTP | other *** search
/ Fish 'n' More 2 / fishmore-publicdomainlibraryvol.ii1991xetec.iso / dirs / dkbtrace_397.lzh / DKBTrace / DKBSource.LZH / objects.c < prev    next >
C/C++ Source or Header  |  1990-08-26  |  12KB  |  411 lines

  1. /*****************************************************************************
  2. *
  3. *                                    objects.c
  4. *
  5. *   from DKBTrace (c) 1990  David Buck
  6. *
  7. *  This module implements the methods for objects and composite objects.
  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. extern RAY *VP_Ray;
  57. extern long Bounding_Region_Tests, Bounding_Region_Tests_Succeeded;
  58.  
  59. METHODS Composite_Methods =
  60.    { Object_Intersect, All_Composite_Intersections,
  61.      Inside_Composite_Object, NULL,
  62.      Copy_Composite_Object,
  63.      Translate_Composite_Object, Rotate_Composite_Object,
  64.      Scale_Composite_Object, Invert_Composite_Object};
  65.  
  66. METHODS Basic_Object_Methods =
  67.    { Object_Intersect, All_Object_Intersections,
  68.      Inside_Basic_Object, NULL,
  69.      Copy_Basic_Object,
  70.      Translate_Basic_Object, Rotate_Basic_Object,
  71.      Scale_Basic_Object, Invert_Basic_Object};
  72.  
  73.  
  74. INTERSECTION *Object_Intersect (Object, Ray)
  75.    OBJECT *Object;
  76.    RAY *Ray;
  77.    {
  78.    INTERSECTION *Local_Intersection, *Queue_Element;
  79.    PRIOQ *Depth_Queue;
  80.  
  81.    Depth_Queue = pq_new (128);
  82.  
  83.    if ((All_Intersections (Object, Ray, Depth_Queue))
  84.       && ((Queue_Element = pq_get_highest (Depth_Queue)) != NULL))
  85.       {
  86.       Local_Intersection = (INTERSECTION *) malloc(sizeof(INTERSECTION));
  87.       Local_Intersection -> Point = Queue_Element -> Point;
  88.       Local_Intersection -> Shape = Queue_Element -> Shape;
  89.       Local_Intersection -> Depth = Queue_Element -> Depth;
  90.       Local_Intersection -> Object = Queue_Element -> Object;
  91.       pq_free (Depth_Queue);
  92.       return (Local_Intersection);
  93.       }
  94.    else
  95.       {
  96.       pq_free (Depth_Queue);
  97.       return (NULL);
  98.       }
  99.    }
  100.  
  101. int All_Composite_Intersections (Object, Ray, Depth_Queue)
  102.    OBJECT *Object;
  103.    RAY *Ray;
  104.    PRIOQ *Depth_Queue;
  105.    {
  106.    register int Intersection_Found;
  107.    SHAPE *Bounding_Shape;
  108.    INTERSECTION *Local_Intersection;
  109.    OBJECT *Local_Object;
  110.  
  111.    for (Bounding_Shape = ((COMPOSITE *) Object) -> Bounding_Shapes ;
  112.         Bounding_Shape != NULL ;
  113.         Bounding_Shape = Bounding_Shape -> Next_Object) {
  114.  
  115.      Bounding_Region_Tests++;
  116.      if ((Local_Intersection = Intersection ((OBJECT *) Bounding_Shape, Ray)) != NULL)
  117.         free (Local_Intersection);
  118.      else
  119.         if (!Inside (&Ray -> Initial, (OBJECT *) Bounding_Shape))
  120.            return (FALSE);
  121.      Bounding_Region_Tests_Succeeded++;
  122.      }
  123.  
  124.    Intersection_Found = FALSE;
  125.    for (Local_Object = ((COMPOSITE *) Object) -> Objects ;
  126.         Local_Object != NULL ;
  127.         Local_Object = Local_Object -> Next_Object)
  128.  
  129.       if (All_Intersections (Local_Object, Ray, Depth_Queue))
  130.          Intersection_Found = TRUE;
  131.  
  132.    return (Intersection_Found);
  133.    }
  134.  
  135. int All_Object_Intersections (Object, Ray, Depth_Queue)
  136.    OBJECT *Object;
  137.    RAY *Ray;
  138.    PRIOQ *Depth_Queue;
  139.    {
  140.    INTERSECTION *Local_Intersection;
  141.    SHAPE *Bounding_Shape;
  142.  
  143.    for (Bounding_Shape = Object -> Bounding_Shapes ;
  144.         Bounding_Shape != NULL ;
  145.         Bounding_Shape = Bounding_Shape -> Next_Object) {
  146.  
  147.       Bounding_Region_Tests++;
  148.       if ((Local_Intersection = Intersection ((OBJECT *) Bounding_Shape, Ray)) != NULL)
  149.          free (Local_Intersection);
  150.       else
  151.          if (!Inside (&Ray -> Initial, (OBJECT *) Bounding_Shape))
  152.             return (FALSE);
  153.       Bounding_Region_Tests_Succeeded++;
  154.       }
  155.  
  156.    All_Intersections ((OBJECT *) Object -> Shape, Ray, Depth_Queue);
  157.  
  158.    if (pq_is_empty (Depth_Queue))
  159.      return (FALSE);
  160.    return (TRUE);
  161.    }
  162.  
  163.  
  164. int Inside_Basic_Object (Point, Object)
  165.    VECTOR *Point;
  166.    OBJECT *Object;
  167.    {
  168.    SHAPE *Bounding_Shape;
  169.  
  170.    for (Bounding_Shape = Object -> Bounding_Shapes ;
  171.         Bounding_Shape != NULL ;
  172.         Bounding_Shape = Bounding_Shape -> Next_Object)
  173.  
  174.       if (!Inside (Point, (OBJECT *) Bounding_Shape))
  175.          return (FALSE);
  176.  
  177.    if (Inside (Point, (OBJECT *) Object -> Shape))
  178.       return (TRUE);
  179.    return (FALSE);
  180.    }
  181.  
  182. int Inside_Composite_Object (Point, Object)
  183.    VECTOR *Point;
  184.    OBJECT *Object;
  185.    {
  186.    SHAPE *Bounding_Shape;
  187.    OBJECT *Local_Object;
  188.  
  189.    for (Bounding_Shape = ((COMPOSITE *) Object) -> Bounding_Shapes ;
  190.         Bounding_Shape != NULL ;
  191.         Bounding_Shape = Bounding_Shape -> Next_Object)
  192.  
  193.      if (!Inside (Point, (OBJECT *) Bounding_Shape))
  194.        return (FALSE);
  195.  
  196.    for (Local_Object = ((COMPOSITE *) Object) -> Objects ;
  197.         Local_Object != NULL ;
  198.         Local_Object = Local_Object -> Next_Object)
  199.  
  200.       if (Inside (Point, Local_Object))
  201.          return (TRUE);
  202.  
  203.    return (FALSE);
  204.    }
  205.  
  206. void *Copy_Basic_Object (Object)
  207.    OBJECT *Object;
  208.    {
  209.    SHAPE *Local_Shape, *Copied_Shape;
  210.    OBJECT *New_Object;
  211.  
  212.    New_Object = Get_Object();
  213.    *New_Object = *Object;
  214.    New_Object -> Next_Object = NULL;
  215.    New_Object -> Bounding_Shapes = NULL;
  216.    for (Local_Shape = Object -> Bounding_Shapes ;
  217.         Local_Shape != NULL ;
  218.         Local_Shape = Local_Shape -> Next_Object) {
  219.  
  220.       Copied_Shape = (SHAPE *) Copy((OBJECT *) Local_Shape);
  221.       Link ((OBJECT *) Copied_Shape,
  222.             (OBJECT **) &(Copied_Shape -> Next_Object),
  223.             (OBJECT **) &(New_Object -> Bounding_Shapes));
  224.       Copied_Shape -> Parent_Object = Object;
  225.       }
  226.  
  227.    New_Object -> Shape = (SHAPE *) Copy((OBJECT *) Object -> Shape);
  228.    Object -> Shape -> Parent_Object = Object;
  229.    return (New_Object);
  230.    }
  231.  
  232. void *Copy_Composite_Object (Object)
  233.    OBJECT *Object;
  234.    {
  235.    COMPOSITE *New_Object;
  236.    SHAPE *Local_Shape;
  237.    OBJECT *Local_Object, *Copied_Object;
  238.  
  239.    New_Object = Get_Composite_Object();
  240.    *New_Object = *((COMPOSITE *) Object);
  241.    New_Object -> Next_Object = NULL;
  242.    New_Object -> Objects = NULL;
  243.    for (Local_Object = ((COMPOSITE *) Object) -> Objects;
  244.         Local_Object != NULL ;
  245.         Local_Object = Local_Object -> Next_Object) {
  246.  
  247.       Copied_Object = (OBJECT *) Copy(Local_Object);
  248.       Link (Copied_Object,
  249.             &(Copied_Object -> Next_Object),
  250.             &(New_Object -> Objects));
  251.       }
  252.  
  253.    New_Object -> Bounding_Shapes = NULL;
  254.    for (Local_Shape = ((COMPOSITE *) Object) -> Bounding_Shapes;
  255.         Local_Shape != NULL ;
  256.         Local_Shape = Local_Shape -> Next_Object) {
  257.  
  258.       Copied_Object = (OBJECT *) Copy((OBJECT *) Local_Shape);
  259.       Link (Copied_Object,
  260.             &(Copied_Object -> Next_Object),
  261.             (OBJECT **) &(New_Object -> Bounding_Shapes));
  262.       }
  263.    return (New_Object);
  264.    }
  265.  
  266. void Translate_Basic_Object (Object, Vector)
  267.    OBJECT *Object;
  268.    VECTOR *Vector;
  269.    {
  270.    SHAPE *Local_Shape;
  271.  
  272.    for (Local_Shape = Object -> Bounding_Shapes ;
  273.         Local_Shape != NULL ;
  274.         Local_Shape = Local_Shape -> Next_Object)
  275.  
  276.       Translate ((OBJECT *) Local_Shape, Vector);
  277.  
  278.    Translate ((OBJECT *) Object -> Shape, Vector);
  279.  
  280.    VAdd (Object -> Object_Center, Object -> Object_Center, *Vector);
  281.    }
  282.  
  283. void Rotate_Basic_Object (Object, Vector)
  284.    OBJECT *Object;
  285.    VECTOR *Vector;
  286.    {
  287.    SHAPE *Local_Shape;
  288.    TRANSFORMATION Transformation;
  289.  
  290.    for (Local_Shape = Object -> Bounding_Shapes ;
  291.         Local_Shape != NULL ;
  292.         Local_Shape = Local_Shape -> Next_Object)
  293.  
  294.       Rotate ((OBJECT *) Local_Shape, Vector);
  295.  
  296.    Rotate ((OBJECT *) Object -> Shape, Vector);
  297.    Get_Rotation_Transformation (&Transformation, Vector);
  298.    MTransformVector (&Object->Object_Center,
  299.                      &Object->Object_Center, &Transformation);
  300.    }
  301.  
  302. void Scale_Basic_Object (Object, Vector)
  303.    OBJECT *Object;
  304.    VECTOR *Vector;
  305.    {
  306.    SHAPE *Local_Shape;
  307.  
  308.    for (Local_Shape = Object -> Bounding_Shapes ;
  309.         Local_Shape != NULL ;
  310.         Local_Shape = Local_Shape -> Next_Object)
  311.  
  312.       Scale ((OBJECT *) Local_Shape, Vector);
  313.  
  314.    Scale ((OBJECT *) Object -> Shape, Vector);
  315.  
  316.    VEvaluate (Object -> Object_Center, Object -> Object_Center, *Vector);
  317.    }
  318.  
  319. void Translate_Composite_Object (Object, Vector)
  320.    OBJECT *Object;
  321.    VECTOR *Vector;
  322.    {
  323.    OBJECT *Local_Object;
  324.    SHAPE *Local_Shape;
  325.  
  326.    for (Local_Object = ((COMPOSITE *) Object) -> Objects;
  327.         Local_Object != NULL ;
  328.         Local_Object = Local_Object -> Next_Object)
  329.  
  330.       Translate (Local_Object, Vector);   
  331.  
  332.    for (Local_Shape = ((COMPOSITE *) Object) -> Bounding_Shapes ;
  333.         Local_Shape != NULL ;
  334.         Local_Shape = Local_Shape -> Next_Object)
  335.  
  336.       Translate ((OBJECT *) Local_Shape, Vector);
  337.    }
  338.  
  339. void Rotate_Composite_Object (Object, Vector)
  340.    OBJECT *Object;
  341.    VECTOR *Vector;
  342.    {
  343.    OBJECT *Local_Object;
  344.    SHAPE *Local_Shape;
  345.  
  346.    for (Local_Object = ((COMPOSITE *) Object) -> Objects;
  347.         Local_Object != NULL ;
  348.         Local_Object = Local_Object -> Next_Object)
  349.  
  350.       Rotate (Local_Object, Vector);   
  351.  
  352.    for (Local_Shape = ((COMPOSITE *) Object) -> Bounding_Shapes ;
  353.         Local_Shape != NULL ;
  354.         Local_Shape = Local_Shape -> Next_Object)
  355.  
  356.       Rotate ((OBJECT *) Local_Shape, Vector);
  357.    }
  358.  
  359. void Scale_Composite_Object (Object, Vector)
  360.    OBJECT *Object;
  361.    VECTOR *Vector;
  362.    {
  363.    OBJECT *Local_Object;
  364.    SHAPE *Local_Shape;
  365.  
  366.    for (Local_Object = ((COMPOSITE *) Object) -> Objects;
  367.         Local_Object != NULL ;
  368.         Local_Object = Local_Object -> Next_Object)
  369.  
  370.       Scale (Local_Object, Vector);   
  371.  
  372.    for (Local_Shape = ((COMPOSITE *) Object) -> Bounding_Shapes ;
  373.         Local_Shape != NULL ;
  374.         Local_Shape = Local_Shape -> Next_Object)
  375.  
  376.       Scale ((OBJECT *) Local_Shape, Vector);
  377.    }
  378.  
  379.  
  380. void Invert_Basic_Object (Object)
  381.    OBJECT *Object;
  382.    {
  383.    SHAPE *Local_Shape;
  384.  
  385.    for (Local_Shape = Object -> Bounding_Shapes ;
  386.         Local_Shape != NULL ;
  387.         Local_Shape = Local_Shape -> Next_Object)
  388.  
  389.       Invert ((OBJECT *) Local_Shape);
  390.    Invert ((OBJECT *) Object -> Shape);
  391.    }
  392.  
  393. void Invert_Composite_Object (Object)
  394.    OBJECT *Object;
  395.    {
  396.    OBJECT *Local_Object;
  397.    SHAPE *Local_Shape;
  398.  
  399.    for (Local_Object = ((COMPOSITE *)Object) -> Objects;
  400.         Local_Object != NULL ;
  401.         Local_Object = Local_Object -> Next_Object)
  402.  
  403.       Invert (Local_Object);   
  404.  
  405.    for (Local_Shape = ((COMPOSITE *) Object) -> Bounding_Shapes ;
  406.         Local_Shape != NULL ;
  407.         Local_Shape = Local_Shape -> Next_Object)
  408.  
  409.       Invert ((OBJECT *) Local_Shape);
  410.    }
  411.