Back to home page

EIC code displayed by LXR

 
 

    


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

0001 // This file is part of the actsvg packge.
0002 //
0003 // Copyright (C) 2022 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 http://mozilla.org/MPL/2.0/.
0008 
0009 #pragma once
0010 
0011 #include <cmath>
0012 #include <fstream>
0013 #include <map>
0014 #include <vector>
0015 
0016 #include "draw.hpp"
0017 #include "svg.hpp"
0018 #include "utils.hpp"
0019 
0020 namespace actsvg {
0021 
0022 namespace connectors {
0023 
0024 enum type { e_highlight, e_associate_id, e_associate_info };
0025 
0026 /** Helper method to connect objects
0027  *
0028  * @param sources_ are the source objects
0029  * @param targests_ are the target objects
0030  * @param s_t_connections_ are the connections from source to target
0031  * @param ls_ label the source or not (in case of multiple connection sheets)
0032  * @param on_off_ are the connection effects
0033  *
0034  * In case of e.g. a grid surface connection:
0035  * - the @param sources_ are the grid tiles
0036  * - the @param targets_ are the surfaces
0037  * - the @param s_t_connections are the indices which type of association
0038  * - the @param font_ is used for associate info action
0039  *
0040  **/
0041 static inline std::vector<svg::object> connect_action(
0042     std::vector<svg::object> &sources_, std::vector<svg::object> &targets_,
0043     const std::vector<std::vector<size_t> > &s_t_connections_, bool ls_ = true,
0044     const std::array<std::string, 2u> &on_off_ = {"mouseover", "mouseout"},
0045     const std::vector<connectors::type> &ct_ = {e_highlight, e_associate_info},
0046     const style::font &font_ = style::font()) {
0047 
0048     std::vector<svg::object> additional_connections;
0049 
0050     for (auto [s, ts] : utils::enumerate(s_t_connections_)) {
0051         if (s < sources_.size()) {
0052             // The source object group
0053             auto &sog = sources_[s];
0054             // A connections string
0055             std::vector<std::string> id_assoc_text;
0056             std::vector<std::string> info_assoc_text;
0057             // Continue if you do not have a source identification
0058             if (sog._id.empty()) {
0059                 continue;
0060             }
0061             // Associate the id tags, i.e. labl the source
0062             if (ls_) {
0063                 id_assoc_text.push_back("Source: ");
0064                 id_assoc_text.push_back("* " + sog._id);
0065                 // Associate the auxiliary information
0066                 info_assoc_text.push_back("Source: ");
0067                 for (const auto &sai : sog._aux_info) {
0068                     info_assoc_text.push_back(sai);
0069                 }
0070             }
0071             // Enumerate over the connections
0072             for (auto [it, t] : utils::enumerate(ts)) {
0073                 if (t < targets_.size()) {
0074                     // Get the target (object group)
0075                     auto &tog = targets_[t];
0076                     // remember the associations
0077                     if (it == 0) {
0078                         id_assoc_text.push_back("Target:");
0079                         info_assoc_text.push_back("Target:");
0080                     }
0081                     id_assoc_text.push_back("* " + tog._id);
0082                     for (const auto &tai : tog._aux_info) {
0083                         info_assoc_text.push_back(tai);
0084                     }
0085                     // Highlight connection
0086                     if (std::find(ct_.begin(), ct_.end(), e_highlight) !=
0087                         ct_.end()) {
0088                         svg::object on_off;
0089                         on_off._tag = "set";
0090                         on_off._attribute_map["attributeName"] = "fill";
0091                         on_off._attribute_map["begin"] =
0092                             sog._id + __d + on_off_[0];
0093                         on_off._attribute_map["end"] =
0094                             sog._id + __d + on_off_[1];
0095                         // Stroke and fill sterile
0096                         on_off._stroke._sterile = true;
0097                         on_off._fill._sterile = true;
0098                         // If the object has a use object, the connection goes
0099                         // to the use object and not the the top object
0100                         bool connection_done = false;
0101                         for (auto &sob_tog : tog._sub_objects) {
0102                             if (sob_tog._tag == "use") {
0103                                 on_off._attribute_map["to"] =
0104                                     style::rgb_attr(sob_tog._fill._fc._hl_rgb);
0105                                 sob_tog._sub_objects.push_back(on_off);
0106                                 connection_done = true;
0107                                 break;
0108                             }
0109                         }
0110                         // On off tests
0111                         if (not connection_done) {
0112                             on_off._attribute_map["to"] =
0113                                 style::rgb_attr(tog._fill._fc._hl_rgb);
0114                             tog._sub_objects.push_back(on_off);
0115                         }
0116                     }
0117                 }
0118             }
0119             // Assocate as text
0120             if (std::find(ct_.begin(), ct_.end(), e_associate_id) !=
0121                 ct_.end()) {
0122                 // Associate id connection
0123                 auto c_text = draw::connected_text(
0124                     sog._id + "_id_associations",
0125                     {sog._barycenter[0], sog._barycenter[1]}, id_assoc_text,
0126                     font_, style::transform(), sog);
0127                 additional_connections.push_back(c_text);
0128             } else if (std::find(ct_.begin(), ct_.end(), e_associate_info) !=
0129                        ct_.end()) {
0130                 // Associate info connection
0131                 auto c_text = draw::connected_text(
0132                     sog._id + "_info_associations",
0133                     {sog._barycenter[0], sog._barycenter[1]}, info_assoc_text,
0134                     font_, style::transform(), sog);
0135                 additional_connections.push_back(c_text);
0136             }
0137         }
0138     }
0139     return additional_connections;
0140 }
0141 }  // namespace connectors
0142 }  // namespace actsvg