home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 January / usenetsourcesnewsgroupsinfomagicjanuary1994.iso / sources / unix / volume21 / rayshade / part01 / src / box.c < prev    next >
Encoding:
C/C++ Source or Header  |  1990-03-21  |  3.1 KB  |  122 lines

  1. /*
  2.  * box.c
  3.  *
  4.  * Copyright (C) 1989, Craig E. Kolb
  5.  *
  6.  * This software may be freely copied, modified, and redistributed,
  7.  * provided that this copyright notice is preserved on all copies.
  8.  *
  9.  * There is no warranty or other guarantee of fitness for this software,
  10.  * it is provided solely .  Bug reports or fixes may be sent
  11.  * to the author, who may or may not act on them as he desires.
  12.  *
  13.  * You may not include this software in a program or other software product
  14.  * without supplying the source, or without informing the end-user that the
  15.  * source is available for no extra charge.
  16.  *
  17.  * If you modify this software, you should include a notice giving the
  18.  * name of the person performing the modification, the date of modification,
  19.  * and the reason for such modification.
  20.  *
  21.  * $Id: box.c,v 3.0 89/10/27 02:05:47 craig Exp $
  22.  *
  23.  * $Log:    box.c,v $
  24.  * Revision 3.0  89/10/27  02:05:47  craig
  25.  * Baseline for first official release.
  26.  * 
  27.  */
  28. #include <math.h>
  29. #include <stdio.h>
  30. #include "constants.h"
  31. #include "typedefs.h"
  32. #include "funcdefs.h"
  33.  
  34. Object *
  35. makbox(surf, x, y, z, xs, ys, zs)
  36. char *surf;
  37. double x, y, z, xs, ys, zs;
  38. {
  39.     Box          *box;
  40.     Primitive *prim;
  41.     Object *newobj;
  42.     extern int Quiet, yylineno;
  43.  
  44.     if (xs < EPSILON || ys < EPSILON || zs < EPSILON) {
  45.         if (!Quiet)
  46.             fprintf(stderr,"Degenerate box (line %d)\n",yylineno);
  47.         return (Object *)0;
  48.     }
  49.     prim = mallocprim();
  50.     prim->surf = find_surface(surf);
  51.     newobj = new_object(NULL, BOX, (char *)prim, (Trans *)NULL);
  52.     prim->type = BOX;
  53.     box = (Box *)Malloc(sizeof(Box));
  54.     prim->objpnt.p_box = box;
  55.     box->bounds[LOW][X] = x - xs;
  56.     box->bounds[HIGH][X] = x + xs;
  57.     box->bounds[LOW][Y] = y - ys;
  58.     box->bounds[HIGH][Y] = y + ys;
  59.     box->bounds[LOW][Z] = z - zs;
  60.     box->bounds[HIGH][Z] = z + zs;
  61.     return newobj;
  62. }
  63.  
  64. double
  65. intbox(pos, ray, obj)
  66. Vector *ray;    /* ray vector */
  67. Vector *pos;    /* origin of ray */
  68. Primitive *obj;    /* box description */
  69. {
  70.     Ray tmpray;
  71.     extern unsigned long BVTests, primtests[];
  72.  
  73.     /*
  74.      * IntBounds will increment BVTests, even though we're
  75.      * not really doing a BV intersection test.
  76.      */
  77.     primtests[BOX]++;
  78.     BVTests--;
  79.     tmpray.pos = *pos;
  80.     tmpray.dir = *ray;
  81.     return IntBounds(&tmpray, obj->objpnt.p_box->bounds);
  82. }
  83.  
  84. nrmbox(pos, obj, nrm)
  85. Vector           *pos, *nrm;    /* point of intersection */
  86. Primitive       *obj;    /* box description */
  87. {
  88.     Box          *box;
  89.  
  90.     box = obj->objpnt.p_box;
  91.     nrm->x = nrm->y = nrm->z = 0.;
  92.  
  93.     if (equal(pos->x, box->bounds[HIGH][X]))
  94.         nrm->x = 1.;
  95.     else if (equal(pos->x, box->bounds[LOW][X]))
  96.         nrm->x = -1.;
  97.     else if (equal(pos->y, box->bounds[HIGH][Y]))
  98.         nrm->y = 1.;
  99.     else if (equal(pos->y, box->bounds[LOW][Y]))
  100.         nrm->y = -1.;
  101.     else if (equal(pos->z, box->bounds[HIGH][Z]))
  102.         nrm->z = 1.;
  103.     else if (equal(pos->z, box->bounds[LOW][Z]))
  104.         nrm->z = -1.;
  105.     else
  106.         fprintf(stderr,"Nrmbox: confusion!\n");
  107. }
  108.  
  109. boxextent(o, bounds)
  110. Primitive *o;
  111. double bounds[2][3];
  112. {
  113.     Box *b = o->objpnt.p_box;
  114.  
  115.     bounds[LOW][X] = b->bounds[LOW][X];
  116.     bounds[HIGH][X] = b->bounds[HIGH][X];
  117.     bounds[LOW][Y] = b->bounds[LOW][Y];
  118.     bounds[HIGH][Y] = b->bounds[HIGH][Y];
  119.     bounds[LOW][Z] = b->bounds[LOW][Z];
  120.     bounds[HIGH][Z] = b->bounds[HIGH][Z];
  121. }
  122.