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10.5.p1 ] [ 10.6 ] [ 10.6.p1 ] [ 10.6.p2 ] [ 10.6.p3 ] [ 10.7 ] [ 10.7.p1 ] [ 10.7.p2 ] [ 10.7.p3 ] [ 10.7.p4 ] [ 11.0 ] [ 11.0.p1 ] [ 11.0.p2 ] [ 11.0.p3, ] [ 11.0.p4 ] [ 11.1 ] [ 11.1.1 ] [ 11.1.2 ] [ 11.1.3 ] [ 11.2 ] [ 11.2.1 ] [ 11.2.2 ] [ 11.3.0 ]

  1 // Copyright (C) 2010, Guy Barrand. All rights reserved.
  2 // See the file tools.license for terms.
  3 
  4 #ifndef tools_line
  5 #define tools_line
  6 
  7 namespace tools {
  8 
  9 // Parametric description:
 10 //  l(t) = pos + t * dir
 11 
 12 template <class VEC3>
 13 class line {
 14 protected:
 15   typedef typename VEC3::elem_t T;
 16 public:
 17   line(){}
 18   line(const VEC3& a_p0,const VEC3& a_p1) {
 19     // Construct a line from two points lying on the line.  If you
 20     // want to construct a line from a position and a direction, use
 21     // line(p, p + d).
 22     // line is directed from p0 to p1.
 23     m_pos = a_p0;
 24     //m_dir = a_p1-a_p0;
 25     m_dir = a_p0;
 26     m_dir.multiply(-1);
 27     m_dir.add(a_p1);
 28     m_dir.normalize();
 29   }
 30   line(const T& a_0_x,const T& a_0_y,const T& a_0_z,
 31        const T& a_1_x,const T& a_1_y,const T& a_1_z) {
 32     m_pos.set_value(a_0_x,a_0_y,a_0_z);
 33     m_dir.set_value(a_1_x-a_0_x,a_1_y-a_0_y,a_1_z-a_0_z);
 34     m_dir.normalize();
 35   }
 36   virtual ~line() {}
 37 public:
 38   line(const line& a_from)
 39   :m_pos(a_from.m_pos)
 40   ,m_dir(a_from.m_dir)
 41   {}
 42   line& operator=(const line& a_from) {
 43     m_pos = a_from.m_pos;
 44     m_dir = a_from.m_dir;
 45     return *this;
 46   }
 47 public:
 48   void set_value(const VEC3& a_p0,const VEC3& a_p1) {
 49     m_pos = a_p0;
 50     m_dir = a_p0;
 51     m_dir.multiply(-1);
 52     m_dir.add(a_p1);
 53     m_dir.normalize();
 54   }
 55   void set_value(const T& a_0_x,const T& a_0_y,const T& a_0_z,
 56                  const T& a_1_x,const T& a_1_y,const T& a_1_z) {
 57     m_pos.set_value(a_0_x,a_0_y,a_0_z);
 58     m_dir.set_value(a_1_x-a_0_x,a_1_y-a_0_y,a_1_z-a_0_z);
 59     m_dir.normalize();
 60   }
 61 
 62   const VEC3& position() const {return m_pos;}
 63   const VEC3& direction() const {return m_dir;}
 64 
 65 /* not tested :
 66   void closest_point(const VEC3& a_point,VEC& a_out) const {
 67     //from coin3d/SbLine.cpp.
 68 
 69     //
 70     //           a_out
 71     // m_pos x-----x-------> m_dir
 72     //        \    |
 73     //         \   |
 74     //          \  |
 75     //           \ |
 76     //            \|
 77     //             x a_point
 78 
 79     a_out = m_pos + m_dir * ((a_point - m_pos).dot(m_dir));
 80   }
 81 
 82 
 83   bool closest_points(const line<T>& a_line,VEC3& a_on_this,VEC3& a_on_line) const {
 84     //from coin3d/SbLine.cpp.
 85 
 86     //WARNING : if ret false, a_on_this, a_on_line not set.
 87 
 88     // Check if the lines are parallel.
 89     // FIXME: should probably use equals() here.
 90     if(a_line.m_dir == m_dir) return false;
 91     if(a_line.m_dir == T(-1)*m_dir) return false;
 92 
 93     VEC3 P0 = m_pos;
 94     VEC3 P1 = a_line.m_pos;
 95     VEC3 D0 = m_dir;
 96     VEC3 D1 = a_line.m_dir;
 97     VEC3 D0N = D0;
 98 
 99     T c[3], d[3];
100 
101     for(unsigned int i=0;i<3;i++) {
102       d[i] = D1[i] - D0N[i]*(D0[0]*D1[0] + D0[1]*D1[1] + D0[2]*D1[2]);
103       c[i] = P1[i] - P0[i] + D0N[i]*(D0[0]*P0[0] + D0[1]*P0[1] + D0[2]*P0[2]);
104     }
105 
106     T den = d[0]*d[0]+d[1]*d[1]+d[2]*d[2];
107     if(den==T()) return false;
108 
109     T t = -(c[0]*d[0]+c[1]*d[1]+c[2]*d[2]) / den;
110 
111     a_on_line = a_line.m_pos + a_line.m_dir * t;
112     closest_point(a_on_line,a_on_this);
113     return true;
114   }
115 
116   bool intersect(const line<T>& a_line,VEC3& a_out,const T& a_prec) const {
117     VEC3 p,q;
118     if(!closest_points(a_line,p,q)) return false;
119     if((q-p).length()>a_prec) return false;
120     a_out = p;
121     return true;
122   }
123 */
124 
125 protected:
126   VEC3 m_pos;
127   VEC3 m_dir; //normalized.
128 };
129 
130 }
131 
132 #endif