forked from Kitware/vtk-js
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
189 lines (156 loc) · 5.58 KB
/
Copy pathindex.js
File metadata and controls
189 lines (156 loc) · 5.58 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
import macro from 'vtk.js/Sources/macros';
const { vtkErrorMacro } = macro;
function notImplemented(method) {
return () => vtkErrorMacro(`vtkViewport::${method} - NOT IMPLEMENTED`);
}
// ----------------------------------------------------------------------------
// vtkViewport methods
// ----------------------------------------------------------------------------
function vtkViewport(publicAPI, model) {
// Set our className
model.classHierarchy.push('vtkViewport');
// Public API methods
publicAPI.getViewProps = () => model.props;
publicAPI.hasViewProp = (prop) => model.props.includes(prop);
publicAPI.addViewProp = (prop) => {
if (prop && !publicAPI.hasViewProp(prop)) {
model.props.push(prop);
}
};
publicAPI.removeViewProp = (prop) => {
const newPropList = model.props.filter((item) => item !== prop);
if (model.props.length !== newPropList.length) {
model.props = newPropList;
}
};
publicAPI.removeAllViewProps = () => {
model.props = [];
};
// this method get all the props including any nested props
function gatherProps(prop, allProps = []) {
allProps.push(prop);
const children = prop.getNestedProps();
if (children && children.length) {
for (let i = 0; i < children.length; i++) {
gatherProps(children[i], allProps);
}
}
return allProps;
}
publicAPI.getViewPropsWithNestedProps = () => {
let allPropsArray = [];
// Repopulate the actor2D list to minimize browsing operations.
model.actors2D = [];
for (let i = 0; i < model.props.length; i++) {
// Handle actor2D instances separately so that they can be overlayed and layered
const isActor2D = model.props[i].getActors2D();
if (isActor2D && (!Array.isArray(isActor2D) || isActor2D.length > 0)) {
model.actors2D = model.actors2D.concat(isActor2D);
} else {
gatherProps(model.props[i], allPropsArray);
}
}
// Actor2D must be rendered by layer number order
model.actors2D.sort((a, b) => a.getLayerNumber() - b.getLayerNumber());
// Finally, add the actor2D props at the end of the list
// This works because, when traversing the render pass in vtkOpenGLRenderer, the children are
// traversed in the order that they are added to the list
allPropsArray = allPropsArray.concat(model.actors2D);
return allPropsArray;
};
publicAPI.addActor2D = publicAPI.addViewProp;
publicAPI.removeActor2D = (prop) => {
// VTK way: model.actors2D.RemoveItem(prop);
publicAPI.removeViewProp(prop);
};
publicAPI.getActors2D = () => {
model.actors2D = [];
model.props.forEach((prop) => {
model.actors2D = model.actors2D.concat(prop.getActors2D());
});
return model.actors2D;
};
publicAPI.displayToView = () =>
vtkErrorMacro('call displayToView on your view instead');
publicAPI.viewToDisplay = () =>
vtkErrorMacro('callviewtodisplay on your view instead');
publicAPI.getSize = () => vtkErrorMacro('call getSize on your View instead');
publicAPI.normalizedDisplayToProjection = (x, y, z) => {
// first to normalized viewport
const nvp = publicAPI.normalizedDisplayToNormalizedViewport(x, y, z);
// then to view
return publicAPI.normalizedViewportToProjection(nvp[0], nvp[1], nvp[2]);
};
publicAPI.normalizedDisplayToNormalizedViewport = (x, y, z) => {
const scale = [
model.viewport[2] - model.viewport[0],
model.viewport[3] - model.viewport[1],
];
return [
(x - model.viewport[0]) / scale[0],
(y - model.viewport[1]) / scale[1],
z,
];
};
publicAPI.normalizedViewportToProjection = (x, y, z) => [
x * 2.0 - 1.0,
y * 2.0 - 1.0,
z * 2.0 - 1.0,
];
publicAPI.projectionToNormalizedDisplay = (x, y, z) => {
// first to nvp
const nvp = publicAPI.projectionToNormalizedViewport(x, y, z);
// then to ndp
return publicAPI.normalizedViewportToNormalizedDisplay(
nvp[0],
nvp[1],
nvp[2]
);
};
publicAPI.normalizedViewportToNormalizedDisplay = (x, y, z) => {
const scale = [
model.viewport[2] - model.viewport[0],
model.viewport[3] - model.viewport[1],
];
return [
x * scale[0] + model.viewport[0],
y * scale[1] + model.viewport[1],
z,
];
};
publicAPI.projectionToNormalizedViewport = (x, y, z) => [
(x + 1.0) * 0.5,
(y + 1.0) * 0.5,
(z + 1.0) * 0.5,
];
publicAPI.PickPropFrom = notImplemented('PickPropFrom');
}
// ----------------------------------------------------------------------------
// Object factory
// ----------------------------------------------------------------------------
const DEFAULT_VALUES = {
// _vtkWindow: null,
background: [0, 0, 0],
background2: [0.2, 0.2, 0.2],
gradientBackground: false,
viewport: [0, 0, 1, 1],
aspect: [1, 1],
pixelAspect: [1, 1],
props: [],
actors2D: [],
};
// ----------------------------------------------------------------------------
export function extend(publicAPI, model, initialValues = {}) {
Object.assign(model, DEFAULT_VALUES, initialValues);
// Build VTK API
macro.obj(publicAPI, model);
macro.event(publicAPI, model, 'event');
macro.setGet(publicAPI, model, ['gradientBackground']);
macro.setGetArray(publicAPI, model, ['viewport'], 4);
macro.setGetArray(publicAPI, model, ['background', 'background2'], 3);
vtkViewport(publicAPI, model);
}
// ----------------------------------------------------------------------------
export const newInstance = macro.newInstance(extend, 'vtkViewport');
// ----------------------------------------------------------------------------
export default { newInstance, extend };