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  *  Portions of this file are subject to the VTK Toolkit Version 3 copyright.
21  *
22  *  Copyright (c) Ken Martin, Will Schroeder, Bill Lorensen
23  *
24  *  For complete copyright, license and disclaimer of warranty information
25  *  please refer to the NOTICE file at the top of the ITK source tree.
26  *
27  *=========================================================================*/
28 #ifndef itkMeshToMeshFilter_hxx
29 #define itkMeshToMeshFilter_hxx
30 
31 #include "itkMeshToMeshFilter.h"
32 
33 namespace itk
34 {
35 /**
36  *
37  */
38 template< typename TInputMesh, typename TOutputMesh >
39 MeshToMeshFilter< TInputMesh, TOutputMesh >
MeshToMeshFilter()40 ::MeshToMeshFilter()
41 {
42   // Modify superclass default values, can be overridden by subclasses
43   this->SetNumberOfRequiredInputs(1);
44 }
45 
46 /**
47  *
48  */
49 template< typename TInputMesh, typename TOutputMesh >
50 void
51 MeshToMeshFilter< TInputMesh, TOutputMesh >
SetInput(const TInputMesh * input)52 ::SetInput(const TInputMesh *input)
53 {
54   // Process object is not const-correct so the const_cast is required here
55   this->ProcessObject::SetNthInput( 0,
56                                     const_cast< TInputMesh * >( input ) );
57 }
58 
59 /**
60  *
61  */
62 template< typename TInputMesh, typename TOutputMesh >
63 const typename MeshToMeshFilter< TInputMesh, TOutputMesh >::InputMeshType *
64 MeshToMeshFilter< TInputMesh, TOutputMesh >
GetInput() const65 ::GetInput() const
66 {
67   return itkDynamicCastInDebugMode< const TInputMesh * >( this->GetPrimaryInput() );
68 }
69 
70 /**
71  *
72  */
73 template< typename TInputMesh, typename TOutputMesh >
74 const typename MeshToMeshFilter< TInputMesh, TOutputMesh >::InputMeshType *
75 MeshToMeshFilter< TInputMesh, TOutputMesh >
GetInput(unsigned int idx) const76 ::GetInput(unsigned int idx) const
77 {
78   return dynamic_cast< const TInputMesh * >
79          ( this->ProcessObject::GetInput(idx) );
80 }
81 
82 template< typename TInputMesh, typename TOutputMesh >
83 void
84 MeshToMeshFilter< TInputMesh, TOutputMesh >
CopyInputMeshToOutputMeshPoints()85 ::CopyInputMeshToOutputMeshPoints()
86 {
87   const InputMeshType *inputMesh   =  this->GetInput();
88   OutputMeshPointer    outputMesh   =  this->GetOutput();
89 
90   using OutputPointsContainer = typename TOutputMesh::PointsContainer;
91   using InputPointsContainer = typename TInputMesh::PointsContainer;
92 
93   typename OutputPointsContainer::Pointer outputPoints = OutputPointsContainer::New();
94   const InputPointsContainer *inputPoints = inputMesh->GetPoints();
95 
96   if ( inputPoints )
97     {
98     outputPoints->Reserve( inputPoints->Size() );
99 
100     typename InputPointsContainer::ConstIterator inputItr = inputPoints->Begin();
101     typename InputPointsContainer::ConstIterator inputEnd = inputPoints->End();
102 
103     typename OutputPointsContainer::Iterator outputItr = outputPoints->Begin();
104 
105     while ( inputItr != inputEnd )
106       {
107       outputItr.Value() = inputItr.Value();
108       ++inputItr;
109       ++outputItr;
110       }
111 
112     outputMesh->SetPoints(outputPoints);
113     }
114 }
115 
116 template< typename TInputMesh, typename TOutputMesh >
117 void
118 MeshToMeshFilter< TInputMesh, TOutputMesh >
CopyInputMeshToOutputMeshPointData()119 ::CopyInputMeshToOutputMeshPointData()
120 {
121   const InputMeshType *inputMesh   =  this->GetInput();
122   OutputMeshPointer    outputMesh   =  this->GetOutput();
123 
124   using OutputPointDataContainer = typename TOutputMesh::PointDataContainer;
125   using InputPointDataContainer = typename TInputMesh::PointDataContainer;
126 
127   typename OutputPointDataContainer::Pointer outputPointData = OutputPointDataContainer::New();
128   const InputPointDataContainer *inputPointData = inputMesh->GetPointData();
129 
130   if ( inputPointData )
131     {
132     outputPointData->Reserve( inputPointData->Size() );
133 
134     typename InputPointDataContainer::ConstIterator inputItr = inputPointData->Begin();
135     typename InputPointDataContainer::ConstIterator inputEnd = inputPointData->End();
136 
137     typename OutputPointDataContainer::Iterator outputItr = outputPointData->Begin();
138 
139     while ( inputItr != inputEnd )
140       {
141       outputItr.Value() = inputItr.Value();
142       ++inputItr;
143       ++outputItr;
144       }
145 
146     outputMesh->SetPointData(outputPointData);
147     }
148 }
149 
150 template< typename TInputMesh, typename TOutputMesh >
151 void
152 MeshToMeshFilter< TInputMesh, TOutputMesh >
CopyInputMeshToOutputMeshCellLinks()153 ::CopyInputMeshToOutputMeshCellLinks()
154 {
155   const InputMeshType *inputMesh   =  this->GetInput();
156   OutputMeshPointer    outputMesh   =  this->GetOutput();
157 
158   using OutputCellLinksContainer = typename TOutputMesh::CellLinksContainer;
159   using InputCellLinksContainer = typename TInputMesh::CellLinksContainer;
160 
161   typename OutputCellLinksContainer::Pointer outputCellLinks = OutputCellLinksContainer::New();
162   const InputCellLinksContainer *inputCellLinks = inputMesh->GetCellLinks();
163 
164   if ( inputCellLinks )
165     {
166     outputCellLinks->Reserve( inputCellLinks->Size() );
167 
168     typename InputCellLinksContainer::ConstIterator inputItr = inputCellLinks->Begin();
169     typename InputCellLinksContainer::ConstIterator inputEnd = inputCellLinks->End();
170 
171     typename OutputCellLinksContainer::Iterator outputItr = outputCellLinks->Begin();
172 
173     while ( inputItr != inputEnd )
174       {
175       outputItr.Value() = inputItr.Value();
176       ++inputItr;
177       ++outputItr;
178       }
179 
180     outputMesh->SetCellLinks(outputCellLinks);
181     }
182 }
183 
184 template< typename TInputMesh, typename TOutputMesh >
185 void
186 MeshToMeshFilter< TInputMesh, TOutputMesh >
CopyInputMeshToOutputMeshCells()187 ::CopyInputMeshToOutputMeshCells()
188 {
189   const InputMeshType *inputMesh   =  this->GetInput();
190   OutputMeshPointer    outputMesh   =  this->GetOutput();
191 
192   using OutputCellsContainer = typename TOutputMesh::CellsContainer;
193   using InputCellsContainer = typename TInputMesh::CellsContainer;
194   using CellAutoPointer = typename TOutputMesh::CellAutoPointer;
195 
196   outputMesh->SetCellsAllocationMethod(OutputMeshType::CellsAllocatedDynamicallyCellByCell);
197 
198   typename OutputCellsContainer::Pointer outputCells = OutputCellsContainer::New();
199   const InputCellsContainer *inputCells = inputMesh->GetCells();
200 
201   if ( inputCells )
202     {
203     outputCells->Reserve( inputCells->Size() );
204 
205     typename InputCellsContainer::ConstIterator inputItr = inputCells->Begin();
206     typename InputCellsContainer::ConstIterator inputEnd = inputCells->End();
207 
208     typename OutputCellsContainer::Iterator outputItr = outputCells->Begin();
209 
210     CellAutoPointer clone;
211 
212     while ( inputItr != inputEnd )
213       {
214 //      outputItr.Value() = inputItr.Value();
215       // BUG: FIXME: Here we are copying a pointer, which is a mistake. What we
216       // should do is to clone the cell.
217       inputItr.Value()->MakeCopy(clone);
218       outputItr.Value() = clone.ReleaseOwnership();
219 
220       ++inputItr;
221       ++outputItr;
222       }
223 
224     outputMesh->SetCells(outputCells);
225     }
226 }
227 
228 template< typename TInputMesh, typename TOutputMesh >
229 void
230 MeshToMeshFilter< TInputMesh, TOutputMesh >
CopyInputMeshToOutputMeshCellData()231 ::CopyInputMeshToOutputMeshCellData()
232 {
233   const InputMeshType *inputMesh   =  this->GetInput();
234   OutputMeshPointer    outputMesh   =  this->GetOutput();
235 
236   using OutputCellDataContainer = typename TOutputMesh::CellDataContainer;
237   using InputCellDataContainer = typename TInputMesh::CellDataContainer;
238 
239   typename OutputCellDataContainer::Pointer outputCellData = OutputCellDataContainer::New();
240   const InputCellDataContainer *inputCellData = inputMesh->GetCellData();
241 
242   if ( inputCellData )
243     {
244     outputCellData->Reserve( inputCellData->Size() );
245 
246     typename InputCellDataContainer::ConstIterator inputItr = inputCellData->Begin();
247     typename InputCellDataContainer::ConstIterator inputEnd = inputCellData->End();
248 
249     typename OutputCellDataContainer::Iterator outputItr = outputCellData->Begin();
250 
251     while ( inputItr != inputEnd )
252       {
253       outputItr.Value() = inputItr.Value();
254       ++inputItr;
255       ++outputItr;
256       }
257 
258     outputMesh->SetCellData(outputCellData);
259     }
260 }
261 } // end namespace itk
262 
263 #endif
264