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
31#ifndef vtkVector_h
32#define vtkVector_h
33
34#include "vtkObject.h" // for legacy macros
35#include "vtkTuple.h"
36
37#include <cmath> // For math functions
38
39template <typename T, int Size>
40class vtkVector : public vtkTuple<T, Size>
41{
42public:
43 vtkVector() = default;
44
48 explicit vtkVector(const T& scalar)
49 : vtkTuple<T, Size>(scalar)
50 {
51 }
52
58 explicit vtkVector(const T* init)
59 : vtkTuple<T, Size>(init)
60 {
61 }
62
64
67 T SquaredNorm() const
68 {
69 T result = 0;
70 for (int i = 0; i < Size; ++i)
71 {
72 result += this->Data[i] * this->Data[i];
73 }
74 return result;
75 }
77
81 double Norm() const { return sqrt(static_cast<double>(this->SquaredNorm())); }
82
84
88 double Normalize()
89 {
90 const double norm(this->Norm());
91 if (norm == 0.0)
92 {
93 return 0.0;
94 }
95 const double inv(1.0 / norm);
96 for (int i = 0; i < Size; ++i)
97 {
98 this->Data[i] = static_cast<T>(this->Data[i] * inv);
99 }
100 return norm;
101 }
103
105
110 {
111 vtkVector<T, Size> temp(*this);
112 temp.Normalize();
113 return temp;
114 }
116
118
121 T Dot(const vtkVector<T, Size>& other) const
122 {
123 T result(0);
124 for (int i = 0; i < Size; ++i)
125 {
126 result += this->Data[i] * other[i];
127 }
128 return result;
129 }
131
133
136 template <typename TR>
138 {
139 vtkVector<TR, Size> result;
140 for (int i = 0; i < Size; ++i)
141 {
142 result[i] = static_cast<TR>(this->Data[i]);
143 }
144 return result;
145 }
147};
148
149// .NAME vtkVector2 - templated base type for storage of 2D vectors.
150//
151template <typename T>
152class vtkVector2 : public vtkVector<T, 2>
153{
154public:
155 vtkVector2() = default;
156
157 explicit vtkVector2(const T& scalar)
158 : vtkVector<T, 2>(scalar)
159 {
160 }
161
162 explicit vtkVector2(const T* init)
163 : vtkVector<T, 2>(init)
164 {
165 }
166
167 vtkVector2(const T& x, const T& y)
168 {
169 this->Data[0] = x;
170 this->Data[1] = y;
171 }
172
174
177 void Set(const T& x, const T& y)
178 {
179 this->Data[0] = x;
180 this->Data[1] = y;
181 }
183
187 void SetX(const T& x) { this->Data[0] = x; }
188
192 const T& GetX() const { return this->Data[0]; }
193
197 void SetY(const T& y) { this->Data[1] = y; }
198
202 const T& GetY() const { return this->Data[1]; }
203
205
208 bool operator<(const vtkVector2<T>& v) const
209 {
210 return (this->Data[0] < v.Data[0]) || (this->Data[0] == v.Data[0] && this->Data[1] < v.Data[1]);
211 }
213};
214
215// .NAME vtkVector3 - templated base type for storage of 3D vectors.
216//
217template <typename T>
218class vtkVector3 : public vtkVector<T, 3>
219{
220public:
221 vtkVector3() = default;
222
223 explicit vtkVector3(const T& scalar)
224 : vtkVector<T, 3>(scalar)
225 {
226 }
227
228 explicit vtkVector3(const T* init)
229 : vtkVector<T, 3>(init)
230 {
231 }
232
233 vtkVector3(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 }
239
241
244 void Set(const T& x, const T& y, const T& z)
245 {
246 this->Data[0] = x;
247 this->Data[1] = y;
248 this->Data[2] = z;
249 }
251
255 void SetX(const T& x) { this->Data[0] = x; }
256
260 const T& GetX() const { return this->Data[0]; }
261
265 void SetY(const T& y) { this->Data[1] = y; }
266
270 const T& GetY() const { return this->Data[1]; }
271
275 void SetZ(const T& z) { this->Data[2] = z; }
276
280 const T& GetZ() const { return this->Data[2]; }
281
283
287 {
288 vtkVector3<T> res;
289 res[0] = this->Data[1] * other.Data[2] - this->Data[2] * other.Data[1];
290 res[1] = this->Data[2] * other.Data[0] - this->Data[0] * other.Data[2];
291 res[2] = this->Data[0] * other.Data[1] - this->Data[1] * other.Data[0];
292 return res;
293 }
295
297
300 bool operator<(const vtkVector3<T>& v) const
301 {
302 return (this->Data[0] < v.Data[0]) ||
303 (this->Data[0] == v.Data[0] && this->Data[1] < v.Data[1]) ||
304 (this->Data[0] == v.Data[0] && this->Data[1] == v.Data[1] && this->Data[2] < v.Data[2]);
305 }
307};
308
309// .NAME vtkVector4 - templated base type for storage of 4D vectors.
310//
311template <typename T>
312class vtkVector4 : public vtkVector<T, 4>
313{
314public:
315 vtkVector4() = default;
316
317 explicit vtkVector4(const T& scalar)
318 : vtkVector<T, 4>(scalar)
319 {
320 }
321
322 explicit vtkVector4(const T* init)
323 : vtkVector<T, 4>(init)
324 {
325 }
326
327 vtkVector4(const T& x, const T& y, const T& z, const T& w)
328 {
329 this->Data[0] = x;
330 this->Data[1] = y;
331 this->Data[2] = z;
332 this->Data[3] = w;
333 }
334
336
339 void Set(const T& x, const T& y, const T& z, const T& w)
340 {
341 this->Data[0] = x;
342 this->Data[1] = y;
343 this->Data[2] = z;
344 this->Data[3] = w;
345 }
347
351 void SetX(const T& x) { this->Data[0] = x; }
352
356 const T& GetX() const { return this->Data[0]; }
357
361 void SetY(const T& y) { this->Data[1] = y; }
362
366 const T& GetY() const { return this->Data[1]; }
367
371 void SetZ(const T& z) { this->Data[2] = z; }
372
376 const T& GetZ() const { return this->Data[2]; }
377
381 void SetW(const T& w) { this->Data[3] = w; }
382
386 const T& GetW() const { return this->Data[3]; }
387};
388
392#define vtkVectorNormalized(vectorType, type, size) \
393 vectorType Normalized() const \
394 { \
395 return vectorType(vtkVector<type, size>::Normalized().GetData()); \
396 }
397
398#define vtkVectorDerivedMacro(vectorType, type, size) \
399 vtkVectorNormalized(vectorType, type, size); \
400 explicit vectorType(type s) \
401 : Superclass(s) \
402 { \
403 } \
404 explicit vectorType(const type* i) \
405 : Superclass(i) \
406 { \
407 } \
408 explicit vectorType(const vtkTuple<type, size>& o) \
409 : Superclass(o.GetData()) \
410 { \
411 } \
412 vectorType(const vtkVector<type, size>& o) \
413 : Superclass(o.GetData()) \
414 { \
415 }
416
418
421class vtkVector2i : public vtkVector2<int>
422{
423public:
425 vtkVector2i() = default;
426 vtkVector2i(int x, int y)
427 : vtkVector2<int>(x, y)
428 {
429 }
431};
433
434class vtkVector2f : public vtkVector2<float>
435{
436public:
438 vtkVector2f() = default;
439 vtkVector2f(float x, float y)
440 : vtkVector2<float>(x, y)
441 {
442 }
444};
445
446class vtkVector2d : public vtkVector2<double>
447{
448public:
450 vtkVector2d() = default;
451 vtkVector2d(double x, double y)
452 : vtkVector2<double>(x, y)
453 {
454 }
456};
457
458#define vtkVector3Cross(vectorType, type) \
459 vectorType Cross(const vectorType& other) const \
460 { \
461 return vectorType(vtkVector3<type>::Cross(other).GetData()); \
462 }
463
464class vtkVector3i : public vtkVector3<int>
465{
466public:
468 vtkVector3i() = default;
469 vtkVector3i(int x, int y, int z)
470 : vtkVector3<int>(x, y, z)
471 {
472 }
475};
476
477class vtkVector3f : public vtkVector3<float>
478{
479public:
481 vtkVector3f() = default;
482 vtkVector3f(float x, float y, float z)
483 : vtkVector3<float>(x, y, z)
484 {
485 }
488};
489
490class vtkVector3d : public vtkVector3<double>
491{
492public:
494 vtkVector3d() = default;
495 vtkVector3d(double x, double y, double z)
496 : vtkVector3<double>(x, y, z)
497 {
498 }
501};
502
503class vtkVector4i : public vtkVector4<int>
504{
505public:
507 vtkVector4i() = default;
508 vtkVector4i(int x, int y, int z, int w)
509 : vtkVector4<int>(x, y, z, w)
510 {
511 }
513};
514
515class vtkVector4d : public vtkVector4<double>
516{
517public:
519 vtkVector4d() = default;
520 vtkVector4d(double x, double y, double z, double w)
521 : vtkVector4<double>(x, y, z, w){};
523};
524
525#endif // vtkVector_h
526// 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:192
const T & GetY() const
Get the y component of the vector, i.e.
Definition vtkVector.h:202
void Set(const T &x, const T &y)
Set the x and y components of the vector.
Definition vtkVector.h:177
vtkVector2(const T *init)
Definition vtkVector.h:162
void SetY(const T &y)
Set the y component of the vector, i.e.
Definition vtkVector.h:197
vtkVector2(const T &x, const T &y)
Definition vtkVector.h:167
vtkVector2(const T &scalar)
Definition vtkVector.h:157
void SetX(const T &x)
Set the x component of the vector, i.e.
Definition vtkVector.h:187
bool operator<(const vtkVector2< T > &v) const
Lexicographical comparison of two vector.
Definition vtkVector.h:208
vtkVector2()=default
vtkVector2d()=default
vtkVectorDerivedMacro(vtkVector2d, double, 2)
vtkVector2d(double x, double y)
Definition vtkVector.h:451
vtkVector2< double > Superclass
Definition vtkVector.h:449
vtkVector2f()=default
vtkVector2< float > Superclass
Definition vtkVector.h:437
vtkVector2f(float x, float y)
Definition vtkVector.h:439
vtkVectorDerivedMacro(vtkVector2f, float, 2)
Some derived classes for the different vectors commonly used.
Definition vtkVector.h:422
vtkVector2i()=default
vtkVector2i(int x, int y)
Definition vtkVector.h:426
vtkVector2< int > Superclass
Definition vtkVector.h:424
vtkVectorDerivedMacro(vtkVector2i, int, 2)
void SetZ(const T &z)
Set the z component of the vector, i.e.
Definition vtkVector.h:275
const T & GetY() const
Get the y component of the vector, i.e.
Definition vtkVector.h:270
bool operator<(const vtkVector3< T > &v) const
Lexicographical comparison of two vector.
Definition vtkVector.h:300
vtkVector3(const T *init)
Definition vtkVector.h:228
const T & GetZ() const
Get the z component of the vector, i.e.
Definition vtkVector.h:280
void SetX(const T &x)
Set the x component of the vector, i.e.
Definition vtkVector.h:255
vtkVector3(const T &scalar)
Definition vtkVector.h:223
void Set(const T &x, const T &y, const T &z)
Set the x, y and z components of the vector.
Definition vtkVector.h:244
vtkVector3(const T &x, const T &y, const T &z)
Definition vtkVector.h:233
vtkVector3< T > Cross(const vtkVector3< T > &other) const
Return the cross product of this X other.
Definition vtkVector.h:286
const T & GetX() const
Get the x component of the vector, i.e.
Definition vtkVector.h:260
void SetY(const T &y)
Set the y component of the vector, i.e.
Definition vtkVector.h:265
vtkVector3()=default
vtkVector3Cross(vtkVector3d, double)
vtkVector3< double > Superclass
Definition vtkVector.h:493
vtkVector3d(double x, double y, double z)
Definition vtkVector.h:495
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:482
vtkVector3< float > Superclass
Definition vtkVector.h:480
vtkVector3Cross(vtkVector3i, int)
vtkVectorDerivedMacro(vtkVector3i, int, 3)
vtkVector3< int > Superclass
Definition vtkVector.h:467
vtkVector3i()=default
vtkVector3i(int x, int y, int z)
Definition vtkVector.h:469
void SetY(const T &y)
Set the y component of the vector, i.e.
Definition vtkVector.h:361
vtkVector4(const T &x, const T &y, const T &z, const T &w)
Definition vtkVector.h:327
void SetZ(const T &z)
Set the z component of the vector, i.e.
Definition vtkVector.h:371
const T & GetZ() const
Get the z component of the vector, i.e.
Definition vtkVector.h:376
void SetW(const T &w)
Set the w component of the vector, i.e.
Definition vtkVector.h:381
vtkVector4(const T *init)
Definition vtkVector.h:322
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:339
vtkVector4()=default
const T & GetY() const
Get the y component of the vector, i.e.
Definition vtkVector.h:366
void SetX(const T &x)
Set the x component of the vector, i.e.
Definition vtkVector.h:351
vtkVector4(const T &scalar)
Definition vtkVector.h:317
const T & GetX() const
Get the x component of the vector, i.e.
Definition vtkVector.h:356
const T & GetW() const
Get the w component of the vector, i.e.
Definition vtkVector.h:386
vtkVectorDerivedMacro(vtkVector4d, double, 4)
vtkVector4d(double x, double y, double z, double w)
Definition vtkVector.h:520
vtkVector4d()=default
vtkVector4< int > Superclass
Definition vtkVector.h:506
vtkVector4i(int x, int y, int z, int w)
Definition vtkVector.h:508
vtkVector4i()=default
vtkVectorDerivedMacro(vtkVector4i, int, 4)
templated base type for storage of vectors.
Definition vtkVector.h:41
vtkVector(const T &scalar)
Initialize all of the vector's elements with the supplied scalar.
Definition vtkVector.h:48
T Dot(const vtkVector< T, Size > &other) const
The dot product of this and the supplied vector.
Definition vtkVector.h:121
double Normalize()
Normalize the vector in place.
Definition vtkVector.h:88
vtkVector()=default
vtkVector(const T *init)
Initialize the vector's elements with the elements of the supplied array.
Definition vtkVector.h:58
vtkVector< TR, Size > Cast() const
Cast the vector to the specified type, returning the result.
Definition vtkVector.h:137
double Norm() const
Get the norm of the vector, i.e.
Definition vtkVector.h:81
vtkVector< T, Size > Normalized() const
Return the normalized form of this vector.
Definition vtkVector.h:109
T SquaredNorm() const
Get the squared norm of the vector.
Definition vtkVector.h:67