Orfeo Toolbox  3.16
itkMedialNodeTripletCorrespondenceProcess.txx
Go to the documentation of this file.
1 /*=========================================================================
2 
3  Program: Insight Segmentation & Registration Toolkit
4  Module: $RCSfile: itkMedialNodeTripletCorrespondenceProcess.txx,v $
5  Language: C++
6  Date: $Date: 2009-01-24 20:03:00 $
7  Version: $Revision: 1.6 $
8 
9  Copyright (c) Insight Software Consortium. All rights reserved.
10  See ITKCopyright.txt or http://www.itk.org/HTML/Copyright.htm for details.
11 
12  This software is distributed WITHOUT ANY WARRANTY; without even
13  the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
14  PURPOSE. See the above copyright notices for more information.
15 
16 =========================================================================*/
17 
18 #ifndef __itkMedialNodeTripletCorrespondenceProcess_txx
19 #define __itkMedialNodeTripletCorrespondenceProcess_txx
20 
22 
23 #include <fstream>
24 
25 namespace itk
26 {
27 
31 template< typename TSourceImage >
34 {
35  itkDebugMacro(<< "itkMedialNodeTripletCorrespondenceProcess::itkMedialNodeTripletCorrespondenceProcess() called");
36 
37  m_NumberOfNodeBaseTriplets = 0;
38  m_NumberOfTriplets = 0;
39 
40  m_CreateOutputFile = false;
41 
42  //Setting the output
44  output = static_cast<typename MedialNodeTripletCorrespondenceProcess::OutputDataStructureType*>(this->MakeOutput(0).GetPointer());
46  this->ProcessObject::SetNthOutput(0, output.GetPointer());
47 }
48 
52 template< typename TSourceImage >
55 ::MakeOutput(unsigned int)
56 {
57  return static_cast<DataObject*>(OutputDataStructureType::New().GetPointer());
58 }
59 
63 template< typename TSourceImage >
67 {
68  if (this->GetNumberOfOutputs() < 1)
69  {
70  return 0;
71  }
72 
73  return static_cast< OutputDataStructureType * >
74  (this->ProcessObject::GetOutput(0));
75 }
76 
80 template< typename TSourceImage >
81 void
83 ::SetPairDataStructure(const InputDataStructureType * InputDataStructure )
84 {
85  itkDebugMacro(<< "MedialNodeTripletCorrespondenceProcess: Setting first input");
86  // Process object is not const-correct so the const casting is required.
87  SetNthInput(0, const_cast<InputDataStructureType *>( InputDataStructure ) );
88 }
89 
93 template< typename TSourceImage >
94 void
96 ::SetDistanceMatrixA( const DistanceMatrixType * DistanceMatrixA )
97 {
98  itkDebugMacro(<< "MedialNodeTripletCorrespondenceProcess: Setting first distance matrix");
99  // Process object is not const-correct so the const casting is required.
100  SetNthInput(1, const_cast<DistanceMatrixType *>( DistanceMatrixA ) );
101 }
102 
106 template< typename TSourceImage >
107 void
109 ::SetDistanceMatrixB( const DistanceMatrixType * DistanceMatrixB )
110 {
111  itkDebugMacro(<< "MedialNodeTripletCorrespondenceProcess: Setting second distance matrix");
112  // Process object is not const-correct so the const casting is required.
113  SetNthInput(2, const_cast<DistanceMatrixType *>( DistanceMatrixB ) );
114 }
115 
119 template< typename TSourceImage >
120 void
122 ::SetCoreAtomImageA(const CoreAtomImageType * CoreAtomImageA )
123 {
124  itkDebugMacro(<< "MedialNodeTripletCorrespondenceProcess: Setting first core atom image");
125  // Process object is not const-correct so the const casting is required.
126  SetNthInput(3, const_cast<TSourceImage *>( CoreAtomImageA ) );
127 }
128 
132 template< typename TSourceImage >
133 void
135 ::SetCoreAtomImageB(const CoreAtomImageType * CoreAtomImageB )
136 {
137  itkDebugMacro(<< "MedialNodeTripletCorrespondenceProcess: Setting second core atom image");
138  // Process object is not const-correct so the const casting is required.
139  SetNthInput(4, const_cast<TSourceImage *>( CoreAtomImageB ) );
140 }
141 
145 template< typename TSourceImage >
146 TSourceImage *
149 {
150  // Process object is not const-correct so the const casting is required.
151  return const_cast<TSourceImage *>(this->GetNthInput(3));
152 }
153 
157 template< typename TSourceImage >
158 TSourceImage *
161 {
162  // Process object is not const-correct so the const casting is required.
163  return const_cast<TSourceImage *>(this->GetNthInput(4));
164 }
165 
169 template< typename TSourceImage >
173 {
174  // Process object is not const-correct so the const casting is required.
175  return const_cast<DistanceMatrixType *>(this->GetNthInput(1));
176 }
177 
181 template< typename TSourceImage >
185 {
186  // Process object is not const-correct so the const casting is required.
187  return const_cast<DistanceMatrixType *>(this->GetNthInput(2));
188 }
189 
193 template< typename TSourceImage >
194 void
197 {
198  itkDebugMacro(<< "itkMedialNodeTripletCorrespondenceProcess::GenerateData() called");
199 
200  // Optional output file for debugging purposes.
201  std::ofstream OutputFile;
202  if(m_CreateOutputFile)
203  OutputFile.open("triplet_creation_log.txt",std::ios::out);
204 
205  // Pointers to the input, output objects.
206  m_InputDataStructure = dynamic_cast<InputDataStructureType*>(ProcessObject::GetInput(0));
207  m_DistanceMatrixA = dynamic_cast<DistanceMatrixType*>(ProcessObject::GetInput(1));
208  m_DistanceMatrixB = dynamic_cast<DistanceMatrixType*>(ProcessObject::GetInput(2));
209  m_CoreAtomImageA = dynamic_cast<CoreAtomImageType*>(ProcessObject::GetInput(3));
210  m_CoreAtomImageB = dynamic_cast<CoreAtomImageType*>(ProcessObject::GetInput(4));
211  m_OutputDataStructure = dynamic_cast<OutputDataStructureType*>(ProcessObject::GetOutput(0));
212 
213  int counter = 0;
214  int counter2 = 0;
215  int counter3 = 0;
216 
217  double TemporaryDistanceA;
218  double TemporaryDistanceB;
219 
220  // Get node pointer lists.
221  typedef BloxCoreAtomPixel<NDimensions> PixelType;
222  typedef std::vector<PixelType*> NodePointerListType;
223 
224  NodePointerListType NodePointerListA;
225  NodePointerListType NodePointerListB;
226 
227  NodePointerListA = *(m_CoreAtomImageA->GetNodePointerList());
228  NodePointerListB = *(m_CoreAtomImageB->GetNodePointerList());
229 
230  typename InputDataStructureType::NodeListType::iterator NodeListIterator = m_InputDataStructure->m_NodeList->begin();
231 
232  typename InputDataStructureType::SecondaryNodeListType::iterator SecondaryListIterator1;
233  typename InputDataStructureType::SecondaryNodeListType::iterator SecondaryListIterator2;
234  typename InputDataStructureType::CorrespondingListType::iterator CorrespondingListIterator1;
235  typename InputDataStructureType::CorrespondingListType::iterator CorrespondingListIterator2;
236 
237  bool BaseTripAdded;
238 
239  while( NodeListIterator != m_InputDataStructure->m_NodeList->end() )//iterate through NodeList in pair structure
240  {
241  counter2 = 0;
242 
243  // Create a new CorresponsingPairList.
245 
246  for(SecondaryListIterator1 = NodeListIterator->begin();SecondaryListIterator1 != NodeListIterator->end();SecondaryListIterator1++)//iterate through SecondaryNodeLists in pair structure
247  {
248  SecondaryListIterator2 = NodeListIterator->begin();
249  counter3 = 0;
250 
251  while( SecondaryListIterator2 != NodeListIterator->end() && counter3 < counter2)//concurrent iteration through SecondaryNodeList
252  { //with different iterator
253  if(m_CreateOutputFile)
254  {
255  OutputFile << "\n\n**NEW BASE TRIPLET**" << "\nTesting Triplet: (" << NodeListIterator->GetIndex() << ","
256  << SecondaryListIterator1->GetIndex(0) << "," << SecondaryListIterator2->GetIndex(0) << ") : ";
257  }
258 
259  //If statement to make sure no two indices in the BASE triplet are the same
260  if(NodeListIterator->GetIndex() != SecondaryListIterator1->GetIndex(0) &&
261  SecondaryListIterator1->GetIndex(0) != SecondaryListIterator2->GetIndex(0) &&
262  NodeListIterator->GetIndex() != SecondaryListIterator2->GetIndex(0))
263  {
264 
265  if(m_CreateOutputFile)
266  OutputFile << " no duplicate indices :)" << std::endl;
267 
268  // Starting a new base triplet, so:
269  BaseTripAdded = false;
270 
271  // our triplet is represented by:
272  // it->GetIndex(0);
273  // SecondaryListIterator1->GetIndex(0);
274  // SecondaryListIterator2->GetIndex(0);
275 
276  typename OutputDataStructureType::CorrespondingListType * CorrespondingTripletListPointer = new typename OutputDataStructureType::CorrespondingListType();
277  typename OutputDataStructureType::CorrespondingListType::iterator CorrespondingTripletListIterator;
278 
279  bool FoundCorrespondingTriplet = false;
280 
281  // Iterate through the two corresponding pair lists simultaneously
282  // finding every possible pair, and testing for triplet correspondence.
283  //iterate through CorrPairList of SecondaryListIterator1
284  for(CorrespondingListIterator1 = SecondaryListIterator1->begin();CorrespondingListIterator1 != SecondaryListIterator1->end();CorrespondingListIterator1++)
285  {
286  //iterate through CorrPairList of SecondaryListIterator2
287  for(CorrespondingListIterator2 = SecondaryListIterator2->begin();CorrespondingListIterator2 != SecondaryListIterator2->end();CorrespondingListIterator2++)
288  {
289  // Check NodeIndex[0] (base node correspondence) in the corresponding pairs to make sure they are the
290  // same node in the corresponding image.
291  if(CorrespondingListIterator1->GetNodeIndex(0) == CorrespondingListIterator2->GetNodeIndex(0))
292  {
293  // Check to see if the corresponding triplet has a duplicate node.
294  if(CorrespondingListIterator1->GetNodeIndex(0) != CorrespondingListIterator1->GetNodeIndex(1) &&
295  CorrespondingListIterator1->GetNodeIndex(0) != CorrespondingListIterator2->GetNodeIndex(1) &&
296  CorrespondingListIterator1->GetNodeIndex(1) != CorrespondingListIterator2->GetNodeIndex(1))
297  {
298  bool DuplicateCorrespondingTriplet = false;
299 
300  if(BaseTripAdded == true)
301  {
302  // Need an iterator for CorrespondingTripletListPointer.
303  CorrespondingTripletListIterator = CorrespondingTripletListPointer->begin();
304 
305  while( CorrespondingTripletListIterator != CorrespondingTripletListPointer->end() && DuplicateCorrespondingTriplet == false)
306  {
307  if( (CorrespondingListIterator1->GetNodeIndex(0) == CorrespondingTripletListIterator->GetNodeIndex(0) ||
308  CorrespondingListIterator1->GetNodeIndex(1) == CorrespondingTripletListIterator->GetNodeIndex(0) ||
309  CorrespondingListIterator2->GetNodeIndex(1) == CorrespondingTripletListIterator->GetNodeIndex(0)) &&
310  (CorrespondingListIterator1->GetNodeIndex(0) == CorrespondingTripletListIterator->GetNodeIndex(1) ||
311  CorrespondingListIterator1->GetNodeIndex(1) == CorrespondingTripletListIterator->GetNodeIndex(1) ||
312  CorrespondingListIterator2->GetNodeIndex(1) == CorrespondingTripletListIterator->GetNodeIndex(1)) &&
313  (CorrespondingListIterator1->GetNodeIndex(0) == CorrespondingTripletListIterator->GetNodeIndex(2) ||
314  CorrespondingListIterator1->GetNodeIndex(1) == CorrespondingTripletListIterator->GetNodeIndex(2) ||
315  CorrespondingListIterator2->GetNodeIndex(1) == CorrespondingTripletListIterator->GetNodeIndex(2)) )
316  {
317  DuplicateCorrespondingTriplet = true;
318  }
319  CorrespondingTripletListIterator++;
320  }
321  }
322 
323  if(m_CreateOutputFile)
324  {
325  OutputFile << " Checking Triplet: (" << CorrespondingListIterator1->GetNodeIndex(0) << ","
326  << CorrespondingListIterator1->GetNodeIndex(1) << "," << CorrespondingListIterator2->GetNodeIndex(1)
327  << ") for correspondence" << std::endl;
328  }
329 
330  if(DuplicateCorrespondingTriplet == false)
331  {
332  // Get distances between secondary nodes in triplets.
333  TemporaryDistanceA = m_DistanceMatrixA->get(SecondaryListIterator1->GetIndex(0), SecondaryListIterator2->GetIndex(0));
334  TemporaryDistanceB = m_DistanceMatrixB->get(CorrespondingListIterator1->GetNodeIndex(1),CorrespondingListIterator2->GetNodeIndex(1));
335 
336  if( TemporaryDistanceA <= (TemporaryDistanceB+0.1) && TemporaryDistanceA >= (TemporaryDistanceB-0.1) )
337  {
338  // If this comes out true, we have found a corresponding triplet.
339 
340  if(m_CreateOutputFile)
341  OutputFile << " Distance test passed...this is a corresponding triplet" << std::endl;
342 
343  if(BaseTripAdded == false)
344  {
345  // Add base triplet.
346  ++m_NumberOfNodeBaseTriplets;
347 
348  // Set the index of the base node in the triplet.
349  TripletListPointer->SetIndex(NodeListIterator->GetIndex() );
350  // Set the index of the second node in the triplet.
351  CorrespondingTripletListPointer->SetIndex(0, SecondaryListIterator1->GetIndex(0) );
352  // Set the index of the third node in the triplet.
353  CorrespondingTripletListPointer->SetIndex(1, SecondaryListIterator2->GetIndex(0) );
354 
355  // Make a new Node for corresponding triplet.
356  OutputNodeType NewNode;
357 
358  NewNode.SetNodeIndex(0,CorrespondingListIterator1->GetNodeIndex(0));
359  NewNode.SetNodeIndex(1,CorrespondingListIterator1->GetNodeIndex(1));
360  NewNode.SetNodeIndex(2,CorrespondingListIterator2->GetNodeIndex(1));
361 
362  if(m_CreateOutputFile)
363  {
364  OutputFile << " Created node for corresponding triplet: (" << NewNode.GetNodeIndex(0)
365  << "," << NewNode.GetNodeIndex(1) << "," << NewNode.GetNodeIndex(2)
366  << ")" << std::endl;
367  }
368 
369  CorrespondingTripletListPointer->push_back(NewNode);
370 
371  if(m_CreateOutputFile)
372  OutputFile << " Added new node to CorrespondingTripletListPointer" << std::endl;
373 
374  FoundCorrespondingTriplet = true;
375 
376  BaseTripAdded = true;
377  }
378  else
379  {
380  if(m_CreateOutputFile)
381  {
382  OutputFile << " Base triplet " << "(" << NodeListIterator->GetIndex()
383  << "," << SecondaryListIterator1->GetIndex(0) << "," << SecondaryListIterator2->GetIndex(0)
384  << ")" << " is already in structure" << std::endl;
385  }
386 
387  OutputNodeType NewNode;
388 
389  NewNode.SetNodeIndex(0,CorrespondingListIterator1->GetNodeIndex(0));
390  NewNode.SetNodeIndex(1,CorrespondingListIterator1->GetNodeIndex(1));
391  NewNode.SetNodeIndex(2,CorrespondingListIterator2->GetNodeIndex(1));
392 
393  if(m_CreateOutputFile)
394  {
395  OutputFile << " Created node for corresponding triplet: (" << NewNode.GetNodeIndex(0)
396  << "," << NewNode.GetNodeIndex(1) << "," << NewNode.GetNodeIndex(2)
397  << ")" << std::endl;
398  }
399 
400  CorrespondingTripletListPointer->push_back(NewNode);
401  FoundCorrespondingTriplet = true;
402 
403  if(m_CreateOutputFile)
404  OutputFile << " Added new node to CorrespondingTripletListPointer" << std::endl;
405  }
406  }
407  }
408  else
409  {
410  if(m_CreateOutputFile)
411  OutputFile << " DUPLICATE" << std::endl;
412  }
413  }
414  }
415  }// End for CorrespondingListIterator2.
416  }// End for CorrespondingListIterator1.
417  // Add a new CorrespondingTripletList to the structure.
418  if(FoundCorrespondingTriplet == true)
419  {
420  TripletListPointer->push_back(*CorrespondingTripletListPointer);
421  }
422  }
423  counter++;
424  SecondaryListIterator2++;
425  counter3++;
426  }
427  counter2++;
428  }
429  NodeListIterator++;
430 
431  // Add a new index of the NodeList.
432  if(!TripletListPointer->empty())
433  {
434  m_OutputDataStructure->m_NodeList->push_back(*TripletListPointer);
435 
436  if(m_CreateOutputFile)
437  OutputFile << "\n Added new list to m_NodeList" << std::endl;
438  }
439  else
440  {
441  if(m_CreateOutputFile)
442  OutputFile << "TripletListPointer is EMPTY, NOT adding new list to m_NodeList" << std::endl;
443  }
444  }// End while loop for NodeListIterator.
445 
446  itkDebugMacro(<< "End Initialize Triplets: " << m_NumberOfTriplets << " found!" << std::endl);
447  itkDebugMacro(<< "Number of Base Triplets: " << m_NumberOfNodeBaseTriplets << " found!" << std::endl);
448 
449  if(m_CreateOutputFile)
450  OutputFile.close();
451 
452  itkDebugMacro(<< "Finished MedialNodeTripletCorrespondenceProcess\n");
453 }
454 
458 template< typename TSourceImage >
459 void
461 ::PrintSelf(std::ostream& os, Indent indent) const
462 {
463  Superclass::PrintSelf(os,indent);
464 }
465 
466 } // end namespace
467 
468 #endif

Generated at Sat Feb 2 2013 23:52:24 for Orfeo Toolbox with doxygen 1.8.1.1