summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorsanine <sanine.not@pm.me>2023-01-08 22:35:38 -0600
committersanine <sanine.not@pm.me>2023-01-08 22:35:38 -0600
commit38a71a0e8eb25418f6a0be23960a30de566dfb7c (patch)
tree554a7ed5bdb6900af7b1bcb42d1f3548eea9bb96
parenta18d004be2217bd59c34ba5a3600f72de70c9419 (diff)
add kai_parse_float_array
-rw-r--r--include/kalmia.h62
-rw-r--r--src/CMakeLists.txt1
-rw-r--r--src/geometry/CMakeLists.txt10
-rw-r--r--src/geometry/geometry.c29
-rw-r--r--src/geometry/geometry.h9
-rw-r--r--src/geometry/geometry.test.c42
-rw-r--r--src/test/test.h1
7 files changed, 154 insertions, 0 deletions
diff --git a/include/kalmia.h b/include/kalmia.h
index 20e5f4f..524072b 100644
--- a/include/kalmia.h
+++ b/include/kalmia.h
@@ -63,6 +63,68 @@ typedef float ka_real_t;
#endif
+/**************** geometry types ****************/
+
+struct ka_float_array_t {
+ char *id;
+ size_t count;
+ ka_real_t *data;
+};
+
+
+struct ka_param_t {
+ char *name;
+ char *type;
+};
+
+
+struct ka_accessor_t {
+ size_t count;
+ size_t offset;
+ char *source;
+ size_t stride;
+
+ struct ka_param_t *param;
+ size_t param_count;
+};
+
+
+struct ka_technique_common_t {
+ struct ka_accessor_t accessor;
+};
+
+
+struct ka_source_t {
+ char *id;
+ struct ka_float_array_t float_array;
+ struct ka_technique_common_t technique_common;
+};
+
+
+struct ka_vertices_t {
+};
+
+
+struct ka_triangles_t {
+};
+
+
+struct ka_mesh_t {
+ struct ka_source_t *source;
+ size_t source_count;
+
+ struct ka_vertices_t vertices;
+
+ struct ka_triangles_t *triangles;
+ size_t triangles_count;
+};
+
+
+struct ka_geometry_t {
+ struct ka_mesh_t mesh;
+};
+
+
typedef ka_real_t ka_matrix_t[16];
diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt
index bb8aa82..d0589ed 100644
--- a/src/CMakeLists.txt
+++ b/src/CMakeLists.txt
@@ -11,3 +11,4 @@ if (KALMIA_BUILD_TESTS)
endif()
add_subdirectory(util)
+add_subdirectory(geometry)
diff --git a/src/geometry/CMakeLists.txt b/src/geometry/CMakeLists.txt
new file mode 100644
index 0000000..006fc27
--- /dev/null
+++ b/src/geometry/CMakeLists.txt
@@ -0,0 +1,10 @@
+project(kalmia)
+
+target_sources(kalmia PUBLIC geometry.c)
+
+if (KALMIA_BUILD_TESTS)
+ target_sources(
+ kalmia-tests PUBLIC
+ geometry.test.c
+ )
+endif()
diff --git a/src/geometry/geometry.c b/src/geometry/geometry.c
new file mode 100644
index 0000000..1d26a70
--- /dev/null
+++ b/src/geometry/geometry.c
@@ -0,0 +1,29 @@
+#include <string.h>
+
+#include <kalmia.h>
+#include <ezxml.h>
+#include "util/util.h"
+#include "geometry.h"
+
+
+void kai_parse_float_array(struct ka_float_array_t *dest, ezxml_t src)
+{
+ /* get id, if present */
+ const char *id = ezxml_attr(src, "id");
+ if (id != NULL) {
+ size_t id_len = strlen(id) + 1;
+ dest->id = malloc(sizeof(char) * id_len);
+ strncpy(dest->id, id, id_len);
+ }
+ else {
+ dest->id = NULL;
+ }
+
+ /* get count */
+ const char *count = ezxml_attr(src, "count");
+ dest->count = strtol(count, NULL, 10);
+
+ /* parse floats */
+ dest->data = malloc(sizeof(ka_real_t) * dest->count);
+ kai_text_to_reals(dest->data, ezxml_txt(src), dest->count);
+}
diff --git a/src/geometry/geometry.h b/src/geometry/geometry.h
new file mode 100644
index 0000000..7da5c3d
--- /dev/null
+++ b/src/geometry/geometry.h
@@ -0,0 +1,9 @@
+#ifndef KALMIA_GEOMETRY_H
+#define KALMIA_GEOMETRY_H
+
+#include <kalmia.h>
+#include <ezxml.h>
+
+void kai_parse_float_array(struct ka_float_array_t *dest, ezxml_t src);
+
+#endif
diff --git a/src/geometry/geometry.test.c b/src/geometry/geometry.test.c
new file mode 100644
index 0000000..bb06aeb
--- /dev/null
+++ b/src/geometry/geometry.test.c
@@ -0,0 +1,42 @@
+#include <string.h>
+
+#include <kalmia.h>
+#include <ezxml.h>
+#include "geometry.h"
+#include "test/test.h"
+
+
+LILY_FILE_BEGIN(geometry_suite)
+
+
+LILY_TEST("parse float array")
+{
+ char str[1024];
+ strncpy(
+ str,
+ "<float_array id=\"sequence\" count=\"5\">\n"
+ " -0.5 -0.25 0 0.25 0.5\n"
+ "</float_array>",
+ 1024
+ );
+
+ ezxml_t tag = ezxml_parse_str(str, strlen(str)+1);
+
+ struct ka_float_array_t float_array;
+ kai_parse_float_array(&float_array, tag);
+
+ CHECK_EQS(float_array.id, "sequence");
+ REQUIRE_EQ(float_array.count, 5, "%ul");
+ REQUIRE_NEQ(float_array.data, NULL, "%p");
+
+ CHECK_EQ(float_array.data[0], -0.50, "%f");
+ CHECK_EQ(float_array.data[1], -0.25, "%f");
+ CHECK_EQ(float_array.data[2], 0.00, "%f");
+ CHECK_EQ(float_array.data[3], 0.25, "%f");
+ CHECK_EQ(float_array.data[4], 0.50, "%f");
+}
+#include LILY_PUSH_TEST()
+
+
+#define LILY_FILE_END
+#include LILY_REGISTER_TESTS()
diff --git a/src/test/test.h b/src/test/test.h
index f8d07c9..910fe65 100644
--- a/src/test/test.h
+++ b/src/test/test.h
@@ -7,6 +7,7 @@
#define TESTS \
X(util_suite) \
+ X(geometry_suite) \
#define X(test) extern void (*test)();