VTK  9.1.0
vtkVector.h
Go to the documentation of this file.
1/*=========================================================================
2
3 Program: Visualization Toolkit
4 Module: vtkVector.h
5
6 Copyright (c) Ken Martin, Will Schroeder, Bill Lorensen
7 All rights reserved.
8 See Copyright.txt or http://www.kitware.com/Copyright.htm for details.
9
10 This software is distributed WITHOUT ANY WARRANTY; without even
11 the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
12 PURPOSE. See the above copyright notice for more information.
13
14=========================================================================*/
15
37#ifndef vtkVector_h
38#define vtkVector_h
39
40#include "vtkObject.h" // for legacy macros
41#include "vtkTuple.h"
42
43#include <cmath> // For math functions
44
45template <typename T, int Size>
46class vtkVector : public vtkTuple<T, Size>
47{
48public:
49 vtkVector() = default;
50
54 explicit vtkVector(const T& scalar)
55 : vtkTuple<T, Size>(scalar)
56 {
57 }
58
64 explicit vtkVector(const T* init)
65 : vtkTuple<T, Size>(init)
66 {
67 }
68
70
73 T SquaredNorm() const
74 {
75 T result = 0;
76 for (int i = 0; i < Size; ++i)
77 {
78 result += this->Data[i] * this->Data[i];
79 }
80 return result;
81 }
83
87 double Norm() const { return sqrt(static_cast<double>(this->SquaredNorm())); }
88
90
94 double Normalize()
95 {
96 const double norm(this->Norm());
97 if (norm == 0.0)
98 {
99 return 0.0;
100 }
101 const double inv(1.0 / norm);
102 for (int i = 0; i < Size; ++i)
103 {
104 this->Data[i] = static_cast<T>(this->Data[i] * inv);
105 }
106 return norm;
107 }
109
111
116 {
117 vtkVector<T, Size> temp(*this);
118 temp.Normalize();
119 return temp;
120 }
122
124
127 T Dot(const vtkVector<T, Size>& other) const
128 {
129 T result(0);
130 for (int i = 0; i < Size; ++i)
131 {
132 result += this->Data[i] * other[i];
133 }
134 return result;
135 }
137
139
142 template <typename TR>
144 {
145 vtkVector<TR, Size> result;
146 for (int i = 0; i < Size; ++i)
147 {
148 result[i] = static_cast<TR>(this->Data[i]);
149 }
150 return result;
151 }
153};
154
155// .NAME vtkVector2 - templated base type for storage of 2D vectors.
156//
157template <typename T>
158class vtkVector2 : public vtkVector<T, 2>
159{
160public:
161 vtkVector2() = default;
162
163 explicit vtkVector2(const T& scalar)
164 : vtkVector<T, 2>(scalar)
165 {
166 }
167
168 explicit vtkVector2(const T* init)
169 : vtkVector<T, 2>(init)
170 {
171 }
172
173 vtkVector2(const T& x, const T& y)
174 {
175 this->Data[0] = x;
176 this->Data[1] = y;
177 }
178
180
183 void Set(const T& x, const T& y)
184 {
185 this->Data[0] = x;
186 this->Data[1] = y;
187 }
189
193 void SetX(const T& x) { this->Data[0] = x; }
194
198 const T& GetX() const { return this->Data[0]; }
199
203 void SetY(const T& y) { this->Data[1] = y; }
204
208 const T& GetY() const { return this->Data[1]; }
209
211
214 bool operator<(const vtkVector2<T>& v) const
215 {
216 return (this->Data[0] < v.Data[0]) || (this->Data[0] == v.Data[0] && this->Data[1] < v.Data[1]);
217 }
219};
220
221// .NAME vtkVector3 - templated base type for storage of 3D vectors.
222//
223template <typename T>
224class vtkVector3 : public vtkVector<T, 3>
225{
226public:
227 vtkVector3() = default;
228
229 explicit vtkVector3(const T& scalar)
230 : vtkVector<T, 3>(scalar)
231 {
232 }
233
234 explicit vtkVector3(const T* init)
235 : vtkVector<T, 3>(init)
236 {
237 }
238
239 vtkVector3(const T& x, const T& y, const T& z)
240 {
241 this->Data[0] = x;
242 this->Data[1] = y;
243 this->Data[2] = z;
244 }
245
247
250 void Set(const T& x, const T& y, const T& z)
251 {
252 this->Data[0] = x;
253 this->Data[1] = y;
254 this->Data[2] = z;
255 }
257
261 void SetX(const T& x) { this->Data[0] = x; }
262
266 const T& GetX() const { return this->Data[0]; }
267
271 void SetY(const T& y) { this->Data[1] = y; }
272
276 const T& GetY() const { return this->Data[1]; }
277
281 void SetZ(const T& z) { this->Data[2] = z; }
282
286 const T& GetZ() const { return this->Data[2]; }
287
289
293 {
294 vtkVector3<T> res;
295 res[0] = this->Data[1] * other.Data[2] - this->Data[2] * other.Data[1];
296 res[1] = this->Data[2] * other.Data[0] - this->Data[0] * other.Data[2];
297 res[2] = this->Data[0] * other.Data[1] - this->Data[1] * other.Data[0];
298 return res;
299 }
301
303
306 bool operator<(const vtkVector3<T>& v) const
307 {
308 return (this->Data[0] < v.Data[0]) ||
309 (this->Data[0] == v.Data[0] && this->Data[1] < v.Data[1]) ||
310 (this->Data[0] == v.Data[0] && this->Data[1] == v.Data[1] && this->Data[2] < v.Data[2]);
311 }
313};
314
315// .NAME vtkVector4 - templated base type for storage of 4D vectors.
316//
317template <typename T>
318class vtkVector4 : public vtkVector<T, 4>
319{
320public:
321 vtkVector4() = default;
322
323 explicit vtkVector4(const T& scalar)
324 : vtkVector<T, 4>(scalar)
325 {
326 }
327
328 explicit vtkVector4(const T* init)
329 : vtkVector<T, 4>(init)
330 {
331 }
332
333 vtkVector4(const T& x, const T& y, const T& z, const T& w)
334 {
335 this->Data[0] = x;
336 this->Data[1] = y;
337 this->Data[2] = z;
338 this->Data[3] = w;
339 }
340
342
345 void Set(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 }
353
357 void SetX(const T& x) { this->Data[0] = x; }
358
362 const T& GetX() const { return this->Data[0]; }
363
367 void SetY(const T& y) { this->Data[1] = y; }
368
372 const T& GetY() const { return this->Data[1]; }
373
377 void SetZ(const T& z) { this->Data[2] = z; }
378
382 const T& GetZ() const { return this->Data[2]; }
383
387 void SetW(const T& w) { this->Data[3] = w; }
388
392 const T& GetW() const { return this->Data[3]; }
393};
394
398#define vtkVectorNormalized(vectorType, type, size) \
399 vectorType Normalized() const \
400 { \
401 return vectorType(vtkVector<type, size>::Normalized().GetData()); \
402 }
403
404#define vtkVectorDerivedMacro(vectorType, type, size) \
405 vtkVectorNormalized(vectorType, type, size); \
406 explicit vectorType(type s) \
407 : Superclass(s) \
408 { \
409 } \
410 explicit vectorType(const type* i) \
411 : Superclass(i) \
412 { \
413 } \
414 explicit vectorType(const vtkTuple<type, size>& o) \
415 : Superclass(o.GetData()) \
416 { \
417 } \
418 vectorType(const vtkVector<type, size>& o) \
419 : Superclass(o.GetData()) \
420 { \
421 }
422
424
427class vtkVector2i : public vtkVector2<int>
428{
429public:
431 vtkVector2i() = default;
432 vtkVector2i(int x, int y)
433 : vtkVector2<int>(x, y)
434 {
435 }
437};
439
440class vtkVector2f : public vtkVector2<float>
441{
442public:
444 vtkVector2f() = default;
445 vtkVector2f(float x, float y)
446 : vtkVector2<float>(x, y)
447 {
448 }
450};
451
452class vtkVector2d : public vtkVector2<double>
453{
454public:
456 vtkVector2d() = default;
457 vtkVector2d(double x, double y)
458 : vtkVector2<double>(x, y)
459 {
460 }
462};
463
464#define vtkVector3Cross(vectorType, type) \
465 vectorType Cross(const vectorType& other) const \
466 { \
467 return vectorType(vtkVector3<type>::Cross(other).GetData()); \
468 }
469
470class vtkVector3i : public vtkVector3<int>
471{
472public:
474 vtkVector3i() = default;
475 vtkVector3i(int x, int y, int z)
476 : vtkVector3<int>(x, y, z)
477 {
478 }
481};
482
483class vtkVector3f : public vtkVector3<float>
484{
485public:
487 vtkVector3f() = default;
488 vtkVector3f(float x, float y, float z)
489 : vtkVector3<float>(x, y, z)
490 {
491 }
494};
495
496class vtkVector3d : public vtkVector3<double>
497{
498public:
500 vtkVector3d() = default;
501 vtkVector3d(double x, double y, double z)
502 : vtkVector3<double>(x, y, z)
503 {
504 }
507};
508
509class vtkVector4i : public vtkVector4<int>
510{
511public:
513 vtkVector4i() = default;
514 vtkVector4i(int x, int y, int z, int w)
515 : vtkVector4<int>(x, y, z, w)
516 {
517 }
519};
520
521class vtkVector4d : public vtkVector4<double>
522{
523public:
525 vtkVector4d() = default;
526 vtkVector4d(double x, double y, double z, double w)
527 : vtkVector4<double>(x, y, z, w){};
529};
530
531#endif // vtkVector_h
532// VTK-HeaderTest-Exclude: vtkVector.h
templated base type for containers of constant size.
Definition: vtkTuple.h:38
T Data[Size]
The only thing stored in memory!
Definition: vtkTuple.h:154
const T & GetX() const
Get the x component of the vector, i.e.
Definition: vtkVector.h:198
const T & GetY() const
Get the y component of the vector, i.e.
Definition: vtkVector.h:208
void Set(const T &x, const T &y)
Set the x and y components of the vector.
Definition: vtkVector.h:183
vtkVector2(const T *init)
Definition: vtkVector.h:168
void SetY(const T &y)
Set the y component of the vector, i.e.
Definition: vtkVector.h:203
vtkVector2(const T &x, const T &y)
Definition: vtkVector.h:173
vtkVector2(const T &scalar)
Definition: vtkVector.h:163
void SetX(const T &x)
Set the x component of the vector, i.e.
Definition: vtkVector.h:193
bool operator<(const vtkVector2< T > &v) const
Lexicographical comparison of two vector.
Definition: vtkVector.h:214
vtkVector2()=default
vtkVector2d()=default
vtkVectorDerivedMacro(vtkVector2d, double, 2)
vtkVector2d(double x, double y)
Definition: vtkVector.h:457
vtkVector2< double > Superclass
Definition: vtkVector.h:455
vtkVector2f()=default
vtkVector2< float > Superclass
Definition: vtkVector.h:443
vtkVector2f(float x, float y)
Definition: vtkVector.h:445
vtkVectorDerivedMacro(vtkVector2f, float, 2)
Some derived classes for the different vectors commonly used.
Definition: vtkVector.h:428
vtkVector2i()=default
vtkVector2i(int x, int y)
Definition: vtkVector.h:432
vtkVector2< int > Superclass
Definition: vtkVector.h:430
vtkVectorDerivedMacro(vtkVector2i, int, 2)
void SetZ(const T &z)
Set the z component of the vector, i.e.
Definition: vtkVector.h:281
const T & GetY() const
Get the y component of the vector, i.e.
Definition: vtkVector.h:276
bool operator<(const vtkVector3< T > &v) const
Lexicographical comparison of two vector.
Definition: vtkVector.h:306
vtkVector3(const T *init)
Definition: vtkVector.h:234
const T & GetZ() const
Get the z component of the vector, i.e.
Definition: vtkVector.h:286
void SetX(const T &x)
Set the x component of the vector, i.e.
Definition: vtkVector.h:261
vtkVector3(const T &scalar)
Definition: vtkVector.h:229
void Set(const T &x, const T &y, const T &z)
Set the x, y and z components of the vector.
Definition: vtkVector.h:250
vtkVector3(const T &x, const T &y, const T &z)
Definition: vtkVector.h:239
vtkVector3< T > Cross(const vtkVector3< T > &other) const
Return the cross product of this X other.
Definition: vtkVector.h:292
const T & GetX() const
Get the x component of the vector, i.e.
Definition: vtkVector.h:266
void SetY(const T &y)
Set the y component of the vector, i.e.
Definition: vtkVector.h:271
vtkVector3()=default
vtkVector3Cross(vtkVector3d, double)
vtkVector3< double > Superclass
Definition: vtkVector.h:499
vtkVector3d(double x, double y, double z)
Definition: vtkVector.h:501
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:488
vtkVector3< float > Superclass
Definition: vtkVector.h:486
vtkVector3Cross(vtkVector3i, int)
vtkVectorDerivedMacro(vtkVector3i, int, 3)
vtkVector3< int > Superclass
Definition: vtkVector.h:473
vtkVector3i()=default
vtkVector3i(int x, int y, int z)
Definition: vtkVector.h:475
void SetY(const T &y)
Set the y component of the vector, i.e.
Definition: vtkVector.h:367
vtkVector4(const T &x, const T &y, const T &z, const T &w)
Definition: vtkVector.h:333
void SetZ(const T &z)
Set the z component of the vector, i.e.
Definition: vtkVector.h:377
const T & GetZ() const
Get the z component of the vector, i.e.
Definition: vtkVector.h:382
void SetW(const T &w)
Set the w component of the vector, i.e.
Definition: vtkVector.h:387
vtkVector4(const T *init)
Definition: vtkVector.h:328
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:345
vtkVector4()=default
const T & GetY() const
Get the y component of the vector, i.e.
Definition: vtkVector.h:372
void SetX(const T &x)
Set the x component of the vector, i.e.
Definition: vtkVector.h:357
vtkVector4(const T &scalar)
Definition: vtkVector.h:323
const T & GetX() const
Get the x component of the vector, i.e.
Definition: vtkVector.h:362
const T & GetW() const
Get the w component of the vector, i.e.
Definition: vtkVector.h:392
vtkVectorDerivedMacro(vtkVector4d, double, 4)
vtkVector4d(double x, double y, double z, double w)
Definition: vtkVector.h:526
vtkVector4d()=default
vtkVector4< int > Superclass
Definition: vtkVector.h:512
vtkVector4i(int x, int y, int z, int w)
Definition: vtkVector.h:514
vtkVector4i()=default
vtkVectorDerivedMacro(vtkVector4i, int, 4)
templated base type for storage of vectors.
Definition: vtkVector.h:47
vtkVector(const T &scalar)
Initialize all of the vector's elements with the supplied scalar.
Definition: vtkVector.h:54
T Dot(const vtkVector< T, Size > &other) const
The dot product of this and the supplied vector.
Definition: vtkVector.h:127
double Normalize()
Normalize the vector in place.
Definition: vtkVector.h:94
vtkVector()=default
vtkVector(const T *init)
Initialize the vector's elements with the elements of the supplied array.
Definition: vtkVector.h:64
vtkVector< TR, Size > Cast() const
Cast the vector to the specified type, returning the result.
Definition: vtkVector.h:143
double Norm() const
Get the norm of the vector, i.e.
Definition: vtkVector.h:87
vtkVector< T, Size > Normalized() const
Return the normalized form of this vector.
Definition: vtkVector.h:115
T SquaredNorm() const
Get the squared norm of the vector.
Definition: vtkVector.h:73