示例#1
0
// -----------------------------------------------------------------------------
//
// -----------------------------------------------------------------------------
void Hex2SqrConverter::execute()
{
  std::stringstream ss;
  herr_t err = 0;

  std::vector<int> indices;
  // Loop on Each EBSD File
  float total = static_cast<float>( m_ZEndIndex - m_ZStartIndex );
  int progress = 0;
  int64_t z = m_ZStartIndex;
  /* There is a frailness about the z index and the file list. The programmer
   * using this code MUST ensure that the list of files that is sent into this
   * class is in the appropriate order to match up with the z index (slice index)
   * otherwise the import will have subtle errors. The programmer is urged NOT to
   * simply gather a list from the file system as those lists are sorted in such
   * a way that if the number of digits appearing in the filename are NOT the same
   * then the list will be wrong, ie, this example:
   *
   * slice_1.ang
   * slice_2.ang
   * ....
   * slice_10.ang
   *
   * Most, if not ALL C++ libraries when asked for that list will return the list
   * sorted like the following:
   *
   * slice_1.ang
   * slice_10.ang
   * slice_2.ang
   *
   * which is going to cause problems because the data is going to be placed
   * into the HDF5 file at the wrong index. YOU HAVE BEEN WARNED.
   */
  // int totalSlicesImported = 0;
  for (std::vector<std::string>::iterator filepath = m_EbsdFileList.begin(); filepath != m_EbsdFileList.end(); ++filepath)
  {
    std::string ebsdFName = *filepath;

    progress = static_cast<int>( z - m_ZStartIndex );
    progress = (int)(100.0f * (float)(progress) / total);
    std::string msg = "Converting File: " + ebsdFName;
    ss.str("");

    notifyStatusMessage(msg.c_str());

    // Write the Manufacturer of the OIM file here
    // This list will grow to be the number of EBSD file formats we support
    std::string ext = MXAFileInfo::extension(ebsdFName);
    std::string base = MXAFileInfo::fileNameWithOutExtension(ebsdFName);
    std::string path = MXAFileInfo::parentPath(ebsdFName);
    if(ext.compare(Ebsd::Ang::FileExt) == 0)
    {
      AngReader reader;
      reader.setFileName(ebsdFName);
      reader.setReadHexGrid(true);
      int err = reader.readFile();

      if(err < 0 && err != -600)
      {
        addErrorMessage(getHumanLabel(), reader.getErrorMessage(), reader.getErrorCode());
        setErrorCondition(reader.getErrorCode());
        return;
      }
      else if(reader.getGrid().find(Ebsd::Ang::SquareGrid) == 0)
      {
        ss.str("");
        ss << "Ang File is already a square grid: " << ebsdFName;
        setErrorCondition(-55000);
        addErrorMessage(getHumanLabel(), ss.str(), getErrorCondition());
        return;
      }
      else
      {
        if (err == -600)
        {
          notifyWarningMessage( reader.getErrorMessage(), reader.getErrorCode() );
        }
        std::string origHeader = reader.getOriginalHeader();
        if (origHeader.empty() == true)
        {
          ss.str();
          ss << "Header could not be retrieved: " << ebsdFName;
          setErrorCondition(-55001);
          addErrorMessage(getHumanLabel(), ss.str(), getErrorCondition());
        }
        char buf[kBufferSize];
        std::stringstream in(origHeader);

        std::string newEbsdFName = path + "/Sqr_" + base + "." + ext;
        std::ofstream outFile;
        outFile.open(newEbsdFName.c_str());

        m_HeaderIsComplete = false;

        float HexXStep = reader.getXStep();
        float HexYStep = reader.getYStep();
        int HexNumColsOdd = reader.getNumOddCols();
        int HexNumColsEven = reader.getNumEvenCols();
        int HexNumRows = reader.getNumRows();
        m_NumCols = (HexNumColsOdd*HexXStep)/m_XResolution;
        m_NumRows = (HexNumRows*HexYStep)/m_YResolution;
        float xSqr, ySqr, xHex1, yHex1, xHex2, yHex2;
        int point, point1, point2;
        int row1, row2, col1, col2;
        float dist1, dist2;
        float* phi1 = reader.getPhi1Pointer();
        float* PHI = reader.getPhiPointer();
        float* phi2 = reader.getPhi2Pointer();
        float* ci = reader.getConfidenceIndexPointer();
        float* iq = reader.getImageQualityPointer();
        float* semsig = reader.getSEMSignalPointer();
        float* fit = reader.getFitPointer();
        int* phase = reader.getPhaseDataPointer();
        while (!in.eof())
        {
          std::string line;
          ::memset(buf, 0, kBufferSize);
          in.getline(buf, kBufferSize);
          line = modifyAngHeaderLine(buf, kBufferSize);
          if(m_HeaderIsComplete == false) outFile << line << std::endl;
        }
        for(int j = 0; j < m_NumRows; j++)
        {
          for(int i = 0; i < m_NumCols; i++)
          {
            xSqr = float(i)*m_XResolution;
            ySqr = float(j)*m_YResolution;
            row1 = ySqr/(HexYStep);
            yHex1 = row1*HexYStep;
            row2 = row1 + 1;
            yHex2 = row2*HexYStep;
            if(row1%2 == 0)
            {
              col1 = xSqr/(HexXStep);
              xHex1 = col1*HexXStep;
              point1 = ((row1/2)*HexNumColsEven) + ((row1/2)*HexNumColsOdd) + col1;
              col2 = (xSqr-(HexXStep/2.0))/(HexXStep);
              xHex2 = col2*HexXStep + (HexXStep/2.0);
              point2 = ((row1/2)*HexNumColsEven) + (((row1/2)+1)*HexNumColsOdd) + col2;
            }
            else
            {
              col1 = (xSqr-(HexXStep/2.0))/(HexXStep);
              xHex1 = col1*HexXStep + (HexXStep/2.0);
              point1 = ((row1/2)*HexNumColsEven) + (((row1/2)+1)*HexNumColsOdd) + col1;
              col2 = xSqr/(HexXStep);
              xHex2 = col2*HexXStep;
              point2 = (((row1/2)+1)*HexNumColsEven) + (((row1/2)+1)*HexNumColsOdd) + col2;
            }
            dist1 = ((xSqr-xHex1)*(xSqr-xHex1)) + ((ySqr-yHex1)*(ySqr-yHex1));
            dist2 = ((xSqr-xHex2)*(xSqr-xHex2)) + ((ySqr-yHex2)*(ySqr-yHex2));
            if(dist1 <= dist2 || row1 == (HexNumRows-1)) {point = point1;}
            else {point = point2;}
            outFile << "  " << phi1[point] << "	" << PHI[point] << "	" << phi2[point] << "	" << xSqr << "	" << ySqr << "	" << iq[point] << "	" << ci[point] << "	" << phase[point] << "	" << semsig[point] << "	" << fit[point] << "	" << std::endl;
          }
        }
      }
    }
    else if(ext.compare(Ebsd::Ctf::FileExt) == 0)
    {
      std::cout << "Ctf files are not on a hexagonal grid and do not need to be converted." << std::endl;
    }
    else
    {
      err = -1;
      ss.str("");
      ss << "The File extension was not detected correctly";
      addErrorMessage(getHumanLabel(), ss.str(), err);
      setErrorCondition(-1);
      return;
    }

  }

  notifyStatusMessage("Import Complete");
}