Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-09-17 08:59:58

0001 //------------------------------- -*- C++ -*- -------------------------------//
0002 // Copyright Celeritas contributors: see top-level COPYRIGHT file for details
0003 // SPDX-License-Identifier: (Apache-2.0 OR MIT)
0004 //---------------------------------------------------------------------------//
0005 //! \file geocel/rasterize/SafetyImager.t.hh
0006 //---------------------------------------------------------------------------//
0007 #pragma once
0008 
0009 #include "SafetyImager.hh"
0010 
0011 #include <fstream>
0012 #include <nlohmann/json.hpp>
0013 
0014 #include "ImageIO.json.hh"
0015 
0016 #include "detail/SafetyCalculator.hh"
0017 
0018 namespace celeritas
0019 {
0020 //---------------------------------------------------------------------------//
0021 /*!
0022  * Construct with geometry and build a single state.
0023  */
0024 template<class G>
0025 SafetyImager<G>::SafetyImager(SPConstGeo geo) : geo_{std::move(geo)}
0026 {
0027     CELER_EXPECT(geo_);
0028 
0029     host_state_ = {geo_->host_ref(), 1};
0030 }
0031 
0032 //---------------------------------------------------------------------------//
0033 /*!
0034  * Write an image to a file.
0035  */
0036 template<class G>
0037 void SafetyImager<G>::operator()(ImageParams const& image, std::string filename)
0038 {
0039     std::ofstream out{filename, std::ios::out | std::ios::trunc};
0040     CELER_VALIDATE(out, << "failed to open '" << filename << "'");
0041     out << nlohmann::json(image).dump() << std::endl;
0042 
0043     auto const& scalars = image.scalars();
0044     real_type max_distance = celeritas::max(scalars.dims[0], scalars.dims[1])
0045                              * scalars.pixel_width;
0046 
0047     detail::SafetyCalculator calc_safety{
0048         GeoTrackView{geo_->host_ref(), host_state_.ref(), TrackSlotId{0}},
0049         image.host_ref(),
0050         max_distance};
0051 
0052     std::vector<double> line;
0053     for (auto i : range(scalars.dims[0]))
0054     {
0055         line.clear();
0056         for (auto j : range(scalars.dims[1]))
0057         {
0058             line.push_back(calc_safety(j, i));  // Note: col is 'x' position
0059         }
0060         out << nlohmann::json(line).dump() << std::endl;
0061     }
0062 }
0063 
0064 //---------------------------------------------------------------------------//
0065 }  // namespace celeritas