Skip to content

feat(ShrinkPolyData): add vtkShrinkPolyData #3291

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Aug 12, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
3 changes: 2 additions & 1 deletion Documentation/content/examples/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ This will allow you to see the some live code running in your browser. Just pick
[![Cutter Example][Cutter]](./Cutter.html "Cutter")
[![PolyDataNormals Example][PolyDataNormals]](./PolyDataNormals.html "PolyDataNormals")
[![ThresholdPoints Example][ThresholdPoints]](./ThresholdPoints.html "Cut/Treshold points with point data criteria")

[![ShrinkPolyData Example][ShrinkPolyData]](./ShrinkPolyData.html "ShrinkPolyData")

</div>

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

# Sources

Expand Down
13 changes: 13 additions & 0 deletions Sources/Common/DataModel/Polygon/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,19 @@ export function pointInPolygon(
normal: Vector3
): PolygonIntersectionState;

/**
* Compute the centroid of a polygon.
* @param {Array<number>} poly - Array of point indices for the polygon
* @param {vtkPoints} points - vtkPoints instance
* @param {Vector3} [centroid] - Optional output array (length 3)
* @returns {Vector3} The centroid as [x, y, z]
*/
export function computeCentroid(
poly: Array<number>,
points: TypedArray,
centroid?: Vector3
): Vector3;

/**
* Method used to decorate a given object (publicAPI+model) with vtkPolygon characteristics.
*
Expand Down
26 changes: 26 additions & 0 deletions Sources/Common/DataModel/Polygon/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,31 @@ export function getNormal(poly, points, normal) {
return vtkMath.normalize(normal);
}

/**
* Compute the centroid of a polygon.
* @param {Array<number>} poly - Array of point indices for the polygon
* @param {vtkPoints} points - vtkPoints instance
* @param {Vector3} [centroid] - Optional output array (length 3)
* @returns {Vector3} The centroid as [x, y, z]
*/
export function computeCentroid(poly, points, centroid = [0, 0, 0]) {
centroid[0] = 0;
centroid[1] = 0;
centroid[2] = 0;
const n = poly.length;
const p = [];
for (let i = 0; i < n; i++) {
points.getPoint(poly[i], p);
centroid[0] += p[0];
centroid[1] += p[1];
centroid[2] += p[2];
}
centroid[0] /= n;
centroid[1] /= n;
centroid[2] /= n;
return centroid;
}

// ----------------------------------------------------------------------------
// Static API
// ----------------------------------------------------------------------------
Expand All @@ -224,6 +249,7 @@ const STATIC = {
pointInPolygon,
getBounds,
getNormal,
computeCentroid,
};

// ----------------------------------------------------------------------------
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<table>
<tr>
<td>Shrink Factor</td>
<td colspan="3">
<input class='shrinkFactor' type="range" min="0.1" max="1" step="0.1" value="0.25" />
</td>
</tr>
</table>
52 changes: 52 additions & 0 deletions Sources/Filters/General/ShrinkPolyData/example/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
import '@kitware/vtk.js/favicon';

// Load the rendering pieces we want to use (for both WebGL and WebGPU)
import '@kitware/vtk.js/Rendering/Profiles/Geometry';
import '@kitware/vtk.js/IO/Core/DataAccessHelper/HttpDataAccessHelper';

import vtkFullScreenRenderWindow from '@kitware/vtk.js/Rendering/Misc/FullScreenRenderWindow';
import vtkActor from '@kitware/vtk.js/Rendering/Core/Actor';
import vtkHttpDataSetReader from '@kitware/vtk.js/IO/Core/HttpDataSetReader';
import vtkShrinkPolyData from '@kitware/vtk.js/Filters/General/ShrinkPolyData';
import vtkMapper from '@kitware/vtk.js/Rendering/Core/Mapper';
import controlPanel from './controlPanel.html';

// ----------------------------------------------------------------------------
// Standard rendering code setup
// ----------------------------------------------------------------------------

const fullScreenRenderer = vtkFullScreenRenderWindow.newInstance();
const renderer = fullScreenRenderer.getRenderer();
const renderWindow = fullScreenRenderer.getRenderWindow();

fullScreenRenderer.addController(controlPanel);

// ----------------------------------------------------------------------------
// Example code
// ----------------------------------------------------------------------------
const shrinkPolyData = vtkShrinkPolyData.newInstance();
shrinkPolyData.setShrinkFactor(0.25);

const actor = vtkActor.newInstance();
const mapper = vtkMapper.newInstance();
mapper.setInputConnection(shrinkPolyData.getOutputPort());
actor.setMapper(mapper);

const reader = vtkHttpDataSetReader.newInstance({ fetchGzip: true });
shrinkPolyData.setInputConnection(reader.getOutputPort());

reader.setUrl(`${__BASE_PATH__}/data/cow.vtp`).then(() => {
reader.loadData().then(() => {
renderer.addActor(actor);
renderer.resetCamera();
renderWindow.render();
});
});

['shrinkFactor'].forEach((propertyName) => {
document.querySelector(`.${propertyName}`).addEventListener('input', (e) => {
const value = Number(e.target.value);
shrinkPolyData.set({ [propertyName]: value });
renderWindow.render();
});
});
85 changes: 85 additions & 0 deletions Sources/Filters/General/ShrinkPolyData/index.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
import { DesiredOutputPrecision } from '../../../Common/DataModel/DataSetAttributes';
import { vtkAlgorithm, vtkObject } from '../../../interfaces';
import { Vector3 } from '../../../types';

/**
*
*/
export interface IShrinkPolyDataInitialValues {
shrinkFactor?: number;
}

type vtkShrinkPolyDataBase = vtkObject & vtkAlgorithm;

export interface vtkShrinkPolyData extends vtkShrinkPolyDataBase {
/**
* Expose methods
* @param inData
* @param outData
*/
requestData(inData: any, outData: any): void;

/**
* Get the shrink factor.
*/
getShrinkFactor(): number;

/**
* Set the shrink factor.
* @param {Number} shrinkFactor
*/
setShrinkFactor(shrinkFactor: number): boolean;

/**
* Shrink two points towards their midpoint by a shrink factor.
* @param {Vector3} p1 - The [x, y, z] coordinates of the first point
* @param {Vector3} p2 - The [x, y, z] coordinates of the second point
* @param {number} shrinkFactor - The shrink factor (0.0 to 1.0)
* @param {Number[]} [shrunkPoints] - Optional array to store the shrunk points
* @returns {Number[]} Array containing the two new points
*/
shrinkLine(
p1: Vector3,
p2: Vector3,
shrinkFactor: number,
shrunkPoints?: Number[]
): Number[];
}

/**
* Method used to decorate a given object (publicAPI+model) with vtkShrinkPolyData characteristics.
*
* @param publicAPI object on which methods will be bounds (public)
* @param model object on which data structure will be bounds (protected)
* @param {IShrinkPolyDataInitialValues} [initialValues] (default: {})
*/
export function extend(
publicAPI: object,
model: object,
initialValues?: IShrinkPolyDataInitialValues
): void;

/**
* Method used to create a new instance of vtkShrinkPolyData.
* @param {IShrinkPolyDataInitialValues} [initialValues] for pre-setting some of its content
*/
export function newInstance(
initialValues?: IShrinkPolyDataInitialValues
): vtkShrinkPolyData;

/**
* vtkShrinkPolyData shrinks cells composing a polygonal dataset (e.g.,
* vertices, lines, polygons, and triangle strips) towards their centroid. The
* centroid of a cell is computed as the average position of the cell points.
* Shrinking results in disconnecting the cells from one another. The output
* dataset type of this filter is polygonal data.
*
* During execution the filter passes its input cell data to its output. Point
* data attributes are copied to the points created during the shrinking
* process.
*/
export declare const vtkShrinkPolyData: {
newInstance: typeof newInstance;
extend: typeof extend;
};
export default vtkShrinkPolyData;
Loading