Medical Imaging Interaction Toolkit  2018.4.99-f51274ea
Medical Imaging Interaction Toolkit
mitkPAVector.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 "mitkPAVector.h"
14 #include "chrono"
15 #include <cmath>
17 {
18  m_Vector.Fill(0);
19 }
20 
22 {
23  m_Vector.Fill(0);
24 }
25 
27 {
28  return m_Vector.GetNorm();
29 }
30 
31 double mitk::pa::Vector::GetElement(unsigned short index)
32 {
33  return m_Vector.GetElement(index);
34 }
35 
36 void mitk::pa::Vector::SetElement(unsigned short index, double value)
37 {
38  m_Vector.SetElement(index, value);
39 }
40 
42 {
43  double norm = m_Vector.GetNorm();
44  m_Vector.SetElement(0, m_Vector.GetElement(0) / norm);
45  m_Vector.SetElement(1, m_Vector.GetElement(1) / norm);
46  m_Vector.SetElement(2, m_Vector.GetElement(2) / norm);
47 }
48 
49 void mitk::pa::Vector::SetValue(mitk::pa::Vector::Pointer value)
50 {
51  m_Vector.SetElement(0, value->GetElement(0));
52  m_Vector.SetElement(1, value->GetElement(1));
53  m_Vector.SetElement(2, value->GetElement(2));
54 }
55 
56 void mitk::pa::Vector::RandomizeByPercentage(double percentage, double bendingFactor, std::mt19937* rng)
57 {
58  std::uniform_real_distribution<> range(-percentage, percentage);
59  m_Vector.SetElement(0, m_Vector.GetElement(0) + (bendingFactor * range(*rng)));
60  m_Vector.SetElement(1, m_Vector.GetElement(1) + (bendingFactor * range(*rng)));
61  m_Vector.SetElement(2, m_Vector.GetElement(2) + (bendingFactor * range(*rng)));
62 }
63 
64 void mitk::pa::Vector::Randomize(double xLowerLimit, double xUpperLimit, double yLowerLimit, double yUpperLimit, double zLowerLimit, double zUpperLimit, std::mt19937* rng)
65 {
66  std::uniform_real_distribution<> rangeX(xLowerLimit, xUpperLimit);
67  std::uniform_real_distribution<> rangeY(yLowerLimit, yUpperLimit);
68  std::uniform_real_distribution<> rangeZ(zLowerLimit, zUpperLimit);
69  m_Vector.SetElement(0, rangeX(*rng));
70  m_Vector.SetElement(1, rangeY(*rng));
71  m_Vector.SetElement(2, rangeZ(*rng));
72 }
73 
74 void mitk::pa::Vector::Randomize(double xLimit, double yLimit, double zLimit, std::mt19937* rng)
75 {
76  Randomize(0, xLimit, 0, yLimit, 0, zLimit, rng);
77 }
78 
79 void mitk::pa::Vector::Randomize(std::mt19937* rng)
80 {
81  Randomize(-1, 1, -1, 1, -1, 1, rng);
82 }
83 
84 void mitk::pa::Vector::PrintSelf(std::ostream& os, itk::Indent /*indent*/) const
85 {
86  os << "X: " << m_Vector.GetElement(0) << std::endl;
87  os << "Y: " << m_Vector.GetElement(1) << std::endl;
88  os << "Z: " << m_Vector.GetElement(2) << std::endl;
89  os << "Length: " << m_Vector.GetNorm() << std::endl;
90 }
91 
92 void mitk::pa::Vector::Rotate(double thetaChange, double phiChange)
93 {
94  MITK_DEBUG << "Vector before rotation: (" << GetElement(0) << "|" << GetElement(1) << "|" << GetElement(2) << ")";
95  if (thetaChange == 0 && phiChange == 0)
96  return;
97 
98  double x = GetElement(0);
99  double y = GetElement(1);
100  double z = GetElement(2);
101 
102  double r = sqrt(x*x + y*y + z*z);
103  if (r == 0)
104  return;
105 
106  double theta = acos(z / r);
107  double phi = atan2(y, x);
108 
109  theta += thetaChange;
110  phi += phiChange;
111 
112  SetElement(0, r * sin(theta) * cos(phi));
113  SetElement(1, r * sin(theta) * sin(phi));
114  SetElement(2, r * cos(theta));
115 
116  MITK_DEBUG << "Vector after rotation: (" << GetElement(0) << "|" << GetElement(1) << "|" << GetElement(2) << ")";
117 }
118 
119 void mitk::pa::Vector::Scale(double factor)
120 {
121  m_Vector.SetElement(0, m_Vector.GetElement(0)*factor);
122  m_Vector.SetElement(1, m_Vector.GetElement(1)*factor);
123  m_Vector.SetElement(2, m_Vector.GetElement(2)*factor);
124 }
125 
126 mitk::pa::Vector::Pointer mitk::pa::Vector::Clone()
127 {
128  auto returnVector = Vector::New();
129  returnVector->SetElement(0, this->GetElement(0));
130  returnVector->SetElement(1, this->GetElement(1));
131  returnVector->SetElement(2, this->GetElement(2));
132  return returnVector;
133 }
134 
135 void mitk::pa::Vector::Subtract(Vector::Pointer other)
136 {
137  m_Vector.SetElement(0, m_Vector.GetElement(0) - other->GetElement(0));
138  m_Vector.SetElement(1, m_Vector.GetElement(1) - other->GetElement(1));
139  m_Vector.SetElement(2, m_Vector.GetElement(2) - other->GetElement(2));
140 }
141 
142 void mitk::pa::Vector::Add(Vector::Pointer other)
143 {
144  m_Vector.SetElement(0, m_Vector.GetElement(0) + other->GetElement(0));
145  m_Vector.SetElement(1, m_Vector.GetElement(1) + other->GetElement(1));
146  m_Vector.SetElement(2, m_Vector.GetElement(2) + other->GetElement(2));
147 }
148 
149 bool mitk::pa::Equal(const Vector::Pointer leftHandSide, const Vector::Pointer rightHandSide, double eps, bool verbose)
150 {
151  MITK_INFO(verbose) << "=== mitk::pa::Vector Equal ===";
152 
153  if (rightHandSide.IsNull() || leftHandSide.IsNull())
154  {
155  MITK_INFO(verbose) << "Cannot compare nullpointers";
156  return false;
157  }
158 
159  if (leftHandSide->GetElement(0) - rightHandSide->GetElement(0) > eps)
160  {
161  MITK_INFO(verbose) << "Element[0] not equal";
162  return false;
163  }
164 
165  if (leftHandSide->GetElement(1) - rightHandSide->GetElement(1) > eps)
166  {
167  MITK_INFO(verbose) << "Element[1] not equal";
168  return false;
169  }
170 
171  if (leftHandSide->GetElement(2) - rightHandSide->GetElement(2) > eps)
172  {
173  MITK_INFO(verbose) << "Element[2] not equal";
174  return false;
175  }
176 
177  return true;
178 }
void Rotate(double xAngle, double yAngle)
Rotate rotates this Vector around the x, y and z axis with the given angles in radians.
void SetElement(unsigned short index, double value)
void PrintSelf(std::ostream &os, itk::Indent indent) const override
#define MITK_INFO
Definition: mitkLogMacros.h:18
void Randomize(double xLowerLimit, double xUpperLimit, double yLowerLimit, double yUpperLimit, double zLowerLimit, double zUpperLimit, std::mt19937 *rng)
Randomize randomizes this vector to be [lowerLimit, upperLimit] in each element.
#define MITK_DEBUG
Definition: mitkLogMacros.h:22
void Add(Vector::Pointer other)
double GetElement(unsigned short index)
void RandomizeByPercentage(double percentage, double bendingFactor, std::mt19937 *rng)
RandomizeByPercentage alters this vector randomly by [-percentage, percentage] of the bendingFactor...
Vector::Pointer Clone()
Clone create a deep copy of this vector.
MITKPHOTOACOUSTICSLIB_EXPORT bool Equal(const MonteCarloThreadHandler::Pointer leftHandSide, const MonteCarloThreadHandler::Pointer rightHandSide, double eps, bool verbose)
Equal A function comparing two thread handlers for beeing equal.
void Scale(double factor)
Scale scales this Vector with the given factor.
bool verbose(false)
void SetValue(Vector::Pointer value)
void Normalize()
Normalize normalizes this vector. After calling this GetNorm() will return 1.
double GetNorm()
GetNorm calculates the length of this vector.
static Pointer New()
MITKCORE_EXPORT const ScalarType eps
void Subtract(Vector::Pointer other)
~Vector() override