1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178
|
/*=========================================================================
Program: Advanced Normalization Tools
Copyright (c) ConsortiumOfANTS. All rights reserved.
See accompanying COPYING.txt or
https://github.com/stnava/ANTs/blob/master/ANTSCopyright.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 "antsUtilities.h"
#include <algorithm>
#include "itkImageFileReader.h"
#include "itkImageRegionIteratorWithIndex.h"
#include "vtkUnstructuredGrid.h"
#include "vtkUnstructuredGridWriter.h"
#include "vtkFloatArray.h"
#include "vtkPoints.h"
#include "vtkPointData.h"
namespace ants
{
// entry point for the library; parameter 'args' is equivalent to 'argv' in (argc,argv) of commandline parameters to
// 'main()'
int
ConvertVectorFieldToVTK(std::vector<std::string> args, std::ostream * out_stream = nullptr)
{
// put the arguments coming in as 'args' into standard (argc,argv) format;
// 'args' doesn't have the command name as first, argument, so add it manually;
// 'args' may have adjacent arguments concatenated into one argument,
// which the parser should handle
args.insert(args.begin(), "ConvertVectorFieldToVTK");
int argc = args.size();
char ** argv = new char *[args.size() + 1];
for (unsigned int i = 0; i < args.size(); ++i)
{
// allocate space for the string plus a null character
argv[i] = new char[args[i].length() + 1];
std::strncpy(argv[i], args[i].c_str(), args[i].length());
// place the null character in the end
argv[i][args[i].length()] = '\0';
}
argv[argc] = 0;
// class to automatically cleanup argv upon destruction
class Cleanup_argv
{
public:
Cleanup_argv(char ** argv_, int argc_plus_one_)
: argv(argv_)
, argc_plus_one(argc_plus_one_)
{}
~Cleanup_argv()
{
for (unsigned int i = 0; i < argc_plus_one; ++i)
{
delete[] argv[i];
}
delete[] argv;
}
private:
char ** argv;
unsigned int argc_plus_one;
};
Cleanup_argv cleanup_argv(argv, argc + 1);
// antscout->set_stream( out_stream );
if (argc < 3)
{
std::cout << "Usage: " << argv[0]
<< " inputDisplacementField outputVTKFile maskImage(optional) slice(optional) whichAxis(optional)"
<< std::endl;
return EXIT_FAILURE;
}
typedef float PixelType;
constexpr unsigned int ImageDimension = 3;
typedef itk::Image<PixelType, ImageDimension> ImageType;
typedef itk::Image<int, ImageDimension> MaskImageType;
typedef double RealType;
typedef itk::Vector<RealType, ImageDimension> VectorType;
typedef itk::Image<VectorType, ImageDimension> DisplacementFieldType;
typedef itk::ImageFileReader<DisplacementFieldType> ReaderType;
ReaderType::Pointer reader = ReaderType::New();
reader->SetFileName(argv[1]);
// reader->SetUseAvantsNamingConvention( true );
reader->Update();
MaskImageType::Pointer mask;
if (argc >= 4)
{
typedef itk::ImageFileReader<MaskImageType> MaskReaderType;
MaskReaderType::Pointer maskreader = MaskReaderType::New();
maskreader->SetFileName(argv[3]);
maskreader->Update();
mask = maskreader->GetOutput();
}
else
{
// ORIENTATION ALERT -- the original code here
// set the region, spacing, and origin without setting directions.
mask = AllocImage<MaskImageType>(reader->GetOutput(), 1);
}
int size[ImageDimension];
int totalsize = 1;
for (unsigned int i = 0; i < ImageDimension; i++)
{
size[i] = reader->GetOutput()->GetLargestPossibleRegion().GetSize()[i];
if (argc > 4 && std::stoi(argv[5]) == (int)i)
{
size[i] = 1;
}
totalsize *= size[i];
}
int totalPoints = totalsize;
vtkUnstructuredGrid * field = vtkUnstructuredGrid::New();
vtkPoints * points = vtkPoints::New();
points->Allocate(totalPoints);
vtkFloatArray * vectors = vtkFloatArray::New();
vectors->SetNumberOfComponents(3);
vectors->SetNumberOfTuples(totalPoints);
float x[3], v[3];
int offset = 0;
itk::ImageRegionIteratorWithIndex<MaskImageType> It(mask, mask->GetLargestPossibleRegion());
for (It.GoToBegin(); !It.IsAtEnd(); ++It)
{
DisplacementFieldType::IndexType idx = It.GetIndex();
if ((argc > 4 && idx[atoi(argv[5])] != std::stoi(argv[4])) || It.Get() == 0)
{
continue;
}
DisplacementFieldType::PointType point;
reader->GetOutput()->TransformIndexToPhysicalPoint(idx, point);
VectorType V = reader->GetOutput()->GetPixel(idx);
for (unsigned int i = 0; i < ImageDimension; i++)
{
x[i] = point[i];
v[i] = V[i];
}
// offset = idx[0] + idx[1]*(size[1]+1) + idx[2]*(size[1]+1)*(size[3]+1);
points->InsertPoint(offset, x);
vectors->InsertTuple(offset++, v);
}
field->SetPoints(points);
field->GetPointData()->SetVectors(vectors);
points->Delete();
vectors->Delete();
vtkUnstructuredGridWriter * writer = vtkUnstructuredGridWriter::New();
writer->SetInput(field);
// writer->SetFileTypeToBinary();
writer->SetFileName(argv[2]);
writer->Write();
return 0;
}
} // namespace ants
|