vtk捡选实例

#include "vtkCellPicker.h"
#include "vtkPointPicker.h"
#include "vtkAbstractMapper3D.h"
#include "vtkAssemblyPath.h"
#include "vtkProp3D.h"
class vtkMyCellPicker : public vtkCellPicker
{
public:
 vtkMyCellPicker();
 virtual ~vtkMyCellPicker();
public:
 void InsertLine(double a[3], double b[3], double c,vtkAssemblyPath* path, vtkProp3D* DD, vtkAbstractMapper3D * cc){IntersectWithLine(a,b,c,path,DD,cc);};

};

 

#include "vtkCommand.h"
#include "vtkPolyDataReader.h"
#include "vtkProperty.h"
#include "vtkInteractorStyleTrackballCamera.h"
#include "vtkBoxWidget.h"

#include "vtkTransform.h"
#include "vtkLight.h"
#include  "vtkCamera.h"
#include "vtkDataSetMapper.h"
#include "vtkCellArray.h"
#include "vtkPlaneSource.h"
#include "vtkUnstructuredGridReader.h"
#include "vtkUnstructuredGrid.h"
#include "vtkGeometryFilter.h"
#include "vtkPolyDataNormals.h"
#include "vtkLODActor.h"
#include "vtkTextActor.h"
#include  "vtkTextProperty.h"
#include  "vtkPolyDataMapper2D.h"
#include "vtkIdFilter.h"
#include "vtkSelectVisiblePoints.h"
#include "vtkLabeledDataMapper.h"
#include "vtkCellCenters.h"
#include "vtkActor2D.h"
#include "vtkSphereSource.h"
#include "vtkIdList.h"
#include "vtkFloatArray.h"
#include "vtkIntArray.h"
#include "vtkLookupTable.h"
#include "vtkCellData.h"
#include  "vtkProperty2D.h"
#include "vtkTextProperty.h"

#include "vtkCellPicker.h"
#include "vtkAssemblyPath.h"
#include "vtkMyCellPicker.h"
//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////
extern int x11,y11;
vtkRenderer * ren;
extern CRect rect;
vtkMyCellPicker *picker;


class vtkMyCallback : public vtkCommand
{
public:
  static vtkMyCallback *New()
    { return new vtkMyCallback; }
      virtual void Execute(vtkObject *caller, unsigned long, void*)
    {
    

                vtkRenderWindowInteractor *ir = (vtkRenderWindowInteractor*)(caller);
                int x = ir->GetEventPosition()[0];
                int y = ir->GetEventPosition()[1];
  
 
                picker->Pick(x,y,0.0,ren);
                vtkAssemblyPath *path;
                path = picker->GetPath();

    double cen[3];
          (  (vtkActor*)( path->GetFirstNode()->GetViewProp() )  )->GetMapper()->GetCenter(cen);
                double pos[3];                
         ren->GetActiveCamera()->GetPosition(pos);   
            vtkMapper *mm = (  (vtkActor*)( path->GetFirstNode()->GetViewProp() ) )->GetMapper();  
    picker->InsertLine(pos,cen,0.1, path,(vtkProp3D*)( path->GetFirstNode()->GetViewProp() ),mm );
    int id = picker->GetPointId();

    CString str;
    str.Format("%d",id);
    AfxMessageBox(str);
                if (path != NULL)
                {
                  (  (vtkActor*)( path->GetFirstNode()->GetViewProp() )  )->GetProperty()->SetColor(1.0,0.0,0.0);
                }

    }

   
};


CVTK::CVTK()
{
 //在构造函数中把各个vtk对象连成通道

//////////////////////////读入数据

//在构造函数中把各个vtk对象连成通道
//////////////////////////////////
 
  //选点改变艳色,并且改变ID

        vtkSphereSource **Geometry = new vtkSphereSource*[5];
        vtkPolyDataMapper **mapper = new vtkPolyDataMapper*[5];
        vtkActor **actor = new vtkActor*[5];

        ren = vtkRenderer::New();

        renWin = vtkRenderWindow::New();

        picker = new vtkMyCellPicker();
        picker->SetTolerance(0.001);

        for (int j = 0; j < 5; j++)
        {     
              
          Geometry[j] = vtkSphereSource::New();
                   Geometry[j]->SetThetaResolution(16);
                   Geometry[j]->SetPhiResolution(8);
                   Geometry[j]->SetRadius(0.01);
                   Geometry[j]->SetCenter(j*0.1,0,0);
          
                   mapper[j] = vtkPolyDataMapper::New();
                   mapper[j]->SetInput(Geometry[j]->GetOutput());

                   actor[j] = vtkActor::New();
                   actor[j]->SetMapper(mapper[j]);

                           ren->AddActor(actor[j]);

               
        }


     vtkMyCallback *myCommand = vtkMyCallback::New();


    iren = vtkRenderWindowInteractor::New();

 vtkInteractorStyleTrackballCamera *style = vtkInteractorStyleTrackballCamera::New();
 iren->SetInteractorStyle(style);

    iren->SetRenderWindow(renWin);
   


     renWin->AddRenderer(ren);
 

    iren->AddObserver(vtkCommand::LeftButtonPressEvent,myCommand);


       
       
 


  

  
 
/////////////////////////////////


 
}

CVTK::~CVTK()
{
 renWin->Delete();
// renderer->Delete();
// iren->Delete();
// cone->Delete();
// coneMapper->Delete();
// coneActor->Delete();
}

void CVTK::BeginRenderOn(CStatic * aStatic)
{
 CRect rect;
 aStatic->GetClientRect(&rect);
 renWin->SetSize(rect.Width(),rect.Height());
// renWin->SetWindowId(aStatic->m_hWnd );
 renWin->SetParentId(aStatic->m_hWnd);   //关键代码一:设置父窗口
//    iren->Start();
 renWin->Render();                               // 关键代码二:开始绘制,启动交互器


}


 

原文地址:https://www.cnblogs.com/lizhengjin/p/1251112.html