ML Reference
mlVector3.h
Go to the documentation of this file.
1 /*************************************************************************************
2 **
3 ** Copyright 2007, MeVis Medical Solutions AG
4 **
5 ** The user may use this file in accordance with the license agreement provided with
6 ** the Software or, alternatively, in accordance with the terms contained in a
7 ** written agreement between the user and MeVis Medical Solutions AG.
8 **
9 ** For further information use the contact form at https://www.mevislab.de/contact
10 **
11 **************************************************************************************/
12 
13 #ifndef ML_VECTOR3_H
14 #define ML_VECTOR3_H
15 
17 
18 // Include system independent file and project settings.
19 #include "mlLinearAlgebraSystem.h"
20 #include "mlLinearAlgebraDefs.h"
21 #include "mlFloatingPointVector.h"
22 #include "mlPrintTemplateErrors.h"
23 #include "mlVector2.h"
24 
25 #include <mlErrorMacros.h>
26 #include <mlUtilsAPI.h>
27 
28 // All declarations of this header will be in the ML_LA_NAMESPACE namespace.
29 ML_LA_START_NAMESPACE
30 
32 template <class DT>
34 {
35 public:
36  union {
37  struct {
39  DT x;
41  DT y;
43  DT z;
44  };
46  DT _buffer[3];
47  };
48 };
49 
50 //--------------------------------------------------------------------
52 // This is necessary, because we do not know whether vector or
53 // matrix header is included first and we cannot move template
54 // code into the C++ file.
55 //--------------------------------------------------------------------
56 template <class DT> class Tvec4;
57 template <class DT> class Tmat3;
58 template <class DT> class Tmat4;
60 
61 //--------------------------------------------------------------------
63 //--------------------------------------------------------------------
64 template <class DT>
65 class Tvec3 : public FloatingPointVector<DT, 3, Vector3DataContainer<DT> >
66 {
67 
68 public:
69 
72 
74  typedef DT ComponentType;
75 
76  //--------------------------------------------------------------------
79  //--------------------------------------------------------------------
82  inline explicit Tvec3(const DT value=0) : FloatingPointVector<DT, 3, Vector3DataContainer<DT> >(value)
83  {
84  }
85 
89  inline Tvec3(const Superclass &v) : Superclass(v)
90  {
91  }
92 
94  inline Tvec3(const DT px, const DT py, const DT pz)
95  {
96  Superclass::_buffer[0] = px;
97  Superclass::_buffer[1] = py;
98  Superclass::_buffer[2] = pz;
99  };
100 
102  inline Tvec3(const Tvec2<DT>& v, const DT pz)
103  {
104  Superclass::_buffer[0] = v[0];
105  Superclass::_buffer[1] = v[1];
106  Superclass::_buffer[2] = pz;
107  };
108 
114  Tvec3(const Tvec4<DT>& v, const bool normalizeV)
115  {
116  if (normalizeV){
117  // Homogeneous cast.
118  if (MLValuesDifferWOM(v[3], static_cast<DT>(0))){
119  DT divi = 1.0/v[3];
120  Superclass::_buffer[0] = v[0]*divi;
121  Superclass::_buffer[1] = v[1]*divi;
122  Superclass::_buffer[2] = v[2]*divi;
123  }
124  else{
125  // This must/should not occur.
126  printTemplateError("Tvec3::Tvec3(const Tvec4<DT>& v, const bool normalizeV)",
128  "Normalization with zero fourth component of homogeneous vector "
129  "performed. Leaving vector unchanged.");
130  }
131  }
132  else{
133  // Normal cast.
134  Superclass::_buffer[0] = v[0];
135  Superclass::_buffer[1] = v[1];
136  Superclass::_buffer[2] = v[2];
137  }
138  }
139 
143  Tvec3(const Tvec4<DT>& v, const int axis)
144  {
145  switch (axis) {
146  case 0:
147  Superclass::_buffer[0] = v[1];
148  Superclass::_buffer[1] = v[2];
149  Superclass::_buffer[2] = v[3];
150  break;
151  case 1:
152  Superclass::_buffer[0] = v[0];
153  Superclass::_buffer[1] = v[2];
154  Superclass::_buffer[2] = v[3];
155  break;
156  case 2:
157  Superclass::_buffer[0] = v[0];
158  Superclass::_buffer[1] = v[1];
159  Superclass::_buffer[2] = v[3];
160  break;
161  default:
162  Superclass::_buffer[0] = v[0];
163  Superclass::_buffer[1] = v[1];
164  Superclass::_buffer[2] = v[2];
165  break;
166  }
167  }
169 
171  inline void assign(const DT px, const DT py, const DT pz)
172  {
173  Superclass::_buffer[0] = px;
174  Superclass::_buffer[1] = py;
175  Superclass::_buffer[2] = pz;
176  }
177 
183  {
184  const DT lastComp = Superclass::_buffer[2];
185  ML_CHECK_FLOAT(lastComp);
186 
187  const DT div = static_cast<DT>(1) / lastComp;
188 
189  return Tvec3(Superclass::_buffer[0] * div,
190  Superclass::_buffer[1] * div,
191  1);
192  }
193 
196  inline Tvec4<DT> affineVec() const
197  {
198  return Tvec4<DT>(*this, 0);
199  }
200 
203  inline Tvec4<DT> affinePoint() const
204  {
205  return Tvec4<DT>(*this, 1);
206  }
207 
208 }; // end of class *Tvec3*
209 
210 
211 //--------------------------------------------------------------------
214 //--------------------------------------------------------------------
215 template <class DT, class DT2>
216 inline Tvec3<DT> operator*(const Tmat3<DT>& a, const Tvec3<DT2>& v)
217 {
218  return Tvec3<DT>(a[0][0]*v[0] + a[0][1]*v[1] + a[0][2]*v[2],
219  a[1][0]*v[0] + a[1][1]*v[1] + a[1][2]*v[2],
220  a[2][0]*v[0] + a[2][1]*v[1] + a[2][2]*v[2]);
221 }
222 
223 //--------------------------------------------------------------------
226 //--------------------------------------------------------------------
227 template <class DT, class DT2>
228 inline Tvec3<DT> operator*(const Tvec3<DT>& v, const Tmat3<DT2>& a)
229 {
230  return a.transpose() * v;
231 }
232 
233 //--------------------------------------------------------------------
240 //--------------------------------------------------------------------
241 template <class DT, class DT2>
242 inline Tvec3<DT> operator*(const Tmat4<DT>& a, const Tvec3<DT2>& v)
243 {
244  // Normalize homogeneous 4D result to Tvec3 by dividing by the fourth component.
245  return Tvec3<DT>(a * v.affinePoint(), true);
246 }
247 
248 //--------------------------------------------------------------------
255 //--------------------------------------------------------------------
256 template <class DT, class DT2>
257 inline Tvec3<DT> operator*(const Tvec3<DT>& v, const Tmat4<DT2>& a)
258 {
259  // Note: This uses homogeneous "operator*(const Tmat4<DT>& a, const Tvec3<DT2>& v)"
260  // implemented above.
261  return a.transpose() * v;
262 }
263 
264 //--------------------------------------------------------------------
266 //--------------------------------------------------------------------
267 template <class DT, class DT2>
268 inline Tvec3<DT> operator^(const Tvec3<DT>& a, const Tvec3<DT2>& b)
269 {
270  return Tvec3<DT>(a[1]*b[2] - a[2]*b[1],
271  a[2]*b[0] - a[0]*b[2],
272  a[0]*b[1] - a[1]*b[0]);
273 }
274 
275 //-----------------------------------------------------------------------------------
278 //-----------------------------------------------------------------------------------
288 
289 ML_LA_END_NAMESPACE
290 
291 #endif //of __mlVector3_H
292 
293 
Template class for vector arithmetic with floating point datatypes.
T operator*(const FloatingPointVector< T, size, DataContainer > &a, const FloatingPointVector< T, size, DataContainer > &b)
Dot product, returns a.dot(b).
A 3x3 matrix class of three row vectors.
Definition: mlMatrix3.h:37
Tmat3 transpose() const
Returns the transpose of this matrix.
Definition: mlMatrix3.h:426
A 4x4 matrix class consisting of four row vectors.
Definition: mlMatrix4.h:36
Tmat4< DT > transpose() const
Returns the transposed *this.
Definition: mlMatrix4.h:572
Declaration of float vector type traits.
Definition: mlVector2.h:57
Forward declarations to resolve header file dependencies.
Definition: mlVector3.h:66
DT ComponentType
A typedef to 'export' the type of components.
Definition: mlVector3.h:74
Tvec3< DT > divideByLastComp() const
Divides all vector components by its last component and returns it as Tvec3, which then has a 1 as la...
Definition: mlVector3.h:182
FloatingPointVector< DT, 3, Vector3DataContainer< DT > > Superclass
A typedef as a shorthand for the base class.
Definition: mlVector3.h:71
Tvec4< DT > affinePoint() const
Builds a homogeneous Tvec4 point from *this and returns it, i.e., leaves all components unchanged and...
Definition: mlVector3.h:203
Tvec4< DT > affineVec() const
Builds a homogeneous Tvec4 vector from *this and returns it, i.e., leaves all components unchanged an...
Definition: mlVector3.h:196
Tvec3(const Tvec4< DT > &v, const bool normalizeV)
Casts a Tvec4 v to Tvec3.
Definition: mlVector3.h:114
void assign(const DT px, const DT py, const DT pz)
Sets all components to the passed values.
Definition: mlVector3.h:171
Tvec3(const DT value=0)
Default and value constructor.
Definition: mlVector3.h:82
Tvec3(const Superclass &v)
Copy constructor from FloatingPointVector.
Definition: mlVector3.h:89
Tvec3(const Tvec4< DT > &v, const int axis)
Casts a Tvec4 v to Tvec3.
Definition: mlVector3.h:143
Tvec3(const DT px, const DT py, const DT pz)
Constructor building the vector px, py, and pz.
Definition: mlVector3.h:94
Tvec3(const Tvec2< DT > &v, const DT pz)
Builds a Tvec3 from a Tvec2 and a scalar. Sets the third entry to user given value pz.
Definition: mlVector3.h:102
Forward declarations to resolve header file dependencies.
Definition: mlVector4.h:50
Specialized base class for the FloatingPointVectorDataContainerBase.
Definition: mlVector3.h:34
DT x
X-component of the vector, same as _buffer[0].
Definition: mlVector3.h:39
DT z
Z-component of the vector, same as _buffer[2].
Definition: mlVector3.h:43
DT y
Y-component of the vector, same as _buffer[1].
Definition: mlVector3.h:41
bool MLValuesDifferWOM(MLint8 a, MLint8 b)
Returns true if values differ; otherwise, it returns false.
#define ML_BAD_PARAMETER
A bad/invalid parameter (or even an inappropriate image) has been passed to a module or an algorithm,...
Definition: mlTypeDefs.h:823
#define ML_CHECK_FLOAT(x)
void ML_UTILS_EXPORT printTemplateError(const char *location, MLErrorCode reason, const std::string_view &handling)
Tvec3< MLdouble > Vector3
A vector with three components of type double.
Definition: mlVector3.h:286
Tvec3< MLdouble > Vector3d
A vector with three components of type double.
Definition: mlVector3.h:282
Tvec3< MLldouble > Vector3ld
A vector with three components of type long double.
Definition: mlVector3.h:284
Tvec3< DT > operator^(const Tvec3< DT > &a, const Tvec3< DT2 > &b)
Returns a vector orthogonal to a and b.
Definition: mlVector3.h:268
Tvec3< MLfloat > Vector3f
A vector with three components of type float.
Definition: mlVector3.h:280