library of assembled shared sources

http://lass.cocamware.com

line_segment_3d_ray_3d.h

Go to the documentation of this file.
00001 /** @file
00002  *  @author Bram de Greve (bramz@users.sourceforge.net)
00003  *  @author Tom De Muer (tomdemuer@users.sourceforge.net)
00004  *
00005  *  *** BEGIN LICENSE INFORMATION ***
00006  *  
00007  *  The contents of this file are subject to the Common Public Attribution License 
00008  *  Version 1.0 (the "License"); you may not use this file except in compliance with 
00009  *  the License. You may obtain a copy of the License at 
00010  *  http://lass.sourceforge.net/cpal-license. The License is based on the 
00011  *  Mozilla Public License Version 1.1 but Sections 14 and 15 have been added to cover 
00012  *  use of software over a computer network and provide for limited attribution for 
00013  *  the Original Developer. In addition, Exhibit A has been modified to be consistent 
00014  *  with Exhibit B.
00015  *  
00016  *  Software distributed under the License is distributed on an "AS IS" basis, WITHOUT 
00017  *  WARRANTY OF ANY KIND, either express or implied. See the License for the specific 
00018  *  language governing rights and limitations under the License.
00019  *  
00020  *  The Original Code is LASS - Library of Assembled Shared Sources.
00021  *  
00022  *  The Initial Developer of the Original Code is Bram de Greve and Tom De Muer.
00023  *  The Original Developer is the Initial Developer.
00024  *  
00025  *  All portions of the code written by the Initial Developer are:
00026  *  Copyright (C) 2004-2007 the Initial Developer.
00027  *  All Rights Reserved.
00028  *  
00029  *  Contributor(s):
00030  *
00031  *  Alternatively, the contents of this file may be used under the terms of the 
00032  *  GNU General Public License Version 2 or later (the GPL), in which case the 
00033  *  provisions of GPL are applicable instead of those above.  If you wish to allow use
00034  *  of your version of this file only under the terms of the GPL and not to allow 
00035  *  others to use your version of this file under the CPAL, indicate your decision by 
00036  *  deleting the provisions above and replace them with the notice and other 
00037  *  provisions required by the GPL License. If you do not delete the provisions above,
00038  *  a recipient may use your version of this file under either the CPAL or the GPL.
00039  *  
00040  *  *** END LICENSE INFORMATION ***
00041  */
00042 
00043 #ifndef LASS_GUARDIAN_OF_INCLUSION_PRIM_LINE_SEGMENT_3D_RAY_3D_H
00044 #define LASS_GUARDIAN_OF_INCLUSION_PRIM_LINE_SEGMENT_3D_RAY_3D_H
00045 
00046 #include "prim_common.h"
00047 #include "line_segment_3d.h"
00048 #include "ray_3d.h"
00049 
00050 namespace lass
00051 {
00052 namespace prim
00053 {
00054 
00055 
00056 template<typename T, class PP1, class NP2, class PP2>
00057 T distance(
00058         const LineSegment3D<T, PP1>& lineSegment, const Ray3D<T, NP2, PP2>& ray, 
00059         const T& tMin = T())
00060 {
00061     return num::sqrt(squaredDistance(lineSegment, ray, tMin));
00062 }
00063 
00064 template<typename T, class PP1, class NP2, class PP2>
00065 T squaredDistance(
00066         const LineSegment3D<T, PP1>& lineSegment, const Ray3D<T, NP2, PP2>& ray, 
00067         const T& tMin = T())
00068 { 
00069     typedef typename LineSegment3D<T, PP1>::TValue TValue;
00070     typedef Point3D<TValue> TPoint;
00071     typedef Vector3D<TValue> TVector;
00072 
00073     const TPoint S1 = ray.support();
00074     const TVector D = ray.direction();
00075     const TPoint R = lineSegment.tail();
00076     const TVector E = lineSegment.vector();
00077 
00078     TVector SR = R - S1;
00079     const TVector N = cross(D,E);
00080     if(N.squaredNorm() == 0)
00081         return squaredDistance(ray.project(R), R);
00082     const TPoint S = S1 + N.project(SR);
00083     SR = R - S;
00084 
00085     TValue tRay, tSeg;
00086 
00087     // cramer
00088     if(num::abs(N.z) > num::abs(N.x) &&
00089        num::abs(N.z) > num::abs(N.y))
00090     {
00091         tRay = (SR.x * E.y - SR.y * E.x) / N.z;
00092         tSeg = (SR.x * D.y - SR.y * D.x) / N.z;
00093     }
00094     else if(num::abs(N.x) > num::abs(N.y))
00095     {
00096         tRay = (SR.y * E.z - SR.z * E.y) / N.x;
00097         tSeg = (SR.y * D.z - SR.z * D.y) / N.x;
00098     }else
00099     {
00100         tRay = (SR.z * E.x - SR.x * E.z) / N.y;
00101         tSeg = (SR.z * D.x - SR.x * D.z) / N.y; 
00102     }
00103 
00104     if(tSeg > 1)
00105     {
00106         TValue tHead = ray.t(lineSegment.head());
00107         if(tHead > tMin)
00108             return squaredDistance(ray.point(tHead), lineSegment.head());
00109         else
00110         {
00111             tSeg = lineSegment.t(S1);
00112             if(tSeg < 0)
00113                 return squaredDistance(S1, lineSegment.tail());
00114             if(tSeg > 1)
00115                 return squaredDistance(S1, lineSegment.head());
00116             return squaredDistance(S1, lineSegment.point(tSeg));
00117         }
00118     }
00119     if(tSeg < 0)
00120     {
00121         TValue tTail = ray.t(R);
00122         if(tTail > tMin)
00123             return squaredDistance(ray.point(tTail), R);
00124         else
00125         {
00126             tSeg = lineSegment.t(S1);
00127             if(tSeg < 0)
00128                 return squaredDistance(S1, lineSegment.tail());
00129             if(tSeg > 1)
00130                 return squaredDistance(S1, lineSegment.head());
00131             return squaredDistance(S1, lineSegment.point(tSeg));
00132         }
00133     }
00134 
00135     if(tRay > tMin)
00136         return squaredDistance(ray.point(tRay), lineSegment.point(tSeg));
00137     
00138     return lineSegment.squaredDistance(S1);
00139 }
00140 
00141 template<typename T, class PP1, class NP2, class PP2>
00142 T closestsPoints(
00143         const LineSegment3D<T, PP1>& lineSegment, const Ray3D<T, NP2, PP2>& ray,
00144         T &tSeg, T &tRay, const T& tMin = T())
00145 {
00146     typedef typename LineSegment3D<T, PP1>::TValue TValue;
00147     typedef Point3D<TValue> TPoint;
00148     typedef Vector3D<TValue> TVector;
00149 
00150     const TPoint S1 = ray.support();
00151     const TVector D = ray.direction();
00152     const TPoint R = lineSegment.tail();
00153     const TVector E = lineSegment.vector();
00154 
00155     TVector SR = R - S1;
00156     const TVector N = cross(D,E);
00157     if(N.squaredNorm() == 0)
00158         return squaredDistance(ray.project(R), R);
00159     const TPoint S = S1 + N.project(SR);
00160     SR = R - S;
00161 
00162     // cramer
00163     if(num::abs(N.z) > num::abs(N.x) &&
00164        num::abs(N.z) > num::abs(N.y))
00165     {
00166         tRay = (SR.x * E.y - SR.y * E.x) / N.z;
00167         tSeg = (SR.x * D.y - SR.y * D.x) / N.z;
00168     }
00169     else if(num::abs(N.x) > num::abs(N.y))
00170     {
00171         tRay = (SR.y * E.z - SR.z * E.y) / N.x;
00172         tSeg = (SR.y * D.z - SR.z * D.y) / N.x;
00173     }else
00174     {
00175         tRay = (SR.z * E.x - SR.x * E.z) / N.y;
00176         tSeg = (SR.z * D.x - SR.x * D.z) / N.y; 
00177     }
00178 
00179     if(tSeg > 1)
00180     {
00181         TValue tHead = ray.t(lineSegment.head());
00182         if(tHead > tMin)
00183         {
00184             tRay = tHead;
00185             tSeg = 1.0;
00186             return squaredDistance(ray.point(tHead), lineSegment.head());
00187         }
00188         else
00189         {
00190             tSeg = lineSegment.t(S1);
00191             tRay = 0.0;
00192             if(tSeg < 0)
00193             {
00194                 tSeg = 0.0;
00195                 return squaredDistance(S1, lineSegment.tail());
00196             }
00197             if(tSeg > 1)
00198             {
00199                 tSeg = 1.0;
00200                 return squaredDistance(S1, lineSegment.head());
00201             }
00202             return squaredDistance(S1, lineSegment.point(tSeg));
00203         }
00204     }
00205     if(tSeg < 0)
00206     {
00207         TValue tTail = ray.t(R);
00208         if(tTail > tMin)
00209         {
00210             tRay = tTail;
00211             return squaredDistance(ray.point(tTail), R);
00212         }
00213         else
00214         {
00215             tSeg = lineSegment.t(S1);
00216             tRay = 0.0;
00217             if(tSeg < 0)
00218             {
00219                 tSeg = 0.0;
00220                 return squaredDistance(S1, lineSegment.tail());
00221             }
00222             if(tSeg > 1)
00223             {
00224                 tSeg = 1.0;
00225                 return squaredDistance(S1, lineSegment.head());
00226             }
00227             return squaredDistance(S1, lineSegment.point(tSeg));
00228         }
00229     }
00230 
00231     if(tRay > tMin)
00232         return squaredDistance(ray.point(tRay), lineSegment.point(tSeg));
00233     
00234     tRay = 0.0;
00235     return lineSegment.closestsPoint(S1, tSeg);
00236 }
00237 
00238 }
00239 }
00240 
00241 #endif
00242 
00243 // EOF

Generated on Mon Nov 10 14:20:30 2008 for Library of Assembled Shared Sources by doxygen 1.5.7.1
SourceForge.net Logo