| Title: | Fast C++ Primitives for the 'NeuroAnatomy Toolbox' |
|---|---|
| Description: | Fast functions implemented in C++ via 'Rcpp' to support the 'NeuroAnatomy Toolbox' ('nat') ecosystem. These functions provide large speed-ups for basic manipulation of neuronal skeletons over pure R functions found in the 'nat' package. The expectation is that end users will not use this package directly, but instead the 'nat' package will automatically use routines from this package when it is available to enable large performance gains. |
| Authors: | Gregory Jefferis [aut, cre] (ORCID: <https://orcid.org/0000-0002-0587-9355>), libigl contributors [ctb, cph] (bundled libigl fast winding number code (MPL-2.0); see src/vendor/README.md) |
| Maintainer: | Gregory Jefferis <[email protected]> |
| License: | GPL (>= 3) |
| Version: | 0.3.2.9000 |
| Built: | 2026-09-22 10:18:58 UTC |
| Source: | https://github.com/natverse/natcpp |
Convert physical coordinates to 1d indices into image array
c_coords21dindex(xyz, origin, voxdims, dims, clamp = FALSE)c_coords21dindex(xyz, origin, voxdims, dims, clamp = FALSE)
xyz |
Nx3 matrix or data.frame of physical coordinates |
origin |
Numeric: 3d coordinates of the origin |
voxdims |
Numeric: 3 numbers describing the voxel dimensions |
dims |
Integer dimensions of the 3d image array |
clamp |
Logical: whether or not to clamp values within the pixel boundaries of the image. |
Nx3 integer matrix of pixel coordinates
Turn a segment list into an edgelist suitable for constructing an ngraph
c_EdgeListFromSegList(L)c_EdgeListFromSegList(L)
L |
a list containing integer vectors from |
It is up to the caller to generate the seglist.
Note that isolated points will be dropped since they have no edges.
An integer matrix of N rows and 2 columns
## Not run: library(nat) # make a neuron with multiple subtrees n=prune_vertices(Cell07PNs[[1]], 48L) # Must use flatten=T if including all subtrees sl=as.seglist(n, all = TRUE, flatten = TRUE) c_EdgeListFromSegList(sl) ## End(Not run)## Not run: library(nat) # make a neuron with multiple subtrees n=prune_vertices(Cell07PNs[[1]], 48L) # Must use flatten=T if including all subtrees sl=as.seglist(n, all = TRUE, flatten = TRUE) c_EdgeListFromSegList(sl) ## End(Not run)
Convert physical coordinates to pixel coordinates
c_ijkpos(xyz, origin, voxdims, dims, clamp = FALSE)c_ijkpos(xyz, origin, voxdims, dims, clamp = FALSE)
xyz |
Nx3 matrix of physical coordinates |
origin |
Numeric: 3d coordinates of the origin |
voxdims |
Numeric: 3 numbers describing the voxel dimensions |
dims |
Integer dimensions of the 3d image array |
clamp |
Logical: whether or not to clamp values within the pixel boundaries of the image. |
Nx3 integer matrix of pixel coordinates
A simple function to compute the lengths of the elements of an R list
c_listlengths(L)c_listlengths(L)
L |
a list |
This is equivalent to the base::lengths however it it much
faster for long lists (and somewhat slower for short ones).
An integer vector containing the length of each element of L
Convert a matrix into list of row vectors
c_ListofMatrixRows(object)c_ListofMatrixRows(object)
object |
An integer, numeric, character or logical matrix of N rows and M columns |
Typically this will be for 3D coordinates but there are no limits on row length.
a list containing N vectors of length M corresponding to the rows of
object.
## Not run: library(nat) xyz=xyzmatrix(Cell07PNs) mat2list = function(m) { um=unname(m) lapply(1:nrow(um), function(i) um[i,]) } bench::mark(rcpp=c_ListofMatrixRows(xyz), r=mat2list(xyz)) ## End(Not run)## Not run: library(nat) xyz=xyzmatrix(Cell07PNs) mat2list = function(m) { um=unname(m) lapply(1:nrow(um), function(i) um[i,]) } bench::mark(rcpp=c_ListofMatrixRows(xyz), r=mat2list(xyz)) ## End(Not run)
Robust point-in-mesh test based on the generalised (solid-angle)
winding number. For a closed mesh the winding number is approximately
for interior points and for exterior points, so
abs(w) > 0.5 classifies points as inside. Unlike a closest-point
signed-distance test it does not depend on surface normals and has no
ray-casting tie-breaking, so it does not produce the spurious "outside
point classified as inside" results that normal-based tests can give near
thin protrusions or sharp features.
c_pointsinside( points, vertices, faces, method = c("auto", "bvh", "bruteforce"), threads = NULL, accuracy = 2 )c_pointsinside( points, vertices, faces, method = c("auto", "bvh", "bruteforce"), threads = NULL, accuracy = 2 )
points |
An Nx3 matrix of query point coordinates (or anything
coercible with |
vertices |
An Nx3 matrix of mesh vertex coordinates. |
faces |
An Nx3 integer matrix of 1-based vertex indices (one triangle
per row), e.g. |
method |
Winding-number back end: |
threads |
Number of threads for parallel computation. The default
|
accuracy |
libigl accuracy-scale parameter for |
The mesh should be closed (watertight) and triangular; the result is
independent of face orientation (winding). It is intended as the
accelerated back end for nat::pointsinside().
Two back ends are available, selected by method:
"bruteforce"A self-contained
implementation (P points, F faces), parallelised over points with
RcppThread. No setup cost, so it is fastest for small meshes.
"bvh"libigl's "Fast Winding Numbers for Soups and Clouds"
(Barill et al. 2018): a bounding-volume hierarchy is built once over
the mesh and each query point is then evaluated in , so
it scales to millions of points on meshes of tens of thousands of
faces. accuracy tunes the multipole approximation.
"auto" (the default) picks "bvh" only for large meshes queried
by enough points to amortise building the hierarchy, and "bruteforce"
otherwise (so a simple mesh such as a cuboid always uses brute force). The
two back ends agree to within the winding-number tolerance.
A logical vector of length nrow(points) (TRUE = inside).
# tetrahedron V <- rbind(c(0,0,0), c(1,0,0), c(0,1,0), c(0,0,1)) F <- rbind(c(1,3,2), c(1,2,4), c(1,4,3), c(2,3,4)) c_pointsinside(rbind(c(.2,.2,.2), c(2,2,2)), V, F) # TRUE FALSE# tetrahedron V <- rbind(c(0,0,0), c(1,0,0), c(0,1,0), c(0,0,1)) F <- rbind(c(1,3,2), c(1,2,4), c(1,4,3), c(2,3,4)) c_pointsinside(rbind(c(.2,.2,.2), c(2,2,2)), V, F) # TRUE FALSE
c_seglengths computes the summed segment length equivalent
to nat::seglengths(sumsegment = T)
c_total_cable computes the summed total cable for a
whole neuron. It's intended use is the nat::summary.neuron function.
c_seglengths(sl, x, y, z) c_total_cable(sl, x, y, z)c_seglengths(sl, x, y, z) c_total_cable(sl, x, y, z)
sl |
A |
x, y, z
|
Numeric vectors with 3D coordinate data (which could be columns from a data frame) |
Find 1D index given n-dimensional indices
c_sub2ind(dims, indices)c_sub2ind(dims, indices)
dims |
Integer dimensions of the array (usually 3d) |
indices |
Nx3 integer matrix of pixel coordinates |
numeric vector of linear indices into the array
c_topntail returns an 2xN matrix containing the start and end
of each of the vectors in the input list. Length 0 vectors are ignored, while
length 1 vectors are duplicated
For c_topntail_list, a list of the same length as
L having the same elements when their length is <=2 or
the first and last elements when length>2.
c_topntail(L) c_topntail_list(L)c_topntail(L) c_topntail_list(L)
L |
a list containing integer vectors, typically a |
For c_topntail an integer matrix. For c_topntail_list
a list.
Compute the full weighted Jaccard similarity matrix for a
dgCMatrix, returning a dense matrix (or a
dist-layout vector).
c_weighted_jaccard_dense( x, transpose = FALSE, threads = NULL, triangle = FALSE, distance = FALSE )c_weighted_jaccard_dense( x, transpose = FALSE, threads = NULL, triangle = FALSE, distance = FALSE )
x |
A dgCMatrix (sparse column-compressed matrix) |
transpose |
If |
threads |
Number of threads for parallel computation. The default
|
triangle |
If |
distance |
If |
Uses an adaptive dense accumulation strategy: for small output matrices a feature-oriented loop, switching to a column-oriented loop for larger outputs for better cache performance.
A dense numeric similarity matrix, or a numeric vector in
dist layout when triangle = TRUE.
c_weighted_jaccard_sparse for the sparse equivalent
## Not run: library(Matrix) m <- sparseMatrix(i = c(1,2,1,2,3,3), j = c(1,1,2,2,2,3), x = c(4,2,1,3,3,1), dims = c(3,3)) c_weighted_jaccard_dense(m) ## End(Not run)## Not run: library(Matrix) m <- sparseMatrix(i = c(1,2,1,2,3,3), j = c(1,1,2,2,2,3), x = c(4,2,1,3,3,1), dims = c(3,3)) c_weighted_jaccard_dense(m) ## End(Not run)
Compute the weighted Jaccard similarity matrix for a
dgCMatrix, returning a
sparse result. Uses weighted_jaccard_sparse_fill to compute min-sums
only for column (or row) pairs that share at least one non-zero feature, then
normalises to similarity. Only the upper triangle is computed, taking
advantage of the symmetry of the Jaccard index.
c_weighted_jaccard_sparse( x, transpose = FALSE, display_progress = TRUE, threads = NULL, triangle = FALSE, distance = FALSE )c_weighted_jaccard_sparse( x, transpose = FALSE, display_progress = TRUE, threads = NULL, triangle = FALSE, distance = FALSE )
x |
A dgCMatrix (sparse column-compressed matrix) |
transpose |
If |
display_progress |
Whether to show a text progress bar (default
|
threads |
Number of threads for parallel computation. The default
|
triangle |
If |
distance |
If |
A sparse similarity (or distance) matrix: dsCMatrix when
triangle = TRUE, dgCMatrix otherwise.
c_weighted_jaccard_dense for the dense equivalent
## Not run: library(Matrix) m <- sparseMatrix(i = c(1,2,1,2,3,3), j = c(1,1,2,2,2,3), x = c(4,2,1,3,3,1), dims = c(3,3)) c_weighted_jaccard_sparse(m) ## End(Not run)## Not run: library(Matrix) m <- sparseMatrix(i = c(1,2,1,2,3,3), j = c(1,1,2,2,2,3), x = c(4,2,1,3,3,1), dims = c(3,3)) c_weighted_jaccard_sparse(m) ## End(Not run)