1
1
import macro from 'vtk.js/Sources/macros' ;
2
- import * as vtkMath from 'vtk.js/Sources/Common/Core/Math' ;
2
+ import vtkCellTypes from 'vtk.js/Sources/Common/DataModel/CellTypes' ;
3
+ import vtkLine from 'vtk.js/Sources/Common/DataModel/Line' ;
3
4
import vtkPicker from 'vtk.js/Sources/Rendering/Core/Picker' ;
4
- import vtkPoints from 'vtk.js/Sources/Common/Core/Points ' ;
5
+ import vtkPolyLine from 'vtk.js/Sources/Common/DataModel/PolyLine ' ;
5
6
import vtkTriangle from 'vtk.js/Sources/Common/DataModel/Triangle' ;
6
-
7
+ import * as vtkMath from 'vtk.js/Sources/Common/Core/Math' ;
8
+ import { CellType } from 'vtk.js/Sources/Common/DataModel/CellTypes/Constants' ;
7
9
import { vec3 } from 'gl-matrix' ;
8
10
9
11
// ----------------------------------------------------------------------------
10
12
// Global methods
11
13
// ----------------------------------------------------------------------------
12
14
15
+ function createCellMap ( ) {
16
+ return {
17
+ [ CellType . VTK_LINE ] : vtkLine . newInstance ( ) ,
18
+ [ CellType . VTK_POLY_LINE ] : vtkPolyLine . newInstance ( ) ,
19
+ [ CellType . VTK_TRIANGLE ] : vtkTriangle . newInstance ( ) ,
20
+ } ;
21
+ }
22
+
13
23
function clipLineWithPlane ( mapper , matrix , p1 , p2 ) {
14
24
const outObj = { planeId : - 1 , t1 : 0.0 , t2 : 1.0 , intersect : 0 } ;
15
25
const nbClippingPlanes = mapper . getNumberOfClippingPlanes ( ) ;
@@ -238,8 +248,10 @@ function vtkCellPicker(publicAPI, model) {
238
248
const minXYZ = [ 0 , 0 , 0 ] ;
239
249
let pDistMin = Number . MAX_VALUE ;
240
250
const minPCoords = [ 0 , 0 , 0 ] ;
241
- let minCellId = - 1 ;
242
- const minCell = vtkTriangle . newInstance ( ) ;
251
+ let minCellId = null ;
252
+ let minCell = null ;
253
+ let minCellType = null ;
254
+ let subId = null ;
243
255
const x = [ ] ;
244
256
const data = mapper . getInputData ( ) ;
245
257
const isPolyData = 1 ;
@@ -263,31 +275,47 @@ function vtkCellPicker(publicAPI, model) {
263
275
const locator = null ;
264
276
if ( locator ) {
265
277
// TODO when cell locator will be implemented
266
- } else if ( data . getPolys ) {
267
- const cellObject = data . getPolys ( ) ;
268
- const points = data . getPoints ( ) ;
269
- const cellData = cellObject . getData ( ) ;
270
- let cellId = 0 ;
271
- const pointsIdList = [ - 1 , - 1 , - 1 ] ;
272
- const cell = vtkTriangle . newInstance ( ) ;
273
- const cellPoints = vtkPoints . newInstance ( ) ;
274
- // cross all cells
275
- for ( let i = 0 ; i < cellData . length ; cellId ++ ) {
278
+ } else if ( data . getCells ) {
279
+ if ( ! data . getCells ( ) ) {
280
+ data . buildLinks ( ) ;
281
+ }
282
+
283
+ const tempCellMap = createCellMap ( ) ;
284
+ const minCellMap = createCellMap ( ) ;
285
+
286
+ const numberOfCells = data . getNumberOfCells ( ) ;
287
+
288
+ for ( let cellId = 0 ; cellId < numberOfCells ; cellId ++ ) {
276
289
const pCoords = [ 0 , 0 , 0 ] ;
277
- const nbPointsInCell = cellData [ i ++ ] ;
278
290
279
- cellPoints . setNumberOfPoints ( nbPointsInCell ) ;
280
- // Extract cell points
281
- for ( let j = 0 ; j < nbPointsInCell ; j ++ ) {
282
- pointsIdList [ j ] = cellData [ i ++ ] ;
291
+ minCellType = data . getCellType ( cellId ) ;
292
+ const cell = tempCellMap [ minCellType ] ;
293
+
294
+ if ( cell == null ) {
295
+ // eslint-disable-next-line no-continue
296
+ continue ;
283
297
}
284
298
285
- // Create cell from points
286
- cell . initialize ( points , pointsIdList ) ;
299
+ minCell = minCellMap [ minCellType ] ;
300
+
301
+ data . getCell ( cellId , cell ) ;
287
302
288
303
let cellPicked ;
304
+
289
305
if ( isPolyData ) {
290
- cellPicked = cell . intersectWithLine ( p1 , p2 , tol , x , pCoords ) ;
306
+ if ( vtkCellTypes . hasSubCells ( minCellType ) ) {
307
+ cellPicked = cell . intersectWithLine (
308
+ t1 ,
309
+ t2 ,
310
+ p1 ,
311
+ p2 ,
312
+ tol ,
313
+ x ,
314
+ pCoords
315
+ ) ;
316
+ } else {
317
+ cellPicked = cell . intersectWithLine ( p1 , p2 , tol , x , pCoords ) ;
318
+ }
291
319
} else {
292
320
cellPicked = cell . intersectWithLine ( q1 , q2 , tol , x , pCoords ) ;
293
321
if ( t1 !== 0.0 || t2 !== 1.0 ) {
@@ -302,9 +330,11 @@ function vtkCellPicker(publicAPI, model) {
302
330
cellPicked . t <= t2
303
331
) {
304
332
const pDist = cell . getParametricDistance ( pCoords ) ;
333
+
305
334
if ( pDist < pDistMin || ( pDist === pDistMin && cellPicked . t < tMin ) ) {
306
335
tMin = cellPicked . t ;
307
336
pDistMin = pDist ;
337
+ subId = cellPicked . subId ;
308
338
minCellId = cellId ;
309
339
cell . deepCopy ( minCell ) ;
310
340
for ( let k = 0 ; k < 3 ; k ++ ) {
@@ -324,7 +354,12 @@ function vtkCellPicker(publicAPI, model) {
324
354
weights [ i ] = 0.0 ;
325
355
}
326
356
const point = [ ] ;
327
- minCell . evaluateLocation ( minPCoords , point , weights ) ;
357
+
358
+ if ( vtkCellTypes . hasSubCells ( minCellType ) ) {
359
+ minCell . evaluateLocation ( subId , minPCoords , point , weights ) ;
360
+ } else {
361
+ minCell . evaluateLocation ( minPCoords , point , weights ) ;
362
+ }
328
363
329
364
// Return the polydata to the user
330
365
model . dataSet = data ;
0 commit comments