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