Medical Imaging Interaction Toolkit  2018.4.99-b20efe7f
Medical Imaging Interaction Toolkit
mitkBoundingShapeUtil.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 "mitkBoundingShapeUtil.h"
14 #include "mitkGeometry3D.h"
15 
16 #include "vtkDoubleArray.h"
17 #include "vtkMath.h"
18 #include <vtkSmartPointer.h>
19 
20 #include <algorithm>
21 
22 mitk::Handle::Handle() : m_IsActive(false), m_FaceIndices(4), m_Index(0)
23 {
24  m_Position.Fill(0.0);
25 }
26 
27 mitk::Handle::Handle(mitk::Point3D pos, int index, std::vector<int> faceIndices, bool active)
28  : m_IsActive(active), m_Position(pos), m_FaceIndices(faceIndices), m_Index(index)
29 {
30 }
31 
33 {
34 }
35 
37 {
38  return m_IsActive;
39 }
41 {
42  return !m_IsActive;
43 };
44 void mitk::Handle::SetActive(bool status)
45 {
46  m_IsActive = status;
47 };
49 {
50  m_Position = pos;
51 };
53 {
54  return m_Position;
55 };
56 void mitk::Handle::SetIndex(int index)
57 {
58  m_Index = index;
59 };
61 {
62  return m_Index;
63 };
64 std::vector<int> mitk::Handle::GetFaceIndices()
65 {
66  return m_FaceIndices;
67 };
68 
70 {
71  mitk::Point3D c;
72  c[0] = (a[0] + b[0]) / 2.0;
73  c[1] = (a[1] + b[1]) / 2.0;
74  c[2] = (a[2] + b[2]) / 2.0;
75  return c;
76 }
77 
78 std::vector<mitk::Point3D> mitk::GetCornerPoints(mitk::BaseGeometry::Pointer geometry, bool visualizationOffset)
79 {
80  if (geometry == nullptr)
81  mitkThrow() << "Geometry is not valid.";
82 
83  mitk::BoundingBox::ConstPointer boundingBox = geometry->GetBoundingBox();
84  mitk::Point3D BBmin = boundingBox->GetMinimum();
85  mitk::Point3D BBmax = boundingBox->GetMaximum();
86 
87  // use 0.5 offset because the vtkCubeSource is not center pixel based (only for visualization purpose)
88  if (visualizationOffset)
89  {
90  BBmin -= 0.5;
91  BBmax -= 0.5;
92  }
93  mitk::Point3D p0;
94  p0[0] = BBmin[0];
95  p0[1] = BBmin[1];
96  p0[2] = BBmin[2]; // bottom - left - back corner
97  mitk::Point3D p1;
98  p1[0] = BBmin[0];
99  p1[1] = BBmin[1];
100  p1[2] = BBmax[2]; // top - left - back corner
101  mitk::Point3D p2;
102  p2[0] = BBmin[0];
103  p2[1] = BBmax[1];
104  p2[2] = BBmin[2]; // bottom - left - front corner
105  mitk::Point3D p3;
106  p3[0] = BBmin[0];
107  p3[1] = BBmax[1];
108  p3[2] = BBmax[2]; // top - left - front corner
109  mitk::Point3D p4;
110  p4[0] = BBmax[0];
111  p4[1] = BBmin[1];
112  p4[2] = BBmin[2]; // bottom - right - back corner
113  mitk::Point3D p5;
114  p5[0] = BBmax[0];
115  p5[1] = BBmin[1];
116  p5[2] = BBmax[2]; // top - right - back corner
117  mitk::Point3D p6;
118  p6[0] = BBmax[0];
119  p6[1] = BBmax[1];
120  p6[2] = BBmin[2]; // bottom - right - front corner
121  mitk::Point3D p7;
122  p7[0] = BBmax[0];
123  p7[1] = BBmax[1];
124  p7[2] = BBmax[2]; // top - right - front corner
125 
126  std::vector<mitk::Point3D> cornerPoints;
127 
128  cornerPoints.push_back(geometry->GetIndexToWorldTransform()->TransformPoint(p0));
129  cornerPoints.push_back(geometry->GetIndexToWorldTransform()->TransformPoint(p1));
130  cornerPoints.push_back(geometry->GetIndexToWorldTransform()->TransformPoint(p2));
131  cornerPoints.push_back(geometry->GetIndexToWorldTransform()->TransformPoint(p3));
132  cornerPoints.push_back(geometry->GetIndexToWorldTransform()->TransformPoint(p4));
133  cornerPoints.push_back(geometry->GetIndexToWorldTransform()->TransformPoint(p5));
134  cornerPoints.push_back(geometry->GetIndexToWorldTransform()->TransformPoint(p6));
135  cornerPoints.push_back(geometry->GetIndexToWorldTransform()->TransformPoint(p7));
136 
137  return cornerPoints;
138 }
139 
140 std::vector<int> mitk::GetHandleIndices(int index)
141 {
142  std::vector<int> faceIndices;
143  faceIndices.resize(4);
144 
145  // +------+
146  // / /|
147  // +------+ |
148  // | | +
149  // | |/
150  // +------+
151 
152  switch (index)
153  {
154  case 0:
155  {
156  faceIndices[0] = 0;
157  faceIndices[1] = 1;
158  faceIndices[2] = 2;
159  faceIndices[3] = 3;
160  }
161  break;
162  case 1:
163  {
164  faceIndices[0] = 4;
165  faceIndices[1] = 5;
166  faceIndices[2] = 6;
167  faceIndices[3] = 7;
168  }
169  break;
170  case 3:
171  {
172  faceIndices[0] = 0;
173  faceIndices[1] = 2;
174  faceIndices[2] = 4;
175  faceIndices[3] = 6;
176  }
177  break;
178  case 2:
179  {
180  faceIndices[0] = 1;
181  faceIndices[1] = 3;
182  faceIndices[2] = 5;
183  faceIndices[3] = 7;
184  }
185  break;
186  case 4:
187  {
188  faceIndices[0] = 0;
189  faceIndices[1] = 1;
190  faceIndices[2] = 4;
191  faceIndices[3] = 5;
192  }
193  break;
194  case 5:
195  {
196  faceIndices[0] = 2;
197  faceIndices[1] = 3;
198  faceIndices[2] = 6;
199  faceIndices[3] = 7;
200  }
201  break;
202  default:
203  {
204  faceIndices[0] = 0;
205  faceIndices[1] = 0;
206  faceIndices[2] = 0;
207  faceIndices[3] = 0;
208  }
209  break;
210  }
211 
212  return faceIndices;
213 }
std::vector< mitk::Point3D > GetCornerPoints(mitk::BaseGeometry::Pointer geometry, bool visualizationOffset)
helper function for calculating corner points of the bounding object from a given geometry ...
std::vector< int > GetHandleIndices(int index)
mitk::Point3D GetPosition()
void SetPosition(mitk::Point3D pos)
#define mitkThrow()
std::vector< int > GetFaceIndices()
void SetActive(bool status)
void SetIndex(int index)
mitk::Point3D CalcAvgPoint(mitk::Point3D a, mitk::Point3D b)
helper function for calculating the average of two points