Skip to content

Commit b571a08

Browse files
author
Alexis Girault
committed
feat(RepresentationProxy): add sliced geometry representation
1 parent 8cf642b commit b571a08

File tree

1 file changed

+120
-0
lines changed
  • Sources/Proxy/Representations/SlicedGeometryRepresentationProxy

1 file changed

+120
-0
lines changed
Lines changed: 120 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,120 @@
1+
import macro from 'vtk.js/Sources/macro';
2+
import vtkActor from 'vtk.js/Sources/Rendering/Core/Actor';
3+
import vtkCutter from 'vtk.js/Sources/Filters/Core/Cutter';
4+
import vtkImageMapper from 'vtk.js/Sources/Rendering/Core/ImageMapper';
5+
import vtkMapper from 'vtk.js/Sources/Rendering/Core/Mapper';
6+
import vtkPlane from 'vtk.js/Sources/Common/DataModel/Plane';
7+
8+
import vtkAbstractRepresentationProxy from 'vtk.js/Sources/Proxy/Core/AbstractRepresentationProxy';
9+
10+
// ----------------------------------------------------------------------------
11+
// vtkSlicedGeometryRepresentationProxy methods
12+
// ----------------------------------------------------------------------------
13+
14+
function vtkSlicedGeometryRepresentationProxy(publicAPI, model) {
15+
// Set our className
16+
model.classHierarchy.push('vtkSlicedGeometryRepresentationProxy');
17+
18+
// Internals
19+
model.plane = vtkPlane.newInstance();
20+
model.cutter = vtkCutter.newInstance();
21+
model.cutter.setCutFunction(model.plane);
22+
model.mapper = vtkMapper.newInstance();
23+
model.actor = vtkActor.newInstance();
24+
model.property = model.actor.getProperty();
25+
model.property.setLighting(false);
26+
27+
// connect rendering pipeline
28+
model.mapper.setInputConnection(model.cutter.getOutputPort());
29+
model.actor.setMapper(model.mapper);
30+
model.actors.push(model.actor);
31+
32+
// Keep things updated
33+
model.sourceDependencies.push(model.cutter);
34+
35+
// Internal functions -------------------------------------------------------
36+
function updateSlice(slice) {
37+
model.slice = slice;
38+
const n = model.plane.getNormal();
39+
model.plane.setOrigin(n[0] * slice, n[1] * slice, n[2] * slice);
40+
model.cutter.modified();
41+
}
42+
43+
// API ----------------------------------------------------------------------
44+
publicAPI.setSlice = (slice) => {
45+
if (slice === model.slice || slice === undefined) {
46+
return;
47+
}
48+
updateSlice(slice);
49+
publicAPI.modified();
50+
};
51+
52+
publicAPI.setSlicingMode = (mode) => {
53+
if (model.slicingMode === mode || !mode) {
54+
console.log('skip setSlicingMode', mode);
55+
return;
56+
}
57+
model.slicingMode = mode;
58+
switch (vtkImageMapper.SlicingMode[mode]) {
59+
case vtkImageMapper.SlicingMode.X:
60+
model.plane.setNormal(1, 0, 0);
61+
break;
62+
case vtkImageMapper.SlicingMode.Y:
63+
model.plane.setNormal(0, 1, 0);
64+
break;
65+
case vtkImageMapper.SlicingMode.Z:
66+
model.plane.setNormal(0, 0, 1);
67+
break;
68+
default:
69+
return;
70+
}
71+
// Reslice properly along that new axis
72+
updateSlice(model.slice);
73+
74+
// Update pipeline
75+
publicAPI.modified();
76+
};
77+
}
78+
79+
// ----------------------------------------------------------------------------
80+
// Object factory
81+
// ----------------------------------------------------------------------------
82+
83+
const DEFAULT_VALUES = {
84+
slicingMode: vtkImageMapper.SlicingMode.NONE,
85+
slice: 0,
86+
};
87+
88+
// ----------------------------------------------------------------------------
89+
90+
export function extend(publicAPI, model, initialValues = {}) {
91+
Object.assign(model, DEFAULT_VALUES, initialValues);
92+
93+
// Object methods
94+
vtkAbstractRepresentationProxy.extend(publicAPI, model);
95+
macro.get(publicAPI, model, ['slicingMode', 'slice']);
96+
97+
// Object specific methods
98+
vtkSlicedGeometryRepresentationProxy(publicAPI, model);
99+
100+
// Map proxy properties
101+
macro.proxyPropertyState(publicAPI, model);
102+
macro.proxyPropertyMapping(publicAPI, model, {
103+
opacity: { modelKey: 'property', property: 'opacity' },
104+
visibility: { modelKey: 'actor', property: 'visibility' },
105+
color: { modelKey: 'property', property: 'diffuseColor' },
106+
useShadow: { modelKey: 'property', property: 'lighting' },
107+
useBounds: { modelKey: 'actor', property: 'useBounds' },
108+
});
109+
}
110+
111+
// ----------------------------------------------------------------------------
112+
113+
export const newInstance = macro.newInstance(
114+
extend,
115+
'vtkSlicedGeometryRepresentationProxy'
116+
);
117+
118+
// ----------------------------------------------------------------------------
119+
120+
export default { newInstance, extend };

0 commit comments

Comments
 (0)