[vtkusers] Get point coordinates by mouse click
付文宇
fuwenyu at emails.bjut.edu.cn
Sat May 26 10:05:49 EDT 2007
Hi everyone:
vtk has given an example written by tcl, which can get point coordinates by
clicking leftmousebutton directly. I converted tcl codes into C++. But C++
programme converted by myself only can get point coordinates by preesing 'p' key.
I guess the problem lie in callback function. I converted following tcl codes
"picker AddObserver EndPickEvent annotatePick"
"style AddObserver LeftButtonPressEvent {set MouseMotion 0; style
OnLeftButtonDown}"
"style AddObserver MouseMoveEvent {set MouseMotion 1; style OnMouseMove}"
"style AddObserver LeftButtonReleaseEvent cbLBR" into C++ codes:
class vtkMyCallback : public vtkCommand
{
public:
static vtkMyCallback *New()
{ return new vtkMyCallback; }
virtual void Execute(vtkObject *caller, unsigned long, void*)
{
if(picker->GetCellId()<0)
{
textActor->VisibilityOff();
}
else
{
double *coordinates=new double[3];
coordinates=picker->GetSelectionPoint();
double x=coordinates[0];
double y=coordinates[1];
double *data=new double[3];
data=picker->GetPickPosition();
double xp=data[0];
double yp=data[1];
double zp=data[2];
textMapper->SetInput("xp+yp+zp");
textActor->SetPosition(x, y);
textActor->VisibilityOn();
}
renWin->Render();
}
};
class vtkMyCallback1 : public vtkCommand
{
public:
static vtkMyCallback1 *New()
{ return new vtkMyCallback1; }
virtual void Execute(vtkObject *caller, unsigned long, void*)
{
MouseMotion=0;
style->OnLeftButtonDown();
}
};
class vtkMyCallback2 : public vtkCommand
{
public:
static vtkMyCallback2 *New()
{ return new vtkMyCallback2; }
virtual void Execute(vtkObject *caller, unsigned long, void*)
{
MouseMotion=1;
style->OnMouseMove();
}
};
class cbLBR : public vtkCommand
{
public:
static cbLBR *New()
{ return new cbLBR; }
virtual void Execute(vtkObject *caller, unsigned long, void*)
{
if(MouseMotion==0)
{
double *xycoordinate=new double[2];
xycoordinate=(double*)iren->GetEventPosition();
double x=xycoordinate[0];
double y=xycoordinate[1];
picker->Pick(x,y,0,ren1);
}
//#Do(the, default, things, for, the, trackball, style);
style->OnLeftButtonUp();
}
};
int main()
{
.....
vtkMyCallback *annotatePick = vtkMyCallback::New();
picker->AddObserver(vtkCommand::EndPickEvent, annotatePick);
.....
// Create the Renderer, RenderWindow, and RenderWindowInteractor
style = vtkInteractorStyleTrackballCamera::New();
vtkMyCallback1 *LeftButPre = vtkMyCallback1::New();
style->AddObserver(vtkCommand::LeftButtonPressEvent, LeftButPre);
vtkMyCallback2 *mousemove = vtkMyCallback2::New();
style->AddObserver(vtkCommand::MouseMoveEvent,mousemove);
cbLBR *cblbr=cbLBR::New();
style->AddObserver(vtkCommand::LeftButtonReleaseEvent,cblbr);
ren1 = vtkRenderer::New();
renWin = vtkRenderWindow::New();
renWin->AddRenderer(ren1);
iren = vtkRenderWindowInteractor::New();
iren->SetRenderWindow(renWin);
iren->SetInteractorStyle(style);
iren->SetPicker(picker);
.....
}
But C++ programme converted by myself only can get point coordinates by pressing
'p' key. It cannot get point coordinates by clicking leftmousebutton
directly.
The following are original TCL programme.
---------------------------------------------------------
package require vtk
package require vtkinteraction
vtkSphereSource sphere
vtkPolyDataMapper sphereMapper
sphereMapper SetInput [sphere GetOutput]
sphereMapper GlobalImmediateModeRenderingOn
vtkLODActor sphereActor
sphereActor SetMapper sphereMapper
vtkConeSource cone
vtkGlyph3D glyph
glyph SetInput [sphere GetOutput]
glyph SetSource [cone GetOutput]
glyph SetVectorModeToUseNormal
glyph SetScaleModeToScaleByVector
glyph SetScaleFactor 0.25
vtkPolyDataMapper spikeMapper
spikeMapper SetInput [glyph GetOutput]
vtkLODActor spikeActor
spikeActor SetMapper spikeMapper
vtkCellPicker picker
picker AddObserver EndPickEvent annotatePick
vtkTextMapper textMapper
set tprop [textMapper GetTextProperty]
$tprop SetFontFamilyToArial
$tprop SetFontSize 10
$tprop BoldOn
$tprop ShadowOn
$tprop SetColor 1 0 0
vtkActor2D textActor
textActor VisibilityOff
textActor SetMapper textMapper
vtkInteractorStyleTrackballCamera style
style AddObserver LeftButtonPressEvent {set MouseMotion 0; style
OnLeftButtonDown}
style AddObserver MouseMoveEvent {set MouseMotion 1; style OnMouseMove}
style AddObserver LeftButtonReleaseEvent cbLBR
vtkRenderer ren1
vtkRenderWindow renWin
renWin AddRenderer ren1
vtkRenderWindowInteractor iren
iren SetRenderWindow renWin
iren SetInteractorStyle style
iren SetPicker picker
ren1 AddActor2D textActor
ren1 AddActor sphereActor
ren1 AddActor spikeActor
ren1 SetBackground 1 1 1
renWin SetSize 300 300
set cam1 [ren1 GetActiveCamera]
$cam1 Zoom 1.4
iren AddObserver UserEvent {wm deiconify .vtkInteract}
iren Initialize
wm withdraw .
proc annotatePick {} {
if { [picker GetCellId] < 0 } {
textActor VisibilityOff
} else {
set selPt [picker GetSelectionPoint]
set x [lindex $selPt 0]
set y [lindex $selPt 1]
set pickPos [picker GetPickPosition]
set xp [lindex $pickPos 0]
set yp [lindex $pickPos 1]
set zp [lindex $pickPos 2]
textMapper SetInput "($xp, $yp, $zp)"
textActor SetPosition $x $y
textActor VisibilityOn
}
renWin Render
}
proc cbLBR {} {
global MouseMotion
if {$MouseMotion == 0} {
eval picker Pick [iren GetEventPosition] 0 ren1
}
# Do the default things for the trackball style
style OnLeftButtonUp
}
picker Pick 85 126 0 ren1
--------------------------------------------------------------------------
Could someone give me some suggestions?
Thanks in advance.
Fuwenyu
2007.05.26
More information about the vtkusers
mailing list