VTK
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 "vtkTuple.h"
35#include "vtkObject.h" // for legacy macros
36
37#include <cmath> // For math functions
38
39template<typename T, int Size>
40class vtkVector : public vtkTuple<T, Size>
41{
42public:
44 {
45 }
46
50 explicit vtkVector(const T& scalar) : vtkTuple<T, Size>(scalar)
51 {
52 }
53
59 explicit vtkVector(const T* init) : 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
82 {
83 return sqrt(static_cast<double>(this->SquaredNorm()));
84 }
85
87
91 double Normalize()
92 {
93 const double norm(this->Norm());
94 const double inv(1.0 / norm);
95 for (int i = 0; i < Size; ++i)
96 {
97 this->Data[i] = static_cast<T>(this->Data[i] * inv);
98 }
99 return norm;
100 }
102
104
109 {
110 vtkVector<T, Size> temp(*this);
111 temp.Normalize();
112 return temp;
113 }
115
117
120 T Dot(const vtkVector<T, Size>& other) const
121 {
122 T result(0);
123 for (int i = 0; i < Size; ++i)
124 {
125 result += this->Data[i] * other[i];
126 }
127 return result;
128 }
130
132
135 template<typename TR>
137 {
138 vtkVector<TR, Size> result;
139 for (int i = 0; i < Size; ++i)
140 {
141 result[i] = static_cast<TR>(this->Data[i]);
142 }
143 return result;
144 }
145};
147
148// .NAME vtkVector2 - templated base type for storage of 2D vectors.
149//
150template<typename T>
151class vtkVector2 : public vtkVector<T, 2>
152{
153public:
155 {
156 }
157
158 explicit vtkVector2(const T& scalar) : vtkVector<T, 2>(scalar)
159 {
160 }
161
162 explicit vtkVector2(const T* init) : vtkVector<T, 2>(init)
163 {
164 }
165
166 vtkVector2(const T& x, const T& y)
167 {
168 this->Data[0] = x;
169 this->Data[1] = y;
170 }
171
173
176 void Set(const T& x, const T& y)
177 {
178 this->Data[0] = x;
179 this->Data[1] = y;
180 }
182
186 void SetX(const T& x) { this->Data[0] = x; }
187
191 const T& GetX() const { return this->Data[0]; }
192
196 void SetY(const T& y) { this->Data[1] = y; }
197
201 const T& GetY() const { return this->Data[1]; }
202
204
207 bool operator<(const vtkVector2<T> &v) const
208 {
209 return (this->Data[0] < v.Data[0]) || (this->Data[0] == v.Data[0] && this->Data[1] < v.Data[1]);
210 }
211};
213
214// .NAME vtkVector3 - templated base type for storage of 3D vectors.
215//
216template<typename T>
217class vtkVector3 : public vtkVector<T, 3>
218{
219public:
221 {
222 }
223
224 explicit vtkVector3(const T& scalar) : vtkVector<T, 3>(scalar)
225 {
226 }
227
228 explicit vtkVector3(const T* init) : vtkVector<T, 3>(init)
229 {
230 }
231
232 vtkVector3(const T& x, const T& y, const T& z)
233 {
234 this->Data[0] = x;
235 this->Data[1] = y;
236 this->Data[2] = z;
237 }
238
240
243 void Set(const T& x, const T& y, const T& z)
244 {
245 this->Data[0] = x;
246 this->Data[1] = y;
247 this->Data[2] = z;
248 }
250
254 void SetX(const T& x) { this->Data[0] = x; }
255
259 const T& GetX() const { return this->Data[0]; }
260
264 void SetY(const T& y) { this->Data[1] = y; }
265
269 const T& GetY() const { return this->Data[1]; }
270
274 void SetZ(const T& z) { this->Data[2] = z; }
275
279 const T& GetZ() const { return this->Data[2]; }
280
282
286 {
287 vtkVector3<T> res;
288 res[0] = this->Data[1] * other.Data[2] - this->Data[2] * other.Data[1];
289 res[1] = this->Data[2] * other.Data[0] - this->Data[0] * other.Data[2];
290 res[2] = this->Data[0] * other.Data[1] - this->Data[1] * other.Data[0];
291 return res;
292 }
294
296
299 bool operator<(const vtkVector3<T> &v) const
300 {
301 return (this->Data[0] < v.Data[0]) || (this->Data[0] == v.Data[0] && this->Data[1] < v.Data[1]) ||
302 (this->Data[0] == v.Data[0] && this->Data[1] == v.Data[1] && this->Data[2] < v.Data[2]);
303 }
304};
306
310#define vtkVectorNormalized(vectorType, type, size) \
311vectorType Normalized() const \
312{ \
313 return vectorType(vtkVector<type, size>::Normalized().GetData()); \
314} \
315
316#define vtkVectorDerivedMacro(vectorType, type, size) \
317vtkVectorNormalized(vectorType, type, size) \
318explicit vectorType(type s) : Superclass(s) {} \
319explicit vectorType(const type *i) : Superclass(i) {} \
320explicit vectorType(const vtkTuple<type, size> &o) : Superclass(o.GetData()) {} \
321vectorType(const vtkVector<type, size> &o) : Superclass(o.GetData()) {} \
322
324
327class vtkVector2i : public vtkVector2<int>
328{
329public:
332 vtkVector2i(int x, int y) : vtkVector2<int>(x, y) {}
334};
336
337class vtkVector2f : public vtkVector2<float>
338{
339public:
342 vtkVector2f(float x, float y) : vtkVector2<float>(x, y) {}
344};
345
346class vtkVector2d : public vtkVector2<double>
347{
348public:
351 vtkVector2d(double x, double y) : vtkVector2<double>(x, y) {}
353};
354
355#define vtkVector3Cross(vectorType, type) \
356vectorType Cross(const vectorType& other) const \
357{ \
358 return vectorType(vtkVector3<type>::Cross(other).GetData()); \
359} \
360
361class vtkVector3i : public vtkVector3<int>
362{
363public:
366 vtkVector3i(int x, int y, int z) : vtkVector3<int>(x, y, z) {}
369};
370
371class vtkVector3f : public vtkVector3<float>
372{
373public:
376 vtkVector3f(float x, float y, float z) : vtkVector3<float>(x, y, z) {}
379};
380
381class vtkVector3d : public vtkVector3<double>
382{
383public:
386 vtkVector3d(double x, double y, double z) : vtkVector3<double>(x, y, z) {}
389};
390
391#endif // vtkVector_h
392// VTK-HeaderTest-Exclude: vtkVector.h
templated base type for containers of constant size.
Definition: vtkTuple.h:36
T Data[Size]
The only thing stored in memory!
Definition: vtkTuple.h:145
const T & GetX() const
Get the x component of the vector, i.e.
Definition: vtkVector.h:191
const T & GetY() const
Get the y component of the vector, i.e.
Definition: vtkVector.h:201
void Set(const T &x, const T &y)
Set the x and y components of the vector.
Definition: vtkVector.h:176
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:196
vtkVector2(const T &x, const T &y)
Definition: vtkVector.h:166
vtkVector2(const T &scalar)
Definition: vtkVector.h:158
void SetX(const T &x)
Set the x component of the vector, i.e.
Definition: vtkVector.h:186
bool operator<(const vtkVector2< T > &v) const
Lexicographical comparison of two vector.
Definition: vtkVector.h:207
vtkVector2d(double x, double y)
Definition: vtkVector.h:351
vtkVector2< double > Superclass
Definition: vtkVector.h:349
vtkVector2< float > Superclass
Definition: vtkVector.h:340
vtkVector2f(float x, float y)
Definition: vtkVector.h:342
Some derived classes for the different vectors commonly used.
Definition: vtkVector.h:328
vtkVector2i(int x, int y)
Definition: vtkVector.h:332
vtkVector2< int > Superclass
Definition: vtkVector.h:330
void SetZ(const T &z)
Set the z component of the vector, i.e.
Definition: vtkVector.h:274
const T & GetY() const
Get the y component of the vector, i.e.
Definition: vtkVector.h:269
bool operator<(const vtkVector3< T > &v) const
Lexicographical comparison of two vector.
Definition: vtkVector.h:299
vtkVector3(const T *init)
Definition: vtkVector.h:228
const T & GetZ() const
Get the z component of the vector, i.e.
Definition: vtkVector.h:279
void SetX(const T &x)
Set the x component of the vector, i.e.
Definition: vtkVector.h:254
vtkVector3(const T &scalar)
Definition: vtkVector.h:224
void Set(const T &x, const T &y, const T &z)
Set the x, y and z components of the vector.
Definition: vtkVector.h:243
vtkVector3(const T &x, const T &y, const T &z)
Definition: vtkVector.h:232
vtkVector3< T > Cross(const vtkVector3< T > &other) const
Return the cross product of this X other.
Definition: vtkVector.h:285
const T & GetX() const
Get the x component of the vector, i.e.
Definition: vtkVector.h:259
void SetY(const T &y)
Set the y component of the vector, i.e.
Definition: vtkVector.h:264
vtkVector3< double > Superclass
Definition: vtkVector.h:384
vtkVector3d(double x, double y, double z)
Definition: vtkVector.h:386
vtkVectorDerivedMacro(vtkVector3d, double, 3) vtkVector3Cross(vtkVector3d
vtkVector3f(float x, float y, float z)
Definition: vtkVector.h:376
vtkVector3< float > Superclass
Definition: vtkVector.h:374
vtkVectorDerivedMacro(vtkVector3f, float, 3) vtkVector3Cross(vtkVector3f
vtkVector3< int > Superclass
Definition: vtkVector.h:364
vtkVectorDerivedMacro(vtkVector3i, int, 3) vtkVector3Cross(vtkVector3i
vtkVector3i(int x, int y, int z)
Definition: vtkVector.h:366
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:50
T Dot(const vtkVector< T, Size > &other) const
The dot product of this and the supplied vector.
Definition: vtkVector.h:120
double Normalize()
Normalize the vector in place.
Definition: vtkVector.h:91
vtkVector()
Definition: vtkVector.h:43
vtkVector(const T *init)
Initalize the vector's elements with the elements of the supplied array.
Definition: vtkVector.h:59
vtkVector< TR, Size > Cast() const
Cast the vector to the specified type, returning the result.
Definition: vtkVector.h:136
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:108
T SquaredNorm() const
Get the squared norm of the vector.
Definition: vtkVector.h:67
#define vtkVector3Cross(vectorType, type)
Definition: vtkVector.h:355
#define vtkVectorDerivedMacro(vectorType, type, size)
Definition: vtkVector.h:316