kd_tree.h
Go to the documentation of this file.
1 #pragma once
2 
13 
14 #include <stddef.h>
15 #include <inttypes.h>
16 #include <stdbool.h>
17 
18 #include <crater/vec.h>
19 
30 typedef struct cr8r_kd_ft cr8r_kd_ft;
32 struct cr8r_kd_ft{
52  uint64_t dim;
60  size_t bounds_size;
65  void (*split)(const cr8r_kd_ft *ft, const void *self, const void *root_pt, void *o1, void *o2);
70  void (*update)(const cr8r_kd_ft *ft, void *self, const void *pt);
72  double (*min_sqdist)(const cr8r_kd_ft *ft, const void *self, const void *pt);
74  double (*sqdist)(const cr8r_kd_ft *ft, const void *a, const void *b);
75 };
76 
86 typedef struct{
92  int64_t bl[3];
94  int64_t tr[3];
96 
98 typedef struct{
99  cr8r_kd_ft ft;
100  const void *pt;
101  cr8r_vec *ents;
102  uint64_t k;
103  double max_sqdist;
105 
111 typedef cr8r_walk_decision (*cr8r_kdvisitor)(cr8r_kd_ft *ft, const void *win, void *ent, void *data);
112 
114 bool cr8r_kdwin_init_s2i64(cr8r_kdwin_s2i64*, const int64_t bl[3], const int64_t tr[3]);
115 
117 bool cr8r_kdwin_init_c3i64(cr8r_kdwin_s2i64*, const int64_t bl[3], const int64_t tr[3]);
118 
121 
132 bool cr8r_kd_ify(cr8r_vec*, cr8r_kd_ft *ft, uint64_t a, uint64_t b);
133 
141 void cr8r_kd_walk(cr8r_vec*, const cr8r_kd_ft *ft, const void *bounds, cr8r_kdvisitor visitor, void *data);
142 
158 bool cr8r_kd_k_closest(cr8r_vec*, cr8r_kd_ft *ft, const void *bounds, const void *pt, uint64_t k, cr8r_vec *out);
159 
165 int cr8r_default_cmp_kd_kcs_pt_dist(const cr8r_base_ft *_ft, const void *a, const void *b);
170 
cr8r_walk_decision
Enum to allow a callee/visitor to control the behavior of a tree traversal.
Definition: container.h:32
bool cr8r_kd_ify(cr8r_vec *, cr8r_kd_ft *ft, uint64_t a, uint64_t b)
Rearrange a vector of points into a kd tree.
cr8r_walk_decision(* cr8r_kdvisitor)(cr8r_kd_ft *ft, const void *win, void *ent, void *data)
Type of a visitor callback for a kd tree traversal.
Definition: kd_tree.h:111
cr8r_kd_ft cr8r_kdft_s2i64
kdft implementation for spherical kd trees in 3 dimensions with i64 coordinates
int cr8r_default_cmp_kd_kcs_pt_dist(const cr8r_base_ft *_ft, const void *a, const void *b)
Comparison function that compares two kd tree points based on their distance from a fixed point.
bool cr8r_kdwin_init_s2i64(cr8r_kdwin_s2i64 *, const int64_t bl[3], const int64_t tr[3])
Initialize a i64x3 bounds object with a given "bottom left" and "top right" point.
cr8r_kd_ft cr8r_kdft_c3i64
kdft implementation for cuboid kd trees in 3 dimensions with i64 coordintates
bool cr8r_kd_k_closest(cr8r_vec *, cr8r_kd_ft *ft, const void *bounds, const void *pt, uint64_t k, cr8r_vec *out)
Find the k closest points to a given point.
bool cr8r_kdwin_init_c3i64(cr8r_kdwin_s2i64 *, const int64_t bl[3], const int64_t tr[3])
Initialize a i64x3 bounds object with a given "bottom left" and "top right" point.
void cr8r_kd_walk(cr8r_vec *, const cr8r_kd_ft *ft, const void *bounds, cr8r_kdvisitor visitor, void *data)
Perform a preorder traversal of the points in a kd tree and call a visitor callback on each.
bool cr8r_kdwin_bounding_i64x3(cr8r_kdwin_s2i64 *, const cr8r_vec *ents, const cr8r_kd_ft *ft)
Initialize a i64x3 bounds object with the "bottom left" and "top right" points bounding a list of poi...
Base function table for all Crater containers.
Definition: container.h:22
Function table for kd tree, see cr8r_kd_ft.
Definition: kd_tree.h:32
uint64_t dim
Dimension of the kd tree.
Definition: kd_tree.h:52
double(* sqdist)(const cr8r_kd_ft *ft, const void *a, const void *b)
find the squared distance between two points
Definition: kd_tree.h:74
size_t bounds_size
Size of a bounds object.
Definition: kd_tree.h:60
void(* split)(const cr8r_kd_ft *ft, const void *self, const void *root_pt, void *o1, void *o2)
split a bounds object in two along some dimension
Definition: kd_tree.h:65
cr8r_vec_ft super
Subtable for vector ft.
Definition: kd_tree.h:39
double(* min_sqdist)(const cr8r_kd_ft *ft, const void *self, const void *pt)
find the minimum squared distance from a bounds object to a point
Definition: kd_tree.h:72
void(* update)(const cr8r_kd_ft *ft, void *self, const void *pt)
update a bounds object to include some point
Definition: kd_tree.h:70
Additional state for .
Definition: kd_tree.h:98
Concrete bounds object for spherical and cuboid kd trees with 3 i64 coords.
Definition: kd_tree.h:86
Function table for vector.
Definition: vec.h:40
A vector Take care if manipulating these fields directly, this should only be done if the functions i...
Definition: vec.h:23