MeVisLab Toolbox 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 known whether vector or
53 // matrix header is included first and we cannot move template
54 // code into 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 
95  inline Tvec3(const DT px, const DT py, const DT pz)
96  {
97  Superclass::_buffer[0] = px;
98  Superclass::_buffer[1] = py;
99  Superclass::_buffer[2] = pz;
100  };
101 
103  inline Tvec3(const Tvec2<DT>& v, const DT pz)
104  {
105  Superclass::_buffer[0] = v[0];
106  Superclass::_buffer[1] = v[1];
107  Superclass::_buffer[2] = pz;
108  };
109 
115  Tvec3(const Tvec4<DT>& v, const bool normalizeV)
116  {
117  if (normalizeV){
118  // Homogeneous cast.
119  if (MLValuesDifferWOM(v[3], static_cast<DT>(0))){
120  DT divi = 1.0/v[3];
121  Superclass::_buffer[0] = v[0]*divi;
122  Superclass::_buffer[1] = v[1]*divi;
123  Superclass::_buffer[2] = v[2]*divi;
124  }
125  else{
126  // This must/should not occur.
127  printTemplateError("Tvec3::Tvec3(const Tvec4<DT>& v, const bool normalizeV)",
129  "Normalization with zero fourth component of homogeneous vector "
130  "performed. Leaving vector unchanged.");
131  }
132  }
133  else{
134  // Normal cast.
135  Superclass::_buffer[0] = v[0];
136  Superclass::_buffer[1] = v[1];
137  Superclass::_buffer[2] = v[2];
138  }
139  }
140 
144  Tvec3(const Tvec4<DT>& v, const int axis)
145  {
146  switch (axis) {
147  case 0:
148  Superclass::_buffer[0] = v[1];
149  Superclass::_buffer[1] = v[2];
150  Superclass::_buffer[2] = v[3];
151  break;
152  case 1:
153  Superclass::_buffer[0] = v[0];
154  Superclass::_buffer[1] = v[2];
155  Superclass::_buffer[2] = v[3];
156  break;
157  case 2:
158  Superclass::_buffer[0] = v[0];
159  Superclass::_buffer[1] = v[1];
160  Superclass::_buffer[2] = v[3];
161  break;
162  default:
163  Superclass::_buffer[0] = v[0];
164  Superclass::_buffer[1] = v[1];
165  Superclass::_buffer[2] = v[2];
166  break;
167  }
168  }
170 
172  inline void assign(const DT px, const DT py, const DT pz)
173  {
174  Superclass::_buffer[0] = px;
175  Superclass::_buffer[1] = py;
176  Superclass::_buffer[2] = pz;
177  }
178 
184  {
185  const DT lastComp = Superclass::_buffer[2];
186  ML_CHECK_FLOAT(lastComp);
187 
188  const DT div = static_cast<DT>(1) / lastComp;
189 
190  return Tvec3(Superclass::_buffer[0] * div,
191  Superclass::_buffer[1] * div,
192  1);
193  }
194 
197  inline Tvec4<DT> affineVec() const
198  {
199  return Tvec4<DT>(*this, 0);
200  }
201 
204  inline Tvec4<DT> affinePoint() const
205  {
206  return Tvec4<DT>(*this, 1);
207  }
208 
209 #if ML_DEPRECATED_SINCE(3,5,0)
214  inline ML_DEPRECATED void Set(const DT px, const DT py, const DT pz)
215  {
216  assign(px, py, pz);
217  }
219 #endif
220 
221 
222 }; // end of class *Tvec3*
223 
224 
225 //--------------------------------------------------------------------
228 //--------------------------------------------------------------------
229 template <class DT, class DT2>
230 inline Tvec3<DT> operator*(const Tmat3<DT>& a, const Tvec3<DT2>& v)
231 {
232  return Tvec3<DT>(a[0][0]*v[0] + a[0][1]*v[1] + a[0][2]*v[2],
233  a[1][0]*v[0] + a[1][1]*v[1] + a[1][2]*v[2],
234  a[2][0]*v[0] + a[2][1]*v[1] + a[2][2]*v[2]);
235 }
236 
237 //--------------------------------------------------------------------
240 //--------------------------------------------------------------------
241 template <class DT, class DT2>
242 inline Tvec3<DT> operator*(const Tvec3<DT>& v, const Tmat3<DT2>& a)
243 {
244  return a.transpose() * v;
245 }
246 
247 //--------------------------------------------------------------------
254 //--------------------------------------------------------------------
255 template <class DT, class DT2>
256 inline Tvec3<DT> operator*(const Tmat4<DT>& a, const Tvec3<DT2>& v)
257 {
258  // Normalize homogeneous 4d result to Tvec3 by dividing by the fourth component.
259  return Tvec3<DT>(a * v.affinePoint(), true);
260 }
261 
262 //--------------------------------------------------------------------
269 //--------------------------------------------------------------------
270 template <class DT, class DT2>
271 inline Tvec3<DT> operator*(const Tvec3<DT>& v, const Tmat4<DT2>& a)
272 {
273  // Note: This uses homogeneous "operator*(const Tmat4<DT>& a, const Tvec3<DT2>& v)"
274  // implemented above.
275  return a.transpose() * v;
276 }
277 
278 //--------------------------------------------------------------------
280 //--------------------------------------------------------------------
281 template <class DT, class DT2>
282 inline Tvec3<DT> operator^(const Tvec3<DT>& a, const Tvec3<DT2>& b)
283 {
284  return Tvec3<DT>(a[1]*b[2] - a[2]*b[1],
285  a[2]*b[0] - a[0]*b[2],
286  a[0]*b[1] - a[1]*b[0]);
287 }
288 
289 //-----------------------------------------------------------------------------------
292 //-----------------------------------------------------------------------------------
302 
303 
304 #if ML_DEPRECATED_SINCE(3,5,0)
309 ML_DEPRECATED typedef Tvec3<MLfloat> vecf3;
312 ML_DEPRECATED typedef Tvec3<MLdouble> vecd3;
315 ML_DEPRECATED typedef Tvec3<MLldouble> vecld3;
318 ML_DEPRECATED typedef Tvec3<MLdouble> vec3;
320 
321 #endif
322 
323 
324 ML_LA_END_NAMESPACE
325 
326 #endif //of __mlVector3_H
327 
328 
#define ML_DEPRECATED
Definition: CSOGroup.h:371
Template class for vector arithmetic with floating point data types.
T operator*(const FloatingPointVector< T, size, DataContainer > &a, const FloatingPointVector< T, size, DataContainer > &b)
Dot product, returns a.dot(b).
A three by three matrix class of three row vectors.
Definition: mlMatrix3.h:37
Tmat3 transpose() const
Returns the transpose of this matrix.
Definition: mlMatrix3.h:426
A four by four matrix class consisting of 4 row vectors.
Definition: mlMatrix4.h:36
Tmat4< DT > transpose() const
Returns the transposed *this.
Definition: mlMatrix4.h:584
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 las...
Definition: mlVector3.h:183
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 return it, i.e.
Definition: mlVector3.h:204
Tvec4< DT > affineVec() const
Builds a homogeneous Tvec4 vector from *this and return it, i.e.
Definition: mlVector3.h:197
Tvec3(const Tvec4< DT > &v, const bool normalizeV)
Casts a Tvec4 v to Tvec3.
Definition: mlVector3.h:115
void assign(const DT px, const DT py, const DT pz)
Sets all components to the passed values.
Definition: mlVector3.h:172
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:144
Tvec3(const DT px, const DT py, const DT pz)
Constructor building the vector x (first entry), y (second entry) and z (third entry).
Definition: mlVector3.h:95
Tvec3(const Tvec2< DT > &v, const DT pz)
Builds a Tvec3 from a Tvec2 and a scalar. Set third entry to user given value z.
Definition: mlVector3.h:103
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 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:925
#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 3 components of type double.
Definition: mlVector3.h:300
Tvec3< MLdouble > Vector3d
A vector with 3 components of type double.
Definition: mlVector3.h:296
Tvec3< MLldouble > Vector3ld
A vector with 3 components of type long double.
Definition: mlVector3.h:298
Tvec3< DT > operator^(const Tvec3< DT > &a, const Tvec3< DT2 > &b)
Returns a vector orthogonal to a and b.
Definition: mlVector3.h:282
Tvec3< MLfloat > Vector3f
A vector with 3 components of type float.
Definition: mlVector3.h:294