This repository has been archived by the owner on Jul 6, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 69
/
wbDataset.cpp
177 lines (165 loc) · 5.05 KB
/
wbDataset.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
#include "wb.h"
template <typename T>
static inline T _min(const T &x, const T &y) {
return x < y ? x : y;
}
template <typename T>
static inline T _max(const T &x, const T &y) {
return x > y ? x : y;
}
template <typename T>
inline T lerp(const double &x, const T &start, const T &end) {
return (1 - x) * start + x * end;
}
static inline void genRandom(void *trgt, wbType_t type, double minVal,
double maxVal) {
const int span = maxVal - minVal;
const int r = rand();
const double rf = ((double)r) / ((double)RAND_MAX);
switch (type) {
case wbType_ascii:
*((char *)trgt) = (r % span) + minVal; // random printable character;
break;
case wbType_bit8:
*((char *)trgt) = lerp<char>(rf, minVal, maxVal);
break;
case wbType_ubit8:
*((unsigned char *)trgt) = lerp<unsigned char>(rf, minVal, maxVal);
break;
case wbType_integer:
*((int *)trgt) = lerp<int>(rf, minVal, maxVal);
break;
case wbType_float: {
*((float *)trgt) = lerp<float>(rf, minVal, maxVal);
break;
}
case wbType_double: {
*((double *)trgt) = lerp<double>(rf, minVal, maxVal);
break;
}
case wbType_unknown:
wbAssert(false && "Invalid wbType_unknown");
break;
}
return;
}
static inline void *genRandomList(wbType_t type, size_t len, double minVal,
double maxVal) {
size_t ii;
void *data = wbNewArray(char, wbType_size(type) * len);
switch (type) {
case wbType_ascii:
case wbType_bit8: {
char *iter = (char *)data;
for (ii = 0; ii < len; ii++) {
genRandom(iter++, type, minVal, maxVal);
}
break;
}
case wbType_ubit8: {
unsigned char *iter = (unsigned char *)data;
for (ii = 0; ii < len; ii++) {
genRandom(iter++, type, minVal, maxVal);
}
break;
}
case wbType_integer: {
int *iter = (int *)data;
for (ii = 0; ii < len; ii++) {
genRandom(iter++, type, minVal, maxVal);
}
break;
}
case wbType_float: {
float *iter = (float *)data;
for (ii = 0; ii < len; ii++) {
genRandom(iter++, type, minVal, maxVal);
}
break;
}
case wbType_double: {
double *iter = (double *)data;
for (ii = 0; ii < len; ii++) {
genRandom(iter++, type, minVal, maxVal);
}
break;
}
case wbType_unknown:
wbAssert(false && "Invalid wbType_unknown");
break;
}
return data;
}
static void genRaw(const char *path, wbRaw_GenerateParams_t params) {
int rows = _max(1, params.rows);
int cols = _max(1, params.cols);
double minVal = params.minVal;
double maxVal = params.maxVal;
wbType_t type = params.type;
void *data = genRandomList(type, rows * cols, minVal, maxVal);
wbExport(path, wbExportKind_raw, data, rows, cols, type);
wbDelete(data);
}
static void genCSV(const char *path, wbCSV_GenerateParams_t params) {
int rows = _max(1, params.rows);
int cols = _max(1, params.cols);
double minVal = params.minVal;
double maxVal = params.maxVal;
wbType_t type = params.type;
void *data = genRandomList(type, rows * cols, minVal, maxVal);
wbExport(path, wbExportKind_csv, data, rows, cols, type);
wbDelete(data);
}
static void genTSV(const char *path, wbTSV_GenerateParams_t params) {
int rows = _max(1, params.rows);
int cols = _max(1, params.cols);
double minVal = params.minVal;
double maxVal = params.maxVal;
wbType_t type = params.type;
void *data = genRandomList(type, rows * cols, minVal, maxVal);
wbExport(path, wbExportKind_tsv, data, rows, cols, type);
wbDelete(data);
}
static void genText(const char *path, wbText_GenerateParams_t params) {
int length = _max(1, params.length);
wbType_t type = wbType_ascii;
void *data = genRandomList(type, length, 32, 128);
wbExport(path, wbExportKind_text, data, length, 1, type);
wbDelete(data);
}
static void genPPM(const char *path, wbPPM_GenerateParams_t params) {
int width = _max(1, params.width);
int height = _max(1, params.height);
int channels = _max(1, params.channels);
double minVal = params.minVal;
double maxVal = params.maxVal;
wbType_t type = wbType_float;
float *data = (float *)genRandomList(type, width * height * channels,
minVal, maxVal);
wbImage_t img = wbImage_new(width, height, channels, data);
wbExport(path, img);
wbImage_delete(img);
}
EXTERN_C void wbDataset_generate(const char *path, wbExportKind_t kind,
wbGenerateParams_t params) {
wbDirectory_create(wbDirectory_name(path));
switch (kind) {
case wbExportKind_raw:
genRaw(path, params.raw);
break;
case wbExportKind_csv:
genCSV(path, params.csv);
break;
case wbExportKind_tsv:
genTSV(path, params.tsv);
break;
case wbExportKind_ppm:
genPPM(path, params.ppm);
break;
case wbExportKind_text:
genText(path, params.text);
break;
default:
wbAssert(false && "Invalid Export kind");
}
}