Skip to content

Commit

Permalink
feat: adding possibility to read flat surface container (#3668)
Browse files Browse the repository at this point in the history
This PR adds a function to the `JsonSurfacesReader` that allows to read in a flat container rather than a Hierarchy map:

```python
>>> import acts
>>> from acts import examples
>>> sOptions = acts.examples.SurfaceJsonOptions()
>>> sOptions.inputFile = "some_sensitive_mod.json"
>>> sOptions.jsonEntryPath = ["surfaces"]
>>> surfaces = acts.examples.rreadSurfaceVectorFromJson(sOptions)
>>> print(len(surfaces))
16668
>>> geoContext = acts.GeometryContext()
>>> viewConfig = acts.ViewConfig()
>>> acts.examples.writeSurfacesObj(surfaces, geoContext, viewConfig, "some-surfaces.obj")
```

will work with this out of the box.
  • Loading branch information
asalzburger authored Sep 30, 2024
1 parent f0dfbcd commit 32f67b3
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,14 @@ struct Options {
/// @param options specifies which file and what to read
///
/// @return a vector of surfaces
Acts::GeometryHierarchyMap<std::shared_ptr<Acts::Surface>> read(
Acts::GeometryHierarchyMap<std::shared_ptr<Acts::Surface>> readHierarchyMap(
const Options& options);

/// @brief Read the flat surfaces from the input file
///
/// @param inputFile is the input file to read from
///
/// @return a vector of surfaces
std::vector<std::shared_ptr<Acts::Surface>> readVector(const Options& options);

} // namespace ActsExamples::JsonSurfacesReader
25 changes: 24 additions & 1 deletion Examples/Io/Json/src/JsonSurfacesReader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@
namespace ActsExamples {

Acts::GeometryHierarchyMap<std::shared_ptr<Acts::Surface>>
JsonSurfacesReader::read(const JsonSurfacesReader::Options& options) {
JsonSurfacesReader::readHierarchyMap(
const JsonSurfacesReader::Options& options) {
// Read the json file into a json object
nlohmann::json j;
std::ifstream in(options.inputFile);
Expand Down Expand Up @@ -50,4 +51,26 @@ JsonSurfacesReader::read(const JsonSurfacesReader::Options& options) {
return SurfaceHierachyMap(std::move(surfaceElements));
}

std::vector<std::shared_ptr<Acts::Surface>> JsonSurfacesReader::readVector(
const Options& options) {
// Read the json file into a json object
nlohmann::json j;
std::ifstream in(options.inputFile);
in >> j;
in.close();

// Walk down the path to the surface entries
nlohmann::json jSurfaces = j;
for (const auto& jep : options.jsonEntryPath) {
jSurfaces = jSurfaces[jep];
}

std::vector<std::shared_ptr<Acts::Surface>> surfaces;
for (const auto& jSurface : jSurfaces) {
auto surface = Acts::SurfaceJsonConverter::fromJson(jSurface);
surfaces.push_back(surface);
}
return surfaces;
}

} // namespace ActsExamples
6 changes: 5 additions & 1 deletion Examples/Python/src/Json.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,11 @@ void addJson(Context& ctx) {
ACTS_PYTHON_MEMBER(jsonEntryPath);
ACTS_PYTHON_STRUCT_END();

mex.def("readSurfaceFromJson", ActsExamples::JsonSurfacesReader::read);
mex.def("readSurfaceHierarchyMapFromJson",
ActsExamples::JsonSurfacesReader::readHierarchyMap);

mex.def("readSurfaceVectorFromJson",
ActsExamples::JsonSurfacesReader::readVector);
}

{
Expand Down

0 comments on commit 32f67b3

Please sign in to comment.