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