VTK  9.2.6
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 {
54  for (int dim = 0; dim < Size; ++dim)
55  {
56  a[dim] += b[dim];
57  }
58 
59  return a;
60 }
61 
62 // Description:
63 // Performs subtraction of vectors of the same basic type.
64 template <typename A, int Size>
66 {
68  for (int i = 0; i < Size; ++i)
69  {
70  ret[i] = v1[i] - v2[i];
71  }
72  return ret;
73 }
74 
75 // Description:
76 // Substract the vector b to the vector a of the same basic type.
77 template <typename T, int Size>
79 {
80  for (int dim = 0; dim < Size; ++dim)
81  {
82  a[dim] -= b[dim];
83  }
84 
85  return a;
86 }
87 
88 // Description:
89 // Performs multiplication of vectors of the same basic type.
90 template <typename A, int Size>
92 {
94  for (int i = 0; i < Size; ++i)
95  {
96  ret[i] = v1[i] * v2[i];
97  }
98  return ret;
99 }
100 
101 // Description:
102 // Performs multiplication of vectors by a scalar value.
103 template <typename A, typename B, int Size>
105 {
106  vtkVector<A, Size> ret;
107  for (int i = 0; i < Size; ++i)
108  {
109  ret[i] = v1[i] * scalar;
110  }
111  return ret;
112 }
113 
114 // Description:
115 // Performs divisiom of vectors of the same type.
116 template <typename A, int Size>
118 {
119  vtkVector<A, Size> ret;
120  for (int i = 0; i < Size; ++i)
121  {
122  ret[i] = v1[i] / v2[i];
123  }
124  return ret;
125 }
126 
127 // Description:
128 // Several macros to define the various operator overloads for the vectors.
129 #define vtkVectorOperatorNegate(vectorType, type, size) \
130  inline vectorType operator-(const vectorType& v) \
131  { \
132  return vectorType((-static_cast<vtkVector<type, size>>(v)).GetData()); \
133  }
134 #define vtkVectorOperatorPlus(vectorType, type, size) \
135  inline vectorType operator+(const vectorType& v1, const vectorType& v2) \
136  { \
137  return vectorType( \
138  (static_cast<vtkVector<type, size>>(v1) + static_cast<vtkVector<type, size>>(v2)) \
139  .GetData()); \
140  }
141 #define vtkVectorOperatorMinus(vectorType, type, size) \
142  inline vectorType operator-(const vectorType& v1, const vectorType& v2) \
143  { \
144  return vectorType( \
145  (static_cast<vtkVector<type, size>>(v1) - static_cast<vtkVector<type, size>>(v2)) \
146  .GetData()); \
147  }
148 #define vtkVectorOperatorMultiply(vectorType, type, size) \
149  inline vectorType operator*(const vectorType& v1, const vectorType& v2) \
150  { \
151  return vectorType( \
152  (static_cast<vtkVector<type, size>>(v1) * static_cast<vtkVector<type, size>>(v2)) \
153  .GetData()); \
154  }
155 #define vtkVectorOperatorMultiplyScalar(vectorType, type, size) \
156  template <typename B> \
157  inline vectorType operator*(const vectorType& v1, const B& scalar) \
158  { \
159  return vectorType((static_cast<vtkVector<type, size>>(v1) * scalar).GetData()); \
160  }
161 #define vtkVectorOperatorMultiplyScalarPre(vectorType, type, size) \
162  template <typename B> \
163  inline vectorType operator*(const B& scalar, const vectorType& v1) \
164  { \
165  return vectorType((static_cast<vtkVector<type, size>>(v1) * scalar).GetData()); \
166  }
167 #define vtkVectorOperatorDivide(vectorType, type, size) \
168  inline vectorType operator/(const vectorType& v1, const vectorType& v2) \
169  { \
170  return vectorType( \
171  (static_cast<vtkVector<type, size>>(v1) / static_cast<vtkVector<type, size>>(v2)) \
172  .GetData()); \
173  }
174 
175 #define vtkVectorOperatorMacro(vectorType, type, size) \
176  vtkVectorOperatorNegate(vectorType, type, size); \
177  vtkVectorOperatorPlus(vectorType, type, size); \
178  vtkVectorOperatorMinus(vectorType, type, size); \
179  vtkVectorOperatorMultiply(vectorType, type, size); \
180  vtkVectorOperatorMultiplyScalar(vectorType, type, size); \
181  vtkVectorOperatorMultiplyScalarPre(vectorType, type, size); \
182  vtkVectorOperatorDivide(vectorType, type, size)
183 
184 // Description:
185 // Overload the operators for the common types.
192 
193 #endif
194 // VTK-HeaderTest-Exclude: vtkVectorOperators.h
templated base type for storage of vectors.
Definition: vtkVector.h:40
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)
Some derived classes for the different vectors commonly used.
Definition: vtkVector.h:421
vtkVector< A, Size > operator-(const vtkVector< A, Size > &v)
vtkVector< T, Size > & operator-=(vtkVector< T, Size > &a, const vtkVector< T, Size > &b)
#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)