예제 #1
0
extern void FindSolution(float *dX, float *dY, float *Orient,
			 float *X1, float *Y1, float *M1, int NL1,
			 float *X2, float *Y2, float *M2, int NL2,
			 int *NNeigh, int **NeighList,
			 SuperAlignParmRec *PSAPP)
/* Tune up the best fit *dX, *dY, and *Orient that matches
   the (X1,Y1,M1) array with the (X2,Y2,M2) array.  Since the solution
   should already be very close, no need to compute the number of neighbors
   or neighbor lists for each object -- contained in NNeigh and NNeighList */
{
  float Score,dX1,dY1,Orient1,D,BestScore,NumC,BestNumC;
  int I;
  long Global2;

  Score = CalcSigma(*dX,*dY,*Orient,X1,Y1,M1,NL1,X2,Y2,M2,NL2,
		    NNeigh,NeighList,PSAPP,0,NULL,NULL,&NumC);
  BestScore = Score;
  BestNumC = NumC;
  Global2 = -1;
  /* Use a simulated annealing type procedure to determine the
     solution with the highest score */
  for (I=0;I<1000;I++) {
    D = PSAPP->MaxDist*exp(-(float)I/200)/4;
    Orient1 = *Orient + (0.5-ran3(&Global2))*D/100;
    dX1 = *dX + (0.5-ran3(&Global2))*D;
    dY1 = *dY + (0.5-ran3(&Global2))*D;
    Score = CalcSigma(dX1,dY1,Orient1,X1,Y1,M1,NL1,X2,Y2,M2,NL2,
		      NNeigh,NeighList,PSAPP,0,NULL,NULL,&NumC);
    if (Score > BestScore) {
      BestScore = Score;
      *dX = dX1;
      *dY = dY1;
      *Orient = Orient1;
    }
    else {
      Score = BestScore;
      NumC = BestNumC;
    }
  }
}
예제 #2
0
extern int ImproveAlignment(StarCatRec *PStarCatBaseProp,
			    StarCatRec *PStarCatProp,
			    SuperAlignParmRec *PSAPP,
			    float *PBestdX, float *PBestdY,
			    float *PBestRotAngle)
/* BestX > 0 ... means PStarCatProp is to the right of PStarCatBaseProp */
/* BestX < 0 ... means PStarCatProp is to the left of PStarCatBaseProp */
/* BestRotAngle > 0 means PStarCatProp is CCW of PStarCatBaseProp */
/* BestRotAngle < 0 means PStarCatProp is CW of PStarCatBaseProp */
{
  FILE *fout;
  int NL1,NL2,I,J;
  float CosF,SinF,ShiftX,ShiftY,RotateAng;
  float *X1,*Y1,*M1,*X2,*Y2,*M2,BestdX,BestdY,BestOrient,BestScore,Score;
  float *TX2,*TY2,NumC,YDiff,XDiff,TempX2,TempY2,BestNumC;
  int *POK1,*POK2,*ind,*ind2;
  int *NNeigh,**NeighList;
  float *PAng1,*PAng2,*PAngT,*PDist1,*PDist2,*PDistT,Ang;

  DetermineGuess(PStarCatBaseProp,PStarCatProp,
		 &ShiftX,&ShiftY,&RotateAng);
  Verbose = 1;
  
  fprintf(stdout,"Aligning %s (%i objs) to %s (%i objs)\n",
	  PStarCatProp->FN,PStarCatProp->NObj,
	  PStarCatBaseProp->FN,PStarCatBaseProp->NObj);
  ShowGuess(PStarCatBaseProp,PStarCatProp,ShiftX,ShiftY,RotateAng);
  NL1 = PStarCatBaseProp->NObj;
  X1 = (float *)calloc(NL1,sizeof(float));
  Y1 = (float *)calloc(NL1,sizeof(float));
  M1 = (float *)calloc(NL1,sizeof(float));
  POK1 = (int *)calloc(NL1,sizeof(int));
  for (I=0;I<NL1;I++) {
    X1[I] = PStarCatBaseProp->PX[I];
    Y1[I] = PStarCatBaseProp->PY[I];
    M1[I] = pow(10.0,-0.4*PStarCatBaseProp->PM[I]);
  }
  CosF = cos(M_PI*RotateAng/180);
  SinF = sin(M_PI*RotateAng/180);
  NL2 = PStarCatProp->NObj;
  TX2 = (float *)calloc(NL2,sizeof(float));
  TY2 = (float *)calloc(NL2,sizeof(float));
  X2 = (float *)calloc(NL2,sizeof(float));
  Y2 = (float *)calloc(NL2,sizeof(float));
  M2 = (float *)calloc(NL2,sizeof(float));
  POK2 = (int *)calloc(NL2,sizeof(int));
  for (I=0;I<NL2;I++) {
    TX2[I] = ShiftX+PStarCatProp->PX[I]*CosF+
      PStarCatProp->PY[I]*SinF;
    TY2[I] = ShiftY-PStarCatProp->PX[I]*SinF+
      PStarCatProp->PY[I]*CosF;
    X2[I] = PStarCatProp->PX[I];
    Y2[I] = PStarCatProp->PY[I];
    M2[I] = pow(10.0,-0.4*PStarCatProp->PM[I]);
  }

  RemoveClosePairs(X1,Y1,NL1,1,PSAPP);
  RemoveClosePairs(TX2,TY2,NL2,-1,PSAPP);

  NNeigh = (int *)calloc(NL1,sizeof(int));
  NeighList = (int **)calloc(NL1,sizeof(int *));
  NeighborList(X1,Y1,NL1,TX2,TY2,NL2,PSAPP->MaxDist,
	       NNeigh,NeighList);

  BestdX = ShiftX;
  BestdY = ShiftY;
  BestOrient = RotateAng;

  FindSolution(&BestdX,&BestdY,&BestOrient,X1,Y1,M1,NL1,X2,Y2,M2,NL2,
	       NNeigh,NeighList,PSAPP);

  Score = CalcSigma(BestdX,BestdY,BestOrient,X1,Y1,M1,NL1,X2,Y2,M2,NL2,
		    NNeigh,NeighList,PSAPP,1,POK1,POK2,&NumC);
  free(X1);
  free(Y1);
  free(M1);
  free(POK1);
  free(TX2);
  free(TY2);
  free(X2);
  free(Y2);
  free(M2);
  free(POK2);
  *PBestdX = BestdX;
  *PBestdY = BestdY;
  *PBestRotAngle = BestOrient;
  fprintf(stdout,"Converged on %g good stars.\n",NumC);
  /*  fprintf(stdout,"Solution: dx = %.2f, dy = %.2f, theta = %.2f\n",
      BestdX,BestdY,BestOrient);*/
}
예제 #3
0
extern int TryAlignment(StarCatRec *PStarCatBaseProp,
			StarCatRec *PStarCatProp,
			SuperAlignParmRec *PSAPP,
			float *PBestdX, float *PBestdY,
			float *PBestRotAngle)
/* BestX > 0 ... means PStarCatProp is to the right of PStarCatBaseProp */
/* BestX < 0 ... means PStarCatProp is to the left of PStarCatBaseProp */
/* BestRotAngle > 0 means PStarCatProp is CCW of PStarCatBaseProp */
/* BestRotAngle < 0 means PStarCatProp is CW of PStarCatBaseProp */
{
  FILE *fout;
  int NL1,NL2,I,J;
  float CosF,SinF,ShiftX,ShiftY,RotateAng;
  float *X1,*Y1,*M1,*X2,*Y2,*M2,BestdX,BestdY,BestOrient,BestScore,Score;
  float *TX2,*TY2,NumC,YDiff,XDiff,TempX2,TempY2,BestNumC;
  int *POK1,*POK2,*ind,*ind2;
  int *NNeigh,**NeighList;
  float *PAng1,*PAng2,*PAngT,*PDist1,*PDist2,*PDistT,Ang;
  int K,K1,LL,HL,L,Tot,NTot;

  DetermineGuess(PStarCatBaseProp,PStarCatProp,
		 &ShiftX,&ShiftY,&RotateAng);
  Verbose = 1;
  
  fprintf(stdout,"Aligning %s (%i objs) to %s (%i objs)\n",
	  PStarCatProp->FN,PStarCatProp->NObj,
	  PStarCatBaseProp->FN,PStarCatBaseProp->NObj);
  ShowGuess(PStarCatBaseProp,PStarCatProp,ShiftX,ShiftY,RotateAng);
  NL1 = PStarCatBaseProp->NObj;
  X1 = (float *)calloc(NL1,sizeof(float));
  Y1 = (float *)calloc(NL1,sizeof(float));
  M1 = (float *)calloc(NL1,sizeof(float));
  POK1 = (int *)calloc(NL1,sizeof(int));
  for (I=0;I<NL1;I++) {
    X1[I] = PStarCatBaseProp->PX[I];
    Y1[I] = PStarCatBaseProp->PY[I];
    M1[I] = pow(10.0,-0.4*PStarCatBaseProp->PM[I]);
  }
  CosF = cos(M_PI*RotateAng/180);
  SinF = sin(M_PI*RotateAng/180);
  NL2 = PStarCatProp->NObj;
  TX2 = (float *)calloc(NL2,sizeof(float));
  TY2 = (float *)calloc(NL2,sizeof(float));
  X2 = (float *)calloc(NL2,sizeof(float));
  Y2 = (float *)calloc(NL2,sizeof(float));
  M2 = (float *)calloc(NL2,sizeof(float));
  POK2 = (int *)calloc(NL2,sizeof(int));
  for (I=0;I<NL2;I++) {
    TX2[I] = ShiftX+PStarCatProp->PX[I]*CosF+
      PStarCatProp->PY[I]*SinF;
    TY2[I] = ShiftY-PStarCatProp->PX[I]*SinF+
      PStarCatProp->PY[I]*CosF;
    X2[I] = PStarCatProp->PX[I];
    Y2[I] = PStarCatProp->PY[I];
    M2[I] = pow(10.0,-0.4*PStarCatProp->PM[I]);
  }

  OutputXYM(X1,Y1,M1,NL1,"A");
  OutputXYM(TX2,TY2,M2,NL2,"B");
  OutputXYM(X2,Y2,M2,NL2,"C");
  RemoveClosePairs(X1,Y1,NL1,1,PSAPP);
  RemoveClosePairs(TX2,TY2,NL2,-1,PSAPP);

  NNeigh = (int *)calloc(NL1,sizeof(int));
  NeighList = (int **)calloc(NL1,sizeof(int *));
  NeighborList(X1,Y1,NL1,TX2,TY2,NL2,PSAPP->MaxShiftErr,
	       NNeigh,NeighList);
  BestScore = 0;
  ind2 = (int *)calloc(NL2,sizeof(int));
  PAngT = (float *)calloc(NL2,sizeof(float));
  PDistT = (float *)calloc(NL2,sizeof(float));
  PAng2 = (float *)calloc(NL2,sizeof(float));
  PDist2 = (float *)calloc(NL2,sizeof(float));
  PAng1 = (float *)calloc(NL1,sizeof(float));
  PDist1 = (float *)calloc(NL1,sizeof(float));

  ind = (int *)calloc(NL1,sizeof(int));
  hpsort(NL1,M1,ind);
  BestdX = 0.0;
  BestdY = 0.0;
  BestOrient = 0.0;
  for (I=NL1-1;I>=0;I--) {
    int Wh;

    Wh = ind[I];
    if (!strcmp(PStarCatBaseProp->FN,
		PStarCatProp->FN)) {
      NNeigh[Wh] = 0;
      I = 0;
    }
    
    for (J=0;J<NNeigh[Wh];J++) {
      float Score,dX,dY;
      
      for (K=0;K<NL2;K++) {
	XDiff = X2[K] - X2[NeighList[Wh][J]];
	YDiff = Y2[K] - Y2[NeighList[Wh][J]];
	PAngT[K] = (180/M_PI)*atan2(YDiff,XDiff);
	PDistT[K] = sqrt(YDiff*YDiff+XDiff*XDiff);
      }
      hpsort(NL2,PAngT,ind2);
      for (K=0;K<NL2;K++) {
	PAng2[K] = PAngT[ind2[K]];
	PDist2[K] = PDistT[ind2[K]];
      }
      for (K=0;K<NL1;K++) {
	XDiff = X1[K]-X1[Wh];
	YDiff = Y1[K]-Y1[Wh];
	PAng1[K] = (180/M_PI)*atan2(YDiff,XDiff);
	PDist1[K] = sqrt(YDiff*YDiff+XDiff*XDiff);
	
	for (K1=-1;K1<2;K1++) {
	  LL = BinSearchF(PAng2,PAng1[K]+RotateAng-PSAPP->MaxRotErr+K1*360,
			  NL2-1,TakeBound);
	  HL = BinSearchF(PAng2,PAng1[K]+RotateAng+PSAPP->MaxRotErr+K1*360,
			  NL2-1,TakeBound)+1;
	  if (LL < 0) {
	    LL = BinSearchF(PAng2,PAng1[K]+RotateAng-PSAPP->MaxRotErr+K1*360,
			    NL2-1,TakeBound);
	    HL = BinSearchF(PAng2,PAng1[K]+RotateAng+PSAPP->MaxRotErr+K1*360,
			    NL2-1,TakeBound)+1;	    
	  }

	  for (L=LL;L<HL;L++) {
	    if ((fabs(PAng1[K]+RotateAng+K1*360-PAng2[L])<PSAPP->MaxRotErr)&&
		(fabs(PDist1[K] - PDist2[L]) < PSAPP->MaxDist)) {
	      Ang = PAng2[L]-PAng1[K];
	      CosF = cos(M_PI*Ang/180);
	      SinF = sin(M_PI*Ang/180);
	      TempX2 = X2[NeighList[Wh][J]]*CosF+Y2[NeighList[Wh][J]]*SinF;
	      TempY2 = -X2[NeighList[Wh][J]]*SinF+Y2[NeighList[Wh][J]]*CosF;
	      dX = X1[Wh]-TempX2;
	      dY = Y1[Wh]-TempY2;
	      Score = CalcSigma(dX,dY,Ang,X1,Y1,M1,NL1,X2,Y2,M2,NL2,
				NNeigh,NeighList,PSAPP,
				0,NULL,NULL,&NumC);
	      if (Score > BestScore) {
		BestScore = CalcSigma(dX,dY,Ang,X1,Y1,M1,NL1,X2,Y2,M2,NL2,
				      NNeigh,NeighList,PSAPP,
				      0,NULL,NULL,&NumC);
		BestdX = dX;
		BestdY = dY;
		BestOrient = Ang;
		BestNumC = NumC;
		if (NumC > 18) {
                  /* if more than 30 matches are obtained, this is good
                     enough and we will simply abort. */
		  I = 0;
		  J = NNeigh[Wh];
		  L = HL;
		  K1 = 2;
		  K = NL1;
		}
	      }
	    }
	  }
	}
      }
    }
  }
  /* Attempt one last iterative improvement to best transformation
     obtained above */
  FindSolution(&BestdX,&BestdY,&BestOrient,X1,Y1,M1,NL1,X2,Y2,M2,NL2,
	       NNeigh,NeighList,PSAPP);

  Score = CalcSigma(BestdX,BestdY,BestOrient,X1,Y1,M1,NL1,X2,Y2,M2,NL2,
		    NNeigh,NeighList,PSAPP,1,POK1,POK2,&NumC);
  free(X1);
  free(Y1);
  free(M1);
  free(POK1);
  free(TX2);
  free(TY2);
  free(X2);
  free(Y2);
  free(M2);
  free(POK2);
  free(ind);
  free(ind2);
  free(PAngT);
  free(PDistT);
  free(PAng2);
  free(PDist2);
  free(PAng1);
  free(PDist1);
  free(NNeigh);
  for (I=0;I<NL1;I++)
    free(NeighList[I]);
  free(NeighList);

  *PBestdX = BestdX;
  *PBestdY = BestdY;
  *PBestRotAngle = BestOrient;
  fprintf(stdout,"Converged on %g good stars.\n",NumC);
  /*  fprintf(stdout,"Solution: dx = %.2f, dy = %.2f, theta = %.2f\n",
      BestdX,BestdY,BestOrient);*/
}
예제 #4
0
void svaria_AppearanceSpacings (unif01_Gen * gen, sres_Basic * res,
   long N, long Q, long K, int r, int s, int L)
{
   double E[AS_DIM + 1];          /* Theoretical mean of the log (Base2) of
                                     the most recent occurrence of a block */
   double KV[AS_DIM + 1];         /* K times the theoretical variance of the
                                     same */
   long Seq;
   long block;
   long Nblocks;                  /* 2^L = total number of distinct blocks */
   long K2;
   long Q2;
   long i;
   long sBits;                    /* Numerical value of the s given bits */
   long d;                        /* 2^s */
   const int SdivL = s / L;
   const int LdivS = L / s;
   const int LmodS = L % s;
   long sd;                       /* 2^LmodS */
   long rang;
   double n;                      /* Total number of bits in a sequence */
   double sigma;                  /* Standard deviation = sqrt (Variance) */
   double somme;
   double ARang;                  /* Most recent occurrence of block */
   long *Count;                   /* Index of most recent occurrence of
                                     block */
   double FactMoy;
   lebool localRes = FALSE;
   chrono_Chrono *Timer;

   Timer = chrono_Create ();
   n = ((double) K + (double) Q) * L;
   if (swrite_Basic)
      WriteDataAppear (gen, N, r, s, L, Q, K, n);
   util_Assert (s < 32, "svaria_AppearanceSpacings:   s >= 32");
   InitAppear (r, s, L, Q, E, KV);
   sigma = CalcSigma (L, K, KV);
   d = num_TwoExp[s];
   Nblocks = num_TwoExp[L];
   FactMoy = 1.0 / (num_Ln2 * K);

   if (res == NULL) {
      localRes = TRUE;
      res = sres_CreateBasic ();
   }
   sres_InitBasic (res, N, "svaria_AppearanceSpacings");
   Count = util_Calloc ((size_t) Nblocks + 2, sizeof (long));

   statcoll_SetDesc (res->sVal1,
      "AppearanceSpacings sVal1:   standard normal");

   if (LdivS > 0) {
      sd = num_TwoExp[LmodS];

      for (Seq = 1; Seq <= N; Seq++) {
         for (i = 0; i < Nblocks; i++)
            Count[i] = 0;

         /* Initialization with Q blocks */
         for (rang = 0; rang < Q; rang++) {
            block = 0;
            for (i = 1; i <= LdivS; i++) {
               sBits = unif01_StripB (gen, r, s);
               block = block * d + sBits;
            }
            if (LmodS > 0) {
               sBits = unif01_StripB (gen, r, LmodS);
               block = block * sd + sBits;
            }
            Count[block] = rang;
         }

         /* Test proper with K blocks */
         somme = 0.0;
         for (rang = Q; rang < Q + K; rang++) {
            block = 0;
            for (i = 1; i <= LdivS; i++) {
               sBits = unif01_StripB (gen, r, s);
               block = block * d + sBits;
            }
            if (LmodS > 0) {
               sBits = unif01_StripB (gen, r, LmodS);
               block = block * sd + sBits;
            }
            ARang = rang - Count[block];
            somme += log (ARang);
            Count[block] = rang;
         }
         statcoll_AddObs (res->sVal1, (somme * FactMoy - E[L]) / sigma);
      }

   } else {                       /* s > L */
      Q2 = Q / SdivL;
      K2 = K / SdivL;
      for (Seq = 1; Seq <= N; Seq++) {
         for (i = 0; i < Nblocks; i++)
            Count[i] = 0;

         /* Initialization: Q blocks */
         for (rang = 0; rang < Q2; rang++) {
            sBits = unif01_StripB (gen, r, s);
            for (i = 0; i < SdivL; i++) {
               block = sBits % Nblocks;
               Count[block] = SdivL * rang + i;
               sBits /= Nblocks;
            }
         }
         /* Test proper with K blocks */
         somme = 0.0;
         for (rang = Q2; rang < Q2 + K2; rang++) {
            sBits = unif01_StripB (gen, r, s);
            for (i = 0; i < SdivL; i++) {
               block = sBits % Nblocks;
               ARang = SdivL * rang + i - Count[block];
               somme += log (ARang);
               Count[block] = SdivL * rang + i;
               sBits /= Nblocks;
            }
         }
         statcoll_AddObs (res->sVal1, (somme * FactMoy - E[L]) / sigma);
      }
   }

   gofw_ActiveTests2 (res->sVal1->V, res->pVal1->V, N, wdist_Normal,
      (double *) NULL, res->sVal2, res->pVal2);
   res->pVal1->NObs = N;
   sres_GetNormalSumStat (res);

   if (swrite_Collectors)
      statcoll_Write (res->sVal1, 5, 12, 4, 3);

   if (swrite_Basic) {
      gofw_WriteActiveTests2 (N, res->sVal2, res->pVal2,
         "Normal statistic                      :");
      swrite_NormalSumTest (N, res);
      swrite_Final (gen, Timer);
   }
   util_Free (Count);
   if (localRes)
      sres_DeleteBasic (res);
   chrono_Delete (Timer);
}