Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-11-16 10:16:26

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/Image.hh
0006 //---------------------------------------------------------------------------//
0007 #pragma once
0008 
0009 #include "corecel/Types.hh"
0010 #include "corecel/data/ParamsDataInterface.hh"
0011 #include "corecel/data/ParamsDataStore.hh"
0012 
0013 #include "ImageData.hh"
0014 #include "ImageInterface.hh"
0015 
0016 namespace celeritas
0017 {
0018 //---------------------------------------------------------------------------//
0019 /*!
0020  * Image construction arguments.
0021  *
0022  * Image scale in this struct is *native* units, but JSON I/O defaults to
0023  * centimeters for the window coordinates and accepts an optional "_units"
0024  * parameter that can take values of cgs, si, or clhep to interpret the input
0025  * as centimeters, meters, or millimeters.
0026  */
0027 struct ImageInput
0028 {
0029     //!@{
0030     //! Coordinates of the window [length]
0031     Real3 lower_left{0, 0, 0};
0032     Real3 upper_right{};
0033     //!@}
0034 
0035     //! Rightward basis vector, the new "x" axis
0036     Real3 rightward{1, 0, 0};
0037 
0038     //! Number of vertical pixels, aka threads when raytracing
0039     size_type vertical_pixels{};
0040 
0041     //! Round the number of horizontal pixels to this value
0042     size_type horizontal_divisor{CELER_USE_DEVICE ? 128 / sizeof(int) : 1};
0043 
0044     //! True if the input is unassigned
0045     explicit operator bool() const
0046     {
0047         return vertical_pixels != 0 && lower_left != upper_right;
0048     }
0049 };
0050 
0051 //---------------------------------------------------------------------------//
0052 /*!
0053  * Manage properties of an image.
0054  *
0055  * An image is a "window", a 2D rectangle slice of 3D space. As with computer
0056  * GUI windows, matplotlib \c imshow, and other visual rendering layouts, the
0057  * pixel order is like text on a page: left to right, then top to bottom.
0058  * Because this is vertically flipped from "mathematical" ordering, we store
0059  * the upper left coordinate and a \em -y basis vector rather than a lower left
0060  * coordinate and a \em +y basis vector.
0061  *
0062  * The same image params can be used to construct multiple images (using
0063  * different ray tracing methods or different geometries or on host vs device).
0064  */
0065 class ImageParams final : public ParamsDataInterface<ImageParamsData>
0066 {
0067   public:
0068     // Construct with image properties
0069     explicit ImageParams(ImageInput const&);
0070 
0071     //! Access scalar image properties
0072     ImageParamsScalars const& scalars() const
0073     {
0074         return this->host_ref().scalars;
0075     }
0076 
0077     //! Number of pixels in an image created from these params
0078     size_type num_pixels() const
0079     {
0080         auto const& dims = this->scalars().dims;
0081         return dims[0] * dims[1];
0082     }
0083 
0084     //! Number of horizontal lines to be used for raytracing
0085     size_type num_lines() const { return this->scalars().dims[0]; }
0086 
0087     //! Access properties on the host
0088     HostRef const& host_ref() const final { return data_.host_ref(); }
0089 
0090     //! Access properties on the device
0091     DeviceRef const& device_ref() const final { return data_.device_ref(); }
0092 
0093   private:
0094     ParamsDataStore<ImageParamsData> data_;
0095 };
0096 
0097 //---------------------------------------------------------------------------//
0098 /*!
0099  * Implement an image on host or device.
0100  */
0101 template<MemSpace M>
0102 class Image final : public ImageInterface
0103 {
0104   public:
0105     //!@{
0106     //! \name Type aliases
0107     using Value = ImageStateData<Ownership::value, M>;
0108     using Ref = ImageStateData<Ownership::reference, M>;
0109     //!@}
0110 
0111   public:
0112     // Construct from parameters
0113     explicit Image(SPConstParams params);
0114 
0115     //! Access image properties
0116     SPConstParams const& params() const final { return params_; }
0117 
0118     // Write the image to a stream in binary format
0119     void copy_to_host(SpanInt) const final;
0120 
0121     //! Access the mutable state data
0122     Ref const& ref() { return ref_; }
0123 
0124   private:
0125     SPConstParams params_;
0126     Value value_;
0127     Ref ref_;
0128 };
0129 
0130 //---------------------------------------------------------------------------//
0131 }  // namespace celeritas