# Generated by using Rcpp::compileAttributes() -> do not edit by hand
# Generator token: 10BE3573-1514-4C36-9D1C-5A225CD40393

#' Compute summed segment lengths or total cable
#' @description \code{c_seglengths} computes the summed segment length equivalent
#'  to \code{nat::seglengths(sumsegment = T)}
#' @param sl A \code{seglist} with 1-indices into vectors x,y,z
#' @param x,y,z Numeric vectors with 3D coordinate data
#' (which could be columns from a data frame)
#' @export
c_seglengths <- function(sl, x, y, z) {
    .Call(`_natcpp_c_seglengths`, sl, x, y, z)
}

#' @description \code{c_total_cable} computes the summed total cable for a
#' whole neuron. It's intended use is the \code{nat::summary.neuron} function.
#' @rdname c_seglengths
#' @export
c_total_cable <- function(sl, x, y, z) {
    .Call(`_natcpp_c_total_cable`, sl, x, y, z)
}

#' Convert physical coordinates to pixel coordinates
#'
#' @param xyz Nx3 matrix of physical coordinates
#' @param dims Integer dimensions of the 3d image array
#' @param origin Numeric: 3d coordinates of the origin
#' @param voxdims Numeric: 3 numbers describing the voxel dimensions
#' @param clamp Logical: whether or not to clamp values within the pixel
#'   boundaries of the image.
#' @return Nx3 integer matrix of pixel coordinates
#' @export
c_ijkpos <- function(xyz, origin, voxdims, dims, clamp = FALSE) {
    .Call(`_natcpp_c_ijkpos`, xyz, origin, voxdims, dims, clamp)
}

#' Find 1D index given n-dimensional indices
#' @param dims Integer dimensions of the array (usually 3d)
#' @param indices Nx3 integer matrix of pixel coordinates
#' @return numeric vector of linear indices into the array
#' @export
c_sub2ind <- function(dims, indices) {
    .Call(`_natcpp_c_sub2ind`, dims, indices)
}

#' Convert physical coordinates to 1d indices into image array
#'
#' @param xyz Nx3 matrix or data.frame of physical coordinates
#' @param dims Integer dimensions of the 3d image array
#' @param origin Numeric: 3d coordinates of the origin
#' @param voxdims Numeric: 3 numbers describing the voxel dimensions
#' @param clamp Logical: whether or not to clamp values within the pixel
#'   boundaries of the image.
#' @return Nx3 integer matrix of pixel coordinates
#' @export
c_coords21dindex <- function(xyz, origin, voxdims, dims, clamp = FALSE) {
    .Call(`_natcpp_c_coords21dindex`, xyz, origin, voxdims, dims, clamp)
}

#' Convert a matrix into list of row vectors
#'
#' @details Typically this will be for 3D coordinates but there are no limits
#'   on row length.
#' @param object An integer, numeric, character or logical matrix of N rows and
#'   M columns
#' @return a list containing N vectors of length M corresponding to the rows of
#'   \code{object}.
#' @export
#' @examples
#' \dontrun{
#' 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))
#' }
#' @export
c_ListofMatrixRows <- function(object) {
    .Call(`_natcpp_c_ListofMatrixRows`, object)
}

#' A simple function to compute the lengths of the elements of an R list
#'
#' @details This is equivalent to the \code{base::lengths} however it it much
#' faster for long lists (and somewhat slower for short ones).
#' @param L a list
#' @return An integer vector containing the length of each element of \code{L}
#' @export
c_listlengths <- function(L) {
    .Call(`_natcpp_c_listlengths`, L)
}

#' Find the first and last elements of all vectors in a list
#'
#' @description \code{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
#'
#' @param L a list containing integer vectors, typically a \code{seglist}
#' @return For \code{c_topntail} an integer \code{matrix}. For \code{c_topntail_list}
#'   a \code{list}.
#' @export
c_topntail <- function(L) {
    .Call(`_natcpp_c_topntail`, L)
}

#' @description For \code{c_topntail_list}, a list of the same length as
#' \code{L} having the same elements when their length is <=2 or
#' the first and last elements when length>2.
#' @export
#' @rdname c_topntail
c_topntail_list <- function(L) {
    .Call(`_natcpp_c_topntail_list`, L)
}

#' Turn a segment list into an edgelist suitable for constructing an ngraph
#' @details It is up to the caller to generate the \code{seglist}.
#' Note that isolated points will be dropped since they have no edges.
#' @param L a list containing integer vectors from \code{as.seglist}
#' @return An integer matrix of N rows and 2 columns
#' @export
#' @examples
#'
#' \dontrun{
#' 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)
#' }
c_EdgeListFromSegList <- function(L) {
    .Call(`_natcpp_c_EdgeListFromSegList`, L)
}

#' @noRd
weighted_jaccard_sparse_fill <- function(x, pattern, transpose = FALSE, display_progress = TRUE, threads = 4L) {
    .Call(`_natcpp_weighted_jaccard_sparse_fill`, x, pattern, transpose, display_progress, threads)
}

#' Dense weighted Jaccard similarity via C++
#'
#' Compute the full weighted Jaccard similarity matrix for a
#' \link[Matrix:dgCMatrix-class]{dgCMatrix} using
#' an adaptive dense accumulation strategy. For small output matrices, uses a
#' feature-oriented loop; for large outputs, switches to a column-oriented
#' loop for better cache performance.
#'
#' @param x A \link[Matrix:dgCMatrix-class]{dgCMatrix} (sparse column-compressed matrix)
#' @param transpose If \code{FALSE}, compare columns; if \code{TRUE}, compare
#'   rows
#' @param threads Number of threads (default 4). Set to 0 for all cores.
#' @param triangle If \code{TRUE}, return only the lower triangle as a flat
#'   numeric vector in \code{\link{dist}} layout. If \code{FALSE} (default),
#'   return a full square matrix.
#' @param distance If \code{TRUE}, return distance (\code{1 - similarity})
#'   instead of similarity. Default \code{FALSE}.
#' @return A dense numeric similarity matrix, or a numeric vector in
#'   \code{\link{dist}} layout when \code{triangle = TRUE}.
#' @export
c_weighted_jaccard_dense <- function(x, transpose = FALSE, threads = 4L, triangle = FALSE, distance = FALSE) {
    .Call(`_natcpp_c_weighted_jaccard_dense`, x, transpose, threads, triangle, distance)
}

