diff --git a/src/gmio_amf/amf_document.h b/src/gmio_amf/amf_document.h
index 44bb0c8..fcfc024 100644
--- a/src/gmio_amf/amf_document.h
+++ b/src/gmio_amf/amf_document.h
@@ -202,9 +202,14 @@ enum gmio_amf_mesh_element {
/*! Index of an element(vertex, edge or volume) within a mesh */
struct gmio_amf_object_mesh_element_index {
- uint32_t object_index; /*!< Index of the object that owns the mesh */
- uint32_t mesh_index; /*!< Index of the mesh that owns element */
- uint32_t value; /*!< Index of the inner element */
+ /*! Index of the object that owns the mesh */
+ uint32_t object_index;
+ /*! Index of the mesh that owns element */
+ uint32_t mesh_index;
+ /*! Index of the inner element */
+ uint32_t value;
+ /*! Type of the inner element */
+ enum gmio_amf_mesh_element element_type;
};
/*! Provides an interface for accessing the underlying(hidden) user AMF data
@@ -257,7 +262,7 @@ struct gmio_amf_document {
* \c material element
*
* Function not required(can be set to \c NULL) if there is no material
- * composite in the document to write */
+ * composite in the document */
void (*func_get_material_composite)(
const void* cookie,
uint32_t material_index,
@@ -268,7 +273,7 @@ struct gmio_amf_document {
* \c constellation element
*
* Function not required(can be set to \c NULL) if there is no
- * constellation in the document to write */
+ * constellation in the document */
void (*func_get_constellation_instance)(
const void* cookie,
uint32_t constellation_index,
@@ -284,7 +289,6 @@ struct gmio_amf_document {
/*! Function that retrieves the i-th sub-element of a \c mesh element
*
- * \p element is the type of the sub-element of interest.\n
* \p element_index is the index of the sub-element within the \c mesh
* element.\n
* The domain of this index(ie. \c value field) depends on \p element :
@@ -296,12 +300,10 @@ struct gmio_amf_document {
*/
void (*func_get_object_mesh_element)(
const void* cookie,
- enum gmio_amf_mesh_element element,
const struct gmio_amf_object_mesh_element_index* element_index,
void* ptr_element);
- /*! Function that retrieves the i-th \c triangle within a mesh \c volume
- * element */
+ /*! Function that retrieves the i-th \c triangle within a mesh \c volume */
void (*func_get_object_mesh_volume_triangle)(
const void* cookie,
const struct gmio_amf_object_mesh_element_index* volume_index,
@@ -324,7 +326,7 @@ struct gmio_amf_document {
* CONSTELLATION | [0 .. constellation_count[ | gmio_amf_constellation
*
* Function not required(can be set to \c NULL) if there is no metadata in
- * the document to write */
+ * the document */
void (*func_get_document_element_metadata)(
const void* cookie,
enum gmio_amf_document_element element,
@@ -332,25 +334,14 @@ struct gmio_amf_document {
uint32_t metadata_index,
struct gmio_amf_metadata* ptr_metadata);
- /*! Optional function that retrieves the i-th metadata assigned to a mesh
- * vertex
+ /*! Optional function that retrieves the i-th metadata attached to a mesh
+ * element(only vertex or volume)
*
- * Function not required(can be set to \c NULL) if there is no mesh vertex
- * metadata in the document to write */
- void (*func_get_object_mesh_vertex_metadata)(
+ * Function not required(can be set to \c NULL) if there is no metadata
+ * for all mesh elements */
+ void (*func_get_object_mesh_element_metadata)(
const void* cookie,
- const struct gmio_amf_object_mesh_element_index* vertex_index,
- uint32_t metadata_index,
- struct gmio_amf_metadata* ptr_metadata);
-
- /*! Optional function that retrieves the i-th metadata assigned to a mesh
- * volume
- *
- * Function not required(can be set to \c NULL) if there is no mesh volume
- * metadata in the document to write */
- void (*func_get_object_mesh_volume_metadata)(
- const void* cookie,
- const struct gmio_amf_object_mesh_element_index* volume_index,
+ const struct gmio_amf_object_mesh_element_index* mesh_element_index,
uint32_t metadata_index,
struct gmio_amf_metadata* ptr_metadata);
};
diff --git a/src/gmio_amf/amf_error.h b/src/gmio_amf/amf_error.h
index 0bb4d5c..a54d060 100644
--- a/src/gmio_amf/amf_error.h
+++ b/src/gmio_amf/amf_error.h
@@ -73,13 +73,9 @@ enum gmio_amf_error
* is \c NULL while some gmio_amf_material::composite_count > 0 */
GMIO_AMF_ERROR_NULL_FUNC_GET_MATERIAL_COMPOSITE,
- /*! Function pointer gmio_amf_document::func_get_object_mesh_vertex_metadata
- * is \c NULL while some gmio_amf_vertex::metadata_count > 0 */
- GMIO_AMF_ERROR_NULL_FUNC_GET_OBJECT_MESH_VERTEX_METADATA,
-
- /*! Function pointer gmio_amf_document::func_get_object_mesh_volume_metadata
- * is \c NULL while some gmio_amf_volume::metadata_count > 0 */
- GMIO_AMF_ERROR_NULL_FUNC_GET_OBJECT_MESH_VOLUME_METADATA
+ /*! Function pointer gmio_amf_document::func_get_object_mesh_element_metadata
+ * is \c NULL while some metadata is attached to a mesh element */
+ GMIO_AMF_ERROR_NULL_FUNC_GET_OBJECT_MESH_ELEMENT_METADATA
};
/*! @} */
diff --git a/src/gmio_amf/amf_io.c b/src/gmio_amf/amf_io.c
index 4305f42..faa0be9 100644
--- a/src/gmio_amf/amf_io.c
+++ b/src/gmio_amf/amf_io.c
@@ -274,6 +274,26 @@ static void gmio_amf_write_texmap(
gmio_ostringstream_write_chararray(sstream, "\n");
}
+static bool gmio_amf_write_mesh_object_element_metadata(
+ struct gmio_amf_wcontext* context,
+ const struct gmio_amf_object_mesh_element_index* mesh_element_index,
+ uint32_t metadata_count)
+{
+ const struct gmio_amf_document* doc = context->document;
+ if (doc->func_get_object_mesh_element_metadata == NULL) {
+ return gmio_amf_wcontext_set_error(
+ context,
+ GMIO_AMF_ERROR_NULL_FUNC_GET_OBJECT_MESH_ELEMENT_METADATA);
+ }
+ struct gmio_amf_metadata metadata = {0};
+ for (uint32_t imeta = 0; imeta < metadata_count; ++imeta) {
+ doc->func_get_object_mesh_element_metadata(
+ doc->cookie, mesh_element_index, imeta, &metadata);
+ gmio_amf_write_metadata(&context->sstream, &metadata);
+ }
+ return gmio_no_error(context->error);
+}
+
/* Writes gmio_amf_mesh to stream */
static bool gmio_amf_write_mesh(
struct gmio_amf_wcontext* context,
@@ -287,13 +307,13 @@ static bool gmio_amf_write_mesh(
const struct gmio_ostringstream_format_float* f64_format =
&context->f64_format;
/* Write mesh element */
+ mesh_elt_index.element_type = GMIO_AMF_MESH_ELEMENT_VERTEX;
struct gmio_amf_vertex vertex = {0};
gmio_ostringstream_write_chararray(sstream, "\n\n");
for (uint32_t ivert = 0; ivert < mesh->vertex_count; ++ivert) {
mesh_elt_index.value = ivert;
doc->func_get_object_mesh_element(
- doc->cookie,
- GMIO_AMF_MESH_ELEMENT_VERTEX, &mesh_elt_index, &vertex);
+ doc->cookie, &mesh_elt_index, &vertex);
/* Write element */
gmio_ostringstream_write_chararray(sstream, "");
gmio_ostringstream_write_chararray(sstream, "");
@@ -318,17 +338,10 @@ static bool gmio_amf_write_mesh(
}
/* Write elements */
if (vertex.metadata_count > 0) {
- if (doc->func_get_object_mesh_vertex_metadata == NULL) {
- return gmio_amf_wcontext_set_error(
- context,
- GMIO_AMF_ERROR_NULL_FUNC_GET_OBJECT_MESH_VERTEX_METADATA);
- }
- struct gmio_amf_metadata metadata = {0};
- for (uint32_t imeta = 0; imeta < vertex.metadata_count; ++imeta) {
- doc->func_get_object_mesh_vertex_metadata(
- doc->cookie, &mesh_elt_index, imeta, &metadata);
- gmio_amf_write_metadata(sstream, &metadata);
- }
+ gmio_amf_write_mesh_object_element_metadata(
+ context, &mesh_elt_index, vertex.metadata_count);
+ if (gmio_error(context->error))
+ return false;
}
gmio_ostringstream_write_chararray(sstream, "\n");
gmio_amf_wcontext_incr_task_progress(context);
@@ -337,12 +350,12 @@ static bool gmio_amf_write_mesh(
}
/* Write mesh vertices elements */
if (mesh->edge_count > 0) {
+ mesh_elt_index.element_type = GMIO_AMF_MESH_ELEMENT_EDGE;
struct gmio_amf_edge edge = {0};
for (uint32_t iedge = 0; iedge < mesh->edge_count; ++iedge) {
mesh_elt_index.value = iedge;
doc->func_get_object_mesh_element(
- doc->cookie,
- GMIO_AMF_MESH_ELEMENT_EDGE, &mesh_elt_index, &edge);
+ doc->cookie, &mesh_elt_index, &edge);
gmio_ostringstream_write_chararray(sstream, "");
gmio_ostringstream_write_u32(sstream, edge.v1);
gmio_ostringstream_write_chararray(sstream, "");
@@ -368,12 +381,12 @@ static bool gmio_amf_write_mesh(
gmio_ostringstream_write_chararray(sstream, "\n");
/* Write mesh elements */
if (mesh->volume_count > 0) {
+ mesh_elt_index.element_type = GMIO_AMF_MESH_ELEMENT_VOLUME;
struct gmio_amf_volume volume = {0};
for (uint32_t ivol = 0; ivol < mesh->volume_count; ++ivol) {
mesh_elt_index.value = ivol;
doc->func_get_object_mesh_element(
- doc->cookie,
- GMIO_AMF_MESH_ELEMENT_VOLUME, &mesh_elt_index, &volume);
+ doc->cookie, &mesh_elt_index, &volume);
/* Write element begin */
gmio_ostringstream_write_chararray(sstream, "\n");
/* Write volume elements */
if (volume.metadata_count > 0) {
- if (doc->func_get_object_mesh_volume_metadata == NULL) {
- return gmio_amf_wcontext_set_error(
- context,
- GMIO_AMF_ERROR_NULL_FUNC_GET_OBJECT_MESH_VOLUME_METADATA);
- }
- struct gmio_amf_metadata metadata = {0};
- for (uint32_t imeta = 0; imeta < volume.metadata_count; ++imeta) {
- doc->func_get_object_mesh_volume_metadata(
- doc->cookie, &mesh_elt_index, imeta, &metadata);
- gmio_amf_write_metadata(sstream, &metadata);
- }
+ gmio_amf_write_mesh_object_element_metadata(
+ context, &mesh_elt_index, volume.metadata_count);
+ if (gmio_error(context->error))
+ return false;
}
/* Write volume element */
if (volume.has_color)
@@ -477,8 +483,8 @@ static bool gmio_amf_write_root_objects(struct gmio_amf_wcontext* context)
if (object.mesh_count > 0) {
struct gmio_amf_mesh mesh = {0};
for (uint32_t imesh = 0; imesh < object.mesh_count; ++imesh) {
- struct gmio_amf_object_mesh_element_index base_mesh_elt_index;
doc->func_get_object_mesh(doc->cookie, iobj, imesh, &mesh);
+ struct gmio_amf_object_mesh_element_index base_mesh_elt_index;
base_mesh_elt_index.object_index = iobj;
base_mesh_elt_index.mesh_index = imesh;
base_mesh_elt_index.value = 0;
@@ -766,6 +772,7 @@ static intmax_t gmio_amf_task_progress_max(const struct gmio_amf_document* doc)
progress_max += mesh.vertex_count;
progress_max += mesh.edge_count;
struct gmio_amf_object_mesh_element_index mesh_elt_index;
+ mesh_elt_index.element_type = GMIO_AMF_MESH_ELEMENT_VOLUME;
mesh_elt_index.object_index = iobj;
mesh_elt_index.mesh_index = imesh;
mesh_elt_index.value = 0;
@@ -773,10 +780,7 @@ static intmax_t gmio_amf_task_progress_max(const struct gmio_amf_document* doc)
struct gmio_amf_volume volume = {0};
mesh_elt_index.value = ivol;
doc->func_get_object_mesh_element(
- doc->cookie,
- GMIO_AMF_MESH_ELEMENT_VOLUME,
- &mesh_elt_index,
- &volume);
+ doc->cookie, &mesh_elt_index, &volume);
progress_max += volume.triangle_count;
}
}
diff --git a/src/gmio_amf/amf_io.h b/src/gmio_amf/amf_io.h
index 1419765..57b05ef 100644
--- a/src/gmio_amf/amf_io.h
+++ b/src/gmio_amf/amf_io.h
@@ -63,9 +63,9 @@ GMIO_C_LINKAGE_BEGIN
* \sa gmio_amf_write_file()
*/
GMIO_API int gmio_amf_write(
- struct gmio_stream* stream,
- const struct gmio_amf_document* doc,
- const struct gmio_amf_write_options* opts);
+ struct gmio_stream* stream,
+ const struct gmio_amf_document* doc,
+ const struct gmio_amf_write_options* opts);
/*! Writes AMF document to stream
*
@@ -86,9 +86,9 @@ GMIO_API int gmio_amf_write(
* \sa gmio_amf_write(), gmio_stream_stdio(FILE*)
*/
GMIO_API int gmio_amf_write_file(
- const char* filepath,
- const struct gmio_amf_document* doc,
- const struct gmio_amf_write_options* opts);
+ const char* filepath,
+ const struct gmio_amf_document* doc,
+ const struct gmio_amf_write_options* opts);
GMIO_C_LINKAGE_END
diff --git a/tests/test_amf_io.c b/tests/test_amf_io.c
index becd089..86c6d61 100644
--- a/tests/test_amf_io.c
+++ b/tests/test_amf_io.c
@@ -124,12 +124,11 @@ static void __tamf__get_object_mesh(
static void __tamf__get_object_mesh_element(
const void* cookie,
- enum gmio_amf_mesh_element element,
const struct gmio_amf_object_mesh_element_index* element_index,
void* ptr_element)
{
const struct __tamf__document* doc = (const struct __tamf__document*)cookie;
- switch (element) {
+ switch (element_index->element_type) {
case GMIO_AMF_MESH_ELEMENT_VERTEX: {
struct gmio_amf_vertex* ptr_vertex = (struct gmio_amf_vertex*)ptr_element;
ptr_vertex->coords = doc->mesh.vec_vertex[element_index->value];