ClosestPair2DBase.hh

00001 //STARTHEADER
00002 // $Id: ClosestPair2DBase.hh 1761 2010-09-16 10:43:18Z soyez $
00003 //
00004 // Copyright (c) 2005-2006, Matteo Cacciari and Gavin Salam
00005 //
00006 //----------------------------------------------------------------------
00007 // This file is part of FastJet.
00008 //
00009 //  FastJet is free software; you can redistribute it and/or modify
00010 //  it under the terms of the GNU General Public License as published by
00011 //  the Free Software Foundation; either version 2 of the License, or
00012 //  (at your option) any later version.
00013 //
00014 //  The algorithms that underlie FastJet have required considerable
00015 //  development and are described in hep-ph/0512210. If you use
00016 //  FastJet as part of work towards a scientific publication, please
00017 //  include a citation to the FastJet paper.
00018 //
00019 //  FastJet is distributed in the hope that it will be useful,
00020 //  but WITHOUT ANY WARRANTY; without even the implied warranty of
00021 //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00022 //  GNU General Public License for more details.
00023 //
00024 //  You should have received a copy of the GNU General Public License
00025 //  along with FastJet; if not, write to the Free Software
00026 //  Foundation, Inc.:
00027 //      59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00028 //----------------------------------------------------------------------
00029 //ENDHEADER
00030 
00031 #ifndef __FASTJET_CLOSESTPAIR2DBASE__HH__
00032 #define __FASTJET_CLOSESTPAIR2DBASE__HH__
00033 
00034 #include<vector>
00035 #include "fastjet/internal/base.hh"
00036 
00037 FASTJET_BEGIN_NAMESPACE      // defined in fastjet/internal/base.hh
00038 
00039 //----------------------------------------------------------------------
00046 class Coord2D {
00047 public:
00048   double x, y;
00049 
00050   Coord2D() {};
00051 
00052   Coord2D(double a, double b): x(a), y(b) {};
00053 
00055   Coord2D operator-(const Coord2D & other) const {
00056     return Coord2D(x - other.x,  y - other.y);};
00057 
00059   Coord2D operator+(const Coord2D & other) const {
00060     return Coord2D(x + other.x,  y + other.y);};
00061 
00063   Coord2D operator*(double factor) const {return Coord2D(factor*x,factor*y);};
00064   friend Coord2D operator*(double factor, const Coord2D & coord) {
00065     return Coord2D(factor*coord.x,factor*coord.y);
00066   }
00067 
00069   Coord2D operator/(double divisor) const {
00070     return Coord2D(x / divisor,  y / divisor);};
00071 
00073   friend double distance2(const Coord2D & a, const Coord2D & b) {
00074     double dx = a.x - b.x, dy = a.y-b.y;
00075     return dx*dx+dy*dy;
00076   };
00078   double distance2(const Coord2D & b) const {
00079     double dx = x - b.x, dy = y-b.y;
00080     return dx*dx+dy*dy;
00081   };
00082 };
00083 
00084 
00085 //----------------------------------------------------------------------
00091 class ClosestPair2DBase {
00092 public:
00095   virtual void closest_pair(unsigned int & ID1, unsigned int & ID2, 
00096                             double & distance2) const = 0;
00097   
00099   virtual void remove(unsigned int ID) = 0;
00100  
00103   virtual unsigned int insert(const Coord2D & position) = 0;
00104 
00108   virtual unsigned int replace(unsigned int ID1, unsigned int ID2, 
00109                                const Coord2D & position) {
00110     remove(ID1); 
00111     remove(ID2); 
00112     unsigned new_ID = insert(position);
00113     return(new_ID);
00114   };
00115 
00118   virtual void replace_many(const std::vector<unsigned int> & IDs_to_remove,
00119                        const std::vector<Coord2D> & new_positions,
00120                        std::vector<unsigned int> & new_IDs) {
00121     for(unsigned i = 0; i < IDs_to_remove.size(); i++) {
00122       remove(IDs_to_remove[i]);}
00123     new_IDs.resize(0);
00124     for(unsigned i = 0; i < new_positions.size(); i++) {
00125       new_IDs.push_back(insert(new_positions[i]));}
00126   }
00127 
00128   virtual unsigned int size() = 0;
00129 
00130   virtual ~ClosestPair2DBase() {};
00131   
00132 };
00133 
00134 
00135 FASTJET_END_NAMESPACE
00136 
00137 #endif // __FASTJET_CLOSESTPAIR2DBASE__HH__