Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-01-30 10:09:32

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