|
| 1 | +import macro from 'vtk.js/Sources/macros'; |
| 2 | +import vtkCell from 'vtk.js/Sources/Common/DataModel/Cell'; |
| 3 | +import vtkLine from 'vtk.js/Sources/Common/DataModel/Line'; |
| 4 | + |
| 5 | +function vtkPolyLine(publicAPI, model) { |
| 6 | + model.classHierarchy.push('vtkPolyLine'); |
| 7 | + const superClass = { ...publicAPI }; |
| 8 | + |
| 9 | + const line = vtkLine.newInstance(); |
| 10 | + line.getPoints().setNumberOfPoints(2); |
| 11 | + |
| 12 | + publicAPI.getCellDimension = () => 1; |
| 13 | + publicAPI.intersectWithLine = (t1, t2, p1, p2, tol, x, pcoords) => { |
| 14 | + const outObj = { |
| 15 | + intersect: 0, |
| 16 | + t: Number.MAX_VALUE, |
| 17 | + subId: 0, |
| 18 | + betweenPoints: null, |
| 19 | + }; |
| 20 | + |
| 21 | + const numLines = superClass.getPoints().getNumberOfPoints() - 1; |
| 22 | + let pDistMin = Number.MAX_VALUE; |
| 23 | + const minXYZ = [0, 0, 0]; |
| 24 | + const minPCoords = [0, 0, 0]; |
| 25 | + for (let subId = 0; subId < numLines; subId++) { |
| 26 | + const pCoords = [0, 0, 0]; |
| 27 | + |
| 28 | + line |
| 29 | + .getPoints() |
| 30 | + .getData() |
| 31 | + .set(model.points.getData().subarray(3 * subId, 3 * (subId + 2))); |
| 32 | + |
| 33 | + const lineIntersected = line.intersectWithLine(p1, p2, tol, x, pcoords); |
| 34 | + |
| 35 | + if ( |
| 36 | + lineIntersected.intersect === 1 && |
| 37 | + lineIntersected.t <= outObj.t + tol && |
| 38 | + lineIntersected.t >= t1 && |
| 39 | + lineIntersected.t <= t2 |
| 40 | + ) { |
| 41 | + outObj.intersect = 1; |
| 42 | + const pDist = line.getParametricDistance(pCoords); |
| 43 | + if ( |
| 44 | + pDist < pDistMin || |
| 45 | + (pDist === pDistMin && lineIntersected.t < outObj.t) |
| 46 | + ) { |
| 47 | + outObj.subId = subId; |
| 48 | + outObj.t = lineIntersected.t; |
| 49 | + pDistMin = pDist; |
| 50 | + for (let k = 0; k < 3; k++) { |
| 51 | + minXYZ[k] = x[k]; |
| 52 | + minPCoords[k] = pCoords[k]; |
| 53 | + } |
| 54 | + } |
| 55 | + } |
| 56 | + } |
| 57 | + |
| 58 | + return outObj; |
| 59 | + }; |
| 60 | + |
| 61 | + publicAPI.evaluateLocation = (subId, pcoords, x, weights) => { |
| 62 | + line |
| 63 | + .getPoints() |
| 64 | + .getData() |
| 65 | + .set(model.points.getData().subarray(3 * subId, 3 * (subId + 2))); |
| 66 | + |
| 67 | + return line.evaluateLocation(pcoords, x, weights); |
| 68 | + }; |
| 69 | +} |
| 70 | + |
| 71 | +// ---------------------------------------------------------------------------- |
| 72 | +// Object factory |
| 73 | +// ---------------------------------------------------------------------------- |
| 74 | + |
| 75 | +const DEFAULT_VALUES = {}; |
| 76 | + |
| 77 | +// ---------------------------------------------------------------------------- |
| 78 | + |
| 79 | +export function extend(publicAPI, model, initialValues = {}) { |
| 80 | + Object.assign(model, DEFAULT_VALUES, initialValues); |
| 81 | + |
| 82 | + vtkCell.extend(publicAPI, model, initialValues); |
| 83 | + |
| 84 | + vtkPolyLine(publicAPI, model); |
| 85 | +} |
| 86 | + |
| 87 | +// ---------------------------------------------------------------------------- |
| 88 | + |
| 89 | +export const newInstance = macro.newInstance(extend, 'vtkPolyLine'); |
| 90 | + |
| 91 | +// ---------------------------------------------------------------------------- |
| 92 | + |
| 93 | +export default { newInstance, extend }; |
0 commit comments