epoc32/include/stdapis/boost/graph/gursoy_atun_layout.hpp
branchSymbian2
changeset 2 2fe1408b6811
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/epoc32/include/stdapis/boost/graph/gursoy_atun_layout.hpp	Tue Mar 16 16:12:26 2010 +0000
     1.3 @@ -0,0 +1,631 @@
     1.4 +// Copyright 2004 The Trustees of Indiana University.
     1.5 +
     1.6 +// Distributed under the Boost Software License, Version 1.0.
     1.7 +// (See accompanying file LICENSE_1_0.txt or copy at
     1.8 +// http://www.boost.org/LICENSE_1_0.txt)
     1.9 +
    1.10 +//  Authors: Jeremiah Willcock
    1.11 +//           Douglas Gregor
    1.12 +//           Andrew Lumsdaine
    1.13 +#ifndef BOOST_GRAPH_GURSOY_ATUN_LAYOUT_HPP
    1.14 +#define BOOST_GRAPH_GURSOY_ATUN_LAYOUT_HPP
    1.15 +
    1.16 +// Gursoy-Atun graph layout, based on:
    1.17 +// "Neighbourhood Preserving Load Balancing: A Self-Organizing Approach"
    1.18 +// in EuroPar 2000, p. 234 of LNCS 1900
    1.19 +// http://springerlink.metapress.com/link.asp?id=pcu07ew5rhexp9yt
    1.20 +
    1.21 +#include <cmath>
    1.22 +#include <vector>
    1.23 +#include <exception>
    1.24 +#include <algorithm>
    1.25 +
    1.26 +#include <boost/graph/visitors.hpp>
    1.27 +#include <boost/graph/properties.hpp>
    1.28 +#include <boost/random/uniform_01.hpp>
    1.29 +#include <boost/random/linear_congruential.hpp>
    1.30 +#include <boost/shared_ptr.hpp>
    1.31 +#include <boost/graph/breadth_first_search.hpp>
    1.32 +#include <boost/graph/dijkstra_shortest_paths.hpp>
    1.33 +#include <boost/graph/named_function_params.hpp>
    1.34 +
    1.35 +namespace boost { 
    1.36 +
    1.37 +namespace detail {
    1.38 +
    1.39 +struct over_distance_limit : public std::exception {};
    1.40 +
    1.41 +template <typename PositionMap, typename NodeDistanceMap,  typename Topology,
    1.42 +          typename Graph>
    1.43 +struct update_position_visitor {
    1.44 +  typedef typename Topology::point_type Point;
    1.45 +  PositionMap position_map;
    1.46 +  NodeDistanceMap node_distance;
    1.47 +  const Topology& space;
    1.48 +  Point input_vector;
    1.49 +  double distance_limit;
    1.50 +  double learning_constant;
    1.51 +  double falloff_ratio;
    1.52 +
    1.53 +  typedef boost::on_examine_vertex event_filter;
    1.54 +
    1.55 +  typedef typename graph_traits<Graph>::vertex_descriptor
    1.56 +    vertex_descriptor;
    1.57 +
    1.58 +  update_position_visitor(PositionMap position_map,
    1.59 +                          NodeDistanceMap node_distance,
    1.60 +                          const Topology& space,
    1.61 +                          const Point& input_vector,
    1.62 +                          double distance_limit,
    1.63 +                          double learning_constant,
    1.64 +                          double falloff_ratio):
    1.65 +    position_map(position_map), node_distance(node_distance), 
    1.66 +    space(space),
    1.67 +    input_vector(input_vector), distance_limit(distance_limit),
    1.68 +    learning_constant(learning_constant), falloff_ratio(falloff_ratio) {}
    1.69 +
    1.70 +  void operator()(vertex_descriptor v, const Graph&) const 
    1.71 +  {
    1.72 +#ifndef BOOST_NO_STDC_NAMESPACE
    1.73 +    using std::pow;
    1.74 +#endif
    1.75 +
    1.76 +    if (get(node_distance, v) > distance_limit)
    1.77 +      throw over_distance_limit();
    1.78 +    Point old_position = get(position_map, v);
    1.79 +    double distance = get(node_distance, v);
    1.80 +    double fraction = 
    1.81 +      learning_constant * pow(falloff_ratio, distance * distance);
    1.82 +    put(position_map, v,
    1.83 +        space.move_position_toward(old_position, fraction, input_vector));
    1.84 +  }
    1.85 +};
    1.86 +
    1.87 +template<typename EdgeWeightMap>
    1.88 +struct gursoy_shortest
    1.89 +{
    1.90 +  template<typename Graph, typename NodeDistanceMap, typename UpdatePosition>
    1.91 +  static inline void 
    1.92 +  run(const Graph& g, typename graph_traits<Graph>::vertex_descriptor s,
    1.93 +      NodeDistanceMap node_distance,  UpdatePosition& update_position,
    1.94 +      EdgeWeightMap weight)
    1.95 +  {
    1.96 +    boost::dijkstra_shortest_paths(g, s, weight_map(weight).
    1.97 +      visitor(boost::make_dijkstra_visitor(std::make_pair(
    1.98 +       boost::record_distances(node_distance, boost::on_edge_relaxed()),
    1.99 +        update_position))));
   1.100 +  }
   1.101 +};
   1.102 +
   1.103 +template<>
   1.104 +struct gursoy_shortest<dummy_property_map>
   1.105 +{
   1.106 +  template<typename Graph, typename NodeDistanceMap, typename UpdatePosition>
   1.107 +  static inline void 
   1.108 +  run(const Graph& g, typename graph_traits<Graph>::vertex_descriptor s,
   1.109 +      NodeDistanceMap node_distance,  UpdatePosition& update_position,
   1.110 +      dummy_property_map)
   1.111 +  {
   1.112 +    boost::breadth_first_search(g, s,
   1.113 +      visitor(boost::make_bfs_visitor(std::make_pair(
   1.114 +        boost::record_distances(node_distance, boost::on_tree_edge()),
   1.115 +        update_position))));
   1.116 +  }
   1.117 +};
   1.118 +
   1.119 +} // namespace detail
   1.120 +
   1.121 +template <typename VertexListAndIncidenceGraph,  typename Topology,
   1.122 +          typename PositionMap, typename Diameter, typename VertexIndexMap, 
   1.123 +          typename EdgeWeightMap>
   1.124 +void 
   1.125 +gursoy_atun_step
   1.126 +  (const VertexListAndIncidenceGraph& graph,  
   1.127 +   const Topology& space,
   1.128 +   PositionMap position,
   1.129 +   Diameter diameter,
   1.130 +   double learning_constant,
   1.131 +   VertexIndexMap vertex_index_map,
   1.132 +   EdgeWeightMap weight)
   1.133 +{
   1.134 +#ifndef BOOST_NO_STDC_NAMESPACE
   1.135 +  using std::pow;
   1.136 +  using std::exp;
   1.137 +#endif
   1.138 +
   1.139 +  typedef typename graph_traits<VertexListAndIncidenceGraph>::vertex_iterator
   1.140 +    vertex_iterator;
   1.141 +  typedef typename graph_traits<VertexListAndIncidenceGraph>::vertex_descriptor
   1.142 +    vertex_descriptor;
   1.143 +  typedef typename Topology::point_type point_type;
   1.144 +  vertex_iterator i, iend;
   1.145 +  std::vector<double> distance_from_input_vector(num_vertices(graph));
   1.146 +  typedef boost::iterator_property_map<std::vector<double>::iterator, 
   1.147 +                                       VertexIndexMap,
   1.148 +                                       double, double&>
   1.149 +    DistanceFromInputMap;
   1.150 +  DistanceFromInputMap distance_from_input(distance_from_input_vector.begin(),
   1.151 +                                           vertex_index_map);
   1.152 +  std::vector<double> node_distance_map_vector(num_vertices(graph));
   1.153 +  typedef boost::iterator_property_map<std::vector<double>::iterator, 
   1.154 +                                       VertexIndexMap,
   1.155 +                                       double, double&>
   1.156 +    NodeDistanceMap;
   1.157 +  NodeDistanceMap node_distance(node_distance_map_vector.begin(),
   1.158 +                                vertex_index_map);
   1.159 +  point_type input_vector = space.random_point();
   1.160 +  vertex_descriptor min_distance_loc 
   1.161 +    = graph_traits<VertexListAndIncidenceGraph>::null_vertex();
   1.162 +  double min_distance = 0.0;
   1.163 +  bool min_distance_unset = true;
   1.164 +  for (boost::tie(i, iend) = vertices(graph); i != iend; ++i) {
   1.165 +    double this_distance = space.distance(get(position, *i), input_vector);
   1.166 +    put(distance_from_input, *i, this_distance);
   1.167 +    if (min_distance_unset || this_distance < min_distance) {
   1.168 +      min_distance = this_distance;
   1.169 +      min_distance_loc = *i;
   1.170 +    }
   1.171 +    min_distance_unset = false;
   1.172 +  }
   1.173 +  assert (!min_distance_unset); // Graph must have at least one vertex
   1.174 +  boost::detail::update_position_visitor<
   1.175 +      PositionMap, NodeDistanceMap, Topology,
   1.176 +      VertexListAndIncidenceGraph> 
   1.177 +    update_position(position, node_distance, space,
   1.178 +                    input_vector, diameter, learning_constant, 
   1.179 +                    exp(-1. / (2 * diameter * diameter)));
   1.180 +  std::fill(node_distance_map_vector.begin(), node_distance_map_vector.end(), 0);
   1.181 +  try {
   1.182 +    typedef detail::gursoy_shortest<EdgeWeightMap> shortest;
   1.183 +    shortest::run(graph, min_distance_loc, node_distance, update_position,
   1.184 +                  weight);    
   1.185 +  } catch (detail::over_distance_limit) { 
   1.186 +    /* Thrown to break out of BFS or Dijkstra early */ 
   1.187 +  }
   1.188 +}
   1.189 +
   1.190 +template <typename VertexListAndIncidenceGraph,  typename Topology,
   1.191 +          typename PositionMap, typename VertexIndexMap, 
   1.192 +          typename EdgeWeightMap>
   1.193 +void gursoy_atun_refine(const VertexListAndIncidenceGraph& graph,  
   1.194 +                        const Topology& space,
   1.195 +                        PositionMap position,
   1.196 +                        int nsteps,
   1.197 +                        double diameter_initial,
   1.198 +                        double diameter_final,
   1.199 +                        double learning_constant_initial,
   1.200 +                        double learning_constant_final,
   1.201 +                        VertexIndexMap vertex_index_map,
   1.202 +                        EdgeWeightMap weight) 
   1.203 +{
   1.204 +#ifndef BOOST_NO_STDC_NAMESPACE
   1.205 +  using std::pow;
   1.206 +  using std::exp;
   1.207 +#endif
   1.208 +
   1.209 +  typedef typename graph_traits<VertexListAndIncidenceGraph>::vertex_iterator
   1.210 +    vertex_iterator;
   1.211 +  typedef typename graph_traits<VertexListAndIncidenceGraph>::vertex_descriptor
   1.212 +    vertex_descriptor;
   1.213 +  typedef typename Topology::point_type point_type;
   1.214 +  vertex_iterator i, iend;
   1.215 +  double diameter_ratio = (double)diameter_final / diameter_initial;
   1.216 +  double learning_constant_ratio = 
   1.217 +    learning_constant_final / learning_constant_initial;
   1.218 +  std::vector<double> distance_from_input_vector(num_vertices(graph));
   1.219 +  typedef boost::iterator_property_map<std::vector<double>::iterator, 
   1.220 +                                       VertexIndexMap,
   1.221 +                                       double, double&>
   1.222 +    DistanceFromInputMap;
   1.223 +  DistanceFromInputMap distance_from_input(distance_from_input_vector.begin(),
   1.224 +                                           vertex_index_map);
   1.225 +  std::vector<int> node_distance_map_vector(num_vertices(graph));
   1.226 +  typedef boost::iterator_property_map<std::vector<int>::iterator, 
   1.227 +                                       VertexIndexMap, double, double&>
   1.228 +    NodeDistanceMap;
   1.229 +  NodeDistanceMap node_distance(node_distance_map_vector.begin(),
   1.230 +                                vertex_index_map);
   1.231 +  for (int round = 0; round < nsteps; ++round) {
   1.232 +    double part_done = (double)round / (nsteps - 1);
   1.233 +    int diameter = (int)(diameter_initial * pow(diameter_ratio, part_done));
   1.234 +    double learning_constant = 
   1.235 +      learning_constant_initial * pow(learning_constant_ratio, part_done);
   1.236 +    gursoy_atun_step(graph, space, position, diameter, learning_constant, 
   1.237 +                     vertex_index_map, weight);
   1.238 +  }
   1.239 +}
   1.240 +
   1.241 +template <typename VertexListAndIncidenceGraph,  typename Topology,
   1.242 +          typename PositionMap, typename VertexIndexMap, 
   1.243 +          typename EdgeWeightMap>
   1.244 +void gursoy_atun_layout(const VertexListAndIncidenceGraph& graph,  
   1.245 +                        const Topology& space,
   1.246 +                        PositionMap position,
   1.247 +                        int nsteps,
   1.248 +                        double diameter_initial,
   1.249 +                        double diameter_final,
   1.250 +                        double learning_constant_initial,
   1.251 +                        double learning_constant_final,
   1.252 +                        VertexIndexMap vertex_index_map,
   1.253 +                        EdgeWeightMap weight)
   1.254 +{
   1.255 +  typedef typename graph_traits<VertexListAndIncidenceGraph>::vertex_iterator
   1.256 +    vertex_iterator;
   1.257 +  vertex_iterator i, iend;
   1.258 +  for (boost::tie(i, iend) = vertices(graph); i != iend; ++i) {
   1.259 +    put(position, *i, space.random_point());
   1.260 +  }
   1.261 +  gursoy_atun_refine(graph, space,
   1.262 +                     position, nsteps,
   1.263 +                     diameter_initial, diameter_final, 
   1.264 +                     learning_constant_initial, learning_constant_final,
   1.265 +                     vertex_index_map, weight);
   1.266 +}
   1.267 +
   1.268 +template <typename VertexListAndIncidenceGraph,  typename Topology,
   1.269 +          typename PositionMap, typename VertexIndexMap>
   1.270 +void gursoy_atun_layout(const VertexListAndIncidenceGraph& graph,  
   1.271 +                        const Topology& space,
   1.272 +                        PositionMap position,
   1.273 +                        int nsteps,
   1.274 +                        double diameter_initial,
   1.275 +                        double diameter_final,
   1.276 +                        double learning_constant_initial,
   1.277 +                        double learning_constant_final,
   1.278 +                        VertexIndexMap vertex_index_map)
   1.279 +{
   1.280 +  gursoy_atun_layout(graph, space, position, nsteps, 
   1.281 +                     diameter_initial, diameter_final, 
   1.282 +                     learning_constant_initial, learning_constant_final, 
   1.283 +                     vertex_index_map, dummy_property_map());
   1.284 +}
   1.285 +
   1.286 +template <typename VertexListAndIncidenceGraph, typename Topology,
   1.287 +          typename PositionMap>
   1.288 +void gursoy_atun_layout(const VertexListAndIncidenceGraph& graph,  
   1.289 +                        const Topology& space,
   1.290 +                        PositionMap position,
   1.291 +                        int nsteps,
   1.292 +                        double diameter_initial,
   1.293 +                        double diameter_final = 1.0,
   1.294 +                        double learning_constant_initial = 0.8,
   1.295 +                        double learning_constant_final = 0.2)
   1.296 +{ 
   1.297 +  gursoy_atun_layout(graph, space, position, nsteps, diameter_initial,
   1.298 +                     diameter_final, learning_constant_initial,
   1.299 +                     learning_constant_final, get(vertex_index, graph)); 
   1.300 +}
   1.301 +
   1.302 +template <typename VertexListAndIncidenceGraph, typename Topology,
   1.303 +          typename PositionMap>
   1.304 +void gursoy_atun_layout(const VertexListAndIncidenceGraph& graph,  
   1.305 +                        const Topology& space,
   1.306 +                        PositionMap position,
   1.307 +                        int nsteps)
   1.308 +{
   1.309 +#ifndef BOOST_NO_STDC_NAMESPACE
   1.310 +  using std::sqrt;
   1.311 +#endif
   1.312 +
   1.313 +  gursoy_atun_layout(graph, space, position, nsteps, 
   1.314 +                     sqrt((double)num_vertices(graph)));
   1.315 +}
   1.316 +
   1.317 +template <typename VertexListAndIncidenceGraph, typename Topology,
   1.318 +          typename PositionMap>
   1.319 +void gursoy_atun_layout(const VertexListAndIncidenceGraph& graph,  
   1.320 +                        const Topology& space,
   1.321 +                        PositionMap position)
   1.322 +{
   1.323 +  gursoy_atun_layout(graph, space, position, num_vertices(graph));
   1.324 +}
   1.325 +
   1.326 +template<typename VertexListAndIncidenceGraph, typename Topology,
   1.327 +         typename PositionMap, typename P, typename T, typename R>
   1.328 +void 
   1.329 +gursoy_atun_layout(const VertexListAndIncidenceGraph& graph,  
   1.330 +                   const Topology& space,
   1.331 +                   PositionMap position,
   1.332 +                   const bgl_named_params<P,T,R>& params)
   1.333 +{
   1.334 +#ifndef BOOST_NO_STDC_NAMESPACE
   1.335 +  using std::sqrt;
   1.336 +#endif
   1.337 +
   1.338 +  std::pair<double, double> diam(sqrt(double(num_vertices(graph))), 1.0);
   1.339 +  std::pair<double, double> learn(0.8, 0.2);
   1.340 +  gursoy_atun_layout(graph, space, position,
   1.341 +                     choose_param(get_param(params, iterations_t()),
   1.342 +                                  num_vertices(graph)),
   1.343 +                     choose_param(get_param(params, diameter_range_t()), 
   1.344 +                                  diam).first,
   1.345 +                     choose_param(get_param(params, diameter_range_t()), 
   1.346 +                                  diam).second,
   1.347 +                     choose_param(get_param(params, learning_constant_range_t()), 
   1.348 +                                  learn).first,
   1.349 +                     choose_param(get_param(params, learning_constant_range_t()), 
   1.350 +                                  learn).second,
   1.351 +                     choose_const_pmap(get_param(params, vertex_index), graph,
   1.352 +                                       vertex_index),
   1.353 +                     choose_param(get_param(params, edge_weight), 
   1.354 +                                  dummy_property_map()));
   1.355 +}
   1.356 +
   1.357 +/***********************************************************
   1.358 + * Topologies                                              *
   1.359 + ***********************************************************/
   1.360 +template<std::size_t Dims>
   1.361 +class convex_topology 
   1.362 +{
   1.363 +  struct point 
   1.364 +  {
   1.365 +    point() { }
   1.366 +    double& operator[](std::size_t i) {return values[i];}
   1.367 +    const double& operator[](std::size_t i) const {return values[i];}
   1.368 +
   1.369 +  private:
   1.370 +    double values[Dims];
   1.371 +  };
   1.372 +
   1.373 + public:
   1.374 +  typedef point point_type;
   1.375 +
   1.376 +  double distance(point a, point b) const 
   1.377 +  {
   1.378 +    double dist = 0;
   1.379 +    for (std::size_t i = 0; i < Dims; ++i) {
   1.380 +      double diff = b[i] - a[i];
   1.381 +      dist += diff * diff;
   1.382 +    }
   1.383 +    // Exact properties of the distance are not important, as long as
   1.384 +    // < on what this returns matches real distances
   1.385 +    return dist;
   1.386 +  }
   1.387 +
   1.388 +  point move_position_toward(point a, double fraction, point b) const 
   1.389 +  {
   1.390 +    point result;
   1.391 +    for (std::size_t i = 0; i < Dims; ++i)
   1.392 +      result[i] = a[i] + (b[i] - a[i]) * fraction;
   1.393 +    return result;
   1.394 +  }
   1.395 +};
   1.396 +
   1.397 +template<std::size_t Dims,
   1.398 +         typename RandomNumberGenerator = minstd_rand>
   1.399 +class hypercube_topology : public convex_topology<Dims>
   1.400 +{
   1.401 +  typedef uniform_01<RandomNumberGenerator, double> rand_t;
   1.402 +
   1.403 + public:
   1.404 +  typedef typename convex_topology<Dims>::point_type point_type;
   1.405 +
   1.406 +  explicit hypercube_topology(double scaling = 1.0) 
   1.407 +    : gen_ptr(new RandomNumberGenerator), rand(new rand_t(*gen_ptr)), 
   1.408 +      scaling(scaling) 
   1.409 +  { }
   1.410 +
   1.411 +  hypercube_topology(RandomNumberGenerator& gen, double scaling = 1.0) 
   1.412 +    : gen_ptr(), rand(new rand_t(gen)), scaling(scaling) { }
   1.413 +                     
   1.414 +  point_type random_point() const 
   1.415 +  {
   1.416 +    point_type p;
   1.417 +    for (std::size_t i = 0; i < Dims; ++i)
   1.418 +      p[i] = (*rand)() * scaling;
   1.419 +    return p;
   1.420 +  }
   1.421 +
   1.422 + private:
   1.423 +  shared_ptr<RandomNumberGenerator> gen_ptr;
   1.424 +  shared_ptr<rand_t> rand;
   1.425 +  double scaling;
   1.426 +};
   1.427 +
   1.428 +template<typename RandomNumberGenerator = minstd_rand>
   1.429 +class square_topology : public hypercube_topology<2, RandomNumberGenerator>
   1.430 +{
   1.431 +  typedef hypercube_topology<2, RandomNumberGenerator> inherited;
   1.432 +
   1.433 + public:
   1.434 +  explicit square_topology(double scaling = 1.0) : inherited(scaling) { }
   1.435 +  
   1.436 +  square_topology(RandomNumberGenerator& gen, double scaling = 1.0) 
   1.437 +    : inherited(gen, scaling) { }
   1.438 +};
   1.439 +
   1.440 +template<typename RandomNumberGenerator = minstd_rand>
   1.441 +class cube_topology : public hypercube_topology<3, RandomNumberGenerator>
   1.442 +{
   1.443 +  typedef hypercube_topology<3, RandomNumberGenerator> inherited;
   1.444 +
   1.445 + public:
   1.446 +  explicit cube_topology(double scaling = 1.0) : inherited(scaling) { }
   1.447 +  
   1.448 +  cube_topology(RandomNumberGenerator& gen, double scaling = 1.0) 
   1.449 +    : inherited(gen, scaling) { }
   1.450 +};
   1.451 +
   1.452 +template<std::size_t Dims,
   1.453 +         typename RandomNumberGenerator = minstd_rand>
   1.454 +class ball_topology : public convex_topology<Dims>
   1.455 +{
   1.456 +  typedef uniform_01<RandomNumberGenerator, double> rand_t;
   1.457 +
   1.458 + public:
   1.459 +  typedef typename convex_topology<Dims>::point_type point_type;
   1.460 +
   1.461 +  explicit ball_topology(double radius = 1.0) 
   1.462 +    : gen_ptr(new RandomNumberGenerator), rand(new rand_t(*gen_ptr)), 
   1.463 +      radius(radius) 
   1.464 +  { }
   1.465 +
   1.466 +  ball_topology(RandomNumberGenerator& gen, double radius = 1.0) 
   1.467 +    : gen_ptr(), rand(new rand_t(gen)), radius(radius) { }
   1.468 +                     
   1.469 +  point_type random_point() const 
   1.470 +  {
   1.471 +    point_type p;
   1.472 +    double dist_sum;
   1.473 +    do {
   1.474 +      dist_sum = 0.0;
   1.475 +      for (std::size_t i = 0; i < Dims; ++i) {
   1.476 +        double x = (*rand)() * 2*radius - radius;
   1.477 +        p[i] = x;
   1.478 +        dist_sum += x * x;
   1.479 +      }
   1.480 +    } while (dist_sum > radius*radius);
   1.481 +    return p;
   1.482 +  }
   1.483 +
   1.484 + private:
   1.485 +  shared_ptr<RandomNumberGenerator> gen_ptr;
   1.486 +  shared_ptr<rand_t> rand;
   1.487 +  double radius;
   1.488 +};
   1.489 +
   1.490 +template<typename RandomNumberGenerator = minstd_rand>
   1.491 +class circle_topology : public ball_topology<2, RandomNumberGenerator>
   1.492 +{
   1.493 +  typedef ball_topology<2, RandomNumberGenerator> inherited;
   1.494 +
   1.495 + public:
   1.496 +  explicit circle_topology(double radius = 1.0) : inherited(radius) { }
   1.497 +  
   1.498 +  circle_topology(RandomNumberGenerator& gen, double radius = 1.0) 
   1.499 +    : inherited(gen, radius) { }
   1.500 +};
   1.501 +
   1.502 +template<typename RandomNumberGenerator = minstd_rand>
   1.503 +class sphere_topology : public ball_topology<3, RandomNumberGenerator>
   1.504 +{
   1.505 +  typedef ball_topology<3, RandomNumberGenerator> inherited;
   1.506 +
   1.507 + public:
   1.508 +  explicit sphere_topology(double radius = 1.0) : inherited(radius) { }
   1.509 +  
   1.510 +  sphere_topology(RandomNumberGenerator& gen, double radius = 1.0) 
   1.511 +    : inherited(gen, radius) { }
   1.512 +};
   1.513 +
   1.514 +template<typename RandomNumberGenerator = minstd_rand>
   1.515 +class heart_topology 
   1.516 +{
   1.517 +  // Heart is defined as the union of three shapes:
   1.518 +  // Square w/ corners (+-1000, -1000), (0, 0), (0, -2000)
   1.519 +  // Circle centered at (-500, -500) radius 500*sqrt(2)
   1.520 +  // Circle centered at (500, -500) radius 500*sqrt(2)
   1.521 +  // Bounding box (-1000, -2000) - (1000, 500*(sqrt(2) - 1))
   1.522 +
   1.523 +  struct point 
   1.524 +  {
   1.525 +    point() { values[0] = 0.0; values[1] = 0.0; }
   1.526 +    point(double x, double y) { values[0] = x; values[1] = y; }
   1.527 +
   1.528 +    double& operator[](std::size_t i)       { return values[i]; }
   1.529 +    double  operator[](std::size_t i) const { return values[i]; }
   1.530 +
   1.531 +  private:
   1.532 +    double values[2];
   1.533 +  };
   1.534 +
   1.535 +  bool in_heart(point p) const 
   1.536 +  {
   1.537 +#ifndef BOOST_NO_STDC_NAMESPACE
   1.538 +    using std::abs;
   1.539 +    using std::pow;
   1.540 +#endif
   1.541 +
   1.542 +    if (p[1] < abs(p[0]) - 2000) return false; // Bottom
   1.543 +    if (p[1] <= -1000) return true; // Diagonal of square
   1.544 +    if (pow(p[0] - -500, 2) + pow(p[1] - -500, 2) <= 500000)
   1.545 +      return true; // Left circle
   1.546 +    if (pow(p[0] - 500, 2) + pow(p[1] - -500, 2) <= 500000)
   1.547 +      return true; // Right circle
   1.548 +    return false;
   1.549 +  }
   1.550 +
   1.551 +  bool segment_within_heart(point p1, point p2) const 
   1.552 +  {
   1.553 +    // Assumes that p1 and p2 are within the heart
   1.554 +    if ((p1[0] < 0) == (p2[0] < 0)) return true; // Same side of symmetry line
   1.555 +    if (p1[0] == p2[0]) return true; // Vertical
   1.556 +    double slope = (p2[1] - p1[1]) / (p2[0] - p1[0]);
   1.557 +    double intercept = p1[1] - p1[0] * slope;
   1.558 +    if (intercept > 0) return false; // Crosses between circles
   1.559 +    return true;
   1.560 +  }
   1.561 +
   1.562 +  typedef uniform_01<RandomNumberGenerator, double> rand_t;
   1.563 +
   1.564 + public:
   1.565 +  typedef point point_type;
   1.566 +
   1.567 +  heart_topology() 
   1.568 +    : gen_ptr(new RandomNumberGenerator), rand(new rand_t(*gen_ptr)) { }
   1.569 +
   1.570 +  heart_topology(RandomNumberGenerator& gen) 
   1.571 +    : gen_ptr(), rand(new rand_t(gen)) { }
   1.572 +
   1.573 +  point random_point() const 
   1.574 +  {
   1.575 +#ifndef BOOST_NO_STDC_NAMESPACE
   1.576 +    using std::sqrt;
   1.577 +#endif
   1.578 +
   1.579 +    point result;
   1.580 +    double sqrt2 = sqrt(2.);
   1.581 +    do {
   1.582 +      result[0] = (*rand)() * (1000 + 1000 * sqrt2) - (500 + 500 * sqrt2);
   1.583 +      result[1] = (*rand)() * (2000 + 500 * (sqrt2 - 1)) - 2000;
   1.584 +    } while (!in_heart(result));
   1.585 +    return result;
   1.586 +  }
   1.587 +
   1.588 +  double distance(point a, point b) const 
   1.589 +  {
   1.590 +#ifndef BOOST_NO_STDC_NAMESPACE
   1.591 +    using std::sqrt;
   1.592 +#endif
   1.593 +    if (segment_within_heart(a, b)) {
   1.594 +      // Straight line
   1.595 +      return sqrt((b[0] - a[0]) * (b[0] - a[0]) + (b[1] - a[1]) * (b[1] - a[1]));
   1.596 +    } else {
   1.597 +      // Straight line bending around (0, 0)
   1.598 +      return sqrt(a[0] * a[0] + a[1] * a[1]) + sqrt(b[0] * b[0] + b[1] * b[1]);
   1.599 +    }
   1.600 +  }
   1.601 +
   1.602 +  point move_position_toward(point a, double fraction, point b) const 
   1.603 +  {
   1.604 +#ifndef BOOST_NO_STDC_NAMESPACE
   1.605 +    using std::sqrt;
   1.606 +#endif
   1.607 +
   1.608 +    if (segment_within_heart(a, b)) {
   1.609 +      // Straight line
   1.610 +      return point(a[0] + (b[0] - a[0]) * fraction,
   1.611 +                   a[1] + (b[1] - a[1]) * fraction);
   1.612 +    } else {
   1.613 +      double distance_to_point_a = sqrt(a[0] * a[0] + a[1] * a[1]);
   1.614 +      double distance_to_point_b = sqrt(b[0] * b[0] + b[1] * b[1]);
   1.615 +      double location_of_point = distance_to_point_a / 
   1.616 +                                   (distance_to_point_a + distance_to_point_b);
   1.617 +      if (fraction < location_of_point)
   1.618 +        return point(a[0] * (1 - fraction / location_of_point), 
   1.619 +                     a[1] * (1 - fraction / location_of_point));
   1.620 +      else
   1.621 +        return point(
   1.622 +          b[0] * ((fraction - location_of_point) / (1 - location_of_point)),
   1.623 +          b[1] * ((fraction - location_of_point) / (1 - location_of_point)));
   1.624 +    }
   1.625 +  }
   1.626 +
   1.627 + private:
   1.628 +  shared_ptr<RandomNumberGenerator> gen_ptr;
   1.629 +  shared_ptr<rand_t> rand;
   1.630 +};
   1.631 +
   1.632 +} // namespace boost
   1.633 +
   1.634 +#endif // BOOST_GRAPH_GURSOY_ATUN_LAYOUT_HPP