[vtkusers] SafeDownCast does not work properly
Allie Vacanti
allison.vacanti at kitware.com
Thu Aug 10 09:09:19 EDT 2017
Put another way, for SafeDownCast/dynamic_cast to work in this case, the
object being pointed to must be allocated and constructed as a Mesh (or a
subclass of Mesh).
The object in question is being allocated as a vtkPolyData by the file
reader. Base classes cannot be converted to derived classes by casting --
when an object is allocated as a vtkPolyData, it will always be a
vtkPolyData only, and never a Mesh. This is why the casts are returning
NULL -- SafeDownCast checks the object's class name (which is vtkPolyData)
and knows that it's not a Mesh, and thus returns NULL. dynamic_cast does
the same thing by querying RTTI. In both cases, the PolyData object is not
a Mesh as it lacks the additional fields and members that a Mesh would be
expected to have, and cannot be safely treated as a Mesh.
What you probably want to do here instead of casting is construct a Mesh
object from the PolyData object. Look at the ShallowCopy and CopyStructure
methods of most VTK datasets to see how you can copy the bulk of the
PolyData internals into your new Mesh without actually making new copies of
the data in memory.
Also, please be sure to "reply to all" when responding to mailing lists --
otherwise the conversation will not be archived or visible to the rest of
the list's members.
HTH
Allie
On Wed, Aug 9, 2017 at 5:17 PM, Utkarsh Ayachit <utkarsh.ayachit at kitware.com
> wrote:
> Maximillian,
>
> > *a downcast is required in order to convert a vtkPolyData object into
> a **Mesh object*
>
> Not true. A downcast is required to convert a *pointer* of type
> vtkPolyData to *pointer* of type Mesh, but it need not succeed i.e.
> return non-null. The cast will only succeed if the the object pointed to
> via the vtkPolyData pointer is indeed of type Mesh. vtkSTLReader will never
> create Mesh, it will only create vtkPolyData. As a result you cannot
> "downcast" it to Mesh with success.
>
> > *why do dynamic_cast work just fine whereas SafeDownCast don‘t*
>
> Hopefully the attached C++ code helps.
>
> Utkarsh
>
>
> On Wed, Aug 9, 2017 at 4:23 PM, Maximilian Weiherer <
> weiherer.maximilian at gmx.de> wrote:
>
>> Hi Utkarsh,
>>
>> as *Mesh* *is a* vtkPolyData, a downcast is required in order to convert
>> a vtkPolyData object into a *Mesh* object. I still don’t know why
>> SafeDownCast do not work properly. As far as I know, the parent class
>> (vtkPolyData) must be downcasted to *Mesh*… . If a dynamic_cast is
>> essentially the same as SafeDownCast, why do dynamic_cast work just fine
>> whereas SafeDownCast don‘t?
>>
>> Best regards
>> Maximilian
>>
>> *Von:* Utkarsh Ayachit [mailto:utkarsh.ayachit at kitware.com]
>> *Gesendet:* Mittwoch, 9. August 2017 20:33
>> *An:* Maximilian Weiherer <weiherer.maximilian at gmx.de>; ParaView <
>> paraview at paraview.org>
>>
>> *Betreff:* Re: [vtkusers] SafeDownCast does not work properly
>>
>>
>>
>> Maximilian,
>>
>>
>>
>> That's not a reasonable expectation. Mesh **isa** vtkPolyData, and not
>> vice-versa. Look at docs for dynamic_cast in C++. In spirit, SafeDownCast
>> is essentially the same thing.
>>
>>
>>
>> Utkarsh
>>
>>
>>
>> On Wed, Aug 9, 2017 at 2:12 PM, Maximilian Weiherer <
>> weiherer.maximilian at gmx.de> wrote:
>>
>> Hi Utkarsh,
>>
>>
>>
>> thanks for your reply. I created the custom mesh class in order to add
>> some new functionality (e.g. a method to check whether a mesh intersects
>> another mesh). The vtkPolyData object which should be casted is the output
>> of a vtkSTLReader, i.e.
>>
>> Mesh* mesh = Mesh::SafeDownCast(reader->GetOutput()); // mesh always
>> equals NULL
>>
>> Best regards
>>
>> Maximilian
>>
>>
>>
>> *Von:* Utkarsh Ayachit [mailto:utkarsh.ayachit at kitware.com]
>> *Gesendet:* Mittwoch, 9. August 2017 17:53
>> *An:* Maximilian Weiherer <weiherer.maximilian at gmx.de>
>> *Cc:* vtk <vtkusers at vtk.org>
>> *Betreff:* Re: [vtkusers] SafeDownCast does not work properly
>>
>>
>>
>> Maximilian,
>>
>>
>>
>> I am confused why any arbitrary vtkPolyData object would be castable to
>> "Mesh". It will only return non-null, if it's is indeed created as a Mesh.
>> Is it? Where's the "<vtkPolyData object>" coming from?
>>
>>
>>
>> Utkarsh
>>
>>
>>
>> On Wed, Aug 9, 2017 at 11:50 AM, Maximilian Weiherer <
>> weiherer.maximilian at gmx.de> wrote:
>>
>> Hi all,
>>
>>
>>
>> I defined my own mesh class (named *Mesh*) by inheriting from
>> vtkPolyData. Now I am struggling with the SafeDownCast method in order to
>> cast a vtkPolyData object into a *Mesh* object. Whenever I am applying a
>> SafeDownCast to a vtkPolyData object, NULL is returned.
>>
>>
>>
>> The *Mesh* class looks as follows:
>>
>>
>>
>> class Mesh : public vtkPolyData
>>
>> {
>>
>> public:
>>
>> static Mesh* New();
>>
>> vtkTypeMacro(Mesh, vtkPolyData);
>>
>> void PrintSelf(ostream& os, vtkIndent indent);
>>
>>
>>
>> protected:
>>
>> Mesh();
>>
>> ~Mesh();
>>
>> private:
>>
>> Mesh(const Mesh&); // Not implemented.
>>
>> void operator=(const Mesh&); // Not implemented.
>>
>> };
>>
>>
>>
>> …and this is the cast that does not work properly:
>>
>>
>>
>> Mesh* mesh = Mesh::SafeDownCast(<vtkPolyData object>); // mesh always
>> equals NULL
>>
>>
>>
>> Any help would be appreciated. Thanks!
>>
>>
>>
>> Best regards
>>
>> Maximilian
>>
>>
>>
>> [image: Das Bild wurde vom Absender entfernt.]
>> <https://www.avast.com/sig-email?utm_medium=email&utm_source=link&utm_campaign=sig-email&utm_content=emailclient>
>>
>> Virenfrei. www.avast.com
>> <https://www.avast.com/sig-email?utm_medium=email&utm_source=link&utm_campaign=sig-email&utm_content=emailclient>
>>
>>
>> _______________________________________________
>> Powered by www.kitware.com
>>
>> Visit other Kitware open-source projects at
>> http://www.kitware.com/opensource/opensource.html
>>
>> Please keep messages on-topic and check the VTK FAQ at:
>> http://www.vtk.org/Wiki/VTK_FAQ
>>
>> Search the list archives at: http://markmail.org/search/?q=vtkusers
>>
>> Follow this link to subscribe/unsubscribe:
>> http://public.kitware.com/mailman/listinfo/vtkusers
>>
>>
>>
>>
>>
>
>
> _______________________________________________
> Powered by www.kitware.com
>
> Visit other Kitware open-source projects at http://www.kitware.com/
> opensource/opensource.html
>
> Please keep messages on-topic and check the VTK FAQ at:
> http://www.vtk.org/Wiki/VTK_FAQ
>
> Search the list archives at: http://markmail.org/search/?q=vtkusers
>
> Follow this link to subscribe/unsubscribe:
> http://public.kitware.com/mailman/listinfo/vtkusers
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://public.kitware.com/pipermail/vtkusers/attachments/20170810/532650c9/attachment.html>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: image001.jpg
Type: image/jpeg
Size: 350 bytes
Desc: not available
URL: <http://public.kitware.com/pipermail/vtkusers/attachments/20170810/532650c9/attachment.jpg>
More information about the vtkusers
mailing list