#include "CustomVtkInteractorStyleTapeMeasure.h" #include "vtkProperty.h" #include "vtkSphereSource.h" #include vtkStandardNewMacro(CustomVtkInteractorStyleTapeMeasure); CustomVtkInteractorStyleTapeMeasure::CustomVtkInteractorStyleTapeMeasure() { mPicked = false; mPointCount = 0; mLastPos[0] = mLastPos[1] = mLastPos[2] = 0; mActor = vtkSmartPointer::New(); mPolyLine = vtkSmartPointer::New(); mPolyData = vtkSmartPointer::New(); mPolyDataMapper = vtkSmartPointer::New(); mPoints = vtkSmartPointer::New(); mCells = vtkSmartPointer::New(); mPointsDisplays = vtkSmartPointer::New(); mActor->GetProperty()->SetColor(0,1,0); mActor->GetProperty()->SetLineWidth(2.0); mPolyDataMapper->SetInputData(mPolyData); mActor->SetMapper(mPolyDataMapper); //mPolyDataMapper->SetInputData(mPolyData); const double units0 = -66000; mPolyDataMapper->SetResolveCoincidentTopologyToPolygonOffset(); mPolyDataMapper->SetRelativeCoincidentTopologyLineOffsetParameters(0, units0); mPolyDataMapper->SetRelativeCoincidentTopologyPolygonOffsetParameters(0, units0); mPolyDataMapper->SetRelativeCoincidentTopologyPointOffsetParameter(units0); //mActor->SetMapper(mPolyDataMapper); } CustomVtkInteractorStyleTapeMeasure::~CustomVtkInteractorStyleTapeMeasure() {} void CustomVtkInteractorStyleTapeMeasure::Show() { /* mPoints->Reset(); mPointCount = 0;*/ vtkRenderer* renderer = this->Interactor->GetRenderWindow()->GetRenderers()->GetFirstRenderer(); renderer->AddActor(mActor); //mPolyLine->GetPointIds()->SetNumberOfIds(100);;// 设置点数 } void CustomVtkInteractorStyleTapeMeasure::Hide() { vtkRenderer* renderer = this->Interactor->GetRenderWindow()->GetRenderers()->GetFirstRenderer(); renderer->RemoveActor(mActor); mPointsDisplays->InitTraversal(); while (vtkActor* pointActor = mPointsDisplays->GetNextActor()) { renderer->RemoveActor(pointActor); } mPointsDisplays->RemoveAllItems(); } void CustomVtkInteractorStyleTapeMeasure::Rotate() { Superclass::Rotate(); } void CustomVtkInteractorStyleTapeMeasure::StartRotate() { Superclass::StartRotate(); vtkRenderer* renderer = this->Interactor->GetRenderWindow()->GetRenderers()->GetFirstRenderer(); this->Interactor->GetRenderWindow()->AddRenderer(renderer); int* clickPos = this->GetInteractor()->GetEventPosition(); vtkAbstractPicker* abstractPicker = this->GetInteractor()->GetPicker(); int pickCode = abstractPicker->Pick(clickPos[0], clickPos[1], 0, renderer); if (mPicked = pickCode != 0) { double picked[3]; abstractPicker->GetPickPosition(picked); vtkNew vtkPointDisplay; vtkNew sphereSource; sphereSource->Update(); vtkNew sphereMapper; sphereMapper->SetInputData(sphereSource->GetOutput()); vtkPointDisplay->SetMapper(sphereMapper); vtkPointDisplay->SetPosition(picked); vtkPointDisplay->SetScale(5,5,5); mPointsDisplays->AddItem(vtkPointDisplay); renderer->AddActor(vtkPointDisplay); mLastPos[0] = picked[0]; mLastPos[1] = picked[1]; mLastPos[2] = picked[2]; if (mPointCount == 0) { mPoints->InsertNextPoint(picked); mPointCount++; } else { mPoints->SetPoint(mPointCount-1, picked); } mPoints->InsertNextPoint(picked); mPointCount++; mPolyLine->GetPointIds()->SetNumberOfIds(mPointCount);;// 设置点数 for (size_t i = 0; i < mPointCount; i++) { mPolyLine->GetPointIds()->SetId(i, i); //double* point = mPoints->GetPoint(i); //logger::log("(%f,%f,%f)", point[0], point[1], point[2]); } mCells->InsertNextCell(mPolyLine); mCells->Modified(); mPolyData->SetPoints(mPoints); mPolyData->SetLines(mCells); mPolyData->Modified(); //mPolyDataMapper->SetInputData(mPolyData); It didn't have any effect //mPolyDataMapper->Update(); //mPolyDataMapper->Modified(); //mActor->SetMapper(mPolyDataMapper); vtkNew mapper; mapper->SetInputData(mPolyData); mActor->SetMapper(mapper); mActor->Modified(); Interactor->Render(); } } void CustomVtkInteractorStyleTapeMeasure::EndRotate() { Superclass::EndRotate(); } void CustomVtkInteractorStyleTapeMeasure::OnMouseMove() { Superclass::OnMouseMove(); int* clickPos = this->GetInteractor()->GetEventPosition(); int x = clickPos[0]; int y = clickPos[1]; vtkRenderer* renderer = this->Interactor->GetRenderWindow()->GetRenderers()->GetFirstRenderer(); double worldPoint[4]; renderer->SetDisplayPoint(x, y, 0); renderer->DisplayToWorld(); renderer->GetWorldPoint(worldPoint); if (mPicked) { mPoints->SetPoint(mPointCount - 1, worldPoint[0], worldPoint[1], worldPoint[2]); /*vtkNew mapper; mapper->SetInputData(mPolyData); mActor->SetMapper(mapper);*/ mPolyData->Modified(); mActor->GetMapper()->Update(); Interactor->Render(); } }