]> git.street.me.uk Git - andy/viking.git/blame - src/misc/kdtree.h
Open files in selected layer
[andy/viking.git] / src / misc / kdtree.h
CommitLineData
c3102c63
RN
1/*
2This file is part of ``kdtree'', a library for working with kd-trees.
3Copyright (C) 2007-2011 John Tsiombikas <nuclear@member.fsf.org>
4
5Redistribution and use in source and binary forms, with or without
6modification, are permitted provided that the following conditions are met:
7
81. Redistributions of source code must retain the above copyright notice, this
9 list of conditions and the following disclaimer.
102. Redistributions in binary form must reproduce the above copyright notice,
11 this list of conditions and the following disclaimer in the documentation
12 and/or other materials provided with the distribution.
133. The name of the author may not be used to endorse or promote products
14 derived from this software without specific prior written permission.
15
16THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
17WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
18MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
19EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
20EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
21OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
22INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
23CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
24IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
25OF SUCH DAMAGE.
26*/
27#ifndef _KDTREE_H_
28#define _KDTREE_H_
29
30#ifdef __cplusplus
31extern "C" {
32#endif
33
34struct kdtree;
35struct kdres;
36
37
38/* create a kd-tree for "k"-dimensional data */
39struct kdtree *kd_create(int k);
40
41/* free the struct kdtree */
42void kd_free(struct kdtree *tree);
43
44/* remove all the elements from the tree */
45void kd_clear(struct kdtree *tree);
46
47/* if called with non-null 2nd argument, the function provided
48 * will be called on data pointers (see kd_insert) when nodes
49 * are to be removed from the tree.
50 */
51void kd_data_destructor(struct kdtree *tree, void (*destr)(void*));
52
53/* insert a node, specifying its position, and optional data */
54int kd_insert(struct kdtree *tree, const double *pos, void *data);
55int kd_insertf(struct kdtree *tree, const float *pos, void *data);
56int kd_insert3(struct kdtree *tree, double x, double y, double z, void *data);
57int kd_insert3f(struct kdtree *tree, float x, float y, float z, void *data);
58
59/* Find the nearest node from a given point.
60 *
61 * This function returns a pointer to a result set with at most one element.
62 */
63struct kdres *kd_nearest(struct kdtree *tree, const double *pos);
64struct kdres *kd_nearestf(struct kdtree *tree, const float *pos);
65struct kdres *kd_nearest3(struct kdtree *tree, double x, double y, double z);
66struct kdres *kd_nearest3f(struct kdtree *tree, float x, float y, float z);
67
68/* Find the N nearest nodes from a given point.
69 *
70 * This function returns a pointer to a result set, with at most N elements,
71 * which can be manipulated with the kd_res_* functions.
72 * The returned pointer can be null as an indication of an error. Otherwise
73 * a valid result set is always returned which may contain 0 or more elements.
74 * The result set must be deallocated with kd_res_free after use.
75 */
76/*
77struct kdres *kd_nearest_n(struct kdtree *tree, const double *pos, int num);
78struct kdres *kd_nearest_nf(struct kdtree *tree, const float *pos, int num);
79struct kdres *kd_nearest_n3(struct kdtree *tree, double x, double y, double z);
80struct kdres *kd_nearest_n3f(struct kdtree *tree, float x, float y, float z);
81*/
82
83/* Find any nearest nodes from a given point within a range.
84 *
85 * This function returns a pointer to a result set, which can be manipulated
86 * by the kd_res_* functions.
87 * The returned pointer can be null as an indication of an error. Otherwise
88 * a valid result set is always returned which may contain 0 or more elements.
89 * The result set must be deallocated with kd_res_free after use.
90 */
91struct kdres *kd_nearest_range(struct kdtree *tree, const double *pos, double range);
92struct kdres *kd_nearest_rangef(struct kdtree *tree, const float *pos, float range);
93struct kdres *kd_nearest_range3(struct kdtree *tree, double x, double y, double z, double range);
94struct kdres *kd_nearest_range3f(struct kdtree *tree, float x, float y, float z, float range);
95
96/* frees a result set returned by kd_nearest_range() */
97void kd_res_free(struct kdres *set);
98
99/* returns the size of the result set (in elements) */
100int kd_res_size(struct kdres *set);
101
102/* rewinds the result set iterator */
103void kd_res_rewind(struct kdres *set);
104
105/* returns non-zero if the set iterator reached the end after the last element */
106int kd_res_end(struct kdres *set);
107
108/* advances the result set iterator, returns non-zero on success, zero if
109 * there are no more elements in the result set.
110 */
111int kd_res_next(struct kdres *set);
112
113/* returns the data pointer (can be null) of the current result set item
114 * and optionally sets its position to the pointers(s) if not null.
115 */
116void *kd_res_item(struct kdres *set, double *pos);
117void *kd_res_itemf(struct kdres *set, float *pos);
118void *kd_res_item3(struct kdres *set, double *x, double *y, double *z);
119void *kd_res_item3f(struct kdres *set, float *x, float *y, float *z);
120
121/* equivalent to kd_res_item(set, 0) */
122void *kd_res_item_data(struct kdres *set);
123
124
125#ifdef __cplusplus
126}
127#endif
128
129#endif /* _KDTREE_H_ */