aboutsummaryrefslogtreecommitdiff
path: root/common.h
blob: 950c9de17c8ba365038fef76321939fe5b5cac6f (plain)
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
#ifndef _COMMON_H
#define _COMMON_H

#include <string.h>
#include <stdio.h>
#include <assert.h>
#include <math.h>

enum val_type { VEC3D, BOX };

struct val {
	enum val_type type;
};

struct vec3d {
	enum val_type type;
	double x;
	double y;
	double z;
};

struct box {
	enum val_type type;
	double min_x;
	double min_y;
	double min_z;
	double max_x;
	double max_y;
	double max_z;
};

static inline double *val_get_index(struct val *val, int index)
{
	switch (val->type)
	{
		case VEC3D:
			struct vec3d *d_v3d = (struct vec3d *)val;
			switch (index)
			{
				case 0: return &d_v3d->x;
				case 1: return &d_v3d->y;
				case 2: return &d_v3d->z;
				default: assert(0);
			}
		case BOX:
			struct box *v_box = (struct box *)val;
			switch (index)
			{
				case 0: return &v_box->min_x;
				case 1: return &v_box->min_y;
				case 2: return &v_box->min_z;
				case 3: return &v_box->max_x;
				case 4: return &v_box->max_y;
				case 5: return &v_box->max_z;
				default: assert(0);
			}
		default:
			assert(0);
	}
}

static int val_parse(char *in, struct val *o)
{
	int i = 0;
	char *p = strtok(in, ",");
	int required_vars = 0;
	switch (o->type)
	{
		case VEC3D:
			required_vars = 3;
			break;
		case BOX:
			required_vars = 6;
			break;
	}
	while (p != NULL)
	{
		sscanf(p, "%lf", val_get_index(o, i++));
		p = strtok(NULL, ",");
	}
	if (i != required_vars)
	{
		fprintf(stderr, "Invalid value: %s.\n", in);
		return 64;
	}
	return 0;
}

#endif /* _COMMON_H */