[vtkusers] vtkPointPicker problems

Jacques Beaurain jacques at gsd.is.co.za
Mon Jul 31 09:10:06 EDT 2000


> Hi All,
> 
> I'm using VTK 3.1 on WinNT and a point picker to pick points on any
> visible actor 
> in a render window. I'm using a MFC dialog with a CWnd derived class
> instantiated
> in the dialog to do the rendering. If m_bPick is true (see code snippets
> below) a dialog 
> is displayed with the pick position and actor name as soon as a
> WM_LBUTTONDOWN
> message is received by the window. The pointpicker's return value seems
> dodgy. 
> Sometimes it will return "Nothing Picked!" even though the whole render
> window is 
> filled with actors, or it will return an actor which is behind the
> intended actor. The 
positions always seems to be a bit off, but I can't detect a pattern.

> Am I using the correct 2D coordinates (see
> Picker->Pick(float(LOWORD(lParam)), float(rect.Height()-HIWORD(lParam)),
> 0, Renderer)) 
> or is there something else amiss.
> 
> Regards,
> 
> Jacques Beaurain
> Quantitative Mineral Exploration
> Tel: +27 11 6383007
> 
> //********* The CVtkWnd class declaration:
> 
> class CVtkWnd : public CWnd
> {
> // Construction
> public:
> 	CVtkWnd();
> 
> // Attributes
> public:
> 
> // Operations
> public:
> 
> // Overrides
> 	// ClassWizard generated virtual function overrides
> 	//{{AFX_VIRTUAL(CVtkWnd)
> 	protected:
> 	virtual BOOL PreCreateWindow(CREATESTRUCT& cs);
> 	virtual LRESULT WindowProc(UINT message, WPARAM wParam, LPARAM
> lParam);
> 	//}}AFX_VIRTUAL
> 
> // Implementation
> public:
> 	void Init(float height);
> 	BOOL m_bPick;
> 	myActor * m_actorArray[30];
> 	vtkRenderer *Renderer;
> 	int m_iActorCount;
> 
> 	virtual ~CVtkWnd();
>     vtkWindow *GetVTKWindow() {return RenderWindow;};
>     void Render() {RenderWindow->Render();}
> 
> 	// Generated message map functions
> protected:
> 	float m_fHeightEx;
> 	CMyLookupTable *m_lut;
> 	vtkPointPicker *Picker;
>     vtkWin32OpenGLRenderWindow *RenderWindow;
> 	vtkWin32RenderWindowInteractor *Interactor;
> 
> 
> 	//{{AFX_MSG(CVtkWnd)
> 	afx_msg int OnCreate(LPCREATESTRUCT lpCreateStruct);
> 	afx_msg void OnPaint();
> 	afx_msg void OnSize(UINT nType, int cx, int cy);
> 	//}}AFX_MSG
> 	DECLARE_MESSAGE_MAP()
> };
> 
> 
> //************* Implementation
> 
> CVtkWnd::CVtkWnd()
> {
> 	int i;
> 
>     Renderer = vtkRenderer::New();
>     RenderWindow = vtkWin32OpenGLRenderWindow::New();
> 	RenderWindow->AddRenderer(Renderer);
>     Interactor = vtkWin32RenderWindowInteractor::New();
> 	Picker = vtkPointPicker::New();
> 	Picker->SetTolerance(0.01);
> 	Interactor->SetPicker(Picker);
> 
> 	for (i=0; i<30; i++) m_actorArray[i] = 0;
> 	m_lut = NULL;
> 	m_bPick = FALSE;
> }
> 
> CVtkWnd::~CVtkWnd()
> {
> 	int i;
> 
> 	if (Interactor) Interactor->Delete();
> 	if (RenderWindow) RenderWindow->Delete();
> 	if (Renderer) Renderer->Delete();
> 	if (Picker) Picker->Delete();
> 	for (i=0; i<30; i++) if (m_actorArray[i]) m_actorArray[i]->Delete();
>     if (m_lut) m_lut->Delete();
> }
> 
> 
> BEGIN_MESSAGE_MAP(CVtkWnd, CWnd)
> 	//{{AFX_MSG_MAP(CVtkWnd)
> 	ON_WM_CREATE()
> 	ON_WM_PAINT()
> 	ON_WM_SIZE()
> 	//}}AFX_MSG_MAP
> END_MESSAGE_MAP()
> 
> 
> //////////////////////////////////////////////////////////////////////////
> ///
> // CVtkWnd message handlers
> 
> int CVtkWnd::OnCreate(LPCREATESTRUCT lpCreateStruct) 
> {
> 	if (CWnd::OnCreate(lpCreateStruct) == -1)
> 		return -1;
> 	
> 	RenderWindow->SetParentId(lpCreateStruct->hwndParent);
>     RenderWindow->SetWindowId(m_hWnd);
> 	RenderWindow->WindowInitialize();
> 
> 	return 0;
> }
> 
> BOOL CVtkWnd::PreCreateWindow(CREATESTRUCT& cs) 
> {
> 	cs.style |= WS_CLIPSIBLINGS | WS_CLIPCHILDREN | CS_OWNDC;
> 	
>     return CWnd::PreCreateWindow(cs);
> }
> 
> LRESULT CVtkWnd::WindowProc(UINT message, WPARAM wParam, LPARAM lParam) 
> {
> 	switch (message)
>     {
>     //case WM_PAINT: 
>     case WM_LBUTTONDOWN:
> 			if (m_bPick) {
> 				CRect rect;
> 
> 				GetClientRect(&rect);
> 				if (!Picker->Pick(float(LOWORD(lParam)),
> float(rect.Height()-HIWORD(lParam)), 0, Renderer))
> 					AfxMessageBox("Nothing Picked!");
> 				else {
> 					int i;
> 					float *pos;
> 					CPickInfo infoDlg;
> 
> 					myActor *tmpActor;
> 					tmpActor = (myActor *)
> Picker->GetActor();
> 					
> 					if (tmpActor) infoDlg.m_strName =
> *tmpActor->m_strName;
> 					else infoDlg.m_strName = "Could not
> determine!";
> 
> 					pos = Picker->GetPickPosition();
> 					infoDlg.m_fX = pos[0];
> 					infoDlg.m_fY = pos[1];
> 					infoDlg.m_fZ = pos[2]/m_fHeightEx;
> 					
> 					infoDlg.DoModal();
> 					
> 				}
> 				break;
> 			}
>     case WM_LBUTTONUP: 
>     case WM_MBUTTONDOWN: 
>     case WM_MBUTTONUP: 
>     case WM_RBUTTONDOWN: 
>     case WM_RBUTTONUP: 
>     case WM_MOUSEMOVE:
>     case WM_CHAR:
>     case WM_TIMER:
> 			if (Interactor->GetInitialized())
> 				{
> 				return vtkHandleMessage(m_hWnd, message,
> wParam, lParam);
> 				}
> 			break;
> 	}
> 
> 	return CWnd::WindowProc(message, wParam, lParam);
> }
> 
> void CVtkWnd::OnPaint() 
> {
> 	CPaintDC dc(this); // device context for painting
> 	
>     if (!Interactor->GetInitialized())
> 	{
> 		Interactor->SetRenderWindow(RenderWindow);
> 		WNDPROC OldProc =
> (WNDPROC)GetWindowLong(m_hWnd,GWL_WNDPROC);
> 		Interactor->Initialize();
> 		SetWindowLong(m_hWnd,GWL_WNDPROC,(LONG)OldProc);
> 	}
> 	
> 
> 	Render();
> 	// Do not call CWnd::OnPaint() for painting messages
> }
> 
> void CVtkWnd::OnSize(UINT nType, int cx, int cy) 
> {
> 	CWnd::OnSize(nType, cx, cy);
> 	
>     if (Interactor->GetInitialized())
> 	{
> 		Interactor->SetSize(cx,cy);		
> 	}	
> }
> 
> 
> 




More information about the vtkusers mailing list