-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdirichlet.c
More file actions
executable file
·1688 lines (1456 loc) · 50.3 KB
/
dirichlet.c
File metadata and controls
executable file
·1688 lines (1456 loc) · 50.3 KB
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
/****************************************************************************
* dirichlet.c
* Author Joel Welling
* Copyright 1991, Pittsburgh Supercomputing Center, Carnegie Mellon University
*
* Permission use, copy, and modify this software and its documentation
* without fee for personal use or use within your organization is hereby
* granted, provided that the above copyright notice is preserved in all
* copies and that that copyright and this permission notice appear in
* supporting documentation. Permission to redistribute this software to
* other organizations or individuals is not granted; that must be
* negotiated with the PSC. Neither the PSC nor Carnegie Mellon
* University make any representations about the suitability of this
* software for any purpose. It is provided "as is" without express or
* implied warranty.
*****************************************************************************/
/* This module is an implementation of A. Bowyer's algorithm to calculate
* the Dirichlet tesselation of a set of randomly distributed data points.
* See The Computer Journal, Vol. 24, No. 2, 1981, pp. 162-166 for the
* algorithm. The algorithm runs in O( k**(1+1/n) ) time, where k is
* the number of data points and n is the dimensionality of space. The
* process used is to create a valid tesselation from contrived data
* (the 'bogus points' mentioned below), and then add additional points
* in such a way that at every step the tesselation is valid.
*
* This implementation will work in two or more dimensions. The data
* structure returned includes not only the Dirichlet tesselation of
* the data points, but also the Voronoi polyhedra dual to the tesselation.
* The complete connectivity mesh of data points (called points in the
* code), Voronoi vertices (called vertices), and their neighbor and
* forming point relationships is maintained.
*
* Boyer's algorithm includes a simplex which completely encloses the
* given data points. The points of that simplex are the 'bogus points'
* mentioned in the code.
*
* The function dch_create_dirichlet_tess creates and returns a
* tesselation, and dch_destroy_tesselation destroys the data structure
* returned.
*
*
* Calling Information:
*
* The two routines supplied by this package are:
*
* dch_Tess *dch_create_dirichlet_tess( float *coorddata, int npts,
* int dim, float *(*access_coords)(float *, int, P_Void_ptr *) )
*
* void dch_destroy_tesselation ___(( dch_Tess *tess_to_destroy ))
* Tesselations are created with dch_create_dirichlet_tess, and
* the structure returned is destroyed with dch_destroy_tesselation.
* Calling parameters to dch_create_dirichlet_tess are used to
* create the data points to tesselate, as follows. dim specifies the
* dimensionality of the space; it must be 2 or greater. npts specifies
* the number of points to be tesselated. access_coords should be a
* pointer to a function defined as follows:
*
* float *access_coords( float *coorddata, int ipt, P_Void_ptr *hook )
*
* It is called for each ipt from 0 through npts-1, and in each case
* should return a pointer to an array of dim floats representing
* coordinates. The floats are immediately recopied, so it is not
* necessary to allocate memory for them. hook is a pointer to a
* P_Void_ptr which the access_coords routine may set; the value
* set is stored in the 'user_hook' field of the resulting vertex
* and may be used to point back to the user's data. (Note that
* the 'user_hook' fields of the bogus points are left null). The
* vertices of the tesselation also have 'user_hook' fields which are
* available to the user, though these must be set explicitly in the
* returned tesselation. The 'deleted' fields of the vertices of the
* tesselation will all be 0 when the tesselation is returned, and
* can also be used by the calling routine.
*
* dch_destroy_tesselation deallocates all memory associated with the
* tesselation. Note that it does not deallocate any memory which the
* user may have hung on the various 'user_hook' fields.
*
*
* Implementation Details:
*
* Boyer's algorithm includes a simplex which completely encloses the
* given data points. The points of that simplex are the 'bogus points'
* mentioned in the code.
*
* Each point has associated with it a list of neighboring points, and
* a list of vertices of which the point is a forming point. Each vertex
* has associated with it a list of forming points, and a list of
* neighboring vertices. Note that the list of neighboring vertices
* will be null unless MAINTAIN_VTX_NEIGHBOR_LISTS is defined; this
* is done to avoid the significant memory use associated with the
* neighbor lists.
*
* The first step of Boyer's algorithm for adding a point to a valid
* Dirichlet tesselation requires that a vertex which is closer to the
* new points than to its forming points be found. This is accomplished
* by walking the lattice of already-existing points until the point
* closest to the new point is found, and then searching the vertices of
* that (existing) point to find a deleted vertex. (Note that the vertex
* closest to the new point is not necessarily deleted). The starting
* point for the walk is either the most recently added point or the
* point closest to the center of the tesselation. Which is used is
* determined by trying both for the first few points ('few' being set
* by the constant TRIAL_SEARCHES) and then using the method which works
* best over that sample.
*
* Some of the vertices (those with n bogus points as forming points,
* where n is the dimensionality of space) are located at infinity.
* This is denoted by storing null pointers in their vtx->coords slots,
* but with the vertex's 'degenerate' flag not set. Degenerate vertices
* may also form if the data contains cyclic collections of n+1 points
* all lieing in a hyperplane of the n dimensional space. Such vertices
* also have null vtx->coords slots, but their 'degenerate' flag is set.
* The forming points of such vertices form Delaunay simplices with
* zero volume.
*
* Memory usage:
*
* Experience has shown that for n random points in 3D, memory usage
* is roughly as follows:
*
* points: n
* vertices: about 6.5 n
* point list cells: about 40 n
* vertex list cells: about 60 n if MAINTAIN_VTX_NEIGHBOR_LISTS is
* defined; about 30 n otherwise.
* point pair cells: less than 50 (doesn't vary with n)
*
* The code provides for vertex pair cells, but doesn't use them.
*
*/
#include <math.h>
#include <stdio.h>
#include "p3dgen.h"
#include "ge_error.h"
#include "dirichlet.h"
/* Notes:
-merging of vertices may never be necessary
*/
/* Uncomment the following to cause vertex neighbor lists to be maintained */
/*
*/
#define MAINTAIN_VTX_NEIGHBOR_LISTS
/* Some parameters */
#define SIMPLEX_MARGIN 0.1 /* buffer around points in outer bounding simplex */
#define TRIAL_SEARCHES 20 /* number of times to try both methods of finding
the closest point to a new point */
#define PROXIMITY_TOLERANCE 0.000001 /* factor used to determine if points
or vertices are too close together */
/* Dimensionality, and some globals to help memory management */
static int current_dim= 0;
static int current_vtx_id= 0;
static int current_pt_id= 0;
static int created_vtx_count= 0;
static int created_vtx_highwater= 0;
static int created_pt_count= 0;
static int created_pt_highwater= 0;
static int created_vtx_list_count= 0;
static int created_vtx_list_highwater= 0;
static int created_pt_list_count= 0;
static int created_pt_list_highwater= 0;
static int created_vtx_pair_count= 0;
static int created_vtx_pair_highwater= 0;
static int created_pt_pair_count= 0;
static int created_pt_pair_highwater= 0;
static int dataset_size;
/* Space to perform Gaussian eliminations in */
static float *gauss_matrix= (float *)0;
/* Points from which to hang free cell lists */
static dch_Vtx_list *free_vtx_list_cells= (dch_Vtx_list *)0;
static dch_Pt_list *free_pt_list_cells= (dch_Pt_list *)0;
static dch_Vtx *free_vtx_cells= (dch_Vtx *)0; /* connected via
'neighbors' slots */
/* Some forward definitions */
static void destroy_vtx_list( dch_Vtx_list * );
static void destroy_pt_list( dch_Pt_list * );
static void destroy_vtx( dch_Vtx * );
static void destroy_pt( dch_Pt * );
static void mem_init(int npts, int dim)
{
ger_debug("mem_init: %d pts of dimension %d",npts,dim);
dataset_size= npts;
current_dim= dim;
current_vtx_id= 0;
current_pt_id= 0;
if (gauss_matrix) free( (P_Void_ptr)gauss_matrix );
if ( !(gauss_matrix= (float *)malloc( dim*(dim+1)*sizeof(float) )) )
ger_fatal("mem_init: unable to allocate %d floats!",dim*(dim+1));
}
static float distance( float *coords1, float *coords2 )
/* This actually returns the squared distance, since it is only
* for comparison.
*/
{
int i;
float result= 0.0;
if(!coords1 || !coords2) ger_fatal("distance: Null coords in distance!");
for (i=0; i<current_dim; i++) {
result += ( *coords1 - *coords2 ) * ( *coords1 - *coords2 );
coords1++;
coords2++;
}
return(result);
}
static float dist_to_center( float *coords, dch_Tess *tess )
/* This actually returns the squared distance to the center of the
* tesselation's bounding box, since it is only for comparison.
*/
{
int i;
float result= 0.0;
float term;
for (i=0; i<current_dim; i++) {
term= *coords - 0.5*( tess->bndbx->corner1[i] + tess->bndbx->corner2[i] );
result += term*term;
coords++;
}
return(result);
}
static int too_close( float *coords1, float *coords2, dch_Tess *tess )
{
return( ( distance(coords1,coords2) <=
PROXIMITY_TOLERANCE*tess->characteristic_length ) );
}
static dch_Vtx *create_vtx( float *coords )
{
dch_Vtx *vtx;
int i;
if (!free_vtx_cells) {
if ( !(vtx= (dch_Vtx *)malloc(sizeof(dch_Vtx))) )
ger_fatal("create_vtx: unable to allocate %d bytes!",sizeof(dch_Vtx));
}
else {
vtx= free_vtx_cells;
free_vtx_cells= (dch_Vtx *)vtx->neighbors;
}
if (coords) { /* don't do this for vertices at infinity */
if ( !(vtx->coords= (float *)malloc(current_dim * sizeof(float))) )
ger_fatal("create_vtx: unable to allocate %d floats!",current_dim);
for (i=0; i<current_dim; i++) vtx->coords[i]= coords[i];
}
else vtx->coords= (float *)0;
vtx->distance= 0.0;
vtx->deleted= 0;
vtx->degenerate= 0;
vtx->forming_pts= (dch_Pt_list *)0;
vtx->neighbors= (dch_Vtx_list *)0;
vtx->id= current_vtx_id++;
vtx->user_hook= (P_Void_ptr)0;
created_vtx_count++;
if (created_vtx_count > created_vtx_highwater)
created_vtx_highwater= created_vtx_count;
return (vtx);
}
static void add_list_vtx( dch_Vtx_list **vlist, dch_Vtx *vtx )
/* Add the given vertex to the given list */
{
dch_Vtx_list *temp;
if ( !free_vtx_list_cells ) { /* need to create a cell */
if ( !(temp= (dch_Vtx_list *)malloc(sizeof(dch_Vtx_list))) )
ger_fatal("add_list_vtx: unable to allocate %d bytes!",
sizeof(dch_Vtx_list));
}
else {
temp= free_vtx_list_cells;
free_vtx_list_cells= temp->next;
}
temp->next= *vlist;
temp->vtx= vtx;
*vlist= temp;
created_vtx_list_count++;
if (created_vtx_list_count > created_vtx_list_highwater)
created_vtx_list_highwater= created_vtx_list_count;
}
static void add_pair_vtxs( dch_Vtx_pair_list **vplist,
dch_Vtx *vtx1, dch_Vtx *vtx2 )
{
dch_Vtx_pair_list *temp;
if ( !(temp= (dch_Vtx_pair_list *)malloc(sizeof(dch_Vtx_pair_list))) )
ger_fatal("add_pair_vtxs: unable to allocate %d bytes!",
sizeof(dch_Vtx_pair_list));
temp->next= *vplist;
temp->vtx1= vtx1;
temp->vtx2= vtx2;
*vplist= temp;
created_vtx_pair_count++;
if (created_vtx_pair_count > created_vtx_pair_highwater)
created_vtx_pair_highwater= created_vtx_pair_count;
}
static void remove_list_vtx( dch_Vtx_list **vlist, dch_Vtx *vtx )
/* This routine removes the given vertex from the list */
{
dch_Vtx_list **set_addr;
dch_Vtx_list *current, *holder;
set_addr= vlist;
current= *vlist;
while (current) {
if (current->vtx == vtx) {
holder= current->next;
current->next= free_vtx_list_cells;
free_vtx_list_cells= current;
*set_addr= holder;
created_vtx_list_count--;
return;
}
set_addr= &(current->next);
current= current->next;
}
}
static dch_Vtx *pop_vtx( dch_Vtx_list **vlist )
/* This routine pops the first vertex off the list, destroying its
* cell but preserving the rest of the list.
*/
{
dch_Vtx *tmp_vtx;
dch_Vtx_list *tmp_list;
tmp_vtx= (*vlist)->vtx;
tmp_list= *vlist;
*vlist= (*vlist)->next;
tmp_list->next= free_vtx_list_cells;
free_vtx_list_cells= tmp_list;
created_vtx_list_count--;
return( tmp_vtx );
}
static void destroy_vtx_list( dch_Vtx_list *vlist )
/* This destroys the vtx list, leaving the vertices intact. */
{
dch_Vtx_list *temp,*temp2;
ger_debug("destroy_vtx_list");
temp= vlist;
while (temp) {
temp2= temp->next;
temp->next= free_vtx_list_cells;
free_vtx_list_cells= temp;
temp= temp2;
created_vtx_list_count--;
}
}
static void destroy_vtx_pairs( dch_Vtx_pair_list *vplist )
/* This destroys the vertex pair list, leaving the vertices intact. */
{
dch_Vtx_pair_list *temp,*temp2;
ger_debug("destroy_vtx_pairs");
temp= vplist;
while (temp) {
temp2= temp->next;
free( (P_Void_ptr)temp );
temp= temp2;
created_vtx_pair_count--;
}
}
static void destroy_vtx( dch_Vtx *vtx )
{
if (!vtx) return;
destroy_pt_list( vtx->forming_pts );
destroy_vtx_list( vtx->neighbors );
if (vtx->coords) free( (P_Void_ptr)vtx->coords );
vtx->neighbors= (dch_Vtx_list *)free_vtx_cells;
free_vtx_cells= vtx;
created_vtx_count--;
}
static int vtx_in_list( dch_Vtx_list *vlist, dch_Vtx *vtx )
/* Returns true if the given vertex is in the sorted list, false otherwise */
{
while (vlist) {
if (vlist->vtx == vtx) return(1);
vlist= vlist->next;
}
return(0);
}
static dch_Pt *create_pt( float *coords )
{
dch_Pt *pt;
int i;
if ( !(pt= (dch_Pt *)malloc(sizeof(dch_Pt))) )
ger_fatal("create_pt: unable to allocate %d bytes!",sizeof(dch_Vtx));
if ( !(pt->coords= (float *)malloc(current_dim * sizeof(float))) )
ger_fatal("create_pt: unable to allocate %d floats!",current_dim);
for (i=0; i<current_dim; i++) pt->coords[i]= coords[i];
pt->neighbors= (dch_Pt_list *)0;
pt->verts= (dch_Vtx_list *)0;
pt->user_hook= (P_Void_ptr)0;
pt->id= current_pt_id++;
created_pt_count++;
if (created_pt_count > created_pt_highwater)
created_pt_highwater= created_pt_count;
return (pt);
}
static void destroy_pt( dch_Pt *pt )
{
if (!pt) ger_fatal("Tried to free a null point; algorithm error!");
destroy_pt_list( pt->neighbors );
destroy_vtx_list( pt->verts );
free( (P_Void_ptr)pt );
created_pt_count--;
}
static void add_list_pt( dch_Pt_list **plist, dch_Pt *pt )
/* Add the given point to the given list */
{
dch_Pt_list *temp;
if (!free_pt_list_cells) { /* need to allocate a new cell */
if ( !(temp= (dch_Pt_list *)malloc(sizeof(dch_Pt_list))) )
ger_fatal("add_list_pt: unable to allocate %d bytes!",
sizeof(dch_Pt_list));
}
else {
temp= free_pt_list_cells;
free_pt_list_cells= temp->next;
}
temp->next= *plist;
temp->pt= pt;
*plist= temp;
created_pt_list_count++;
if (created_pt_list_count > created_pt_list_highwater)
created_pt_list_highwater= created_pt_list_count;
}
static void remove_list_pt( dch_Pt_list **plist, dch_Pt *pt )
/* This routine removes the given point from the list */
{
dch_Pt_list **set_addr;
dch_Pt_list *current, *holder;
set_addr= plist;
current= *plist;
while (current) {
if (current->pt == pt) {
holder= current->next;
current->next= free_pt_list_cells;
free_pt_list_cells= current;
*set_addr= holder;
created_pt_list_count--;
return;
}
set_addr= &(current->next);
current= current->next;
}
}
static void add_pair_pts( dch_Pt_pair_list **pplist, dch_Pt *pt1, dch_Pt *pt2 )
{
dch_Pt_pair_list *temp;
if ( !(temp= (dch_Pt_pair_list *)malloc(sizeof(dch_Pt_pair_list))) )
ger_fatal("add_pair_pts: unable to allocate %d bytes!",
sizeof(dch_Pt_pair_list));
temp->next= *pplist;
temp->pt1= pt1;
temp->pt2= pt2;
*pplist= temp;
created_pt_pair_count++;
if (created_pt_pair_count > created_pt_pair_highwater)
created_pt_pair_highwater= created_pt_pair_count;
}
static dch_Pt *pop_pt( dch_Pt_list **plist )
/* This routine pops the first point off the list, destroying its
* cell but preserving the rest of the list.
*/
{
dch_Pt *tmp_pt;
dch_Pt_list *tmp_list;
tmp_pt= (*plist)->pt;
tmp_list= *plist;
*plist= (*plist)->next;
tmp_list->next= free_pt_list_cells;
free_pt_list_cells= tmp_list;
created_pt_list_count--;
return( tmp_pt );
}
static void destroy_pt_list( dch_Pt_list *plist )
/* This destroys the point list, leaving the points intact. */
{
dch_Pt_list *temp,*temp2;
ger_debug("destroy_pt_list");
temp= plist;
while (temp) {
temp2= temp->next;
temp->next= free_pt_list_cells;
free_pt_list_cells= temp;
temp= temp2;
created_pt_list_count--;
}
}
static void destroy_pt_pairs( dch_Pt_pair_list *pplist )
/* This destroys the point pair list, leaving the points intact. */
{
dch_Pt_pair_list *temp,*temp2;
ger_debug("destroy_pt_pairs");
temp= pplist;
while (temp) {
temp2= temp->next;
free( (P_Void_ptr)temp );
temp= temp2;
created_pt_pair_count--;
}
}
static int pt_in_list( dch_Pt_list *plist, dch_Pt *pt )
/* Returns true if the given point is in the sorted list, false otherwise */
{
while (plist) {
if (plist->pt == pt) return(1);
plist= plist->next;
}
return(0);
}
static void dump_pt_list( dch_Pt_list *plist )
/* This routine is for debugging only */
{
fprintf(stderr,"points (");
while (plist) {
if (plist->pt) fprintf(stderr," %d",plist->pt->id);
else ger_fatal("Tried to dump a null point; algorithm error!");
plist= plist->next;
}
fprintf(stderr," )\n");
}
static void dump_vtx_list( dch_Vtx_list *vlist )
/* This routine is for debugging only */
{
fprintf(stderr,"vertices (");
while (vlist) {
fprintf(stderr," %d",vlist->vtx->id);
vlist= vlist->next;
}
fprintf(stderr," )\n");
}
static void dump_vtx( dch_Vtx *vtx )
/* This routine is for debugging only */
{
int i;
if (!vtx) fprintf(stderr,"Attempt to dump null vertex!\n");
else {
fprintf(stderr,"Vertex %d:\n",vtx->id);
if (vtx->coords) {
fprintf(stderr," coords ( ");
for (i=0; i<current_dim; i++) fprintf(stderr,"%f ",vtx->coords[i]);
fprintf(stderr,")\n");
}
else fprintf(stderr," coords infinite\n");
fprintf(stderr," distance %f; deleted= %d, degenerate= %d\n",
vtx->distance,vtx->deleted,vtx->degenerate);
fprintf(stderr," neighbors: "); dump_vtx_list(vtx->neighbors);
fprintf(stderr," forming points: "); dump_pt_list(vtx->forming_pts);
}
}
static void dump_pt( dch_Pt *pt )
/* This routine is for debugging only */
{
int i;
if (!pt) fprintf(stderr,"Attempt to dump null point!\n");
else {
fprintf(stderr,"Point %d:\n",pt->id);
fprintf(stderr," coords ( ");
for (i=0; i<current_dim; i++) fprintf(stderr,"%f ",pt->coords[i]);
fprintf(stderr,")\n");
fprintf(stderr," neighbors: "); dump_pt_list(pt->neighbors);
fprintf(stderr," vertices: "); dump_vtx_list(pt->verts);
}
}
static dch_Bndbx *create_bndbx()
{
dch_Bndbx *result;
ger_debug("create_bndbx:");
if ( !(result= (dch_Bndbx *)malloc(sizeof(dch_Bndbx))) )
ger_fatal("create_bndbx: unable to allocate %d bytes!",sizeof(dch_Bndbx));
if ( !(result->corner1= (float *)malloc(current_dim*sizeof(float))) )
ger_fatal("create_bndbx: unable to allocate %d floats!",current_dim);
if ( !(result->corner2= (float *)malloc(current_dim*sizeof(float))) )
ger_fatal("create_bndbx: unable to allocate %d floats!",current_dim);
result->empty_flag= 1;
return(result);
}
static void destroy_bndbx( dch_Bndbx *bndbx )
{
ger_debug("destroy_bndbx:");
free( (P_Void_ptr)bndbx->corner1 );
free( (P_Void_ptr)bndbx->corner2 );
free( (P_Void_ptr)bndbx );
}
static void dump_bndbx( dch_Bndbx *bndbx )
{
int i;
ger_debug("dump_bndbx:");
if (bndbx->empty_flag) fprintf(stderr,"Bounding box: empty\n");
else {
fprintf(stderr,"Bounding box: corner1 ( ");
for (i=0; i<current_dim; i++) fprintf(stderr,"%f ",bndbx->corner1[i]);
fprintf(stderr,")\n");
fprintf(stderr," corner2 ( ");
for (i=0; i<current_dim; i++) fprintf(stderr,"%f ",bndbx->corner2[i]);
fprintf(stderr,")\n");
}
}
static void add_pt_to_bndbx( dch_Bndbx *bndbx, dch_Pt *pt )
{
int i;
if (bndbx->empty_flag) {
for (i=0; i<current_dim; i++)
bndbx->corner1[i]= bndbx->corner2[i]= pt->coords[i];
bndbx->empty_flag= 0;
}
else for (i=0; i<current_dim; i++) {
if (bndbx->corner1[i] > pt->coords[i]) bndbx->corner1[i]= pt->coords[i];
if (bndbx->corner2[i] < pt->coords[i]) bndbx->corner2[i]= pt->coords[i];
}
}
static int pt_in_bndbx( dch_Bndbx *bndbx, dch_Pt *pt )
{
int i;
for (i=0; i<current_dim; i++) {
if (bndbx->corner1[i] > pt->coords[i]) return(0);
if (bndbx->corner2[i] < pt->coords[i]) return(0);
}
return(1);
}
static void dump_tesselation( dch_Tess *tess )
/* This routine is for debugging only */
{
dch_Vtx_list *vlist;
dch_Pt_list *plist;
fprintf(stderr,"***Dumping tesselation***\n");
fprintf(stderr," dimensionality %d, characteristic length %f\n",
tess->dimensionality,tess->characteristic_length);
dump_bndbx( tess->bndbx );
fprintf(stderr,"Point list contents follow:\n");
plist= tess->pt_list;
while (plist) {
dump_pt( plist->pt );
plist= plist->next;
}
fprintf(stderr,"Vertex list contents follow:\n");
vlist= tess->vtx_list;
while (vlist) {
dump_vtx( vlist->vtx );
vlist= vlist->next;
}
fprintf(stderr,"Bogus point list: ");
dump_pt_list(tess->bogus_pts);
fprintf(stderr,"Current center point is %d\n",tess->center_pt->id);
fprintf(stderr,"Current most recent point is %d\n",
tess->most_recent_pt->id);
fprintf(stderr,"***end of dump***\n");
}
static dch_Pt_list *calc_bogus_pts( dch_Bndbx *bndbx )
/* This routine calculates the corners of a simplex completely enclosing
* the given bounding box.
*/
{
float edge_length= 0.0;
float *coords;
int i,j;
dch_Pt_list *result= (dch_Pt_list *)0;
ger_debug("calc_bogus_pts:");
/* Malloc some space for needed coordinates */
if ( !(coords= (float *)malloc(current_dim*sizeof(float))) )
ger_fatal("calc_bogus_pts: unable to allocate %d floats!",current_dim);
/* Find the maximum edge length; assume an n-cube of that edge length. */
for (i=0; i<current_dim; i++)
if ( (bndbx->corner2[i] - bndbx->corner1[i]) > edge_length )
edge_length= bndbx->corner2[i] - bndbx->corner1[i];
/* First corner is the minimum bounding box corner minus SIMPLEX_MARGIN
* times the edge length.
*/
for (i=0; i<current_dim; i++)
coords[i]= bndbx->corner1[i] - SIMPLEX_MARGIN*edge_length;
add_list_pt( &result, create_pt(coords) );
/* Other corners are (1 + SIMPLEX_MARGIN)*n*edge length up the coordinate
* axes, where n is the dimensionality of the space.
*/
for (i=0; i<current_dim; i++) {
for (j=0; j<current_dim; j++)
coords[j]= bndbx->corner1[j] - SIMPLEX_MARGIN*edge_length;
coords[i]=
bndbx->corner1[i] + (1.0+SIMPLEX_MARGIN)*current_dim*edge_length;
add_list_pt( &result, create_pt(coords) );
}
/* Clean up */
free( (P_Void_ptr)coords );
return( result );
}
static void connect_neighboring_pts( dch_Pt_list *pts, dch_Vtx *vtx )
/* This routine takes a set of forming points and their vertex, makes
* all the points neighbors of eachother, and adds the vertex to each
* point's vertex list.
*/
{
dch_Pt_list *thispt,*thatpt;
thispt= pts;
while (thispt) {
add_list_vtx( &(thispt->pt->verts), vtx );
thatpt= thispt->next;
while (thatpt) {
if ( (thispt->pt != thatpt->pt) &&
!pt_in_list(thispt->pt->neighbors, thatpt->pt) ) {
add_list_pt( &(thispt->pt->neighbors),thatpt->pt );
add_list_pt( &(thatpt->pt->neighbors),thispt->pt );
}
thatpt= thatpt->next;
}
thispt= thispt->next;
}
}
static float calc_product_term( dch_Pt_list *thispt )
/* This routine is used in find_central_vtx */
{
float result= 0.0;
int i;
for (i=0; i<current_dim; i++)
result += (thispt->pt->coords[i]*thispt->pt->coords[i])
-(thispt->next->pt->coords[i]*thispt->next->pt->coords[i]);
return( result/2.0 );
}
static dch_Vtx *find_central_vtx( dch_Pt_list *pts )
/* This routine returns a vertex equidistant from all the given points. */
{
int i,j;
dch_Pt_list *thispt;
float *coords;
dch_Vtx *result;
ger_debug("find_central_vtx:");
/* We want a point equidistant from the given points. This can
* be found by solving the matrix equation (e.g. in 2D, with
* forming points A, B, and C and solution P):
*
* / Ax-Bx Ay-By \ /Px}\ / (A**2 - B**2)/2 \
* | || | = | |
* \ Bx-Cx By-Cy / \Py}/ \ (B**2 - C**2)/2 /
*
* where A**2 and B**2 are the squared norms of A and B. This
* is easy to prove by simply writing the requirement that A and
* B be equidistant from P, expanding the squares, and simplifying.
* The algorithm relies on the fact that there will be current_dim+1
* points in the input point list.
*/
thispt= pts;
for (i=0; i<current_dim; i++) {
for (j=0; j<current_dim; j++)
*(gauss_matrix + i*(current_dim+1) + j)=
thispt->pt->coords[j] - thispt->next->pt->coords[j];
*(gauss_matrix + i*(current_dim+1) + current_dim)=
calc_product_term(thispt);
thispt= thispt->next;
}
/* Find the vertex coordinates by Gaussian elimination. If no
* solution can be found, we create a vertex at infinty and mark
* it for deletion.
*/
coords= dch_gauss_elim( gauss_matrix, current_dim );
result= create_vtx( coords );
result->forming_pts= pts;
if (coords) result->distance= distance( coords, pts->pt->coords );
else result->degenerate= 1;
/* The forming points of the new vertex are now all neighbors, and
* need to know about their new vertex */
connect_neighboring_pts( pts, result );
free( (P_Void_ptr)coords );
return(result);
}
static void add_first_vtx_neighbors( dch_Vtx *vtx, dch_Tess *tess )
/* This routine creates neighbor vertices at infinity, and appropriately
* hooks them up the the newly formed first vertex.
*/
{
dch_Vtx *inf_vtx;
dch_Pt **point_array;
dch_Pt_list *plist;
int i,j;
/* Generate an array to hold the current_dim+1 bogus points, to
* simplify constructing the point lists for forming points.
*/
if ( !(point_array= (dch_Pt **)malloc( (current_dim+1)*sizeof(dch_Pt *) )) )
ger_fatal("add_first_vtx_neighbors: unable to allocate %d bytes!",
(current_dim+1)*sizeof(dch_Pt *));
plist= tess->bogus_pts;
for (i=0; i<(current_dim+1); i++ ) {
point_array[i]= plist->pt;
plist= plist->next;
}
/* We create current_dim+1 vertices at infinity, each with the
* known vertex as a neighbor and current_dim of the bogus
* points as forming points. This leaves each forming point
* list one vertex short.
*/
for (i=0; i<(current_dim+1); i++) {
inf_vtx= create_vtx( (float *)0 ); /* create vertex at infinity */
add_list_vtx( &(tess->infinite_vtxs), inf_vtx );
for (j=0; j<(current_dim+1); j++)
if (i != j) {
add_list_pt( &(inf_vtx->forming_pts), point_array[j] );
add_list_vtx( &(point_array[j]->verts), inf_vtx );
}
#ifdef MAINTAIN_VTX_NEIGHBOR_LISTS
add_list_vtx( &(inf_vtx->neighbors), vtx );
add_list_vtx( &(vtx->neighbors), inf_vtx );
#endif
}
/* Clean up */
free( (P_Void_ptr)point_array );
}
static dch_Tess *create_initial_tesselation( dch_Bndbx *bndbx )
{
dch_Tess *tess;
dch_Pt_list *plist, *forming_pt_list= (dch_Pt_list *)0;
dch_Vtx *new_vtx;
ger_debug("create_initial_tesselation:");
if ( !(tess= (dch_Tess *)malloc(sizeof(dch_Tess))) )
ger_fatal("create_initial_tesselation: unable to allocate %d bytes!",
sizeof(dch_Tess));
tess->dimensionality= current_dim;
tess->bndbx= bndbx;
tess->vtx_list= (dch_Vtx_list *)0;
tess->infinite_vtxs= (dch_Vtx_list *)0;
tess->pt_list= (dch_Pt_list *)0;
/* Calculate the bogus points, which define the outer bound of
* a simplex guaranteed to hold all the data points. Copy the
* resulting point list as the initial pt_list.
*/
plist= tess->bogus_pts= calc_bogus_pts( tess->bndbx );
tess->pt_list= (dch_Pt_list *)0;
while (plist) {
add_list_pt( &(tess->pt_list), plist->pt );
plist= plist->next;
}
/* Calculate a first vertex based on the bogus points.
* We need another copy of the list, as it will become the forming
* point list of the new vertex and we have to keep separate lists
* for memory management purposes.
*/
plist= tess->bogus_pts;
while (plist) {
add_list_pt( &(forming_pt_list), plist->pt );
plist= plist->next;
}
new_vtx= find_central_vtx( forming_pt_list );
/* The new vertex needs current_dim + 1 neighbors at infinity */
add_first_vtx_neighbors( new_vtx, tess );
/* The center point we set is not necessarily really closest
* to the center, but the next point insertion will replace
* it with an appropriate value.
*/
tess->most_recent_pt= tess->center_pt= tess->pt_list->pt;
/* The following data is used to pick an appropriate search method
* for finding points closest to newly added points.
*/
tess->best_search_method= UNKNOWN;
tess->searches_done= 0;
tess->center_steps= 0;
tess->most_recent_steps= 0;
/* The following data is used with PROXIMITY_TOLERANCE to determine
* when to drop points or vertices because they are too close together.
*/
tess->characteristic_length= new_vtx->distance;
return(tess);
}
void dch_destroy_tesselation( dch_Tess *tess )
{
dch_Pt_list *plist;
dch_Vtx_list *vlist;
ger_debug("destroy_tesselation:");
destroy_bndbx( tess->bndbx );
plist= tess->pt_list;
while (plist) {
destroy_pt( plist->pt );
plist= plist->next;
}
vlist= tess->vtx_list;
while (vlist) {
destroy_vtx( vlist->vtx );
vlist= vlist->next;
}
destroy_pt_list( tess->bogus_pts );