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.
20#include "mlLinearAlgebraDefs.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.
29ML_LA_START_NAMESPACE
30
32template <class DT>
34{
35public:
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//--------------------------------------------------------------------
56template <class DT> class Tvec4;
57template <class DT> class Tmat3;
58template <class DT> class Tmat4;
60
61//--------------------------------------------------------------------
63//--------------------------------------------------------------------
64template <class DT>
65class Tvec3 : public FloatingPointVector<DT, 3, Vector3DataContainer<DT> >
66{
67
68public:
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}; // end of class *Tvec3*
210
211
212//--------------------------------------------------------------------
215//--------------------------------------------------------------------
216template <class DT, class DT2>
217inline Tvec3<DT> operator*(const Tmat3<DT>& a, const Tvec3<DT2>& v)
218{
219 return Tvec3<DT>(a[0][0]*v[0] + a[0][1]*v[1] + a[0][2]*v[2],
220 a[1][0]*v[0] + a[1][1]*v[1] + a[1][2]*v[2],
221 a[2][0]*v[0] + a[2][1]*v[1] + a[2][2]*v[2]);
222}
223
224//--------------------------------------------------------------------
227//--------------------------------------------------------------------
228template <class DT, class DT2>
229inline Tvec3<DT> operator*(const Tvec3<DT>& v, const Tmat3<DT2>& a)
230{
231 return a.transpose() * v;
232}
233
234//--------------------------------------------------------------------
241//--------------------------------------------------------------------
242template <class DT, class DT2>
243inline Tvec3<DT> operator*(const Tmat4<DT>& a, const Tvec3<DT2>& v)
244{
245 // Normalize homogeneous 4d result to Tvec3 by dividing by the fourth component.
246 return Tvec3<DT>(a * v.affinePoint(), true);
247}
248
249//--------------------------------------------------------------------
256//--------------------------------------------------------------------
257template <class DT, class DT2>
258inline Tvec3<DT> operator*(const Tvec3<DT>& v, const Tmat4<DT2>& a)
259{
260 // Note: This uses homogeneous "operator*(const Tmat4<DT>& a, const Tvec3<DT2>& v)"
261 // implemented above.
262 return a.transpose() * v;
263}
264
265//--------------------------------------------------------------------
267//--------------------------------------------------------------------
268template <class DT, class DT2>
269inline Tvec3<DT> operator^(const Tvec3<DT>& a, const Tvec3<DT2>& b)
270{
271 return Tvec3<DT>(a[1]*b[2] - a[2]*b[1],
272 a[2]*b[0] - a[0]*b[2],
273 a[0]*b[1] - a[1]*b[0]);
274}
275
276//-----------------------------------------------------------------------------------
279//-----------------------------------------------------------------------------------
289
290ML_LA_END_NAMESPACE
291
292#endif //of __mlVector3_H
293
294
Template class for vector arithmetic with floating point data types.
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: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
FloatingPointVector< DT, 3, Vector3DataContainer< DT > > Superclass
A typedef as a shorthand for the base class.
Definition mlVector3.h:71
Tvec3(const Tvec4< DT > &v, const bool normalizeV)
Casts a Tvec4 v to Tvec3.
Definition mlVector3.h:115
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
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
Tvec4< DT > affineVec() const
Builds a homogeneous Tvec4 vector from *this and return it, i.e.
Definition mlVector3.h:197
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
Tvec4< DT > affinePoint() const
Builds a homogeneous Tvec4 point from *this and return it, i.e.
Definition mlVector3.h:204
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:823
#define ML_CHECK_FLOAT(x)
void ML_UTILS_EXPORT printTemplateError(const char *location, MLErrorCode reason, const std::string_view &handling)
T operator*(const FloatingPointVector< T, size, DataContainer > &a, const FloatingPointVector< T, size, DataContainer > &b)
Dot product, returns a.dot(b).
Tvec3< DT > operator^(const Tvec3< DT > &a, const Tvec3< DT2 > &b)
Returns a vector orthogonal to a and b.
Definition mlVector3.h:269