
/**********************************************************

  Relative position module.  This is used for patterns:
  when an object intersection is found, its location is
  passed to one of these routines, which returns two
  object-relative coordinates.

 **********************************************************/


#include "qrt.h"

/* #define RELPOSDEBUG 1 */

/**********************************************************

   Finds relative coords on plane given position in space.
   object should be parallelogram or ring.
   loc is point in space.
   pos1, pos2 are set to relative coords.

 **********************************************************/

Plane_Pos(obj,loc,pos1,pos2)
  OBJ_PTR obj;
  VECT_PTR loc;
  float *pos1, *pos2;
{
    VECTOR delta;
    register float len1, len2;

#   ifdef ROBUST
      if (!((obj->type == RING) ||
            (obj->type == PARALLELOGRAM) ||
            (obj->type == RING) ||
            (obj->type == TRIANGLE)))
        Error(INTERNAL_ERROR,701);
#   endif

    VecSubtract(&delta,loc,&(obj->loc));

    len1 = sqrt(DotProd(obj->vect1,obj->vect1));
    len2 = sqrt(DotProd(obj->vect2,obj->vect2));

    *pos1 = DotProd(delta,obj->vect1)/len1;
    *pos2 = DotProd(delta,obj->vect2)/len2;

#   ifdef RELPOSDEBUG
      printf("PLANEPOS: len1,2 = %f %f\n",len1,len2);
      printf("          pos1,2 = %f %f\n",*pos1,*pos2);
#   endif

}


/**********************************************************

   Finds relative coords on sphere given position in space
     obj->vect1.x = radius of sphere

 **********************************************************/

Sphere_Pos(obj,loc,pos1,pos2)
  OBJ_PTR obj;
  VECT_PTR loc;
  float *pos1, *pos2;
{
    float atan2w();
    VECTOR delta;

#   ifdef ROBUST
      if (obj->type!=SPHERE) Error(INTERNAL_ERROR,702);
#   endif

    VecSubtract(&delta,loc,&(obj->loc));

#   ifdef ROBUST
      if (delta.x==0 && delta.y==0 && delta.z==0)
        Error(INTERNAL_ERROR,703);
#   endif

    *pos1 = atan2w(delta.x,delta.y) * obj->vect1.x;
    *pos2 = atan2w(sqrt(sqr(delta.x)+sqr(delta.y)),delta.z) *
            obj->vect1.x;

#   ifdef RELPOSDEBUG
      printf("SPHEREPOS: pos1,2 = %f %f\n",*pos1,*pos2);
#   endif

}


/**********************************************************

 Finds relative coords on quadratic given position in space

 **********************************************************/

Quadratic_Pos(obj,loc,pos1,pos2)
  OBJ_PTR obj;
  VECT_PTR loc;
  float *pos1, *pos2;
{
   VECTOR newpos;

   VecSubtract(&newpos,loc,&(obj->loc));

   *pos1 = newpos.x;                     /** This isn't right! **/
   *pos2 = newpos.y;                     /** fix it later      **/

#   ifdef RELPOSDEBUG
      printf("QUADRATICPOS: pos1,2 = %f %f\n",*pos1,*pos2);
#   endif
}


