Skip to content

Commit 4f0d842

Browse files
committed
feat(polyline): add polyline data model
1 parent a932c53 commit 4f0d842

File tree

1 file changed

+102
-0
lines changed
  • Sources/Common/DataModel/PolyLine

1 file changed

+102
-0
lines changed
Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
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.evaluatePosition = (
62+
x,
63+
closestPoint,
64+
subId,
65+
pcoords,
66+
dist2,
67+
weights
68+
) => {}; // virtual
69+
70+
publicAPI.evaluateLocation = (subId, pcoords, x, weights) => {
71+
line
72+
.getPoints()
73+
.getData()
74+
.set(model.points.getData().subarray(3 * subId, 3 * (subId + 2)));
75+
76+
return line.evaluateLocation(pcoords, x, weights);
77+
};
78+
}
79+
80+
// ----------------------------------------------------------------------------
81+
// Object factory
82+
// ----------------------------------------------------------------------------
83+
84+
const DEFAULT_VALUES = {};
85+
86+
// ----------------------------------------------------------------------------
87+
88+
export function extend(publicAPI, model, initialValues = {}) {
89+
Object.assign(model, DEFAULT_VALUES, initialValues);
90+
91+
vtkCell.extend(publicAPI, model, initialValues);
92+
93+
vtkPolyLine(publicAPI, model);
94+
}
95+
96+
// ----------------------------------------------------------------------------
97+
98+
export const newInstance = macro.newInstance(extend, 'vtkPolyLine');
99+
100+
// ----------------------------------------------------------------------------
101+
102+
export default { newInstance, extend };

0 commit comments

Comments
 (0)