diff --git a/descriptors/description_factory.cpp b/descriptors/description_factory.cpp index e92aacd6c..87af445e5 100644 --- a/descriptors/description_factory.cpp +++ b/descriptors/description_factory.cpp @@ -1,6 +1,6 @@ /* -Copyright (c) 2014, Project OSRM, Dennis Luxen, others +Copyright (c) 2015, Project OSRM, Dennis Luxen, others All rights reserved. Redistribution and use in source and binary forms, with or without modification, @@ -27,10 +27,10 @@ SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. #include "description_factory.hpp" -#include "../typedefs.h" #include "../algorithms/polyline_formatter.hpp" #include "../data_structures/internal_route_result.hpp" #include "../data_structures/turn_instructions.hpp" +#include "../typedefs.h" DescriptionFactory::DescriptionFactory() : entire_length(0) { via_indices.push_back(0); } @@ -120,3 +120,131 @@ void DescriptionFactory::BuildRouteSummary(const double distance, const unsigned summary.target_name_id = target_phantom.name_id; summary.BuildDurationAndLengthStrings(distance, time); } + +void DescriptionFactory::Run(const unsigned zoom_level) +{ + if (path_description.empty()) + { + return; + } + + /** starts at index 1 */ + path_description[0].length = 0; + for (unsigned i = 1; i < path_description.size(); ++i) + { + // move down names by one, q&d hack + path_description[i - 1].name_id = path_description[i].name_id; + path_description[i].length = FixedPointCoordinate::ApproximateEuclideanDistance( + path_description[i - 1].location, path_description[i].location); + } + + /*Simplify turn instructions + Input : + 10. Turn left on B 36 for 20 km + 11. Continue on B 35; B 36 for 2 km + 12. Continue on B 36 for 13 km + + becomes: + 10. Turn left on B 36 for 35 km + */ + // TODO: rework to check only end and start of string. + // stl string is way to expensive + + // unsigned lastTurn = 0; + // for(unsigned i = 1; i < path_description.size(); ++i) { + // string1 = sEngine.GetEscapedNameForNameID(path_description[i].name_id); + // if(TurnInstruction::GoStraight == path_description[i].turn_instruction) { + // if(std::string::npos != string0.find(string1+";") + // || std::string::npos != string0.find(";"+string1) + // || std::string::npos != string0.find(string1+" ;") + // || std::string::npos != string0.find("; "+string1) + // ){ + // SimpleLogger().Write() << "->next correct: " << string0 << " contains " << + // string1; + // for(; lastTurn != i; ++lastTurn) + // path_description[lastTurn].name_id = path_description[i].name_id; + // path_description[i].turn_instruction = TurnInstruction::NoTurn; + // } else if(std::string::npos != string1.find(string0+";") + // || std::string::npos != string1.find(";"+string0) + // || std::string::npos != string1.find(string0+" ;") + // || std::string::npos != string1.find("; "+string0) + // ){ + // SimpleLogger().Write() << "->prev correct: " << string1 << " contains " << + // string0; + // path_description[i].name_id = path_description[i-1].name_id; + // path_description[i].turn_instruction = TurnInstruction::NoTurn; + // } + // } + // if (TurnInstruction::NoTurn != path_description[i].turn_instruction) { + // lastTurn = i; + // } + // string0 = string1; + // } + + float segment_length = 0.; + unsigned segment_duration = 0; + unsigned segment_start_index = 0; + + for (unsigned i = 1; i < path_description.size(); ++i) + { + entire_length += path_description[i].length; + segment_length += path_description[i].length; + segment_duration += path_description[i].duration; + path_description[segment_start_index].length = segment_length; + path_description[segment_start_index].duration = segment_duration; + + if (TurnInstruction::NoTurn != path_description[i].turn_instruction) + { + BOOST_ASSERT(path_description[i].necessary); + segment_length = 0; + segment_duration = 0; + segment_start_index = i; + } + } + + // Post-processing to remove empty or nearly empty path segments + if (std::numeric_limits::epsilon() > path_description.back().length) + { + if (path_description.size() > 2) + { + path_description.pop_back(); + path_description.back().necessary = true; + path_description.back().turn_instruction = TurnInstruction::NoTurn; + target_phantom.name_id = (path_description.end() - 2)->name_id; + } + } + if (std::numeric_limits::epsilon() > path_description.front().length) + { + if (path_description.size() > 2) + { + path_description.erase(path_description.begin()); + path_description.front().turn_instruction = TurnInstruction::HeadOn; + path_description.front().necessary = true; + start_phantom.name_id = path_description.front().name_id; + } + } + + // Generalize poly line + polyline_generalizer.Run(path_description.begin(), path_description.end(), zoom_level); + + // fix what needs to be fixed else + unsigned necessary_pieces = 0; // a running index that counts the necessary pieces + for (unsigned i = 0; i < path_description.size() - 1 && path_description.size() >= 2; ++i) + { + if (path_description[i].necessary) + { + ++necessary_pieces; + if (path_description[i].is_via_location) + { // mark the end of a leg + via_indices.push_back(necessary_pieces); + } + const double angle = + path_description[i + 1].location.GetBearing(path_description[i].location); + path_description[i].bearing = static_cast(angle * 10); + } + } + via_indices.push_back(necessary_pieces + 1); + BOOST_ASSERT(via_indices.size() >= 2); + // BOOST_ASSERT(0 != necessary_pieces || path_description.empty()); + return; +} diff --git a/descriptors/description_factory.hpp b/descriptors/description_factory.hpp index 406e0530c..cb24b13c8 100644 --- a/descriptors/description_factory.hpp +++ b/descriptors/description_factory.hpp @@ -93,133 +93,7 @@ class DescriptionFactory return entire_length; } - template void Run(const DataFacadeT *facade, const unsigned zoomLevel) - { - if (path_description.empty()) - { - return; - } - - /** starts at index 1 */ - path_description[0].length = 0; - for (unsigned i = 1; i < path_description.size(); ++i) - { - // move down names by one, q&d hack - path_description[i - 1].name_id = path_description[i].name_id; - path_description[i].length = FixedPointCoordinate::ApproximateEuclideanDistance( - path_description[i - 1].location, path_description[i].location); - } - - /*Simplify turn instructions - Input : - 10. Turn left on B 36 for 20 km - 11. Continue on B 35; B 36 for 2 km - 12. Continue on B 36 for 13 km - - becomes: - 10. Turn left on B 36 for 35 km - */ - // TODO: rework to check only end and start of string. - // stl string is way to expensive - - // unsigned lastTurn = 0; - // for(unsigned i = 1; i < path_description.size(); ++i) { - // string1 = sEngine.GetEscapedNameForNameID(path_description[i].name_id); - // if(TurnInstruction::GoStraight == path_description[i].turn_instruction) { - // if(std::string::npos != string0.find(string1+";") - // || std::string::npos != string0.find(";"+string1) - // || std::string::npos != string0.find(string1+" ;") - // || std::string::npos != string0.find("; "+string1) - // ){ - // SimpleLogger().Write() << "->next correct: " << string0 << " contains " << - // string1; - // for(; lastTurn != i; ++lastTurn) - // path_description[lastTurn].name_id = path_description[i].name_id; - // path_description[i].turn_instruction = TurnInstruction::NoTurn; - // } else if(std::string::npos != string1.find(string0+";") - // || std::string::npos != string1.find(";"+string0) - // || std::string::npos != string1.find(string0+" ;") - // || std::string::npos != string1.find("; "+string0) - // ){ - // SimpleLogger().Write() << "->prev correct: " << string1 << " contains " << - // string0; - // path_description[i].name_id = path_description[i-1].name_id; - // path_description[i].turn_instruction = TurnInstruction::NoTurn; - // } - // } - // if (TurnInstruction::NoTurn != path_description[i].turn_instruction) { - // lastTurn = i; - // } - // string0 = string1; - // } - - float segment_length = 0.; - unsigned segment_duration = 0; - unsigned segment_start_index = 0; - - for (unsigned i = 1; i < path_description.size(); ++i) - { - entire_length += path_description[i].length; - segment_length += path_description[i].length; - segment_duration += path_description[i].duration; - path_description[segment_start_index].length = segment_length; - path_description[segment_start_index].duration = segment_duration; - - if (TurnInstruction::NoTurn != path_description[i].turn_instruction) - { - BOOST_ASSERT(path_description[i].necessary); - segment_length = 0; - segment_duration = 0; - segment_start_index = i; - } - } - - // Post-processing to remove empty or nearly empty path segments - if (std::numeric_limits::epsilon() > path_description.back().length) - { - if (path_description.size() > 2) - { - path_description.pop_back(); - path_description.back().necessary = true; - path_description.back().turn_instruction = TurnInstruction::NoTurn; - target_phantom.name_id = (path_description.end() - 2)->name_id; - } - } - if (std::numeric_limits::epsilon() > path_description.front().length) - { - if (path_description.size() > 2) - { - path_description.erase(path_description.begin()); - path_description.front().turn_instruction = TurnInstruction::HeadOn; - path_description.front().necessary = true; - start_phantom.name_id = path_description.front().name_id; - } - } - - // Generalize poly line - polyline_generalizer.Run(path_description.begin(), path_description.end(), zoomLevel); - - // fix what needs to be fixed else - unsigned necessary_pieces = 0; // a running index that counts the necessary pieces - for (unsigned i = 0; i < path_description.size() - 1 && path_description.size() >= 2; ++i) - { - if (path_description[i].necessary) - { - ++necessary_pieces; - if (path_description[i].is_via_location) - { // mark the end of a leg - via_indices.push_back(necessary_pieces); - } - const double angle = - path_description[i + 1].location.GetBearing(path_description[i].location); - path_description[i].bearing = static_cast(angle * 10); - } - } - via_indices.push_back(necessary_pieces + 1); - BOOST_ASSERT(via_indices.size() >= 2); - // BOOST_ASSERT(0 != necessary_pieces || path_description.empty()); - return; - } + void Run(const unsigned zoom_level); }; #endif /* DESCRIPTION_FACTORY_HPP */ diff --git a/descriptors/json_descriptor.hpp b/descriptors/json_descriptor.hpp index 8365d576d..74850d8f5 100644 --- a/descriptors/json_descriptor.hpp +++ b/descriptors/json_descriptor.hpp @@ -1,6 +1,6 @@ /* -Copyright (c) 2013, Project OSRM, Dennis Luxen, others +Copyright (c) 2015, Project OSRM, Dennis Luxen, others All rights reserved. Redistribution and use in source and binary forms, with or without modification, @@ -134,7 +134,7 @@ template class JSONDescriptor final : public BaseDescriptor< raw_route.is_via_leg(i)); BOOST_ASSERT(0 < added_segments); } - description_factory.Run(facade, config.zoom_level); + description_factory.Run(config.zoom_level); if (config.geometry) { @@ -210,7 +210,7 @@ template class JSONDescriptor final : public BaseDescriptor< alternate_description_factory.SetEndSegment( raw_route.segment_end_coordinates.back().target_phantom, raw_route.alt_source_traversed_in_reverse.back()); - alternate_description_factory.Run(facade, config.zoom_level); + alternate_description_factory.Run(config.zoom_level); if (config.geometry) {