Skip to content

Commit 8224664

Browse files
authored
Merge pull request #2115 from tomsuchel/small-widgets-fixes
fix(widgets): fix paintWidget example and small bugs
2 parents 1fe0925 + 48a936d commit 8224664

File tree

37 files changed

+367
-220
lines changed

37 files changed

+367
-220
lines changed

Sources/Filters/Texture/TextureMapToPlane/index.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -284,9 +284,10 @@ export function extend(publicAPI, model, initialValues = {}) {
284284
macro.setGetArray(
285285
publicAPI,
286286
model,
287-
['origin', 'point1', 'point2', 'normal', 'sRange', 'tRange'],
287+
['origin', 'point1', 'point2', 'normal'],
288288
3
289289
);
290+
macro.setGetArray(publicAPI, model, ['sRange', 'tRange'], 2);
290291
macro.setGet(publicAPI, model, ['automaticPlaneGeneration']);
291292

292293
macro.algo(publicAPI, model, 1, 1);

Sources/Filters/Texture/TextureMapToSphere/index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,7 @@ export function extend(publicAPI, model, initialValues = {}) {
145145
// Build VTK API
146146
macro.obj(publicAPI, model);
147147

148-
macro.setGetArray(publicAPI, model, ['center']);
148+
macro.setGetArray(publicAPI, model, ['center'], 3);
149149
macro.setGet(publicAPI, model, ['automaticSphereGeneration', 'preventSeam']);
150150

151151
macro.algo(publicAPI, model, 1, 1);

Sources/Rendering/Core/Renderer/index.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,6 @@ function vtkRenderer(publicAPI, model) {
2121
// Set our className
2222
model.classHierarchy.push('vtkRenderer');
2323

24-
// make sure background has 4 entries. Default to opaque black
25-
if (!model.background) model.background = [0, 0, 0, 1];
26-
while (model.background.length < 3) model.background.push(0);
27-
if (model.background.length === 3) model.background.push(1);
28-
2924
// Events
3025
const COMPUTE_VISIBLE_PROP_BOUNDS_EVENT = {
3126
type: 'ComputeVisiblePropBoundsEvent',
@@ -630,6 +625,11 @@ export function extend(publicAPI, model, initialValues = {}) {
630625
// Inheritance
631626
vtkViewport.extend(publicAPI, model, initialValues);
632627

628+
// make sure background has 4 entries. Default to opaque black
629+
if (!model.background) model.background = [0, 0, 0, 1];
630+
while (model.background.length < 3) model.background.push(0);
631+
if (model.background.length === 3) model.background.push(1);
632+
633633
// Build VTK API
634634
macro.get(publicAPI, model, [
635635
'renderWindow',

Sources/Testing/testMacro.js

Lines changed: 56 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,8 @@ const DEFAULT_VALUES = {
3030
myProp6: [0.1, 0.2, 0.3, 0.4, 0.5],
3131
myProp7: MY_ENUM.FIRST,
3232
myProp8: [1, 2, 3],
33+
myProp9: null,
34+
// myProp10: null,
3335
};
3436

3537
// ----------------------------------------------------------------------------
@@ -58,6 +60,12 @@ function extend(publicAPI, model, initialValues = {}) {
5860
// setArray macros with default value
5961
macro.setGetArray(publicAPI, model, ['myProp8'], 3, 0);
6062

63+
// setArray macros with no initial value
64+
macro.setGetArray(publicAPI, model, ['myProp9'], 3);
65+
66+
// setArray macros with no size
67+
macro.setGetArray(publicAPI, model, ['myProp10']);
68+
6169
// Object specific methods
6270
myClass(publicAPI, model);
6371

@@ -180,12 +188,24 @@ test('Macro methods array tests', (t) => {
180188

181189
// Test default values
182190
t.ok(myTestClass.setMyProp8(), 'OK to set no argument');
183-
t.ok(myTestClass.setMyProp8(1), 'OK to set not enough argument');
184-
t.ok(myTestClass.setMyProp8([2, 3]), 'OK to set too-short array argument');
185-
t.ok(
186-
myTestClass.setMyProp8(new Float64Array(2)),
191+
t.equal(
192+
myTestClass.setMyProp8([]),
193+
false,
194+
'OK to set same empty array as argument'
195+
);
196+
t.equal(
197+
myTestClass.setMyProp8(new Uint8Array()),
198+
false,
199+
'OK to set same empty typedarray as argument'
200+
);
201+
t.ok(myTestClass.setMyProp8(10), 'OK to set not enough argument');
202+
t.equal(
203+
myTestClass.setMyProp8(new Float64Array([10])),
204+
false,
187205
'OK to set too short typed array argument'
188206
);
207+
t.ok(myTestClass.setMyProp8([2, 3]), 'OK to set too-short array argument');
208+
189209
t.throws(
190210
() => myTestClass.setMyProp8(1, 2, 3, 4),
191211
/RangeError/,
@@ -202,6 +222,38 @@ test('Macro methods array tests', (t) => {
202222
'Too large array should throw'
203223
);
204224

225+
t.throws(
226+
() => newInstance({ myProp9: [] }),
227+
/RangeError/,
228+
'Empty array should throw'
229+
);
230+
231+
t.equal(myTestClass.setMyProp9(null), false);
232+
t.equal(myTestClass.setMyProp9([0, 1, 2]), true);
233+
t.throws(
234+
() => myTestClass.setMyProp9(),
235+
/RangeError/,
236+
'Empty array should throw'
237+
);
238+
t.throws(
239+
() => myTestClass.setMyProp9([]),
240+
/RangeError/,
241+
'Empty array should throw'
242+
);
243+
244+
t.ok(
245+
myTestClass.setMyProp10([0, 1, 2]),
246+
'Test setting array from undefined to unlimited size'
247+
);
248+
t.ok(
249+
myTestClass.setMyProp10([0, 1, 2, 3]),
250+
'Test setting larger array for unlimited size array'
251+
);
252+
t.ok(
253+
myTestClass.setMyProp10([0, 1, 2]),
254+
'Test setting smaller array for unlimited size array'
255+
);
256+
205257
t.end();
206258
});
207259

Sources/Widgets/Core/StateBuilder/originMixin.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ function vtkOriginMixin(publicAPI, model) {
1212
// ----------------------------------------------------------------------------
1313

1414
const DEFAULT_VALUES = {
15-
origin: [0, 0, 0],
15+
origin: null,
1616
};
1717

1818
// ----------------------------------------------------------------------------

Sources/Widgets/Representations/ArrowHandleRepresentation/index.js

Lines changed: 29 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -253,7 +253,15 @@ function vtkArrowHandleRepresentation(publicAPI, model) {
253253

254254
publicAPI.requestDataInternal = (inData, outData) => {
255255
const { points, scale, color, direction } = model.internalArrays;
256-
const list = publicAPI.getRepresentationStates(inData[0]);
256+
const list = publicAPI
257+
.getRepresentationStates(inData[0])
258+
.filter(
259+
(state) =>
260+
state.getOrigin &&
261+
state.getOrigin() &&
262+
state.isVisible &&
263+
state.isVisible()
264+
);
257265
const totalCount = list.length;
258266

259267
if (color.getNumberOfValues() !== totalCount) {
@@ -271,32 +279,36 @@ function vtkArrowHandleRepresentation(publicAPI, model) {
271279
direction: direction.getData(),
272280
};
273281

274-
for (let i = 0; i < list.length; i++) {
282+
for (let i = 0; i < totalCount; i++) {
275283
const state = list[i];
276284
const isActive = state.getActive();
277285
const scaleFactor = isActive ? model.activeScaleFactor : 1;
278286

279287
const coord = state.getOrigin();
280-
typedArray.points[i * 3 + 0] = coord[0];
281-
typedArray.points[i * 3 + 1] = coord[1];
282-
typedArray.points[i * 3 + 2] = coord[2];
288+
if (coord) {
289+
typedArray.points[i * 3 + 0] = coord[0];
290+
typedArray.points[i * 3 + 1] = coord[1];
291+
typedArray.points[i * 3 + 2] = coord[2];
283292

284-
let scale3 = state.getScale3 ? state.getScale3() : [1, 1, 1];
285-
scale3 = scale3.map((x) => (x === 0 ? 2 * model.defaultScale : 2 * x));
293+
let scale3 = state.getScale3 ? state.getScale3() : [1, 1, 1];
294+
scale3 = scale3.map((x) => (x === 0 ? 2 * model.defaultScale : 2 * x));
286295

287-
const rotation = getGlyphRotation(scale3);
296+
const rotation = getGlyphRotation(scale3);
288297

289-
typedArray.direction.set(rotation, 9 * i);
290-
typedArray.scale[i] =
291-
scaleFactor *
292-
(state.getScale1 ? state.getScale1() : model.defaultScale);
298+
typedArray.direction.set(rotation, 9 * i);
299+
typedArray.scale[i] =
300+
scaleFactor *
301+
(state.getScale1 ? state.getScale1() : model.defaultScale);
293302

294-
if (publicAPI.getScaleInPixels()) {
295-
typedArray.scale[i] *= publicAPI.getPixelWorldHeightAtCoord(coord);
296-
}
303+
if (publicAPI.getScaleInPixels()) {
304+
typedArray.scale[i] *= publicAPI.getPixelWorldHeightAtCoord(coord);
305+
}
297306

298-
typedArray.color[i] =
299-
model.useActiveColor && isActive ? model.activeColor : state.getColor();
307+
typedArray.color[i] =
308+
model.useActiveColor && isActive
309+
? model.activeColor
310+
: state.getColor();
311+
}
300312
}
301313

302314
model.internalPolyData.modified();

Sources/Widgets/Representations/CircleContextRepresentation/index.js

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,15 @@ function vtkCircleContextRepresentation(publicAPI, model) {
115115

116116
publicAPI.requestData = (inData, outData) => {
117117
const { points, scale, color, direction } = model.internalArrays;
118-
const list = publicAPI.getRepresentationStates(inData[0]);
118+
const list = publicAPI
119+
.getRepresentationStates(inData[0])
120+
.filter(
121+
(state) =>
122+
state.getOrigin &&
123+
state.getOrigin() &&
124+
state.isVisible &&
125+
state.isVisible()
126+
);
119127
const totalCount = list.length;
120128

121129
if (color.getNumberOfValues() !== totalCount) {
@@ -132,7 +140,7 @@ function vtkCircleContextRepresentation(publicAPI, model) {
132140
direction: direction.getData(),
133141
};
134142

135-
for (let i = 0; i < list.length; i++) {
143+
for (let i = 0; i < totalCount; i++) {
136144
const state = list[i];
137145
const isActive = state.getActive();
138146
const scaleFactor = isActive ? model.activeScaleFactor : 1;
@@ -167,14 +175,9 @@ function vtkCircleContextRepresentation(publicAPI, model) {
167175
const scale1 =
168176
(state.getScale1 ? state.getScale1() : model.defaultScale) / 2;
169177

170-
let sFactor = scaleFactor;
171-
if (state.getVisible && !state.getVisible()) {
172-
sFactor = 0;
173-
}
174-
175-
typedArray.scale[i * 3 + 0] = scale1 * sFactor * scale3[0];
176-
typedArray.scale[i * 3 + 1] = scale1 * sFactor * scale3[1];
177-
typedArray.scale[i * 3 + 2] = scale1 * sFactor * scale3[2];
178+
typedArray.scale[i * 3 + 0] = scale1 * scaleFactor * scale3[0];
179+
typedArray.scale[i * 3 + 1] = scale1 * scaleFactor * scale3[1];
180+
typedArray.scale[i * 3 + 2] = scale1 * scaleFactor * scale3[2];
178181

179182
typedArray.color[i] =
180183
model.useActiveColor && isActive ? model.activeColor : state.getColor();

Sources/Widgets/Representations/ConvexFaceContextRepresentation/index.js

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -58,11 +58,12 @@ function vtkConvexFaceContextRepresentation(publicAPI, model) {
5858

5959
publicAPI.requestData = (inData, outData) => {
6060
const list = publicAPI.getRepresentationStates(inData[0]);
61+
const validState = list.filter((state) => state.getOrigin());
6162

62-
const points = allocateSize(list.length);
63+
const points = allocateSize(validState.length);
6364

64-
for (let i = 0; i < list.length; i++) {
65-
const coords = list[i].getOrigin();
65+
for (let i = 0; i < validState.length; i++) {
66+
const coords = validState[i].getOrigin();
6667
points[i * 3] = coords[0];
6768
points[i * 3 + 1] = coords[1];
6869
points[i * 3 + 2] = coords[2];

Sources/Widgets/Representations/CroppingOutlineRepresentation/index.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,9 @@ function vtkCroppingOutlineRepresentation(publicAPI, model) {
5959
// --------------------------------------------------------------------------
6060

6161
publicAPI.requestData = (inData, outData) => {
62-
const list = publicAPI.getRepresentationStates(inData[0]);
62+
const list = publicAPI
63+
.getRepresentationStates(inData[0])
64+
.filter((state) => state.getOrigin && state.getOrigin());
6365
if (list.length === 8) {
6466
let pi = 0;
6567
for (let i = 0; i < list.length; i++) {

Sources/Widgets/Representations/CubeHandleRepresentation/index.js

Lines changed: 27 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,15 @@ function vtkCubeHandleRepresentation(publicAPI, model) {
5959

6060
publicAPI.requestData = (inData, outData) => {
6161
const { points, scale, color } = model.internalArrays;
62-
const list = publicAPI.getRepresentationStates(inData[0]);
62+
const list = publicAPI
63+
.getRepresentationStates(inData[0])
64+
.filter(
65+
(state) =>
66+
state.getOrigin &&
67+
state.getOrigin() &&
68+
state.isVisible &&
69+
state.isVisible()
70+
);
6371
const totalCount = list.length;
6472

6573
if (color.getNumberOfValues() !== totalCount) {
@@ -74,27 +82,30 @@ function vtkCubeHandleRepresentation(publicAPI, model) {
7482
color: color.getData(),
7583
};
7684

77-
for (let i = 0; i < list.length; i++) {
85+
for (let i = 0; i < totalCount; i++) {
7886
const state = list[i];
7987
const isActive = state.getActive();
8088
const scaleFactor = isActive ? model.activeScaleFactor : 1;
8189

8290
const coord = state.getOrigin();
83-
typedArray.points[i * 3 + 0] = coord[0];
84-
typedArray.points[i * 3 + 1] = coord[1];
85-
typedArray.points[i * 3 + 2] = coord[2];
86-
87-
typedArray.scale[i] =
88-
scaleFactor *
89-
(!state.isVisible || state.isVisible() ? 1 : 0) *
90-
(state.getScale1 ? state.getScale1() : model.defaultScale);
91-
92-
if (publicAPI.getScaleInPixels()) {
93-
typedArray.scale[i] *= publicAPI.getPixelWorldHeightAtCoord(coord);
91+
if (coord) {
92+
typedArray.points[i * 3 + 0] = coord[0];
93+
typedArray.points[i * 3 + 1] = coord[1];
94+
typedArray.points[i * 3 + 2] = coord[2];
95+
96+
typedArray.scale[i] =
97+
scaleFactor *
98+
(state.getScale1 ? state.getScale1() : model.defaultScale);
99+
100+
if (publicAPI.getScaleInPixels()) {
101+
typedArray.scale[i] *= publicAPI.getPixelWorldHeightAtCoord(coord);
102+
}
103+
104+
typedArray.color[i] =
105+
model.useActiveColor && isActive
106+
? model.activeColor
107+
: state.getColor();
94108
}
95-
96-
typedArray.color[i] =
97-
model.useActiveColor && isActive ? model.activeColor : state.getColor();
98109
}
99110

100111
model.internalPolyData.modified();

0 commit comments

Comments
 (0)