summaryrefslogtreecommitdiff
path: root/src/geometry/geometry.c
blob: 78fbaea8e435121fc906e3015d51a0f852560e66 (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
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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
#include <string.h>

#include <kalmia.h>
#include "util/util.h"
#include "xml/xml.h"
#include "geometry.h"


/* macro to ensure tag type matches what we expect */
#define CHECK_TAG_TYPE(expected) \
if (strcmp(src->type, expected) != 0) { \
	return -1; \
}

/* helper macro for iterating over tag children of a particular type */
#define FOR_CHILD_OF_TYPE(parent, child, type) \
for ( \
	child = kai_tag_get_first_child_with_type(parent, type); \
	child != NULL; \
	child = kai_tag_get_next_sibling_with_type(child, type) \
)


int kai_read_float_array(struct ka_float_array_t *dest, struct kai_tag_t *src)
{
	CHECK_TAG_TYPE("float_array");

	long count = kai_tag_attr_to_long(src, "count", -1);
	if (count == -1) {
		/* count not present */
		return -1;
	}

	dest->count = count;
	dest->id = kai_tag_attr_to_dup(src, "id");
	dest->digits = kai_tag_attr_to_long(src, "digits", 6);
	dest->magnitude = kai_tag_attr_to_long(src, "magnitude", 38);

	/* allocate & fill buffer */
	dest->buf = kai_alloc(
		dest->count * sizeof(ka_real_t),
		"float_array buffer"
	);
	if (dest->buf == NULL) {
		free(dest->id);
		return -1;
	}
	kai_text_to_reals(dest->buf, src->content, dest->count);

	return 0;
}

void kai_release_float_array(struct ka_float_array_t fa)
{
	free(fa.id);
	free(fa.buf);
}


int kai_read_param(struct ka_param_t *dest, struct kai_tag_t *src)
{
	CHECK_TAG_TYPE("param");

	char *type = kai_tag_attr_to_dup(src, "type");
	if (type == NULL) {
		return -1;
	}

	dest->name = kai_tag_attr_to_dup(src, "name");
	dest->sid = kai_tag_attr_to_dup(src, "sid");
	dest->type = type;
	dest->semantic = kai_tag_attr_to_dup(src, "semantic");

	return 0;
}

void kai_release_param(struct ka_param_t p)
{
	free(p.name);
	free(p.sid);
	free(p.type);
	free(p.semantic);
}


int kai_read_accessor(struct ka_accessor_t *dest, struct kai_tag_t *src)
{
	CHECK_TAG_TYPE("accessor");

	long count = kai_tag_attr_to_long(src, "count", -1);
	if (count == -1) { return -1; }
	char *source = kai_tag_attr_to_dup(src, "source");
	if (source == NULL) { return -1; }

	dest->count = count;
	dest->offset = kai_tag_attr_to_long(src, "offset", 0);
	dest->source = source;
	dest->stride = kai_tag_attr_to_long(src, "stride", 1);

	dest->param_count = kai_tag_num_children(src);
	dest->param = kai_alloc(
		dest->param_count * sizeof(struct ka_param_t),
		"accessor tag param array"
	);
	if (dest->param == NULL) {
		free(source);
		return -1;
	}
	struct kai_tag_t *t = src->children;
	int i;
	for (i=0; i<dest->param_count; i++) {
		int result = kai_read_param(dest->param + i, t);
		if (result != 0) {
			free(dest->param);
			free(dest->source);
			return -1;
		}
		t = t->next;
	}

	return 0;
}

void kai_release_accessor(struct ka_accessor_t a)
{
	free(a.source);
	int i;
	for (i=0; i<a.param_count; i++) {
		kai_release_param(a.param[i]);
	}
	free(a.param);
}


int kai_read_source(struct ka_source_t *dest, struct kai_tag_t *src)
{
	CHECK_TAG_TYPE("source");

	char *id = kai_tag_attr_to_dup(src, "id");
	if (id == NULL) { return -1; }

	dest->id = id;
	dest->name = kai_tag_attr_to_dup(src, "name");

	struct kai_tag_t *child;
	for (child = src->children; child != NULL; child = child->next) {
		if (strcmp(child->type, "float_array") == 0) {
			kai_read_float_array(&(dest->float_array), child);
		}
		else if (strcmp(child->type, "technique_common") == 0) {
			kai_read_accessor(&(dest->accessor), child->children);
		}
		/* ignore others */
	}

	return 0;
}

void kai_release_source(struct ka_source_t s)
{
	free(s.id);
	free(s.name);

	kai_release_float_array(s.float_array);
	kai_release_accessor(s.accessor);
}


int kai_read_input_unshared(struct ka_input_t *dest, struct kai_tag_t *src)
{
	CHECK_TAG_TYPE("input");

	char *semantic = kai_tag_attr_to_dup(src, "semantic");
	if (semantic == NULL) { return -1; }
	char *source = kai_tag_attr_to_dup(src, "source");
	if (source == NULL) { free(semantic); return -1; }

	dest->shared = false;
	dest->offset = 0;
	dest->semantic = semantic;
	dest->source = source;
	dest->set = 0;

	return 0;
}

int kai_read_input_shared(struct ka_input_t *dest, struct kai_tag_t *src)
{
	int result = kai_read_input_unshared(dest, src);
	if (result != 0) { return result; }

	long offset = kai_tag_attr_to_long(src, "offset", -1);
	if (offset == -1) { return -1; }

	dest->shared = true;
	dest->offset = offset;

	long set = kai_tag_attr_to_long(src, "set", -1);
	if (set != -1) {
		dest->use_set = true;
		dest->set = set;
	}
	else {
		dest->use_set = false;
	}

	return 0;
}

void kai_release_input(struct ka_input_t i)
{
	free(i.semantic);
	free(i.source);
}


int kai_read_vertices(struct ka_vertices_t *dest, struct kai_tag_t *src)
{
	CHECK_TAG_TYPE("vertices");

	char *id = kai_tag_attr_to_dup(src, "id");
	if (id == NULL) { return -1; }

	dest->id = id;
	dest->name = kai_tag_attr_to_dup(src, "name");

	/* count input children */
	int count = 0;
	struct kai_tag_t *child;
	FOR_CHILD_OF_TYPE(src, child, "input") {
		count += 1;
	}

	dest->input_count = count;
	dest->input = malloc(count * sizeof(struct ka_input_t));
	if (dest->input == NULL) {
		kai_release_vertices(*dest);
		return -1;
	}

	int i=0;
	FOR_CHILD_OF_TYPE(src, child, "input") {
		int result = kai_read_input_unshared(dest->input + i, child);
		if (result != 0) {
			dest->input_count = i;
			kai_release_vertices(*dest);
			return -1;
		}
		i += 1;
	}

	return 0;
}

void kai_release_vertices(struct ka_vertices_t v)
{
	free(v.id);
	free(v.name);
	int i;
	for (i=0; i<v.input_count; i++) {
		kai_release_input(v.input[i]);
	}
	free(v.input);
}


int kai_read_triangles(struct ka_triangles_t *dest, struct kai_tag_t *src)
{
	CHECK_TAG_TYPE("triangles");

	long count = kai_tag_attr_to_long(src, "count", -1);
	if (count == -1) { return -1; }

	dest->name = kai_tag_attr_to_dup(src, "name");
	dest->count = count;
	dest->material = kai_tag_attr_to_dup(src, "material");

	
	/* zero these out so that any error-related calls to kai_release_triangles
	 * won't cause an invalid free
	 */
	dest->input_count = 0;
	dest->input = NULL;
	dest->p_count = 0;
	dest->p = NULL;

	/* load the child inputs into the structure */
	int input_count = 0;
	struct kai_tag_t *child;
	FOR_CHILD_OF_TYPE(src, child, "input") {
		input_count += 1;
	}

	dest->input = kai_alloc(input_count * sizeof(struct ka_input_t), "triangles input array");
	if (dest->input == NULL) {
		kai_release_triangles(*dest);
		return -1;
	}

	int i=0;
	FOR_CHILD_OF_TYPE(src, child, "input") {
		int result = kai_read_input_shared(dest->input + i, child);
		if (result != 0) {
			dest->input_count = i;
			kai_release_triangles(*dest);
			return -1;
		}
		i += 1;
	}
	dest->input_count = input_count;


	/* load the child p tag */
	struct kai_tag_t *p = kai_tag_get_first_child_with_type(src, "p");
	if (p != NULL) {
		dest->p_count = 3 * dest->count * dest->input_count;
		dest->p = kai_alloc(dest->p_count * sizeof(unsigned int), "triangles primitive array");
		if (dest->p == NULL) {
			dest->p_count = 0;
			kai_release_triangles(*dest);
			return -1;
		}
		kai_text_to_uints(dest->p, p->content, dest->p_count);
	}

	return 0;
}

void kai_release_triangles(struct ka_triangles_t t)
{
	free(t.name);
	free(t.material);

	int i;
	for (i=0; i<t.input_count; i++) {
		kai_release_input(t.input[i]);
	}
	free(t.input);
	free(t.p);
}