VTK  9.4.20241221
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
48#ifndef vtkVector_h
49#define vtkVector_h
50
51#include "vtkObject.h" // for legacy macros
52#include "vtkTuple.h"
53
54#include <cmath> // For math functions
55
56VTK_ABI_NAMESPACE_BEGIN
57template <typename T, int Size>
58class vtkVector : public vtkTuple<T, Size>
59{
60public:
61 vtkVector() = default;
62
66 explicit vtkVector(const T& scalar)
67 : vtkTuple<T, Size>(scalar)
68 {
69 }
70
76 explicit vtkVector(const T* init)
77 : vtkTuple<T, Size>(init)
78 {
79 }
80
82
85 T SquaredNorm() const
86 {
87 T result = 0;
88 for (int i = 0; i < Size; ++i)
89 {
90 result += this->Data[i] * this->Data[i];
91 }
92 return result;
93 }
95
99 double Norm() const { return sqrt(static_cast<double>(this->SquaredNorm())); }
100
102
106 double Normalize()
107 {
108 const double norm(this->Norm());
109 if (norm == 0.0)
110 {
111 return 0.0;
112 }
113 const double inv(1.0 / norm);
114 for (int i = 0; i < Size; ++i)
115 {
116 this->Data[i] = static_cast<T>(this->Data[i] * inv);
117 }
118 return norm;
119 }
121
123
128 {
129 vtkVector<T, Size> temp(*this);
130 temp.Normalize();
131 return temp;
132 }
134
136
139 T Dot(const vtkVector<T, Size>& other) const
140 {
141 T result(0);
142 for (int i = 0; i < Size; ++i)
143 {
144 result += this->Data[i] * other[i];
145 }
146 return result;
147 }
149
151
154 template <typename TR>
156 {
157 vtkVector<TR, Size> result;
158 for (int i = 0; i < Size; ++i)
159 {
160 result[i] = static_cast<TR>(this->Data[i]);
161 }
162 return result;
163 }
165};
166
167// .NAME vtkVector2 - templated base type for storage of 2D vectors.
168//
169template <typename T>
170class vtkVector2 : public vtkVector<T, 2>
171{
172public:
173 vtkVector2() = default;
174
175 explicit vtkVector2(const T& scalar)
176 : vtkVector<T, 2>(scalar)
177 {
178 }
179
180 explicit vtkVector2(const T* init)
181 : vtkVector<T, 2>(init)
182 {
183 }
184
185 vtkVector2(const T& x, const T& y)
186 {
187 this->Data[0] = x;
188 this->Data[1] = y;
189 }
190
192
195 void Set(const T& x, const T& y)
196 {
197 this->Data[0] = x;
198 this->Data[1] = y;
199 }
201
205 void SetX(const T& x) { this->Data[0] = x; }
206
210 const T& GetX() const { return this->Data[0]; }
211
215 void SetY(const T& y) { this->Data[1] = y; }
216
220 const T& GetY() const { return this->Data[1]; }
221
223
226 bool operator<(const vtkVector2<T>& v) const
227 {
228 return (this->Data[0] < v.Data[0]) || (this->Data[0] == v.Data[0] && this->Data[1] < v.Data[1]);
229 }
231};
232
233// .NAME vtkVector3 - templated base type for storage of 3D vectors.
234//
235template <typename T>
236class vtkVector3 : public vtkVector<T, 3>
237{
238public:
239 vtkVector3() = default;
240
241 explicit vtkVector3(const T& scalar)
242 : vtkVector<T, 3>(scalar)
243 {
244 }
245
246 explicit vtkVector3(const T* init)
247 : vtkVector<T, 3>(init)
248 {
249 }
250
251 vtkVector3(const T& x, const T& y, const T& z)
252 {
253 this->Data[0] = x;
254 this->Data[1] = y;
255 this->Data[2] = z;
256 }
257
259
262 void Set(const T& x, const T& y, const T& z)
263 {
264 this->Data[0] = x;
265 this->Data[1] = y;
266 this->Data[2] = z;
267 }
269
273 void SetX(const T& x) { this->Data[0] = x; }
274
278 const T& GetX() const { return this->Data[0]; }
279
283 void SetY(const T& y) { this->Data[1] = y; }
284
288 const T& GetY() const { return this->Data[1]; }
289
293 void SetZ(const T& z) { this->Data[2] = z; }
294
298 const T& GetZ() const { return this->Data[2]; }
299
301
305 {
306 vtkVector3<T> res;
307 res[0] = this->Data[1] * other.Data[2] - this->Data[2] * other.Data[1];
308 res[1] = this->Data[2] * other.Data[0] - this->Data[0] * other.Data[2];
309 res[2] = this->Data[0] * other.Data[1] - this->Data[1] * other.Data[0];
310 return res;
311 }
313
315
318 bool operator<(const vtkVector3<T>& v) const
319 {
320 return (this->Data[0] < v.Data[0]) ||
321 (this->Data[0] == v.Data[0] && this->Data[1] < v.Data[1]) ||
322 (this->Data[0] == v.Data[0] && this->Data[1] == v.Data[1] && this->Data[2] < v.Data[2]);
323 }
325};
326
327// .NAME vtkVector4 - templated base type for storage of 4D vectors.
328//
329template <typename T>
330class vtkVector4 : public vtkVector<T, 4>
331{
332public:
333 vtkVector4() = default;
334
335 explicit vtkVector4(const T& scalar)
336 : vtkVector<T, 4>(scalar)
337 {
338 }
339
340 explicit vtkVector4(const T* init)
341 : vtkVector<T, 4>(init)
342 {
343 }
344
345 vtkVector4(const T& x, const T& y, const T& z, const T& w)
346 {
347 this->Data[0] = x;
348 this->Data[1] = y;
349 this->Data[2] = z;
350 this->Data[3] = w;
351 }
352
354
357 void Set(const T& x, const T& y, const T& z, const T& w)
358 {
359 this->Data[0] = x;
360 this->Data[1] = y;
361 this->Data[2] = z;
362 this->Data[3] = w;
363 }
365
369 void SetX(const T& x) { this->Data[0] = x; }
370
374 const T& GetX() const { return this->Data[0]; }
375
379 void SetY(const T& y) { this->Data[1] = y; }
380
384 const T& GetY() const { return this->Data[1]; }
385
389 void SetZ(const T& z) { this->Data[2] = z; }
390
394 const T& GetZ() const { return this->Data[2]; }
395
399 void SetW(const T& w) { this->Data[3] = w; }
400
404 const T& GetW() const { return this->Data[3]; }
405};
406
410#define vtkVectorNormalized(vectorType, type, size) \
411 vectorType Normalized() const \
412 { \
413 return vectorType(vtkVector<type, size>::Normalized().GetData()); \
414 }
415
416#define vtkVectorDerivedMacro(vectorType, type, size) \
417 vtkVectorNormalized(vectorType, type, size); \
418 explicit vectorType(type s) \
419 : Superclass(s) \
420 { \
421 } \
422 explicit vectorType(const type* i) \
423 : Superclass(i) \
424 { \
425 } \
426 explicit vectorType(const vtkTuple<type, size>& o) \
427 : Superclass(o.GetData()) \
428 { \
429 } \
430 vectorType(const vtkVector<type, size>& o) \
431 : Superclass(o.GetData()) \
432 { \
433 }
434
436
439class vtkVector2i : public vtkVector2<int>
440{
441public:
443 vtkVector2i() = default;
444 vtkVector2i(int x, int y)
445 : vtkVector2<int>(x, y)
446 {
447 }
449};
451
452class vtkVector2f : public vtkVector2<float>
453{
454public:
456 vtkVector2f() = default;
457 vtkVector2f(float x, float y)
458 : vtkVector2<float>(x, y)
459 {
460 }
462};
463
464class vtkVector2d : public vtkVector2<double>
465{
466public:
468 vtkVector2d() = default;
469 vtkVector2d(double x, double y)
470 : vtkVector2<double>(x, y)
471 {
472 }
474};
475
476#define vtkVector3Cross(vectorType, type) \
477 vectorType Cross(const vectorType& other) const \
478 { \
479 return vectorType(vtkVector3<type>::Cross(other).GetData()); \
480 }
481
482class vtkVector3i : public vtkVector3<int>
483{
484public:
486 vtkVector3i() = default;
487 vtkVector3i(int x, int y, int z)
488 : vtkVector3<int>(x, y, z)
489 {
490 }
493};
494
495class vtkVector3f : public vtkVector3<float>
496{
497public:
499 vtkVector3f() = default;
500 vtkVector3f(float x, float y, float z)
501 : vtkVector3<float>(x, y, z)
502 {
503 }
506};
507
508class vtkVector3d : public vtkVector3<double>
509{
510public:
512 vtkVector3d() = default;
513 vtkVector3d(double x, double y, double z)
514 : vtkVector3<double>(x, y, z)
515 {
516 }
519};
520
521class vtkVector4i : public vtkVector4<int>
522{
523public:
525 vtkVector4i() = default;
526 vtkVector4i(int x, int y, int z, int w)
527 : vtkVector4<int>(x, y, z, w)
528 {
529 }
531};
532
533class vtkVector4d : public vtkVector4<double>
534{
535public:
537 vtkVector4d() = default;
538 vtkVector4d(double x, double y, double z, double w)
539 : vtkVector4<double>(x, y, z, w)
540 {
541 }
543};
544
553template <typename A, int Size>
555{
557 for (int i = 0; i < Size; ++i)
558 {
559 ret[i] = -v[i];
560 }
561 return ret;
562}
563
567template <typename A, int Size>
569{
571 for (int i = 0; i < Size; ++i)
572 {
573 ret[i] = v1[i] + v2[i];
574 }
575 return ret;
576}
577
581template <typename T, int Size>
583{
584 for (int dim = 0; dim < Size; ++dim)
585 {
586 a[dim] += b[dim];
587 }
588
589 return a;
590}
591
595template <typename A, int Size>
597{
599 for (int i = 0; i < Size; ++i)
600 {
601 ret[i] = v1[i] - v2[i];
602 }
603 return ret;
604}
605
609template <typename T, int Size>
611{
612 for (int dim = 0; dim < Size; ++dim)
613 {
614 a[dim] -= b[dim];
615 }
616
617 return a;
618}
619
623template <typename A, int Size>
625{
627 for (int i = 0; i < Size; ++i)
628 {
629 ret[i] = v1[i] * v2[i];
630 }
631 return ret;
632}
633
637template <typename A, typename B, int Size>
639{
641 for (int i = 0; i < Size; ++i)
642 {
643 ret[i] = v1[i] * scalar;
644 }
645 return ret;
646}
647
651template <typename A, int Size>
653{
655 for (int i = 0; i < Size; ++i)
656 {
657 ret[i] = v1[i] / v2[i];
658 }
659 return ret;
660}
661
669#define vtkVectorOperatorNegate(vectorType, type, size) \
670 inline vectorType operator-(const vectorType& v) \
671 { \
672 return vectorType((-static_cast<vtkVector<type, size>>(v)).GetData()); \
673 }
674#define vtkVectorOperatorPlus(vectorType, type, size) \
675 inline vectorType operator+(const vectorType& v1, const vectorType& v2) \
676 { \
677 return vectorType( \
678 (static_cast<vtkVector<type, size>>(v1) + static_cast<vtkVector<type, size>>(v2)) \
679 .GetData()); \
680 }
681#define vtkVectorOperatorMinus(vectorType, type, size) \
682 inline vectorType operator-(const vectorType& v1, const vectorType& v2) \
683 { \
684 return vectorType( \
685 (static_cast<vtkVector<type, size>>(v1) - static_cast<vtkVector<type, size>>(v2)) \
686 .GetData()); \
687 }
688#define vtkVectorOperatorMultiply(vectorType, type, size) \
689 inline vectorType operator*(const vectorType& v1, const vectorType& v2) \
690 { \
691 return vectorType( \
692 (static_cast<vtkVector<type, size>>(v1) * static_cast<vtkVector<type, size>>(v2)) \
693 .GetData()); \
694 }
695#define vtkVectorOperatorMultiplyScalar(vectorType, type, size) \
696 template <typename B> \
697 inline vectorType operator*(const vectorType& v1, const B& scalar) \
698 { \
699 return vectorType((static_cast<vtkVector<type, size>>(v1) * scalar).GetData()); \
700 }
701#define vtkVectorOperatorMultiplyScalarPre(vectorType, type, size) \
702 template <typename B> \
703 inline vectorType operator*(const B& scalar, const vectorType& v1) \
704 { \
705 return vectorType((static_cast<vtkVector<type, size>>(v1) * scalar).GetData()); \
706 }
707#define vtkVectorOperatorDivide(vectorType, type, size) \
708 inline vectorType operator/(const vectorType& v1, const vectorType& v2) \
709 { \
710 return vectorType( \
711 (static_cast<vtkVector<type, size>>(v1) / static_cast<vtkVector<type, size>>(v2)) \
712 .GetData()); \
713 }
714
715#define vtkVectorOperatorMacro(vectorType, type, size) \
716 vtkVectorOperatorNegate(vectorType, type, size); \
717 vtkVectorOperatorPlus(vectorType, type, size); \
718 vtkVectorOperatorMinus(vectorType, type, size); \
719 vtkVectorOperatorMultiply(vectorType, type, size); \
720 vtkVectorOperatorMultiplyScalar(vectorType, type, size); \
721 vtkVectorOperatorMultiplyScalarPre(vectorType, type, size); \
722 vtkVectorOperatorDivide(vectorType, type, size)
723
733
734VTK_ABI_NAMESPACE_END
735#endif // vtkVector_h
736// 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:210
const T & GetY() const
Get the y component of the vector, i.e.
Definition vtkVector.h:220
void Set(const T &x, const T &y)
Set the x and y components of the vector.
Definition vtkVector.h:195
vtkVector2(const T *init)
Definition vtkVector.h:180
void SetY(const T &y)
Set the y component of the vector, i.e.
Definition vtkVector.h:215
vtkVector2(const T &x, const T &y)
Definition vtkVector.h:185
vtkVector2(const T &scalar)
Definition vtkVector.h:175
void SetX(const T &x)
Set the x component of the vector, i.e.
Definition vtkVector.h:205
bool operator<(const vtkVector2< T > &v) const
Lexicographical comparison of two vector.
Definition vtkVector.h:226
vtkVector2()=default
vtkVector2d()=default
vtkVectorDerivedMacro(vtkVector2d, double, 2)
vtkVector2d(double x, double y)
Definition vtkVector.h:469
vtkVector2< double > Superclass
Definition vtkVector.h:467
vtkVector2f()=default
vtkVector2< float > Superclass
Definition vtkVector.h:455
vtkVector2f(float x, float y)
Definition vtkVector.h:457
vtkVectorDerivedMacro(vtkVector2f, float, 2)
Some derived classes for the different vectors commonly used.
Definition vtkVector.h:440
vtkVector2i()=default
vtkVector2i(int x, int y)
Definition vtkVector.h:444
vtkVector2< int > Superclass
Definition vtkVector.h:442
vtkVectorDerivedMacro(vtkVector2i, int, 2)
void SetZ(const T &z)
Set the z component of the vector, i.e.
Definition vtkVector.h:293
const T & GetY() const
Get the y component of the vector, i.e.
Definition vtkVector.h:288
bool operator<(const vtkVector3< T > &v) const
Lexicographical comparison of two vector.
Definition vtkVector.h:318
vtkVector3(const T *init)
Definition vtkVector.h:246
const T & GetZ() const
Get the z component of the vector, i.e.
Definition vtkVector.h:298
void SetX(const T &x)
Set the x component of the vector, i.e.
Definition vtkVector.h:273
vtkVector3(const T &scalar)
Definition vtkVector.h:241
void Set(const T &x, const T &y, const T &z)
Set the x, y and z components of the vector.
Definition vtkVector.h:262
vtkVector3(const T &x, const T &y, const T &z)
Definition vtkVector.h:251
vtkVector3< T > Cross(const vtkVector3< T > &other) const
Return the cross product of this X other.
Definition vtkVector.h:304
const T & GetX() const
Get the x component of the vector, i.e.
Definition vtkVector.h:278
void SetY(const T &y)
Set the y component of the vector, i.e.
Definition vtkVector.h:283
vtkVector3()=default
vtkVector3Cross(vtkVector3d, double)
vtkVector3< double > Superclass
Definition vtkVector.h:511
vtkVector3d(double x, double y, double z)
Definition vtkVector.h:513
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:500
vtkVector3< float > Superclass
Definition vtkVector.h:498
vtkVector3Cross(vtkVector3i, int)
vtkVectorDerivedMacro(vtkVector3i, int, 3)
vtkVector3< int > Superclass
Definition vtkVector.h:485
vtkVector3i()=default
vtkVector3i(int x, int y, int z)
Definition vtkVector.h:487
void SetY(const T &y)
Set the y component of the vector, i.e.
Definition vtkVector.h:379
vtkVector4(const T &x, const T &y, const T &z, const T &w)
Definition vtkVector.h:345
void SetZ(const T &z)
Set the z component of the vector, i.e.
Definition vtkVector.h:389
const T & GetZ() const
Get the z component of the vector, i.e.
Definition vtkVector.h:394
void SetW(const T &w)
Set the w component of the vector, i.e.
Definition vtkVector.h:399
vtkVector4(const T *init)
Definition vtkVector.h:340
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:357
vtkVector4()=default
const T & GetY() const
Get the y component of the vector, i.e.
Definition vtkVector.h:384
void SetX(const T &x)
Set the x component of the vector, i.e.
Definition vtkVector.h:369
vtkVector4(const T &scalar)
Definition vtkVector.h:335
const T & GetX() const
Get the x component of the vector, i.e.
Definition vtkVector.h:374
const T & GetW() const
Get the w component of the vector, i.e.
Definition vtkVector.h:404
vtkVectorDerivedMacro(vtkVector4d, double, 4)
vtkVector4d(double x, double y, double z, double w)
Definition vtkVector.h:538
vtkVector4d()=default
vtkVector4< int > Superclass
Definition vtkVector.h:524
vtkVector4i(int x, int y, int z, int w)
Definition vtkVector.h:526
vtkVector4i()=default
vtkVectorDerivedMacro(vtkVector4i, int, 4)
templated base type for storage of vectors.
Definition vtkVector.h:59
vtkVector(const T &scalar)
Initialize all of the vector's elements with the supplied scalar.
Definition vtkVector.h:66
T Dot(const vtkVector< T, Size > &other) const
The dot product of this and the supplied vector.
Definition vtkVector.h:139
double Normalize()
Normalize the vector in place.
Definition vtkVector.h:106
vtkVector()=default
vtkVector(const T *init)
Initialize the vector's elements with the elements of the supplied array.
Definition vtkVector.h:76
vtkVector< TR, Size > Cast() const
Cast the vector to the specified type, returning the result.
Definition vtkVector.h:155
double Norm() const
Get the norm of the vector, i.e.
Definition vtkVector.h:99
vtkVector< T, Size > Normalized() const
Return the normalized form of this vector.
Definition vtkVector.h:127
T SquaredNorm() const
Get the squared norm of the vector.
Definition vtkVector.h:85
#define vtkVectorOperatorMacro(vectorType, type, size)
Definition vtkVector.h:715
vtkVector< A, Size > operator/(const vtkVector< A, Size > &v1, const vtkVector< A, Size > &v2)
Performs division of vectors of the same type.
Definition vtkVector.h:652
vtkVector< A, Size > operator+(const vtkVector< A, Size > &v1, const vtkVector< A, Size > &v2)
Performs addition of vectors of the same basic type.
Definition vtkVector.h:568
vtkVector< T, Size > & operator-=(vtkVector< T, Size > &a, const vtkVector< T, Size > &b)
Subtract the vector b to the vector a of the same basic type.
Definition vtkVector.h:610
vtkVector< A, Size > operator*(const vtkVector< A, Size > &v1, const vtkVector< A, Size > &v2)
Performs multiplication of vectors of the same basic type.
Definition vtkVector.h:624
vtkVector< A, Size > operator-(const vtkVector< A, Size > &v)
This following operators enhance the vtkVector classes, allowing various operator overloads one might...
Definition vtkVector.h:554
vtkVector< T, Size > & operator+=(vtkVector< T, Size > &a, const vtkVector< T, Size > &b)
Add the vector b to the vector a of the same basic type.
Definition vtkVector.h:582