VTK
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  const vtkVector<A, Size>& v2)
41 {
43  for (int i = 0; i < Size; ++i)
44  {
45  ret[i] = v1[i] + v2[i];
46  }
47  return ret;
48 }
49 
50 // Description:
51 // Performs subtraction of vectors of the same basic type.
52 template<typename A, int Size>
54  const vtkVector<A, Size>& v2)
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 // Performs multiplication of vectors of the same basic type.
66 template<typename A, int Size>
68  const vtkVector<A, Size>& v2)
69 {
71  for (int i = 0; i < Size; ++i)
72  {
73  ret[i] = v1[i] * v2[i];
74  }
75  return ret;
76 }
77 
78 // Description:
79 // Performs multiplication of vectors by a scalar value.
80 template<typename A, typename B, int Size>
82  const B& scalar)
83 {
85  for (int i = 0; i < Size; ++i)
86  {
87  ret[i] = v1[i] * scalar;
88  }
89  return ret;
90 }
91 
92 // Description:
93 // Performs divisiom of vectors of the same type.
94 template<typename A, int Size>
96  const vtkVector<A, Size>& v2)
97 {
99  for (int i = 0; i < Size; ++i)
100  {
101  ret[i] = v1[i] / v2[i];
102  }
103  return ret;
104 }
105 
106 // Description:
107 // Several macros to define the various operator overloads for the vectors.
108 #define vtkVectorOperatorNegate(vectorType, type, size) \
109 inline vectorType operator-(const vectorType& v) \
110 { \
111  return vectorType((-static_cast<vtkVector<type, size> >(v)).GetData()); \
112 }
113 #define vtkVectorOperatorPlus(vectorType, type, size) \
114 inline vectorType operator+(const vectorType& v1, const vectorType& v2) \
115 { \
116  return vectorType((static_cast<vtkVector<type, size> >(v1) + \
117  static_cast<vtkVector<type, size> >(v2)).GetData()); \
118 }
119 #define vtkVectorOperatorMinus(vectorType, type, size) \
120 inline vectorType operator-(const vectorType& v1, const vectorType& v2) \
121 { \
122  return vectorType((static_cast<vtkVector<type, size> >(v1) - \
123  static_cast<vtkVector<type, size> >(v2)).GetData()); \
124 }
125 #define vtkVectorOperatorMultiply(vectorType, type, size) \
126 inline vectorType operator*(const vectorType& v1, const vectorType& v2) \
127 { \
128  return vectorType((static_cast<vtkVector<type, size> >(v1) * \
129  static_cast<vtkVector<type, size> >(v2)).GetData()); \
130 }
131 #define vtkVectorOperatorMultiplyScalar(vectorType, type, size) \
132 template<typename B> \
133 inline vectorType operator*(const vectorType& v1, const B& scalar) \
134 { \
135  return vectorType((static_cast<vtkVector<type, size> >(v1) * scalar).GetData()); \
136 }
137 #define vtkVectorOperatorMultiplyScalarPre(vectorType, type, size) \
138 template<typename B> \
139 inline vectorType operator*(const B& scalar, const vectorType& v1) \
140 { \
141  return vectorType((static_cast<vtkVector<type, size> >(v1) * scalar).GetData()); \
142 }
143 #define vtkVectorOperatorDivide(vectorType, type, size) \
144 inline vectorType operator/(const vectorType& v1, const vectorType& v2) \
145 { \
146  return vectorType((static_cast<vtkVector<type, size> >(v1) / \
147  static_cast<vtkVector<type, size> >(v2)).GetData()); \
148 }
149 
150 #define vtkVectorOperatorMacro(vectorType, type, size) \
151 vtkVectorOperatorNegate(vectorType, type, size) \
152 vtkVectorOperatorPlus(vectorType, type, size) \
153 vtkVectorOperatorMinus(vectorType, type, size) \
154 vtkVectorOperatorMultiply(vectorType, type, size) \
155 vtkVectorOperatorMultiplyScalar(vectorType, type, size) \
156 vtkVectorOperatorMultiplyScalarPre(vectorType, type, size) \
157 vtkVectorOperatorDivide(vectorType, type, size)
158 
159 // Description:
160 // Overload the operators for the common types.
167 
168 #endif
169 // VTK-HeaderTest-Exclude: vtkVectorOperators.h
vtkVector
templated base type for storage of vectors.
Definition: vtkVector.h:40
vtkVector.h
operator*
vtkVector< A, Size > operator*(const vtkVector< A, Size > &v1, const vtkVector< A, Size > &v2)
Definition: vtkVectorOperators.h:67
operator/
vtkVector< A, Size > operator/(const vtkVector< A, Size > &v1, const vtkVector< A, Size > &v2)
Definition: vtkVectorOperators.h:95
vtkVector2d
Definition: vtkVector.h:431
vtkVector3i
Definition: vtkVector.h:446
vtkVector3f
Definition: vtkVector.h:456
operator-
vtkVector< A, Size > operator-(const vtkVector< A, Size > &v)
Definition: vtkVectorOperators.h:26
vtkVector2i
Some derived classes for the different vectors commonly used.
Definition: vtkVector.h:412
vtkVector3d
Definition: vtkVector.h:466
vtkVector2f
Definition: vtkVector.h:422
vtkVectorOperatorMacro
#define vtkVectorOperatorMacro(vectorType, type, size)
Definition: vtkVectorOperators.h:150
operator+
vtkVector< A, Size > operator+(const vtkVector< A, Size > &v1, const vtkVector< A, Size > &v2)
Definition: vtkVectorOperators.h:39