Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-01-30 09:15:17

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 #include "Acts/Plugins/Json/AlgebraJsonConverter.hpp"
0010 
0011 #include <array>
0012 #include <stdexcept>
0013 
0014 void Acts::to_json(nlohmann::json& j, const Acts::Transform3& t) {
0015   auto translation = t.translation();
0016   if (translation != Acts::Vector3(0., 0., 0)) {
0017     std::array<double, 3> tdata = {translation.x(), translation.y(),
0018                                    translation.z()};
0019     j["translation"] = tdata;
0020   } else {
0021     j["translation"] = nlohmann::json();
0022   }
0023 
0024   auto rotation = t.rotation();
0025   if (rotation != Acts::RotationMatrix3::Identity()) {
0026     std::array<double, 9> rdata = {
0027         rotation(0, 0), rotation(0, 1), rotation(0, 2),
0028         rotation(1, 0), rotation(1, 1), rotation(1, 2),
0029         rotation(2, 0), rotation(2, 1), rotation(2, 2)};
0030     j["rotation"] = rdata;
0031   } else {
0032     j["rotation"] = nlohmann::json();
0033   }
0034 }
0035 
0036 void Acts::from_json(const nlohmann::json& j, Acts::Transform3& t) {
0037   t = Acts::Transform3::Identity();
0038   if (j.find("rotation") != j.end() && !j["rotation"].empty()) {
0039     std::array<double, 9> rdata = j["rotation"];
0040     Acts::RotationMatrix3 rot;
0041     rot << rdata[0], rdata[1], rdata[2], rdata[3], rdata[4], rdata[5], rdata[6],
0042         rdata[7], rdata[8];
0043     t.prerotate(rot);
0044   }
0045   if (j.find("translation") != j.end() && !j["translation"].empty()) {
0046     std::array<double, 3> tdata = j["translation"];
0047     t.pretranslate(Acts::Vector3(tdata[0], tdata[1], tdata[2]));
0048   }
0049 }
0050 
0051 nlohmann::json Acts::Transform3JsonConverter::toJson(const Transform3& t,
0052                                                      const Options& options) {
0053   nlohmann::json jTransform;
0054   // Write out the translation
0055   auto translation = t.translation();
0056   if (translation != Acts::Vector3(0., 0., 0) || options.writeIdentity) {
0057     std::array<double, 3> tdata = {translation.x(), translation.y(),
0058                                    translation.z()};
0059     jTransform["translation"] = tdata;
0060   } else {
0061     jTransform["translation"] = nlohmann::json();
0062   }
0063   // Write out the rotation, could be transposed
0064   auto rotation = options.transpose ? t.rotation().transpose() : t.rotation();
0065   if (rotation != Acts::RotationMatrix3::Identity() || options.writeIdentity) {
0066     std::array<double, 9> rdata = {
0067         rotation(0, 0), rotation(0, 1), rotation(0, 2),
0068         rotation(1, 0), rotation(1, 1), rotation(1, 2),
0069         rotation(2, 0), rotation(2, 1), rotation(2, 2)};
0070     jTransform["rotation"] = rdata;
0071   } else {
0072     jTransform["rotation"] = nlohmann::json();
0073   }
0074   // Return the converted transform
0075   return jTransform;
0076 }
0077 
0078 Acts::Transform3 Acts::Transform3JsonConverter::fromJson(
0079     const nlohmann::json& jTransform) {
0080   Transform3 t = Transform3::Identity();
0081   Acts::from_json(jTransform, t);
0082   return t;
0083 }