[vtk-developers] Casting vtkIntArray vs vtkIdTypeArray

pat marion pat.marion at kitware.com
Fri Sep 17 17:42:56 EDT 2010


David,

Instead of saving a file and opening it in paraview, you could try:

      filter->GetOutput()->Print(cout);

This will display all the point & cell arrays on the output and their names
and data types.  Make sure you have updated the filter first.  Also, if you
notice that SafeDownCast has failed, you can do a debug print to get the
correct class type:

       cout << filter->GetOutput()->GetArray("RegionId")->GetClassName() <<
endl;

this would print: vtkIdTypeArray

GetClassName is great for debugging, but I wouldn't recommend it for
comparisons.  For example, instead of:

      if (array->GetClassName() == std::string("vtkIdTypeArray")) cout <<
"It's an id type array";

it is preferable to use:

      if (vtkIdTypeArray::SafeDownCast(array)) cout << "It's an id type
array";

The compiler can error check a SafeDownCast statement, but can't catch a
typo in a string comparison.

Pat


On Fri, Sep 17, 2010 at 4:03 PM, David Gobbi <david.gobbi at gmail.com> wrote:

> Hi David,
>
> I think you misunderstand what "SafeDownCast" does.
> After every SafeDownCast(), you must check the result
> to make sure that it isn't NULL.  That is what dynamic
> casting is all about: it is an _attempt_ to do a cast when
> you aren't sure if the object is of the correct type.
>
> You can write code that is robust to a large number of
> array types by adding a "switch" statement.  If you combine
> the switch with a templated function, then you will
> end up with the same mechanism that VTK's image filters
> use to deal with arrays of different types.
>
>  David
>
>
> On Fri, Sep 17, 2010 at 1:53 PM, David Doria <daviddoria at gmail.com> wrote:
> > On Fri, Sep 17, 2010 at 3:50 PM, David Cole <david.cole at kitware.com>
> wrote:
> >>
> >> That's not a C++ cast, that's a transformation operation....
> >> All the array classes are siblings of each other, aren't they?
> >>
> >
> > Maybe my terminology is off, but whatever it is called I feel that it
> should
> > work and it currently doesn't :)
> > Yes, they are all siblings. Are you recommending first SafeDownCasting to
> a
> > vtkDataArray, checking the type, and then performing the appropriate
> > SafeDownCast? That seems like a lot of trouble, no?
> > David
> > _______________________________________________
> > Powered by www.kitware.com
> >
> > Visit other Kitware open-source projects at
> > http://www.kitware.com/opensource/opensource.html
> >
> > Follow this link to subscribe/unsubscribe:
> > http://www.vtk.org/mailman/listinfo/vtk-developers
> >
> >
> >
> _______________________________________________
> Powered by www.kitware.com
>
> Visit other Kitware open-source projects at
> http://www.kitware.com/opensource/opensource.html
>
> Follow this link to subscribe/unsubscribe:
> http://www.vtk.org/mailman/listinfo/vtk-developers
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://public.kitware.com/pipermail/vtk-developers/attachments/20100917/e482b313/attachment.html>


More information about the vtk-developers mailing list