📄 itkreadwritespatialobjecttest.cxx
字号:
/*=========================================================================
Program: Insight Segmentation & Registration Toolkit
Module: $RCSfile: itkReadWriteSpatialObjectTest.cxx,v $
Language: C++
Date: $Date: 2008-01-27 19:48:47 $
Version: $Revision: 1.24 $
Copyright (c) Insight Software Consortium. All rights reserved.
See ITKCopyright.txt or http://www.itk.org/HTML/Copyright.htm 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.
=========================================================================*/
#if defined(_MSC_VER)
#pragma warning ( disable : 4786 )
#endif
#include "itkSpatialObject.h"
#include "itkTubeSpatialObject.h"
#include "itkDTITubeSpatialObject.h"
#include "itkVesselTubeSpatialObject.h"
#include "itkContourSpatialObject.h"
#include "itkGroupSpatialObject.h"
#include "itkSpatialObjectWriter.h"
#include "itkSpatialObjectReader.h"
#include "itkImageMaskSpatialObject.h"
int itkReadWriteSpatialObjectTest(int argc, char* argv[])
{
typedef itk::TubeSpatialObject<3> TubeType;
typedef TubeType::Pointer TubePointer;
typedef itk::EllipseSpatialObject<3> EllipseType;
typedef EllipseType::Pointer EllipsePointer;
typedef itk::BlobSpatialObject<3> BlobType;
typedef BlobType::Pointer BlobPointer;
typedef itk::SurfaceSpatialObject<3> SurfaceType;
typedef SurfaceType::Pointer SurfacePointer;
typedef itk::LineSpatialObject<3> LineType;
typedef LineType::Pointer LinePointer;
typedef itk::GroupSpatialObject<3> GroupType;
typedef GroupType::Pointer GroupPointer;
typedef itk::LandmarkSpatialObject<3> LandmarkType;
typedef LandmarkType::Pointer LandmarkPointer;
typedef itk::VesselTubeSpatialObject<3> VesselTubeType;
typedef itk::DTITubeSpatialObject<3> DTITubeType;
typedef itk::ContourSpatialObject<3> ContourType;
typedef itk::ImageSpatialObject<3,unsigned short> ImageType;
typedef itk::ImageMaskSpatialObject<3> ImageMaskType;
typedef itk::SpatialObjectWriter<3,unsigned short> WriterType;
typedef itk::SpatialObjectReader<3,unsigned short> ReaderType;
typedef itk::TubeSpatialObjectPoint<3> TubePointType;
typedef itk::VesselTubeSpatialObjectPoint<3> VesselTubePointType;
typedef itk::DTITubeSpatialObjectPoint<3> DTITubePointType;
typedef itk::SpatialObjectPoint<3> BlobPointType;
typedef itk::SurfaceSpatialObjectPoint<3> SurfacePointType;
typedef itk::LineSpatialObjectPoint<3> LinePointType;
// Tubes
std::cout << " --- Testing Read-Write SpatialObject ---" << std::endl;
TubeType::PointListType list;
VesselTubeType::PointListType list2;
DTITubeType::PointListType list3;
BlobType::PointListType list4;
SurfaceType::PointListType list5;
LineType::PointListType list6;
LandmarkType::PointListType list7;
for( unsigned int i=0; i<10; i++)
{
TubePointType p;
p.SetPosition(i,i,i);
p.SetRadius(i);
p.SetRed(i);
p.SetGreen(i+1);
p.SetBlue(i+2);
p.SetAlpha(i+3);
list.push_back(p);
}
for( unsigned int i=0; i<5; i++)
{
VesselTubePointType p;
p.SetPosition(i*2,i*2,i*2);
p.SetRadius(i);
p.SetRed(i);
p.SetGreen(i+1);
p.SetBlue(i+2);
p.SetAlpha(i+3);
p.SetRed(i);
p.SetGreen(i+1);
p.SetBlue(i+2);
p.SetAlpha(i+3);
p.SetRidgeness(i*1);
p.SetMedialness(i*2);
p.SetBranchness(i*3);
p.SetMark(true);
p.SetAlpha1(i*1);
p.SetAlpha2(i*2);
p.SetAlpha3(i*3);
list2.push_back(p);
}
for( unsigned int i=0; i<7; i++)
{
DTITubePointType p;
p.SetPosition(i*3,i*3,i*3);
p.SetRadius(i);
p.SetRed(i);
p.SetGreen(i+1);
p.SetBlue(i+2);
p.SetAlpha(i+3);
p.AddField(DTITubePointType::FA,i);
p.SetField(DTITubePointType::FA,i+1);
p.AddField(DTITubePointType::ADC,2*i);
p.AddField(DTITubePointType::GA,3*i);
p.AddField("Lambda1",4*i);
p.AddField("Lambda2",5*i);
p.AddField("Lambda3",6*i);
float* v = new float[6];
// this is only for testing
// the tensor matrix should be definite positive
// in the real case
for(unsigned int k=0;k<6;k++)
{
v[k] = k;
}
p.SetTensorMatrix(v);
delete []v;
list3.push_back(p);
}
// Blob point list
for( unsigned int i=0; i<3; i++)
{
BlobPointType p;
p.SetPosition(i,i,i);
p.SetRed(i);
p.SetGreen(i+1);
p.SetBlue(i+2);
p.SetAlpha(i+3);
list4.push_back(p);
}
// Surface point list
for( unsigned int i=0; i<3; i++)
{
SurfacePointType p;
p.SetPosition(i,i,i);
SurfacePointType::VectorType normal;
for(unsigned int j=0;j<3;j++)
{
normal[j]=j;
}
p.SetNormal(normal);
p.SetRed(i);
p.SetGreen(i+1);
p.SetBlue(i+2);
p.SetAlpha(i+3);
list5.push_back(p);
}
// Line point list
for( unsigned int i=0; i<3; i++)
{
LinePointType p;
p.SetPosition(i,i,i);
p.SetRed(i);
p.SetGreen(i+1);
p.SetBlue(i+2);
p.SetAlpha(i+3);
LinePointType::VectorType normal1;
LinePointType::VectorType normal2;
for(unsigned int j=0;j<3;j++)
{
normal1[j]=j;
normal2[j]=2*j;
}
p.SetNormal(normal1,0);
p.SetNormal(normal2,1);
list6.push_back(p);
}
// Landmark point list
for( unsigned int i=0; i<3; i++)
{
LinePointType p;
p.SetPosition(i,i,i);
p.SetColor(1,0,0,1);
list7.push_back(p);
}
/** Create a Tube composed of 3 tubes */
TubePointer tube1 = TubeType::New();
tube1->GetProperty()->SetName("Tube 1");
tube1->SetId(1);
tube1->SetPoints(list);
tube1->ComputeBoundingBox();
VesselTubeType::Pointer tube2 = VesselTubeType::New();
tube2->GetProperty()->SetName("Tube 2");
tube2->SetId(2);
tube2->SetPoints(list2);
tube2->ComputeBoundingBox();
DTITubeType::Pointer tube3 = DTITubeType::New();
tube3->GetProperty()->SetName("Tube 3");
tube3->SetId(3);
tube3->SetPoints(list3);
tube3->ComputeBoundingBox();
GroupPointer tubeN1 = GroupType::New();
tubeN1->GetProperty()->SetName("tube network 1");
tubeN1->SetId(0);
tubeN1->AddSpatialObject( tube1 );
tubeN1->AddSpatialObject( tube2 );
GroupPointer tubeN2 = GroupType::New();
tubeN2->SetId(1);
tubeN2->GetProperty()->SetName("tube network 2");
tubeN2->AddSpatialObject( tube3 );
EllipsePointer ellipse = EllipseType::New();
ellipse->SetRadius(9);
ellipse->GetProperty()->SetName("ellipse 1");
BlobPointer blob = BlobType::New();
blob->SetPoints(list4);
blob->GetProperty()->SetName("Blob 1");
SurfacePointer surface = SurfaceType::New();
surface->SetPoints(list5);
surface->GetProperty()->SetName("Surface 1");
LinePointer line = LineType::New();
line->SetPoints(list6);
line->GetProperty()->SetName("Line 1");
LandmarkPointer landmark = LandmarkType::New();
landmark->SetPoints(list7);
landmark->GetProperty()->SetName("Landmark 1");
typedef ImageType::ImageType itkImageType;
typedef itkImageType::Pointer ImagePointer;
typedef itkImageType::SizeType SizeType;
typedef itkImageType::RegionType RegionType;
ImagePointer itkImage = itkImageType::New();
SizeType size;
unsigned int dim = 3;
double spacing[3];
for(unsigned int i=0;i<dim;i++)
{
size[i] = 10;
spacing[i] = i;
}
RegionType region;
region.SetSize(size);
itk::Index<3> zeroIndex;
zeroIndex.Fill(0);
region.SetIndex( zeroIndex );
itkImage->SetLargestPossibleRegion(region);
itkImage->SetBufferedRegion(region);
itkImage->SetRequestedRegion(region);
itkImage->SetSpacing(spacing);
itkImage->Allocate();
itk::ImageRegionIteratorWithIndex< itkImageType > sit(itkImage, region);
for(unsigned int i = 0; !sit.IsAtEnd(); i++, ++sit)
{
sit.Set(i);
}
ImageType::Pointer image = ImageType::New();
image->GetProperty()->SetName("Image 1");
image->SetImage(itkImage);
tubeN2->AddSpatialObject( image );
// Create Mask Image
typedef ImageMaskType::ImageType itkImageMaskType;
typedef itkImageMaskType::Pointer ImageMaskPointer;
ImageMaskPointer itkImageMask = itkImageMaskType::New();
itkImageMask->SetLargestPossibleRegion(region);
itkImageMask->SetBufferedRegion(region);
itkImageMask->SetRequestedRegion(region);
itkImageMask->SetSpacing(spacing);
itkImageMask->Allocate();
itk::ImageRegionIteratorWithIndex< itkImageMaskType > itM(itkImageMask, region);
for(unsigned int i = 0; !itM.IsAtEnd(); i++, ++itM)
{
itM.Set(i);
}
ImageMaskType::Pointer maskImage = ImageMaskType::New();
maskImage->GetProperty()->SetName("Mask Image 1");
maskImage->SetImage(itkImageMask);
tubeN2->AddSpatialObject( maskImage );
// Define a contour
ContourType::Pointer contour = ContourType::New();
contour->GetProperty()->SetName("My First Contour");
contour->SetInterpolationType(ContourType::EXPLICIT_INTERPOLATION);
contour->SetClosed(true);
contour->SetAttachedToSlice(50);
contour->SetDisplayOrientation(2);
for(int i = 0;i<10;i++)
{
ContourType::ControlPointType ctrlPt;
ctrlPt.SetID(i);
ctrlPt.SetPickedPoint(-i,-i,-i);
ctrlPt.SetPosition(i,i,i);
ctrlPt.SetNormal(i,i,i);
ctrlPt.SetRed(i);
ctrlPt.SetGreen(i+1);
ctrlPt.SetBlue(i+2);
ctrlPt.SetAlpha(i+3);
contour->GetControlPoints().push_back(ctrlPt);
ContourType::InterpolatedPointType iPt;
iPt.SetID(i);
iPt.SetRed(i);
iPt.SetGreen(i+1);
iPt.SetBlue(i+2);
iPt.SetAlpha(i+3);
iPt.SetPosition(i,i,i);
contour->GetInterpolatedPoints().push_back(iPt);
}
tubeN1->AddSpatialObject( tubeN2 );
tubeN1->AddSpatialObject( blob );
tubeN1->AddSpatialObject( line );
tubeN1->AddSpatialObject( surface );
tubeN1->AddSpatialObject( landmark );
tubeN1->AddSpatialObject( ellipse );
tubeN1->AddSpatialObject( contour );
std::cout<<"Testing Number of children: ";
if( tubeN1->GetNumberOfChildren() != 9)
{
std::cout<< tubeN1->GetNumberOfChildren() << "[FAILED]"<<std::endl;
return EXIT_FAILURE;
}
else
{
std::cout<<"[PASSED]"<<std::endl;
}
std::cout<<"Testing Writing SceneSpatialObject: ";
WriterType::Pointer writer = WriterType::New();
writer->SetInput(tubeN1);
writer->SetFileName("Objects.meta");
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -