GRASS 8 Programmer's Manual 8.6.0dev(2026)-4bb960b182
Loading...
Searching...
No Matches
qtree.c
Go to the documentation of this file.
1/*!
2 * \file qtree.c
3 *
4 * \author
5 * H. Mitasova, I. Kosinovsky, D. Gerdes, Fall 1993,
6 * University of Illinois and
7 * US Army Construction Engineering Research Lab
8 *
9 * \author H. Mitasova (University of Illinois),
10 * \author I. Kosinovsky, (USA-CERL)
11 * \author D.Gerdes (USA-CERL)
12 *
13 * \author updated/checked by Mitasova Nov. 96 (no changes necessary)
14 *
15 * SPDX-FileCopyrightText: 1993-1996 Helena Mitasova
16 * SPDX-FileCopyrightText: GRASS Development Team
17 * SPDX-License-Identifier: GPL-2.0-or-later
18 */
19
20#include <stdio.h>
21#include <stdlib.h>
22#include <math.h>
23
24#include <grass/dataquad.h>
25#include <grass/qtree.h>
26
27/*! Initializes multfunc structure with given arguments */
29 int (*compare)(struct triple *, struct quaddata *),
30 struct quaddata **(*divide_data)(struct quaddata *, int, double),
31 int (*add_data)(struct triple *, struct quaddata *, double),
32 int (*intersect)(struct quaddata *, struct quaddata *),
33 int (*division_check)(struct quaddata *, int),
34 int (*get_points)(struct quaddata *, struct quaddata *, int))
35{
36 struct multfunc *functions;
37
38 if (!(functions = (struct multfunc *)malloc(sizeof(struct multfunc)))) {
39 return NULL;
40 }
41 functions->compare = compare;
42 functions->divide_data = divide_data;
43 functions->add_data = add_data;
44 functions->intersect = intersect;
45 functions->division_check = division_check;
46 functions->get_points = get_points;
47 return functions;
48}
49
50/*! Initializes tree_info using given arguments */
52 struct multfunc *functions, double dmin,
53 int kmax)
54{
55 struct tree_info *info;
56
57 if (!(info = (struct tree_info *)malloc(sizeof(struct tree_info)))) {
58 return NULL;
59 }
60 info->root = root;
61 info->functions = functions;
62 info->dmin = dmin;
63 info->kmax = kmax;
64 return info;
65}
66
67/** Initializes multtree using given arguments */
68struct multtree *MT_tree_new(struct quaddata *data, struct multtree **leafs,
69 struct multtree *parent, int multant)
70{
71 struct multtree *tree;
72
73 if (!(tree = (struct multtree *)malloc(sizeof(struct multtree)))) {
74 return NULL;
75 }
76 tree->data = data;
77 tree->leafs = leafs;
78 tree->parent = parent;
79 tree->multant = multant;
80 return tree;
81}
82
83/*!
84 * First checks for dividing cond. (if n_points>=KMAX) and tree
85 * is a leaf by calling one of tree's functions (`division_check()`).
86 * If tree is not a leaf (is a node) uses function compare to determine
87 * into which "son" we need to insert the point and calls MT_insert()
88 * with this son as a n argument.
89 *
90 * If TREE is a leaf but we don't need to divide it (n_points<KMAX) then
91 * calls function `add_data(point, ...)` to add point to the data of tree
92 * and returns the result of `add_data()` (which returns 1 if the point is
93 * inserted and 0 if its ignored (when its too dense)).
94 *
95 * If `division_check()` returns true, calls MT_divide() and then calls
96 * MT_insert() to insert the point into divided tree and returns the
97 * result of MT_divide().
98 */
99int MT_insert(struct triple *point, struct tree_info *info,
100 struct multtree *tree, int n_leafs)
101{
102 int j = 0, i, k, comp;
103
104 if (tree == NULL) {
105 fprintf(stderr, "insert: tree is NULL\n");
106 return -5;
107 }
108 if (tree->data == NULL) {
109 fprintf(stderr, "insert: tree->data is NULL\n");
110 return -5;
111 }
112 i = info->functions->division_check(tree->data, info->kmax);
113 if (i <= 0) {
114 if (i == -1) {
115 comp = info->functions->compare(point, tree->data);
116 if ((comp < 1) || (comp > n_leafs))
117 return -3;
118 j = MT_insert(point, info, tree->leafs[comp - 1], n_leafs);
119 }
120 else {
121 if (i == 0) {
122 j = info->functions->add_data(point, tree->data, info->dmin);
123 }
124 }
125 }
126 else {
127 k = MT_divide(info, tree, n_leafs);
128 if (k == 1)
129 j = MT_insert(point, info, tree, n_leafs);
130 if (k == -3) {
131 static int once = 0;
132
133 if (!once) {
134 fprintf(stderr, "Point out of range!\n");
135 once = 1;
136 }
137 }
138 if (k < 0)
139 return k;
140 }
141 return j;
142}
143
144/*!
145 * Divide a tree
146 *
147 * Divides the tree by calling one of tree's functions (divide_data())
148 * and returns the result of divide_data()
149 */
150int MT_divide(struct tree_info *info, struct multtree *tree, int n_leafs)
151{
152 int i;
153 struct quaddata **datas;
154 struct multtree *par;
155 struct multtree **leafs;
156
157 datas = info->functions->divide_data(tree->data, info->kmax, info->dmin);
158 if (datas == NULL) {
159 fprintf(stderr, "datas is NULL\n");
160 return -7;
161 }
162 par = tree;
163 leafs = (struct multtree **)malloc(sizeof(struct multtree *) * n_leafs);
164 for (i = 1; i <= n_leafs; i++) {
165 leafs[i - 1] = MT_tree_new(datas[i], NULL, par, i);
166 }
167 tree->leafs = leafs;
168 return 1;
169}
170
171/*!
172 * Get points inside a region from a tree
173 *
174 * Gets points inside the region defined by DATA from TREE and
175 * adds them to DATA. If the number of eligible
176 * point is more than MAX returns MAX+1 otherwise returns number of points added
177 * to DATA.
178 *
179 * Uses tree's functions intersect() to find leafs that intersect given region
180 * and get_points() to get points from such leafs.
181 */
182int MT_region_data(struct tree_info *info, struct multtree *tree,
183 struct quaddata *data,
184 int MAX, /*!< max number of points we can add (KMAX2) */
185 int n_leafs)
186{
187 int n = 0, j;
188
189 if (tree == NULL) {
190 fprintf(stderr, "MT_region_data: tree is NULL\n");
191 return n;
192 }
193 if (tree->data == NULL) {
194 fprintf(stderr, "MT_region_data: data is NULL\n");
195 return n;
196 }
197 if (info->functions->intersect(data, tree->data)) {
198 if (tree->leafs != NULL) {
199 for (j = 0; j < n_leafs; j++) {
200 if ((n = n + MT_region_data(info, tree->leafs[j], data, MAX - n,
201 n_leafs)) > MAX)
202 return n;
203 }
204 }
205 else {
206 n = info->functions->get_points(data, tree->data, MAX);
207 }
208 return n;
209 }
210 return 0;
211}
#define NULL
Definition ccmath.h:32
#define MAX(a, b)
Definition gis.h:145
int MT_region_data(struct tree_info *info, struct multtree *tree, struct quaddata *data, int MAX, int n_leafs)
Definition qtree.c:182
struct multtree * MT_tree_new(struct quaddata *data, struct multtree **leafs, struct multtree *parent, int multant)
Definition qtree.c:68
int MT_divide(struct tree_info *info, struct multtree *tree, int n_leafs)
Definition qtree.c:150
struct tree_info * MT_tree_info_new(struct multtree *root, struct multfunc *functions, double dmin, int kmax)
Definition qtree.c:51
int MT_insert(struct triple *point, struct tree_info *info, struct multtree *tree, int n_leafs)
Definition qtree.c:99
struct multfunc * MT_functions_new(int(*compare)(struct triple *, struct quaddata *), struct quaddata **(*divide_data)(struct quaddata *, int, double), int(*add_data)(struct triple *, struct quaddata *, double), int(*intersect)(struct quaddata *, struct quaddata *), int(*division_check)(struct quaddata *, int), int(*get_points)(struct quaddata *, struct quaddata *, int))
Definition qtree.c:28
void * malloc(unsigned)
int(* compare)(struct triple *, struct quaddata *)
Definition qtree.h:33
struct quaddata **(* divide_data)(struct quaddata *, int, double)
Definition qtree.h:34
int(* division_check)(struct quaddata *, int)
Definition qtree.h:37
int(* get_points)(struct quaddata *, struct quaddata *, int)
Definition qtree.h:38
int(* add_data)(struct triple *, struct quaddata *, double)
Definition qtree.h:35
int(* intersect)(struct quaddata *, struct quaddata *)
Definition qtree.h:36
struct multtree ** leafs
Definition qtree.h:50
struct quaddata * data
Definition qtree.h:49
struct multtree * parent
Definition qtree.h:51
int multant
Definition qtree.h:52
struct multtree * root
Definition qtree.h:45
int kmax
Definition qtree.h:44
struct multfunc * functions
Definition qtree.h:42
double dmin
Definition qtree.h:43