Wm4Query2TRational.inl

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 namespace Wm4
00018 {
00019 //----------------------------------------------------------------------------
00020 template <class Real>
00021 Query2TRational<Real>::Query2TRational (int iVQuantity,
00022     const Vector2<Real>* akVertex)
00023     :
00024     Query2<Real>(iVQuantity,akVertex)
00025 {
00026     m_akRVertex = WM4_NEW RVector[m_iVQuantity];
00027     m_abEvaluated = WM4_NEW bool[m_iVQuantity];
00028     memset(m_abEvaluated,0,m_iVQuantity*sizeof(bool));
00029 }
00030 //----------------------------------------------------------------------------
00031 template <class Real>
00032 Query2TRational<Real>::~Query2TRational ()
00033 {
00034     WM4_DELETE[] m_akRVertex;
00035     WM4_DELETE[] m_abEvaluated;
00036 }
00037 //----------------------------------------------------------------------------
00038 template <class Real>
00039 Query::Type Query2TRational<Real>::GetType () const
00040 {
00041     return Query::QT_RATIONAL;
00042 }
00043 //----------------------------------------------------------------------------
00044 template <class Real>
00045 int Query2TRational<Real>::ToLine (int i, int iV0, int iV1) const
00046 {
00047     int aiIndex[3] = { i, iV0, iV1 };
00048     Convert(3,aiIndex);
00049     return ToLine(m_akRVertex[i],iV0,iV1);
00050 }
00051 //----------------------------------------------------------------------------
00052 template <class Real>
00053 int Query2TRational<Real>::ToLine (const Vector2<Real>& rkP, int iV0, int iV1)
00054     const
00055 {
00056     RVector kRP;
00057     kRP[0] = Rational(rkP[0]);
00058     kRP[1] = Rational(rkP[1]);
00059     int aiIndex[2] = { iV0, iV1 };
00060     Convert(2,aiIndex);
00061     return ToLine(kRP,iV0,iV1);
00062 }
00063 //----------------------------------------------------------------------------
00064 template <class Real>
00065 int Query2TRational<Real>::ToTriangle (int i, int iV0, int iV1, int iV2)
00066     const
00067 {
00068     int aiIndex[4] = { i, iV0, iV1, iV2 };
00069     Convert(4,aiIndex);
00070     return ToTriangle(m_akRVertex[i],iV0,iV1,iV2);
00071 }
00072 //----------------------------------------------------------------------------
00073 template <class Real>
00074 int Query2TRational<Real>::ToTriangle (const Vector2<Real>& rkP, int iV0,
00075     int iV1, int iV2) const
00076 {
00077     RVector kRP;
00078     kRP[0] = Rational(rkP[0]);
00079     kRP[1] = Rational(rkP[1]);
00080     int aiIndex[3] = { iV0, iV1, iV2 };
00081     Convert(3,aiIndex);
00082     return ToTriangle(kRP,iV0,iV1,iV2);
00083 }
00084 //----------------------------------------------------------------------------
00085 template <class Real>
00086 int Query2TRational<Real>::ToCircumcircle (int i, int iV0, int iV1, int iV2)
00087     const
00088 {
00089     int aiIndex[4] = { i, iV0, iV1, iV2 };
00090     Convert(4,aiIndex);
00091     return ToCircumcircle(m_akRVertex[i],iV0,iV1,iV2);
00092 }
00093 //----------------------------------------------------------------------------
00094 template <class Real>
00095 int Query2TRational<Real>::ToCircumcircle (const Vector2<Real>& rkP,
00096     int iV0, int iV1, int iV2) const
00097 {
00098     RVector kRP;
00099     kRP[0] = Rational(rkP[0]);
00100     kRP[1] = Rational(rkP[1]);
00101     int aiIndex[3] = { iV0, iV1, iV2 };
00102     Convert(3,aiIndex);
00103     return ToCircumcircle(kRP,iV0,iV1,iV2);
00104 }
00105 //----------------------------------------------------------------------------
00106 template <class Real>
00107 void Query2TRational<Real>::Convert (int iQuantity, int* aiIndex) const
00108 {
00109     for (int i = 0; i < iQuantity; i++)
00110     {
00111         int j = aiIndex[i];
00112         if (!m_abEvaluated[j])
00113         {
00114             m_abEvaluated[j] = true;
00115             m_akRVertex[j][0] = Rational(m_akVertex[j][0]);
00116             m_akRVertex[j][1] = Rational(m_akVertex[j][1]);
00117         }
00118     }
00119 }
00120 //----------------------------------------------------------------------------
00121 template <class Real>
00122 int Query2TRational<Real>::ToLine (const RVector& rkRP, int iV0,
00123     int iV1) const
00124 {
00125     Rational kX0 = rkRP[0] - m_akRVertex[iV0][0];
00126     Rational kY0 = rkRP[1] - m_akRVertex[iV0][1];
00127     Rational kX1 = m_akRVertex[iV1][0] - m_akRVertex[iV0][0];
00128     Rational kY1 = m_akRVertex[iV1][1] - m_akRVertex[iV0][1];
00129 
00130     Rational kDet2 = Det2(kX0,kY0,kX1,kY1);
00131     return (kDet2 > 0 ? +1 : (kDet2 < 0 ? -1 : 0));
00132 }
00133 //----------------------------------------------------------------------------
00134 template <class Real>
00135 int Query2TRational<Real>::ToTriangle (const RVector& rkRP, int iV0, int iV1,
00136     int iV2) const
00137 {
00138     int iSign0 = ToLine(rkRP,iV1,iV2);
00139     if (iSign0 > 0)
00140     {
00141         return +1;
00142     }
00143 
00144     int iSign1 = ToLine(rkRP,iV0,iV2);
00145     if (iSign1 < 0)
00146     {
00147         return +1;
00148     }
00149 
00150     int iSign2 = ToLine(rkRP,iV0,iV1);
00151     if (iSign2 > 0)
00152     {
00153         return +1;
00154     }
00155 
00156     return ((iSign0 && iSign1 && iSign2) ? -1 : 0);
00157 }
00158 //----------------------------------------------------------------------------
00159 template <class Real>
00160 int Query2TRational<Real>::ToCircumcircle (const RVector& rkRP, int iV0,
00161     int iV1, int iV2) const
00162 {
00163     RVector& rkRV0 = m_akRVertex[iV0];
00164     RVector& rkRV1 = m_akRVertex[iV1];
00165     RVector& rkRV2 = m_akRVertex[iV2];
00166 
00167     Rational kS0x = rkRV0[0] + rkRP[0];
00168     Rational kD0x = rkRV0[0] - rkRP[0];
00169     Rational kS0y = rkRV0[1] + rkRP[1];
00170     Rational kD0y = rkRV0[1] - rkRP[1];
00171     Rational kS1x = rkRV1[0] + rkRP[0];
00172     Rational kD1x = rkRV1[0] - rkRP[0];
00173     Rational kS1y = rkRV1[1] + rkRP[1];
00174     Rational kD1y = rkRV1[1] - rkRP[1];
00175     Rational kS2x = rkRV2[0] + rkRP[0];
00176     Rational kD2x = rkRV2[0] - rkRP[0];
00177     Rational kS2y = rkRV2[1] + rkRP[1];
00178     Rational kD2y = rkRV2[1] - rkRP[1];
00179     Rational kZ0 = kS0x*kD0x + kS0y*kD0y;
00180     Rational kZ1 = kS1x*kD1x + kS1y*kD1y;
00181     Rational kZ2 = kS2x*kD2x + kS2y*kD2y;
00182     Rational kDet3 = Det3(kD0x,kD0y,kZ0,kD1x,kD1y,kZ1,kD2x,kD2y,kZ2);
00183     return (kDet3 < 0 ? 1 : (kDet3 > 0 ? -1 : 0));
00184 }
00185 //----------------------------------------------------------------------------
00186 template <class Real>
00187 typename Query2TRational<Real>::Rational Query2TRational<Real>::Dot (
00188     Rational& rkX0, Rational& rkY0, Rational& rkX1, Rational& rkY1)
00189 {
00190     return rkX0*rkX1 + rkY0*rkY1;
00191 }
00192 //----------------------------------------------------------------------------
00193 template <class Real>
00194 typename Query2TRational<Real>::Rational Query2TRational<Real>::Det2 (
00195     Rational& rkX0, Rational& rkY0, Rational& rkX1, Rational& rkY1)
00196 {
00197     return rkX0*rkY1 - rkX1*rkY0;
00198 }
00199 //----------------------------------------------------------------------------
00200 template <class Real>
00201 typename Query2TRational<Real>::Rational Query2TRational<Real>::Det3 (
00202     Rational& rkX0, Rational& rkY0, Rational& rkZ0, Rational& rkX1,
00203     Rational& rkY1, Rational& rkZ1, Rational& rkX2, Rational& rkY2,
00204     Rational& rkZ2)
00205 {
00206     Rational kC00 = rkY1*rkZ2 - rkY2*rkZ1;
00207     Rational kC01 = rkY2*rkZ0 - rkY0*rkZ2;
00208     Rational kC02 = rkY0*rkZ1 - rkY1*rkZ0;
00209     return rkX0*kC00 + rkX1*kC01 + rkX2*kC02;
00210 }
00211 //----------------------------------------------------------------------------
00212 } //namespace Wm4

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