From 9d71a275c3ce84617f1c57625cf658e1be884eb5 Mon Sep 17 00:00:00 2001 From: Andre Schulz Date: Sun, 3 Dec 2023 13:05:10 +0100 Subject: [PATCH] X3D: Don't convert IndexedLineSet polylines with > 2 indices to triangles/polygons Currently, when the coordIndex attribute of an IndexedLineSet contains a polyline with > 2 indices, X3DGeoHelper::coordIdx_str2faces_arr() will incorrectly determine the primitive type to be aiPrimitiveType_TRIANGLE or aiPrimitiveType_POLYGON instead of aiPrimitiveType_LINE. To fix this, this commit adds functions to explicitly handle an IndexedLineSet. Fixes #3101 --- code/AssetLib/X3D/X3DGeoHelper.cpp | 81 +++++++++++++++++++ code/AssetLib/X3D/X3DGeoHelper.h | 2 + code/AssetLib/X3D/X3DImporter_Postprocess.cpp | 2 +- 3 files changed, 84 insertions(+), 1 deletion(-) diff --git a/code/AssetLib/X3D/X3DGeoHelper.cpp b/code/AssetLib/X3D/X3DGeoHelper.cpp index 0a62ff9b0..1c962a460 100644 --- a/code/AssetLib/X3D/X3DGeoHelper.cpp +++ b/code/AssetLib/X3D/X3DGeoHelper.cpp @@ -188,6 +188,51 @@ mg_m_err: pFaces.clear(); } +void X3DGeoHelper::coordIdx_str2lines_arr(const std::vector &pCoordIdx, std::vector &pFaces) { + std::vector f_data(pCoordIdx); + + if (f_data.back() != (-1)) { + f_data.push_back(-1); + } + + // reserve average size. + pFaces.reserve(f_data.size() / 2); + for (std::vector::const_iterator startIt = f_data.cbegin(), endIt = f_data.cbegin(); endIt != f_data.cend(); ++endIt) { + // check for end of current polyline + if (*endIt != -1) + continue; + + // found end of polyline, check if this is a valid polyline + std::size_t numIndices = std::distance(startIt, endIt); + if (numIndices <= 1) + goto mg_m_err; + + // create line faces out of polyline indices + for (int32_t idx0 = *startIt++; startIt != endIt; ++startIt) { + int32_t idx1 = *startIt; + + aiFace tface; + tface.mNumIndices = 2; + tface.mIndices = new unsigned int[2]; + tface.mIndices[0] = idx0; + tface.mIndices[1] = idx1; + pFaces.push_back(tface); + + idx0 = idx1; + } + + ++startIt; + } + + return; + +mg_m_err: + for (size_t i = 0, i_e = pFaces.size(); i < i_e; i++) + delete[] pFaces[i].mIndices; + + pFaces.clear(); +} + void X3DGeoHelper::add_color(aiMesh &pMesh, const std::list &pColors, const bool pColorPerVertex) { std::list tcol; @@ -528,4 +573,40 @@ aiMesh *X3DGeoHelper::make_mesh(const std::vector &pCoordIdx, const std return tmesh; } +aiMesh *X3DGeoHelper::make_line_mesh(const std::vector &pCoordIdx, const std::list &pVertices) { + std::vector faces; + + // create faces array from input string with vertices indices. + X3DGeoHelper::coordIdx_str2lines_arr(pCoordIdx, faces); + if (!faces.size()) { + throw DeadlyImportError("Failed to create mesh, faces list is empty."); + } + + // + // Create new mesh and copy geometry data. + // + aiMesh *tmesh = new aiMesh; + size_t ts = faces.size(); + // faces + tmesh->mFaces = new aiFace[ts]; + tmesh->mNumFaces = static_cast(ts); + for (size_t i = 0; i < ts; i++) + tmesh->mFaces[i] = faces[i]; + + // vertices + std::list::const_iterator vit = pVertices.begin(); + + ts = pVertices.size(); + tmesh->mVertices = new aiVector3D[ts]; + tmesh->mNumVertices = static_cast(ts); + for (size_t i = 0; i < ts; i++) { + tmesh->mVertices[i] = *vit++; + } + + // set primitive type and return result. + tmesh->mPrimitiveTypes = aiPrimitiveType_LINE; + + return tmesh; +} + } // namespace Assimp diff --git a/code/AssetLib/X3D/X3DGeoHelper.h b/code/AssetLib/X3D/X3DGeoHelper.h index 78e57f9da..c740b4288 100644 --- a/code/AssetLib/X3D/X3DGeoHelper.h +++ b/code/AssetLib/X3D/X3DGeoHelper.h @@ -21,6 +21,7 @@ public: static void polylineIdx_to_lineIdx(const std::list &pPolylineCoordIdx, std::list &pLineCoordIdx); static void rect_parallel_epiped(const aiVector3D &pSize, std::list &pVertices); static void coordIdx_str2faces_arr(const std::vector &pCoordIdx, std::vector &pFaces, unsigned int &pPrimitiveTypes); + static void coordIdx_str2lines_arr(const std::vector &pCoordIdx, std::vector &pFaces); static void add_color(aiMesh &pMesh, const std::list &pColors, const bool pColorPerVertex); static void add_color(aiMesh &pMesh, const std::list &pColors, const bool pColorPerVertex); static void add_color(aiMesh &pMesh, const std::vector &pCoordIdx, const std::vector &pColorIdx, @@ -34,6 +35,7 @@ public: const std::list &pTexCoords); static void add_tex_coord(aiMesh &pMesh, const std::list &pTexCoords); static aiMesh *make_mesh(const std::vector &pCoordIdx, const std::list &pVertices); + static aiMesh *make_line_mesh(const std::vector &pCoordIdx, const std::list &pVertices); }; } // namespace Assimp diff --git a/code/AssetLib/X3D/X3DImporter_Postprocess.cpp b/code/AssetLib/X3D/X3DImporter_Postprocess.cpp index 87121ef5f..216929076 100644 --- a/code/AssetLib/X3D/X3DImporter_Postprocess.cpp +++ b/code/AssetLib/X3D/X3DImporter_Postprocess.cpp @@ -320,7 +320,7 @@ void X3DImporter::Postprocess_BuildMesh(const X3DNodeElementBase &pNodeElement, // at first search for node and create mesh. for (std::list::iterator ch_it = tnemesh.Children.begin(); ch_it != tnemesh.Children.end(); ++ch_it) { if ((*ch_it)->Type == X3DElemType::ENET_Coordinate) { - *pMesh = X3DGeoHelper::make_mesh(tnemesh.CoordIndex, ((X3DNodeElementCoordinate *)*ch_it)->Value); + *pMesh = X3DGeoHelper::make_line_mesh(tnemesh.CoordIndex, ((X3DNodeElementCoordinate *)*ch_it)->Value); } }