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 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483
|
/*=========================================================================
*
* Copyright Insight Software Consortium
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0.txt
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*=========================================================================*/
/*
* itkTubeSpatialObject test file.
* This test file test also the basic functions of the CompositeSpatialObject class,
* like Add/RemoveSpatialObject(...), Get/SetChildren(...), etc...
*/
#include "itkTubeSpatialObject.h"
#include "itkGroupSpatialObject.h"
int itkTubeSpatialObjectTest(int, char * [] )
{
typedef double ScalarType;
typedef itk::Vector< ScalarType, 3> Vector;
typedef itk::Point< ScalarType, 3> Point;
typedef itk::TubeSpatialObject<3> TubeType;
typedef itk::SmartPointer< TubeType > TubePointer;
typedef itk::GroupSpatialObject<3> GroupType;
typedef itk::SmartPointer< GroupType > GroupPointer;
typedef TubeType::TubePointType TubePointType;
typedef TubeType::PointListType TubePointListType;
typedef std::list< itk::SpatialObject<3>::Pointer > ChildrenListType;
typedef ChildrenListType * ChildrenListPointer;
Vector axis, translation;
Point in, out;
double angle;
bool passed = true;
//======================================
// testing of a single SpatialObject...
//======================================
std::cout<<"=================================="<<std::endl;
std::cout<<"Testing SpatialObject:"<<std::endl<<std::endl;
TubePointer tube1 = TubeType::New();
tube1->GetProperty()->SetName("Tube 1");
tube1->SetId(1);
TubePointListType list;
TubeType::TransformType::OffsetType offset;
offset.Fill(10);
tube1->GetObjectToParentTransform()->SetOffset(offset);
tube1->ComputeObjectToWorldTransform();
for( unsigned int i=0; i<10; i++)
{
TubePointType p;
p.SetPosition(i,i,i);
p.SetRadius(1);
list.push_back(p);
}
// For coverage
TubePointType p;
p.SetPosition(1,2,3);
p.SetRadius(1);
p.Print(std::cout);
tube1->SetPoints(list);
tube1->ComputeBoundingBox();
in.Fill(15);
out.Fill(5);
std::cout<<"IsInside()...";
if( !tube1->IsInside(in) || tube1->IsInside(out) )
{
std::cout<<"[FAILED]"<<std::endl;
return EXIT_FAILURE;
}
else
{
std::cout<<"[PASSED]"<<std::endl;
}
TubeType::OutputVectorType derivative;
std::cout<<"DerivativeAt()...";
try
{
tube1->DerivativeAt(in,1,derivative);
}
catch(...)
{
std::cout<<"[FAILED]"<<std::endl;
return EXIT_FAILURE;
}
TubeType::OutputVectorType expectedDerivative;
expectedDerivative.Fill(0);
if( expectedDerivative != derivative )
{
std::cout<<"[FAILED]"<<std::endl;
return EXIT_FAILURE;
}
else
{
std::cout<<"[PASSED]"<<std::endl;
}
std::cout<<"itkTubeSpatialObjectTest ";
if( passed )
{
std::cout<<"[PASSED]"<<std::endl;
}
else
{
std::cout<<"[FAILED]"<<std::endl;
}
//==============================================
// testing of a single CompositeSpatialObject...
//==============================================
std::cout<<"=================================="<<std::endl;
std::cout<<"Testing GroupSpatialObject:"<<std::endl<<std::endl;
ChildrenListType childrenList;
ChildrenListPointer returnedList;
unsigned int nbChildren;
TubePointer tube2 = TubeType::New();
tube2->GetProperty()->SetName("Tube 2");
tube2->SetId(2);
tube2->SetPoints(list);
tube2->ComputeBoundingBox();
TubePointer tube3 = TubeType::New();
tube3->GetProperty()->SetName("Tube 3");
tube3->SetId(3);
tube3->SetPoints(list);
tube3->ComputeBoundingBox();
GroupPointer tubeNet1 = GroupType::New();
tubeNet1->GetProperty()->SetName("tube network 1");
tubeNet1->AddSpatialObject( tube1 );
tubeNet1->AddSpatialObject( tube2 );
tubeNet1->AddSpatialObject( tube3 );
// testing the AddSpatialObject() function...
nbChildren = tubeNet1->GetNumberOfChildren();
std::cout<<"AddSpatialObject()...";
if( nbChildren != 3 )
{
std::cout<<"[FAILED] ["<< nbChildren << "!= 3]" << std::endl;
return EXIT_FAILURE;
}
else
{
std::cout<<"[PASSED]"<<std::endl;
}
// testing the RemoveSpatialObject() function...
tubeNet1->RemoveSpatialObject( tube1 );
tubeNet1->RemoveSpatialObject( tube2 );
tubeNet1->RemoveSpatialObject( tube3 );
nbChildren = tubeNet1->GetNumberOfChildren();
std::cout<<"RemoveSpatialObject()...";
if( nbChildren != 0 )
{
std::cout<<"[FAILED]"<<std::endl;
return EXIT_FAILURE;
}
else
{
std::cout<<"[PASSED]"<<std::endl;
}
tubeNet1->AddSpatialObject( tube1 );
tubeNet1->AddSpatialObject( tube2 );
tubeNet1->AddSpatialObject( tube3 );
// testing the GetChildren() function...
childrenList.push_back( tube1.GetPointer() );
childrenList.push_back( tube2.GetPointer() );
childrenList.push_back( tube3.GetPointer() );
returnedList = tubeNet1->GetChildren();
if( childrenList.size() == returnedList->size() )
{
ChildrenListType::iterator itTest = returnedList->begin();
ChildrenListType::iterator it = childrenList.begin();
ChildrenListType::iterator end = childrenList.end();
for(unsigned int i=0; it!=end; ++itTest,++it,i++ )
{
if((*itTest) != (*it))
{
passed = false;
break;
}
}
}
else
{
passed = false;
}
std::cout<<"GetChildren()...";
if( !passed )
{
std::cout<<"[FAILED]"<<std::endl;
return EXIT_FAILURE;
}
else
{
std::cout<<"[PASSED]"<<std::endl;
}
tubeNet1->RemoveSpatialObject( tube1 );
tubeNet1->RemoveSpatialObject( tube2 );
tubeNet1->RemoveSpatialObject( tube3 );
delete returnedList;
// testing the SetChildren() function...
std::cout << "Set children ..." << std::endl;
tubeNet1->SetChildren(childrenList);
returnedList = tubeNet1->GetChildren();
if( childrenList.size() == returnedList->size() )
{
ChildrenListType::iterator itTest = returnedList->begin();
ChildrenListType::iterator it = childrenList.begin();
ChildrenListType::iterator end = childrenList.end();
passed = true;
for(unsigned int i=0; it!=end; ++itTest,++it,i++ )
{
if((*itTest) != (*it))
{
passed = false;
break;
}
}
}
else
{
passed = false;
}
delete returnedList;
std::cout<<"SetChildren()...";
if( !passed )
{
std::cout<<"[FAILED]"<<std::endl;
return EXIT_FAILURE;
}
else
{
std::cout<<"[PASSED]"<<std::endl;
}
tubeNet1->ComputeBoundingBox();
std::cout<<"HasParent()...";
if( !tube2->HasParent() )
{
std::cout<<"[FAILED]"<<std::endl;
return EXIT_FAILURE;
}
else
{
std::cout<<"[PASSED]"<<std::endl;
}
translation.Fill(10);
tubeNet1->GetObjectToParentTransform()->Translate(translation,false);
tubeNet1->ComputeObjectToWorldTransform();
axis.Fill(0);
axis[1] = 1;
angle = itk::Math::pi_over_2;
tube2->GetObjectToParentTransform()->Rotate3D(axis,angle);
tube2->ComputeObjectToWorldTransform();
angle = -itk::Math::pi_over_2;
tube3->GetObjectToParentTransform()->Rotate3D(axis,angle);
tube3->ComputeObjectToWorldTransform();
in.Fill(25);
out.Fill(15);
Point p1,p2;
p1.Fill(15);
p1[2]=5;
p2.Fill(15);
p2[0]=5;
std::cout<<"IsInside()...";
if( !tubeNet1->IsInside(in,3) || tubeNet1->IsInside(out,3) )
{
std::cout<<"[FAILED]"<<std::endl;
return EXIT_FAILURE;
}
else
{
std::cout<<"[PASSED]"<<std::endl;
}
std::cout<<"DerivativeAt()...";
try
{
tubeNet1->DerivativeAt(in,(unsigned short)1,derivative,true);
}
catch(...)
{
std::cout<<"[FAILED]"<<std::endl;
}
if( derivative == expectedDerivative )
{
std::cout<<"[PASSED]"<<std::endl;
}
else
{
std::cout<<"[FAILED]"<<std::endl;
return EXIT_FAILURE;
}
//====================================================
// testing of references behavior for SpatialObject...
//====================================================
std::cout<<"=============================================="<<std::endl;
std::cout<<"Testing references behavior for SpatialObject:"<<std::endl<<std::endl;
TubePointer tube = TubeType::New();
GroupPointer net = GroupType::New();
unsigned int tubeCount, netCount;
tubeCount = tube->GetReferenceCount();
netCount = net->GetReferenceCount();
std::cout<<"References test...";
if( tubeCount != 1 )
{
std::cout<<"[FAILED]: Problem in Tube initialization of references count" << tubeCount <<std::endl;
return EXIT_FAILURE;
}
else
{
TubePointer localTube = tube;
tubeCount = tube->GetReferenceCount();
if( tubeCount != 2 )
{
std::cout<<"[FAILED]: Problem in Tube with incrementation of references count"<<std::endl;
return EXIT_FAILURE;
}
}
if( netCount != 1 )
{
std::cout<<"[FAILED]: Problem in TubeNetwork initialization of references count"<<std::endl;
return EXIT_FAILURE;
}
else
{
GroupPointer localNet = net;
netCount = net->GetReferenceCount();
if( netCount != 2 )
{
std::cout<<"[FAILED]: Problem in TubeNetwork with incrementation of references count"<<std::endl;
return EXIT_FAILURE;
}
}
tubeCount = tube->GetReferenceCount();
netCount = net->GetReferenceCount();
if( tubeCount != 1 )
{
std::cout<<"[FAILED]: Problem in Tube with decrementation of references count"<<std::endl;
return EXIT_FAILURE;
}
if( netCount != 1 )
{
std::cout << "[FAILED]: Problem in TubeNetwork with decrementation of references count"<<std::endl;
return EXIT_FAILURE;
}
std::cout << "[PASSED]" << std::endl;
// Testing Set/GetParentPoint
std::cout << "Set/GetParentPoint: ";
tube->SetParentPoint(1);
if(tube->GetParentPoint() != 1)
{
std::cout << "[FAILED]" << std::endl;
return EXIT_FAILURE;
}
std::cout << "[PASSED]" << std::endl;
// Testing ComputeTangentAndNormals();
std::cout << "ComputeTangentAndNormals: ";
tube1->ComputeTangentAndNormals();
TubePointType::VectorType t = static_cast<const TubePointType*>(tube1->GetPoint(1))->GetTangent();
TubePointType::CovariantVectorType n1 = static_cast<const TubePointType*>(tube1->GetPoint(1))->GetNormal1();
TubePointType::CovariantVectorType n2 = static_cast<const TubePointType*>(tube1->GetPoint(1))->GetNormal2();
if( (std::fabs(t[0]-0.57735)>0.0001)
|| (std::fabs(t[1]-0.57735)>0.0001)
|| (std::fabs(t[2]-0.57735)>0.0001)
|| (std::fabs(n1[0]-0.0)>0.0001)
|| (std::fabs(n1[1]+0.57735)>0.0001)
|| (std::fabs(n1[2]-0.57735)>0.0001)
|| (std::fabs(n2[0]-0.666667)>0.0001)
|| (std::fabs(n2[1]+0.333333)>0.0001)
|| (std::fabs(n2[2]+0.333333)>0.0001)
)
{
std::cout << "[FAILED]" << std::endl;
return EXIT_FAILURE;
}
std::cout << "[PASSED]" << std::endl;
// Testing IsInside() with m_EndType set to 1 (rounded end-type);
std::cout << "IsInside() with m_EndType=1: ";
p1.Fill(19.5);
if(tube1->IsInside(p1))
{
std::cout << "[FAILED]" << std::endl;
return EXIT_FAILURE;
}
tube1->SetEndType(1);
if(!tube1->IsInside(p1))
{
std::cout << "[FAILED]" << std::endl;
return EXIT_FAILURE;
}
std::cout << "[PASSED]" << std::endl;
// For coverage only
std::cout << "Testing PointBasedSO: ";
typedef itk::PointBasedSpatialObject<3> PointBasedType;
PointBasedType::Pointer pBSO = PointBasedType::New();
pBSO->GetPoint(0);
pBSO->ComputeBoundingBox();
std::cout << "[PASSED]" << std::endl;
std::cout << "[DONE]" << std::endl;
return EXIT_SUCCESS;
}
|