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 {
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 // Performs subtraction of vectors of the same basic type.
51 template <typename A, int Size>
53 {
55  for (int i = 0; i < Size; ++i)
56  {
57  ret[i] = v1[i] - v2[i];
58  }
59  return ret;
60 }
61 
62 // Description:
63 // Performs multiplication 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 // Performs multiplication of vectors by a scalar value.
77 template <typename A, typename B, int Size>
78 vtkVector<A, Size> operator*(const vtkVector<A, Size>& v1, const B& scalar)
79 {
81  for (int i = 0; i < Size; ++i)
82  {
83  ret[i] = v1[i] * scalar;
84  }
85  return ret;
86 }
87 
88 // Description:
89 // Performs divisiom of vectors of the same 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 // Several macros to define the various operator overloads for the vectors.
103 #define vtkVectorOperatorNegate(vectorType, type, size) \
104  inline vectorType operator-(const vectorType& v) \
105  { \
106  return vectorType((-static_cast<vtkVector<type, size> >(v)).GetData()); \
107  }
108 #define vtkVectorOperatorPlus(vectorType, type, size) \
109  inline vectorType operator+(const vectorType& v1, const vectorType& v2) \
110  { \
111  return vectorType( \
112  (static_cast<vtkVector<type, size> >(v1) + static_cast<vtkVector<type, size> >(v2)) \
113  .GetData()); \
114  }
115 #define vtkVectorOperatorMinus(vectorType, type, size) \
116  inline vectorType operator-(const vectorType& v1, const vectorType& v2) \
117  { \
118  return vectorType( \
119  (static_cast<vtkVector<type, size> >(v1) - static_cast<vtkVector<type, size> >(v2)) \
120  .GetData()); \
121  }
122 #define vtkVectorOperatorMultiply(vectorType, type, size) \
123  inline vectorType operator*(const vectorType& v1, const vectorType& v2) \
124  { \
125  return vectorType( \
126  (static_cast<vtkVector<type, size> >(v1) * static_cast<vtkVector<type, size> >(v2)) \
127  .GetData()); \
128  }
129 #define vtkVectorOperatorMultiplyScalar(vectorType, type, size) \
130  template <typename B> \
131  inline vectorType operator*(const vectorType& v1, const B& scalar) \
132  { \
133  return vectorType((static_cast<vtkVector<type, size> >(v1) * scalar).GetData()); \
134  }
135 #define vtkVectorOperatorMultiplyScalarPre(vectorType, type, size) \
136  template <typename B> \
137  inline vectorType operator*(const B& scalar, const vectorType& v1) \
138  { \
139  return vectorType((static_cast<vtkVector<type, size> >(v1) * scalar).GetData()); \
140  }
141 #define vtkVectorOperatorDivide(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 
149 #define vtkVectorOperatorMacro(vectorType, type, size) \
150  vtkVectorOperatorNegate(vectorType, type, size); \
151  vtkVectorOperatorPlus(vectorType, type, size); \
152  vtkVectorOperatorMinus(vectorType, type, size); \
153  vtkVectorOperatorMultiply(vectorType, type, size); \
154  vtkVectorOperatorMultiplyScalar(vectorType, type, size); \
155  vtkVectorOperatorMultiplyScalarPre(vectorType, type, size); \
156  vtkVectorOperatorDivide(vectorType, type, size)
157 
158 // Description:
159 // Overload the operators for the common types.
166 
167 #endif
168 // VTK-HeaderTest-Exclude: vtkVectorOperators.h
vtkVector
templated base type for storage of vectors.
Definition: vtkVector.h:37
vtkVector.h
operator*
vtkVector< A, Size > operator*(const vtkVector< A, Size > &v1, const vtkVector< A, Size > &v2)
Definition: vtkVectorOperators.h:65
operator/
vtkVector< A, Size > operator/(const vtkVector< A, Size > &v1, const vtkVector< A, Size > &v2)
Definition: vtkVectorOperators.h:91
vtkVector2d
Definition: vtkVector.h:444
vtkVector3i
Definition: vtkVector.h:462
vtkVector3f
Definition: vtkVector.h:475
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:419
vtkVector3d
Definition: vtkVector.h:488
vtkVector2f
Definition: vtkVector.h:432
vtkVectorOperatorMacro
#define vtkVectorOperatorMacro(vectorType, type, size)
Definition: vtkVectorOperators.h:149
operator+
vtkVector< A, Size > operator+(const vtkVector< A, Size > &v1, const vtkVector< A, Size > &v2)
Definition: vtkVectorOperators.h:39