From 73b04a75889aab0c80c8a87cf8b2134f12f9729f Mon Sep 17 00:00:00 2001 From: David Kekesi Date: Tue, 22 Oct 2024 12:38:22 +0200 Subject: [PATCH] code cleanup --- SpeckleLib/Speckle/Primitive/Mesh/Mesh.cpp | 7 +- SpeckleLib/Speckle/Primitive/Mesh/Mesh.h | 11 +- SpeckleLib/Speckle/Record/Element/Element.cpp | 101 +----------------- 3 files changed, 18 insertions(+), 101 deletions(-) diff --git a/SpeckleLib/Speckle/Primitive/Mesh/Mesh.cpp b/SpeckleLib/Speckle/Primitive/Mesh/Mesh.cpp index 44cf936..fa060e0 100644 --- a/SpeckleLib/Speckle/Primitive/Mesh/Mesh.cpp +++ b/SpeckleLib/Speckle/Primitive/Mesh/Mesh.cpp @@ -34,14 +34,15 @@ namespace { Append a single face to the Mesh given by the vertices --------------------------------------------------------------------*/ void Mesh::appendFace(const std::vector& vertices) { + if (vertices.empty()) + return; + m_vertices.insert(m_vertices.end(), vertices.begin(), vertices.end()); - int lastVertexIndex = m_faces.back(); + int lastVertexIndex = m_faces.empty() ? -1 : m_faces.back(); int faceSize = vertices.size() / 3; m_faces.push_back(faceSize); for (int i = 0; i < faceSize; i++) - { m_faces.push_back(++lastVertexIndex); - } } /*-------------------------------------------------------------------- diff --git a/SpeckleLib/Speckle/Primitive/Mesh/Mesh.h b/SpeckleLib/Speckle/Primitive/Mesh/Mesh.h index 0604730..c9a8e9a 100644 --- a/SpeckleLib/Speckle/Primitive/Mesh/Mesh.h +++ b/SpeckleLib/Speckle/Primitive/Mesh/Mesh.h @@ -29,10 +29,19 @@ namespace speckle::primitive { Mesh(active::measure::LengthType unit = active::measure::LengthType::metre) : base{utility::Guid{true}, utility::Guid{}, unit} {} /*! Constructor + @param unit The mesh unit type + @param material The mesh material + */ + Mesh(const ModelerAPI::Material& material, + active::measure::LengthType unit = active::measure::LengthType::metre) : + base{ utility::Guid{true}, utility::Guid{}, unit }, m_material{ material } {} + /*! + Constructor + @param unit The mesh unit type @param vertices The mesh vertices @param faces The mesh faces (the number of indices in the face followed by the vertex indices) @param colors The mesh face colours - @param unit The mesh unit type + @param material The mesh material */ Mesh(std::vector&& vertices, std::vector&& faces, std::vector&& colors, const ModelerAPI::Material& material, active::measure::LengthType unit = active::measure::LengthType::metre) : diff --git a/SpeckleLib/Speckle/Record/Element/Element.cpp b/SpeckleLib/Speckle/Record/Element/Element.cpp index 91a6fb8..805cc8d 100644 --- a/SpeckleLib/Speckle/Record/Element/Element.cpp +++ b/SpeckleLib/Speckle/Record/Element/Element.cpp @@ -135,91 +135,6 @@ String Element::getTypeName() const { return: A pointer to the element body --------------------------------------------------------------------*/ -/*Element::Body* Element::getBody() const { -#ifdef ARCHICAD - if (m_data->m_cache) { - return m_data->m_cache.get(); - } - - - void* dummy = nullptr; - GSErrCode err = ACAPI_Sight_GetCurrentWindowSight(&dummy); - if (err != NoError) - { - // TODO: should this throw? - } - - Modeler::SightPtr currentSightPtr((Modeler::Sight*)dummy); // init the shared ptr with the raw pointer - ModelerAPI::Model acModel; - Modeler::IAttributeReader* attrReader = ACAPI_Attribute_GetCurrentAttributeSetReader(); - - err = EXPGetModel(currentSightPtr, &acModel, attrReader); - if (err != NoError) - { - // TODO: should this throw? - } - - auto elementBody = new Element::Body(); - - Int32 nElements = acModel.GetElementCount(); - for (Int32 iElement = 1; iElement <= nElements; iElement++) - { - ModelerAPI::Element elem{}; - acModel.GetElement(iElement, &elem); - if (elem.GetElemGuid() != getHead().guid) - continue; - - Int32 nBodies = elem.GetTessellatedBodyCount(); - for (Int32 bodyIndex = 1; bodyIndex <= nBodies; ++bodyIndex) - { - ModelerAPI::MeshBody body{}; - elem.GetTessellatedBody(bodyIndex, &body); - - Int32 polyCount = body.GetPolygonCount(); - for (Int32 polyIndex = 1; polyIndex <= polyCount; ++polyIndex) - { - ModelerAPI::Polygon polygon{}; - body.GetPolygon(polyIndex, &polygon); - - ModelerAPI::Material material{}; - polygon.GetMaterial(&material); - auto materialName = material.GetName(); - - Int32 convexPolyCount = polygon.GetConvexPolygonCount(); - - for (Int32 convPolyIndex = 1; convPolyIndex <= convexPolyCount; ++convPolyIndex) - { - std::vector vertices; - std::vector faces; - std::vector colors; - - ModelerAPI::ConvexPolygon convexPolygon{}; - polygon.GetConvexPolygon(convPolyIndex, &convexPolygon); - Int32 vertexCount = convexPolygon.GetVertexCount(); - - faces.push_back(vertexCount); - for (Int32 vertexIndex = 1; vertexIndex <= vertexCount; ++vertexIndex) - { - ModelerAPI::Vertex vertex{}; - body.GetVertex(convexPolygon.GetVertexIndex(vertexIndex), &vertex); - - // TODO: change vertices array to hold Vertex instead of double values - vertices.push_back(vertex.x); - vertices.push_back(vertex.y); - vertices.push_back(vertex.z); - - faces.push_back(vertexIndex - 1); - } - elementBody->push_back(primitive::Mesh(std::move(vertices), std::move(faces), std::move(colors), material)); - } - } - } - } - m_data->m_cache.reset(elementBody); - return m_data->m_cache.get(); -#endif -}*/ - Element::Body* Element::getBody() const { #ifdef ARCHICAD if (m_data->m_cache) { @@ -271,20 +186,19 @@ Element::Body* Element::getBody() const { ModelerAPI::Material material{}; polygon.GetMaterial(&material); auto materialName = material.GetName(); + if (materialMeshMap.find(materialName) == materialMeshMap.end()) { + materialMeshMap[materialName] = primitive::Mesh(material); + } Int32 convexPolyCount = polygon.GetConvexPolygonCount(); for (Int32 convPolyIndex = 1; convPolyIndex <= convexPolyCount; ++convPolyIndex) { std::vector vertices; - std::vector faces; - std::vector colors; - ModelerAPI::ConvexPolygon convexPolygon{}; polygon.GetConvexPolygon(convPolyIndex, &convexPolygon); Int32 vertexCount = convexPolygon.GetVertexCount(); - faces.push_back(vertexCount); for (Int32 vertexIndex = 1; vertexIndex <= vertexCount; ++vertexIndex) { ModelerAPI::Vertex vertex{}; @@ -294,16 +208,9 @@ Element::Body* Element::getBody() const { vertices.push_back(vertex.x); vertices.push_back(vertex.y); vertices.push_back(vertex.z); - - faces.push_back(vertexIndex - 1); } - if (materialMeshMap.find(materialName) != materialMeshMap.end()) { - materialMeshMap[materialName].appendFace(std::move(vertices)); - } - else { - materialMeshMap[materialName] = primitive::Mesh(std::move(vertices), std::move(faces), std::move(colors), material); - } + materialMeshMap[materialName].appendFace(std::move(vertices)); } } }