Skip to content

Commit e887c9d

Browse files
committed
fix(Picking): remove onSelect and split pick func
1 parent 02f6d7b commit e887c9d

File tree

1 file changed

+138
-127
lines changed

1 file changed

+138
-127
lines changed

src/core-ts/Picking.ts

Lines changed: 138 additions & 127 deletions
Original file line numberDiff line numberDiff line change
@@ -68,14 +68,6 @@ interface Props {
6868
*/
6969
onHover?: (selection: PickResult, event: PointerEvent) => void;
7070

71-
/**
72-
* Callback when a box select occurs.
73-
*
74-
* @param selection Selection info
75-
* @param event the originating pointer event
76-
*/
77-
onSelect?: (selection: FrustumPickResult, event: PointerEvent) => void;
78-
7971
/**
8072
* Callback when an actor is clicked with a mouse.
8173
*
@@ -157,7 +149,7 @@ export default forwardRef(function ViewPicking(props: Props, fwdRef) {
157149
// --- API --- //
158150

159151
const pickClosest = useCallback(
160-
(xp: number, yp: number, tolerance: number) => {
152+
(xp: number, yp: number, tolerance: number): PickResult[] => {
161153
const x1 = Math.floor(xp - tolerance);
162154
const y1 = Math.floor(yp - tolerance);
163155
const x2 = Math.ceil(xp + tolerance);
@@ -169,158 +161,177 @@ export default forwardRef(function ViewPicking(props: Props, fwdRef) {
169161

170162
selector.setArea(x1, y1, x2, y2);
171163

172-
if (selector.captureBuffers()) {
173-
const pos: Vector2 = [xp, yp];
174-
const outSelectedPosition: Vector2 = [0, 0];
175-
const info = selector.getPixelInformation(
176-
pos,
177-
tolerance,
178-
outSelectedPosition
179-
);
180-
181-
if (info == null || info.prop == null) return [];
182-
183-
const startPoint = openGLRenderWindow.displayToWorld(
184-
Math.round((x1 + x2) / 2),
185-
Math.round((y1 + y2) / 2),
186-
0,
187-
renderer
188-
);
164+
if (!selector.captureBuffers()) {
165+
return [];
166+
}
189167

190-
const endPoint = openGLRenderWindow.displayToWorld(
191-
Math.round((x1 + x2) / 2),
192-
Math.round((y1 + y2) / 2),
193-
1,
168+
const pos: Vector2 = [xp, yp];
169+
const outSelectedPosition: Vector2 = [0, 0];
170+
const info = selector.getPixelInformation(
171+
pos,
172+
tolerance,
173+
outSelectedPosition
174+
);
175+
176+
if (info == null || info.prop == null) return [];
177+
178+
const startPoint = openGLRenderWindow.displayToWorld(
179+
Math.round((x1 + x2) / 2),
180+
Math.round((y1 + y2) / 2),
181+
0,
182+
renderer
183+
);
184+
185+
const endPoint = openGLRenderWindow.displayToWorld(
186+
Math.round((x1 + x2) / 2),
187+
Math.round((y1 + y2) / 2),
188+
1,
189+
renderer
190+
);
191+
192+
const ray = [Array.from(startPoint), Array.from(endPoint)];
193+
194+
const worldPosition = Array.from(
195+
openGLRenderWindow.displayToWorld(
196+
info.displayPosition[0],
197+
info.displayPosition[1],
198+
info.zValue,
194199
renderer
195-
);
200+
)
201+
);
202+
203+
const displayPosition = [
204+
info.displayPosition[0],
205+
info.displayPosition[1],
206+
info.zValue,
207+
];
208+
209+
return [
210+
{
211+
worldPosition,
212+
displayPosition,
213+
compositeID: info.compositeID,
214+
...info.prop.get('representationID'),
215+
ray,
216+
},
217+
] as PickResult[];
218+
},
219+
[rendererAPI, openGLRenderWindowAPI, getSelector]
220+
);
221+
222+
const pick = useCallback(
223+
(x1: number, y1: number, x2: number, y2: number): PickResult[] => {
224+
const selector = getSelector();
225+
const openGLRenderWindow = openGLRenderWindowAPI.get();
226+
const renderer = rendererAPI.get();
196227

197-
const ray = [Array.from(startPoint), Array.from(endPoint)];
228+
selector.setArea(x1, y1, x2, y2);
229+
230+
if (!selector.captureBuffers()) {
231+
return [];
232+
}
198233

199-
const worldPosition = Array.from(
234+
const ray = [
235+
Array.from(
200236
openGLRenderWindow.displayToWorld(
201-
info.displayPosition[0],
202-
info.displayPosition[1],
203-
info.zValue,
237+
Math.round((x1 + x2) / 2),
238+
Math.round((y1 + y2) / 2),
239+
0,
204240
renderer
205241
)
206-
);
207-
208-
const displayPosition = [
209-
info.displayPosition[0],
210-
info.displayPosition[1],
211-
info.zValue,
212-
];
213-
214-
return [
215-
{
216-
worldPosition,
242+
),
243+
Array.from(
244+
openGLRenderWindow.displayToWorld(
245+
Math.round((x1 + x2) / 2),
246+
Math.round((y1 + y2) / 2),
247+
1,
248+
renderer
249+
)
250+
),
251+
];
252+
253+
const selections = selector.generateSelection(x1, y1, x2, y2) || [];
254+
return selections
255+
.map((v) => {
256+
const { prop, compositeID, displayPosition } = v.getProperties();
257+
258+
// Return false to mark this item for removal
259+
if (prop == null || !displayPosition) return false;
260+
261+
return {
262+
worldPosition: Array.from(
263+
openGLRenderWindow.displayToWorld(
264+
displayPosition[0],
265+
displayPosition[1],
266+
displayPosition[2],
267+
renderer
268+
)
269+
),
217270
displayPosition,
218-
compositeID: info.compositeID,
219-
...info.prop.get('representationID'),
271+
compositeID, // Not yet useful unless GlyphRepresentation
272+
...prop.get('representationID'),
220273
ray,
221-
},
222-
] as PickResult[];
223-
}
224-
return [];
274+
};
275+
})
276+
.filter(Boolean) as PickResult[];
225277
},
226278
[rendererAPI, openGLRenderWindowAPI, getSelector]
227279
);
228280

229-
const pick = useCallback(
281+
const pickWithFrustum = useCallback(
230282
(
231283
x1: number,
232284
y1: number,
233285
x2: number,
234-
y2: number,
235-
useFrustrum = false
236-
): PickResult[] | FrustumPickResult => {
286+
y2: number
287+
): FrustumPickResult | null => {
237288
const selector = getSelector();
238289
const openGLRenderWindow = openGLRenderWindowAPI.get();
239290
const renderer = rendererAPI.get();
240291

241292
selector.setArea(x1, y1, x2, y2);
242293

243-
if (selector.captureBuffers()) {
244-
const selections = selector.generateSelection(x1, y1, x2, y2) || [];
245-
if (useFrustrum) {
246-
const frustum = [
247-
Array.from(openGLRenderWindow.displayToWorld(x1, y1, 0, renderer)),
248-
Array.from(openGLRenderWindow.displayToWorld(x2, y1, 0, renderer)),
249-
Array.from(openGLRenderWindow.displayToWorld(x2, y2, 0, renderer)),
250-
Array.from(openGLRenderWindow.displayToWorld(x1, y2, 0, renderer)),
251-
Array.from(openGLRenderWindow.displayToWorld(x1, y1, 1, renderer)),
252-
Array.from(openGLRenderWindow.displayToWorld(x2, y1, 1, renderer)),
253-
Array.from(openGLRenderWindow.displayToWorld(x2, y2, 1, renderer)),
254-
Array.from(openGLRenderWindow.displayToWorld(x1, y2, 1, renderer)),
255-
];
256-
const representationIds: string[] = [];
257-
selections.forEach((v) => {
258-
const { prop } = v.getProperties();
259-
const getterResult:
260-
| {
261-
representationID?: string;
262-
}
263-
| undefined = prop?.get('representationID');
264-
const representationId = getterResult?.representationID;
265-
if (representationId) {
266-
representationIds.push(representationId);
294+
if (!selector.captureBuffers()) {
295+
return null;
296+
}
297+
298+
const frustum = [
299+
Array.from(openGLRenderWindow.displayToWorld(x1, y1, 0, renderer)),
300+
Array.from(openGLRenderWindow.displayToWorld(x2, y1, 0, renderer)),
301+
Array.from(openGLRenderWindow.displayToWorld(x2, y2, 0, renderer)),
302+
Array.from(openGLRenderWindow.displayToWorld(x1, y2, 0, renderer)),
303+
Array.from(openGLRenderWindow.displayToWorld(x1, y1, 1, renderer)),
304+
Array.from(openGLRenderWindow.displayToWorld(x2, y1, 1, renderer)),
305+
Array.from(openGLRenderWindow.displayToWorld(x2, y2, 1, renderer)),
306+
Array.from(openGLRenderWindow.displayToWorld(x1, y2, 1, renderer)),
307+
];
308+
309+
const representationIds: string[] = [];
310+
const selections = selector.generateSelection(x1, y1, x2, y2) || [];
311+
selections.forEach((v) => {
312+
const { prop } = v.getProperties();
313+
const getterResult:
314+
| {
315+
representationID?: string;
267316
}
268-
});
269-
return { frustum, representationIds };
317+
| undefined = prop?.get('representationID');
318+
const representationId = getterResult?.representationID;
319+
if (representationId) {
320+
representationIds.push(representationId);
270321
}
271-
const ray = [
272-
Array.from(
273-
openGLRenderWindow.displayToWorld(
274-
Math.round((x1 + x2) / 2),
275-
Math.round((y1 + y2) / 2),
276-
0,
277-
renderer
278-
)
279-
),
280-
Array.from(
281-
openGLRenderWindow.displayToWorld(
282-
Math.round((x1 + x2) / 2),
283-
Math.round((y1 + y2) / 2),
284-
1,
285-
renderer
286-
)
287-
),
288-
];
289-
return selections
290-
.map((v) => {
291-
const { prop, compositeID, displayPosition } = v.getProperties();
292-
293-
// Return false to mark this item for removal
294-
if (prop == null || !displayPosition) return false;
295-
296-
return {
297-
worldPosition: Array.from(
298-
openGLRenderWindow.displayToWorld(
299-
displayPosition[0],
300-
displayPosition[1],
301-
displayPosition[2],
302-
renderer
303-
)
304-
),
305-
displayPosition,
306-
compositeID, // Not yet useful unless GlyphRepresentation
307-
...prop.get('representationID'),
308-
ray,
309-
};
310-
})
311-
.filter(Boolean) as PickResult[];
312-
}
313-
return [];
322+
});
323+
return { frustum, representationIds };
314324
},
315325
[rendererAPI, openGLRenderWindowAPI, getSelector]
316326
);
317327

318328
const api = useMemo(
319329
() => ({
320330
pick,
331+
pickWithFrustum,
321332
pickClosest,
322333
}),
323-
[pick, pickClosest]
334+
[pick, pickWithFrustum, pickClosest]
324335
);
325336

326337
useImperativeHandle(fwdRef, () => api);

0 commit comments

Comments
 (0)