VAPoR  0.1
kdtree.h
Go to the documentation of this file.
1 /*
2 This file is part of ``kdtree'', a library for working with kd-trees.
3 Copyright (C) 2007-2011 John Tsiombikas <nuclear@member.fsf.org>
4 
5 Redistribution and use in source and binary forms, with or without
6 modification, are permitted provided that the following conditions are met:
7 
8 1. Redistributions of source code must retain the above copyright notice, this
9  list of conditions and the following disclaimer.
10 2. 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.
13 3. 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 
16 THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
17 WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
18 MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
19 EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
20 EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
21 OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
22 INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
23 CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
24 IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
25 OF SUCH DAMAGE.
26 */
27 #ifndef _KDTREE_H_
28 #define _KDTREE_H_
29 
30 #ifdef __cplusplus
31 extern "C" {
32 #endif
33 
34 struct kdtree;
35 struct kdres;
36 
37 
38 /* create a kd-tree for "k"-dimensional data */
39 struct kdtree *kd_create(int k);
40 
41 /* free the struct kdtree */
42 void kd_free(struct kdtree *tree);
43 
44 /* remove all the elements from the tree */
45 void 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  */
51 void kd_data_destructor(struct kdtree *tree, void (*destr)(void*));
52 
53 /* insert a node, specifying its position, and optional data */
54 int kd_insert(struct kdtree *tree, const double *pos, void *data);
55 int kd_insertf(struct kdtree *tree, const float *pos, void *data);
56 int kd_insert3(struct kdtree *tree, double x, double y, double z, void *data);
57 int 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  */
63 struct kdres *kd_nearest(struct kdtree *tree, const double *pos);
64 struct kdres *kd_nearestf(struct kdtree *tree, const float *pos);
65 struct kdres *kd_nearest3(struct kdtree *tree, double x, double y, double z);
66 struct 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 /*
77 struct kdres *kd_nearest_n(struct kdtree *tree, const double *pos, int num);
78 struct kdres *kd_nearest_nf(struct kdtree *tree, const float *pos, int num);
79 struct kdres *kd_nearest_n3(struct kdtree *tree, double x, double y, double z);
80 struct 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  */
91 struct kdres *kd_nearest_range(struct kdtree *tree, const double *pos, double range);
92 struct kdres *kd_nearest_rangef(struct kdtree *tree, const float *pos, float range);
93 struct kdres *kd_nearest_range3(struct kdtree *tree, double x, double y, double z, double range);
94 struct kdres *kd_nearest_range3f(struct kdtree *tree, float x, float y, float z, float range);
95 
96 struct kdres *kd_region(struct kdtree *kd, const double *min, const double *max);
97 
98 /* frees a result set returned by kd_nearest_range() */
99 void kd_res_free(struct kdres *set);
100 
101 /* returns the size of the result set (in elements) */
102 int kd_res_size(struct kdres *set);
103 
104 /* rewinds the result set iterator */
105 void kd_res_rewind(struct kdres *set);
106 
107 /* returns non-zero if the set iterator reached the end after the last element */
108 int kd_res_end(struct kdres *set);
109 
110 /* advances the result set iterator, returns non-zero on success, zero if
111  * there are no more elements in the result set.
112  */
113 int kd_res_next(struct kdres *set);
114 
115 /* returns the data pointer (can be null) of the current result set item
116  * and optionally sets its position to the pointers(s) if not null.
117  */
118 void *kd_res_item(struct kdres *set, double *pos);
119 void *kd_res_itemf(struct kdres *set, float *pos);
120 void *kd_res_item3(struct kdres *set, double *x, double *y, double *z);
121 void *kd_res_item3f(struct kdres *set, float *x, float *y, float *z);
122 
123 /* equivalent to kd_res_item(set, 0) */
124 void *kd_res_item_data(struct kdres *set);
125 
126 
127 #ifdef __cplusplus
128 }
129 #endif
130 
131 #endif /* _KDTREE_H_ */
struct kdres * kd_nearest_rangef(struct kdtree *tree, const float *pos, float range)
void * kd_res_item3(struct kdres *set, double *x, double *y, double *z)
struct kdres * kd_nearest_range3f(struct kdtree *tree, float x, float y, float z, float range)
int kd_insert(struct kdtree *tree, const double *pos, void *data)
void * kd_res_item_data(struct kdres *set)
struct kdres * kd_nearest3f(struct kdtree *tree, float x, float y, float z)
int kd_res_end(struct kdres *set)
int kd_res_next(struct kdres *set)
void kd_clear(struct kdtree *tree)
int kd_res_size(struct kdres *set)
struct kdres * kd_nearest3(struct kdtree *tree, double x, double y, double z)
void * kd_res_item3f(struct kdres *set, float *x, float *y, float *z)
void kd_data_destructor(struct kdtree *tree, void(*destr)(void *))
struct kdres * kd_nearest_range3(struct kdtree *tree, double x, double y, double z, double range)
void * kd_res_itemf(struct kdres *set, float *pos)
int kd_insert3(struct kdtree *tree, double x, double y, double z, void *data)
struct kdres * kd_region(struct kdtree *kd, const double *min, const double *max)
void kd_res_rewind(struct kdres *set)
int kd_insert3f(struct kdtree *tree, float x, float y, float z, void *data)
struct kdres * kd_nearest_range(struct kdtree *tree, const double *pos, double range)
void * kd_res_item(struct kdres *set, double *pos)
struct kdres * kd_nearest(struct kdtree *tree, const double *pos)
void kd_free(struct kdtree *tree)
struct kdres * kd_nearestf(struct kdtree *tree, const float *pos)
struct kdtree * kd_create(int k)
void kd_res_free(struct kdres *set)
int kd_insertf(struct kdtree *tree, const float *pos, void *data)