KWStyle - itkSimplexMesh.txx
 
Matrix View
Description

1 /*=========================================================================
2
3 Program:   Insight Segmentation & Registration Toolkit
4 Module:    $RCSfile: itkSimplexMesh.txx.html,v $
5 Language:  C++
6 Date:      $Date: 2006/01/17 19:15:47 $
7 Version:   $Revision: 1.4 $
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 DEF #ifndef _itkSimplexMesh_txx
18 DEF #define _itkSimplexMesh_txx
19
20 #include "itkSimplexMesh.h"
21
22 #include "itkObjectFactory.h"
23 #include "itkProcessObject.h"
24 #include <algorithm>
25
26 #include <vxl_version.h>
27 #if VXL_VERSION_DATE_FULL > 20040406
28 # include <vnl/vnl_cross.h>
29 # define itk_cross_3d vnl_cross_3d
30 #else
31 # define itk_cross_3d cross_3d
32 #endif
33
34 namespace itk
35 {
36
37 /**
38  * A protected default constructor allows the New() routine to create an
39  * instance of SimplexMesh.   All the containers are initialized to empty
40  * containers.
41  */
42 template <typename TPixelType, unsigned int VDimension, typename TMeshTraits>
43 SimplexMesh<TPixelType, VDimension, TMeshTraits>
44 ::SimplexMesh() : m_LastCellId ( 0 ) 
45 {
46   m_GeometryData = GeometryMapType::New();
47 }
48
49
50 EML
51 /**
52  * Mesh Destructor takes care of releasing the memory of Cells
53  * and CellBoundaries objects for which normal pointers are
54  * stored.
55  */
56 template <typename TPixelType, unsigned int VDimension, typename TMeshTraits>
57 SimplexMesh<TPixelType, VDimension, TMeshTraits>
58 ::~SimplexMesh()
59 {
60   itkDebugMacro("Mesh Destructor ");
61
62   GeometryMapPointer  geometryMap = this->GetGeometryData();
63   GeometryMapIterator pointDataIterator = geometryMap->Begin();
64   GeometryMapIterator pointDataEnd = geometryMap->End();
65
66   while (pointDataIterator != pointDataEnd)
67     {
68     SimplexMeshGeometry* geometry = pointDataIterator->Value();
69     if( geometry )
70       {
71       delete geometry;
72       }
73     pointDataIterator++;
74     }
75   // clear the map
76   geometryMap->Initialize();
77   this->ReleaseCellsMemory();
78 }
79
80
81 template <typename TPixelType, unsigned int VDimension, typename TMeshTraits>
82 void 
83 SimplexMesh<TPixelType, VDimension, TMeshTraits>
84 ::CopyInformation(const DataObject *data)
85 {
86   const Superclass *mesh;
87
88   mesh = dynamic_cast<const Superclass*>(data);
89
90   if (mesh)
91     {
92     this->m_MaximumNumberOfRegions = mesh->GetMaximumNumberOfRegions();
93     }
94   else
95     {
96     // pointer could not be cast back down
97     itkExceptionMacro(<< "itk::Mesh::CopyInformation() cannot cast "
98                       << typeid(data).name() << " to "
99                       << typeid(Superclass*).name() );
100     }
101
102
103 }
104
105
106 template <typename TPixelType, unsigned int VDimension, typename TMeshTraits>
107 void 
108 SimplexMesh<TPixelType, VDimension, TMeshTraits>
109 ::SetBarycentricCoordinates(unsigned long idx, PointType value)
110 {
111   SimplexMeshGeometry* geometry = m_GeometryData->GetElement(idx);
112   geometry->eps = value;
113 }
114
115 template <typename TPixelType, unsigned int VDimension, typename TMeshTraits>
116 typename SimplexMesh<TPixelType, VDimension, TMeshTraits>::PointType
117 SimplexMesh<TPixelType, VDimension, TMeshTraits>
118 ::GetBarycentricCoordinates(unsigned long idx) const
119 {
120   return m_GeometryData->GetElement(idx)->eps;
121 }
122
123 template <typename TPixelType, unsigned int VDimension, typename TMeshTraits>
124 void 
125 SimplexMesh<TPixelType, VDimension, TMeshTraits>
126 ::SetReferenceMetrics(unsigned long idx, PointType value)
127 {
128   SimplexMeshGeometry* geometry = m_GeometryData->GetElement(idx);
129   geometry->referenceMetrics = value;
130 }
131
132
133 template <typename TPixelType, unsigned int VDimension, typename TMeshTraits>
134 typename SimplexMesh<TPixelType, VDimension, TMeshTraits>::PointType
135 SimplexMesh<TPixelType, VDimension, TMeshTraits>
136 ::GetReferenceMetrics(unsigned long idx) const
137 {
138   return m_GeometryData->GetElement(idx)->referenceMetrics;
139 }
140
141 template <typename TPixelType, unsigned int VDimension, typename TMeshTraits>
142 void 
143 SimplexMesh<TPixelType, VDimension, TMeshTraits>
144 ::SetPhi(unsigned long idx, double value)
145 {
146   SimplexMeshGeometry* geometry = m_GeometryData->GetElement(idx);
147   geometry->phi = value;
148 }
149
150
151 template <typename TPixelType, unsigned int VDimension, typename TMeshTraits>
152 double
153 SimplexMesh<TPixelType, VDimension, TMeshTraits>
154 ::GetPhi(unsigned long idx) const
155 {
156   SimplexMeshGeometry* geometry = m_GeometryData->GetElement(idx);
157   PointType test;
158   this->GetPoint(idx, &test);
159
160   return m_GeometryData->GetElement(idx)->phi;
161 }
162
163 template <typename TPixelType, unsigned int VDimension, typename TMeshTraits>
164 void 
165 SimplexMesh<TPixelType, VDimension, TMeshTraits>
166 ::SetMeanCurvature(unsigned long idx, double value)
167 {
168   SimplexMeshGeometry* geometry = m_GeometryData->GetElement(idx);
169   geometry->meanCurvature = value;
170 }
171
172
173 template <typename TPixelType, unsigned int VDimension, typename TMeshTraits>
174 double 
175 SimplexMesh<TPixelType, VDimension, TMeshTraits>
176 ::GetMeanCurvature(unsigned long idx) const
177 {
178   return m_GeometryData->GetElement(idx)->meanCurvature;
179 }
180
181 template <typename TPixelType, unsigned int VDimension, typename TMeshTraits>
182 void 
183 SimplexMesh<TPixelType, VDimension, TMeshTraits>
184 ::SetRadius(unsigned long idx, double value)
185 {
186   SimplexMeshGeometry* geometry = m_GeometryData->GetElement(idx);
187   geometry->circleRadius = value;
188 }
189
190
191 template <typename TPixelType, unsigned int VDimension, typename TMeshTraits>
192 double
193 SimplexMesh<TPixelType, VDimension, TMeshTraits>
194 ::GetRadius(unsigned long idx) const
195 {
196   return m_GeometryData->GetElement(idx)->circleRadius;
197 }
198
199 template <typename TPixelType, unsigned int VDimension, typename TMeshTraits>
200 void 
201 SimplexMesh<TPixelType, VDimension, TMeshTraits>
202 ::SetDistance(unsigned long idx, double value)
203 {
204   SimplexMeshGeometry* geometry = m_GeometryData->GetElement(idx);
205   geometry->distance = value;
206 }
207
208 template <typename TPixelType, unsigned int VDimension, typename TMeshTraits>
209 double
210 SimplexMesh<TPixelType, VDimension, TMeshTraits>
211 ::GetDistance(unsigned long idx) const
212 {
213   return m_GeometryData->GetElement(idx)->distance;
214 }
215
216 template <typename TPixelType, unsigned int VDimension, typename TMeshTraits>
217 unsigned long 
218 SimplexMesh<TPixelType, VDimension, TMeshTraits>
219 ::AddEdge(unsigned long startPointId, unsigned long endPointId)
220
221   CellAutoPointer NewCellPointer;
222   unsigned long edgeId = m_LastCellId;
223
224   NewCellPointer.TakeOwnership( new LineType );
225   NewCellPointer->SetPointId( 0, startPointId );
226   NewCellPointer->SetPointId( 1, endPointId );
227
228   this->SetCell( edgeId, NewCellPointer );
229   m_LastCellId++;
230   return edgeId;
231 }
232
233 template <typename TPixelType, unsigned int VDimension, typename TMeshTraits>
234 unsigned long 
235 SimplexMesh<TPixelType, VDimension, TMeshTraits>
236 ::AddFace(CellAutoPointer &cellPointer)
237 {
238   this->SetCell( m_LastCellId , cellPointer );
239   m_LastCellId++;
240   return m_LastCellId-1;      
241 }
242
243 template <typename TPixelType, unsigned int VDimension, typename TMeshTraits>
244 unsigned long 
245 SimplexMesh<TPixelType, VDimension, TMeshTraits>
246 ::ReplaceFace(unsigned long replaceIndex, CellAutoPointer &cellPointer)
247 {
248   this->GetCells()->DeleteIndex( replaceIndex );
249   this->SetCell( replaceIndex , cellPointer );
250   this->SetCellData( replaceIndex , (PixelType) 1.0 );
251   return replaceIndex;      
252 }
253
254 /* PrintSelf. */
255 template <typename TPixelType, unsigned int VDimension, typename TMeshTraits>
256 void 
257 SimplexMesh<TPixelType, VDimension, TMeshTraits>
258 ::PrintSelf(std::ostream& os, Indent indent) const
259 {
260   this->Superclass::PrintSelf(os,indent);
261
262   os << indent << "LastCellId = " << m_LastCellId << std::endl;
263
264   CellsContainerPointer cells = this->GetCells();
265   CellsContainerIterator cellIt = cells->Begin();
266
267   os << indent << "Cells Point Ids:" << std::endl;
268   while ( cellIt != cells->End() )
269     {
270     os << indent << "cell id: " << cellIt->Index() << ", point ids: ";
271     CellType *nextCell = cellIt->Value();
272 SEM     typename CellType::PointIdIterator pointIt = nextCell->PointIdsBegin() ;
273     while (pointIt != nextCell->PointIdsEnd() ) { os << *pointIt++ << "-"; }
274     os << std::endl;
275     cellIt++;
276     }
277
278   PointsContainerConstIterator pointsIt = this->GetPoints()->Begin();
279   os << indent << "Point locations:" << std::endl;
280   while ( pointsIt != this->GetPoints()->End() )
281     {
282 LEN     os << indent << "pt index:" << pointsIt->Index() << " , coords: " << pointsIt->Value() << std::endl;
283     pointsIt++;
284     }
285
286   GeometryMapPointer geometryMap = this->GetGeometryData();
287   GeometryMapIterator pointDataIterator = geometryMap->Begin();
288   GeometryMapIterator pointDataEnd = geometryMap->End();
289
290   while (pointDataIterator != pointDataEnd)
291     {
292     SimplexMeshGeometry* geometry = pointDataIterator->Value();
293 LEN     os << indent << "Mesh Geometry Data for point:"<< pointDataIterator->Index() << std::endl;
294     os << indent << "Direct Neighbors indices: " 
295        << geometry->neighborIndices[0] << ", "
296        << geometry->neighborIndices[1] << ", " 
297        << geometry->neighborIndices[2] << std::endl;
298     pointDataIterator++;
299     }
300 }
301
302 template <typename TPixelType, unsigned int VDimension, typename TMeshTraits>
303 void 
304 SimplexMesh<TPixelType, VDimension, TMeshTraits>
305 ::SetGeometryData(unsigned long pointId, SimplexMeshGeometry* geometryData)
306 {
307   m_GeometryData->InsertElement(pointId, geometryData);
308 }
309
310
311 template <typename TPixelType, unsigned int VDimension, typename TMeshTraits>
312 typename SimplexMesh<TPixelType, VDimension, TMeshTraits>::IndexArray 
313 SimplexMesh<TPixelType, VDimension, TMeshTraits>
314 ::GetNeighbors(unsigned long idx) const
315 {
316   return m_GeometryData->GetElement(idx)->neighborIndices;
317 }
318
319 template <typename TPixelType, unsigned int VDimension, typename TMeshTraits>
320 typename SimplexMesh<TPixelType, VDimension, TMeshTraits>::NeighborListType*  
321 SimplexMesh<TPixelType, VDimension, TMeshTraits>
322 LEN ::GetNeighbors(unsigned long idx, unsigned int radius, NeighborListType* list ) const
323 {
324   if (list == NULL)
325     {
326     list = new NeighborListType();
327     IndexArray neighborArray = GetNeighbors(idx);
328     list->push_back(neighborArray[0]);
329     list->push_back(neighborArray[1]);
330     list->push_back(neighborArray[2]);
331
332     if(radius>0)
333       {
334       list = GetNeighbors(neighborArray[0], radius-1, list);
335       list = GetNeighbors(neighborArray[1], radius-1, list);
336       list = GetNeighbors(neighborArray[2], radius-1, list);
337       }
338     NeighborListType::iterator it = std::find( list->begin(),list->end(),idx );
339     if (it != list->end()) list->erase(it);
340
341     return list;
342     }
343   else 
344     {
345     IndexArray neighborArray = GetNeighbors(idx);
346
347 LEN     NeighborListType::iterator foundIt1 = std::find( list->begin(),list->end(),neighborArray[0] );
348 LEN     NeighborListType::iterator foundIt2 = std::find( list->begin(),list->end(),neighborArray[1] );
349 LEN     NeighborListType::iterator foundIt3 = std::find( list->begin(),list->end(),neighborArray[2] );
350     NeighborListType::iterator endIt = list->end();
351     bool found1=false, found2=false, found3=false;
352
353     if (foundIt1 != endIt) found1 =true;
354     if (foundIt2 != endIt) found2 = true;
355     if (foundIt3 != endIt) found3 = true;
356
357     if (!found1) list->push_back(neighborArray[0]);
358     if (!found2) list->push_back(neighborArray[1]);
359     if (!found3) list->push_back(neighborArray[2]);
360
361     if (radius == 0) 
362       {
363       return list;
364       }
365     else
366       {
367       list = GetNeighbors(neighborArray[0], radius-1, list);
368       list = GetNeighbors(neighborArray[1], radius-1, list);
369       list = GetNeighbors(neighborArray[2], radius-1, list);
370       return list;
371       }
372     }
373 }
374
375 template <typename TPixelType, unsigned int VDimension, typename TMeshTraits>
376 void
377 SimplexMesh<TPixelType, VDimension, TMeshTraits>
378 ::AddNeighbor(unsigned long pointIdx, unsigned long neighborIdx)
379 {
380   SimplexMeshGeometry* data = m_GeometryData->GetElement(pointIdx);
381
382   for (int i = 0; i < 3; i++)
383     {
384 LEN     if (data->neighborIndices[i] == ((unsigned long)NumericTraits<unsigned long>::max() ))
385       {
386       data->neighborIndices[i] = neighborIdx;
387       break;
388       }
389     }
390 }
391
392 template <typename TPixelType, unsigned int VDimension, typename TMeshTraits>
393 void 
394 SimplexMesh<TPixelType, VDimension, TMeshTraits>
395 LEN ::ReplaceNeighbor(unsigned long pointIdx, unsigned long oldIdx,unsigned long newIdx)
396 {
397   SimplexMeshGeometry* data = m_GeometryData->GetElement(pointIdx);
398
399   for (int i = 0; i < 3;i++)
400     {
401     if (data->neighborIndices[i] == oldIdx)
402       {
403       data->neighborIndices[i] = newIdx;
404       }
405     }
406 }
407
408
409 template <typename TPixelType, unsigned int VDimension, typename TMeshTraits>
410 void 
411 SimplexMesh<TPixelType, VDimension, TMeshTraits>
412 LEN ::SwapNeighbors(unsigned long pointIdx, unsigned long firstIdx,unsigned long secondIdx)
413 {
414   SimplexMeshGeometry* data = m_GeometryData->GetElement(pointIdx);
415   int i;
416   int firstFound = -1;
417   int secondFound = -1;
418
419   for (i = 0; i < 3;i++)
420     {
421     if (data->neighborIndices[i] == firstIdx)
422       {
423       firstFound = i;
424       }
425     else if (data->neighborIndices[i] == secondIdx)
426       {
427       secondFound = i;
428       }
429     }
430   if(firstFound == -1 || secondFound == -1)
431     {
432     itkExceptionMacro("first and second not found");
433     }
434   data->neighborIndices[firstFound] = secondIdx;
435   data->neighborIndices[secondFound] = firstIdx;
436 }
437
438 template <typename TPixelType, unsigned int VDimension, typename TMeshTraits>
439 typename SimplexMesh<TPixelType, VDimension, TMeshTraits>::PointType 
440 SimplexMesh<TPixelType, VDimension, TMeshTraits>
441 ::ComputeNormal(unsigned long idx ) const
442 {
443   PointType p,n1,n2,n3;
444
445   IndexArray neighbors = this->GetNeighbors( idx );  
446   this->GetPoint(idx,&p);
447   this->GetPoint(neighbors[0],&n1);
448   this->GetPoint(neighbors[1],&n2);
449   this->GetPoint(neighbors[2],&n3);
450
451   // compute normals
452   PointType normal;
453   normal.Fill(0.0);
454   VectorType z;
455 LEN   z.SetVnlVector( itk_cross_3d((n2-n1).GetVnlVector() , (n3-n1).GetVnlVector()) );
456   z.Normalize();
457   normal += z;
458
459   return normal;
460 }
461
462 // end namespace itk
463
464 #endif
465

Generated by KWStyle 1.0b on Tuesday January,17 at 02:14:46PM
© Kitware Inc.