Main Page | Namespace List | Class Hierarchy | Alphabetical List | Class List | File List | Namespace Members | Class Members | File Members | Related Pages

gfx/Line3

Go to the documentation of this file.
00001 /* **-*-c++-*-**************************************************************
00002   Copyright (C)2003 David Jung <opensim@pobox.com>
00003 
00004   This program/file is free software; you can redistribute it and/or modify
00005   it under the terms of the GNU General Public License as published by
00006   the Free Software Foundation; either version 2 of the License, or
00007   (at your option) any later version.
00008   
00009   This program is distributed in the hope that it will be useful,
00010   but WITHOUT ANY WARRANTY; without even the implied warranty of
00011   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00012   GNU General Public License for more details. (http://www.gnu.org)
00013   
00014   You should have received a copy of the GNU General Public License
00015   along with this program; if not, write to the Free Software
00016   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00017   
00018   $Id: Line3 1030 2004-02-11 20:46:17Z jungd $
00019   $Revision: 1.2 $
00020   $Date: 2004-02-11 15:46:17 -0500 (Wed, 11 Feb 2004) $
00021   $Author: jungd $
00022  
00023 ****************************************************************************/
00024 
00025 #ifndef _GFX_LINE3_
00026 #define _GFX_LINE3_
00027 
00028 #include <gfx/gfx>
00029 #include <base/Point3>
00030 #include <base/Vector3>
00031 #include <gfx/Segment3>
00032 
00033 #include <iostream>
00034 
00035 
00036 namespace gfx {
00037 
00038 
00039 class Line3
00040 {
00041 
00042 public:
00043   Line3() {}
00044   Line3(const Point3& origin, const Vector3& direction) 
00045     : o(origin), d(direction) { d.normalize(); }
00046   Line3(const Segment3& seg)
00047     : o(seg.s), d(seg.e-seg.s) { d.normalize(); }
00048   Line3(const Line3& l) 
00049     : o(l.o), d(l.d) {}
00050   ~Line3() {}
00051   
00052   Line3& operator=(const Line3& l)
00053     {
00054       if (&l == this) return *this;
00055       o=l.o;
00056       d=l.d;
00057       d.normalize();
00058       return *this; 
00059     }
00060   
00061   bool operator==(const Line3& l) const
00062     {
00063       if (&l == this) return true;
00064       return (o==l.o) && (d==l.d);
00065     }
00066     
00067   bool operator!=(const Line3& l) const
00068     { 
00069       if (&l == this) return false;
00070       return (o!=l.o) || (d!=l.d);
00071     }
00072     
00073   bool equals(const Line3& l, Real epsilon = consts::epsilon) const
00074     { 
00075       if (&l == this) return true;
00076       return (o.equals(l.o,epsilon) && d.equals(l.d,epsilon));
00077     }
00078   
00079   /// shortest distance between line and p
00080   Real distanceTo(const Point3& p) const
00081     {
00082       return cross(d,p-o).length();
00083     }
00084   
00085   
00086   /// shortest distance between line and line l2
00087   Real distanceTo(const Line3& l2) const
00088     { return shortestSegmentBetween(l2).length(); }
00089   
00090   /// find point on the line that is closest to p
00091   Point3 pointClosestTo(const Point3& p) const
00092     {
00093       Real u = dot(d,p-o);
00094       return o + u*d;
00095     }
00096   
00097   /// is point p on the line?
00098   bool contains(const Point3& p) const
00099     {
00100       if (p.equals(o)) return true;
00101 
00102       Point3 pn = (p-o).normalize();
00103       return pn.equals(d) || pn.equals(-d);
00104     }
00105   
00106   
00107   /// return shortest segment between this line and line l2
00108   Segment3 shortestSegmentBetween(const Line3& l2) const;
00109   
00110   
00111   Point3 o;  ///< origin
00112   Vector3 d; ///< direction
00113 };
00114 
00115 
00116 // Operations
00117 
00118 inline std::ostream& operator<<(std::ostream& out, const Line3& l) // Output
00119 { return out << "(origin:" << l.o << " dir:" << l.d << ")"; }
00120 
00121 
00122 } // gfx
00123 
00124 #endif

Generated on Thu Jul 29 15:56:19 2004 for OpenSim by doxygen 1.3.6