用vtk在屏幕中选一个点,并加上标记

在土石坝剖分程序的开发过程中,由于要用可视化的形式加约束,因此需要选一个点并加上标记,但在实际开发的过程中发现

加上标记后,在旋转的大坝的过程中,标记发生闪烁的现像,原因是因为标记和坝体单元的深度计算方法的不一样,这个问题应

该可以用setresoveconindepolytopoyoffset()解决,可惜这个函数在这里却失效了,算例代码如下

     

extern int x11,y11;
extern CRect rect;


vtkRenderer * ren;
vtkRenderWindow *renWin;
vtkMyCellPicker *picker;
vtkFollower *myactor;
//vtkPointPicker *picker;
vtkActor2D *actor2d;
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 = NULL;
                path = picker->GetPath();
                if (!path)
                {
     return;
                }
    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.01, path,(vtkProp3D*)( path->GetFirstNode()->GetViewProp() ),mm );
    int id = picker->GetCellId();

    CString str;
    str.Format("%d",id);
//    AfxMessageBox(str);
                if (path != NULL)
                {
                  (  (vtkActor*)( path->GetFirstNode()->GetViewProp() )  )->GetProperty()->SetColor(1.0,0.0,0.0);
               
                }
                vtkCell *mycell = (  (vtkActor*)( path->GetFirstNode()->GetViewProp() )  )->GetMapper()->GetInput()->GetCell(id);
    vtkPoints *mypoints = mycell->GetPoints();
    int nid = mypoints->GetNumberOfPoints();
    int idnum = 0;
    double d = 0.0;
    double d_v = 1000000;
    for (int i = 0; i < nid; i++)
    {
                      double m[4];
       m[3] = 1.0;
       mypoints->GetPoint(i,m);
       ren->SetWorldPoint(m[0],m[1],m[2],m[3]);
       ren->WorldToDisplay();
       double ss[3];
       ren->GetDisplayPoint(ss);
         
                      d = (ss[0] - x) * (ss[0] - x) + (ss[1] - y) * (ss[1] - y);

       if (d < d_v)
       {
        d_v = d;
        idnum = i;
       }
     
      
    }
       double dd[4];
    dd[3] = 1.0;
    mypoints->GetPoint(idnum,dd);
    myactor->SetPosition(dd);

//    ren->SetWorldPoint(dd);
//    ren->WorldToDisplay();
//    ren->GetDisplayPoint(dd);
//    actor2d->SetDisplayPosition(dd[0], dd[1]);
 
  
       
    
       renWin->Render();
//      
                      CString str1;
          str1.Format("%d",idnum);
//          AfxMessageBox(str1);
    
//
    }

   
};


vtkMyCallback *myCommand;

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 = vtkPointPicker::New();
        picker->SetTolerance(0.001);


  vtkPoints *point = vtkPoints::New();
  point->SetNumberOfPoints(3);
  point->InsertPoint(0,0.0, 0.0, 0.0);
  point->InsertPoint(1,10.0, 0.0, 0.0);
  point->InsertPoint(2, 0.0, 10.0, 0.0);

  vtkCellArray *cell = vtkCellArray::New();
  vtkIdType num[] = {0, 1, 2};
  cell->InsertNextCell(3,num);

  vtkPolyData *data = vtkPolyData::New();
  data->SetPoints(point);
  data->SetPolys(cell);
  
  vtkPolyDataMapper *polymapper = vtkPolyDataMapper::New();
  polymapper->SetInput(data);
  vtkActor *vtkactor = vtkActor::New();
  vtkactor->SetMapper(polymapper);
  vtkactor->GetProperty()->SetColor(0.8,0.1,0.0);

  ren->AddActor(vtkactor);
  

        vtkTextMapper *textmapper = vtkTextMapper::New();
  textmapper->SetInput("#");
  actor2d = vtkActor2D::New();
  actor2d->SetMapper(textmapper);
//  actor2d->SetDisplayPosition(100, 100);
  ren->AddActor2D(actor2d);

  
  vtkVectorText *mytext = vtkVectorText::New();
  mytext->SetText("#");
  vtkPolyDataMapper *mmapper = vtkPolyDataMapper::New();
  mmapper->SetInput(mytext->GetOutput());
  mmapper->SetResolveCoincidentTopologyToPolygonOffset();
  myactor = vtkFollower::New();
  myactor->SetMapper(mmapper);
  
     myactor->GetProperty()->SetColor(0.0,1.0,0.0);
  
  ren->AddActor(myactor);

        for (int j = 0; j < 5; j++)
        {     
              
          Geometry[j] = vtkSphereSource::New();
                   Geometry[j]->SetThetaResolution(16);
                   Geometry[j]->SetPhiResolution(8);
                   Geometry[j]->SetRadius(0.005);
                   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]);
       actor[j]->VisibilityOff();

                           ren->AddActor(actor[j]);

               
        }


    myCommand = vtkMyCallback::New();


    iren = vtkRenderWindowInteractor::New();

    style = new vtkMystyle();
 vtkInteractorStyleTrackballCamera *style1 = vtkInteractorStyleTrackballCamera::New();
 iren->SetInteractorStyle(style1);
    iren->Disable();
  
    iren->SetRenderWindow(renWin);
 
  

     renWin->AddRenderer(ren);
 

 


       
       
 


  

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


 
}

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/1261741.html