13 #ifndef MITKMESHUTIL_H_INCLUDED 14 #define MITKMESHUTIL_H_INCLUDED 16 #if (_MSC_VER == 1200) 17 #error MeshUtils currently not supported for MS Visual C++ 6.0. Sorry. 21 #include <itkCellInterface.h> 22 #include <itkLineCell.h> 23 #include <itkPolygonCell.h> 24 #include <itkQuadrilateralCell.h> 25 #include <itkTriangleCell.h> 27 #include <itkSphereMeshSource.h> 31 #include <itkAutomaticTopologyMeshSource.h> 32 #include <itkRegularSphereMeshSource.h> 33 #include <vnl/vnl_cross.h> 36 #include <vtkCellArray.h> 37 #include <vtkCellData.h> 38 #include <vtkFloatArray.h> 39 #include <vtkPointData.h> 40 #include <vtkPoints.h> 41 #include <vtkPolyData.h> 42 #include <vtkProperty.h> 43 #include <vtkUnstructuredGrid.h> 48 template <
typename MeshType>
52 static inline double GetPointScalar(
typename MeshType::PointDataContainer * ,
53 typename MeshType::PointIdentifier ,
60 static inline double GetCellScalar(
typename MeshType::CellDataContainer * ,
61 typename MeshType::CellIdentifier ,
69 template <
typename MeshType>
73 static inline double GetPointScalar(
typename MeshType::PointDataContainer *pointData,
74 typename MeshType::PointIdentifier idx,
78 return (
double)pointData->GetElement(idx);
81 static inline double GetCellScalar(
typename MeshType::CellDataContainer *cellData,
82 typename MeshType::CellIdentifier idx,
86 return (
double)cellData->GetElement(idx);
90 template <
typename MeshType>
94 static inline double GetPointScalar(
typename MeshType::PointDataContainer * ,
95 typename MeshType::PointIdentifier idx,
99 typename MeshType::PixelType dis = 0;
100 mesh->GetPointData(idx, &dis);
105 template <
typename MeshType>
110 typename MeshType::PointIdentifier idx,
112 unsigned int type = 0)
114 typename MeshType::GeometryMapPointer geometryData = mesh->GetGeometryData();
118 double val = mesh->GetMeanCurvature(idx);
119 mesh->SetPointData(idx, val);
124 double val = geometryData->GetElement(idx)->meanTension;
125 mesh->SetPointData(idx, val);
130 double val = geometryData->GetElement(idx)->externalForce.GetNorm();
131 mesh->SetPointData(idx, val);
135 return geometryData->GetElement(idx)->internalForce.GetNorm();
137 return geometryData->GetElement(idx)->externalForce.GetNorm() * mesh->GetDistance(idx);
140 typename MeshType::PixelType dis = 0;
141 mesh->GetPointData(idx, &dis);
146 return (
double)((geometryData->GetElement(idx))->allowSplitting);
160 template <
typename MeshType,
class ScalarAccessor = NullScalarAccessor<MeshType>>
186 template <
class InsertImplementation>
187 class SwitchByCellType :
public InsertImplementation
190 typedef typename itk::CellInterface<typename MeshType::CellPixelType, typename MeshType::CellTraits>
192 typedef itk::LineCell<CellInterfaceType> floatLineCell;
193 typedef itk::TriangleCell<CellInterfaceType> floatTriangleCell;
194 typedef itk::PolygonCell<CellInterfaceType> floatPolygonCell;
195 typedef itk::QuadrilateralCell<CellInterfaceType> floatQuadrilateralCell;
196 typedef itk::TetrahedronCell<CellInterfaceType> floatTetrahedronCell;
197 typedef itk::HexahedronCell<CellInterfaceType> floatHexahedronCell;
198 typedef typename CellInterfaceType::PointIdConstIterator PointIdIterator;
204 void Visit(
unsigned long cellId, floatLineCell *t)
208 unsigned long num = t->GetNumberOfVertices();
209 vtkIdType vtkCellId = -1;
212 for (PointIdIterator it = t->PointIdsBegin(); it != t->PointIdsEnd(); it++)
214 vtkCellId = this->InsertLine((vtkIdType *)pts);
217 if (this->m_UseCellScalarAccessor && vtkCellId >= 0)
219 this->m_CellScalars->InsertTuple1(vtkCellId, ScalarAccessor::GetCellScalar(this->m_CellData, cellId));
226 void Visit(
unsigned long cellId, floatPolygonCell *t)
230 unsigned long num = t->GetNumberOfVertices();
231 vtkIdType vtkCellId = -1;
234 MITK_ERROR <<
"Problem in mitkMeshUtil: Polygon with more than maximum number of vertices encountered." 239 for (PointIdIterator it = t->PointIdsBegin(); it != t->PointIdsEnd(); it++)
241 vtkCellId = this->InsertPolygon(num, (vtkIdType *)pts);
245 for (PointIdIterator it = t->PointIdsBegin(); it != t->PointIdsEnd(); it++)
247 vtkCellId = this->InsertTriangle((vtkIdType *)pts);
251 for (PointIdIterator it = t->PointIdsBegin(); it != t->PointIdsEnd(); it++)
253 vtkCellId = this->InsertLine((vtkIdType *)pts);
256 if (this->m_UseCellScalarAccessor && vtkCellId >= 0)
258 this->m_CellScalars->InsertTuple1(vtkCellId, ScalarAccessor::GetCellScalar(this->m_CellData, cellId));
265 void Visit(
unsigned long cellId, floatTriangleCell *t)
269 unsigned long num = t->GetNumberOfVertices();
270 vtkIdType vtkCellId = -1;
273 for (PointIdIterator it = t->PointIdsBegin(); it != t->PointIdsEnd(); it++)
275 vtkCellId = this->InsertTriangle((vtkIdType *)pts);
279 for (PointIdIterator it = t->PointIdsBegin(); it != t->PointIdsEnd(); it++)
281 vtkCellId = this->InsertLine((vtkIdType *)pts);
284 if (this->m_UseCellScalarAccessor && vtkCellId >= 0)
286 this->m_CellScalars->InsertTuple1(vtkCellId, ScalarAccessor::GetCellScalar(this->m_CellData, cellId));
293 void Visit(
unsigned long cellId, floatQuadrilateralCell *t)
297 unsigned long num = t->GetNumberOfVertices();
298 vtkIdType vtkCellId = -1;
301 for (PointIdIterator it = t->PointIdsBegin(); it != t->PointIdsEnd(); it++)
312 vtkCellId = this->InsertQuad((vtkIdType *)pts);
316 for (PointIdIterator it = t->PointIdsBegin(); it != t->PointIdsEnd(); it++)
318 vtkCellId = this->InsertTriangle((vtkIdType *)pts);
322 for (PointIdIterator it = t->PointIdsBegin(); it != t->PointIdsEnd(); it++)
324 vtkCellId = this->InsertLine((vtkIdType *)pts);
327 if (this->m_UseCellScalarAccessor && vtkCellId >= 0)
329 this->m_CellScalars->InsertTuple1(vtkCellId, ScalarAccessor::GetCellScalar(this->m_CellData, cellId));
336 void Visit(
unsigned long cellId, floatTetrahedronCell *t)
340 unsigned long num = t->GetNumberOfVertices();
341 vtkIdType vtkCellId = -1;
344 for (PointIdIterator it = t->PointIdsBegin(); it != t->PointIdsEnd(); it++)
346 vtkCellId = this->InsertTetra((vtkIdType *)pts);
350 for (PointIdIterator it = t->PointIdsBegin(); it != t->PointIdsEnd(); it++)
352 vtkCellId = this->InsertTriangle((vtkIdType *)pts);
356 for (PointIdIterator it = t->PointIdsBegin(); it != t->PointIdsEnd(); it++)
358 vtkCellId = this->InsertLine((vtkIdType *)pts);
361 if (this->m_UseCellScalarAccessor && vtkCellId >= 0)
363 this->m_CellScalars->InsertTuple1(vtkCellId, ScalarAccessor::GetCellScalar(this->m_CellData, cellId));
370 void Visit(
unsigned long cellId, floatHexahedronCell *t)
374 unsigned long num = t->GetNumberOfVertices();
375 vtkIdType vtkCellId = -1;
378 for (PointIdIterator it = t->PointIdsBegin(); it != t->PointIdsEnd(); it++)
381 pts[i++] = *(it + 1);
383 pts[i++] = *(it - 1);
385 pts[i++] = *(it + 1);
387 pts[i++] = *(it - 1);
391 vtkCellId = this->InsertHexahedron((vtkIdType *)pts);
395 for (PointIdIterator it = t->PointIdsBegin(); it != t->PointIdsEnd(); it++)
397 vtkCellId = this->InsertQuad((vtkIdType *)pts);
401 for (PointIdIterator it = t->PointIdsBegin(); it != t->PointIdsEnd(); it++)
403 vtkCellId = this->InsertTriangle((vtkIdType *)pts);
407 for (PointIdIterator it = t->PointIdsBegin(); it != t->PointIdsEnd(); it++)
409 vtkCellId = this->InsertLine((vtkIdType *)pts);
412 if (this->m_UseCellScalarAccessor && vtkCellId >= 0)
414 this->m_CellScalars->InsertTuple1(vtkCellId, ScalarAccessor::GetCellScalar(this->m_CellData, cellId));
431 template <
class InsertImplementation>
432 class ExactSwitchByCellType :
public InsertImplementation
435 typedef typename itk::CellInterface<typename MeshType::CellPixelType, typename MeshType::CellTraits>
437 typedef itk::LineCell<CellInterfaceType> floatLineCell;
438 typedef itk::TriangleCell<CellInterfaceType> floatTriangleCell;
439 typedef itk::PolygonCell<CellInterfaceType> floatPolygonCell;
440 typedef itk::QuadrilateralCell<CellInterfaceType> floatQuadrilateralCell;
441 typedef itk::TetrahedronCell<CellInterfaceType> floatTetrahedronCell;
442 typedef itk::HexahedronCell<CellInterfaceType> floatHexahedronCell;
443 typedef typename CellInterfaceType::PointIdConstIterator PointIdIterator;
449 void Visit(
unsigned long, floatLineCell *t)
451 unsigned long num = t->GetNumberOfVertices();
457 for (PointIdIterator it = t->PointIdsBegin(); it != t->PointIdsEnd(); it++)
459 this->InsertLine(pts);
466 void Visit(
unsigned long, floatPolygonCell *t)
469 unsigned long num = t->GetNumberOfVertices();
472 MITK_ERROR <<
"Problem in mitkMeshUtil: Polygon with more than maximum number of vertices encountered." 479 for (PointIdIterator it = t->PointIdsBegin(); it != t->PointIdsEnd(); it++)
481 this->InsertPolygon(num, pts);
488 void Visit(
unsigned long, floatTriangleCell *t)
490 unsigned long num = t->GetNumberOfVertices();
496 for (PointIdIterator it = t->PointIdsBegin(); it != t->PointIdsEnd(); it++)
498 this->InsertTriangle(pts);
505 void Visit(
unsigned long, floatQuadrilateralCell *t)
507 unsigned long num = t->GetNumberOfVertices();
513 for (PointIdIterator it = t->PointIdsBegin(); it != t->PointIdsEnd(); it++)
516 vtkIdType tmpId = pts[2];
519 this->InsertQuad(pts);
526 void Visit(
unsigned long, floatTetrahedronCell *t)
528 unsigned long num = t->GetNumberOfVertices();
535 for (PointIdIterator it = t->PointIdsBegin(); it != t->PointIdsEnd(); it++)
537 this->InsertTetra(pts);
544 void Visit(
unsigned long, floatHexahedronCell *t)
546 unsigned long num = t->GetNumberOfVertices();
552 for (PointIdIterator it = t->PointIdsBegin(); it != t->PointIdsEnd(); it++)
556 for (
unsigned int i = 0; i < 8; i++)
562 this->InsertHexahedron(pts);
573 class SingleCellArrayInsertImplementation
575 vtkCellArray *m_Cells;
580 bool m_UseCellScalarAccessor;
581 vtkFloatArray *m_CellScalars;
582 typename MeshType::CellDataContainer::Pointer m_CellData;
585 SingleCellArrayInsertImplementation() : m_UseCellScalarAccessor(
false) {}
588 void SetCellArray(vtkCellArray *cells) { m_Cells = cells; }
592 void SetTypeArray(
int *i) { m_TypeArray = i; }
593 void SetUseCellScalarAccessor(
bool flag) { m_UseCellScalarAccessor = flag; }
594 void SetCellScalars(vtkFloatArray *scalars) { m_CellScalars = scalars; }
595 vtkFloatArray *GetCellScalars() {
return m_CellScalars; }
596 void SetMeshCellData(
typename MeshType::CellDataContainer *data) { m_CellData = data; }
597 vtkIdType InsertLine(vtkIdType *pts)
599 vtkIdType cellId = m_Cells->InsertNextCell(2, pts);
600 m_TypeArray[cellId] = VTK_LINE;
604 vtkIdType InsertTriangle(vtkIdType *pts)
606 vtkIdType cellId = m_Cells->InsertNextCell(3, pts);
607 m_TypeArray[cellId] = VTK_TRIANGLE;
611 vtkIdType InsertPolygon(vtkIdType npts, vtkIdType *pts)
613 vtkIdType cellId = m_Cells->InsertNextCell(npts, pts);
614 m_TypeArray[cellId] = VTK_POLYGON;
618 vtkIdType InsertQuad(vtkIdType *pts)
620 vtkIdType cellId = m_Cells->InsertNextCell(4, pts);
621 m_TypeArray[cellId] = VTK_QUAD;
625 vtkIdType InsertTetra(vtkIdType *pts)
627 vtkIdType cellId = m_Cells->InsertNextCell(4, pts);
628 m_TypeArray[cellId] = VTK_TETRA;
632 vtkIdType InsertHexahedron(vtkIdType *pts)
634 vtkIdType cellId = m_Cells->InsertNextCell(8, pts);
635 m_TypeArray[cellId] = VTK_HEXAHEDRON;
645 class DistributeInsertImplementation
647 vtkCellArray *m_LineCells;
648 vtkCellArray *m_TriangleCells;
649 vtkCellArray *m_PolygonCells;
650 vtkCellArray *m_QuadCells;
653 bool m_UseCellScalarAccessor;
654 vtkFloatArray *m_CellScalars;
655 typename MeshType::CellDataContainer::Pointer m_CellData;
658 DistributeInsertImplementation() : m_UseCellScalarAccessor(
false) {}
661 void SetCellArrays(vtkCellArray *lines, vtkCellArray *triangles, vtkCellArray *polygons, vtkCellArray *quads)
664 m_TriangleCells = triangles;
665 m_PolygonCells = polygons;
669 vtkIdType InsertLine(vtkIdType *pts) {
return m_LineCells->InsertNextCell(2, pts); }
670 vtkIdType InsertTriangle(vtkIdType *pts) {
return m_TriangleCells->InsertNextCell(3, pts); }
671 vtkIdType InsertPolygon(vtkIdType npts, vtkIdType *pts) {
return m_PolygonCells->InsertNextCell(npts, pts); }
672 vtkIdType InsertQuad(vtkIdType *pts) {
return m_QuadCells->InsertNextCell(4, pts); }
673 vtkIdType InsertTetra(vtkIdType * ) {
return -1; }
674 vtkIdType InsertHexahedron(vtkIdType * ) {
return -1; }
682 typedef SwitchByCellType<SingleCellArrayInsertImplementation> SingleCellArrayUserVisitorType;
683 typedef SwitchByCellType<DistributeInsertImplementation> DistributeUserVisitorType;
684 typedef ExactSwitchByCellType<DistributeInsertImplementation> ExactUserVisitorType;
687 typedef itk::MatrixOffsetTransformBase<typename MeshType::CoordRepType, 3, 3>
ITKTransformType;
696 typename MITKTransformType::MatrixType mitkM = mitkTransform->GetMatrix();
697 typename ITKTransformType::MatrixType itkM;
699 typename MITKTransformType::OffsetType mitkO = mitkTransform->GetOffset();
700 typename ITKTransformType::OffsetType itkO;
702 for (
short i = 0; i < 3; ++i)
704 for (
short j = 0; j < 3; ++j)
706 itkM[i][j] = (double)mitkM[i][j];
708 itkO[i] = (double)mitkO[i];
711 itkTransform->SetMatrix(itkM);
712 itkTransform->SetOffset(itkO);
723 typename MeshType::Pointer output = MeshType::New();
724 output->SetCellsAllocationMethod(MeshType::CellsAllocatedDynamicallyCellByCell);
726 typedef typename MeshType::CellDataContainer MeshCellDataContainerType;
728 output->SetCellData(MeshCellDataContainerType::New());
731 vtkPoints *vtkpoints = poly->GetPoints();
732 const unsigned int numPoints = poly->GetNumberOfPoints();
743 typename MeshType::PointType itkPhysicalPoint;
744 if (geometryFrame ==
nullptr)
746 if (polyDataGeometryFrame ==
nullptr)
748 for (
unsigned int i = 0; i < numPoints; ++i)
750 vtkpoints->GetPoint(i, vtkpoint);
754 output->SetPoint(i, itkPhysicalPoint);
759 for (
unsigned int i = 0; i < numPoints; ++i)
761 vtkpoints->GetPoint(i, vtkpoint);
766 polyDataGeometryFrame->IndexToWorld(mitkWorldPoint, mitkWorldPoint);
768 output->SetPoint(i, itkPhysicalPoint);
775 if (polyDataGeometryFrame ==
nullptr)
777 for (
unsigned int i = 0; i < numPoints; ++i)
779 vtkpoints->GetPoint(i, vtkpoint);
783 geometryFrame->WorldToItkPhysicalPoint(mitkWorldPoint, itkPhysicalPoint);
784 output->SetPoint(i, itkPhysicalPoint);
789 for (
unsigned int i = 0; i < numPoints; ++i)
791 vtkpoints->GetPoint(i, vtkpoint);
795 polyDataGeometryFrame->IndexToWorld(mitkWorldPoint, mitkWorldPoint);
796 geometryFrame->WorldToItkPhysicalPoint(mitkWorldPoint, itkPhysicalPoint);
797 output->SetPoint(i, itkPhysicalPoint);
802 vtkCellArray *vtkcells = poly->GetPolys();
807 int numcells = vtkcells->GetNumberOfCells();
808 int *vtkCellTypes =
new int[numcells];
811 int cellIdOfs = poly->GetNumberOfVerts() + poly->GetNumberOfLines();
812 for (; cellId < numcells; cellId++)
814 vtkCellTypes[cellId] = poly->GetCellType(cellId + cellIdOfs);
822 typedef typename MeshType::MeshTraits OMeshTraits;
823 typedef typename OMeshTraits::PixelType OPixelType;
824 typedef typename MeshType::CellTraits CellTraits;
825 typedef typename itk::CellInterface<OPixelType, CellTraits> CellInterfaceType;
826 typedef typename itk::TriangleCell<CellInterfaceType> TriCellType;
827 typedef typename TriCellType::CellAutoPointer TriCellPointer;
829 TriCellPointer newCell;
830 output->GetCells()->Reserve(poly->GetNumberOfPolys() + poly->GetNumberOfStrips());
831 output->GetCellData()->Reserve(poly->GetNumberOfPolys() + poly->GetNumberOfStrips());
833 for (vtkcells->InitTraversal(); vtkcells->GetNextCell(npts, pts); cellId++)
835 switch (vtkCellTypes[cellId])
841 itk::IdentifierType pointIds[3];
842 pointIds[0] = (
unsigned long)pts[0];
843 pointIds[1] = (
unsigned long)pts[1];
844 pointIds[2] = (
unsigned long)pts[2];
846 newCell.TakeOwnership(
new TriCellType);
847 newCell->SetPointIds(pointIds);
848 output->SetCell(cellId, newCell);
849 output->SetCellData(cellId, (
typename MeshType::PixelType)3);
857 itk::IdentifierType pointIds[3];
859 pointIds[0] = (
unsigned long)pts[0];
860 pointIds[1] = (
unsigned long)pts[1];
861 pointIds[2] = (
unsigned long)pts[2];
862 newCell.TakeOwnership(
new TriCellType);
863 newCell->SetPointIds(pointIds);
864 output->SetCell(cellId, newCell);
865 output->SetCellData(cellId, (
typename MeshType::PixelType)3);
868 pointIds[0] = (
unsigned long)pts[2];
869 pointIds[1] = (
unsigned long)pts[3];
870 pointIds[2] = (
unsigned long)pts[0];
871 newCell.TakeOwnership(
new TriCellType);
872 newCell->SetPointIds(pointIds);
873 output->SetCell(cellId, newCell);
874 output->SetCellData(cellId, (
typename MeshType::PixelType)3);
882 MITK_ERROR <<
"Only empty triangle cell supported by now..." << std::endl;
885 itk::IdentifierType pointIds[3];
886 pointIds[0] = (
unsigned long)pts[0];
887 pointIds[1] = (
unsigned long)pts[1];
888 pointIds[2] = (
unsigned long)pts[2];
890 newCell.TakeOwnership(
new TriCellType);
891 newCell->SetPointIds(pointIds);
892 output->SetCell(cellId, newCell);
893 output->SetCellData(cellId, (
typename MeshType::PixelType)3);
906 itk::IdentifierType pointIds[3];
907 for (
unsigned int idx = 0; idx <= 1; idx++)
909 pointIds[0] = (
unsigned long)pts[idx];
910 pointIds[1] = (
unsigned long)pts[idx + 1];
911 pointIds[2] = (
unsigned long)pts[idx + 2];
912 newCell.TakeOwnership(
new TriCellType);
913 newCell->SetPointIds(pointIds);
914 output->SetCell(cellId + idx, newCell);
915 output->SetCellData(cellId + idx, (
typename MeshType::PixelType)3);
926 case VTK_PARAMETRIC_CURVE:
927 case VTK_PARAMETRIC_SURFACE:
929 MITK_WARN <<
"Warning, unhandled cell type " << vtkCellTypes[cellId] << std::endl;
933 if (poly->GetNumberOfStrips() != 0)
935 vtkcells = poly->GetStrips();
936 numcells = vtkcells->GetNumberOfCells();
937 vtkCellTypes =
new int[numcells];
940 int stripIdOfs = poly->GetNumberOfVerts() + poly->GetNumberOfLines() + poly->GetNumberOfPolys();
941 for (; stripId < numcells; stripId++)
943 vtkCellTypes[stripId] = poly->GetCellType(stripId + stripIdOfs);
947 vtkcells->InitTraversal();
948 while (vtkcells->GetNextCell(npts, pts))
950 if (vtkCellTypes[stripId] != VTK_TRIANGLE_STRIP)
952 MITK_ERROR <<
"Only triangle strips supported!" << std::endl;
957 unsigned int numberOfTrianglesInStrip = npts - 2;
958 itk::IdentifierType pointIds[3];
959 pointIds[0] = (
unsigned long)pts[0];
960 pointIds[1] = (
unsigned long)pts[1];
961 pointIds[2] = (
unsigned long)pts[2];
963 for (
unsigned int t = 0; t < numberOfTrianglesInStrip; t++)
965 newCell.TakeOwnership(
new TriCellType);
966 newCell->SetPointIds(pointIds);
967 output->SetCell(cellId, newCell);
968 output->SetCellData(cellId, (
typename MeshType::PixelType)3);
970 pointIds[0] = pointIds[1];
971 pointIds[1] = pointIds[2];
972 pointIds[2] = pts[t + 3];
977 output->BuildCellLinks();
978 delete[] vtkCellTypes;
987 if (surface ==
nullptr)
996 bool usePointScalarAccessor =
false,
997 bool useCellScalarAccessor =
false,
998 unsigned int pointDataType = 0,
1004 typedef typename itk::CellInterfaceVisitorImplementation<
typename MeshType::CellPixelType,
1005 typename MeshType::CellTraits,
1006 itk::LineCell<typename MeshType::CellType>,
1007 SingleCellArrayUserVisitorType>
1008 SingleCellArrayLineVisitor;
1013 typedef typename itk::CellInterfaceVisitorImplementation<
typename MeshType::CellPixelType,
1014 typename MeshType::CellTraits,
1015 itk::PolygonCell<typename MeshType::CellType>,
1016 SingleCellArrayUserVisitorType>
1017 SingleCellArrayPolygonVisitor;
1022 typedef typename itk::CellInterfaceVisitorImplementation<
1023 typename MeshType::CellPixelType,
1024 typename MeshType::CellTraits,
1025 itk::TriangleCell<itk::CellInterface<typename MeshType::CellPixelType, typename MeshType::CellTraits>>,
1026 SingleCellArrayUserVisitorType>
1027 SingleCellArrayTriangleVisitor;
1032 typedef typename itk::CellInterfaceVisitorImplementation<
1033 typename MeshType::CellPixelType,
1034 typename MeshType::CellTraits,
1035 itk::QuadrilateralCell<itk::CellInterface<typename MeshType::CellPixelType, typename MeshType::CellTraits>>,
1036 SingleCellArrayUserVisitorType>
1037 SingleCellArrayQuadrilateralVisitor;
1042 typedef typename itk::CellInterfaceVisitorImplementation<
1043 typename MeshType::CellPixelType,
1044 typename MeshType::CellTraits,
1045 itk::TetrahedronCell<itk::CellInterface<typename MeshType::CellPixelType, typename MeshType::CellTraits>>,
1046 SingleCellArrayUserVisitorType>
1047 SingleCellArrayTetrahedronVisitor;
1052 typedef typename itk::CellInterfaceVisitorImplementation<
1053 typename MeshType::CellPixelType,
1054 typename MeshType::CellTraits,
1055 itk::HexahedronCell<itk::CellInterface<typename MeshType::CellPixelType, typename MeshType::CellTraits>>,
1056 SingleCellArrayUserVisitorType>
1057 SingleCellArrayHexahedronVisitor;
1060 int numPoints = mesh->GetNumberOfPoints();
1064 MITK_FATAL <<
"no points in Grid " << std::endl;
1068 vtkUnstructuredGrid *vgrid = vtkUnstructuredGrid::New();
1070 vtkPoints *vpoints = vtkPoints::New(VTK_DOUBLE);
1072 vtkFloatArray *pointScalars = vtkFloatArray::New();
1073 vtkFloatArray *cellScalars = vtkFloatArray::New();
1074 pointScalars->SetNumberOfComponents(1);
1075 cellScalars->SetNumberOfComponents(1);
1077 typename MeshType::PointsContainer::Pointer points = mesh->GetPoints();
1078 typename MeshType::PointsContainer::Iterator i;
1082 unsigned int maxIndex = 0;
1083 for (i = points->Begin(); i != points->End(); ++i)
1085 if (maxIndex < i->Index())
1086 maxIndex = i->Index();
1090 vpoints->SetNumberOfPoints(maxIndex + 1);
1091 pointScalars->SetNumberOfTuples(maxIndex + 1);
1092 cellScalars->SetNumberOfTuples(mesh->GetNumberOfCells());
1095 typename MeshType::PointType itkPhysicalPoint;
1096 if (geometryFrame ==
nullptr)
1098 for (i = points->Begin(); i != points->End(); ++i)
1101 int idx = i->Index();
1103 itkPhysicalPoint = i->Value();
1106 vpoints->SetPoint(idx, vtkpoint);
1108 if (usePointScalarAccessor)
1110 pointScalars->InsertTuple1(
1111 idx, ScalarAccessor::GetPointScalar(mesh->GetPointData(), i->Index(), mesh, pointDataType));
1118 for (i = points->Begin(); i != points->End(); ++i)
1121 int idx = i->Index();
1123 itkPhysicalPoint = i->Value();
1124 geometryFrame->ItkPhysicalPointToWorld(itkPhysicalPoint, mitkWorldPoint);
1127 vpoints->SetPoint(idx, vtkpoint);
1129 if (usePointScalarAccessor)
1131 pointScalars->InsertTuple1(
1132 idx, ScalarAccessor::GetPointScalar(mesh->GetPointData(), i->Index(), mesh, pointDataType));
1137 vgrid->SetPoints(vpoints);
1138 if (usePointScalarAccessor)
1139 vgrid->GetPointData()->SetScalars(pointScalars);
1143 typename MeshType::CellType::MultiVisitor::Pointer mv = MeshType::CellType::MultiVisitor::New();
1145 typename SingleCellArrayLineVisitor::Pointer lv = SingleCellArrayLineVisitor::New();
1146 typename SingleCellArrayPolygonVisitor::Pointer pv = SingleCellArrayPolygonVisitor::New();
1147 typename SingleCellArrayTriangleVisitor::Pointer tv = SingleCellArrayTriangleVisitor::New();
1148 typename SingleCellArrayQuadrilateralVisitor::Pointer qv = SingleCellArrayQuadrilateralVisitor::New();
1149 typename SingleCellArrayTetrahedronVisitor::Pointer tetv = SingleCellArrayTetrahedronVisitor::New();
1150 typename SingleCellArrayHexahedronVisitor::Pointer hv = SingleCellArrayHexahedronVisitor::New();
1153 int numCells = mesh->GetNumberOfCells();
1154 int *types =
new int[numCells];
1156 vtkCellArray *cells = vtkCellArray::New();
1157 cells->Allocate(numCells);
1159 lv->SetTypeArray(types);
1160 lv->SetCellArray(cells);
1161 pv->SetTypeArray(types);
1162 pv->SetCellArray(cells);
1163 tv->SetTypeArray(types);
1165 tv->SetCellArray(cells);
1166 qv->SetTypeArray(types);
1168 qv->SetCellArray(cells);
1169 tetv->SetTypeArray(types);
1170 tetv->SetCellArray(cells);
1171 hv->SetTypeArray(types);
1172 hv->SetCellArray(cells);
1174 if (useCellScalarAccessor)
1176 lv->SetUseCellScalarAccessor(
true);
1177 lv->SetCellScalars(cellScalars);
1178 lv->SetMeshCellData(mesh->GetCellData());
1180 pv->SetUseCellScalarAccessor(
true);
1181 pv->SetCellScalars(cellScalars);
1182 pv->SetMeshCellData(mesh->GetCellData());
1184 tv->SetUseCellScalarAccessor(
true);
1185 tv->SetCellScalars(cellScalars);
1186 tv->SetMeshCellData(mesh->GetCellData());
1188 qv->SetUseCellScalarAccessor(
true);
1189 qv->SetCellScalars(cellScalars);
1190 qv->SetMeshCellData(mesh->GetCellData());
1192 tetv->SetUseCellScalarAccessor(
true);
1193 tetv->SetCellScalars(cellScalars);
1194 tetv->SetMeshCellData(mesh->GetCellData());
1196 hv->SetUseCellScalarAccessor(
true);
1197 hv->SetCellScalars(cellScalars);
1198 hv->SetMeshCellData(mesh->GetCellData());
1206 mv->AddVisitor(tetv);
1214 vgrid->SetCells(types, cells);
1215 vgrid->GetCellData()->SetScalars(cellScalars);
1222 pointScalars->Delete();
1223 cellScalars->Delete();
1232 bool onlyTriangles =
false,
1233 bool useScalarAccessor =
false,
1234 unsigned int pointDataType = 0,
1236 vtkPolyData *polydata =
nullptr)
1241 typedef typename itk::CellInterfaceVisitorImplementation<
typename MeshType::CellPixelType,
1242 typename MeshType::CellTraits,
1243 itk::LineCell<typename MeshType::CellType>,
1244 DistributeUserVisitorType>
1245 DistributeLineVisitor;
1250 typedef typename itk::CellInterfaceVisitorImplementation<
typename MeshType::CellPixelType,
1251 typename MeshType::CellTraits,
1252 itk::PolygonCell<typename MeshType::CellType>,
1253 DistributeUserVisitorType>
1254 DistributePolygonVisitor;
1259 typedef typename itk::CellInterfaceVisitorImplementation<
1260 typename MeshType::CellPixelType,
1261 typename MeshType::CellTraits,
1262 itk::TriangleCell<itk::CellInterface<typename MeshType::CellPixelType, typename MeshType::CellTraits>>,
1263 DistributeUserVisitorType>
1264 DistributeTriangleVisitor;
1269 typedef typename itk::CellInterfaceVisitorImplementation<
1270 typename MeshType::CellPixelType,
1271 typename MeshType::CellTraits,
1272 itk::QuadrilateralCell<itk::CellInterface<typename MeshType::CellPixelType, typename MeshType::CellTraits>>,
1273 DistributeUserVisitorType>
1274 DistributeQuadrilateralVisitor;
1279 typedef typename itk::CellInterfaceVisitorImplementation<
1280 typename MeshType::CellPixelType,
1281 typename MeshType::CellTraits,
1282 itk::TriangleCell<itk::CellInterface<typename MeshType::CellPixelType, typename MeshType::CellTraits>>,
1283 ExactUserVisitorType>
1284 ExactTriangleVisitor;
1287 int numPoints = mesh->GetNumberOfPoints();
1291 MITK_ERROR <<
"no points in Grid " << std::endl;
1294 if (polydata ==
nullptr)
1295 polydata = vtkPolyData::New();
1297 polydata->Initialize();
1300 vtkPoints *vpoints = vtkPoints::New(VTK_DOUBLE);
1302 vtkFloatArray *scalars = vtkFloatArray::New();
1303 scalars->SetNumberOfComponents(1);
1305 typename MeshType::PointsContainer::Pointer points = mesh->GetPoints();
1306 typename MeshType::PointsContainer::Iterator i;
1310 unsigned int maxIndex = 0;
1311 for (i = points->Begin(); i != points->End(); ++i)
1313 if (maxIndex < i->Index())
1314 maxIndex = i->Index();
1318 vpoints->SetNumberOfPoints(maxIndex + 1);
1319 scalars->SetNumberOfTuples(maxIndex + 1);
1325 typename MeshType::PointType itkPhysicalPoint;
1326 if (geometryFrame ==
nullptr)
1328 for (i = points->Begin(); i != points->End(); ++i)
1331 int idx = i->Index();
1333 itkPhysicalPoint = i->Value();
1339 vpoints->SetPoint(idx, vtkpoint);
1341 if (useScalarAccessor)
1343 scalars->InsertTuple1(idx,
1344 ScalarAccessor::GetPointScalar(mesh->GetPointData(), i->Index(), mesh, pointDataType));
1351 for (i = points->Begin(); i != points->End(); ++i)
1354 int idx = i->Index();
1356 itkPhysicalPoint = i->Value();
1357 geometryFrame->ItkPhysicalPointToWorld(itkPhysicalPoint, mitkWorldPoint);
1363 vpoints->SetPoint(idx, vtkpoint);
1365 if (useScalarAccessor)
1367 scalars->InsertTuple1(idx,
1368 ScalarAccessor::GetPointScalar(mesh->GetPointData(), i->Index(), mesh, pointDataType));
1374 polydata->SetPoints(vpoints);
1375 if (useScalarAccessor)
1376 polydata->GetPointData()->SetScalars(scalars);
1377 polydata->GetPointData()->CopyAllOn();
1381 typedef typename MeshType::CellType::MultiVisitor MeshMV;
1382 typename MeshMV::Pointer mv = MeshMV::New();
1384 int numCells = mesh->GetNumberOfCells();
1389 vtkCellArray *trianglecells = vtkCellArray::New();
1390 trianglecells->Allocate(numCells);
1393 typename ExactTriangleVisitor::Pointer tv = ExactTriangleVisitor::New();
1394 tv->SetCellArrays(
nullptr, trianglecells,
nullptr,
nullptr);
1402 if (trianglecells->GetNumberOfCells() > 0)
1403 polydata->SetStrips(trianglecells);
1406 trianglecells->Delete();
1411 vtkCellArray *linecells = vtkCellArray::New();
1412 vtkCellArray *trianglecells = vtkCellArray::New();
1413 vtkCellArray *polygoncells = vtkCellArray::New();
1414 linecells->Allocate(numCells);
1415 trianglecells->Allocate(numCells);
1416 polygoncells->Allocate(numCells);
1419 typename DistributeLineVisitor::Pointer lv = DistributeLineVisitor::New();
1420 typename DistributePolygonVisitor::Pointer pv = DistributePolygonVisitor::New();
1421 typename DistributeTriangleVisitor::Pointer tv = DistributeTriangleVisitor::New();
1422 typename DistributeQuadrilateralVisitor::Pointer qv = DistributeQuadrilateralVisitor::New();
1424 lv->SetCellArrays(linecells, trianglecells, polygoncells, polygoncells);
1425 pv->SetCellArrays(linecells, trianglecells, polygoncells, polygoncells);
1426 tv->SetCellArrays(linecells, trianglecells, polygoncells, polygoncells);
1427 qv->SetCellArrays(linecells, trianglecells, polygoncells, polygoncells);
1440 if (linecells->GetNumberOfCells() > 0)
1441 polydata->SetLines(linecells);
1442 if (trianglecells->GetNumberOfCells() > 0)
1443 polydata->SetStrips(trianglecells);
1444 if (polygoncells->GetNumberOfCells() > 0)
1445 polydata->SetPolys(polygoncells);
1448 linecells->Delete();
1449 trianglecells->Delete();
1450 polygoncells->Delete();
1460 typename MeshType::PointType::VectorType scale,
1463 typedef itk::RegularSphereMeshSource<MeshType> SphereSourceType;
1464 typename SphereSourceType::Pointer mySphereSource = SphereSourceType::New();
1466 mySphereSource->SetCenter(center);
1467 mySphereSource->SetScale(scale);
1468 mySphereSource->SetResolution(resolution);
1469 mySphereSource->Update();
1471 typename MeshType::Pointer resultMesh = mySphereSource->GetOutput();
1472 resultMesh->Register();
1477 typename MeshType::PointType scale,
1480 typedef typename itk::SphereMeshSource<MeshType> SphereSource;
1482 typename SphereSource::Pointer mySphereSource = SphereSource::New();
1484 mySphereSource->SetCenter(center);
1485 mySphereSource->SetScale(scale);
1486 mySphereSource->SetResolutionX(resolution[0]);
1487 mySphereSource->SetResolutionY(resolution[1]);
1488 mySphereSource->SetSquareness1(1);
1489 mySphereSource->SetSquareness2(1);
1490 mySphereSource->Update();
1491 mySphereSource->GetOutput();
1493 typename MeshType::Pointer resultMesh = mySphereSource->GetOutput();
1494 resultMesh->Register();
1546 typename MeshType::PointType scale,
1549 typedef typename itk::AutomaticTopologyMeshSource<MeshType> MeshSourceType;
1550 typename MeshSourceType::Pointer mySphereSource = MeshSourceType::New();
1552 typename MeshType::PointType pnt0, pnt1, pnt2, pnt3, pnt4, pnt5, pnt6, pnt7, pnt8, pnt9, pnt10, pnt11;
1553 double c1 = 0.5 * (1.0 + sqrt(5.0));
1555 double len = sqrt(c1 * c1 + c2 * c2);
1559 pnt0[0] = center[0] - c1 * scale[0];
1560 pnt0[1] = center[1];
1561 pnt0[2] = center[2] + c2 * scale[2];
1562 pnt1[0] = center[0];
1563 pnt1[1] = center[1] + c2 * scale[1];
1564 pnt1[2] = center[2] - c1 * scale[2];
1565 pnt2[0] = center[0];
1566 pnt2[1] = center[1] + c2 * scale[1];
1567 pnt2[2] = center[2] + c1 * scale[2];
1568 pnt3[0] = center[0] + c1 * scale[0];
1569 pnt3[1] = center[1];
1570 pnt3[2] = center[2] - c2 * scale[2];
1571 pnt4[0] = center[0] - c2 * scale[0];
1572 pnt4[1] = center[1] - c1 * scale[1];
1573 pnt4[2] = center[2];
1574 pnt5[0] = center[0] - c2 * scale[0];
1575 pnt5[1] = center[1] + c1 * scale[1];
1576 pnt5[2] = center[2];
1577 pnt6[0] = center[0];
1578 pnt6[1] = center[1] - c2 * scale[1];
1579 pnt6[2] = center[2] + c1 * scale[2];
1580 pnt7[0] = center[0] + c2 * scale[0];
1581 pnt7[1] = center[1] + c1 * scale[1];
1582 pnt7[2] = center[2];
1583 pnt8[0] = center[0];
1584 pnt8[1] = center[1] - c2 * scale[1];
1585 pnt8[2] = center[2] - c1 * scale[2];
1586 pnt9[0] = center[0] + c1 * scale[0];
1587 pnt9[1] = center[1];
1588 pnt9[2] = center[2] + c2 * scale[2];
1589 pnt10[0] = center[0] + c2 * scale[0];
1590 pnt10[1] = center[1] - c1 * scale[1];
1591 pnt10[2] = center[2];
1592 pnt11[0] = center[0] - c1 * scale[0];
1593 pnt11[1] = center[1];
1594 pnt11[2] = center[2] - c2 * scale[2];
1596 addTriangle(mySphereSource, scale, pnt9, pnt2, pnt6, resolution);
1597 addTriangle(mySphereSource, scale, pnt1, pnt11, pnt5, resolution);
1598 addTriangle(mySphereSource, scale, pnt11, pnt1, pnt8, resolution);
1599 addTriangle(mySphereSource, scale, pnt0, pnt11, pnt4, resolution);
1600 addTriangle(mySphereSource, scale, pnt3, pnt1, pnt7, resolution);
1601 addTriangle(mySphereSource, scale, pnt3, pnt8, pnt1, resolution);
1602 addTriangle(mySphereSource, scale, pnt9, pnt3, pnt7, resolution);
1603 addTriangle(mySphereSource, scale, pnt0, pnt6, pnt2, resolution);
1604 addTriangle(mySphereSource, scale, pnt4, pnt10, pnt6, resolution);
1605 addTriangle(mySphereSource, scale, pnt1, pnt5, pnt7, resolution);
1606 addTriangle(mySphereSource, scale, pnt7, pnt5, pnt2, resolution);
1607 addTriangle(mySphereSource, scale, pnt8, pnt3, pnt10, resolution);
1608 addTriangle(mySphereSource, scale, pnt4, pnt11, pnt8, resolution);
1609 addTriangle(mySphereSource, scale, pnt9, pnt7, pnt2, resolution);
1610 addTriangle(mySphereSource, scale, pnt10, pnt9, pnt6, resolution);
1611 addTriangle(mySphereSource, scale, pnt0, pnt5, pnt11, resolution);
1612 addTriangle(mySphereSource, scale, pnt0, pnt2, pnt5, resolution);
1613 addTriangle(mySphereSource, scale, pnt8, pnt10, pnt4, resolution);
1614 addTriangle(mySphereSource, scale, pnt3, pnt9, pnt10, resolution);
1615 addTriangle(mySphereSource, scale, pnt6, pnt0, pnt4, resolution);
1617 return mySphereSource->GetOutput();
1621 static void addTriangle(
typename itk::AutomaticTopologyMeshSource<MeshType>::Pointer meshSource,
1622 typename MeshType::PointType scale,
1623 typename MeshType::PointType pnt0,
1624 typename MeshType::PointType pnt1,
1625 typename MeshType::PointType pnt2,
1628 if (resolution == 0)
1631 meshSource->AddTriangle(meshSource->AddPoint(pnt0), meshSource->AddPoint(pnt1), meshSource->AddPoint(pnt2));
1635 vnl_vector_fixed<typename MeshType::CoordRepType, 3> v1, v2, res, pv;
1636 v1 = (pnt1 - pnt0).Get_vnl_vector();
1637 v2 = (pnt2 - pnt0).Get_vnl_vector();
1638 res = vnl_cross_3d(v1, v2);
1639 pv = pnt0.GetVectorFromOrigin().Get_vnl_vector();
1643 typename MeshType::PointType pnt01, pnt12, pnt20;
1644 for (
int d = 0; d < 3; d++)
1646 pnt01[d] = (pnt0[d] + pnt1[d]) / 2.0;
1647 pnt12[d] = (pnt1[d] + pnt2[d]) / 2.0;
1648 pnt20[d] = (pnt2[d] + pnt0[d]) / 2.0;
1651 double lenPnt01 = 0;
1652 for (
int d = 0; d < 3; d++)
1653 lenPnt01 += pnt01[d] * pnt01[d];
1654 lenPnt01 = sqrt(lenPnt01);
1655 double lenPnt12 = 0;
1656 for (
int d = 0; d < 3; d++)
1657 lenPnt12 += pnt12[d] * pnt12[d];
1658 lenPnt12 = sqrt(lenPnt12);
1659 double lenPnt20 = 0;
1660 for (
int d = 0; d < 3; d++)
1661 lenPnt20 += pnt20[d] * pnt20[d];
1662 lenPnt20 = sqrt(lenPnt20);
1663 for (
int d = 0; d < 3; d++)
1665 pnt01[d] *= scale[d] / lenPnt01;
1666 pnt12[d] *= scale[d] / lenPnt12;
1667 pnt20[d] *= scale[d] / lenPnt20;
1669 addTriangle(meshSource, scale, pnt0, pnt01, pnt20, resolution - 1);
1670 addTriangle(meshSource, scale, pnt01, pnt1, pnt12, resolution - 1);
1671 addTriangle(meshSource, scale, pnt20, pnt12, pnt2, resolution - 1);
1672 addTriangle(meshSource, scale, pnt01, pnt12, pnt20, resolution - 1);
1677 #endif // MITKMESHUTIL_H_INCLUDED
Class for storing surfaces (vtkPolyData).
virtual vtkPolyData * GetVtkPolyData(unsigned int t=0) const
static double GetCellScalar(typename MeshType::CellDataContainer *cellData, typename MeshType::CellIdentifier idx, MeshType *=nullptr, unsigned int=0)
static MeshType::Pointer CreateRegularSphereMesh(typename MeshType::PointType center, typename MeshType::PointType::VectorType scale, int resolution)
static MeshType::Pointer CreateSphereMesh(typename MeshType::PointType center, typename MeshType::PointType scale, int *resolution)
The class provides mehtods for ITK - VTK mesh conversion.
static double GetPointScalar(typename MeshType::PointDataContainer *, typename MeshType::PointIdentifier idx, MeshType *mesh, unsigned int=0)
static vtkPolyData * MeshToPolyData(MeshType *mesh, bool onlyTriangles=false, bool useScalarAccessor=false, unsigned int pointDataType=0, mitk::BaseGeometry *geometryFrame=nullptr, vtkPolyData *polydata=nullptr)
static MeshType::Pointer CreateRegularSphereMesh2(typename MeshType::PointType center, typename MeshType::PointType scale, int resolution)
void vtk2itk(const Tin &in, Tout &out)
static double GetPointScalar(typename MeshType::PointDataContainer *pointData, typename MeshType::PointIdentifier idx, MeshType *=nullptr, unsigned int=0)
static void ConvertTransformToItk(const MITKTransformType *mitkTransform, ITKTransformType *itkTransform)
itk::MatrixOffsetTransformBase< typename MeshType::CoordRepType, 3, 3 > ITKTransformType
static double GetPointScalar(typename MeshType::PointDataContainer *, typename MeshType::PointIdentifier, MeshType *=nullptr, unsigned int=0)
void itk2vtk(const Tin &in, Tout &out)
static vtkUnstructuredGrid * MeshToUnstructuredGrid(MeshType *mesh, bool usePointScalarAccessor=false, bool useCellScalarAccessor=false, unsigned int pointDataType=0, mitk::BaseGeometry *geometryFrame=nullptr)
static MeshType::Pointer MeshFromSurface(mitk::Surface *surface, mitk::BaseGeometry *geometryFrame=nullptr)
itk::MatrixOffsetTransformBase< mitk::ScalarType, 3, 3 > MITKTransformType
static double GetPointScalar(typename MeshType::PointDataContainer *, typename MeshType::PointIdentifier idx, MeshType *mesh, unsigned int type=0)
mitk::BaseGeometry * GetGeometry(int t=0) const
Return the geometry, which is a TimeGeometry, of the data as non-const pointer.
static double GetCellScalar(typename MeshType::CellDataContainer *, typename MeshType::CellIdentifier, MeshType *=nullptr, unsigned int=0)
BaseGeometry Describes the geometry of a data object.
static MeshType::Pointer MeshFromPolyData(vtkPolyData *poly, mitk::BaseGeometry *geometryFrame=nullptr, mitk::BaseGeometry *polyDataGeometryFrame=nullptr)