-
Notifications
You must be signed in to change notification settings - Fork 3
/
gmx_dump_vir.cpp
3539 lines (3162 loc) · 121 KB
/
gmx_dump_vir.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
*
* This source code is part of
*
* G R O M A C S
*
* GROningen MAchine for Chemical Simulations
*
* VERSION 3.2.0
* Written by David van der Spoel, Erik Lindahl, Berk Hess, and others.
* Copyright (c) 1991-2000, University of Groningen, The Netherlands.
* Copyright (c) 2001-2004, The GROMACS development team,
* check out http://www.gromacs.org for more information.
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* If you want to redistribute modifications, please consider that
* scientific software is very special. Version control is crucial -
* bugs must be traceable. We will be happy to consider code for
* inclusion in the official distribution, but derived work must not
* be called official GROMACS. Details are found in the README & COPYING
* files - if they are missing, get the official version at www.gromacs.org.
*
* To help us fund GROMACS development, we humbly ask that you cite
* the papers on the package - you can find them in the top README file.
*
* For more info, check our website at http://www.gromacs.org
*
* And Hey:
* Green Red Orange Magenta Azure Cyan Skyblue
*/
/*
NOTE: how to acess atom properties
if you have the index of the phase :
for(i=0;i<itim->n[phase];i++){
atom_index = itim->gmx_index[phase][i];
resindex = top->atoms.atom[atom_index].resind;
resname = *top->atoms.resinfo[top->atoms.atom[atom_index].resind].name
atomname = *(top->atoms.atomname[atom_index]);
pos_x = itim->phase[phase][3*i];
}
if you have the index of the alpha-shape:
for(i=0;i<itim->nalphapoints;i++){
phase_index =itim->alpha_index[i];
atom_index =itim->gmx_index[SUPPORT_PHASE][phase_index];
...
}
*/
#if GMX_VERSION < 50000
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
#include <stdlib.h>
#include <math.h>
#include <ctype.h>
#include "string.h"
#include "smalloc.h"
#include "gstat.h"
#include "vec.h"
#include "xvgr.h"
#include "pbc.h"
#include "copyrite.h"
#include "futil.h"
#include "tpxio.h"
#include "gmx_ana.h"
#include <nbsearch.h>
#ifdef UNIX
// used for handling signals
#include <stdio.h>
#include <unistd.h>
#include <sys/signal.h>
#endif //UNIX
#define gmx_ffopen ffopen
#define gmx_ffclose ffclose
#define wrap_gmx_rmpbc_init(a,b,c,d) gmx_rmpbc_init((a),(b),(c),(d))
#define wrap_read_next_x(a,b,c,d,e,f) read_next_x((a),(b),(c),(d),(e),(f))
#else
#define wrap_gmx_rmpbc_init(a,b,c,d) gmx_rmpbc_init((a),(b),(c))
#define wrap_read_next_x(a,b,c,d,e,f) read_next_x((a),(b),(c),(e),(f))
//#include "gmxpre.h"
#include "gromacs/commandline/cmdlinemodulemanager.h"
#include "gromacs/commandline/cmdlinehelpcontext.h"
#include <ctype.h>
#include <math.h>
#include <stdlib.h>
#include <string.h>
#include "commandline/pargs.h"
#include "tpxio.h"
#include "trxio.h"
#include "xvgr.h"
#include "gmx_ana.h"
#include "gstat.h"
#include "macros.h"
#include "typedefs.h"
#include "viewit.h"
#include "vec.h"
#include "pbc.h"
#include "rmpbc.h"
#include "index.h"
#include "cstringutil.h"
#include "file.h"
#include "futil.h"
#include "smalloc.h"
#endif
#ifdef VIRIAL_EXTENSION
// this is defined in tpxio.h, in case...
#warning COMPILING THE CODE USING THE VIRIAL_EXTENSION
#endif
#ifdef TIME_PROFILE
#include <sys/time.h>
#endif
#define NORMAL_UNDEFINED -100
#define LAYER_OFFSET 10000
using namespace std;
int global_interrupt = 0;
typedef enum { SUPPORT_PHASE=0, INNER_PHASE=1, OUTER_PHASE=2 } PHASE; // These value are not arbitrary and should not be changed.
/* These value are not arbitrary and should not be changed:
OFF_NUMBER -> for number density profile, used to normalize the order parameter density profile as well.
OFF_ORDER[12] -> for order parameter density profile wrt macroscopic axis
OFF_ORDER[34] -> for order parameter density profile wrt microscopic axis
*/
typedef enum { OFF_NUMBER=4, OFF_ORDER1=7, OFF_ORDER1_2=10, OFF_ORDER2=13, OFF_ORDER2_2=16,
OFF_ORDER3=19, OFF_ORDER3_2=22, OFF_ORDER4=25, OFF_ORDER4_2=28} HISTO_OFFSET;
typedef enum {SURFACE_PLANE, SURFACE_SPHERE, SURFACE_CYLINDER, SURFACE_GENERIC } GEOMETRY;
typedef enum { NONE, PATCH, FULL} PERIODIC ;
typedef enum { METHOD_ITIM, METHOD_A_SHAPE} METHOD;
typedef struct {
double * rdata;
double size;
double minsize;
int nbins;
int N;
int iterations;
double bWidth;
} Histogram;
typedef struct {
struct kdtree * tree;
int * flag;
int n[2];
real size[2];
int nelem;
} MESH;
/* TODO: put alphashape / surface variables in a separate structure, to be included here ?*/
typedef struct {
real box[3];
real alpha;
real * masses;
real * charges;
real skin;
real range;
real *pradii;
int nphases;
int RANDOM_PHASE;
int ngmxphases;
int normal;
int info;
int n_histo;
int nadd_index;
real ** phase;
int ** phase_index;
int * inclusive_map;
int dump_mol;
int *n;
real * alphapoints;
int nalphapoints;
int current_layer;
int maxlayers;
int * alpha_index;
real * radii;
int * mask;
int ** mask_add;
int * gmx_alpha_id;
int * gmx_alpha_phase_id;
int ** gmx_index;
int *indexm; // for the molecular version of the support group.
int *backindex; // for the molecular version of the support group.
int com_opt[64];
int bCom;
int bMol;
int bInclusive;
int bMCnormalization;
int bOrder;
int side;
real target_mesh_size;
MESH mesh;
GEOMETRY geometry;
PERIODIC *periodic;
METHOD method;
char method_name[2][128];
void (* dump_surface_points)(t_topology*,FILE*);
void (* dump_slabs)(t_topology*);
void (* dump_surface_molecules)(t_topology*,FILE*,atom_id **);
void (* dump_phase_points)(PHASE,t_topology*,FILE*);
Histogram * histograms;
} ITIM;
ITIM * global_itim;
#ifndef _KDTREE_H_
#define _KDTREE_H_
#ifdef __cplusplus
extern "C" {
#endif
#define my_min(a,b) ( (a) > (b) ? (b) : (a))
double *CLUSTERCUT; /* group dependent cut-off for cluster analysis */ // gh
double *CLUSTERCUT2; /* group dependent cut-off for cluster analysis */ // gh
// TODO: make it an option? change the algorithm ?
#define MAX_KDTREE_CHECK 60
struct kdtree;
struct kdres;
//typedef float real;
/* create a kd-tree for "k"-dimensional data */
struct kdtree *kd_create(int k);
/* free the struct kdtree */
void kd_free(struct kdtree *tree);
/* remove all the elements from the tree */
void kd_clear(struct kdtree *tree);
/* if called with non-null 2nd argument, the function provided
* will be called on data pointers (see kd_insert) when nodes
* are to be removed from the tree.
*/
void kd_data_destructor(struct kdtree *tree, void (*destr)(void*));
/* insert a node, specifying its position, and optional data */
int kd_insert(struct kdtree *tree, const real *pos, real *data);
int kd_insertf(struct kdtree *tree, const float *pos, real *data);
int kd_insert3(struct kdtree *tree, real x, real y, real z, real *data);
int kd_insert3p(struct kdtree *tree, real x, real y, real z, real *data,real cut);
int kd_insert3f(struct kdtree *tree, float x, float y, float z, real *data);
/* Find one of the nearest nodes from the specified point.
*
* This function returns a pointer to a result set with at most one element.
*/
struct kdres *kd_nearest(struct kdtree *tree, const real *pos);
struct kdres *kd_nearestf(struct kdtree *tree, const float *pos);
struct kdres *kd_nearest3(struct kdtree *tree, real x, real y, real z);
struct kdres *kd_nearest3f(struct kdtree *tree, float x, float y, float z);
/* Find any nearest nodes from the specified point within a range.
*
* This function returns a pointer to a result set, which can be manipulated
* by the kd_res_* functions.
* The returned pointer can be null as an indication of an error. Otherwise
* a valid result set is always returned which may contain 0 or more elements.
* The result set must be deallocated with kd_res_free, after use.
*/
struct kdres *kd_nearest_range(struct kdtree *tree, const real *pos, real range);
struct kdres *kd_nearest_rangef(struct kdtree *tree, const float *pos, float range);
struct kdres *kd_nearest_range3(struct kdtree *tree, real x, real y, real z, real range);
struct kdres *kd_nearest_range3f(struct kdtree *tree, float x, float y, float z, float range);
/* frees a result set returned by kd_nearest_range() */
void kd_res_free(struct kdres *set);
/* returns the size of the result set (in elements) */
int kd_res_size(struct kdres *set);
/* rewinds the result set iterator */
void kd_res_rewind(struct kdres *set);
/* returns non-zero if the set iterator reached the end after the last element */
int kd_res_end(struct kdres *set);
/* advances the result set iterator, returns non-zero on success, zero if
* there are no more elements in the result set.
*/
int kd_res_next(struct kdres *set);
/* returns the data pointer (can be null) of the current result set item
* and optionally sets its position to the pointers(s) if not null.
*/
void *kd_res_item(struct kdres *set, real *pos);
void *kd_res_itemf(struct kdres *set, float *pos);
void *kd_res_item3(struct kdres *set, real *x, real *y, real *z);
void *kd_res_item3f(struct kdres *set, float *x, float *y, float *z);
/* equivalent to kd_res_item(set, 0) */
void *kd_res_item_data(struct kdres *set);
real compute_osculating_sphere_radius(real p[3], real q[3], real r[3] ,real s[3], real wp, real wq, real wr, real ws);
real interpolate_distance3D(real *A,real *B,real *C,real *I);
#ifdef __cplusplus
}
#endif
#endif /* _KDTREE_H_ */
#define NO_ALLOCA
#if defined(WIN32) || defined(__WIN32__)
#include <malloc.h>
#endif
#ifdef USE_LIST_NODE_ALLOCATOR
#ifndef NO_PTHREADS
#include <pthread.h>
#else
#ifndef I_WANT_THREAD_BUGS
#error "You are compiling with the fast list node allocator, with pthreads disabled! This WILL break if used from multiple threads."
#endif /* I want thread bugs */
#endif /* pthread support */
#endif /* use list node allocator */
struct kdhyperrect {
int dim;
real *min, *max; /* minimum/maximum coords */
};
struct kdnode {
real *pos;
int dir;
real *data;
struct kdnode *left, *right; /* negative/positive side */
};
struct res_node {
struct kdnode *item;
real dist_sq;
struct res_node *next;
};
struct kdtree {
int dim;
struct kdnode *root;
struct kdhyperrect *rect;
void (*destr)(void*);
};
struct kdres {
struct kdtree *tree;
struct res_node *rlist, *riter;
int size;
};
#define SQ(x) ((x) * (x))
static void clear_rec(struct kdnode *node, void (*destr)(void*));
static int insert_rec(struct kdnode **node, const real *pos, real *data, int dir, int dim);
static int rlist_insert(struct res_node *list, struct kdnode *item, real dist_sq);
static void clear_results(struct kdres *set);
static struct kdhyperrect* hyperrect_create(int dim, const real *min, const real *max);
static void hyperrect_free(struct kdhyperrect *rect);
static struct kdhyperrect* hyperrect_duplicate(const struct kdhyperrect *rect);
static void hyperrect_extend(struct kdhyperrect *rect, const real *pos);
static real hyperrect_dist_sq(struct kdhyperrect *rect, const real *pos);
#ifdef USE_LIST_NODE_ALLOCATOR
static struct res_node *alloc_resnode(void);
static void free_resnode(struct res_node*);
#else
#define alloc_resnode() (struct res_node*)malloc(sizeof(struct res_node))
#define free_resnode(n) free(n)
#endif
struct kdtree *kd_create(int k)
{
struct kdtree *tree;
if(!(tree = (struct kdtree*)malloc(sizeof *tree))) {
return 0;
}
tree->dim = k;
tree->root = 0;
tree->destr = free;
tree->rect = 0;
return tree;
}
void kd_free(struct kdtree *tree)
{
if(tree) {
kd_clear(tree);
free(tree);
}
}
static void clear_rec(struct kdnode *node, void (*destr)(void*))
{
if(!node) return;
clear_rec(node->left, destr);
clear_rec(node->right, destr);
// if(destr) {
// destr(node->data);
// }
free(node->pos);
free(node);
}
void kd_clear(struct kdtree *tree)
{
clear_rec(tree->root, tree->destr);
tree->root = 0;
if (tree->rect) {
hyperrect_free(tree->rect);
tree->rect = 0;
}
}
void kd_data_destructor(struct kdtree *tree, void (*destr)(void*))
{
tree->destr = destr;
}
static int insert_rec(struct kdnode **nptr, const real *pos, real *data, int dir, int dim)
{
int new_dir;
struct kdnode *node;
if(!*nptr) {
if(!(node = (struct kdnode*)malloc(sizeof *node))) {
return -1;
}
if(!(node->pos = (real*)malloc(dim * sizeof *node->pos))) {
free(node);
return -1;
}
// if(!(node->data = (real *) malloc(sizeof *node->data))) {
// free(node);
// return -1;
// }
memcpy(node->pos, pos, dim * sizeof *node->pos);
// memcpy(node->data, data, sizeof *node->data);
node->data = data;
node->dir = dir;
node->left = node->right = 0;
*nptr = node;
return 0;
}
node = *nptr;
new_dir = (node->dir + 1) % dim;
if(pos[node->dir] < node->pos[node->dir]) {
return insert_rec(&(*nptr)->left, pos, data, new_dir, dim);
}
return insert_rec(&(*nptr)->right, pos, data, new_dir, dim);
}
int kd_insert(struct kdtree *tree, const real *pos, real *data)
{
if (insert_rec(&tree->root, pos, data, 0, tree->dim)) {
return -1;
}
if (tree->rect == 0) {
tree->rect = hyperrect_create(tree->dim, pos, pos);
} else {
hyperrect_extend(tree->rect, pos);
}
return 0;
}
int kd_insertf(struct kdtree *tree, const float *pos, real *data)
{
static real sbuf[16];
real *bptr, *buf = 0;
int res, dim = tree->dim;
if(dim > 16) {
#ifndef NO_ALLOCA
if(dim <= 256)
bptr = buf = alloca(dim * sizeof *bptr);
else
#endif
if(!(bptr = buf = (real*)malloc(dim * sizeof *bptr))) {
return -1;
}
} else {
bptr = sbuf;
}
while(dim-- > 0) {
*bptr++ = *pos++;
}
res = kd_insert(tree, buf, data);
#ifndef NO_ALLOCA
if(tree->dim > 256)
#else
if(tree->dim > 16)
#endif
free(buf);
return res;
}
int kd_insert3(struct kdtree *tree, real x, real y, real z, real *data)
{
real buf[3];
buf[0] = x;
buf[1] = y;
buf[2] = z;
return kd_insert(tree, buf, data);
}
int kd_insert3p(struct kdtree *tree, real x, real y, real z, real *data, real cut)
{
ITIM* itim = global_itim;
int i,j,k,ret,count=0;
real buf[3],buf2[3];
buf[0]=x;
buf[1]=y;
buf[2]=z;
ret = kd_insert(tree, buf, data);
if(cut>0){
for(i=-1;i<2;i++){
if( (i==-1 && buf[0]+cut > itim->box[0]/2.) || (i==1 && buf[0]-cut < -itim->box[0]/2.) || i==0 ){
buf2[0]=buf[0]+i*itim->box[0];
for(j=-1;j<2;j++){
if( (j==-1 && buf[1]+cut > itim->box[1]/2.) || (j==1 && buf[1]-cut < -itim->box[1]/2.) || j==0 ){
buf2[1]=buf[1]+j*itim->box[1];
for(k=-1;k<2;k++){
if( (k==-1 && buf[2]+cut > itim->box[2]/2.) || (k==1 && buf[2]-cut < -itim->box[2]/2.) || k==0 ){
buf2[2]=buf[2]+k*itim->box[2];
if(k==0 && j==0 && i==0) continue;
ret = kd_insert(tree, buf2, data);
count++;
}
}
}
}
}
}
}
return ret;
}
int kd_insert3f(struct kdtree *tree, float x, float y, float z, real *data)
{
real buf[3];
buf[0] = x;
buf[1] = y;
buf[2] = z;
return kd_insert(tree, buf, data);
}
static int find_nearest(struct kdnode *node, const real *pos, real range, struct res_node *list, int ordered, int dim)
{
real dist_sq, dx;
int i, ret, added_res = 0;
ITIM * itim = global_itim ;
if(!node) return 0;
dist_sq = 0;
if(itim!=NULL) {
for(i=0; i<dim; i++) {
dist_sq += SQ(node->pos[i] - pos[i]);
}
} else {
for(i=0; i<dim; i++) {
real dist = node->pos[i] - pos[i];
while(dist > itim->box[i]/2.) dist-=itim->box[i];
while(dist < -itim->box[i]/2.) dist+=itim->box[i];
dist_sq += SQ(dist);
}
}
if(dist_sq <= SQ(range)) {
if(rlist_insert(list, node, ordered ? dist_sq : -1.0) == -1) {
return -1;
}
added_res = 1;
}
dx = pos[node->dir] - node->pos[node->dir];
ret = find_nearest(dx <= 0.0 ? node->left : node->right, pos, range, list, ordered, dim);
if(ret >= 0 && fabs(dx) < range) {
added_res += ret;
ret = find_nearest(dx <= 0.0 ? node->right : node->left, pos, range, list, ordered, dim);
}
if(ret == -1) {
return -1;
}
added_res += ret;
return added_res;
}
static void kd_nearest_i(struct kdnode *node, const real *pos, struct kdnode **result, real *result_dist_sq, struct kdhyperrect* rect)
{
int dir = node->dir;
int i, side;
real dummy, dist_sq;
struct kdnode *nearer_subtree, *farther_subtree;
real *nearer_hyperrect_coord, *farther_hyperrect_coord;
/* Decide whether to go left or right in the tree */
dummy = pos[dir] - node->pos[dir];
if (dummy <= 0) {
nearer_subtree = node->left;
farther_subtree = node->right;
nearer_hyperrect_coord = rect->max + dir;
farther_hyperrect_coord = rect->min + dir;
side = 0;
} else {
nearer_subtree = node->right;
farther_subtree = node->left;
nearer_hyperrect_coord = rect->min + dir;
farther_hyperrect_coord = rect->max + dir;
side = 1;
}
if (nearer_subtree) {
/* Slice the hyperrect to get the hyperrect of the nearer subtree */
dummy = *nearer_hyperrect_coord;
*nearer_hyperrect_coord = node->pos[dir];
/* Recurse down into nearer subtree */
kd_nearest_i(nearer_subtree, pos, result, result_dist_sq, rect);
/* Undo the slice */
*nearer_hyperrect_coord = dummy;
}
/* Check the distance of the point at the current node, compare it
* with our best so far */
dist_sq = 0;
for(i=0; i < rect->dim; i++) {
dist_sq += SQ(node->pos[i] - pos[i]);
}
if (dist_sq < *result_dist_sq) {
*result = node;
*result_dist_sq = dist_sq;
}
if (farther_subtree) {
/* Get the hyperrect of the farther subtree */
dummy = *farther_hyperrect_coord;
*farther_hyperrect_coord = node->pos[dir];
/* Check if we have to recurse down by calculating the closest
* point of the hyperrect and see if it's closer than our
* minimum distance in result_dist_sq. */
if (hyperrect_dist_sq(rect, pos) < *result_dist_sq) {
/* Recurse down into farther subtree */
kd_nearest_i(farther_subtree, pos, result, result_dist_sq, rect);
}
/* Undo the slice on the hyperrect */
*farther_hyperrect_coord = dummy;
}
}
struct kdres *kd_nearest(struct kdtree *kd, const real *pos)
{
struct kdhyperrect *rect;
struct kdnode *result;
struct kdres *rset;
real dist_sq;
int i;
if (!kd) return 0;
if (!kd->rect) return 0;
/* Allocate result set */
if(!(rset = (struct kdres *)malloc(sizeof *rset))) {
return 0;
}
if(!(rset->rlist = alloc_resnode())) {
free(rset);
return 0;
}
rset->rlist->next = 0;
rset->tree = kd;
/* Duplicate the bounding hyperrectangle, we will work on the copy */
if (!(rect = hyperrect_duplicate(kd->rect))) {
kd_res_free(rset);
return 0;
}
/* Our first guesstimate is the root node */
result = kd->root;
dist_sq = 0;
for (i = 0; i < kd->dim; i++)
dist_sq += SQ(result->pos[i] - pos[i]);
/* Search for the nearest neighbour recursively */
kd_nearest_i(kd->root, pos, &result, &dist_sq, rect);
/* Free the copy of the hyperrect */
hyperrect_free(rect);
/* Store the result */
if (result) {
if (rlist_insert(rset->rlist, result, -1.0) == -1) {
kd_res_free(rset);
return 0;
}
rset->size = 1;
kd_res_rewind(rset);
return rset;
} else {
kd_res_free(rset);
return 0;
}
}
struct kdres *kd_nearestf(struct kdtree *tree, const float *pos)
{
static real sbuf[16];
real *bptr, *buf = 0;
int dim = tree->dim;
struct kdres *res;
if(dim > 16) {
#ifndef NO_ALLOCA
if(dim <= 256)
bptr = buf = alloca(dim * sizeof *bptr);
else
#endif
if(!(bptr = buf = (real *)malloc(dim * sizeof *bptr))) {
return 0;
}
} else {
bptr = sbuf;
}
while(dim-- > 0) {
*bptr++ = *pos++;
}
res = kd_nearest(tree, buf);
#ifndef NO_ALLOCA
if(tree->dim > 256)
#else
if(tree->dim > 16)
#endif
free(buf);
return res;
}
struct kdres *kd_nearest3(struct kdtree *tree, real x, real y, real z)
{
real pos[3];
pos[0] = x;
pos[1] = y;
pos[2] = z;
return kd_nearest(tree, pos);
}
struct kdres *kd_nearest3f(struct kdtree *tree, float x, float y, float z)
{
real pos[3];
pos[0] = x;
pos[1] = y;
pos[2] = z;
return kd_nearest(tree, pos);
}
struct kdres *kd_nearest_range(struct kdtree *kd, const real *pos, real range)
{
int ret;
struct kdres *rset;
if(!(rset = (struct kdres *) malloc(sizeof *rset))) {
return 0;
}
if(!(rset->rlist = alloc_resnode())) {
free(rset);
return 0;
}
rset->rlist->next = 0;
rset->tree = kd;
if((ret = find_nearest(kd->root, pos, range, rset->rlist, 1, kd->dim)) == -1) {
kd_res_free(rset);
return 0;
}
rset->size = ret;
kd_res_rewind(rset);
return rset;
}
struct kdres *kd_nearest_rangef(struct kdtree *kd, const float *pos, float range)
{
static real sbuf[16];
real *bptr, *buf = 0;
int dim = kd->dim;
struct kdres *res;
if(dim > 16) {
#ifndef NO_ALLOCA
if(dim <= 256)
bptr = buf = alloca(dim * sizeof *bptr);
else
#endif
if(!(bptr = buf = (real *)malloc(dim * sizeof *bptr))) {
return 0;
}
} else {
bptr = sbuf;
}
while(dim-- > 0) {
*bptr++ = *pos++;
}
res = kd_nearest_range(kd, buf, range);
#ifndef NO_ALLOCA
if(kd->dim > 256)
#else
if(kd->dim > 16)
#endif
free(buf);
return res;
}
struct kdres *kd_nearest_range3(struct kdtree *tree, real x, real y, real z, real range)
{
real buf[3];
buf[0] = x;
buf[1] = y;
buf[2] = z;
return kd_nearest_range(tree, buf, range);
}
struct kdres *kd_nearest_range3f(struct kdtree *tree, float x, float y, float z, float range)
{
real buf[3];
buf[0] = x;
buf[1] = y;
buf[2] = z;
return kd_nearest_range(tree, buf, range);
}
void kd_res_free(struct kdres *rset)
{
clear_results(rset);
free_resnode(rset->rlist);
free(rset);
}
int kd_res_size(struct kdres *set)
{
return (set->size);
}
void kd_res_rewind(struct kdres *rset)
{
rset->riter = rset->rlist->next;
}
int kd_res_end(struct kdres *rset)
{
return rset->riter == 0;
}
int kd_res_next(struct kdres *rset)
{
rset->riter = rset->riter->next;
return rset->riter != 0;
}
void *kd_res_item(struct kdres *rset, real *pos)
{
if(rset->riter) {
if(pos) {
memcpy(pos, rset->riter->item->pos, rset->tree->dim * sizeof *pos);
}
return rset->riter->item->data;
}
return 0;
}
void *kd_res_itemf(struct kdres *rset, float *pos)
{
if(rset->riter) {
if(pos) {
int i;
for(i=0; i<rset->tree->dim; i++) {
pos[i] = rset->riter->item->pos[i];
}
}
return rset->riter->item->data;
}
return 0;
}
void *kd_res_item3(struct kdres *rset, real *x, real *y, real *z)
{
if(rset->riter) {
if(*x) *x = rset->riter->item->pos[0];
if(*y) *y = rset->riter->item->pos[1];
if(*z) *z = rset->riter->item->pos[2];
}
return 0;
}
void *kd_res_item3f(struct kdres *rset, float *x, float *y, float *z)
{
if(rset->riter) {
if(*x) *x = rset->riter->item->pos[0];
if(*y) *y = rset->riter->item->pos[1];
if(*z) *z = rset->riter->item->pos[2];
}
return 0;
}
void *kd_res_item_data(struct kdres *set)
{
return kd_res_item(set, 0);
}
/* ---- hyperrectangle helpers ---- */
static struct kdhyperrect* hyperrect_create(int dim, const real *min, const real *max)
{
size_t size = dim * sizeof(real);
struct kdhyperrect* rect = 0;
if (!(rect =(struct kdhyperrect* )malloc(sizeof(struct kdhyperrect)))) {
return 0;
}
rect->dim = dim;
if (!(rect->min = (real *)malloc(size*sizeof(real*)))) {
free(rect);
return 0;
}
if (!(rect->max = (real *)malloc(size*sizeof(real*)))) {
free(rect->min);
free(rect);
return 0;
}
memcpy(rect->min, min, size);
memcpy(rect->max, max, size);
return rect;
}
static void hyperrect_free(struct kdhyperrect *rect)
{
free(rect->min);
free(rect->max);
free(rect);
}
static struct kdhyperrect* hyperrect_duplicate(const struct kdhyperrect *rect)
{
return hyperrect_create(rect->dim, rect->min, rect->max);
}
static void hyperrect_extend(struct kdhyperrect *rect, const real *pos)
{
int i;
for (i=0; i < rect->dim; i++) {
if (pos[i] < rect->min[i]) {
rect->min[i] = pos[i];
}
if (pos[i] > rect->max[i]) {
rect->max[i] = pos[i];
}