1 /*=========================================================================
2 *
3 * Copyright Insight Software Consortium
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0.txt
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 *
17 *=========================================================================*/
18
19 /**
20 * This is a test file for the itkBoxSpatialObject class.
21 */
22 #include "itkImageFileWriter.h"
23 #include "itkGroupSpatialObject.h"
24 #include "itkSpatialObjectToImageFilter.h"
25 #include "itkBoxSpatialObject.h"
26 #include "itkTestingMacros.h"
27
itkBoxSpatialObjectTest(int argc,char * argv[])28 int itkBoxSpatialObjectTest( int argc, char *argv[] )
29 {
30 if (argc < 2)
31 {
32 std::cerr << "Missing Parameters: Usage " << itkNameOfTestExecutableMacro(argv) << "OutputImageFile"
33 << std::endl;
34 }
35
36 constexpr unsigned int Dimension = 2;
37 using SceneType = itk::GroupSpatialObject< Dimension >;
38 using BoxType = itk::BoxSpatialObject< Dimension >;
39 using OutputImageType = itk::Image< unsigned char, Dimension >;
40 using WriterType = itk::ImageFileWriter< OutputImageType >;
41 using SpatialObjectToImageFilterType =
42 itk::SpatialObjectToImageFilter< SceneType, OutputImageType >;
43
44 SceneType::Pointer scene = SceneType::New();
45 BoxType::Pointer box1 = BoxType::New();
46 box1->Print(std::cout);
47 BoxType::Pointer box2 = BoxType::New();
48 box1->SetId(1);
49
50 // Test the SetProperty()
51 scene->AddChild(box1);
52 scene->AddChild(box2);
53
54 BoxType::SizeType boxsize1;
55 BoxType::SizeType boxsize2;
56
57 boxsize1[0] = 30;
58 boxsize1[1] = 30;
59 box1->SetSizeInObjectSpace( boxsize1 );
60 boxsize2[0] = 30;
61 boxsize2[1] = 30;
62 box2->SetSizeInObjectSpace( boxsize2 );
63
64 BoxType::TransformType::OffsetType offset1;
65 BoxType::TransformType::OffsetType offset2;
66
67 offset1[0] = 29.0;
68 offset1[1] = 29.0;
69 box1->GetModifiableObjectToParentTransform()->SetOffset( offset1 );
70 box1->Update();
71
72 offset2[0] = 50.0;
73 offset2[1] = 50.0;
74 box2->SetPositionInObjectSpace( offset2 );
75 box2->Update();
76
77 scene->Update();
78
79 std::cout <<"Test Update(): " << std::endl;
80 std::cout << box1->GetMyBoundingBoxInWorldSpace()->GetBounds() << std::endl;
81 std::cout << box2->GetMyBoundingBoxInWorldSpace()->GetBounds() << std::endl;
82 const BoxType::BoundingBoxType * boundingBox
83 = box1->GetMyBoundingBoxInWorldSpace();
84
85 if( itk::Math::NotAlmostEquals(boundingBox->GetBounds()[0], 29)
86 || itk::Math::NotAlmostEquals(boundingBox->GetBounds()[1], 59)
87 || itk::Math::NotAlmostEquals(boundingBox->GetBounds()[2], 29)
88 || itk::Math::NotAlmostEquals(boundingBox->GetBounds()[3], 59) )
89 {
90 std::cout << "[FAILED] Test returned" << std::endl;
91 std::cout << box1->GetMyBoundingBoxInWorldSpace()->GetBounds() << std::endl;
92 std::cout << "Instead of [29 59 29 59]" << std::endl;
93 return EXIT_FAILURE;
94 }
95
96 box2->ComputeFamilyBoundingBox();
97 const BoxType::BoundingBoxType * boundingBox2
98 = box2->GetFamilyBoundingBoxInWorldSpace();
99 if( itk::Math::NotAlmostEquals(boundingBox2->GetBounds()[0], 50)
100 || itk::Math::NotAlmostEquals(boundingBox2->GetBounds()[1], 80)
101 || itk::Math::NotAlmostEquals(boundingBox2->GetBounds()[2], 50)
102 || itk::Math::NotAlmostEquals(boundingBox2->GetBounds()[3], 80) )
103 {
104 std::cout << "[FAILED] Test returned" << std::endl;
105 std::cout << box2->GetMyBoundingBoxInWorldSpace()->GetBounds() << std::endl;
106 std::cout << "Instead of [50 80 50 80]" << std::endl;
107 return EXIT_FAILURE;
108 }
109
110 std::cout << "[PASSED]" << std::endl;
111
112 // Point consistency
113 std::cout << "Test Is Inside: ";
114 itk::Point<double,2> in;
115 in[0]=30.0;
116 in[1]=30.0;
117 itk::Point<double,2> out;
118 out[0]=0;
119 out[1]=4;
120
121 if(!box1->IsInsideInWorldSpace(in))
122 {
123 std::cout<<"[FAILED]"<<std::endl;
124 return EXIT_FAILURE;
125 }
126 if(box1->IsInsideInWorldSpace(out))
127 {
128 std::cout<<"[FAILED]"<<std::endl;
129 return EXIT_FAILURE;
130 }
131 std::cout << "[PASSED]" << std::endl;
132
133 std::cout << "Test SpatialObjectToImageFilter / IsInside " << std::endl;
134 SpatialObjectToImageFilterType::Pointer imageFilter
135 = SpatialObjectToImageFilterType::New();
136 imageFilter->SetInput( scene );
137
138 OutputImageType::SizeType size;
139 size[ 0 ] = 100;
140 size[ 1 ] = 100;
141 imageFilter->SetSize( size );
142
143 SpatialObjectToImageFilterType::PointType origin;
144 origin[0]=0;
145 origin[1]=0;
146 imageFilter->SetOrigin( origin );
147
148 imageFilter->SetInsideValue( 255 );
149 imageFilter->SetOutsideValue( 0 );
150 imageFilter->Update();
151
152 const char * outputFilename = argv[1];
153 WriterType::Pointer writer = WriterType::New();
154 writer->SetFileName( outputFilename );
155 writer->SetInput( imageFilter->GetOutput() );
156 try
157 {
158 writer->Update();
159 }
160 catch( itk::ExceptionObject & err )
161 {
162 std::cout << "ExceptionObject caught !" << std::endl;
163 std::cout << err << std::endl;
164 return EXIT_FAILURE;
165 }
166 box1->Print(std::cout);
167 return EXIT_SUCCESS;
168 }
169