#ifndef _SOLVE_H_
#define _SOLVE_H_

/*-=-=-=-=-=-=-=-=-=-=-=- RCCL/RCI Notice of Copyright -=-=-=-=-=-=-=-=-=-=-*
 *									    *
 *	Copyright (C) 1991 by John E. Lloyd and Vincent Hayward.	    *
 *									    *
 *	Information on copyright permissions, as well as a complete	    *
 *	disclaimer of warranty, is given in the file COPYRIGHT.		    *
 *									    *
 *-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/

/*---------------------------------------------------------------------------
 *
 * DESCRIPTION:	These macros assist the trajectory generator in evaluating 
 *		multiplying together various components of a loop equation
 *		quickly.
 *
 * AUTHOR/DATE:	John Lloyd, Summer 1989
 *
 * MODIFICATION HISTORY:
 *
 *--------------------------------------------------------------------------*/

#include "../h/cdefs.h"
#include "../h/rcclDefs.h"

/* functions that evaluate components which rendezvous with other CPUs
   before reading values of variable transforms */
extern TRSF *solveToolR();
extern TRSF *solveToolInvR();
extern TRSF *solveCoordR();
extern TRSF *solveCoordInvR();

/* functions which don't bother with rendezvous */
extern TRSF *solveT6U();

/* Macros to solve for various components for the position equation.
   These should be subroutines, but we need the speed, particularly
   on MicroVAXen!
*/

/*
   Return COORD term
*/
#define GET_COORD(mpb)							\
	(mpb->coordTerm 						\
	  ? (mpb->coord 						\
	      ? mpb->coord						\
	      : (mpb->coord = solveCoordR (&mpb->COORD, mpb->goalPos,	\
					  (int)mpb->sysTime)) )		\
	  : NULL )


/*
   Return TOOL term
*/
#define GET_TOOL(mpb)							\
	(mpb->toolTerm 							\
	  ? (mpb->tool 							\
	      ? mpb->tool						\
	      : (mpb->tool = solveToolR (&mpb->TOOL, mpb->goalPos,	\
					  (int)mpb->sysTime)) )		\
	  : NULL )

/*                  -1 
   Compute R = COORD   X TOOL
*/
#define SOLVE_COORDi_X_TOOL(r,x,mpb)					\
	if (GET_COORD(mpb))						\
	 { multLiTrsf ((r), mpb->coord, (x));				\
	   if (GET_TOOL(mpb))						\
	    { multTrsf ((r), (r), mpb->tool);				\
	    }								\
	 }								\
	else if (GET_TOOL(mpb))						\
	 { multTrsf ((r), (x), mpb->tool);				\
	 }								\
	else								\
	 { *(r) = *(x);							\
	 }
	
/*                         -1
   Compute R = COORD X TOOL
*/
#define SOLVE_COORD_X_TOOLi(r,x,mpb)					\
	if (GET_COORD(mpb))						\
	 { multTrsf ((r), mpb->coord, (x));				\
	   if (GET_TOOL(mpb))						\
	    { multRiTrsf ((r), (r), mpb->tool);				\
	    }								\
	 }								\
	else if (GET_TOOL(mpb))						\
	 { multRiTrsf ((r), (x), mpb->tool);				\
	 }								\
	else								\
	 { *(r) = *(x);							\
	 }

/*                       -1
   Compute R = COORD TOOL
*/
#define SOLVE_COORD_TOOLi(r,mpb)					\
	if (GET_COORD(mpb))						\
	 { if (GET_TOOL(mpb))						\
	    { multRiTrsf ((r), mpb->coord, mpb->tool);			\
	    }								\
	   else								\
	    { *(r) = *mpb->coord;					\
	    }								\
	 }								\
	else if (GET_TOOL(mpb))						\
	 { invertTrsf ((r), mpb->tool);					\
	 }								\
	else								\
	 { identTrsf (r);						\
	 }

/* 
   Compute R = X TOOL
*/
#define SOLVE_X_TOOL(r,x,mpb)						\
	if (GET_TOOL(mpb))						\
	 { multTrsf ((r), (x), mpb->tool);				\
	 }								\
	else								\
	 { *(r) = *(x);							\
	 }

/*                   -1
   Compute R = X TOOL
*/
#define SOLVE_X_TOOLi(r,x,mpb)						\
	if (GET_TOOL(mpb))						\
	 { multRiTrsf ((r), (x), mpb->tool);				\
	 }								\
	else								\
	 { *(r) = *(x);							\
	 }

/* 
   Compute R = COORD X
*/
#define SOLVE_COORD_X(r,x,mpb)						\
	if (GET_COORD(mpb))						\
	 { multTrsf ((r), mpb->coord, (x));				\
	 }								\
	else								\
	 { *(r) = *(x);							\
	 }
	
/*                  -1
   Compute R = COORD   X
*/
#define SOLVE_COORDi_X(r,x,mpb)						\
	if (GET_COORD(mpb))						\
	 { multLiTrsf ((r), mpb->coord, (x));				\
	 }								\
	else								\
	 { *(r) = *(x);							\
	 }
	
#endif
