From ce5b1530e3d05615a0e3b9fd9772c2dff4450844 Mon Sep 17 00:00:00 2001 From: Thomas Krijnen Date: Wed, 28 Feb 2024 11:12:17 +0100 Subject: [PATCH] Add buffer (bytestring) accessors to material colors - plus small fixes --- src/ifcwrap/IfcGeomWrapper.i | 28 ++++++++++++++++++++++++++-- 1 file changed, 26 insertions(+), 2 deletions(-) diff --git a/src/ifcwrap/IfcGeomWrapper.i b/src/ifcwrap/IfcGeomWrapper.i index 8811ae6a4b..394784819a 100644 --- a/src/ifcwrap/IfcGeomWrapper.i +++ b/src/ifcwrap/IfcGeomWrapper.i @@ -83,7 +83,7 @@ template std::pair vector_to_buffer(const T& t) { using V = typename std::remove_reference::type; - return { reinterpret_cast(t.data()), t.size() * sizeof(V::value_type) }; + return { reinterpret_cast(t.data()), t.size() * sizeof(typename V::value_type) }; } %} @@ -321,6 +321,29 @@ struct ShapeRTTI : public boost::static_visitor return vector_to_buffer(self->normals()); } + PyObject* colors_buffer() const { + std::vector clrs; + clrs.reserve(self->materials().size() * 4); + for (auto& m : self->materials()) { + if (m.diffuse) { + clrs.push_back(m.diffuse.ccomponents()[0]); + clrs.push_back(m.diffuse.ccomponents()[1]); + clrs.push_back(m.diffuse.ccomponents()[2]); + } else { + clrs.push_back(0.); + clrs.push_back(0.); + clrs.push_back(0.); + } + if (m.has_transparency()) { + clrs.push_back(1. - m.transparency); + } else { + clrs.push_back(1.); + } + } + auto p = vector_to_buffer(clrs); + return PyBytes_FromStringAndSize(p.first, p.second); + } + %pythoncode %{ # Hide the getters with read-only property implementations id = property(id) @@ -337,7 +360,8 @@ struct ShapeRTTI : public boost::static_visitor material_ids_buffer = property(material_ids_buffer) item_ids_buffer = property(item_ids_buffer) verts_buffer = property(verts_buffer) - normals = property(normals) + normals_buffer = property(normals_buffer) + colors_buffer = property(colors_buffer) %} };