Commit 1fdddf9b authored by Marcus D. Hanwell's avatar Marcus D. Hanwell
Browse files

ENH: Added basic operator overloads to vtkVector.

Added ==, !=, +, -, * and / to the vtkVector types along with an ostream
<< for easy printing of the vector types.

Change-Id: I7bd872b78feab3a5155a6c913b8463c2bdc9ca0b
parent 8737ed28
......@@ -29,6 +29,7 @@ CREATE_TEST_SOURCELIST(Tests ${KIT}CxxTests.cxx
TestUnicodeStringArrayAPI.cxx
TestVariantComparison.cxx
TestVector.cxx
TestVectorOperators.cxx
TestWeakPointer.cxx
TestSystemInformation.cxx
EXTRA_INCLUDE vtkTestDriver.h
......
/*=========================================================================
Program: Visualization Toolkit
Module: TestVector.cxx
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.
=========================================================================*/
#include "vtkVector.h"
#include "vtkVectorOperators.h"
#include "vtkSetGet.h"
//----------------------------------------------------------------------------
int TestVectorOperators(int, char*[])
{
vtkVector3i vec3i(0, 6, 9);
// Store up any errors, return non-zero if something fails.
int retVal = 0;
// Test out vtkVector3i and ensure the ostream operator is working.
cout << "vec3i -> " << vec3i << endl;
// Test the equality operator.
vtkVector3i vec3ia(0, 6, 9);
vtkVector3i vec3ib(0, 6, 8);
vtkVector<int, 3> vector3f(vec3i.GetData());
vector3f[0] = vector3f[2] = 6;
if (!(vec3i == vec3ia))
{
cerr << "vec3i == vec3ia failed (are equal, but reported not)." << endl
<< "vec3i = " << vec3i << ", vec3ia = " << vec3ia << endl;
++retVal;
}
if (vec3ia == vec3ib)
{
cerr << "vec3ia == vec3ib failed (are not equal, but reported equal)."
<< endl << "vec3i = " << vec3i << ", vec3ia = " << vec3ia << endl;
++retVal;
}
if (vector3f == vec3i)
{
cerr << "vector3f == vec3ib failed (are not equal, but reported equal)."
<< endl << "vec3i = " << vector3f << ", vec3ia = " << vec3ia << endl;
++retVal;
}
// Test the inequality operator.
if (vec3i != vec3ia)
{
cerr << "vec3i != vec3ia (reported as not equal, but are equal)." << endl
<< "vec3i = " << vec3i << ", vec3ia = " << vec3ia << endl;
++retVal;
}
// Does the + operator work as expected???
vtkVector3i result = vec3ia + vec3ib;
if (result != vtkVector3i(0, 12, 17))
{
cerr << "Vector addition operator failed." << endl;
cerr << vec3ia << " + " << vec3ib << " = " << result << endl;
++retVal;
}
result = vec3ia - vec3ib;
if (result != vtkVector3i(0, 0, 1))
{
cerr << "Vector subtraction operator failed." << endl;
cerr << vec3ia << " - " << vec3ib << " = " << result << endl;
++retVal;
}
result = vec3ia * vec3ib;
if (result != vtkVector3i(0, 36, 72))
{
cerr << "Vector multiplication operator failed." << endl;
cerr << vec3ia << " * " << vec3ib << " = " << result << endl;
++retVal;
}
vec3i.SetX(1);
result = vec3ia / vec3i;
if (result != vtkVector3i(0, 1, 1))
{
cerr << "Vector division operator failed." << endl;
cerr << vec3ia << " / " << vec3i << " = " << result << endl;
++retVal;
}
return retVal;
}
......@@ -226,14 +226,4 @@ public:
explicit vtkVector3d(const double *init) : vtkVector3<double>(init) {}
};
// Some operators for easy addition etc
inline const vtkVector2f operator+(const vtkVector2f &lhs, const vtkVector2f &rhs)
{
return vtkVector2f(lhs[0] + rhs[0], lhs[1] + rhs[1]);
}
inline const vtkVector2f operator-(const vtkVector2f &lhs, const vtkVector2f &rhs)
{
return vtkVector2f(lhs[0] - rhs[0], lhs[1] - rhs[1]);
}
#endif // __vtkVector_h
// This set of operators enhance the vtkVector classes, allowing various
// operator overloads one might expect.
#include "vtkVector.h"
#include "vtkIOStream.h"
// Description:
// Output the contents of a vector, mainly useful for debugging.
template<typename A, int Size>
ostream& operator<<(ostream& out, const vtkVector<A, Size>& v)
{
out << "(";
bool first = true;
for (int i = 0; i < Size; ++i)
{
if (first)
{
first = false;
}
else
{
out << ", ";
}
out << v[i];
}
out << ")";
return out;
}
// Description:
// Equality operator performs an equality check on each component.
template<typename A, int Size>
bool operator==(const vtkVector<A, Size>& v1, const vtkVector<A, Size>& v2)
{
bool ret = true;
for (int i = 0; i < Size; ++i)
{
if (v1[i] != v2[i])
{
ret = false;
}
}
return ret;
}
// Description:
// Inequality for vector type.
template<typename A, int Size>
bool operator!=(const vtkVector<A, Size>& v1, const vtkVector<A, Size>& v2)
{
return !(v1 == v2);
}
// 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:
// 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:
// 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 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 vtkVectorOperatorPlus(vectorType, type, size) \
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) \
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) \
vectorType operator*(const vectorType& v1, const vectorType& v2) \
{ \
return vectorType((static_cast<vtkVector<type, size> >(v1) * \
static_cast<vtkVector<type, size> >(v2)).GetData()); \
}
#define vtkVectorOperatorDivide(vectorType, type, size) \
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) \
vtkVectorOperatorPlus(vectorType, type, size) \
vtkVectorOperatorMinus(vectorType, type, size) \
vtkVectorOperatorMultiply(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)
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment