GeometryUtilities.cc
Go to the documentation of this file.
1 #include <utl/Math.h>
2 #include <utl/AxialVector.h>
3 #include <utl/GeometryUtilities.h>
4 #include <utl/MathConstants.h>
5 #include <utl/ReferenceEllipsoid.h>
6 #include <utl/Test.h>
7 #include <utl/CoordinateSystemPtr.h>
8 #include <utl/PhysicalConstants.h>
9 
10 #include <cmath>
11 
12 using namespace std;
13 
14 
15 namespace utl {
16 
17  Line
18  Intersection(const Plane& p1, const Plane& p2)
19  {
20  const auto& a1 = p1.GetAnchor();
21  const Vector b = 0.5 * (a1 - p2.GetAnchor());
22  const auto& n1 = p1.GetNormal();
23  const auto& n2 = p2.GetNormal();
24  const double n1b = n1 * b;
25  const double n2b = n2 * b;
26  const double nn = n1 * n2;
27  const double det = 1 - Sqr(nn);
28  const double alpha = (nn*n2b + n1b) / det;
29  const double beta = - (nn*n1b + n2b) / det;
30  const Point anch = a1 - b + alpha*n1 + beta*n2;
31  const Vector dir = Cross(n1, n2);
32 
33  return Line(anch, dir);
34  }
35 
36 
37  Point
38  Intersection(const Plane& plane, const Line& line)
39  {
40  const auto& al = line.GetAnchor();
41  const auto& dir = line.GetDirection();
42  const auto& n = plane.GetNormal();
43  return al - ((n*(al - plane.GetAnchor())) / (n*dir)) * dir;
44  }
45 
46 
47  vector<Point>
48  Intersection(const ReferenceEllipsoid& ellipsoid,
49  const double height,
50  const Line& line)
51  {
52  const double alpha = ellipsoid.GetPolarRadius() + height; // minor axis
53  const double beta = ellipsoid.GetEquatorialRadius() + height; // major axis
54 
55  const double alpha2 = Sqr(alpha);
56  const double beta2 = Sqr(beta);
57 
58  const auto& csECEF = CoordinateSystem::GetRootCoordinateSystem();
59  const auto& direction = line.GetDirection();
60 
61  // direction in kartesian coordinate system Earth Centered Earth Fixed
62  const auto uvw = direction.GetCoordinates(csECEF);
63  const double& u = uvw.get<0>();
64  const double& v = uvw.get<1>();
65  const double& w = uvw.get<2>();
66 
67  const auto& anchor = line.GetAnchor();
68  // point in kartesian coordinates
69  const auto xyz = anchor.GetCoordinates(csECEF);
70  const double& x = xyz.get<0>();
71  const double& y = xyz.get<1>();
72  const double& z = xyz.get<2>();
73 
74  // -- constants for quadratic equation --
75  const double a = (Sqr(u) + Sqr(v))*alpha2 + Sqr(w*beta);
76  const double b = (x*u + y*v)*alpha2 + z*w*beta2;
77  const double c = (Sqr(x) + Sqr(y))*alpha2 + (Sqr(z) - alpha2)*beta2;
78 
79  // -- for quadratic equation --
80  const double ba = -b/a;
81  const double dis = Sqr(ba) - c/a; // D
82 
83  vector<Point> intersectionPoints; // 0 <= intersection points <= 2
84 
85  // since 0 is hardly reached with double-division...
86  if (CloseTo()(dis, 0.)) {
87 
88  // D == 0 //just touching the surface
89  // point, where particle touches the ellipsoid
90  intersectionPoints.push_back(anchor + ba*direction);
91 
92  } else if (dis > 0) { // ----------------- 2 intersection points with ellipsoid
93 
94  // two solutions of quadratic equation
95  const double sdis = sqrt(dis);
96 
97  // point, where particle left the ellipsoid
98  intersectionPoints.push_back(anchor + (ba + sdis)*direction);
99  // point, where particle hit the ellipsoid
100  intersectionPoints.push_back(anchor + (ba - sdis)*direction);
101 
102  }
103 
104  return intersectionPoints;
105  }
106 
107 
108  double
109  Distance(const Line& line1, const Line& line2)
110  {
111  const auto& s1 = line1.GetDirection();
112  const auto& s2 = line2.GetDirection();
113  const Vector b = line1.GetAnchor() - line2.GetAnchor();
114  Vector n = Cross(s1, s2);
115  const double norm2 = n.GetMag2();
116  if (norm2) {
117  n /= sqrt(norm2);
118  return b * n;
119  } else
120  return sqrt(b*b - Sqr(b*s1));
121  }
122 
123 
124  double
125  Distance(const Point& point, const Line& line)
126  {
127  const Vector a = point - line.GetAnchor();
128  const auto& dir = line.GetDirection();
129  const Vector b = (dir*a)*dir;
130  const Vector perp = a - b;
131  return perp.GetMag();
132  }
133 
134 }
AxialVector Cross(const Vector &l, const Vector &r)
Definition: OperationsAV.h:25
constexpr T Sqr(const T &x)
Point object.
Definition: Point.h:32
const Point & GetAnchor() const
Definition: Plane.h:22
double GetMag() const
Definition: Vector.h:58
Line Intersection(const Plane &p1, const Plane &p2)
Reference ellipsoids for UTM transformations.
Class describing a Plane object.
Definition: Plane.h:17
double GetEquatorialRadius() const
Get equatorial radius ( )
double GetPolarRadius() const
Get Polar radius ( )
double Distance(const Point &p, const sdet::Station &s)
Vector object.
Definition: Vector.h:30
const Vector & GetDirection() const
Definition: Line.h:22
const Point & GetAnchor() const
Definition: Line.h:21
const Vector & GetNormal() const
Definition: Plane.h:23
Definition: Line.h:17
Predicate for approximate equality (for floating point)
Definition: Test.h:91
double GetMag2() const
Definition: Vector.h:61

, generated on Tue Sep 26 2023.