VTK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
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 // Performs addition of vectors of the same basic type.
25 template<typename A, int Size>
27  const vtkVector<A, Size>& v2)
28 {
30  for (int i = 0; i < Size; ++i)
31  {
32  ret[i] = v1[i] + v2[i];
33  }
34  return ret;
35 }
36 
37 // Description:
38 // Performs subtraction of vectors of the same basic type.
39 template<typename A, int Size>
41  const vtkVector<A, Size>& v2)
42 {
44  for (int i = 0; i < Size; ++i)
45  {
46  ret[i] = v1[i] - v2[i];
47  }
48  return ret;
49 }
50 
51 // Description:
52 // Performs multiplication of vectors of the same basic type.
53 template<typename A, int Size>
55  const vtkVector<A, Size>& v2)
56 {
58  for (int i = 0; i < Size; ++i)
59  {
60  ret[i] = v1[i] * v2[i];
61  }
62  return ret;
63 }
64 
65 // Description:
66 // Performs multiplication of vectors by a scalar value.
67 template<typename A, typename B, int Size>
69  const B& scalar)
70 {
72  for (int i = 0; i < Size; ++i)
73  {
74  ret[i] = v1[i] * scalar;
75  }
76  return ret;
77 }
78 
79 // Description:
80 // Performs divisiom of vectors of the same type.
81 template<typename A, int Size>
83  const vtkVector<A, Size>& v2)
84 {
86  for (int i = 0; i < Size; ++i)
87  {
88  ret[i] = v1[i] / v2[i];
89  }
90  return ret;
91 }
92 
93 // Description:
94 // Several macros to define the various operator overloads for the vectors.
95 #define vtkVectorOperatorPlus(vectorType, type, size) \
96 inline vectorType operator+(const vectorType& v1, const vectorType& v2) \
97 { \
98  return vectorType((static_cast<vtkVector<type, size> >(v1) + \
99  static_cast<vtkVector<type, size> >(v2)).GetData()); \
100 }
101 #define vtkVectorOperatorMinus(vectorType, type, size) \
102 inline vectorType operator-(const vectorType& v1, const vectorType& v2) \
103 { \
104  return vectorType((static_cast<vtkVector<type, size> >(v1) - \
105  static_cast<vtkVector<type, size> >(v2)).GetData()); \
106 }
107 #define vtkVectorOperatorMultiply(vectorType, type, size) \
108 inline vectorType operator*(const vectorType& v1, const vectorType& v2) \
109 { \
110  return vectorType((static_cast<vtkVector<type, size> >(v1) * \
111  static_cast<vtkVector<type, size> >(v2)).GetData()); \
112 }
113 #define vtkVectorOperatorMultiplyScalar(vectorType, type, size) \
114 template<typename B> \
115 inline vectorType operator*(const vectorType& v1, const B& scalar) \
116 { \
117  return vectorType((static_cast<vtkVector<type, size> >(v1) * scalar).GetData()); \
118 }
119 #define vtkVectorOperatorMultiplyScalarPre(vectorType, type, size) \
120 template<typename B> \
121 inline vectorType operator*(const B& scalar, const vectorType& v1) \
122 { \
123  return vectorType((static_cast<vtkVector<type, size> >(v1) * scalar).GetData()); \
124 }
125 #define vtkVectorOperatorDivide(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 
132 #define vtkVectorOperatorMacro(vectorType, type, size) \
133 vtkVectorOperatorPlus(vectorType, type, size) \
134 vtkVectorOperatorMinus(vectorType, type, size) \
135 vtkVectorOperatorMultiply(vectorType, type, size) \
136 vtkVectorOperatorMultiplyScalar(vectorType, type, size) \
137 vtkVectorOperatorMultiplyScalarPre(vectorType, type, size) \
138 vtkVectorOperatorDivide(vectorType, type, size)
139 
140 // Description:
141 // Overload the operators for the common types.
148 
149 #endif
150 // VTK-HeaderTest-Exclude: vtkVectorOperators.h
templated base type for storage of vectors.
Definition: vtkVector.h:39
#define vtkVectorOperatorMacro(vectorType, type, size)
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)
vtkVector< A, Size > operator/(const vtkVector< A, Size > &v1, const vtkVector< A, Size > &v2)
GLfloat GLfloat GLfloat v2
Definition: vtkgl.h:12015
GLfloat GLfloat v1
Definition: vtkgl.h:12014
vtkVector< A, Size > operator*(const vtkVector< A, Size > &v1, const vtkVector< A, Size > &v2)