Exemplo n.º 1
0
/*!
* \ingroup	HGU_GL
* \brief	Update the trackball rotation for the given current
*		position. The current and last positions are then used
*		two calculate two normalised points (ie [-1.0, +1.0]).
*		The axis of rotation is	then the cross product of the
*		two vectors through the	normalised points and the
*		trackball's origin.
* \param	tbW			Canvas / trackball widget instance.
* \param	curPos			New position.
*/
void		HGUglwCanvasTbCalculateRotate(HGUglwCanvasTbWidget tbW,
				     	      WlzIVertex2 curPos)
{
  int		width,
  		height;
  double	phi,
  		tD0;
  WlzDVertex2	point1,
  		point2;
  WlzDVertex3	axis,
  		proj1,
		proj2,
		diff;
  HGUglwCanvasTbPart *ctb;

  ctb = &(tbW->hguGLwCanvasTb);
  if(((width = tbW->core.width) > 0) && ((height = tbW->core.height) > 0))
  {
    point1.vtX = ((2.0 * ctb->motionPos.vtX) / width) - 1.0;
    point1.vtY = 1.0 - ((2.0 * ctb->motionPos.vtY ) / height);
    point2.vtX = ((2.0 * curPos.vtX) / width) - 1.0;
    point2.vtY = 1.0 - ((2.0 * curPos.vtY ) / height);
    ctb->motionPos = curPos;
    tD0 = 0.01 * ctb->trackballSize;
    WLZ_VTX_3_SET(proj1, point1.vtX, point1.vtY,
		  0.0 - HGUglwCanvasTbProjectToZ(tD0, point1));
    WLZ_VTX_3_SET(proj2, point2.vtX, point2.vtY,
		  0.0 - HGUglwCanvasTbProjectToZ(tD0, point2));
    WLZ_VTX_3_CROSS(axis, proj1, proj2);
    WLZ_VTX_3_SUB(diff, proj1, proj2);
    tD0 = WLZ_VTX_3_LENGTH(diff) / (2 * 0.01 * ctb->trackballSize);
    if(tD0 > 1.0)
    {
      tD0 = 1.0;
    }
    else if(tD0 < -1.0)
    {
      tD0 = -1.0;
    }
    phi = 2.0 * asin(tD0);
    ctb->rotateDelta = HGUglQuatFromAxis(axis, phi);
  }
}
Exemplo n.º 2
0
/*!
* \return	Calculated quaternion.
* \ingroup	HGU_GL
* \brief	Calculates a rotation quaternion (which lies on the unit
* 		sphere) using the given axis and rotation about that axis.
* \param	axis			Axis defined by vector throgh
* 					(0, 0, 0) and this vertex.
* \param	phi			Rotation about the given axis.
*/
HGUglQuaternion	HGUglQuatFromAxis(WlzDVertex3 axis, double phi)
{
  double	halfPhi,
  		axisLen,
		tD1;
  HGUglQuaternion quat;

  halfPhi = phi / 2.0;
  axisLen = WLZ_VTX_3_LENGTH(axis);
  if(axisLen > DBL_EPSILON)
  {
    quat.qW = cos(halfPhi);
    tD1 = sin(halfPhi) / axisLen;
    WLZ_VTX_3_SCALE(quat.qV, axis, tD1);
  }
  else
  {
    HGUgl_QUAT_SET4(quat, 1.0, 0.0, 0.0, 0.0);
    
  }
  return(quat);
}
Exemplo n.º 3
0
/*!
* \return	Woolz error code.
* \ingroup	WlzTransform
* \brief	Fits a plane to the given vertices using singular value
* 		decomposition. The best fit plane is returned (provided
* 		there is no error) as a unit normal \f$n\f$ and the
* 		centroid of the given vertices ( \f$r_0\f$ ) which in a
* 		point in the plane, with the plane equation
* 		\f[
		\vec{n}\cdot(\vec{r} - \vec{r_0}) = \vec{0}
 		\f]
* \param	vtxType			Given vertex type.
* \param	nVtx			Number of given vertices.
* \param	vtx			Given vertices.
* \param	dstPinP			Destination pointer for the return
* 					of the median point (\f$r_0\f$) in
* 					the plane.
* \param	dstNrm			Destination pointer for the unit
*   					normal to the plane (\f$n\f$).
*/
WlzErrorNum WlzFitPlaneSVD(WlzVertexType vtxType, int nVtx, WlzVertexP vtx,
			   WlzDVertex3 *dstPinP, WlzDVertex3 *dstNrm)
{
  AlgMatrix     aMat,
  		vMat;
  WlzDVertex3	centroid,
                normal;
  double	*sVec = NULL;
  AlgError	algErr = ALG_ERR_NONE;
  WlzErrorNum	errNum = WLZ_ERR_NONE;

  aMat.core = NULL;
  vMat.core = NULL;
  if(nVtx < 0)
  {
    errNum = WLZ_ERR_PARAM_DATA;
  }
  else if(vtx.v == NULL)
  {
    errNum = WLZ_ERR_PARAM_NULL;
  }
  if(errNum == WLZ_ERR_NONE)
  {
    if(((sVec = (double *)AlcCalloc(sizeof(double), 3)) == NULL) ||
       ((aMat.rect = AlgMatrixRectNew(nVtx, 3, NULL)) == NULL) ||
       ((vMat.rect = AlgMatrixRectNew(3, 3, NULL)) == NULL))
    {
      errNum = WLZ_ERR_MEM_ALLOC;
    }
  }
  /* Compute a Nx3 matrix from the given vertices then subtract their
   * centroid. */
  if(errNum == WLZ_ERR_NONE)
  {
    int		i;
    double	*row;
    
    WLZ_VTX_3_ZERO(centroid);
    switch(vtxType)
    {
      case WLZ_VERTEX_I3:
	for(i = 0; i < nVtx; ++i)
	{
	  WlzIVertex3 *v;

	  v = vtx.i3 + i;
          row = aMat.rect->array[i];
	  centroid.vtX += row[0] = v->vtX;
	  centroid.vtY += row[1] = v->vtY;
	  centroid.vtZ += row[2] = v->vtZ;
	}
	break;
      case WLZ_VERTEX_F3:
	for(i = 0; i < nVtx; ++i)
	{
	  WlzFVertex3 *v;

	  v = vtx.f3 + i;
          row = aMat.rect->array[i];
	  centroid.vtX += row[0] = v->vtX;
	  centroid.vtY += row[1] = v->vtY;
	  centroid.vtZ += row[2] = v->vtZ;
	}
	break;
      case WLZ_VERTEX_D3:
	for(i = 0; i < nVtx; ++i)
	{
	  WlzDVertex3 *v;

	  v = vtx.d3 + i;
          row = aMat.rect->array[i];
	  centroid.vtX += row[0] = v->vtX;
	  centroid.vtY += row[1] = v->vtY;
	  centroid.vtZ += row[2] = v->vtZ;
	}
	break;
      default:
	errNum = WLZ_ERR_PARAM_TYPE;
        break;
    }
    if(errNum == WLZ_ERR_NONE)
    {
      WLZ_VTX_3_SCALE(centroid, centroid, (1.0 / nVtx));
      for(i = 0; i < nVtx; ++i)
      {
	row = aMat.rect->array[i];
	row[0] -= centroid.vtX;
	row[1] -= centroid.vtY;
	row[2] -= centroid.vtZ;
      }
    }
  }
  /* Compute SVD. */
  if(errNum == WLZ_ERR_NONE)
  {
    algErr = AlgMatrixSVDecomp(aMat, sVec, vMat);
    errNum = WlzErrorFromAlg(algErr);
  }
  /* Find the vector corresponding to the least singular value. */
  if(errNum == WLZ_ERR_NONE)
  {
    int		i,
    		m;
    double	len;
    double	**ary;
    const double eps = 1.0e-06;

    m  = 0;
    for(i = 1; i < 3; ++i)
    {
      if(sVec[i] < sVec[m])
      {
        m = i;
      }
    }
    ary = vMat.rect->array;
#ifdef WLZ_FITPLANESVD_DEBUG
    (void )fprintf(stderr,
                   "WlzFitPlaneSVD() singular values = {%lg, %lg, %lg}\n",
		   sVec[0], sVec[1], sVec[2]);
    (void )fprintf(stderr,
                   "WlzFitPlaneSVD() index of min singular value = %d\n",
		   m);
    (void )fprintf(stderr,
                   "WlzFitPlaneSVD() singular vectors = \n"
		   "{\n");
    for(i = 0; i < 3; ++i)
    {
      (void )fprintf(stderr,
                     " {%lg, %lg, %lg}\n",
		     ary[i][0], ary[i][1], ary[i][2]);
    }
    (void )fprintf(stderr,
		   "}\n");
#endif /* WLZ_FITPLANESVD_DEBUG */
    normal.vtX = ary[0][m];
    normal.vtY = ary[1][m];
    normal.vtZ = ary[2][m];
    len = WLZ_VTX_3_LENGTH(normal);
    if(len < eps)
    {
      errNum = WLZ_ERR_ALG_SINGULAR;
    }
    else
    {
      WLZ_VTX_3_SCALE(normal, normal, (1.0 / len));
    }
  }
  AlgMatrixRectFree(aMat.rect);
  AlgMatrixRectFree(vMat.rect);
  AlcFree(sVec);
  if(errNum == WLZ_ERR_NONE)
  {
    if(dstNrm)
    {
      *dstNrm = normal;
    }
    if(dstPinP)
    {
      *dstPinP = centroid;
    }
  }
  return(errNum);
}
Exemplo n.º 4
0
/*!
* \return	New Woolz interval domain or NULL on error.
* \ingroup	WlzTransform
* \brief	Creates a new 2D Woolz interval domain with a single line
* 		which is the profile from the given start position to the
* 		given end position for the given spatial domain object.
* \param	gObj		Given Woolz object.
* \param	sPos		Start position.
* \param	ePos		End position.
* \param	dstErr		Destination error pointer, may be NULL.
*/
WlzIntervalDomain *WlzProfileLineIDom(
  WlzObject *gObj,
  WlzIVertex3 sPos,
  WlzIVertex3 ePos,
  WlzErrorNum *dstErr)
{
  WlzIntervalDomain *iDom = NULL;
  WlzErrorNum	errNum = WLZ_ERR_NONE;

  /* Check given object. */
  if(gObj == NULL)
  {
    errNum = WLZ_ERR_OBJECT_NULL;
  }
  else
  {
    switch(gObj->type)
    {
      case WLZ_2D_DOMAINOBJ:
      case WLZ_3D_DOMAINOBJ:
	if(gObj->domain.core == NULL)
	{
	  errNum = WLZ_ERR_DOMAIN_NULL;
	}
        break;
      default:
	errNum = WLZ_ERR_OBJECT_TYPE;
        break;
    }
  }
  if(errNum == WLZ_ERR_NONE)
  {
    int		len;
    WlzIVertex3	dPos;
    WlzProfileWalkWSp pWSp = {0};
    WlzInterval *rItv = NULL;

    WLZ_VTX_3_SUB(dPos, ePos, sPos);
    len = (int )ceil(WLZ_VTX_3_LENGTH(dPos));
    /* Create return object. */
    iDom = WlzMakeIntervalDomain(WLZ_INTERVALDOMAIN_INTVL,
                                 0, 0, 0, len - 1, &errNum);
    if(errNum == WLZ_ERR_NONE)
    {
      if((rItv = (WlzInterval *)
		 AlcCalloc((len / 2) + 1, sizeof(WlzInterval))) == NULL)
      {
	errNum = WLZ_ERR_MEM_ALLOC;
	(void )WlzFreeIntervalDomain(iDom);
	iDom = NULL;
      }
    }
    if(errNum == WLZ_ERR_NONE)
    {
      WlzIntervalLine *itvLn;

      iDom->freeptr = AlcFreeStackPush(iDom->freeptr, (void *)rItv, NULL);
      pWSp.start = sPos;
      pWSp.end = ePos;
      pWSp.domain.i = iDom;
      itvLn = pWSp.domain.i->intvlines + 0;
      itvLn->intvs = rItv;
      itvLn->nintvs = 0;
      errNum = WlzProfileWalk(gObj, WlzProfileSetIDom, &pWSp);
    }
  }
  if(errNum != WLZ_ERR_NONE)
  {
    iDom = NULL;
  }
  if(dstErr)
  {
    *dstErr = errNum;
  }
  return(iDom);
}
int main(int	argc,
	 char	**argv)
{

  FILE	       *inFile = NULL;   /* used to read Woolz object */ 
  int           i,j,k=0, m;
  int		option;
  int           startJ;
  int           cycleI, cycleNext;
  double           ntnm,nfnm,nsnm;
  int           numOf2DWlzFiles = 0, numOfBibFiles = 0;
  int           ok, usage1, idx;
  double        deltaZ;
  double        sx=0,sy=0,sz=0, fx=0,fy=100,fz=0, tx=100,ty=0,tz=0;
  double        Sx=0,Sy=0,Sz=60, Fx=0,Fy=100,Fz=60, Tx=100,Ty=0,Tz=60;
  double        lengthTS;
  char         *inFileStr, *inFileStr1, *outFileStr;
  char         *inFileStrw=NULL, *outputWlzFilesDir=NULL;
  char          TwoDImageFilesNameList[700][120];
  char          TwoDImagePureFilesNameList[700][120];
  char          warpedFilesNameList[700][120];
  char          BibFilesNameList[700][120];

  char          TwoDImageFilesDir[120];
  char         *List2DImageFilesStr= NULL, *ListBibFilesStr= NULL;


  WlzVertex      vs, vt, vf,  vS, vT, vF;
  WlzVertex      vs1, vt1, vf1, vsp, vtp, vfp, vST;

  WlzVertex      vsr, vtr, vfr;
  WlzVertex      ns, nf, nt, nsp, nfp, ntp, nz,nm;
  WlzDVertex2   *vtxVec1;
  WlzErrorNum	 errNum = WLZ_ERR_NONE;
  WlzThreeDViewStruct *wlzViewStr, *wlzViewStr1;
  WlzObject     *WObjS; 
  char		*cutStr[3];
  double        cutVal[3];

  /* read the argument list and check for an input file */
  static char	optList[] = "i:I:w:o:d:s:S:f:F:t:T:M:n:r:R:L:B:h",opterr = 0;
  ok = 1;
 
   while( (option = getopt(argc, argv, optList)) != EOF )
   {
      switch( option )
      {
        case 'h':
             usage(argv[0]);
	     return(0);
        case 'i':
	    inFileStr  = optarg;
	    /*
	    outFileStr = optarg;
	    */
	    break;
        case 'I':
	    inFileStr1 = optarg;
	    break;
        case 'L':
	    List2DImageFilesStr = optarg;
	    break;
        case 'B':
	    ListBibFilesStr = optarg;
	    break;
        case 'w':
	    inFileStrw = optarg;
	    break;
        case 'n':
	 if(sscanf(optarg, "%d", &startJ) != 1)
	    {
	      printf("read error");
	      exit(1);
	    }
	    break;
        case 's':
        case 'f':
        case 't':
        case 'S':
        case 'F':
        case 'T':
	  if(optarg)
	  {
	  	while(*optarg && isspace(*optarg))
	  	{
                    ++optarg;
		}
	 	if(*optarg == ',')
	  	{
	    		cutStr[0] = NULL;
	    		cutStr[1] = strtok(optarg, ",");
	    		cutStr[2] = strtok(optarg, ",");
	  	}
	  	else
	  	{
	    		cutStr[0] = strtok(optarg, ",");
	    		cutStr[1] = strtok(NULL, ",");
		    	cutStr[2] = strtok(NULL, ",");
	  	}
	  	if((cutStr[0] == NULL) && (cutStr[1] == NULL) && (cutStr[2] == NULL)   )
	  	{
	    		usage1 = 1;
	    		ok = 0;
	  	}
	  	else
	  	{
	    		idx = 0;
	    		while(ok && (idx < 3))
	    		{
	      			if(cutStr[idx] && (sscanf(cutStr[idx], "%lg",
					 cutVal + idx) != 1))
	      			{
					usage1 = 1;
					ok = 0;
	      			}
	      			++idx;
	    		}
	  	}
	}
	if(ok)
	{
	  switch(option)
	  {
	    case 's':
		sx = (double) cutVal[0];
		sy = (double) cutVal[1];
		sz = (double) cutVal[2];
		break;
	    case 'f':
		fx = (double) cutVal[0];
		fy = (double) cutVal[1];
		fz = (double) cutVal[2];
		break;
	    case 't':
		tx = (double) cutVal[0];
		ty = (double) cutVal[1];
		tz = (double) cutVal[2];
		break;
	    case 'S':
		Sx = (double) cutVal[0];
		Sy = (double) cutVal[1];
		Sz = (double) cutVal[2];
		break;
	    case 'F':
		Fx = (double) cutVal[0];
		Fy = (double) cutVal[1];
		Fz = (double) cutVal[2];
		break;
	    case 'T':
		Tx = (double) cutVal[0];
		Ty = (double) cutVal[1];
		Tz = (double) cutVal[2];
		break;
	  }	

	}
	break;
        case 'o':
	    outFileStr = optarg;
	    break;
        case 'd':
	    outputWlzFilesDir = optarg;
	    break;
        default:
              return(0);
      }
   }

      printf("t: %f %f %f\n",tx,ty, tz );
      printf("T: %f %f %f\n",Tx,Ty, Tz );

    /*------- allocate memory --------*/
    if (
         ((vtxVec1 = (WlzDVertex2 *)AlcCalloc(sizeof(WlzDVertex2), 3))  == NULL) 
      )
    {
       errNum = WLZ_ERR_MEM_ALLOC;
    }

    /* ------Read 2D Wlz files List------- */
     printf("Read 2D Wlz file list\n");
    if((inFile = fopen(List2DImageFilesStr, "r")) == NULL )
    {
        printf("cannot open the 2D image files list file.\n");
        exit(1);
    }
    
    i=0;
    /* get the number of files */
    while(!feof(inFile)){

        fscanf(inFile, "%s", *(TwoDImageFilesNameList + i ));
	/*  printf("%s\n", *(TwoDImageFilesNameList + i ) ); */
	i++;
    }
    fclose(inFile); 
    inFile = NULL;

    numOf2DWlzFiles = i-1;
    printf("number of files = %d\n", numOf2DWlzFiles );

    /* extract pure wlz filename without directory */
    printf("extract pure wlz filename without directory\n");

    for(m=0; m<numOf2DWlzFiles; m++)
    {
       for(i=0; i<sizeof(TwoDImageFilesNameList[m]); i++){
          if( TwoDImageFilesNameList[m][i] == (char) NULL )
           break; 
          if( TwoDImageFilesNameList[m][i] == '/' )
            k = i; 	   
       }
       if(k != 0)
       k++;
       for(j=k; j<i; j++)
       {
         TwoDImagePureFilesNameList[m][j-k] = TwoDImageFilesNameList[m][j];
         warpedFilesNameList[m][j-k]        = TwoDImageFilesNameList[m][j];
       }
       TwoDImagePureFilesNameList[m][j-k] = ( char ) NULL;
       warpedFilesNameList[m][j-k]        = ( char ) NULL;
         
       /* extract directory */
       if(m == 0)
       {
          for(j=0; j<k; j++)
	  {
	     TwoDImageFilesDir[j] = TwoDImageFilesNameList[m][j];
	  }
	     TwoDImageFilesDir[j] = ( char ) NULL;
	     printf("I_DIR  %s\n",TwoDImageFilesDir );
       }
       printf("%s\n", TwoDImagePureFilesNameList[m]);   
    }

/*--- Read bib File name list ----*/
    if((inFile = fopen(ListBibFilesStr, "r")) == NULL )
    {
        printf("cannot open the 2D image files list file.\n");
        exit(1);
    }
    i=0;
    while(!feof(inFile)){
        fscanf(inFile, "%s", BibFilesNameList[i]);
	printf("%s\n", BibFilesNameList[i]);
	i++;
    }
      fclose(inFile); 
      inFile = NULL;
    numOfBibFiles = i-1;
    printf("number of files bib files = %d\n", numOfBibFiles );

/* check whether the number of bib files is the same as number of Wlz 2D sections */
    if(numOfBibFiles != numOf2DWlzFiles )
    {
         printf("Number of 2D woolz files and number of bib files are different\n");
	 exit(1);
    }

/* get the output file namelist */
   i =  (int) strlen( (const char *) outputWlzFilesDir);
   printf("%d\n", i);
   for( j = 0; j < numOfBibFiles; j++)
   {
      strcpy( warpedFilesNameList[j], (const char *) outputWlzFilesDir );
      strcat( warpedFilesNameList[j], "/");
      strcat(warpedFilesNameList[j], (const char *) TwoDImagePureFilesNameList[j]);
      printf("%s\n", warpedFilesNameList[j] );   
   }   
   
/* --------read Woolz object--------- */
      if((inFile = fopen(inFileStrw, "r")) == NULL )
      {
        printf("cannot open the input woolz file.\n");
        exit(1);
      }

      if( !(WObjS = WlzReadObj(inFile, &errNum) ) )
      {
        printf("input Woolz Object Error.\n");
        fclose(inFile); 
        exit(1);
      }
      fclose(inFile); 
      inFile = NULL;

/* ------get the fixed S , T and F ready and s, t, f  in OPT space------ */

      vS.d3.vtX = Sx; 
      vS.d3.vtY = Sy;
      vS.d3.vtZ = Sz;
      vT.d3.vtX = Tx; 
      vT.d3.vtY = Ty;
      vT.d3.vtZ = Tz;
      vF.d3.vtX = Fx; 
      vF.d3.vtY = Fy;
      vF.d3.vtZ = Fz;
      
      vs.d3.vtX = sx; 
      vs.d3.vtY = sy;
      vs.d3.vtZ = sz;
      vt.d3.vtX = tx; 
      vt.d3.vtY = ty;
      vt.d3.vtZ = tz;
      vf.d3.vtX = fx; 
      vf.d3.vtY = fy;
      vf.d3.vtZ = fz;
      
      printf("vt: %f %f %f\n",vt.d3.vtX,vt.d3.vtY, vt.d3.vtZ );
      printf("vT: %f %f %f\n",vT.d3.vtX,vT.d3.vtY, vT.d3.vtZ );
/*  -----get the three directions in OPT space------- 
    n = 1/(|r2-r1|) { (x2-x1) i  + (y2-y1) j + (z2-z1) k  }

  */
   
      WLZ_VTX_3_SUB(vST.d3,vS.d3,vs.d3);
      lengthTS  =  WLZ_VTX_3_LENGTH( vST.d3);
      ns.d3.vtX = vST.d3.vtX/ lengthTS;
      ns.d3.vtY = vST.d3.vtY/ lengthTS;
      ns.d3.vtZ = vST.d3.vtZ/ lengthTS;


      WLZ_VTX_3_SUB(vST.d3,vF.d3,vf.d3);
      lengthTS  =  WLZ_VTX_3_LENGTH( vST.d3);
      nf.d3.vtX = vST.d3.vtX/ lengthTS;
      nf.d3.vtY = vST.d3.vtY/ lengthTS;
      nf.d3.vtZ = vST.d3.vtZ/ lengthTS;

      WLZ_VTX_3_SUB(vST.d3,vT.d3,vt.d3);
      printf("vt: %f %f %f\n",vt.d3.vtX,vt.d3.vtY, vt.d3.vtZ );
      printf("vT: %f %f %f\n",vT.d3.vtX,vT.d3.vtY, vT.d3.vtZ );
      lengthTS  =  WLZ_VTX_3_LENGTH( vST.d3);
      printf("Length: %f\n",lengthTS);
      nt.d3.vtX = vST.d3.vtX/ lengthTS;
      nt.d3.vtY = vST.d3.vtY/ lengthTS;
      nt.d3.vtZ = vST.d3.vtZ/ lengthTS;

/* 
  ---- get the three points in the master plane ( startJ plane ) ----

*/

/* allocate memory and give default values for viewer strcuture */
      wlzViewStr      = WlzMake3DViewStruct(WLZ_3D_VIEW_STRUCT, NULL);
      wlzViewStr1     = WlzMake3DViewStruct(WLZ_3D_VIEW_STRUCT, NULL);

/* set up */
   cycleI     = startJ;
   /* read Section viewer parameters */
   errNum = ReadBibFile(inFile, (char *) BibFilesNameList[cycleI],   wlzViewStr  );
   if(errNum != WLZ_ERR_NONE)
   {
         printf("Read or parse bib file err.\n");
         exit(1);
   
   }
   inFile = NULL;

   WlzInit3DViewStruct(wlzViewStr, WObjS);


/* -------- get the corresponding three directions in Main view space -------
    (It is also the stack up space 
    nsp = Tm ns, 
    nfp = Tm nf, 
    ntp = Tm nt;
  */
  errNum = Wlz3DSectionTransformVtxR(wlzViewStr, ns.d3, &nsp.d3 );
  if(errNum != WLZ_ERR_NONE)
   {
         printf("can not get the direction in stack up space for ns err.\n");
	 exit(1);
   }
  errNum = Wlz3DSectionTransformVtxR(wlzViewStr, nf.d3, &nfp.d3 );
  if(errNum != WLZ_ERR_NONE)
   {
         printf("can not get the direction in stack up space for nf err.\n");
	 exit(1);
   }
  errNum = Wlz3DSectionTransformVtxR(wlzViewStr, nt.d3, &ntp.d3 );
  if(errNum != WLZ_ERR_NONE)
   {
         printf("can not get the direction in stack up space for np err.\n");
	 exit(1);
   }
     

  /* --- get the main plane direction in OPT space --- */
  /* it can got by inverse transform from stack up viewer space to
     OPT space */
     /* in stack up space or master plane sapce */
      nz.d3.vtX = 0.0;
      nz.d3.vtY = 0.0;
      nz.d3.vtZ = 1.0;
  
  /*  nm = Tm^-1  nz  (inverse transform used here ) nm is in OPT space */
  errNum = Wlz3DSectionTransformInvVtxR(wlzViewStr, nz.d3, &nm.d3 );
  if(errNum != WLZ_ERR_NONE)
   {
         printf("can not get the main plane direction in OPT space err.\n");
         printf("Which means your three straight line has to be rechosen\n");
	 exit(1);
   }
   /* get the nt dot nm, etc */

  ntnm  =  WLZ_VTX_3_DOT(nt.d3, nm.d3);
  nfnm  =  WLZ_VTX_3_DOT(nf.d3, nm.d3);
  nsnm  =  WLZ_VTX_3_DOT(ns.d3, nm.d3);
  ntnm = nm.d3.vtX * nt.d3.vtX +  nm.d3.vtY * nt.d3.vtY + nm.d3.vtZ * nt.d3.vtZ;


  if(  ( ( ntnm == 0 )  || ( nfnm == 0 ) ) ||( nsnm == 0 ) )
    {
      printf("At least one of your three strait lines are parallel to the main plane err.\n");
      printf("Which means your three straight line has to be rechosen or chose another plane\n");
      printf("as main plane\n");
      exit(1);
    }




  /* get crossed point in this plane p_{i} then project to viewer space */
  errNum = WlzGetCorssPoint(vs,  ns,  &vsr,  wlzViewStr);
  if(errNum != WLZ_ERR_NONE)
    {
      printf("can not find crossed point err.\n");
      exit(1);
    }
  errNum = WlzGetCorssPoint(vt,  nt,  &vtr,  wlzViewStr);
  if(errNum != WLZ_ERR_NONE)
    {
      printf("can not find crossed point err.\n");
      exit(1);
    }

  errNum = WlzGetCorssPoint(vf,  nf,  &vfr,  wlzViewStr);
  if(errNum != WLZ_ERR_NONE)
  {
         printf("can not find crossed point err.\n");
         exit(1);
   
  }

  /* use this 3 point as refernce */
  /* --------Get Tie-points -------- */


   /* get the target points for this plane in stack up space */

   (vtxVec1    )->vtX = vsr.d3.vtX;
   (vtxVec1    )->vtY = vsr.d3.vtY;
   (vtxVec1 + 1)->vtX = vtr.d3.vtX;
   (vtxVec1 + 1)->vtY = vtr.d3.vtY;
   (vtxVec1 + 2)->vtX = vfr.d3.vtX;
   (vtxVec1 + 2)->vtY = vfr.d3.vtY;

  /* get the source point */
   /* get ( sx' sy' sz' )_i  */
   printf("%f  %f\n", sx, sy);
   vsp.d3.vtX = vsr.d3.vtX;
   vsp.d3.vtY = vsr.d3.vtY;
   vsp.d3.vtZ = vsr.d3.vtZ;

   /* get ( tx' ty' tz' )_i  */
   printf("%f  %f\n", tx, ty);
   vtp.d3.vtX = vtr.d3.vtX;
   vtp.d3.vtY = vtr.d3.vtY;
   vtp.d3.vtZ = vtr.d3.vtZ;

   /* get ( fx' fy' fz' )_i  */
   printf("%f  %f\n", fx, fy);
   vfp.d3.vtX =  vfr.d3.vtX;
   vfp.d3.vtY =  vfr.d3.vtY;
   vfp.d3.vtZ =  vfr.d3.vtZ;
 
   /* ------ output the affine transformed Wlz ------*/
   errNum =        outputTheWarpedWlz( vsp, 
                                       vtp,
				       vfp,
                                       vtxVec1,
                                       TwoDImageFilesNameList[cycleI],
				       warpedFilesNameList[cycleI]
				     );

   while(cycleI < numOfBibFiles - 1)
   {

      	cycleNext = cycleI + 1;
	
   	errNum = ReadBibFile(inFile, (char *) BibFilesNameList[cycleNext],   wlzViewStr1  );
   	WlzInit3DViewStruct(wlzViewStr1, WObjS);

  	/* ------ get Tie - points -------*/

 	/* get crossed point in this plane p_{i} then project to viewer space */
   	errNum = WlzGetCorssPoint(vs,  ns,  &vs1,  wlzViewStr1);
  	if(errNum != WLZ_ERR_NONE)
   	{
        	printf("can not find crossed point err.\n");
         	exit(1);
   
  	 }
  	errNum = WlzGetCorssPoint(vt,  nt,  &vt1,  wlzViewStr1);
  	if(errNum != WLZ_ERR_NONE)
   	{
        	printf("can not find crossed point err.\n");
         	exit(1);
   
   	}

  	errNum = WlzGetCorssPoint(vf,  nf,  &vf1,  wlzViewStr1);
  	if(errNum != WLZ_ERR_NONE)
   	{
        	printf("can not find crossed point err.\n");
         	exit(1);
   
   	}


  	/*  get the source points */
   	/* get ( sx' sy' sz' )_i  */
   	/* printf("%f  %f\n", sx, sy); */
   	vsp.d3.vtX = vs1.d3.vtX;
   	vsp.d3.vtY = vs1.d3.vtY;
   	vsp.d3.vtZ = vs1.d3.vtZ;

   	/* get ( tx' ty' tz' )_i  */
   	/* printf("%f  %f\n", tx, ty); */
   	vtp.d3.vtX = vt1.d3.vtX;
   	vtp.d3.vtY = vt1.d3.vtY;
   	vtp.d3.vtZ = vt1.d3.vtZ;

   	/* get ( fx' fy' fz' )_i  */
   	/* printf("%f  %f\n", fx, fy); */
   	vfp.d3.vtX =  vf1.d3.vtX;
   	vfp.d3.vtY =  vf1.d3.vtY;
   	vfp.d3.vtZ =  vf1.d3.vtZ;

  	/*  get the target points */

   	/* find the target points by keep the same direction in
	   stack up space and the OPT space.The following are in stack up space */
        deltaZ = (double) ( cycleNext - startJ );


  	errNum = WlzGetDxDy( nsp,  deltaZ,  &vs1);
  	if(errNum != WLZ_ERR_NONE)
   	{
        	printf("can not get Dx Dy err.\n");
         	exit(1);
   
   	}
  	errNum = WlzGetDxDy( nfp,  deltaZ,  &vf1);
  	if(errNum != WLZ_ERR_NONE)
   	{
        	printf("can not get Dx Dy err.\n");
         	exit(1);
   
   	}
  	errNum = WlzGetDxDy( ntp,  deltaZ,  &vt1);
  	if(errNum != WLZ_ERR_NONE)
   	{
        	printf("can not get Dx Dy err.\n");
         	exit(1);
   
   	}

   	(vtxVec1    )->vtX = vsr.d3.vtX + vs1.d3.vtX;
   	(vtxVec1    )->vtY = vsr.d3.vtY + vs1.d3.vtY;
   	(vtxVec1 + 1)->vtX = vtr.d3.vtX + vt1.d3.vtX;
   	(vtxVec1 + 1)->vtY = vtr.d3.vtY + vt1.d3.vtY;
   	(vtxVec1 + 2)->vtX = vfr.d3.vtX + vf1.d3.vtX;
   	(vtxVec1 + 2)->vtY = vfr.d3.vtY + vf1.d3.vtY;
 
   	/* ------ output the affine transformed Wlz ------*/
	printf("----------- %d\n",cycleI);
	/* out put the source and target for checking */
   	errNum =        outputTheWarpedWlz( vsp, 
                                       vtp,
				       vfp,
                                       vtxVec1,
                                       TwoDImageFilesNameList[cycleNext],
				       warpedFilesNameList[cycleNext]
				     );

   	/* track from s_i' to  s_{i+1}' */
   	/*  scale = |T - S |/|t - s|      */
        
        cycleI++;

   }


   cycleI = startJ;
   while(  cycleI > 0  )
   {
      	cycleNext = cycleI - 1;
	
   	errNum = ReadBibFile(inFile, (char *) BibFilesNameList[cycleNext],   wlzViewStr1  );
   	WlzInit3DViewStruct(wlzViewStr1, WObjS);

  	/* ------ get Tie - points -------*/

 	/* get crossed point in this plane p_{i} then project to viewer space */
   	errNum = WlzGetCorssPoint(vs,  ns,  &vs1,  wlzViewStr1);
  	if(errNum != WLZ_ERR_NONE)
   	{
        	printf("can not find crossed point err.\n");
         	exit(1);
   
  	 }
  	errNum = WlzGetCorssPoint(vt,  nt,  &vt1,  wlzViewStr1);
  	if(errNum != WLZ_ERR_NONE)
   	{
        	printf("can not find crossed point err.\n");
         	exit(1);
   
   	}

  	errNum = WlzGetCorssPoint(vf,  nf,  &vf1,  wlzViewStr1);
  	if(errNum != WLZ_ERR_NONE)
   	{
        	printf("can not find crossed point err.\n");
         	exit(1);
   
   	}


  	/*  get the source points */
   	/* get ( sx' sy' sz' )_i  */
   	printf("%f  %f\n", sx, sy);
   	vsp.d3.vtX = vs1.d3.vtX;
   	vsp.d3.vtY = vs1.d3.vtY;
   	vsp.d3.vtZ = vs1.d3.vtZ;

   	/* get ( tx' ty' tz' )_i  */
   	printf("%f  %f\n", tx, ty);
   	vtp.d3.vtX = vt1.d3.vtX;
   	vtp.d3.vtY = vt1.d3.vtY;
   	vtp.d3.vtZ = vt1.d3.vtZ;

   	/* get ( fx' fy' fz' )_i  */
   	printf("%f  %f\n", fx, fy);
   	vfp.d3.vtX =  vf1.d3.vtX;
   	vfp.d3.vtY =  vf1.d3.vtY;
   	vfp.d3.vtZ =  vf1.d3.vtZ;

  	/*  get the target points */

   	/* find the target points by keep the same direction in
	   stack up space and the OPT space.The following are in stack up space */
        deltaZ = (double) ( cycleNext - startJ );


  	errNum = WlzGetDxDy( nsp,  deltaZ,  &vs1);
  	if(errNum != WLZ_ERR_NONE)
   	{
        	printf("can not get Dx Dy err.\n");
         	exit(1);
   
   	}
  	errNum = WlzGetDxDy( nfp,  deltaZ,  &vf1);
  	if(errNum != WLZ_ERR_NONE)
   	{
        	printf("can not get Dx Dy err.\n");
         	exit(1);
   
   	}
  	errNum = WlzGetDxDy( ntp,  deltaZ,  &vt1);
  	if(errNum != WLZ_ERR_NONE)
   	{
        	printf("can not get Dx Dy err.\n");
         	exit(1);
   
   	}


       /* target points */
   	(vtxVec1    )->vtX = vsr.d3.vtX + vs1.d3.vtX;
   	(vtxVec1    )->vtY = vsr.d3.vtY + vs1.d3.vtY;
   	(vtxVec1 + 1)->vtX = vtr.d3.vtX + vt1.d3.vtX;
   	(vtxVec1 + 1)->vtY = vtr.d3.vtY + vt1.d3.vtY;
   	(vtxVec1 + 2)->vtX = vfr.d3.vtX + vf1.d3.vtX;
   	(vtxVec1 + 2)->vtY = vfr.d3.vtY + vf1.d3.vtY;
 
   	/* ------ output the affine transformed Wlz ------*/
   	errNum =   outputTheWarpedWlz( vsp, 
                                       vtp,
				       vfp,
                                       vtxVec1,
                                       TwoDImageFilesNameList[cycleNext],
				       warpedFilesNameList[cycleNext]
				     );

   	/* track from s_i' to  s_{i+1}' */
   	/*  scale = |T - S |/|t - s|      */
        
        cycleI--;

	}



     AlcFree(vtxVec1 );

   /* */
       WlzFree3DViewStruct(wlzViewStr);
       WlzFree3DViewStruct(wlzViewStr1);

  return ( 0 );
}