[vtkusers] Assigning multiple colors to two merged cylinders

David Doria daviddoria+vtk at gmail.com
Fri Jan 8 20:31:11 EST 2010


On Fri, Jan 8, 2010 at 8:55 AM, David Doria <daviddoria+vtk at gmail.com> wrote:
> On Fri, Jan 8, 2010 at 6:47 AM, Mirko Boettcher <mail at mirkoboettcher.de> wrote:
>> Hi,
>>
>> I'm facing the following problem in vtk. I have two vtkPolyData objects
>> each created from vtkCylinderSource. I will combine both into one
>> vtkActor, but each one should have a different color.
>>
>> While combining the vtkPolyData using vtkAppendPolyData is quite
>> simple I still have no clue how to implement that each cylinder has
>> a different color.
>>
>> Could someone please hint me at how to carry out this task?
>>
>> Kind Regards,
>>
>> Mirko
>>
>> Here is my code:
>>
>>       // create the first cylinder
>>       vtkCylinderSource cyl = new vtkCylinderSource();
>>        cyl.SetHeight(length);
>>        cyl.SetCenter(0,length, 0);
>>        cyl.SetRadius(RADIUS);
>>        cyl.SetResolution(RESOLUTION);
>>        cyl.CappingOn();
>>
>>        // get the polydata
>>        vtkPolyData cylData = cyl.GetOutput();
>>
>>
>>        // create the second cylinder
>>        vtkCylinderSource projLine = new vtkCylinderSource();
>>        projLine.SetHeight(time);
>>        projLine.SetCenter(0, length2, 0);
>>        projLine.SetRadius(RADIUS / 50.0);
>>        projLine.SetResolution(RESOLUTION);
>>        projLine.CappingOn();
>>
>>        // get the polydata
>>        vtkPolyData projLineData = projLine.GetOutput();
>>
>>        // append both polydata sets
>>        vtkAppendPolyData apd = new vtkAppendPolyData();
>>        apd.AddInput(cylData);
>>        apd.AddInput(projLineData);
>>
>>
>>        // create the actor
>>        vtkActor actor = new vtkActor();
>>        actor.SetPosition(position);
>>        actor.RotateX(90);
>>        actor.SetOrigin(0, 0, 0);
>>
>>        actor.SetMapper(cylMapper);
>>        actor.VisibilityOn();
>>
>
> I made a shell example if anyone wants to add the coloring code:
>
> http://www.cmake.org/Wiki/VTK/Examples/ColorAnActor
>
> Thanks,
>
> David
>

I added the code to color an actor. However, if you want to combine
two polydata's and then create an actor, you'd have to color the
polydata's first.

The only way I'd know to do that is create a color array such that
there is one color per cell in the polydata. See
http://www.cmake.org/Wiki/VTK/Examples/TriangleSolidColor

for a demo.

Thanks,

David



More information about the vtkusers mailing list