EdgeLocator

Introduction

vtkEdgeLocator

Methods

initialize

Remove all the edges previously added.

newInstance

Method use to create a new instance of vtkEdgeLocator

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

Source

index.d.ts
import { Nullable } from '../../../types';

/**
*
*/
export interface IEdgeLocatorInitialValues {
oriented?: boolean;
}

export interface IEdge<T = unknown> {
key: number;
edgeId: number;
value?: T;
}

export interface vtkEdgeLocator {
/**
* Remove all the edges previously added.
*/
initialize(): void;

/**
* Returns the inserted edge or null if no edge was inserted.
* @param {Number} pointId0 Edge first point id
* @param {Number} pointId1 Edge last point id
* @return {IEdge|null} an edge object ({ key, edgeId, value }) or null
*/
isInsertedEdge<T = unknown>(
pointId0: number,
pointId1: number
): Nullable<IEdge<T>>;

/**
* Insert edge if it does not already exist.
* Returns the existing or newly inserted edge.
*
* @param {Number} pointId0 Edge first point id
* @param {Number} pointId1 Edge last point id
* @param {unknown} value Optional value option
* @return {IEdge|null} an edge object ({ key, edgeId, value }) or null
* @see insertEdge()
* @see isInsertedEdge()
*/
insertUniqueEdge<T>(pointId0: number, pointId1: number, value?: T): IEdge<T>;

/**
* Insert edge. If the edge already exists, it is overwritten by this
* new edge. You may verify that the edge did not previously exist with
* `isInsertedEdge()`.
* Returns the newly inserted edge.
* @param {Number} pointId0 Edge first point id
* @param {Number} pointId1 Edge last point id
* @param {unknown} value Optional value option
* @return {Edge|null} an edge object ({ key, edgeId, value }) or null
* @see isInsertedEdge
* @see insertUniqueEdge
*/
insertEdge<T>(pointId0: number, pointId1: number, value?: T): IEdge<T>;
}

// ----------------------------------------------------------------------------
// Static API
// ----------------------------------------------------------------------------

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

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

/**
* vtkEdgeLocator
*/
export declare const vtkEdgeLocator: {
newInstance: typeof newInstance;
};

export default vtkEdgeLocator;
index.js
class EdgeLocator {
constructor(oriented = false) {
this.oriented = oriented;
this.edgeMap = new Map();
}

initialize() {
this.edgeMap.clear();
}

computeEdgeKey(pointId0, pointId1) {
return this.oriented || pointId0 < pointId1
? // Cantor pairing function:
0.5 * (pointId0 * pointId1) * (pointId0 * pointId1 + 1) + pointId1
: 0.5 * (pointId1 * pointId0) * (pointId1 * pointId0 + 1) + pointId0;
}

insertUniqueEdge(pointId0, pointId1, newEdgeValue) {
// Generate a unique key
const key = this.computeEdgeKey(pointId0, pointId1);
let node = this.edgeMap.get(key);
if (!node) {
// Didn't find key, so add a new edge entry
node = { key, edgeId: this.edgeMap.size, value: newEdgeValue };
this.edgeMap.set(key, node);
}
return node;
}

insertEdge(pointId0, pointId1, newEdgeValue) {
// Generate a unique key
const key = this.computeEdgeKey(pointId0, pointId1);
const node = { key, edgeId: this.edgeMap.size, value: newEdgeValue };
this.edgeMap.set(key, node);
return node;
}

isInsertedEdge(pointId0, pointId1) {
const key = this.computeEdgeKey(pointId0, pointId1);
return this.edgeMap.get(key);
}

static getEdgePointIds(node) {
const n = 0.5 * (-1 + Math.sqrt(8 * node.key + 1));
const pointId0 = node.key - 0.5 * (n + 1) * n;
const pointId1 = n - pointId0;
return [pointId0, pointId1];
}
}

function newInstance(initialValues = {}) {
return new EdgeLocator(initialValues.oriented);
}

export default { newInstance };