Skip to content

Commit 81b4eaa

Browse files
committed
feat(ShrinkPolyData): add vtkShrinkPolyData
1 parent 527c2d9 commit 81b4eaa

File tree

11 files changed

+593
-1
lines changed

11 files changed

+593
-1
lines changed
36.8 KB
Loading

Documentation/content/examples/index.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ This will allow you to see the some live code running in your browser. Just pick
108108
[![Cutter Example][Cutter]](./Cutter.html "Cutter")
109109
[![PolyDataNormals Example][PolyDataNormals]](./PolyDataNormals.html "PolyDataNormals")
110110
[![ThresholdPoints Example][ThresholdPoints]](./ThresholdPoints.html "Cut/Treshold points with point data criteria")
111-
111+
[![ShrinkPolyData Example][ShrinkPolyData]](./ShrinkPolyData.html "ShrinkPolyData")
112112

113113
</div>
114114

@@ -128,6 +128,7 @@ This will allow you to see the some live code running in your browser. Just pick
128128
[Cutter]: ../docs/gallery/Cutter.jpg
129129
[PolyDataNormals]: ../docs/gallery/PolyDataNormals.jpg
130130
[ThresholdPoints]: ../docs/gallery/ThresholdPoints.jpg
131+
[ShrinkPolyData]: ../docs/gallery/ShrinkPolyData.jpg
131132

132133
# Sources
133134

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

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,19 @@ export function pointInPolygon(
6060
normal: Vector3
6161
): PolygonIntersectionState;
6262

63+
/**
64+
* Compute the centroid of a polygon.
65+
* @param {Array<number>} poly - Array of point indices for the polygon
66+
* @param {vtkPoints} points - vtkPoints instance
67+
* @param {Vector3} [centroid] - Optional output array (length 3)
68+
* @returns {Vector3} The centroid as [x, y, z]
69+
*/
70+
export function computeCentroid(
71+
poly: Array<number>,
72+
points: TypedArray,
73+
centroid?: Vector3
74+
): void;
75+
6376
/**
6477
* Method used to decorate a given object (publicAPI+model) with vtkPolygon characteristics.
6578
*

Sources/Common/DataModel/Polygon/index.js

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -215,6 +215,31 @@ export function getNormal(poly, points, normal) {
215215
return vtkMath.normalize(normal);
216216
}
217217

218+
/**
219+
* Compute the centroid of a polygon.
220+
* @param {Array<number>} poly - Array of point indices for the polygon
221+
* @param {vtkPoints} points - vtkPoints instance
222+
* @param {Vector3} [centroid] - Optional output array (length 3)
223+
* @returns {Vector3} The centroid as [x, y, z]
224+
*/
225+
export function computeCentroid(poly, points, centroid = [0, 0, 0]) {
226+
centroid[0] = 0;
227+
centroid[1] = 0;
228+
centroid[2] = 0;
229+
const n = poly.length;
230+
const p = [];
231+
for (let i = 0; i < n; i++) {
232+
points.getPoint(poly[i], p);
233+
centroid[0] += p[0];
234+
centroid[1] += p[1];
235+
centroid[2] += p[2];
236+
}
237+
centroid[0] /= n;
238+
centroid[1] /= n;
239+
centroid[2] /= n;
240+
return centroid;
241+
}
242+
218243
// ----------------------------------------------------------------------------
219244
// Static API
220245
// ----------------------------------------------------------------------------
@@ -224,6 +249,7 @@ const STATIC = {
224249
pointInPolygon,
225250
getBounds,
226251
getNormal,
252+
computeCentroid,
227253
};
228254

229255
// ----------------------------------------------------------------------------
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<table>
2+
<tr>
3+
<td>Shrink Factor</td>
4+
<td colspan="3">
5+
<input class='shrinkFactor' type="range" min="0.1" max="1" step="0.1" value="0.25" />
6+
</td>
7+
</tr>
8+
</table>
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
import '@kitware/vtk.js/favicon';
2+
3+
// Load the rendering pieces we want to use (for both WebGL and WebGPU)
4+
import '@kitware/vtk.js/Rendering/Profiles/Geometry';
5+
import '@kitware/vtk.js/IO/Core/DataAccessHelper/HttpDataAccessHelper';
6+
7+
import vtkFullScreenRenderWindow from '@kitware/vtk.js/Rendering/Misc/FullScreenRenderWindow';
8+
import vtkActor from '@kitware/vtk.js/Rendering/Core/Actor';
9+
import vtkHttpDataSetReader from '@kitware/vtk.js/IO/Core/HttpDataSetReader';
10+
import vtkShrinkPolyData from '@kitware/vtk.js/Filters/General/ShrinkPolyData';
11+
import vtkMapper from '@kitware/vtk.js/Rendering/Core/Mapper';
12+
import controlPanel from './controlPanel.html';
13+
14+
// ----------------------------------------------------------------------------
15+
// Standard rendering code setup
16+
// ----------------------------------------------------------------------------
17+
18+
const fullScreenRenderer = vtkFullScreenRenderWindow.newInstance();
19+
const renderer = fullScreenRenderer.getRenderer();
20+
const renderWindow = fullScreenRenderer.getRenderWindow();
21+
22+
fullScreenRenderer.addController(controlPanel);
23+
24+
// ----------------------------------------------------------------------------
25+
// Example code
26+
// ----------------------------------------------------------------------------
27+
const shrinkPolyData = vtkShrinkPolyData.newInstance();
28+
shrinkPolyData.setShrinkFactor(0.25);
29+
30+
const actor = vtkActor.newInstance();
31+
const mapper = vtkMapper.newInstance();
32+
mapper.setInputConnection(shrinkPolyData.getOutputPort());
33+
actor.setMapper(mapper);
34+
35+
const reader = vtkHttpDataSetReader.newInstance({ fetchGzip: true });
36+
shrinkPolyData.setInputConnection(reader.getOutputPort());
37+
38+
reader.setUrl(`${__BASE_PATH__}/data/cow.vtp`).then(() => {
39+
reader.loadData().then(() => {
40+
renderer.addActor(actor);
41+
renderer.resetCamera();
42+
renderWindow.render();
43+
});
44+
});
45+
46+
['shrinkFactor'].forEach((propertyName) => {
47+
document.querySelector(`.${propertyName}`).addEventListener('input', (e) => {
48+
const value = Number(e.target.value);
49+
shrinkPolyData.set({ [propertyName]: value });
50+
renderWindow.render();
51+
});
52+
});
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
import { DesiredOutputPrecision } from '../../../Common/DataModel/DataSetAttributes';
2+
import { vtkAlgorithm, vtkObject } from '../../../interfaces';
3+
import { Vector3 } from '../../../types';
4+
5+
/**
6+
*
7+
*/
8+
export interface IShrinkPolyDataInitialValues {
9+
shrinkFactor?: number;
10+
}
11+
12+
type vtkShrinkPolyDataBase = vtkObject & vtkAlgorithm;
13+
14+
export interface vtkShrinkPolyData extends vtkShrinkPolyDataBase {
15+
/**
16+
* Expose methods
17+
* @param inData
18+
* @param outData
19+
*/
20+
requestData(inData: any, outData: any): void;
21+
22+
/**
23+
* Get the shrink factor.
24+
*/
25+
getShrinkFactor(): number;
26+
27+
/**
28+
* Set the shrink factor.
29+
* @param {Number} shrinkFactor
30+
*/
31+
setShrinkFactor(shrinkFactor: number): boolean;
32+
33+
/**
34+
* Shrink two points towards their midpoint by a shrink factor.
35+
* @param {Vector3} p1 - The [x, y, z] coordinates of the first point
36+
* @param {Vector3} p2 - The [x, y, z] coordinates of the second point
37+
* @param {number} shrinkFactor - The shrink factor (0.0 to 1.0)
38+
* @returns {Vector3[]} Array containing the two new points
39+
*/
40+
shrinkLine(p1: Vector3, p2: Vector3, shrinkFactor: number): Vector3[];
41+
}
42+
43+
/**
44+
* Method used to decorate a given object (publicAPI+model) with vtkShrinkPolyData characteristics.
45+
*
46+
* @param publicAPI object on which methods will be bounds (public)
47+
* @param model object on which data structure will be bounds (protected)
48+
* @param {IShrinkPolyDataInitialValues} [initialValues] (default: {})
49+
*/
50+
export function extend(
51+
publicAPI: object,
52+
model: object,
53+
initialValues?: IShrinkPolyDataInitialValues
54+
): void;
55+
56+
/**
57+
* Method used to create a new instance of vtkShrinkPolyData.
58+
* @param {IShrinkPolyDataInitialValues} [initialValues] for pre-setting some of its content
59+
*/
60+
export function newInstance(
61+
initialValues?: IShrinkPolyDataInitialValues
62+
): vtkShrinkPolyData;
63+
64+
/**
65+
* vtkShrinkPolyData shrinks cells composing a polygonal dataset (e.g.,
66+
* vertices, lines, polygons, and triangle strips) towards their centroid. The
67+
* centroid of a cell is computed as the average position of the cell points.
68+
* Shrinking results in disconnecting the cells from one another. The output
69+
* dataset type of this filter is polygonal data.
70+
*
71+
* During execution the filter passes its input cell data to its output. Point
72+
* data attributes are copied to the points created during the shrinking
73+
* process.
74+
*/
75+
export declare const vtkShrinkPolyData: {
76+
newInstance: typeof newInstance;
77+
extend: typeof extend;
78+
};
79+
export default vtkShrinkPolyData;

0 commit comments

Comments
 (0)