|
BWAPI
|
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_2.h $ 00015 // $Id: Sqrt_extension_2.h 45641 2008-09-18 16:32:35Z hemmer $ 00016 // 00017 // 00018 // Author(s) : Menelaos Karavelas <mkaravel@cse.nd.edu> 00019 00020 00021 00022 00023 #ifndef CGAL_SEGMENT_DELAUNAY_GRAPH_2_SQRT_EXTENSION_2_H 00024 #define CGAL_SEGMENT_DELAUNAY_GRAPH_2_SQRT_EXTENSION_2_H 00025 00026 #include <CGAL/Segment_Delaunay_graph_2/Sqrt_extension_1.h> 00027 00028 00029 00030 00031 CGAL_BEGIN_NAMESPACE 00032 00033 00034 template<class NT> 00035 class Sqrt_extension_2 00036 { 00037 private: 00038 typedef Sqrt_extension_2<NT> Self; 00039 typedef Sqrt_extension_1<NT> Sqrt_1; 00040 00041 NT a0_, a1_, a2_, a3_; 00042 NT A_, B_; 00043 00044 public: 00045 typedef NT FT; 00046 typedef NT RT; 00047 00048 public: 00049 Sqrt_extension_2() 00050 : a0_(0), a1_(0), a2_(0), a3_(0), A_(0), B_(0) {} 00051 Sqrt_extension_2(int i) 00052 : a0_(i), a1_(0), a2_(0), a3_(0), A_(0), B_(0) {} 00053 Sqrt_extension_2(const NT& a) 00054 : a0_(a), a1_(0), a2_(0), a3_(0), A_(0), B_(0) {} 00055 Sqrt_extension_2(const NT& a0, const NT& a1, const NT& a2, 00056 const NT& a3, const NT& A, const NT& B) 00057 : a0_(a0), a1_(a1), a2_(a2), a3_(a3), A_(A), B_(B) 00058 { 00059 CGAL_exactness_precondition( !(CGAL::is_negative(A_)) ); 00060 CGAL_exactness_precondition( !(CGAL::is_negative(B_)) ); 00061 } 00062 00063 Sqrt_extension_2(const Sqrt_extension_2<NT>& other) 00064 : a0_(other.a0_), a1_(other.a1_), a2_(other.a2_), 00065 a3_(other.a3_), A_(other.A_), B_(other.B_) {} 00066 00067 00068 NT a() const { return a0_; } 00069 NT b() const { return a1_; } 00070 NT c() const { return a2_; } 00071 NT d() const { return a3_; } 00072 NT e() const { return A_; } 00073 NT f() const { return B_; } 00074 00075 00076 Self operator*(const Self& b) const 00077 { 00078 CGAL_exactness_precondition( CGAL::compare(A_, b.A_) == EQUAL ); 00079 CGAL_exactness_precondition( CGAL::compare(B_, b.B_) == EQUAL ); 00080 00081 NT a0 = a0_ * b.a0_ + a1_ * b.a1_ * A_ + a2_ * b.a2_ * B_ 00082 + a3_ * b.a3_ * A_ * B_; 00083 NT a1 = a0_ * b.a1_ + a1_ * b.a0_ + (a2_ * b.a3_ + a3_ * b.a2_) * B_; 00084 NT a2 = a0_ * b.a2_ + a2_ * b.a0_ + (a1_ * b.a3_ + a3_ * b.a1_) * A_; 00085 NT a3 = a0_ * b.a3_ + a3_ * b.a0_ + a1_ * b.a2_ + a2_ * b.a1_; 00086 00087 return Self(a0, a1, a2, a3, A_, B_); 00088 } 00089 00090 Self operator-() const 00091 { 00092 return Self(-a0_, -a1_, -a2_, -a3_, A_, B_); 00093 } 00094 00095 Self operator+() const 00096 { 00097 return (*this); 00098 } 00099 00100 Self square() const 00101 { 00102 NT a0 = CGAL::square(a0_) + CGAL::square(a1_) * A_ 00103 + CGAL::square(a2_) * B_ + CGAL::square(a3_) * A_ * B_; 00104 NT a1_half = a0_ * a1_ + a2_ * a3_ * B_; 00105 NT a2_half = a0_ * a2_ + a1_ * a3_ * A_; 00106 NT a3_half = a0_ * a3_ + a1_ * a2_; 00107 00108 NT a1 = a1_half + a1_half; 00109 NT a2 = a2_half + a2_half; 00110 NT a3 = a3_half + a3_half; 00111 00112 return Self(a0, a1, a2, a3, A_, B_); 00113 } 00114 00115 Sign sign() const 00116 { 00117 Sqrt_1 x(a0_, a1_, A_); 00118 Sqrt_1 y(a2_, a3_, A_); 00119 00120 Sign s_x = CGAL_NTS sign(x); 00121 Sign s_y = CGAL_NTS sign(y); 00122 Sign s_B = CGAL_NTS sign(B_); 00123 00124 if ( s_B == ZERO ) { 00125 return s_x; 00126 } else if ( s_x == s_y ) { 00127 return s_x; 00128 } else if ( s_x == ZERO ) { 00129 return s_y; 00130 } else if ( s_y == ZERO ) { 00131 return s_x; 00132 } else { 00133 Sqrt_1 Q = CGAL::square(x) - CGAL::square(y) * B_; 00134 return s_x * CGAL_NTS sign(Q); 00135 } 00136 } 00137 00138 double to_double() const 00139 { 00140 // THIS MUST BE CHECK WITH SYLVAIN FOR CORRECTNESS 00141 double a0d = CGAL::to_double(a0_); 00142 double a1d = CGAL::to_double(a1_); 00143 double a2d = CGAL::to_double(a2_); 00144 double a3d = CGAL::to_double(a3_); 00145 double Ad = CGAL::to_double(A_); 00146 double Bd = CGAL::to_double(B_); 00147 00148 return (a0d + a1d * CGAL::sqrt(Ad) + a2d * CGAL::sqrt(Bd) 00149 + a3d * CGAL::sqrt(Ad * Bd)); 00150 } 00151 00152 }; 00153 00154 00155 00156 00157 00158 // operator * 00159 template<class NT> 00160 inline 00161 Sqrt_extension_2<NT> 00162 operator*(const Sqrt_extension_2<NT>& x, const NT& n) 00163 { 00164 return Sqrt_extension_2<NT>(x.a() * n, x.b() * n, x.c() * n, 00165 x.d() * n, x.e(), x.f()); 00166 } 00167 00168 00169 template<class NT> 00170 inline 00171 Sqrt_extension_2<NT> 00172 operator*(const NT& n, const Sqrt_extension_2<NT>& x) 00173 { 00174 return (x * n); 00175 } 00176 00177 00178 00179 // operator + 00180 template<class NT> 00181 inline 00182 Sqrt_extension_2<NT> 00183 operator+(const Sqrt_extension_2<NT>& x, const NT& n) 00184 { 00185 return Sqrt_extension_2<NT>(x.a() + n, x.b(), x.c(), x.d(), x.e(), x.f()); 00186 } 00187 00188 00189 template<class NT> 00190 inline 00191 Sqrt_extension_2<NT> 00192 operator+(const NT& n, const Sqrt_extension_2<NT>& x) 00193 { 00194 return (x + n); 00195 } 00196 00197 template<class NT> 00198 inline 00199 Sqrt_extension_2<NT> 00200 operator+(const Sqrt_extension_2<NT>& x, const Sqrt_extension_2<NT>& y) 00201 { 00202 CGAL_exactness_precondition( CGAL::compare(x.e(), y.e()) == EQUAL ); 00203 CGAL_exactness_precondition( CGAL::compare(x.f(), y.f()) == EQUAL ); 00204 00205 return Sqrt_extension_2<NT>(x.a() + y.a(), x.b() + y.b(), 00206 x.c() + y.c(), x.d() + y.d(), 00207 x.e(), x.f()); 00208 } 00209 00210 00211 00212 // operator - 00213 template<class NT> 00214 inline 00215 Sqrt_extension_2<NT> 00216 operator-(const Sqrt_extension_2<NT>& x, const NT& n) 00217 { 00218 return x + (-n); 00219 } 00220 00221 00222 template<class NT> 00223 inline 00224 Sqrt_extension_2<NT> 00225 operator-(const NT& n, const Sqrt_extension_2<NT>& x) 00226 { 00227 return -(x - n); 00228 } 00229 00230 template<class NT> 00231 inline 00232 Sqrt_extension_2<NT> 00233 operator-(const Sqrt_extension_2<NT>& x, const Sqrt_extension_2<NT>& y) 00234 { 00235 return (x + (-y)); 00236 } 00237 00238 00239 00240 //=================================================================== 00241 00242 00243 template <class NT> 00244 struct Algebraic_structure_traits<Sqrt_extension_2<NT> > 00245 :public Algebraic_structure_traits_base<Sqrt_extension_2<NT>,CGAL::Integral_domain_without_division_tag>{ 00246 private: 00247 typedef Algebraic_structure_traits<NT> AST_NT; 00248 public: 00249 typedef Sqrt_extension_2<NT> Algebraic_structure; 00250 typedef typename AST_NT::Is_exact Is_exact; 00251 }; 00252 00253 template<class NT> 00254 struct Real_embeddable_traits<Sqrt_extension_2<NT> >{ 00255 private: 00256 typedef Real_embeddable_traits<NT> RET_NT; 00257 public: 00258 00259 typedef Sqrt_extension_2<NT> Real_embeddable; 00260 00261 class Abs 00262 : public std::unary_function< Real_embeddable, Real_embeddable >{ 00263 public: 00264 Real_embeddable operator()(const Real_embeddable& x) const { 00265 return (x>=0)?x:-x; 00266 } 00267 }; 00268 00269 class Sgn 00270 : public std::unary_function< Real_embeddable, CGAL::Sign >{ 00271 public: 00272 CGAL::Sign operator()(const Real_embeddable& x) const { 00273 return x.sign(); 00274 } 00275 }; 00276 00277 class Compare 00278 : public std::binary_function< Real_embeddable, 00279 Real_embeddable, 00280 CGAL::Comparison_result >{ 00281 public: 00282 CGAL::Comparison_result operator()( 00283 const Real_embeddable& x, 00284 const Real_embeddable& y) const { 00285 CGAL_exactness_precondition( CGAL::compare(x.e(), y.e()) == EQUAL ); 00286 CGAL_exactness_precondition( CGAL::compare(x.f(), y.f()) == EQUAL ); 00287 return (x - y).sign(); 00288 } 00289 }; 00290 00291 class To_double 00292 : public std::unary_function< Real_embeddable, double >{ 00293 public: 00294 double operator()(const Real_embeddable& x) const { 00295 return x.to_double(); 00296 } 00297 }; 00298 00299 class To_interval 00300 : public std::unary_function< Real_embeddable, std::pair< double, double > >{ 00301 public: 00302 std::pair<double,double> operator()(const Real_embeddable& x) const { 00303 return x.to_interval(); 00304 } 00305 }; 00306 }; 00307 00308 // operator << 00309 template<class Stream, class NT> 00310 inline 00311 Stream& 00312 operator<<(Stream& os, const Sqrt_extension_2<NT>& x) 00313 { 00314 os << "(" << x.a() << ")+(" << x.b() << ") sqrt{" << x.e() << "}"; 00315 os << "+(" << x.c() << ") sqrt{" << x.f() << "}"; 00316 os << "+(" << x.d() << ") sqrt{(" << x.e() << ") (" << x.f() 00317 << ")}"; 00318 return os; 00319 } 00320 00321 00322 00323 00324 00325 00326 CGAL_END_NAMESPACE 00327 00328 00329 #endif // CGAL_SEGMENT_DELAUNAY_GRAPH_2_SQRT_EXTENSION_2_H
1.7.6.1