/**
 * @brief MDHWInMemoryLoadingPresenter::transposeWs
 *
 * vtkDataSets are usually provided in 3D, trying to create these where one of
 *those dimensions
 * might be integrated out leads to empty datasets. To avoid this we reorder the
 *dimensions in our workspace
 * prior to visualisation by transposing if if needed.
 *
 * @param inHistoWs : An input workspace that may integrated dimensions
 *anywhere.
 * @param outCachedHistoWs : Cached histo workspace. To write to if needed.
 * @return A workspace that can be directly rendered from. Integrated dimensions
 *are always last.
 */
void MDHWLoadingPresenter::transposeWs(
    Mantid::API::IMDHistoWorkspace_sptr &inHistoWs,
    Mantid::API::IMDHistoWorkspace_sptr &outCachedHistoWs) {
  using namespace Mantid::API;

  if (!outCachedHistoWs) {
    /*
     Construct dimension indexes list for transpose. We do this by forcing
     integrated
     dimensions to be the last in the list. All other orderings are kept.
     */
    std::vector<int> integratedDims;
    std::vector<int> nonIntegratedDims;
    for (int i = 0; i < int(inHistoWs->getNumDims()); ++i) {
      auto dim = inHistoWs->getDimension(i);
      if (dim->getIsIntegrated()) {
        integratedDims.push_back(i);
      } else {
        nonIntegratedDims.push_back(i);
      }
    }

    std::vector<int> orderedDims = nonIntegratedDims;
    orderedDims.insert(orderedDims.end(), integratedDims.begin(),
                       integratedDims.end());

    /*
     If there has been any reordering above, then the dimension indexes will
     no longer be sorted. We use that to determine if we can avoid transposing
     the workspace.
     */
    if (!std::is_sorted(orderedDims.begin(), orderedDims.end())) {
      IAlgorithm_sptr alg = AlgorithmManager::Instance().create("TransposeMD");
      alg->setChild(true);
      alg->initialize();
      alg->setProperty("InputWorkspace", inHistoWs);
      alg->setPropertyValue("OutputWorkspace", "dummy");
      alg->setProperty("Axes", orderedDims);
      alg->execute();
      IMDHistoWorkspace_sptr visualHistoWs =
          alg->getProperty("OutputWorkspace");
      outCachedHistoWs = visualHistoWs;
    } else {
      // No need to transpose anything.
      outCachedHistoWs = inHistoWs;
    }
  }
}
     /*
    Extract the geometry and function information 
    @param histoWs : histogram workspace to get the information from.
    */
    void MDHWLoadingPresenter::extractMetadata(Mantid::API::IMDHistoWorkspace_sptr histoWs)
    {
      using namespace Mantid::Geometry;
      MDGeometryBuilderXML<NoDimensionPolicy> refresh;
      xmlBuilder= refresh; //Reassign.
      std::vector<IMDDimension_sptr> dimensions;
      size_t nDimensions = histoWs->getNumDims();
      for (size_t d=0; d<nDimensions; d++)
      {
        IMDDimension_const_sptr inDim = histoWs->getDimension(d);
        coord_t min = inDim->getMinimum();
        coord_t max = inDim->getMaximum();
        if (min > max)
        {
          min = 0.0;
          max = 1.0;
        }
        //std::cout << "dim " << d << min << " to " <<  max << std::endl;
        axisLabels.push_back(makeAxisTitle(inDim));
        MDHistoDimension_sptr dim(new MDHistoDimension(inDim->getName(), inDim->getName(), inDim->getUnits(), min, max, inDim->getNBins()));
        dimensions.push_back(dim);
      }

      //Configuring the geometry xml builder allows the object panel associated with this reader to later
      //determine how to display all geometry related properties.
      if(nDimensions > 0)
      {
        xmlBuilder.addXDimension( dimensions[0] );
      }
      if(nDimensions > 1)
      {
        xmlBuilder.addYDimension( dimensions[1] );
      }
      if(nDimensions > 2)
      {
        xmlBuilder.addZDimension( dimensions[2]  );
      }
      if(nDimensions > 3)
      {
        tDimension = dimensions[3];
        xmlBuilder.addTDimension(tDimension);
      }
      m_isSetup = true;
    }