@@ -130,6 +130,16 @@ PYBIND11_MODULE(tinyobjloader, tobj_module)
130130 })
131131 .def_readonly (" indices" , &mesh_t ::indices)
132132 .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+
133143 auto ret = py::array_t <int >(instance.indices .size () * 3 );
134144 py::buffer_info buf = ret.request ();
135145 memcpy (buf.ptr , instance.indices .data (), instance.indices .size () * 3 * sizeof (int ));
0 commit comments