BWAPI
SPAR/AIModule/BWTA/vendors/CGAL/CGAL/Number_types/internal_functions_comparison_root_of_2.h
Go to the documentation of this file.
00001 // Copyright (c) 2005,2006  INRIA Sophia-Antipolis (France)
00002 // All rights reserved.
00003 //
00004 // This file is part of CGAL (www.cgal.org); you can redistribute it and/or
00005 // modify it under the terms of the GNU Lesser General Public License as
00006 // published by the Free Software Foundation; version 2.1 of the License.
00007 // See the file LICENSE.LGPL distributed with CGAL.
00008 //
00009 // Licensees holding a valid commercial license may use this file in
00010 // accordance with the commercial license agreement provided with the software.
00011 //
00012 // This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
00013 // WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
00014 //
00015 // $URL: svn+ssh://scm.gforge.inria.fr/svn/cgal/branches/CGAL-3.5-branch/Number_types/include/CGAL/Number_types/internal_functions_comparison_root_of_2.h $
00016 // $Id: internal_functions_comparison_root_of_2.h 44918 2008-08-12 14:59:20Z spion $
00017 //
00018 //
00019 // Author(s)     : Sylvain Pion, Monique Teillaud, Athanasios Kakargias
00020 //                 Olivier Devillers
00021 
00022 #ifndef CGAL_NUMBER_TypeS_ROOT_OF_COMPARISON_FUNCTIONS_22_H
00023 #define CGAL_NUMBER_TypeS_ROOT_OF_COMPARISON_FUNCTIONS_22_H
00024 
00025 #include <CGAL/enum.h>
00026 #include <CGAL/kernel_assertions.h>
00027 
00028 namespace CGAL {
00029 namespace CGALi {
00030 
00031 // Maybe we can trash this
00032 /*1 1*/template <class FT>
00033 /*1 1*/Comparison_result
00034 compare_11_11( const FT& A1, const FT& B1,
00035                const FT& A2, const FT& B2 )
00036 {
00037   // Compares roots of (A1 X + B1) and (A2 X + B2).
00038   CGAL_precondition( A1 > 0 & A2 > 0 );
00039   return CGAL_NTS compare(B2*A1, B1*A2);
00040 }
00041 
00042 /*2 1*/template <class FT>
00043 /*1 1*/Comparison_result
00044 compare_21_11(const FT& A2, const FT& B2, const FT& C2,
00045               const FT& A1, const FT& B1 )
00046 {
00047   // Compares roots of (A1 X + B1) and the smaller of (A2 X^2 + B2 X + C2).
00048   CGAL_precondition(A2 > 0);
00049 
00050   // First, we compare the root of P1 to the root of the derivative of P2.
00051 
00052   int cmp = compare_11_11<FT>(A1, B1, A2*2, B2);
00053 
00054   if (cmp > 0)
00055     return LARGER;
00056 
00057   // If it doesn't work, we evaluate the sign of P2 at the root of P1.
00058 
00059   FT p2 = B1 * (A1*B2 - A2*B1) - C2 * CGAL_NTS square(A1);
00060 
00061   return CGAL_NTS sign(p2);
00062 }
00063 
00064 /*2 2*/template <class FT>
00065 /*2 1*/Comparison_result
00066 compare_22_21( const FT& A1p, const FT& B1p, const FT& C1p,
00067                const FT& A2p, const FT& B2p, const FT& C2p )
00068 {
00069     // Compares the larger root of (A1 X^2 + B1 X + C1)
00070     //      to the smaller root of (A2 X^2 + B2 X + C2)
00071     // It boils down to the code from the DFMT paper
00072     // by multiplying A* and C* by 2, and B* by -1.
00073 
00074     CGAL_precondition(A1p > 0 & A2p > 0);
00075 
00076     FT A1 = 2 * A1p;
00077     FT C1 = 2 * C1p;
00078     FT B1 = -B1p;
00079 
00080     FT A2 = 2 * A2p;
00081     FT C2 = 2 * C2p;
00082     FT B2 = -B2p;
00083 
00084     // Now compares the larger root of (A1 X^2 -2B1 X + C1)
00085     //          to the smaller root of (A2 X^2 -2B2 X + C2)
00086     FT J = calcJ(A1,B1,A2,B2);
00087 
00088     if ( J < 0 ) return LARGER;   // r1 > l2
00089 
00090     FT K = calcK(A1,B1,C1,A2,B2,C2);
00091 
00092     if ( K < 0 ) return LARGER;   // r1 > l2
00093 
00094     FT Jp = calcJp(B1,C1,B2,C2);
00095 
00096     if ( Jp < 0 ) return SMALLER;  // r1 < l2
00097 
00098     FT P4 = calcP4(J,Jp,A1,C1,A2,C2);
00099 
00100     return - CGAL_NTS sign(P4);
00101     // if ( P4< FT(0) ) return LARGER;   // r1 > l2
00102     // if ( P4> FT(0) ) return SMALLER;  // r1 < l2
00103     // return EQUAL;
00104 }
00105 
00106 /*2 2*/template <class FT> inline
00107 /*1 2*/Comparison_result
00108 compare_22_12( const FT& A1, const FT& B1, const FT& C1,
00109                const FT& A2, const FT& B2, const FT& C2 )
00110 {
00111     // _22_12 boils down to _22_21 by :
00112     // - swapping the two polynomials
00113     // - changing the sign of the result
00114     return - compare_22_21(A2, B2, C2, A1, B1, C1);
00115 }
00116 
00117 /*2 2*/template <class FT>
00118 /*1 1*/Comparison_result
00119 compare_22_11( const FT& A1p, const FT& B1p, const FT& C1p,
00120                const FT& A2p, const FT& B2p, const FT& C2p )
00121 {
00122   // Compares the smaller root of (A1 X^2 + B1 X + C1)
00123   //       to the smaller root of (A2 X^2 + B2 X + C2)
00124   // It boils down to the code from the DFMT paper
00125   // by multiplying A* and C* by 2, and B* by -1.
00126 
00127   CGAL_precondition(A1p > 0 & A2p > 0);
00128 
00129   FT A1 = 2 * A1p;
00130   FT C1 = 2 * C1p;
00131   FT B1 = -B1p;
00132 
00133   FT A2 = 2 * A2p;
00134   FT C2 = 2 * C2p;
00135   FT B2 = -B2p;
00136 
00137   // Compares the smaller root of (A1 X^2 -2B1 X + C1)
00138   //       to the smaller root of (A2 X^2 -2B2 X + C2)
00139   FT J = calcJ(A1,B1,A2,B2);
00140   FT K = calcK(A1,B1,C1,A2,B2,C2);
00141 
00142   if (J > 0)
00143   {
00144     if (K > 0) return SMALLER;  // l1 < l2
00145 
00146     FT I1= calcI(A1,B1,C1);
00147     FT I2= calcI(A2,B2,C2);
00148     FT D = calcD(A1,I1,A2,I2);
00149 
00150     if (D > 0) return SMALLER;  // l1 < l2
00151 
00152     FT Jp = calcJp(B1,C1,B2,C2);
00153 
00154     if (Jp < 0) return LARGER;   // l1 > l2
00155 
00156     FT P4 = calcP4(I1,I2,K);
00157 
00158     return CGAL_NTS sign(P4);
00159   }
00160 
00161   // J <= 0
00162   if (K > 0) return LARGER;   // l1 > l2
00163 
00164   FT I1= calcI(A1,B1,C1);
00165   FT I2= calcI(A2,B2,C2);
00166   FT D = calcD(A1,I1,A2,I2);
00167 
00168   if (D < 0) return LARGER;   // l1 > l2
00169 
00170   FT Jp = calcJp(B1,C1,B2,C2);
00171 
00172   if (Jp > 0) return SMALLER;  // l1 < l2
00173 
00174   FT P4 = calcP4(I1,I2,K);
00175 
00176   return - CGAL_NTS sign(P4);
00177 }
00178 
00179 /*2 2*/template <class FT> inline
00180 /*2 2*/Comparison_result
00181 compare_22_22( const FT& A1, const FT& B1, const FT& C1,
00182                const FT& A2, const FT& B2, const FT& C2 )
00183 {
00184   // _22_22 boils down to _22_11 by :
00185   // - changing the sign of the two roots (X <-> -X in the polynomial)
00186   // - swapping the two polynomials
00187   return compare_22_11<FT>(A2, -B2, C2, A1, -B1, C1);
00188 }
00189 
00190 template <class FT>
00191 inline FT calcI(const FT& A, const FT& B, const FT& C)
00192 { return CGAL_NTS square(B)-A*C; }
00193 
00194 template <class FT>
00195 inline FT calcJ(const FT& A1, const FT& B1, const FT& A2, const FT& B2)
00196 { return A1*B2-A2*B1; }
00197 
00198 template <class FT>
00199 inline FT calcK(const FT& A1, const FT& B1, const FT& C1,
00200                 const FT& A2, const FT& B2, const FT& C2)
00201 { return C1*A2+A1*C2-2*B1*B2; }
00202 
00203 template <class FT>
00204 inline FT calcJp(const FT& B1, const FT& C1, const FT& B2, const FT& C2)
00205 { return B1*C2-C1*B2; }
00206 
00207 template <class FT>
00208 inline FT calcP4(const FT& J,  const FT& Jp,
00209                  const FT& A1, const FT& C1,
00210                  const FT& A2, const FT& C2)
00211 { return CGAL_NTS square(A1*C2-C1*A2)-4*J*Jp;}
00212 
00213 template <class FT>
00214 inline FT calcP4(const FT& I1, const FT& I2, const FT& K)
00215 { return CGAL_NTS square(K)-4*I1*I2;}
00216 
00217 template <class FT>
00218 inline FT calcD(const FT& A1, const FT& I1, const FT& A2, const FT& I2)
00219 { return I1*CGAL_NTS square(A2) - I2*CGAL_NTS square(A1);}
00220 
00221 } // namespace CGALi
00222 } // namespace CGAL
00223 
00224 #endif // CGAL_NUMBER_TypeS_ROOT_OF_COMPARISON_FUNCTIONS_22_H
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines