-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcvt.cpp
More file actions
3295 lines (3050 loc) · 79.1 KB
/
Copy pathcvt.cpp
File metadata and controls
3295 lines (3050 loc) · 79.1 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
# include <cstdlib>
# include <cmath>
# include <ctime>
# include <iostream>
# include <iomanip>
# include <fstream>
# include <cstring>
# include <random>
using namespace std;
# include "cvt.h"
//****************************************************************************80
char ch_cap ( char c )
//****************************************************************************80
//
// Purpose:
//
// CH_CAP capitalizes a single character.
//
// Discussion:
//
// This routine should be equivalent to the library "toupper" function.
//
// Licensing:
//
// This code is distributed under the GNU LGPL license.
//
// Modified:
//
// 19 July 1998
//
// Author:
//
// John Burkardt
//
// Parameters:
//
// Input, char C, the character to capitalize.
//
// Output, char CH_CAP, the capitalized character.
//
{
if ( 97 <= c && c <= 122 )
{
c = c - 32;
}
return c;
}
//****************************************************************************80
bool ch_eqi ( char c1, char c2 )
//****************************************************************************80
//
// Purpose:
//
// CH_EQI is true if two characters are equal, disregarding case.
//
// Licensing:
//
// This code is distributed under the GNU LGPL license.
//
// Modified:
//
// 13 June 2003
//
// Author:
//
// John Burkardt
//
// Parameters:
//
// Input, char C1, char C2, the characters to compare.
//
// Output, bool CH_EQI, is true if the two characters are equal,
// disregarding case.
//
{
if ( 97 <= c1 && c1 <= 122 )
{
c1 = c1 - 32;
}
if ( 97 <= c2 && c2 <= 122 )
{
c2 = c2 - 32;
}
return ( c1 == c2 );
}
//****************************************************************************80
int ch_to_digit ( char c )
//****************************************************************************80
//
// Purpose:
//
// CH_TO_DIGIT returns the integer value of a base 10 digit.
//
// Example:
//
// C DIGIT
// --- -----
// '0' 0
// '1' 1
// ... ...
// '9' 9
// ' ' 0
// 'X' -1
//
// Licensing:
//
// This code is distributed under the GNU LGPL license.
//
// Modified:
//
// 13 June 2003
//
// Author:
//
// John Burkardt
//
// Parameters:
//
// Input, char C, the decimal digit, '0' through '9' or blank are legal.
//
// Output, int CH_TO_DIGIT, the corresponding integer value. If C was
// 'illegal', then DIGIT is -1.
//
{
int digit;
if ( '0' <= c && c <= '9' )
{
digit = c - '0';
}
else if ( c == ' ' )
{
digit = 0;
}
else
{
digit = -1;
}
return digit;
}
//****************************************************************************80
void cvt ( int dim_num, int n, int batch, int init, int sample, int sample_num,
int it_max, int it_fixed, int *seed, double r[],int *it_num, double *it_diff,
double *energy )
//****************************************************************************80
//
// Purpose:
//
// CVT computes a Centroidal Voronoi Tessellation.
//
// Discussion:
//
// This routine initializes the data, and carries out the
// CVT iteration.
//
// Licensing:
//
// This code is distributed under the GNU LGPL license.
//
// Modified:
//
// 23 June 2005
//
// Author:
//
// John Burkardt
//
// Reference:
//
// Qiang Du, Vance Faber, and Max Gunzburger,
// Centroidal Voronoi Tessellations: Applications and Algorithms,
// SIAM Review, Volume 41, 1999, pages 637-676.
//
// Parameters:
//
// Input, int DIM_NUM, the spatial dimension.
//
// Input, int N, the number of Voronoi cells.
//
// Input, int BATCH, sets the maximum number of sample points
// generated at one time. It is inefficient to generate the sample
// points 1 at a time, but memory intensive to generate them all
// at once. You might set BATCH to min ( SAMPLE_NUM, 10000 ), for instance.
// BATCH must be at least 1.
//
// Input, int INIT, specifies how the points are to be initialized.
// -1, 'RANDOM', using C++ RANDOM function;
// 0, 'UNIFORM', using a simple uniform RNG;
// 1, 'HALTON', from a Halton sequence;
// 2, 'GRID', points from a grid;
// 3, 'USER', call "user" routine;
// 4, points are already initialized on input.
//
// Input, int SAMPLE, specifies how the sampling is done.
// -1, 'RANDOM', using C++ RANDOM function;
// 0, 'UNIFORM', using a simple uniform RNG;
// 1, 'HALTON', from a Halton sequence;
// 2, 'GRID', points from a grid;
// 3, 'USER', call "user" routine.
//
// Input, int SAMPLE_NUM, the number of sample points.
//
// Input, int IT_MAX, the maximum number of iterations.
//
// Input, int IT_FIXED, the maximum number of iterations to take
// with a fixed set of sample points.
//
// Input/output, int *SEED, the random number seed.
//
// Input/output, double R[DIM_NUM*N], the approximate CVT points.
// If INIT = 4 on input, then it is assumed that these values have been
// initialized. On output, the CVT iteration has been applied to improve
// the value of the points.
//
// Output, int *IT_NUM, the number of iterations taken. Generally,
// this will be equal to IT_MAX, unless the iteration tolerance was
// satisfied early.
//
// Output, double *IT_DIFF, the L2 norm of the difference
// between the iterates.
//
// Output, double *ENERGY, the discrete "energy", divided
// by the number of sample points.
//
{
bool DEBUG = false;
bool initialize;
int seed_base;
int seed_init;
if ( batch < 1 )
{
cout << "\n";
cout << "CVT - Fatal error!\n";
cout << " The input value BATCH < 1.\n";
exit ( 1 );
}
if ( *seed <= 0 )
{
cout << "\n";
cout << "CVT - Fatal error!\n";
cout << " The input value SEED <= 0.\n";
exit ( 1 );
}
if ( DEBUG )
{
cout << "\n";
cout << " Step SEED L2-Change Energy\n";
cout << "\n";
}
*it_num = 0;
*it_diff = 0.0;
*energy = 0.0;
seed_init = *seed;
//
// Initialize the data, unless the user has already done that.
//
if ( init != 4 )
{
initialize = true;
cvt_sample ( dim_num, n, n, init, initialize, seed, r );
}
if ( DEBUG )
{
cout << " "
<< setw(4) << *it_num << " "
<< setw(12) << seed_init << "\n";
}
//
// If the initialization and sampling steps use the same random number
// scheme, then the sampling scheme does not have to be initialized.
//
if ( init == sample )
{
initialize = false;
}
else
{
initialize = true;
}
//
// Carry out the iteration.
//
while ( *it_num < it_max )
{
//
// If it's time to update the seed, save its current value
// as the starting value for all iterations in this cycle.
// If it's not time to update the seed, restore it to its initial
// value for this cycle.
//
if ( ( (*it_num) % it_fixed ) == 0 )
{
seed_base = *seed;
}
else
{
*seed = seed_base;
}
*it_num = *it_num + 1;
seed_init = *seed;
cvt_iterate ( dim_num, n, batch, sample, initialize, sample_num, seed,
r, it_diff, energy );
initialize = false;
if ( DEBUG )
{
cout << " "
<< setw(4) << *it_num << " "
<< setw(12) << seed_init << " "
<< setw(14) << *it_diff << " "
<< setw(14) << *energy << "\n";
}
}
return;
}
//****************************************************************************80
double cvt_energy ( int dim_num, int n, int batch, int sample, bool initialize,
int sample_num, int *seed, double r[] )
//****************************************************************************80
//
// Purpose:
//
// CVT_ENERGY computes the CVT energy of a dataset.
//
// Discussion:
//
// For a given number of generators, a CVT is a minimizer (or at least
// a local minimizer) of the CVT energy. During a CVT iteration,
// it should generally be the case that the CVT energy decreases from
// step to step, and that perturbations or adjustments of an
// approximate CVT will almost always have higher CVT energy.
//
// Licensing:
//
// This code is distributed under the GNU LGPL license.
//
// Modified:
//
// 03 December 2004
//
// Author:
//
// John Burkardt
//
// Parameters:
//
// Input, int DIM_NUM, the spatial dimension.
//
// Input, int N, the number of generators.
//
// Input, int BATCH, the maximum number of sample points to generate
// at one time.
//
// Input, int SAMPLE, specifies how the sampling is done.
// -1, 'RANDOM', using C++ RANDOM function;
// 0, 'UNIFORM', using a simple uniform RNG;
// 1, 'HALTON', from a Halton sequence;
// 2, 'GRID', points from a grid;
// 3, 'USER', call "user" routine.
//
// Input, bool INITIALIZE, is TRUE if the pseudorandom process
// should be reinitialized.
//
// Input, int SAMPLE_NUM, the number of sample points to use.
//
// Input/output, int *SEED, a seed for the random number generator.
//
// Input, double R[DIM_NUM*N], the coordinates of the points.
//
// Output, double CVT_ENERGY, the estimated CVT energy.
//
{
double energy;
int get;
int have;
int i;
int j;
int *nearest;
double *s;
nearest = new int[batch];
s = new double [dim_num*batch];
have = 0;
energy = 0.0;
while ( have < sample_num )
{
get = i4_min ( sample_num - have, batch );
cvt_sample ( dim_num, sample_num, get, sample, initialize, seed, s );
have = have + get;
find_closest ( dim_num, n, get, s, r, nearest );
for ( j = 0; j < get; j++ )
{
for ( i = 0; i < dim_num; i++ )
{
energy = energy + ( s[i+j*dim_num] - r[i+nearest[j]*dim_num] )
* ( s[i+j*dim_num] - r[i+nearest[j]*dim_num] );
}
}
}
energy = energy / ( double ) ( sample_num );
delete [] nearest;
delete [] s;
return energy;
}
//****************************************************************************80
void cvt_iterate ( int dim_num, int n, int batch, int sample, bool initialize,
int sample_num, int *seed, double r[], double *it_diff, double *energy )
//****************************************************************************80
//
// Purpose:
//
// CVT_ITERATE takes one step of the CVT iteration.
//
// Discussion:
//
// The routine is given a set of points, called "generators", which
// define a tessellation of the region into Voronoi cells. Each point
// defines a cell. Each cell, in turn, has a centroid, but it is
// unlikely that the centroid and the generator coincide.
//
// Each time this CVT iteration is carried out, an attempt is made
// to modify the generators in such a way that they are closer and
// closer to being the centroids of the Voronoi cells they generate.
//
// A large number of sample points are generated, and the nearest generator
// is determined. A count is kept of how many points were nearest to each
// generator. Once the sampling is completed, the location of all the
// generators is adjusted. This step should decrease the discrepancy
// between the generators and the centroids.
//
// The centroidal Voronoi tessellation minimizes the "energy",
// defined to be the integral, over the region, of the square of
// the distance between each point in the region and its nearest generator.
// The sampling technique supplies a discrete estimate of this
// energy.
//
// Licensing:
//
// This code is distributed under the GNU LGPL license.
//
// Modified:
//
// 20 September 2004
//
// Author:
//
// John Burkardt
//
// Reference:
//
// Qiang Du, Vance Faber, and Max Gunzburger,
// Centroidal Voronoi Tessellations: Applications and Algorithms,
// SIAM Review, Volume 41, 1999, pages 637-676.
//
// Parameters:
//
// Input, int DIM_NUM, the spatial dimension.
//
// Input, int N, the number of Voronoi cells.
//
// Input, int BATCH, sets the maximum number of sample points
// generated at one time. It is inefficient to generate the sample
// points 1 at a time, but memory intensive to generate them all
// at once. You might set BATCH to min ( SAMPLE_NUM, 10000 ), for instance.
// BATCH must be at least 1.
//
// Input, int SAMPLE, specifies how the sampling is done.
// -1, 'RANDOM', using C++ RANDOM function;
// 0, 'UNIFORM', using a simple uniform RNG;
// 1, 'HALTON', from a Halton sequence;
// 2, 'GRID', points from a grid;
// 3, 'USER', call "user" routine.
//
// Input, bool INITIALIZE, is TRUE if the SEED must be reset to SEED_INIT
// before computation. Also, the pseudorandom process may need to be
// reinitialized.
//
// Input, int SAMPLE_NUM, the number of sample points.
//
// Input/output, int *SEED, the random number seed.
//
// Input/output, double R[DIM_NUM*N], the Voronoi
// cell generators. On output, these have been modified
//
// Output, double *IT_DIFF, the L2 norm of the difference
// between the iterates.
//
// Output, double *ENERGY, the discrete "energy", divided
// by the number of sample points.
//
{
int *count;
int get;
int have;
int i;
int j;
int j2;
int *nearest;
double *r2;
double *s;
double term;
//
// Take each generator as the first sample point for its region.
// This can slightly slow the convergence, but it simplifies the
// algorithm by guaranteeing that no region is completely missed
// by the sampling.
//
*energy = 0.0;
r2 = new double[dim_num*n];
count = new int[n];
nearest = new int[sample_num];
s = new double[dim_num*sample_num];
for ( j = 0; j < n; j++ )
{
for ( i = 0; i < dim_num; i++ )
{
r2[i+j*dim_num] = r[i+j*dim_num];
}
}
for ( j = 0; j < n; j++ )
{
count[j] = 1;
}
//
// Generate the sampling points S.
//
have = 0;
while ( have < sample_num )
{
get = i4_min ( sample_num - have, batch );
cvt_sample ( dim_num, sample_num, get, sample, initialize, seed, s );
initialize = false;
have = have + get;
//
// Find the index N of the nearest cell generator to each sample point S.
//
find_closest ( dim_num, n, get, s, r, nearest );
//
// Add S to the centroid associated with generator N.
//
for ( j = 0; j < get; j++ )
{
j2 = nearest[j];
for ( i = 0; i < dim_num; i++ )
{
r2[i+j2*dim_num] = r2[i+j2*dim_num] + s[i+j*dim_num];
}
for ( i = 0; i < dim_num; i++ )
{
*energy = *energy + pow ( r[i+j2*dim_num] - s[i+j*dim_num], 2 );
}
count[j2] = count[j2] + 1;
}
}
//
// Estimate the centroids.
//
for ( j = 0; j < n; j++ )
{
for ( i = 0; i < dim_num; i++ )
{
r2[i+j*dim_num] = r2[i+j*dim_num] / ( double ) ( count[j] );
}
}
//
// Determine the sum of the distances between generators and centroids.
//
*it_diff = 0.0;
for ( j = 0; j < n; j++ )
{
term = 0.0;
for ( i = 0; i < dim_num; i++ )
{
term = term + ( r2[i+j*dim_num] - r[i+j*dim_num] )
* ( r2[i+j*dim_num] - r[i+j*dim_num] );
}
*it_diff = *it_diff + sqrt ( term );
}
//
// Replace the generators by the centroids.
//
for ( j = 0; j < n; j++ )
{
for ( i = 0; i < dim_num; i++ )
{
r[i+j*dim_num] = r2[i+j*dim_num];
}
}
//
// Normalize the discrete energy estimate.
//
*energy = *energy / sample_num;
delete [] count;
delete [] nearest;
delete [] r2;
delete [] s;
return;
}
//****************************************************************************80
void cvt_sample ( int dim_num, int n, int n_now, int sample, bool initialize,
int *seed, double r[] )
//****************************************************************************80
//
// Purpose:
//
// CVT_SAMPLE returns sample points.
//
// Discussion:
//
// N sample points are to be taken from the unit box of dimension DIM_NUM.
//
// These sample points are usually created by a pseudorandom process
// for which the points are essentially indexed by a quantity called
// SEED. To get N sample points, we generate values with indices
// SEED through SEED+N-1.
//
// It may not be practical to generate all the sample points in a
// single call. For that reason, the routine allows the user to
// request a total of N points, but to require that only N_NOW be
// generated now (on this call).
//
// Licensing:
//
// This code is distributed under the GNU LGPL license.
//
// Modified:
//
// 23 June 2005
//
// Author:
//
// John Burkardt
//
// Parameters:
//
// Input, int DIM_NUM, the spatial dimension.
//
// Input, int N, the number of Voronoi cells.
//
// Input, int N_NOW, the number of sample points to be generated
// on this call. N_NOW must be at least 1.
//
// Input, int SAMPLE, specifies how the sampling is done.
// -1, 'RANDOM', using C++ RANDOM function;
// 0, 'UNIFORM', using a simple uniform RNG;
// 1, 'HALTON', from a Halton sequence;
// 2, 'GRID', points from a grid;
// 3, 'USER', call "user" routine.
//
// Input, bool INITIALIZE, is TRUE if the pseudorandom process should be
// reinitialized.
//
// Input/output, int *SEED, the random number seed.
//
// Output, double R[DIM_NUM*N_NOW], the sample points.
//
{
double exponent;
static int *halton_base = NULL;
static int *halton_leap = NULL;
static int *halton_seed = NULL;
int halton_step;
int i;
int j;
static int ngrid;
static int rank;
int rank_max;
static int *tuple = NULL;
if ( n_now < 1 )
{
cout << "\n";
cout << "CVT_SAMPLE - Fatal error!\n";
cout << " N_NOW < 1.\n";
exit ( 1 );
}
if ( sample == -1 )
{
if ( initialize )
{
random_initialize ( *seed );
}
for ( j = 0; j < n_now; j++ )
{
for ( i = 0; i < dim_num; i++ )
{
r[i+j*dim_num] = ( double ) rand ( ) / ( double ) RAND_MAX;
}
}
*seed = ( *seed ) + n_now * dim_num;
}
else if ( sample == 0 )
{
r8mat_uniform_01 ( dim_num, n_now, seed, r );
}
else if ( sample == 1 )
{
halton_seed = new int[dim_num];
halton_leap = new int[dim_num];
halton_base = new int[dim_num];
halton_step = *seed;
for ( i = 0; i < dim_num; i++ )
{
halton_seed[i] = 0;
}
for ( i = 0; i < dim_num; i++ )
{
halton_leap[i] = 1;
}
for ( i = 0; i < dim_num; i++ )
{
halton_base[i] = prime ( i + 1 );
}
i4_to_halton_sequence ( dim_num, n_now, halton_step, halton_seed,
halton_leap, halton_base, r );
delete [] halton_seed;
delete [] halton_leap;
delete [] halton_base;
*seed = *seed + n_now;
}
else if ( sample == 2 )
{
exponent = 1.0 / ( double ) ( dim_num );
ngrid = ( int ) pow ( ( double ) n, exponent );
rank_max = ( int ) pow ( ( double ) ngrid, ( double ) dim_num );
tuple = new int[dim_num];
if ( rank_max < n )
{
ngrid = ngrid + 1;
rank_max = ( int ) pow ( ( double ) ngrid, ( double ) dim_num );
}
if ( initialize )
{
rank = -1;
tuple_next_fast ( ngrid, dim_num, rank, tuple );
}
rank = ( *seed ) % rank_max;
for ( j = 0; j < n_now; j++ )
{
tuple_next_fast ( ngrid, dim_num, rank, tuple );
rank = rank + 1;
rank = rank % rank_max;
for ( i = 0; i < dim_num; i++ )
{
r[i+j*dim_num] = double ( 2 * tuple[i] - 1 ) / double ( 2 * ngrid );
}
}
delete [] tuple;
*seed = *seed + n_now;
}
else if ( sample == 3 )
{
user ( dim_num, n_now, seed, r );
}
else
{
cout << "\n";
cout << "CVT_SAMPLE - Fatal error!\n";
cout << " The value of SAMPLE = " << sample << " is illegal.\n";
exit ( 1 );
}
return;
}
//****************************************************************************80
void data_read ( char *file_in_name, int dim_num, int n, double r[] )
//****************************************************************************80
//
// Purpose:
//
// DATA_READ reads generator coordinate data from a file.
//
// Discussion:
//
// The file is assumed to contain one record per line.
//
// Records beginning with the '#' character are comments, and are ignored.
// Blank lines are also ignored.
//
// Each line that is not ignored is assumed to contain exactly (or at least)
// M real numbers, representing the coordinates of a point.
//
// There are assumed to be exactly (or at least) N such records.
//
// Licensing:
//
// This code is distributed under the GNU LGPL license.
//
// Modified:
//
// 03 August 2004
//
// Author:
//
// John Burkardt
//
// Parameters:
//
// Input, char *FILE_IN_NAME, the name of the input file.
//
// Input, int DIM_NUM, the number of spatial dimensions.
//
// Input, int N, the number of points. The program
// will stop reading data once N values have been read.
//
// Output, double R[DIM_NUM*N], the point coordinates.
//
{
bool error;
ifstream file_in;
int i;
int j;
char line[255];
double *x;
file_in.open ( file_in_name );
if ( !file_in )
{
cout << "\n";
cout << "DATA_READ - Fatal error!\n";
cout << " Could not open the input file: \"" << file_in_name << "\"\n";
exit ( 1 );
}
x = new double[dim_num];
j = 0;
while ( j < n )
{
file_in.getline ( line, sizeof ( line ) );
if ( file_in.eof ( ) )
{
break;
}
if ( line[0] == '#' || s_len_trim ( line ) == 0 )
{
continue;
}
error = s_to_r8vec ( line, dim_num, x );
if ( error )
{
continue;
}
for ( i = 0; i < dim_num; i++ )
{
r[i+j*dim_num] = x[i];
}
j = j + 1;
}
file_in.close ( );
delete [] x;
cout << "\n";
cout << "DATA_READ:\n";
cout << " Read coordinate data from file.\n";
return;
}
//****************************************************************************80
char digit_to_ch ( int i )
//****************************************************************************80
//
// Purpose:
//
// DIGIT_TO_CH returns the base 10 digit character corresponding to a digit.
//
// Example:
//
// I C
// ----- ---
// 0 '0'
// 1 '1'
// ... ...
// 9 '9'
// 10 '*'
// -83 '*'
//
// Licensing:
//
// This code is distributed under the GNU LGPL license.
//
// Modified:
//
// 16 June 2003
//
// Author:
//
// John Burkardt
//
// Parameters:
//
// Input, int I, the digit, which should be between 0 and 9.
//
// Output, char DIGIT_TO_CH, the appropriate character '0' through '9' or '*'.
//
{
char c;
if ( 0 <= i && i <= 9 )
{
c = '0' + i;
}
else
{
c = '*';
}
return c;
}
//****************************************************************************80
void find_closest ( int dim_num, int n, int sample_num, double s[], double r[],
int nearest[] )
//****************************************************************************80
//
// Purpose:
//
// FIND_CLOSEST finds the nearest R point to each S point.
//
// Discussion:
//
// This routine finds the closest Voronoi cell generator by checking every
// one. For problems with many cells, this process can take the bulk
// of the CPU time. Other approaches, which group the cell generators into
// bins, can run faster by a large factor.
//
// Licensing:
//