1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196
|
/*=========================================================================
Program: Visualization Toolkit
Module: vtkVectorOperators.h
Copyright (c) Ken Martin, Will Schroeder, Bill Lorensen
All rights reserved.
See Copyright.txt or http://www.kitware.com/Copyright.htm for details.
This software is distributed WITHOUT ANY WARRANTY; without even
the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
PURPOSE. See the above copyright notice for more information.
=========================================================================*/
#ifndef vtkVectorOperators_h
#define vtkVectorOperators_h
// This set of operators enhance the vtkVector classes, allowing various
// operator overloads one might expect.
#include "vtkVector.h"
// Description:
// Unary minus / negation of vector.
VTK_ABI_NAMESPACE_BEGIN
template <typename A, int Size>
vtkVector<A, Size> operator-(const vtkVector<A, Size>& v)
{
vtkVector<A, Size> ret;
for (int i = 0; i < Size; ++i)
{
ret[i] = -v[i];
}
return ret;
}
// Description:
// Performs addition of vectors of the same basic type.
template <typename A, int Size>
vtkVector<A, Size> operator+(const vtkVector<A, Size>& v1, const vtkVector<A, Size>& v2)
{
vtkVector<A, Size> ret;
for (int i = 0; i < Size; ++i)
{
ret[i] = v1[i] + v2[i];
}
return ret;
}
// Description:
// Add the vector b to the vector a of the same basic type.
template <typename T, int Size>
vtkVector<T, Size>& operator+=(vtkVector<T, Size>& a, const vtkVector<T, Size>& b)
{
for (int dim = 0; dim < Size; ++dim)
{
a[dim] += b[dim];
}
return a;
}
// Description:
// Performs subtraction of vectors of the same basic type.
template <typename A, int Size>
vtkVector<A, Size> operator-(const vtkVector<A, Size>& v1, const vtkVector<A, Size>& v2)
{
vtkVector<A, Size> ret;
for (int i = 0; i < Size; ++i)
{
ret[i] = v1[i] - v2[i];
}
return ret;
}
// Description:
// Substract the vector b to the vector a of the same basic type.
template <typename T, int Size>
vtkVector<T, Size>& operator-=(vtkVector<T, Size>& a, const vtkVector<T, Size>& b)
{
for (int dim = 0; dim < Size; ++dim)
{
a[dim] -= b[dim];
}
return a;
}
// Description:
// Performs multiplication of vectors of the same basic type.
template <typename A, int Size>
vtkVector<A, Size> operator*(const vtkVector<A, Size>& v1, const vtkVector<A, Size>& v2)
{
vtkVector<A, Size> ret;
for (int i = 0; i < Size; ++i)
{
ret[i] = v1[i] * v2[i];
}
return ret;
}
// Description:
// Performs multiplication of vectors by a scalar value.
template <typename A, typename B, int Size>
vtkVector<A, Size> operator*(const vtkVector<A, Size>& v1, const B& scalar)
{
vtkVector<A, Size> ret;
for (int i = 0; i < Size; ++i)
{
ret[i] = v1[i] * scalar;
}
return ret;
}
// Description:
// Performs divisiom of vectors of the same type.
template <typename A, int Size>
vtkVector<A, Size> operator/(const vtkVector<A, Size>& v1, const vtkVector<A, Size>& v2)
{
vtkVector<A, Size> ret;
for (int i = 0; i < Size; ++i)
{
ret[i] = v1[i] / v2[i];
}
return ret;
}
// Description:
// Several macros to define the various operator overloads for the vectors.
#define vtkVectorOperatorNegate(vectorType, type, size) \
inline vectorType operator-(const vectorType& v) \
{ \
return vectorType((-static_cast<vtkVector<type, size>>(v)).GetData()); \
}
#define vtkVectorOperatorPlus(vectorType, type, size) \
inline vectorType operator+(const vectorType& v1, const vectorType& v2) \
{ \
return vectorType( \
(static_cast<vtkVector<type, size>>(v1) + static_cast<vtkVector<type, size>>(v2)) \
.GetData()); \
}
#define vtkVectorOperatorMinus(vectorType, type, size) \
inline vectorType operator-(const vectorType& v1, const vectorType& v2) \
{ \
return vectorType( \
(static_cast<vtkVector<type, size>>(v1) - static_cast<vtkVector<type, size>>(v2)) \
.GetData()); \
}
#define vtkVectorOperatorMultiply(vectorType, type, size) \
inline vectorType operator*(const vectorType& v1, const vectorType& v2) \
{ \
return vectorType( \
(static_cast<vtkVector<type, size>>(v1) * static_cast<vtkVector<type, size>>(v2)) \
.GetData()); \
}
#define vtkVectorOperatorMultiplyScalar(vectorType, type, size) \
template <typename B> \
inline vectorType operator*(const vectorType& v1, const B& scalar) \
{ \
return vectorType((static_cast<vtkVector<type, size>>(v1) * scalar).GetData()); \
}
#define vtkVectorOperatorMultiplyScalarPre(vectorType, type, size) \
template <typename B> \
inline vectorType operator*(const B& scalar, const vectorType& v1) \
{ \
return vectorType((static_cast<vtkVector<type, size>>(v1) * scalar).GetData()); \
}
#define vtkVectorOperatorDivide(vectorType, type, size) \
inline vectorType operator/(const vectorType& v1, const vectorType& v2) \
{ \
return vectorType( \
(static_cast<vtkVector<type, size>>(v1) / static_cast<vtkVector<type, size>>(v2)) \
.GetData()); \
}
#define vtkVectorOperatorMacro(vectorType, type, size) \
vtkVectorOperatorNegate(vectorType, type, size); \
vtkVectorOperatorPlus(vectorType, type, size); \
vtkVectorOperatorMinus(vectorType, type, size); \
vtkVectorOperatorMultiply(vectorType, type, size); \
vtkVectorOperatorMultiplyScalar(vectorType, type, size); \
vtkVectorOperatorMultiplyScalarPre(vectorType, type, size); \
vtkVectorOperatorDivide(vectorType, type, size)
// Description:
// Overload the operators for the common types.
vtkVectorOperatorMacro(vtkVector2i, int, 2);
vtkVectorOperatorMacro(vtkVector2f, float, 2);
vtkVectorOperatorMacro(vtkVector2d, double, 2);
vtkVectorOperatorMacro(vtkVector3i, int, 3);
vtkVectorOperatorMacro(vtkVector3f, float, 3);
vtkVectorOperatorMacro(vtkVector3d, double, 3);
VTK_ABI_NAMESPACE_END
#endif
// VTK-HeaderTest-Exclude: vtkVectorOperators.h
|