Medical Imaging Interaction Toolkit  2018.4.99-3e3f1a6e
Medical Imaging Interaction Toolkit
mitkUnstructuredGrid.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 (DKFZ)
6 All rights reserved.
7 
8 Use of this source code is governed by a 3-clause BSD license that can be
9 found in the LICENSE file.
10 
11 ============================================================================*/
12 
13 #include "mitkUnstructuredGrid.h"
14 
15 #include <vtkUnstructuredGrid.h>
16 
17 void mitk::UnstructuredGrid::SetVtkUnstructuredGrid(vtkUnstructuredGrid *grid, unsigned int t)
18 {
19  this->Expand(t + 1);
20 
21  if (m_GridSeries[t] != nullptr)
22  {
23  m_GridSeries[t]->Delete();
24  }
25 
26  m_GridSeries[t] = grid;
27 
28  // call m_VtkPolyData->Register(nullptr) to tell the reference counting that we
29  // want to keep a reference on the object
30  if (m_GridSeries[t] != nullptr)
31  m_GridSeries[t]->Register(grid);
32 
33  this->Modified();
35 }
36 
37 void mitk::UnstructuredGrid::Expand(unsigned int timeSteps)
38 {
39  // check if the vector is long enough to contain the new element
40  // at the given position. If not, expand it with sufficient zero-filled elements.
41  if (timeSteps > m_GridSeries.size())
42  {
43  Superclass::Expand(timeSteps);
44  vtkUnstructuredGrid *pdnull = nullptr;
45  m_GridSeries.resize(timeSteps, pdnull);
47  }
48 }
49 
51 {
52  for (auto it = m_GridSeries.begin(); it != m_GridSeries.end(); ++it)
53  {
54  if ((*it) != nullptr)
55  (*it)->Delete();
56  }
57  m_GridSeries.clear();
58 
59  Superclass::ClearData();
60 }
61 
63 {
64  vtkUnstructuredGrid *pdnull = nullptr;
65  m_GridSeries.resize(1, pdnull);
66  Superclass::InitializeTimeGeometry(1);
67 
68  m_Initialized = true;
69 }
70 
71 vtkUnstructuredGrid *mitk::UnstructuredGrid::GetVtkUnstructuredGrid(unsigned int t)
72 {
73  if (t < m_GridSeries.size())
74  {
75  vtkUnstructuredGrid *grid = m_GridSeries[t];
76  if ((grid == nullptr) && (GetSource().GetPointer() != nullptr))
77  {
78  RegionType requestedregion;
79  requestedregion.SetIndex(3, t);
80  requestedregion.SetSize(3, 1);
81  SetRequestedRegion(&requestedregion);
82  GetSource()->Update();
83  }
84  grid = m_GridSeries[t];
85  return grid;
86  }
87  else
88  return nullptr;
89 }
90 
91 void mitk::UnstructuredGrid::Graft(const DataObject *data)
92 {
93  const auto *grid = dynamic_cast<const UnstructuredGrid *>(data);
94 
95  if (grid == nullptr)
96  mitkThrow() << "Data object used to graft surface is not a mitk::Surface.";
97 
98  this->CopyInformation(data);
99  m_GridSeries.clear();
100 
101  for (unsigned int i = 0; i < grid->m_GridSeries.size(); ++i)
102  {
103  m_GridSeries.push_back(vtkUnstructuredGrid::New());
104  m_GridSeries.back()->DeepCopy(const_cast<mitk::UnstructuredGrid *>(grid)->GetVtkUnstructuredGrid(i));
105  }
106 }
107 
109 {
110  this->InitializeEmpty();
111 }
112 
114  : BaseData(other),
117 {
118  if (!other.m_Initialized)
119  {
120  this->InitializeEmpty();
121  }
122  else
123  {
124  m_GridSeries = other.m_GridSeries;
126  }
127  this->SetRequestedRegion(&other);
128 }
129 
131 {
132  this->ClearData();
133 }
134 
136 {
137  if (this->GetSource())
138  {
139  this->GetSource()->UpdateOutputInformation();
140  }
141  if ((m_CalculateBoundingBox) && (m_GridSeries.size() > 0))
143  else
144  GetTimeGeometry()->Update();
145 }
146 
148 {
149  //
150  // first make sure, that the associated time sliced geometry has
151  // the same number of geometry 3d's as vtkUnstructuredGrids are present
152  //
153  TimeGeometry *timeGeometry = GetTimeGeometry();
154  if (timeGeometry->CountTimeSteps() != m_GridSeries.size())
155  {
156  itkExceptionMacro(<< "timeGeometry->CountTimeSteps() != m_GridSeries.size() -- use Initialize(timeSteps) with "
157  "correct number of timeSteps!");
158  }
159 
160  //
161  // Iterate over the vtkUnstructuredGrids and update the Geometry
162  // information of each of the items.
163  //
164  for (unsigned int i = 0; i < m_GridSeries.size(); ++i)
165  {
166  vtkUnstructuredGrid *grid = m_GridSeries[i];
167  double bounds[] = {0.0, 0.0, 0.0, 0.0, 0.0, 0.0};
168  if ((grid != nullptr) && (grid->GetNumberOfCells() > 0))
169  {
170  // grid->Update(); //VTK6_TODO
171  grid->ComputeBounds();
172  grid->GetBounds(bounds);
173  }
175  assert(g3d.IsNotNull());
176  g3d->SetFloatBounds(bounds);
177  }
178  timeGeometry->Update();
179 
180  mitk::BoundingBox::Pointer bb = timeGeometry->GetBoundingBoxInWorld();
181  itkDebugMacro(<< "boundingbox min: " << bb->GetMinimum());
182  itkDebugMacro(<< "boundingbox max: " << bb->GetMaximum());
183  m_CalculateBoundingBox = false;
184 }
185 
187 {
189 }
190 
192 {
193  RegionType::IndexValueType end = m_RequestedRegion.GetIndex(3) + m_RequestedRegion.GetSize(3);
194 
195  if (((RegionType::IndexValueType)m_GridSeries.size()) < end)
196  return true;
197 
198  for (RegionType::IndexValueType t = m_RequestedRegion.GetIndex(3); t < end; ++t)
199  if (m_GridSeries[t] == nullptr)
200  return true;
201 
202  return false;
203 }
204 
206 {
207  if ((m_RequestedRegion.GetIndex(3) >= 0) &&
208  (m_RequestedRegion.GetIndex(3) + m_RequestedRegion.GetSize(3) <= m_GridSeries.size()))
209  return true;
210 
211  return false;
212 }
213 
214 void mitk::UnstructuredGrid::SetRequestedRegion(const itk::DataObject *data)
215 {
216  const mitk::UnstructuredGrid *gridData;
217 
218  gridData = dynamic_cast<const mitk::UnstructuredGrid *>(data);
219 
220  if (gridData)
221  {
223  }
224  else
225  {
226  // pointer could not be cast back down
227  itkExceptionMacro(<< "mitk::UnstructuredGrid::SetRequestedRegion(DataObject*) cannot cast " << typeid(data).name()
228  << " to "
229  << typeid(UnstructuredGrid *).name());
230  }
231 }
232 
234 {
235  if (region != nullptr)
236  {
237  m_RequestedRegion = *region;
238  }
239  else
240  {
241  // pointer could not be cast back down
242  itkExceptionMacro(<< "mitk::UnstructuredGrid::SetRequestedRegion(UnstructuredGrid::RegionType*) cannot cast "
243  << typeid(region).name()
244  << " to "
245  << typeid(UnstructuredGrid *).name());
246  }
247 }
248 
249 void mitk::UnstructuredGrid::CopyInformation(const itk::DataObject *data)
250 {
251  Superclass::CopyInformation(data);
252 }
253 
255 {
256  if (GetSource().IsNull())
257  {
258  for (auto it = m_GridSeries.begin(); it != m_GridSeries.end(); ++it)
259  {
260  // if ( ( *it ) != 0 )
261  // ( *it )->Update(); //VTK6_TODO
262  }
263  }
265 }
void ClearData() override
reset to non-initialized state, release memory
BoundingBox * GetBoundingBoxInWorld() const
Returns a bounding box that covers all time steps.
itk::SmartPointer< mitk::BaseDataSource > GetSource() const
Get the process object that generated this data object.
virtual TimeStepType CountTimeSteps() const =0
Returns the number of time steps.
void CopyInformation(const itk::DataObject *data) override
Base of all data objects.
Definition: mitkBaseData.h:37
static void Update(vtkPolyData *)
Definition: mitkSurface.cpp:31
void SetRequestedRegion(const itk::DataObject *data) override
Set the requested region from this data object to match the requested region of the data object passe...
VTKUnstructuredGridSeries m_GridSeries
virtual const RegionType & GetRequestedRegion() const
const mitk::TimeGeometry * GetTimeGeometry() const
Return the TimeGeometry of the data as const pointer.
Definition: mitkBaseData.h:61
itk::ImageRegion< 5 > RegionType
bool VerifyRequestedRegion() override
Verify that the RequestedRegion is within the LargestPossibleRegion.
void Expand(unsigned int timeSteps=1) override
Expands the TimeGeometry to a number of TimeSteps.
#define mitkThrow()
virtual void SetVtkUnstructuredGrid(vtkUnstructuredGrid *grid, unsigned int t=0)
virtual vtkUnstructuredGrid * GetVtkUnstructuredGrid(unsigned int t=0)
void Graft(const DataObject *data) override
bool RequestedRegionIsOutsideOfTheBufferedRegion() override
Determine whether the RequestedRegion is outside of the BufferedRegion.
void InitializeEmpty() override
Pure virtual; Must be used in subclasses to get a data object to a valid state. Should at least creat...
const RegionType & GetLargestPossibleRegion() const
virtual BaseGeometry::Pointer GetGeometryForTimeStep(TimeStepType timeStep) const =0
Returns the geometry which corresponds to the given time step.
Class for storing unstructured grids (vtkUnstructuredGrid)
void Update()
Updates the geometry.
void UpdateOutputInformation() override
void SetRequestedRegionToLargestPossibleRegion() override
Set the RequestedRegion to the LargestPossibleRegion.