Wm4DistSegment3Triangle3.cpp

Go to the documentation of this file.
00001 // Wild Magic Source Code
00002 // David Eberly
00003 // http://www.geometrictools.com
00004 // Copyright (c) 1998-2007
00005 //
00006 // This library is free software; you can redistribute it and/or modify it
00007 // under the terms of the GNU Lesser General Public License as published by
00008 // the Free Software Foundation; either version 2.1 of the License, or (at
00009 // your option) any later version.  The license is available for reading at
00010 // either of the locations:
00011 //     http://www.gnu.org/copyleft/lgpl.html
00012 //     http://www.geometrictools.com/License/WildMagicLicense.pdf
00013 // The license applies to versions 0 through 4 of Wild Magic.
00014 //
00015 // Version: 4.0.0 (2006/06/28)
00016 
00017 #include "Wm4FoundationPCH.h"
00018 #include "Wm4DistSegment3Triangle3.h"
00019 #include "Wm4DistLine3Triangle3.h"
00020 #include "Wm4DistVector3Triangle3.h"
00021 
00022 namespace Wm4
00023 {
00024 //----------------------------------------------------------------------------
00025 template <class Real>
00026 DistSegment3Triangle3<Real>::DistSegment3Triangle3 (
00027     const Segment3<Real>& rkSegment, const Triangle3<Real>& rkTriangle)
00028     :
00029     m_rkSegment(rkSegment),
00030     m_rkTriangle(rkTriangle)
00031 {
00032 }
00033 //----------------------------------------------------------------------------
00034 template <class Real>
00035 const Segment3<Real>& DistSegment3Triangle3<Real>::GetSegment () const
00036 {
00037     return m_rkSegment;
00038 }
00039 //----------------------------------------------------------------------------
00040 template <class Real>
00041 const Triangle3<Real>& DistSegment3Triangle3<Real>::GetTriangle () const
00042 {
00043     return m_rkTriangle;
00044 }
00045 //----------------------------------------------------------------------------
00046 template <class Real>
00047 Real DistSegment3Triangle3<Real>::Get ()
00048 {
00049     Real fSqrDist = GetSquared();
00050     return Math<Real>::Sqrt(fSqrDist);
00051 }
00052 //----------------------------------------------------------------------------
00053 template <class Real>
00054 Real DistSegment3Triangle3<Real>::GetSquared ()
00055 {
00056     DistLine3Triangle3<Real> kLTDist(Line3<Real>(m_rkSegment.Origin,
00057         m_rkSegment.Direction),m_rkTriangle);
00058 
00059     Real fSqrDist;
00060 
00061     m_fSegmentParameter = kLTDist.GetLineParameter();
00062     if (m_fSegmentParameter >= -m_rkSegment.Extent)
00063     {
00064         if (m_fSegmentParameter <= m_rkSegment.Extent)
00065         {
00066             fSqrDist = kLTDist.GetSquared();
00067             m_kClosestPoint0 = kLTDist.GetClosestPoint0();
00068             m_kClosestPoint1 = kLTDist.GetClosestPoint1();
00069             m_afTriangleBary[0] = kLTDist.GetTriangleBary(0);
00070             m_afTriangleBary[1] = kLTDist.GetTriangleBary(1);
00071             m_afTriangleBary[2] = kLTDist.GetTriangleBary(2);
00072         }
00073         else
00074         {
00075             m_kClosestPoint0 = m_rkSegment.GetPosEnd();
00076             DistVector3Triangle3<Real> kVTDist(m_kClosestPoint0,m_rkTriangle);
00077             fSqrDist = kVTDist.GetSquared();
00078             m_kClosestPoint1 = kVTDist.GetClosestPoint1();
00079             m_fSegmentParameter = m_rkSegment.Extent;
00080             m_afTriangleBary[0] = kVTDist.GetTriangleBary(0);
00081             m_afTriangleBary[1] = kVTDist.GetTriangleBary(1);
00082             m_afTriangleBary[2] = kVTDist.GetTriangleBary(2);
00083         }
00084     }
00085     else
00086     {
00087         m_kClosestPoint0 = m_rkSegment.GetNegEnd();
00088         DistVector3Triangle3<Real> kVTDist(m_kClosestPoint0,m_rkTriangle);
00089         fSqrDist = kVTDist.GetSquared();
00090         m_kClosestPoint1 = kVTDist.GetClosestPoint1();
00091         m_fSegmentParameter = -m_rkSegment.Extent;
00092         m_afTriangleBary[0] = kVTDist.GetTriangleBary(0);
00093         m_afTriangleBary[1] = kVTDist.GetTriangleBary(1);
00094         m_afTriangleBary[2] = kVTDist.GetTriangleBary(2);
00095     }
00096 
00097     return fSqrDist;
00098 }
00099 //----------------------------------------------------------------------------
00100 template <class Real>
00101 Real DistSegment3Triangle3<Real>::Get (Real fT,
00102     const Vector3<Real>& rkVelocity0, const Vector3<Real>& rkVelocity1)
00103 {
00104     Vector3<Real> kMOrigin = m_rkSegment.Origin + fT*rkVelocity0;
00105     Vector3<Real> kMV0 = m_rkTriangle.V[0] + fT*rkVelocity1;
00106     Vector3<Real> kMV1 = m_rkTriangle.V[1] + fT*rkVelocity1;
00107     Vector3<Real> kMV2 = m_rkTriangle.V[2] + fT*rkVelocity1;
00108     Segment3<Real> kMSegment(kMOrigin,m_rkSegment.Direction,
00109         m_rkSegment.Extent);
00110     Triangle3<Real> kMTriangle(kMV0,kMV1,kMV2);
00111     return DistSegment3Triangle3<Real>(kMSegment,kMTriangle).Get();
00112 }
00113 //----------------------------------------------------------------------------
00114 template <class Real>
00115 Real DistSegment3Triangle3<Real>::GetSquared (Real fT,
00116     const Vector3<Real>& rkVelocity0, const Vector3<Real>& rkVelocity1)
00117 {
00118     Vector3<Real> kMOrigin = m_rkSegment.Origin + fT*rkVelocity0;
00119     Vector3<Real> kMV0 = m_rkTriangle.V[0] + fT*rkVelocity1;
00120     Vector3<Real> kMV1 = m_rkTriangle.V[1] + fT*rkVelocity1;
00121     Vector3<Real> kMV2 = m_rkTriangle.V[2] + fT*rkVelocity1;
00122     Segment3<Real> kMSegment(kMOrigin,m_rkSegment.Direction,
00123         m_rkSegment.Extent);
00124     Triangle3<Real> kMTriangle(kMV0,kMV1,kMV2);
00125     return DistSegment3Triangle3<Real>(kMSegment,kMTriangle).GetSquared();
00126 }
00127 //----------------------------------------------------------------------------
00128 template <class Real>
00129 Real DistSegment3Triangle3<Real>::GetSegmentParameter () const
00130 {
00131     return m_fSegmentParameter;
00132 }
00133 //----------------------------------------------------------------------------
00134 template <class Real>
00135 Real DistSegment3Triangle3<Real>::GetTriangleBary (int i) const
00136 {
00137     assert(0 <= i && i < 3);
00138     return m_afTriangleBary[i];
00139 }
00140 //----------------------------------------------------------------------------
00141 
00142 //----------------------------------------------------------------------------
00143 // explicit instantiation
00144 //----------------------------------------------------------------------------
00145 template WM4_FOUNDATION_ITEM
00146 class DistSegment3Triangle3<float>;
00147 
00148 template WM4_FOUNDATION_ITEM
00149 class DistSegment3Triangle3<double>;
00150 //----------------------------------------------------------------------------
00151 }

Generated on Wed Nov 23 19:01:03 2011 for FreeCAD by  doxygen 1.6.1