Medical Imaging Interaction Toolkit  2016.11.0
Medical Imaging Interaction Toolkit
mitkMeshMitkLoader.cpp
Go to the documentation of this file.
1 /*===================================================================
2 
3 The Medical Imaging Interaction Toolkit (MITK)
4 
5 Copyright (c) German Cancer Research Center,
6 Division of Medical and Biological Informatics.
7 All rights reserved.
8 
9 This software is distributed WITHOUT ANY WARRANTY; without
10 even the implied warranty of MERCHANTABILITY or FITNESS FOR
11 A PARTICULAR PURPOSE.
12 
13 See LICENSE.txt or http://www.mitk.org for details.
14 
15 ===================================================================*/
16 
17 #include "internal/org_mitk_simulation_Activator.h"
18 #include "mitkMeshMitkLoader.h"
19 #include <mitkDataStorage.h>
22 #include <mitkSurface.h>
23 #include <vtkCellArray.h>
24 #include <vtkLinearTransform.h>
25 #include <vtkPolyData.h>
26 #include <vtkSmartPointer.h>
27 #include <vtkTransformPolyDataFilter.h>
28 
30 {
31  mitk::IDataStorageService* dataStorageService = mitk::org_mitk_simulation_Activator::GetService<mitk::IDataStorageService>();
32 
33  if (dataStorageService != nullptr)
34  {
35  mitk::IDataStorageReference::Pointer dataStorageReference = dataStorageService->GetDefaultDataStorage();
36 
37  if (dataStorageReference.IsNotNull())
38  return dataStorageReference->GetDataStorage();
39  }
40 
41  return nullptr;
42 }
43 
44 template <class T>
45 typename T::Pointer GetData(const std::string& name)
46 {
48 
49  if (dataStorage.IsNull())
50  return nullptr;
51 
53  mitk::DataStorage::SetOfObjects::ConstPointer subset = dataStorage->GetSubset(predicate);
54 
55  for (mitk::DataStorage::SetOfObjects::ConstIterator it = subset->Begin(); it != subset->End(); ++it)
56  {
57  mitk::DataNode::Pointer dataNode = it.Value();
58 
59  if (dataNode->GetName() == name)
60  {
61  typename T::Pointer data = static_cast<T*>(dataNode->GetData());
62 
63  if (data.IsNotNull())
64  return data;
65  }
66  }
67 
68  return nullptr;
69 }
70 
71 mitk::MeshMitkLoader::MeshMitkLoader()
72 {
73  this->addAlias(&m_filename, "dataNode");
74  this->addAlias(&m_filename, "surface");
75 }
76 
77 mitk::MeshMitkLoader::~MeshMitkLoader()
78 {
79 }
80 
82 {
83  Surface::Pointer surface = GetData<Surface>(m_filename.getValue());
84 
85  if (surface.IsNull())
86  return false;
87 
88  vtkPolyData* polyData = surface->GetVtkPolyData();
89 
90  if (polyData == nullptr || polyData->GetNumberOfCells() == 0)
91  return false;
92 
93  return true;
94 }
95 
97 {
98  Surface::Pointer surface = GetData<Surface>(m_filename.getValue());
99  vtkPolyData* polyData = surface->GetVtkPolyData();
100 
101  // vtkSmartPointer<vtkTransformPolyDataFilter> transformFilter = vtkSmartPointer<vtkTransformPolyDataFilter>::New();
102  // transformFilter->SetTransform(surface->GetGeometry()->GetVtkTransform());
103  // transformFilter->SetInputConnection(polyData->GetProducerPort());
104  // transformFilter->Update();
105 
106  // polyData = vtkPolyData::SafeDownCast(transformFilter->GetOutputDataObject(0));
107 
108  sofa::helper::vector<sofa::defaulttype::Vector3>& positions = *this->positions.beginEdit();
109  sofa::helper::vector<Edge>& edges = *this->edges.beginEdit();
110  sofa::helper::vector<Triangle>& triangles = *this->triangles.beginEdit();
111  sofa::helper::vector<Quad>& quads = *this->quads.beginEdit();
112  sofa::helper::vector<sofa::helper::vector<unsigned int> > polygons = *this->polygons.beginEdit();
113 
114  vtkPoints* points = polyData->GetPoints();
115  vtkIdType numPoints = points->GetNumberOfPoints();
116  double point[3];
117 
118  for (vtkIdType i = 0; i < numPoints; ++i)
119  {
120  points->GetPoint(i, point);
121  positions.push_back(sofa::defaulttype::Vec3d(point[0], point[1], point[2]));
122  }
123 
124  vtkCellArray* polys = polyData->GetPolys();
125  vtkIdType numPolys = polys->GetNumberOfCells();
126  vtkSmartPointer<vtkIdList> poly = vtkSmartPointer<vtkIdList>::New();
127  Edge edge;
128  Triangle triangle;
129  Quad quad;
130 
131  polys->InitTraversal();
132 
133  while (polys->GetNextCell(poly) != 0)
134  {
135  switch (poly->GetNumberOfIds())
136  {
137  case 1:
138  break;
139 
140  case 2:
141  edge[0] = poly->GetId(0);
142  edge[1] = poly->GetId(1);
143 
144  edges.push_back(edge);
145  break;
146 
147  case 3:
148  triangle[0] = poly->GetId(0);
149  triangle[1] = poly->GetId(1);
150  triangle[2] = poly->GetId(2);
151 
152  triangles.push_back(triangle);
153  break;
154 
155  case 4:
156  quad[0] = poly->GetId(0);
157  quad[1] = poly->GetId(1);
158  quad[2] = poly->GetId(2);
159  quad[3] = poly->GetId(3);
160 
161  quads.push_back(quad);
162  break;
163 
164  default:
165  sofa::helper::vector<unsigned int> polygon;
166  vtkIdType numIds = poly->GetNumberOfIds();
167 
168  polygon.reserve(numIds);
169 
170  for (vtkIdType i = 0; i < numIds; ++i)
171  polygon.push_back(poly->GetId(i));
172 
173  polygons.push_back(polygon);
174  break;
175  }
176  }
177 
178  this->positions.endEdit();
179  this->edges.endEdit();
180  this->triangles.endEdit();
181  this->quads.endEdit();
182  this->polygons.endEdit();
183 
184  return true;
185 }
virtual IDataStorageReference::Pointer GetDefaultDataStorage() const =0
itk::SmartPointer< Self > Pointer
static mitk::DataStorage::Pointer GetDataStorage()
itk::SmartPointer< const Self > ConstPointer
T::Pointer GetData(const std::string &name)
static itkEventMacro(BoundingShapeInteractionEvent, itk::AnyEvent) class MITKBOUNDINGSHAPE_EXPORT BoundingShapeInteractor Pointer New()
Basic interaction methods for mitk::GeometryData.