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

Generated at Sun Jun 16 2013 00:08:21 for Orfeo Toolbox with doxygen 1.8.3.1