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
90#ifndef vtkVector_h
91#define vtkVector_h
92
93#include "vtkObject.h" // for legacy macros
94#include "vtkTuple.h"
95
96#include <cmath> // For math functions
97
98template <typename T, int Size>
99class vtkVector : public vtkTuple<T, Size>
100{
101public:
102 vtkVector() = default;
103
107 explicit vtkVector(const T& scalar)
108 : vtkTuple<T, Size>(scalar)
109 {
110 }
111
117 explicit vtkVector(const T* init)
118 : vtkTuple<T, Size>(init)
119 {
120 }
121
123
126 T SquaredNorm() const
127 {
128 T result = 0;
129 for (int i = 0; i < Size; ++i)
130 {
131 result += this->Data[i] * this->Data[i];
132 }
133 return result;
134 }
136
140 double Norm() const { return sqrt(static_cast<double>(this->SquaredNorm())); }
141
143
147 double Normalize()
148 {
149 const double norm(this->Norm());
150 if (norm == 0.0)
151 {
152 return 0.0;
153 }
154 const double inv(1.0 / norm);
155 for (int i = 0; i < Size; ++i)
156 {
157 this->Data[i] = static_cast<T>(this->Data[i] * inv);
158 }
159 return norm;
160 }
162
164
169 {
170 vtkVector<T, Size> temp(*this);
171 temp.Normalize();
172 return temp;
173 }
175
177
180 T Dot(const vtkVector<T, Size>& other) const
181 {
182 T result(0);
183 for (int i = 0; i < Size; ++i)
184 {
185 result += this->Data[i] * other[i];
186 }
187 return result;
188 }
190
192
195 template <typename TR>
197 {
198 vtkVector<TR, Size> result;
199 for (int i = 0; i < Size; ++i)
200 {
201 result[i] = static_cast<TR>(this->Data[i]);
202 }
203 return result;
204 }
206};
207
208// .NAME vtkVector2 - templated base type for storage of 2D vectors.
209//
210template <typename T>
211class vtkVector2 : public vtkVector<T, 2>
212{
213public:
214 vtkVector2() = default;
215
216 explicit vtkVector2(const T& scalar)
217 : vtkVector<T, 2>(scalar)
218 {
219 }
220
221 explicit vtkVector2(const T* init)
222 : vtkVector<T, 2>(init)
223 {
224 }
225
226 vtkVector2(const T& x, const T& y)
227 {
228 this->Data[0] = x;
229 this->Data[1] = y;
230 }
231
233
236 void Set(const T& x, const T& y)
237 {
238 this->Data[0] = x;
239 this->Data[1] = y;
240 }
242
246 void SetX(const T& x) { this->Data[0] = x; }
247
251 const T& GetX() const { return this->Data[0]; }
252
256 void SetY(const T& y) { this->Data[1] = y; }
257
261 const T& GetY() const { return this->Data[1]; }
262
264
267 bool operator<(const vtkVector2<T>& v) const
268 {
269 return (this->Data[0] < v.Data[0]) || (this->Data[0] == v.Data[0] && this->Data[1] < v.Data[1]);
270 }
272};
273
274// .NAME vtkVector3 - templated base type for storage of 3D vectors.
275//
276template <typename T>
277class vtkVector3 : public vtkVector<T, 3>
278{
279public:
280 vtkVector3() = default;
281
282 explicit vtkVector3(const T& scalar)
283 : vtkVector<T, 3>(scalar)
284 {
285 }
286
287 explicit vtkVector3(const T* init)
288 : vtkVector<T, 3>(init)
289 {
290 }
291
292 vtkVector3(const T& x, const T& y, const T& z)
293 {
294 this->Data[0] = x;
295 this->Data[1] = y;
296 this->Data[2] = z;
297 }
298
300
303 void Set(const T& x, const T& y, const T& z)
304 {
305 this->Data[0] = x;
306 this->Data[1] = y;
307 this->Data[2] = z;
308 }
310
314 void SetX(const T& x) { this->Data[0] = x; }
315
319 const T& GetX() const { return this->Data[0]; }
320
324 void SetY(const T& y) { this->Data[1] = y; }
325
329 const T& GetY() const { return this->Data[1]; }
330
334 void SetZ(const T& z) { this->Data[2] = z; }
335
339 const T& GetZ() const { return this->Data[2]; }
340
342
346 {
347 vtkVector3<T> res;
348 res[0] = this->Data[1] * other.Data[2] - this->Data[2] * other.Data[1];
349 res[1] = this->Data[2] * other.Data[0] - this->Data[0] * other.Data[2];
350 res[2] = this->Data[0] * other.Data[1] - this->Data[1] * other.Data[0];
351 return res;
352 }
354
356
359 bool operator<(const vtkVector3<T>& v) const
360 {
361 return (this->Data[0] < v.Data[0]) ||
362 (this->Data[0] == v.Data[0] && this->Data[1] < v.Data[1]) ||
363 (this->Data[0] == v.Data[0] && this->Data[1] == v.Data[1] && this->Data[2] < v.Data[2]);
364 }
366};
367
368// .NAME vtkVector4 - templated base type for storage of 4D vectors.
369//
370template <typename T>
371class vtkVector4 : public vtkVector<T, 4>
372{
373public:
374 vtkVector4() = default;
375
376 explicit vtkVector4(const T& scalar)
377 : vtkVector<T, 4>(scalar)
378 {
379 }
380
381 explicit vtkVector4(const T* init)
382 : vtkVector<T, 4>(init)
383 {
384 }
385
386 vtkVector4(const T& x, const T& y, const T& z, const T& w)
387 {
388 this->Data[0] = x;
389 this->Data[1] = y;
390 this->Data[2] = z;
391 this->Data[3] = w;
392 }
393
395
398 void Set(const T& x, const T& y, const T& z, const T& w)
399 {
400 this->Data[0] = x;
401 this->Data[1] = y;
402 this->Data[2] = z;
403 this->Data[3] = w;
404 }
406
410 void SetX(const T& x) { this->Data[0] = x; }
411
415 const T& GetX() const { return this->Data[0]; }
416
420 void SetY(const T& y) { this->Data[1] = y; }
421
425 const T& GetY() const { return this->Data[1]; }
426
430 void SetZ(const T& z) { this->Data[2] = z; }
431
435 const T& GetZ() const { return this->Data[2]; }
436
440 void SetW(const T& w) { this->Data[3] = w; }
441
445 const T& GetW() const { return this->Data[3]; }
446};
447
451#define vtkVectorNormalized(vectorType, type, size) \
452 vectorType Normalized() const \
453 { \
454 return vectorType(vtkVector<type, size>::Normalized().GetData()); \
455 }
456
457#define vtkVectorDerivedMacro(vectorType, type, size) \
458 vtkVectorNormalized(vectorType, type, size); \
459 explicit vectorType(type s) \
460 : Superclass(s) \
461 { \
462 } \
463 explicit vectorType(const type* i) \
464 : Superclass(i) \
465 { \
466 } \
467 explicit vectorType(const vtkTuple<type, size>& o) \
468 : Superclass(o.GetData()) \
469 { \
470 } \
471 vectorType(const vtkVector<type, size>& o) \
472 : Superclass(o.GetData()) \
473 { \
474 }
475
477
480class vtkVector2i : public vtkVector2<int>
481{
482public:
484 vtkVector2i() = default;
485 vtkVector2i(int x, int y)
486 : vtkVector2<int>(x, y)
487 {
488 }
490};
492
493class vtkVector2f : public vtkVector2<float>
494{
495public:
497 vtkVector2f() = default;
498 vtkVector2f(float x, float y)
499 : vtkVector2<float>(x, y)
500 {
501 }
503};
504
505class vtkVector2d : public vtkVector2<double>
506{
507public:
509 vtkVector2d() = default;
510 vtkVector2d(double x, double y)
511 : vtkVector2<double>(x, y)
512 {
513 }
515};
516
517#define vtkVector3Cross(vectorType, type) \
518 vectorType Cross(const vectorType& other) const \
519 { \
520 return vectorType(vtkVector3<type>::Cross(other).GetData()); \
521 }
522
523class vtkVector3i : public vtkVector3<int>
524{
525public:
527 vtkVector3i() = default;
528 vtkVector3i(int x, int y, int z)
529 : vtkVector3<int>(x, y, z)
530 {
531 }
534};
535
536class vtkVector3f : public vtkVector3<float>
537{
538public:
540 vtkVector3f() = default;
541 vtkVector3f(float x, float y, float z)
542 : vtkVector3<float>(x, y, z)
543 {
544 }
547};
548
549class vtkVector3d : public vtkVector3<double>
550{
551public:
553 vtkVector3d() = default;
554 vtkVector3d(double x, double y, double z)
555 : vtkVector3<double>(x, y, z)
556 {
557 }
560};
561
562class vtkVector4i : public vtkVector4<int>
563{
564public:
566 vtkVector4i() = default;
567 vtkVector4i(int x, int y, int z, int w)
568 : vtkVector4<int>(x, y, z, w)
569 {
570 }
572};
573
574class vtkVector4d : public vtkVector4<double>
575{
576public:
578 vtkVector4d() = default;
579 vtkVector4d(double x, double y, double z, double w)
580 : vtkVector4<double>(x, y, z, w){};
582};
583
584#endif // vtkVector_h
585// 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:251
const T & GetY() const
Get the y component of the vector, i.e.
Definition: vtkVector.h:261
void Set(const T &x, const T &y)
Set the x and y components of the vector.
Definition: vtkVector.h:236
vtkVector2(const T *init)
Definition: vtkVector.h:221
void SetY(const T &y)
Set the y component of the vector, i.e.
Definition: vtkVector.h:256
vtkVector2(const T &x, const T &y)
Definition: vtkVector.h:226
vtkVector2(const T &scalar)
Definition: vtkVector.h:216
void SetX(const T &x)
Set the x component of the vector, i.e.
Definition: vtkVector.h:246
bool operator<(const vtkVector2< T > &v) const
Lexicographical comparison of two vector.
Definition: vtkVector.h:267
vtkVector2()=default
vtkVector2d()=default
vtkVectorDerivedMacro(vtkVector2d, double, 2)
vtkVector2d(double x, double y)
Definition: vtkVector.h:510
vtkVector2< double > Superclass
Definition: vtkVector.h:508
vtkVector2f()=default
vtkVector2< float > Superclass
Definition: vtkVector.h:496
vtkVector2f(float x, float y)
Definition: vtkVector.h:498
vtkVectorDerivedMacro(vtkVector2f, float, 2)
Some derived classes for the different vectors commonly used.
Definition: vtkVector.h:481
vtkVector2i()=default
vtkVector2i(int x, int y)
Definition: vtkVector.h:485
vtkVector2< int > Superclass
Definition: vtkVector.h:483
vtkVectorDerivedMacro(vtkVector2i, int, 2)
void SetZ(const T &z)
Set the z component of the vector, i.e.
Definition: vtkVector.h:334
const T & GetY() const
Get the y component of the vector, i.e.
Definition: vtkVector.h:329
bool operator<(const vtkVector3< T > &v) const
Lexicographical comparison of two vector.
Definition: vtkVector.h:359
vtkVector3(const T *init)
Definition: vtkVector.h:287
const T & GetZ() const
Get the z component of the vector, i.e.
Definition: vtkVector.h:339
void SetX(const T &x)
Set the x component of the vector, i.e.
Definition: vtkVector.h:314
vtkVector3(const T &scalar)
Definition: vtkVector.h:282
void Set(const T &x, const T &y, const T &z)
Set the x, y and z components of the vector.
Definition: vtkVector.h:303
vtkVector3(const T &x, const T &y, const T &z)
Definition: vtkVector.h:292
vtkVector3< T > Cross(const vtkVector3< T > &other) const
Return the cross product of this X other.
Definition: vtkVector.h:345
const T & GetX() const
Get the x component of the vector, i.e.
Definition: vtkVector.h:319
void SetY(const T &y)
Set the y component of the vector, i.e.
Definition: vtkVector.h:324
vtkVector3()=default
vtkVector3Cross(vtkVector3d, double)
vtkVector3< double > Superclass
Definition: vtkVector.h:552
vtkVector3d(double x, double y, double z)
Definition: vtkVector.h:554
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:541
vtkVector3< float > Superclass
Definition: vtkVector.h:539
vtkVector3Cross(vtkVector3i, int)
vtkVectorDerivedMacro(vtkVector3i, int, 3)
vtkVector3< int > Superclass
Definition: vtkVector.h:526
vtkVector3i()=default
vtkVector3i(int x, int y, int z)
Definition: vtkVector.h:528
void SetY(const T &y)
Set the y component of the vector, i.e.
Definition: vtkVector.h:420
vtkVector4(const T &x, const T &y, const T &z, const T &w)
Definition: vtkVector.h:386
void SetZ(const T &z)
Set the z component of the vector, i.e.
Definition: vtkVector.h:430
const T & GetZ() const
Get the z component of the vector, i.e.
Definition: vtkVector.h:435
void SetW(const T &w)
Set the w component of the vector, i.e.
Definition: vtkVector.h:440
vtkVector4(const T *init)
Definition: vtkVector.h:381
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:398
vtkVector4()=default
const T & GetY() const
Get the y component of the vector, i.e.
Definition: vtkVector.h:425
void SetX(const T &x)
Set the x component of the vector, i.e.
Definition: vtkVector.h:410
vtkVector4(const T &scalar)
Definition: vtkVector.h:376
const T & GetX() const
Get the x component of the vector, i.e.
Definition: vtkVector.h:415
const T & GetW() const
Get the w component of the vector, i.e.
Definition: vtkVector.h:445
vtkVectorDerivedMacro(vtkVector4d, double, 4)
vtkVector4d(double x, double y, double z, double w)
Definition: vtkVector.h:579
vtkVector4d()=default
vtkVector4< int > Superclass
Definition: vtkVector.h:565
vtkVector4i(int x, int y, int z, int w)
Definition: vtkVector.h:567
vtkVector4i()=default
vtkVectorDerivedMacro(vtkVector4i, int, 4)
templated base type for storage of vectors.
Definition: vtkVector.h:100
vtkVector(const T &scalar)
Initialize all of the vector's elements with the supplied scalar.
Definition: vtkVector.h:107
T Dot(const vtkVector< T, Size > &other) const
The dot product of this and the supplied vector.
Definition: vtkVector.h:180
double Normalize()
Normalize the vector in place.
Definition: vtkVector.h:147
vtkVector()=default
vtkVector(const T *init)
Initialize the vector's elements with the elements of the supplied array.
Definition: vtkVector.h:117
vtkVector< TR, Size > Cast() const
Cast the vector to the specified type, returning the result.
Definition: vtkVector.h:196
double Norm() const
Get the norm of the vector, i.e.
Definition: vtkVector.h:140
vtkVector< T, Size > Normalized() const
Return the normalized form of this vector.
Definition: vtkVector.h:168
T SquaredNorm() const
Get the squared norm of the vector.
Definition: vtkVector.h:126