Shinya Kitaoka 810553
#pragma once
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
#ifndef TCG_MESH_BGL_H
Toshihiro Shimizu 890ddd
#define TCG_MESH_BGL_H
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
// TCG includes
Toshihiro Shimizu 890ddd
#include "mesh.h"
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
// Boost includes
Toshihiro Shimizu 890ddd
#include <boost graph="" graph_traits.hpp=""></boost>
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
/*
Toshihiro Shimizu 890ddd
  \file     tcg_mesh_bgl.h
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
  \brief    This file contains TCG adapters to Boost's Graph Library (BGL).
Toshihiro Shimizu 890ddd
*/
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//************************************************************************************
Toshihiro Shimizu 890ddd
//    boost::graph_traits to tcg::Mesh concept type
Toshihiro Shimizu 890ddd
//************************************************************************************
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
namespace boost {
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
template <typename e,="" f="" typename="" v,=""></typename>
Toshihiro Shimizu 890ddd
struct graph_traits<typename e,="" f="" tcg::mesh<v,="">> {</typename>
Shinya Kitaoka 120a6e
  // Preliminar typedefs
Shinya Kitaoka 120a6e
  typedef typename tcg::Mesh<v, e,="" f=""> mesh_type;</v,>
Shinya Kitaoka 120a6e
  typedef typename mesh_type::vertex_type vertex_type;
Shinya Kitaoka 120a6e
  typedef typename mesh_type::edge_type edge_type;
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  typedef typename vertex_type::edges_const_iterator edge_const_iterator;
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  // V/E descriptors
Shinya Kitaoka 120a6e
  typedef int vertex_descriptor;
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  struct edge_descriptor  // NOTE: Can't use std::pair due to std conflicts
Shinya Kitaoka 120a6e
  {
Shinya Kitaoka 120a6e
    int m_e, m_src;
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
    edge_descriptor() : m_e(-1), m_src(-1) {}
Shinya Kitaoka 120a6e
    edge_descriptor(int e, int src) : m_e(e), m_src(src) {}
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
    bool operator==(const edge_descriptor &e_d) {
Shinya Kitaoka 120a6e
      return m_e == e_d.m_e;
Shinya Kitaoka 120a6e
    }  // Undirected
Shinya Kitaoka 120a6e
    bool operator!=(const edge_descriptor &e_d) { return !operator==(e_d); }
Shinya Kitaoka 120a6e
  };
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  // Iterators
Shinya Kitaoka 120a6e
  struct out_edge_iterator : public edge_const_iterator {
Shinya Kitaoka 120a6e
    int m_src;
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
    out_edge_iterator() : edge_const_iterator(), m_src(-1) {}
Shinya Kitaoka 120a6e
    out_edge_iterator(const edge_const_iterator &it, int src)
Shinya Kitaoka 120a6e
        : edge_const_iterator(it), m_src(src) {}
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
    edge_descriptor operator*() const {
Shinya Kitaoka 120a6e
      return edge_descriptor(edge_const_iterator::operator*(), m_src);
Shinya Kitaoka 120a6e
    }
Shinya Kitaoka 120a6e
  };
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  typedef typename tcg::list<vertex_type>::const_iterator vertex_iterator;</vertex_type>
Shinya Kitaoka 120a6e
  typedef typename tcg::list<edge_type>::const_iterator edge_iterator;</edge_type>
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  // Categories
Shinya Kitaoka 120a6e
  typedef undirected_tag directed_category;
Shinya Kitaoka 120a6e
  typedef allow_parallel_edge_tag edge_parallel_category;
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  typedef struct our_category : public bidirectional_graph_tag,
Shinya Kitaoka 120a6e
                                public vertex_list_graph_tag,
Shinya Kitaoka 120a6e
                                public edge_list_graph_tag {
Shinya Kitaoka 120a6e
  } traversal_category;
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  // Sizes
Shinya Kitaoka 120a6e
  typedef int vertices_size_type;
Shinya Kitaoka 120a6e
  typedef int edges_size_type;
Shinya Kitaoka 120a6e
  typedef int degree_size_type;
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  // Functions
Shinya Kitaoka 120a6e
  static inline vertex_descriptor null_vertex() { return -1; }
Toshihiro Shimizu 890ddd
};
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
template <typename e,="" f="" typename="" v,=""></typename>
Toshihiro Shimizu 890ddd
struct graph_traits<typename e,="" f="" tcg::trimesh<v,="">></typename>
Shinya Kitaoka 120a6e
    : public graph_traits<typename e,="" f="" tcg::mesh<v,="">> {};</typename>
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
}  // namespace boost
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//************************************************************************************
Toshihiro Shimizu 890ddd
//    tcg::bgl  helpers to edge data
Toshihiro Shimizu 890ddd
//************************************************************************************
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
namespace tcg {
Shinya Kitaoka 120a6e
namespace bgl {
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
template <typename mesh=""></typename>
Shinya Kitaoka 120a6e
inline int source(
Shinya Kitaoka 120a6e
    const typename boost::graph_traits<mesh>::edge_descriptor &edge_descr,</mesh>
Shinya Kitaoka 120a6e
    const Mesh &mesh) {
Shinya Kitaoka 120a6e
  return edge_descr.m_src;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//---------------------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
template <typename mesh=""></typename>
Shinya Kitaoka 120a6e
inline int target(
Shinya Kitaoka 120a6e
    const typename boost::graph_traits<mesh>::edge_descriptor &edge_descr,</mesh>
Shinya Kitaoka 120a6e
    const Mesh &mesh) {
Shinya Kitaoka 120a6e
  const typename Mesh::edge_type &ed = mesh.edge(edge_descr.m_e);
Shinya Kitaoka 120a6e
  return (ed.vertex(0) == edge_descr.m_src) ? ed.vertex(1) : ed.vertex(0);
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//---------------------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
template <typename mesh=""></typename>
Toshihiro Shimizu 890ddd
inline std::pair<typename boost::graph_traits<mesh="">::out_edge_iterator,</typename>
Shinya Kitaoka 120a6e
                 typename boost::graph_traits<mesh>::out_edge_iterator></mesh>
Shinya Kitaoka 120a6e
out_edges(int v, const Mesh &mesh) {
Shinya Kitaoka 120a6e
  typedef
Shinya Kitaoka 120a6e
      typename boost::graph_traits<mesh>::out_edge_iterator out_edge_iterator;</mesh>
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  return make_pair(out_edge_iterator(mesh.vertex(v).edgesBegin(), v),
Shinya Kitaoka 120a6e
                   out_edge_iterator(mesh.vertex(v).edgesEnd(), v));
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//---------------------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
template <typename mesh=""></typename>
Shinya Kitaoka 120a6e
inline int out_degree(int v, const Mesh &mesh) {
Shinya Kitaoka 120a6e
  return mesh.vertex(v).edgesCount();
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
}
Shinya Kitaoka 120a6e
}  // namespace tcg::bgl
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
#endif  // TCG_MESH_BGL_H