📄 itkreadwritespatialobjecttest.cxx
字号:
/*=========================================================================
Program: Insight Segmentation & Registration Toolkit
Module: $RCSfile: itkReadWriteSpatialObjectTest.cxx,v $
Language: C++
Date: $Date: 2003/09/10 14:30:10 $
Version: $Revision: 1.9 $
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 "itkGroupSpatialObject.h"
#include "itkSpatialObjectWriter.h"
#include "itkSpatialObjectReader.h"
int itkReadWriteSpatialObjectTest(int, char*[])
{
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::ImageSpatialObject<3,unsigned short> ImageType;
typedef itk::SpatialObjectWriter<3,unsigned short> WriterType;
typedef itk::SpatialObjectReader<3,unsigned short> ReaderType;
typedef itk::TubeSpatialObjectPoint<3> TubePointType;
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;
TubeType::PointListType list2;
TubeType::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++)
{
TubePointType 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);
list2.push_back(p);
}
for( unsigned int i=0; i<7; i++)
{
TubePointType 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);
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();
TubePointer tube2 = TubeType::New();
tube2->GetProperty()->SetName("Tube 2");
tube2->SetId(2);
tube2->SetPoints(list2);
tube2->ComputeBoundingBox();
TubePointer tube3 = TubeType::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 > it(itkImage, region);
for(unsigned int i = 0; !it.IsAtEnd(); i++, ++it)
{
it.Set(i);
}
ImageType::Pointer image = ImageType::New();
image->GetProperty()->SetName("Image 1");
image->SetImage(itkImage);
tubeN2->AddSpatialObject( image );
//tubeN2->AddSpatialObject( ellipse );
tubeN1->AddSpatialObject( tubeN2 );
tubeN1->AddSpatialObject( blob );
tubeN1->AddSpatialObject( line );
tubeN1->AddSpatialObject( surface );
tubeN1->AddSpatialObject( landmark );
std::cout<<"Testing Number of children: ";
if( tubeN1->GetNumberOfChildren() != 7 )
{
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->SetFullFileName("Objects.meta");
writer->Update();
std::cout<<"[PASSED]"<<std::endl;
std::cout<<"Testing Reading SceneSpatialObject: ";
ReaderType::Pointer reader = ReaderType::New();
reader->SetFileName("Objects.meta");
reader->Update();
ReaderType::ScenePointer myScene = reader->GetScene();
if(!myScene)
{
std::cout<<"No Scene : [FAILED]"<<std::endl;
return EXIT_FAILURE;
}
else
{
std::cout<<" [PASSED]"<<std::endl;
}
std::cout<<"Testing Number of children:";
if(myScene->GetNumberOfObjects(1) != 8)
{
std::cout << "found " << myScene->GetNumberOfObjects(1) << " instead of 8" << std::endl;
std::cout<<" [FAILED]"<<std::endl;
return EXIT_FAILURE;
}
else
{
std::cout<<" [PASSED]"<<std::endl;
}
std::cout<<"Testing CenterLine Position:";
TubeType::PointListType::const_iterator j;
ReaderType::SceneType::ObjectListType * mySceneChildren =
myScene->GetObjects(999999);
ReaderType::SceneType::ObjectListType::const_iterator i;
for(i = mySceneChildren->begin(); i != mySceneChildren->end(); i++)
{
if(!strcmp((*i)->GetTypeName(),"TubeSpatialObject"))
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -