<br><div class="gmail_quote">On Tue, Oct 6, 2009 at 4:30 PM, Fabio Meneghini <span dir="ltr"><<a href="mailto:fab.meneghini@gmail.com">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><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>