BWAPI
SPAR/AIModule/BWTA/vendors/CGAL/CGAL/squared_distance_3_0.h
Go to the documentation of this file.
00001 // Copyright (c) 1998  Utrecht University (The Netherlands),
00002 // ETH Zurich (Switzerland), Freie Universitaet Berlin (Germany),
00003 // INRIA Sophia-Antipolis (France), Martin-Luther-University Halle-Wittenberg
00004 // (Germany), Max-Planck-Institute Saarbruecken (Germany), RISC Linz (Austria),
00005 // and Tel-Aviv University (Israel).  All rights reserved.
00006 //
00007 // This file is part of CGAL (www.cgal.org); you can redistribute it and/or
00008 // modify it under the terms of the GNU Lesser General Public License as
00009 // published by the Free Software Foundation; version 2.1 of the License.
00010 // See the file LICENSE.LGPL distributed with CGAL.
00011 //
00012 // Licensees holding a valid commercial license may use this file in
00013 // accordance with the commercial license agreement provided with the software.
00014 //
00015 // This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
00016 // WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
00017 //
00018 // $URL: svn+ssh://scm.gforge.inria.fr/svn/cgal/branches/CGAL-3.5-branch/Distance_3/include/CGAL/squared_distance_3_0.h $
00019 // $Id: squared_distance_3_0.h 39776 2007-08-08 15:15:20Z spion $
00020 // 
00021 //
00022 // Author(s)     : Geert-Jan Giezeman, Andreas Fabri
00023 
00024 
00025 #ifndef CGAL_DISTANCE_3_0_H
00026 #define CGAL_DISTANCE_3_0_H
00027 
00028 #include <CGAL/kernel_assertions.h>
00029 #include <CGAL/enum.h>
00030 #include <CGAL/wmult.h>
00031 
00032 #include <CGAL/Point_3.h>
00033 #include <CGAL/Vector_3.h>
00034 
00035 
00036 CGAL_BEGIN_NAMESPACE
00037 
00038 
00039 namespace CGALi {
00040 
00041 template <class K>
00042 bool is_null(const typename K::Vector_3 &v, const K&)
00043 {
00044     typedef typename K::RT RT;
00045     return v.hx()==RT(0) && v.hy()==RT(0) && v.hz()==RT(0);
00046 }
00047 
00048 
00049 
00050 template <class K>
00051 typename K::RT
00052 wdot(const typename K::Vector_3 &u,
00053      const typename K::Vector_3 &v, 
00054      const K&)
00055 {
00056     return  (u.hx()*v.hx() + u.hy()*v.hy() + u.hz()*v.hz());
00057 }
00058 
00059 template <class K>
00060 typename K::RT
00061 wdot_tag(const typename K::Point_3 &p,
00062          const typename K::Point_3 &q,
00063          const typename K::Point_3 &r, 
00064          const K&,
00065          const Cartesian_tag&)
00066 {
00067   return  ( (p.x() - q.x()) * (r.x()- q.x())
00068             + (p.y()- q.y())* (r.y()- q.y())
00069             + (p.z()- q.z()) * (r.z() - q.z()) );
00070 }
00071 
00072 template <class K>
00073 typename K::RT
00074 wdot_tag(const typename K::Point_3 &p,
00075          const typename K::Point_3 &q,
00076          const typename K::Point_3 &r, 
00077          const K&,
00078          const Homogeneous_tag&)
00079 {
00080     return  ( (p.hx() * q.hw() - q.hx() * p.hw())
00081               * (r.hx() * q.hw() - q.hx() * r.hw())
00082               + (p.hy() * q.hw() - q.hy() * p.hw())
00083               * (r.hy() * q.hw() - q.hy() * r.hw())
00084               + (p.hz() * q.hw() - q.hz() * p.hw())
00085               * (r.hz() * q.hw() - q.hz() * r.hw()));
00086 }
00087 
00088 template <class K>
00089 inline
00090 typename K::RT
00091 wdot(const typename K::Point_3 &p,
00092      const typename K::Point_3 &q,
00093      const typename K::Point_3 &r, 
00094      const K& k)
00095 { 
00096   typedef typename K::Kernel_tag Tag;
00097   Tag tag;
00098   return wdot_tag(p, q, r, k, tag);
00099 }
00100 
00101 
00102 
00103 
00104 template <class K>
00105 typename K::Vector_3
00106 wcross(const typename K::Vector_3 &u,
00107        const typename K::Vector_3 &v, 
00108        const K&)
00109 {
00110   typedef typename K::Vector_3 Vector_3;
00111     return Vector_3(
00112         u.hy()*v.hz() - u.hz()*v.hy(),
00113         u.hz()*v.hx() - u.hx()*v.hz(),
00114         u.hx()*v.hy() - u.hy()*v.hx());
00115 }
00116 
00117 
00118 template <class K>
00119 inline 
00120 bool 
00121 is_acute_angle(const typename K::Vector_3 &u,
00122                const typename K::Vector_3 &v, 
00123                const K& k)
00124 {
00125     typedef typename K::RT RT;
00126     return RT(wdot(u, v, k)) > RT(0) ;
00127 }
00128 
00129 template <class K>
00130 inline 
00131 bool 
00132 is_straight_angle(const typename K::Vector_3 &u,
00133                   const typename K::Vector_3 &v, 
00134                   const K& k)
00135 {
00136     typedef typename K::RT RT;
00137     return RT(wdot(u, v, k)) == RT(0) ;
00138 }
00139 
00140 template <class K>
00141 inline 
00142 bool 
00143 is_obtuse_angle(const typename K::Vector_3 &u,
00144                 const typename K::Vector_3 &v, 
00145                 const K& k)
00146 {
00147     typedef typename K::RT RT;
00148     return RT(wdot(u, v, k)) < RT(0) ;
00149 }
00150 
00151 template <class K>
00152 inline 
00153 bool 
00154 is_acute_angle(const typename K::Point_3 &p,
00155                const typename K::Point_3 &q,
00156                const typename K::Point_3 &r, 
00157                const K& k)
00158 {
00159     typedef typename K::RT RT;
00160     return RT(wdot(p, q, r, k)) > RT(0) ;
00161 }
00162 
00163 template <class K>
00164 inline
00165 bool 
00166 is_straight_angle(const typename K::Point_3 &p,
00167                   const typename K::Point_3 &q,
00168                   const typename K::Point_3 &r, 
00169                   const K& k)
00170 {
00171     typedef typename K::RT RT;
00172     return RT(wdot(p, q, r, k)) == RT(0) ;
00173 }
00174 
00175 template <class K>
00176 inline 
00177 bool 
00178 is_obtuse_angle(const typename K::Point_3 &p,
00179                 const typename K::Point_3 &q, 
00180                 const typename K::Point_3 &r, 
00181                   const K& k)
00182 {
00183     typedef typename K::RT RT;
00184     return RT(wdot(p, q, r, k)) < RT(0) ;
00185 }
00186 template <class K>
00187 inline 
00188 typename K::FT
00189 squared_distance(const typename K::Point_3 & pt1,
00190                  const typename K::Point_3 & pt2, 
00191                  const K& k)
00192 {
00193   return k.compute_squared_distance_3_object()(pt1, pt2);
00194 }
00195 
00196 
00197 template <class K>
00198 typename K::FT
00199 squared_distance_to_plane(const typename K::Vector_3 & normal,
00200                           const typename K::Vector_3 & diff, 
00201                           const K& k)
00202 {
00203   typedef typename K::RT RT;
00204   typedef typename K::FT FT;
00205   RT dot, squared_length;
00206   dot = wdot(normal, diff, k);
00207   squared_length = wdot(normal, normal, k);
00208   return FT(dot*dot) /
00209     FT(wmult((K*)0, squared_length, diff.hw(), diff.hw()));
00210 }
00211 
00212 
00213 template <class K>
00214 typename K::FT
00215 squared_distance_to_line(const typename K::Vector_3 & dir,
00216                          const typename K::Vector_3 & diff, 
00217                          const K& k)
00218 {
00219   typedef typename K::Vector_3 Vector_3;
00220   typedef typename K::RT RT;
00221   typedef typename K::FT FT;
00222   Vector_3 wcr = wcross(dir, diff, k);
00223   return FT(wcr*wcr)/FT(wmult(
00224                               (K*)0, RT(wdot(dir, dir, k)), diff.hw(), diff.hw()));
00225 }
00226 
00227 
00228 template <class K>
00229 inline
00230 bool
00231 same_direction_tag(const typename K::Vector_3 &u,
00232                    const typename K::Vector_3 &v,
00233                    const K&,
00234                    const Cartesian_tag&)
00235 { 
00236   typedef typename K::FT FT;
00237   const FT& ux = u.x();
00238   const FT& uy = u.y();
00239   const FT& uz = u.z();
00240   if (CGAL_NTS abs(ux) > CGAL_NTS abs(uy)) {
00241     if (CGAL_NTS abs(ux) > CGAL_NTS abs(uz)) {
00242       return CGAL_NTS sign(ux) == CGAL_NTS sign(v.x());
00243     } else {
00244       return CGAL_NTS sign(uz) == CGAL_NTS sign(v.z());
00245     }
00246   } else {
00247     if (CGAL_NTS abs(uy) > CGAL_NTS abs(uz)) {
00248       return CGAL_NTS sign(uy) == CGAL_NTS sign(v.y());
00249     } else {
00250       return CGAL_NTS sign(uz) == CGAL_NTS sign(v.z());
00251     }
00252   } 
00253 }
00254 
00255 
00256 template <class K>
00257 inline
00258 bool
00259 same_direction_tag(const typename K::Vector_3 &u,
00260                    const typename K::Vector_3 &v,
00261                    const K&,
00262                    const Homogeneous_tag&)
00263 {   
00264   typedef typename K::RT RT;
00265   const RT& uhx = u.hx();
00266   const RT& uhy = u.hy();
00267   const RT& uhz = u.hz();
00268   if (CGAL_NTS abs(uhx) > CGAL_NTS abs(uhy)) {
00269     if (CGAL_NTS abs(uhx) > CGAL_NTS abs(uhz)) {
00270       return CGAL_NTS sign(uhx) == CGAL_NTS sign(v.hx());
00271     } else {
00272       return CGAL_NTS sign(uhz) == CGAL_NTS sign(v.hz());
00273     }
00274   } else {
00275     if (CGAL_NTS abs(uhy) > CGAL_NTS abs(uhz)) {
00276       return CGAL_NTS sign(uhy) == CGAL_NTS sign(v.hy());
00277     } else {
00278       return CGAL_NTS sign(uhz) == CGAL_NTS sign(v.hz());
00279     }
00280   }
00281 }
00282 
00283 
00284 template <class K>
00285 inline
00286 bool
00287 same_direction(const typename K::Vector_3 &u,
00288                const typename K::Vector_3 &v,
00289                const K& k)
00290 {  
00291   typedef typename K::Kernel_tag Tag;
00292   Tag tag;
00293   return same_direction_tag(u, v, k, tag);
00294 }
00295 
00296 
00297 } // namespace CGALi
00298 
00299 template <class K>
00300 inline 
00301 typename K::FT
00302 squared_distance(const Point_3<K> & pt1,
00303                  const Point_3<K> & pt2)
00304 {
00305   return CGALi::squared_distance(pt1,pt2, K());
00306 }
00307 
00308 
00309 template <class K>
00310 inline 
00311 typename K::FT
00312 squared_distance_to_plane(const Vector_3<K> & normal,
00313                           const Vector_3<K> & diff)
00314 {
00315   return CGALi::squared_distance_to_plane(normal, diff, K());
00316 }
00317 
00318 
00319 template <class K>
00320 inline
00321 typename K::FT
00322 squared_distance_to_line(const Vector_3<K> & dir,
00323                          const Vector_3<K> & diff)
00324 {
00325   return CGALi::squared_distance_to_line(dir, diff, K());
00326 }
00327 
00328 
00329 
00330 CGAL_END_NAMESPACE
00331 
00332 
00333 #endif
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines