Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-10-31 08:16:30

0001 // This file is part of the ACTS project.
0002 //
0003 // Copyright (C) 2016 CERN for the benefit of the ACTS project
0004 //
0005 // This Source Code Form is subject to the terms of the Mozilla Public
0006 // License, v. 2.0. If a copy of the MPL was not distributed with this
0007 // file, You can obtain one at https://mozilla.org/MPL/2.0/.
0008 
0009 #pragma once
0010 
0011 #include <array>
0012 #include <filesystem>
0013 #include <string_view>
0014 
0015 namespace Acts {
0016 
0017 /// Struct describing a color. Internally, the color is represented using a
0018 /// triplet of integers between 0 and 255 corresponding to red green and blue.
0019 struct Color {
0020   // Copy and move constructors are defaulted
0021   Color() = default;
0022   /// Copy constructor
0023   Color(const Color&) = default;
0024   /// Move constructor
0025   Color(Color&&) = default;
0026   /// Copy assignment operator
0027   /// @return Reference to this Color after copying
0028   Color& operator=(const Color&) = default;
0029   /// Move assignment operator
0030   /// @return Reference to this Color after moving
0031   Color& operator=(Color&&) = default;
0032 
0033   /// Constructor from raw integer rgb values [0, 255]
0034   /// @param r The red component
0035   /// @param g The green component
0036   /// @param b The blue component
0037   constexpr Color(int r, int g, int b) : rgb{r, g, b} {}
0038 
0039   /// Constructor from array of integer rgb values [0, 255]
0040   /// @param values The rgb values
0041   constexpr explicit Color(std::array<int, 3> values) : rgb(values) {}
0042 
0043   /// Constructor from array of double rgb values [0, 1]
0044   /// @param values The rgb values
0045   constexpr explicit Color(std::array<double, 3> values) {
0046     rgb[0] = static_cast<int>(values[0] * 255);
0047     rgb[1] = static_cast<int>(values[1] * 255);
0048     rgb[2] = static_cast<int>(values[2] * 255);
0049   }
0050 
0051   /// Constructor from raw double dgb values [0, 1]
0052   /// @param r The red component
0053   /// @param g The green component
0054   /// @param b The blue component
0055   constexpr Color(double r, double g, double b)
0056       : Color{std::array<double, 3>{r, g, b}} {}
0057 
0058  private:
0059   constexpr static int hexToInt(std::string_view hex) {
0060     constexpr auto hexCharToInt = [](char c) {
0061       if (c >= '0' && c <= '9') {
0062         return c - '0';
0063       } else if (c >= 'a' && c <= 'f') {
0064         return c - 'a' + 10;
0065       } else if (c >= 'A' && c <= 'F') {
0066         return c - 'A' + 10;
0067       } else {
0068         throw std::invalid_argument("Invalid hex character");
0069       }
0070     };
0071 
0072     int value = 0;
0073     for (char c : hex) {
0074       value = (value << 4) + hexCharToInt(c);
0075     }
0076     return value;
0077   };
0078 
0079  public:
0080   /// Constructor from hex string. The expected format is `#RRGGBB`
0081   /// @param hex The hex string
0082   constexpr explicit Color(std::string_view hex) {
0083     if (hex[0] == '#' && hex.size() == 7) {
0084       rgb[0] = hexToInt(hex.substr(1, 2));  // Extract R component
0085       rgb[1] = hexToInt(hex.substr(3, 2));  // Extract G component
0086       rgb[2] = hexToInt(hex.substr(5, 2));  // Extract B component
0087     } else {
0088       throw std::invalid_argument{
0089           "Invalid hex color format. Expected format: #RRGGBB"};
0090     }
0091   }
0092 
0093   /// Operator to access the color components
0094   /// @param i The index of the component
0095   /// @return The color component
0096   int operator[](unsigned int i) const { return rgb.at(i); }
0097 
0098   /// Operator to access the color components
0099   /// @param i The index of the component
0100   /// @return The color component
0101   int& operator[](unsigned int i) { return rgb.at(i); }
0102 
0103   /// Operator to compare two colors
0104   /// @param lhs The first color
0105   /// @param rhs The second color
0106   /// @return True if the colors are equal
0107   friend bool operator==(const Color& lhs, const Color& rhs) = default;
0108   /// Output stream operator
0109   /// @param os The output stream
0110   /// @param color The color to be printed
0111   /// @return The output stream
0112   friend std::ostream& operator<<(std::ostream& os, const Color& color) {
0113     os << "[" << color.rgb[0] << ", " << color.rgb[1] << ", " << color.rgb[2]
0114        << "]";
0115     return os;
0116   }
0117 
0118   /// RGB color values (0-255) for red, green, blue components
0119   std::array<int, 3> rgb{};
0120 };
0121 
0122 /// Default color for surfaces
0123 constexpr Color s_defaultSurfaceColor{"#0000aa"};
0124 /// Default color for portals
0125 constexpr Color s_defaultPortalColor{"#308c48"};
0126 /// Default color for volumes
0127 constexpr Color s_defaultVolumColor{"#ffaa00"};
0128 
0129 /// @brief Struct to concentrate all visualization configurations
0130 /// in order to harmonize visualization interfaces
0131 struct ViewConfig {
0132   /// Visible flag
0133   bool visible = true;
0134   /// The color for this object
0135   Color color = {250, 0, 0};
0136   /// Out of plane drawing parameter for objects
0137   double offset = 0.1;
0138   /// The visual line thickness for this object
0139   double lineThickness = 0.15;
0140   /// The visual surface thickness for this object
0141   double surfaceThickness = 0.15;
0142   /// The number of segments to approximate a quarter of the circle
0143   unsigned int quarterSegments = 72;
0144   /// Whether to triangulate or not
0145   bool triangulate = false;
0146   /// Write name - non-empty string indicates writing
0147   std::filesystem::path outputName = std::filesystem::path("");
0148 };
0149 
0150 static const ViewConfig s_viewSurface = {.color = {170, 170, 170}};
0151 static const ViewConfig s_viewPortal = {.color = Color{"#308c48"}};
0152 static const ViewConfig s_viewSensitive = {.color = {0, 180, 240}};
0153 static const ViewConfig s_viewPassive = {.color = {240, 280, 0}};
0154 static const ViewConfig s_viewVolume = {.color = {220, 220, 0}};
0155 static const ViewConfig s_viewGrid = {.color = {220, 0, 0}};
0156 static const ViewConfig s_viewLine = {.color = {0, 0, 220}};
0157 
0158 }  // namespace Acts