BWAPI
SPAR/AIModule/BWTA/vendors/CGAL/CGAL/squared_distance_3_2.h
Go to the documentation of this file.
00001 // Copyright (c) 1998-2004  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_2.h $
00019 // $Id: squared_distance_3_2.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_2_H
00026 #define CGAL_DISTANCE_3_2_H
00027 
00028 #include <CGAL/squared_distance_3_0.h>
00029 
00030 #include <CGAL/Segment_3.h>
00031 #include <CGAL/Line_3.h>
00032 #include <CGAL/Ray_3.h>
00033 #include <CGAL/Plane_3.h>
00034 
00035 CGAL_BEGIN_NAMESPACE
00036 
00037 namespace CGALi {
00038 
00039 template <class K>
00040 bool
00041 contains_vector(const typename K::Plane_3 &pl, 
00042                 const typename K::Vector_3 &vec,
00043                 const K&)
00044 {
00045   typedef typename K::RT RT;
00046   return pl.a()*vec.hx() + pl.b()*vec.hy() + pl.c() * vec.hz() == RT(0);
00047 }
00048 
00049 
00050 template <class K>
00051 inline typename K::FT
00052 squared_distance(
00053     const typename K::Point_3 & pt,
00054     const typename K::Plane_3 & plane,
00055     const K& k)
00056 {
00057   typename K::Construct_vector_3 construct_vector;
00058   typedef typename K::Vector_3 Vector_3;
00059   Vector_3 diff = construct_vector(plane.point(), pt);
00060   return squared_distance_to_plane(plane.orthogonal_vector(), diff, k);
00061 }
00062 
00063 
00064 
00065 template <class K>
00066 inline typename K::FT
00067 squared_distance(
00068     const typename K::Plane_3 & plane,
00069     const typename K::Point_3 & pt,
00070     const K& k)
00071 {
00072     return squared_distance(pt, plane, k);
00073 }
00074 
00075 template <class K>
00076 typename K::FT
00077 squared_distance(
00078     const typename K::Line_3 &line,
00079     const typename K::Plane_3 &plane,
00080     const K& k)
00081 {
00082     typedef typename K::FT FT;
00083     if (contains_vector(plane, line.direction().vector(), k))
00084         return squared_distance(plane, line.point(), k);
00085     return FT(0);
00086 }
00087 
00088 
00089 template <class K>
00090 inline typename K::FT
00091 squared_distance(
00092     const typename K::Plane_3 & p,
00093     const typename K::Line_3 & line,
00094     const K& k)
00095 {
00096     return squared_distance(line, p, k);
00097 }
00098 
00099 template <class K>
00100 typename K::FT
00101 squared_distance(
00102     const typename K::Ray_3 &ray,
00103     const typename K::Plane_3 &plane,
00104     const K& k)
00105 {
00106     typename K::Construct_vector_3 construct_vector;
00107     typedef typename K::Point_3 Point_3;
00108     typedef typename K::Vector_3 Vector_3;
00109     typedef typename K::RT RT;
00110     typedef typename K::FT FT;
00111     const Point_3 &start = ray.start();
00112     const Point_3 &planepoint = plane.point();
00113     Vector_3 start_min_pp = construct_vector(planepoint, start);
00114     Vector_3 end_min_pp = ray.direction().vector();
00115     const Vector_3 &normal = plane.orthogonal_vector();
00116     RT sdm_rs2pp = wdot(normal, start_min_pp, k);
00117     RT sdm_re2pp = wdot(normal, end_min_pp, k);
00118     switch (CGAL_NTS sign(sdm_rs2pp)) {
00119     case -1:
00120         if (sdm_re2pp > RT(0))
00121             return FT(0);
00122         return squared_distance_to_plane(normal, start_min_pp, k);
00123     case 0:
00124     default:
00125         return FT(0);
00126     case 1:
00127         if (sdm_re2pp < RT(0))
00128             return FT(0);
00129         return squared_distance_to_plane(normal, start_min_pp, k);
00130     }
00131 }
00132 
00133 
00134 template <class K>
00135 inline typename K::FT
00136 squared_distance(
00137     const typename K::Plane_3 & plane,
00138     const typename K::Ray_3 & ray,
00139     const K& k)
00140 {
00141     return squared_distance(ray, plane, k);
00142 }
00143 
00144 template <class K>
00145 typename K::FT
00146 squared_distance(
00147     const typename K::Segment_3 &seg,
00148     const typename K::Plane_3 &plane,
00149     const K& k)
00150 {
00151     typename K::Construct_vector_3 construct_vector;
00152     typedef typename K::Point_3 Point_3;
00153     typedef typename K::Vector_3 Vector_3;
00154     typedef typename K::RT RT;
00155     typedef typename K::FT FT;
00156     const Point_3 &start = seg.start();
00157     const Point_3 &end = seg.end();
00158     if (start == end)
00159         return squared_distance(start, plane, k);
00160     const Point_3 &planepoint = plane.point();
00161     Vector_3 start_min_pp = construct_vector(planepoint, start);
00162     Vector_3 end_min_pp = construct_vector(planepoint, end);
00163     const Vector_3 &normal = plane.orthogonal_vector();
00164     RT sdm_ss2pp = wdot(normal, start_min_pp, k);
00165     RT sdm_se2pp = wdot(normal, end_min_pp, k);
00166     switch (CGAL_NTS sign(sdm_ss2pp)) {
00167     case -1:
00168         if (sdm_se2pp >= RT(0))
00169             return FT(0);
00170         if (sdm_ss2pp >= sdm_se2pp)
00171             return squared_distance_to_plane(normal, start_min_pp, k);
00172         else
00173             return squared_distance_to_plane(normal, end_min_pp, k);
00174     case 0:
00175     default:
00176         return FT(0);
00177     case 1:
00178         if (sdm_se2pp <= RT(0))
00179             return FT(0);
00180         if (sdm_ss2pp <= sdm_se2pp)
00181             return squared_distance_to_plane(normal, start_min_pp, k);
00182         else
00183             return squared_distance_to_plane(normal, end_min_pp, k);
00184     }
00185 }
00186 
00187 
00188 template <class K>
00189 inline typename K::FT
00190 squared_distance(
00191     const typename K::Plane_3 & plane,
00192     const typename K::Segment_3 & seg,
00193     const K& k)
00194 {
00195     return squared_distance(seg, plane, k);
00196 }
00197 
00198 
00199 } // namespace CGALi
00200 
00201 
00202 template <class K>
00203 bool
00204 contains_vector(const Plane_3<K> &pl, const Vector_3<K> &vec)
00205 {
00206   return CGALi::contains_vector(pl,vec, K());
00207 }
00208 
00209 
00210 template <class K>
00211 inline 
00212 typename K::FT
00213 squared_distance(
00214     const Point_3<K> & pt,
00215     const Plane_3<K> & plane)
00216 {
00217   return CGALi::squared_distance(pt, plane, K());
00218 }
00219 
00220 
00221 
00222 template <class K>
00223 inline 
00224 typename K::FT
00225 squared_distance(
00226     const Plane_3<K> & plane,
00227     const Point_3<K> & pt)
00228 {
00229     return CGALi::squared_distance(pt, plane, K());
00230 }
00231 
00232 template <class K>
00233 inline
00234 typename K::FT
00235 squared_distance(
00236     const Line_3<K> &line,
00237     const Plane_3<K> &plane)
00238 {
00239     return CGALi::squared_distance(line, plane, K());
00240 }
00241 
00242 
00243 template <class K>
00244 inline 
00245 typename K::FT
00246 squared_distance(
00247     const Plane_3<K> & p,
00248     const Line_3<K> & line)
00249 {
00250     return CGALi::squared_distance(line, p, K());
00251 }
00252 
00253 template <class K>
00254 inline
00255 typename K::FT
00256 squared_distance(
00257     const Ray_3<K> &ray,
00258     const Plane_3<K> &plane)
00259 {
00260   return CGALi::squared_distance(ray, plane, K());
00261 }
00262 
00263 
00264 
00265 template <class K>
00266 inline 
00267 typename K::FT
00268 squared_distance(
00269     const Plane_3<K> & plane,
00270     const Ray_3<K> & ray)
00271 {
00272     return CGALi::squared_distance(ray, plane, K());
00273 }
00274 
00275 template <class K>
00276 inline
00277 typename K::FT
00278 squared_distance(
00279     const Segment_3<K> &seg,
00280     const Plane_3<K> &plane)
00281 {
00282   return CGALi::squared_distance(seg, plane, K());
00283 
00284 }
00285 
00286 
00287 template <class K>
00288 inline 
00289 typename K::FT
00290 squared_distance(
00291     const Plane_3<K> & plane,
00292     const Segment_3<K> & seg)
00293 {
00294     return CGALi::squared_distance(seg, plane, K());
00295 }
00296 
00297 
00298 CGAL_END_NAMESPACE
00299 
00300 
00301 #endif
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines