[vtkusers] Adding chars to 3D char array in python or how to add colors to array?

lynx.abraxas at freenet.de lynx.abraxas at freenet.de
Mon Dec 21 15:22:24 EST 2009


On 21/12/09 12:56:42, David Gobbi wrote:
> On Mon, Dec 21, 2009 at 12:32 PM,  <lynx.abraxas at freenet.de> wrote:
> > On 21/12/09 01:42:22, David Gobbi wrote:
> >> Hi Lynx,
> >>
> >> Use SetTuple3(), the doubles are converted to the array type before
> >> they are stored.
> >
> > Thanks David G. for this hint. It works nicely with this.
> >
> >> 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)
> >
> > Hm, I'm not getting this. Isn't my colors actually a vector? If I use
> > smoothed_polys.GetPointData().SetScalars(colors) instead of
> > smoothed_polys.GetPointData().AddArray(colors)
> 
> VTK Scalars are allowed to have multiple components, so that is not a problem.

Puh, this is not very intuitive, but OK.
 
> > I get an error in vtkDataArray.cxx line 470:
> > vtkUnsignedCharArray ...: The number of components do not match the number requested: 3 != 1
> 
> Before you add any tuples, you have to set the number of components in
> your array:
> 
> colors.SetNumberOfComponents(3)

I do so but still the error:

    ## Generate the colors for each point based on the color map
    colors= vtk.vtkUnsignedCharArray()
    colors.SetNumberOfComponents ( 3 )
    colors.SetName ("Colors") #"Colors" doesn't have any special meaning in VTK
 
    color= [0] * 3
    for i in range(0, smoothed_polys.GetNumberOfPoints()):
        #p= smoothed_polys.GetPoint(i)
        cp= smoother_error.GetTuple1(i)
        #dcolor= colorLookupTable.GetColor(cp) #not possible with python
        dcolor= colorLookupTable.GetTableValue(cp) #rgba
        for j in range(0, 3): #using only rgb
            dc= 255 * dcolor[j]
            color[j]= int(dc)
            #colors.InsertNextValue(int(dc))
        #colors.InsertNextTupleValue(color) #not possible with python
        colors.InsertNextTuple3(color[0], color[1], color[2])#doubles are converted to the array type before they are stored

    ##assign colours
    #smoothed_polys.GetPointData().AddArray(colors) #works
    smoothed_polys.GetPointData().SetScalars(colors)#doesn't work


Any way, I think I can do what I want much easier: I just want my vertices (the face color interpolating between vert colours) to be colored according to the amount they got moved by the smoother filter (windowedSinc).
So if I use smoother.GenerateErrorScalarsOn() will these scalars aready be used by the mapper? I just read something similar in the Visualization Toolkit book on page 197. So I'd just need to define my lookup table as I want the point displacement to be coloured, is that right? Strangely there is black in the colours which shouldn't be the case since I just allow a change in the hue range:

    ## Create the color map
    colorLookupTable= vtk.vtkLookupTable()
    colorLookupTable.SetTableRange(minz, maxz)
    colorLookupTable.SetHueRange(1, 0)
    colorLookupTable.SetSaturationRange(1, 1)
    colorLookupTable.SetValueRange(1, 1)
    #colorLookupTable.SetNumberOfColors(256) #256 default
    colorLookupTable.Build()

    mapper= vtk.vtkPolyDataMapper()
    mapper.SetInput(smoothed_polys) #this has no normals
    #mapper.SetInput(triangleCellNormals.GetOutput()) #this is better for vis;-)
    mapper.ScalarVisibilityOn()#show colour 
    #mapper.SetScalarModeToUseCellData()
    mapper.SetScalarModeToUsePointData() #the smoother error relates to the verts
    mapper.SetLookupTable(colorLookupTable)


Would that be correct? If I get it working I'll up load an example;-)

Thanks,
Lynx



More information about the vtkusers mailing list