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 #include <fstream>
21 
22 #include "itkPointSetToListSampleAdaptor.h"
23 
itkPointSetToListSampleAdaptorTest(int,char * [])24 int itkPointSetToListSampleAdaptorTest( int, char * [] )
25 {
26   using PointSetType = itk::PointSet< double, 3 >;
27   using PointSetToListSampleAdaptorType = itk::Statistics::PointSetToListSampleAdaptor< PointSetType >;
28 
29   PointSetType::Pointer pointSet = PointSetType::New();
30   PointSetType::PointType point;
31 
32   unsigned int numberOfPoints=10;
33   for( unsigned int i=0; i < numberOfPoints; i++ )
34     {
35     point[0] = i*3;
36     point[1] = i*3 + 1;
37     point[2] = i*3 + 2;
38     pointSet->SetPoint( i, point );
39     }
40 
41   PointSetToListSampleAdaptorType::Pointer  listSample = PointSetToListSampleAdaptorType::New();
42 
43   bool exceptionsProperlyCaught=true;
44   //Test if the methods throw exceptions if invoked before setting the pointset
45   try
46     {
47     // Purposely calling the Size() method in order to trigger an exception.
48     listSample->Size();
49     std::cerr << "Exception should have been thrown since the input point set  \
50                   is not set yet" << std::endl;
51     exceptionsProperlyCaught=false;
52     }
53   catch ( itk::ExceptionObject & excp )
54     {
55     std::cerr << "Caught expected exception: " << excp << std::endl;
56     }
57   try
58     {
59     // Purposely calling the GetTotalFrequency() method in order to trigger an exception.
60     listSample->GetTotalFrequency();
61     std::cerr << "Exception should have been thrown since the input point set  \
62                   is not set yet" << std::endl;
63     exceptionsProperlyCaught=false;
64     }
65   catch ( itk::ExceptionObject & excp )
66     {
67     std::cerr << "Caught expected exception: " << excp << std::endl;
68     }
69 
70   try
71     {
72     PointSetToListSampleAdaptorType::MeasurementVectorType m = listSample->GetMeasurementVector( 0 );
73     std::cerr << "Exception should have been thrown since the input point set  \
74                   is not set yet" << std::endl;
75     std::cerr << "The invalid listSample->GetMeasurementVector is: " << m << std::endl;
76     exceptionsProperlyCaught=false;
77     }
78   catch ( itk::ExceptionObject & excp )
79     {
80     std::cerr << "Caught expected exception: " << excp << std::endl;
81     }
82 
83   try
84     {
85     // Purposely calling the GetPointSet() method in order to trigger an exception.
86     listSample->GetPointSet();
87     std::cerr << "Exception should have been thrown since the input point set  \
88                   is not set yet" << std::endl;
89     exceptionsProperlyCaught=false;
90     }
91   catch ( itk::ExceptionObject & excp )
92     {
93     std::cerr << "Caught expected exception: " << excp << std::endl;
94     }
95 
96   try
97     {
98     // Purposely calling the GetFrequency() method in order to trigger an exception.
99     listSample->GetFrequency(0 );
100     std::cerr << "Exception should have been thrown since the input point set  \
101                   is not set yet" << std::endl;
102     exceptionsProperlyCaught=false;
103     }
104   catch ( itk::ExceptionObject & excp )
105     {
106     std::cerr << "Caught expected exception: " << excp << std::endl;
107     }
108 
109   if( ! exceptionsProperlyCaught )
110     {
111     std::cerr << "At least one exception that should have been caught was not." << std::endl;
112     return EXIT_FAILURE;
113     }
114 
115 
116   listSample->SetPointSet( pointSet );
117 
118   //exercise returned pointset
119   const PointSetToListSampleAdaptorType::PointSetType * pointSetReturned = listSample->GetPointSet( );
120 
121   //check for nullptr
122   if( pointSetReturned == nullptr )
123     {
124     std::cerr << "GetPointSet() returned a nullptr pointer"<< std::endl;
125     return EXIT_FAILURE;
126     }
127 
128   //check size
129   if (numberOfPoints != listSample->Size())
130     {
131     std::cerr << "Size() is not returning the correct size"<< std::endl;
132     return EXIT_FAILURE;
133     }
134 
135   //check frequency
136   if ( listSample->GetFrequency( 0 ) != 1 )
137     {
138     std::cerr << "GetFrequency() is not returning the correct frequency"<< std::endl;
139     return EXIT_FAILURE;
140     }
141 
142   //check frequency
143   if (numberOfPoints != listSample->GetTotalFrequency())
144     {
145     std::cerr << "GetTotalFrequency() is not returning the correct frequency"<< std::endl;
146     return EXIT_FAILURE;
147     }
148 
149 
150   listSample->Print( std::cout );
151 
152   for( unsigned int i=0; i < numberOfPoints; i++ )
153     {
154     PointSetToListSampleAdaptorType::InstanceIdentifier id = i;
155     PointSetType::PointType   tempPointSet( 0.0 );
156     pointSet->GetPoint( i, &tempPointSet );
157 
158     if ( listSample->GetMeasurementVector( id ) != tempPointSet )
159       {
160       std::cerr << "Error in point set accessed by the adaptor" << std::endl;
161       return EXIT_FAILURE;
162       }
163     }
164 
165   //Test the iterators
166   std::cerr << "Iterators..." << std::endl;
167     {
168     // forward iterator
169     using IteratorType = PointSetToListSampleAdaptorType::Iterator;
170 
171     IteratorType s_iter = listSample->Begin();
172 
173     // copy constructor
174     IteratorType bs_iter(s_iter);
175     if (bs_iter != s_iter)
176       {
177       std::cerr << "Iterator::Copy Constructor failed" << std::endl;
178       return EXIT_FAILURE;
179       }
180 
181     // assignment operator
182     IteratorType assignment_iter( bs_iter );
183     assignment_iter = s_iter;
184     if (assignment_iter != s_iter)
185       {
186       std::cerr << "Iterator::assignment operator failed" << std::endl;
187       return EXIT_FAILURE;
188       }
189 
190     PointSetToListSampleAdaptorType::InstanceIdentifier id = 0;
191     while (s_iter != listSample->End())
192       {
193       if (listSample->GetMeasurementVector(id) !=
194           s_iter.GetMeasurementVector())
195         {
196         std::cerr << "Iterator::GetMeasurementVector (forward) failed"
197                   << std::endl;
198         return EXIT_FAILURE;
199         }
200       if (id != s_iter.GetInstanceIdentifier())
201         {
202         std::cerr << "Iterator::GetInstanceIdentifier (forward) failed"
203                   << std::endl;
204         return EXIT_FAILURE;
205         }
206       if (s_iter.GetFrequency() != 1)
207         {
208         std::cerr << "Iterator::GetFrequency (forward) failed" << std::endl;
209         return EXIT_FAILURE;
210         }
211       if (listSample->GetFrequency(id) != 1)
212         {
213         std::cerr << "GetFrequency (forward) failed" << std::endl;
214         return EXIT_FAILURE;
215         }
216       ++id;
217       ++s_iter;
218       }
219 
220     if (s_iter != listSample->End())
221       {
222       std::cerr << "Iterator::End (forward) failed" << std::endl;
223       return EXIT_FAILURE;
224       }
225 
226     }
227 
228   std::cerr << "Const Iterators..." << std::endl;
229     {
230     // forward iterator
231     using ConstIteratorType = PointSetToListSampleAdaptorType::ConstIterator;
232 
233     ConstIteratorType s_iter = listSample->Begin();
234 
235     // copy constructor
236     ConstIteratorType bs_iter(s_iter);
237     if (bs_iter != s_iter)
238       {
239       std::cerr << "Iterator::Copy Constructor (from const) failed"
240                 << std::endl;
241       return EXIT_FAILURE;
242       }
243 
244     // assignment operator
245     ConstIteratorType assignment_iter( bs_iter );
246     assignment_iter = s_iter;
247     if (assignment_iter != s_iter)
248       {
249       std::cerr << "Const Iterator::operator= () failed"
250                 << std::endl;
251       return EXIT_FAILURE;
252       }
253 
254     // copy from non-const iterator
255     PointSetToListSampleAdaptorType::Iterator nonconst_iter = listSample->Begin();
256     PointSetToListSampleAdaptorType::ConstIterator s2_iter(nonconst_iter);
257     if (s2_iter != s_iter)
258       {
259       std::cerr << "Iterator::Copy Constructor (from non-const) failed"
260                 << std::endl;
261       return EXIT_FAILURE;
262       }
263 
264     // assignment from non-const iterator
265     s2_iter = nonconst_iter;
266     if (s2_iter != s_iter)
267       {
268       std::cerr << "Iterator::assignment (from non-const) failed" << std::endl;
269       return EXIT_FAILURE;
270       }
271 
272     PointSetToListSampleAdaptorType::InstanceIdentifier id = 0;
273     while (s_iter != listSample->End())
274       {
275       if (listSample->GetMeasurementVector(id) !=
276           s_iter.GetMeasurementVector())
277         {
278         std::cerr << "Iterator::GetMeasurementVector (forward) failed"
279                   << std::endl;
280         return EXIT_FAILURE;
281         }
282       if (id != s_iter.GetInstanceIdentifier())
283         {
284         std::cerr << "Iterator::GetInstanceIdentifier (forward) failed"
285                   << std::endl;
286         return EXIT_FAILURE;
287         }
288       if (s_iter.GetFrequency() != 1)
289         {
290         std::cerr << "Iterator::GetFrequency (forward) failed" << std::endl;
291         return EXIT_FAILURE;
292         }
293       ++id;
294       ++s_iter;
295       }
296 
297     if (s_iter != listSample->End())
298       {
299       std::cerr << "Iterator::End (forward) failed" << std::endl;
300       return EXIT_FAILURE;
301       }
302     }
303 
304   std::cout << "Test passed." << std::endl;
305   return EXIT_SUCCESS;
306 }
307