-
Notifications
You must be signed in to change notification settings - Fork 7
/
PDSample.cpp
191 lines (167 loc) · 4.32 KB
/
PDSample.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
#ifdef _WIN32
#define WIN32_LEAN_AND_MEAN
#include <windows.h>
#endif
#include <math.h>
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include "PDSampling.h"
///
#ifdef _WIN32
double timeInSeconds()
{
static double perfFreq;
static int hasPerfTimer = -1;
if (hasPerfTimer==-1) {
LARGE_INTEGER perfFreqCountsPerSec;
if (QueryPerformanceFrequency(&perfFreqCountsPerSec)) {
hasPerfTimer = 1;
perfFreq = (double) perfFreqCountsPerSec.QuadPart;
} else {
hasPerfTimer = 0;
}
}
LARGE_INTEGER count;
if (hasPerfTimer && QueryPerformanceCounter(&count)) {
return (double) count.QuadPart/perfFreq;
} else {
return GetTickCount()/1000.0;
}
}
#else
#include <time.h>
#include <sys/time.h>
double timeInSeconds()
{
struct timeval tv;
struct timezone tz;
gettimeofday(&tv, &tz);
return tv.tv_sec + tv.tv_usec/1000000.0;
}
#endif
///
void usage(char *app)
{
printf( "Usage: %s [-m] [-t] [-r <relax count=0>] [-M <multiplier=10>] [-N <minMaxThrows=1000>] <method> <radius> <output>\n", app);
printf( " -m maximize point set after sampling\n");
printf( " -t use tiled domain\n");
printf( " -r apply the specified number of relaxations after sampling (requires qvoronoi)\n");
printf( " -M set multiplier for DartThrowing and BestCandidate samplers\n");
printf( " -N set minimum number of maximum throws for DartThrowing sampler\n");
printf( " available methods = {\n"
" DartThrowing, (uses multiplier and minMaxThrows)\n"
" BestCandidate, (uses multiplier)\n"
" Boundary, \n"
" Pure, \n"
" LinearPure, \n"
" Penrose, \n"
" Uniform" "}\n");
exit(1);
}
int main(int argc, char **argv)
{
int multiplier = 1, minMaxThrows = 1000;
bool maximize = false;
bool isTiled = false;
int relax = 0;
char *method;
float radius;
char *outputPath;
PDSampler *sampler;
FILE *output;
double startTime;
float elapTime;
int i, N;
for (i=1; i<argc; i++) {
char *arg = argv[i];
if (strcmp(arg,"-m")==0) {
maximize = true;
} else if (strcmp(arg,"-t")==0) {
isTiled = true;
} else if (strcmp(arg,"-r")==0) {
if (i+1<argc) {
i++;
relax = atoi(argv[i]);
} else {
usage(argv[0]);
}
} else if (strcmp(arg,"-N")==0) {
if (i+1<argc) {
i++;
minMaxThrows = atoi(argv[i]);
} else {
usage(argv[0]);
}
} else if (strcmp(arg,"-M")==0) {
if (i+1<argc) {
i++;
multiplier = atoi(argv[i]);
} else {
usage(argv[0]);
}
} else {
break;
}
}
if (i+3!=argc) usage(argv[0]);
method = argv[i++];
radius = (float) strtod(argv[i++],NULL);
outputPath = argv[i++];
if (radius<0.0005 || radius>.2) {
printf("Radius (%f) is outside allowable range.\n", radius);
exit(1);
}
if (!strcmp(method, "DartThrowing")) {
sampler = new DartThrowing(radius, isTiled, minMaxThrows, multiplier);
} else if (!strcmp(method, "BestCandidate")) {
sampler = new BestCandidate(radius, isTiled, multiplier);
} else if (!strcmp(method, "Boundary")) {
sampler = new BoundarySampler(radius, isTiled);
} else if (!strcmp(method, "Pure")) {
if (!isTiled) {
printf("Pure sampler does not support untiled domain.\n");
exit(1);
}
sampler = new PureSampler(radius);
} else if (!strcmp(method, "LinearPure")) {
if (!isTiled) {
printf("LinearPure sampler does not support untiled domain.\n");
exit(1);
}
sampler = new LinearPureSampler(radius);
} else if (!strcmp(method, "Penrose")) {
if (isTiled) {
printf("Penrose sampler does not support tiled domain.\n");
exit(1);
}
sampler = new PenroseSampler(radius);
} else if (!strcmp(method, "Uniform")) {
sampler = new UniformSampler(radius);
} else {
printf("Unrecognized sampling method (%s).\n", method);
exit(1);
}
startTime = timeInSeconds();
sampler->complete();
if (maximize) sampler->maximize();
for (i=0; i<relax; i++) sampler->relax();
elapTime = (float) (timeInSeconds() - startTime);
N = (int) sampler->points.size();
output = fopen(outputPath,"wb");
if (!output) {
printf("Unable to open output file (%s).\n", outputPath);
exit(1);
}
fwrite(&N, 4, 1, output);
fwrite(&elapTime, 4, 1, output);
fwrite(&radius, 4, 1, output);
fwrite(&isTiled, 4, 1, output);
for (i=0; i<N; i++) {
fwrite(&sampler->points[i], 8, 1, output);
}
fclose(output);
printf("Wrote: %s (%d points, %fs, %6.1f pts/s)\n", outputPath, N, elapTime, N/elapTime);
delete sampler;
return 0;
}