[vtk-developers] paint on plane, triggered by mouse event

David Doria daviddoria at gmail.com
Sun Sep 18 10:36:45 EDT 2011


On Sun, Sep 18, 2011 at 6:39 AM, Martin Waitzbauer <mazzok at gmx.at> wrote:

> Hello, i want to write a small program, that is painting the tiles o a
> plane, if the user clicks the left mouse button
>
> i tried it like this (most parts taken from
> http://www.vtk.org/Wiki/VTK/Examples/Cxx/PolyData/ColorCells)
>
> KeyPressInteractorStyle is a self written class, doesnt do anything special
> than logging left mouse buttons, if the user clicks the left button than
> arraydata is being filled with a new value,and the window is rendered
>
> the method is like this
> virtual void OnLeftButtonDown(){
>                std::cout << "The left mouse key was pressed." << std::endl;
>                int r = rand() %2;
>                arraydata->SetValue(1,r);
>                this->GetInteractor()->GetRenderWindow()->Render();
>        }
>
> Unforrtuantly i wasnt able to make it do so!
> changes in the array arent displayed on the screen
>
> so my quetion is, how can i access the plane grid data
> (set by aPlane->GetOutput()->GetCellData()->SetScalars(cellData);) in a
> way, that changes , triggered by mouse events get displayed?
>
> Tahsnk alot!
>
> cheers,M
>
> int main...{
>  int resolution = 3;
>  vtkSmartPointer<vtkPlaneSource> aPlane =
>    vtkSmartPointer<vtkPlaneSource>::New();
>  aPlane->SetXResolution(resolution);
>  aPlane->SetYResolution(resolution);
>
>  // Create cell data
>  vtkSmartPointer<vtkFloatArray> cellData =
>    vtkSmartPointer<vtkFloatArray>::New();
>  for (int i = 0; i < resolution * resolution; i++)
>    {
>          cellData->InsertNextValue(0);
>  }
>
>  cellData->SetValue(5,2);
>
>  // Create a lookup table to map cell data to colors
>  vtkSmartPointer<vtkLookupTable> lut =
>    vtkSmartPointer<vtkLookupTable>::New();
>  int tableSize = std::max(resolution*resolution + 1, 10);
>  lut->SetNumberOfTableValues(tableSize);
>  lut->Build();
>
>  // Fill in a few known colors, the rest will be generated if needed
>  lut->SetTableValue(0     , 0     , 0     , 0, 1);  //Black
>  lut->SetTableValue(1, 0.8900, 0.8100, 0.3400, 1); // Banana
>  lut->SetTableValue(2, 1.0000, 0.3882, 0.2784, 1); // Tomato
>  lut->SetTableValue(3, 0.9608, 0.8706, 0.7020, 1); // Wheat
>  lut->SetTableValue(4, 0.9020, 0.9020, 0.9804, 1); // Lavender
>  lut->SetTableValue(5, 1.0000, 0.4900, 0.2500, 1); // Flesh
>  lut->SetTableValue(6, 0.5300, 0.1500, 0.3400, 1); // Raspberry
>  lut->SetTableValue(7, 0.9804, 0.5020, 0.4471, 1); // Salmon
>  lut->SetTableValue(8, 0.7400, 0.9900, 0.7900, 1); // Mint
>  lut->SetTableValue(9, 0.2000, 0.6300, 0.7900, 1); // Peacock
>
>  aPlane->Update(); // Force an update so we can set cell data
>  aPlane->GetOutput()->GetCellData()->SetScalars(cellData);
>  // Setup actor and mapper
>  vtkSmartPointer<vtkPolyDataMapper> mapper =
>    vtkSmartPointer<vtkPolyDataMapper>::New();
>  mapper->SetInputConnection(aPlane->GetOutputPort());
>  mapper->SetScalarRange(0, tableSize - 1);
>
>  mapper->SetLookupTable(lut);
>
>  vtkSmartPointer<vtkActor> actor =
>    vtkSmartPointer<vtkActor>::New();
>  actor->SetMapper(mapper);
>
>  // Setup render window, renderer, and interactor
>  vtkSmartPointer<vtkRenderer> renderer =
>    vtkSmartPointer<vtkRenderer>::New();
>  vtkSmartPointer<vtkRenderWindow> renderWindow =
>    vtkSmartPointer<vtkRenderWindow>::New();
>  renderWindow->AddRenderer(renderer);
>  vtkSmartPointer<vtkRenderWindowInteractor> renderWindowInteractor =
>      vtkSmartPointer<vtkRenderWindowInteractor>::New();
>  renderWindowInteractor->SetRenderWindow(renderWindow);
>  renderer->AddActor(actor);
>  renderer->SetBackground(.1,.2,.3);
>  renderWindowInteractor->Initialize();
>
> //set up interactor
>  KeyPressInteractorStyle * ss = KeyPressInteractorStyle::New();
>
>  renderWindowInteractor->SetInteractorStyle(ss);
>  ss->Setarraydata(cellData);
>
>  renderWindowInteractor->Start();
> }
>
>
This type of question is better posed to the vtk-users mailing list. I
suggest sending it there, as well as modifying the code you posted to be
fully compilable, so that a reader can simply copy and paste, compile, and
see what is going on. You can also simply the code as much as possible -
perhaps use a more standard lookup table instead of the manual one you are
creating. It psychologically makes it look like less code so people are more
inclined to look at it :)

David
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://public.kitware.com/pipermail/vtk-developers/attachments/20110918/3f4f0737/attachment.html>


More information about the vtk-developers mailing list