AI_Line.h

Go to the documentation of this file.
00001 #ifndef AI_LINE_H
00002 #define AI_LINE_H
00003 //-------------------------------------------------------------------
00010 #include "AI_Vector.h"
00011 
00012 class AI_Line2 {
00013 public:
00014     AI_Vector2 b;
00015     AI_Vector2 m;
00016 
00017     AI_Line2() {};
00018     AI_Line2(const AI_Vector2& v0, const AI_Vector2& v1) : b(v0), m(v1-v0) {};
00019     AI_Line2(const AI_Line2& l) : b(l.b), m(l.m) {};
00020 
00021     //--- minimal distance of point to line -------------------------
00022     float distance(const AI_Vector2& p)
00023     {
00024         AI_Vector2 diff(p-b);
00025         float l = (m % m);
00026         if (l > 0.0f) {
00027             float t = (m % diff) / l;
00028             return (diff - m*t).len();
00029         } else {
00030             // line is really a point...
00031             return (p-b).len();
00032         }
00033     };
00034     //--- minimal distance of point to line segment -----------------
00035     float distance(const AI_Vector2& p, const float t0, const float t1)
00036     {
00037         AI_Vector2 diff(p-b);
00038         float l = (m % m);
00039         if (l > 0.0f) 
00040         {
00041             float t = (m % diff) / l;
00042 
00043             if (t<t0)
00044                 return (diff - m*t0).len();
00045             else if (t>t1)
00046                 return (diff - m*t1).len();
00047             else 
00048                 return (diff - m*t).len();
00049         } 
00050         else 
00051         {
00052             // line is really a point...
00053             return (p-b).len();
00054         }
00055     };
00056     void set (const AI_Vector2& v0, const AI_Vector2& v1)
00057     {
00058         b.set(v0);
00059         m.set(v1-v0);
00060     };
00061     const AI_Vector2& start(void) const
00062     {
00063         return b;
00064     };
00065     AI_Vector2 end(void) const
00066     {
00067         return (b+m);
00068     };
00069     float len(void) const
00070     {
00071         return m.len();
00072     };
00073     //--- get 2d point on line given t ------------------------------
00074     AI_Vector2 ipol(const float t) const
00075     {
00076         return AI_Vector2(b + m*t);
00077     };
00078     bool intersection(const AI_Line2 &other, float tol) const
00079     {
00080         AI_Vector2 params;
00081         if (intersection(other, params, tol))
00082         {
00083             float min = -tol;
00084             float max = 1.0f+tol;
00085             if (params.x >= min && params.x <= max &&
00086                 params.y >= min && params.y <= max)
00087                 return true;
00088         }
00089         return false;
00090     };
00091     bool intersection(const AI_Line2 &other, AI_Vector2 &params, float tol) const
00092     {
00093         float D = other.m.x * m.y - m.x * other.m.y;
00094         if ( ai_fequal(D, 0.0f, tol) )
00095             return false;
00096         
00097         float del1 = other.b.x - b.x;
00098         float del2 = other.b.y - b.y;
00099 
00100         float D1 = other.m.x * del2 - del1 * other.m.y;
00101         float D2 = m.x * del2 - del1 * m.y;
00102 
00103         params.x = D1 / D;
00104         params.y = D2 / D;
00105 
00106         return true;
00107     }
00108 
00109 };
00110 
00119 //-------------------------------------------------------------------
00120 class AI_Line3 {
00121 public:
00122     AI_Vector3 b;
00123     AI_Vector3 m;
00124 
00125     AI_Line3() {};
00126     AI_Line3(const AI_Vector3& v0, const AI_Vector3& v1) : b(v0), m(v1-v0) {};
00127     AI_Line3(const AI_Line3& l) : b(l.b), m(l.m) {};
00128 
00129     void set(const AI_Vector3& v0, const AI_Vector3& v1)
00130     {
00131         b = v0;
00132         m = v1-v0;
00133     };
00134     const AI_Vector3& start(void) const
00135     {
00136         return b;
00137     };
00138     AI_Vector3 end(void) const
00139     {
00140         return (b+m);
00141     };
00142     float len(void) const
00143     {
00144         return m.len();
00145     };
00146     float len_squared(void) const
00147     {
00148         return m.lensquared();
00149     }
00150     //--- minimal distance of point to line -------------------------
00151     float distance(const AI_Vector3& p) {
00152         AI_Vector3 diff(p-b);
00153         float l = (m % m);
00154         if (l > 0.0f) {
00155             float t = (m % diff) / l;
00156             diff = diff - m*t;
00157             return diff.len();
00158         } else {
00159             // line is really a point...
00160             AI_Vector3 v(p-b);
00161             return v.len();
00162         }
00163     };
00164 
00165     //--- get 3d point on line given t ------------------------------
00166     AI_Vector3 ipol(const float t) const
00167     {
00168         return AI_Vector3(b + m*t);
00169     };
00170 };
00171 
00172 //-------------------------------------------------------------------
00173 #endif