Class: Indexer

PD. Indexer

A tool for identifying unique and non-unique entries within a dataset.

This class generates indices into a list of unique objects from a stream of objects that may contain duplicates. This is useful for generating compact indexed meshes from un-indexed data.

The uniqueness or otherwise of an object depends on how its key is generated. The PD.Indexer#addById method uses a numeric id which is assigned to each vector array in order to absolutely identify any that the indexer has seen before. The PD.Indexer#addVectorObject and PD.Indexer#addVectorArray methods uses a string constructed from the axial components of each vector component, whilst the PD.Indexer#addObject method uses a unique hash constructed using JSON.stringify.

For each stream, you should only use one of the above methods for indexing objects.


new Indexer( [decimals])

Creates a new indexer.

Parameters:
Name Type Argument Description
decimals number <optional>

The number of decimal places to use when comparing coordinates (0 or more), defaults to 3.

Author:
  • drajmarsh

Members


:number

decimals

The number of decimal places to use when encoding vectors (0 or more).

Type
  • number

:number

indexId

The current key value when using the PD.Indexer#addById method.

Type
  • number

:object

map

An empty object storing the array index of each unique item.

Type
  • object

:Array

unique

An array of unique items in the data stream.

Type
  • Array

Methods


addById(obj)

Checks for an entry and adds it if it doesn't already exist.

This method adds a new _indexId property to each object and uses that as its unique key. It returns the numeric index of the object in the list.

NOTE: If you use multiple indexers across shared objects, you will need to manually reset the _indexId property of all previously indexed objects back to zero.

Parameters:
Name Type Description
obj object | Array

An object or array to stamp and index.

Returns:

Returns the index of the object within the list.

Type
number

addEntry(key, data)

Returns the index of an existing entry for a key, or adds a new entry.

Parameters:
Name Type Description
key string

The map key to check for.

data any

The object or data to store if no existing entry.

Returns:

Returns the index of the key entry within the list.

Type
number

addObject(obj)

Adds the given object if it doesn't already exist and returns its numeric index in the list.

This method uses a JSON representation of the object as its unique key.

Parameters:
Name Type Description
obj object

An arbitrary object with a viable JSON representation.

Returns:

Returns the index of the object within the list.

Type
number

addVectorArray(vec)

Checks for a vector array and adds it if it doesn't already exist.

This method uses the decimals property of the Indexer to generate each item's unique key. This defaults to 3 decimal places which should typically be fine as all dimensions are assumed to be in millimetres, so this means accuracy is down to the micrometre.

You can set the decimal places to 0 if you want objects to share coordinates that are within one millimeter of each other.

Parameters:
Name Type Description
vec Array.<number>

A vector array in the form [x,y,z].

Returns:

Returns the index of the object within the list.

Type
number

addVectorObject(pnt)

Checks for a vector object and adds it if it doesn't already exist.

This method uses the decimals property of the Indexer to generate each item's unique key. This defaults to 3 decimal places which should typically be fine as all dimensions are assumed to be in millimetres, so this means accuracy is down to the micrometre.

You can set the decimal places to 0 if you want objects to share coordinates that are within one millimeter of each other.

Parameters:
Name Type Description
pnt object

An object with numeric x, y and z properties.

Returns:

Returns the index of the object within the list.

Type
number

clear()

Clears all results from the indexer.

Returns:

Returns this indexer to support method chaining.

Type
PD.Indexer

getBRepVertex(brep, pnt)

Returns a unique vertex index for the given position.

Parameters:
Name Type Description
brep PD.BRep

The boundary representation to add new vertices to.

pnt PD.Utils.VectorObject

An {x,y,z} vector with the position to look for.

Returns:

Returns an existing vertex if the keys match, or adds a new vertex with its key.

Type
PD.Utils.VectorObject

getClipPtKey2D(clipPt)

Returns a key created from the given {X,Y} ClipperLib point object.

This method uses the decimals property of the Indexer to generate each item's unique key. This defaults to 3 decimal places which should typically be fine as all dimensions are assumed to be in millimetres, so this means accuracy is down to the micrometre.

You can set the decimal places to 0 if you want objects to share coordinates that are within one millimeter of each other.

Parameters:
Name Type Description
clipPt object

An object with numeric X and Y properties.

Returns:

Returns the key for this 3D point.

Type
string

getEntry(key, data)

Returns the value of an existing entry for a key, or adds a new entry.

Parameters:
Name Type Description
key string

The map key to check for.

data any

The object or data to store if no existing entry.

Returns:

Returns the data associated with of the key.

Type
any

getEntryIndex(key, data)

Returns the index of an existing entry for a key, or adds a new entry.

Parameters:
Name Type Description
key string

The map key to check for.

data any

The object or data to store if no existing entry.

Returns:

Returns the index of the key entry within the list.

Type
number

getKey2D(X, Y)

Returns a key created from the given 2D x and y position.

This method uses the decimals property of the Indexer to generate a unique unique key for this position. This defaults to 3 decimal places which should typically be fine as all dimensions are assumed to be in millimetres, so this means accuracy is down to the micrometre.

You can set the decimal places to 0 if you want objects to share coordinates that are within one millimeter of each other.

Parameters:
Name Type Description
X number

The 2D X-axis component.

Y number

The 2D Y-axis component.

Returns:

Returns the key for XY position.

Type
string

getMeshVertexIndex(mesh, pnt)

Returns a unique mesh vertex index for the given position.

Parameters:
Name Type Description
mesh PD.PolyMesh

The mesh to add new vertices to.

pnt PD.Utils.VectorObject

An {x,y,z} vector with the position to look for.

Returns:

Returns the index of an existing vertex if the keys match, or adds a new vertex with the key and returns its index.

Type
number

getMeshVertexIndexXY(mesh, vec)

Returns the index of an existing entry for a key, or adds a new entry.

Parameters:
Name Type Description
mesh PD.PolyMesh

The dynamic mesh to add new vertices to.

vec Array

A vector array in the form [x,y,z] with the position to look for.

Returns:

Returns the index of a matching mesh vertex or -1 if the given vector array is not valid.

Type
number

getShellVertex(shell, pnt)

Returns a unique vertex index for the given position.

Parameters:
Name Type Description
shell PD.Shell

The shell to add new vertices to.

pnt PD.Utils.VectorObject

An {x,y,z} vector with the position to look for.

Returns:

Returns an existing vertex if the keys match, or adds a new vertex with its key.

Type
PD.Utils.VectorObject

getUniqueEntry(key, obj)

Returns a matching vector array in the XY plane or adds it.

This method uses the decimals property of the Indexer to generate each item's unique key. This defaults to 3 decimal places which should typically be fine as all dimensions are assumed to be in millimetres, so this means accuracy is down to the micrometre.

You can set the decimal places to 0 if you want objects to share coordinates that are within one millimeter of each other.

Parameters:
Name Type Description
key string

The map key to check for.

obj any

The object or data to store if no existing entry.

Returns:

Returns an existing or new entry matching the key.

Type
any

getUniqueVectorArray(vec)

Returns a matching vector array or adds it if not found.

This method uses the decimals property of the Indexer to generate each item's unique key. This defaults to 3 decimal places which should typically be fine as all dimensions are assumed to be in millimetres, so this means accuracy is down to the micrometre.

You can set the decimal places to 0 if you want objects to share coordinates that are within one millimeter of each other.

Parameters:
Name Type Description
vec Array.<number>

A vector array in the form [x,y,z].

Returns:

Returns a matching vector array from the list.

Type
Array

getUniqueVectorObject(pnt)

Returns a matching vector object or adds it if not found.

This method uses the decimals property of the Indexer to generate each item's unique key. This defaults to 3 decimal places which should typically be fine as all dimensions are assumed to be in millimetres, so this means accuracy is down to the micrometre.

You can set the decimal places to 0 if you want objects to share coordinates that are within one millimeter of each other.

Parameters:
Name Type Description
pnt object

An object with numeric x, y and z properties.

Returns:

Returns a matching vector object within the list.

Type
object

getVectorArrayKey2D(vec)

Returns a key created from the given [x,y] vector array.

This method uses the decimals property of the Indexer to generate each item's unique key. This defaults to 3 decimal places which should typically be fine as all dimensions are assumed to be in millimetres, so this means accuracy is down to the micrometre.

You can set the decimal places to 0 if you want objects to share coordinates that are within one millimeter of each other.

Parameters:
Name Type Description
vec Array

A vector array in the form [x,y].

Returns:

Returns the key for this 2D vector.

Type
string

getVectorArrayKey3D(vec)

Returns a key created from the given [x,y,z] vector array.

This method uses the decimals property of the Indexer to generate each item's unique key. This defaults to 3 decimal places which should typically be fine as all dimensions are assumed to be in millimetres, so this means accuracy is down to the micrometre.

You can set the decimal places to 0 if you want objects to share coordinates that are within one millimeter of each other.

Parameters:
Name Type Description
vec Array

A vector array in the form [x,y[,z]].

Returns:

Returns the key for this 2D vector.

Type
string

getVectorKey2D(pnt)

Returns a key created from the given {x,y} point object.

This method uses the decimals property of the Indexer to generate each item's unique key. This defaults to 3 decimal places which should typically be fine as all dimensions are assumed to be in millimetres, so this means accuracy is down to the micrometre.

You can set the decimal places to 0 if you want objects to share coordinates that are within one millimeter of each other.

Parameters:
Name Type Description
pnt object

An object with numeric x and y properties.

Returns:

Returns the key for this 2D point.

Type
string

getVectorKey3D(pnt)

Returns a key created from the given {x,y,z} point object.

This method uses the decimals property of the Indexer to generate each item's unique key. This defaults to 3 decimal places which should typically be fine as all dimensions are assumed to be in millimetres, so this means accuracy is down to the micrometre.

You can set the decimal places to 0 if you want objects to share coordinates that are within one millimeter of each other.

Parameters:
Name Type Description
pnt object

An object with numeric x, y and z properties.

Returns:

Returns the key for this 3D point.

Type
string