-
Notifications
You must be signed in to change notification settings - Fork 154
/
Copy pathgdiam_test.cpp
151 lines (117 loc) · 3.84 KB
/
gdiam_test.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
/*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*
* gdiam_test.cpp -
* Test code for computing diameter and tight-fitting
* bounding box.
*
* Copyright 2000 Sariel Har-Peled ([email protected])
*
* * the GNU General Public License as published by the Free
* Software Foundation; either version 2, or (at your option)
* any later version.
*
* or
*
* * the GNU Lesser General Public License as published by the Free
* Software Foundation; either version 2.1, or (at your option)
* any later version.
*
* Code is based on the paper:
* A Practical Approach for Computing the Diameter of a Point-Set.
* Sariel Har-Peled (http://www.uiuc.edu/~sariel)
*---------------------------------------------------------------
* History:
* 3/28/01 - Extended test program, so that it can load a
* text-file with points. Format is
* [npoints]
* [x_1 y_1 z_1]
8 ...
* [x_n y_n z_n]
\*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*/
#include <stdlib.h>
#include <stdio.h>
#include <assert.h>
#include <memory.h>
#include <math.h>
#include "gdiam.hpp"
/*--- Start of Code ---*/
void test_itself( gdiam_real * points, int num )
{
GPointPair pair;
printf( "Computing the diameter for %d points selected "
"uniformly from the unit cube\n", num );
pair = gdiam_approx_diam_pair( (gdiam_real *)points, num, 0.0 );
printf( "Diameter distance: %g\n", pair.distance );
printf( "Points realizing the diameter\n"
"\t(%g, %g, %g) - (%g, %g, %g)\n",
pair.p[ 0 ], pair.p[ 1 ], pair.p[ 2 ],
pair.q[ 0 ], pair.q[ 1 ], pair.q[ 2 ] );
gdiam_point * pnt_arr;
gdiam_bbox bb;
pnt_arr = gdiam_convert( (gdiam_real *)points, num );
printf( "Computing a tight-fitting bounding box of the point-set\n" );
bb = gdiam_approx_mvbb_grid_sample( pnt_arr, num, 5, 400 );
printf( "Resulting bounding box:\n" );
bb.dump();
printf( "Axis parallel bounding box\n" );
GBBox bbx;
bbx.init();
for ( int ind = 0; ind < num; ind++ )
bbx.bound( points + (ind * 3) );
bbx.dump();
}
void standard_test()
{
gdiam_real * points;
int num;
num = 1000000;
points = (gdiam_point)malloc( sizeof( gdiam_point_t ) * num );
assert( points != NULL );
// Pick randomly points from the unit cube */
for ( int ind = 0; ind < num; ind++ ) {
points[ ind * 3 + 0 ] = drand48();
points[ ind * 3 + 1 ] = drand48();
points[ ind * 3 + 2 ] = drand48();
}
test_itself( points, num );
}
void read_points( FILE * fl, gdiam_real * points, int points_num )
{
int args;
double x, y, z;
for ( int ind = 0; ind < points_num; ind++ ) {
args = fscanf( fl, "%lg %lg %lg\n", &x, &y, &z );
assert( args == 3 );
points[ ind * 3 + 0 ] = x;
points[ ind * 3 + 1 ] = y;
points[ ind * 3 + 2 ] = z;
}
}
void test_file( const char * file_name )
{
gdiam_real * points;
FILE * fl;
int args, points_num;
fl = fopen( file_name, "rt" );
if ( fl == NULL ) {
printf( "Unable to open file: [%s]\n", file_name );
exit( -1 );
}
args = fscanf( fl, "%d\n", &points_num );
assert( ( args > 0 ) && ( points_num > 0 ) );
points = (gdiam_point)malloc( sizeof( gdiam_point_t ) * points_num );
assert( points != NULL );
read_points( fl, points, points_num );
fclose( fl );
test_itself( points, points_num );
}
int main_test( int argc, char ** argv )
{
if ( argc == 1 ) {
standard_test();
return 0;
}
for ( int ind = 1; ind < argc; ind++ )
test_file( argv[ ind ] );
return 0;
}
/* gdiam_test.C - End of File ------------------------------------------*/