home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Professional / OS2PRO194.ISO / os2 / graphic / qrt / relpos.c < prev    next >
C/C++ Source or Header  |  1989-03-26  |  4KB  |  172 lines

  1.  
  2. /**********************************************************
  3.  
  4.   Relative position module.  This is used for patterns:
  5.   when an object intersection is found, its location is
  6.   passed to one of these routines, which returns two
  7.   object-relative coordinates.
  8.  
  9.  **********************************************************/
  10.  
  11.  
  12. #include "qrt.h"
  13.  
  14. /* #define RELPOSDEBUG 1 */
  15.  
  16. /**********************************************************
  17.  
  18.    Finds the coordinates of a point on a given plane.
  19.  
  20.    Added 11 Mar 89 to fix bug in finding plane coordinates.
  21.  
  22.  **********************************************************/
  23.  
  24. Find_Plane_Coords(v1,v2,delta,pos1,pos2, t1, t2, t3)
  25.   VECT_PTR v1, v2, delta;
  26.   float    *pos1, *pos2, t1, t2, t3;
  27. {
  28.  
  29.   if (t1!= 0) {
  30.     *pos1 = (delta->x*v2->y-v2->x*delta->y)/t1;
  31.   } else {
  32.     if (t2!=0) {
  33.       *pos1 = (delta->x*v2->z-v2->x*delta->z)/t2;
  34.     } else {
  35.       if (t3!=0) {
  36.         *pos1 = (delta->y*v2->z-v2->y*delta->z)/t3;
  37.       } else Error(ILLEGAL_OBJECT,700);
  38.     }
  39.   }
  40.  
  41.   if (v2->x != 0) {
  42.     *pos2 = (delta->x-*pos1*v1->x)/v2->x;
  43.     return;
  44.   }
  45.  
  46.   if (v2->y != 0) {
  47.     *pos2 = (delta->y-*pos1*v1->y)/v2->y;
  48.     return;
  49.   }
  50.  
  51.   if (v2->z != 0) {
  52.     *pos2 = (delta->z-*pos1*v1->z)/v2->z;
  53.     return;
  54.   }
  55.  
  56.   Error(ILLEGAL_OBJECT,701);
  57. }
  58.  
  59.  
  60. /**********************************************************
  61.  
  62.    Finds relative coords on plane given position in space.
  63.    object should be parallelogram or ring.
  64.    loc is point in space.
  65.    pos1, pos2 are set to relative coords.
  66.  
  67.    Changed 11 Mar 88 to use Find_Plane_Coords().
  68.  
  69.  **********************************************************/
  70.  
  71. Plane_Pos(obj,loc,pos1,pos2,normalize)
  72.   OBJ_PTR obj;
  73.   VECT_PTR loc;
  74.   float *pos1, *pos2;
  75.   int normalize;
  76. {
  77.     VECTOR delta;
  78.  
  79. #   ifdef ROBUST
  80.       if (!((obj->type == RING) ||
  81.             (obj->type == PARALLELOGRAM) ||
  82.             (obj->type == RING) ||
  83.             (obj->type == TRIANGLE)))
  84.         Error(INTERNAL_ERROR,702);
  85. #   endif
  86.  
  87.     VecSubtract(&delta,loc,&(obj->loc));
  88.  
  89.     Find_Plane_Coords( &(obj->vect1),
  90.                        &(obj->vect2),
  91.                        &delta,
  92.                        pos1,
  93.                        pos2,
  94.                        obj->precomp.sin1,
  95.                        obj->precomp.cos1,
  96.                        obj->precomp.sin2
  97.                      );
  98.  
  99.     if (!normalize) {
  100.       *pos1 *= sqrt(obj->precomp.len1);
  101.       *pos2 *= sqrt(obj->precomp.len2);
  102.     }
  103.  
  104. #   ifdef RELPOSDEBUG
  105.       printf("Plane_Pos: pos1,2 = %f %f\n",*pos1,*pos2);
  106. #   endif
  107.  
  108. }
  109.  
  110.  
  111. /**********************************************************
  112.  
  113.    Finds relative coords on sphere given position in space
  114.      obj->vect1.x = radius of sphere
  115.  
  116.  **********************************************************/
  117.  
  118. Sphere_Pos(obj,loc,pos1,pos2)
  119.   OBJ_PTR obj;
  120.   VECT_PTR loc;
  121.   float *pos1, *pos2;
  122. {
  123.     float atan2w();
  124.     VECTOR delta;
  125.  
  126. #   ifdef ROBUST
  127.       if (obj->type!=SPHERE) Error(INTERNAL_ERROR,703);
  128. #   endif
  129.  
  130.     VecSubtract(&delta,loc,&(obj->loc));
  131.  
  132. #   ifdef ROBUST
  133.       if (delta.x==0 && delta.y==0 && delta.z==0)
  134.         Error(INTERNAL_ERROR,704);
  135. #   endif
  136.  
  137.     *pos1 = atan2w(delta.x,delta.y) * obj->vect1.x;
  138.     *pos2 = atan2w(sqrt(sqr(delta.x)+sqr(delta.y)),delta.z) *
  139.             obj->vect1.x;
  140.  
  141. #   ifdef RELPOSDEBUG
  142.       printf("SPHEREPOS: pos1,2 = %f %f\n",*pos1,*pos2);
  143. #   endif
  144.  
  145. }
  146.  
  147.  
  148. /**********************************************************
  149.  
  150.  Finds relative coords on quadratic given position in space
  151.  
  152.  **********************************************************/
  153.  
  154. Quadratic_Pos(obj,loc,pos1,pos2)
  155.   OBJ_PTR obj;
  156.   VECT_PTR loc;
  157.   float *pos1, *pos2;
  158. {
  159.    VECTOR newpos;
  160.  
  161.    VecSubtract(&newpos,loc,&(obj->loc));
  162.  
  163.    *pos1 = newpos.x;                     /** This isn't right! **/
  164.    *pos2 = newpos.y;                     /** fix it later      **/
  165.  
  166. #   ifdef RELPOSDEBUG
  167.       printf("QUADRATICPOS: pos1,2 = %f %f\n",*pos1,*pos2);
  168. #   endif
  169. }
  170.  
  171.  
  172.