VTK: Common/DataModel/vtkVectorOperators.h Source File

| VTK 9.3.1

vtkVectorOperators.h
Go to the documentation of this file.
1 // SPDX-FileCopyrightText: Copyright (c) Ken Martin, Will Schroeder, Bill Lorensen
2 // SPDX-License-Identifier: BSD-3-Clause
3
4 #ifndef vtkVectorOperators_h
5 #define vtkVectorOperators_h
6
7 // This set of operators enhance the vtkVector classes, allowing various
8 // operator overloads one might expect.
9 #include "vtkVector.h"
10
11 // Description:
12 // Unary minus / negation of vector.
13 VTK_ABI_NAMESPACE_BEGIN
14 template <typename A, int Size>
15 vtkVector<A, Size> operator-(const vtkVector<A, Size>& v)
16 {
17 vtkVector<A, Size> ret;
18 for (int i = 0; i < Size; ++i)
19 {
20 ret[i] = -v[i];
21 }
22 return ret;
23 }
24
25 // Description:
26 // Performs addition of vectors of the same basic type.
27 template <typename A, int Size>
28 vtkVector<A, Size> operator+(const vtkVector<A, Size>& v1, const vtkVector<A, Size>& v2)
29 {
30 vtkVector<A, Size> ret;
31 for (int i = 0; i < Size; ++i)
32 {
33 ret[i] = v1[i] + v2[i];
34 }
35 return ret;
36 }
37
38 // Description:
39 // Add the vector b to the vector a of the same basic type.
40 template <typename T, int Size>
41 vtkVector<T, Size>& operator+=(vtkVector<T, Size>& a, const vtkVector<T, Size>& b)
42 {
43 for (int dim = 0; dim < Size; ++dim)
44 {
45 a[dim] += b[dim];
46 }
47
48 return a;
49 }
50
51 // Description:
52 // Performs subtraction of vectors of the same basic type.
53 template <typename A, int Size>
54 vtkVector<A, Size> operator-(const vtkVector<A, Size>& v1, const vtkVector<A, Size>& v2)
55 {
56 vtkVector<A, Size> ret;
57 for (int i = 0; i < Size; ++i)
58 {
59 ret[i] = v1[i] - v2[i];
60 }
61 return ret;
62 }
63
64 // Description:
65 // Substract the vector b to the vector a of the same basic type.
66 template <typename T, int Size>
67 vtkVector<T, Size>& operator-=(vtkVector<T, Size>& a, const vtkVector<T, Size>& b)
68 {
69 for (int dim = 0; dim < Size; ++dim)
70 {
71 a[dim] -= b[dim];
72 }
73
74 return a;
75 }
76
77 // Description:
78 // Performs multiplication of vectors of the same basic type.
79 template <typename A, int Size>
80 vtkVector<A, Size> operator*(const vtkVector<A, Size>& v1, const vtkVector<A, Size>& v2)
81 {
82 vtkVector<A, Size> ret;
83 for (int i = 0; i < Size; ++i)
84 {
85 ret[i] = v1[i] * v2[i];
86 }
87 return ret;
88 }
89
90 // Description:
91 // Performs multiplication of vectors by a scalar value.
92 template <typename A, typename B, int Size>
93 vtkVector<A, Size> operator*(const vtkVector<A, Size>& v1, const B& scalar)
94 {
95 vtkVector<A, Size> ret;
96 for (int i = 0; i < Size; ++i)
97 {
98 ret[i] = v1[i] * scalar;
99 }
100 return ret;
101 }
102
103 // Description:
104 // Performs divisiom of vectors of the same type.
105 template <typename A, int Size>
106 vtkVector<A, Size> operator/(const vtkVector<A, Size>& v1, const vtkVector<A, Size>& v2)
107 {
108 vtkVector<A, Size> ret;
109 for (int i = 0; i < Size; ++i)
110 {
111 ret[i] = v1[i] / v2[i];
112 }
113 return ret;
114 }
115
116 // Description:
117 // Several macros to define the various operator overloads for the vectors.
118 #define vtkVectorOperatorNegate(vectorType, type, size) \
119 inline vectorType operator-(const vectorType& v) \
120 { \
121 return vectorType((-static_cast<vtkVector<type, size>>(v)).GetData()); \
122 }
123 #define vtkVectorOperatorPlus(vectorType, type, size) \
124 inline vectorType operator+(const vectorType& v1, const vectorType& v2) \
125 { \
126 return vectorType( \
127 (static_cast<vtkVector<type, size>>(v1) + static_cast<vtkVector<type, size>>(v2)) \
128 .GetData()); \
129 }
130 #define vtkVectorOperatorMinus(vectorType, type, size) \
131 inline vectorType operator-(const vectorType& v1, const vectorType& v2) \
132 { \
133 return vectorType( \
134 (static_cast<vtkVector<type, size>>(v1) - static_cast<vtkVector<type, size>>(v2)) \
135 .GetData()); \
136 }
137 #define vtkVectorOperatorMultiply(vectorType, type, size) \
138 inline vectorType operator*(const vectorType& v1, const vectorType& v2) \
139 { \
140 return vectorType( \
141 (static_cast<vtkVector<type, size>>(v1) * static_cast<vtkVector<type, size>>(v2)) \
142 .GetData()); \
143 }
144 #define vtkVectorOperatorMultiplyScalar(vectorType, type, size) \
145 template <typename B> \
146 inline vectorType operator*(const vectorType& v1, const B& scalar) \
147 { \
148 return vectorType((static_cast<vtkVector<type, size>>(v1) * scalar).GetData()); \
149 }
150 #define vtkVectorOperatorMultiplyScalarPre(vectorType, type, size) \
151 template <typename B> \
152 inline vectorType operator*(const B& scalar, const vectorType& v1) \
153 { \
154 return vectorType((static_cast<vtkVector<type, size>>(v1) * scalar).GetData()); \
155 }
156 #define vtkVectorOperatorDivide(vectorType, type, size) \
157 inline vectorType operator/(const vectorType& v1, const vectorType& v2) \
158 { \
159 return vectorType( \
160 (static_cast<vtkVector<type, size>>(v1) / static_cast<vtkVector<type, size>>(v2)) \
161 .GetData()); \
162 }
163
164 #define vtkVectorOperatorMacro(vectorType, type, size) \
165 vtkVectorOperatorNegate(vectorType, type, size); \
166 vtkVectorOperatorPlus(vectorType, type, size); \
167 vtkVectorOperatorMinus(vectorType, type, size); \
168 vtkVectorOperatorMultiply(vectorType, type, size); \
169 vtkVectorOperatorMultiplyScalar(vectorType, type, size); \
170 vtkVectorOperatorMultiplyScalarPre(vectorType, type, size); \
171 vtkVectorOperatorDivide(vectorType, type, size)
172
173 // Description:
174 // Overload the operators for the common types.
175 vtkVectorOperatorMacro(vtkVector2i, int, 2);
176 vtkVectorOperatorMacro(vtkVector2f, float, 2);
177 vtkVectorOperatorMacro(vtkVector2d, double, 2);
178 vtkVectorOperatorMacro(vtkVector3i, int, 3);
179 vtkVectorOperatorMacro(vtkVector3f, float, 3);
180 vtkVectorOperatorMacro(vtkVector3d, double, 3);
181
182 VTK_ABI_NAMESPACE_END
183 #endif
184 // VTK-HeaderTest-Exclude: vtkVectorOperators.h
vtkVector2d
Definition: vtkVector.h:475
vtkVector2f
Definition: vtkVector.h:463
vtkVector2i
Some derived classes for the different vectors commonly used.
Definition: vtkVector.h:450
vtkVector3d
Definition: vtkVector.h:519
vtkVector3f
Definition: vtkVector.h:506
vtkVector3i
Definition: vtkVector.h:493
vtkVector
templated base type for storage of vectors.
Definition: vtkVector.h:39
operator-
vtkVector< A, Size > operator-(const vtkVector< A, Size > &v)
Definition: vtkVectorOperators.h:15
vtkVectorOperatorMacro
#define vtkVectorOperatorMacro(vectorType, type, size)
Definition: vtkVectorOperators.h:164
operator*
vtkVector< A, Size > operator*(const vtkVector< A, Size > &v1, const vtkVector< A, Size > &v2)
Definition: vtkVectorOperators.h:80
operator+=
vtkVector< T, Size > & operator+=(vtkVector< T, Size > &a, const vtkVector< T, Size > &b)
Definition: vtkVectorOperators.h:41
operator-=
vtkVector< T, Size > & operator-=(vtkVector< T, Size > &a, const vtkVector< T, Size > &b)
Definition: vtkVectorOperators.h:67
operator+
vtkVector< A, Size > operator+(const vtkVector< A, Size > &v1, const vtkVector< A, Size > &v2)
Definition: vtkVectorOperators.h:28
operator/
vtkVector< A, Size > operator/(const vtkVector< A, Size > &v1, const vtkVector< A, Size > &v2)
Definition: vtkVectorOperators.h:106
vtkVector.h