Analysis Software
Documentation for sPHENIX simulation software
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
AlgebraJsonConverter.cpp
Go to the documentation of this file. Or view the newest version in sPHENIX GitHub for file AlgebraJsonConverter.cpp
1 // This file is part of the Acts project.
2 //
3 // Copyright (C) 2021 CERN for the benefit of the Acts project
4 //
5 // This Source Code Form is subject to the terms of the Mozilla Public
6 // License, v. 2.0. If a copy of the MPL was not distributed with this
7 // file, You can obtain one at http://mozilla.org/MPL/2.0/.
8 
10 
11 #include <array>
12 #include <stdexcept>
13 
14 void Acts::to_json(nlohmann::json& j, const Acts::Transform3& t) {
15  auto translation = t.translation();
16  if (translation != Acts::Vector3(0., 0., 0)) {
17  std::array<Acts::ActsScalar, 3> tdata = {translation.x(), translation.y(),
18  translation.z()};
19  j["translation"] = tdata;
20  } else {
21  j["translation"] = nlohmann::json();
22  }
23 
24  auto rotation = t.rotation();
25  if (rotation != Acts::RotationMatrix3::Identity()) {
26  std::array<Acts::ActsScalar, 9> rdata = {
27  rotation(0, 0), rotation(0, 1), rotation(0, 2),
28  rotation(1, 0), rotation(1, 1), rotation(1, 2),
29  rotation(2, 0), rotation(2, 1), rotation(2, 2)};
30  j["rotation"] = rdata;
31  } else {
32  j["rotation"] = nlohmann::json();
33  }
34 }
35 
36 void Acts::from_json(const nlohmann::json& j, Acts::Transform3& t) {
37  t = Acts::Transform3::Identity();
38  if (j.find("rotation") != j.end() and not j["rotation"].empty()) {
39  std::array<Acts::ActsScalar, 9> rdata = j["rotation"];
41  rot << rdata[0], rdata[1], rdata[2], rdata[3], rdata[4], rdata[5], rdata[6],
42  rdata[7], rdata[8];
43  t.prerotate(rot);
44  }
45  if (j.find("translation") != j.end() and not j["translation"].empty()) {
46  std::array<Acts::ActsScalar, 3> tdata = j["translation"];
47  t.pretranslate(Acts::Vector3(tdata[0], tdata[1], tdata[2]));
48  }
49 }
50 
52  const Options& options) {
53  nlohmann::json jTransform;
54  // Write out the translation
55  auto translation = t.translation();
56  if (translation != Acts::Vector3(0., 0., 0) or options.writeIdentity) {
57  std::array<Acts::ActsScalar, 3> tdata = {translation.x(), translation.y(),
58  translation.z()};
59  jTransform["translation"] = tdata;
60  } else {
61  jTransform["translation"] = nlohmann::json();
62  }
63  // Write out the rotation, could be transposed
64  auto rotation = options.transpose ? t.rotation().transpose() : t.rotation();
65  if (rotation != Acts::RotationMatrix3::Identity() or options.writeIdentity) {
66  std::array<Acts::ActsScalar, 9> rdata = {
67  rotation(0, 0), rotation(0, 1), rotation(0, 2),
68  rotation(1, 0), rotation(1, 1), rotation(1, 2),
69  rotation(2, 0), rotation(2, 1), rotation(2, 2)};
70  jTransform["rotation"] = rdata;
71  } else {
72  jTransform["rotation"] = nlohmann::json();
73  }
74  // Return the converted transform
75  return jTransform;
76 }
77 
79  const nlohmann::json& jTransform) {
80  Transform3 t = Transform3::Identity();
81  Acts::from_json(jTransform, t);
82  return t;
83 }