Skip to content

Commit

Permalink
fix: spatial graph base_type inference
Browse files Browse the repository at this point in the history
  • Loading branch information
Becheler committed Jan 18, 2024
1 parent 6827156 commit 24f99c2
Show file tree
Hide file tree
Showing 4 changed files with 134 additions and 104 deletions.
2 changes: 1 addition & 1 deletion example/geography_graph_complete.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ int main()

// A little helper function
auto sphere_distance = [&]( auto const& p1, auto const& p2 ){
return land.to_latlon( p1 ).great_circle_distance( land.to_latlon( p2 ) );}
return land.to_latlon( p1 ).great_circle_distance( land.to_latlon( p2 ) );};

// auto v = graph1.edges()
// | std::transform( [&](auto const& e){ sphere_distance( graph.source( e ), graph.target( e ) );} )
Expand Down
5 changes: 5 additions & 0 deletions src/include/quetzal/geography/graph/detail/bound_policy.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ namespace quetzal::geography
/// @brief Individuals can not escape the landscape's borders.
class mirror
{
public:
/// @brief Does nothing as by default the bounding box is reflective
/// @tparam EdgeProperty The edge information
/// @tparam Graph The graph type
Expand All @@ -33,6 +34,8 @@ namespace quetzal::geography
/// @brief Individuals can migrate out of the landscape to a sink vertex, but can not come back.
class sink
{
public:

/// @brief Connect source vertex s to a sink vertex if on the border
/// @tparam EdgeProperty
/// @tparam Graph The graph type
Expand All @@ -55,6 +58,8 @@ namespace quetzal::geography
/// @brief The 2D landscape becomes a 3D torus connecting opposed borders
class torus
{
public:

/// @brief Connect edges of source vertex s in a graph given a spatial grid
/// @tparam EdgeProperty
/// @tparam Graph The graph type
Expand Down
71 changes: 36 additions & 35 deletions src/include/quetzal/geography/graph/detail/vicinity.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
#pragma once

#include "concepts.hpp"
#include "../graph.hpp"

#include <boost/graph/adjacency_list.hpp>
#include <boost/graph/adjacency_matrix.hpp>
Expand All @@ -20,24 +21,24 @@ namespace quetzal::geography
struct connect_fully
{
template <directional T, class VertexProperty, class EdgeProperty>
using graph_type = boost::adjacency_matrix< boost::setS, boost::vecS,T, VertexProperty, EdgeProperty>;
using graph_type = graph< VertexProperty, EdgeProperty, boost::adjacency_matrix, T>;

template<class G, two_dimensional S, directional D, bounding<G,S> B>
void connect(G & graph, [[maybe_unused]] S const& grid, [[maybe_unused]] B policy)
template<class G, two_dimensional S, bounding<G,S> B>
void connect(G & graph, [[maybe_unused]] S const& grid, [[maybe_unused]] B policy) const
{
using directed_type = typename G::directed_type;
using vertex_property_type = typename G::vertex_property_type;
using edge_property_type = typename G::edge_property_type;
using directed_category = typename G::directed_category;
using vertex_property = typename G::vertex_property;
using edge_property = typename G::edge_property;

static_assert( std::same_as<G, graph_type<directed_type, vertex_property_type, edge_property_type>>);
static_assert( std::same_as<G, graph_type<directed_category, vertex_property, edge_property>>);

for (auto s = 0; s < graph.num_vertices(); ++s)
{
for (auto t = s + 1; t < graph.num_vertices(); ++t)
{
graph.add_edge(s, t, edge_property_type(s, t)); // (s -> v) == (s <- v) if undirected
if constexpr (std::same_as<directed_type, anisotropy>)
graph.add_edge(t, s, edge_property_type(t, s)); // (s -> v) != (s <- v) because directed
graph.add_edge(s, t, edge_property(s, t)); // (s -> v) == (s <- v) if undirected
if constexpr (std::same_as<directed_category, anisotropy>)
graph.add_edge(t, s, edge_property(t, s)); // (s -> v) != (s <- v) because directed
}
}
}
Expand All @@ -49,18 +50,18 @@ namespace quetzal::geography
public:

template <two_dimensional T, class VertexProperty, class EdgeProperty>
using graph_type = boost::adjacency_list<boost::setS, boost::vecS, T, VertexProperty, EdgeProperty>;
using graph_type = graph< VertexProperty, EdgeProperty, boost::adjacency_list, T>;

private:

template<typename G>
void connect(auto s, auto t, G & graph, auto const& grid)
{
using edge_property_type = typename G::edge_property_type;
graph.add_edge(s, t, edge_property_type(s, t));
if constexpr (std::same_as< typename G::directed_type, anisotropy >)
using edge_property = typename G::edge_property;
graph.add_edge(s, t, edge_property(s, t));
if constexpr (std::same_as< typename G::directed_category, anisotropy >)
{
graph.add_edge(t, s, edge_property_type(t, s));
graph.add_edge(t, s, edge_property(t, s));
}
}

Expand Down Expand Up @@ -135,14 +136,14 @@ namespace quetzal::geography

public:

template<class G, two_dimensional S, directional D, bounding<G, S> B>
void connect(G & graph, S const& grid, B bound_policy)
template<class G, two_dimensional S, bounding<G, S> B>
void connect(G & graph, S const& grid, B bound_policy) const
{
using directed_type = typename G::directed_type;
using vertex_property_type = typename G::vertex_property_type;
using edge_property_type = typename G::edge_property_type;
using directed_category = typename G::directed_category;
using vertex_property = typename G::vertex_property;
using edge_property = typename G::edge_property;

static_assert(std::same_as<G, graph_type<directed_type, vertex_property_type, edge_property_type>> );
static_assert(std::same_as<G, graph_type<directed_category, vertex_property, edge_property>> );

int width = grid.width();
int height = grid.height();
Expand Down Expand Up @@ -203,24 +204,24 @@ namespace quetzal::geography
public:

template <two_dimensional T, class VertexProperty, class EdgeProperty>
using graph_type = boost::adjacency_list< boost::setS, boost::vecS, T, VertexProperty, EdgeProperty>;
using graph_type = graph< VertexProperty, EdgeProperty, boost::adjacency_list, T> ;

private:

template<class G>
void connect(auto s, auto t, G & graph, auto const& grid)
{
using directed_type = typename G::directed_type;
using vertex_property_type = typename G::vertex_property_type;
using edge_property_type = typename G::edge_property_type;
using directed_category = typename G::directed_category;
using vertex_property = typename G::vertex_property;
using edge_property = typename G::edge_property;

static_assert(std::same_as< G, graph_type<directed_type, vertex_property_type, edge_property_type>> );
static_assert(std::same_as< G, graph_type<directed_category, vertex_property, edge_property>> );

graph.add_edge(s, t, edge_property_type(s, t));
graph.add_edge(s, t, edge_property(s, t));

if constexpr (std::same_as<directed_type, anisotropy>)
if constexpr (std::same_as<directed_category, anisotropy>)
{
graph.add_edge(t, s, edge_property_type(t, s));
graph.add_edge(t, s, edge_property(t, s));
}

}
Expand Down Expand Up @@ -312,13 +313,13 @@ namespace quetzal::geography

public:

template<class G, two_dimensional S, directional D, bounding<G, S> B>
void connect(G & graph, S const& grid, B bound_policy)
template<class G, two_dimensional S, bounding<G, S> B>
void connect(G & graph, S const& grid, B bound_policy) const
{
using directed_type = typename G::directed_type;
using vertex_property_type = typename G::vertex_property_type;
using edge_property_type = typename G::edge_property_type;
static_assert(std::same_as<G, graph_type<directed_type, vertex_property_type, edge_property_type> > );
using directed_category = typename G::directed_category;
using vertex_property = typename G::vertex_property;
using edge_property = typename G::edge_property;
static_assert(std::same_as<G, graph_type<directed_category, vertex_property, edge_property> > );

int width = grid.width();
int height = grid.height();
Expand Down
Loading

0 comments on commit 24f99c2

Please sign in to comment.