MeVisLab Toolbox Reference
Loading...
Searching...
No Matches
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.
30
32template <class DT> class Vector3DataContainer
33{
34public:
35 union
36 {
37 struct
38 {
40 DT x;
42 DT y;
44 DT z;
45 };
47 DT _buffer[3];
48 };
49};
50
51//--------------------------------------------------------------------
53// This is necessary, because we do not know whether vector or
54// matrix header is included first and we cannot move template
55// code into the C++ file.
56//--------------------------------------------------------------------
57template <class DT> class Tvec4;
58template <class DT> class Tmat3;
59template <class DT> class Tmat4;
61
62//--------------------------------------------------------------------
64//--------------------------------------------------------------------
65template <class DT> class Tvec3 : public FloatingPointVector<DT, 3, Vector3DataContainer<DT>>
66{
67
68public:
71
73 typedef DT ComponentType;
74
75 //--------------------------------------------------------------------
78 //--------------------------------------------------------------------
81 inline explicit Tvec3(const DT value = 0)
82 : FloatingPointVector<DT, 3, Vector3DataContainer<DT>>(value)
83 {}
84
88 inline Tvec3(const Superclass& v)
89 : Superclass(v)
90 {}
91
93 inline Tvec3(const DT px, const DT py, const DT pz)
94 {
95 Superclass::_buffer[0] = px;
96 Superclass::_buffer[1] = py;
97 Superclass::_buffer[2] = pz;
98 };
99
101 inline Tvec3(const Tvec2<DT>& v, const DT pz)
102 {
103 Superclass::_buffer[0] = v[0];
104 Superclass::_buffer[1] = v[1];
105 Superclass::_buffer[2] = pz;
106 };
107
113 Tvec3(const Tvec4<DT>& v, const bool normalizeV)
114 {
115 if (normalizeV) {
116 // Homogeneous cast.
117 if (MLValuesDifferWOM(v[3], static_cast<DT>(0))) {
118 DT divi = 1.0 / v[3];
119 Superclass::_buffer[0] = v[0] * divi;
120 Superclass::_buffer[1] = v[1] * divi;
121 Superclass::_buffer[2] = v[2] * divi;
122 }
123 else {
124 // This must/should not occur.
125 printTemplateError("Tvec3::Tvec3(const Tvec4<DT>& v, const bool normalizeV)", ML_BAD_PARAMETER,
126 "Normalization with zero fourth component of homogeneous vector "
127 "performed. Leaving vector unchanged.");
128 }
129 }
130 else {
131 // Normal cast.
132 Superclass::_buffer[0] = v[0];
133 Superclass::_buffer[1] = v[1];
134 Superclass::_buffer[2] = v[2];
135 }
136 }
137
141 Tvec3(const Tvec4<DT>& v, const int axis)
142 {
143 switch (axis) {
144 case 0:
145 Superclass::_buffer[0] = v[1];
146 Superclass::_buffer[1] = v[2];
147 Superclass::_buffer[2] = v[3];
148 break;
149 case 1:
150 Superclass::_buffer[0] = v[0];
151 Superclass::_buffer[1] = v[2];
152 Superclass::_buffer[2] = v[3];
153 break;
154 case 2:
155 Superclass::_buffer[0] = v[0];
156 Superclass::_buffer[1] = v[1];
157 Superclass::_buffer[2] = v[3];
158 break;
159 default:
160 Superclass::_buffer[0] = v[0];
161 Superclass::_buffer[1] = v[1];
162 Superclass::_buffer[2] = v[2];
163 break;
164 }
165 }
167
169 inline void assign(const DT px, const DT py, const DT pz)
170 {
171 Superclass::_buffer[0] = px;
172 Superclass::_buffer[1] = py;
173 Superclass::_buffer[2] = pz;
174 }
175
181 {
182 const DT lastComp = Superclass::_buffer[2];
184
185 const DT div = static_cast<DT>(1) / lastComp;
186
187 return Tvec3(Superclass::_buffer[0] * div, Superclass::_buffer[1] * div, 1);
188 }
189
192 inline Tvec4<DT> affineVec() const
193 {
194 return Tvec4<DT>(*this, 0);
195 }
196
199 inline Tvec4<DT> affinePoint() const
200 {
201 return Tvec4<DT>(*this, 1);
202 }
203
204}; // end of class *Tvec3*
205
206
207//--------------------------------------------------------------------
210//--------------------------------------------------------------------
211template <class DT, class DT2> inline Tvec3<DT> operator*(const Tmat3<DT>& a, const Tvec3<DT2>& v)
212{
213 return Tvec3<DT>(a[0][0] * v[0] + a[0][1] * v[1] + a[0][2] * v[2], a[1][0] * v[0] + a[1][1] * v[1] + a[1][2] * v[2],
214 a[2][0] * v[0] + a[2][1] * v[1] + a[2][2] * v[2]);
215}
216
217//--------------------------------------------------------------------
220//--------------------------------------------------------------------
221template <class DT, class DT2> inline Tvec3<DT> operator*(const Tvec3<DT>& v, const Tmat3<DT2>& a)
222{
223 return a.transpose() * v;
224}
225
226//--------------------------------------------------------------------
233//--------------------------------------------------------------------
234template <class DT, class DT2> inline Tvec3<DT> operator*(const Tmat4<DT>& a, const Tvec3<DT2>& v)
235{
236 // Normalize homogeneous 4D result to Tvec3 by dividing by the fourth component.
237 return Tvec3<DT>(a * v.affinePoint(), true);
238}
239
240//--------------------------------------------------------------------
247//--------------------------------------------------------------------
248template <class DT, class DT2> inline Tvec3<DT> operator*(const Tvec3<DT>& v, const Tmat4<DT2>& a)
249{
250 // Note: This uses homogeneous "operator*(const Tmat4<DT>& a, const Tvec3<DT2>& v)"
251 // implemented above.
252 return a.transpose() * v;
253}
254
255//--------------------------------------------------------------------
257//--------------------------------------------------------------------
258template <class DT, class DT2> inline Tvec3<DT> operator^(const Tvec3<DT>& a, const Tvec3<DT2>& b)
259{
260 return Tvec3<DT>(a[1] * b[2] - a[2] * b[1], a[2] * b[0] - a[0] * b[2], a[0] * b[1] - a[1] * b[0]);
261}
262
263//-----------------------------------------------------------------------------------
266//-----------------------------------------------------------------------------------
276
278
279#endif // of __mlVector3_H
Template class for vector arithmetic with floating point datatypes.
A 3x3 matrix class of three row vectors.
Definition mlMatrix3.h:36
Tmat3 transpose() const
Returns the transpose of this matrix.
Definition mlMatrix3.h:431
A 4x4 matrix class consisting of four row vectors.
Definition mlMatrix4.h:35
Tmat4< DT > transpose() const
Returns the transposed *this.
Definition mlMatrix4.h:598
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:73
FloatingPointVector< DT, 3, Vector3DataContainer< DT > > Superclass
A typedef as a shorthand for the base class.
Definition mlVector3.h:70
Tvec3(const Tvec4< DT > &v, const bool normalizeV)
Casts a Tvec4 v to Tvec3.
Definition mlVector3.h:113
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:180
void assign(const DT px, const DT py, const DT pz)
Sets all components to the passed values.
Definition mlVector3.h:169
Tvec3(const DT value=0)
Default and value constructor.
Definition mlVector3.h:81
Tvec3(const Superclass &v)
Copy constructor from FloatingPointVector.
Definition mlVector3.h:88
Tvec3(const Tvec4< DT > &v, const int axis)
Casts a Tvec4 v to Tvec3.
Definition mlVector3.h:141
Tvec3(const DT px, const DT py, const DT pz)
Constructor building the vector px, py, and pz.
Definition mlVector3.h:93
Tvec4< DT > affineVec() const
Builds a homogeneous Tvec4 vector from *this and returns it, i.e., leaves all components unchanged an...
Definition mlVector3.h:192
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:101
Tvec4< DT > affinePoint() const
Builds a homogeneous Tvec4 point from *this and returns it, i.e., leaves all components unchanged and...
Definition mlVector3.h:199
Forward declarations to resolve header file dependencies.
Definition mlVector4.h:49
Specialized base class for the FloatingPointVectorDataContainerBase.
Definition mlVector3.h:33
DT x
X-component of the vector, same as _buffer[0].
Definition mlVector3.h:40
DT z
Z-component of the vector, same as _buffer[2].
Definition mlVector3.h:44
DT y
Y-component of the vector, same as _buffer[1].
Definition mlVector3.h:42
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:879
#define ML_CHECK_FLOAT(x)
Target mlrange_cast(Source arg)
Generic version of checked ML casts.
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:258