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 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193
|
/*=========================================================================
Program: Insight Segmentation & Registration Toolkit
Module: $RCSfile: itkMeshSpatialObjectTest.cxx,v $
Language: C++
Date: $Date: 2009-04-07 14:34:48 $
Version: $Revision: 1.6 $
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 <itkDefaultDynamicMeshTraits.h>
#include <itkMesh.h>
#include <itkMeshSpatialObject.h>
#include <itkTetrahedronCell.h>
int itkMeshSpatialObjectTest(int, char * [] )
{
typedef itk::DefaultDynamicMeshTraits< float , 3, 3 > MeshTrait;
typedef itk::Mesh<float,3,MeshTrait> MeshType;
typedef MeshType::CellTraits CellTraits;
typedef itk::CellInterface< float, CellTraits > CellInterfaceType;
typedef itk::TetrahedronCell<CellInterfaceType> TetraCellType;
typedef itk::MeshSpatialObject<MeshType> MeshSpatialObjectType;
typedef MeshType::PointType PointType;
typedef MeshType::CellType CellType;
typedef CellType::CellAutoPointer CellAutoPointer;
// Create an itkMesh
MeshType::Pointer mesh = MeshType::New();
MeshType::CoordRepType testPointCoords[4][3]
= { {0,0,0}, {9,0,0}, {9,9,0}, {0,0,9} };
unsigned long tetraPoints[4] = {0,1,2,3};
int i;
for(i=0; i < 4 ; ++i)
{
mesh->SetPoint(i, PointType(testPointCoords[i]));
}
mesh->SetCellsAllocationMethod( MeshType::CellsAllocatedDynamicallyCellByCell );
CellAutoPointer testCell1;
testCell1.TakeOwnership( new TetraCellType );
testCell1->SetPointIds(tetraPoints);
mesh->SetCell(0, testCell1 );
// Create the mesh Spatial Object
MeshSpatialObjectType::Pointer meshSO = MeshSpatialObjectType::New();
meshSO->Print(std::cout);
meshSO->SetMesh(mesh);
std::cout << "Testing GetMesh(): ";
if(mesh != meshSO->GetMesh())
{
std::cout<<"[FAILED]"<<std::endl;
return EXIT_FAILURE;
}
std::cout<<"[PASSED]"<<std::endl;
std::cout << "Testing Bounding Box: ";
if( (meshSO->GetBoundingBox()->GetBounds()[0] != 0)
|| (meshSO->GetBoundingBox()->GetBounds()[1] != 9)
|| (meshSO->GetBoundingBox()->GetBounds()[2] != 0)
|| (meshSO->GetBoundingBox()->GetBounds()[3] != 9)
|| (meshSO->GetBoundingBox()->GetBounds()[4] != 0)
|| (meshSO->GetBoundingBox()->GetBounds()[5] != 9)
)
{
std::cout<<"[FAILED]"<<std::endl;
return EXIT_FAILURE;
}
std::cout<<"[PASSED]"<<std::endl;
// Testing is inside
std::cout << "Testing IsInside: ";
MeshSpatialObjectType::PointType inside;
inside[0] = 1;
inside[1] = 1;
inside[2] = 1;
MeshSpatialObjectType::PointType outside;
outside[0] = 0;
outside[1] = 3;
outside[2] = 0;
if(!meshSO->IsInside(inside) || meshSO->IsInside(outside))
{
std::cout<<"[FAILED]"<<std::endl;
if(!meshSO->IsInside(inside))
{
std::cout << inside << " is not inside the mesh!" << std::endl;
}
if(meshSO->IsInside(outside))
{
std::cout << outside << " is inside the mesh!" << std::endl;
}
return EXIT_FAILURE;
}
std::cout<<"[PASSED]"<<std::endl;
// Testing is valueAt
std::cout << "Testing ValueAt: ";
double value;
meshSO->ValueAt(inside,value);
if(!value)
{
std::cout<<"[FAILED]"<<std::endl;
return EXIT_FAILURE;
}
meshSO->ValueAt(outside,value);
if(value)
{
std::cout<<"[FAILED]"<<std::endl;
return EXIT_FAILURE;
}
std::cout<<"[PASSED]"<<std::endl;
// Testing IsInside() for triangle mesh
std::cout << "Testing IsInside() Triangle: ";
typedef itk::TriangleCell<CellInterfaceType> TriangleCellType;
// Create an itkMesh
MeshType::Pointer meshTriangle = MeshType::New();
MeshType::CoordRepType testTrianglePointCoords[4][3]
= { {50,50,64}, {50,100,64}, {100,50,64} , {100,100,64}};
unsigned long trianglePoint1[3] = {0,1,2};
for(i=0; i < 4 ; ++i)
{
meshTriangle->SetPoint(i, PointType(testTrianglePointCoords[i]));
}
unsigned long trianglePoint2[] = {1,2,3};
meshTriangle->SetCellsAllocationMethod( MeshType::CellsAllocatedDynamicallyCellByCell );
CellAutoPointer testCell3;
testCell3.TakeOwnership( new TriangleCellType );
testCell3->SetPointIds(trianglePoint1);
meshTriangle->SetCell(0, testCell3 );
CellAutoPointer testCell4;
testCell4.TakeOwnership( new TriangleCellType );
testCell4->SetPointIds(trianglePoint2);
meshTriangle->SetCell(1, testCell4 );
// Create the mesh Spatial Object
MeshSpatialObjectType::Pointer meshTriangleSO = MeshSpatialObjectType::New();
meshTriangleSO->SetMesh(meshTriangle);
itk::Point<double,3> pIn;
pIn[0] = 60;
pIn[1] = 60;
pIn[2] = 64;
itk::Point<double,3> pOut;
pOut[0] = 60;
pOut[1] = 102;
pOut[2] = 64;
if(!meshTriangleSO->IsInside(pIn) || meshTriangleSO->IsInside(pOut))
{
std::cout<<"[FAILED]"<<std::endl;
return EXIT_FAILURE;
}
meshSO->Print(std::cout);
std::cout<<"[PASSED]"<<std::endl;
std::cout<<"[TEST DONE]"<<std::endl;
return EXIT_SUCCESS;
}
|