// -----------------------------------------------------------------------------
//
// -----------------------------------------------------------------------------
void AlignSections::execute()
{
  setErrorCondition(0);
  dataCheck();
  if(getErrorCondition() < 0) { return; }

  DataContainer::Pointer m = getDataContainerArray()->getDataContainer(getDataContainerName());

  size_t dims[3] = { 0, 0, 0 };
  m->getGeometryAs<ImageGeom>()->getDimensions(dims);

  int64_t xspot = 0, yspot = 0;
  int64_t newPosition = 0;
  int64_t currentPosition = 0;

  std::vector<int64_t> xshifts(dims[2], 0);
  std::vector<int64_t> yshifts(dims[2], 0);

  find_shifts(xshifts, yshifts);


  QList<QString> voxelArrayNames = m->getAttributeMatrix(getCellAttributeMatrixName())->getAttributeArrayNames();
  size_t progIncrement = dims[2] / 100;
  size_t prog = 1;
  size_t progressInt = 0;
  size_t slice = 0;

  for (size_t i = 1; i < dims[2]; i++)
  {
    if (i > prog)
    {

      progressInt = ((float)i / dims[2]) * 100.0f;
      QString ss = QObject::tr("Transferring Cell Data || %1% Complete").arg(progressInt);
      notifyStatusMessage(getMessagePrefix(), getHumanLabel(), ss);
      prog = prog + progIncrement;
    }
    if (getCancel() == true)
    {
      return;
    }
    slice = (dims[2] - 1) - i;
    for (size_t l = 0; l < dims[1]; l++)
    {
      for (size_t n = 0; n < dims[0]; n++)
      {
        if (yshifts[i] >= 0) { yspot = l; }
        else if (yshifts[i] < 0) { yspot = dims[1] - 1 - l; }
        if (xshifts[i] >= 0) { xspot = n; }
        else if (xshifts[i] < 0) { xspot = dims[0] - 1 - n; }
        newPosition = (slice * dims[0] * dims[1]) + (yspot * dims[0]) + xspot;
        currentPosition = (slice * dims[0] * dims[1]) + ((yspot + yshifts[i]) * dims[0]) + (xspot + xshifts[i]);
        if ((yspot + yshifts[i]) >= 0 && (yspot + yshifts[i]) <= static_cast<int64_t>(dims[1]) - 1 && (xspot + xshifts[i]) >= 0
            && (xspot + xshifts[i]) <= static_cast<int64_t>(dims[0]) - 1)
        {
          for (QList<QString>::iterator iter = voxelArrayNames.begin(); iter != voxelArrayNames.end(); ++iter)
          {
            IDataArray::Pointer p = m->getAttributeMatrix(getCellAttributeMatrixName())->getAttributeArray(*iter);
            p->copyTuple( static_cast<size_t>(currentPosition), static_cast<size_t>(newPosition));
          }
        }
        if ((yspot + yshifts[i]) < 0 || (yspot + yshifts[i]) > static_cast<int64_t>(dims[1] - 1) || (xspot + xshifts[i]) < 0
            || (xspot + xshifts[i]) > static_cast<int64_t>(dims[0]) - 1)
        {
          for (QList<QString>::iterator iter = voxelArrayNames.begin(); iter != voxelArrayNames.end(); ++iter)
          {
            IDataArray::Pointer p = m->getAttributeMatrix(getCellAttributeMatrixName())->getAttributeArray(*iter);
            EXECUTE_FUNCTION_TEMPLATE(this, initializeArrayValues, p, p, newPosition)
          }
        }
      }
    }
  }

  // If there is an error set this to something negative and also set a message
  notifyStatusMessage(getHumanLabel(), "Complete");
}
// -----------------------------------------------------------------------------
//
// -----------------------------------------------------------------------------
void AlignSections::execute()
{
  setErrorCondition(0);
  VoxelDataContainer* m = getVoxelDataContainer();
  if (NULL == m)
  {
    setErrorCondition(-1);
    std::stringstream ss;
    ss << " DataContainer was NULL";
    notifyErrorMessage(ss.str(), -1);
    return;
  }

  int64_t totalPoints = m->getTotalPoints();
  size_t numgrains = m->getNumFieldTuples();
  size_t numensembles = m->getNumEnsembleTuples();
  dataCheck(false, totalPoints, numgrains, numensembles);
  if (getErrorCondition() < 0)
  {
    return;
  }

  size_t udims[3] = {0,0,0};
  m->getDimensions(udims);
#if (CMP_SIZEOF_SIZE_T == 4)
  typedef int32_t DimType;
#else
  typedef int64_t DimType;
#endif
  DimType dims[3] = {
    static_cast<DimType>(udims[0]),
    static_cast<DimType>(udims[1]),
    static_cast<DimType>(udims[2]),
  };

  int slice;
  int xspot, yspot;
  DimType newPosition;
  DimType currentPosition;
  //  unsigned int  phase2;

  std::vector<int> xshifts;
  std::vector<int> yshifts;
  xshifts.resize(dims[2],0);
  yshifts.resize(dims[2],0);

  find_shifts(xshifts, yshifts);

  std::list<std::string> voxelArrayNames = m->getCellArrayNameList();
  DimType progIncrement = dims[2]/100;
  DimType prog = 1;
  int progressInt = 0;
  std::stringstream ss;

  for (DimType i = 1; i < dims[2]; i++)
  {
    if (i > prog)
    {
      ss.str("");
      progressInt = ((float)i/dims[2])*100.0;
      ss << "Transferring Cell Data - " << progressInt << "% Complete";
      notifyStatusMessage(ss.str());
      prog = prog + progIncrement;
    }
    if (getCancel() == true)
    {
      return;
    }
    slice = static_cast<int>( (dims[2] - 1) - i );
    for (DimType l = 0; l < dims[1]; l++)
    {
      for (DimType n = 0; n < dims[0]; n++)
      {
        if(yshifts[i] >= 0) yspot = static_cast<int>(l);
        else if(yshifts[i] < 0) yspot = static_cast<int>( dims[1] - 1 - l );
        if(xshifts[i] >= 0) xspot = static_cast<int>(n);
        else if(xshifts[i] < 0) xspot = static_cast<int>( dims[0] - 1 - n );
        newPosition = (slice * dims[0] * dims[1]) + (yspot * dims[0]) + xspot;
        currentPosition = (slice * dims[0] * dims[1]) + ((yspot + yshifts[i]) * dims[0]) + (xspot + xshifts[i]);
        if((yspot + yshifts[i]) >= 0 && (yspot + yshifts[i]) <= dims[1] - 1 && (xspot + xshifts[i]) >= 0
           && (xspot + xshifts[i]) <= dims[0] - 1)
        {
          for(std::list<std::string>::iterator iter = voxelArrayNames.begin(); iter != voxelArrayNames.end(); ++iter)
          {
            std::string name = *iter;
            IDataArray::Pointer p = m->getCellData(*iter);
            p->CopyTuple(currentPosition, newPosition);
          }
        }
        if((yspot + yshifts[i]) < 0 || (yspot + yshifts[i]) > dims[1] - 1 || (xspot + xshifts[i]) < 0
           || (xspot + xshifts[i]) > dims[0] - 1)
        {
          for(std::list<std::string>::iterator iter = voxelArrayNames.begin(); iter != voxelArrayNames.end(); ++iter)
          {
            std::string name = *iter;
            IDataArray::Pointer p = m->getCellData(*iter);
            p->InitializeTuple(newPosition, 0.0);          }
        }
      }
    }
  }

  // If there is an error set this to something negative and also set a message
  notifyStatusMessage("Complete");
}
Beispiel #3
0
int main(int argc, char *argv[])
{
  int no_error=1;       /* Flag set to 0 on error */
  int usefile=0;        /* Flag set to 1 to use input setup file */
  int ncat1,ncat2;      /* Number of catalog objects */
  int nshift=0;         /* Number of matching objects */
  int another_loop=1;   /* Flag set to 0 when ending a loop */
  char fitsfile1[MAXC]; /* The name of the 1st FITS file to load */
  char catfile1[MAXC];  /* The name of the 1st SExtractor catalog file */
  char fitsfile2[MAXC]; /* The name of the 2nd FITS file to load */
  char catfile2[MAXC];  /* The name of the 2nd SExtractor catalog file */
  char setupfile[MAXC]; /* Name of the optional setup file */
  char line[MAXC];      /* General string to read input */
  Pos initshift;        /* Initial shift between two images */
  Pos shiftmean;        /* Mean shift */
  Pos shiftrms;         /* RMS on shift */
  Pos shiftmed;         /* Median shift */
  Image *image1;        /* The container of the 1st FITS image array */
  Image *image2;        /* The container of the 2nd FITS image array */
  Setup *setup1;        /* Container for the 1st image display info */
  Setup *setup2;        /* Container for the 2nd image display info */
  Secat *catdat1=NULL;  /* SExtractor data for 1st catalog */
  Secat *catdat2=NULL;  /* SExtractor data for 2nd catalog */
  Secat *shiftcat=NULL; /* Catalog of shifts between 1st and 2nd catalogs */

  /*
   * Check the command line
   */

  if(argc < 5) {
    fprintf(stderr,"\nfind_dither fitsfile1 catfile1 fitsfile2 catfile2 ");
    fprintf(stderr,"(setupfile)\n\n");
    return 1;
  }

  /*
   * Get the names of the files.
   */

  strcpy(fitsfile1,argv[1]);
  strcpy(catfile1,argv[2]);
  strcpy(fitsfile2,argv[3]);
  strcpy(catfile2,argv[4]);
  if(argc == 6) {
    usefile = 1;
    strcpy(setupfile,argv[5]);
  }
  else
    sprintf(setupfile,"");

  /*
   * Read the 1st data array.
   */

  if(!(image1 = new_Image(fitsfile1))) {
    fprintf(stderr,"ERROR.  Exiting program. Could not open %s\n\n",
	    fitsfile1);
    return 1;
  }

  if(load_image_data(image1) == ERROR)
    no_error = 0;

  /*
   * Create setup structure and fill with information used to set plotting
   *  parameters.
   */

  if(no_error) 
    if(!(setup1 = fill_setup(image1,setupfile,usefile,PIXEL)))
      no_error = 0;

  /*
   * Loop over displaying image until happy with results
   */

  if(no_error) {
    open_plot_window();
    if(display_image(image1,setup1))
      no_error = 0;
    else
      while(no_error && another_loop) {
	switch(another_loop = setup_menu(image1,setup1)) {
	case 1:
	  if(display_image(image1,setup1))
	    no_error = 0;
	  break;
	case 0:
	  break;
	case -1:
	  no_error = 0;
	  break;
	default:
	  break;
	}
      }
  }

  another_loop = 1;

  /*
   * Read the 2nd data array.
   */

  if(!(image2 = new_Image(fitsfile2))) {
    fprintf(stderr,"ERROR.  Exiting program. Could not open %s\n\n",
	    fitsfile1);
    return 1;
  }

  if(load_image_data(image2) == ERROR)
    no_error = 0;

  /*
   * Create setup structure and fill with information used to set plotting
   *  parameters.
   */

  if(no_error)
    if(!(setup2 = fill_setup(image2,setupfile,usefile,PIXEL)))
      no_error = 0;

  /*
   * Loop over displaying image until happy with results
   */

  if(no_error) {
    open_plot_window();
    if(display_image(image2,setup2))
      no_error = 0;
    else
      while(no_error && another_loop) {
	switch(another_loop = setup_menu(image2,setup2)) {
	case 1:
	  if(display_image(image2,setup2))
	    no_error = 0;
	  break;
	case 0:
	  break;
	case -1:
	  no_error = 0;
	  break;
	default:
	  break;
	}
      }
  }

  another_loop = 1;

  /*
   * Read in the first SExtractor catalog file
   */

  printf("---------------------------------------------------------------\n");
  if(no_error)
    if(!(catdat1 = read_secat(catfile1,'#',&ncat1,6)))
      no_error = 0;

  /*
   * Plot the catalog
   */

  if(no_error) {
    cpgslct(1);
    printf("\nPlotting SExtractor positions for first catalog\n");
    if(plot_secat(setup1,catdat1,ncat1,2,3,2.0))
      no_error = 0;
  }

  /*
   * Read in second SExtractor catalog file
   */

  if(no_error)
    if(!(catdat2 = read_secat(catfile2,'#',&ncat2,6)))
      no_error = 0;

  /*
   * Plot the catalog
   */

  if(no_error) {
    cpgslct(2);
    printf("\nPlotting SExtractor positions for second catalog\n");
    if(plot_secat(setup2,catdat2,ncat2,2,3,2.0))
      no_error = 0;
  }

  /*
   * Get initial shift
   */

  if(no_error) {
    get_init_shift(catdat1,ncat1,catdat2,ncat2,&initshift);
  }

  /*
   * Calculate shifts between first and second catalogs
   */

  if(no_error)
    if(!(shiftcat = find_shifts(catdat1,ncat1,catdat2,ncat2,initshift,
				setup2,&nshift,0)))
      no_error = 0;
    else
      printf("\nCalculated shifts between %d pairs.\n",nshift);

  /*
   * Get statistics on the shifts and plot results
   */

  if(no_error)
    plot_shifts(shiftcat,nshift);

  /*
   * Clean up and exit
   */

  cpgend();
  cpgend();
  image1 = del_Image(image1);
  setup1 = del_setup(setup1);
  catdat1 = del_secat(catdat1);
  image2 = del_Image(image2);
  setup2 = del_setup(setup2);
  catdat2 = del_secat(catdat2);
  shiftcat = del_secat(shiftcat);
  
  if(no_error) {
    printf("\nProgram find_dither.c completed.\n\n");
    return 0;
  }
  else {
    fprintf(stderr,"\nERROR. Exiting find_dither.c\n\n");
    return 1;
  }
}
// -----------------------------------------------------------------------------
//
// -----------------------------------------------------------------------------
void AlignSections::execute()
{
  setErrorCondition(0);
  dataCheck();
  if(getErrorCondition() < 0) { return; }

  DataContainer::Pointer m = getDataContainerArray()->getDataContainer(getDataContainerName());

  size_t udims[3] = { 0, 0, 0 };
  m->getGeometryAs<ImageGeom>()->getDimensions(udims);
#if (CMP_SIZEOF_SIZE_T == 4)
  typedef int32_t DimType;
#else
  typedef int64_t DimType;
#endif
  DimType dims[3] =
  {
    static_cast<DimType>(udims[0]),
    static_cast<DimType>(udims[1]),
    static_cast<DimType>(udims[2]),
  };

  DimType slice = 0;
  DimType xspot = 0, yspot = 0;
  DimType newPosition = 0;
  DimType currentPosition = 0;

  std::vector<int64_t> xshifts(dims[2], 0);
  std::vector<int64_t> yshifts(dims[2], 0);

  find_shifts(xshifts, yshifts);

  if (getSubtractBackground())
  {
    /**fit x and y shifts to lines
     *
     * y = mx + b
     *
     * m = (n*sum(x_i * y_i) - sum(x_i) * sum(y_i)) / (n*sum(x_i^2)-sum(x_i)^2
     *
     * b = (sum(y_i)-m*sum(x_i))/n
     *
     */

    // same for both
    double sumX = 0.0; // sum(x_i)
    double sumX_2 = 0.0; // sum(x_i^2)

    // x shift line
    double x_sumY = 0.0; // sum(y_i)
    double x_sumXY = 0.0; // sum(x_i * y_i)

    // y shift line
    double y_sumY = 0.0; // sum(y_i)
    double y_sumXY = 0.0; // sum(x_i * y_i)

    for (DimType iter = 0; iter < dims[2]; iter++)
    {
      slice = static_cast<DimType>( (dims[2] - 1) - iter );
      sumX = static_cast<double>(sumX + iter);
      sumX_2 = static_cast<double>(sumX_2 + iter * iter);
      x_sumY = static_cast<double>(x_sumY + xshifts[iter]);
      x_sumXY = static_cast<double>(x_sumXY + iter * xshifts[iter]);
      y_sumY = static_cast<double>(y_sumY + yshifts[iter]);
      y_sumXY = static_cast<double>(y_sumXY + iter * yshifts[iter]);
    }

    double mx = static_cast<double>((dims[2] * x_sumXY - x_sumXY) / (dims[2] * sumX_2 - sumX));
    double my = static_cast<double>((dims[2] * y_sumXY - y_sumXY) / (dims[2] * sumX_2 - sumX));

    // adjust shifts so that fit line has 0 slope (~ends of the sample are fixed)
    for (DimType iter = 1; iter < dims[2]; iter++)
    {
      slice = (dims[2] - 1) - iter;
      xshifts[iter] = static_cast<int64_t>(xshifts[iter] - iter * mx);
      yshifts[iter] = static_cast<int64_t>(yshifts[iter] - iter * my);
    }
  }

  QList<QString> voxelArrayNames = m->getAttributeMatrix(getCellAttributeMatrixName())->getAttributeArrayNames();
  DimType progIncrement = dims[2] / 100;
  DimType prog = 1;
  DimType progressInt = 0;

  for (DimType i = 1; i < dims[2]; i++)
  {
    if (i > prog)
    {

      progressInt = ((float)i / dims[2]) * 100.0f;
      QString ss = QObject::tr("Transferring Cell Data || %1% Complete").arg(progressInt);
      notifyStatusMessage(getMessagePrefix(), getHumanLabel(), ss);
      prog = prog + progIncrement;
    }
    if (getCancel() == true)
    {
      return;
    }
    slice = (dims[2] - 1) - i;
    for (DimType l = 0; l < dims[1]; l++)
    {
      for (DimType n = 0; n < dims[0]; n++)
      {
        if (yshifts[i] >= 0) { yspot = l; }
        else if (yshifts[i] < 0) { yspot = dims[1] - 1 - l; }
        if (xshifts[i] >= 0) { xspot = n; }
        else if (xshifts[i] < 0) { xspot = dims[0] - 1 - n; }
        newPosition = (slice * dims[0] * dims[1]) + (yspot * dims[0]) + xspot;
        currentPosition = (slice * dims[0] * dims[1]) + ((yspot + yshifts[i]) * dims[0]) + (xspot + xshifts[i]);
        if ((yspot + yshifts[i]) >= 0 && (yspot + yshifts[i]) <= dims[1] - 1 && (xspot + xshifts[i]) >= 0
            && (xspot + xshifts[i]) <= dims[0] - 1)
        {
          for (QList<QString>::iterator iter = voxelArrayNames.begin(); iter != voxelArrayNames.end(); ++iter)
          {
            IDataArray::Pointer p = m->getAttributeMatrix(getCellAttributeMatrixName())->getAttributeArray(*iter);
            p->copyTuple(currentPosition, newPosition);
          }
        }
        if ((yspot + yshifts[i]) < 0 || (yspot + yshifts[i]) > dims[1] - 1 || (xspot + xshifts[i]) < 0
            || (xspot + xshifts[i]) > dims[0] - 1)
        {
          for (QList<QString>::iterator iter = voxelArrayNames.begin(); iter != voxelArrayNames.end(); ++iter)
          {
            IDataArray::Pointer p = m->getAttributeMatrix(getCellAttributeMatrixName())->getAttributeArray(*iter);
            p->initializeTuple(newPosition, 0);
          }
        }
      }
    }
  }

  // If there is an error set this to something negative and also set a message
  notifyStatusMessage(getHumanLabel(), "Complete");
}