VTK  9.1.0
vtkVectorOperators.h
Go to the documentation of this file.
1 /*=========================================================================
2 
3  Program: Visualization Toolkit
4  Module: vtkVectorOperators.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 
16 #ifndef vtkVectorOperators_h
17 #define vtkVectorOperators_h
18 
19 // This set of operators enhance the vtkVector classes, allowing various
20 // operator overloads one might expect.
21 #include "vtkVector.h"
22 
23 // Description:
24 // Unary minus / negation of vector.
25 template <typename A, int Size>
27 {
29  for (int i = 0; i < Size; ++i)
30  {
31  ret[i] = -v[i];
32  }
33  return ret;
34 }
35 
36 // Description:
37 // Performs addition of vectors of the same basic type.
38 template <typename A, int Size>
40 {
42  for (int i = 0; i < Size; ++i)
43  {
44  ret[i] = v1[i] + v2[i];
45  }
46  return ret;
47 }
48 
49 // Description:
50 // Add the vector b to the vector a of the same basic type.
51 template <typename T, int Size>
53 {
55  for (int dim = 0; dim < Size; ++dim)
56  {
57  a[dim] += b[dim];
58  }
59 
60  return ret;
61 }
62 
63 // Description:
64 // Performs subtraction of vectors of the same basic type.
65 template <typename A, int Size>
67 {
69  for (int i = 0; i < Size; ++i)
70  {
71  ret[i] = v1[i] - v2[i];
72  }
73  return ret;
74 }
75 
76 // Description:
77 // Substract the vector b to the vector a of the same basic type.
78 template <typename T, int Size>
80 {
82  for (int dim = 0; dim < Size; ++dim)
83  {
84  a[dim] -= b[dim];
85  }
86 
87  return ret;
88 }
89 
90 // Description:
91 // Performs multiplication of vectors of the same basic type.
92 template <typename A, int Size>
94 {
96  for (int i = 0; i < Size; ++i)
97  {
98  ret[i] = v1[i] * v2[i];
99  }
100  return ret;
101 }
102 
103 // Description:
104 // Performs multiplication of vectors by a scalar value.
105 template <typename A, typename B, int Size>
107 {
108  vtkVector<A, Size> ret;
109  for (int i = 0; i < Size; ++i)
110  {
111  ret[i] = v1[i] * scalar;
112  }
113  return ret;
114 }
115 
116 // Description:
117 // Performs divisiom of vectors of the same type.
118 template <typename A, int Size>
120 {
121  vtkVector<A, Size> ret;
122  for (int i = 0; i < Size; ++i)
123  {
124  ret[i] = v1[i] / v2[i];
125  }
126  return ret;
127 }
128 
129 // Description:
130 // Several macros to define the various operator overloads for the vectors.
131 #define vtkVectorOperatorNegate(vectorType, type, size) \
132  inline vectorType operator-(const vectorType& v) \
133  { \
134  return vectorType((-static_cast<vtkVector<type, size>>(v)).GetData()); \
135  }
136 #define vtkVectorOperatorPlus(vectorType, type, size) \
137  inline vectorType operator+(const vectorType& v1, const vectorType& v2) \
138  { \
139  return vectorType( \
140  (static_cast<vtkVector<type, size>>(v1) + static_cast<vtkVector<type, size>>(v2)) \
141  .GetData()); \
142  }
143 #define vtkVectorOperatorMinus(vectorType, type, size) \
144  inline vectorType operator-(const vectorType& v1, const vectorType& v2) \
145  { \
146  return vectorType( \
147  (static_cast<vtkVector<type, size>>(v1) - static_cast<vtkVector<type, size>>(v2)) \
148  .GetData()); \
149  }
150 #define vtkVectorOperatorMultiply(vectorType, type, size) \
151  inline vectorType operator*(const vectorType& v1, const vectorType& v2) \
152  { \
153  return vectorType( \
154  (static_cast<vtkVector<type, size>>(v1) * static_cast<vtkVector<type, size>>(v2)) \
155  .GetData()); \
156  }
157 #define vtkVectorOperatorMultiplyScalar(vectorType, type, size) \
158  template <typename B> \
159  inline vectorType operator*(const vectorType& v1, const B& scalar) \
160  { \
161  return vectorType((static_cast<vtkVector<type, size>>(v1) * scalar).GetData()); \
162  }
163 #define vtkVectorOperatorMultiplyScalarPre(vectorType, type, size) \
164  template <typename B> \
165  inline vectorType operator*(const B& scalar, const vectorType& v1) \
166  { \
167  return vectorType((static_cast<vtkVector<type, size>>(v1) * scalar).GetData()); \
168  }
169 #define vtkVectorOperatorDivide(vectorType, type, size) \
170  inline vectorType operator/(const vectorType& v1, const vectorType& v2) \
171  { \
172  return vectorType( \
173  (static_cast<vtkVector<type, size>>(v1) / static_cast<vtkVector<type, size>>(v2)) \
174  .GetData()); \
175  }
176 
177 #define vtkVectorOperatorMacro(vectorType, type, size) \
178  vtkVectorOperatorNegate(vectorType, type, size); \
179  vtkVectorOperatorPlus(vectorType, type, size); \
180  vtkVectorOperatorMinus(vectorType, type, size); \
181  vtkVectorOperatorMultiply(vectorType, type, size); \
182  vtkVectorOperatorMultiplyScalar(vectorType, type, size); \
183  vtkVectorOperatorMultiplyScalarPre(vectorType, type, size); \
184  vtkVectorOperatorDivide(vectorType, type, size)
185 
186 // Description:
187 // Overload the operators for the common types.
194 
195 #endif
196 // VTK-HeaderTest-Exclude: vtkVectorOperators.h
Some derived classes for the different vectors commonly used.
Definition: vtkVector.h:419
templated base type for storage of vectors.
Definition: vtkVector.h:38
vtkVector< A, Size > operator-(const vtkVector< A, Size > &v)
#define vtkVectorOperatorMacro(vectorType, type, size)
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< 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)