From 5fd836bfdd26e92b87040ddb3b657d4e09f87831 Mon Sep 17 00:00:00 2001 From: Jeremy HU Date: Sun, 13 Nov 2022 20:41:49 +1100 Subject: [PATCH] Collect vertex source nodes --- dust3d/base/object.h | 4 +-- dust3d/mesh/mesh_generator.cc | 67 +++++------------------------------ dust3d/mesh/mesh_generator.h | 18 +++------- 3 files changed, 14 insertions(+), 75 deletions(-) diff --git a/dust3d/base/object.h b/dust3d/base/object.h index 4028b73b..bf3c8921 100644 --- a/dust3d/base/object.h +++ b/dust3d/base/object.h @@ -56,10 +56,8 @@ struct ObjectNode { class Object { public: - //std::vector nodes; - //std::vector, std::pair>> edges; std::vector vertices; - std::vector> vertexSourceNodes; + std::map sourceNodeMap; std::vector> triangleAndQuads; std::vector> triangles; std::unordered_map, std::array>> partTriangleUvs; diff --git a/dust3d/mesh/mesh_generator.cc b/dust3d/mesh/mesh_generator.cc index 758bce9f..a88bb0b0 100644 --- a/dust3d/mesh/mesh_generator.cc +++ b/dust3d/mesh/mesh_generator.cc @@ -682,6 +682,10 @@ std::unique_ptr MeshGenerator::combinePartMesh(const std::string& par { uv[2], uv[3], uv[0] } }); } } + const auto& vertexSources = tubeMeshBuilder->generatedVertexSources(); + for (size_t i = 0; i < vertexSources.size(); ++i) { + partCache.sourceNodeMap.emplace(std::make_pair(PositionKey(partCache.vertices[i]), vertexSources[i])); + } } } @@ -824,12 +828,8 @@ std::unique_ptr MeshGenerator::combineComponentMesh(const std::string componentCache.noneSeamVertices.insert(vertex); collectSharedQuadEdges(partCache.vertices, partCache.faces, &componentCache.sharedQuadEdges); componentCache.partTriangleUvs.insert({ Uuid(partIdString), partCache.triangleUvs }); - for (const auto& it : partCache.objectNodes) - componentCache.objectNodes.push_back(it); - for (const auto& it : partCache.objectEdges) - componentCache.objectEdges.push_back(it); - for (const auto& it : partCache.objectNodeVertices) - componentCache.objectNodeVertices.push_back(it); + for (const auto& it : partCache.sourceNodeMap) + componentCache.sourceNodeMap.emplace(it); } if (!partCache.joined) { if (mesh) @@ -963,12 +963,8 @@ std::unique_ptr MeshGenerator::combineComponentChildGroupMesh(const s componentCache.sharedQuadEdges.insert(it); for (const auto& it : childComponentCache.partTriangleUvs) componentCache.partTriangleUvs.insert({ it.first, it.second }); - for (const auto& it : childComponentCache.objectNodes) - componentCache.objectNodes.push_back(it); - for (const auto& it : childComponentCache.objectEdges) - componentCache.objectEdges.push_back(it); - for (const auto& it : childComponentCache.objectNodeVertices) - componentCache.objectNodeVertices.push_back(it); + for (const auto& it : childComponentCache.sourceNodeMap) + componentCache.sourceNodeMap.emplace(it); if (nullptr == subMesh || subMesh->isNull()) { continue; @@ -1021,28 +1017,6 @@ void MeshGenerator::setWeldEnabled(bool enabled) m_weldEnabled = enabled; } -void MeshGenerator::collectErroredParts() -{ - for (const auto& it : m_cacheContext->parts) { - if (!it.second.isSuccessful) { - if (!it.second.joined) - continue; - - auto updateVertexIndices = [=](std::vector>& faces, size_t vertexStartIndex) { - for (auto& it : faces) { - for (auto& subIt : it) - subIt += vertexStartIndex; - } - }; - - auto errorTriangleAndQuads = it.second.faces; - updateVertexIndices(errorTriangleAndQuads, m_object->vertices.size()); - m_object->vertices.insert(m_object->vertices.end(), it.second.vertices.begin(), it.second.vertices.end()); - m_object->triangleAndQuads.insert(m_object->triangleAndQuads.end(), errorTriangleAndQuads.begin(), errorTriangleAndQuads.end()); - } - } -} - void MeshGenerator::postprocessObject(Object* object) { std::vector combinedFacesNormals; @@ -1055,22 +1029,7 @@ void MeshGenerator::postprocessObject(Object* object) object->triangleNormals = combinedFacesNormals; - //std::vector> sourceNodes; - //resolveTriangleSourceNode(*object, m_nodeVertices, sourceNodes, &object->vertexSourceNodes); - //object->setTriangleSourceNodes(sourceNodes); - - //std::map, Color> sourceNodeToColorMap; - //for (const auto& node : object->nodes) - // sourceNodeToColorMap.insert({ { node.partId, node.nodeId }, node.color }); - object->triangleColors.resize(object->triangles.size(), Color::createWhite()); - //const std::vector>* triangleSourceNodes = object->triangleSourceNodes(); - //if (nullptr != triangleSourceNodes) { - //for (size_t triangleIndex = 0; triangleIndex < object->triangles.size(); triangleIndex++) { - //const auto& source = (*triangleSourceNodes)[triangleIndex]; - //object->triangleColors[triangleIndex] = sourceNodeToColorMap[source]; - //} - //} std::vector> triangleVertexNormals; smoothNormal(object->vertices, @@ -1116,11 +1075,6 @@ void MeshGenerator::collectUncombinedComponent(const std::string& componentIdStr if (nullptr == componentCache.mesh || componentCache.mesh->isNull()) { return; } - - //m_object->nodes.insert(m_object->nodes.end(), componentCache.objectNodes.begin(), componentCache.objectNodes.end()); - //m_object->edges.insert(m_object->edges.end(), componentCache.objectEdges.begin(), componentCache.objectEdges.end()); - m_nodeVertices.insert(m_nodeVertices.end(), componentCache.objectNodeVertices.begin(), componentCache.objectNodeVertices.end()); - collectIncombinableMesh(componentCache.mesh.get(), componentCache); return; } @@ -1293,10 +1247,8 @@ void MeshGenerator::generate() const auto& componentCache = m_cacheContext->components[to_string(Uuid())]; - //m_object->nodes = componentCache.objectNodes; - //m_object->edges = componentCache.objectEdges; + m_object->sourceNodeMap = componentCache.sourceNodeMap; m_object->partTriangleUvs = componentCache.partTriangleUvs; - m_nodeVertices = componentCache.objectNodeVertices; std::vector combinedVertices; std::vector> combinedFaces; @@ -1325,7 +1277,6 @@ void MeshGenerator::generate() // Recursively check uncombined components collectUncombinedComponent(to_string(Uuid())); - collectErroredParts(); postprocessObject(m_object); if (needDeleteCacheContext) { diff --git a/dust3d/mesh/mesh_generator.h b/dust3d/mesh/mesh_generator.h index 1f87e8ef..cc7549d5 100644 --- a/dust3d/mesh/mesh_generator.h +++ b/dust3d/mesh/mesh_generator.h @@ -44,11 +44,9 @@ public: struct GeneratedPart { std::vector vertices; + std::map sourceNodeMap; std::vector> faces; std::map, std::array> triangleUvs; - std::vector objectNodes; - std::vector, std::pair>> objectEdges; - std::vector>> objectNodeVertices; Color color = Color(1.0, 1.0, 1.0); float metalness = 0.0; float roughness = 1.0; @@ -59,9 +57,7 @@ public: vertices.clear(); faces.clear(); triangleUvs.clear(); - objectNodes.clear(); - objectEdges.clear(); - objectNodeVertices.clear(); + sourceNodeMap.clear(); color = Color(1.0, 1.0, 1.0); metalness = 0.0; roughness = 1.0; @@ -76,9 +72,7 @@ public: std::unordered_map, std::array>> partTriangleUvs; std::vector, std::array>> seamTriangleUvs; std::set noneSeamVertices; - std::vector objectNodes; - std::vector, std::pair>> objectEdges; - std::vector>> objectNodeVertices; + std::map sourceNodeMap; void reset() { mesh.reset(); @@ -86,9 +80,7 @@ public: partTriangleUvs.clear(); seamTriangleUvs.clear(); noneSeamVertices.clear(); - objectNodes.clear(); - objectEdges.clear(); - objectNodeVertices.clear(); + sourceNodeMap.clear(); } }; @@ -137,7 +129,6 @@ private: float m_mainProfileMiddleX = 0; float m_sideProfileMiddleX = 0; float m_mainProfileMiddleY = 0; - std::vector>> m_nodeVertices; std::map> m_partNodeIds; std::map> m_partEdgeIds; bool m_isSuccessful = false; @@ -172,7 +163,6 @@ private: void collectUncombinedComponent(const std::string& componentIdString); void cutFaceStringToCutTemplate(const std::string& cutFaceString, std::vector& cutTemplate); void postprocessObject(Object* object); - void collectErroredParts(); void preprocessMirror(); std::string reverseUuid(const std::string& uuidString); void recoverQuads(const std::vector& vertices, const std::vector>& triangles, const std::set>& sharedQuadEdges, std::vector>& triangleAndQuads);