KitwarePublic talk:Community Portal

From KitwarePublic
Revision as of 13:49, 23 November 2009 by Jacky ling (talk | contribs) (Have problem on Picking and callback function)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigationJump to search

Hello! I have a problem on Picking and callback function. I want to pick the coordinate of the object and show it, but I can't obtain the right result, what's wrong with my program? thank you!

the code is :

  1. include <vtkRenderer.h>
  2. include <vtkRenderWindow.h>
  3. include <vtkActor.h>
  4. include <vtkActor2D.h>
  5. include <vtkProperty.h>
  6. include <vtkRenderWindowInteractor.h>
  7. include <vtkPolyDataMapper.h>
  8. include <vtkCellPicker.h>
  9. include <vtkCallbackCommand.h>
  10. include <vtkCommand.h>
  11. include <vtkConeSource.h>
  12. include <vtkTextMapper.h>
  13. include <vtkActor2D.h>
  14. include <vtkTextProperty.h>
  15. include <vtkProperty2D.h>

class mycallback:public vtkCallbackCommand { public:

static mycallback*New()
{return new mycallback;}
virtual void Execute(vtkObject* caller, unsigned long eventID, void* calldata)
{
 vtkCellPicker *picker=reinterpret_cast<vtkCellPicker*>(caller);
 vtkActor2D *actor2D=reinterpret_cast<vtkActor2D*>(calldata);
 vtkTextMapper *textmapper=reinterpret_cast<vtkTextMapper*>(calldata);
 if(picker->GetCellId()<0)
 {
  actor2D->VisibilityOff();
 }
 else
 {
  char* string="";
  sprintf(string,"(%d,%d,%d)",picker->GetPickPosition()[0],picker->GetPickPosition()[1],
   picker->GetPickPosition()[2]);
  textmapper->SetInput(string);
  actor2D->SetPosition(picker->GetSelectionPoint()[0],picker->GetSelectionPoint()[1]);
  actor2D->VisibilityOn();
 }
}

}; int main() {

vtkRenderer *renderer=vtkRenderer::New();
vtkRenderWindow *renWin=vtkRenderWindow::New();
renWin->AddRenderer(renderer);
renWin->SetSize(500,400);
renWin->SetPosition(100,100);
renderer->SetBackground(1,1,1);
vtkRenderWindowInteractor *interactor=vtkRenderWindowInteractor::New();
interactor->SetRenderWindow(renWin);
vtkConeSource *cone=vtkConeSource::New();
cone->SetResolution(8);
cone->SetHeight(2);
vtkPolyDataMapper *coneMapper=vtkPolyDataMapper::New();
coneMapper->SetInput(cone->GetOutput());
vtkProperty *coneProperty=vtkProperty::New();
coneProperty->SetColor(0.9,0.3,0.5);
vtkActor *coneActor=vtkActor::New();
coneActor->SetMapper(coneMapper);
coneActor->SetProperty(coneProperty);
mycallback *callback=mycallback::New();
vtkCellPicker *picker=vtkCellPicker::New();
picker->AddObserver(vtkCommand::EndPickEvent, callback); 
vtkTextProperty*textProperty=vtkTextProperty::New();
textProperty->SetFontFamilyToArial();
textProperty->SetFontSize(20);
textProperty->BoldOn();
textProperty->ShadowOn();
vtkTextMapper *textMapper=vtkTextMapper::New();
textMapper->SetTextProperty (textProperty);
vtkActor2D *textActor=vtkActor2D::New();
textActor->VisibilityOff(); 
textActor->SetMapper(textMapper);
textActor->GetProperty()->SetColor(1,0,0);
callback->SetClientData(textActor);
interactor->SetPicker(picker); 
renderer->AddActor(coneActor);
renderer->AddActor2D(textActor);
renderer->Render();
interactor->Initialize();
interactor->Start(); 
return 0;

}