Common/Geom/SegAlg.h

Go to the documentation of this file.
00001 //-------------------------------------------------------------------
00004 //  Copyright (c) 2007 Celeritive Technologies, Inc.
00005 //
00006 // This library is free software; you can redistribute it and/or
00007 // modify it under the terms of the GNU Lesser General Public
00008 // License as published by the Free Software Foundation; either
00009 // version 2.1 of the License, or (at your option) any later version.
00010 //
00011 // This library is distributed in the hope that it will be useful,
00012 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00013 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
00014 // Lesser General Public License for more details.
00015 //
00016 // You should have received a copy of the GNU Lesser General Public
00017 // License along with this library; if not, write to the Free Software
00018 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
00019 //
00020 //-------------------------------------------------------------------
00021 
00022 #pragma once
00023 #ifndef VOLUMILL_SEGALG_H
00024 #define VOLUMILL_SEGALG_H
00025 
00026 #include <Geom/Points.h>
00027 #include <Geom/PointAlg.h>
00028 
00029 namespace geom
00030 {
00032    template<class P>
00033    P projectPointToLine (
00034       const P& startPt,
00035       const P& endPt,
00036       const P& pt)
00037    {
00038       VecNd<P::Dim> u (endPt-startPt);
00039       VecNd<P::Dim> v (pt-startPt);
00040       double denom = u * u;
00041       VALIDATE (denom > util::doubleEps * util::doubleEps);
00042       double param = (u * v) / denom;
00043       return geom::linComb (1-param, startPt, param, endPt);
00044    }
00045 
00049    template<class P>
00050    bool projectPointToSegment (
00051       const P& startPt,
00052       const P& endPt,
00053       const P& pt,
00054       double tol,
00055       P* pProjPt = 0,
00056       double* pParam = 0)
00057    {
00058       VecNd<P::Dim> u (endPt-startPt);
00059       VecNd<P::Dim> v (pt-startPt);
00060       double uLen = length (u);
00061       VALIDATE (uLen > util::doubleEps);
00062       double param = (u * v) / uLen;
00063       if (param < tol)
00064       {
00065          util::assignPtr (pParam, 0.0);
00066          util::assignPtr (pProjPt, startPt);
00067          return param > -tol;
00068       }
00069       else if (param > uLen-tol)
00070       {
00071          util::assignPtr (pParam, 1.0);
00072          util::assignPtr (pProjPt, endPt);
00073          return param < uLen + tol;
00074       }
00075       param /= uLen;
00076       util::assignPtr (pParam, param);
00077       util::assignPtr (pProjPt, geom::linComb (1-param, startPt, param, endPt));
00078       return true;
00079    }
00080 
00083    template<class P>
00084    int getOrientation (
00085       const P& p0,
00086       const P& p1,
00087       const P& pt,
00088       double tol)
00089    {
00090       BOOST_STATIC_ASSERT (P::Dim == 2);
00091       VecNd<P::Dim> u (p1-p0);
00092       VecNd<P::Dim> v (pt-p1);
00093       double uLen = length (u);
00094       VALIDATE (uLen > util::doubleEps);
00095       double cross = crossProduct (u, v) / uLen;
00096       if (cross < -tol)
00097          return -1;
00098       else if (cross > tol)
00099          return 1;
00100       return 0;
00101    }
00102 }
00103 #endif

Generated on Tue Jan 29 21:37:56 2008 for VoluMill Universal Client by  doxygen 1.4.6