TrdMCClusterR Fit Classifier
Line3DCompare.h
Go to the documentation of this file.
1 #include <cmath>
2 #include <vector>
3 #include <iostream>
4 
5 struct Vector3D
6 {
7  double x, y, z;
8 
9  // Constructors
10  Vector3D() : x(0), y(0), z(0) {}
11  Vector3D(double x, double y, double z) : x(x), y(y), z(z) {}
12 
13  // Operations
14  Vector3D operator-(const Vector3D& v) const {
15  return {x - v.x, y - v.y, z - v.z};
16  }
17  Vector3D cross(const Vector3D& v) const {
18  return {y * v.z - z * v.y, z * v.x - x * v.z, x * v.y - y * v.x};
19  }
20  double dot(const Vector3D& v) const {
21  return x * v.x + y * v.y + z * v.z;
22  }
23  double magnitude() const {
24  return std::sqrt(x*x + y*y + z*z);
25  }
26 };
27 
28 
29 bool areCoplanar(const Vector3D& d1, const Vector3D& d2, const Vector3D& v) {
30  return std::abs(d1.cross(d2).dot(v)) < 1e-6;
31 }
32 
33 
34 bool areParallel(const Vector3D& d1, const Vector3D& d2) {
35  Vector3D crossProduct = d1.cross(d2);
36  return crossProduct.magnitude() < 1e-6;
37 }
38 
39 
40 bool areCoincident(const Vector3D& d1, const Vector3D& v) {
41  return std::abs(d1.dot(v)) < 1e-6;
42 }
43 
44 
45 double shortestDistance(const Vector3D& d1, const Vector3D& d2, const Vector3D& v) {
46  return std::abs(v.dot(d1.cross(d2))) / d1.cross(d2).magnitude();
47 }
48 
49 
50 void compareLines(const FitLine3D& line1, const FitLine3D& line2)
51 {
52  Vector3D P1(line1.fitParameters[0], line1.fitParameters[1], line1.fitParameters[2]);
53  Vector3D d1(line1.fitParameters[3], line1.fitParameters[4], line1.fitParameters[5]);
54 
55  Vector3D P2(line2.fitParameters[0], line2.fitParameters[1], line2.fitParameters[2]);
56  Vector3D d2(line2.fitParameters[3], line2.fitParameters[4], line2.fitParameters[5]);
57 
58  Vector3D v = P2 - P1;
59 
60  // Check for coplanarity
61  if (areCoplanar(d1, d2, v)) {
62  std::cout << "The lines are coplanar." << std::endl;
63  } else {
64  std::cout << "The lines are not coplanar." << std::endl;
65  }
66 
67  // Check for parallel, coincident, or skewed
68  if (areParallel(d1, d2)) {
69  if (areCoincident(d1, v)) {
70  std::cout << "The lines are coincident." << std::endl;
71  } else {
72  std::cout << "The lines are parallel." << std::endl;
73  }
74  } else {
75  std::cout << "The lines are skewed." << std::endl;
76  }
77 
78  // Print the shortest distance between the lines
79  double distance = shortestDistance(d1, d2, v);
80  std::cout << "Shortest distance between the lines: " << distance << std::endl;
81 }
bool areCoplanar(const Vector3D &d1, const Vector3D &d2, const Vector3D &v)
Definition: Line3DCompare.h:29
void compareLines(const FitLine3D &line1, const FitLine3D &line2)
Definition: Line3DCompare.h:50
bool areParallel(const Vector3D &d1, const Vector3D &d2)
Definition: Line3DCompare.h:34
bool areCoincident(const Vector3D &d1, const Vector3D &v)
Definition: Line3DCompare.h:40
double shortestDistance(const Vector3D &d1, const Vector3D &d2, const Vector3D &v)
Definition: Line3DCompare.h:45
Definition: Line3DCompare.h:6
Vector3D()
Definition: Line3DCompare.h:10
Vector3D cross(const Vector3D &v) const
Definition: Line3DCompare.h:17
double magnitude() const
Definition: Line3DCompare.h:23
double x
Definition: Line3DCompare.h:7
double z
Definition: Line3DCompare.h:7
Vector3D(double x, double y, double z)
Definition: Line3DCompare.h:11
double dot(const Vector3D &v) const
Definition: Line3DCompare.h:20
Vector3D operator-(const Vector3D &v) const
Definition: Line3DCompare.h:14
double y
Definition: Line3DCompare.h:7