I applied a translation matrix to the data of PolyData, but CellPicker could not pick up the translated model after translation, I want to know what position did not update to cause this problem?
You are not supposed to edit/modify your input. If you want to do it, it should happen before the binding to the mapper or within a filter of your pipeline when using getOutputPort/setInputConnection.
<template>
<div></div>
</template>
<script setup>
// import { ref, onMounted } from 'vue';
import '@kitware/vtk.js/favicon';
// Load the rendering pieces we want to use (for both WebGL and WebGPU)
import '@kitware/vtk.js/Rendering/Profiles/Geometry';
import '@kitware/vtk.js/Rendering/Profiles/Glyph';
import vtkFullScreenRenderWindow from '@kitware/vtk.js/Rendering/Misc/FullScreenRenderWindow';
import vtkSTLReader from '@kitware/vtk.js/IO/Geometry/STLReader';
import vtkActor from '@kitware/vtk.js/Rendering/Core/Actor';
import vtkMapper from '@kitware/vtk.js/Rendering/Core/Mapper';
import vtkInteractorStyleTrackballCamera from "@kitware/vtk.js/Interaction/Style/InteractorStyleTrackballCamera"
import vtkCellPicker from '@kitware/vtk.js/Rendering/Core/CellPicker';
import vtkOBBTree from '@kitware/vtk.js/Filters/General/OBBTree';
import vtkSphereSource from '@kitware/vtk.js/Filters/Sources/SphereSource';
import vtkCircleSource from '@kitware/vtk.js/Filters/Sources/CircleSource';
import vtkMatrixBuilder from '@kitware/vtk.js/Common/Core/MatrixBuilder';
import vtkOrientationMarkerWidget from '@kitware/vtk.js/Interaction/Widgets/OrientationMarkerWidget';
import vtkAxesActor from '@kitware/vtk.js/Rendering/Core/AxesActor';
import vtkMath from '@kitware/vtk.js/Common/Core/Math';
// import vtkPointPicker from '@kitware/vtk.js/Rendering/Core/PointPicker';
import vtkTransform from '@kitware/vtk.js/Common/Transform/Transform';
const fullScreenRenderer = vtkFullScreenRenderWindow.newInstance({
background: [0, 0, 0],
});
const renderer = fullScreenRenderer.getRenderer();
const renderWindow = fullScreenRenderer.getRenderWindow();
// const openGLRenderWindow = fullScreenRenderer.getApiSpecificRenderWindow();
renderer.getActiveCamera().setParallelProjection(true);
// ----------------------------------------------------------------------------
// Orientation widget setup
// ----------------------------------------------------------------------------
const axes = vtkAxesActor.newInstance({ pickable: false });
const orientationWidget = vtkOrientationMarkerWidget.newInstance({
actor: axes,
interactor: renderWindow.getInteractor(),
});
orientationWidget.setViewportCorner(
vtkOrientationMarkerWidget.Corners.BOTTOM_RIGHT
);
orientationWidget.setMinPixelSize(200);
orientationWidget.setMaxPixelSize(200);
orientationWidget.setEnabled(true)
const controlsList = [];
const picker = vtkCellPicker.newInstance();
picker.setPickFromList(1);
picker.setTolerance(0.0001);
picker.initializePickList();
const testSphere = vtkSphereSource.newInstance();
console.log("testSphere", testSphere);
testSphere.setRadius(1);
testSphere.setCenter(0, 5, 0);
const testsphereMapper = vtkMapper.newInstance();
// testsphereMapper.setInputData(testSphere.getOutputData());
testsphereMapper.setInputConnection(testSphere.getOutputPort());
const testsphereActor = vtkActor.newInstance();
testsphereActor.setMapper(testsphereMapper);
testsphereActor.getProperty().setColor(1.0, 0.0, 0.0);
picker.addPickList(testsphereActor);
renderer.addActor(testsphereActor);
renderer.resetCamera();
renderWindow.render()
///////////////////////////////////////////
let isPicking = false;
let selectedActor;
let prePoint;
renderWindow.getInteractor().onMouseMove((callData) => {
if (renderer !== callData.pokedRenderer) {
return;
}
if (isPicking) {
const pos = callData.position;
const point = [pos.x, pos.y, 0.0];
picker.pick(point, renderer);
const pickedPoint = picker.getPickPosition();
let translation = [pickedPoint[0] - prePoint[0], pickedPoint[1] - prePoint[1], pickedPoint[2] - prePoint[2]]
prePoint = pickedPoint;
vtkMatrixBuilder
.buildFromDegree()
.translate(translation[0], translation[1], translation[2])
.apply(selectedActor.getMapper().getInputData().getPoints().getData());
selectedActor.getMapper().getInputData().modified()
renderer.resetCamera();
renderWindow.render();
console.log("actor", selectedActor.getBounds())
console.log("polydata", selectedActor.getMapper().getInputData().getBounds());
}
});
renderWindow.getInteractor().onRightButtonRelease((callData) => {
if (renderer !== callData.pokedRenderer) {
return;
}
isPicking = false;
});
renderWindow.getInteractor().onRightButtonPress((callData) => {
if (renderer !== callData.pokedRenderer) {
return;
}
const pos = callData.position;
const point = [pos.x, pos.y, 0.0];
console.log(`Pick at: ${point}`);
picker.pick(point, renderer);
console.log("picker", picker);
const pickedPoint = picker.getPickPosition();
console.log("pickedPoint", pickedPoint);
prePoint = pickedPoint;
console.log("getCellId", picker.getCellId());
console.log("selectedActor", picker.getActors());
if (picker.getActors().length != 0) {
// const pickedPoints = picker.getPickedPositions();
selectedActor = picker.getActors()[0];
isPicking = true;
selectedActor.getProperty().setColor(0.0, 1.0, 0.0);
} else {
if (null != selectedActor) {
selectedActor.getProperty().setColor(1, 1, 1);
}
isPicking = false;
selectedActor = null;
}
renderWindow.render();
});
</script>
<style scoped></style>
Hello, I changed setInputData
to setInputConnection
, but I’m still having issues with picking. Could it be an issue with how I’m retrieving the PolyData while moving the model? Could you provide an example of how to do it properly?
My comment still applies. You should not do that.
vtkMatrixBuilder
.buildFromDegree()
.translate(translation[0], translation[1], translation[2])
.apply(selectedActor.getMapper().getInputData().getPoints().getData());
You must create a filter with the translation unless you do it at the actor layer.