VTK  9.5.20250816
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
58#ifndef vtkVector_h
59#define vtkVector_h
60
61#include "vtkObject.h" // for legacy macros
62#include "vtkTuple.h"
63
64#include <cmath> // For math functions
65
66VTK_ABI_NAMESPACE_BEGIN
67template <typename T, int Size>
68class vtkVector : public vtkTuple<T, Size>
69{
70public:
71 vtkVector() = default;
72
76 explicit vtkVector(const T& scalar)
77 : vtkTuple<T, Size>(scalar)
78 {
79 }
80
86 explicit vtkVector(const T* init)
87 : vtkTuple<T, Size>(init)
88 {
89 }
90
92
95 T SquaredNorm() const
96 {
97 T result = 0;
98 for (int i = 0; i < Size; ++i)
99 {
100 result += this->Data[i] * this->Data[i];
101 }
102 return result;
103 }
105
109 double Norm() const { return sqrt(static_cast<double>(this->SquaredNorm())); }
110
112
116 double Normalize()
117 {
118 const double norm(this->Norm());
119 if (norm == 0.0)
120 {
121 return 0.0;
122 }
123 const double inv(1.0 / norm);
124 for (int i = 0; i < Size; ++i)
125 {
126 this->Data[i] = static_cast<T>(this->Data[i] * inv);
127 }
128 return norm;
129 }
131
133
138 {
139 vtkVector<T, Size> temp(*this);
140 temp.Normalize();
141 return temp;
142 }
144
146
149 T Dot(const vtkVector<T, Size>& other) const
150 {
151 T result(0);
152 for (int i = 0; i < Size; ++i)
153 {
154 result += this->Data[i] * other[i];
155 }
156 return result;
157 }
159
161
164 template <typename TR>
166 {
167 vtkVector<TR, Size> result;
168 for (int i = 0; i < Size; ++i)
169 {
170 result[i] = static_cast<TR>(this->Data[i]);
171 }
172 return result;
173 }
175};
176
177// .NAME vtkVector2 - templated base type for storage of 2D vectors.
178//
179template <typename T>
180class vtkVector2 : public vtkVector<T, 2>
181{
182public:
183 vtkVector2() = default;
184
185 explicit vtkVector2(const T& scalar)
186 : vtkVector<T, 2>(scalar)
187 {
188 }
189
190 explicit vtkVector2(const T* init)
191 : vtkVector<T, 2>(init)
192 {
193 }
194
195 vtkVector2(const T& x, const T& y)
196 {
197 this->Data[0] = x;
198 this->Data[1] = y;
199 }
200
202
205 void Set(const T& x, const T& y)
206 {
207 this->Data[0] = x;
208 this->Data[1] = y;
209 }
211
215 void SetX(const T& x) { this->Data[0] = x; }
216
220 const T& GetX() const { return this->Data[0]; }
221
225 void SetY(const T& y) { this->Data[1] = y; }
226
230 const T& GetY() const { return this->Data[1]; }
231
233
236 bool operator<(const vtkVector2<T>& v) const
237 {
238 return (this->Data[0] < v.Data[0]) || (this->Data[0] == v.Data[0] && this->Data[1] < v.Data[1]);
239 }
241};
242
243// .NAME vtkVector3 - templated base type for storage of 3D vectors.
244//
245template <typename T>
246class vtkVector3 : public vtkVector<T, 3>
247{
248public:
249 vtkVector3() = default;
250
251 explicit vtkVector3(const T& scalar)
252 : vtkVector<T, 3>(scalar)
253 {
254 }
255
256 explicit vtkVector3(const T* init)
257 : vtkVector<T, 3>(init)
258 {
259 }
260
261 vtkVector3(const T& x, const T& y, const T& z)
262 {
263 this->Data[0] = x;
264 this->Data[1] = y;
265 this->Data[2] = z;
266 }
267
269
272 void Set(const T& x, const T& y, const T& z)
273 {
274 this->Data[0] = x;
275 this->Data[1] = y;
276 this->Data[2] = z;
277 }
279
283 void SetX(const T& x) { this->Data[0] = x; }
284
288 const T& GetX() const { return this->Data[0]; }
289
293 void SetY(const T& y) { this->Data[1] = y; }
294
298 const T& GetY() const { return this->Data[1]; }
299
303 void SetZ(const T& z) { this->Data[2] = z; }
304
308 const T& GetZ() const { return this->Data[2]; }
309
311
315 {
316 vtkVector3<T> res;
317 res[0] = this->Data[1] * other.Data[2] - this->Data[2] * other.Data[1];
318 res[1] = this->Data[2] * other.Data[0] - this->Data[0] * other.Data[2];
319 res[2] = this->Data[0] * other.Data[1] - this->Data[1] * other.Data[0];
320 return res;
321 }
323
325
328 bool operator<(const vtkVector3<T>& v) const
329 {
330 return (this->Data[0] < v.Data[0]) ||
331 (this->Data[0] == v.Data[0] && this->Data[1] < v.Data[1]) ||
332 (this->Data[0] == v.Data[0] && this->Data[1] == v.Data[1] && this->Data[2] < v.Data[2]);
333 }
335};
336
337// .NAME vtkVector4 - templated base type for storage of 4D vectors.
338//
339template <typename T>
340class vtkVector4 : public vtkVector<T, 4>
341{
342public:
343 vtkVector4() = default;
344
345 explicit vtkVector4(const T& scalar)
346 : vtkVector<T, 4>(scalar)
347 {
348 }
349
350 explicit vtkVector4(const T* init)
351 : vtkVector<T, 4>(init)
352 {
353 }
354
355 vtkVector4(const T& x, const T& y, const T& z, const T& w)
356 {
357 this->Data[0] = x;
358 this->Data[1] = y;
359 this->Data[2] = z;
360 this->Data[3] = w;
361 }
362
364
367 void Set(const T& x, const T& y, const T& z, const T& w)
368 {
369 this->Data[0] = x;
370 this->Data[1] = y;
371 this->Data[2] = z;
372 this->Data[3] = w;
373 }
375
379 void SetX(const T& x) { this->Data[0] = x; }
380
384 const T& GetX() const { return this->Data[0]; }
385
389 void SetY(const T& y) { this->Data[1] = y; }
390
394 const T& GetY() const { return this->Data[1]; }
395
399 void SetZ(const T& z) { this->Data[2] = z; }
400
404 const T& GetZ() const { return this->Data[2]; }
405
409 void SetW(const T& w) { this->Data[3] = w; }
410
414 const T& GetW() const { return this->Data[3]; }
415};
416
420#define vtkVectorNormalized(vectorType, type, size) \
421 vectorType Normalized() const \
422 { \
423 return vectorType(vtkVector<type, size>::Normalized().GetData()); \
424 }
425
426#define vtkVectorDerivedMacro(vectorType, type, size) \
427 vtkVectorNormalized(vectorType, type, size); \
428 explicit vectorType(type s) \
429 : Superclass(s) \
430 { \
431 } \
432 explicit vectorType(const type* i) \
433 : Superclass(i) \
434 { \
435 } \
436 explicit vectorType(const vtkTuple<type, size>& o) \
437 : Superclass(o.GetData()) \
438 { \
439 } \
440 vectorType(const vtkVector<type, size>& o) \
441 : Superclass(o.GetData()) \
442 { \
443 }
444
446
449class vtkVector2i : public vtkVector2<int>
450{
451public:
453 vtkVector2i() = default;
454 vtkVector2i(int x, int y)
455 : vtkVector2<int>(x, y)
456 {
457 }
459};
461
462class vtkVector2f : public vtkVector2<float>
463{
464public:
466 vtkVector2f() = default;
467 vtkVector2f(float x, float y)
468 : vtkVector2<float>(x, y)
469 {
470 }
472};
473
474class vtkVector2d : public vtkVector2<double>
475{
476public:
478 vtkVector2d() = default;
479 vtkVector2d(double x, double y)
480 : vtkVector2<double>(x, y)
481 {
482 }
484};
485
486#define vtkVector3Cross(vectorType, type) \
487 vectorType Cross(const vectorType& other) const \
488 { \
489 return vectorType(vtkVector3<type>::Cross(other).GetData()); \
490 }
491
492class vtkVector3i : public vtkVector3<int>
493{
494public:
496 vtkVector3i() = default;
497 vtkVector3i(int x, int y, int z)
498 : vtkVector3<int>(x, y, z)
499 {
500 }
503};
504
505class vtkVector3f : public vtkVector3<float>
506{
507public:
509 vtkVector3f() = default;
510 vtkVector3f(float x, float y, float z)
511 : vtkVector3<float>(x, y, z)
512 {
513 }
516};
517
518class vtkVector3d : public vtkVector3<double>
519{
520public:
522 vtkVector3d() = default;
523 vtkVector3d(double x, double y, double z)
524 : vtkVector3<double>(x, y, z)
525 {
526 }
529};
530
531class vtkVector4i : public vtkVector4<int>
532{
533public:
535 vtkVector4i() = default;
536 vtkVector4i(int x, int y, int z, int w)
537 : vtkVector4<int>(x, y, z, w)
538 {
539 }
541};
542
543class vtkVector4d : public vtkVector4<double>
544{
545public:
547 vtkVector4d() = default;
548 vtkVector4d(double x, double y, double z, double w)
549 : vtkVector4<double>(x, y, z, w)
550 {
551 }
553};
554
563template <typename A, int Size>
565{
567 for (int i = 0; i < Size; ++i)
568 {
569 ret[i] = -v[i];
570 }
571 return ret;
572}
573
577template <typename A, int Size>
579{
581 for (int i = 0; i < Size; ++i)
582 {
583 ret[i] = v1[i] + v2[i];
584 }
585 return ret;
586}
587
591template <typename T, int Size>
593{
594 for (int dim = 0; dim < Size; ++dim)
595 {
596 a[dim] += b[dim];
597 }
598
599 return a;
600}
601
605template <typename A, int Size>
607{
609 for (int i = 0; i < Size; ++i)
610 {
611 ret[i] = v1[i] - v2[i];
612 }
613 return ret;
614}
615
619template <typename T, int Size>
621{
622 for (int dim = 0; dim < Size; ++dim)
623 {
624 a[dim] -= b[dim];
625 }
626
627 return a;
628}
629
633template <typename A, int Size>
635{
637 for (int i = 0; i < Size; ++i)
638 {
639 ret[i] = v1[i] * v2[i];
640 }
641 return ret;
642}
643
647template <typename A, typename B, int Size>
649{
651 for (int i = 0; i < Size; ++i)
652 {
653 ret[i] = v1[i] * scalar;
654 }
655 return ret;
656}
657
661template <typename A, int Size>
663{
665 for (int i = 0; i < Size; ++i)
666 {
667 ret[i] = v1[i] / v2[i];
668 }
669 return ret;
670}
671
679#define vtkVectorOperatorNegate(vectorType, type, size) \
680 inline vectorType operator-(const vectorType& v) \
681 { \
682 return vectorType((-static_cast<vtkVector<type, size>>(v)).GetData()); \
683 }
684#define vtkVectorOperatorPlus(vectorType, type, size) \
685 inline vectorType operator+(const vectorType& v1, const vectorType& v2) \
686 { \
687 return vectorType( \
688 (static_cast<vtkVector<type, size>>(v1) + static_cast<vtkVector<type, size>>(v2)) \
689 .GetData()); \
690 }
691#define vtkVectorOperatorMinus(vectorType, type, size) \
692 inline vectorType operator-(const vectorType& v1, const vectorType& v2) \
693 { \
694 return vectorType( \
695 (static_cast<vtkVector<type, size>>(v1) - static_cast<vtkVector<type, size>>(v2)) \
696 .GetData()); \
697 }
698#define vtkVectorOperatorMultiply(vectorType, type, size) \
699 inline vectorType operator*(const vectorType& v1, const vectorType& v2) \
700 { \
701 return vectorType( \
702 (static_cast<vtkVector<type, size>>(v1) * static_cast<vtkVector<type, size>>(v2)) \
703 .GetData()); \
704 }
705#define vtkVectorOperatorMultiplyScalar(vectorType, type, size) \
706 template <typename B> \
707 inline vectorType operator*(const vectorType& v1, const B& scalar) \
708 { \
709 return vectorType((static_cast<vtkVector<type, size>>(v1) * scalar).GetData()); \
710 }
711#define vtkVectorOperatorMultiplyScalarPre(vectorType, type, size) \
712 template <typename B> \
713 inline vectorType operator*(const B& scalar, const vectorType& v1) \
714 { \
715 return vectorType((static_cast<vtkVector<type, size>>(v1) * scalar).GetData()); \
716 }
717#define vtkVectorOperatorDivide(vectorType, type, size) \
718 inline vectorType operator/(const vectorType& v1, const vectorType& v2) \
719 { \
720 return vectorType( \
721 (static_cast<vtkVector<type, size>>(v1) / static_cast<vtkVector<type, size>>(v2)) \
722 .GetData()); \
723 }
724
725#define vtkVectorOperatorMacro(vectorType, type, size) \
726 vtkVectorOperatorNegate(vectorType, type, size); \
727 vtkVectorOperatorPlus(vectorType, type, size); \
728 vtkVectorOperatorMinus(vectorType, type, size); \
729 vtkVectorOperatorMultiply(vectorType, type, size); \
730 vtkVectorOperatorMultiplyScalar(vectorType, type, size); \
731 vtkVectorOperatorMultiplyScalarPre(vectorType, type, size); \
732 vtkVectorOperatorDivide(vectorType, type, size)
733
743
744VTK_ABI_NAMESPACE_END
745#endif // vtkVector_h
746// VTK-HeaderTest-Exclude: vtkVector.h
templated base type for containers of constant size.
Definition vtkTuple.h:30
T Data[Size]
The only thing stored in memory!
Definition vtkTuple.h:146
const T & GetX() const
Get the x component of the vector, i.e.
Definition vtkVector.h:220
const T & GetY() const
Get the y component of the vector, i.e.
Definition vtkVector.h:230
void Set(const T &x, const T &y)
Set the x and y components of the vector.
Definition vtkVector.h:205
vtkVector2(const T *init)
Definition vtkVector.h:190
void SetY(const T &y)
Set the y component of the vector, i.e.
Definition vtkVector.h:225
vtkVector2(const T &x, const T &y)
Definition vtkVector.h:195
vtkVector2(const T &scalar)
Definition vtkVector.h:185
void SetX(const T &x)
Set the x component of the vector, i.e.
Definition vtkVector.h:215
bool operator<(const vtkVector2< T > &v) const
Lexicographical comparison of two vector.
Definition vtkVector.h:236
vtkVector2()=default
vtkVector2d()=default
vtkVectorDerivedMacro(vtkVector2d, double, 2)
vtkVector2d(double x, double y)
Definition vtkVector.h:479
vtkVector2< double > Superclass
Definition vtkVector.h:477
vtkVector2f()=default
vtkVector2< float > Superclass
Definition vtkVector.h:465
vtkVector2f(float x, float y)
Definition vtkVector.h:467
vtkVectorDerivedMacro(vtkVector2f, float, 2)
Some derived classes for the different vectors commonly used.
Definition vtkVector.h:450
vtkVector2i()=default
vtkVector2i(int x, int y)
Definition vtkVector.h:454
vtkVector2< int > Superclass
Definition vtkVector.h:452
vtkVectorDerivedMacro(vtkVector2i, int, 2)
void SetZ(const T &z)
Set the z component of the vector, i.e.
Definition vtkVector.h:303
const T & GetY() const
Get the y component of the vector, i.e.
Definition vtkVector.h:298
bool operator<(const vtkVector3< T > &v) const
Lexicographical comparison of two vector.
Definition vtkVector.h:328
vtkVector3(const T *init)
Definition vtkVector.h:256
const T & GetZ() const
Get the z component of the vector, i.e.
Definition vtkVector.h:308
void SetX(const T &x)
Set the x component of the vector, i.e.
Definition vtkVector.h:283
vtkVector3(const T &scalar)
Definition vtkVector.h:251
void Set(const T &x, const T &y, const T &z)
Set the x, y and z components of the vector.
Definition vtkVector.h:272
vtkVector3(const T &x, const T &y, const T &z)
Definition vtkVector.h:261
vtkVector3< T > Cross(const vtkVector3< T > &other) const
Return the cross product of this X other.
Definition vtkVector.h:314
const T & GetX() const
Get the x component of the vector, i.e.
Definition vtkVector.h:288
void SetY(const T &y)
Set the y component of the vector, i.e.
Definition vtkVector.h:293
vtkVector3()=default
vtkVector3Cross(vtkVector3d, double)
vtkVector3< double > Superclass
Definition vtkVector.h:521
vtkVector3d(double x, double y, double z)
Definition vtkVector.h:523
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:510
vtkVector3< float > Superclass
Definition vtkVector.h:508
vtkVector3Cross(vtkVector3i, int)
vtkVectorDerivedMacro(vtkVector3i, int, 3)
vtkVector3< int > Superclass
Definition vtkVector.h:495
vtkVector3i()=default
vtkVector3i(int x, int y, int z)
Definition vtkVector.h:497
void SetY(const T &y)
Set the y component of the vector, i.e.
Definition vtkVector.h:389
vtkVector4(const T &x, const T &y, const T &z, const T &w)
Definition vtkVector.h:355
void SetZ(const T &z)
Set the z component of the vector, i.e.
Definition vtkVector.h:399
const T & GetZ() const
Get the z component of the vector, i.e.
Definition vtkVector.h:404
void SetW(const T &w)
Set the w component of the vector, i.e.
Definition vtkVector.h:409
vtkVector4(const T *init)
Definition vtkVector.h:350
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:367
vtkVector4()=default
const T & GetY() const
Get the y component of the vector, i.e.
Definition vtkVector.h:394
void SetX(const T &x)
Set the x component of the vector, i.e.
Definition vtkVector.h:379
vtkVector4(const T &scalar)
Definition vtkVector.h:345
const T & GetX() const
Get the x component of the vector, i.e.
Definition vtkVector.h:384
const T & GetW() const
Get the w component of the vector, i.e.
Definition vtkVector.h:414
vtkVectorDerivedMacro(vtkVector4d, double, 4)
vtkVector4d(double x, double y, double z, double w)
Definition vtkVector.h:548
vtkVector4d()=default
vtkVector4< int > Superclass
Definition vtkVector.h:534
vtkVector4i(int x, int y, int z, int w)
Definition vtkVector.h:536
vtkVector4i()=default
vtkVectorDerivedMacro(vtkVector4i, int, 4)
templated base type for storage of vectors.
Definition vtkVector.h:69
vtkVector(const T &scalar)
Initialize all of the vector's elements with the supplied scalar.
Definition vtkVector.h:76
T Dot(const vtkVector< T, Size > &other) const
The dot product of this and the supplied vector.
Definition vtkVector.h:149
double Normalize()
Normalize the vector in place.
Definition vtkVector.h:116
vtkVector()=default
vtkVector(const T *init)
Initialize the vector's elements with the elements of the supplied array.
Definition vtkVector.h:86
vtkVector< TR, Size > Cast() const
Cast the vector to the specified type, returning the result.
Definition vtkVector.h:165
double Norm() const
Get the norm of the vector, i.e.
Definition vtkVector.h:109
vtkVector< T, Size > Normalized() const
Return the normalized form of this vector.
Definition vtkVector.h:137
T SquaredNorm() const
Get the squared norm of the vector.
Definition vtkVector.h:95
#define vtkVectorOperatorMacro(vectorType, type, size)
Definition vtkVector.h:725
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:662
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:578
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:620
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:634
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:564
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:592