Medical Imaging Interaction Toolkit  2016.11.0
Medical Imaging Interaction Toolkit
mitkSimulation.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 "mitkSimulation.h"
18 #include <sofa/simulation/graph/DAGSimulation.h>
19 #include "mitkGeometry3D.h"
20 
21 static sofa::simulation::Simulation::SPtr CreateSimulation()
22 {
23  const std::string key = "MultiMappingObject";
24 
25  if (sofa::simulation::xml::BaseElement::NodeFactory::HasKey(key))
26  sofa::simulation::xml::BaseElement::NodeFactory::ResetEntry(key);
27 
28  return sofa::core::objectmodel::New<sofa::simulation::graph::DAGSimulation>();
29 }
30 
31 mitk::Simulation::Simulation()
32  : m_SOFASimulation(CreateSimulation())
33 {
34 }
35 
36 mitk::Simulation::~Simulation()
37 {
38  if (m_RootNode)
39  {
40  if (m_SOFASimulation)
41  m_SOFASimulation->unload(m_RootNode);
42  }
43 }
44 
46 {
47  if (!m_RootNode || !m_SOFASimulation)
48  return;
49 
50  boost::chrono::high_resolution_clock::time_point t0 = boost::chrono::high_resolution_clock::now();
51  m_SOFASimulation->animate(m_RootNode.get(), m_RootNode->getDt());
52  this->SetElapsedTime(boost::chrono::high_resolution_clock::now() - t0);
53 
54  this->UpdateOutputInformation();
55 }
56 
57 sofa::core::visual::DrawTool* mitk::Simulation::GetDrawTool()
58 {
59  return &m_DrawTool;
60 }
61 
62 sofa::simulation::Node::SPtr mitk::Simulation::GetRootNode() const
63 {
64  return m_RootNode;
65 }
66 
67 sofa::simulation::Simulation::SPtr mitk::Simulation::GetSOFASimulation() const
68 {
69  return m_SOFASimulation;
70 }
71 
73 {
74  if (!m_RootNode || !m_SOFASimulation)
75  return;
76 
77  m_SOFASimulation->reset(m_RootNode.get());
78  m_RootNode->setTime(0.0);
79  m_SOFASimulation->updateContext(m_RootNode.get());
80 }
81 
83 {
84  return m_RootNode
85  ? m_RootNode->getContext()->getAnimate()
86  : false;
87 }
88 
90 {
91  if (m_RootNode)
92  m_RootNode->getContext()->setAnimate(animate);
93 }
94 
95 void mitk::Simulation::SetDt(double dt)
96 {
97  if (m_RootNode)
98  m_RootNode->setDt(dt);
99 }
100 
101 void mitk::Simulation::SetRootNode(sofa::simulation::Node::SPtr rootNode)
102 {
103  m_RootNode = rootNode;
104 }
105 
107 {
108  return false;
109 }
110 
111 void mitk::Simulation::SetRequestedRegion(const itk::DataObject*)
112 {
113 }
114 
116 {
117 }
118 
120 {
122  using sofa::defaulttype::Vector3;
123 
124  if (this->GetSource().IsNotNull())
125  this->GetSource()->UpdateOutputInformation();
126 
127  if (m_RootNode)
128  {
129  const BoundingBox& boundingBox = m_RootNode->f_bbox.getValue();
131 
132  if (boundingBox.isValid())
133  {
134  const Vector3& min = boundingBox.minBBox();
135  const Vector3& max = boundingBox.maxBBox();
136 
137  bounds[0] = static_cast<mitk::Geometry3D::BoundsArrayType::ValueType>(min.x());
138  bounds[1] = static_cast<mitk::Geometry3D::BoundsArrayType::ValueType>(max.x());
139  bounds[2] = static_cast<mitk::Geometry3D::BoundsArrayType::ValueType>(min.y());
140  bounds[3] = static_cast<mitk::Geometry3D::BoundsArrayType::ValueType>(max.y());
141  bounds[4] = static_cast<mitk::Geometry3D::BoundsArrayType::ValueType>(min.z());
142  bounds[5] = static_cast<mitk::Geometry3D::BoundsArrayType::ValueType>(max.z());
143  }
144  else
145  {
146  bounds.Fill(0.0f);
147  }
148 
149  mitk::BaseGeometry::Pointer geometry = this->GetGeometry();
150 
151  if (geometry.IsNull())
152  {
153  geometry = Geometry3D::New();
154  geometry->SetBounds(bounds);
155  this->SetGeometry(geometry);
156  }
157  else
158  {
159  geometry->SetBounds(bounds);
160  }
161  }
162 
163  this->GetTimeGeometry()->Update();
164 }
165 
167 {
168  return true;
169 }
itk::BoundingBox< unsigned long, 3, ScalarType > BoundingBox
Standard 3D-BoundingBox typedef.
BoundingBoxType::BoundsArrayType BoundsArrayType
void SetRequestedRegionToLargestPossibleRegion() override
Set the RequestedRegion to the LargestPossibleRegion.
void SetDt(double dt)
static Pointer New()
bool GetAnimationFlag() const
void UpdateOutputInformation() override
bool RequestedRegionIsOutsideOfTheBufferedRegion() override
Determine whether the RequestedRegion is outside of the BufferedRegion.
bool VerifyRequestedRegion() override
Verify that the RequestedRegion is within the LargestPossibleRegion.
void SetRootNode(sofa::simulation::Node::SPtr rootNode)
ValueType
Type of the value held by a Value object.
Definition: jsoncpp.h:345
sofa::simulation::Node::SPtr GetRootNode() const
static T max(T x, T y)
Definition: svm.cpp:70
sofa::simulation::Simulation::SPtr GetSOFASimulation() const
static T min(T x, T y)
Definition: svm.cpp:67
itk::BoundingBox< unsigned long, 3, mitk::ScalarType > BoundingBox
void SetAnimationFlag(bool animate)
static sofa::simulation::Simulation::SPtr CreateSimulation()
void SetRequestedRegion(const itk::DataObject *) override
Set the requested region from this data object to match the requested region of the data object passe...
sofa::core::visual::DrawTool * GetDrawTool()