MergePoints

Introduction

vtkMergePoints merge exactly coincident points.

vtkMergePoints is a locator object to quickly locate points in 3D.

Methods

extend

Method used to decorate a given object (publicAPI+model) with vtkMergePoints 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 IMergePointsInitialValues No (default: {})

insertUniquePoint

Insert a point into the merge points structure.
If the point is already present, it returns the existing ID.
Otherwise, it inserts the point and returns a new ID.

Argument Type Required Description
x Vector3 Yes The point to insert as an array of 3 numbers.

Returns

Type Description
IInsertPointResult An object indicating if the point was inserted and its ID.

isInsertedPoint

Check if a point is already inserted in the merge points structure.

Argument Type Required Description
x Vector3 Yes The point to check.

Returns

Type Description
Number The ID of the point if it exists, otherwise -1.

newInstance

Method used to create a new instance of vtkMergePoints.

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

Source

index.d.ts
import { Vector3 } from '../../../types';
import vtkPointLocator, {
IInsertPointResult,
IPointLocatorInitialValues,
} from '../PointLocator';

/**
* Initial values for vtkMergePoints.
*/
export interface IMergePointsInitialValues extends IPointLocatorInitialValues {
bucketSize?: number;
}

export interface vtkMergePoints extends vtkPointLocator {
/**
* Check if a point is already inserted in the merge points structure.
*
* @param {Vector3} x The point to check.
* @returns {Number} The ID of the point if it exists, otherwise -1.
*/
isInsertedPoint(x: Vector3): number;

/**
* Insert a point into the merge points structure.
* If the point is already present, it returns the existing ID.
* Otherwise, it inserts the point and returns a new ID.
*
* @param {Vector3} x The point to insert as an array of 3 numbers.
* @returns {IInsertPointResult} An object indicating if the point was inserted and its ID.
*/
insertUniquePoint(x: Vector3): IInsertPointResult;
}

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

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

/**
* vtkMergePoints merge exactly coincident points.
*
* vtkMergePoints is a locator object to quickly locate points in 3D.
*/
export declare const vtkMergePoints: {
newInstance: typeof newInstance;
extend: typeof extend;
};
export default vtkMergePoints;
index.js
import macro from 'vtk.js/Sources/macros';
import vtkPointLocator from 'vtk.js/Sources/Common/DataModel/PointLocator';

const { vtkErrorMacro } = macro;

/**
* Search for a point in the array using indices from bucketIds
* @param {Number[]} bucketIds - The list of point IDs in the bucket.
* @param {vtkPoints} points - The vtkPoints object containing the points.
* @param {Vector3} x - The point to check.
* @returns {Number} - The ID of the point if it exists, otherwise -1.
*/
function findPointInBucket(bucketIds, points, x) {
const data = points.getData();
for (let i = 0; i < bucketIds.length; ++i) {
const ptId = bucketIds[i];
const idx = ptId * 3;
if (
x[0] === data[idx] &&
x[1] === data[idx + 1] &&
x[2] === data[idx + 2]
) {
return ptId;
}
}
return -1;
}

// ----------------------------------------------------------------------------
// vtkMergePoints methods
// ----------------------------------------------------------------------------

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

/**
* Check if a point is already inserted in the merge points structure.
*
* @param {Vector3} x The point to check.
* @returns {Number} The ID of the point if it exists, otherwise -1.
*/
publicAPI.isInsertedPoint = (x) => {
const idx = publicAPI.getBucketIndex(x);
const bucketIds = model.hashTable.get(idx);
if (bucketIds) {
return findPointInBucket(bucketIds, model.points, x);
}
return -1;
};

/**
* Insert a point into the merge points structure.
* If the point is already present, it returns the existing ID.
* Otherwise, it inserts the point and returns a new ID.
*
* @param {Vector3} x The point to insert as an array of 3 numbers.
* @returns {IInsertPointResult} An object indicating if the point was inserted and its ID.
*/
publicAPI.insertUniquePoint = (x) => {
if (!x || x.length !== 3) {
vtkErrorMacro('Point must be a Vector3.');
return { inserted: false, id: -1 };
}

const idx = publicAPI.getBucketIndex(x);
let bucketIds = model.hashTable.get(idx);
let id = null;

if (bucketIds !== undefined) {
const ptId = findPointInBucket(bucketIds, model.points, x);
if (ptId !== -1) {
id = ptId;
return { inserted: false, id };
}
} else {
bucketIds = [];
model.hashTable.set(idx, bucketIds);
}

// Insert new point
bucketIds.push(model.insertionPointId);
model.points.insertNextPoint(...x);
id = model.insertionPointId++;
return { inserted: true, id };
};
}

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

function defaultValues(initialValues) {
return {
// points: null,
// hashTable: null,
...initialValues,
};
}

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

export function extend(publicAPI, model, initialValues = {}) {
vtkPointLocator.extend(publicAPI, model, defaultValues(initialValues));

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

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

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

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

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

export default { newInstance, extend };