Graphviz 16.1.3~dev.20261203.0324
Loading...
Searching...
No Matches
QuadTree.c
Go to the documentation of this file.
1/*************************************************************************
2 * Copyright (c) 2011 AT&T Intellectual Property
3 * All rights reserved. This program and the accompanying materials
4 * are made available under the terms of the Eclipse Public License v2.0
5 * which accompanies this distribution, and is available at
6 * https://www.eclipse.org/org/documents/epl-2.0/EPL-2.0.html
7 *
8 * Contributors: Details at https://graphviz.org
9 *************************************************************************/
10
11#include "config.h"
12
13#include <sparse/general.h>
14#include <common/geom.h>
15#include <common/arith.h>
16#include <math.h>
17#include <sparse/QuadTree.h>
18#include <stdbool.h>
19#include <stddef.h>
20#include <util/alloc.h>
21
22extern double distance_cropped(double *x, int dim, int i, int j);
23
24static node_data node_data_new(int dim, double weight, const double *coord, int id){
25 int i;
26 node_data nd = gv_alloc(sizeof(struct node_data_struct));
27 nd->node_weight = weight;
28 nd->coord = gv_calloc(dim, sizeof(double));
29 nd->id = id;
30 for (i = 0; i < dim; i++) nd->coord[i] = coord[i];
31 nd->data = NULL;
32 return nd;
33}
34
36 free(nd->coord);
37 /*delete outside if (nd->data) free(nd->data);*/
38 free(nd);
39}
40
41static void check_or_realloc_arrays(int dim, int *nsuper, int *nsupermax, double **center, double **supernode_wgts, double **distances){
42
43 if (*nsuper >= *nsupermax) {
44 int new_nsupermax = *nsuper + 10;
45 *center = gv_recalloc(*center, dim * *nsupermax, dim * new_nsupermax, sizeof(double));
46 *supernode_wgts = gv_recalloc(*supernode_wgts, *nsupermax, new_nsupermax, sizeof(double));
47 *distances = gv_recalloc(*distances, *nsupermax, new_nsupermax, sizeof(double));
48 *nsupermax = new_nsupermax;
49 }
50}
51
52static void QuadTree_get_supernodes_internal(QuadTree qt, double bh, double *pt, int nodeid, int *nsuper, int *nsupermax, double **center, double **supernode_wgts, double **distances, double *counts) {
53 double *coord, dist;
54 int dim, i;
55
56 (*counts)++;
57
58 if (!qt) return;
59 dim = qt->dim;
60 node_data l = qt->l;
61 while (l) {
62 check_or_realloc_arrays(dim, nsuper, nsupermax, center, supernode_wgts, distances);
63 if (l->id != nodeid){
64 coord = l->coord;
65 for (i = 0; i < dim; i++){
66 (*center)[dim*(*nsuper)+i] = coord[i];
67 }
68 (*supernode_wgts)[*nsuper] = l->node_weight;
69 (*distances)[*nsuper] = point_distance(pt, coord, dim);
70 (*nsuper)++;
71 }
72 l = l->next;
73 }
74
75 if (qt->qts){
76 dist = point_distance(qt->center, pt, dim);
77 if (qt->width < bh*dist){
78 check_or_realloc_arrays(dim, nsuper, nsupermax, center, supernode_wgts, distances);
79 for (i = 0; i < dim; i++){
80 (*center)[dim*(*nsuper)+i] = qt->average[i];
81 }
82 (*supernode_wgts)[*nsuper] = qt->total_weight;
83 (*distances)[*nsuper] = point_distance(qt->average, pt, dim);
84 (*nsuper)++;
85 } else {
86 for (i = 0; i < 1<<dim; i++){
87 QuadTree_get_supernodes_internal(qt->qts[i], bh, pt, nodeid, nsuper, nsupermax, center,
88 supernode_wgts, distances, counts);
89 }
90 }
91 }
92
93}
94
95void QuadTree_get_supernodes(QuadTree qt, double bh, double *pt, int nodeid, int *nsuper,
96 double **center, double **supernode_wgts, double **distances, double *counts) {
97 int dim = qt->dim;
98
99 (*counts) = 0;
100
101 *nsuper = 0;
102
103 int nsupermax = 10;
104 if (!*center) *center = gv_calloc(nsupermax * dim, sizeof(double));
105 if (!*supernode_wgts) *supernode_wgts = gv_calloc(nsupermax, sizeof(double));
106 if (!*distances) *distances = gv_calloc(nsupermax, sizeof(double));
107 QuadTree_get_supernodes_internal(qt, bh, pt, nodeid, nsuper, &nsupermax, center, supernode_wgts, distances, counts);
108
109}
110
111static double *get_or_assign_node_force(double *force, int i, node_data l,
112 int dim) {
113
114 double *f = l->data;
115
116 if (!f){
117 l->data = &(force[i*dim]);
118 f = l->data;
119 }
120 return f;
121}
122static double *get_or_alloc_force_qt(QuadTree qt, int dim){
123 double *force = qt->data;
124 if (!force){
125 qt->data = gv_calloc(dim, sizeof(double));
126 force = qt->data;
127 }
128 return force;
129}
130
131static void QuadTree_repulsive_force_interact(QuadTree qt1, QuadTree qt2, double *x, double *force, double bh, double p, double KP, double *counts){
132 // calculate the all to all repulsive force and accumulate on each node of the
133 // quadtree if an interaction is possible.
134 // force[i × dim + j], j=1,..., dim is the force on node i
135 double *x1, *x2, dist, wgt1, wgt2, f, *f1, *f2, w1, w2;
136 int dim, i, j, i1, i2, k;
137 QuadTree qt11, qt12;
138
139 if (!qt1 || !qt2) return;
140 assert(qt1->n > 0 && qt2->n > 0);
141 dim = qt1->dim;
142
143 node_data l1 = qt1->l;
144 node_data l2 = qt2->l;
145
146 /* far enough, calculate repulsive force */
147 dist = point_distance(qt1->average, qt2->average, dim);
148 if (qt1->width + qt2->width < bh*dist){
149 counts[0]++;
150 x1 = qt1->average;
151 w1 = qt1->total_weight;
152 f1 = get_or_alloc_force_qt(qt1, dim);
153 x2 = qt2->average;
154 w2 = qt2->total_weight;
155 f2 = get_or_alloc_force_qt(qt2, dim);
156 assert(dist > 0);
157 for (k = 0; k < dim; k++){
158 f = w1 * w2 * KP * (x1[k] - x2[k]) / pow(dist, 1.0 - p);
159 f1[k] += f;
160 f2[k] -= f;
161 }
162 return;
163 }
164
165
166 /* both at leaves, calculate repulsive force */
167 if (l1 && l2){
168 while (l1){
169 x1 = l1->coord;
170 wgt1 = l1->node_weight;
171 i1 = l1->id;
172 f1 = get_or_assign_node_force(force, i1, l1, dim);
173 l2 = qt2->l;
174 while (l2){
175 x2 = l2->coord;
176 wgt2 = l2->node_weight;
177 i2 = l2->id;
178 f2 = get_or_assign_node_force(force, i2, l2, dim);
179 if ((qt1 == qt2 && i2 < i1) || i1 == i2) {
180 l2 = l2->next;
181 continue;
182 }
183 counts[1]++;
184 dist = distance_cropped(x, dim, i1, i2);
185 for (k = 0; k < dim; k++){
186 f = wgt1 * wgt2 * KP * (x1[k] - x2[k]) / pow(dist, 1.0 - p);
187 f1[k] += f;
188 f2[k] -= f;
189 }
190 l2 = l2->next;
191 }
192 l1 = l1->next;
193 }
194 return;
195 }
196
197
198 /* identical, split one */
199 if (qt1 == qt2){
200 for (i = 0; i < 1<<dim; i++){
201 qt11 = qt1->qts[i];
202 for (j = i; j < 1<<dim; j++){
203 qt12 = qt1->qts[j];
204 QuadTree_repulsive_force_interact(qt11, qt12, x, force, bh, p, KP, counts);
205 }
206 }
207 } else {
208 /* split the one with bigger box, or one not at the last level */
209 if (qt1->width > qt2->width && !l1){
210 for (i = 0; i < 1<<dim; i++){
211 qt11 = qt1->qts[i];
212 QuadTree_repulsive_force_interact(qt11, qt2, x, force, bh, p, KP, counts);
213 }
214 } else if (qt2->width > qt1->width && !l2){
215 for (i = 0; i < 1<<dim; i++){
216 qt11 = qt2->qts[i];
217 QuadTree_repulsive_force_interact(qt11, qt1, x, force, bh, p, KP, counts);
218 }
219 } else if (!l1){/* pick one that is not at the last level */
220 for (i = 0; i < 1<<dim; i++){
221 qt11 = qt1->qts[i];
222 QuadTree_repulsive_force_interact(qt11, qt2, x, force, bh, p, KP, counts);
223 }
224 } else if (!l2){
225 for (i = 0; i < 1<<dim; i++){
226 qt11 = qt2->qts[i];
227 QuadTree_repulsive_force_interact(qt11, qt1, x, force, bh, p, KP, counts);
228 }
229 } else {
230 assert(0); // can be both at the leaf level since that should be caught at
231 // the beginning of this func
232 }
233 }
234}
235
236static void QuadTree_repulsive_force_accumulate(QuadTree qt, double *force, double *counts){
237 /* push down forces on cells into the node level */
238 double wgt, wgt2;
239 double *f, *f2;
240 node_data l = qt->l;
241 int i, k, dim;
242 QuadTree qt2;
243
244 dim = qt->dim;
245 wgt = qt->total_weight;
246 f = get_or_alloc_force_qt(qt, dim);
247 assert(wgt > 0);
248 counts[2]++;
249
250 if (l){
251 while (l){
252 i = l->id;
253 f2 = get_or_assign_node_force(force, i, l, dim);
254 wgt2 = l->node_weight;
255 wgt2 = wgt2/wgt;
256 for (k = 0; k < dim; k++) f2[k] += wgt2*f[k];
257 l = l->next;
258 }
259 return;
260 }
261
262 for (i = 0; i < 1<<dim; i++){
263 qt2 = qt->qts[i];
264 if (!qt2) continue;
265 assert(qt2->n > 0);
266 f2 = get_or_alloc_force_qt(qt2, dim);
267 wgt2 = qt2->total_weight;
268 wgt2 = wgt2/wgt;
269 for (k = 0; k < dim; k++) f2[k] += wgt2*f[k];
270 QuadTree_repulsive_force_accumulate(qt2, force, counts);
271 }
272
273}
274
275void QuadTree_get_repulsive_force(QuadTree qt, double *force, double *x, double bh, double p, double KP, double *counts){
276 // get repulsive force by a more efficient algorithm: we consider two cells,
277 // if they are well separated, we calculate the overall repulsive force on the
278 // cell level, if not well separated, we divide one of the cell. If both cells
279 // are at the leaf level, we calculate repulsive force among individual nodes.
280 // Finally we accumulate forces at the cell levels to the node level
281 // qt: the quadtree
282 // x: current coordinates for node i is x[i*dim+j], j = 0, ..., dim-1
283 // force: the repulsive force, an array of length dim*nnodes, the force for node i is at force[i*dim+j], j = 0, ..., dim - 1
284 // bh: Barnes-Hut coefficient. If width_cell1+width_cell2 < bh*dist_between_cells, we treat each cell as a supernode.
285 // p: the repulsive force power
286 // KP: pow(K, 1 - p)
287 // counts: array of size 4.
288 // . counts[0]: number of cell-cell interaction
289 // . counts[1]: number of cell-node interaction
290 // . counts[2]: number of total cells in the quadtree
291 // . Al normalized by dividing by number of nodes
292 int n = qt->n, dim = qt->dim, i;
293
294 for (i = 0; i < 4; i++) counts[i] = 0;
295
296 for (i = 0; i < dim*n; i++) force[i] = 0;
297
298 QuadTree_repulsive_force_interact(qt, qt, x, force, bh, p, KP, counts);
299 QuadTree_repulsive_force_accumulate(qt, force, counts);
300 for (i = 0; i < 4; i++) counts[i] /= n;
301
302}
303QuadTree QuadTree_new_from_point_list(int dim, int n, int max_level, const double *coord){
304 /* form a new QuadTree data structure from a list of coordinates of n points
305 coord: of length n*dim, point i sits at [i*dim, i*dim+dim - 1]
306 */
307 double width;
308 QuadTree qt = NULL;
309 int i, k;
310
311 double *const xmin = gv_calloc(dim, sizeof(double));
312 double *const xmax = gv_calloc(dim, sizeof(double));
313 double *const center = gv_calloc(dim, sizeof(double));
314
315 for (i = 0; i < dim; i++) xmin[i] = coord[i];
316 for (i = 0; i < dim; i++) xmax[i] = coord[i];
317
318 for (i = 1; i < n; i++){
319 for (k = 0; k < dim; k++){
320 xmin[k] = fmin(xmin[k], coord[i*dim+k]);
321 xmax[k] = fmax(xmax[k], coord[i*dim+k]);
322 }
323 }
324
325 width = xmax[0] - xmin[0];
326 for (i = 0; i < dim; i++) {
327 center[i] = (xmin[i] + xmax[i])*0.5;
328 width = fmax(width, xmax[i] - xmin[i]);
329 }
330 width = fmax(width, 0.00001);/* if we only have one point, width = 0! */
331 width *= 0.52;
332 qt = QuadTree_new(dim, center, width, max_level);
333
334 for (i = 0; i < n; i++){
335 qt = QuadTree_add(qt, &coord[i * dim], 1, i);
336 }
337
338
339 free(xmin);
340 free(xmax);
341 free(center);
342 return qt;
343}
344
345QuadTree QuadTree_new(int dim, double *center, double width, int max_level){
346 int i;
347 QuadTree q = gv_alloc(sizeof(struct QuadTree_struct));
348 q->dim = dim;
349 q->n = 0;
350 q->center = gv_calloc(dim, sizeof(double));
351 for (i = 0; i < dim; i++) q->center[i] = center[i];
352 assert(width > 0);
353 q->width = width;
354 q->total_weight = 0;
355 q->average = NULL;
356 q->qts = NULL;
357 q->l = NULL;
358 q->max_level = max_level;
359 q->data = NULL;
360 return q;
361}
362
364 int i, dim;
365 if (!q) return;
366 dim = q->dim;
367 free(q->center);
368 free(q->average);
369 free(q->data);
370 if (q->qts){
371 for (i = 0; i < 1<<dim; i++){
372 QuadTree_delete(q->qts[i]);
373 }
374 free(q->qts);
375 }
376 while (q->l) {
377 node_data next = q->l->next;
379 q->l = next;
380 }
381 free(q);
382}
383
384static int QuadTree_get_quadrant(int dim, double *center, const double *coord){
385 /* find the quadrant that a point of coordinates coord is going into with reference to the center.
386 if coord - center == {+,-,+,+} = {1,0,1,1}, then it will sit in the i-quadrant where
387 i's binary representation is 1011 (that is, decimal 11).
388 */
389 int d = 0, i;
390
391 for (i = dim - 1; i >= 0; i--){
392 if (coord[i] - center[i] < 0){
393 d = 2*d;
394 } else {
395 d = 2*d+1;
396 }
397 }
398 return d;
399}
400
401QuadTree QuadTree_new_in_quadrant(int dim, double *center, double width, int max_level, int i){
402 /* a new quadtree in quadrant i of the original cell. The original cell is centered at 'center".
403 The new cell have width "width".
404 */
405 QuadTree qt;
406 int k;
407
408 qt = QuadTree_new(dim, center, width, max_level);
409 center = qt->center;/* right now this has the center for the parent */
410 for (k = 0; k < dim; k++){/* decompose child id into binary, if {1,0}, say, then
411 add {width/2, -width/2} to the parents' center
412 to get the child's center. */
413 if (i%2 == 0){
414 center[k] -= width;
415 } else {
416 center[k] += width;
417 }
418 i = (i - i%2)/2;
419 }
420 return qt;
421
422}
423static QuadTree QuadTree_add_internal(QuadTree q, const double *coord, double weight, int id, int level){
424 int i, dim = q->dim, ii;
425 node_data nd = NULL;
426
427 int max_level = q->max_level;
428 int idd;
429
430 /* Make sure that coord is within bounding box */
431 for (i = 0; i < q->dim; i++) {
432 if (coord[i] < q->center[i] - q->width - 1.e5*MACHINEACC*q->width || coord[i] > q->center[i] + q->width + 1.e5*MACHINEACC*q->width) {
433#ifdef DEBUG_PRINT
434 fprintf(stderr,"coordinate %f is outside of the box:{%f, %f}, \n(q->center[i] - q->width) - coord[i] =%g, coord[i]-(q->center[i] + q->width) = %g\n",coord[i], (q->center[i] - q->width), (q->center[i] + q->width),
435 (q->center[i] - q->width) - coord[i], coord[i]-(q->center[i] + q->width));
436#endif
437 //return NULL;
438 }
439 }
440
441 if (q->n == 0){
442 /* if this node is empty right now */
443 q->n = 1;
444 q->total_weight = weight;
445 q->average = gv_calloc(dim, sizeof(double));
446 for (i = 0; i < q->dim; i++) q->average[i] = coord[i];
447 nd = node_data_new(q->dim, weight, coord, id);
448 assert(!(q->l));
449 q->l = nd;
450 } else if (level < max_level){
451 /* otherwise open up into 2^dim quadtrees unless the level is too high */
452 q->total_weight += weight;
453 for (i = 0; i < q->dim; i++) q->average[i] = (q->average[i] * q->n + coord[i]) / (q->n + 1);
454 if (!q->qts){
455 q->qts = gv_calloc((size_t)1 << dim, sizeof(QuadTree));
456 }/* done adding new quadtree, now add points to them */
457
458 /* insert the old node (if exist) and the current node into the appropriate child quadtree */
460 assert(ii < 1<<dim && ii >= 0);
461 if (q->qts[ii] == NULL) q->qts[ii] = QuadTree_new_in_quadrant(q->dim, q->center, q->width / 2, max_level, ii);
462
463 q->qts[ii] = QuadTree_add_internal(q->qts[ii], coord, weight, id, level + 1);
464 assert(q->qts[ii]);
465
466 if (q->l){
467 idd = q->l->id;
468 assert(q->n == 1);
469 coord = q->l->coord;
470 weight = q->l->node_weight;
472 assert(ii < 1<<dim && ii >= 0);
473
474 if (q->qts[ii] == NULL) q->qts[ii] = QuadTree_new_in_quadrant(q->dim, q->center, (q->width)/2, max_level, ii);
475
476 q->qts[ii] = QuadTree_add_internal(q->qts[ii], coord, weight, idd, level + 1);
477 assert(q->qts[ii]);
478
479 /* delete the old node data on parent */
480 while (q->l != NULL) {
481 node_data next = q->l->next;
483 q->l = next;
484 }
485 }
486
487 q->n++;
488 } else {
489 assert(!(q->qts));
490 /* level is too high, append data in the linked list */
491 q->n++;
492 q->total_weight += weight;
493 for (i = 0; i < q->dim; i++) q->average[i] = (q->average[i] * q->n + coord[i]) / (q->n + 1);
494 nd = node_data_new(q->dim, weight, coord, id);
495 assert(q->l);
496 nd->next = q->l;
497 q->l = nd;
498 }
499 return q;
500}
501
502QuadTree QuadTree_add(QuadTree q, const double *coord, double weight, int id){
503 if (!q) return q;
504 return QuadTree_add_internal(q, coord, weight, id, 0);
505
506}
507
508static void draw_polygon(FILE *fp, int dim, double *center, double width){
509 // plot the enclosing square
510 if (dim < 2 || dim > 3) return;
511 fprintf(fp,"(*in c*){Line[{");
512
513 if (dim == 2){
514 fprintf(fp, "{%f, %f}", center[0] + width, center[1] + width);
515 fprintf(fp, ",{%f, %f}", center[0] - width, center[1] + width);
516 fprintf(fp, ",{%f, %f}", center[0] - width, center[1] - width);
517 fprintf(fp, ",{%f, %f}", center[0] + width, center[1] - width);
518 fprintf(fp, ",{%f, %f}", center[0] + width, center[1] + width);
519 } else if (dim == 3){
520 /* top */
521 fprintf(fp,"{");
522 fprintf(fp, "{%f, %f, %f}", center[0] + width, center[1] + width, center[2] + width);
523 fprintf(fp, ",{%f, %f, %f}", center[0] - width, center[1] + width, center[2] + width);
524 fprintf(fp, ",{%f, %f, %f}", center[0] - width, center[1] - width, center[2] + width);
525 fprintf(fp, ",{%f, %f, %f}", center[0] + width, center[1] - width, center[2] + width);
526 fprintf(fp, ",{%f, %f, %f}", center[0] + width, center[1] + width, center[2] + width);
527 fprintf(fp,"},");
528 /* bot */
529 fprintf(fp,"{");
530 fprintf(fp, "{%f, %f, %f}", center[0] + width, center[1] + width, center[2] - width);
531 fprintf(fp, ",{%f, %f, %f}", center[0] - width, center[1] + width, center[2] - width);
532 fprintf(fp, ",{%f, %f, %f}", center[0] - width, center[1] - width, center[2] - width);
533 fprintf(fp, ",{%f, %f, %f}", center[0] + width, center[1] - width, center[2] - width);
534 fprintf(fp, ",{%f, %f, %f}", center[0] + width, center[1] + width, center[2] - width);
535 fprintf(fp,"},");
536 /* for sides */
537 fprintf(fp,"{");
538 fprintf(fp, "{%f, %f, %f}", center[0] + width, center[1] + width, center[2] - width);
539 fprintf(fp, ",{%f, %f, %f}", center[0] + width, center[1] + width, center[2] + width);
540 fprintf(fp,"},");
541
542 fprintf(fp,"{");
543 fprintf(fp, "{%f, %f, %f}", center[0] - width, center[1] + width, center[2] - width);
544 fprintf(fp, ",{%f, %f, %f}", center[0] - width, center[1] + width, center[2] + width);
545 fprintf(fp,"},");
546
547 fprintf(fp,"{");
548 fprintf(fp, "{%f, %f, %f}", center[0] + width, center[1] - width, center[2] - width);
549 fprintf(fp, ",{%f, %f, %f}", center[0] + width, center[1] - width, center[2] + width);
550 fprintf(fp,"},");
551
552 fprintf(fp,"{");
553 fprintf(fp, "{%f, %f, %f}", center[0] - width, center[1] - width, center[2] - width);
554 fprintf(fp, ",{%f, %f, %f}", center[0] - width, center[1] - width, center[2] + width);
555 fprintf(fp,"}");
556 }
557
558
559 fprintf(fp, "}]}(*end C*)");
560
561
562}
563static void QuadTree_print_internal(FILE *fp, QuadTree q, int level){
564 /* dump a quad tree in Mathematica format. */
565 node_data l, l0;
566 double *coord;
567 int i, dim;
568
569 if (!q) return;
570
571 draw_polygon(fp, q->dim, q->center, q->width);
572 dim = q->dim;
573
574 l0 = l = q->l;
575 if (l){
576 printf(",(*a*) {Red,");
577 while (l){
578 if (l != l0) printf(",");
579 coord = l->coord;
580 fprintf(fp, "(*node %d*) Point[{", l->id);
581 for (i = 0; i < dim; i++){
582 if (i != 0) printf(",");
583 fprintf(fp, "%f",coord[i]);
584 }
585 fprintf(fp, "}]");
586 l = l->next;
587 }
588 fprintf(fp, "}");
589 }
590
591 if (q->qts){
592 for (i = 0; i < 1<<dim; i++){
593 fprintf(fp, ",(*b*){");
594 QuadTree_print_internal(fp, q->qts[i], level + 1);
595 fprintf(fp, "}");
596 }
597 }
598
599
600}
601
602void QuadTree_print(FILE *fp, QuadTree q){
603 if (!fp) return;
604 if (q->dim == 2){
605 fprintf(fp, "Graphics[{");
606 } else if (q->dim == 3){
607 fprintf(fp, "Graphics3D[{");
608 } else {
609 return;
610 }
611 QuadTree_print_internal(fp, q, 0);
612 if (q->dim == 2){
613 fprintf(fp, "}, PlotRange -> All, Frame -> True, FrameTicks -> True]\n");
614 } else {
615 fprintf(fp, "}, PlotRange -> All]\n");
616 }
617}
618
619static void QuadTree_get_nearest_internal(QuadTree qt, const double *x, double *y,
620 double *min, int *imin,
621 bool tentative) {
622 /* get the nearest point years to {x[0], ..., x[dim]} and store in y.*/
623 double *coord, dist;
624 int dim, i, iq = -1;
625 double qmin;
626
627 if (!qt) return;
628 dim = qt->dim;
629 node_data l = qt->l;
630 while (l){
631 coord = l->coord;
633 if(*min < 0 || dist < *min) {
634 *min = dist;
635 *imin = l->id;
636 for (i = 0; i < dim; i++) y[i] = coord[i];
637 }
638 l = l->next;
639 }
640
641 if (qt->qts){
642 dist = point_distance(qt->center, x, dim);
643 if (*min >= 0 && (dist - sqrt((double) dim) * qt->width > *min)){
644 return;
645 } else {
646 if (tentative){/* quick first approximation*/
647 qmin = -1;
648 for (i = 0; i < 1<<dim; i++){
649 if (qt->qts[i]){
650 dist = point_distance(qt->qts[i]->average, x, dim);
651 if (dist < qmin || qmin < 0){
652 qmin = dist; iq = i;
653 }
654 }
655 }
656 assert(iq >= 0);
657 QuadTree_get_nearest_internal(qt->qts[iq], x, y, min, imin, tentative);
658 } else {
659 for (i = 0; i < 1<<dim; i++){
660 QuadTree_get_nearest_internal(qt->qts[i], x, y, min, imin, tentative);
661 }
662 }
663 }
664 }
665
666}
667
668
669void QuadTree_get_nearest(QuadTree qt, double *x, double *ymin, int *imin, double *min){
670
671 *min = -1;
672
673 QuadTree_get_nearest_internal(qt, x, ymin, min, imin, true);
674 QuadTree_get_nearest_internal(qt, x, ymin, min, imin, false);
675}
void QuadTree_get_nearest(QuadTree qt, double *x, double *ymin, int *imin, double *min)
Definition QuadTree.c:669
static void QuadTree_print_internal(FILE *fp, QuadTree q, int level)
Definition QuadTree.c:563
void QuadTree_get_repulsive_force(QuadTree qt, double *force, double *x, double bh, double p, double KP, double *counts)
Definition QuadTree.c:275
static void draw_polygon(FILE *fp, int dim, double *center, double width)
Definition QuadTree.c:508
QuadTree QuadTree_new_in_quadrant(int dim, double *center, double width, int max_level, int i)
Definition QuadTree.c:401
static void QuadTree_get_nearest_internal(QuadTree qt, const double *x, double *y, double *min, int *imin, bool tentative)
Definition QuadTree.c:619
static void node_data_delete(node_data nd)
Definition QuadTree.c:35
static int QuadTree_get_quadrant(int dim, double *center, const double *coord)
Definition QuadTree.c:384
QuadTree QuadTree_add(QuadTree q, const double *coord, double weight, int id)
Definition QuadTree.c:502
static void check_or_realloc_arrays(int dim, int *nsuper, int *nsupermax, double **center, double **supernode_wgts, double **distances)
Definition QuadTree.c:41
void QuadTree_print(FILE *fp, QuadTree q)
Definition QuadTree.c:602
static node_data node_data_new(int dim, double weight, const double *coord, int id)
Definition QuadTree.c:24
static double * get_or_assign_node_force(double *force, int i, node_data l, int dim)
Definition QuadTree.c:111
void QuadTree_get_supernodes(QuadTree qt, double bh, double *pt, int nodeid, int *nsuper, double **center, double **supernode_wgts, double **distances, double *counts)
Definition QuadTree.c:95
QuadTree QuadTree_new(int dim, double *center, double width, int max_level)
Definition QuadTree.c:345
static QuadTree QuadTree_add_internal(QuadTree q, const double *coord, double weight, int id, int level)
Definition QuadTree.c:423
static double * get_or_alloc_force_qt(QuadTree qt, int dim)
Definition QuadTree.c:122
static void QuadTree_repulsive_force_interact(QuadTree qt1, QuadTree qt2, double *x, double *force, double bh, double p, double KP, double *counts)
Definition QuadTree.c:131
static void QuadTree_repulsive_force_accumulate(QuadTree qt, double *force, double *counts)
Definition QuadTree.c:236
QuadTree QuadTree_new_from_point_list(int dim, int n, int max_level, const double *coord)
Definition QuadTree.c:303
static void QuadTree_get_supernodes_internal(QuadTree qt, double bh, double *pt, int nodeid, int *nsuper, int *nsupermax, double **center, double **supernode_wgts, double **distances, double *counts)
Definition QuadTree.c:52
void QuadTree_delete(QuadTree q)
Definition QuadTree.c:363
double distance_cropped(double *x, int dim, int i, int j)
Definition general.c:97
Memory allocation wrappers that exit on failure.
static void * gv_recalloc(void *ptr, size_t old_nmemb, size_t new_nmemb, size_t size)
Definition alloc.h:73
static void * gv_calloc(size_t nmemb, size_t size)
Definition alloc.h:26
static void * gv_alloc(size_t size)
Definition alloc.h:47
static double dist(int dim, double *x, double *y)
double point_distance(const double *p1, const double *p2, int dim)
Definition general.c:112
#define MACHINEACC
Definition general.h:70
geometric types and macros (e.g. points and boxes)
double xmax
Definition geometry.c:17
double ymin
Definition geometry.c:17
double xmin
Definition geometry.c:17
void free(void *)
node NULL
Definition grammar.y:181
static uint64_t id
Definition gv2gml.c:42
static int imin(int a, int b)
minimum of two integers
Definition gv_math.h:35
static const int dim
pointf coord(node_t *n)
Definition utils.c:154
static const double bh
node_data l
Definition QuadTree.h:44
double total_weight
Definition QuadTree.h:37
double * average
Definition QuadTree.h:42
QuadTree * qts
Definition QuadTree.h:43
double * center
Definition QuadTree.h:39
double * coord
Definition QuadTree.h:23
node_data next
Definition QuadTree.h:26
double node_weight
Definition QuadTree.h:22
static point center(point vertex[], size_t n)