[vtkusers] Setting colors to individual cell

Xianjin Yang Yang at AGIUSA.COM
Mon Apr 21 15:02:33 EDT 2003


Hi Jonathan and Nanditha,

The demo code below renders 8 points at corners of a fictitious cube 
with 8 different colors. With the help of vtkLookupTable and scalars,
you can modify the color of any point or cell. Note that scalars is a
1-D integer array and is served as the index of vtkLookupTable. 
You should be able to adapt this demo to solve your problem.

HTH.

Yang

----------------- C++ code -------------------------------
  int i;
  float DataPoints[8][3] = { {0.0, 0.0, 0.0}, {1.0, 0.0, 0.0}, {1.0, 1.0, 0.0}, 
                     {0.0, 1.0, 0.0}, {0.0, 0.0, 1.0}, {1.0, 0.0, 1.0}, 
                     {1.0, 1.0, 1.0},  {0.0, 1.0, 1.0}};
  float colors[8][3] = { {1.0, 0.0, 0.0}, {1.0, 0.0, 0.0}, {1.0, 1.0, 0.0},
                       {0.0, 1.0, 0.0}, {0.0, 0.0, 1.0}, {1.0, 0.0, 1.0}, 
                       {1.0, 1.0, 1.0},  {0.0, 1.0, 1.0} };
  vtkLookupTable *lut = vtkLookupTable::New();
    lut->SetNumberOfColors(8);
    lut->Build();
    for (i=0; i<8; ++i)
        lut->SetTableValue(i, colors[i][0], colors[i][1], colors[i][2], 1.0);

  vtkPoints * pts = vtkPoints::New();
  vtkCellArray * ca = vtkCellArray::New();
  vtkPolyData * pd = vtkPolyData::New();
  vtkIntArray * scalars = vtkIntArray::New();

  for (i=0;i<8;i++)
  {
      pts->InsertNextPoint(DataPoints[i][0],DataPoints[i][1],DataPoints[i][2]);
      ca->InsertNextCell(1);
      ca->InsertCellPoint(i);
      scalars->InsertNextTuple1(i);
  }
  pd->SetPoints(pts);
  pd->SetVerts(ca);
  pd->GetPointData()->SetScalars(scalars);

  vtkPolyDataMapper * m = vtkPolyDataMapper::New();
  m->SetLookupTable(lut);
  m->SetInput(pd);
  m->SetScalarRange(0, 7);

  vtkRenderer* ren = vtkRenderer::New();
  vtkActor  * a = vtkActor::New();
  a->SetMapper(m);
  a->GetProperty()->SetPointSize(8);
  ren->AddActor(a);
  vtkWindow1->GetRenderer()->AddActor(a);

  scalars->Delete();
  ca->Delete();
  lut->Delete();
  pd->Delete();
  m->Delete();
  a->Delete();
  ren->Delete();

  // I am using Borland C++ Builder, and
  // vtkWindow1 is a TvtkBorlandRenderWindow on my form
  vtkWindow1->GetRenderer()->ResetCamera();
  vtkWindow1->Invalidate();
-------------------------------------

-----Original Message-----
From: Nanditha Thakur [mailto:nanditha_thakur at rediffmail.com] 
Sent: Friday, April 18, 2003 7:51 AM
To: Jonathan Bailleul
Cc: VTK-User
Subject: Re: Re: [vtkusers] Setting colors to individual cell


Hi Jonathan,
              I still have not succeeded in my method. I am able 
to change the scalar value, but the result is far from satisfying. 
Can you please elborate on your approach. It will be of great 
help.

thanks
Nanditha
On Thu, 03 Apr 2003 Jonathan Bailleul wrote :
>Nanditha Thakur wrote:
> >
> > Hello Users,
> >                I have a 3D object. Can I set different color
>to
> > some of the cells(or points) of this object? This is how my 
> > requirement is going to be. Say my actor is blue in color.I
>will
> > pick some of the cells of the object by clicking on it. Now I
>want
> > to set, say red color, to these picked cells. Is it possible
>to do
> > so in VTK? Help!
> >
> > Thanks
> > Nanditha
> >
>
>
>Dear Nanditha,
>
>I am just dealing with a similar problem right now! My choice
>consisted
>in glyphing the points of the selected cell instead, since I just 
>didn't
>figure out how to proceed otherwise.
>
>I still have problems to solve, but it seems fairly usable. Let
>me know
>if you need to know more about this, or if you managed to do what 
>you
>planned!
>
>Best Regards,
>
>



More information about the vtkusers mailing list