From 74d9aa24653027858659e28f3e6f0275cf799d6c Mon Sep 17 00:00:00 2001 From: "Alexander J. Pfleger" <70842573+AJPfleger@users.noreply.github.com> Date: Mon, 9 Sep 2024 19:47:39 +0200 Subject: [PATCH] refactor!: path handling to use `std::filesystem` (#3308) Switch to using `std::filesystem` for path operations to modernize code and improve readability. --- .../Acts/Visualization/GeometryView3D.hpp | 20 ++--- .../Acts/Visualization/IVisualization3D.hpp | 22 +----- .../Acts/Visualization/ObjVisualization3D.hpp | 7 +- .../Acts/Visualization/PlyVisualization3D.hpp | 7 +- .../include/Acts/Visualization/ViewConfig.hpp | 5 +- .../detail/ObjVisualization3D.ipp | 23 +++--- .../detail/PlyVisualization3D.ipp | 12 +-- Core/src/Visualization/CMakeLists.txt | 5 +- Core/src/Visualization/GeometryView3D.cpp | 77 ++++++------------- Core/src/Visualization/IVisualization3D.cpp | 23 ------ .../Io/Obj/src/ObjTrackingGeometryWriter.cpp | 6 +- 11 files changed, 73 insertions(+), 134 deletions(-) delete mode 100644 Core/src/Visualization/IVisualization3D.cpp diff --git a/Core/include/Acts/Visualization/GeometryView3D.hpp b/Core/include/Acts/Visualization/GeometryView3D.hpp index 0243e8e58ad..f1abba71424 100644 --- a/Core/include/Acts/Visualization/GeometryView3D.hpp +++ b/Core/include/Acts/Visualization/GeometryView3D.hpp @@ -1,6 +1,6 @@ // This file is part of the Acts project. // -// Copyright (C) 2020 CERN for the benefit of the Acts project +// Copyright (C) 2020-2024 CERN for the benefit of the Acts project // // This Source Code Form is subject to the terms of the Mozilla Public // License, v. 2.0. If a copy of the MPL was not distributed with this @@ -14,6 +14,7 @@ #include "Acts/Visualization/IVisualization3D.hpp" #include "Acts/Visualization/ViewConfig.hpp" +#include #include namespace Acts { @@ -75,7 +76,7 @@ struct GeometryView3D { const ViewConfig& sensitiveConfig = s_viewSensitive, const ViewConfig& passiveConfig = s_viewPassive, const ViewConfig& gridConfig = s_viewGrid, - const std::string& outputDir = "."); + const std::filesystem::path& outputDir = std::filesystem::path(".")); /// Helper method to draw Volume objects /// @@ -132,12 +133,12 @@ struct GeometryView3D { /// @param sensitiveConfig The drawing configuration for sensitive surfaces /// @param gridConfig The drawing configuration for grid display /// @param outputDir Directory to write to - static void drawLayer(IVisualization3D& helper, const Layer& layer, - const GeometryContext& gctx, - const ViewConfig& layerConfig = s_viewPassive, - const ViewConfig& sensitiveConfig = s_viewSensitive, - const ViewConfig& gridConfig = s_viewGrid, - const std::string& outputDir = "."); + static void drawLayer( + IVisualization3D& helper, const Layer& layer, const GeometryContext& gctx, + const ViewConfig& layerConfig = s_viewPassive, + const ViewConfig& sensitiveConfig = s_viewSensitive, + const ViewConfig& gridConfig = s_viewGrid, + const std::filesystem::path& outputDir = std::filesystem::path(".")); /// Helper method to draw TrackingVolume objects /// @@ -161,7 +162,8 @@ struct GeometryView3D { const ViewConfig& layerView = s_viewPassive, const ViewConfig& sensitiveView = s_viewSensitive, const ViewConfig& gridView = s_viewGrid, bool writeIt = true, - const std::string& tag = "", const std::string& outputDir = "."); + const std::string& tag = "", + const std::filesystem::path& outputDir = std::filesystem::path(".")); /// Helper method to draw lines - base for all lines /// diff --git a/Core/include/Acts/Visualization/IVisualization3D.hpp b/Core/include/Acts/Visualization/IVisualization3D.hpp index 6daf34c4e2f..96af6328979 100644 --- a/Core/include/Acts/Visualization/IVisualization3D.hpp +++ b/Core/include/Acts/Visualization/IVisualization3D.hpp @@ -1,6 +1,6 @@ // This file is part of the Acts project. // -// Copyright (C) 2019 CERN for the benefit of the Acts project +// Copyright (C) 2019-2024 CERN for the benefit of the Acts project // // This Source Code Form is subject to the terms of the Mozilla Public // License, v. 2.0. If a copy of the MPL was not distributed with this @@ -13,6 +13,7 @@ #include #include +#include #include #include #include @@ -67,28 +68,11 @@ class IVisualization3D { /// Write the content of the helper to an outstream. /// @param path is the file system path for writing the file - /// @note will change to std::filesystem::path once gcc9 is standard - virtual void write(const std::string& path) const = 0; + virtual void write(const std::filesystem::path& path) const = 0; /// Remove all contents of this helper /// virtual void clear() = 0; - - protected: - /// Helper: check for extension - /// - /// @note this is a placeholder for std::filesystem::has_extension - /// which needs special linking until gcc9 - /// @param path the path to be checked - bool hasExtension(const std::string& path) const; - - /// Helper: replace the extension - /// - /// @note this is a placeholder for std::filesystem::replace_extension - /// which needs special linking until gcc9 - /// @param path [in,out] the path to be changed - /// @param suffix the extension to be added - void replaceExtension(std::string& path, const std::string& suffix) const; }; /// Overload of the << operator to facilitate writing to streams. diff --git a/Core/include/Acts/Visualization/ObjVisualization3D.hpp b/Core/include/Acts/Visualization/ObjVisualization3D.hpp index a915ecadee8..79da56d3e9c 100644 --- a/Core/include/Acts/Visualization/ObjVisualization3D.hpp +++ b/Core/include/Acts/Visualization/ObjVisualization3D.hpp @@ -1,6 +1,6 @@ // This file is part of the Acts project. // -// Copyright (C) 2019 CERN for the benefit of the Acts project +// Copyright (C) 2019-2024 CERN for the benefit of the Acts project // // This Source Code Form is subject to the terms of the Mozilla Public // License, v. 2.0. If a copy of the MPL was not distributed with this @@ -13,6 +13,7 @@ #include "Acts/Visualization/ViewConfig.hpp" #include +#include #include #include #include @@ -61,8 +62,8 @@ class ObjVisualization3D : public IVisualization3D { const std::vector& faces, ColorRGB color = {0, 0, 0}) final; - /// @copydoc Acts::IVisualization3D::write(const std::string&) const - void write(const std::string& path) const final; + /// @copydoc Acts::IVisualization3D::write(const std::filesystem::path&) const + void write(const std::filesystem::path& path) const final; /// @copydoc Acts::IVisualization3D::write(std::ostream&) const void write(std::ostream& os) const final; diff --git a/Core/include/Acts/Visualization/PlyVisualization3D.hpp b/Core/include/Acts/Visualization/PlyVisualization3D.hpp index b28783a0bcd..95f2fdacd28 100644 --- a/Core/include/Acts/Visualization/PlyVisualization3D.hpp +++ b/Core/include/Acts/Visualization/PlyVisualization3D.hpp @@ -1,6 +1,6 @@ // This file is part of the Acts project. // -// Copyright (C) 2019 CERN for the benefit of the Acts project +// Copyright (C) 2019-2024 CERN for the benefit of the Acts project // // This Source Code Form is subject to the terms of the Mozilla Public // License, v. 2.0. If a copy of the MPL was not distributed with this @@ -13,6 +13,7 @@ #include "Acts/Visualization/ViewConfig.hpp" #include +#include #include #include #include @@ -50,8 +51,8 @@ class PlyVisualization3D : public IVisualization3D { void line(const Vector3& a, const Vector3& b, ColorRGB color = {120, 120, 120}) final; - /// @copydoc Acts::IVisualization3D::write(const std::string&) const - void write(const std::string& path) const final; + /// @copydoc Acts::IVisualization3D::write(const std::filesystem::path&) const + void write(const std::filesystem::path& path) const final; /// @copydoc Acts::IVisualization3D::write(std::ostream&) const void write(std::ostream& os) const final; diff --git a/Core/include/Acts/Visualization/ViewConfig.hpp b/Core/include/Acts/Visualization/ViewConfig.hpp index 06c4f7a675b..7e6e70dbdcc 100644 --- a/Core/include/Acts/Visualization/ViewConfig.hpp +++ b/Core/include/Acts/Visualization/ViewConfig.hpp @@ -1,6 +1,6 @@ // This file is part of the Acts project. // -// Copyright (C) 2020 CERN for the benefit of the Acts project +// Copyright (C) 2020-2024 CERN for the benefit of the Acts project // // This Source Code Form is subject to the terms of the Mozilla Public // License, v. 2.0. If a copy of the MPL was not distributed with this @@ -9,6 +9,7 @@ #pragma once #include +#include #include namespace Acts { @@ -42,7 +43,7 @@ struct ViewConfig { /// Whether to triangulate or not bool triangulate = false; /// Write name - non-empty string indicates writing - std::string outputName = ""; + std::filesystem::path outputName = std::filesystem::path(""); }; } // namespace Acts diff --git a/Core/include/Acts/Visualization/detail/ObjVisualization3D.ipp b/Core/include/Acts/Visualization/detail/ObjVisualization3D.ipp index 51a0854b85e..e43bbb11f19 100644 --- a/Core/include/Acts/Visualization/detail/ObjVisualization3D.ipp +++ b/Core/include/Acts/Visualization/detail/ObjVisualization3D.ipp @@ -1,6 +1,6 @@ // This file is part of the Acts project. // -// Copyright (C) 2020 CERN for the benefit of the Acts project +// Copyright (C) 2020-2024 CERN for the benefit of the Acts project // // This Source Code Form is subject to the terms of the Mozilla Public // License, v. 2.0. If a copy of the MPL was not distributed with this @@ -69,18 +69,21 @@ void ObjVisualization3D::faces(const std::vector& vtxs, } template -void ObjVisualization3D::write(const std::string& path) const { +void ObjVisualization3D::write(const std::filesystem::path& path) const { std::ofstream os; - std::string objectpath = path; - if (!IVisualization3D::hasExtension(objectpath)) { - objectpath += std::string(".obj"); + std::filesystem::path objectpath = path; + if (!objectpath.has_extension()) { + objectpath.replace_extension(std::filesystem::path("obj")); } - os.open(objectpath); - std::string mtlpath = objectpath; - IVisualization3D::replaceExtension(mtlpath, ".mtl"); - os << "mtllib " << mtlpath << "\n"; + os.open(std::filesystem::absolute(objectpath).string()); + std::filesystem::path mtlpath = objectpath; + mtlpath.replace_extension(std::filesystem::path("mtl")); + + const std::string mtlpathString = std::filesystem::absolute(mtlpath).string(); + os << "mtllib " << mtlpathString << "\n"; std::ofstream mtlos; - mtlos.open(mtlpath); + mtlos.open(mtlpathString); + write(os, mtlos); os.close(); mtlos.close(); diff --git a/Core/include/Acts/Visualization/detail/PlyVisualization3D.ipp b/Core/include/Acts/Visualization/detail/PlyVisualization3D.ipp index b97dbde0fc9..c5c12b2dc89 100644 --- a/Core/include/Acts/Visualization/detail/PlyVisualization3D.ipp +++ b/Core/include/Acts/Visualization/detail/PlyVisualization3D.ipp @@ -1,6 +1,6 @@ // This file is part of the Acts project. // -// Copyright (C) 2020 CERN for the benefit of the Acts project +// Copyright (C) 2020-2024 CERN for the benefit of the Acts project // // This Source Code Form is subject to the terms of the Mozilla Public // License, v. 2.0. If a copy of the MPL was not distributed with this @@ -41,13 +41,13 @@ void PlyVisualization3D::line(const Vector3& a, const Vector3& b, } template -void PlyVisualization3D::write(const std::string& path) const { +void PlyVisualization3D::write(const std::filesystem::path& path) const { std::ofstream os; - std::string objectpath = path; - if (!IVisualization3D::hasExtension(path)) { - objectpath += std::string(".ply"); + std::filesystem::path objectpath = path; + if (!objectpath.has_extension()) { + objectpath.replace_extension(std::filesystem::path("ply")); } - os.open(objectpath); + os.open(std::filesystem::absolute(objectpath).string()); write(os); os.close(); } diff --git a/Core/src/Visualization/CMakeLists.txt b/Core/src/Visualization/CMakeLists.txt index ea2bd2427e6..5d4f916af1c 100644 --- a/Core/src/Visualization/CMakeLists.txt +++ b/Core/src/Visualization/CMakeLists.txt @@ -1,4 +1 @@ -target_sources( - ActsCore - PRIVATE IVisualization3D.cpp GeometryView3D.cpp EventDataView3D.cpp -) +target_sources(ActsCore PRIVATE GeometryView3D.cpp EventDataView3D.cpp) diff --git a/Core/src/Visualization/GeometryView3D.cpp b/Core/src/Visualization/GeometryView3D.cpp index 93f45c4e91d..14923b66a50 100644 --- a/Core/src/Visualization/GeometryView3D.cpp +++ b/Core/src/Visualization/GeometryView3D.cpp @@ -1,6 +1,6 @@ // This file is part of the Acts project. // -// Copyright (C) 2020 CERN for the benefit of the Acts project +// Copyright (C) 2020-2024 CERN for the benefit of the Acts project // // This Source Code Form is subject to the terms of the Mozilla Public // License, v. 2.0. If a copy of the MPL was not distributed with this @@ -34,36 +34,12 @@ #include #include +#include #include #include #include #include -#include -#include - -namespace { - -std::string joinPaths(const std::string& a, const std::string& b) { - if (b.substr(0, 1) == "/" || a.empty()) { - return b; - } - - if (a.substr(a.size() - 1) == "/") { - return a.substr(a.size() - 1) + "/" + b; - } - - return a + "/" + b; -} - -std::string getWorkingDirectory() { - char buffer[PATH_MAX]; - return (getcwd(buffer, sizeof(buffer)) != nullptr ? std::string(buffer) - : std::string("")); -} - -} // namespace - namespace Acts::Experimental { ViewConfig s_viewSensitive = ViewConfig({0, 180, 240}); ViewConfig s_viewPassive = ViewConfig({240, 280, 0}); @@ -102,9 +78,7 @@ void Acts::GeometryView3D::drawSurfaceArray( IVisualization3D& helper, const SurfaceArray& surfaceArray, const GeometryContext& gctx, const Transform3& transform, const ViewConfig& sensitiveConfig, const ViewConfig& passiveConfig, - const ViewConfig& gridConfig, const std::string& _outputDir) { - std::string outputDir = - _outputDir == "." ? getWorkingDirectory() : _outputDir; + const ViewConfig& gridConfig, const std::filesystem::path& outputDir) { // Draw all the surfaces Extent arrayExtent; for (const auto& sf : surfaceArray.surfaces()) { @@ -117,7 +91,7 @@ void Acts::GeometryView3D::drawSurfaceArray( } if (!sensitiveConfig.outputName.empty()) { - helper.write(joinPaths(outputDir, sensitiveConfig.outputName)); + helper.write(outputDir / sensitiveConfig.outputName); helper.clear(); } @@ -181,7 +155,7 @@ void Acts::GeometryView3D::drawSurfaceArray( } if (!gridConfig.outputName.empty()) { - helper.write(joinPaths(outputDir, gridConfig.outputName)); + helper.write(outputDir / gridConfig.outputName); helper.clear(); } } @@ -239,10 +213,7 @@ void Acts::GeometryView3D::drawDetectorVolume( void Acts::GeometryView3D::drawLayer( IVisualization3D& helper, const Layer& layer, const GeometryContext& gctx, const ViewConfig& layerConfig, const ViewConfig& sensitiveConfig, - const ViewConfig& gridConfig, const std::string& _outputDir) { - std::string outputDir = - _outputDir == "." ? getWorkingDirectory() : _outputDir; - + const ViewConfig& gridConfig, const std::filesystem::path& outputDir) { if (layerConfig.visible) { auto layerVolume = layer.representingVolume(); if (layerVolume != nullptr) { @@ -254,7 +225,7 @@ void Acts::GeometryView3D::drawLayer( layerConfig); } if (!layerConfig.outputName.empty()) { - helper.write(joinPaths(outputDir, layerConfig.outputName)); + helper.write(outputDir / layerConfig.outputName); helper.clear(); } } @@ -273,9 +244,7 @@ void Acts::GeometryView3D::drawTrackingVolume( const GeometryContext& gctx, const ViewConfig& containerView, const ViewConfig& volumeView, const ViewConfig& layerView, const ViewConfig& sensitiveView, const ViewConfig& gridView, bool writeIt, - const std::string& tag, const std::string& _outputDir) { - std::string outputDir = - _outputDir == "." ? getWorkingDirectory() : _outputDir; + const std::string& tag, const std::filesystem::path& outputDir) { if (tVolume.confinedVolumes() != nullptr) { const auto& subVolumes = tVolume.confinedVolumes()->arrayObjects(); for (const auto& tv : subVolumes) { @@ -303,10 +272,9 @@ void Acts::GeometryView3D::drawTrackingVolume( } if (tVolume.confinedVolumes() == nullptr) { vcConfig = vConfig; - vcConfig.outputName = vname + std::string("_boundaries") + tag; + vcConfig.outputName = + std::filesystem::path(vname + std::string("_boundaries") + tag); } else { - std::stringstream vs; - vs << "Container"; std::vector ids{tVolume.geometryId().volume()}; for (const auto* current = &tVolume; current->motherVolume() != nullptr; @@ -314,11 +282,14 @@ void Acts::GeometryView3D::drawTrackingVolume( ids.push_back(current->motherVolume()->geometryId().volume()); } - for (std::size_t i = ids.size() - 1; i < ids.size(); --i) { - vs << "_v" << ids[i]; + std::ranges::reverse(ids); + vname = "Container"; + for (const auto& id : ids) { + vname += "_v" + std::to_string(id); } - vname = vs.str(); - vcConfig.outputName = vname + std::string("_boundaries") + tag; + + vcConfig.outputName = + std::filesystem::path(vname + std::string("_boundaries") + tag); } } @@ -328,7 +299,7 @@ void Acts::GeometryView3D::drawTrackingVolume( Transform3::Identity(), vcConfig); } if (writeIt) { - std::string outputName = joinPaths(outputDir, vcConfig.outputName); + const std::filesystem::path outputName = outputDir / vcConfig.outputName; helper.write(outputName); helper.clear(); } @@ -338,12 +309,12 @@ void Acts::GeometryView3D::drawTrackingVolume( std::size_t il = 0; for (const auto& tl : layers) { if (writeIt) { - lConfig.outputName = - vname + std::string("_passives_l") + std::to_string(il) + tag; - sConfig.outputName = - vname + std::string("_sensitives_l") + std::to_string(il) + tag; - gConfig.outputName = - vname + std::string("_grids_l") + std::to_string(il) + tag; + lConfig.outputName = std::filesystem::path( + vname + std::string("_passives_l") + std::to_string(il) + tag); + sConfig.outputName = std::filesystem::path( + vname + std::string("_sensitives_l") + std::to_string(il) + tag); + gConfig.outputName = std::filesystem::path( + vname + std::string("_grids_l") + std::to_string(il) + tag); } drawLayer(helper, *tl, gctx, lConfig, sConfig, gConfig, outputDir); ++il; diff --git a/Core/src/Visualization/IVisualization3D.cpp b/Core/src/Visualization/IVisualization3D.cpp deleted file mode 100644 index aad131a156d..00000000000 --- a/Core/src/Visualization/IVisualization3D.cpp +++ /dev/null @@ -1,23 +0,0 @@ -// This file is part of the Acts project. -// -// Copyright (C) 2020 CERN for the benefit of the Acts project -// -// This Source Code Form is subject to the terms of the Mozilla Public -// License, v. 2.0. If a copy of the MPL was not distributed with this -// file, You can obtain one at http://mozilla.org/MPL/2.0/. - -#include "Acts/Visualization/IVisualization3D.hpp" - -bool Acts::IVisualization3D::hasExtension(const std::string& path) const { - return (path.find(".") != std::string::npos); -} - -void Acts::IVisualization3D::replaceExtension(std::string& path, - const std::string& suffix) const { - auto ppoint = path.find_last_of("."); - if (ppoint != std::string::npos) { - path.replace(ppoint, path.length(), suffix); - } else { - path += suffix; - } -} diff --git a/Examples/Io/Obj/src/ObjTrackingGeometryWriter.cpp b/Examples/Io/Obj/src/ObjTrackingGeometryWriter.cpp index 707add19bfd..a0f4948a1ff 100644 --- a/Examples/Io/Obj/src/ObjTrackingGeometryWriter.cpp +++ b/Examples/Io/Obj/src/ObjTrackingGeometryWriter.cpp @@ -1,6 +1,6 @@ // This file is part of the Acts project. // -// Copyright (C) 2017 CERN for the benefit of the Acts project +// Copyright (C) 2017-2024 CERN for the benefit of the Acts project // // This Source Code Form is subject to the terms of the Mozilla Public // License, v. 2.0. If a copy of the MPL was not distributed with this @@ -14,6 +14,8 @@ #include #include +#include + ActsExamples::ObjTrackingGeometryWriter::ObjTrackingGeometryWriter( const ActsExamples::ObjTrackingGeometryWriter::Config& config, Acts::Logging::Level level) @@ -43,5 +45,5 @@ void ActsExamples::ObjTrackingGeometryWriter::write( Acts::GeometryView3D::drawTrackingVolume( objVis, tVolume, context.geoContext, m_cfg.containerView, m_cfg.volumeView, m_cfg.passiveView, m_cfg.sensitiveView, m_cfg.gridView, - true, "", m_cfg.outputDir); + true, "", std::filesystem::path(m_cfg.outputDir)); }