ATLAS Offline Software
TriangleBounds.cxx
Go to the documentation of this file.
1 /*
2  Copyright (C) 2002-2021 CERN for the benefit of the ATLAS collaboration
3 */
4 
6 // TriangleBounds.cxx, (c) ATLAS Detector Software
8 
9 // Trk
11 // Gaudi
12 #include "GaudiKernel/MsgStream.h"
13 // STD
14 #include <iomanip>
15 #include <iostream>
16 
17 // default constructor
19  : m_boundValues(TriangleBounds::bv_length, 0.)
20 {}
21 
22 // rectangle constructor - float constructor
23 Trk::TriangleBounds::TriangleBounds(const std::vector<std::pair<float, float>>& vertices)
24  : m_boundValues(TriangleBounds::bv_length, 0.)
25 {
26  size_t ib = 0;
27  for (const std::pair<float, float>& p : vertices) {
28  m_boundValues[2 * ib] = p.first;
29  m_boundValues[2 * ib + 1] = p.second;
30  if (ib == 2)
31  break;
32  ++ib;
33  }
34 }
35 
36 // rectangle constructor - double constructor
37 Trk::TriangleBounds::TriangleBounds(const std::vector<std::pair<double, double>>& vertices)
38  : m_boundValues(TriangleBounds::bv_length, 0.)
39 {
40  size_t ib = 0;
41  for (const std::pair<double, double>& p : vertices) {
42  m_boundValues[2 * ib] = p.first;
43  m_boundValues[2 * ib + 1] = p.second;
44  if (ib == 2)
45  break;
46  ++ib;
47  }
48 }
49 
50 // constructor from three points
52  : m_boundValues(TriangleBounds::bv_length, 0.)
53 {
60 }
61 
62 bool
64 {
65  // check the type first not to compare apples with oranges
66  const Trk::TriangleBounds* tribo = dynamic_cast<const Trk::TriangleBounds*>(&sbo);
67  if (!tribo)
68  return false;
69  return (m_boundValues == tribo->m_boundValues);
70 }
71 
72 bool
74  double tol1,
75  double tol2) const
76 {
77  std::pair<double, double> locB(m_boundValues[TriangleBounds::bv_x2] -
78  m_boundValues[TriangleBounds::bv_x1],
79  m_boundValues[TriangleBounds::bv_y2] -
80  m_boundValues[TriangleBounds::bv_y1]);
81  std::pair<double, double> locT(
82  m_boundValues[TriangleBounds::bv_x3] - locpo[0],
83  m_boundValues[TriangleBounds::bv_y3] - locpo[1]);
84  std::pair<double, double> locV(
85  m_boundValues[TriangleBounds::bv_x1] - locpo[0],
86  m_boundValues[TriangleBounds::bv_y1] - locpo[1]);
87 
88  // special case :: third vertex ?
89  if (locT.first * locT.first + locT.second * locT.second < tol1 * tol1)
90  return true;
91 
92  // special case : lies on base ?
93  double db = locB.first * locV.second - locB.second * locV.first;
94  if (std::abs(db) < tol1) {
95  double a =
96  (locB.first != 0) ? -locV.first / locB.first : -locV.second / locB.second;
97  return a > -tol2 && a - 1. < tol2;
98  }
99 
100  double dn = locB.first * locT.second - locB.second * locT.first;
101 
102  if (std::abs(dn) > std::abs(tol1)) {
103  double t = (locB.first * locV.second - locB.second * locV.first) / dn;
104  if (t > 0.)
105  return false;
106 
107  double a = (locB.first != 0.)
108  ? (t * locT.first - locV.first) / locB.first
109  : (t * locT.second - locV.second) / locB.second;
110  if (a < -tol2 || a - 1. > tol2)
111  return false;
112  } else {
113  return false;
114  }
115  return true;
116 }
117 
118 bool
120  const BoundaryCheck& bchk) const
121 {
122  if (bchk.bcType == 0)
123  return TriangleBounds::inside(
124  locpo, bchk.toleranceLoc1, bchk.toleranceLoc2);
125 
126  // a fast FALSE
127  double fabsR = std::sqrt(locpo[Trk::locX] * locpo[Trk::locX] +
128  locpo[Trk::locY] * locpo[Trk::locY]);
129  double max_ell = bchk.lCovariance(0, 0) > bchk.lCovariance(1, 1)
130  ? bchk.lCovariance(0, 0)
131  : bchk.lCovariance(1, 1);
132  double limit = bchk.nSigmas * std::sqrt(max_ell);
133  double r_max = TriangleBounds::r();
134  if (fabsR > (r_max + limit))
135  return false;
136 
137  // compute KDOP and axes for surface polygon
138  std::vector<KDOP> elementKDOP(3);
139  std::vector<Amg::Vector2D> elementP(3);
140  float theta =
141  (bchk.lCovariance(1, 0) != 0 &&
142  (bchk.lCovariance(1, 1) - bchk.lCovariance(0, 0)) != 0)
143  ? .5 * bchk.FastArcTan(2 * bchk.lCovariance(1, 0) /
144  (bchk.lCovariance(1, 1) - bchk.lCovariance(0, 0)))
145  : 0.;
146  sincosCache scResult = bchk.FastSinCos(theta);
147  AmgMatrix(2, 2) rotMatrix;
148  rotMatrix << scResult.cosC, scResult.sinC, -scResult.sinC, scResult.cosC;
149  AmgMatrix(2, 2) normal;
150  // cppcheck-suppress constStatement
151  normal << 0, -1, 1, 0;
152  // ellipse is always at (0,0), surface is moved to ellipse position and then
153  // rotated
155  p << m_boundValues[TriangleBounds::bv_x1],
156  m_boundValues[TriangleBounds::bv_y1];
157  elementP[0] = (rotMatrix * (p - locpo));
158  p << m_boundValues[TriangleBounds::bv_x2],
159  m_boundValues[TriangleBounds::bv_y2];
160  elementP[1] = (rotMatrix * (p - locpo));
161  p << m_boundValues[TriangleBounds::bv_x3],
162  m_boundValues[TriangleBounds::bv_y3];
163  elementP[2] = (rotMatrix * (p - locpo));
164  std::vector<Amg::Vector2D> axis = { normal * (elementP[1] - elementP[0]),
165  normal * (elementP[2] - elementP[1]),
166  normal * (elementP[2] - elementP[0]) };
167  bchk.ComputeKDOP(elementP, axis, elementKDOP);
168  // compute KDOP for error ellipse
169  std::vector<KDOP> errelipseKDOP(3);
170  bchk.ComputeKDOP(bchk.EllipseToPoly(3), axis, errelipseKDOP);
171  // check if KDOPs overlap and return result
172  return bchk.TestKDOPKDOP(elementKDOP, errelipseKDOP);
173 }
174 
175 double
177 {
178  const int Np = 3;
179 
180  double X[3] = { m_boundValues[TriangleBounds::bv_x1],
181  m_boundValues[TriangleBounds::bv_x2],
182  m_boundValues[TriangleBounds::bv_x3] };
183  double Y[3] = { m_boundValues[TriangleBounds::bv_y1],
184  m_boundValues[TriangleBounds::bv_y2],
185  m_boundValues[TriangleBounds::bv_y3] };
186 
187  double dm = 1.e+20;
188  double Ao = 0.;
189  bool in = true;
190 
191  for (int i = 0; i != Np; ++i) {
192 
193  int j = (i == Np-1 ? 0 : i+1);
194 
195  double x = X[i] - pos[0];
196  double y = Y[i] - pos[1];
197  double dx = X[j] - X[i];
198  double dy = Y[j] - Y[i];
199  double A = x * dy - y * dx;
200  double S = -(x * dx + y * dy);
201 
202  if (S <= 0.) {
203  double d = x * x + y * y;
204  if (d < dm)
205  dm = d;
206  } else {
207  double a = dx * dx + dy * dy;
208  if (S <= a) {
209  double d = (A * A) / a;
210  if (d < dm)
211  dm = d;
212  }
213  }
214  if (i && in && Ao * A < 0.)
215  in = false;
216  Ao = A;
217  }
218  if (in){
219  return -sqrt(dm);
220  }
221  return sqrt(dm);
222 }
223 
224 // ostream operator overload
225 MsgStream&
226 Trk::TriangleBounds::dump(MsgStream& sl) const
227 {
228  sl << std::setiosflags(std::ios::fixed);
229  sl << std::setprecision(7);
230  sl << "Trk::TriangleBounds: generating vertices (X, Y) " << '\n';
231  sl << "(" << m_boundValues[TriangleBounds::bv_x1] << " , " << m_boundValues[TriangleBounds::bv_y1] << ") " << '\n';
232  sl << "(" << m_boundValues[TriangleBounds::bv_x2] << " , " << m_boundValues[TriangleBounds::bv_y2] << ") " << '\n';
233  sl << "(" << m_boundValues[TriangleBounds::bv_x3] << " , " << m_boundValues[TriangleBounds::bv_y3] << ") ";
234  sl << std::setprecision(-1);
235  return sl;
236 }
237 
238 std::ostream&
239 Trk::TriangleBounds::dump(std::ostream& sl) const
240 {
241  sl << std::setiosflags(std::ios::fixed);
242  sl << std::setprecision(7);
243  sl << "Trk::TriangleBounds: generating vertices (X, Y)";
244  sl << "(" << m_boundValues[TriangleBounds::bv_x1] << " , " << m_boundValues[TriangleBounds::bv_y1] << ") " << '\n';
245  sl << "(" << m_boundValues[TriangleBounds::bv_x2] << " , " << m_boundValues[TriangleBounds::bv_y2] << ") " << '\n';
246  sl << "(" << m_boundValues[TriangleBounds::bv_x3] << " , " << m_boundValues[TriangleBounds::bv_y3] << ") ";
247  sl << std::setprecision(-1);
248  return sl;
249 }
Trk::y
@ y
Definition: ParamDefs.h:62
Trk::AmgMatrix
AmgMatrix(3, 3) NeutralParticleParameterCalculator
Definition: NeutralParticleParameterCalculator.cxx:233
python.PerfMonSerializer.p
def p
Definition: PerfMonSerializer.py:743
Trk::locX
@ locX
Definition: ParamDefs.h:43
Trk::locY
@ locY
local cartesian
Definition: ParamDefs.h:44
Amg::Vector2D
Eigen::Matrix< double, 2, 1 > Vector2D
Definition: GeoPrimitives.h:48
hist_file_dump.d
d
Definition: hist_file_dump.py:137
Trk::SurfaceBounds
Definition: SurfaceBounds.h:47
Trk::TriangleBounds::inside
virtual bool inside(const Amg::Vector2D &locpo, double tol1=0., double tol2=0.) const override final
This method checks if the provided local coordinates are inside the surface bounds.
Definition: TriangleBounds.cxx:73
CaloCondBlobAlgs_fillNoiseFromASCII.db
db
Definition: CaloCondBlobAlgs_fillNoiseFromASCII.py:43
Trk::TriangleBounds::r
virtual double r() const override final
This method returns the maximal extension on the local plane, i.e.
yodamerge_tmp.axis
list axis
Definition: yodamerge_tmp.py:241
PlotCalibFromCool.ib
ib
Definition: PlotCalibFromCool.py:419
Trk::TriangleBounds::vertices
std::vector< std::pair< TDD_real_t, TDD_real_t > > vertices() const
This method returns the coordinates of vertices.
read_hist_ntuple.t
t
Definition: read_hist_ntuple.py:5
Trk::TriangleBounds::bv_x3
@ bv_x3
Definition: TriangleBounds.h:49
JetTiledMap::S
@ S
Definition: TiledEtaPhiMap.h:44
Trk::TriangleBounds::m_boundValues
std::vector< TDD_real_t > m_boundValues
Definition: TriangleBounds.h:118
Monitored::X
@ X
Definition: HistogramFillerUtils.h:24
ParticleJetTools::p3
Amg::Vector3D p3(const xAOD::TruthVertex *p)
Definition: ParticleJetLabelCommon.cxx:55
Trk::TriangleBounds::bv_y2
@ bv_y2
Definition: TriangleBounds.h:48
Trk::BoundaryCheck::EllipseToPoly
std::vector< Amg::Vector2D > EllipseToPoly(int resolution=3) const
Trk::BoundaryCheck::toleranceLoc1
double toleranceLoc1
absolute tolerance in local 1 coordinate
Definition: BoundaryCheck.h:68
Trk::TriangleBounds
Definition: TriangleBounds.h:39
Trk::TriangleBounds::minDistance
virtual double minDistance(const Amg::Vector2D &pos) const override final
Minimal distance to boundary ( > 0 if outside and <=0 if inside)
Definition: TriangleBounds.cxx:176
lumiFormat.i
int i
Definition: lumiFormat.py:92
Trk::theta
@ theta
Definition: ParamDefs.h:72
python.getCurrentFolderTag.dn
dn
Definition: getCurrentFolderTag.py:64
Trk::BoundaryCheck::bcType
BoundaryCheckType bcType
Definition: BoundaryCheck.h:70
Trk::sincosCache
Definition: BoundaryCheck.h:45
Trk::BoundaryCheck::TestKDOPKDOP
bool TestKDOPKDOP(const std::vector< KDOP > &a, const std::vector< KDOP > &b) const
ReadCellNoiseFromCool.dm
dm
Definition: ReadCellNoiseFromCool.py:235
Monitored::Y
@ Y
Definition: HistogramFillerUtils.h:24
Trk::sincosCache::sinC
double sinC
Definition: BoundaryCheck.h:46
Trk::BoundaryCheck::nSigmas
int nSigmas
allowed sigmas for chi2 boundary check
Definition: BoundaryCheck.h:67
Trk::TriangleBounds::dump
virtual MsgStream & dump(MsgStream &sl) const override
Output Method for MsgStream.
Definition: TriangleBounds.cxx:226
Trk::TriangleBounds::bv_x2
@ bv_x2
Definition: TriangleBounds.h:47
Trk::BoundaryCheck::ComputeKDOP
void ComputeKDOP(const std::vector< Amg::Vector2D > &v, const std::vector< Amg::Vector2D > &KDOPAxes, std::vector< KDOP > &kdop) const
Each Bounds has a method inside, which checks if a LocalPosition is inside the bounds.
Trk::TriangleBounds::operator==
virtual bool operator==(const SurfaceBounds &sbo) const override
Equality operator.
Definition: TriangleBounds.cxx:63
python.LumiBlobConversion.pos
pos
Definition: LumiBlobConversion.py:18
Trk::BoundaryCheck::FastArcTan
double FastArcTan(double x) const
makeTRTBarrelCans.dy
tuple dy
Definition: makeTRTBarrelCans.py:21
a
TList * a
Definition: liststreamerinfos.cxx:10
TriangleBounds.h
Trk::BoundaryCheck
Definition: BoundaryCheck.h:51
Trk::sincosCache::cosC
double cosC
Definition: BoundaryCheck.h:47
Trk::TriangleBounds::bv_y3
@ bv_y3
Definition: TriangleBounds.h:50
Trk::TriangleBounds::bv_x1
@ bv_x1
Definition: TriangleBounds.h:45
makeTRTBarrelCans.dx
tuple dx
Definition: makeTRTBarrelCans.py:20
Trk::BoundaryCheck::FastSinCos
sincosCache FastSinCos(double x) const
Trk::BoundaryCheck::toleranceLoc2
double toleranceLoc2
absolute tolerance in local 2 coordinate
Definition: BoundaryCheck.h:69
Trk::x
@ x
Definition: ParamDefs.h:61
updateCoolNtuple.limit
int limit
Definition: updateCoolNtuple.py:45
Trk::TriangleBounds::bv_y1
@ bv_y1
Definition: TriangleBounds.h:46
Trk::TriangleBounds::TriangleBounds
TriangleBounds()
Default Constructor - needed for persistency.
Definition: TriangleBounds.cxx:18