Back to home page

EIC code displayed by LXR

 
 

    


Warning, file /acts/Plugins/Json/src/AlgebraJsonConverter.cpp was not indexed or was modified since last indexation (in which case cross-reference links may be missing, inaccurate or erroneous).

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