Skip to content

Commit

Permalink
update writer
Browse files Browse the repository at this point in the history
  • Loading branch information
AJPfleger committed Sep 2, 2024
1 parent 5a40a31 commit 9e32a5c
Show file tree
Hide file tree
Showing 8 changed files with 70 additions and 77 deletions.
20 changes: 2 additions & 18 deletions Core/include/Acts/Visualization/IVisualization3D.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
4 changes: 2 additions & 2 deletions Core/include/Acts/Visualization/ObjVisualization3D.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,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
4 changes: 2 additions & 2 deletions Core/include/Acts/Visualization/PlyVisualization3D.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,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
3 changes: 2 additions & 1 deletion Core/include/Acts/Visualization/ViewConfig.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
12 changes: 6 additions & 6 deletions Core/include/Acts/Visualization/detail/ObjVisualization3D.ipp
Original file line number Diff line number Diff line change
Expand Up @@ -69,15 +69,15 @@ 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");
std::filesystem::path mtlpath = objectpath;
mtlpath.replace_extension(std::filesystem::path("mtl"));
os << "mtllib " << mtlpath << "\n";
std::ofstream mtlos;
mtlos.open(mtlpath);
Expand Down
8 changes: 4 additions & 4 deletions Core/include/Acts/Visualization/detail/PlyVisualization3D.ipp
Original file line number Diff line number Diff line change
Expand Up @@ -41,11 +41,11 @@ 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 (!path.has_extension()) {
objectpath.replace_extension(std::filesystem::path("ply"));
}
os.open(objectpath);
write(os);
Expand Down
65 changes: 35 additions & 30 deletions Core/src/Visualization/GeometryView3D.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -53,18 +53,26 @@ namespace {
/// @param relativeFilePath The relative file path to be combined with the base directory.
///
/// @return Absolute path created by combining baseDir and pathFileRelative, if pathFileRelative is not absolute.
std::filesystem::path makeAbsPath(
std::filesystem::path pathBaseDir,
const std::filesystem::path& pathFileRelative) {
if (pathFileRelative.is_absolute()) {
return pathFileRelative;
}

if (pathBaseDir.empty()) {
pathBaseDir = std::filesystem::current_path();
}

return pathBaseDir / pathFileRelative;
// std::filesystem::path makeAbsPath(
// std::filesystem::path pathBaseDir,
// const std::filesystem::path& pathFileRelative) {
// if (pathFileRelative.is_absolute()) {
// return pathFileRelative;
// }
//
// if (pathBaseDir.empty()) {
// pathBaseDir = std::filesystem::current_path();
// }
//
// return pathBaseDir / pathFileRelative;
// }

// TODO write summary
std::filesystem::path tryToInterpretAsCurrentPath(
const std::filesystem::path& testPath) {
return testPath == std::filesystem::path(".") || testPath.empty()
? std::filesystem::current_path()
: testPath;
}

} // namespace
Expand Down Expand Up @@ -109,9 +117,7 @@ void Acts::GeometryView3D::drawSurfaceArray(
const ViewConfig& sensitiveConfig, const ViewConfig& passiveConfig,
const ViewConfig& gridConfig, const std::filesystem::path& _outputDir) {
const std::filesystem::path outputDir =
_outputDir == std::filesystem::path(".")
? std::filesystem::current_path()
: std::filesystem::path(_outputDir);
tryToInterpretAsCurrentPath(_outputDir);
// Draw all the surfaces
Extent arrayExtent;
for (const auto& sf : surfaceArray.surfaces()) {
Expand All @@ -124,8 +130,7 @@ void Acts::GeometryView3D::drawSurfaceArray(
}

if (!sensitiveConfig.outputName.empty()) {
helper.write(makeAbsPath(
outputDir, std::filesystem::path(sensitiveConfig.outputName)));
helper.write(outputDir / sensitiveConfig.outputName);
helper.clear();
}

Expand Down Expand Up @@ -189,8 +194,7 @@ void Acts::GeometryView3D::drawSurfaceArray(
}

if (!gridConfig.outputName.empty()) {
helper.write(
makeAbsPath(outputDir, std::filesystem::path(gridConfig.outputName)));
helper.write(outputDir / gridConfig.outputName);
helper.clear();
}
}
Expand Down Expand Up @@ -250,9 +254,7 @@ void Acts::GeometryView3D::drawLayer(
const ViewConfig& layerConfig, const ViewConfig& sensitiveConfig,
const ViewConfig& gridConfig, const std::filesystem::path& _outputDir) {
const std::filesystem::path outputDir =
_outputDir == std::filesystem::path(".")
? std::filesystem::current_path()
: std::filesystem::path(_outputDir);
tryToInterpretAsCurrentPath(_outputDir);

if (layerConfig.visible) {
auto layerVolume = layer.representingVolume();
Expand All @@ -265,8 +267,7 @@ void Acts::GeometryView3D::drawLayer(
layerConfig);
}
if (!layerConfig.outputName.empty()) {
helper.write(makeAbsPath(outputDir,
std::filesystem::path(layerConfig.outputName)));
helper.write(outputDir / layerConfig.outputName);
helper.clear();
}
}
Expand All @@ -287,8 +288,8 @@ void Acts::GeometryView3D::drawTrackingVolume(
const ViewConfig& sensitiveView, const ViewConfig& gridView, bool writeIt,
const std::string& tag, const std::filesystem::path& _outputDir) {
const std::filesystem::path outputDir =
_outputDir == std::filesystem::path(".") ? std::filesystem::current_path()
: _outputDir;
tryToInterpretAsCurrentPath(_outputDir);

if (tVolume.confinedVolumes() != nullptr) {
const auto& subVolumes = tVolume.confinedVolumes()->arrayObjects();
for (const auto& tv : subVolumes) {
Expand Down Expand Up @@ -316,7 +317,9 @@ void Acts::GeometryView3D::drawTrackingVolume(
}
if (tVolume.confinedVolumes() == nullptr) {
vcConfig = vConfig;
vcConfig.outputName = vname + std::string("_boundaries") + tag;
// TODO FS
vcConfig.outputName =
std::filesystem::path(vname + std::string("_boundaries") + tag);
} else {
std::stringstream vs;
vs << "Container";
Expand All @@ -331,7 +334,9 @@ void Acts::GeometryView3D::drawTrackingVolume(
vs << "_v" << ids[i];
}
vname = vs.str();
vcConfig.outputName = vname + std::string("_boundaries") + tag;
// TODO FS
vcConfig.outputName =
std::filesystem::path(vname + std::string("_boundaries") + tag);
}
}

Expand All @@ -341,8 +346,7 @@ void Acts::GeometryView3D::drawTrackingVolume(
Transform3::Identity(), vcConfig);
}
if (writeIt) {
std::string outputName =
makeAbsPath(outputDir, std::filesystem::path(vcConfig.outputName));
const std::filesystem::path outputName = outputDir / vcConfig.outputName;
helper.write(outputName);
helper.clear();
}
Expand All @@ -352,6 +356,7 @@ void Acts::GeometryView3D::drawTrackingVolume(
std::size_t il = 0;
for (const auto& tl : layers) {
if (writeIt) {
// TODO FS
lConfig.outputName =
vname + std::string("_passives_l") + std::to_string(il) + tag;
sConfig.outputName =
Expand Down
31 changes: 17 additions & 14 deletions Core/src/Visualization/IVisualization3D.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,21 @@
// 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"
// TODO REMOVE THIS FILE

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;
}
}
// #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;
// }
// }

0 comments on commit 9e32a5c

Please sign in to comment.