geometry

package
v0.2.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Oct 25, 2025 License: Apache-2.0 Imports: 9 Imported by: ? modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package geometry provides supporting functionality when dealing with graphs that have geometric coordinates (except if otherwise stated, it assumes Euclidean spaces).

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type KDTree

type KDTree[T KDTreePointType] struct {
	// Points has size NumPoints * Dimension, the underlying shape being [NumPoints, Dimension], stored in row-major order.
	// This slice is modified in-place to reflect the KD-tree's sorting.
	Points []T

	// NumPoints stored in the KD-tree.
	NumPoints int

	// Dimension of each point.
	Dimension int

	// Order maps each index on KDTree.Points (conceptual point index) to the corresponding point index in original pointData.
	// So if KDTree.Points[i*Dimension:(i+1)*Dimension] corresponds to the original pointData[j*Dimension:(j+1)*Dimension], we have Order[i] = j.
	// len(Order) == NumPoints.
	// This slice is also modified in-place during tree construction.
	Order []int

	// Root of the tree.
	Root *KDTreeNode[T]
}

KDTree is a structured container of NumPoints points with the given Dimension, organized as a binary tree.

It's a convenient structure to quickly search for points in areas of the space.

See NewKDTree to construct the kd-tree.

func NewKDTree

func NewKDTree[T KDTreePointType](pointsData []T, dimension int, minPointsPerLeaf int) (*KDTree[T], error)

NewKDTree builds a K-d tree from a flat slice of point values. The splits are chosen on the axis with the largest range, and they take the median point for the axis to keep the generated tree approximately balanced.

Args:

  • pointsData: A flat slice of float64 where points are laid out contiguously (e.g., [x1, y1, z1, x2, y2, z2, ...]). It will be cloned and sorted into KDTree.Points.
  • D: The number of axes for each point (dimension of a point).
  • minPointsPerLeaf: The minimum number of points a node must contain to be split further.

It is an error to provide 0 points.

func (*KDTree[T]) NumPointsForNode

func (tree *KDTree[T]) NumPointsForNode(node *KDTreeNode[T]) int

NumPointsForNode contained inside the bounding box of the node.

func (*KDTree[T]) String

func (tree *KDTree[T]) String() string

String implements the fmt.Stringer interface for KDTree, providing a hierarchical representation of the tree structure.

type KDTreeNode

type KDTreeNode[T KDTreePointType] struct {
	// Min coordinates for the bounding box of this node's region.
	// len(Min) == KDTree.Dimension.
	Min []T

	// Max coordinates for the bounding box of this node's region
	// len(Max) == KDTree.Dimension.
	Max []T

	// StartIdx is the index of the first point (in KDTree.Points and KDTree.Order) included in this node.
	StartIdx int

	// EndIdx is the one-past index of the last point (in KDTree.Points and KDTree.Order) included in this node.
	EndIdx int // End index (exclusive) in the sorted points slice

	// Left And Right if not a leaf node.
	Left, Right *KDTreeNode[T]

	// SplitAxis for this node, if not a leaf node.
	SplitAxis int

	// SplitValue for this node, if not a leaf node.
	// Points with the SplitAxis value < SplitValue go to the Left node. Otherwise, they go to the Right node.
	SplitValue T
}

KDTreeNode represents a node in the K-d tree.

func (*KDTreeNode[T]) IsLeaf

func (node *KDTreeNode[T]) IsLeaf() bool

IsLeaf node.

type KDTreePointType

type KDTreePointType interface{ float32 | float64 }

KDTreePointType accepted by KDTree.

type NearestEdgesConfig

type NearestEdgesConfig struct {
	// contains filtered or unexported fields
}

NearestEdgesConfig is created with NearestEdges and once fully configured, can be executed with Done.

func NearestEdges

func NearestEdges(source, target *tensors.Tensor) *NearestEdgesConfig

NearestEdges returns edges connecting each source point to its closest target point.

This runs only on CPU -- no graphs or backends are used.

Args:

  • source: shaped [numSourcePoints, dimension], where the dimension is usually 2 or 3. Only float32 and float64 data types are supported.
  • target: shaped [numTargetPoints, dimension], where the dimension must match the source dimension. Same data type as source.

It returns a configuration that can be optionally configured. Call NearestEdgesConfig.Done to perform the operation. It then returns a tensor "edges" with the shape [2, numSourcePoints]Int32, where edge_i connects source point edges[0][i] to target point edges[1][i].

func (*NearestEdgesConfig) Done

func (c *NearestEdgesConfig) Done() (*tensors.Tensor, error)

Done performs the NearestEdges operation as configured.

It returns a tensor "edges" with the shape [2, numSourcePoints]Int32, where edge_i connects source point i to its closest target point.

It is an error if there are no target points.

type RadiusEdgesConfig

type RadiusEdgesConfig struct {
	// contains filtered or unexported fields
}

RadiusEdgesConfig is created with RadiusEdges and once fully configured, can be executed with Done.

func RadiusEdges

func RadiusEdges(source, target *tensors.Tensor, radius float64) *RadiusEdgesConfig

RadiusEdges returns edges connecting the source to target points that are within the given radius.

This runs only in CPU -- no graphs or backends are used.

Args:

  • source: shaped [numSourcePoints, dimension], where the dimension is usually 2 or 3. Only float32 and float64 data types are supported.
  • target: shaped [numTargetPoints, dimension], where the dimension is usually 2 or 3 and must match the source dimension. Same data type as source.
  • radius: if L2(p_source, p_target) < radius, and edge is created.

It returns a configuration that can be optionally configured. Call RadiusEdgesConfig.Done to perform the operation. It then returns a tensor "edges" with the shape [2][numEdges]Int32, where edge_i connects source point edges[0][i] to target point edges[1][i]. The number of edges (numEdges) varies with the points themselves, and if it is not limited, it may be as large as numSourcePoints * numTargetPoints.

TODO: Add MaxNeighbors, batch support, reverting source/target if numTargetPoints >> numSourcePoints.

func (*RadiusEdgesConfig) Done

func (c *RadiusEdgesConfig) Done() (*tensors.Tensor, error)

Done performs the RadiusEdges operation as configured.

It then returns a tensor "edges" with the shape [2][numEdges]Int32, where edge_i connects source point edges[0][i] to target point edges[1][i]. The number of edges (numEdges) varies with the points themselves, and if it is not limited, it may be as large as numSourcePoints * numTargetPoints.

If no edges are found, it returns an error.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL