gm #
Geometry algorithms and structures
This package provides some functions to help with the solution of geometry problems. It also includes some routines loosely related with geometry.
Examples
You can find comprehensive examples demonstrating the geometry module features in the main examples directory:
gm_basic_geometry
- Fundamental 3D geometry operationsgm_advanced_analysis
- Advanced geometric analysisgm_distance_analysis
- Distance calculation analysisgm_spatial_binning
- Spatial indexing and binning systemsgm_geometry_playground
- Interactive geometry explorationgm_trajectory_simulation
- Motion analysis
Each example includes a complete main.v
file and detailed README.md
with mathematical explanations.
Constants #
const xdelzero = 1e-10 // minimum distance between coordinates; i.e. xmax[i]-xmin[i] mininum
fn dist_point_line #
fn dist_point_line(p &Point, a &Point, b &Point, tol f64) f64
dist_point_line computes the distance from p to line passing through a -> b
fn dist_point_point #
fn dist_point_point(a &Point, b &Point) f64
dist_point_point computes the unsigned distance from a to b
fn is_point_in #
fn is_point_in(p &Point, cmin []f64, cmax []f64, tol f64) bool
is_point_in returns whether p is inside box with cmin and cmax
fn is_point_in_line #
fn is_point_in_line(p &Point, a &Point, b &Point, zero f64, told f64, tolin f64) bool
is_point_in_line returns whether p is inside line passing through a and b
fn points_lims #
fn points_lims(pp []&Point) ([]f64, []f64)
points_lims returns the limits of a set of points
fn vector_add #
fn vector_add(alpha f64, u []f64, beta f64, v []f64) []f64
vector_add returns a new vector by adding two other vectors w := αu + βv
fn vector_dot #
fn vector_dot(u []f64, v []f64) f64
vector_dot returns the dot product between two vectors
fn vector_new #
fn vector_new(m f64, u []f64) []f64
vector_new returns a new vector scaled by m
fn vector_norm #
fn vector_norm(u []f64) f64
vector_norm returns the length (Euclidean norm) of a vector
fn Bins.new #
fn Bins.new(xmin []f64, xmax []f64, ndiv_ []int) &Bins
Bins.new initialise bins structure xmin -- [ndim] min/initial coordinates of the whole space (box/cube) xmax -- [ndim] max/final coordinates of the whole space (box/cube) ndiv -- [ndim] number of divisions for xmax-xmin
fn Point.new #
fn Point.new(x f64, y f64, z f64) &Point
Point.new creates a new point
fn Segment.new #
fn Segment.new(a &Point, b &Point) &Segment
Segment.new creates a new segment from a to b
type PointsDiffFn #
type PointsDiffFn = fn (is_old int, x_new []f64) bool
struct Bin #
struct Bin {
pub mut:
index int // index of bin
entries []&BinEntry // entries
}
Bin defines one bin in Bins (holds entries for search)
fn (Bin) str #
fn (o Bin) str() string
str returns the string representation of one Bin
struct BinEntry #
struct BinEntry {
pub mut:
id int // object Id
x []f64 // entry coordinate (read only)
extra voidptr // any entity attached to this entry
}
BinEntry holds data of an entry to bin
struct Bins #
struct Bins {
mut:
tmp []int // [ndim] temporary (auxiliary) slice
pub mut:
ndim int // space dimension
xmin []f64 // [ndim] left/lower-most point
xmax []f64 // [ndim] right/upper-most point
xdel []f64 // [ndim] the lengths along each direction (whole box)
size []f64 // size of bins
ndiv []int // [ndim] number of divisions along each direction
all []&Bin // [nbins] all bins (there will be an extra "ghost" bin along each dimension)
}
Bins implements a set of bins holding entries and is used to fast search entries by given coordinates.
fn (Bins) append #
fn (mut o Bins) append(x []f64, id int, extra voidptr)
append adds a new entry {x, id, something} into the bins structure
fn (Bins) clear #
fn (mut o Bins) clear()
clear clears all biBinsns
fn (Bins) find_bin_by_index #
fn (mut o Bins) find_bin_by_index(idx int) &Bin
find_bin_by_index finds or allocate new bin corresponding to index idx
fn (Bins) calc_index #
fn (mut o Bins) calc_index(x []f64) int
calc_index calculates the bin index where the point x is returns -1 if out-of-range
fn (Bins) find_closest #
fn (mut o Bins) find_closest(x []f64) (int, f64)
find_closest returns the id of the entry whose coordinates are closest to x id_closest -- the id of the closest entity. return -1 if out-of-range or not found sq_dist_min -- the minimum distance (squared) between x and the closest entity in the same bin
Note: find_closest does search the whole area.It only locates neighbours in the same bin where the given x is located. So, if there area no entries in the bin containing x, no entry will be found.
fn (Bins) find_closest_and_append #
fn (mut o Bins) find_closest_and_append(mut next_id &int, x []f64, extra voidptr, rad_tol f64, diff PointsDiffFn) (int, bool)
find_closest_and_append finds closest point and, if not found, append to bins with a new Id input: next_id -- is the Id of the next point. Will be incremented if x is a new point to be added. x -- is the point to be added extra -- extra information attached to point rad_tol -- is the tolerance for the radial distance (i.e. NOT squared) to decide whether a new point will be appended or not. diff -- [optional] a function for further check that the new and an eventual existent points are really different, even after finding that they coincide (within tol) output: id -- the id attached to x existent -- flag telling if x was found, based on given tolerance
fn (Bins) find_along_segment #
fn (mut o Bins) find_along_segment(xi_ []f64, xf_ []f64, tol f64) []int
find_along_segment gets the ids of entries that lie close to a segment
Note: the initial (xi) and final (xf) points on segment define a bounding box to filter points
fn (Bins) get_limits #
fn (o &Bins) get_limits(idx_bin int) ([]f64, []f64)
get_limits returns limigs of a specific bin
fn (Bins) nactive #
fn (o &Bins) nactive() int
nactive returns the number of active bins; i.e. non-nil bins
fn (Bins) nentries #
fn (o &Bins) nentries() int
nentries returns the total number of entries (in active bins)
fn (Bins) summary #
fn (o &Bins) summary() string
summary returns the summary of this Bins' data
fn (Bins) str #
fn (o &Bins) str() string
str returns the string representation of a set of Bins
struct Point #
struct Point {
pub mut:
x f64
y f64
z f64
}
Point holds the Cartesian coordinates of a point in 3D space
fn (Point) clone #
fn (o &Point) clone() &Point
clone creates a new copy of Point
fn (Point) disp #
fn (o &Point) disp(dx f64, dy f64, dz f64) &Point
disp creates a new copy of Point displaced by dx, dy, dz
fn (Point) str #
fn (o &Point) str() string
str outputs Point
struct Segment #
struct Segment {
pub:
a &Point = unsafe { nil }
b &Point = unsafe { nil }
}
Segment represents a directed segment from a to b
fn (Segment) len #
fn (o &Segment) len() f64
len computes the length of Segment == Euclidean norm
fn (Segment) scaled #
fn (o &Segment) scaled(m f64) &Segment
New creates a new Segment scaled by m and starting from A
fn (Segment) vector #
fn (o &Segment) vector(m f64) []f64
vector returns the vector representing Segment from A to B (scaled by m)
fn (Segment) str #
fn (o &Segment) str() string
str outputs Segment