[vtkusers] Adding chars to 3D char array in python or how to add colors to array?
David Gobbi
david.gobbi at gmail.com
Mon Dec 21 03:42:22 EST 2009
Hi Lynx,
Use SetTuple3(), the doubles are converted to the array type before
they are stored.
colors = vtk.vtkUnsignedCharArray()
colors.SetNumberOfComponents (3)
colors.InsertNextTuple3(255, 255, 255)
colors.InsertNextTuple3(0, 255, 127)
Also a note about this example: it's best to use SetScalars() to add
colors to the data, so that you don't have to do anything special in
order to display them. That example is misleading, the array name
"Colors" doesn't have any special meaning in VTK.
smoothed_polys.GetPointData().SetScalars(colors)
Hope this helps.
David
On Mon, Dec 21, 2009 at 12:18 AM, <lynx.abraxas at freenet.de> wrote:
> Hello!
>
>
> I've got the problem now that colors.InsertNextTupleValue(color) yields an
> attribute error in my python code (translation from
> http://www.vtk.org/Wiki/VTK_Examples_Color_a_mesh_by_height).
>
> It would need a pointeerr that again does not exist in python but there seems no
> function like InsertNextTupleValue(color[0],color[1],color[2]). SetTuple3 of
> vtkArray wouldn't do because the colours need to be chars (if I'm not
> mistaken).
> It works with colors.InsertNextValue(int(dc)) (see code below and pic) but it
> seems to me that only one of rgb is added that way or is that just the default
> lookuptabel?
> Is there a documentation for the wrapper API of vtk somewhere?
>
> Thanks for any help or hints.
> Lynx
>
>
> _______________________
>
> smoother= vtk.vtkWindowedSincPolyDataFilter()
> smoother.SetInput(contour.GetOutput());
> smoother.SetNumberOfIterations(20);
> #smoother.BoundarySmoothingOff();
> #smoother.FeatureEdgeSmoothingOff();
> #smoother.SetFeatureAngle(120.0);
> #smoother.SetPassBand(.001);
> smoother.NonManifoldSmoothingOn();
> smoother.NormalizeCoordinatesOn();
> smoother.GenerateErrorScalarsOn(); #GenerateErrorVectorsOn ();
> smoother.Update();
>
> smoothed_polys= smoother.GetOutput()
>
>
> ## colouring of verts according to displacement of smoother
>
> smoother_error= smoothed_polys.GetPointData().GetScalars()
>
> ##Find min and max z
> se_range= smoother_error.GetRange ()
> minz= se_range[0] #min(smoother_error)
> maxz= se_range[1] #max(smoother_error)
> print "min/max", minz, maxz
> minz= -1 #this way colours of different particles are comparable
> maxz= 1
>
> ## Create the color map
> colorLookupTable= vtk.vtkLookupTable()
> colorLookupTable.SetTableRange(minz, maxz)
> colorLookupTable.Build()
>
> ## Generate the colors for each point based on the color map
> colors= vtk.vtkUnsignedCharArray()
> colors.SetNumberOfComponents ( 3 )
> colors.SetName ( "Colors" );
>
> color= [0] * 3
> #color= [0, 0, 0]
> for i in range(0, smoothed_polys.GetNumberOfPoints()):
> #p= smoothed_polys.GetPoint(i)
> cp= smoother_error.GetTuple1(i)
> #dcolor= colorLookupTable.GetColor(cp)
> dcolor= colorLookupTable.GetTableValue(cp) #rgba
> #print "dcolor", dcolor
> for j in range(0, 3): #using only rgb
> dc= 255 * dcolor[j]
> #print j, color
> color[j]= int(dc)
> colors.InsertNextValue(int(dc))
> #colors.InsertNextTupleValue(color)
>
> ##assign colours
> smoothed_polys.GetPointData().AddArray(colors);
>
>
>
>
>
>
> _______________________________________________
> 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
>
> Follow this link to subscribe/unsubscribe:
> http://www.vtk.org/mailman/listinfo/vtkusers
>
>
More information about the vtkusers
mailing list