I want to update the input points,and update the textActor defined by vtkPixelSpaceCallbackMapper, but the code follow I tried do not work, I don’t know how to achieve it, I’d appreciate it if anyone could help.
const renderWindow = vtkRenderWindow.newInstance();
const renderer = vtkRenderer.newInstance({ background: [0.2, 0.3, 0.4] });
renderWindow.addRenderer(renderer);
let textCtx = null;
let dims = null;
const psMapper = vtkPixelSpaceCallbackMapper.newInstance();
const points = vtkPoints.newInstance();
points.insertNextPoint(0, 0, 0);
points.insertNextPoint(1, 0, 0);
points.insertNextPoint(1, 1, 0);
points.insertNextPoint(0, 1, 0);
const pointSet = vtkPointSet.newInstance();
pointSet.setPoints(points);
psMapper.setInputData(pointSet);
psMapper.setCallback((coordsList) => {
console.log(coordsList);
if (textCtx && dims) {
textCtx.clearRect(0, 0, dims.width, dims.height);
coordsList.forEach((xy, idx) => {
textCtx.font = "12px serif";
textCtx.textAlign = "center";
textCtx.textBaseline = "middle";
textCtx.fillText(`p ${idx}`, xy[0], dims.height - xy[1]);
});
}
});
const textActor = vtkActor.newInstance();
textActor.setMapper(psMapper);
renderer.addActor(textActor);
renderer.resetCamera();
// ----------------------------------------------------------------------------
// Use OpenGL as the backend to view the all this
// ----------------------------------------------------------------------------
const openGLRenderWindow = vtkOpenGLRenderWindow.newInstance();
renderWindow.addView(openGLRenderWindow);
// ----------------------------------------------------------------------------
// Create a div section to put this into
// ----------------------------------------------------------------------------
openGLRenderWindow.setContainer(containerRef.current);
const textCanvas = document.createElement("canvas");
textCanvas.classList.add(style.container, "textCanvas");
containerRef.current.appendChild(textCanvas);
textCtx = textCanvas.getContext("2d");
const interactor = vtkRenderWindowInteractor.newInstance();
interactor.setView(openGLRenderWindow);
interactor.initialize();
interactor.bindEvents(containerRef.current);
interactor.setInteractorStyle(
vtkInteractorStyleTrackballCamera.newInstance()
);
// Handle window resize
function resize() {
dims = containerRef.current.getBoundingClientRect();
openGLRenderWindow.setSize(dims.width, dims.height);
textCanvas.setAttribute("width", dims.width);
textCanvas.setAttribute("height", dims.height);
renderWindow.render();
}
window.addEventListener("resize", resize);
resize();
renderer.resetCamera();
renderWindow.render();
// Add the label widget to the renderer
//renderer.addActor(labelWidget.getActor());
context.current = {
renderWindow,
renderer,
};
setTimeout(() => {
// Retrieve the original input data
const originalPointSet = psMapper.getInputData();
// Create a new vtkPolyData object to store the modified data
const modifiedPointSet = vtkPointSet.newInstance();
modifiedPointSet.shallowCopy(originalPointSet);
// Modify the points' coordinates in the new vtkPolyData
const modifiedPoints = modifiedPointSet.getPoints();
const numPoints = modifiedPoints.getNumberOfPoints();
for (let i = 0; i < numPoints; i++) {
const point = modifiedPoints.getPoint(i);
// Modify the point coordinates here
point[0] += 2; // Example: increase x-coordinate by 10
point[1] += 0; // Example: increase y-coordinate by 20
point[2] += 0; // Example: increase z-coordinate by 5
modifiedPoints.setPoint(i, point); // Update the modified point
}
// Set the modified vtkPolyData as the new input for the mapper
psMapper.setInputData(modifiedPointSet);
// Update the mapper and render the scene
psMapper.update();
renderWindow.render();
}, 3000);