00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018 #ifndef MITKMESHUTIL_H_INCLUDED
00019 #define MITKMESHUTIL_H_INCLUDED
00020
00021
00022 #if(_MSC_VER==1200)
00023 #error MeshUtils currently not supported for MS Visual C++ 6.0. Sorry.
00024 #endif
00025
00026
00027 #include <itkLineCell.h>
00028 #include <itkTriangleCell.h>
00029 #include <itkPolygonCell.h>
00030 #include <itkQuadrilateralCell.h>
00031 #include <itkCellInterface.h>
00032
00033 #include <itkSphereMeshSource.h>
00034
00035
00036
00037 #include <itkAutomaticTopologyMeshSource.h>
00038 #include <itkRegularSphereMeshSource.h>
00039 #include <vnl/vnl_cross.h>
00040
00041 #include <vtkActor.h>
00042 #include <vtkCellArray.h>
00043 #include <vtkPolyData.h>
00044 #include <vtkUnstructuredGrid.h>
00045 #include <vtkPoints.h>
00046 #include <vtkPointData.h>
00047 #include <vtkCellData.h>
00048 #include <vtkProperty.h>
00049 #include <vtkFloatArray.h>
00050
00051 #include <mitkGeometry3D.h>
00052 #include <mitkSurface.h>
00053
00054 template <typename MeshType>
00055 class NullScalarAccessor
00056 {
00057 public:
00058 static inline vtkFloatingPointType GetPointScalar(typename MeshType::PointDataContainer* , typename MeshType::PointIdentifier , MeshType* = NULL, unsigned int = 0)
00059 {
00060 return (vtkFloatingPointType) 0.0;
00061 };
00062
00063 static inline vtkFloatingPointType GetCellScalar(typename MeshType::CellDataContainer* , typename MeshType::CellIdentifier , MeshType* = NULL, unsigned int = 0)
00064 {
00065 return (vtkFloatingPointType) 0.0;
00066 };
00067 };
00068
00069 template <typename MeshType>
00070 class MeshScalarAccessor
00071 {
00072 public:
00073 static inline vtkFloatingPointType GetPointScalar(typename MeshType::PointDataContainer* pointData, typename MeshType::PointIdentifier idx, MeshType* = NULL, unsigned int = 0)
00074 {
00075 return (vtkFloatingPointType)pointData->GetElement(idx);
00076 };
00077
00078 static inline vtkFloatingPointType GetCellScalar(typename MeshType::CellDataContainer* cellData, typename MeshType::CellIdentifier idx, MeshType* = NULL, unsigned int = 0)
00079 {
00080 return (vtkFloatingPointType)cellData->GetElement(idx);
00081 };
00082 };
00083
00084 template <typename MeshType>
00085 class MeanCurvatureAccessor : public NullScalarAccessor<MeshType>
00086 {
00087 public:
00088 static inline vtkFloatingPointType GetPointScalar(typename MeshType::PointDataContainer* , typename MeshType::PointIdentifier idx, MeshType* mesh, unsigned int = 0)
00089 {
00090 typename MeshType::PixelType dis = 0;
00091 mesh->GetPointData(idx, &dis);
00092 return (vtkFloatingPointType) dis;
00093 };
00094 };
00095
00096 template <typename MeshType>
00097 class SimplexMeshAccessor : public NullScalarAccessor<MeshType>
00098 {
00099 public:
00100 static inline vtkFloatingPointType GetPointScalar(typename MeshType::PointDataContainer* point, typename MeshType::PointIdentifier idx, MeshType* mesh, unsigned int type = 0 )
00101 {
00102 typename MeshType::GeometryMapPointer geometryData = mesh->GetGeometryData();
00103
00104 if (type == 0)
00105 {
00106 double val = mesh->GetMeanCurvature( idx );
00107 mesh->SetPointData(idx, val);
00108 return val;
00109 }
00110 else if (type == 1)
00111 {
00112 double val = geometryData->GetElement(idx)->meanTension;
00113 mesh->SetPointData(idx, val);
00114 return val;
00115 }
00116 else if (type == 2)
00117 {
00118 double val = geometryData->GetElement(idx)->externalForce.GetNorm();
00119 mesh->SetPointData(idx, val);
00120 return val;
00121 }
00122 else if (type == 3)
00123 return geometryData->GetElement(idx)->internalForce.GetNorm();
00124 else if (type == 4)
00125 return geometryData->GetElement(idx)->externalForce.GetNorm() *
00126 mesh->GetDistance(idx);
00127 else if (type == 5)
00128 {
00129 typename MeshType::PixelType dis = 0;
00130 mesh->GetPointData(idx, &dis);
00131 return (vtkFloatingPointType) dis;
00132 }
00133 else if (type == 6)
00134 {
00135 return (vtkFloatingPointType) ((geometryData->GetElement(idx))->allowSplitting);
00136 }
00137 else
00138 return (vtkFloatingPointType) 0;
00139 };
00140 };
00141
00149 template <typename MeshType, class ScalarAccessor=NullScalarAccessor<MeshType> >
00150 class MeshUtil
00151 {
00175 template <class InsertImplementation>
00176 class SwitchByCellType : public InsertImplementation
00177 {
00178
00179 typedef typename itk::CellInterface< typename MeshType::CellPixelType,
00180 typename MeshType::CellTraits > CellInterfaceType;
00181 typedef itk::LineCell<CellInterfaceType> floatLineCell;
00182 typedef itk::TriangleCell<CellInterfaceType> floatTriangleCell;
00183 typedef itk::PolygonCell<CellInterfaceType> floatPolygonCell;
00184 typedef itk::QuadrilateralCell<CellInterfaceType> floatQuadrilateralCell;
00185 typedef itk::TetrahedronCell<CellInterfaceType> floatTetrahedronCell;
00186 typedef itk::HexahedronCell<CellInterfaceType> floatHexahedronCell;
00187 typedef typename CellInterfaceType::PointIdConstIterator PointIdIterator;
00188
00189 public:
00193 void Visit(unsigned long cellId, floatLineCell* t)
00194 {
00195 vtkIdType pts[2];
00196 int i=0;
00197 unsigned long num = t->GetNumberOfVertices();
00198 vtkIdType vtkCellId = -1;
00199 if (num==2) {
00200 for (PointIdIterator it=t->PointIdsBegin(); it!=t->PointIdsEnd(); it++) pts[i++] = *it;
00201 vtkCellId = this->InsertLine( (vtkIdType*)pts );
00202 }
00203
00204 if (this->m_UseCellScalarAccessor && vtkCellId >= 0)
00205 {
00206 this->m_CellScalars->InsertTuple1(vtkCellId,
00207 ScalarAccessor::GetCellScalar(this->m_CellData, cellId));
00208 }
00209 }
00210
00214 void Visit(unsigned long cellId, floatPolygonCell* t)
00215 {
00216 vtkIdType pts[4096];
00217 int i=0;
00218 unsigned long num = t->GetNumberOfVertices();
00219 vtkIdType vtkCellId = -1;
00220 if (num > 4096) {
00221 MITK_ERROR << "Problem in mitkMeshUtil: Polygon with more than maximum number of vertices encountered." << std::endl;
00222 }
00223 else if (num > 3) {
00224 for (PointIdIterator it=t->PointIdsBegin(); it!=t->PointIdsEnd(); it++) pts[i++] = *it;
00225 vtkCellId = this->InsertPolygon( num, (vtkIdType*)pts );
00226 }
00227 else if (num == 3) {
00228 for (PointIdIterator it=t->PointIdsBegin(); it!=t->PointIdsEnd(); it++) pts[i++] = *it;
00229 vtkCellId = this->InsertTriangle( (vtkIdType*)pts );
00230 }
00231 else if (num==2) {
00232 for (PointIdIterator it=t->PointIdsBegin(); it!=t->PointIdsEnd(); it++) pts[i++] = *it;
00233 vtkCellId = this->InsertLine( (vtkIdType*)pts );
00234 }
00235
00236 if (this->m_UseCellScalarAccessor && vtkCellId >= 0)
00237 {
00238 this->m_CellScalars->InsertTuple1(vtkCellId,
00239 ScalarAccessor::GetCellScalar(this->m_CellData, cellId));
00240 }
00241 }
00242
00246 void Visit(unsigned long cellId, floatTriangleCell* t)
00247 {
00248 vtkIdType pts[3];
00249 int i=0;
00250 unsigned long num = t->GetNumberOfVertices();
00251 vtkIdType vtkCellId = -1;
00252 if (num == 3) {
00253 for (PointIdIterator it=t->PointIdsBegin(); it!=t->PointIdsEnd(); it++) pts[i++] = *it;
00254 vtkCellId = this->InsertTriangle( (vtkIdType*)pts );
00255 }
00256 else if (num==2) {
00257 for (PointIdIterator it=t->PointIdsBegin(); it!=t->PointIdsEnd(); it++) pts[i++] = *it;
00258 vtkCellId = this->InsertLine( (vtkIdType*)pts );
00259 }
00260
00261 if (this->m_UseCellScalarAccessor && vtkCellId >= 0)
00262 {
00263 this->m_CellScalars->InsertTuple1(vtkCellId,
00264 ScalarAccessor::GetCellScalar(this->m_CellData, cellId));
00265 }
00266 }
00267
00271 void Visit(unsigned long cellId, floatQuadrilateralCell* t)
00272 {
00273 vtkIdType pts[4];
00274 int i=0;
00275 unsigned long num = t->GetNumberOfVertices();
00276 vtkIdType vtkCellId = -1;
00277 if (num == 4) {
00278 for (PointIdIterator it=t->PointIdsBegin(); it!=t->PointIdsEnd(); it++)
00279 {
00280 if (i == 2) pts[3] = *it;
00281 else if (i == 3) pts[2] = *it;
00282 else pts[i] = *it;
00283 i++;
00284
00285 }
00286 vtkCellId = this->InsertQuad( (vtkIdType*)pts );
00287 }
00288 else if (num == 3) {
00289 for (PointIdIterator it=t->PointIdsBegin(); it!=t->PointIdsEnd(); it++) pts[i++] = *it;
00290 vtkCellId = this->InsertTriangle( (vtkIdType*)pts );
00291 }
00292 else if (num==2) {
00293 for (PointIdIterator it=t->PointIdsBegin(); it!=t->PointIdsEnd(); it++) pts[i++] = *it;
00294 vtkCellId = this->InsertLine( (vtkIdType*)pts );
00295 }
00296
00297 if (this->m_UseCellScalarAccessor && vtkCellId >= 0)
00298 {
00299 this->m_CellScalars->InsertTuple1(vtkCellId,
00300 ScalarAccessor::GetCellScalar(this->m_CellData, cellId));
00301 }
00302 }
00303
00307 void Visit(unsigned long cellId, floatTetrahedronCell* t)
00308 {
00309 vtkIdType pts[4];
00310 int i=0;
00311 unsigned long num = t->GetNumberOfVertices();
00312 vtkIdType vtkCellId = -1;
00313 if (num == 4) {
00314 for (PointIdIterator it=t->PointIdsBegin(); it!=t->PointIdsEnd(); it++) pts[i++] = *it;
00315 vtkCellId = this->InsertTetra( (vtkIdType*)pts );
00316 }
00317 else if (num == 3) {
00318 for (PointIdIterator it=t->PointIdsBegin(); it!=t->PointIdsEnd(); it++) pts[i++] = *it;
00319 vtkCellId = this->InsertTriangle( (vtkIdType*)pts );
00320 }
00321 else if (num==2) {
00322 for (PointIdIterator it=t->PointIdsBegin(); it!=t->PointIdsEnd(); it++) pts[i++] = *it;
00323 vtkCellId = this->InsertLine( (vtkIdType*)pts );
00324 }
00325
00326 if (this->m_UseCellScalarAccessor && vtkCellId >= 0)
00327 {
00328 this->m_CellScalars->InsertTuple1(vtkCellId,
00329 ScalarAccessor::GetCellScalar(this->m_CellData, cellId));
00330 }
00331 }
00332
00336 void Visit(unsigned long cellId, floatHexahedronCell* t)
00337 {
00338 vtkIdType pts[8];
00339 int i=0;
00340 unsigned long num = t->GetNumberOfVertices();
00341 vtkIdType vtkCellId = -1;
00342 if (num == 8) {
00343 for (PointIdIterator it=t->PointIdsBegin(); it!=t->PointIdsEnd(); it++)
00344 {
00345 if (i == 2)
00346 pts[i++] = *(it+1);
00347 else if (i == 3)
00348 pts[i++] = *(it-1);
00349 else if (i == 6)
00350 pts[i++] = *(it+1);
00351 else if (i == 7)
00352 pts[i++] = *(it-1);
00353 else
00354 pts[i++] = *it;
00355 }
00356 vtkCellId = this->InsertHexahedron( (vtkIdType*)pts );
00357 }
00358 else if (num == 4) {
00359 for (PointIdIterator it=t->PointIdsBegin(); it!=t->PointIdsEnd(); it++) pts[i++] = *it;
00360 vtkCellId = this->InsertQuad( (vtkIdType*)pts );
00361 }
00362 else if (num == 3) {
00363 for (PointIdIterator it=t->PointIdsBegin(); it!=t->PointIdsEnd(); it++) pts[i++] = *it;
00364 vtkCellId = this->InsertTriangle( (vtkIdType*)pts );
00365 }
00366 else if (num==2) {
00367 for (PointIdIterator it=t->PointIdsBegin(); it!=t->PointIdsEnd(); it++) pts[i++] = *it;
00368 vtkCellId = this->InsertLine( (vtkIdType*)pts );
00369 }
00370
00371 if (this->m_UseCellScalarAccessor && vtkCellId >= 0)
00372 {
00373 this->m_CellScalars->InsertTuple1(vtkCellId,
00374 ScalarAccessor::GetCellScalar(this->m_CellData, cellId));
00375 }
00376 }
00377 };
00378
00391 template <class InsertImplementation>
00392 class ExactSwitchByCellType : public InsertImplementation
00393 {
00394
00395 typedef typename itk::CellInterface< typename MeshType::CellPixelType,
00396 typename MeshType::CellTraits > CellInterfaceType;
00397 typedef itk::LineCell<CellInterfaceType> floatLineCell;
00398 typedef itk::TriangleCell<CellInterfaceType> floatTriangleCell;
00399 typedef itk::PolygonCell<CellInterfaceType> floatPolygonCell;
00400 typedef itk::QuadrilateralCell<CellInterfaceType> floatQuadrilateralCell;
00401 typedef itk::TetrahedronCell<CellInterfaceType> floatTetrahedronCell;
00402 typedef itk::HexahedronCell<CellInterfaceType> floatHexahedronCell;
00403
00404 public:
00408 void Visit(unsigned long , floatLineCell* t)
00409 {
00410 unsigned long num = t->GetNumberOfVertices();
00411 vtkIdType *pts = (vtkIdType*)t->PointIdsBegin();
00412 if (num==2)
00413 this->InsertLine(pts);
00414 }
00415
00419 void Visit(unsigned long , floatPolygonCell* t)
00420 {
00421 unsigned long num = t->GetNumberOfVertices();
00422 vtkIdType *pts = (vtkIdType*)t->PointIdsBegin();
00423 if (num > 3)
00424 this->InsertPolygon(num, pts);
00425 }
00426
00430 void Visit(unsigned long , floatTriangleCell* t)
00431 {
00432 unsigned long num = t->GetNumberOfVertices();
00433 vtkIdType *pts = (vtkIdType*)t->PointIdsBegin();
00434 if (num == 3)
00435 this->InsertTriangle(pts);
00436 }
00437
00441 void Visit(unsigned long , floatQuadrilateralCell* t)
00442 {
00443 unsigned long num = t->GetNumberOfVertices();
00444 vtkIdType *pts = (vtkIdType*)t->PointIdsBegin();
00445 if (num == 4) {
00446 vtkIdType tmpId = pts[3];
00447 pts[3] = pts[4];
00448 pts[4] = tmpId;
00449 this->InsertQuad(pts);
00450 }
00451 }
00452
00456 void Visit(unsigned long , floatTetrahedronCell* t)
00457 {
00458 unsigned long num = t->GetNumberOfVertices();
00459 vtkIdType *pts = (vtkIdType*)t->PointIdsBegin();
00460 if (num == 4)
00461 this->InsertTetra(pts);
00462 }
00463
00467 void Visit(unsigned long , floatHexahedronCell* t)
00468 {
00469 unsigned long num = t->GetNumberOfVertices();
00470 vtkIdType *pts = (vtkIdType*)t->PointIdsBegin();
00471 if (num == 8)
00472 {
00473 vtkIdType tmp[8];
00474 for (unsigned int i = 0; i < 8; i++) tmp[i] = pts[i];
00475 pts[2] = tmp[3];
00476 pts[3] = tmp[2];
00477 pts[6] = tmp[7];
00478 pts[7] = tmp[6];
00479 this->InsertHexahedron(pts);
00480 }
00481 }
00482 };
00483
00490 class SingleCellArrayInsertImplementation
00491 {
00492 vtkCellArray* m_Cells;
00493 int* m_TypeArray;
00494
00495
00496 protected:
00497 bool m_UseCellScalarAccessor;
00498 vtkFloatArray* m_CellScalars;
00499 typename MeshType::CellDataContainer::Pointer m_CellData;
00500
00501 public:
00502
00503 SingleCellArrayInsertImplementation() : m_UseCellScalarAccessor(false) {}
00504
00507 void SetCellArray(vtkCellArray* cells)
00508 {
00509 m_Cells = cells;
00510 }
00511
00515 void SetTypeArray(int* i)
00516 {
00517 m_TypeArray = i;
00518 }
00519
00520 void SetUseCellScalarAccessor(bool flag)
00521 {
00522 m_UseCellScalarAccessor = flag;
00523 }
00524
00525 void SetCellScalars(vtkFloatArray* scalars)
00526 {
00527 m_CellScalars = scalars;
00528 }
00529
00530 vtkFloatArray* GetCellScalars() { return m_CellScalars; }
00531
00532 void SetMeshCellData(typename MeshType::CellDataContainer* data)
00533 {
00534 m_CellData = data;
00535 }
00536
00537 vtkIdType InsertLine(vtkIdType *pts)
00538 {
00539 vtkIdType cellId = m_Cells->InsertNextCell(2, pts);
00540 m_TypeArray[cellId] = VTK_LINE;
00541 return cellId;
00542 }
00543
00544 vtkIdType InsertTriangle(vtkIdType *pts)
00545 {
00546 vtkIdType cellId = m_Cells->InsertNextCell(3, pts);
00547 m_TypeArray[cellId] = VTK_TRIANGLE;
00548 return cellId;
00549 }
00550
00551 vtkIdType InsertPolygon(vtkIdType npts, vtkIdType *pts)
00552 {
00553 vtkIdType cellId = m_Cells->InsertNextCell(npts, pts);
00554 m_TypeArray[cellId] = VTK_POLYGON;
00555 return cellId;
00556 }
00557
00558 vtkIdType InsertQuad(vtkIdType *pts)
00559 {
00560 vtkIdType cellId = m_Cells->InsertNextCell(4, pts);
00561 m_TypeArray[cellId] = VTK_QUAD;
00562 return cellId;
00563 }
00564
00565 vtkIdType InsertTetra(vtkIdType *pts)
00566 {
00567 vtkIdType cellId = m_Cells->InsertNextCell(4, pts);
00568 m_TypeArray[cellId] = VTK_TETRA;
00569 return cellId;
00570 }
00571
00572 vtkIdType InsertHexahedron(vtkIdType *pts)
00573 {
00574 vtkIdType cellId = m_Cells->InsertNextCell(8, pts);
00575 m_TypeArray[cellId] = VTK_HEXAHEDRON;
00576 return cellId;
00577 }
00578 };
00579
00585 class DistributeInsertImplementation
00586 {
00587 vtkCellArray* m_LineCells;
00588 vtkCellArray* m_TriangleCells;
00589 vtkCellArray* m_PolygonCells;
00590 vtkCellArray* m_QuadCells;
00591
00592 protected:
00593 bool m_UseCellScalarAccessor;
00594 vtkFloatArray* m_CellScalars;
00595 typename MeshType::CellDataContainer::Pointer m_CellData;
00596
00597 public:
00598
00599 DistributeInsertImplementation() : m_UseCellScalarAccessor(false) {}
00600
00601
00604 void SetCellArrays(vtkCellArray* lines, vtkCellArray* triangles, vtkCellArray* polygons, vtkCellArray* quads)
00605 {
00606 m_LineCells = lines;
00607 m_TriangleCells = triangles;
00608 m_PolygonCells = polygons;
00609 m_QuadCells = quads;
00610 }
00611
00612 vtkIdType InsertLine(vtkIdType *pts)
00613 {
00614 return m_LineCells->InsertNextCell(2, pts);
00615 }
00616
00617 vtkIdType InsertTriangle(vtkIdType *pts)
00618 {
00619 return m_TriangleCells->InsertNextCell(3, pts);
00620 }
00621
00622 vtkIdType InsertPolygon(vtkIdType npts, vtkIdType *pts)
00623 {
00624 return m_PolygonCells->InsertNextCell(npts, pts);
00625 }
00626
00627 vtkIdType InsertQuad(vtkIdType *pts)
00628 {
00629 return m_QuadCells->InsertNextCell(4, pts);
00630 }
00631
00632 vtkIdType InsertTetra(vtkIdType *pts) { return -1; }
00633 vtkIdType InsertHexahedron(vtkIdType *pts) { return -1; }
00634 };
00635
00636
00637
00638
00639
00640
00641 typedef SwitchByCellType<SingleCellArrayInsertImplementation> SingleCellArrayUserVisitorType;
00642 typedef SwitchByCellType<DistributeInsertImplementation> DistributeUserVisitorType;
00643 typedef ExactSwitchByCellType<DistributeInsertImplementation> ExactUserVisitorType;
00644
00645 public:
00646
00647 typedef itk::MatrixOffsetTransformBase<typename MeshType::CoordRepType,3,3> ITKTransformType;
00648 typedef itk::MatrixOffsetTransformBase<mitk::ScalarType,3,3> MITKTransformType;
00649
00654 static void ConvertTransformToItk(const MITKTransformType* mitkTransform, ITKTransformType* itkTransform)
00655 {
00656 typename MITKTransformType::MatrixType mitkM = mitkTransform->GetMatrix();
00657 typename ITKTransformType::MatrixType itkM;
00658
00659 typename MITKTransformType::OffsetType mitkO = mitkTransform->GetOffset();
00660 typename ITKTransformType::OffsetType itkO;
00661
00662 for(short i = 0; i < 3; ++i)
00663 {
00664 for(short j = 0; j<3; ++j)
00665 {
00666 itkM[i][j] = (double)mitkM[i][j];
00667 }
00668 itkO[i] = (double)mitkO[i];
00669 }
00670
00671 itkTransform->SetMatrix(itkM);
00672 itkTransform->SetOffset(itkO);
00673 }
00674
00678 static typename MeshType::Pointer MeshFromPolyData(vtkPolyData* poly, mitk::Geometry3D* geometryFrame=NULL, mitk::Geometry3D* polyDataGeometryFrame=NULL)
00679 {
00680
00681 typename MeshType::Pointer output = MeshType::New();
00682 output->SetCellsAllocationMethod( MeshType::CellsAllocatedDynamicallyCellByCell );
00683
00684 typedef typename MeshType::CellDataContainer MeshCellDataContainerType;
00685
00686 output->SetCellData(MeshCellDataContainerType::New());
00687
00688
00689 vtkPoints* vtkpoints = poly->GetPoints();
00690 const unsigned int numPoints = poly->GetNumberOfPoints();
00691
00692
00693
00694
00695
00696
00697
00698
00699
00700 vtkFloatingPointType vtkpoint[3];
00701 typename MeshType::PointType itkPhysicalPoint;
00702 if(geometryFrame==NULL)
00703 {
00704 if(polyDataGeometryFrame==NULL)
00705 {
00706 for(unsigned int i=0; i < numPoints; ++i)
00707 {
00708 vtkpoints->GetPoint(i, vtkpoint);
00709
00710
00711 mitk::vtk2itk(vtkpoint, itkPhysicalPoint);
00712 output->SetPoint( i, itkPhysicalPoint );
00713 }
00714 }
00715 else
00716 {
00717 for(unsigned int i=0; i < numPoints; ++i)
00718 {
00719 vtkpoints->GetPoint(i, vtkpoint);
00720
00721
00722 mitk::Point3D mitkWorldPoint;
00723 mitk::vtk2itk(vtkpoint, mitkWorldPoint);
00724 polyDataGeometryFrame->IndexToWorld(mitkWorldPoint, mitkWorldPoint);
00725 mitk::vtk2itk(mitkWorldPoint, itkPhysicalPoint);
00726 output->SetPoint( i, itkPhysicalPoint );
00727 }
00728 }
00729 }
00730 else
00731 {
00732 mitk::Point3D mitkWorldPoint;
00733 if(polyDataGeometryFrame==NULL)
00734 {
00735 for(unsigned int i=0; i < numPoints; ++i)
00736 {
00737 vtkpoints->GetPoint(i, vtkpoint);
00738
00739
00740 mitk::vtk2itk(vtkpoint, mitkWorldPoint);
00741 geometryFrame->WorldToItkPhysicalPoint(mitkWorldPoint, itkPhysicalPoint);
00742 output->SetPoint( i, itkPhysicalPoint );
00743 }
00744 }
00745 else
00746 {
00747 for(unsigned int i=0; i < numPoints; ++i)
00748 {
00749 vtkpoints->GetPoint(i, vtkpoint);
00750
00751
00752 mitk::vtk2itk(vtkpoint, mitkWorldPoint);
00753 polyDataGeometryFrame->IndexToWorld(mitkWorldPoint, mitkWorldPoint);
00754 geometryFrame->WorldToItkPhysicalPoint(mitkWorldPoint, itkPhysicalPoint);
00755 output->SetPoint( i, itkPhysicalPoint );
00756 }
00757 }
00758 }
00759
00760 vtkCellArray* vtkcells = poly->GetPolys();
00761
00762
00763
00764
00765 int numcells = vtkcells->GetNumberOfCells();
00766 int* vtkCellTypes = new int[numcells];
00767 int cellId = 0;
00768
00769 int cellIdOfs = poly->GetNumberOfVerts() + poly->GetNumberOfLines();
00770 for(; cellId < numcells; cellId++)
00771 {
00772 vtkCellTypes[cellId] = poly->GetCellType( cellId+cellIdOfs );
00773 }
00774
00775
00776 vtkIdType npts;
00777 vtkIdType* pts;
00778 cellId = 0;
00779
00780 typedef typename MeshType::MeshTraits OMeshTraits;
00781 typedef typename OMeshTraits::PixelType OPixelType;
00782 typedef typename MeshType::CellTraits CellTraits;
00783 typedef typename itk::CellInterface<OPixelType, CellTraits> CellInterfaceType;
00784 typedef typename itk::TriangleCell<CellInterfaceType> TriCellType;
00785 typedef typename TriCellType::CellAutoPointer TriCellPointer;
00786
00787 TriCellPointer newCell;
00788 output->GetCells()->Reserve( poly->GetNumberOfPolys() + poly->GetNumberOfStrips() );
00789 output->GetCellData()->Reserve( poly->GetNumberOfPolys() + poly->GetNumberOfStrips() );
00790
00791 for(vtkcells->InitTraversal(); vtkcells->GetNextCell(npts, pts); cellId++)
00792 {
00793 switch(vtkCellTypes[cellId])
00794 {
00795 case VTK_TRIANGLE:
00796 {
00797 if (npts != 3) continue;
00798 unsigned long pointIds[3];
00799 pointIds[0] = (unsigned long) pts[0];
00800 pointIds[1] = (unsigned long) pts[1];
00801 pointIds[2] = (unsigned long) pts[2];
00802
00803 newCell.TakeOwnership( new TriCellType );
00804 newCell->SetPointIds(pointIds);
00805 output->SetCell(cellId, newCell );
00806 output->SetCellData(cellId, (typename MeshType::PixelType)3);
00807 break;
00808 }
00809
00810 case VTK_QUAD:
00811 {
00812 if (npts != 4 ) continue;
00813 unsigned long pointIds[3];
00814
00815 pointIds[0] = (unsigned long) pts[0];
00816 pointIds[1] = (unsigned long) pts[1];
00817 pointIds[2] = (unsigned long) pts[2];
00818 newCell.TakeOwnership( new TriCellType );
00819 newCell->SetPointIds(pointIds);
00820 output->SetCell(cellId, newCell );
00821 output->SetCellData(cellId, (typename MeshType::PixelType)3);
00822 cellId++;
00823
00824 pointIds[0] = (unsigned long) pts[2];
00825 pointIds[1] = (unsigned long) pts[3];
00826 pointIds[2] = (unsigned long) pts[0];
00827 newCell.TakeOwnership( new TriCellType );
00828 newCell->SetPointIds(pointIds);
00829 output->SetCell(cellId, newCell );
00830 output->SetCellData(cellId, (typename MeshType::PixelType)3);
00831 break;
00832 }
00833
00834 case VTK_EMPTY_CELL:
00835 {
00836 if (npts != 3)
00837 {
00838 MITK_ERROR << "Only empty triangle cell supported by now..." << std::endl;
00839 continue;
00840 }
00841 unsigned long pointIds[3];
00842 pointIds[0] = (unsigned long) pts[0];
00843 pointIds[1] = (unsigned long) pts[1];
00844 pointIds[2] = (unsigned long) pts[2];
00845
00846 newCell.TakeOwnership( new TriCellType );
00847 newCell->SetPointIds(pointIds);
00848 output->SetCell(cellId, newCell );
00849 output->SetCellData(cellId, (typename MeshType::PixelType)3);
00850 break;
00851 }
00852
00853
00854
00855
00856
00857 case VTK_POLYGON:
00858 case VTK_PIXEL:
00859 {
00860 if (npts != 4 ) continue;
00861 unsigned long pointIds[3];
00862 for ( unsigned int idx = 0; idx <= 1; idx++ )
00863 {
00864 pointIds[0] = (unsigned long) pts[idx];
00865 pointIds[1] = (unsigned long) pts[idx+1];
00866 pointIds[2] = (unsigned long) pts[idx+2];
00867 newCell.TakeOwnership( new TriCellType );
00868 newCell->SetPointIds(pointIds);
00869 output->SetCell(cellId+idx, newCell );
00870 output->SetCellData(cellId+idx, (typename MeshType::PixelType)3);
00871 }
00872 cellId++;
00873 break;
00874 }
00875
00876 case VTK_TETRA:
00877 case VTK_VOXEL:
00878 case VTK_HEXAHEDRON:
00879 case VTK_WEDGE:
00880 case VTK_PYRAMID:
00881 case VTK_PARAMETRIC_CURVE:
00882 case VTK_PARAMETRIC_SURFACE:
00883 default:
00884 MITK_WARN << "Warning, unhandled cell type "
00885 << vtkCellTypes[cellId] << std::endl;
00886 }
00887 }
00888
00889 if (poly->GetNumberOfStrips() != 0)
00890 {
00891 vtkcells = poly->GetStrips();
00892 numcells = vtkcells->GetNumberOfCells();
00893 vtkCellTypes = new int[numcells];
00894 int stripId = 0;
00895
00896 int stripIdOfs = poly->GetNumberOfVerts() + poly->GetNumberOfLines() + poly->GetNumberOfPolys();
00897 for(; stripId < numcells; stripId++)
00898 {
00899 vtkCellTypes[stripId] = poly->GetCellType( stripId+stripIdOfs );
00900 }
00901 stripId = 0;
00902
00903 vtkcells->InitTraversal();
00904 while( vtkcells->GetNextCell(npts, pts) )
00905 {
00906 if (vtkCellTypes[stripId] != VTK_TRIANGLE_STRIP)
00907 {
00908 MITK_ERROR << "Only triangle strips supported!" << std::endl;
00909 continue;
00910 }
00911 stripId++;
00912
00913 unsigned int numberOfTrianglesInStrip = npts - 2;
00914 unsigned long pointIds[3];
00915 pointIds[0] = (unsigned long) pts[0];
00916 pointIds[1] = (unsigned long) pts[1];
00917 pointIds[2] = (unsigned long) pts[2];
00918
00919 for( unsigned int t=0; t < numberOfTrianglesInStrip; t++ )
00920 {
00921 newCell.TakeOwnership( new TriCellType );
00922 newCell->SetPointIds(pointIds);
00923 output->SetCell(cellId, newCell );
00924 output->SetCellData(cellId, (typename MeshType::PixelType)3);
00925 cellId++;
00926 pointIds[0] = pointIds[1];
00927 pointIds[1] = pointIds[2];
00928 pointIds[2] = pts[t+3];
00929 }
00930 }
00931 }
00932
00933 output->BuildCellLinks();
00934 delete[] vtkCellTypes;
00935 return output;
00936 }
00937
00941 static typename MeshType::Pointer MeshFromSurface(mitk::Surface* surface, mitk::Geometry3D* geometryFrame=NULL)
00942 {
00943 if(surface == NULL)
00944 return NULL;
00945 return MeshFromPolyData(surface->GetVtkPolyData(), geometryFrame, surface->GetGeometry());
00946 }
00947
00951 static vtkUnstructuredGrid* MeshToUnstructuredGrid(
00952 MeshType* mesh,
00953 bool usePointScalarAccessor = false,
00954 bool useCellScalarAccessor = false,
00955 unsigned int pointDataType = 0,
00956 mitk::Geometry3D* geometryFrame=NULL)
00957 {
00961 typedef typename itk::CellInterfaceVisitorImplementation<typename MeshType::CellPixelType,
00962 typename MeshType::CellTraits,
00963 itk::LineCell< typename MeshType::CellType >,
00964 SingleCellArrayUserVisitorType> SingleCellArrayLineVisitor;
00965
00969 typedef typename itk::CellInterfaceVisitorImplementation<typename MeshType::CellPixelType,
00970 typename MeshType::CellTraits,
00971 itk::PolygonCell< typename MeshType::CellType >,
00972 SingleCellArrayUserVisitorType> SingleCellArrayPolygonVisitor;
00973
00977 typedef typename itk::CellInterfaceVisitorImplementation<typename MeshType::CellPixelType,
00978 typename MeshType::CellTraits,
00979 itk::TriangleCell<itk::CellInterface<typename MeshType::CellPixelType, typename MeshType::CellTraits > >,
00980 SingleCellArrayUserVisitorType> SingleCellArrayTriangleVisitor;
00981
00982
00986 typedef typename itk::CellInterfaceVisitorImplementation<typename MeshType::CellPixelType, typename MeshType::CellTraits,
00987 itk::QuadrilateralCell< itk::CellInterface<typename MeshType::CellPixelType, typename MeshType::CellTraits > >,
00988 SingleCellArrayUserVisitorType> SingleCellArrayQuadrilateralVisitor;
00989
00990
00994 typedef typename itk::CellInterfaceVisitorImplementation<typename MeshType::CellPixelType, typename MeshType::CellTraits,
00995 itk::TetrahedronCell< itk::CellInterface<typename MeshType::CellPixelType, typename MeshType::CellTraits > >,
00996 SingleCellArrayUserVisitorType> SingleCellArrayTetrahedronVisitor;
00997
00998
01002 typedef typename itk::CellInterfaceVisitorImplementation<typename MeshType::CellPixelType, typename MeshType::CellTraits,
01003 itk::HexahedronCell< itk::CellInterface<typename MeshType::CellPixelType, typename MeshType::CellTraits > >,
01004 SingleCellArrayUserVisitorType> SingleCellArrayHexahedronVisitor;
01005
01006
01007 int numPoints = mesh->GetNumberOfPoints();
01008 if(numPoints == 0)
01009 {
01010
01011 MITK_FATAL << "no points in Grid " << std::endl;
01012 exit(-1);
01013 }
01014
01015 vtkUnstructuredGrid* vgrid = vtkUnstructuredGrid::New();
01016
01017 #if ((VTK_MAJOR_VERSION > 4) || ((VTK_MAJOR_VERSION==4) && (VTK_MINOR_VERSION>=4) ))
01018 vtkPoints* vpoints = vtkPoints::New( VTK_DOUBLE );
01019 #else
01020 vtkPoints* vpoints = vtkPoints::New();
01021 #endif
01022 vtkFloatArray* pointScalars = vtkFloatArray::New();
01023 vtkFloatArray* cellScalars = vtkFloatArray::New();
01024 pointScalars->SetNumberOfComponents(1);
01025 cellScalars->SetNumberOfComponents(1);
01026
01027 typename MeshType::PointsContainer::Pointer points = mesh->GetPoints();
01028 typename MeshType::PointsContainer::Iterator i;
01029
01030
01031
01032 unsigned int maxIndex = 0;
01033 for(i = points->Begin(); i != points->End(); ++i)
01034 {
01035 if(maxIndex < i->Index())
01036 maxIndex = i->Index();
01037 }
01038
01039
01040 vpoints->SetNumberOfPoints(maxIndex+1);
01041 pointScalars->SetNumberOfTuples(maxIndex+1);
01042 cellScalars->SetNumberOfTuples(mesh->GetNumberOfCells());
01043
01044 vtkFloatingPointType vtkpoint[3];
01045 typename MeshType::PointType itkPhysicalPoint;
01046 if (geometryFrame == 0)
01047 {
01048 for(i = points->Begin(); i != points->End(); ++i)
01049 {
01050
01051 int idx = i->Index();
01052
01053 itkPhysicalPoint = i->Value();
01054 mitk::itk2vtk(itkPhysicalPoint, vtkpoint);
01055
01056 vpoints->SetPoint(idx, vtkpoint);
01057
01058 if(usePointScalarAccessor)
01059 {
01060 pointScalars->InsertTuple1( idx, ScalarAccessor::GetPointScalar( mesh->GetPointData(), i->Index(), mesh, pointDataType ) );
01061 }
01062 }
01063 }
01064 else
01065 {
01066 mitk::Point3D mitkWorldPoint;
01067 for(i = points->Begin(); i != points->End(); ++i)
01068 {
01069
01070 int idx = i->Index();
01071
01072 itkPhysicalPoint = i->Value();
01073 geometryFrame->ItkPhysicalPointToWorld(itkPhysicalPoint, mitkWorldPoint);
01074 mitk::itk2vtk(mitkWorldPoint, vtkpoint);
01075
01076 vpoints->SetPoint(idx, vtkpoint);
01077
01078 if(usePointScalarAccessor)
01079 {
01080 pointScalars->InsertTuple1( idx, ScalarAccessor::GetPointScalar( mesh->GetPointData(), i->Index(), mesh, pointDataType ) );
01081 }
01082 }
01083 }
01084
01085 vgrid->SetPoints(vpoints);
01086 if (usePointScalarAccessor)
01087 vgrid->GetPointData()->SetScalars(pointScalars);
01088
01089
01090
01091 typename MeshType::CellType::MultiVisitor::Pointer mv =
01092 MeshType::CellType::MultiVisitor::New();
01093
01094 typename SingleCellArrayLineVisitor::Pointer lv = SingleCellArrayLineVisitor::New();
01095 typename SingleCellArrayPolygonVisitor::Pointer pv = SingleCellArrayPolygonVisitor::New();
01096 typename SingleCellArrayTriangleVisitor::Pointer tv = SingleCellArrayTriangleVisitor::New();
01097 typename SingleCellArrayQuadrilateralVisitor::Pointer qv = SingleCellArrayQuadrilateralVisitor::New();
01098 typename SingleCellArrayTetrahedronVisitor::Pointer tetv = SingleCellArrayTetrahedronVisitor::New();
01099 typename SingleCellArrayHexahedronVisitor::Pointer hv = SingleCellArrayHexahedronVisitor::New();
01100
01101
01102 int numCells = mesh->GetNumberOfCells();
01103 int *types = new int[numCells];
01104
01105 vtkCellArray* cells = vtkCellArray::New();
01106 cells->Allocate(numCells);
01107
01108 lv->SetTypeArray(types);
01109 lv->SetCellArray(cells);
01110 pv->SetTypeArray(types);
01111 pv->SetCellArray(cells);
01112 tv->SetTypeArray(types);
01113
01114 tv->SetCellArray(cells);
01115 qv->SetTypeArray(types);
01116
01117 qv->SetCellArray(cells);
01118 tetv->SetTypeArray(types);
01119 tetv->SetCellArray(cells);
01120 hv->SetTypeArray(types);
01121 hv->SetCellArray(cells);
01122
01123 if (useCellScalarAccessor)
01124 {
01125 lv->SetUseCellScalarAccessor(true);
01126 lv->SetCellScalars(cellScalars);
01127 lv->SetMeshCellData(mesh->GetCellData());
01128
01129 pv->SetUseCellScalarAccessor(true);
01130 pv->SetCellScalars(cellScalars);
01131 pv->SetMeshCellData(mesh->GetCellData());
01132
01133 tv->SetUseCellScalarAccessor(true);
01134 tv->SetCellScalars(cellScalars);
01135 tv->SetMeshCellData(mesh->GetCellData());
01136
01137 qv->SetUseCellScalarAccessor(true);
01138 qv->SetCellScalars(cellScalars);
01139 qv->SetMeshCellData(mesh->GetCellData());
01140
01141 tetv->SetUseCellScalarAccessor(true);
01142 tetv->SetCellScalars(cellScalars);
01143 tetv->SetMeshCellData(mesh->GetCellData());
01144
01145 hv->SetUseCellScalarAccessor(true);
01146 hv->SetCellScalars(cellScalars);
01147 hv->SetMeshCellData(mesh->GetCellData());
01148 }
01149
01150
01151 mv->AddVisitor(lv);
01152 mv->AddVisitor(pv);
01153 mv->AddVisitor(tv);
01154 mv->AddVisitor(qv);
01155 mv->AddVisitor(tetv);
01156 mv->AddVisitor(hv);
01157
01158
01159
01160 mesh->Accept(mv);
01161
01162
01163 vgrid->SetCells(types, cells);
01164 vgrid->GetCellData()->SetScalars(cellScalars);
01165
01166
01167 cells->Delete();
01168 vpoints->Delete();
01169 delete[] types;
01170
01171 pointScalars->Delete();
01172 cellScalars->Delete();
01173
01174 return vgrid;
01175 }
01176
01177
01181 static vtkPolyData* MeshToPolyData(MeshType* mesh, bool onlyTriangles = false, bool useScalarAccessor = false, unsigned int pointDataType = 0, mitk::Geometry3D* geometryFrame=NULL, vtkPolyData* polydata = NULL)
01182 {
01186 typedef typename itk::CellInterfaceVisitorImplementation<typename MeshType::CellPixelType,
01187 typename MeshType::CellTraits,
01188 itk::LineCell< typename MeshType::CellType >,
01189 DistributeUserVisitorType> DistributeLineVisitor;
01190
01194 typedef typename itk::CellInterfaceVisitorImplementation<typename MeshType::CellPixelType,
01195 typename MeshType::CellTraits,
01196 itk::PolygonCell< typename MeshType::CellType >,
01197 DistributeUserVisitorType> DistributePolygonVisitor;
01198
01202 typedef typename itk::CellInterfaceVisitorImplementation<typename MeshType::CellPixelType,
01203 typename MeshType::CellTraits,
01204 itk::TriangleCell<itk::CellInterface<typename MeshType::CellPixelType, typename MeshType::CellTraits > >,
01205 DistributeUserVisitorType> DistributeTriangleVisitor;
01206
01207
01211 typedef typename itk::CellInterfaceVisitorImplementation<typename MeshType::CellPixelType, typename MeshType::CellTraits,
01212 itk::QuadrilateralCell< itk::CellInterface<typename MeshType::CellPixelType, typename MeshType::CellTraits > >,
01213 DistributeUserVisitorType> DistributeQuadrilateralVisitor;
01214
01215
01219 typedef typename itk::CellInterfaceVisitorImplementation<typename MeshType::CellPixelType,
01220 typename MeshType::CellTraits,
01221 itk::TriangleCell<itk::CellInterface<typename MeshType::CellPixelType, typename MeshType::CellTraits > >,
01222 ExactUserVisitorType> ExactTriangleVisitor;
01223
01224
01225
01226 int numPoints = mesh->GetNumberOfPoints();
01227 if(numPoints == 0)
01228 {
01229
01230 MITK_ERROR << "no points in Grid " << std::endl;
01231 }
01232
01233 if(polydata == NULL)
01234 polydata = vtkPolyData::New();
01235 else
01236 polydata->Initialize();
01237
01238
01239 #if ((VTK_MAJOR_VERSION > 4) || ((VTK_MAJOR_VERSION==4) && (VTK_MINOR_VERSION>=4) ))
01240 vtkPoints* vpoints = vtkPoints::New( VTK_DOUBLE );
01241 #else
01242 vtkPoints* vpoints = vtkPoints::New();
01243 #endif
01244 vtkFloatArray * scalars = vtkFloatArray::New();
01245 scalars->SetNumberOfComponents(1);
01246
01247 typename MeshType::PointsContainer::Pointer points = mesh->GetPoints();
01248 typename MeshType::PointsContainer::Iterator i;
01249
01250
01251
01252 unsigned int maxIndex = 0;
01253 for(i = points->Begin(); i != points->End(); ++i)
01254 {
01255 if(maxIndex < i->Index())
01256 maxIndex = i->Index();
01257 }
01258
01259
01260 vpoints->SetNumberOfPoints(maxIndex+1);
01261 scalars->SetNumberOfTuples(maxIndex+1);
01262
01263
01264
01265
01266 vtkFloatingPointType vtkpoint[3];
01267 typename MeshType::PointType itkPhysicalPoint;
01268 if(geometryFrame==NULL)
01269 {
01270 for(i = points->Begin(); i != points->End(); ++i)
01271 {
01272
01273 int idx = i->Index();
01274
01275 itkPhysicalPoint = i->Value();
01276 mitk::itk2vtk(itkPhysicalPoint, vtkpoint);
01277
01278
01279
01280
01281 vpoints->SetPoint(idx, vtkpoint);
01282
01283 if(useScalarAccessor)
01284 {
01285 scalars->InsertTuple1( idx, ScalarAccessor::GetPointScalar( mesh->GetPointData(), i->Index(), mesh, pointDataType ) );
01286 }
01287 }
01288 }
01289 else
01290 {
01291 mitk::Point3D mitkWorldPoint;
01292 for(i = points->Begin(); i != points->End(); ++i)
01293 {
01294
01295 int idx = i->Index();
01296
01297 itkPhysicalPoint = i->Value();
01298 geometryFrame->ItkPhysicalPointToWorld(itkPhysicalPoint, mitkWorldPoint);
01299 mitk::itk2vtk(mitkWorldPoint, vtkpoint);
01300
01301
01302
01303
01304 vpoints->SetPoint(idx, vtkpoint);
01305
01306 if(useScalarAccessor)
01307 {
01308 scalars->InsertTuple1( idx, ScalarAccessor::GetPointScalar( mesh->GetPointData(), i->Index(), mesh, pointDataType ) );
01309 }
01310 }
01311 }
01312
01313
01314 polydata->SetPoints(vpoints);
01315 if (useScalarAccessor)
01316 polydata->GetPointData()->SetScalars(scalars);
01317 polydata->GetPointData()->CopyAllOn();
01318
01319
01320
01321
01322 typedef typename MeshType::CellType::MultiVisitor MeshMV;
01323 typename MeshMV::Pointer mv = MeshMV::New();
01324
01325 int numCells = mesh->GetNumberOfCells();
01326
01327 if (onlyTriangles)
01328 {
01329
01330 vtkCellArray* trianglecells = vtkCellArray::New();
01331 trianglecells->Allocate(numCells);
01332
01333
01334 typename ExactTriangleVisitor::Pointer tv = ExactTriangleVisitor::New();
01335 tv->SetCellArrays(NULL, trianglecells, NULL, NULL);
01336 mv->AddVisitor(tv);
01337
01338
01339
01340 mesh->Accept(mv);
01341
01342
01343 if(trianglecells->GetNumberOfCells()>0)
01344 polydata->SetStrips(trianglecells);
01345
01346
01347 trianglecells->Delete();
01348 }
01349 else
01350 {
01351
01352 vtkCellArray* linecells = vtkCellArray::New();
01353 vtkCellArray* trianglecells = vtkCellArray::New();
01354 vtkCellArray* polygoncells = vtkCellArray::New();
01355 linecells->Allocate(numCells);
01356 trianglecells->Allocate(numCells);
01357 polygoncells->Allocate(numCells);
01358
01359
01360 typename DistributeLineVisitor::Pointer lv = DistributeLineVisitor::New();
01361 typename DistributePolygonVisitor::Pointer pv = DistributePolygonVisitor::New();
01362 typename DistributeTriangleVisitor::Pointer tv = DistributeTriangleVisitor::New();
01363 typename DistributeQuadrilateralVisitor::Pointer qv = DistributeQuadrilateralVisitor::New();
01364
01365 lv->SetCellArrays(linecells, trianglecells, polygoncells, polygoncells);
01366 pv->SetCellArrays(linecells, trianglecells, polygoncells, polygoncells);
01367 tv->SetCellArrays(linecells, trianglecells, polygoncells, polygoncells);
01368 qv->SetCellArrays(linecells, trianglecells, polygoncells, polygoncells);
01369
01370
01371 mv->AddVisitor(tv);
01372 mv->AddVisitor(lv);
01373 mv->AddVisitor(pv);
01374 mv->AddVisitor(qv);
01375
01376
01377
01378 mesh->Accept(mv);
01379
01380
01381 if(linecells->GetNumberOfCells()>0)
01382 polydata->SetLines(linecells);
01383 if(trianglecells->GetNumberOfCells()>0)
01384 polydata->SetStrips(trianglecells);
01385 if(polygoncells->GetNumberOfCells()>0)
01386 polydata->SetPolys(polygoncells);
01387
01388
01389 linecells->Delete();
01390 trianglecells->Delete();
01391 polygoncells->Delete();
01392 }
01393 vpoints->Delete();
01394 scalars->Delete();
01395
01396
01397 return polydata;
01398 }
01399
01400 static typename MeshType::Pointer CreateRegularSphereMesh(typename MeshType::PointType center, typename MeshType::PointType::VectorType scale, int resolution)
01401 {
01402 typedef itk::RegularSphereMeshSource<MeshType> SphereSourceType;
01403 typename SphereSourceType::Pointer mySphereSource = SphereSourceType::New();
01404
01405 mySphereSource->SetCenter(center);
01406 mySphereSource->SetScale(scale);
01407 mySphereSource->SetResolution( resolution );
01408 mySphereSource->Update();
01409
01410 typename MeshType::Pointer resultMesh = mySphereSource->GetOutput();
01411 resultMesh->Register();
01412 return resultMesh;
01413 }
01414
01415 static typename MeshType::Pointer CreateSphereMesh(typename MeshType::PointType center, typename MeshType::PointType scale, int* resolution)
01416 {
01417 typedef typename itk::SphereMeshSource<MeshType> SphereSource;
01418
01419 typename SphereSource::Pointer mySphereSource = SphereSource::New();
01420
01421 mySphereSource->SetCenter(center);
01422 mySphereSource->SetScale(scale);
01423 mySphereSource->SetResolutionX(resolution[0]);
01424 mySphereSource->SetResolutionY(resolution[1]);
01425 mySphereSource->SetSquareness1(1);
01426 mySphereSource->SetSquareness2(1);
01427 mySphereSource->Update();
01428 mySphereSource->GetOutput();
01429
01430 typename MeshType::Pointer resultMesh = mySphereSource->GetOutput();
01431 resultMesh->Register();
01432
01433 return resultMesh;
01434 }
01435
01436
01437
01438
01439
01440
01441
01442
01443
01444
01445
01446
01447
01448
01449
01450
01451
01452
01453
01454
01455
01456
01457
01458
01459
01460
01461
01462
01463
01464
01465
01466
01467
01468
01469
01470
01471
01472
01473
01474
01475
01476
01477
01479
01480
01481
01482 static typename MeshType::Pointer CreateRegularSphereMesh2(typename MeshType::PointType center, typename MeshType::PointType scale, int resolution)
01483 {
01484 typedef typename itk::AutomaticTopologyMeshSource<MeshType> MeshSourceType;
01485 typename MeshSourceType::Pointer mySphereSource = MeshSourceType::New();
01486
01487 typename MeshType::PointType pnt0, pnt1, pnt2, pnt3, pnt4, pnt5, pnt6, pnt7, pnt8, pnt9, pnt10, pnt11;
01488 double c1= 0.5 * (1.0 + sqrt(5.0));
01489 double c2= 1.0;
01490 double len = sqrt( c1*c1 + c2*c2 );
01491 c1 /= len; c2 /= len;
01492
01493 pnt0[0] = center[0] - c1*scale[0]; pnt0[1] = center[1]; pnt0[2] = center[2] + c2*scale[2];
01494 pnt1[0] = center[0]; pnt1[1] = center[1] + c2*scale[1]; pnt1[2] = center[2] - c1*scale[2];
01495 pnt2[0] = center[0]; pnt2[1] = center[1] + c2*scale[1]; pnt2[2] = center[2] + c1*scale[2];
01496 pnt3[0] = center[0] + c1*scale[0]; pnt3[1] = center[1]; pnt3[2] = center[2] - c2*scale[2];
01497 pnt4[0] = center[0] - c2*scale[0]; pnt4[1] = center[1] - c1*scale[1]; pnt4[2] = center[2];
01498 pnt5[0] = center[0] - c2*scale[0]; pnt5[1] = center[1] + c1*scale[1]; pnt5[2] = center[2];
01499 pnt6[0] = center[0]; pnt6[1] = center[1] - c2*scale[1]; pnt6[2] = center[2] + c1*scale[2];
01500 pnt7[0] = center[0] + c2*scale[0]; pnt7[1] = center[1] + c1*scale[1]; pnt7[2] = center[2];
01501 pnt8[0] = center[0]; pnt8[1] = center[1] - c2*scale[1]; pnt8[2] = center[2] - c1*scale[2];
01502 pnt9[0] = center[0] + c1*scale[0]; pnt9[1] = center[1]; pnt9[2] = center[2] + c2*scale[2];
01503 pnt10[0]= center[0] + c2*scale[0]; pnt10[1]= center[1] - c1*scale[1]; pnt10[2]= center[2];
01504 pnt11[0]= center[0] - c1*scale[0]; pnt11[1]= center[1]; pnt11[2]= center[2] - c2*scale[2];
01505
01506 addTriangle( mySphereSource, scale, pnt9, pnt2, pnt6, resolution );
01507 addTriangle( mySphereSource, scale, pnt1, pnt11, pnt5, resolution );
01508 addTriangle( mySphereSource, scale, pnt11, pnt1, pnt8, resolution );
01509 addTriangle( mySphereSource, scale, pnt0, pnt11, pnt4, resolution );
01510 addTriangle( mySphereSource, scale, pnt3, pnt1, pnt7, resolution );
01511 addTriangle( mySphereSource, scale, pnt3, pnt8, pnt1, resolution );
01512 addTriangle( mySphereSource, scale, pnt9, pnt3, pnt7, resolution );
01513 addTriangle( mySphereSource, scale, pnt0, pnt6, pnt2, resolution );
01514 addTriangle( mySphereSource, scale, pnt4, pnt10, pnt6, resolution );
01515 addTriangle( mySphereSource, scale, pnt1, pnt5, pnt7, resolution );
01516 addTriangle( mySphereSource, scale, pnt7, pnt5, pnt2, resolution );
01517 addTriangle( mySphereSource, scale, pnt8, pnt3, pnt10, resolution );
01518 addTriangle( mySphereSource, scale, pnt4, pnt11, pnt8, resolution );
01519 addTriangle( mySphereSource, scale, pnt9, pnt7, pnt2, resolution );
01520 addTriangle( mySphereSource, scale, pnt10, pnt9, pnt6, resolution );
01521 addTriangle( mySphereSource, scale, pnt0, pnt5, pnt11, resolution );
01522 addTriangle( mySphereSource, scale, pnt0, pnt2, pnt5, resolution );
01523 addTriangle( mySphereSource, scale, pnt8, pnt10, pnt4, resolution );
01524 addTriangle( mySphereSource, scale, pnt3, pnt9, pnt10, resolution );
01525 addTriangle( mySphereSource, scale, pnt6, pnt0, pnt4, resolution );
01526
01527 return mySphereSource->GetOutput();
01528 }
01529
01530
01531
01532 private:
01533
01534 static void addTriangle( typename itk::AutomaticTopologyMeshSource<MeshType>::Pointer meshSource, typename MeshType::PointType scale,
01535 typename MeshType::PointType pnt0, typename MeshType::PointType pnt1, typename MeshType::PointType pnt2, int resolution )
01536 {
01537 if (resolution==0) {
01538
01539 meshSource->AddTriangle( meshSource->AddPoint( pnt0 ),
01540 meshSource->AddPoint( pnt1 ),
01541 meshSource->AddPoint( pnt2 ) );
01542 }
01543 else {
01544 vnl_vector_fixed<typename MeshType::CoordRepType, 3> v1, v2, res, pv;
01545 v1 = (pnt1-pnt0).Get_vnl_vector();
01546 v2 = (pnt2-pnt0).Get_vnl_vector();
01547 res = vnl_cross_3d( v1, v2 );
01548 pv = pnt0.GetVectorFromOrigin().Get_vnl_vector();
01549
01550
01551
01552 typename MeshType::PointType pnt01, pnt12, pnt20;
01553 for (int d=0; d<3; d++) {
01554 pnt01[d] = (pnt0[d] + pnt1[d]) / 2.0;
01555 pnt12[d] = (pnt1[d] + pnt2[d]) / 2.0;
01556 pnt20[d] = (pnt2[d] + pnt0[d]) / 2.0;
01557 }
01558
01559 double lenPnt01=0; for (int d=0; d<3; d++) lenPnt01 += pnt01[d]*pnt01[d]; lenPnt01 = sqrt( lenPnt01 );
01560 double lenPnt12=0; for (int d=0; d<3; d++) lenPnt12 += pnt12[d]*pnt12[d]; lenPnt12 = sqrt( lenPnt12 );
01561 double lenPnt20=0; for (int d=0; d<3; d++) lenPnt20 += pnt20[d]*pnt20[d]; lenPnt20 = sqrt( lenPnt20 );
01562 for (int d=0; d<3; d++) {
01563 pnt01[d] *= scale[d]/lenPnt01;
01564 pnt12[d] *= scale[d]/lenPnt12;
01565 pnt20[d] *= scale[d]/lenPnt20;
01566 }
01567 addTriangle( meshSource, scale, pnt0, pnt01, pnt20, resolution-1 );
01568 addTriangle( meshSource, scale, pnt01, pnt1, pnt12, resolution-1 );
01569 addTriangle( meshSource, scale, pnt20, pnt12, pnt2, resolution-1 );
01570 addTriangle( meshSource, scale, pnt01, pnt12, pnt20, resolution-1 );
01571 }
01572 }
01573
01574 };
01575
01576 #endif // MITKMESHUTIL_H_INCLUDED
01577