BWAPI
SPAR/AIModule/BWTA/vendors/CGAL/CGAL/Arr_point_location/Arr_lm_nearest_neighbor.h
Go to the documentation of this file.
00001 // Copyright (c) 2005  Tel-Aviv University (Israel).
00002 // 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/Arrangement_on_surface_2/include/CGAL/Arr_point_location/Arr_lm_nearest_neighbor.h $
00015 // $Id: Arr_lm_nearest_neighbor.h 40429 2007-09-24 15:01:00Z efif $
00016 // 
00017 // Author(s)     : Idit Haran   <haranidi@post.tau.ac.il>
00018 //                 Ron Wein     <wein@post.tau.ac.il>
00019 #ifndef CGAL_ARR_LANDMARKS_NEAREST_NEIGHBOR_H
00020 #define CGAL_ARR_LANDMARKS_NEAREST_NEIGHBOR_H
00021 
00025 #include <CGAL/basic.h>
00026 #include <CGAL/Search_traits.h>
00027 #include <CGAL/Orthogonal_k_neighbor_search.h>
00028 #include <CGAL/Arrangement_2/Arr_traits_adaptor_2.h>
00029 
00030 CGAL_BEGIN_NAMESPACE
00031 
00037 template <class GeomTraits_>
00038 class Arr_landmarks_nearest_neighbor 
00039 {
00040 public:
00041   
00042   typedef GeomTraits_                                   Geometry_traits_2;
00043   
00044   typedef typename Geometry_traits_2::Approximate_number_type    ANT;
00045   typedef typename Geometry_traits_2::Point_2                    Point_2;
00046   typedef Arr_landmarks_nearest_neighbor<Geometry_traits_2>      Self;
00047 
00052   class  NN_Point_2
00053   {
00054   public:
00055 
00056     Point_2    m_point;      // The point.
00057     Object     m_object;     // The arrangement feature containing the point.
00058     ANT        m_vec[2];     // Approximate x and y-coordinates of the point.
00059 
00060   public:
00061 
00063     NN_Point_2()
00064     { 
00065       m_vec[0] = m_vec[1] = 0;
00066     }
00067 
00069     NN_Point_2 (const Point_2 &p) :
00070       m_point (p)
00071     {
00072       // Obtain the coordinate approximations,
00073       Geometry_traits_2  m_traits;
00074       m_vec[0] = m_traits.approximate_2_object()(p, 0);
00075       m_vec[1] = m_traits.approximate_2_object()(p, 1);
00076     }
00077 
00079     NN_Point_2 (const Point_2& p, const Object& obj) :
00080       m_point (p),
00081       m_object (obj)
00082     { 
00083       // Obtain the coordinate approximations,
00084       Geometry_traits_2  m_traits;
00085       m_vec[0] = m_traits.approximate_2_object()(p, 0);
00086       m_vec[1] = m_traits.approximate_2_object()(p, 1);
00087     }
00088 
00089     /* Get the point. */
00090     const Point_2& point() const
00091     {
00092       return (m_point);
00093     }
00094 
00095     /* Get the object representing the location in the arrangement. */
00096     const Object& object() const
00097     {
00098       return (m_object);
00099     }
00100 
00102     const ANT* begin () const
00103     {
00104       return (m_vec);
00105     }
00106 
00108     const ANT* end () const
00109     {
00110       return (m_vec + 2);
00111     }
00112 
00114     bool operator== (const NN_Point_2& nnp) const
00115     {
00116       return (m_vec[0] == nnp.m_vec[0] &&
00117               m_vec[1] == nnp.m_vec[1]);
00118     }
00119 
00120     bool operator!= (const NN_Point_2& nnp) const 
00121     {
00122       return (m_vec[0] != nnp.m_vec[0] ||
00123               m_vec[1] != nnp.m_vec[1]);
00124     }
00125 
00126   };
00127 
00132   struct Construct_coord_iterator
00133   {
00135     const ANT* operator() (const NN_Point_2& nnp) const
00136     {
00137       return (nnp.begin());
00138     }
00139 
00141     const ANT* operator() (const NN_Point_2& nnp, int) const
00142     {
00143       return (nnp.end());
00144     }
00145   };
00146 
00147 protected:
00148 
00149   typedef CGAL::Search_traits<ANT, NN_Point_2, const ANT*,
00150                               Construct_coord_iterator>      Search_traits;
00151   typedef CGAL::Orthogonal_k_neighbor_search<Search_traits>  Neighbor_search;
00152   typedef typename Neighbor_search::iterator                 Neighbor_iterator;
00153   typedef typename Neighbor_search::Tree                     Tree;
00154 
00155   // Data members:
00156   Tree            *m_tree;        // The search tree.
00157   bool             m_is_empty;    // Is the search tree empty.
00158 
00159 private:
00160 
00162   Arr_landmarks_nearest_neighbor (const Self& );
00163 
00165   Self& operator= (const Self& );
00166 
00167 public:
00168 
00170   Arr_landmarks_nearest_neighbor () :
00171     m_tree(NULL),
00172     m_is_empty(true)
00173   {}
00174 
00176   ~Arr_landmarks_nearest_neighbor() 
00177   {
00178     clear();
00179   }
00180 
00187   template <class InputIterator>
00188   void init (InputIterator begin, InputIterator end)
00189   {
00190     CGAL_precondition_msg (m_tree == NULL,
00191                            "The search tree is already initialized.");
00192 
00193     if (begin != end)
00194     {
00195       m_tree = new Tree (begin, end);
00196       m_is_empty = false;
00197     }
00198     else
00199     {
00200       m_tree = new Tree();
00201       m_is_empty = true;
00202     }
00203 
00204     return;
00205   }
00206 
00208   void clear () 
00209   {
00210     if (m_tree != NULL)
00211       delete m_tree;
00212     m_tree = NULL;
00213     m_is_empty = true;
00214 
00215     return;
00216   }
00217 
00226   Point_2 find_nearest_neighbor (const Point_2& q, Object &obj) const
00227   {
00228     CGAL_precondition_msg (m_tree != NULL && ! m_is_empty,
00229                            "The search tree is not initialized.");
00230 
00231     // Create an NN_Point_2 object from the query point and use it to
00232     // query the search tree to find the nearest landmark point.
00233     NN_Point_2         nn_query (q);
00234     Neighbor_search    search (*m_tree, nn_query, 1);
00235     const NN_Point_2&  nearest_p = search.begin()->first;
00236 
00237     // Return the search result.
00238     obj = nearest_p.object();
00239     return (nearest_p.point());
00240   }
00241 };
00242 
00243 CGAL_END_NAMESPACE
00244 
00245 #endif
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines