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
87#ifndef vtkVector_h
88#define vtkVector_h
89
90#include "vtkObject.h" // for legacy macros
91#include "vtkTuple.h"
92
93#include <cmath> // For math functions
94
95template <typename T, int Size>
96class vtkVector : public vtkTuple<T, Size>
97{
98public:
99 vtkVector() = default;
100
104 explicit vtkVector(const T& scalar)
105 : vtkTuple<T, Size>(scalar)
106 {
107 }
108
114 explicit vtkVector(const T* init)
115 : vtkTuple<T, Size>(init)
116 {
117 }
118
120
123 T SquaredNorm() const
124 {
125 T result = 0;
126 for (int i = 0; i < Size; ++i)
127 {
128 result += this->Data[i] * this->Data[i];
129 }
130 return result;
131 }
133
137 double Norm() const { return sqrt(static_cast<double>(this->SquaredNorm())); }
138
140
144 double Normalize()
145 {
146 const double norm(this->Norm());
147 if (norm == 0.0)
148 {
149 return 0.0;
150 }
151 const double inv(1.0 / norm);
152 for (int i = 0; i < Size; ++i)
153 {
154 this->Data[i] = static_cast<T>(this->Data[i] * inv);
155 }
156 return norm;
157 }
159
161
166 {
167 vtkVector<T, Size> temp(*this);
168 temp.Normalize();
169 return temp;
170 }
172
174
177 T Dot(const vtkVector<T, Size>& other) const
178 {
179 T result(0);
180 for (int i = 0; i < Size; ++i)
181 {
182 result += this->Data[i] * other[i];
183 }
184 return result;
185 }
187
189
192 template <typename TR>
194 {
195 vtkVector<TR, Size> result;
196 for (int i = 0; i < Size; ++i)
197 {
198 result[i] = static_cast<TR>(this->Data[i]);
199 }
200 return result;
201 }
203};
204
205// .NAME vtkVector2 - templated base type for storage of 2D vectors.
206//
207template <typename T>
208class vtkVector2 : public vtkVector<T, 2>
209{
210public:
211 vtkVector2() = default;
212
213 explicit vtkVector2(const T& scalar)
214 : vtkVector<T, 2>(scalar)
215 {
216 }
217
218 explicit vtkVector2(const T* init)
219 : vtkVector<T, 2>(init)
220 {
221 }
222
223 vtkVector2(const T& x, const T& y)
224 {
225 this->Data[0] = x;
226 this->Data[1] = y;
227 }
228
230
233 void Set(const T& x, const T& y)
234 {
235 this->Data[0] = x;
236 this->Data[1] = y;
237 }
239
243 void SetX(const T& x) { this->Data[0] = x; }
244
248 const T& GetX() const { return this->Data[0]; }
249
253 void SetY(const T& y) { this->Data[1] = y; }
254
258 const T& GetY() const { return this->Data[1]; }
259
261
264 bool operator<(const vtkVector2<T>& v) const
265 {
266 return (this->Data[0] < v.Data[0]) || (this->Data[0] == v.Data[0] && this->Data[1] < v.Data[1]);
267 }
269};
270
271// .NAME vtkVector3 - templated base type for storage of 3D vectors.
272//
273template <typename T>
274class vtkVector3 : public vtkVector<T, 3>
275{
276public:
277 vtkVector3() = default;
278
279 explicit vtkVector3(const T& scalar)
280 : vtkVector<T, 3>(scalar)
281 {
282 }
283
284 explicit vtkVector3(const T* init)
285 : vtkVector<T, 3>(init)
286 {
287 }
288
289 vtkVector3(const T& x, const T& y, const T& z)
290 {
291 this->Data[0] = x;
292 this->Data[1] = y;
293 this->Data[2] = z;
294 }
295
297
300 void Set(const T& x, const T& y, const T& z)
301 {
302 this->Data[0] = x;
303 this->Data[1] = y;
304 this->Data[2] = z;
305 }
307
311 void SetX(const T& x) { this->Data[0] = x; }
312
316 const T& GetX() const { return this->Data[0]; }
317
321 void SetY(const T& y) { this->Data[1] = y; }
322
326 const T& GetY() const { return this->Data[1]; }
327
331 void SetZ(const T& z) { this->Data[2] = z; }
332
336 const T& GetZ() const { return this->Data[2]; }
337
339
343 {
344 vtkVector3<T> res;
345 res[0] = this->Data[1] * other.Data[2] - this->Data[2] * other.Data[1];
346 res[1] = this->Data[2] * other.Data[0] - this->Data[0] * other.Data[2];
347 res[2] = this->Data[0] * other.Data[1] - this->Data[1] * other.Data[0];
348 return res;
349 }
351
353
356 bool operator<(const vtkVector3<T>& v) const
357 {
358 return (this->Data[0] < v.Data[0]) ||
359 (this->Data[0] == v.Data[0] && this->Data[1] < v.Data[1]) ||
360 (this->Data[0] == v.Data[0] && this->Data[1] == v.Data[1] && this->Data[2] < v.Data[2]);
361 }
363};
364
365// .NAME vtkVector4 - templated base type for storage of 4D vectors.
366//
367template <typename T>
368class vtkVector4 : public vtkVector<T, 4>
369{
370public:
371 vtkVector4() = default;
372
373 explicit vtkVector4(const T& scalar)
374 : vtkVector<T, 4>(scalar)
375 {
376 }
377
378 explicit vtkVector4(const T* init)
379 : vtkVector<T, 4>(init)
380 {
381 }
382
383 vtkVector4(const T& x, const T& y, const T& z, const T& w)
384 {
385 this->Data[0] = x;
386 this->Data[1] = y;
387 this->Data[2] = z;
388 this->Data[3] = w;
389 }
390
392
395 void Set(const T& x, const T& y, const T& z, const T& w)
396 {
397 this->Data[0] = x;
398 this->Data[1] = y;
399 this->Data[2] = z;
400 this->Data[3] = w;
401 }
403
407 void SetX(const T& x) { this->Data[0] = x; }
408
412 const T& GetX() const { return this->Data[0]; }
413
417 void SetY(const T& y) { this->Data[1] = y; }
418
422 const T& GetY() const { return this->Data[1]; }
423
427 void SetZ(const T& z) { this->Data[2] = z; }
428
432 const T& GetZ() const { return this->Data[2]; }
433
437 void SetW(const T& w) { this->Data[3] = w; }
438
442 const T& GetW() const { return this->Data[3]; }
443};
444
448#define vtkVectorNormalized(vectorType, type, size) \
449 vectorType Normalized() const \
450 { \
451 return vectorType(vtkVector<type, size>::Normalized().GetData()); \
452 }
453
454#define vtkVectorDerivedMacro(vectorType, type, size) \
455 vtkVectorNormalized(vectorType, type, size); \
456 explicit vectorType(type s) \
457 : Superclass(s) \
458 { \
459 } \
460 explicit vectorType(const type* i) \
461 : Superclass(i) \
462 { \
463 } \
464 explicit vectorType(const vtkTuple<type, size>& o) \
465 : Superclass(o.GetData()) \
466 { \
467 } \
468 vectorType(const vtkVector<type, size>& o) \
469 : Superclass(o.GetData()) \
470 { \
471 }
472
474
477class vtkVector2i : public vtkVector2<int>
478{
479public:
481 vtkVector2i() = default;
482 vtkVector2i(int x, int y)
483 : vtkVector2<int>(x, y)
484 {
485 }
487};
489
490class vtkVector2f : public vtkVector2<float>
491{
492public:
494 vtkVector2f() = default;
495 vtkVector2f(float x, float y)
496 : vtkVector2<float>(x, y)
497 {
498 }
500};
501
502class vtkVector2d : public vtkVector2<double>
503{
504public:
506 vtkVector2d() = default;
507 vtkVector2d(double x, double y)
508 : vtkVector2<double>(x, y)
509 {
510 }
512};
513
514#define vtkVector3Cross(vectorType, type) \
515 vectorType Cross(const vectorType& other) const \
516 { \
517 return vectorType(vtkVector3<type>::Cross(other).GetData()); \
518 }
519
520class vtkVector3i : public vtkVector3<int>
521{
522public:
524 vtkVector3i() = default;
525 vtkVector3i(int x, int y, int z)
526 : vtkVector3<int>(x, y, z)
527 {
528 }
531};
532
533class vtkVector3f : public vtkVector3<float>
534{
535public:
537 vtkVector3f() = default;
538 vtkVector3f(float x, float y, float z)
539 : vtkVector3<float>(x, y, z)
540 {
541 }
544};
545
546class vtkVector3d : public vtkVector3<double>
547{
548public:
550 vtkVector3d() = default;
551 vtkVector3d(double x, double y, double z)
552 : vtkVector3<double>(x, y, z)
553 {
554 }
557};
558
559class vtkVector4i : public vtkVector4<int>
560{
561public:
563 vtkVector4i() = default;
564 vtkVector4i(int x, int y, int z, int w)
565 : vtkVector4<int>(x, y, z, w)
566 {
567 }
569};
570
571class vtkVector4d : public vtkVector4<double>
572{
573public:
575 vtkVector4d() = default;
576 vtkVector4d(double x, double y, double z, double w)
577 : vtkVector4<double>(x, y, z, w){};
579};
580
581#endif // vtkVector_h
582// 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:248
const T & GetY() const
Get the y component of the vector, i.e.
Definition: vtkVector.h:258
void Set(const T &x, const T &y)
Set the x and y components of the vector.
Definition: vtkVector.h:233
vtkVector2(const T *init)
Definition: vtkVector.h:218
void SetY(const T &y)
Set the y component of the vector, i.e.
Definition: vtkVector.h:253
vtkVector2(const T &x, const T &y)
Definition: vtkVector.h:223
vtkVector2(const T &scalar)
Definition: vtkVector.h:213
void SetX(const T &x)
Set the x component of the vector, i.e.
Definition: vtkVector.h:243
bool operator<(const vtkVector2< T > &v) const
Lexicographical comparison of two vector.
Definition: vtkVector.h:264
vtkVector2()=default
vtkVector2d()=default
vtkVectorDerivedMacro(vtkVector2d, double, 2)
vtkVector2d(double x, double y)
Definition: vtkVector.h:507
vtkVector2< double > Superclass
Definition: vtkVector.h:505
vtkVector2f()=default
vtkVector2< float > Superclass
Definition: vtkVector.h:493
vtkVector2f(float x, float y)
Definition: vtkVector.h:495
vtkVectorDerivedMacro(vtkVector2f, float, 2)
Some derived classes for the different vectors commonly used.
Definition: vtkVector.h:478
vtkVector2i()=default
vtkVector2i(int x, int y)
Definition: vtkVector.h:482
vtkVector2< int > Superclass
Definition: vtkVector.h:480
vtkVectorDerivedMacro(vtkVector2i, int, 2)
void SetZ(const T &z)
Set the z component of the vector, i.e.
Definition: vtkVector.h:331
const T & GetY() const
Get the y component of the vector, i.e.
Definition: vtkVector.h:326
bool operator<(const vtkVector3< T > &v) const
Lexicographical comparison of two vector.
Definition: vtkVector.h:356
vtkVector3(const T *init)
Definition: vtkVector.h:284
const T & GetZ() const
Get the z component of the vector, i.e.
Definition: vtkVector.h:336
void SetX(const T &x)
Set the x component of the vector, i.e.
Definition: vtkVector.h:311
vtkVector3(const T &scalar)
Definition: vtkVector.h:279
void Set(const T &x, const T &y, const T &z)
Set the x, y and z components of the vector.
Definition: vtkVector.h:300
vtkVector3(const T &x, const T &y, const T &z)
Definition: vtkVector.h:289
vtkVector3< T > Cross(const vtkVector3< T > &other) const
Return the cross product of this X other.
Definition: vtkVector.h:342
const T & GetX() const
Get the x component of the vector, i.e.
Definition: vtkVector.h:316
void SetY(const T &y)
Set the y component of the vector, i.e.
Definition: vtkVector.h:321
vtkVector3()=default
vtkVector3Cross(vtkVector3d, double)
vtkVector3< double > Superclass
Definition: vtkVector.h:549
vtkVector3d(double x, double y, double z)
Definition: vtkVector.h:551
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:538
vtkVector3< float > Superclass
Definition: vtkVector.h:536
vtkVector3Cross(vtkVector3i, int)
vtkVectorDerivedMacro(vtkVector3i, int, 3)
vtkVector3< int > Superclass
Definition: vtkVector.h:523
vtkVector3i()=default
vtkVector3i(int x, int y, int z)
Definition: vtkVector.h:525
void SetY(const T &y)
Set the y component of the vector, i.e.
Definition: vtkVector.h:417
vtkVector4(const T &x, const T &y, const T &z, const T &w)
Definition: vtkVector.h:383
void SetZ(const T &z)
Set the z component of the vector, i.e.
Definition: vtkVector.h:427
const T & GetZ() const
Get the z component of the vector, i.e.
Definition: vtkVector.h:432
void SetW(const T &w)
Set the w component of the vector, i.e.
Definition: vtkVector.h:437
vtkVector4(const T *init)
Definition: vtkVector.h:378
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:395
vtkVector4()=default
const T & GetY() const
Get the y component of the vector, i.e.
Definition: vtkVector.h:422
void SetX(const T &x)
Set the x component of the vector, i.e.
Definition: vtkVector.h:407
vtkVector4(const T &scalar)
Definition: vtkVector.h:373
const T & GetX() const
Get the x component of the vector, i.e.
Definition: vtkVector.h:412
const T & GetW() const
Get the w component of the vector, i.e.
Definition: vtkVector.h:442
vtkVectorDerivedMacro(vtkVector4d, double, 4)
vtkVector4d(double x, double y, double z, double w)
Definition: vtkVector.h:576
vtkVector4d()=default
vtkVector4< int > Superclass
Definition: vtkVector.h:562
vtkVector4i(int x, int y, int z, int w)
Definition: vtkVector.h:564
vtkVector4i()=default
vtkVectorDerivedMacro(vtkVector4i, int, 4)
templated base type for storage of vectors.
Definition: vtkVector.h:97
vtkVector(const T &scalar)
Initialize all of the vector's elements with the supplied scalar.
Definition: vtkVector.h:104
T Dot(const vtkVector< T, Size > &other) const
The dot product of this and the supplied vector.
Definition: vtkVector.h:177
double Normalize()
Normalize the vector in place.
Definition: vtkVector.h:144
vtkVector()=default
vtkVector(const T *init)
Initialize the vector's elements with the elements of the supplied array.
Definition: vtkVector.h:114
vtkVector< TR, Size > Cast() const
Cast the vector to the specified type, returning the result.
Definition: vtkVector.h:193
double Norm() const
Get the norm of the vector, i.e.
Definition: vtkVector.h:137
vtkVector< T, Size > Normalized() const
Return the normalized form of this vector.
Definition: vtkVector.h:165
T SquaredNorm() const
Get the squared norm of the vector.
Definition: vtkVector.h:123