Skip to content

Commit

Permalink
refactor!: path handling to use std::filesystem (#3308)
Browse files Browse the repository at this point in the history
Switch to using `std::filesystem` for path operations to modernize code and improve readability.
  • Loading branch information
AJPfleger authored Sep 9, 2024
1 parent 2798d98 commit 74d9aa2
Show file tree
Hide file tree
Showing 11 changed files with 73 additions and 134 deletions.
20 changes: 11 additions & 9 deletions Core/include/Acts/Visualization/GeometryView3D.hpp
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -14,6 +14,7 @@
#include "Acts/Visualization/IVisualization3D.hpp"
#include "Acts/Visualization/ViewConfig.hpp"

#include <filesystem>
#include <string>

namespace Acts {
Expand Down Expand Up @@ -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
///
Expand Down Expand Up @@ -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
///
Expand All @@ -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
///
Expand Down
22 changes: 3 additions & 19 deletions Core/include/Acts/Visualization/IVisualization3D.hpp
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -13,6 +13,7 @@

#include <array>
#include <cstddef>
#include <filesystem>
#include <fstream>
#include <string>
#include <vector>
Expand Down Expand Up @@ -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.
Expand Down
7 changes: 4 additions & 3 deletions Core/include/Acts/Visualization/ObjVisualization3D.hpp
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -13,6 +13,7 @@
#include "Acts/Visualization/ViewConfig.hpp"

#include <array>
#include <filesystem>
#include <fstream>
#include <iomanip>
#include <map>
Expand Down Expand Up @@ -61,8 +62,8 @@ class ObjVisualization3D : public IVisualization3D {
const std::vector<FaceType>& 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;
Expand Down
7 changes: 4 additions & 3 deletions Core/include/Acts/Visualization/PlyVisualization3D.hpp
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -13,6 +13,7 @@
#include "Acts/Visualization/ViewConfig.hpp"

#include <array>
#include <filesystem>
#include <fstream>
#include <string>
#include <utility>
Expand Down Expand Up @@ -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;
Expand Down
5 changes: 3 additions & 2 deletions Core/include/Acts/Visualization/ViewConfig.hpp
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -9,6 +9,7 @@
#pragma once

#include <array>
#include <filesystem>
#include <string>

namespace Acts {
Expand Down Expand Up @@ -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
23 changes: 13 additions & 10 deletions Core/include/Acts/Visualization/detail/ObjVisualization3D.ipp
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -69,18 +69,21 @@ void ObjVisualization3D<T>::faces(const std::vector<Vector3>& vtxs,
}

template <typename T>
void ObjVisualization3D<T>::write(const std::string& path) const {
void ObjVisualization3D<T>::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();
Expand Down
12 changes: 6 additions & 6 deletions Core/include/Acts/Visualization/detail/PlyVisualization3D.ipp
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -41,13 +41,13 @@ void PlyVisualization3D<T>::line(const Vector3& a, const Vector3& b,
}

template <typename T>
void PlyVisualization3D<T>::write(const std::string& path) const {
void PlyVisualization3D<T>::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();
}
Expand Down
5 changes: 1 addition & 4 deletions Core/src/Visualization/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1 @@
target_sources(
ActsCore
PRIVATE IVisualization3D.cpp GeometryView3D.cpp EventDataView3D.cpp
)
target_sources(ActsCore PRIVATE GeometryView3D.cpp EventDataView3D.cpp)
Loading

0 comments on commit 74d9aa2

Please sign in to comment.