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

  if (m_ApplyToVoxelVolume == true)
  {
    DataContainer::Pointer m = getDataContainerArray()->getDataContainer(getDataContainerName());
    ImageGeom::Pointer image = m->getGeometryAs<ImageGeom>();

    float resolution[3] = { 0.0f, 0.0f, 0.0f };
    image->getResolution(resolution);
    resolution[0] *= m_ScaleFactor.x;
    resolution[1] *= m_ScaleFactor.y;
    resolution[2] *= m_ScaleFactor.z;
    image->setResolution(resolution);
  }

  if (m_ApplyToSurfaceMesh == true)
  {
    updateSurfaceMesh();
  }

  notifyStatusMessage(getHumanLabel(), "Complete");
}
// -----------------------------------------------------------------------------
//
// -----------------------------------------------------------------------------
void SetOriginResolutionImageGeom::dataCheck()
{
  setErrorCondition(0);

  ImageGeom::Pointer image = getDataContainerArray()->getPrereqGeometryFromDataContainer<ImageGeom, AbstractFilter>(this, getDataContainerName());
  if(getErrorCondition() < 0)
  {
    return;
  }
  if(getChangeOrigin())
  {
    image->setOrigin(m_Origin.x, m_Origin.y, m_Origin.z);
  }
  if(getChangeResolution())
  {
    image->setResolution(m_Resolution.x, m_Resolution.y, m_Resolution.z);
  }
}
// -----------------------------------------------------------------------------
//
// -----------------------------------------------------------------------------
int VTKFileReader::readHeader()
{

  int err = 0;
  if (getInputFile().isEmpty() == true)
  {
    setErrorCondition(-1);
    notifyErrorMessage(getHumanLabel(), "FileName was not set and must be valid", -1);
    return -1;
  }

  if (NULL == getDataContainerArray()->getDataContainer(getDataContainerName()).get())
  {
    setErrorCondition(-1);
    notifyErrorMessage(getHumanLabel(), "DataContainer Pointer was NULL and must be valid", -1);
    return -1;
  }

  QFile in(getInputFile());
  if (!in.open(QIODevice::ReadOnly | QIODevice::Text))
  {
    QString msg = QObject::tr("VTF file could not be opened: %1").arg(getInputFile());
    setErrorCondition(-100);
    notifyErrorMessage(getHumanLabel(), msg, getErrorCondition());
    return -100;
  }

  QByteArray buf;
  buf = in.readLine(); // Read Line 1 - VTK Version Info

  buf = in.readLine(); // Read Line 2 - User Comment
  setComment(QString(buf.trimmed()));

  buf = in.readLine(); // Read Line 3 - BINARY or ASCII
  QString fileType(buf);
  if (fileType.startsWith("BINARY") == true)
  {
    setFileIsBinary(true);
  }
  else if (fileType.startsWith("ASCII") == true)
  {
    setFileIsBinary(false);
  }
  else
  {
    err = -1;
    qDebug()
        << "The file type of the VTK legacy file could not be determined. It should be ASCII' or 'BINARY' and should appear on line 3 of the file."
        ;
    return err;
  }


  QList<QByteArray> tokens;
  buf = in.readLine(); // Read Line 4 - Type of Dataset
  {
    tokens = buf.split(' ');
    setDatasetType(QString(tokens[1]));
  }


  buf = in.readLine(); // Read Line 5 which is the Dimension values
  bool ok = false;
  int64_t dims[3];
  tokens = buf.split(' ');
  dims[0] = tokens[1].toLongLong(&ok, 10);
  dims[1] = tokens[2].toLongLong(&ok, 10);
  dims[2] = tokens[3].toLongLong(&ok, 10);
#if   (CMP_SIZEOF_SSIZE_T==4)
  int64_t max = std::numeric_limits<size_t>::max();
#else
  int64_t max = std::numeric_limits<int64_t>::max();
#endif
  if (dims[0] * dims[1] * dims[2] > max )
  {
    err = -1;
    QString ss = QObject::tr("The total number of elements '%1' is greater than this program can hold. Try the 64 bit version.").arg(dims[0] * dims[1] * dims[2]);
    setErrorCondition(err);
    notifyErrorMessage(getHumanLabel(), ss, getErrorCondition());
    return err;
  }

  if (dims[0] > max || dims[1] > max || dims[2] > max)
  {
    err = -1;
    QString ss = QObject::tr("One of the dimensions is greater than the max index for this sysem. Try the 64 bit version. dim[0]=%1  dim[1]=%2im[2]=%3")\
                 .arg(dims[0]).arg(dims[1]).arg(dims[2]);
    setErrorCondition(err);
    notifyErrorMessage(getHumanLabel(), ss, getErrorCondition());
    return err;
  }

  size_t dcDims[3] = { static_cast<size_t>(dims[0]), static_cast<size_t>(dims[1]), static_cast<size_t>(dims[2]) };
  DataContainer::Pointer dc = getDataContainerArray()->getDataContainer(getDataContainerName());
  if (dc.get() == NULL)
  {
    return -1;
  }
  ImageGeom::Pointer image = dc->getGeometryAs<ImageGeom>();
  if (image.get() == NULL)
  {
    return -1;
  }
  image->setDimensions(dcDims);

  buf = in.readLine(); // Read Line 6 which is the Origin values
  float origin[3];
  tokens = buf.split(' ');
  origin[0] = tokens[1].toFloat(&ok);
  origin[1] = tokens[2].toFloat(&ok);
  origin[2] = tokens[3].toFloat(&ok);
  image->setOrigin(origin);

  buf = in.readLine(); // Read Line 7 which is the Scaling values
  float resolution[3];
  tokens = buf.split(' ');
  resolution[0] = tokens[1].toFloat(&ok);
  resolution[1] = tokens[2].toFloat(&ok);
  resolution[2] = tokens[3].toFloat(&ok);
  image->setResolution(resolution);

  return err;

}