BWAPI
SPAR/AIModule/BWTA/vendors/CGAL/CGAL/Segment_Delaunay_graph_2/Sqrt_extension_1.h
Go to the documentation of this file.
00001 // Copyright (c) 2003,2004,2005,2006  INRIA Sophia-Antipolis (France) and
00002 // Notre Dame University (U.S.A.).  All rights reserved.
00003 //
00004 // This file is part of CGAL (www.cgal.org); you may redistribute it under
00005 // the terms of the Q Public License version 1.0.
00006 // See the file LICENSE.QPL distributed with CGAL.
00007 //
00008 // Licensees holding a valid commercial license may use this file in
00009 // accordance with the commercial license agreement provided with the software.
00010 //
00011 // This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
00012 // WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
00013 //
00014 // $URL: svn+ssh://scm.gforge.inria.fr/svn/cgal/branches/CGAL-3.5-branch/Segment_Delaunay_graph_2/include/CGAL/Segment_Delaunay_graph_2/Sqrt_extension_1.h $
00015 // $Id: Sqrt_extension_1.h 45641 2008-09-18 16:32:35Z hemmer $
00016 // 
00017 //
00018 // Author(s)     : Menelaos Karavelas <mkaravel@cse.nd.edu>
00019 
00020 
00021 #ifndef CGAL_SEGMENT_DELAUNAY_GRAPH_2_SQRT_EXTENSION_1_H
00022 #define CGAL_SEGMENT_DELAUNAY_GRAPH_2_SQRT_EXTENSION_1_H
00023 
00024 #include <CGAL/basic.h>
00025 #include <CGAL/enum.h>
00026 
00027 #include <iostream>
00028 
00029 CGAL_BEGIN_NAMESPACE
00030 
00031 template<class NT>
00032 class Sqrt_extension_1;
00033 
00034 template<class NT>
00035 class Sqrt_extension_1
00036 {
00037 private:
00038   NT x, y, r;
00039 
00040 private:
00041   typedef Sqrt_extension_1<NT>   Self;
00042 
00043 public:
00044   typedef NT                  FT;
00045   typedef NT                  RT;
00046 
00047   Sqrt_extension_1() : x(0), y(0), r(0) {}
00048   Sqrt_extension_1(int i) : x(i), y(0), r(0) {}
00049   Sqrt_extension_1(const NT& a) : x(a), y(0), r(0) {}
00050   Sqrt_extension_1(const NT& a, const NT& b, const NT& c)
00051     : x(a), y(b), r(c)
00052   {
00053     CGAL_exactness_assertion( !(CGAL::is_negative(r)) );
00054   }
00055 
00056   Sqrt_extension_1(const Sqrt_extension_1<NT>& other)
00057     : x(other.x), y(other.y), r(other.r) {}
00058 
00059 
00060   NT a() const { return x; }
00061   NT b() const { return y; }
00062   NT c() const { return r; }
00063 
00064   Self square() const
00065   {
00066     NT xy = x * y;
00067 
00068     return Self(CGAL::square(x) + CGAL::square(y) * r,
00069                 xy + xy,
00070                 r);
00071   }
00072 
00073   Sign sign() const
00074   {
00075     Sign sx = CGAL::sign(x);
00076 
00077     if ( CGAL::sign(r) == ZERO )  { return sx; }
00078 
00079     Sign sy = CGAL::sign(y);
00080 
00081     if ( sx == sy )  { return sx; }
00082     if ( sx == ZERO )  { return sy; }
00083 
00084     return sx * CGAL::compare( CGAL::square(x),
00085                                r * CGAL::square(y) );
00086   }
00087 
00088   const Self& operator+() const
00089   {
00090     return (*this);
00091   }
00092 
00093   Self operator-() const
00094   {
00095     return Self(-x, -y, r);
00096   }
00097 
00098   double to_double() const
00099   {
00100     // THIS MUST BE CHECK WITH SYLVAIN FOR CORRECTNESS
00101     double xd = CGAL::to_double(x);
00102     double yd = CGAL::to_double(y);
00103     double rd = CGAL::to_double(r);
00104 
00105     return (xd + yd * CGAL::sqrt(rd));
00106   }
00107 
00108   std::pair<double,double> to_interval() const
00109   {
00110     // THIS MUST BE CHECK WITH SYLVAIN FOR CORRECTNESS
00111     std::pair<double,double> x_ivl = CGAL::to_interval(x);
00112     std::pair<double,double> y_ivl = CGAL::to_interval(y);
00113     std::pair<double,double> r_ivl = CGAL::to_interval(r);
00114 
00115     std::pair<double,double> sqrt_r_ivl(CGAL::sqrt(r_ivl.first),
00116                                         CGAL::sqrt(r_ivl.second));
00117 
00118     std::pair<double,double>
00119       ivl(x_ivl.first + y_ivl.first * sqrt_r_ivl.first,
00120           x_ivl.second + y_ivl.second * sqrt_r_ivl.second);
00121 
00122     return ivl;
00123   }
00124 };
00125 
00126 
00127 // operator *
00128 template<class NT>
00129 inline
00130 Sqrt_extension_1<NT>
00131 operator*(const Sqrt_extension_1<NT>& x, const NT& n)
00132 {
00133   return Sqrt_extension_1<NT>(x.a() * n, x.b() * n, x.c());
00134 }
00135 
00136 
00137 template<class NT>
00138 inline
00139 Sqrt_extension_1<NT>
00140 operator*(const NT& n, const Sqrt_extension_1<NT>& x)
00141 {
00142   return (x * n);
00143 }
00144 
00145 template<class NT>
00146 inline
00147 Sqrt_extension_1<NT>
00148 operator*(const Sqrt_extension_1<NT>& x, const Sqrt_extension_1<NT>& y)
00149 {
00150   CGAL_exactness_precondition( CGAL::compare(x.c(), y.c()) == EQUAL );
00151 
00152   NT a = x.a() * y.a() + x.b() * y.b() * x.c();
00153   NT b = x.a() * y.b() + x.b() * y.a();
00154 
00155   return Sqrt_extension_1<NT>(a, b, x.c());
00156 }
00157 
00158 
00159 // operator +
00160 template<class NT>
00161 inline
00162 Sqrt_extension_1<NT>
00163 operator+(const Sqrt_extension_1<NT>& x, const NT& n)
00164 {
00165   return Sqrt_extension_1<NT>(x.a() + n, x.b(), x.c());
00166 }
00167 
00168 
00169 template<class NT>
00170 inline
00171 Sqrt_extension_1<NT>
00172 operator+(const NT& n, const Sqrt_extension_1<NT>& x)
00173 {
00174   return (x + n);
00175 }
00176 
00177 template<class NT>
00178 inline
00179 Sqrt_extension_1<NT>
00180 operator+(const Sqrt_extension_1<NT>& x, const Sqrt_extension_1<NT>& y)
00181 {
00182   CGAL_exactness_precondition( CGAL::compare(x.c(), y.c()) == EQUAL );
00183 
00184   return Sqrt_extension_1<NT>(x.a() + y.a(), x.b() + y.b(), x.c());
00185 }
00186 
00187 
00188 
00189 // operator -
00190 template<class NT>
00191 inline
00192 Sqrt_extension_1<NT>
00193 operator-(const Sqrt_extension_1<NT>& x, const NT& n)
00194 {
00195   return x + (-n);
00196 }
00197 
00198 
00199 template<class NT>
00200 inline
00201 Sqrt_extension_1<NT>
00202 operator-(const NT& n, const Sqrt_extension_1<NT>& x)
00203 {
00204   return -(x - n);
00205 }
00206 
00207 template<class NT>
00208 inline
00209 Sqrt_extension_1<NT>
00210 operator-(const Sqrt_extension_1<NT>& x, const Sqrt_extension_1<NT>& y)
00211 {
00212   return (x + (-y));
00213 }
00214 
00215 
00216 //=============================================================
00217 
00218 template <class NT> 
00219 struct Algebraic_structure_traits<Sqrt_extension_1<NT> >
00220     :public Algebraic_structure_traits_base<Sqrt_extension_1<NT>,CGAL::Integral_domain_without_division_tag>{
00221     // I haven't found division 
00222 private:
00223     typedef Algebraic_structure_traits<NT> AST_NT;
00224 public:
00225     typedef Sqrt_extension_1<NT> Algebraic_structure;
00226     typedef typename AST_NT::Is_exact Is_exact;
00227 };
00228 
00229 template<class NT>
00230 struct Real_embeddable_traits<Sqrt_extension_1<NT> >{
00231 private:
00232     typedef Real_embeddable_traits<NT> RET_NT;
00233 public:
00234     
00235     typedef Sqrt_extension_1<NT> Real_embeddable;
00236     
00237     class Abs 
00238         : public std::unary_function< Real_embeddable, Real_embeddable >{
00239     public:
00240         Real_embeddable operator()(const Real_embeddable& x) const {
00241             return (x>=0)?x:-x;
00242         }
00243     };    
00244 
00245     class Sgn 
00246         : public std::unary_function< Real_embeddable, CGAL::Sign >{
00247     public:
00248         CGAL::Sign operator()(const Real_embeddable& x) const {
00249             return x.sign();
00250         }
00251     };
00252     
00253     class Compare 
00254         : public std::binary_function< Real_embeddable, 
00255                                   Real_embeddable, 
00256                                   CGAL::Comparison_result >{
00257     public:
00258         CGAL::Comparison_result operator()(
00259                 const Real_embeddable& x, 
00260                 const Real_embeddable& y) const {
00261             CGAL_exactness_precondition( CGAL::compare(x.c(), y.c()) == EQUAL );
00262             return (x - y).sign();
00263             
00264 // This is not needed due to equality of CGAL::Sign CGAL::Comparison_result
00265 //             CGAL::Sign s = (x - y).sign();
00266 //             if ( s == ZERO ) { return EQUAL; }
00267 //             return (s == POSITIVE) ? LARGER : SMALLER;
00268         }
00269     };
00270     
00271     class To_double 
00272         : public std::unary_function< Real_embeddable, double >{
00273     public:
00274         double operator()(const Real_embeddable& x) const {
00275             return x.to_double();
00276         }
00277     };
00278     
00279     class To_interval 
00280         : public std::unary_function< Real_embeddable, std::pair< double, double > >{
00281     public:
00282         std::pair<double,double> operator()(const Real_embeddable& x) const {
00283             return x.to_interval();
00284         }
00285     };   
00286 };
00287 
00288 // operator <<
00289 template<class Stream, class NT>
00290 inline
00291 Stream&
00292 operator<<(Stream& os, const Sqrt_extension_1<NT>& x)
00293 {
00294   os << "(" << x.a()  << ")+(" << x.b() << ") sqrt{" << x.c() << "}";
00295   return os;
00296 }
00297 
00298 
00299 
00300 CGAL_END_NAMESPACE
00301 
00302 
00303 
00304 #endif // CGAL_SEGMENT_DELAUNAY_GRAPH_2_SQUARE_ROOT_1_H
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines