C++ reference

include/meshsearch/MeshGrid.h is the specification: it declares the interface and states the guarantees it makes, and this page is generated from it. Include it as #include "meshsearch/MeshGrid.h" and link the CMake target meshsearch::meshsearch, which also provides the include path and requires C++17. The guide explains how the members work together.

The grid

class MeshGrid

Exact neighbour searches in a three-dimensional point set, over a uniform cubic mesh.

Objects are identified by the index of their coordinates as passed to the constructor. Indices are stable: removeObject leaves an index permanently invalid, addObject issues a new one, and indices are never reused or renumbered. Every member taking an index raises IndexError on an index that is out of range or removed, except isAlive, which answers false.

Const members may be called concurrently on the same grid. Non-const members may not.

Coordinates, cellsize and radii must all be in the same unit; none is imposed. Every member taking a point, except contains, raises Error if the point lies outside the box.

Distances are Euclidean, with the minimum image on periodic axes: along a periodic axis of period L the displacement is taken in [-L/2, L/2), so each object is seen once, at its nearest image. Distances are compared through their squares, computed in double precision as dx*dx+dy*dy+dz*dz. Ties between equal distances are broken by ascending index. No result depends on how objects fall into cells.

Any member that allocates may throw std::bad_alloc, which is not translated into Error. A constructor that throws builds nothing, and addObject leaves the grid as it was if it throws.

The class is final, and everything below the public members is private. Copies are deep, apart from a table that depends only on the geometry and is shared. Moves transfer the storage without copying it; a moved-from grid may only be destroyed or assigned to.

The same input (coordinates in the same order, cellsize, options, the same sequence of addObject and removeObject, the same queries) gives the same results in the same order, on Linux and macOS, on x86-64 and ARM, with any number of threads calling the grid, and with any number of Workers in a batch; a copy answers as the original does. Which order the objects take within a cell, and so the order of the results of closeObjects and get_objectsInCell, is not part of the interface and may change between versions.

Public Functions

MeshGrid() = default

Empty grid. Every member raises or returns empty until it is assigned to.

MeshGrid(const std::vector<double> &X, const std::vector<double> &Y, const std::vector<double> &Z, double cellsize, const Options &opt)

Build the grid.

Each non-periodic side of the box is grown symmetrically to a whole number of cells, always containing the limits; get_lims returns the grown box. An axis of zero extent gets one cell. A periodic axis has period hi-lo and its box is exactly the limits; coordinates are not wrapped. Every check is made before anything is allocated.

Parameters:
  • X, Y, Z – coordinates, all of the same length, copied into the grid

  • cellsize – cell side, finite and positive; on a periodic grid an upper bound, see get_cellsize

  • opt – the box and the periodic axes; see Options

Throws:
  • Error – if the vectors differ in length or hold more than 4294967295 points; if a coordinate is not finite or lies outside the limits; if cellsize or the limits are invalid; if there are no points and no limits; if the grid would be too large to address

  • Error – on a periodic axis, if the limits are unset, if the period is below 2^-1000 or the limits too large to shift by it, or if the period is not an integer multiple of the shortest one, within a relative 1e-12

MeshGrid(const std::vector<double> &X, const std::vector<double> &Y, const std::vector<double> &Z, double cellsize)

As above, with default Options: the box fitted to the data, no periodic axis.

MeshGrid(const double *X, const double *Y, const double *Z, std::size_t n, double cellsize, const Options &opt)

Build the grid from three arrays of coordinates.

For coordinates held in arrays that are not std::vector, such as a buffer from another library, without copying them into vectors first. Point i is (X[i], Y[i], Z[i]) and is object i; the grid is the one the vector form builds from the same values, with the same results in the same order.

Parameters:
  • X, Y, Z – arrays of n coordinates each, read during the call only: the grid copies them and keeps no reference to them

  • n – number of points; with 0, the pointers may be null

  • cellsize – as for the vector form

  • opt – as for the vector form

Throws:

Error – if n > 0 and a pointer is null; otherwise as the vector form

MeshGrid(const double *X, const double *Y, const double *Z, std::size_t n, double cellsize)

As above, with default Options: the box fitted to the data, no periodic axis.

MeshGrid(const double *XYZ, std::size_t n, double cellsize, const Options &opt)

Build the grid from one array of interleaved coordinates.

For points stored as rows of three, such as a C-ordered n x 3 array. Point i is (XYZ[3i], XYZ[3i+1], XYZ[3i+2]) and is object i; the grid is the one the vector form builds from the same values, with the same results in the same order.

Parameters:
  • XYZ – array of 3*n coordinates, x0 y0 z0 x1 y1 z1 …, read during the call only: the grid copies them and keeps no reference to them

  • n – number of points; with 0, the pointer may be null

  • cellsize – as for the vector form

  • opt – as for the vector form

Throws:

Error – if n > 0 and the pointer is null; otherwise as the vector form, a non-finite coordinate being named by its axis and its point

MeshGrid(const double *XYZ, std::size_t n, double cellsize)

As above, with default Options: the box fitted to the data, no periodic axis.

unsigned int get_nObjects() const

Number of objects in the grid, excluding removed ones.

bool isAlive(unsigned int index) const

True if index names an object still in the grid; false if it is out of range or removed. The one member taking an index that does not raise.

std::vector<std::uint8_t> isAlive(const std::vector<unsigned int> &indices) const

isAlive for each index, in order: 1 or 0.

void isAlive(const std::vector<unsigned int> &indices, std::vector<std::uint8_t> &out) const

As above, into out, which is cleared first and keeps its capacity.

bool contains(double X, double Y, double Z) const

True if a query at the point is accepted, that is, if the point lies in the box: the test every member taking a point applies. False for a NaN coordinate, and for every point on a default-constructed grid.

std::vector<std::uint8_t> contains(const std::vector<double> &X, const std::vector<double> &Y, const std::vector<double> &Z) const

contains for each point (X[i], Y[i], Z[i]), in order: 1 or 0.

Throws:

Error – if the vectors differ in length

void contains(const std::vector<double> &X, const std::vector<double> &Y, const std::vector<double> &Z, std::vector<std::uint8_t> &out) const

As above, into out, which is cleared first and keeps its capacity.

std::array<std::array<double, 2>, 3> get_lims() const

The box: grown to whole cells on a non-periodic axis, exactly the limits given on a periodic one. All zeros on a default-constructed grid.

std::array<unsigned int, 3> get_nCells() const

Cells per axis.

double get_cellsize() const

Cell side. The cellsize given, unless an axis is periodic: then the largest side not above it that fits a whole number of times into the shortest period.

std::array<bool, 3> get_periodic() const

Per axis, whether the box is periodic.

unsigned int nearestObject(double X, double Y, double Z) const

Index of the object nearest to a point.

Nothing is excluded: an object lying on the point is at distance 0 and is the answer.

Throws:

Error – if the point is outside the box, or if the grid holds no objects

unsigned int nearestObject(unsigned int index) const

Index of the object nearest to object index, which is excluded.

index is excluded by identity, not by distance: another object coincident with it is at distance 0 and may be the answer.

Throws:
  • IndexError – if index is out of range or removed

  • Error – if no other object remains

std::vector<unsigned int> nearestObject(const std::vector<double> &X, const std::vector<double> &Y, const std::vector<double> &Z, Workers workers = Workers()) const

nearestObject for each point (X[i], Y[i], Z[i]), in order.

On a throw, the queries already answered are lost. Every batch member takes workers last, one by default, and its results do not depend on it. Both hold for every batch member.

Parameters:
  • X, Y, Z – the points, all of the same length

  • workers – threads the batch may use; see Workers

Throws:

Error – if the vectors differ in length, if a point is outside the box, or if the grid holds no objects, even for no point

void nearestObject(const std::vector<double> &X, const std::vector<double> &Y, const std::vector<double> &Z, std::vector<unsigned int> &out, Workers workers = Workers()) const

As above, into out, which is cleared first and keeps its capacity; its content is unspecified if the call throws, and no other thread may use it during the call. This holds for every member taking out, except an index batch answered into its own index container: the answer is then the one a separate container would receive, and the container does not keep its capacity. Every index batch that returns indices honours this.

std::vector<unsigned int> nearestObject(const std::vector<unsigned int> &indices, Workers workers = Workers()) const

nearestObject for each object of indices, in order.

Throws:
  • IndexError – if an index is out of range or removed

  • Error – if fewer than two objects remain, even for no index

void nearestObject(const std::vector<unsigned int> &indices, std::vector<unsigned int> &out, Workers workers = Workers()) const

As above, into out, which may be indices itself; it then does not keep its capacity.

std::vector<unsigned int> nearestObjects(unsigned int N, double X, double Y, double Z) const

The N objects nearest to a point, nearest first; ties by ascending index.

Nothing is excluded: an object lying on the point is at distance 0 and is among them. The result is the first N objects ordered by (distance, index), so a tie at the last place keeps the lower indices.

Parameters:
  • N – at most get_nObjects(); 0 returns an empty vector

  • X, Y, Z – the point, which must lie inside the box

Throws:

Error – if the point is outside the box, or if N is too large

std::vector<unsigned int> nearestObjects(unsigned int N, unsigned int index) const

The N objects nearest to object index, which is excluded; nearest first; ties by ascending index.

index is excluded by identity, not by distance: another object coincident with it is at distance 0 and is among them. The result is the first N objects ordered by (distance, index), so a tie at the last place keeps the lower indices.

Parameters:
  • N – at most get_nObjects()-1; 0 returns an empty vector

  • index – the object to measure from, itself excluded

Throws:
  • IndexError – if index is out of range or removed

  • Error – if N is too large

void nearestObjects(unsigned int N, double X, double Y, double Z, std::vector<unsigned int> &out) const

The point overload of nearestObjects, into out, which is cleared first.

void nearestObjects(unsigned int N, unsigned int index, std::vector<unsigned int> &out) const

The index overload of nearestObjects, into out, which is cleared first.

std::vector<unsigned int> nearestObjects(unsigned int N, const std::vector<double> &X, const std::vector<double> &Y, const std::vector<double> &Z, Workers workers = Workers()) const

nearestObjects for each point (X[i], Y[i], Z[i]).

Throws:

Error – if the vectors differ in length, if a point is outside the box, if N exceeds get_nObjects(), even for no point, or if the batch result is too large

Returns:

n*N indices, query i’s N at [i*N, (i+1)*N), each in the order of the single query

void nearestObjects(unsigned int N, const std::vector<double> &X, const std::vector<double> &Y, const std::vector<double> &Z, std::vector<unsigned int> &out, Workers workers = Workers()) const

The point batch of nearestObjects, into out.

std::vector<unsigned int> nearestObjects(unsigned int N, const std::vector<unsigned int> &indices, Workers workers = Workers()) const

nearestObjects for each object of indices.

Throws:
  • IndexError – if an index is out of range or removed

  • Error – if N is not below get_nObjects(), even for no index, or if the batch result is too large

Returns:

n*N indices, query i’s N at [i*N, (i+1)*N)

void nearestObjects(unsigned int N, const std::vector<unsigned int> &indices, std::vector<unsigned int> &out, Workers workers = Workers()) const

As above, into out, which may be indices itself; it then does not keep its capacity.

std::vector<unsigned int> closeObjects(double X, double Y, double Z, double Rmax, double Rmin = 0.) const

Objects at distance d from a point, with Rmin <= d <= Rmax.

The interval is closed at both ends, so a ball holds every object within Rmax of the point and an object at exactly either end is returned. Rmin == Rmax selects the objects at exactly that distance.

Nothing is excluded by identity: an object lying on the point is at d = 0 and is returned.

A shell reaching past the box is truncated, not an error, except on a periodic axis, where it wraps around and each object is counted once. An infinite Rmax is accepted and selects every object from Rmin outwards.

Parameters:
  • X, Y, Z – the point, which must lie inside the box

  • Rmax – outer radius, inclusive; +inf is accepted

  • Rmin – inner radius, inclusive; 0 by default, giving a ball

Throws:

Error – if the point is outside the box, if a radius is negative or NaN, or if Rmin exceeds Rmax

Returns:

the indices; their order is not meaningful (see the class)

std::vector<unsigned int> closeObjects(unsigned int index, double Rmax, double Rmin = 0.) const

Objects at distance d from object index, with Rmin <= d <= Rmax. The object itself is excluded.

The interval is the point overload’s, closed at both ends. index is excluded by identity, not by distance, so another object coincident with it is at d = 0 and is returned.

An infinite Rmax is accepted and selects every object from Rmin outwards.

Parameters:
  • index – the object to measure from, itself excluded

  • Rmax – outer radius, inclusive; +inf is accepted

  • Rmin – inner radius, inclusive; 0 by default, giving a ball

Throws:
  • IndexError – if index is out of range or removed

  • Error – if a radius is negative or NaN, or if Rmin exceeds Rmax

Returns:

the indices; their order is not meaningful (see the class)

void closeObjects(double X, double Y, double Z, double Rmax, double Rmin, std::vector<unsigned int> &out) const

The point overload of closeObjects, into out, which is cleared first.

void closeObjects(double X, double Y, double Z, double Rmax, std::vector<unsigned int> &out) const

As above, with Rmin = 0.

void closeObjects(unsigned int index, double Rmax, double Rmin, std::vector<unsigned int> &out) const

The index overload of closeObjects, into out, which is cleared first.

void closeObjects(unsigned int index, double Rmax, std::vector<unsigned int> &out) const

As above, with Rmin = 0.

Batch closeObjects(const std::vector<double> &X, const std::vector<double> &Y, const std::vector<double> &Z, double Rmax, double Rmin = 0., Workers workers = Workers()) const

closeObjects for each point (X[i], Y[i], Z[i]), with the same radii.

Throws:

Error – if the vectors differ in length, if a point is outside the box, or on the radii as closeObjects, even for no point

Returns:

each query’s indices, in the order the single query returns them

void closeObjects(const std::vector<double> &X, const std::vector<double> &Y, const std::vector<double> &Z, double Rmax, double Rmin, Batch &out, Workers workers = Workers()) const

The point batch of closeObjects, into out, whose indices and offsets are both replaced.

void closeObjects(const std::vector<double> &X, const std::vector<double> &Y, const std::vector<double> &Z, double Rmax, Batch &out, Workers workers = Workers()) const

As above, with Rmin = 0.

Batch closeObjects(const std::vector<unsigned int> &indices, double Rmax, double Rmin = 0., Workers workers = Workers()) const

closeObjects for each object of indices, with the same radii; each query excludes its own object.

Throws:
  • IndexError – if an index is out of range or removed

  • Error – on the radii as closeObjects, even for no index

void closeObjects(const std::vector<unsigned int> &indices, double Rmax, double Rmin, Batch &out, Workers workers = Workers()) const

The index batch of closeObjects, into out, whose indices and offsets are both replaced. indices may be out.indices itself; the container then does not keep its capacity.

void closeObjects(const std::vector<unsigned int> &indices, double Rmax, Batch &out, Workers workers = Workers()) const

As above, with Rmin = 0.

std::size_t countObjects(double X, double Y, double Z, double Rmax, double Rmin = 0.) const

Number of objects closeObjects would return for the point.

Throws:

Error – as closeObjects

std::size_t countObjects(unsigned int index, double Rmax, double Rmin = 0.) const

Number of objects closeObjects would return for object index, which is excluded.

Throws:
  • IndexError – if index is out of range or removed

  • Error – on the radii as closeObjects

std::vector<std::uint64_t> countObjects(const std::vector<double> &X, const std::vector<double> &Y, const std::vector<double> &Z, double Rmax, double Rmin = 0., Workers workers = Workers()) const

countObjects for each point (X[i], Y[i], Z[i]), in order. Throws as the closeObjects batch.

void countObjects(const std::vector<double> &X, const std::vector<double> &Y, const std::vector<double> &Z, double Rmax, double Rmin, std::vector<std::uint64_t> &out, Workers workers = Workers()) const

The point batch of countObjects, into out.

void countObjects(const std::vector<double> &X, const std::vector<double> &Y, const std::vector<double> &Z, double Rmax, std::vector<std::uint64_t> &out, Workers workers = Workers()) const

As above, with Rmin = 0.

std::vector<std::uint64_t> countObjects(const std::vector<unsigned int> &indices, double Rmax, double Rmin = 0., Workers workers = Workers()) const

countObjects for each object of indices, in order. Throws as the closeObjects batch.

void countObjects(const std::vector<unsigned int> &indices, double Rmax, double Rmin, std::vector<std::uint64_t> &out, Workers workers = Workers()) const

The index batch of countObjects, into out.

void countObjects(const std::vector<unsigned int> &indices, double Rmax, std::vector<std::uint64_t> &out, Workers workers = Workers()) const

As above, with Rmin = 0.

std::size_t get_cell(double X, double Y, double Z) const

Linear index of the cell containing a point: (ix*ny+iy)*nz+iz, with {ix,iy,iz} from get_cellCoords and {nx,ny,nz} from get_nCells.

A point within rounding of a cell face may be assigned to the cell on either side of it. Every member assigns a given point to the same cell, so an object is always among the objects in the cell of its own coordinates.

Throws:

Error – if the point is outside the box

std::array<unsigned int, 3> get_cellCoords(double X, double Y, double Z) const

Cell coordinates {ix, iy, iz} of the cell containing a point.

Throws:

Error – if the point is outside the box

std::vector<unsigned int> get_objectsInCell(double X, double Y, double Z) const

Objects in the cell containing a point.

Throws:

Error – if the point is outside the box

void removeObject(unsigned int index)

Remove an object from the grid.

Its coordinates are no longer kept, and its index stays invalid for the lifetime of the grid. The memory is not returned: the grid does not shrink.

Throws:

IndexError – if index is out of range or already removed

unsigned int addObject(double X, double Y, double Z)

Add an object to the grid.

The box is not refitted. Indices of removed objects are not reused, and the memory of a grid that has grown is not reclaimed. To add many objects, build a new grid: additions do not compact the storage.

Throws:

Error – if the point is outside the box, or if 4294967295 indices have already been issued

Returns:

its index, one past the highest ever issued

struct Options

Construction options.

Public Members

std::optional<std::array<std::array<double, 2>, 3>> limits

Box as {{xlo,xhi},{ylo,yhi},{zlo,zhi}}, finite, with lo <= hi on each axis, containing every coordinate. If unset, the box is fitted to the data with a small margin, and a query point outside the data’s range may raise Error: set it whenever queries can fall outside the data. Required when any axis is periodic, or when there are no points.

std::array<bool, 3> periodic = {}

Per axis, whether distances wrap around the box. All false by default.

struct Batch

Results of a batch of closeObjects queries, in query order.

Public Members

std::vector<unsigned int> indices

The indices found by every query, concatenated.

std::vector<std::uint64_t> offsets

One more entry than queries, starting at 0: query i found indices[offsets[i]] up to, excluding, indices[offsets[i+1]].

Workers

struct Workers

Number of threads a batch query may use: the last argument of every batch form, after Rmin where there is one.

Results do not depend on it: any number of workers gives the same indices and counts, in the same order, bit for bit, and the same error on invalid input, which is checked before any worker starts.

One, the default, runs the batch in the calling thread. -1 means every core available to the process: on Linux the cores of its CPU affinity set, which is what a cluster job is given, elsewhere std::thread::hardware_concurrency(). More workers than cores are allowed but gain nothing. A batch too small to share uses fewer threads, or one. Threads are started for each call, the calling thread being one of them; if one cannot be started, the batch continues on fewer, with the same results. If one throws, such as std::bad_alloc, the others stop and the exception reaches the caller.

-1 suits a large batch on an otherwise idle process; one suits a caller that already runs batches from threads of its own.

Public Functions

Workers() = default

One worker: the batch runs in the calling thread.

explicit Workers(int count)
Parameters:

count – at least 1, or -1 for every core available to the process

Throws:

Error – if count is 0 or below -1

inline int get_count() const

The count as given: -1, or at least 1.

Errors

class Error : public std::runtime_error

Thrown on invalid input: a point outside the box, an invalid argument, or a query the grid cannot answer.

Subclassed by meshsearch::IndexError

Public Functions

explicit Error(const std::string &what)
Parameters:

what – the message, returned by what()

class IndexError : public meshsearch::Error

Thrown on an index that is out of range or names a removed object.

Public Functions

explicit IndexError(const std::string &what)
Parameters:

what – the message, returned by what()

Running out of memory is not translated: any member that allocates may throw std::bad_alloc.

The version

The header meshsearch/Version.h is generated by CMake when the library is built, and installed with MeshGrid.h. It defines four macros:

MESHSEARCH_VERSION

The version as a string literal, such as "1.0.0".

MESHSEARCH_VERSION_MAJOR, MESHSEARCH_VERSION_MINOR, MESHSEARCH_VERSION_PATCH

Its three parts, as integer literals, for use in #if.