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

gfx/Segment3

Go to the documentation of this file.
00001 /* **-*-c++-*-**************************************************************
00002   Copyright (C)1996 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: Segment3 1030 2004-02-11 20:46:17Z jungd $
00019   $Revision: 1.5 $
00020   $Date: 2004-02-11 15:46:17 -0500 (Wed, 11 Feb 2004) $
00021   $Author: jungd $
00022  
00023 ****************************************************************************/
00024 
00025 #ifndef _GFX_SEGMENT3_
00026 #define _GFX_SEGMENT3_
00027 
00028 #include <gfx/gfx>
00029 #include <base/Point3>
00030 #include <base/Vector3>
00031 #include <base/Transform>
00032 
00033 #include <iostream>
00034 
00035 
00036 namespace gfx {
00037 
00038 
00039 class Segment3
00040 {
00041 
00042 public:
00043   Segment3() {}
00044   Segment3(const Vector3& d) 
00045     : s(Point3()), e(d) {}
00046   Segment3(const Point3& s, const Point3& e)
00047     : s(s), e(e) {}
00048   Segment3(const Segment3& seg) 
00049     : s(seg.s), e(seg.e) {}
00050   ~Segment3() {}
00051   
00052   Segment3& operator=(const Segment3& seg)
00053     {
00054       if (&seg == this) return *this;
00055       s=seg.s;
00056       e=seg.e; 
00057       return *this; 
00058     }
00059   
00060   bool operator==(const Segment3& seg) const
00061     {
00062       if (&seg == this) return true;
00063       return (s==seg.s) && (e==seg.e);
00064     }
00065     
00066   bool operator!=(const Segment3& seg) const
00067     { 
00068       if (&seg == this) return false;
00069       return (s!=seg.s) || (e!=seg.e);
00070     }
00071     
00072   bool equals(const Segment3& seg, Real epsilon = consts::epsilon) const
00073     { 
00074       if (&seg == this) return true;
00075       return (s.equals(seg.s,epsilon) && e.equals(seg.e,epsilon));
00076     }
00077   
00078   
00079   Point3& operator[](Int i) { return (i==0)?s:e; }
00080   const Point3& operator[](Int i) const { return (i==0)?s:e; }
00081   
00082   Segment3& operator*=(Real s) { this->s*=s; e*=s; return *this; }
00083   Segment3& operator/=(Real s) { this->s/=s; e/=s; return *this; }
00084   
00085   void transform(const base::Transform& t)
00086   {
00087     s = t*s;
00088     e = t*e;
00089   }
00090 
00091   Real length() const { return (e-s).length(); }
00092   
00093   Real norm() const { return (e-s).norm(); }
00094   
00095   Real magnitude() const { return (e-s).magnitude(); }
00096   
00097   
00098   /// is point p on the segment?
00099   bool contains(const Point3& p) const;
00100 
00101   /// shortest distance between segment and p
00102   Real distanceTo(const Point3& p) const;
00103   
00104   /// shortest distance between this segment and segment g2
00105   Real distanceTo(const Segment3& s2) const
00106   { Real ds; shortestSegmentBetween(s2,ds); return Math::sqrt(ds); }
00107   
00108   /// find point on segment that is closest to p
00109   Point3 pointClosestTo(const Point3& p) const;
00110   
00111   
00112   /// return the shortest segment between this segment and segment s2
00113   Segment3 shortestSegmentBetween(const Segment3& s2) const
00114   { Real ds; return shortestSegmentBetween(s2,ds); }
00115   
00116   /// swap s & e
00117   Segment3& swapEnds() { Point3 os(s); s=e; e=os; return *this; }
00118 
00119   Point3 s, e;
00120   
00121 protected:
00122   Segment3 shortestSegmentBetween(const Segment3& s2, Real& ds) const;
00123   
00124   friend class Triangle3;
00125 };
00126 
00127 
00128 // Operations
00129 inline Segment3 operator*(const Segment3& seg, Real s)
00130 { Segment3 r(seg); r*=s; return r; }
00131 
00132 inline Segment3 operator*(Real s, const Segment3& seg)
00133 { Segment3 r(seg); r*=s; return r; }
00134 
00135 inline Segment3 operator/(const Segment3& seg, Real s)
00136 { Segment3 r(seg); r/=s; return r; }
00137 
00138 
00139 inline std::ostream& operator<<(std::ostream& out, const Segment3& s) // Output
00140 { return out << "[" << s.s << "-" << s.e << "]"; }
00141 
00142 
00143 } // gfx
00144 
00145 #endif

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