After testing, it was found that updating only the vtkMapper of vtkActor itself without replacing the new vtkPolyDataMapper object after editing vtkPoints has no effect

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<vtkActor> vtkPointDisplay;
    vtkNew<vtkSphereSource> sphereSource;
    sphereSource->Update();
    vtkNew<vtkPolyDataMapper> 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<vtkPolyDataMapper> mapper;
    mapper->SetInputData(mPolyData);
    mActor->SetMapper(mapper);
    mActor->Modified();
    
    
    Interactor->Render();

}

}

After testing, it was found that updating only the vtkMapper of vtkActor itself without replacing the new vtkPolyDataMapper object after editing vtkPoints has no effect

Uploading: CustomVtkInteractorStyleTapeMeasure.cpp…

CustomVtkInteractorStyleTapeMeasure.h (1.4 KB)

CustomVtkInteractorStyleTapeMeasure.cpp (5.5 KB)


it‘s use the vtkMapper of vtkActor itself


it’s use the new vtkPolyDataMapper object

Altering a few points does not trigger Modified event in the polydata. This is not a bug but a feature: it allows making changes in bulk without triggering rendering or processing. When you are done with the updates you call Modified() at the appropriate level(s).

I’m really sorry, maybe I didn’t understand your description. I have tried using the modified () function of vtkPolyData, which is useful, but it still doesn’t work if I don’t replace the brand new vtkPolyDataMapper object. After replacing it, I can get the correct effect. If possible, could you provide specific code as a hint

//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();

When you are done modifying your points then mark the points object (mPoints) as modified, not the polydata object (mPolyData).

Thank you, I succeeded. I am truly grateful

1 Like