[vtkusers] Picking in unstructured grids and in polydata
Rob van Tol
rob.vantol at wtcm.be
Thu Sep 11 06:30:46 EDT 2003
Dear vtkusers,
I have a problem with point picking in an unstructured grid and indicating
the picked points in the renderwindow. If I apply the (nearly identical)
code to polydata the picking and showing works fine. The codes were applied
to a cube defined as polydata and as unstructrured grid.
Could it be that ugrid->Modify() does not work correctly? I use
vtkLatestRelease(4.2).
If you have an answer to the problem, please let me know.
Both codes:
//For POLYDATA (wordks
fine)-------------------------------------------------------------------------------------------------------------------
//--- VTK HEADERS ---//
#include "vtkActor.h"
#include "vtkRenderer.h"
#include "vtkRenderWindow.h"
#include "vtkRenderWindowInterActor.h"
//-------------
#include "vtkCommand.h"
#include "vtkIntArray.h"
#include "vtkPointData.h"
#include "vtkPointPicker.h"
#include "vtkPolyData.h"
#include "vtkPolyDataReader.h"
#include "vtkPolyDataMapper.h"
#include "vtkProperty.h"
//-------------
//--- GLOBALS ---//
static vtkPolyData *polydata;
static vtkPointPicker *picker = vtkPointPicker::New();
static vtkIntArray *intarray = vtkIntArray::New();
//--- PICKCOMMAND ---//
class VcPick : public vtkCommand
{
public:
static VcPick *New()
{
return new VcPick;
}
void Delete()
{
delete this;
}
virtual void Execute(vtkObject *caller, unsigned long, void*)
{
int pid = picker->GetPointId() ;
if ( pid >= 0 )
{
cout << pid ;
intarray = (vtkIntArray
*)polydata->GetPointData()->GetScalars();
if (intarray->GetValue(pid) == 0)
{
intarray->SetValue(pid,1);
polydata->GetPointData()->SetScalars(intarray);
polydata->Modified();
}
else
{
intarray->SetValue(pid,0);
polydata->GetPointData()->SetScalars(intarray);
polydata->Modified();
}
}
}
};
void main()
{
//--- SOURCES ---//
vtkPolyDataReader *datareader = vtkPolyDataReader::New();
datareader->SetFileName("Polydata.vtk");
polydata = datareader->GetOutput();
//--- MAPPERS ---//
vtkPolyDataMapper *datamapper = vtkPolyDataMapper::New();
datamapper->SetInput(polydata);
vtkPolyDataMapper *geomapper = vtkPolyDataMapper::New();
geomapper->SetInput(polydata);
//--- ACTORS ---//
vtkActor *dataactor = vtkActor::New();
dataactor->SetMapper(datamapper);
dataactor->GetProperty()->SetPointSize(10);
dataactor->GetProperty()->SetRepresentationToPoints();
vtkActor *geoactor = vtkActor::New();
geoactor->SetMapper(geomapper);
geoactor->GetProperty()->SetColor(0,0,0);
geoactor->GetProperty()->SetRepresentationToWireframe();
//--- PICKING ---//
VcPick *vcpick = VcPick::New();
picker->AddObserver(vtkCommand::EndPickEvent, vcpick);
//--- RENDERING ---//
vtkRenderer *ren1 = vtkRenderer::New();
ren1->AddActor(geoactor);
ren1->AddActor(dataactor);
ren1->SetBackground(1,1,1);
vtkRenderWindow *renwin = vtkRenderWindow::New();
renwin->AddRenderer(ren1);
vtkRenderWindowInteractor *iren = vtkRenderWindowInteractor::New();
iren->SetRenderWindow(renwin);
iren->SetPicker(picker);
renwin->Render();
iren->Start();
}
//For UNSTRUCTURED (does not show the picked
points)-------------------------------------------------------------------------------------------------------------------
//--- VTK HEADERS ---//
#include "vtkActor.h"
#include "vtkRenderer.h"
#include "vtkRenderWindow.h"
#include "vtkRenderWindowInterActor.h"
//-------------
#include "vtkCommand.h"
#include "vtkIntArray.h"
#include "vtkPointData.h"
#include "vtkPointPicker.h"
#include "vtkUnstructuredGrid.h"
#include "vtkUnstructuredGridReader.h"
#include "vtkDataSetMapper.h"
#include "vtkProperty.h"
//-------------
//--- GLOBALS ---//
static vtkUnstructuredGrid *ugrid;
static vtkPointPicker *picker = vtkPointPicker::New();
static vtkIntArray *intarray = vtkIntArray::New();
//--- PICKCOMMAND ---//
class VcPick : public vtkCommand
{
public:
static VcPick *New()
{
return new VcPick;
}
void Delete()
{
delete this;
}
virtual void Execute(vtkObject *caller, unsigned long, void*)
{
int pid = picker->GetPointId() ;
if ( pid >= 0 )
{
intarray = (vtkIntArray
*)ugrid->GetPointData()->GetScalars();
if (intarray->GetValue(pid) == 0)
{
intarray->SetValue(pid,1);
ugrid->GetPointData()->SetScalars(intarray);
ugrid->Modified();
cout <<" on" << endl;
}
else
{
intarray->SetValue(pid,0);
ugrid->GetPointData()->SetScalars(intarray);
ugrid->Modified();
cout <<" off" << endl;
}
}
}
};
void main()
{
//--- SOURCES ---//
vtkUnstructuredGridReader *gridreader =
vtkUnstructuredGridReader::New();
gridreader->SetFileName("UnstructuredGrid.vtk");
ugrid = gridreader->GetOutput();
//--- MAPPERS ---//
vtkDataSetMapper *datamapper = vtkDataSetMapper::New();
datamapper->SetInput(ugrid);
vtkDataSetMapper *geomapper = vtkDataSetMapper::New();
geomapper->SetInput(ugrid);
//--- ACTORS ---//
vtkActor *dataactor = vtkActor::New();
dataactor->SetMapper(datamapper);
dataactor->GetProperty()->SetPointSize(10);
dataactor->GetProperty()->SetRepresentationToPoints();
vtkActor *geoactor = vtkActor::New();
geoactor->SetMapper(geomapper);
geoactor->GetProperty()->SetColor(0,0,0);
geoactor->GetProperty()->SetRepresentationToWireframe();
//--- PICKING ---//
VcPick *vcpick = VcPick::New();
picker->AddObserver(vtkCommand::EndPickEvent, vcpick);
//--- RENDERING ---//
vtkRenderer *ren1 = vtkRenderer::New();
ren1->AddActor(geoactor);
ren1->AddActor(dataactor);
ren1->SetBackground(1,1,1);
vtkRenderWindow *renwin = vtkRenderWindow::New();
renwin->AddRenderer(ren1);
vtkRenderWindowInteractor *iren = vtkRenderWindowInteractor::New();
iren->SetRenderWindow(renwin);
iren->SetPicker(picker);
renwin->Render();
iren->Start();
}
dr. R. van Tol
WTCM Gieterijcentrum
Technologiepark 9
B-9052 Zwijnaarde
tel.:0032 (0)9 2645704
fax:0032 (0)9 2645848
mailto:rob.vantol at wtcm.be
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.vtk.org/pipermail/vtkusers/attachments/20030911/d0b3b067/attachment.htm>
More information about the vtkusers
mailing list