/**
 * Save an MD workspace to a vts/vtu file.
 * @param workspace: the workspace which is to be saved.
 * @param filename: the name of the file to which the workspace is to be saved.
 * @param normalization: the visual normalization option
 * @param recursionDepth: the recursion depth for MDEvent Workspaces determines
 * @param compressorType: the compression type used by VTK
 * from which level data should be displayed
 */
void SaveMDWorkspaceToVTKImpl::saveMDWorkspace(
    const Mantid::API::IMDWorkspace_sptr &workspace,
    const std::string &filename, VisualNormalization normalization,
    int recursionDepth, const std::string &compressorType) {
  auto isHistoWorkspace =
      boost::dynamic_pointer_cast<Mantid::API::IMDHistoWorkspace>(workspace) !=
      nullptr;
  auto fullFilename = getFullFilename(filename, isHistoWorkspace);

  const vtkXMLWriter::CompressorType compressor = [&compressorType] {
    if (compressorType == "NONE") {
      return vtkXMLWriter::NONE;
    } else if (compressorType == "ZLIB") {
      return vtkXMLWriter::ZLIB;
    } else {
      // This should never happen.
      Mantid::Kernel::Logger g_log("SaveMDWorkspaceToVTK");
      g_log.warning("Incorrect CompressorType: " + compressorType +
                    ". Using CompressorType=NONE.");
      return vtkXMLWriter::NONE;
    }
  }();
  // Define a time slice.
  auto time = selectTimeSliceValue(*workspace);

  // Get presenter and data set factory set up
  auto factoryChain =
      getDataSetFactoryChain(isHistoWorkspace, normalization, time);

  auto presenter = getPresenter(isHistoWorkspace, workspace, recursionDepth);

  // Create the vtk data
  NullProgressAction nullProgressA;
  NullProgressAction nullProgressB;
  auto dataSet =
      presenter->execute(factoryChain.get(), nullProgressA, nullProgressB);

  // Do an orthogonal correction
  dataSet = getDataSetWithOrthogonalCorrection(dataSet, presenter.get(),
                                               workspace, isHistoWorkspace);

  // ParaView 5.1 checks the range of the entire signal array, including blank
  // cells.
  if (isHistoWorkspace) {
    auto structuredGrid = vtkStructuredGrid::SafeDownCast(dataSet);
    vtkIdType imageSize = structuredGrid->GetNumberOfCells();
    vtkNew<vtkFloatArray> signal;
    signal->SetNumberOfComponents(1);
    signal->SetNumberOfTuples(imageSize);
    auto oldSignal = structuredGrid->GetCellData()->GetScalars();
    for (vtkIdType index = 0; index < imageSize; ++index) {
      if (structuredGrid->IsCellVisible(index)) {
        signal->SetComponent(index, 0, oldSignal->GetTuple1(index));
      } else {
        signal->SetComponent(index, 0, std::numeric_limits<float>::quiet_NaN());
      }
    }
    structuredGrid->GetCellData()->SetScalars(signal.GetPointer());
  }

  // Write the data to the file
  vtkSmartPointer<vtkXMLWriter> writer = getXMLWriter(isHistoWorkspace);
  auto writeSuccessFlag =
      writeDataSetToVTKFile(writer, dataSet, fullFilename, compressor);
  if (!writeSuccessFlag) {
    throw std::runtime_error("SaveMDWorkspaceToVTK: VTK could not write "
                             "your data set to a file.");
  }
}
Beispiel #2
0
void PickCallbackFunction(vtkObject* caller, long unsigned int vtkNotUsed(eventId),
	void* clientData, void* vtkNotUsed(callData))
{
	vtkAreaPicker *areaPicker = static_cast<vtkAreaPicker*>(caller);
	iARenderer *ren = static_cast<iARenderer*>(clientData);
	ren->GetRenderWindow()->GetRenderers()->GetFirstRenderer()->RemoveActor(ren->selectedActor);

	auto extractSelection = vtkSmartPointer<vtkExtractSelectedFrustum>::New();
	extractSelection->SetInputData(0, ren->getRenderObserver()->GetImageData());
	extractSelection->PreserveTopologyOff();
	extractSelection->SetFrustum(areaPicker->GetFrustum());
	extractSelection->Update();

	if (!extractSelection->GetOutput()->GetNumberOfElements(vtkUnstructuredGrid::CELL))
	{
		ren->emitNoSelectedCells();
		return;
	}
	
	if (ren->GetInteractor()->GetControlKey() &&
		!ren->GetInteractor()->GetShiftKey())
	{
		// Adds cells to selection
		auto append = vtkSmartPointer<vtkAppendFilter>::New();
		append->AddInputData(ren->finalSelection);
		append->AddInputData(extractSelection->GetOutput());
		append->Update();
		ren->finalSelection->ShallowCopy(append->GetOutput());
	}
	else if (ren->GetInteractor()->GetControlKey() &&
		ren->GetInteractor()->GetShiftKey())
	{
		// Removes cells from selection 
		auto newfinalSel = vtkSmartPointer<vtkUnstructuredGrid>::New();
		newfinalSel->Allocate(1, 1);
		newfinalSel->SetPoints(ren->finalSelection->GetPoints());
		auto currSel = vtkSmartPointer<vtkUnstructuredGrid>::New();
		currSel->ShallowCopy(extractSelection->GetOutput());
		double f_Cell[DIM] = { 0,0,0 }, c_Cell[DIM] = { 0,0,0 };
		double* spacing = ren->getRenderObserver()->GetImageData()->GetSpacing();

		for (vtkIdType i = 0; i < ren->finalSelection->GetNumberOfCells(); ++i)
		{
			bool addCell = true;
			GetCellCenter(ren->finalSelection, i, f_Cell, spacing);
			for (vtkIdType j = 0; j < currSel->GetNumberOfCells(); ++j)
			{
				GetCellCenter(currSel, j, c_Cell, spacing);
				if (f_Cell[0] == c_Cell[0] &&
					f_Cell[1] == c_Cell[1] &&
					f_Cell[2] == c_Cell[2])
				{
					addCell = false;
					break;
				}
			}
			if (addCell)
				newfinalSel->InsertNextCell(ren->finalSelection->GetCell(i)->GetCellType(),
					ren->finalSelection->GetCell(i)->GetPointIds());
		}		
		ren->finalSelection->ShallowCopy(newfinalSel);
	}
	else
	{
		// New selection
		ren->finalSelection->ShallowCopy(extractSelection->GetOutput());
	}
	ren->selectedMapper->Update();
	ren->GetRenderWindow()->GetRenderers()->GetFirstRenderer()->AddActor(ren->selectedActor);
	ren->emitSelectedCells(ren->finalSelection);
}