@@ -130,6 +130,16 @@ PYBIND11_MODULE(tinyobjloader, tobj_module)
130
130
})
131
131
.def_readonly (" indices" , &mesh_t ::indices)
132
132
.def (" numpy_indices" , [] (mesh_t &instance) {
133
+ // Flatten indexes. index_t is composed of 3 ints(vertex_index, normal_index, texcoord_index).
134
+ // numpy_indices = [0, -1, -1, 1, -1, -1, ...]
135
+ // C++11 or later should pack POD struct tightly and does not reorder variables,
136
+ // so we can memcpy to copy data.
137
+ // Still, we check the size of struct and byte offsets of each variable just for sure.
138
+ static_assert (sizeof (index_t ) == 12 , " sizeof(index_t) must be 12" );
139
+ static_assert (offsetof (index_t , vertex_index) == 0 , " offsetof(index_t, vertex_index) must be 0" );
140
+ static_assert (offsetof (index_t , normal_index) == 4 , " offsetof(index_t, normal_index) must be 4" );
141
+ static_assert (offsetof (index_t , texcoord_index) == 8 , " offsetof(index_t, texcoord_index) must be 8" );
142
+
133
143
auto ret = py::array_t <int >(instance.indices .size () * 3 );
134
144
py::buffer_info buf = ret.request ();
135
145
memcpy (buf.ptr , instance.indices .data (), instance.indices .size () * 3 * sizeof (int ));
0 commit comments