KitwarePublic talk:Community Portal: Difference between revisions

From KitwarePublic
Jump to navigationJump to search
No edit summary
(Have problem on Picking and callback function)
 
Line 1: Line 1:


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;
}

Latest revision as of 13:49, 23 November 2009

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;

}