VTK  9.3.0
vtkVector.h
Go to the documentation of this file.
1// SPDX-FileCopyrightText: Copyright (c) Ken Martin, Will Schroeder, Bill Lorensen
2// SPDX-License-Identifier: BSD-3-Clause
3
19#ifndef vtkVector_h
20#define vtkVector_h
21
22#include "vtkObject.h" // for legacy macros
23#include "vtkTuple.h"
24
25#include <cmath> // For math functions
26
27VTK_ABI_NAMESPACE_BEGIN
28template <typename T, int Size>
29class vtkVector : public vtkTuple<T, Size>
30{
31public:
32 vtkVector() = default;
33
37 explicit vtkVector(const T& scalar)
38 : vtkTuple<T, Size>(scalar)
39 {
40 }
41
47 explicit vtkVector(const T* init)
48 : vtkTuple<T, Size>(init)
49 {
50 }
51
53
56 T SquaredNorm() const
57 {
58 T result = 0;
59 for (int i = 0; i < Size; ++i)
60 {
61 result += this->Data[i] * this->Data[i];
62 }
63 return result;
64 }
66
70 double Norm() const { return sqrt(static_cast<double>(this->SquaredNorm())); }
71
73
77 double Normalize()
78 {
79 const double norm(this->Norm());
80 if (norm == 0.0)
81 {
82 return 0.0;
83 }
84 const double inv(1.0 / norm);
85 for (int i = 0; i < Size; ++i)
86 {
87 this->Data[i] = static_cast<T>(this->Data[i] * inv);
88 }
89 return norm;
90 }
92
94
99 {
100 vtkVector<T, Size> temp(*this);
101 temp.Normalize();
102 return temp;
103 }
105
107
110 T Dot(const vtkVector<T, Size>& other) const
111 {
112 T result(0);
113 for (int i = 0; i < Size; ++i)
114 {
115 result += this->Data[i] * other[i];
116 }
117 return result;
118 }
120
122
125 template <typename TR>
127 {
128 vtkVector<TR, Size> result;
129 for (int i = 0; i < Size; ++i)
130 {
131 result[i] = static_cast<TR>(this->Data[i]);
132 }
133 return result;
134 }
136};
137
138// .NAME vtkVector2 - templated base type for storage of 2D vectors.
139//
140template <typename T>
141class vtkVector2 : public vtkVector<T, 2>
142{
143public:
144 vtkVector2() = default;
145
146 explicit vtkVector2(const T& scalar)
147 : vtkVector<T, 2>(scalar)
148 {
149 }
150
151 explicit vtkVector2(const T* init)
152 : vtkVector<T, 2>(init)
153 {
154 }
155
156 vtkVector2(const T& x, const T& y)
157 {
158 this->Data[0] = x;
159 this->Data[1] = y;
160 }
161
163
166 void Set(const T& x, const T& y)
167 {
168 this->Data[0] = x;
169 this->Data[1] = y;
170 }
172
176 void SetX(const T& x) { this->Data[0] = x; }
177
181 const T& GetX() const { return this->Data[0]; }
182
186 void SetY(const T& y) { this->Data[1] = y; }
187
191 const T& GetY() const { return this->Data[1]; }
192
194
197 bool operator<(const vtkVector2<T>& v) const
198 {
199 return (this->Data[0] < v.Data[0]) || (this->Data[0] == v.Data[0] && this->Data[1] < v.Data[1]);
200 }
202};
203
204// .NAME vtkVector3 - templated base type for storage of 3D vectors.
205//
206template <typename T>
207class vtkVector3 : public vtkVector<T, 3>
208{
209public:
210 vtkVector3() = default;
211
212 explicit vtkVector3(const T& scalar)
213 : vtkVector<T, 3>(scalar)
214 {
215 }
216
217 explicit vtkVector3(const T* init)
218 : vtkVector<T, 3>(init)
219 {
220 }
221
222 vtkVector3(const T& x, const T& y, const T& z)
223 {
224 this->Data[0] = x;
225 this->Data[1] = y;
226 this->Data[2] = z;
227 }
228
230
233 void Set(const T& x, const T& y, const T& z)
234 {
235 this->Data[0] = x;
236 this->Data[1] = y;
237 this->Data[2] = z;
238 }
240
244 void SetX(const T& x) { this->Data[0] = x; }
245
249 const T& GetX() const { return this->Data[0]; }
250
254 void SetY(const T& y) { this->Data[1] = y; }
255
259 const T& GetY() const { return this->Data[1]; }
260
264 void SetZ(const T& z) { this->Data[2] = z; }
265
269 const T& GetZ() const { return this->Data[2]; }
270
272
276 {
277 vtkVector3<T> res;
278 res[0] = this->Data[1] * other.Data[2] - this->Data[2] * other.Data[1];
279 res[1] = this->Data[2] * other.Data[0] - this->Data[0] * other.Data[2];
280 res[2] = this->Data[0] * other.Data[1] - this->Data[1] * other.Data[0];
281 return res;
282 }
284
286
289 bool operator<(const vtkVector3<T>& v) const
290 {
291 return (this->Data[0] < v.Data[0]) ||
292 (this->Data[0] == v.Data[0] && this->Data[1] < v.Data[1]) ||
293 (this->Data[0] == v.Data[0] && this->Data[1] == v.Data[1] && this->Data[2] < v.Data[2]);
294 }
296};
297
298// .NAME vtkVector4 - templated base type for storage of 4D vectors.
299//
300template <typename T>
301class vtkVector4 : public vtkVector<T, 4>
302{
303public:
304 vtkVector4() = default;
305
306 explicit vtkVector4(const T& scalar)
307 : vtkVector<T, 4>(scalar)
308 {
309 }
310
311 explicit vtkVector4(const T* init)
312 : vtkVector<T, 4>(init)
313 {
314 }
315
316 vtkVector4(const T& x, const T& y, const T& z, const T& w)
317 {
318 this->Data[0] = x;
319 this->Data[1] = y;
320 this->Data[2] = z;
321 this->Data[3] = w;
322 }
323
325
328 void Set(const T& x, const T& y, const T& z, const T& w)
329 {
330 this->Data[0] = x;
331 this->Data[1] = y;
332 this->Data[2] = z;
333 this->Data[3] = w;
334 }
336
340 void SetX(const T& x) { this->Data[0] = x; }
341
345 const T& GetX() const { return this->Data[0]; }
346
350 void SetY(const T& y) { this->Data[1] = y; }
351
355 const T& GetY() const { return this->Data[1]; }
356
360 void SetZ(const T& z) { this->Data[2] = z; }
361
365 const T& GetZ() const { return this->Data[2]; }
366
370 void SetW(const T& w) { this->Data[3] = w; }
371
375 const T& GetW() const { return this->Data[3]; }
376};
377
381#define vtkVectorNormalized(vectorType, type, size) \
382 vectorType Normalized() const \
383 { \
384 return vectorType(vtkVector<type, size>::Normalized().GetData()); \
385 }
386
387#define vtkVectorDerivedMacro(vectorType, type, size) \
388 vtkVectorNormalized(vectorType, type, size); \
389 explicit vectorType(type s) \
390 : Superclass(s) \
391 { \
392 } \
393 explicit vectorType(const type* i) \
394 : Superclass(i) \
395 { \
396 } \
397 explicit vectorType(const vtkTuple<type, size>& o) \
398 : Superclass(o.GetData()) \
399 { \
400 } \
401 vectorType(const vtkVector<type, size>& o) \
402 : Superclass(o.GetData()) \
403 { \
404 }
405
407
410class vtkVector2i : public vtkVector2<int>
411{
412public:
414 vtkVector2i() = default;
415 vtkVector2i(int x, int y)
416 : vtkVector2<int>(x, y)
417 {
418 }
420};
422
423class vtkVector2f : public vtkVector2<float>
424{
425public:
427 vtkVector2f() = default;
428 vtkVector2f(float x, float y)
429 : vtkVector2<float>(x, y)
430 {
431 }
433};
434
435class vtkVector2d : public vtkVector2<double>
436{
437public:
439 vtkVector2d() = default;
440 vtkVector2d(double x, double y)
441 : vtkVector2<double>(x, y)
442 {
443 }
445};
446
447#define vtkVector3Cross(vectorType, type) \
448 vectorType Cross(const vectorType& other) const \
449 { \
450 return vectorType(vtkVector3<type>::Cross(other).GetData()); \
451 }
452
453class vtkVector3i : public vtkVector3<int>
454{
455public:
457 vtkVector3i() = default;
458 vtkVector3i(int x, int y, int z)
459 : vtkVector3<int>(x, y, z)
460 {
461 }
464};
465
466class vtkVector3f : public vtkVector3<float>
467{
468public:
470 vtkVector3f() = default;
471 vtkVector3f(float x, float y, float z)
472 : vtkVector3<float>(x, y, z)
473 {
474 }
477};
478
479class vtkVector3d : public vtkVector3<double>
480{
481public:
483 vtkVector3d() = default;
484 vtkVector3d(double x, double y, double z)
485 : vtkVector3<double>(x, y, z)
486 {
487 }
490};
491
492class vtkVector4i : public vtkVector4<int>
493{
494public:
496 vtkVector4i() = default;
497 vtkVector4i(int x, int y, int z, int w)
498 : vtkVector4<int>(x, y, z, w)
499 {
500 }
502};
503
504class vtkVector4d : public vtkVector4<double>
505{
506public:
508 vtkVector4d() = default;
509 vtkVector4d(double x, double y, double z, double w)
510 : vtkVector4<double>(x, y, z, w){};
512};
513
514VTK_ABI_NAMESPACE_END
515#endif // vtkVector_h
516// VTK-HeaderTest-Exclude: vtkVector.h
templated base type for containers of constant size.
Definition vtkTuple.h:27
T Data[Size]
The only thing stored in memory!
Definition vtkTuple.h:143
const T & GetX() const
Get the x component of the vector, i.e.
Definition vtkVector.h:181
const T & GetY() const
Get the y component of the vector, i.e.
Definition vtkVector.h:191
void Set(const T &x, const T &y)
Set the x and y components of the vector.
Definition vtkVector.h:166
vtkVector2(const T *init)
Definition vtkVector.h:151
void SetY(const T &y)
Set the y component of the vector, i.e.
Definition vtkVector.h:186
vtkVector2(const T &x, const T &y)
Definition vtkVector.h:156
vtkVector2(const T &scalar)
Definition vtkVector.h:146
void SetX(const T &x)
Set the x component of the vector, i.e.
Definition vtkVector.h:176
bool operator<(const vtkVector2< T > &v) const
Lexicographical comparison of two vector.
Definition vtkVector.h:197
vtkVector2()=default
vtkVector2d()=default
vtkVectorDerivedMacro(vtkVector2d, double, 2)
vtkVector2d(double x, double y)
Definition vtkVector.h:440
vtkVector2< double > Superclass
Definition vtkVector.h:438
vtkVector2f()=default
vtkVector2< float > Superclass
Definition vtkVector.h:426
vtkVector2f(float x, float y)
Definition vtkVector.h:428
vtkVectorDerivedMacro(vtkVector2f, float, 2)
Some derived classes for the different vectors commonly used.
Definition vtkVector.h:411
vtkVector2i()=default
vtkVector2i(int x, int y)
Definition vtkVector.h:415
vtkVector2< int > Superclass
Definition vtkVector.h:413
vtkVectorDerivedMacro(vtkVector2i, int, 2)
void SetZ(const T &z)
Set the z component of the vector, i.e.
Definition vtkVector.h:264
const T & GetY() const
Get the y component of the vector, i.e.
Definition vtkVector.h:259
bool operator<(const vtkVector3< T > &v) const
Lexicographical comparison of two vector.
Definition vtkVector.h:289
vtkVector3(const T *init)
Definition vtkVector.h:217
const T & GetZ() const
Get the z component of the vector, i.e.
Definition vtkVector.h:269
void SetX(const T &x)
Set the x component of the vector, i.e.
Definition vtkVector.h:244
vtkVector3(const T &scalar)
Definition vtkVector.h:212
void Set(const T &x, const T &y, const T &z)
Set the x, y and z components of the vector.
Definition vtkVector.h:233
vtkVector3(const T &x, const T &y, const T &z)
Definition vtkVector.h:222
vtkVector3< T > Cross(const vtkVector3< T > &other) const
Return the cross product of this X other.
Definition vtkVector.h:275
const T & GetX() const
Get the x component of the vector, i.e.
Definition vtkVector.h:249
void SetY(const T &y)
Set the y component of the vector, i.e.
Definition vtkVector.h:254
vtkVector3()=default
vtkVector3Cross(vtkVector3d, double)
vtkVector3< double > Superclass
Definition vtkVector.h:482
vtkVector3d(double x, double y, double z)
Definition vtkVector.h:484
vtkVector3d()=default
vtkVectorDerivedMacro(vtkVector3d, double, 3)
vtkVector3Cross(vtkVector3f, float)
vtkVector3f()=default
vtkVectorDerivedMacro(vtkVector3f, float, 3)
vtkVector3f(float x, float y, float z)
Definition vtkVector.h:471
vtkVector3< float > Superclass
Definition vtkVector.h:469
vtkVector3Cross(vtkVector3i, int)
vtkVectorDerivedMacro(vtkVector3i, int, 3)
vtkVector3< int > Superclass
Definition vtkVector.h:456
vtkVector3i()=default
vtkVector3i(int x, int y, int z)
Definition vtkVector.h:458
void SetY(const T &y)
Set the y component of the vector, i.e.
Definition vtkVector.h:350
vtkVector4(const T &x, const T &y, const T &z, const T &w)
Definition vtkVector.h:316
void SetZ(const T &z)
Set the z component of the vector, i.e.
Definition vtkVector.h:360
const T & GetZ() const
Get the z component of the vector, i.e.
Definition vtkVector.h:365
void SetW(const T &w)
Set the w component of the vector, i.e.
Definition vtkVector.h:370
vtkVector4(const T *init)
Definition vtkVector.h:311
void Set(const T &x, const T &y, const T &z, const T &w)
Set the x, y, z and w components of a 3D vector in homogeneous coordinates.
Definition vtkVector.h:328
vtkVector4()=default
const T & GetY() const
Get the y component of the vector, i.e.
Definition vtkVector.h:355
void SetX(const T &x)
Set the x component of the vector, i.e.
Definition vtkVector.h:340
vtkVector4(const T &scalar)
Definition vtkVector.h:306
const T & GetX() const
Get the x component of the vector, i.e.
Definition vtkVector.h:345
const T & GetW() const
Get the w component of the vector, i.e.
Definition vtkVector.h:375
vtkVectorDerivedMacro(vtkVector4d, double, 4)
vtkVector4d(double x, double y, double z, double w)
Definition vtkVector.h:509
vtkVector4d()=default
vtkVector4< int > Superclass
Definition vtkVector.h:495
vtkVector4i(int x, int y, int z, int w)
Definition vtkVector.h:497
vtkVector4i()=default
vtkVectorDerivedMacro(vtkVector4i, int, 4)
templated base type for storage of vectors.
Definition vtkVector.h:30
vtkVector(const T &scalar)
Initialize all of the vector's elements with the supplied scalar.
Definition vtkVector.h:37
T Dot(const vtkVector< T, Size > &other) const
The dot product of this and the supplied vector.
Definition vtkVector.h:110
double Normalize()
Normalize the vector in place.
Definition vtkVector.h:77
vtkVector()=default
vtkVector(const T *init)
Initialize the vector's elements with the elements of the supplied array.
Definition vtkVector.h:47
vtkVector< TR, Size > Cast() const
Cast the vector to the specified type, returning the result.
Definition vtkVector.h:126
double Norm() const
Get the norm of the vector, i.e.
Definition vtkVector.h:70
vtkVector< T, Size > Normalized() const
Return the normalized form of this vector.
Definition vtkVector.h:98
T SquaredNorm() const
Get the squared norm of the vector.
Definition vtkVector.h:56