/*========================================================================= Program: Tensor ToolKit - TTK Module: $URL$ Language: C++ Date: $Date$ Version: $Revision$ Copyright (c) INRIA 2010. All rights reserved. See LICENSE.txt for details. This software is distributed WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the above copyright notices for more information. =========================================================================*/ #include "itkTensorsToVTKUnstructuredGridCommand.h" #include "itkTensorImageIO.h" #include #include #include #include #include #include #include #include #include #include #include #include "GetPot.h" namespace itk { TensorsToVTKUnstructuredGridCommand::TensorsToVTKUnstructuredGridCommand() { m_ShortDescription = "Convert tensor images into a vtk unstructured grid"; m_LongDescription = "Usage:\n"; m_LongDescription += "-i [input tensor images (default : input.list)]\n"; m_LongDescription += "-o [output vtk file (default : output.vtk)]\n\n"; m_LongDescription += m_ShortDescription; } TensorsToVTKUnstructuredGridCommand::~TensorsToVTKUnstructuredGridCommand() {} int TensorsToVTKUnstructuredGridCommand::Execute(int narg, const char* arg[]) { GetPot cl(narg, const_cast(arg)); // argument parser if( cl.size() == 1 || cl.search(2, "--help", "-h") ) { std::cout << this->GetLongDescription() << std::endl; return -1; } const char* inputfile = cl.follow("input.list",2,"-i","-I"); const char* outputfile = cl.follow("output.vtk",2,"-o","-O"); std::cout << "Processing bandwidth extraction with following arguments: " << std::endl; std::cout << "inputfile: " << inputfile << std::endl; std::cout << "outputfile: " << outputfile << std::endl; std::cout << std::flush; // typedefs using ScalarType = double; using TensorIOType = itk::TensorImageIO; using TensorImageType = TensorIOType::TensorImageType; using TensorType = TensorImageType::PixelType; using TensorIteratorType = itk::ImageRegionIterator; using PointType = TensorImageType::PointType; using TensorArrayType = std::vector; using PointArrayType = std::vector; // instantiation // read the input tensors and put tham into a vtkUnstructuredGrid // they come from a text file listing all files to read, either vtk or itk... std::cout << "Reading input tensors: " << inputfile << std::endl; TensorArrayType vecT; PointArrayType vecP; std::cout<<"reading list : "<> NumberOfImageFiles; std::cout<<"encountered : "<SetFileName(line.c_str()); bool success = false; try { reader->Read(); success = true; } catch(itk::ExceptionObject &) { std::cerr << "cannot read with TensorIO"<< std::endl; } if (!success) { vtkDataSetReader* vtkreader = vtkDataSetReader::New(); vtkreader->SetFileName (line.c_str()); vtkreader->Update(); vtkPointSet* dataset = vtkPointSet::SafeDownCast (vtkreader->GetOutput()); if (!dataset || !dataset->GetNumberOfPoints() || !dataset->GetPointData()->GetTensors()) { std::cerr << "cannot read file "<Delete(); continue; } for (int i=0; iGetNumberOfPoints(); i++) { PointType x; for (unsigned int j=0; j<3; j++) x[j] = dataset->GetPoint (i)[j]; TensorType T; double* vals = dataset->GetPointData()->GetTensors()->GetTuple (i); T[0] = vals[0]; T[1] = vals[1]; T[2] = vals[4]; T[3] = vals[2]; T[4] = vals[5]; T[5] = vals[8]; vecT.push_back(T); vecP.push_back(x); } vtkreader->Delete(); } TensorImageType::Pointer tensorimage = reader->GetOutput(); TensorIteratorType it(tensorimage, tensorimage->GetLargestPossibleRegion()); while( !it.IsAtEnd() ) { PointType x; tensorimage->TransformIndexToPhysicalPoint(it.GetIndex(), x); TensorType T = it.Get(); if ((T.GetTrace() > 0.0) && !T.HasNans()) { T = T.ApplyMatrix(tensorimage->GetDirection()); vecT.push_back(T); vecP.push_back(x); } ++it; } std::cout<<" done."<SetNumberOfPoints (vecP.size()); data->SetNumberOfComponents (9); data->SetNumberOfTuples (vecP.size()); for (unsigned int i=0; iSetPoint (i, pt); double vals[9]; vals[0] = vecT[i][0]; vals[1] = vecT[i][1]; vals[2] = vecT[i][3]; vals[3] = vecT[i][1]; vals[4] = vecT[i][2]; vals[5] = vecT[i][4]; vals[6] = vecT[i][3]; vals[7] = vecT[i][4]; vals[8] = vecT[i][5]; data->SetTuple (i, vals); } crossvalidation->SetPoints (points); crossvalidation->GetPointData()->SetTensors (data); vtkDataSetWriter* writer = vtkDataSetWriter::New(); writer->SetFileName(outputfile); writer->SetInputData(crossvalidation); writer->Update(); writer->Delete(); points->Delete(); data->Delete(); crossvalidation->Delete(); std::cout<<" done."<