ATLAS Offline Software
Loading...
Searching...
No Matches
GeoPrimitivesHelpers.h
Go to the documentation of this file.
1/*
2 Copyright (C) 2002-2024 CERN for the benefit of the ATLAS collaboration
3*/
4
6// GeoPrimitivesHelpers.h, (c) ATLAS Detector software
8
9#ifndef GEOPRIMITIVES_GEOPRIMITIVESHELPERS_H
10#define GEOPRIMITIVES_GEOPRIMITIVESHELPERS_H
11
14#include "CxxUtils/sincos.h"
15
16#include "cmath"
17#include <sstream>
18#include <stdexcept>
19
20#include <vector>
21#include <optional>
22#include <set>
23#include <iostream>
24
25
32
33namespace Amg {
34
35
36
37using SetVector3D = std::set<Amg::Vector3D, Vector3DComparer>;
38using SetVectorVector3D = std::set< std::vector< Amg::Vector3D>, VectorVector3DComparer>;
39
40
41
43inline double angle(const Amg::Vector3D& v1, const Amg::Vector3D& v2) {
44 const double dp = std::clamp(v1.dot(v2) / (v1.mag() * v2.mag()), -1. ,1.);
45 return std::acos(dp);
46}
47
48
50inline float distance2(const Amg::Vector3D& p1, const Amg::Vector3D& p2) {
51 float dx = p2.x()-p1.x(), dy = p2.y()-p1.y(), dz = p2.z()-p1.z();
52 return dx*dx + dy*dy + dz*dz;
53}
54
56inline float distance(const Amg::Vector3D& p1, const Amg::Vector3D& p2) {
57 return std::sqrt( distance2(p1, p2) );
58}
59
60
61
62
64inline void setPhi(Amg::Vector3D& v, double phi) {
65 double xy = v.perp();
67 v[0] = xy * sc.cs;
68 v[1] = xy * sc.sn;
69}
70
72inline void setThetaPhi(Amg::Vector3D& v, double theta, double phi) {
73 double mag = v.mag();
76 v[0] = mag * sct.sn * sc.cs;
77 v[1] = mag * sct.sn * sc.sn;
78 v[2] = mag * sct.cs;
79}
80
82inline void setRThetaPhi(Amg::Vector3D& v, double r, double theta, double phi) {
85 v[0] = r * sct.sn * sc.cs;
86 v[1] = r * sct.sn * sc.sn;
87 v[2] = r * sct.cs;
88}
89
91inline void setTheta(Amg::Vector3D& v, double theta) {
92 setThetaPhi(v, theta, v.phi());
93}
94
96inline void setPerp(Amg::Vector3D& v, double perp) {
97 double p = v.perp();
98 if (p != 0.0) {
99 double scale = perp / p;
100 v[0] *= scale;
101 v[1] *= scale;
102 }
103}
104
106inline void setMag(Amg::Vector3D& v, double mag) {
107 double p = v.mag();
108 if (p != 0.0) {
109 double scale = mag / p;
110 v[0] *= scale;
111 v[1] *= scale;
112 v[2] *= scale;
113 }
114}
115inline double deltaPhi(const Amg::Vector3D& v1, const Amg::Vector3D& v2) {
116 double dphi = v2.phi() - v1.phi();
117 if (dphi > M_PI) {
118 dphi -= M_PI*2;
119 } else if (dphi <= -M_PI) {
120 dphi += M_PI*2;
121 }
122 return dphi;
123}
124inline double deltaR(const Amg::Vector3D& v1, const Amg::Vector3D& v2){
125 double a = v1.eta() - v2.eta();
126 double b = deltaPhi(v1,v2);
127 return sqrt(a*a + b*b);
128}
129
130
131
132
133
134
138inline void setVector3DCartesian(Amg::Vector3D& v1, double x1, double y1, double z1) { v1[0] = x1; v1[1] = y1; v1[2] = z1; }
142inline double mag2Vector3D(const Amg::Vector3D& v1) { return v1.x()*v1.x() + v1.y()*v1.y() + v1.z()*v1.z(); }
146inline double magVector3D(const Amg::Vector3D& v1) { return std::sqrt(mag2Vector3D(v1)); }
150inline double rVector3D(const Amg::Vector3D& v1) { return magVector3D(v1); }
151
159 Amg::Vector3D vect;
160 double vx = v.x(), vy = v.y(), vz = v.z();
162 tr(0,0)*vx + tr(0,1)*vy + tr(0,2)*vz + tr(0,3),
163 tr(1,0)*vx + tr(1,1)*vy + tr(1,2)*vz + tr(1,3),
164 tr(2,0)*vx + tr(2,1)*vy + tr(2,2)*vz + tr(2,3));
165 return vect;
166}
167
168
169
170
171/*
172 * the analogous to CLHEP HepGeom::Transform3D trans (localRot, theSurface.transform().translation());
173 */
175{
176 Amg::Transform3D trans = Amg::Transform3D::Identity();
177 trans = trans * rot;
178 trans.translation() = transl_vec;
179 return trans;
180}
181
182/*
183 * Replacing the CLHEP::HepRotation::getAngleAxis() functionality
184 *
185 * Note:
186 * CLHEP has a 'HepRotation::getAngleAxis()' function, e.g.:
187 * ---
188 * CLHEP::HepRotation rotation = transform.getRotation();
189 * CLHEP::Hep3Vector rotationAxis;
190 * double rotationAngle;
191 * rotation.getAngleAxis(rotationAngle,rotationAxis);
192 * ---
193 */
194inline void getAngleAxisFromRotation(Amg::RotationMatrix3D& rotation, double& rotationAngle, Amg::Vector3D& rotationAxis)
195{
196 rotationAngle = 0.;
197
198 double xx = rotation(0,0);
199 double yy = rotation(1,1);
200 double zz = rotation(2,2);
201
202 double cosa = 0.5 * (xx + yy + zz - 1);
203 double cosa1 = 1 - cosa;
204
205 if (cosa1 <= 0) {
206 rotationAngle = 0;
207 rotationAxis = Amg::Vector3D(0,0,1);
208 }
209 else{
210 double x=0, y=0, z=0;
211 if (xx > cosa) x = std::sqrt((xx-cosa)/cosa1);
212 if (yy > cosa) y = std::sqrt((yy-cosa)/cosa1);
213 if (zz > cosa) z = std::sqrt((zz-cosa)/cosa1);
214 if (rotation(2,1) < rotation(1,2)) x = -x;
215 if (rotation(0,2) < rotation(2,0)) y = -y;
216 if (rotation(1,0) < rotation(0,1)) z = -z;
217 rotationAngle = (cosa < -1.) ? std::acos(-1.) : std::acos(cosa);
218 rotationAxis = Amg::Vector3D(x,y,z);
219 }
220
221 return;
222}
223
228 return Amg::Vector3D(tr(0,3),tr(1,3),tr(2,3));
229} // TODO: check! it's perhaps useless, you acn use the transform.translation() method
230
231
232
242
243
247 return Amg::Isometry3D{Amg::AngleAxis3D{angle, Amg::Vector3D::UnitX()}};
248}
249
252 return Amg::Isometry3D{Amg::AngleAxis3D{angle, Amg::Vector3D::UnitY()}};
253}
254
257 return Amg::Isometry3D{Amg::AngleAxis3D{angle, Amg::Vector3D::UnitZ()}};
258}
259
260inline Amg::Isometry3D getTranslateX3D(const double X) {
261 return Amg::Isometry3D{Amg::Translation3D{X * Amg::Vector3D::UnitX()}};
262}
263
264inline Amg::Isometry3D getTranslateY3D(const double Y) {
265 return Amg::Isometry3D{Amg::Translation3D{Y * Amg::Vector3D::UnitY()}};
266}
267
268inline Amg::Isometry3D getTranslateZ3D(const double Z) {
269 return Amg::Isometry3D{Amg::Translation3D{Z * Amg::Vector3D::UnitZ()}};
270}
271
272inline Amg::Isometry3D getTranslate3D(const double X, const double Y, const double Z) {
274}
275
279
285 const double tolerance = 1.e-9) {
286 const double deviation = (trf.linear() * trf.linear().transpose() -
287 Amg::RotationMatrix3D::Identity()).cwiseAbs().maxCoeff();
288 if (deviation > tolerance) {
289 std::stringstream msg{};
290 msg<<__FILE__<<":"<<__LINE__<<" --- Transform is not isometric, its linear part "
291 <<"deviates from orthogonal by "<<deviation<<".";
292 throw std::runtime_error(msg.str());
293 }
294 Amg::Isometry3D iso{Amg::Isometry3D::Identity()};
295 iso.linear() = trf.linear();
296 iso.translation() = trf.translation();
297 return iso;
298}
299
303inline Amg::Vector3D dirFromAngles(const double phi, const double theta) {
304 const CxxUtils::sincos thetaCS{theta}, phiCS{phi};
305 return Amg::Vector3D{phiCS.cs * thetaCS.sn, phiCS.sn* thetaCS.sn, thetaCS.cs};
306}
307
313 const Amg::Vector3D& planeNorm) {
314 return (direction - planeNorm.dot(direction) * planeNorm).unit();
315}
316
322template<int N> double lineDistance(const AmgVector(N)& posA,
323 const AmgVector(N)& dirA,
324 const AmgVector(N)& posB,
325 const AmgVector(N)& dirB) {
326
327 const double dirDots = dirA.dot(dirB);
328 const double divisor = (1. - dirDots * dirDots);
329 const AmgVector(N) AminusB = posA - posB;
330 if (std::abs(divisor) < std::numeric_limits<double>::epsilon()) {
331 const AmgVector(N) d = AminusB - dirA.dot(AminusB)*dirA;
332 return std::sqrt(d.dot(d));
333 }
334 const AmgVector(N) lineTravel = AminusB.dot(dirA) * dirA -
335 AminusB.dot(dirB) * dirB;
336 return std::sqrt(std::max(0., AminusB.dot(AminusB) - lineTravel.dot(lineTravel) / divisor));
337}
338
343inline double signedDistance(const Amg::Vector3D& posA,
344 const Amg::Vector3D& dirA,
345 const Amg::Vector3D& posB,
346 const Amg::Vector3D& dirB) {
348 const double dirDots = dirA.dot(dirB);
349 const Amg::Vector3D AminusB = posA - posB;
350 if (std::abs(dirDots -1.) < std::numeric_limits<float>::epsilon()){
351 return (AminusB - dirA.dot(AminusB)*dirA).mag();
352 }
353 const Amg::Vector3D projDir = (dirA - dirDots*dirB).unit();
354 return AminusB.cross(dirB).dot(projDir);
355}
356
361template <int N> std::optional<double> intersect(const AmgVector(N)& posA,
362 const AmgVector(N)& dirA,
363 const AmgVector(N)& posB,
364 const AmgVector(N)& dirB) {
373 const double dirDots = dirA.dot(dirB);
374 const double divisor = (1. - dirDots * dirDots);
376 if (std::abs(divisor) < std::numeric_limits<double>::epsilon()) return std::nullopt;
377 const AmgVector(N) AminusB = posA - posB;
378 return (AminusB.dot(dirB) - AminusB.dot(dirA) * dirDots) / divisor;
379}
380
382template <int N>
383std::optional<double> intersect(const AmgVector(N)& pos,
384 const AmgVector(N)& dir,
385 const AmgVector(N)& planeNorm,
386 const double offset) {
390 const double normDot = planeNorm.dot(dir);
391 if (std::abs(normDot) < std::numeric_limits<double>::epsilon()) return std::nullopt;
392 return (offset - pos.dot(planeNorm)) / normDot;
393}
394
398 for (unsigned int d = 0; d < 3 ; ++d) {
399 const double defLength = Amg::Vector3D::Unit(d).dot(trans.linear() * Amg::Vector3D::Unit(d));
400 if (std::abs(defLength - 1.) > std::numeric_limits<float>::epsilon()) {
401 return false;
402 }
403 }
404 return true;
405}
406
407inline bool isIdentity(const Amg::Transform3D& trans) {
408 return doesNotDeform(trans) &&
409 trans.translation().mag() < std::numeric_limits<float>::epsilon();
410}
411
412
413} // end of Amg namespace
414
415#endif
#define M_PI
Scalar perp() const
perp method - perpendicular length
Scalar phi() const
phi method
Scalar theta() const
theta method
Scalar mag() const
mag method
detray::unit< scalar_t > unit
#define AmgVector(rows)
static Double_t a
static Double_t sc
TH1F * trans(TH1F *h, bool t=false)
int r
Definition globals.cxx:22
Definition of ATLAS Math & Geometry primitives (Amg).
Amg::Vector3D projectDirOntoPlane(const Amg::Vector3D &direction, const Amg::Vector3D &planeNorm)
Project the direction vector onto the plane and renormalize to unity.
void setVector3DCartesian(Amg::Vector3D &v1, double x1, double y1, double z1)
Sets components in cartesian coordinate system.
Amg::Isometry3D getRotateX3D(double angle)
Rotate the coordinate system by an angle around the x-axis.
std::set< Amg::Vector3D, Vector3DComparer > SetVector3D
double mag2Vector3D(const Amg::Vector3D &v1)
Gets magnitude squared of the vector.
std::optional< double > intersect(const AmgVector(N)&posA, const AmgVector(N)&dirA, const AmgVector(N)&posB, const AmgVector(N)&dirB)
Calculates the point B' along the line B that's closest to a second line A.
double deltaPhi(const Amg::Vector3D &v1, const Amg::Vector3D &v2)
Eigen::AngleAxisd AngleAxis3D
void setRThetaPhi(Amg::Vector3D &v, double r, double theta, double phi)
sets radius, the theta and phi angle of a vector.
double lineDistance(const AmgVector(N)&posA, const AmgVector(N)&dirA, const AmgVector(N)&posB, const AmgVector(N)&dirB)
: Calculates the shortest distance between two lines
Amg::Isometry3D toIsometry3D(const Amg::Transform3D &trf, const double tolerance=1.e-9)
Convert a general transform into an isometric one, e.g.
Amg::Vector3D getTranslationVectorFromTransform(const Amg::Transform3D &tr)
Get the Translation vector out of a Transformation.
Eigen::Quaternion< double > Rotation3D
double signedDistance(const Amg::Vector3D &posA, const Amg::Vector3D &dirA, const Amg::Vector3D &posB, const Amg::Vector3D &dirB)
Calculates the signed distance between two lines in 3D space.
double rVector3D(const Amg::Vector3D &v1)
Gets r-component in spherical coordinate system.
Amg::Isometry3D getTranslateY3D(const double Y)
: Returns a shift transformation along the y-axis
Eigen::Matrix< double, 3, 3 > RotationMatrix3D
bool isIdentity(const Amg::Transform3D &trans)
Checks whether the transformation is the Identity transformation.
bool doesNotDeform(const Amg::Transform3D &trans)
Checks whether the linear part of the transformation rotates or stetches any of the basis vectors.
std::set< std::vector< Amg::Vector3D >, VectorVector3DComparer > SetVectorVector3D
Amg::RotationMatrix3D setPhi(Amg::RotationMatrix3D mat, double angle, int convention=0)
void getAngleAxisFromRotation(Amg::RotationMatrix3D &rotation, double &rotationAngle, Amg::Vector3D &rotationAxis)
Eigen::Isometry3d Isometry3D
void setMag(Amg::Vector3D &v, double mag)
scales the vector length without changing the angles
Amg::Vector3D dirFromAngles(const double phi, const double theta)
Constructs a direction vector from the azimuthal & polar angles.
Amg::Isometry3D getTranslateX3D(const double X)
: Returns a shift transformation along the x-axis
Amg::Transform3D getTransformFromRotTransl(Amg::RotationMatrix3D rot, Amg::Vector3D transl_vec)
double angle(const Amg::Vector3D &v1, const Amg::Vector3D &v2)
calculates the opening angle between two vectors
Amg::Isometry3D getTranslate3D(const double X, const double Y, const double Z)
: Returns a shift transformation along an arbitrary axis
float distance2(const Amg::Vector3D &p1, const Amg::Vector3D &p2)
calculates the squared distance between two point in 3D space
void setPerp(Amg::Vector3D &v, double perp)
scales the vector in the xy plane without changing the z coordinate nor the angles
Eigen::Affine3d Transform3D
float distance(const Amg::Vector3D &p1, const Amg::Vector3D &p2)
calculates the distance between two point in 3D space
Amg::Rotation3D getRotation3DfromAngleAxis(double angle, Amg::Vector3D &axis)
get a AngleAxis from an angle and an axis.
Amg::Vector3D transform(Amg::Vector3D &v, Amg::Transform3D &tr)
Transform a point from a Trasformation3D.
Eigen::Matrix< double, 3, 1 > Vector3D
double magVector3D(const Amg::Vector3D &v1)
Gets magnitude of the vector.
void setTheta(Amg::Vector3D &v, double theta)
sets the theta of a vector without changing phi nor the magnitude
Amg::Isometry3D getTranslateZ3D(const double Z)
: Returns a shift transformation along the z-axis
void setThetaPhi(Amg::Vector3D &v, double theta, double phi)
sets the theta and phi angle of a vector without changing the magnitude
Amg::Isometry3D getRotateY3D(double angle)
Rotate the coordinate system by an angle around the z-axis.
Amg::Isometry3D getRotateZ3D(double angle)
Rotate the coordinate system by an angle around the z-axis.
Eigen::Translation< double, 3 > Translation3D
double deltaR(const Amg::Vector3D &v1, const Amg::Vector3D &v2)
Helper to simultaneously calculate sin and cos of the same angle.
Helper to simultaneously calculate sin and cos of the same angle.
Definition sincos.h:39
MsgStream & msg
Definition testRead.cxx:32