Apply clang-format on Contractor/

This commit is contained in:
Patrick Niklaus 2014-05-08 23:07:16 +02:00
parent 79d33d669c
commit d13cd4d4b3
7 changed files with 968 additions and 835 deletions

File diff suppressed because it is too large Load Diff

View File

@ -25,7 +25,6 @@ SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/ */
#include "EdgeBasedGraphFactory.h" #include "EdgeBasedGraphFactory.h"
#include "../Util/ComputeAngle.h" #include "../Util/ComputeAngle.h"
#include "../DataStructures/BFSComponentExplorer.h" #include "../DataStructures/BFSComponentExplorer.h"
@ -39,107 +38,97 @@ SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#include <numeric> #include <numeric>
EdgeBasedGraphFactory::EdgeBasedGraphFactory( EdgeBasedGraphFactory::EdgeBasedGraphFactory(
const boost::shared_ptr<NodeBasedDynamicGraph>& node_based_graph, const boost::shared_ptr<NodeBasedDynamicGraph> &node_based_graph,
std::unique_ptr<RestrictionMap> restriction_map, std::unique_ptr<RestrictionMap> restriction_map,
std::vector<NodeID> & barrier_node_list, std::vector<NodeID> &barrier_node_list,
std::vector<NodeID> & traffic_light_node_list, std::vector<NodeID> &traffic_light_node_list,
std::vector<NodeInfo> & m_node_info_list, std::vector<NodeInfo> &m_node_info_list,
SpeedProfileProperties & speed_profile SpeedProfileProperties &speed_profile)
) : speed_profile(speed_profile), : speed_profile(speed_profile),
m_number_of_edge_based_nodes(std::numeric_limits<unsigned>::max()), m_number_of_edge_based_nodes(std::numeric_limits<unsigned>::max()),
m_node_info_list(m_node_info_list), m_node_info_list(m_node_info_list), m_node_based_graph(node_based_graph),
m_node_based_graph(node_based_graph), m_restriction_map(std::move(restriction_map)), max_id(0)
m_restriction_map(std::move(restriction_map)),
max_id(0)
{ {
// insert into unordered sets for fast lookup // insert into unordered sets for fast lookup
m_barrier_nodes.insert( m_barrier_nodes.insert(barrier_node_list.begin(), barrier_node_list.end());
barrier_node_list.begin(),
barrier_node_list.end()
);
m_traffic_lights.insert(
traffic_light_node_list.begin(),
traffic_light_node_list.end()
);
m_traffic_lights.insert(traffic_light_node_list.begin(), traffic_light_node_list.end());
} }
void EdgeBasedGraphFactory::GetEdgeBasedEdges(DeallocatingVector<EdgeBasedEdge> &output_edge_list)
void EdgeBasedGraphFactory::GetEdgeBasedEdges( {
DeallocatingVector< EdgeBasedEdge >& output_edge_list BOOST_ASSERT_MSG(0 == output_edge_list.size(), "Vector is not empty");
) {
BOOST_ASSERT_MSG(
0 == output_edge_list.size(),
"Vector is not empty"
);
m_edge_based_edge_list.swap(output_edge_list); m_edge_based_edge_list.swap(output_edge_list);
} }
void EdgeBasedGraphFactory::GetEdgeBasedNodes( std::vector<EdgeBasedNode> & nodes) { void EdgeBasedGraphFactory::GetEdgeBasedNodes(std::vector<EdgeBasedNode> &nodes)
{
#ifndef NDEBUG #ifndef NDEBUG
BOOST_FOREACH(const EdgeBasedNode & node, m_edge_based_node_list){ BOOST_FOREACH (const EdgeBasedNode &node, m_edge_based_node_list)
{
BOOST_ASSERT( m_node_info_list.at(node.u).lat != INT_MAX ); BOOST_ASSERT(m_node_info_list.at(node.u).lat != INT_MAX);
BOOST_ASSERT( m_node_info_list.at(node.u).lon != INT_MAX ); BOOST_ASSERT(m_node_info_list.at(node.u).lon != INT_MAX);
BOOST_ASSERT( m_node_info_list.at(node.v).lon != INT_MAX ); BOOST_ASSERT(m_node_info_list.at(node.v).lon != INT_MAX);
BOOST_ASSERT( m_node_info_list.at(node.v).lat != INT_MAX ); BOOST_ASSERT(m_node_info_list.at(node.v).lat != INT_MAX);
} }
#endif #endif
nodes.swap(m_edge_based_node_list); nodes.swap(m_edge_based_node_list);
} }
void EdgeBasedGraphFactory::InsertEdgeBasedNode(NodeIterator u,
void EdgeBasedGraphFactory::InsertEdgeBasedNode(
NodeIterator u,
NodeIterator v, NodeIterator v,
EdgeIterator e1, EdgeIterator e1,
bool belongs_to_tiny_cc bool belongs_to_tiny_cc)
) { {
// merge edges together into one EdgeBasedNode // merge edges together into one EdgeBasedNode
BOOST_ASSERT( u != SPECIAL_NODEID ); BOOST_ASSERT(u != SPECIAL_NODEID);
BOOST_ASSERT( v != SPECIAL_NODEID ); BOOST_ASSERT(v != SPECIAL_NODEID);
BOOST_ASSERT( e1 != SPECIAL_EDGEID ); BOOST_ASSERT(e1 != SPECIAL_EDGEID);
#ifndef NDEBUG #ifndef NDEBUG
// find forward edge id and // find forward edge id and
const EdgeID e1b = m_node_based_graph->FindEdge(u, v); const EdgeID e1b = m_node_based_graph->FindEdge(u, v);
BOOST_ASSERT( e1 == e1b ); BOOST_ASSERT(e1 == e1b);
#endif #endif
BOOST_ASSERT( e1 != SPECIAL_EDGEID ); BOOST_ASSERT(e1 != SPECIAL_EDGEID);
const EdgeData & forward_data = m_node_based_graph->GetEdgeData(e1); const EdgeData &forward_data = m_node_based_graph->GetEdgeData(e1);
// find reverse edge id and // find reverse edge id and
const EdgeID e2 = m_node_based_graph->FindEdge(v, u); const EdgeID e2 = m_node_based_graph->FindEdge(v, u);
#ifndef NDEBUG #ifndef NDEBUG
if ( e2 == m_node_based_graph->EndEdges(v) ) { if (e2 == m_node_based_graph->EndEdges(v))
{
SimpleLogger().Write(logWARNING) << "Did not find edge (" << v << "," << u << ")"; SimpleLogger().Write(logWARNING) << "Did not find edge (" << v << "," << u << ")";
} }
#endif #endif
BOOST_ASSERT( e2 != SPECIAL_EDGEID ); BOOST_ASSERT(e2 != SPECIAL_EDGEID);
BOOST_ASSERT( e2 < m_node_based_graph->EndEdges(v) ); BOOST_ASSERT(e2 < m_node_based_graph->EndEdges(v));
const EdgeData & reverse_data = m_node_based_graph->GetEdgeData(e2); const EdgeData &reverse_data = m_node_based_graph->GetEdgeData(e2);
if( if (forward_data.edgeBasedNodeID == SPECIAL_NODEID &&
forward_data.edgeBasedNodeID == SPECIAL_NODEID && reverse_data.edgeBasedNodeID == SPECIAL_NODEID)
reverse_data.edgeBasedNodeID == SPECIAL_NODEID {
) {
return; return;
} }
BOOST_ASSERT( m_geometry_compressor.HasEntryForID(e1) == m_geometry_compressor.HasEntryForID(e2) ); BOOST_ASSERT(m_geometry_compressor.HasEntryForID(e1) ==
if( m_geometry_compressor.HasEntryForID(e1) ) { m_geometry_compressor.HasEntryForID(e2));
if (m_geometry_compressor.HasEntryForID(e1))
{
BOOST_ASSERT( m_geometry_compressor.HasEntryForID(e2) ); BOOST_ASSERT(m_geometry_compressor.HasEntryForID(e2));
// reconstruct geometry and put in each individual edge with its offset // reconstruct geometry and put in each individual edge with its offset
const std::vector<GeometryCompressor::CompressedNode> & forward_geometry = m_geometry_compressor.GetBucketReference(e1); const std::vector<GeometryCompressor::CompressedNode> &forward_geometry =
const std::vector<GeometryCompressor::CompressedNode> & reverse_geometry = m_geometry_compressor.GetBucketReference(e2); m_geometry_compressor.GetBucketReference(e1);
BOOST_ASSERT( forward_geometry.size() == reverse_geometry.size() ); const std::vector<GeometryCompressor::CompressedNode> &reverse_geometry =
BOOST_ASSERT( 0 != forward_geometry.size() ); m_geometry_compressor.GetBucketReference(e2);
BOOST_ASSERT(forward_geometry.size() == reverse_geometry.size());
BOOST_ASSERT(0 != forward_geometry.size());
// reconstruct bidirectional edge with individual weights and put each into the NN index // reconstruct bidirectional edge with individual weights and put each into the NN index
@ -155,20 +144,21 @@ void EdgeBasedGraphFactory::InsertEdgeBasedNode(
forward_dist_prefix_sum[i] = temp_sum; forward_dist_prefix_sum[i] = temp_sum;
temp_sum += forward_geometry[i].second; temp_sum += forward_geometry[i].second;
BOOST_ASSERT( forward_data.distance >= temp_sum ); BOOST_ASSERT(forward_data.distance >= temp_sum);
} }
temp_sum = 0; temp_sum = 0;
for( unsigned i = 0; i < reverse_geometry.size(); ++i ) { for (unsigned i = 0; i < reverse_geometry.size(); ++i)
temp_sum += reverse_geometry[reverse_geometry.size()-1-i].second; {
temp_sum += reverse_geometry[reverse_geometry.size() - 1 - i].second;
reverse_dist_prefix_sum[i] = reverse_data.distance - temp_sum; reverse_dist_prefix_sum[i] = reverse_data.distance - temp_sum;
BOOST_ASSERT( reverse_data.distance >= temp_sum ); BOOST_ASSERT(reverse_data.distance >= temp_sum);
} }
BOOST_ASSERT( forward_geometry.size() == reverse_geometry.size() ); BOOST_ASSERT(forward_geometry.size() == reverse_geometry.size());
const unsigned geometry_size = forward_geometry.size(); const unsigned geometry_size = forward_geometry.size();
BOOST_ASSERT( geometry_size > 1 ); BOOST_ASSERT(geometry_size > 1);
NodeID current_edge_start_coordinate_id = u; NodeID current_edge_start_coordinate_id = u;
if (forward_data.edgeBasedNodeID != SPECIAL_NODEID) if (forward_data.edgeBasedNodeID != SPECIAL_NODEID)
@ -181,15 +171,16 @@ void EdgeBasedGraphFactory::InsertEdgeBasedNode(
} }
// traverse arrays from start and end respectively // traverse arrays from start and end respectively
for( unsigned i = 0; i < geometry_size; ++i ) { for (unsigned i = 0; i < geometry_size; ++i)
BOOST_ASSERT( current_edge_start_coordinate_id == reverse_geometry[geometry_size-1-i].first ); {
BOOST_ASSERT(current_edge_start_coordinate_id ==
reverse_geometry[geometry_size - 1 - i].first);
const NodeID current_edge_target_coordinate_id = forward_geometry[i].first; const NodeID current_edge_target_coordinate_id = forward_geometry[i].first;
BOOST_ASSERT( current_edge_target_coordinate_id != current_edge_start_coordinate_id); BOOST_ASSERT(current_edge_target_coordinate_id != current_edge_start_coordinate_id);
// build edges // build edges
m_edge_based_node_list.emplace_back( m_edge_based_node_list.emplace_back(
EdgeBasedNode( EdgeBasedNode(forward_data.edgeBasedNodeID,
forward_data.edgeBasedNodeID,
reverse_data.edgeBasedNodeID, reverse_data.edgeBasedNodeID,
current_edge_start_coordinate_id, current_edge_start_coordinate_id,
current_edge_target_coordinate_id, current_edge_target_coordinate_id,
@ -200,51 +191,46 @@ void EdgeBasedGraphFactory::InsertEdgeBasedNode(
reverse_dist_prefix_sum[i], reverse_dist_prefix_sum[i],
m_geometry_compressor.GetPositionForID(e1), m_geometry_compressor.GetPositionForID(e1),
i, i,
belongs_to_tiny_cc belongs_to_tiny_cc));
)
);
current_edge_start_coordinate_id = current_edge_target_coordinate_id; current_edge_start_coordinate_id = current_edge_target_coordinate_id;
BOOST_ASSERT( m_edge_based_node_list.back().IsCompressed() ); BOOST_ASSERT(m_edge_based_node_list.back().IsCompressed());
BOOST_ASSERT( BOOST_ASSERT(u != m_edge_based_node_list.back().u ||
u != m_edge_based_node_list.back().u || v != m_edge_based_node_list.back().v);
v != m_edge_based_node_list.back().v
);
BOOST_ASSERT( BOOST_ASSERT(u != m_edge_based_node_list.back().v ||
u != m_edge_based_node_list.back().v || v != m_edge_based_node_list.back().u);
v != m_edge_based_node_list.back().u
);
} }
BOOST_ASSERT( current_edge_start_coordinate_id == v ); BOOST_ASSERT(current_edge_start_coordinate_id == v);
BOOST_ASSERT( m_edge_based_node_list.back().IsCompressed() ); BOOST_ASSERT(m_edge_based_node_list.back().IsCompressed());
}
else
{
BOOST_ASSERT(!m_geometry_compressor.HasEntryForID(e2));
} else { if (forward_data.edgeBasedNodeID != SPECIAL_NODEID)
BOOST_ASSERT( !m_geometry_compressor.HasEntryForID(e2) ); {
BOOST_ASSERT(forward_data.forward);
if( forward_data.edgeBasedNodeID != SPECIAL_NODEID ) {
BOOST_ASSERT( forward_data.forward );
} }
if( reverse_data.edgeBasedNodeID != SPECIAL_NODEID ) { if (reverse_data.edgeBasedNodeID != SPECIAL_NODEID)
BOOST_ASSERT( reverse_data.forward ); {
BOOST_ASSERT(reverse_data.forward);
} }
if( forward_data.edgeBasedNodeID == SPECIAL_NODEID ) { if (forward_data.edgeBasedNodeID == SPECIAL_NODEID)
BOOST_ASSERT( !forward_data.forward ); {
BOOST_ASSERT(!forward_data.forward);
} }
if( reverse_data.edgeBasedNodeID == SPECIAL_NODEID ) { if (reverse_data.edgeBasedNodeID == SPECIAL_NODEID)
BOOST_ASSERT( !reverse_data.forward ); {
BOOST_ASSERT(!reverse_data.forward);
} }
BOOST_ASSERT( BOOST_ASSERT(forward_data.edgeBasedNodeID != SPECIAL_NODEID ||
forward_data.edgeBasedNodeID != SPECIAL_NODEID || reverse_data.edgeBasedNodeID != SPECIAL_NODEID);
reverse_data.edgeBasedNodeID != SPECIAL_NODEID
);
m_edge_based_node_list.emplace_back( m_edge_based_node_list.emplace_back(EdgeBasedNode(forward_data.edgeBasedNodeID,
EdgeBasedNode(
forward_data.edgeBasedNodeID,
reverse_data.edgeBasedNodeID, reverse_data.edgeBasedNodeID,
u, u,
v, v,
@ -255,30 +241,23 @@ void EdgeBasedGraphFactory::InsertEdgeBasedNode(
0, 0,
SPECIAL_EDGEID, SPECIAL_EDGEID,
0, 0,
belongs_to_tiny_cc belongs_to_tiny_cc));
) BOOST_ASSERT(!m_edge_based_node_list.back().IsCompressed());
);
BOOST_ASSERT( !m_edge_based_node_list.back().IsCompressed() );
} }
} }
void EdgeBasedGraphFactory::FlushVectorToStream( void EdgeBasedGraphFactory::FlushVectorToStream(
std::ofstream & edge_data_file, std::ofstream &edge_data_file, std::vector<OriginalEdgeData> &original_edge_data_vector) const
std::vector<OriginalEdgeData> & original_edge_data_vector {
) const { edge_data_file.write((char *)&(original_edge_data_vector[0]),
edge_data_file.write( original_edge_data_vector.size() * sizeof(OriginalEdgeData));
(char*)&(original_edge_data_vector[0]),
original_edge_data_vector.size()*sizeof(OriginalEdgeData)
);
original_edge_data_vector.clear(); original_edge_data_vector.clear();
} }
void EdgeBasedGraphFactory::Run( void EdgeBasedGraphFactory::Run(const std::string &original_edge_data_filename,
const std::string & original_edge_data_filename, const std::string &geometry_filename,
const std::string & geometry_filename, lua_State *lua_state)
lua_State *lua_state {
) {
CompressGeometry(); CompressGeometry();
@ -288,8 +267,7 @@ void EdgeBasedGraphFactory::Run(
GenerateEdgeExpandedEdges(original_edge_data_filename, lua_state); GenerateEdgeExpandedEdges(original_edge_data_filename, lua_state);
m_geometry_compressor.SerializeInternalVector( geometry_filename ); m_geometry_compressor.SerializeInternalVector(geometry_filename);
} }
void EdgeBasedGraphFactory::CompressGeometry() void EdgeBasedGraphFactory::CompressGeometry()
@ -302,79 +280,84 @@ void EdgeBasedGraphFactory::CompressGeometry()
Percent p(original_number_of_nodes); Percent p(original_number_of_nodes);
unsigned removed_node_count = 0; unsigned removed_node_count = 0;
for( NodeID v = 0; v < original_number_of_nodes; ++v ) { for (NodeID v = 0; v < original_number_of_nodes; ++v)
{
p.printStatus(v); p.printStatus(v);
// only contract degree 2 vertices // only contract degree 2 vertices
if( 2 != m_node_based_graph->GetOutDegree(v) ) { if (2 != m_node_based_graph->GetOutDegree(v))
{
continue; continue;
} }
// don't contract barrier node // don't contract barrier node
if( m_barrier_nodes.end() != m_barrier_nodes.find(v) ) { if (m_barrier_nodes.end() != m_barrier_nodes.find(v))
{
continue; continue;
} }
const bool reverse_edge_order = !(m_node_based_graph->GetEdgeData(m_node_based_graph->BeginEdges(v)).forward); const bool reverse_edge_order =
!(m_node_based_graph->GetEdgeData(m_node_based_graph->BeginEdges(v)).forward);
const EdgeIterator forward_e2 = m_node_based_graph->BeginEdges(v) + reverse_edge_order; const EdgeIterator forward_e2 = m_node_based_graph->BeginEdges(v) + reverse_edge_order;
BOOST_ASSERT( SPECIAL_EDGEID != forward_e2 ); BOOST_ASSERT(SPECIAL_EDGEID != forward_e2);
const EdgeIterator reverse_e2 = m_node_based_graph->BeginEdges(v) + 1 - reverse_edge_order; const EdgeIterator reverse_e2 = m_node_based_graph->BeginEdges(v) + 1 - reverse_edge_order;
BOOST_ASSERT( SPECIAL_EDGEID != reverse_e2 ); BOOST_ASSERT(SPECIAL_EDGEID != reverse_e2);
const EdgeData & fwd_edge_data2 = m_node_based_graph->GetEdgeData(forward_e2); const EdgeData &fwd_edge_data2 = m_node_based_graph->GetEdgeData(forward_e2);
const EdgeData & rev_edge_data2 = m_node_based_graph->GetEdgeData(reverse_e2); const EdgeData &rev_edge_data2 = m_node_based_graph->GetEdgeData(reverse_e2);
const NodeIterator w = m_node_based_graph->GetTarget(forward_e2); const NodeIterator w = m_node_based_graph->GetTarget(forward_e2);
BOOST_ASSERT( SPECIAL_NODEID != w ); BOOST_ASSERT(SPECIAL_NODEID != w);
BOOST_ASSERT( v != w ); BOOST_ASSERT(v != w);
const NodeIterator u = m_node_based_graph->GetTarget(reverse_e2); const NodeIterator u = m_node_based_graph->GetTarget(reverse_e2);
BOOST_ASSERT( SPECIAL_NODEID != u ); BOOST_ASSERT(SPECIAL_NODEID != u);
BOOST_ASSERT( u != v ); BOOST_ASSERT(u != v);
const EdgeIterator forward_e1 = m_node_based_graph->FindEdge(u, v); const EdgeIterator forward_e1 = m_node_based_graph->FindEdge(u, v);
BOOST_ASSERT( m_node_based_graph->EndEdges(u) != forward_e1 ); BOOST_ASSERT(m_node_based_graph->EndEdges(u) != forward_e1);
BOOST_ASSERT( SPECIAL_EDGEID != forward_e1 ); BOOST_ASSERT(SPECIAL_EDGEID != forward_e1);
BOOST_ASSERT( v == m_node_based_graph->GetTarget(forward_e1)); BOOST_ASSERT(v == m_node_based_graph->GetTarget(forward_e1));
const EdgeIterator reverse_e1 = m_node_based_graph->FindEdge(w, v); const EdgeIterator reverse_e1 = m_node_based_graph->FindEdge(w, v);
BOOST_ASSERT( SPECIAL_EDGEID != reverse_e1 ); BOOST_ASSERT(SPECIAL_EDGEID != reverse_e1);
BOOST_ASSERT( v == m_node_based_graph->GetTarget(reverse_e1)); BOOST_ASSERT(v == m_node_based_graph->GetTarget(reverse_e1));
const EdgeData & fwd_edge_data1 = m_node_based_graph->GetEdgeData(forward_e1); const EdgeData &fwd_edge_data1 = m_node_based_graph->GetEdgeData(forward_e1);
const EdgeData & rev_edge_data1 = m_node_based_graph->GetEdgeData(reverse_e1); const EdgeData &rev_edge_data1 = m_node_based_graph->GetEdgeData(reverse_e1);
if( if ((m_node_based_graph->FindEdge(u, w) != m_node_based_graph->EndEdges(u)) ||
( m_node_based_graph->FindEdge(u, w) != m_node_based_graph->EndEdges(u) ) || (m_node_based_graph->FindEdge(w, u) != m_node_based_graph->EndEdges(w)))
( m_node_based_graph->FindEdge(w, u) != m_node_based_graph->EndEdges(w) ) {
) {
continue; continue;
} }
if ( //TODO: rename to IsCompatibleTo if ( // TODO: rename to IsCompatibleTo
fwd_edge_data1.IsEqualTo(fwd_edge_data2) && fwd_edge_data1.IsEqualTo(fwd_edge_data2) && rev_edge_data1.IsEqualTo(rev_edge_data2))
rev_edge_data1.IsEqualTo(rev_edge_data2) {
) { // Get distances before graph is modified
//Get distances before graph is modified
const int forward_weight1 = m_node_based_graph->GetEdgeData(forward_e1).distance; const int forward_weight1 = m_node_based_graph->GetEdgeData(forward_e1).distance;
const int forward_weight2 = m_node_based_graph->GetEdgeData(forward_e2).distance; const int forward_weight2 = m_node_based_graph->GetEdgeData(forward_e2).distance;
BOOST_ASSERT( 0 != forward_weight1 ); BOOST_ASSERT(0 != forward_weight1);
BOOST_ASSERT( 0 != forward_weight2 ); BOOST_ASSERT(0 != forward_weight2);
const int reverse_weight1 = m_node_based_graph->GetEdgeData(reverse_e1).distance; const int reverse_weight1 = m_node_based_graph->GetEdgeData(reverse_e1).distance;
const int reverse_weight2 = m_node_based_graph->GetEdgeData(reverse_e2).distance; const int reverse_weight2 = m_node_based_graph->GetEdgeData(reverse_e2).distance;
BOOST_ASSERT( 0 != reverse_weight1 ); BOOST_ASSERT(0 != reverse_weight1);
BOOST_ASSERT( 0 != forward_weight2 ); BOOST_ASSERT(0 != forward_weight2);
const bool add_traffic_signal_penalty = (m_traffic_lights.find(v) != m_traffic_lights.end()); const bool add_traffic_signal_penalty =
(m_traffic_lights.find(v) != m_traffic_lights.end());
// add weight of e2's to e1 // add weight of e2's to e1
m_node_based_graph->GetEdgeData(forward_e1).distance += fwd_edge_data2.distance; m_node_based_graph->GetEdgeData(forward_e1).distance += fwd_edge_data2.distance;
m_node_based_graph->GetEdgeData(reverse_e1).distance += rev_edge_data2.distance; m_node_based_graph->GetEdgeData(reverse_e1).distance += rev_edge_data2.distance;
if (add_traffic_signal_penalty) if (add_traffic_signal_penalty)
{ {
m_node_based_graph->GetEdgeData(forward_e1).distance += speed_profile.trafficSignalPenalty; m_node_based_graph->GetEdgeData(forward_e1).distance +=
m_node_based_graph->GetEdgeData(reverse_e1).distance += speed_profile.trafficSignalPenalty; speed_profile.trafficSignalPenalty;
m_node_based_graph->GetEdgeData(reverse_e1).distance +=
speed_profile.trafficSignalPenalty;
} }
// extend e1's to targets of e2's // extend e1's to targets of e2's
@ -386,11 +369,11 @@ void EdgeBasedGraphFactory::CompressGeometry()
m_node_based_graph->DeleteEdge(v, reverse_e2); m_node_based_graph->DeleteEdge(v, reverse_e2);
// update any involved turn restrictions // update any involved turn restrictions
m_restriction_map->FixupStartingTurnRestriction( u, v, w ); m_restriction_map->FixupStartingTurnRestriction(u, v, w);
m_restriction_map->FixupArrivingTurnRestriction( u, v, w ); m_restriction_map->FixupArrivingTurnRestriction(u, v, w);
m_restriction_map->FixupStartingTurnRestriction( w, v, u ); m_restriction_map->FixupStartingTurnRestriction(w, v, u);
m_restriction_map->FixupArrivingTurnRestriction( w, v, u ); m_restriction_map->FixupArrivingTurnRestriction(w, v, u);
// store compressed geometry in container // store compressed geometry in container
m_geometry_compressor.CompressEdge( m_geometry_compressor.CompressEdge(
@ -398,24 +381,21 @@ void EdgeBasedGraphFactory::CompressGeometry()
forward_e2, forward_e2,
v, v,
w, w,
forward_weight1 + (add_traffic_signal_penalty ? speed_profile.trafficSignalPenalty :0), forward_weight1 +
forward_weight2 (add_traffic_signal_penalty ? speed_profile.trafficSignalPenalty : 0),
); forward_weight2);
m_geometry_compressor.CompressEdge( m_geometry_compressor.CompressEdge(
reverse_e1, reverse_e1,
reverse_e2, reverse_e2,
v, v,
u, u,
reverse_weight1 , reverse_weight1,
reverse_weight2 + (add_traffic_signal_penalty ? speed_profile.trafficSignalPenalty :0) reverse_weight2 +
); (add_traffic_signal_penalty ? speed_profile.trafficSignalPenalty : 0));
++removed_node_count; ++removed_node_count;
BOOST_ASSERT BOOST_ASSERT(m_node_based_graph->GetEdgeData(forward_e1).nameID ==
( m_node_based_graph->GetEdgeData(reverse_e1).nameID);
m_node_based_graph->GetEdgeData(forward_e1).nameID ==
m_node_based_graph->GetEdgeData(reverse_e1).nameID
);
} }
} }
SimpleLogger().Write() << "removed " << removed_node_count << " nodes"; SimpleLogger().Write() << "removed " << removed_node_count << " nodes";
@ -423,15 +403,19 @@ void EdgeBasedGraphFactory::CompressGeometry()
unsigned new_node_count = 0; unsigned new_node_count = 0;
unsigned new_edge_count = 0; unsigned new_edge_count = 0;
for( unsigned i = 0; i < m_node_based_graph->GetNumberOfNodes(); ++i ) { for (unsigned i = 0; i < m_node_based_graph->GetNumberOfNodes(); ++i)
if( m_node_based_graph->GetOutDegree(i) > 0 ) { {
if (m_node_based_graph->GetOutDegree(i) > 0)
{
++new_node_count; ++new_node_count;
new_edge_count += (m_node_based_graph->EndEdges(i) - m_node_based_graph->BeginEdges(i)); new_edge_count += (m_node_based_graph->EndEdges(i) - m_node_based_graph->BeginEdges(i));
} }
} }
SimpleLogger().Write() << "new nodes: " << new_node_count << ", edges " << new_edge_count; SimpleLogger().Write() << "new nodes: " << new_node_count << ", edges " << new_edge_count;
SimpleLogger().Write() << "Node compression ratio: " << new_node_count/(double)original_number_of_nodes; SimpleLogger().Write() << "Node compression ratio: "
SimpleLogger().Write() << "Edge compression ratio: " << new_edge_count/(double)original_number_of_edges; << new_node_count / (double)original_number_of_nodes;
SimpleLogger().Write() << "Edge compression ratio: "
<< new_edge_count / (double)original_number_of_edges;
} }
/** /**
@ -441,18 +425,24 @@ void EdgeBasedGraphFactory::RenumberEdges()
{ {
// renumber edge based node IDs // renumber edge based node IDs
unsigned numbered_edges_count = 0; unsigned numbered_edges_count = 0;
for(NodeID current_node = 0; current_node < m_node_based_graph->GetNumberOfNodes(); ++current_node) { for (NodeID current_node = 0; current_node < m_node_based_graph->GetNumberOfNodes();
for(EdgeIterator current_edge = m_node_based_graph->BeginEdges(current_node); current_edge < m_node_based_graph->EndEdges(current_node); ++current_edge) { ++current_node)
EdgeData & edge_data = m_node_based_graph->GetEdgeData(current_edge); {
if( !edge_data.forward ) { for (EdgeIterator current_edge = m_node_based_graph->BeginEdges(current_node);
current_edge < m_node_based_graph->EndEdges(current_node);
++current_edge)
{
EdgeData &edge_data = m_node_based_graph->GetEdgeData(current_edge);
if (!edge_data.forward)
{
continue; continue;
} }
BOOST_ASSERT( numbered_edges_count < m_node_based_graph->GetNumberOfEdges() ); BOOST_ASSERT(numbered_edges_count < m_node_based_graph->GetNumberOfEdges());
edge_data.edgeBasedNodeID = numbered_edges_count; edge_data.edgeBasedNodeID = numbered_edges_count;
++numbered_edges_count; ++numbered_edges_count;
BOOST_ASSERT( SPECIAL_NODEID != edge_data.edgeBasedNodeID); BOOST_ASSERT(SPECIAL_NODEID != edge_data.edgeBasedNodeID);
} }
} }
@ -466,96 +456,84 @@ void EdgeBasedGraphFactory::GenerateEdgeExpandedNodes()
{ {
SimpleLogger().Write() << "Identifying components of the road network"; SimpleLogger().Write() << "Identifying components of the road network";
//Run a BFS on the undirected graph and identify small components // Run a BFS on the undirected graph and identify small components
BFSComponentExplorer<NodeBasedDynamicGraph> component_explorer( BFSComponentExplorer<NodeBasedDynamicGraph> component_explorer(
*m_node_based_graph, *m_node_based_graph, *m_restriction_map, m_barrier_nodes);
*m_restriction_map,
m_barrier_nodes
);
component_explorer.run(); component_explorer.run();
SimpleLogger().Write() << SimpleLogger().Write() << "identified: " << component_explorer.getNumberOfComponents()
"identified: " << component_explorer.getNumberOfComponents() << " many components"; << " many components";
SimpleLogger().Write() << "generating edge-expanded nodes"; SimpleLogger().Write() << "generating edge-expanded nodes";
Percent p(m_node_based_graph->GetNumberOfNodes()); Percent p(m_node_based_graph->GetNumberOfNodes());
//loop over all edges and generate new set of nodes // loop over all edges and generate new set of nodes
for( for (NodeIterator u = 0, end = m_node_based_graph->GetNumberOfNodes(); u < end; ++u)
NodeIterator u = 0, end = m_node_based_graph->GetNumberOfNodes(); {
u < end; BOOST_ASSERT(u != SPECIAL_NODEID);
++u BOOST_ASSERT(u < m_node_based_graph->GetNumberOfNodes());
) {
BOOST_ASSERT( u != SPECIAL_NODEID );
BOOST_ASSERT( u < m_node_based_graph->GetNumberOfNodes() );
p.printIncrement(); p.printIncrement();
for( for (EdgeID e1 = m_node_based_graph->BeginEdges(u),
EdgeID e1 = m_node_based_graph->BeginEdges(u),
last_edge = m_node_based_graph->EndEdges(u); last_edge = m_node_based_graph->EndEdges(u);
e1 < last_edge; e1 < last_edge;
++e1 ++e1)
) { {
const EdgeData & edge_data = m_node_based_graph->GetEdgeData(e1); const EdgeData &edge_data = m_node_based_graph->GetEdgeData(e1);
if( edge_data.edgeBasedNodeID == SPECIAL_NODEID ) { if (edge_data.edgeBasedNodeID == SPECIAL_NODEID)
{
// continue; // continue;
} }
BOOST_ASSERT( e1 != SPECIAL_EDGEID ); BOOST_ASSERT(e1 != SPECIAL_EDGEID);
const NodeID v = m_node_based_graph->GetTarget(e1); const NodeID v = m_node_based_graph->GetTarget(e1);
BOOST_ASSERT( SPECIAL_NODEID != v ); BOOST_ASSERT(SPECIAL_NODEID != v);
// pick only every other edge // pick only every other edge
if( u > v ) { if (u > v)
{
continue; continue;
} }
BOOST_ASSERT( u < v ); BOOST_ASSERT(u < v);
BOOST_ASSERT( edge_data.type != SHRT_MAX ); BOOST_ASSERT(edge_data.type != SHRT_MAX);
//Note: edges that end on barrier nodes or on a turn restriction // Note: edges that end on barrier nodes or on a turn restriction
//may actually be in two distinct components. We choose the smallest // may actually be in two distinct components. We choose the smallest
const unsigned size_of_component = std::min( const unsigned size_of_component = std::min(component_explorer.getComponentSize(u),
component_explorer.getComponentSize(u), component_explorer.getComponentSize(v));
component_explorer.getComponentSize(v)
);
const bool component_is_tiny = ( size_of_component < 1000 ); const bool component_is_tiny = (size_of_component < 1000);
InsertEdgeBasedNode( u, v, e1, component_is_tiny ); InsertEdgeBasedNode(u, v, e1, component_is_tiny);
} }
} }
SimpleLogger().Write() << "Generated " << m_edge_based_node_list.size() << SimpleLogger().Write() << "Generated " << m_edge_based_node_list.size()
" nodes in edge-expanded graph"; << " nodes in edge-expanded graph";
} }
/** /**
* Actually it also generates OriginalEdgeData and serializes them... * Actually it also generates OriginalEdgeData and serializes them...
*/ */
void EdgeBasedGraphFactory::GenerateEdgeExpandedEdges(const std::string& original_edge_data_filename, lua_State* lua_state) void
EdgeBasedGraphFactory::GenerateEdgeExpandedEdges(const std::string &original_edge_data_filename,
lua_State *lua_state)
{ {
SimpleLogger().Write() << "generating edge-expanded edges"; SimpleLogger().Write() << "generating edge-expanded edges";
unsigned node_based_edge_counter = 0; unsigned node_based_edge_counter = 0;
unsigned original_edges_counter = 0; unsigned original_edges_counter = 0;
std::ofstream edge_data_file( std::ofstream edge_data_file(original_edge_data_filename.c_str(), std::ios::binary);
original_edge_data_filename.c_str(),
std::ios::binary
);
//writes a dummy value that is updated later
edge_data_file.write(
(char*)&original_edges_counter,
sizeof(unsigned)
);
// writes a dummy value that is updated later
edge_data_file.write((char *)&original_edges_counter, sizeof(unsigned));
std::vector<OriginalEdgeData> original_edge_data_vector; std::vector<OriginalEdgeData> original_edge_data_vector;
original_edge_data_vector.reserve(1024*1024); original_edge_data_vector.reserve(1024 * 1024);
//Loop over all turns and generate new set of edges. // Loop over all turns and generate new set of edges.
//Three nested loop look super-linear, but we are dealing with a (kind of) // Three nested loop look super-linear, but we are dealing with a (kind of)
//linear number of turns only. // linear number of turns only.
unsigned restricted_turns_counter = 0; unsigned restricted_turns_counter = 0;
unsigned skipped_uturns_counter = 0; unsigned skipped_uturns_counter = 0;
unsigned skipped_barrier_turns_counter = 0; unsigned skipped_barrier_turns_counter = 0;
@ -565,32 +543,37 @@ void EdgeBasedGraphFactory::GenerateEdgeExpandedEdges(const std::string& origina
for (NodeIterator u = 0, end = m_node_based_graph->GetNumberOfNodes(); u < end; ++u) for (NodeIterator u = 0, end = m_node_based_graph->GetNumberOfNodes(); u < end; ++u)
{ {
for (EdgeIterator e1 = m_node_based_graph->BeginEdges(u), last_edge_u = m_node_based_graph->EndEdges(u); e1 < last_edge_u; ++e1) for (EdgeIterator e1 = m_node_based_graph->BeginEdges(u),
last_edge_u = m_node_based_graph->EndEdges(u);
e1 < last_edge_u;
++e1)
{
if (!m_node_based_graph->GetEdgeData(e1).forward)
{ {
if( !m_node_based_graph->GetEdgeData(e1).forward ) {
continue; continue;
} }
++node_based_edge_counter; ++node_based_edge_counter;
const NodeIterator v = m_node_based_graph->GetTarget(e1); const NodeIterator v = m_node_based_graph->GetTarget(e1);
const NodeID to_node_of_only_restriction = m_restriction_map->CheckForEmanatingIsOnlyTurn(u, v); const NodeID to_node_of_only_restriction =
const bool is_barrier_node = ( m_barrier_nodes.find(v) != m_barrier_nodes.end() ); m_restriction_map->CheckForEmanatingIsOnlyTurn(u, v);
const bool is_barrier_node = (m_barrier_nodes.find(v) != m_barrier_nodes.end());
for( for (EdgeIterator e2 = m_node_based_graph->BeginEdges(v),
EdgeIterator e2 = m_node_based_graph->BeginEdges(v),
last_edge_v = m_node_based_graph->EndEdges(v); last_edge_v = m_node_based_graph->EndEdges(v);
e2 < last_edge_v; e2 < last_edge_v;
++e2 ++e2)
) { {
if (!m_node_based_graph->GetEdgeData(e2).forward) if (!m_node_based_graph->GetEdgeData(e2).forward)
{ {
continue; continue;
} }
const NodeIterator w = m_node_based_graph->GetTarget(e2); const NodeIterator w = m_node_based_graph->GetTarget(e2);
if ((to_node_of_only_restriction != SPECIAL_NODEID) && (w != to_node_of_only_restriction)) if ((to_node_of_only_restriction != SPECIAL_NODEID) &&
(w != to_node_of_only_restriction))
{ {
//We are at an only_-restriction but not at the right turn. // We are at an only_-restriction but not at the right turn.
++restricted_turns_counter; ++restricted_turns_counter;
continue; continue;
} }
@ -612,17 +595,19 @@ void EdgeBasedGraphFactory::GenerateEdgeExpandedEdges(const std::string& origina
} }
} }
//only add an edge if turn is not a U-turn except when it is // only add an edge if turn is not a U-turn except when it is
//at the end of a dead-end street // at the end of a dead-end street
if (m_restriction_map->CheckIfTurnIsRestricted(u, v, w) && (to_node_of_only_restriction == SPECIAL_NODEID) && (w != to_node_of_only_restriction)) if (m_restriction_map->CheckIfTurnIsRestricted(u, v, w) &&
(to_node_of_only_restriction == SPECIAL_NODEID) &&
(w != to_node_of_only_restriction))
{ {
++restricted_turns_counter; ++restricted_turns_counter;
continue; continue;
} }
//only add an edge if turn is not prohibited // only add an edge if turn is not prohibited
const EdgeData & edge_data1 = m_node_based_graph->GetEdgeData(e1); const EdgeData &edge_data1 = m_node_based_graph->GetEdgeData(e1);
const EdgeData & edge_data2 = m_node_based_graph->GetEdgeData(e2); const EdgeData &edge_data2 = m_node_based_graph->GetEdgeData(e2);
BOOST_ASSERT(edge_data1.edgeBasedNodeID != edge_data2.edgeBasedNodeID); BOOST_ASSERT(edge_data1.edgeBasedNodeID != edge_data2.edgeBasedNodeID);
BOOST_ASSERT(edge_data1.forward); BOOST_ASSERT(edge_data1.forward);
@ -649,43 +634,36 @@ void EdgeBasedGraphFactory::GenerateEdgeExpandedEdges(const std::string& origina
++compressed; ++compressed;
} }
original_edge_data_vector.push_back( original_edge_data_vector.push_back(OriginalEdgeData(
OriginalEdgeData(
(edge_is_compressed ? m_geometry_compressor.GetPositionForID(e1) : v), (edge_is_compressed ? m_geometry_compressor.GetPositionForID(e1) : v),
edge_data1.nameID, edge_data1.nameID,
turn_instruction, turn_instruction,
edge_is_compressed edge_is_compressed));
)
);
++original_edges_counter; ++original_edges_counter;
if (original_edge_data_vector.size() > 1024*1024*10) if (original_edge_data_vector.size() > 1024 * 1024 * 10)
{ {
FlushVectorToStream(edge_data_file, original_edge_data_vector); FlushVectorToStream(edge_data_file, original_edge_data_vector);
} }
BOOST_ASSERT( SPECIAL_NODEID != edge_data1.edgeBasedNodeID ); BOOST_ASSERT(SPECIAL_NODEID != edge_data1.edgeBasedNodeID);
BOOST_ASSERT( SPECIAL_NODEID != edge_data2.edgeBasedNodeID ); BOOST_ASSERT(SPECIAL_NODEID != edge_data2.edgeBasedNodeID);
m_edge_based_edge_list.emplace_back( m_edge_based_edge_list.emplace_back(EdgeBasedEdge(edge_data1.edgeBasedNodeID,
EdgeBasedEdge(
edge_data1.edgeBasedNodeID,
edge_data2.edgeBasedNodeID, edge_data2.edgeBasedNodeID,
m_edge_based_edge_list.size(), m_edge_based_edge_list.size(),
distance, distance,
true, true,
false false));
)
);
} }
} }
p.printIncrement(); p.printIncrement();
} }
FlushVectorToStream( edge_data_file, original_edge_data_vector ); FlushVectorToStream(edge_data_file, original_edge_data_vector);
edge_data_file.seekp( std::ios::beg ); edge_data_file.seekp(std::ios::beg);
edge_data_file.write( (char*)&original_edges_counter, sizeof(unsigned) ); edge_data_file.write((char *)&original_edges_counter, sizeof(unsigned));
edge_data_file.close(); edge_data_file.close();
SimpleLogger().Write() << "Generated " << m_edge_based_node_list.size() << " edge based nodes"; SimpleLogger().Write() << "Generated " << m_edge_based_node_list.size() << " edge based nodes";
@ -693,103 +671,106 @@ void EdgeBasedGraphFactory::GenerateEdgeExpandedEdges(const std::string& origina
SimpleLogger().Write() << "Edge-expanded graph ..."; SimpleLogger().Write() << "Edge-expanded graph ...";
SimpleLogger().Write() << " contains " << m_edge_based_edge_list.size() << " edges"; SimpleLogger().Write() << " contains " << m_edge_based_edge_list.size() << " edges";
SimpleLogger().Write() << " skips " << restricted_turns_counter << " turns, " SimpleLogger().Write() << " skips " << restricted_turns_counter << " turns, "
"defined by " << m_restriction_map->size() << " restrictions"; "defined by "
<< m_restriction_map->size() << " restrictions";
SimpleLogger().Write() << " skips " << skipped_uturns_counter << " U turns"; SimpleLogger().Write() << " skips " << skipped_uturns_counter << " U turns";
SimpleLogger().Write() << " skips " << skipped_barrier_turns_counter << " turns over barriers"; SimpleLogger().Write() << " skips " << skipped_barrier_turns_counter << " turns over barriers";
} }
int EdgeBasedGraphFactory::GetTurnPenalty(const NodeID u,
int EdgeBasedGraphFactory::GetTurnPenalty(
const NodeID u,
const NodeID v, const NodeID v,
const NodeID w, const NodeID w,
lua_State *lua_state lua_State *lua_state) const
) const { {
const double angle = GetAngleBetweenThreeFixedPointCoordinates ( const double angle = GetAngleBetweenThreeFixedPointCoordinates(
m_node_info_list[u], m_node_info_list[u], m_node_info_list[v], m_node_info_list[w]);
m_node_info_list[v],
m_node_info_list[w]
);
if( speed_profile.has_turn_penalty_function ) { if (speed_profile.has_turn_penalty_function)
try { {
//call lua profile to compute turn penalty try
return luabind::call_function<int>( {
lua_state, // call lua profile to compute turn penalty
"turn_function", return luabind::call_function<int>(lua_state, "turn_function", 180. - angle);
180.-angle }
); catch (const luabind::error &er)
} catch (const luabind::error &er) { {
SimpleLogger().Write(logWARNING) << er.what(); SimpleLogger().Write(logWARNING) << er.what();
} }
} }
return 0; return 0;
} }
TurnInstruction EdgeBasedGraphFactory::AnalyzeTurn( TurnInstruction EdgeBasedGraphFactory::AnalyzeTurn(const NodeID u, const NodeID v, const NodeID w)
const NodeID u, const
const NodeID v, {
const NodeID w if (u == w)
) const { {
if(u == w) {
return TurnInstruction::UTurn; return TurnInstruction::UTurn;
} }
const EdgeIterator edge1 = m_node_based_graph->FindEdge(u, v); const EdgeIterator edge1 = m_node_based_graph->FindEdge(u, v);
const EdgeIterator edge2 = m_node_based_graph->FindEdge(v, w); const EdgeIterator edge2 = m_node_based_graph->FindEdge(v, w);
const EdgeData & data1 = m_node_based_graph->GetEdgeData(edge1); const EdgeData &data1 = m_node_based_graph->GetEdgeData(edge1);
const EdgeData & data2 = m_node_based_graph->GetEdgeData(edge2); const EdgeData &data2 = m_node_based_graph->GetEdgeData(edge2);
if(!data1.contraFlow && data2.contraFlow) { if (!data1.contraFlow && data2.contraFlow)
{
return TurnInstruction::EnterAgainstAllowedDirection; return TurnInstruction::EnterAgainstAllowedDirection;
} }
if(data1.contraFlow && !data2.contraFlow) { if (data1.contraFlow && !data2.contraFlow)
{
return TurnInstruction::LeaveAgainstAllowedDirection; return TurnInstruction::LeaveAgainstAllowedDirection;
} }
//roundabouts need to be handled explicitely // roundabouts need to be handled explicitely
if(data1.roundabout && data2.roundabout) { if (data1.roundabout && data2.roundabout)
//Is a turn possible? If yes, we stay on the roundabout! {
if( 1 == m_node_based_graph->GetOutDegree(v) ) { // Is a turn possible? If yes, we stay on the roundabout!
//No turn possible. if (1 == m_node_based_graph->GetOutDegree(v))
{
// No turn possible.
return TurnInstruction::NoTurn; return TurnInstruction::NoTurn;
} }
return TurnInstruction::StayOnRoundAbout; return TurnInstruction::StayOnRoundAbout;
} }
//Does turn start or end on roundabout? // Does turn start or end on roundabout?
if(data1.roundabout || data2.roundabout) { if (data1.roundabout || data2.roundabout)
//We are entering the roundabout {
if( (!data1.roundabout) && data2.roundabout) { // We are entering the roundabout
if ((!data1.roundabout) && data2.roundabout)
{
return TurnInstruction::EnterRoundAbout; return TurnInstruction::EnterRoundAbout;
} }
//We are leaving the roundabout // We are leaving the roundabout
if(data1.roundabout && (!data2.roundabout) ) { if (data1.roundabout && (!data2.roundabout))
{
return TurnInstruction::LeaveRoundAbout; return TurnInstruction::LeaveRoundAbout;
} }
} }
//If street names stay the same and if we are certain that it is not a // If street names stay the same and if we are certain that it is not a
//a segment of a roundabout, we skip it. // a segment of a roundabout, we skip it.
if( data1.nameID == data2.nameID ) { if (data1.nameID == data2.nameID)
//TODO: Here we should also do a small graph exploration to check for {
// TODO: Here we should also do a small graph exploration to check for
// more complex situations // more complex situations
if( 0 != data1.nameID ) { if (0 != data1.nameID)
{
return TurnInstruction::NoTurn; return TurnInstruction::NoTurn;
} else if (m_node_based_graph->GetOutDegree(v) <= 2) { }
else if (m_node_based_graph->GetOutDegree(v) <= 2)
{
return TurnInstruction::NoTurn; return TurnInstruction::NoTurn;
} }
} }
const double angle = GetAngleBetweenThreeFixedPointCoordinates ( const double angle = GetAngleBetweenThreeFixedPointCoordinates(
m_node_info_list[u], m_node_info_list[u], m_node_info_list[v], m_node_info_list[w]);
m_node_info_list[v],
m_node_info_list[w]
);
return TurnInstructionsClass::GetTurnDirectionOfInstruction(angle); return TurnInstructionsClass::GetTurnDirectionOfInstruction(angle);
} }
unsigned EdgeBasedGraphFactory::GetNumberOfEdgeBasedNodes() const { unsigned EdgeBasedGraphFactory::GetNumberOfEdgeBasedNodes() const
{
return m_number_of_edge_based_nodes; return m_number_of_edge_based_nodes;
} }

View File

@ -58,57 +58,45 @@ SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#include <queue> #include <queue>
#include <vector> #include <vector>
class EdgeBasedGraphFactory : boost::noncopyable { class EdgeBasedGraphFactory : boost::noncopyable
public: {
public:
struct SpeedProfileProperties; struct SpeedProfileProperties;
explicit EdgeBasedGraphFactory( explicit EdgeBasedGraphFactory(const boost::shared_ptr<NodeBasedDynamicGraph> &node_based_graph,
const boost::shared_ptr<NodeBasedDynamicGraph>& node_based_graph,
std::unique_ptr<RestrictionMap> restricion_map, std::unique_ptr<RestrictionMap> restricion_map,
std::vector<NodeID> & barrier_node_list, std::vector<NodeID> &barrier_node_list,
std::vector<NodeID> & traffic_light_node_list, std::vector<NodeID> &traffic_light_node_list,
std::vector<NodeInfo> & m_node_info_list, std::vector<NodeInfo> &m_node_info_list,
SpeedProfileProperties & speed_profile SpeedProfileProperties &speed_profile);
);
void Run( void Run(const std::string &original_edge_data_filename,
const std::string & original_edge_data_filename, const std::string &geometry_filename,
const std::string & geometry_filename, lua_State *myLuaState);
lua_State *myLuaState
);
void GetEdgeBasedEdges( DeallocatingVector< EdgeBasedEdge >& edges ); void GetEdgeBasedEdges(DeallocatingVector<EdgeBasedEdge> &edges);
void GetEdgeBasedNodes( std::vector< EdgeBasedNode> & nodes); void GetEdgeBasedNodes(std::vector<EdgeBasedNode> &nodes);
TurnInstruction AnalyzeTurn( TurnInstruction AnalyzeTurn(const NodeID u, const NodeID v, const NodeID w) const;
const NodeID u,
const NodeID v,
const NodeID w
) const;
int GetTurnPenalty( int GetTurnPenalty(const NodeID u, const NodeID v, const NodeID w, lua_State *myLuaState) const;
const NodeID u,
const NodeID v,
const NodeID w,
lua_State *myLuaState
) const;
unsigned GetNumberOfEdgeBasedNodes() const; unsigned GetNumberOfEdgeBasedNodes() const;
struct SpeedProfileProperties{ struct SpeedProfileProperties
SpeedProfileProperties() : {
trafficSignalPenalty(0), SpeedProfileProperties()
uTurnPenalty(0), : trafficSignalPenalty(0), uTurnPenalty(0), has_turn_penalty_function(false)
has_turn_penalty_function(false) {
{ } }
int trafficSignalPenalty; int trafficSignalPenalty;
int uTurnPenalty; int uTurnPenalty;
bool has_turn_penalty_function; bool has_turn_penalty_function;
} speed_profile; } speed_profile;
private: private:
typedef NodeBasedDynamicGraph::NodeIterator NodeIterator; typedef NodeBasedDynamicGraph::NodeIterator NodeIterator;
typedef NodeBasedDynamicGraph::EdgeIterator EdgeIterator; typedef NodeBasedDynamicGraph::EdgeIterator EdgeIterator;
typedef NodeBasedDynamicGraph::EdgeData EdgeData; typedef NodeBasedDynamicGraph::EdgeData EdgeData;
@ -130,22 +118,16 @@ private:
void CompressGeometry(); void CompressGeometry();
void RenumberEdges(); void RenumberEdges();
void GenerateEdgeExpandedNodes(); void GenerateEdgeExpandedNodes();
void GenerateEdgeExpandedEdges( void GenerateEdgeExpandedEdges(const std::string &original_edge_data_filename,
const std::string& original_edge_data_filename, lua_State *lua_state);
lua_State* lua_state
);
void InsertEdgeBasedNode( void InsertEdgeBasedNode(NodeBasedDynamicGraph::NodeIterator u,
NodeBasedDynamicGraph::NodeIterator u,
NodeBasedDynamicGraph::NodeIterator v, NodeBasedDynamicGraph::NodeIterator v,
NodeBasedDynamicGraph::EdgeIterator e1, NodeBasedDynamicGraph::EdgeIterator e1,
bool belongsToTinyComponent bool belongsToTinyComponent);
);
void FlushVectorToStream( void FlushVectorToStream(std::ofstream &edge_data_file,
std::ofstream & edge_data_file, std::vector<OriginalEdgeData> &original_edge_data_vector) const;
std::vector<OriginalEdgeData> & original_edge_data_vector
) const;
unsigned max_id; unsigned max_id;
}; };

View File

@ -70,14 +70,14 @@ unsigned GeometryCompressor::GetPositionForID(const EdgeID edge_id) const
void GeometryCompressor::SerializeInternalVector(const std::string &path) const void GeometryCompressor::SerializeInternalVector(const std::string &path) const
{ {
boost::filesystem::fstream geometry_out_stream(path, std::ios::binary|std::ios::out); boost::filesystem::fstream geometry_out_stream(path, std::ios::binary | std::ios::out);
const unsigned number_of_compressed_geometries = m_compressed_geometries.size() + 1; const unsigned number_of_compressed_geometries = m_compressed_geometries.size() + 1;
BOOST_ASSERT(UINT_MAX != number_of_compressed_geometries); BOOST_ASSERT(UINT_MAX != number_of_compressed_geometries);
geometry_out_stream.write((char *)&number_of_compressed_geometries, sizeof(unsigned)); geometry_out_stream.write((char *)&number_of_compressed_geometries, sizeof(unsigned));
// write indices array // write indices array
unsigned prefix_sum_of_list_indices = 0; unsigned prefix_sum_of_list_indices = 0;
for (auto & elem : m_compressed_geometries) for (auto &elem : m_compressed_geometries)
{ {
geometry_out_stream.write((char *)&prefix_sum_of_list_indices, sizeof(unsigned)); geometry_out_stream.write((char *)&prefix_sum_of_list_indices, sizeof(unsigned));
@ -94,7 +94,7 @@ void GeometryCompressor::SerializeInternalVector(const std::string &path) const
unsigned control_sum = 0; unsigned control_sum = 0;
// write compressed geometries // write compressed geometries
for (auto & elem : m_compressed_geometries) for (auto &elem : m_compressed_geometries)
{ {
const std::vector<CompressedNode> &current_vector = elem; const std::vector<CompressedNode> &current_vector = elem;
const unsigned unpacked_size = current_vector.size(); const unsigned unpacked_size = current_vector.size();
@ -146,7 +146,8 @@ void GeometryCompressor::CompressEdge(const EdgeID edge_id_1,
m_free_list.pop_back(); m_free_list.pop_back();
} }
const boost::unordered_map<EdgeID, unsigned>::const_iterator iter = m_edge_id_to_list_index_map.find(edge_id_1); const boost::unordered_map<EdgeID, unsigned>::const_iterator iter =
m_edge_id_to_list_index_map.find(edge_id_1);
BOOST_ASSERT(iter != m_edge_id_to_list_index_map.end()); BOOST_ASSERT(iter != m_edge_id_to_list_index_map.end());
const unsigned edge_bucket_id1 = iter->second; const unsigned edge_bucket_id1 = iter->second;
BOOST_ASSERT(edge_bucket_id1 == GetPositionForID(edge_id_1)); BOOST_ASSERT(edge_bucket_id1 == GetPositionForID(edge_id_1));

View File

@ -56,7 +56,7 @@ class GeometryCompressor
private: private:
void IncreaseFreeList(); void IncreaseFreeList();
std::vector<std::vector<CompressedNode> > m_compressed_geometries; std::vector<std::vector<CompressedNode>> m_compressed_geometries;
std::vector<unsigned> m_free_list; std::vector<unsigned> m_free_list;
boost::unordered_map<EdgeID, unsigned> m_edge_id_to_list_index_map; boost::unordered_map<EdgeID, unsigned> m_edge_id_to_list_index_map;
}; };

View File

@ -27,136 +27,154 @@ SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#include "TemporaryStorage.h" #include "TemporaryStorage.h"
TemporaryStorage::TemporaryStorage() { TemporaryStorage::TemporaryStorage() { temp_directory = boost::filesystem::temp_directory_path(); }
temp_directory = boost::filesystem::temp_directory_path();
}
TemporaryStorage & TemporaryStorage::GetInstance(){ TemporaryStorage &TemporaryStorage::GetInstance()
{
static TemporaryStorage static_instance; static TemporaryStorage static_instance;
return static_instance; return static_instance;
} }
TemporaryStorage::~TemporaryStorage() { TemporaryStorage::~TemporaryStorage() { RemoveAll(); }
RemoveAll();
}
void TemporaryStorage::RemoveAll() { void TemporaryStorage::RemoveAll()
{
boost::mutex::scoped_lock lock(mutex); boost::mutex::scoped_lock lock(mutex);
for(unsigned slot_id = 0; slot_id < stream_data_list.size(); ++slot_id) { for (unsigned slot_id = 0; slot_id < stream_data_list.size(); ++slot_id)
{
DeallocateSlot(slot_id); DeallocateSlot(slot_id);
} }
stream_data_list.clear(); stream_data_list.clear();
} }
int TemporaryStorage::AllocateSlot() { int TemporaryStorage::AllocateSlot()
{
boost::mutex::scoped_lock lock(mutex); boost::mutex::scoped_lock lock(mutex);
try { try
{
stream_data_list.push_back(StreamData()); stream_data_list.push_back(StreamData());
} catch(boost::filesystem::filesystem_error & e) { }
catch (boost::filesystem::filesystem_error &e)
{
Abort(e); Abort(e);
} }
CheckIfTemporaryDeviceFull(); CheckIfTemporaryDeviceFull();
return stream_data_list.size() - 1; return stream_data_list.size() - 1;
} }
void TemporaryStorage::DeallocateSlot(const int slot_id) { void TemporaryStorage::DeallocateSlot(const int slot_id)
try { {
StreamData & data = stream_data_list[slot_id]; try
{
StreamData &data = stream_data_list[slot_id];
boost::mutex::scoped_lock lock(*data.readWriteMutex); boost::mutex::scoped_lock lock(*data.readWriteMutex);
if(!boost::filesystem::exists(data.temp_path)) { if (!boost::filesystem::exists(data.temp_path))
{
return; return;
} }
if(data.temp_file->is_open()) { if (data.temp_file->is_open())
{
data.temp_file->close(); data.temp_file->close();
} }
boost::filesystem::remove(data.temp_path); boost::filesystem::remove(data.temp_path);
} catch(boost::filesystem::filesystem_error & e) { }
catch (boost::filesystem::filesystem_error &e)
{
Abort(e); Abort(e);
} }
} }
void TemporaryStorage::WriteToSlot( void TemporaryStorage::WriteToSlot(const int slot_id, char *pointer, const std::size_t size)
const int slot_id, {
char * pointer, try
const std::size_t size {
) { StreamData &data = stream_data_list[slot_id];
try {
StreamData & data = stream_data_list[slot_id];
BOOST_ASSERT(data.write_mode); BOOST_ASSERT(data.write_mode);
boost::mutex::scoped_lock lock(*data.readWriteMutex); boost::mutex::scoped_lock lock(*data.readWriteMutex);
BOOST_ASSERT_MSG( BOOST_ASSERT_MSG(data.write_mode, "Writing after first read is not allowed");
data.write_mode, if (1073741824 < data.buffer.size())
"Writing after first read is not allowed" {
);
if( 1073741824 < data.buffer.size() ) {
data.temp_file->write(&data.buffer[0], data.buffer.size()); data.temp_file->write(&data.buffer[0], data.buffer.size());
// data.temp_file->write(pointer, size); // data.temp_file->write(pointer, size);
data.buffer.clear(); data.buffer.clear();
CheckIfTemporaryDeviceFull(); CheckIfTemporaryDeviceFull();
} }
data.buffer.insert(data.buffer.end(), pointer, pointer+size); data.buffer.insert(data.buffer.end(), pointer, pointer + size);
}
} catch(boost::filesystem::filesystem_error & e) { catch (boost::filesystem::filesystem_error &e)
{
Abort(e); Abort(e);
} }
} }
void TemporaryStorage::ReadFromSlot( void TemporaryStorage::ReadFromSlot(const int slot_id, char *pointer, const std::size_t size)
const int slot_id, {
char * pointer, try
const std::size_t size {
) { StreamData &data = stream_data_list[slot_id];
try {
StreamData & data = stream_data_list[slot_id];
boost::mutex::scoped_lock lock(*data.readWriteMutex); boost::mutex::scoped_lock lock(*data.readWriteMutex);
if( data.write_mode ) { if (data.write_mode)
{
data.write_mode = false; data.write_mode = false;
data.temp_file->write(&data.buffer[0], data.buffer.size()); data.temp_file->write(&data.buffer[0], data.buffer.size());
data.buffer.clear(); data.buffer.clear();
data.temp_file->seekg( data.temp_file->beg ); data.temp_file->seekg(data.temp_file->beg);
BOOST_ASSERT( data.temp_file->beg == data.temp_file->tellg() ); BOOST_ASSERT(data.temp_file->beg == data.temp_file->tellg());
} }
BOOST_ASSERT( !data.write_mode ); BOOST_ASSERT(!data.write_mode);
data.temp_file->read(pointer, size); data.temp_file->read(pointer, size);
} catch(boost::filesystem::filesystem_error & e) { }
catch (boost::filesystem::filesystem_error &e)
{
Abort(e); Abort(e);
} }
} }
uint64_t TemporaryStorage::GetFreeBytesOnTemporaryDevice() { uint64_t TemporaryStorage::GetFreeBytesOnTemporaryDevice()
{
uint64_t value = -1; uint64_t value = -1;
try { try
{
boost::filesystem::path p = boost::filesystem::temp_directory_path(); boost::filesystem::path p = boost::filesystem::temp_directory_path();
boost::filesystem::space_info s = boost::filesystem::space( p ); boost::filesystem::space_info s = boost::filesystem::space(p);
value = s.free; value = s.free;
} catch(boost::filesystem::filesystem_error & e) { }
catch (boost::filesystem::filesystem_error &e)
{
Abort(e); Abort(e);
} }
return value; return value;
} }
void TemporaryStorage::CheckIfTemporaryDeviceFull() { void TemporaryStorage::CheckIfTemporaryDeviceFull()
{
boost::filesystem::path p = boost::filesystem::temp_directory_path(); boost::filesystem::path p = boost::filesystem::temp_directory_path();
boost::filesystem::space_info s = boost::filesystem::space( p ); boost::filesystem::space_info s = boost::filesystem::space(p);
if( (1024*1024) > s.free ) { if ((1024 * 1024) > s.free)
{
throw OSRMException("temporary device is full"); throw OSRMException("temporary device is full");
} }
} }
boost::filesystem::fstream::pos_type TemporaryStorage::Tell(const int slot_id) { boost::filesystem::fstream::pos_type TemporaryStorage::Tell(const int slot_id)
{
boost::filesystem::fstream::pos_type position; boost::filesystem::fstream::pos_type position;
try { try
StreamData & data = stream_data_list[slot_id]; {
StreamData &data = stream_data_list[slot_id];
boost::mutex::scoped_lock lock(*data.readWriteMutex); boost::mutex::scoped_lock lock(*data.readWriteMutex);
position = data.temp_file->tellp(); position = data.temp_file->tellp();
} catch(boost::filesystem::filesystem_error & e) { }
catch (boost::filesystem::filesystem_error &e)
{
Abort(e); Abort(e);
} }
return position; return position;
} }
void TemporaryStorage::Abort(const boost::filesystem::filesystem_error& e) { void TemporaryStorage::Abort(const boost::filesystem::filesystem_error &e)
{
RemoveAll(); RemoveAll();
throw OSRMException(e.what()); throw OSRMException(e.what());
} }

View File

@ -56,61 +56,52 @@ SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
static boost::filesystem::path temp_directory; static boost::filesystem::path temp_directory;
static std::string TemporaryFilePattern("OSRM-%%%%-%%%%-%%%%"); static std::string TemporaryFilePattern("OSRM-%%%%-%%%%-%%%%");
class TemporaryStorage { class TemporaryStorage
public: {
static TemporaryStorage & GetInstance(); public:
static TemporaryStorage &GetInstance();
virtual ~TemporaryStorage(); virtual ~TemporaryStorage();
int AllocateSlot(); int AllocateSlot();
void DeallocateSlot(const int slot_id); void DeallocateSlot(const int slot_id);
void WriteToSlot(const int slot_id, char * pointer, const std::size_t size); void WriteToSlot(const int slot_id, char *pointer, const std::size_t size);
void ReadFromSlot(const int slot_id, char * pointer, const std::size_t size); void ReadFromSlot(const int slot_id, char *pointer, const std::size_t size);
//returns the number of free bytes // returns the number of free bytes
uint64_t GetFreeBytesOnTemporaryDevice(); uint64_t GetFreeBytesOnTemporaryDevice();
boost::filesystem::fstream::pos_type Tell(const int slot_id); boost::filesystem::fstream::pos_type Tell(const int slot_id);
void RemoveAll(); void RemoveAll();
private:
private:
TemporaryStorage(); TemporaryStorage();
TemporaryStorage(TemporaryStorage const &){}; TemporaryStorage(TemporaryStorage const &) {};
TemporaryStorage & operator=(TemporaryStorage const &) { TemporaryStorage &operator=(TemporaryStorage const &) { return *this; }
return *this;
}
void Abort(const boost::filesystem::filesystem_error& e); void Abort(const boost::filesystem::filesystem_error &e);
void CheckIfTemporaryDeviceFull(); void CheckIfTemporaryDeviceFull();
struct StreamData { struct StreamData
{
bool write_mode; bool write_mode;
boost::filesystem::path temp_path; boost::filesystem::path temp_path;
boost::shared_ptr<boost::filesystem::fstream> temp_file; boost::shared_ptr<boost::filesystem::fstream> temp_file;
boost::shared_ptr<boost::mutex> readWriteMutex; boost::shared_ptr<boost::mutex> readWriteMutex;
std::vector<char> buffer; std::vector<char> buffer;
StreamData() : StreamData()
write_mode(true), : write_mode(true), temp_path(boost::filesystem::unique_path(temp_directory.append(
temp_path( TemporaryFilePattern.begin(), TemporaryFilePattern.end()))),
boost::filesystem::unique_path( temp_file(new boost::filesystem::fstream(
temp_directory.append( temp_path, std::ios::in | std::ios::out | std::ios::trunc | std::ios::binary)),
TemporaryFilePattern.begin(),
TemporaryFilePattern.end()
)
)
),
temp_file(
new boost::filesystem::fstream(
temp_path,
std::ios::in|std::ios::out|std::ios::trunc|std::ios::binary
)
),
readWriteMutex(boost::make_shared<boost::mutex>()) readWriteMutex(boost::make_shared<boost::mutex>())
{ {
if( temp_file->fail() ) { if (temp_file->fail())
{
throw OSRMException("temporary file could not be created"); throw OSRMException("temporary file could not be created");
} }
} }
}; };
//vector of file streams that is used to store temporary data // vector of file streams that is used to store temporary data
boost::mutex mutex; boost::mutex mutex;
std::vector<StreamData> stream_data_list; std::vector<StreamData> stream_data_list;
}; };