inline OutputIterator
 kruskal_minimum_spanning_tree(const Graph& g,
                               OutputIterator spanning_tree_edges, 
                               const bgl_named_params<P, T, R>& params)
 {
   typedef typename graph_traits<Graph>::vertices_size_type size_type;
   typedef typename graph_traits<Graph>::vertex_descriptor vertex_t;
   if (num_vertices(g) == 0) return spanning_tree_edges; // Nothing to do in this case
   typename graph_traits<Graph>::vertices_size_type n;
   n = is_default_param(get_param(params, vertex_rank))
                                  ? num_vertices(g) : 1;
   std::vector<size_type> rank_map(n);
   n = is_default_param(get_param(params, vertex_predecessor))
                                  ? num_vertices(g) : 1;
   std::vector<vertex_t> pred_map(n);
   
   return detail::kruskal_mst_impl
     (g, spanning_tree_edges, 
      choose_param
      (get_param(params, vertex_rank), 
       make_iterator_property_map
       (rank_map.begin(), 
        choose_pmap(get_param(params, vertex_index), g, vertex_index), rank_map[0])),
      choose_param
      (get_param(params, vertex_predecessor), 
       make_iterator_property_map
       (pred_map.begin(), 
        choose_const_pmap(get_param(params, vertex_index), g, vertex_index), 
        pred_map[0])),
      choose_const_pmap(get_param(params, edge_weight), g, edge_weight));
 }
Ejemplo n.º 2
0
  void breadth_first_visit
    (const IncidenceGraph& g,
     typename graph_traits<IncidenceGraph>::vertex_descriptor s,
     const bgl_named_params<P, T, R>& params)
  {
    // The graph is passed by *const* reference so that graph adaptors
    // (temporaries) can be passed into this function. However, the
    // graph is not really const since we may write to property maps
    // of the graph.
    IncidenceGraph& ng = const_cast<IncidenceGraph&>(g);

    typedef graph_traits<IncidenceGraph> Traits;
    // Buffer default
    typedef typename Traits::vertex_descriptor vertex_descriptor;
    typedef boost::queue<vertex_descriptor> queue_t;
    queue_t Q;
    detail::wrap_ref<queue_t> Qref(Q);

    breadth_first_visit
      (ng, s,
       choose_param(get_param(params, buffer_param_t()), Qref).ref,
       choose_param(get_param(params, graph_visitor),
                    make_bfs_visitor(null_visitor())),
       choose_pmap(get_param(params, vertex_color), ng, vertex_color)
       );
  }
Ejemplo n.º 3
0
    inline void
    dag_sp_dispatch1
      (const VertexListGraph& g,
       typename graph_traits<VertexListGraph>::vertex_descriptor s, 
       DistanceMap distance, WeightMap weight, ColorMap color, IndexMap id,
       DijkstraVisitor vis, const Params& params)
    {
      typedef typename property_traits<WeightMap>::value_type T;
      typename std::vector<T>::size_type n;
      n = is_default_param(distance) ? num_vertices(g) : 1;
      std::vector<T> distance_map(n);
      n = is_default_param(color) ? num_vertices(g) : 1;
      std::vector<default_color_type> color_map(n);

      dag_sp_dispatch2
        (g, s, 
         choose_param(distance, 
                      make_iterator_property_map(distance_map.begin(), id,
                                                 distance_map[0])),
         weight, 
         choose_param(color,
                      make_iterator_property_map(color_map.begin(), id, 
                                                 color_map[0])),
         id, vis, params);
    }
    bool 
    bellman_dispatch2
      (VertexAndEdgeListGraph& g, 
       typename graph_traits<VertexAndEdgeListGraph>::vertex_descriptor s,
       Size N, WeightMap weight, PredecessorMap pred, DistanceMap distance,
       const bgl_named_params<P, T, R>& params)
    {
      typedef typename property_traits<DistanceMap>::value_type D;
      bellman_visitor<> null_vis;
      typedef typename property_traits<WeightMap>::value_type weight_type;

      weight_type inf = 
          choose_param(get_param(params, distance_inf_t()), 
            std::numeric_limits<weight_type>::max BOOST_PREVENT_MACRO_SUBSTITUTION());

      typename graph_traits<VertexAndEdgeListGraph>::vertex_iterator v, v_end;
      for (tie(v, v_end) = vertices(g); v != v_end; ++v) {
        put(distance, *v, inf);
        put(pred, *v, *v);
      }
      put(distance, s, weight_type(0));
      return bellman_ford_shortest_paths
               (g, N, weight, pred, distance,
                choose_param(get_param(params, distance_combine_t()),
                             closed_plus<D>()),
                choose_param(get_param(params, distance_compare_t()),
                             std::less<D>()),
                choose_param(get_param(params, graph_visitor),
                             null_vis)
                );
    }
Ejemplo n.º 5
0
 void
 undirected_dfs(const Graph& g, 
                const bgl_named_params<P, T, R>& params)
 {
   typedef typename get_param_type< vertex_color_t, bgl_named_params<P, T, R> >::type C;
   detail::udfs_dispatch<C>::apply
     (g,
      choose_param(get_param(params, graph_visitor),
                   make_dfs_visitor(null_visitor())),
      choose_param(get_param(params, root_vertex_t()),
                   *vertices(g).first),
      params,
      get_param(params, edge_color),
      get_param(params, vertex_color)
      );
 }
Ejemplo n.º 6
0
 bool floyd_warshall_noninit_dispatch(const VertexAndEdgeListGraph& g, 
   DistanceMatrix& d, WeightMap w, 
   const bgl_named_params<P, T, R>& params)
 {
   typedef typename property_traits<WeightMap>::value_type WM;
 
   return floyd_warshall_all_pairs_shortest_paths(g, d, w,
     choose_param(get_param(params, distance_compare_t()), 
       std::less<WM>()),
     choose_param(get_param(params, distance_combine_t()), 
       closed_plus<WM>()),
     choose_param(get_param(params, distance_inf_t()), 
       std::numeric_limits<WM>::max BOOST_PREVENT_MACRO_SUBSTITUTION()),
     choose_param(get_param(params, distance_zero_t()), 
       WM()));
 }
    bool bellman_dispatch(EdgeListGraph& g, Size N, 
			  WeightMap weight, DistanceMap distance, 
			  const bgl_named_params<P, T, R>& params)
    {
      typedef typename property_traits<DistanceMap>::value_type D;
      bellman_visitor<> null_vis;
      dummy_property_map dummy_pred;
      return bellman_ford_impl
	(g, N, weight, 
	 choose_param(get_param(params, vertex_predecessor), dummy_pred),
	 distance,
	 choose_param(get_param(params, distance_combine_t()), std::plus<D>()),
	 choose_param(get_param(params, distance_compare_t()), std::less<D>()),
	 choose_param(get_param(params, graph_visitor), null_vis)
	 );
    }
Ejemplo n.º 8
0
 bool
 johnson_dispatch(VertexAndEdgeListGraph& g, 
                  DistanceMatrix& D,
                  const bgl_named_params<P, T, R>& params,
                  Weight w, VertexID id)
 {
   typedef typename property_traits<Weight>::value_type WT;
   
   return johnson_all_pairs_shortest_paths
     (g, D, id, w,
     choose_param(get_param(params, distance_compare_t()), 
       std::less<WT>()),
     choose_param(get_param(params, distance_combine_t()), 
       closed_plus<WT>()),
     choose_param(get_param(params, distance_inf_t()), 
       std::numeric_limits<WT>::max BOOST_PREVENT_MACRO_SUBSTITUTION()),
      choose_param(get_param(params, distance_zero_t()), WT()) );
 }
 bool 
 bellman_dispatch2
   (VertexAndEdgeListGraph& g, 
    detail::error_property_not_found,
    Size N, WeightMap weight, PredecessorMap pred, DistanceMap distance,
    const bgl_named_params<P, T, R>& params)
 {
   typedef typename property_traits<DistanceMap>::value_type D;
   bellman_visitor<> null_vis;
   return bellman_ford_shortest_paths
            (g, N, weight, pred, distance,
             choose_param(get_param(params, distance_combine_t()),
                          closed_plus<D>()),
             choose_param(get_param(params, distance_compare_t()),
                          std::less<D>()),
             choose_param(get_param(params, graph_visitor),
                          null_vis)
             );
 }
Ejemplo n.º 10
0
 inline void
 dag_sp_dispatch2
   (const VertexListGraph& g,
    typename graph_traits<VertexListGraph>::vertex_descriptor s, 
    DistanceMap distance, WeightMap weight, ColorMap color, IndexMap /*id*/,
    DijkstraVisitor vis, const Params& params)
 {
   typedef typename property_traits<DistanceMap>::value_type D;
   dummy_property_map p_map;
   dag_shortest_paths
     (g, s, distance, weight, color, 
      choose_param(get_param(params, vertex_predecessor), p_map),
      vis, 
      choose_param(get_param(params, distance_compare_t()), std::less<D>()),
      choose_param(get_param(params, distance_combine_t()), closed_plus<D>()),
      choose_param(get_param(params, distance_inf_t()), 
                   (std::numeric_limits<D>::max)()),
      choose_param(get_param(params, distance_zero_t()), 
                   D()));
 }
Ejemplo n.º 11
0
 inline void prim_minimum_spanning_tree
   (const VertexListGraph& g,
    PredecessorMap p_map,
    const bgl_named_params<P,T,R>& params)
 {
   detail::prim_mst_impl
     (g, 
      choose_param(get_param(params, root_vertex_t()), *vertices(g).first), 
      params.predecessor_map(p_map),
      choose_const_pmap(get_param(params, edge_weight), g, edge_weight));
 }
Ejemplo n.º 12
0
 static void apply
 (VertexListGraph& g,
  typename graph_traits<VertexListGraph>::vertex_descriptor s,
  const bgl_named_params<P, T, R>& params,
  ColorMap color)
 {
   bfs_helper
     (g, s, color,
      choose_param(get_param(params, graph_visitor),
                   make_bfs_visitor(null_visitor())),
      params);
 }
Ejemplo n.º 13
0
    inline void
    astar_dispatch1
      (VertexListGraph& g,
       typename graph_traits<VertexListGraph>::vertex_descriptor s,
       AStarHeuristic h, CostMap cost, DistanceMap distance,
       WeightMap weight, IndexMap index_map, ColorMap color,
       const Params& params)
    {
      typedef typename property_traits<WeightMap>::value_type D;
      std::vector<D> distance_map;
      std::vector<D> cost_map;
      std::vector<default_color_type> color_map;

      detail::astar_dispatch2
        (g, s, h,
         choose_param(cost, vector_property_map<D, IndexMap>(index_map)),
         choose_param(distance, vector_property_map<D, IndexMap>(index_map)),
         weight, index_map,
         choose_param(color, vector_property_map<default_color_type, IndexMap>(index_map)),
         params);
    }
Ejemplo n.º 14
0
 inline void
 astar_dispatch2
   (VertexListGraph& g,
    typename graph_traits<VertexListGraph>::vertex_descriptor s,
    AStarHeuristic h, CostMap cost, DistanceMap distance,
    WeightMap weight, IndexMap index_map, ColorMap color,
    const Params& params)
 {
   dummy_property_map p_map;
   typedef typename property_traits<CostMap>::value_type C;
   astar_search
     (g, s, h,
      choose_param(get_param(params, graph_visitor),
                   make_astar_visitor(null_visitor())),
      choose_param(get_param(params, vertex_predecessor), p_map),
      cost, distance, weight, index_map, color,
      choose_param(get_param(params, distance_compare_t()),
                   std::less<C>()),
      choose_param(get_param(params, distance_combine_t()),
                   closed_plus<C>()),
      choose_param(get_param(params, distance_inf_t()),
                   std::numeric_limits<C>::max BOOST_PREVENT_MACRO_SUBSTITUTION ()),
      choose_param(get_param(params, distance_zero_t()),
                   C()));
 }
Ejemplo n.º 15
0
    inline void
    dijkstra_dispatch2
      (const VertexListGraph& g,
       typename graph_traits<VertexListGraph>::vertex_descriptor s, 
       DistanceMap distance, WeightMap weight, IndexMap index_map,
       const Params& params)
    {
      // Default for predecessor map
      dummy_property_map p_map;

      typedef typename property_traits<DistanceMap>::value_type D;
      dijkstra_shortest_paths
        (g, s, 
         choose_param(get_param(params, vertex_predecessor), p_map),
         distance, weight, index_map, 
         choose_param(get_param(params, distance_compare_t()), 
                      std::less<D>()),
         choose_param(get_param(params, distance_combine_t()), 
                      closed_plus<D>()),
         choose_param(get_param(params, distance_inf_t()), 
                      std::numeric_limits<D>::max()),
         choose_param(get_param(params, distance_zero_t()), 
                      D()),
         choose_param(get_param(params, graph_visitor),
                      make_dijkstra_visitor(null_visitor())));
    }
 bool bellman_dispatch(EdgeListGraph& g, Size N, 
                       WeightMap weight, DistanceMap distance, 
                       const bgl_named_params<P, T, R>& params)
 {
   dummy_property_map dummy_pred;
   return 
     detail::bellman_dispatch2
       (g, 
        get_param(params, root_vertex_t()),
        N, weight,
        choose_param(get_param(params, vertex_predecessor), dummy_pred),
        distance,
        params);
 }
Ejemplo n.º 17
0
    void transitive_closure_dispatch
      (const Graph & g, GraphTC & tc,
       G_to_TC_VertexMap g_to_tc_map, VertexIndexMap index_map)
    {
      typedef typename graph_traits < GraphTC >::vertex_descriptor tc_vertex;
      typename std::vector < tc_vertex >::size_type
        n = is_default_param(g_to_tc_map) ? num_vertices(g) : 1;
      std::vector < tc_vertex > to_tc_vec(n);

      transitive_closure
        (g, tc,
         choose_param(g_to_tc_map, make_iterator_property_map
                      (to_tc_vec.begin(), index_map, to_tc_vec[0])),
         index_map);
    }
Ejemplo n.º 18
0
 static void apply
 (VertexListGraph& g,
  typename graph_traits<VertexListGraph>::vertex_descriptor s,
  EdgeType e,
  const bgl_named_params<P, T, R>& params,
  ColorMap color)
 {
   bfs_helper
     (g, s, e, color,
      choose_param(get_param(params, graph_visitor),
                   make_bfs_visitor(null_visitor())),
      params,
      boost::mpl::bool_<
      boost::is_base_and_derived<
      distributed_graph_tag,
      typename graph_traits<VertexListGraph>::traversal_category>::value>());
 }
Ejemplo n.º 19
0
 inline void
 dag_shortest_paths
   (const VertexListGraph& g,
    typename graph_traits<VertexListGraph>::vertex_descriptor s,
    const bgl_named_params<Param,Tag,Rest>& params)
 {
   // assert that the graph is directed...
   null_visitor null_vis;
   detail::dag_sp_dispatch1
     (g, s, 
      get_param(params, vertex_distance),
      choose_const_pmap(get_param(params, edge_weight), g, edge_weight),
      get_param(params, vertex_color),
      choose_const_pmap(get_param(params, vertex_index), g, vertex_index),
      choose_param(get_param(params, graph_visitor),
                   make_dijkstra_visitor(null_vis)),
      params);
 }
Ejemplo n.º 20
0
 void bfs_helper
   (VertexListGraph& g,
    typename graph_traits<VertexListGraph>::vertex_descriptor s,
    ColorMap color, 
    BFSVisitor vis,
    const bgl_named_params<P, T, R>& params)
 {
   typedef graph_traits<VertexListGraph> Traits;
   // Buffer default
   typedef typename Traits::vertex_descriptor Vertex;
   typedef boost::queue<Vertex> queue_t;
   queue_t Q;
   detail::wrap_ref<queue_t> Qref(Q);
   breadth_first_search
     (g, s, 
      choose_param(get_param(params, buffer_param_t()), Qref).ref,
      vis, color);
 }
Ejemplo n.º 21
0
 void bfs_helper
 (VertexListGraph& g,
  typename graph_traits<VertexListGraph>::vertex_descriptor s,
  EdgeType e,
  ColorMap color,
  BFSVisitor vis,
  const bgl_named_params<P, T, R>& params,
  boost::mpl::false_)
 {
   typedef graph_traits<VertexListGraph> Traits;
   // Buffer default
   typedef typename Traits::vertex_descriptor Vertex;
   typedef boost::queue<Vertex> queue_t;
   queue_t Q;
   breadth_first_search
     (g, s, e,
      choose_param(get_param(params, buffer_param_t()), boost::ref(Q)).get(),
      vis, color);
 }
Ejemplo n.º 22
0
    inline void
    dijkstra_dispatch1
      (const VertexListGraph& g,
       typename graph_traits<VertexListGraph>::vertex_descriptor s, 
       DistanceMap distance, WeightMap weight, IndexMap index_map,
       const Params& params)
    {
      // Default for distance map
      typedef typename property_traits<WeightMap>::value_type D;
      typename std::vector<D>::size_type 
        n = is_default_param(distance) ? num_vertices(g) : 1;
      std::vector<D> distance_map(n);

      detail::dijkstra_dispatch2
        (g, s, choose_param(distance, make_iterator_property_map
                            (distance_map.begin(), index_map, 
                             distance_map[0])),
         weight, index_map, params);
    }
Ejemplo n.º 23
0
 static void apply
 (VertexListGraph& g,
  typename graph_traits<VertexListGraph>::vertex_descriptor s,
  const bgl_named_params<P, T, R>& params,
  detail::error_property_not_found)
 {
   std::vector<default_color_type> color_vec(num_vertices(g));
   default_color_type c = white_color;
   null_visitor null_vis;
   
   bfs_helper
     (g, s, 
      make_iterator_property_map
      (color_vec.begin(), 
       choose_const_pmap(get_param(params, vertex_index), 
                         g, vertex_index), c),
      choose_param(get_param(params, graph_visitor),
                   make_bfs_visitor(null_vis)),
      params);
 }
Ejemplo n.º 24
0
      static void apply
      (VertexListGraph& g,
       typename graph_traits<VertexListGraph>::vertex_descriptor s,
       EdgeType e,
       const bgl_named_params<P, T, R>& params,
       param_not_found)
      {
        null_visitor null_vis;

        bfs_helper
          (g, s, e,
           make_two_bit_color_map
           (num_vertices(g),
            choose_const_pmap(get_param(params, vertex_index),
                              g, vertex_index)),
           choose_param(get_param(params, graph_visitor),
                        make_bfs_visitor(null_vis)),
           params,
           boost::mpl::bool_<
           boost::is_base_and_derived<
           distributed_graph_tag,
           typename graph_traits<VertexListGraph>::traversal_category>::value>());
      }
Ejemplo n.º 25
0
bool read_off_binary(Surface_mesh& mesh,
                     FILE* in,
                     const bool has_normals,
                     const bool has_texcoords,
                     const bool has_colors,
                     NamedParameters& np)
{
    unsigned int       i, j, idx;
    unsigned int       nV, nF, nE;
    Vec3f              p, n, c;
    Vec2f              t;
    Surface_mesh::Vertex  v;


    // binary cannot (yet) read colors
    if (has_colors) return false;


    // properties
    Surface_mesh::Vertex_property<Normal>              normals;
    Surface_mesh::Vertex_property<Texture_coordinate>  texcoords;
    if (has_normals)   normals   = mesh.vertex_property<Normal>("v:normal");
    if (has_texcoords) texcoords = mesh.vertex_property<Texture_coordinate>("v:texcoord");
    typename CGAL::Polygon_mesh_processing::GetVertexPointMap<Surface_mesh, NamedParameters>::const_type
        vpm = choose_param(get_param(np, CGAL::boost::internal_np::vertex_point),
                           get_const_property_map(CGAL::vertex_point, mesh));


    // #Vertice, #Faces, #Edges
    read(in, nV);
    read(in, nF);
    read(in, nE);
    mesh.clear();
    mesh.reserve(nV, std::max(3*nV, nE), nF);


    // read vertices: pos [normal] [color] [texcoord]
    for (i=0; i<nV && !feof(in); ++i)
    {
        // position
        read(in, p);
        Surface_mesh::Vertex v = mesh.add_vertex();
        put(vpm, v, (Point)p);

        // normal
        if (has_normals)
        {
            read(in, n);
            normals[v] = n;
        }

        // tex coord
        if (has_texcoords)
        {
            read(in, t);
            texcoords[v][0] = t[0];
            texcoords[v][1] = t[1];
        }
    }


    // read faces: #N v[1] v[2] ... v[n-1]
    std::vector<Surface_mesh::Vertex> vertices;
    for (i=0; i<nF; ++i)
    {
        read(in, nV);
        vertices.resize(nV);
        for (j=0; j<nV; ++j)
        {
            read(in, idx);
            vertices[j] = Surface_mesh::Vertex(idx);
        }
        mesh.add_face(vertices);
    }


    return true;
}
Ejemplo n.º 26
0
bool read_off_ascii(Surface_mesh& mesh,
                    FILE* in,
                    const bool has_normals,
                    const bool has_texcoords,
                    const bool has_colors,
                    NamedParameters& np)
{
    char                 line[100], *lp;
    unsigned int         i, j, items, idx, nc;
    unsigned int         nV, nF, nE;
    Vec3f                p, n, c;
    Vec2f                t;
    Surface_mesh::Vertex v;
    typename CGAL::Polygon_mesh_processing::GetVertexPointMap<Surface_mesh, NamedParameters>::const_type
        vpm = choose_param(get_param(np, CGAL::boost::internal_np::vertex_point),
                           get_const_property_map(CGAL::vertex_point, mesh));


    // properties
    Surface_mesh::Vertex_property<Normal>              normals;
    Surface_mesh::Vertex_property<Texture_coordinate>  texcoords;
    Surface_mesh::Vertex_property<Color>               colors;
    if (has_normals)   normals   = mesh.vertex_property<Normal>("v:normal");
    if (has_texcoords) texcoords = mesh.vertex_property<Texture_coordinate>("v:texcoord");
    if (has_colors)    colors    = mesh.vertex_property<Color>("v:color");


    // #Vertice, #Faces, #Edges
    items = fscanf(in, "%d %d %d\n", (int*)&nV, (int*)&nF, (int*)&nE);
    mesh.clear();
    mesh.reserve(nV, std::max(3*nV, nE), nF);


    // read vertices: pos [normal] [color] [texcoord]
    for (i=0; i<nV && !feof(in); ++i)
    {
        // read line
        lp = fgets(line, 100, in);

        // position
        items = sscanf(lp, "%f %f %f%n", &p[0], &p[1], &p[2], &nc);
        assert(items==3);
        Surface_mesh::Vertex v = mesh.add_vertex();
        put(vpm, v, (Point)p);
        lp += nc;

        // normal
        if (has_normals)
        {
            if (sscanf(lp, "%f %f %f%n", &n[0], &n[1], &n[2], &nc) == 3)
            {
                normals[v] = n;
            }
            lp += nc;
        }

        // color
        if (has_colors)
        {
            if (sscanf(lp, "%f %f %f%n", &c[0], &c[1], &c[2], &nc) == 3)
            {
                if (c[0]>1.0f || c[1]>1.0f || c[2]>1.0f) c *= (1.0/255.0);
                colors[v] = c;
            }
            lp += nc;
        }

        // tex coord
        if (has_texcoords)
        {
            items = sscanf(lp, "%f %f%n", &t[0], &t[1], &nc);
            assert(items == 2);
            texcoords[v][0] = t[0];
            texcoords[v][1] = t[1];
            lp += nc;
        }
    }



    // read faces: #N v[1] v[2] ... v[n-1]
    std::vector<Surface_mesh::Vertex> vertices;
    for (i=0; i<nF; ++i)
    {
        // read line
        lp = fgets(line, 100, in);

        // #vertices
        items = sscanf(lp, "%d%n", (int*)&nV, &nc);
        assert(items == 1);
        vertices.resize(nV);
        lp += nc;

        // indices
        for (j=0; j<nV; ++j)
        {
            items = sscanf(lp, "%d%n", (int*)&idx, &nc);
            assert(items == 1);
            vertices[j] = Surface_mesh::Vertex(idx);
            lp += nc;
        }
        mesh.add_face(vertices);
    }


    return true;
}