VTK  9.3.20240328
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>
16 {
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>
29 {
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>
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>
55 {
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 // Subtract the vector b to the vector a of the same basic type.
66 template <typename T, int Size>
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>
81 {
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 {
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>
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.
181 
182 VTK_ABI_NAMESPACE_END
183 #endif
184 // VTK-HeaderTest-Exclude: vtkVectorOperators.h
Some derived classes for the different vectors commonly used.
Definition: vtkVector.h:470
templated base type for storage of vectors.
Definition: vtkVector.h:59
vtkVector< A, Size > operator-(const vtkVector< A, Size > &v)
#define vtkVectorOperatorMacro(vectorType, type, size)
vtkVector< A, Size > operator*(const vtkVector< A, Size > &v1, const vtkVector< A, Size > &v2)
vtkVector< T, Size > & operator+=(vtkVector< T, Size > &a, const vtkVector< T, Size > &b)
vtkVector< T, Size > & operator-=(vtkVector< T, Size > &a, const vtkVector< T, Size > &b)
vtkVector< A, Size > operator+(const vtkVector< A, Size > &v1, const vtkVector< A, Size > &v2)
vtkVector< A, Size > operator/(const vtkVector< A, Size > &v1, const vtkVector< A, Size > &v2)