TransformPolyDataFilter

Introduction

vtkTransformPolyDataFilter is a filter to transform point coordinates and
associated point and cell normals and vectors. Other point and cell data is
passed through the filter unchanged. This filter is specialized for polygonal
data.

Methods

extend

Method used to decorate a given object (publicAPI+model) with vtkTransformPolyDataFilter characteristics.

Argument Type Required Description
publicAPI Yes object on which methods will be bounds (public)
model Yes object on which data structure will be bounds (protected)
initialValues ITransformPolyDataFilterInitialValues No (default: {})

getOutputPointsPrecision

Get the output points precision.

getTransform

Get the transform used by this filter.

newInstance

Method used to create a new instance of vtkTransformPolyDataFilter.

Argument Type Required Description
initialValues ITransformPolyDataFilterInitialValues No for pre-setting some of its content

requestData

Argument Type Required Description
inData Yes
outData Yes

setOutputPointsPrecision

Set the output points precision.

Argument Type Required Description
precision DesiredOutputPrecision Yes

setTransform

Set the transform used by this filter.

Argument Type Required Description
transform vtkTransform Yes

Source

index.d.ts
import { DesiredOutputPrecision } from '../../../Common/DataModel/DataSetAttributes';
import vtkTransform from '../../../Common/Transform/Transform';
import { vtkAlgorithm, vtkObject } from '../../../interfaces';

export interface ITransformPolyDataFilterInitialValues {
transform?: vtkTransform;
outputPointsPrecision?: DesiredOutputPrecision;
}

type vtkTransformPolyDataFilterBase = vtkObject & vtkAlgorithm;

export interface vtkTransformPolyDataFilter
extends vtkTransformPolyDataFilterBase {
/**
* Get the transform used by this filter.
*/
getTransform(): vtkTransform;

/**
* Get the output points precision.
*/
getOutputPointsPrecision(): DesiredOutputPrecision;

/**
*
* @param inData
* @param outData
*/
requestData(inData: any, outData: any): void;

/**
* Set the output points precision.
* @param {DesiredOutputPrecision} precision
*/
setOutputPointsPrecision(precision: DesiredOutputPrecision): boolean;

/**
* Set the transform used by this filter.
* @param {vtkTransform} transform
*/
setTransform(transform: vtkTransform): boolean;
}

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

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

/**
* vtkTransformPolyDataFilter is a filter to transform point coordinates and
* associated point and cell normals and vectors. Other point and cell data is
* passed through the filter unchanged. This filter is specialized for polygonal
* data.
*/
export declare const vtkTransformPolyDataFilter: {
newInstance: typeof newInstance;
extend: typeof extend;
};
export default vtkTransformPolyDataFilter;
index.js
import macro from 'vtk.js/Sources/macros';
import vtkDataArray from 'vtk.js/Sources/Common/Core/DataArray';
import vtkPoints from 'vtk.js/Sources/Common/Core/Points';
import vtkPolyData from 'vtk.js/Sources/Common/DataModel/PolyData';
import { DesiredOutputPrecision } from 'vtk.js/Sources/Common/DataModel/DataSetAttributes/Constants';
import { VtkDataTypes } from 'vtk.js/Sources/Common/Core/DataArray/Constants';

const { vtkErrorMacro } = macro;

// ----------------------------------------------------------------------------
// vtkTransformPolyDataFilter methods
// ----------------------------------------------------------------------------

function vtkTransformPolyDataFilter(publicAPI, model) {
// Set our className
model.classHierarchy.push('vtkTransformPolyDataFilter');

// Internal method to handle the actual transformation
function transformPolyData(input, output) {
if (!model.transform) {
vtkErrorMacro('No transform defined!');
return false;
}

const inPts = input.getPoints();
const inPtData = input.getPointData();
const outPD = output.getPointData();
const inCellData = input.getCellData();
const outCD = output.getCellData();

if (!inPts) {
// Input polydata is empty. This is not an error, the output will be just empty, too.
return true;
}

const numPoints = inPts.getNumberOfPoints();

// Get input vectors and normals (points and cells)
const inVectors = inPtData.getVectors();
const inNormals = inPtData.getNormals();
const inCellVectors = inCellData.getVectors();
const inCellNormals = inCellData.getNormals();

// Set the desired precision for the points in the output
let pointType = inPts.getDataType();
if (model.outputPointsPrecision === DesiredOutputPrecision.SINGLE) {
pointType = VtkDataTypes.FLOAT;
} else if (model.outputPointsPrecision === DesiredOutputPrecision.DOUBLE) {
pointType = VtkDataTypes.DOUBLE;
}

// Create output points with appropriate precision
const outPts = vtkPoints.newInstance({
dataType: pointType,
});

// Transform points
const inPtsData = inPts.getData();

outPts.setNumberOfPoints(numPoints);
const outPtsData = outPts.getData();

// Transform vectors if present
let outVectors = null;
if (inVectors) {
outVectors = vtkDataArray.newInstance({
name: inVectors.getName(),
numberOfComponents: 3,
size: numPoints * 3,
});
}

// Transform normals if present
let outNormals = null;
if (inNormals) {
outNormals = vtkDataArray.newInstance({
name: inNormals.getName(),
numberOfComponents: 3,
size: numPoints * 3,
});
}

if (inVectors || inNormals) {
model.transform.transformPointsNormalsVectors(
inPts,
outPts,
inNormals,
outNormals,
inVectors,
outVectors
);
} else {
model.transform.transformPoints(inPtsData, outPtsData);
}

// Transform cell vectors if present
let outCellVectors = null;
if (inCellVectors) {
const numCells = inCellVectors.getNumberOfTuples();
outCellVectors = vtkDataArray.newInstance({
name: inCellVectors.getName(),
numberOfComponents: 3,
size: numCells * 3,
});
model.transform.transformVectors(inCellVectors, outCellVectors);
}

// Transform cell normals if present
let outCellNormals = null;
if (inCellNormals) {
const numCells = inCellNormals.getNumberOfTuples();
outCellNormals = vtkDataArray.newInstance({
name: inCellNormals.getName(),
numberOfComponents: 3,
size: numCells * 3,
});
model.transform.transformNormals(inCellNormals, outCellNormals);
}

// Set output data
output.setPoints(outPts);

// Copy cell topology
output.setVerts(input.getVerts());
output.setLines(input.getLines());
output.setPolys(input.getPolys());
output.setStrips(input.getStrips());

// Set transformed point data
if (outNormals) {
outPD.setNormals(outNormals);
outPD.copyFieldOff(outNormals.getName());
}
if (outVectors) {
outPD.setVectors(outVectors);
outPD.copyFieldOff(outVectors.getName());
}

// Set transformed cell data
if (outCellNormals) {
outCD.setNormals(outCellNormals);
outCD.copyFieldOff(outCellNormals.getName());
}
if (outCellVectors) {
outCD.setVectors(outCellVectors);
outCD.copyFieldOff(outCellVectors.getName());
}

// Pass through other data
outPD.passData(inPtData);
outCD.passData(inCellData);

return true;
}

publicAPI.requestData = (inData, outData) => {
const input = inData[0];
const output = outData[0]?.initialize() || vtkPolyData.newInstance();

if (transformPolyData(input, output)) {
outData[0] = output;
} else {
vtkErrorMacro('TransformPolyDataFilter failed to transform input data.');
}
};
}

// ----------------------------------------------------------------------------
// Object factory
// ----------------------------------------------------------------------------

const DEFAULT_VALUES = {
transform: null,
outputPointsPrecision: DesiredOutputPrecision.DEFAULT,
};

// ----------------------------------------------------------------------------

export function extend(publicAPI, model, initialValues = {}) {
Object.assign(model, DEFAULT_VALUES, initialValues);

// Make this a VTK object
macro.obj(publicAPI, model);

// Also make it an algorithm with one input and one output
macro.algo(publicAPI, model, 1, 1);

// Set/Get methods
macro.setGet(publicAPI, model, ['transform', 'outputPointsPrecision']);

// Object specific methods
vtkTransformPolyDataFilter(publicAPI, model);
}

// ----------------------------------------------------------------------------

export const newInstance = macro.newInstance(
extend,
'vtkTransformPolyDataFilter'
);

// ----------------------------------------------------------------------------

export default { newInstance, extend };