[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 09:06:16 EST 2009


On Mon, Dec 21, 2009 at 6:30 AM, David Doria <daviddoria+vtk at gmail.com> wrote:
> On Mon, Dec 21, 2009 at 2:42 AM, David Gobbi <david.gobbi at gmail.com> wrote:
>> 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
>
> David G,
>
> I always wondered we can't add a vtkPolyData::SetColors (which could
> simply call SetScalars). It seems much less confusing than having to
> tell people, "rather than create an array called colors, you have to
> use SetScalars". In fact,
> polydata->SetPointColors(colors)
> rather than
> polydata->GetPointData()->SetColors(colors)
>
> would also prevent having to include of vtkPointData.h
>
> What do you think about both of these things?
>
> Lynx - when you get it working, don't forget to add it to the examples
> :) As you're seeing, I don't believe there is much documentation for
> the python API so these examples will help serve as that documentation
> (so comment well!)
>
> Thanks,
>
> David

Oh, you're probably asking the wrong person.  I'm always against
adding new methods that just do the same thing as existing methods.
Let's look at the two changes that you are suggesting:

1) Adding a SetColors() method that just calls SetScalars():

This is bad because someone (a new user, for example) might have
already set Scalars for their data, because they need them for some
filter that uses scalars.  Then they call SetColors().  All of a
sudden, the scalars are gone, they've been replaced by the colors.
That is unexpected and bad.  Having two method names that set the same
data is almost always a bad idea.

2) polydata->SetPointColors(colors):

This is bad because if you do this, then it would also make sense to
add polydata->SetCellColors(), polydata->SetPointNormals(normals),
polydata->SetCellNormals(), etc., etc.  There is no end to it.  You
could say that you would only add SetPointColors() because setting
colors is the only one of those things that is a common operation.
But you would be wrong.  The things that you use VTK for might be
quite different from other people. For example I use Normals and
Scalars all the time but I very rarely use RGB colors with my
polydata.  I actually think that the way Cell/Point data is handled in
VTK is an example of very good software design.

   David



More information about the vtkusers mailing list