BWAPI
SPAR/AIModule/BWTA/spar/src/functions.h
Go to the documentation of this file.
00001 #pragma once
00002 //#define DEBUG_DRAW 1
00003 
00004 // BWTA_DEBUG_DRAW can be set via cmake-gui
00005 #if defined(BWTA_DEBUG_DRAW) && BWTA_DEBUG_DRAW == 0
00006 #undef DEBUG_DRAW
00007 #endif
00008 //#define DEBUG_DRAW 1
00009 //#define DRAW_COLOR 1
00010 #include <iostream>
00011 #include <boost/format.hpp>
00012 #include <CGAL/MP_Float.h>
00013 #include <CGAL/Quotient.h>
00014 #include <vector>
00015 #include <map>
00016 #include <list>
00017 
00018 // SC 2011/06/19 need to include cstint.hpp with boost >= 1.42 for intmax_t and uintmax_t
00019 #include <boost/cstdint.hpp>
00020 using boost::intmax_t;
00021 using boost::uintmax_t;
00022 
00023 #include <CGAL/basic.h>
00024 // standard includes
00025 #include <iostream>
00026 #include <fstream>
00027 #include <cassert>
00028 #include <string>
00029 #include <sstream>
00030 // define the kernel
00031 
00032 #include <CGAL/Simple_cartesian.h>
00033 #include <CGAL/Filtered_kernel.h>
00034 #include <CGAL/Lazy_exact_nt.h>
00035 #include <CGAL/Segment_Delaunay_graph_traits_2.h>
00036 #include <CGAL/Segment_Delaunay_graph_2.h>
00037 #include <CGAL/Line_2.h>
00038 #include <CGAL/Direction_2.h>
00039 #include <CGAL/Polygon_2.h>
00040 #include <CGAL/Point_set_2.h>
00041 #include <CGAL/Cartesian.h>
00042 #include <CGAL/Arr_segment_traits_2.h>
00043 #include <CGAL/Arrangement_2.h>
00044 #include <CGAL/Arr_simple_point_location.h>
00045 #include <CGAL/Arr_extended_dcel.h>
00046 #include <CGAL/Gmpq.h>
00047 #include "VertexData.h"
00048 #include <BWTA/RectangleArray.h>
00049 #include <BWAPI.h>
00050 #include <BWTA/BaseLocation.h>
00051 #include <BWTA/Polygon.h>
00052 #ifdef DEBUG_DRAW
00053   #include <QtGui>
00054   #include <CGAL/Qt/GraphicsViewNavigation.h>
00055   #include <QLineF>
00056   #include <QRectF>
00057 #endif
00058 #define BWTA_FILE_VERSION 6
00059 namespace BWTA
00060 {
00061   #define PI 3.1415926
00062   #define USE_EXACT
00063   typedef CGAL::Lazy_exact_nt<CGAL::Gmpq > NumberType;
00064   #ifdef USE_EXACT
00065   #else
00066   //  typedef double NumberType;
00067   #endif
00068   typedef CGAL::Simple_cartesian< NumberType >    CK;
00069   typedef CGAL::Filtered_kernel<CK>         Kernel;
00070 
00071   typedef CGAL::Simple_cartesian< double >    CKD;
00072   typedef CGAL::Filtered_kernel<CKD>         KernelD;
00073   // typedefs for the traits and the algorithm
00074 
00075   typedef CGAL::Segment_Delaunay_graph_traits_2<Kernel> Gt;
00076   typedef CGAL::Segment_Delaunay_graph_traits_2<KernelD> GtD;
00077 
00078 #if 0
00079   typedef CGAL::Segment_Delaunay_graph_2<GtD> SDG2;
00080 #else
00081   // This makes some protected members of the base class public because get_voronoi_edges requires these.
00082   class SDG2: public CGAL::Segment_Delaunay_graph_2<GtD>
00083   {
00084     private:
00085       typedef CGAL::Segment_Delaunay_graph_2<GtD> Base;
00086 
00087     public:
00088       typedef Base::Construct_sdg_bisector_segment_2 Construct_sdg_bisector_segment_2;
00089 
00090       inline Construct_sdg_bisector_segment_2
00091       construct_sdg_bisector_segment_2_object() const {
00092         return Base::construct_sdg_bisector_segment_2_object();
00093       }
00094   };
00095 #endif
00096 
00097   typedef CGAL::Segment_Delaunay_graph_site_2< CKD > SDGS2;
00098   typedef CGAL::Point_2<CK> Point;
00099   typedef CGAL::Polygon_2<CK> PolygonCK;
00100   typedef CGAL::Line_2<CK> Line;
00101   typedef CGAL::Segment_2<CK> Segment;
00102   typedef CGAL::Circle_2<CK> Circle;
00103   typedef CGAL::Direction_2<CK> Direction;
00104   typedef CGAL::Point_set_2<Kernel> Point_set_2;
00105   typedef CGAL::Point_set_2<Kernel>::Vertex_handle  Vertex_handle;
00106 
00107   typedef CGAL::Point_2<CKD> PointD;
00108   typedef CGAL::Polygon_2<CKD> PolygonD;
00109   typedef CGAL::Line_2<CKD> LineD;
00110   typedef CGAL::Ray_2<CKD> RayD;
00111   typedef CGAL::Segment_2<CKD> SegmentD;
00112   typedef CGAL::Circle_2<CKD> CircleD;
00113   typedef CGAL::Direction_2<CKD> DirectionD;
00114 
00115   typedef CGAL::Arr_segment_traits_2<Kernel>                      Traits_2;
00116   typedef Traits_2::Point_2                                       Point_2;
00117   typedef Traits_2::X_monotone_curve_2                            Segment_2;
00118   typedef CGAL::Arr_extended_dcel<Traits_2,VertexData, Color, int>      Dcel;
00119   typedef CGAL::Arrangement_2<Traits_2, Dcel>                     Arrangement_2;
00120   struct ptcmp
00121   {
00122     bool operator()(const Point &a, const Point &b) const
00123     {
00124       return a.x()<b.x() || (a.x() == b.x() && a.y()<b.y());
00125     }
00126   };
00127 
00128   struct vhcmp
00129   {
00130     bool operator()(const Arrangement_2::Vertex_handle &a, const Arrangement_2::Vertex_handle &b) const
00131     {
00132       return a->point().x()<b->point().x() || (a->point().x() == b->point().x() && a->point().y()<b->point().y());
00133     }
00134   };
00135 
00136   struct vhradiuscmp
00137   {
00138     bool operator()(const Arrangement_2::Vertex_handle &a, const Arrangement_2::Vertex_handle &b) const
00139     {
00140       return a->data().radius<b->data().radius || (a->data().radius==b->data().radius && (a->point()<b->point()));
00141     }
00142   };
00143 
00144   void get_voronoi_edges(SDG2 &sdg, std::vector<Segment> &voronoi_diagram_edges, std::map<Point, std::set< Point >, ptcmp> &nearest, std::map<Point,double, ptcmp> &distance, const std::vector<Polygon> &polygons);
00145 
00146   double get_distance(Point a, Point b);
00147   double get_distance(PointD a, PointD b);
00148   double cast_to_double( double q);
00149   double cast_to_double( CGAL::MP_Float q);
00150   double cast_to_double( CGAL::Quotient<CGAL::MP_Float> q);
00151   double cast_to_double( CGAL::Gmpq q);
00152   double cast_to_double( CGAL::Lazy_exact_nt<CGAL::Gmpq > q);
00153   bool is_real( double q);
00154   bool is_real( CGAL::MP_Float q);
00155   bool is_real( CGAL::Quotient<CGAL::MP_Float> q);
00156   bool is_real( CGAL::Gmpq q);
00157   bool is_real( CGAL::Lazy_exact_nt<CGAL::Gmpq > q);
00158   bool load_map();
00159   bool load_resources();
00160   int str2int(std::string str);
00161   std::string int2str(int number);
00162   int max(int a, int b);
00163   int min(int a, int b);
00164   void log(const char* text, ...);
00165   void writeFile(const char* filename, const char* text, ...);
00166 
00167   template< class T>
00168   double get_distance(CGAL::Point_2<T> a, CGAL::Point_2<T> b)
00169   {
00170     return sqrt(to_double((a.x()-b.x())*(a.x()-b.x())+(a.y()-b.y())*(a.y()-b.y())));
00171   }
00172   double distance_to_border(Polygon& polygon,int width, int height);
00173 
00174   void calculate_walk_distances(const RectangleArray<bool> &read_map
00175                                ,const BWAPI::Position &start
00176                                ,int max_distance
00177                                ,RectangleArray<int> &distance_map);
00178 
00179   void calculate_walk_distances_area(const BWAPI::Position &start
00180                                     ,int width
00181                                     ,int height
00182                                     ,int max_distance
00183                                     ,RectangleArray<int> &distance_map);
00184 
00185   int get_set(std::vector<int> &a,int i);
00186   template <class _Tp1>
00187   _Tp1 get_set2(std::map<_Tp1,_Tp1> &a,_Tp1 i)
00188   {
00189     if (a.find(i)==a.end()) a[i]=i;
00190     if (i==a[i]) return i;
00191     a[i]=get_set2(a,a[i]);
00192     return a[i];
00193   }
00194   void calculate_connectivity();
00195 
00196   float max(float a, float b);
00197   float min(float a, float b);
00198 
00199   double max(double a, double b);
00200   double min(double a, double b);
00201   bool fileExists(std::string filename);
00202   int fileVersion(std::string filename);
00203 
00204   double AstarSearchDistance(BWAPI::TilePosition start, BWAPI::TilePosition end);
00205   std::pair<BWAPI::TilePosition,double> AstarSearchDistance(BWAPI::TilePosition start, std::set<BWAPI::TilePosition>& end);
00206   std::map<BWAPI::TilePosition,double> AstarSearchDistanceAll(BWAPI::TilePosition start, std::set<BWAPI::TilePosition>& end);
00207   std::vector<BWAPI::TilePosition> AstarSearchPath(BWAPI::TilePosition start, BWAPI::TilePosition end);
00208   std::vector<BWAPI::TilePosition> AstarSearchPath(BWAPI::TilePosition start, std::set<BWAPI::TilePosition> end);
00209   
00210 #ifdef DEBUG_DRAW
00211   QColor hsl2rgb(double h, double sl, double l);
00212 #endif
00213 }
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines