Hi,<br><br>SafeDownCast is a simple call of static_cast adding an inheritance check. <br>If you call A->SafeDownCast( B), it will check that B is a A (and perform the cast if yes). In your case, vtkDoubleArray is *not* a vtkIntArray.<br>
That's why you cannot cast in that way. <br><br>You should take a look at vtkImageCast filter, that is able to cast scalars between different types.<br><br>Hope that helps,<br>Jerome<br><br><div class="gmail_quote">2009/10/8 David Doria <span dir="ltr"><<a href="mailto:daviddoria%2Bvtk@gmail.com">daviddoria+vtk@gmail.com</a>></span><br>
<blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;"><br><div class="gmail_quote"><div><div></div><div class="h5">On Tue, Oct 6, 2009 at 4:30 PM, Fabio Meneghini <span dir="ltr"><<a href="mailto:fab.meneghini@gmail.com" target="_blank">fab.meneghini@gmail.com</a>></span> wrote:<br>
<blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;">
Hi all,<br>just like the subject of this topic says:<br>Let's suppose I got a vtkPolyData with one vtkDataArray field with scalar values, of type unsigned char. <br>Is there a way to quickly cast all the unsigned char values after loading and before visualizing the data??<br>
<br>Thanks in advance,<br><br>Cheers.<br><font color="#888888"><br>Fabio Meneghini<br>
</font></blockquote></div></div><div><br>I thought SafeDowncast would do this - but I guess not? Can anyone explain why this example does not work (my "invalid cast" output is hit):<br><br>#include <vtkDoubleArray.h><br>
#include <vtkIntArray.h><br>
#include <vtkPoints.h><br>
#include <vtkPolyData.h><br>
#include <vtkPointData.h><br>
<br>
#include <iostream><br>
<br>
int main(int argc, char *argv[])<br>
{<br>
//create points<br>
vtkPoints* Points = vtkPoints::New();<br>
<br>
unsigned int NumberOfPoints = 3;<br>
Points->InsertNextPoint(0.0, 0.0, 0.0);<br>
Points->InsertNextPoint(1.0, 0.0, 0.0);<br>
Points->InsertNextPoint(0.0, 1.0, 0.0);<br>
<br>
//add the points to a polydata<br>
vtkPolyData* polydata = vtkPolyData::New();<br>
polydata->SetPoints(Points);<br>
<br>
//add distances to each point<br>
vtkDoubleArray* Distances = vtkDoubleArray::New();<br>
Distances->SetNumberOfComponents(1);<br>
Distances->SetName("Distances");<br>
<br>
Distances->InsertNextValue(1.1);<br>
Distances->InsertNextValue(2.2);<br>
Distances->InsertNextValue(3.3);<br>
<br>
polydata->GetPointData()->AddArray(Distances);<br>
<br>
//get the distances from the polydata<br>
vtkDoubleArray* Array = vtkDoubleArray::SafeDownCast(polydata->GetPointData()->GetArray("Distances"));<br>
<br>
if(Array)<br>
{<br>
for(unsigned int i = 0; i < NumberOfPoints; i++)<br>
{<br>
double dist;<br>
dist = Array->GetValue(i);<br>
std::cout << "Distance: " << dist << std::endl;<br>
}<br>
}<br>
<br>
//cast the double distances to ints<br>
vtkDoubleArray* DoubleDistances = vtkDoubleArray::SafeDownCast(polydata->GetPointData()->GetArray("Distances"));<br>
vtkIntArray* IntDistances = vtkIntArray::SafeDownCast(DoubleDistances);<br>
<br>
if(IntDistances)<br>
{<br>
for(unsigned int i = 0; i < NumberOfPoints; i++)<br>
{<br>
int dist;<br>
dist = IntDistances->GetValue(i);<br>
std::cout << "Distance: " << dist << std::endl;<br>
}<br>
}<br>
else<br>
{<br>
std::cout << "invalid cast." << std::endl;<br>
}<br>
<br>
return 0;<br>
}<br>
<br>
<br clear="all">Thanks,<br><br>David <br></div></div>
<br>_______________________________________________<br>
Powered by <a href="http://www.kitware.com" target="_blank">www.kitware.com</a><br>
<br>
Visit other Kitware open-source projects at <a href="http://www.kitware.com/opensource/opensource.html" target="_blank">http://www.kitware.com/opensource/opensource.html</a><br>
<br>
Please keep messages on-topic and check the VTK FAQ at: <a href="http://www.vtk.org/Wiki/VTK_FAQ" target="_blank">http://www.vtk.org/Wiki/VTK_FAQ</a><br>
<br>
Follow this link to subscribe/unsubscribe:<br>
<a href="http://www.vtk.org/mailman/listinfo/vtkusers" target="_blank">http://www.vtk.org/mailman/listinfo/vtkusers</a><br>
<br></blockquote></div><br>