EIC Software
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
IntersectionHelper2D.cpp
Go to the documentation of this file. Or view the newest version in sPHENIX GitHub for file IntersectionHelper2D.cpp
1 // This file is part of the Acts project.
2 //
3 // Copyright (C) 2020 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 
9 #include <cmath>
10 #include <iostream>
11 #include <tuple>
12 
15 
17  const Vector2D& s0, const Vector2D& s1, const Vector2D& origin,
18  const Vector2D& dir) {
19  using Line = Eigen::ParametrizedLine<double, 2>;
20  using Plane = Eigen::Hyperplane<double, 2>;
21 
22  Vector2D ldir(s1 - s0);
23  double det = ldir.x() * dir.y() - ldir.y() * dir.x();
24  if (std::abs(det) < s_epsilon) {
25  return Intersection2D();
26  }
27 
28  auto line = Line(origin, dir);
29  auto d = line.intersectionParameter(Plane::Through(s0, s1));
30 
31  return Intersection2D(origin + d * dir, d, Intersection2D::Status::reachable);
32 }
33 
34 std::pair<Acts::Intersection2D, Acts::Intersection2D>
36  const Vector2D& origin,
37  const Vector2D& dir) {
38  auto createSolution = [&](const Vector2D& sol, const Vector2D& alt)
39  -> std::pair<Acts::Intersection2D, Acts::Intersection2D> {
40  Vector2D toSolD(sol - origin);
41  Vector2D toAltD(alt - origin);
42  double solD = std::copysign(toSolD.norm(), toSolD.dot(dir));
43  double altD = std::copysign(toAltD.norm(), toAltD.dot(dir));
44 
45  if (solD * solD < altD * altD) {
46  return {Intersection2D(sol, solD, Intersection2D::Status::reachable),
47  Intersection2D(alt, altD, Intersection2D::Status::reachable)};
48  }
49  return {Intersection2D(alt, altD, Intersection2D::Status::reachable),
50  Intersection2D(sol, solD, Intersection2D::Status::reachable)};
51  };
52 
53  // Special cases first
54  if (std::abs(dir.x()) < s_epsilon) {
55  double solx = origin.x();
56  double D = 1. - solx * solx / (Rx * Rx);
57  if (D > 0.) {
58  double sqrtD = std::sqrt(D);
59  Vector2D sol(solx, Ry * sqrtD);
60  Vector2D alt(solx, -Ry * sqrtD);
61  return createSolution(sol, alt);
62  } else if (std::abs(D) < s_epsilon) {
63  return {Intersection2D(Vector2D(solx, 0.), -origin.y(),
64  Intersection2D::Status::reachable),
65  Intersection2D()};
66  }
67  return {Intersection2D(), Intersection2D()};
68  } else if (std::abs(dir.y()) < s_epsilon) {
69  double soly = origin.y();
70  double D = 1. - soly * soly / (Ry * Ry);
71  if (D > 0.) {
72  double sqrtD = std::sqrt(D);
73  Vector2D sol(Rx * sqrtD, soly);
74  Vector2D alt(-Rx * sqrtD, soly);
75  return createSolution(sol, alt);
76  } else if (std::abs(D) < s_epsilon) {
77  return {Intersection2D(Vector2D(0., soly), -origin.x(),
78  Intersection2D::Status::reachable),
79  Intersection2D()};
80  }
81  return {Intersection2D(), Intersection2D()};
82  }
83  // General solution
84  double k = dir.y() / dir.x();
85  double d = origin.y() - k * origin.x();
86  double Ry2 = Ry * Ry;
87  double alpha = 1. / (Rx * Rx) + k * k / Ry2;
88  double beta = 2. * k * d / Ry2;
89  double gamma = d * d / Ry2 - 1;
90  Acts::detail::RealQuadraticEquation solver(alpha, beta, gamma);
91  if (solver.solutions == 1) {
92  double x = solver.first;
93  Vector2D sol(x, k * x + d);
94  Vector2D toSolD(sol - origin);
95  double solD = std::copysign(toSolD.norm(), toSolD.dot(dir));
96  return {Intersection2D(sol, solD, Intersection2D::Status::reachable),
97  Intersection2D()};
98  } else if (solver.solutions > 1) {
99  double x0 = solver.first;
100  double x1 = solver.second;
101  Vector2D sol(x0, k * x0 + d);
102  Vector2D alt(x1, k * x1 + d);
103  return createSolution(sol, alt);
104  }
105  return {Intersection2D(), Intersection2D()};
106 }