proj home

Files   Classes   Functions   Hierarchy  

line< PT, PD > Class Template Reference

Line class for 2 and 3 dimensional lines. More...

#include <line.h>

Collaboration diagram for line< PT, PD >:

List of all members.

Public Member Functions

 line (PT const &nml_, PT const &pos_)
 Construct a line from the gradient and an initial point.
 line (PT const &p0, PT const &p1, boolc endpoints)
 Construct a line from the end points.
void construct (PT const &nml_, PT const &pos_)
 Construc the line pos+nml*t.
void constructfromendpoints (PT const &p0, PT const &p1)
 Given two points construct the line (p0,p1).
void normalize ()
 Normalize the gradient.
void normalize (PD const len)
 Normalize the gradient to the given length.
PT const operator() (PD const t) const
 Line as a parametric equation.
boolc tD2 (PD &t, PT const &p) const
 Give a 2D point on the line find its position.
boolc tD3 (PD &t, PT const &p) const
 Give a 3D point on the line find its position.
 operator stringc () const
 Serialize this object by writing it out as a string.
boolc isnormalzero () const
 Test for normal before using functions that may divide by zero.
void nearestpoint (PD &t, PT const &p) const
 Find the nearest point on the line to p, return the t value.
void nearestpointcapped (PD &t, PT const &p) const
 Find the nearest point to the line segment.
void nearestpoint (PT &q, PT const &p) const
 Find the nearest point q on the line to p.
boolc lineD3minimized (PD &t1, PD &t2, line< PT, PD > const &L2) const
 Minimize two points on lines, t1 is this line and t2 is line L2.
boolc intersects (PD &t0, PD &t1, line< PT, PD > const &L2)
 Find the two lines intersection.

Static Public Member Functions

static void nearestpointonline (PT &x, PT const &p, PT const &a, PT const &b)
 Minimize the distance to point p from point x on line a+b*t.

Public Attributes

PT nml
 The lines gradient.
PT pos
 The start of the line.


Detailed Description

template<typename PT, typename PD>
class line< PT, PD >

Line class for 2 and 3 dimensional lines.

Optimization Warning: Use this class with caution when optimizing code. This is because the inlining can have a significant impact on performance in a negative way when not done correctly, which is easy to do.

Logic for nml==0 is not supported but this may change.

Definition at line 21 of file line.h.


Constructor & Destructor Documentation

template<typename PT , typename PD >
line< PT, PD >::line ( PT const &  nml_,
PT const &  pos_ 
) [inline]

Construct a line from the gradient and an initial point.

Definition at line 181 of file line.h.

00182   :nml(nml_), pos(pos_) 
00183 {
00184 }

template<typename PT , typename PD >
line< PT, PD >::line ( PT const &  p0,
PT const &  p1,
boolc  endpoints 
) [inline]

Construct a line from the end points.

Definition at line 174 of file line.h.

00175   : nml(p1-p0), pos(p0) 
00176 { 
00177   assert(endpoints); 
00178 }


Member Function Documentation

template<typename PT , typename PD >
void line< PT, PD >::construct ( PT const &  nml_,
PT const &  pos_ 
) [inline]

Construc the line pos+nml*t.

Definition at line 167 of file line.h.

References line< PT, PD >::nml, and line< PT, PD >::pos.

00168 { 
00169   nml = nml_; 
00170   pos=pos_; 
00171 }

template<typename PT, typename PD>
void line< PT, PD >::constructfromendpoints ( PT const &  p0,
PT const &  p1 
) [inline]

Given two points construct the line (p0,p1).

Definition at line 40 of file line.h.

References line< PT, PD >::nml, and line< PT, PD >::pos.

00041     { pos=p0; nml=(p1-p0); }

template<typename PT , typename PD >
boolc line< PT, PD >::intersects ( PD &  t0,
PD &  t1,
line< PT, PD > const &  L2 
) [inline]

Find the two lines intersection.

Definition at line 116 of file line.h.

References solver< T >::d2linearequ(), line< PT, PD >::nml, and line< PT, PD >::pos.

Referenced by linetest::unittest01(), and bsptreeD2< PT, PD, INDX >::validhalfspaces().

00121 {
00122   return solver<PD>::d2linearequ
00123   (
00124     t0,
00125     t1,
00126     nml.x,
00127     L2.nml.x*((PD)-1.0),
00128     nml.y,
00129     L2.nml.y*((PD)-1.0),
00130     L2.pos.x-pos.x,
00131     L2.pos.y-pos.y
00132   );
00133 }

template<typename PT, typename PD>
boolc line< PT, PD >::isnormalzero (  )  const [inline]

Test for normal before using functions that may divide by zero.

Definition at line 63 of file line.h.

References line< PT, PD >::nml.

Referenced by pathlinesegvec::dist01(), and pathlineseg::dist01().

00064     { return zero<PD>::test(nml.dot()); }

template<typename PT , typename PD >
boolc line< PT, PD >::lineD3minimized ( PD &  t1,
PD &  t2,
line< PT, PD > const &  L2 
) const [inline]

Minimize two points on lines, t1 is this line and t2 is line L2.

Definition at line 143 of file line.h.

References solver< T >::d2linearequ(), line< PT, PD >::nml, and line< PT, PD >::pos.

Referenced by linetest::test02(), and diskinttest::update03lineline().

00148 {
00149   PT C(pos-L2.pos);
00150   PT E1(nml);
00151   PT E2(L2.nml*(-1));
00152 
00153   bool res;
00154   res = solver<PD>::d2linearequ
00155   (
00156     t1,t2,
00157     E1.dot(),E1.dot(E2),
00158     E1.dot(E2),E2.dot(),
00159     C.dot(E1)*(-1),
00160     C.dot(E2)*(-1)
00161   );
00162 
00163   return res;
00164 }

template<typename PT , typename PD >
void line< PT, PD >::nearestpoint ( PT &  q,
PT const &  p 
) const [inline]

Find the nearest point q on the line to p.

Assumes the gradient nml is not zero.

Definition at line 257 of file line.h.

References line< PT, PD >::nml, and line< PT, PD >::pos.

00258 {
00259   assert(zero<PD>::test(nml.dot())==false);
00260 
00261   q = pos;
00262   q += nml*(p-pos).dot(nml)/nml.dot();
00263 }

template<typename PT , typename PD >
void line< PT, PD >::nearestpoint ( PD &  t,
PT const &  p 
) const [inline]

Find the nearest point on the line to p, return the t value.

Assumes the gradient nml is not zero. The line points t=0,1 <--> (pos,pos+nml).

Definition at line 239 of file line.h.

References line< PT, PD >::nml, and line< PT, PD >::pos.

Referenced by line< PT, PD >::nearestpointcapped(), and linetest::test01().

00240 {
00241   assert(zero<PD>::test(nml.dot())==false);
00242 
00243   t=(p-pos).dot(nml)/nml.dot();
00244 }

template<typename PT , typename PD >
void line< PT, PD >::nearestpointcapped ( PD &  t,
PT const &  p 
) const [inline]

Find the nearest point to the line segment.

The line points t=0,1 <--> (pos,pos+nml).

Definition at line 247 of file line.h.

References line< PT, PD >::nearestpoint().

Referenced by pathlinesegvec::dist01(), pathlineseg::dist01(), and circleD2< PT, PD >::intersects().

00248 {
00249   nearestpoint(t,p);
00250   if (t<((PD)0.0))
00251     t = (PD)0.0;
00252   if (t>((PD)1.0))
00253     t = (PD)1.0;
00254 }

template<typename PT , typename PD >
void line< PT, PD >::nearestpointonline ( PT &  x,
PT const &  p,
PT const &  a,
PT const &  b 
) [inline, static]

Minimize the distance to point p from point x on line a+b*t.

Definition at line 267 of file line.h.

Referenced by triangle3D< PT, PD >::innercircle(), and triangle< PT, PD >::innercircle().

00273 {  
00274   assert(zero<PD>::test(b.dot())==false);
00275   x = a + b*( (p-a).dot(b)/(b.dot(b)) );
00276 }

template<typename PT , typename PD >
void line< PT, PD >::normalize ( PD const   len  )  [inline]

Normalize the gradient to the given length.

Definition at line 193 of file line.h.

References line< PT, PD >::nml.

00194 { 
00195   nml.normalize(); 
00196   nml *= len; 
00197 }

template<typename PT , typename PD >
void line< PT, PD >::normalize (  )  [inline]

Normalize the gradient.

Definition at line 187 of file line.h.

References line< PT, PD >::nml.

Referenced by linetest::test01(), and diskinttest::update03lineline().

00188 { 
00189   nml.normalize(); 
00190 }

template<typename PT , typename PD >
line< PT, PD >::operator stringc (  )  const [inline]

Serialize this object by writing it out as a string.

Definition at line 136 of file line.h.

References line< PT, PD >::nml, and line< PT, PD >::pos.

00137 {
00138   return (stringc)nml + " " + (stringc)pos;
00139 }

template<typename PT, typename PD>
PT const line< PT, PD >::operator() ( PD const   t  )  const [inline]

Line as a parametric equation.

Definition at line 50 of file line.h.

References line< PT, PD >::nml, and line< PT, PD >::pos.

00051     { return pos + nml*t; }

template<typename PT , typename PD >
boolc line< PT, PD >::tD2 ( PD &  t,
PT const &  p 
) const [inline]

Give a 2D point on the line find its position.

Definition at line 200 of file line.h.

References line< PT, PD >::nml, and line< PT, PD >::pos.

Referenced by linetest::test01().

00201 {
00202   if (zero<PD>::test(nml.x))
00203   {
00204     if (zero<PD>::test(nml.y))
00205       return false;
00206 
00207     t = (p.y-pos.y)/nml.y;
00208   }
00209   else
00210     t = (p.x-pos.x)/nml.x;
00211 
00212   assert( zero<PD>::test( (operator()(t)-p).dot()) );
00213   return true;
00214 }

template<typename PT , typename PD >
boolc line< PT, PD >::tD3 ( PD &  t,
PT const &  p 
) const [inline]

Give a 3D point on the line find its position.

Definition at line 217 of file line.h.

References line< PT, PD >::nml, and line< PT, PD >::pos.

00218 {
00219   if (zero<PD>::test(nml.x))
00220   {
00221     if (zero<PD>::test(nml.y))
00222     {
00223       if (zero<PD>::test(nml.z))
00224         return false;
00225 
00226       t = (p.z-pos.z)/nml.z;
00227     }
00228     else
00229       t = (p.y-pos.y)/nml.y;
00230   }
00231   else
00232     t = (p.x-pos.x)/nml.x;
00233 
00234   assert( zero<PD>::test( (operator()(t)-p).dot()) );
00235   return true;
00236 }


Member Data Documentation

template<typename PT, typename PD>
PT line< PT, PD >::nml

template<typename PT, typename PD>
PT line< PT, PD >::pos


The documentation for this class was generated from the following file:

Generated on Fri Mar 4 00:50:04 2011 for Chelton Evans Source by  doxygen 1.5.8