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)
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 :
- include <vtkRenderer.h>
- include <vtkRenderWindow.h>
- include <vtkActor.h>
- include <vtkActor2D.h>
- include <vtkProperty.h>
- include <vtkRenderWindowInteractor.h>
- include <vtkPolyDataMapper.h>
- include <vtkCellPicker.h>
- include <vtkCallbackCommand.h>
- include <vtkCommand.h>
- include <vtkConeSource.h>
- include <vtkTextMapper.h>
- include <vtkActor2D.h>
- include <vtkTextProperty.h>
- 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;
}