BWAPI
SPAR/AIModule/BWTA/vendors/CGAL/CGAL/Cartesian_converter.h
Go to the documentation of this file.
00001 // Copyright (c) 2001-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/Cartesian_kernel/include/CGAL/Cartesian_converter.h $
00019 // $Id: Cartesian_converter.h 46841 2008-11-12 11:51:47Z pmachado $
00020 // 
00021 //
00022 // Author(s)     : Sylvain Pion
00023 //                 Menelaos Karavelas <mkaravel@cse.nd.edu>
00024 
00025 #ifndef CGAL_CARTESIAN_CONVERTER_H
00026 #define CGAL_CARTESIAN_CONVERTER_H
00027 
00028 // This file contains the definition of a kernel converter, based on Cartesian
00029 // representation.  It should work between *Cartesian<A> and *Cartesian<B>,
00030 // provided you give a NT converter from A to B.
00031 // There's a Homogeneous counterpart.
00032 
00033 #include <CGAL/basic.h>
00034 #include <CGAL/NT_converter.h>
00035 #include <CGAL/Enum_converter.h>
00036 #include <CGAL/Bbox_2.h>
00037 #include <CGAL/Bbox_3.h>
00038 #include <CGAL/Origin.h>
00039 #include <vector>
00040 
00041 CGAL_BEGIN_NAMESPACE
00042 
00043 // Guess which compiler needs this work around ?
00044 namespace CGALi {
00045 template < typename K1, typename K2 >
00046 struct Default_converter {
00047   typedef typename K1::FT FT1;
00048   typedef typename K2::FT FT2;
00049   typedef ::CGAL::NT_converter<FT1, FT2> Type;
00050 };
00051 } // namespace CGALi
00052 
00053 template < class K1, class K2,
00054 //          class Converter = NT_converter<typename K1::FT, typename K2::FT> >
00055            class Converter = typename CGALi::Default_converter<K1, K2>::Type >
00056 class Cartesian_converter : public Enum_converter
00057 {
00058     typedef Enum_converter   Base;
00059 
00060 public:
00061     typedef K1         Source_kernel;
00062     typedef K2         Target_kernel;
00063     typedef Converter  Number_type_converter;
00064 
00065     using Base::operator();
00066 
00067     Origin
00068     operator()(const Origin& o) const
00069     {
00070         return o;
00071     }
00072 
00073     Null_vector
00074     operator()(const Null_vector& n) const
00075     {
00076         return n;
00077     }
00078 
00079     Bbox_2
00080     operator()(const Bbox_2& b) const
00081     {
00082         return b;
00083     }
00084 
00085     Bbox_3
00086     operator()(const Bbox_3& b) const
00087     {
00088         return b;
00089     }
00090 
00091     typename K2::FT
00092     operator()(const typename K1::FT &a) const
00093     {
00094         return c(a);
00095     }
00096 
00097     typename K2::Object_2
00098     operator()(const typename K1::Object_2 &obj) const
00099     {
00100 #define CGAL_Kernel_obj(X) \
00101       if (const typename K1::X * ptr = object_cast<typename K1::X>(&obj)) \
00102         return make_object(operator()(*ptr));
00103 
00104 #include <CGAL/Kernel/interface_macros.h>
00105 
00106       if (const std::vector<typename K1::Point_2> * ptr = object_cast<std::vector<typename K1::Point_2> >(&obj)) {
00107         std::vector<typename K2::Point_2> res;
00108         res.reserve((*ptr).size());
00109         for(unsigned int i=0; i < (*ptr).size(); i++){
00110           res.push_back(operator()((*ptr)[i]));
00111         }
00112         return make_object(res);
00113       }
00114       CGAL_error_msg("Cartesian_converter is unable to determine what is wrapped in the Object");
00115       return Object();
00116         
00117     }
00118 
00119     std::vector<Object>
00120     operator()(const std::vector<Object>& v) const
00121     {
00122       std::vector<Object> res;
00123       res.reserve(v.size());
00124       for(unsigned int i = 0; i < v.size(); i++) {
00125         res.push_back(operator()(v[i]));
00126       }
00127       return res;
00128     }
00129 
00130 
00131     typename K2::Point_2
00132     operator()(const typename K1::Point_2 &a) const
00133     {
00134         typedef typename K2::Point_2 Point_2;
00135         return Point_2(c(a.x()), c(a.y()));
00136     }
00137 
00138     typename K2::Vector_2
00139     operator()(const typename K1::Vector_2 &a) const
00140     {
00141         typedef typename K2::Vector_2  Vector_2;
00142         return Vector_2(c(a.x()), c(a.y()));
00143     }
00144 
00145     typename K2::Direction_2
00146     operator()(const typename K1::Direction_2 &a) const
00147     {
00148         typedef typename K2::Direction_2  Direction_2;
00149         return Direction_2(c(a.dx()), c(a.dy()));
00150     }
00151 
00152     typename K2::Segment_2
00153     operator()(const typename K1::Segment_2 &a) const
00154     {
00155         typedef typename K2::Segment_2  Segment_2;
00156         return Segment_2(operator()(a.source()), operator()(a.target()));
00157     }
00158 
00159     typename K2::Line_2
00160     operator()(const typename K1::Line_2 &a) const
00161     {
00162         typedef typename K2::Line_2 Line_2;
00163         return Line_2(c(a.a()), c(a.b()), c(a.c()));
00164     }
00165 
00166     typename K2::Ray_2
00167     operator()(const typename K1::Ray_2 &a) const
00168     {
00169         typedef typename K2::Ray_2  Ray_2;
00170         return Ray_2(operator()(a.source()), operator()(a.second_point()));
00171     }
00172 
00173     typename K2::Circle_2
00174     operator()(const typename K1::Circle_2 &a) const
00175     {
00176         typedef typename K2::Circle_2  Circle_2;
00177         return Circle_2(operator()(a.center()),
00178                         c(a.squared_radius()),
00179                         a.rep().orientation());
00180     }
00181 
00182     typename K2::Triangle_2
00183     operator()(const typename K1::Triangle_2 &a) const
00184     {
00185         typedef typename K2::Triangle_2  Triangle_2;
00186         return Triangle_2(operator()(a.vertex(0)),
00187                           operator()(a.vertex(1)),
00188                           operator()(a.vertex(2)));
00189     }
00190 
00191     typename K2::Iso_rectangle_2
00192     operator()(const typename K1::Iso_rectangle_2 &a) const
00193     {
00194         typedef typename K2::Iso_rectangle_2  Iso_rectangle_2;
00195         return Iso_rectangle_2(operator()((a.min)()), operator()((a.max)()), 0);
00196     }
00197 
00198 
00199     typename K2::Point_3
00200     operator()(const typename K1::Point_3 &a) const
00201     {
00202         typedef typename K2::Point_3 Point_3;
00203         return Point_3(c(a.x()), c(a.y()), c(a.z()));
00204     }
00205 
00206     typename K2::Vector_3
00207     operator()(const typename K1::Vector_3 &a) const
00208     {
00209         typedef typename K2::Vector_3  Vector_3;
00210         return Vector_3(c(a.x()), c(a.y()), c(a.z()));
00211     }
00212 
00213     typename K2::Direction_3
00214     operator()(const typename K1::Direction_3 &a) const
00215     {
00216         typedef typename K2::Direction_3  Direction_3;
00217         return Direction_3(c(a.dx()), c(a.dy()), c(a.dz()));
00218     }
00219 
00220     typename K2::Segment_3
00221     operator()(const typename K1::Segment_3 &a) const
00222     {
00223         typedef typename K2::Segment_3  Segment_3;
00224         return Segment_3(operator()(a.source()), operator()(a.target()));
00225     }
00226 
00227     typename K2::Line_3
00228     operator()(const typename K1::Line_3 &a) const
00229     {
00230         typedef typename K2::Line_3  Line_3;
00231         return Line_3(operator()(a.point()), operator()(a.to_vector()));
00232     }
00233 
00234     typename K2::Ray_3
00235     operator()(const typename K1::Ray_3 &a) const
00236     {
00237         typedef typename K2::Ray_3 Ray_3;
00238         return Ray_3(operator()(a.source()), operator()(a.second_point()));
00239     }
00240 
00241     typename K2::Sphere_3
00242     operator()(const typename K1::Sphere_3 &a) const
00243     {
00244         typedef typename K2::Sphere_3  Sphere_3;
00245         return Sphere_3(operator()(a.center()),
00246                         c(a.squared_radius()),
00247                         a.rep().orientation());
00248     }
00249 
00250     typename K2::Circle_3
00251     operator()(const typename K1::Circle_3 &a) const
00252     {
00253         typedef typename K2::Circle_3  Circle_3;
00254         return Circle_3(operator()(a.diametral_sphere()),
00255                         operator()(a.supporting_plane()),1);
00256     }
00257 
00258     typename K2::Triangle_3
00259     operator()(const typename K1::Triangle_3 &a) const
00260     {
00261         typedef typename K2::Triangle_3  Triangle_3;
00262         return Triangle_3(operator()(a.vertex(0)),
00263                           operator()(a.vertex(1)),
00264                           operator()(a.vertex(2)));
00265     }
00266 
00267     typename K2::Tetrahedron_3
00268     operator()(const typename K1::Tetrahedron_3 &a) const
00269     {
00270         typedef typename K2::Tetrahedron_3  Tetrahedron_3;
00271         return Tetrahedron_3(operator()(a.vertex(0)),
00272                              operator()(a.vertex(1)),
00273                              operator()(a.vertex(2)),
00274                              operator()(a.vertex(3)));
00275     }
00276 
00277     typename K2::Plane_3
00278     operator()(const typename K1::Plane_3 &a) const
00279     {
00280         typedef typename K2::Plane_3  Plane_3;
00281         return Plane_3(c(a.a()), c(a.b()), c(a.c()), c(a.d()));
00282     }
00283 
00284     typename K2::Iso_cuboid_3
00285     operator()(const typename K1::Iso_cuboid_3 &a) const
00286     {
00287         typedef typename K2::Iso_cuboid_3 Iso_cuboid_3;
00288         return Iso_cuboid_3(operator()((a.min)()), operator()((a.max)()), 0);
00289     }
00290 
00291     std::pair<typename K2::Point_2, typename K2::Point_2>
00292     operator() (const std::pair<typename K1::Point_2, typename K1::Point_2>& pp) const
00293     {
00294       return std::make_pair(operator()(pp.first), operator()(pp.second));
00295     }
00296 
00297 private:
00298     Converter c;
00299     K2 k;
00300 };
00301 
00302 // Specialization when converting to the same kernel,
00303 // to avoid making copies.
00304 template < class K, class C >
00305 class Cartesian_converter <K, K, C>
00306 {
00307 public:
00308   typedef K Source_kernel;
00309   typedef K Target_kernel;
00310   typedef C Number_type_converter;
00311 
00312   template < typename T >
00313   const T& operator()(const T&t) const { return t; }
00314 };
00315 
00316 CGAL_END_NAMESPACE
00317 
00318 #endif // CGAL_CARTESIAN_CONVERTER_H
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines