Skip to content

Commit 1bd7ace

Browse files
authored
Merge pull request #2394 from Julesdevops/cellpicker-edge-picking
Add edge picking support to the Cellpicker
2 parents a932c53 + 6066498 commit 1bd7ace

File tree

7 files changed

+251
-44
lines changed

7 files changed

+251
-44
lines changed

Sources/Common/DataModel/Cell/index.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,9 @@ function vtkCell(publicAPI, model) {
113113
pcoords,
114114
dist2,
115115
weights
116-
) => {}; // virtual
116+
) => {
117+
macro.vtkErrorMacro('vtkCell.evaluatePosition is not implemented.');
118+
}; // virtual
117119
}
118120

119121
// ----------------------------------------------------------------------------

Sources/Common/DataModel/CellTypes/index.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,14 @@ function isLinear(type) {
4040
);
4141
}
4242

43+
function hasSubCells(cellType) {
44+
return (
45+
cellType === CellType.VTK_TRIANGLE_STRIP ||
46+
cellType === CellType.VTK_POLY_LINE ||
47+
cellType === CellType.VTK_POLY_VERTEX
48+
);
49+
}
50+
4351
// ----------------------------------------------------------------------------
4452
// Static API
4553
// ----------------------------------------------------------------------------
@@ -48,6 +56,7 @@ export const STATIC = {
4856
getClassNameFromTypeId,
4957
getTypeIdFromClassName,
5058
isLinear,
59+
hasSubCells,
5160
};
5261

5362
// ----------------------------------------------------------------------------

Sources/Common/DataModel/Line/index.d.ts

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { Vector3 } from '../../../types';
1+
import { Vector3, Vector2 } from '../../../types';
22
import vtkCell from '../Cell';
33

44
export enum IntersectionState {
@@ -9,7 +9,7 @@ export enum IntersectionState {
99

1010
interface ILineInitialValues { }
1111

12-
interface IIntersectWithLine {
12+
export interface IIntersectWithLine {
1313
intersect: number;
1414
t: number;
1515
subId: number;
@@ -50,6 +50,11 @@ export interface vtkLine extends vtkCell {
5050
* @param {Vector3} pcoords The parametric coordinates.
5151
*/
5252
intersectWithLine(p1: Vector3, p2: Vector3, tol: number, x: Vector3, pcoords: Vector3): IIntersectWithLine;
53+
54+
/**
55+
* Determine the global coordinates `x' and parametric coordinates `pcoords' in the cell.
56+
*/
57+
evaluateLocation(pcoords: Vector3, x: Vector3, weights: Vector2): void
5358
}
5459

5560
/**

Sources/Common/DataModel/Line/index.js

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -63,15 +63,9 @@ function intersection(a1, a2, b1, b2, u, v) {
6363
v[0] = 0.0;
6464

6565
// Determine line vectors.
66-
a21[0] = a2[0] - a1[0];
67-
a21[1] = a2[1] - a1[1];
68-
a21[2] = a2[2] - a1[2];
69-
b21[0] = b2[0] - b1[0];
70-
b21[1] = b2[1] - b1[1];
71-
b21[2] = b2[2] - b1[2];
72-
b1a1[0] = b1[0] - a1[0];
73-
b1a1[1] = b1[1] - a1[1];
74-
b1a1[2] = b1[2] - a1[2];
66+
vtkMath.subtract(a2, a1, a21);
67+
vtkMath.subtract(b2, b1, b21);
68+
vtkMath.subtract(b1, a1, b1a1);
7569

7670
// Compute the system (least squares) matrix.
7771
const A = [];
@@ -217,14 +211,20 @@ function vtkLine(publicAPI, model) {
217211
}
218212
return outObj;
219213
};
220-
publicAPI.evaluatePosition = (
221-
x,
222-
closestPoint,
223-
subId,
224-
pcoords,
225-
dist2,
226-
weights
227-
) => {}; // virtual
214+
215+
publicAPI.evaluateLocation = (pcoords, x, weights) => {
216+
const a1 = [];
217+
const a2 = [];
218+
model.points.getPoint(0, a1);
219+
model.points.getPoint(1, a2);
220+
221+
for (let i = 0; i < 3; i++) {
222+
x[i] = a1[i] + pcoords[0] * (a2[i] - a1[i]);
223+
}
224+
225+
weights[0] = 1.0 - pcoords[0];
226+
weights[1] = pcoords[0];
227+
};
228228
}
229229

230230
// ----------------------------------------------------------------------------
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
import { Vector2, Vector3 } from '../../../types';
2+
import vtkCell, { ICellInitialValues } from '../Cell';
3+
import { IIntersectWithLine } from '../Line';
4+
5+
export interface IPolyLineInitialValues extends ICellInitialValues { }
6+
7+
export interface vtkPolyLine extends vtkCell {
8+
9+
/**
10+
* Get the topological dimensional of the cell (0, 1, 2 or 3).
11+
*/
12+
getCellDimension(): number;
13+
14+
/**
15+
* @param {number} t1
16+
* @param {number} t2
17+
* @param {Vector3} p1 The first point coordinate.
18+
* @param {Vector3} p2 The second point coordinate.
19+
* @param {Number} tol The tolerance to use.
20+
* @param {Vector3} x The point which intersect the line.
21+
* @param {Vector3} pcoords The parametric coordinates.
22+
*/
23+
intersectWithLine(t1: number, t2: number, p1: Vector3, p2: Vector3, tol: number, x: Vector3, pcoords: Vector3): IIntersectWithLine;
24+
25+
/**
26+
* Determine global coordinate (x[3]) from subId and parametric coordinates.
27+
* Also returns interpolation weights. (The number of weights is equal to
28+
* the number of points in the cell.)
29+
*
30+
* @param {number} subId
31+
* @param {Vector3} pcoords The parametric coordinates
32+
* @param {Vector3} x The global coordinate
33+
* @param {Vector2} weights The interpolation weights
34+
*/
35+
evaluateLocation(subId: number, pcoords: Vector3, x: Vector3, weights: Vector2): void
36+
}
37+
38+
/**
39+
* Method used to decorate a given object (publicAPI+model) with vtkPolyLine characteristics.
40+
*
41+
* @param publicAPI object on which methods will be bounds (public)
42+
* @param model object on which data structure will be bounds (protected)
43+
* @param {IPolyLineInitialValues} [initialValues] (default: {})
44+
*/
45+
export function extend(publicAPI: object, model: object, initialValues?: IPolyLineInitialValues): void;
46+
47+
/**
48+
* Method used to create a new instance of vtkPolyLine.
49+
* @param {IPolyLineInitialValues} [initialValues] for pre-setting some of its content
50+
*/
51+
export function newInstance(initialValues?: IPolyLineInitialValues): vtkPolyLine;
52+
53+
/**
54+
* vtkPolyLine is a cell which representant a poly line.
55+
*
56+
* @see vtkCell
57+
*/
58+
export declare const vtkPolyLine: {
59+
newInstance: typeof newInstance,
60+
extend: typeof extend;
61+
};
62+
63+
export default vtkPolyLine;
Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
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

Comments
 (0)