Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-11-25 09:02:44

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 "Acts/Geometry/GeometryObject.hpp"
0012 
0013 #include <array>
0014 #include <filesystem>
0015 #include <string_view>
0016 
0017 namespace Acts {
0018 
0019 /// Struct describing a color. Internally, the color is represented using a
0020 /// triplet of integers between 0 and 255 corresponding to red green and blue.
0021 struct Color {
0022   // Copy and move constructors are defaulted
0023   Color() = default;
0024   /// Copy constructor
0025   Color(const Color&) = default;
0026   /// Move constructor
0027   Color(Color&&) = default;
0028   /// Copy assignment operator
0029   /// @return Reference to this Color after copying
0030   Color& operator=(const Color&) = default;
0031   /// Move assignment operator
0032   /// @return Reference to this Color after moving
0033   Color& operator=(Color&&) = default;
0034 
0035   /// Constructor from raw integer rgb values [0, 255]
0036   /// @param r The red component
0037   /// @param g The green component
0038   /// @param b The blue component
0039   constexpr Color(int r, int g, int b) : rgb{r, g, b} {}
0040 
0041   /// Constructor from array of integer rgb values [0, 255]
0042   /// @param values The rgb values
0043   constexpr explicit Color(std::array<int, 3> values) : rgb(values) {}
0044 
0045   /// Constructor from array of double rgb values [0, 1]
0046   /// @param values The rgb values
0047   constexpr explicit Color(std::array<double, 3> values) {
0048     rgb[0] = static_cast<int>(values[0] * 255);
0049     rgb[1] = static_cast<int>(values[1] * 255);
0050     rgb[2] = static_cast<int>(values[2] * 255);
0051   }
0052 
0053   /// Constructor from raw double dgb values [0, 1]
0054   /// @param r The red component
0055   /// @param g The green component
0056   /// @param b The blue component
0057   constexpr Color(double r, double g, double b)
0058       : Color{std::array<double, 3>{r, g, b}} {}
0059 
0060  private:
0061   constexpr static int hexToInt(std::string_view hex) {
0062     constexpr auto hexCharToInt = [](char c) {
0063       if (c >= '0' && c <= '9') {
0064         return c - '0';
0065       } else if (c >= 'a' && c <= 'f') {
0066         return c - 'a' + 10;
0067       } else if (c >= 'A' && c <= 'F') {
0068         return c - 'A' + 10;
0069       } else {
0070         throw std::invalid_argument("Invalid hex character");
0071       }
0072     };
0073 
0074     int value = 0;
0075     for (char c : hex) {
0076       value = (value << 4) + hexCharToInt(c);
0077     }
0078     return value;
0079   };
0080 
0081  public:
0082   /// Constructor from hex string. The expected format is `#RRGGBB`
0083   /// @param hex The hex string
0084   constexpr explicit Color(std::string_view hex) {
0085     if (hex[0] == '#' && hex.size() == 7) {
0086       rgb[0] = hexToInt(hex.substr(1, 2));  // Extract R component
0087       rgb[1] = hexToInt(hex.substr(3, 2));  // Extract G component
0088       rgb[2] = hexToInt(hex.substr(5, 2));  // Extract B component
0089     } else {
0090       throw std::invalid_argument{
0091           "Invalid hex color format. Expected format: #RRGGBB"};
0092     }
0093   }
0094 
0095   /// Operator to access the color components
0096   /// @param i The index of the component
0097   /// @return The color component
0098   int operator[](unsigned int i) const { return rgb.at(i); }
0099 
0100   /// Operator to access the color components
0101   /// @param i The index of the component
0102   /// @return The color component
0103   int& operator[](unsigned int i) { return rgb.at(i); }
0104 
0105   /// Operator to compare two colors
0106   /// @param lhs The first color
0107   /// @param rhs The second color
0108   /// @return True if the colors are equal
0109   friend bool operator==(const Color& lhs, const Color& rhs) = default;
0110   /// Output stream operator
0111   /// @param os The output stream
0112   /// @param color The color to be printed
0113   /// @return The output stream
0114   friend std::ostream& operator<<(std::ostream& os, const Color& color) {
0115     os << "[" << color.rgb[0] << ", " << color.rgb[1] << ", " << color.rgb[2]
0116        << "]";
0117     return os;
0118   }
0119 
0120   /// RGB color values (0-255) for red, green, blue components
0121   std::array<int, 3> rgb{};
0122 };
0123 
0124 /// Default color for surfaces
0125 constexpr Color s_defaultSurfaceColor{"#0000aa"};
0126 /// Default color for portals
0127 constexpr Color s_defaultPortalColor{"#308c48"};
0128 /// Default color for volumes
0129 constexpr Color s_defaultVolumColor{"#ffaa00"};
0130 
0131 /// @brief Struct to concentrate all visualization configurations
0132 /// in order to harmonize visualization interfaces
0133 struct ViewConfig {
0134   /// Visible flag
0135   bool visible = true;
0136   /// The color for this object
0137   Color color = {250, 0, 0};
0138   /// Out of plane drawing parameter for objects
0139   double offset = 0.1;
0140   /// The visual line thickness for this object
0141   double lineThickness = 0.15;
0142   /// The visual surface thickness for this object
0143   double surfaceThickness = 0.15;
0144   /// The number of segments to approximate a quarter of the circle
0145   unsigned int quarterSegments = 72;
0146   /// Whether to triangulate or not
0147   bool triangulate = false;
0148   /// Write name - non-empty string indicates writing
0149   std::filesystem::path outputName = std::filesystem::path("");
0150 };
0151 
0152 /// View configuration for surface visualization
0153 static const ViewConfig s_viewSurface = {.color = {170, 170, 170}};
0154 /// View configuration for portal visualization
0155 static const ViewConfig s_viewPortal = {.color = Color{"#308c48"}};
0156 /// View configuration for sensitive surface visualization
0157 static const ViewConfig s_viewSensitive = {.color = {0, 180, 240}};
0158 /// View configuration for passive surface visualization
0159 static const ViewConfig s_viewPassive = {.color = {240, 280, 0}};
0160 /// View configuration for volume visualization
0161 static const ViewConfig s_viewVolume = {.color = {220, 220, 0}};
0162 /// View configuration for grid visualization
0163 static const ViewConfig s_viewGrid = {.color = {220, 0, 0}};
0164 /// View configuration for line visualization
0165 static const ViewConfig s_viewLine = {.color = {0, 0, 220}};
0166 
0167 /// Function that takes a GeometryObject as an input and outputs a ViewConfig
0168 /// This allows to use custom functions for determining the colour of
0169 /// GeometryObjects
0170 using ViewConfigFunc = std::function<ViewConfig(const GeometryObject&)>;
0171 
0172 /// Default ViewConfigFunc
0173 /// @return ViewConifg which colors sensitive surfaces green and boundaries orange
0174 ViewConfig defaultGeometryColoring(const GeometryObject& /*geoObj*/);
0175 
0176 }  // namespace Acts