[vtkusers] vtkDenseArray with custom type?
David Doria
daviddoria+vtk at gmail.com
Fri Nov 13 13:39:35 EST 2009
On Thu, Nov 12, 2009 at 1:50 PM, David Doria <daviddoria+vtk at gmail.com> wrote:
> On Fri, Nov 6, 2009 at 11:27 AM, Jeff Baumes <jeff.baumes at kitware.com> wrote:
>>> So since the vtkDenseArray stores everything internally as a
>>> vtkVariant I guess I'd have to read up on vtkVariant and write a
>>> conversion function from my type to vtkVariant? Is this the way to go?
>>
>> Forwarded from vtkArray expert Tim Shead:
>> First-and-foremost, vtkDenseArray *does not* use vtkVariant for storage.
>> The vtkArray interface has GetVariantValue(), GetVariantValueN(),
>> SetVariantValue(), and SetVariantValueN() methods that the concrete array
>> types must implement. The vtkDenseArray and vtkSparseArray implementations
>> assume that your type is implicitly-convertible to vtkVariant, which is true
>> of all the "official" VTK types defined in vtkType.h. These methods are
>> there as a convenience, and for consistency with vtkAbstractArray.
>>
>> So, I see several possible avenues to explore:
>>
>> * Make your type implicitly convertible to vtkVariant. As long as you
>> aren't actually using the vtkVariant get and set methods, this conversion
>> doesn't have to actually do anything ... i.e. it could be as simple as
>>
>> struct Point
>> {
>> // Other stuff here ...
>>
>> operator vtkVariant() { return vtkVariant(); }
>> };
>>
>> * Copy-and-paste vtkDenseArray to create your own array implementation, and
>> replace the vtkVariant get and set methods with non-functioning stubs.
>>
>> * Convince the VTK developers to get rid of the vtkVariant get and set
>> methods, since they're imposing the conversion-to-vtkVariant requirement.
>>
>> * Convince the VTK developers to refactor vtkVariant so it can contain any
>> type, not just "official" types. boost::any is an example of how it's done.
>>
>> Cheers,
>> Tim
>
> Tim and Jeff,
>
> Excellent, simply adding a vtkVariant() operator worked perfectly (it
> has to be const though).
>
> It is just a bit awkward to have to do this, but it surely is not a
> good enough argument to have someone implement boost::any haha.
>
> Thanks for the help - here's a demo for anyone else interested:
>
> #include <vtkSmartPointer.h>
> #include <vtkDenseArray.h>
>
> struct Point
> {
> double x,y,z;
>
> operator vtkVariant() const { return vtkVariant(); }
> };
>
> int main(int argc, char *argv[])
> {
> Point MyPoint;
> MyPoint.x = 1.0;
> MyPoint.y = 2.0;
> MyPoint.z = 3.0;
> vtkstd::cout << MyPoint.x << " " << MyPoint.y << " " << MyPoint.z <<
> vtkstd::endl;
>
> vtkSmartPointer<vtkDenseArray<Point> > array =
> vtkSmartPointer<vtkDenseArray<Point> >::New();
> array->Resize(5,5);
>
> array->SetValue(4,4, MyPoint);
>
> Point RetrievedPoint = array->GetValue(4,4);
> vtkstd::cout << RetrievedPoint.x << " " << RetrievedPoint.y << " "
> << RetrievedPoint.z << vtkstd::endl;
> return 0;
> }
>
> Thanks,
>
> David
>
Another observation - it seems to work fine without adding anything
about Variant if you make a vtkDenseArray of a VTK class (derived from
vtkObject).
Thanks,
David
More information about the vtkusers
mailing list