Warning, /include/Geant4/tools/lina/line is written in an unsupported language. File is not indexed.
0001 // Copyright (C) 2010, Guy Barrand. All rights reserved.
0002 // See the file tools.license for terms.
0003
0004 #ifndef tools_line
0005 #define tools_line
0006
0007 namespace tools {
0008
0009 // Parametric description:
0010 // l(t) = pos + t * dir
0011
0012 template <class VEC3>
0013 class line {
0014 protected:
0015 typedef typename VEC3::elem_t T;
0016 public:
0017 line(){}
0018 line(const VEC3& a_p0,const VEC3& a_p1) {
0019 // Construct a line from two points lying on the line. If you
0020 // want to construct a line from a position and a direction, use
0021 // line(p, p + d).
0022 // line is directed from p0 to p1.
0023 m_pos = a_p0;
0024 //m_dir = a_p1-a_p0;
0025 m_dir = a_p0;
0026 m_dir.multiply(-1);
0027 m_dir.add(a_p1);
0028 m_dir.normalize();
0029 }
0030 line(const T& a_0_x,const T& a_0_y,const T& a_0_z,
0031 const T& a_1_x,const T& a_1_y,const T& a_1_z) {
0032 m_pos.set_value(a_0_x,a_0_y,a_0_z);
0033 m_dir.set_value(a_1_x-a_0_x,a_1_y-a_0_y,a_1_z-a_0_z);
0034 m_dir.normalize();
0035 }
0036 virtual ~line() {}
0037 public:
0038 line(const line& a_from)
0039 :m_pos(a_from.m_pos)
0040 ,m_dir(a_from.m_dir)
0041 {}
0042 line& operator=(const line& a_from) {
0043 m_pos = a_from.m_pos;
0044 m_dir = a_from.m_dir;
0045 return *this;
0046 }
0047 public:
0048 void set_value(const VEC3& a_p0,const VEC3& a_p1) {
0049 m_pos = a_p0;
0050 m_dir = a_p0;
0051 m_dir.multiply(-1);
0052 m_dir.add(a_p1);
0053 m_dir.normalize();
0054 }
0055 void set_value(const T& a_0_x,const T& a_0_y,const T& a_0_z,
0056 const T& a_1_x,const T& a_1_y,const T& a_1_z) {
0057 m_pos.set_value(a_0_x,a_0_y,a_0_z);
0058 m_dir.set_value(a_1_x-a_0_x,a_1_y-a_0_y,a_1_z-a_0_z);
0059 m_dir.normalize();
0060 }
0061
0062 const VEC3& position() const {return m_pos;}
0063 const VEC3& direction() const {return m_dir;}
0064
0065 /* not tested :
0066 void closest_point(const VEC3& a_point,VEC& a_out) const {
0067 //from coin3d/SbLine.cpp.
0068
0069 //
0070 // a_out
0071 // m_pos x-----x-------> m_dir
0072 // \ |
0073 // \ |
0074 // \ |
0075 // \ |
0076 // \|
0077 // x a_point
0078
0079 a_out = m_pos + m_dir * ((a_point - m_pos).dot(m_dir));
0080 }
0081
0082
0083 bool closest_points(const line<T>& a_line,VEC3& a_on_this,VEC3& a_on_line) const {
0084 //from coin3d/SbLine.cpp.
0085
0086 //WARNING : if ret false, a_on_this, a_on_line not set.
0087
0088 // Check if the lines are parallel.
0089 // FIXME: should probably use equals() here.
0090 if(a_line.m_dir == m_dir) return false;
0091 if(a_line.m_dir == T(-1)*m_dir) return false;
0092
0093 VEC3 P0 = m_pos;
0094 VEC3 P1 = a_line.m_pos;
0095 VEC3 D0 = m_dir;
0096 VEC3 D1 = a_line.m_dir;
0097 VEC3 D0N = D0;
0098
0099 T c[3], d[3];
0100
0101 for(unsigned int i=0;i<3;i++) {
0102 d[i] = D1[i] - D0N[i]*(D0[0]*D1[0] + D0[1]*D1[1] + D0[2]*D1[2]);
0103 c[i] = P1[i] - P0[i] + D0N[i]*(D0[0]*P0[0] + D0[1]*P0[1] + D0[2]*P0[2]);
0104 }
0105
0106 T den = d[0]*d[0]+d[1]*d[1]+d[2]*d[2];
0107 if(den==T()) return false;
0108
0109 T t = -(c[0]*d[0]+c[1]*d[1]+c[2]*d[2]) / den;
0110
0111 a_on_line = a_line.m_pos + a_line.m_dir * t;
0112 closest_point(a_on_line,a_on_this);
0113 return true;
0114 }
0115
0116 bool intersect(const line<T>& a_line,VEC3& a_out,const T& a_prec) const {
0117 VEC3 p,q;
0118 if(!closest_points(a_line,p,q)) return false;
0119 if((q-p).length()>a_prec) return false;
0120 a_out = p;
0121 return true;
0122 }
0123 */
0124
0125 protected:
0126 VEC3 m_pos;
0127 VEC3 m_dir; //normalized.
0128 };
0129
0130 }
0131
0132 #endif