-
Notifications
You must be signed in to change notification settings - Fork 48
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #588 from krasznaa/MoreObjWriting-main-20240513
More (Wavefront) Obj Writing, main branch (2024.05.13.)
- Loading branch information
Showing
8 changed files
with
313 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
/** TRACCC library, part of the ACTS project (R&D line) | ||
* | ||
* (c) 2024 CERN for the benefit of the ACTS project | ||
* | ||
* Mozilla Public License Version 2.0 | ||
*/ | ||
|
||
// Local include(s). | ||
#include "write_seeds.hpp" | ||
|
||
// System include(s). | ||
#include <fstream> | ||
|
||
namespace traccc::io::obj { | ||
|
||
void write_seeds(std::string_view filename, | ||
seed_collection_types::const_view seeds_view, | ||
spacepoint_collection_types::const_view spacepoints_view) { | ||
|
||
// Open the output file. | ||
std::ofstream file{filename.data()}; | ||
if (!file.is_open()) { | ||
throw std::runtime_error("Failed to open file: " + | ||
std::string(filename)); | ||
} | ||
|
||
// Create device collections around the views. | ||
const seed_collection_types::const_device seeds{seeds_view}; | ||
const spacepoint_collection_types::const_device spacepoints{ | ||
spacepoints_view}; | ||
|
||
// Map associating in-memory spacepoint indices to in-file ones. | ||
std::map<std::size_t, std::size_t> spacepoint_indices; | ||
|
||
// Helper lambda to write a spacepoint to the output file. | ||
auto write_spacepoint = [&file, &spacepoints, &spacepoint_indices]( | ||
std::size_t memory_index, | ||
std::size_t file_index) -> bool { | ||
// Check whether this spacepoint has already been written. | ||
if (spacepoint_indices.find(memory_index) != spacepoint_indices.end()) { | ||
return false; | ||
} | ||
// Write the spacepoint. | ||
const traccc::spacepoint& sp = spacepoints[memory_index]; | ||
file << "v " << sp.x() << " " << sp.y() << " " << sp.z() << "\n"; | ||
// Remember the mapping. | ||
spacepoint_indices[memory_index] = file_index; | ||
return true; | ||
}; | ||
|
||
// First, write out all of the spacepoints as vertices. Making sure that we | ||
// only write each spacepoint once. And remembering the indices of the | ||
// spacepoints in the output file, to be used when making the seeds. | ||
std::size_t file_index = 1; | ||
file << "# Spacepoints from which the seeds are built\n"; | ||
for (const seed& s : seeds) { | ||
if (write_spacepoint(s.spB_link, file_index)) { | ||
file_index++; | ||
} | ||
if (write_spacepoint(s.spM_link, file_index)) { | ||
file_index++; | ||
} | ||
if (write_spacepoint(s.spT_link, file_index)) { | ||
file_index++; | ||
} | ||
} | ||
|
||
// Helper lambda for getting an element of the spacepoint_indices map, in a | ||
// "safe" way. | ||
auto get_spacepoint_index = | ||
[&spacepoint_indices](std::size_t memory_index) -> std::size_t { | ||
auto it = spacepoint_indices.find(memory_index); | ||
if (it == spacepoint_indices.end()) { | ||
throw std::runtime_error("Spacepoint index not found"); | ||
} | ||
return it->second; | ||
}; | ||
|
||
// Now build the seeds as lines connecting the spacepoint vertices. | ||
file << "# Seeds\n"; | ||
for (const seed& s : seeds) { | ||
file << "l " << get_spacepoint_index(s.spB_link) << " " | ||
<< get_spacepoint_index(s.spM_link) << " " | ||
<< get_spacepoint_index(s.spT_link) << "\n"; | ||
} | ||
} | ||
|
||
} // namespace traccc::io::obj |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
/** TRACCC library, part of the ACTS project (R&D line) | ||
* | ||
* (c) 2024 CERN for the benefit of the ACTS project | ||
* | ||
* Mozilla Public License Version 2.0 | ||
*/ | ||
|
||
#pragma once | ||
|
||
// Project include(s). | ||
#include "traccc/edm/seed.hpp" | ||
#include "traccc/edm/spacepoint.hpp" | ||
|
||
// System include(s). | ||
#include <string_view> | ||
|
||
namespace traccc::io::obj { | ||
|
||
/// Write a seed collection into a Wavefront OBJ file. | ||
/// | ||
/// @param filename is the name of the output file | ||
/// @param seeds is the seed collection to write | ||
/// @param spacepoints is the spacepoint collection that the seeds reference | ||
/// | ||
void write_seeds(std::string_view filename, | ||
seed_collection_types::const_view seeds, | ||
spacepoint_collection_types::const_view spacepoints); | ||
|
||
} // namespace traccc::io::obj |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
/** TRACCC library, part of the ACTS project (R&D line) | ||
* | ||
* (c) 2024 CERN for the benefit of the ACTS project | ||
* | ||
* Mozilla Public License Version 2.0 | ||
*/ | ||
|
||
// Local include(s). | ||
#include "write_track_candidates.hpp" | ||
|
||
// System include(s). | ||
#include <cassert> | ||
#include <fstream> | ||
|
||
namespace traccc::io::obj { | ||
|
||
void write_track_candidates( | ||
std::string_view filename, | ||
track_candidate_container_types::const_view tracks_view, | ||
const detray::detector<>& detector) { | ||
|
||
// Open the output file. | ||
std::ofstream file{filename.data()}; | ||
if (!file.is_open()) { | ||
throw std::runtime_error("Failed to open file: " + | ||
std::string(filename)); | ||
} | ||
|
||
// Create a device collection around the track container view. | ||
const track_candidate_container_types::const_device tracks{tracks_view}; | ||
|
||
// Convenience type. | ||
using size_type = track_candidate_container_types::const_device::size_type; | ||
|
||
// First write out the measurements / spacepoints that the tracks are | ||
// made from. Don't try to resolve the overlaps, just write out duplicate | ||
// measurements if needed. | ||
file << "# Measurements / spacepoints that the tracks are made out of\n"; | ||
for (size_type i = 0; i < tracks.size(); ++i) { | ||
|
||
// The track candidate in question. | ||
const track_candidate_container_types::const_device::const_element_view | ||
track = tracks.at(i); | ||
|
||
// Loop over the measurements that the track candidate is made out of. | ||
for (const measurement& m : track.items) { | ||
|
||
// Find the detector surface that this measurement sits on. | ||
const detray::surface<detray::detector<> > surface{detector, | ||
m.surface_link}; | ||
|
||
// Calculate a position for this measurement in global 3D space. | ||
const auto global = surface.bound_to_global({}, m.local, {}); | ||
|
||
// Write the 3D coordinates of the measurement / spacepoint. | ||
assert(global.size() == 3); | ||
file << "v " << global[0] << " " << global[1] << " " << global[2] | ||
<< "\n"; | ||
} | ||
} | ||
|
||
// Now loop over the track candidates again, and creates lines for each of | ||
// them using the measurements / spacepoints written out earlier. | ||
file << "# Track candidates\n"; | ||
std::size_t vertex_counter = 1; | ||
for (size_type i = 0; i < tracks.size(); ++i) { | ||
|
||
// The track candidate in question. | ||
const track_candidate_container_types::const_device::const_element_view | ||
track = tracks.at(i); | ||
|
||
// Construct the lines. | ||
file << "l"; | ||
for (size_type j = 0; j < track.items.size(); ++j) { | ||
file << " " << vertex_counter++; | ||
} | ||
file << "\n"; | ||
} | ||
} | ||
|
||
} // namespace traccc::io::obj |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
/** TRACCC library, part of the ACTS project (R&D line) | ||
* | ||
* (c) 2024 CERN for the benefit of the ACTS project | ||
* | ||
* Mozilla Public License Version 2.0 | ||
*/ | ||
|
||
#pragma once | ||
|
||
// Project include(s). | ||
#include "traccc/edm/track_candidate.hpp" | ||
|
||
// Detray include(s). | ||
#include "detray/core/detector.hpp" | ||
|
||
// System include(s). | ||
#include <string_view> | ||
|
||
namespace traccc::io::obj { | ||
|
||
/// Write a track candidate container into a Wavefront OBJ file. | ||
/// | ||
/// @param filename is the name of the output file | ||
/// @param tracks is the track candidate container to write | ||
/// @param detector is the Detray detector describing the geometry | ||
/// | ||
void write_track_candidates(std::string_view filename, | ||
track_candidate_container_types::const_view tracks, | ||
const detray::detector<>& detector); | ||
|
||
} // namespace traccc::io::obj |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters