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 "Wm4IntrSegment3Plane3.h" 00019 #include "Wm4IntrLine3Plane3.h" 00020 00021 namespace Wm4 00022 { 00023 //---------------------------------------------------------------------------- 00024 template <class Real> 00025 IntrSegment3Plane3<Real>::IntrSegment3Plane3 (const Segment3<Real>& rkSegment, 00026 const Plane3<Real>& rkPlane) 00027 : 00028 m_rkSegment(rkSegment), 00029 m_rkPlane(rkPlane) 00030 { 00031 } 00032 //---------------------------------------------------------------------------- 00033 template <class Real> 00034 const Segment3<Real>& IntrSegment3Plane3<Real>::GetSegment () const 00035 { 00036 return m_rkSegment; 00037 } 00038 //---------------------------------------------------------------------------- 00039 template <class Real> 00040 const Plane3<Real>& IntrSegment3Plane3<Real>::GetPlane () const 00041 { 00042 return m_rkPlane; 00043 } 00044 //---------------------------------------------------------------------------- 00045 template <class Real> 00046 bool IntrSegment3Plane3<Real>::Test () 00047 { 00048 Vector3<Real> kP0 = m_rkSegment.GetNegEnd(); 00049 Real fSDistance0 = m_rkPlane.DistanceTo(kP0); 00050 if (Math<Real>::FAbs(fSDistance0) <= Math<Real>::ZERO_TOLERANCE) 00051 { 00052 fSDistance0 = (Real)0.0; 00053 } 00054 00055 Vector3<Real> kP1 = m_rkSegment.GetPosEnd(); 00056 Real fSDistance1 = m_rkPlane.DistanceTo(kP1); 00057 if (Math<Real>::FAbs(fSDistance1) <= Math<Real>::ZERO_TOLERANCE) 00058 { 00059 fSDistance1 = (Real)0.0; 00060 } 00061 00062 Real fProd = fSDistance0*fSDistance1; 00063 if (fProd < (Real)0.0) 00064 { 00065 // The segment passes through the plane. 00066 m_iIntersectionType = IT_POINT; 00067 return true; 00068 } 00069 00070 if (fProd > (Real)0.0) 00071 { 00072 // The segment is on one side of the plane. 00073 m_iIntersectionType = IT_EMPTY; 00074 return false; 00075 } 00076 00077 if (fSDistance0 != (Real)0.0 || fSDistance1 != (Real)0.0) 00078 { 00079 // A segment end point touches the plane. 00080 m_iIntersectionType = IT_POINT; 00081 return true; 00082 } 00083 00084 // The segment is coincident with the plane. 00085 m_iIntersectionType = IT_SEGMENT; 00086 return true; 00087 } 00088 //---------------------------------------------------------------------------- 00089 template <class Real> 00090 bool IntrSegment3Plane3<Real>::Find () 00091 { 00092 Line3<Real> kLine(m_rkSegment.Origin,m_rkSegment.Direction); 00093 IntrLine3Plane3<Real> kIntr(kLine,m_rkPlane); 00094 if (kIntr.Find()) 00095 { 00096 // The line intersects the plane, but possibly at a point that is 00097 // not on the segment. 00098 m_iIntersectionType = kIntr.GetIntersectionType(); 00099 m_fSegmentT = kIntr.GetLineT(); 00100 return Math<Real>::FAbs(m_fSegmentT) <= m_rkSegment.Extent; 00101 } 00102 00103 m_iIntersectionType = IT_EMPTY; 00104 return false; 00105 } 00106 //---------------------------------------------------------------------------- 00107 template <class Real> 00108 Real IntrSegment3Plane3<Real>::GetSegmentT () const 00109 { 00110 return m_fSegmentT; 00111 } 00112 //---------------------------------------------------------------------------- 00113 00114 //---------------------------------------------------------------------------- 00115 // explicit instantiation 00116 //---------------------------------------------------------------------------- 00117 template WM4_FOUNDATION_ITEM 00118 class IntrSegment3Plane3<float>; 00119 00120 template WM4_FOUNDATION_ITEM 00121 class IntrSegment3Plane3<double>; 00122 //---------------------------------------------------------------------------- 00123 }