-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrandom_generator.c
62 lines (58 loc) · 1.7 KB
/
random_generator.c
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
#include "random_generator.h"
#include "voxelgrid.h"
#include <stdlib.h>
#include <string.h>
RandomGenerator *random_generator_create(const char *args)
{
// TODO: this is ugly, write some helper functions
char *input_string;
char *pch;
RandomGenerator *random_generator = malloc(sizeof(RandomGenerator));
// make a copy of the args ( that we are allowed to edit)
input_string = malloc((1+strlen(args))*sizeof(char));
strcpy(input_string, args);
// - parse arguments -
// split based on commas
pch = strtok(input_string, ",");
if (pch == NULL)
return NULL;
// read size
random_generator->size = atoi(pch);
// default seed
random_generator->seed = 0;
// read next argument
pch = strtok(NULL,",");
// if there is one
if (pch != NULL){
// update seed
random_generator->seed = atoi(pch);
}
// flush the rest of the arguments
while(pch != NULL){
pch = strtok(NULL,",");
}
free(input_string);
return random_generator;
}
VoxelGrid *random_generator_generate(RandomGenerator *random_generator)
{
// iterators
unsigned int x,y,z;
VoxelGrid *voxelgrid = voxelgrid_create(NULL,random_generator->size,random_generator->size,random_generator->size);
// set seed
srandom(random_generator->seed);
// for each voxel
for(x=0;x<voxelgrid->dim_x;x++){
for(y=0;y<voxelgrid->dim_y;y++){
for(z=0;z<voxelgrid->dim_z;z++){
// randomly set it to true or false
VOXEL(voxelgrid,x,y,z) = random()%2;
}
}
}
return voxelgrid;
}
void random_generator_free(RandomGenerator *random_generator)
{
free(random_generator);
}