VTK  9.2.6
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
27
28#ifndef vtkVector_h
29#define vtkVector_h
30
31#include "vtkObject.h" // for legacy macros
32#include "vtkTuple.h"
33
34#include <cmath> // For math functions
35
36template <typename T, int Size>
37class vtkVector : public vtkTuple<T, Size>
38{
39public:
40 vtkVector() = default;
41
45 explicit vtkVector(const T& scalar)
46 : vtkTuple<T, Size>(scalar)
47 {
48 }
49
55 explicit vtkVector(const T* init)
56 : vtkTuple<T, Size>(init)
57 {
58 }
59
61
64 T SquaredNorm() const
65 {
66 T result = 0;
67 for (int i = 0; i < Size; ++i)
68 {
69 result += this->Data[i] * this->Data[i];
70 }
71 return result;
72 }
73
74
78 double Norm() const { return sqrt(static_cast<double>(this->SquaredNorm())); }
79
81
85 double Normalize()
86 {
87 const double norm(this->Norm());
88 if (norm == 0.0)
89 {
90 return 0.0;
91 }
92 const double inv(1.0 / norm);
93 for (int i = 0; i < Size; ++i)
94 {
95 this->Data[i] = static_cast<T>(this->Data[i] * inv);
96 }
97 return norm;
98 }
99
100
102
107 {
108 vtkVector<T, Size> temp(*this);
109 temp.Normalize();
110 return temp;
111 }
112
113
115
118 T Dot(const vtkVector<T, Size>& other) const
119 {
120 T result(0);
121 for (int i = 0; i < Size; ++i)
122 {
123 result += this->Data[i] * other[i];
124 }
125 return result;
126 }
127
128
130
133 template <typename TR>
135 {
136 vtkVector<TR, Size> result;
137 for (int i = 0; i < Size; ++i)
138 {
139 result[i] = static_cast<TR>(this->Data[i]);
140 }
141 return result;
142 }
143
144};
145
146// .NAME vtkVector2 - templated base type for storage of 2D vectors.
147//
148template <typename T>
149class vtkVector2 : public vtkVector<T, 2>
150{
151public:
152 vtkVector2() = default;
153
154 explicit vtkVector2(const T& scalar)
155 : vtkVector<T, 2>(scalar)
156 {
157 }
158
159 explicit vtkVector2(const T* init)
160 : vtkVector<T, 2>(init)
161 {
162 }
163
164 vtkVector2(const T& x, const T& y)
165 {
166 this->Data[0] = x;
167 this->Data[1] = y;
168 }
169
171
174 void Set(const T& x, const T& y)
175 {
176 this->Data[0] = x;
177 this->Data[1] = y;
178 }
179
180
184 void SetX(const T& x) { this->Data[0] = x; }
185
189 const T& GetX() const { return this->Data[0]; }
190
194 void SetY(const T& y) { this->Data[1] = y; }
195
199 const T& GetY() const { return this->Data[1]; }
200
202
205 bool operator<(const vtkVector2<T>& v) const
206 {
207 return (this->Data[0] < v.Data[0]) || (this->Data[0] == v.Data[0] && this->Data[1] < v.Data[1]);
208 }
209
210};
211
212// .NAME vtkVector3 - templated base type for storage of 3D vectors.
213//
214template <typename T>
215class vtkVector3 : public vtkVector<T, 3>
216{
217public:
218 vtkVector3() = default;
219
220 explicit vtkVector3(const T& scalar)
221 : vtkVector<T, 3>(scalar)
222 {
223 }
224
225 explicit vtkVector3(const T* init)
226 : vtkVector<T, 3>(init)
227 {
228 }
229
230 vtkVector3(const T& x, const T& y, const T& z)
231 {
232 this->Data[0] = x;
233 this->Data[1] = y;
234 this->Data[2] = z;
235 }
236
238
241 void Set(const T& x, const T& y, const T& z)
242 {
243 this->Data[0] = x;
244 this->Data[1] = y;
245 this->Data[2] = z;
246 }
247
248
252 void SetX(const T& x) { this->Data[0] = x; }
253
257 const T& GetX() const { return this->Data[0]; }
258
262 void SetY(const T& y) { this->Data[1] = y; }
263
267 const T& GetY() const { return this->Data[1]; }
268
272 void SetZ(const T& z) { this->Data[2] = z; }
273
277 const T& GetZ() const { return this->Data[2]; }
278
280
284 {
285 vtkVector3<T> res;
286 res[0] = this->Data[1] * other.Data[2] - this->Data[2] * other.Data[1];
287 res[1] = this->Data[2] * other.Data[0] - this->Data[0] * other.Data[2];
288 res[2] = this->Data[0] * other.Data[1] - this->Data[1] * other.Data[0];
289 return res;
290 }
291
292
294
297 bool operator<(const vtkVector3<T>& v) const
298 {
299 return (this->Data[0] < v.Data[0]) ||
300 (this->Data[0] == v.Data[0] && this->Data[1] < v.Data[1]) ||
301 (this->Data[0] == v.Data[0] && this->Data[1] == v.Data[1] && this->Data[2] < v.Data[2]);
302 }
303
304};
305
306// .NAME vtkVector4 - templated base type for storage of 4D vectors.
307//
308template <typename T>
309class vtkVector4 : public vtkVector<T, 4>
310{
311public:
312 vtkVector4() = default;
313
314 explicit vtkVector4(const T& scalar)
315 : vtkVector<T, 4>(scalar)
316 {
317 }
318
319 explicit vtkVector4(const T* init)
320 : vtkVector<T, 4>(init)
321 {
322 }
323
324 vtkVector4(const T& x, const T& y, const T& z, const T& w)
325 {
326 this->Data[0] = x;
327 this->Data[1] = y;
328 this->Data[2] = z;
329 this->Data[3] = w;
330 }
331
333
336 void Set(const T& x, const T& y, const T& z, const T& w)
337 {
338 this->Data[0] = x;
339 this->Data[1] = y;
340 this->Data[2] = z;
341 this->Data[3] = w;
342 }
343
344
348 void SetX(const T& x) { this->Data[0] = x; }
349
353 const T& GetX() const { return this->Data[0]; }
354
358 void SetY(const T& y) { this->Data[1] = y; }
359
363 const T& GetY() const { return this->Data[1]; }
364
368 void SetZ(const T& z) { this->Data[2] = z; }
369
373 const T& GetZ() const { return this->Data[2]; }
374
378 void SetW(const T& w) { this->Data[3] = w; }
379
383 const T& GetW() const { return this->Data[3]; }
384};
385
389#define vtkVectorNormalized(vectorType, type, size) \
390 vectorType Normalized() const \
391 { \
392 return vectorType(vtkVector<type, size>::Normalized().GetData()); \
393 }
394
395#define vtkVectorDerivedMacro(vectorType, type, size) \
396 vtkVectorNormalized(vectorType, type, size); \
397 explicit vectorType(type s) \
398 : Superclass(s) \
399 { \
400 } \
401 explicit vectorType(const type* i) \
402 : Superclass(i) \
403 { \
404 } \
405 explicit vectorType(const vtkTuple<type, size>& o) \
406 : Superclass(o.GetData()) \
407 { \
408 } \
409 vectorType(const vtkVector<type, size>& o) \
410 : Superclass(o.GetData()) \
411 { \
412 }
413
415
418class vtkVector2i : public vtkVector2<int>
419{
420public:
422 vtkVector2i() = default;
423 vtkVector2i(int x, int y)
424 : vtkVector2<int>(x, y)
425 {
426 }
428};
429
430
431class vtkVector2f : public vtkVector2<float>
432{
433public:
435 vtkVector2f() = default;
436 vtkVector2f(float x, float y)
437 : vtkVector2<float>(x, y)
438 {
439 }
441};
442
443class vtkVector2d : public vtkVector2<double>
444{
445public:
447 vtkVector2d() = default;
448 vtkVector2d(double x, double y)
449 : vtkVector2<double>(x, y)
450 {
451 }
453};
454
455#define vtkVector3Cross(vectorType, type) \
456 vectorType Cross(const vectorType& other) const \
457 { \
458 return vectorType(vtkVector3<type>::Cross(other).GetData()); \
459 }
460
461class vtkVector3i : public vtkVector3<int>
462{
463public:
465 vtkVector3i() = default;
466 vtkVector3i(int x, int y, int z)
467 : vtkVector3<int>(x, y, z)
468 {
469 }
472};
473
474class vtkVector3f : public vtkVector3<float>
475{
476public:
478 vtkVector3f() = default;
479 vtkVector3f(float x, float y, float z)
480 : vtkVector3<float>(x, y, z)
481 {
482 }
485};
486
487class vtkVector3d : public vtkVector3<double>
488{
489public:
491 vtkVector3d() = default;
492 vtkVector3d(double x, double y, double z)
493 : vtkVector3<double>(x, y, z)
494 {
495 }
498};
499
500class vtkVector4i : public vtkVector4<int>
501{
502public:
504 vtkVector4i() = default;
505 vtkVector4i(int x, int y, int z, int w)
506 : vtkVector4<int>(x, y, z, w)
507 {
508 }
510};
511
512class vtkVector4d : public vtkVector4<double>
513{
514public:
516 vtkVector4d() = default;
517 vtkVector4d(double x, double y, double z, double w)
518 : vtkVector4<double>(x, y, z, w){};
520};
521
522#endif // vtkVector_h
523// VTK-HeaderTest-Exclude: vtkVector.h
T Data[Size]
The only thing stored in memory!
Definition vtkTuple.h:154
vtkTuple()=default
The default constructor does not initialize values.
const T & GetX() const
Get the x component of the vector, i.e.
Definition vtkVector.h:189
const T & GetY() const
Get the y component of the vector, i.e.
Definition vtkVector.h:199
void Set(const T &x, const T &y)
Set the x and y components of the vector.
Definition vtkVector.h:174
vtkVector2(const T *init)
Definition vtkVector.h:159
void SetY(const T &y)
Set the y component of the vector, i.e.
Definition vtkVector.h:194
vtkVector2(const T &x, const T &y)
Definition vtkVector.h:164
vtkVector2(const T &scalar)
Definition vtkVector.h:154
void SetX(const T &x)
Set the x component of the vector, i.e.
Definition vtkVector.h:184
bool operator<(const vtkVector2< T > &v) const
Lexicographical comparison of two vector.
Definition vtkVector.h:205
vtkVector2()=default
vtkVector2d()=default
vtkVectorDerivedMacro(vtkVector2d, double, 2)
vtkVector2d(double x, double y)
Definition vtkVector.h:448
vtkVector2< double > Superclass
Definition vtkVector.h:446
vtkVector2f()=default
vtkVector2< float > Superclass
Definition vtkVector.h:434
vtkVector2f(float x, float y)
Definition vtkVector.h:436
vtkVectorDerivedMacro(vtkVector2f, float, 2)
vtkVector2i()=default
vtkVector2i(int x, int y)
Definition vtkVector.h:423
vtkVector2< int > Superclass
Definition vtkVector.h:421
vtkVectorDerivedMacro(vtkVector2i, int, 2)
void SetZ(const T &z)
Set the z component of the vector, i.e.
Definition vtkVector.h:272
const T & GetY() const
Get the y component of the vector, i.e.
Definition vtkVector.h:267
bool operator<(const vtkVector3< T > &v) const
Lexicographical comparison of two vector.
Definition vtkVector.h:297
vtkVector3(const T *init)
Definition vtkVector.h:225
const T & GetZ() const
Get the z component of the vector, i.e.
Definition vtkVector.h:277
void SetX(const T &x)
Set the x component of the vector, i.e.
Definition vtkVector.h:252
vtkVector3(const T &scalar)
Definition vtkVector.h:220
void Set(const T &x, const T &y, const T &z)
Set the x, y and z components of the vector.
Definition vtkVector.h:241
vtkVector3(const T &x, const T &y, const T &z)
Definition vtkVector.h:230
vtkVector3< T > Cross(const vtkVector3< T > &other) const
Return the cross product of this X other.
Definition vtkVector.h:283
const T & GetX() const
Get the x component of the vector, i.e.
Definition vtkVector.h:257
void SetY(const T &y)
Set the y component of the vector, i.e.
Definition vtkVector.h:262
vtkVector3()=default
vtkVector3Cross(vtkVector3d, double)
vtkVector3< double > Superclass
Definition vtkVector.h:490
vtkVector3d(double x, double y, double z)
Definition vtkVector.h:492
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:479
vtkVector3< float > Superclass
Definition vtkVector.h:477
vtkVector3Cross(vtkVector3i, int)
vtkVectorDerivedMacro(vtkVector3i, int, 3)
vtkVector3< int > Superclass
Definition vtkVector.h:464
vtkVector3i()=default
vtkVector3i(int x, int y, int z)
Definition vtkVector.h:466
void SetY(const T &y)
Set the y component of the vector, i.e.
Definition vtkVector.h:358
vtkVector4(const T &x, const T &y, const T &z, const T &w)
Definition vtkVector.h:324
void SetZ(const T &z)
Set the z component of the vector, i.e.
Definition vtkVector.h:368
const T & GetZ() const
Get the z component of the vector, i.e.
Definition vtkVector.h:373
void SetW(const T &w)
Set the w component of the vector, i.e.
Definition vtkVector.h:378
vtkVector4(const T *init)
Definition vtkVector.h:319
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:336
vtkVector4()=default
const T & GetY() const
Get the y component of the vector, i.e.
Definition vtkVector.h:363
void SetX(const T &x)
Set the x component of the vector, i.e.
Definition vtkVector.h:348
vtkVector4(const T &scalar)
Definition vtkVector.h:314
const T & GetX() const
Get the x component of the vector, i.e.
Definition vtkVector.h:353
const T & GetW() const
Get the w component of the vector, i.e.
Definition vtkVector.h:383
vtkVectorDerivedMacro(vtkVector4d, double, 4)
vtkVector4< double > Superclass
Definition vtkVector.h:515
vtkVector4d(double x, double y, double z, double w)
Definition vtkVector.h:517
vtkVector4d()=default
vtkVector4< int > Superclass
Definition vtkVector.h:503
vtkVector4i(int x, int y, int z, int w)
Definition vtkVector.h:505
vtkVector4i()=default
vtkVectorDerivedMacro(vtkVector4i, int, 4)
vtkVector(const T &scalar)
Initialize all of the vector's elements with the supplied scalar.
Definition vtkVector.h:45
T Dot(const vtkVector< T, Size > &other) const
The dot product of this and the supplied vector.
Definition vtkVector.h:118
double Normalize()
Normalize the vector in place.
Definition vtkVector.h:85
vtkVector()=default
vtkVector(const T *init)
Initialize the vector's elements with the elements of the supplied array.
Definition vtkVector.h:55
vtkVector< TR, Size > Cast() const
Cast the vector to the specified type, returning the result.
Definition vtkVector.h:134
double Norm() const
Get the norm of the vector, i.e.
Definition vtkVector.h:78
vtkVector< T, Size > Normalized() const
Return the normalized form of this vector.
Definition vtkVector.h:106
T SquaredNorm() const
Get the squared norm of the vector.
Definition vtkVector.h:64