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 #include "vtkIOStream.h"
23 
24 // Description:
25 // Output the contents of a vector, mainly useful for debugging.
26 template<typename A, int Size>
27 ostream& operator<<(ostream& out, const vtkVector<A, Size>& v)
28 {
29  out << "(";
30  bool first = true;
31  for (int i = 0; i < Size; ++i)
32  {
33  if (first)
34  {
35  first = false;
36  }
37  else
38  {
39  out << ", ";
40  }
41  out << v[i];
42  }
43  out << ")";
44  return out;
45 }
46 
47 // Description:
48 // Equality operator performs an equality check on each component.
49 template<typename A, int Size>
51 {
52  bool ret = true;
53  for (int i = 0; i < Size; ++i)
54  {
55  if (v1[i] != v2[i])
56  {
57  ret = false;
58  }
59  }
60  return ret;
61 }
62 
63 // Description:
64 // Inequality for vector type.
65 template<typename A, int Size>
67 {
68  return !(v1 == v2);
69 }
70 
71 // Description:
72 // Performs addition of vectors of the same basic type.
73 template<typename A, int Size>
75  const vtkVector<A, Size>& v2)
76 {
78  for (int i = 0; i < Size; ++i)
79  {
80  ret[i] = v1[i] + v2[i];
81  }
82  return ret;
83 }
84 
85 // Description:
86 // Performs subtraction of vectors of the same basic type.
87 template<typename A, int Size>
89  const vtkVector<A, Size>& v2)
90 {
92  for (int i = 0; i < Size; ++i)
93  {
94  ret[i] = v1[i] - v2[i];
95  }
96  return ret;
97 }
98 
99 // Description:
100 // Performs multiplication of vectors of the same basic type.
101 template<typename A, int Size>
103  const vtkVector<A, Size>& v2)
104 {
105  vtkVector<A, Size> ret;
106  for (int i = 0; i < Size; ++i)
107  {
108  ret[i] = v1[i] * v2[i];
109  }
110  return ret;
111 }
112 
113 // Description:
114 // Performs multiplication of vectors by a scalar value.
115 template<typename A, typename B, int Size>
117  const B& scalar)
118 {
119  vtkVector<A, Size> ret;
120  for (int i = 0; i < Size; ++i)
121  {
122  ret[i] = v1[i] * scalar;
123  }
124  return ret;
125 }
126 
127 // Description:
128 // Performs divisiom of vectors of the same type.
129 template<typename A, int Size>
131  const vtkVector<A, Size>& v2)
132 {
133  vtkVector<A, Size> ret;
134  for (int i = 0; i < Size; ++i)
135  {
136  ret[i] = v1[i] / v2[i];
137  }
138  return ret;
139 }
140 
141 // Description:
142 // Several macros to define the various operator overloads for the vectors.
143 #define vtkVectorOperatorPlus(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 #define vtkVectorOperatorMinus(vectorType, type, size) \
150 inline vectorType operator-(const vectorType& v1, const vectorType& v2) \
151 { \
152  return vectorType((static_cast<vtkVector<type, size> >(v1) - \
153  static_cast<vtkVector<type, size> >(v2)).GetData()); \
154 }
155 #define vtkVectorOperatorMultiply(vectorType, type, size) \
156 inline vectorType operator*(const vectorType& v1, const vectorType& v2) \
157 { \
158  return vectorType((static_cast<vtkVector<type, size> >(v1) * \
159  static_cast<vtkVector<type, size> >(v2)).GetData()); \
160 }
161 #define vtkVectorOperatorMultiplyScalar(vectorType, type, size) \
162 template<typename B> \
163 inline vectorType operator*(const vectorType& v1, const B& scalar) \
164 { \
165  return vectorType((static_cast<vtkVector<type, size> >(v1) * scalar).GetData()); \
166 }
167 #define vtkVectorOperatorMultiplyScalarPre(vectorType, type, size) \
168 template<typename B> \
169 inline vectorType operator*(const B& scalar, const vectorType& v1) \
170 { \
171  return vectorType((static_cast<vtkVector<type, size> >(v1) * scalar).GetData()); \
172 }
173 #define vtkVectorOperatorDivide(vectorType, type, size) \
174 inline vectorType operator/(const vectorType& v1, const vectorType& v2) \
175 { \
176  return vectorType((static_cast<vtkVector<type, size> >(v1) / \
177  static_cast<vtkVector<type, size> >(v2)).GetData()); \
178 }
179 
180 #define vtkVectorOperatorMacro(vectorType, type, size) \
181 vtkVectorOperatorPlus(vectorType, type, size) \
182 vtkVectorOperatorMinus(vectorType, type, size) \
183 vtkVectorOperatorMultiply(vectorType, type, size) \
184 vtkVectorOperatorMultiplyScalar(vectorType, type, size) \
185 vtkVectorOperatorMultiplyScalarPre(vectorType, type, size) \
186 vtkVectorOperatorDivide(vectorType, type, size)
187 
188 // Description:
189 // Overload the operators for the common types.
196 
197 #endif
198 // VTK-HeaderTest-Exclude: vtkVectorOperators.h