Commit 522fa94f authored by Marcus D. Hanwell's avatar Marcus D. Hanwell
Browse files

ENH: Added a Cast() function to vtkVector.

Also added testing, which allows user to cast a vtkVector to a different
type, and with GetData() allows you to pass that cast type into normal
VTK functions that take float*, double* etc. Additional constructors
were added to make initialization from int, float and double pointers
easier too.

Change-Id: I90a2502a21072c271c16c96233a3f9fa6f14e355
parent a28e31ce
......@@ -25,11 +25,11 @@ int TestVector(int, char*[])
vtkVector2i vec2i;
cout << "Size of vtkVector2i: " << sizeof(vec2i) << endl;
int arr2i[2];
// just to avoid warning
arr2i[0]=0;
arr2i[1]=0;
cout << "Size of int[2]: " << sizeof(arr2i) << endl;
if (sizeof(vec2i) != sizeof(arr2i))
......@@ -86,6 +86,18 @@ int TestVector(int, char*[])
}
}
// Test out casting...
vtkVector<float, 3> castVec = vec3i.Cast<float>();
vtkVector3d castVecd(castVec.Cast<double>().GetData());
if (castVecd[0] < -0.0000001 || castVecd[0] > 0.0000001)
{
// Then the number did not make it through within reasonable precision.
cerr << "Error: castVecd value incorrect. Should be ~0.0 for component 1."
<< "\ncastVecd[0] = " << castVecd[0] << endl;
return 1;
}
cout << "castVecd[0] = " << castVecd[0] << endl;
// Now to test out one of the color classes and memory layouts of arrays
// Note that the memory layout of a vtkColor3ub[5] is the same as an unsigned
// char[15], and can be addressed as such.
......
......@@ -65,6 +65,19 @@ public:
// used in much the same way as vector.at(i) is used.
T operator()(int i) const { return this->Data[i]; }
// Description:
// Cast the vector to the specified type, returning the result.
template<typename TR>
vtkVector<TR, Size> Cast() const
{
vtkVector<TR, Size> result;
for (int i = 0; i < Size; ++i)
{
result[i] = static_cast<TR>(Data[i]);
}
return result;
}
protected:
// Description:
// The only thing stored in memory!
......@@ -127,6 +140,8 @@ public:
this->Data[2] = z;
}
vtkVector3(const T* init) : vtkVector<T, 3>(init) { }
// Description:
// Set the x, y and z components of the vector.
void Set(const T& x, const T& y, const T& z)
......@@ -180,6 +195,8 @@ public:
this->Data[3] = height;
}
vtkRect(const T* init) : vtkVector<T, 4>(init) { }
// Description:
// Set the x, y components of the rectangle, and the width/height.
void Set(const T& x, const T& y, const T& width, const T& height)
......@@ -234,6 +251,7 @@ class vtkVector2i : public vtkVector2<int>
{
public:
vtkVector2i(int x = 0, int y = 0) : vtkVector2<int>(x, y) {}
vtkVector2i(const int *init) : vtkVector2<int>(init) {}
};
class vtkVector2f : public vtkVector2<float>
......@@ -247,12 +265,14 @@ class vtkVector2d : public vtkVector2<double>
{
public:
vtkVector2d(double x = 0.0, double y = 0.0) : vtkVector2<double>(x, y) {}
vtkVector2d(const double *init) : vtkVector2<double>(init) {}
};
class vtkVector3i : public vtkVector3<int>
{
public:
vtkVector3i(int x = 0, int y = 0, int z = 0) : vtkVector3<int>(x, y, z) {}
vtkVector3i(const int *init) : vtkVector3<int>(init) {}
};
class vtkVector3f : public vtkVector3<float>
......@@ -260,6 +280,7 @@ class vtkVector3f : public vtkVector3<float>
public:
vtkVector3f(float x = 0.0, float y = 0.0, float z = 0.0)
: vtkVector3<float>(x, y, z) {}
vtkVector3f(const float *init) : vtkVector3<float>(init) {}
};
class vtkVector3d : public vtkVector3<double>
......@@ -267,6 +288,7 @@ class vtkVector3d : public vtkVector3<double>
public:
vtkVector3d(double x = 0.0, double y = 0.0, double z = 0.0)
: vtkVector3<double>(x, y, z) {}
vtkVector3d(const double *init) : vtkVector3<double>(init) {}
};
class vtkRecti : public vtkRect<int>
......@@ -274,6 +296,7 @@ class vtkRecti : public vtkRect<int>
public:
vtkRecti(int x = 0, int y = 0, int width = 0, int height = 0)
: vtkRect<int>(x, y, width, height) {}
vtkRecti(const int *init) : vtkRect<int>(init) {}
};
class vtkRectf : public vtkRect<float>
......@@ -281,6 +304,7 @@ class vtkRectf : public vtkRect<float>
public:
vtkRectf(float x = 0.0, float y = 0.0, float width = 0.0, float height = 0.0)
: vtkRect<float>(x, y, width, height) {}
vtkRectf(const float *init) : vtkRect<float>(init) {}
};
class vtkRectd : public vtkRect<double>
......@@ -289,6 +313,7 @@ public:
vtkRectd(double x = 0.0, double y = 0.0, double width = 0.0,
double height = 0.0)
: vtkRect<double>(x, y, width, height) {}
vtkRectd(const double *init) : vtkRect<double>(init) {}
};
#endif // __vtkVector_h
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