reduce cyclomatic complexity in double->string bearing conversion

This commit is contained in:
Dennis Luxen 2015-01-23 11:44:35 +01:00
parent d6e76fd1c0
commit 62f2a42f28
3 changed files with 53 additions and 51 deletions

View File

@ -1,6 +1,6 @@
/* /*
Copyright (c) 2013, Project OSRM, Dennis Luxen, others Copyright (c) 2015, Project OSRM, Dennis Luxen, others
All rights reserved. All rights reserved.
Redistribution and use in source and binary forms, with or without modification, Redistribution and use in source and binary forms, with or without modification,
@ -27,37 +27,37 @@ SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#include "bearing.hpp" #include "bearing.hpp"
std::string Bearing::Get(const double heading) std::string bearing::get(const double heading)
{ {
if (heading <= 202.5) if (heading <= 22.5)
{
if (heading >= 0. && heading <= 22.5)
{ {
return "N"; return "N";
} }
if (heading > 22.5 && heading <= 67.5) if (heading <= 67.5)
{ {
return "NE"; return "NE";
} }
if (heading > 67.5 && heading <= 112.5) if (heading <= 112.5)
{ {
return "E"; return "E";
} }
if (heading > 112.5 && heading <= 157.5) if (heading <= 157.5)
{ {
return "SE"; return "SE";
} }
if (heading <= 202.5)
{
return "S"; return "S";
} }
if (heading > 202.5 && heading <= 247.5) if (heading <= 247.5)
{ {
return "SW"; return "SW";
} }
if (heading > 247.5 && heading <= 292.5) if (heading <= 292.5)
{ {
return "W"; return "W";
} }
if (heading > 292.5 && heading <= 337.5) if (heading <= 337.5)
{ {
return "NW"; return "NW";
} }

View File

@ -1,6 +1,6 @@
/* /*
Copyright (c) 2013, Project OSRM, Dennis Luxen, others Copyright (c) 2015, Project OSRM, Dennis Luxen, others
All rights reserved. All rights reserved.
Redistribution and use in source and binary forms, with or without modification, Redistribution and use in source and binary forms, with or without modification,
@ -25,14 +25,14 @@ SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/ */
#ifndef BEARING_HPP_ #ifndef BEARING_HPP
#define BEARING_HPP_ #define BEARING_HPP
#include <string> #include <string>
struct Bearing struct bearing
{ {
static std::string Get(const double heading); static std::string get(const double heading);
}; };
#endif // BEARING_HPP_ #endif // BEARING_HPP

View File

@ -73,7 +73,9 @@ template <class DataFacadeT> class JSONDescriptor final : public BaseDescriptor<
ExtractRouteNames<DataFacadeT, Segment> GenerateRouteNames; ExtractRouteNames<DataFacadeT, Segment> GenerateRouteNames;
public: public:
explicit JSONDescriptor(DataFacadeT *facade) : facade(facade), entered_restricted_area_count(0) {} explicit JSONDescriptor(DataFacadeT *facade) : facade(facade), entered_restricted_area_count(0)
{
}
void SetConfig(const DescriptorConfig &c) final { config = c; } void SetConfig(const DescriptorConfig &c) final { config = c; }
@ -91,8 +93,8 @@ template <class DataFacadeT> class JSONDescriptor final : public BaseDescriptor<
description_factory.AppendSegment(current_coordinate, path_data); description_factory.AppendSegment(current_coordinate, path_data);
++added_element_count; ++added_element_count;
} }
description_factory.SetEndSegment( description_factory.SetEndSegment(leg_phantoms.target_phantom, target_traversed_in_reverse,
leg_phantoms.target_phantom, target_traversed_in_reverse, is_via_leg); is_via_leg);
++added_element_count; ++added_element_count;
BOOST_ASSERT((route_leg.size() + 1) == added_element_count); BOOST_ASSERT((route_leg.size() + 1) == added_element_count);
return added_element_count; return added_element_count;
@ -130,8 +132,7 @@ template <class DataFacadeT> class JSONDescriptor final : public BaseDescriptor<
#endif #endif
DescribeLeg(raw_route.unpacked_path_segments[i], DescribeLeg(raw_route.unpacked_path_segments[i],
raw_route.segment_end_coordinates[i], raw_route.segment_end_coordinates[i],
raw_route.target_traversed_in_reverse[i], raw_route.target_traversed_in_reverse[i], raw_route.is_via_leg(i));
raw_route.is_via_leg(i));
BOOST_ASSERT(0 < added_segments); BOOST_ASSERT(0 < added_segments);
} }
description_factory.Run(config.zoom_level); description_factory.Run(config.zoom_level);
@ -145,10 +146,8 @@ template <class DataFacadeT> class JSONDescriptor final : public BaseDescriptor<
if (config.instructions) if (config.instructions)
{ {
JSON::Array json_route_instructions; JSON::Array json_route_instructions;
BuildTextualDescription(description_factory, BuildTextualDescription(description_factory, json_route_instructions,
json_route_instructions, raw_route.shortest_path_length, shortest_path_segments);
raw_route.shortest_path_length,
shortest_path_segments);
json_result.values["route_instructions"] = json_route_instructions; json_result.values["route_instructions"] = json_route_instructions;
} }
description_factory.BuildRouteSummary(description_factory.get_entire_length(), description_factory.BuildRouteSummary(description_factory.get_entire_length(),
@ -225,15 +224,15 @@ template <class DataFacadeT> class JSONDescriptor final : public BaseDescriptor<
JSON::Array json_current_alt_instructions; JSON::Array json_current_alt_instructions;
if (config.instructions) if (config.instructions)
{ {
BuildTextualDescription(alternate_description_factory, BuildTextualDescription(
json_current_alt_instructions, alternate_description_factory, json_current_alt_instructions,
raw_route.alternative_path_length, raw_route.alternative_path_length, alternative_path_segments);
alternative_path_segments);
json_alt_instructions.values.push_back(json_current_alt_instructions); json_alt_instructions.values.push_back(json_current_alt_instructions);
json_result.values["alternative_instructions"] = json_alt_instructions; json_result.values["alternative_instructions"] = json_alt_instructions;
} }
alternate_description_factory.BuildRouteSummary( alternate_description_factory.BuildRouteSummary(
alternate_description_factory.get_entire_length(), raw_route.alternative_path_length); alternate_description_factory.get_entire_length(),
raw_route.alternative_path_length);
JSON::Object json_alternate_route_summary; JSON::Object json_alternate_route_summary;
JSON::Array json_alternate_route_summary_array; JSON::Array json_alternate_route_summary_array;
@ -285,10 +284,12 @@ template <class DataFacadeT> class JSONDescriptor final : public BaseDescriptor<
std::string hint; std::string hint;
for (const auto i : osrm::irange<std::size_t>(0, raw_route.segment_end_coordinates.size())) for (const auto i : osrm::irange<std::size_t>(0, raw_route.segment_end_coordinates.size()))
{ {
ObjectEncoder::EncodeToBase64(raw_route.segment_end_coordinates[i].source_phantom, hint); ObjectEncoder::EncodeToBase64(raw_route.segment_end_coordinates[i].source_phantom,
hint);
json_location_hint_array.values.push_back(hint); json_location_hint_array.values.push_back(hint);
} }
ObjectEncoder::EncodeToBase64(raw_route.segment_end_coordinates.back().target_phantom, hint); ObjectEncoder::EncodeToBase64(raw_route.segment_end_coordinates.back().target_phantom,
hint);
json_location_hint_array.values.push_back(hint); json_location_hint_array.values.push_back(hint);
json_hint_object.values["locations"] = json_location_hint_array; json_hint_object.values["locations"] = json_location_hint_array;
json_result.values["hint_data"] = json_hint_object; json_result.values["hint_data"] = json_hint_object;
@ -331,8 +332,8 @@ template <class DataFacadeT> class JSONDescriptor final : public BaseDescriptor<
std::string current_turn_instruction; std::string current_turn_instruction;
if (TurnInstruction::LeaveRoundAbout == current_instruction) if (TurnInstruction::LeaveRoundAbout == current_instruction)
{ {
temp_instruction = temp_instruction = cast::integral_to_string(
cast::integral_to_string(cast::enum_to_underlying(TurnInstruction::EnterRoundAbout)); cast::enum_to_underlying(TurnInstruction::EnterRoundAbout));
current_turn_instruction += temp_instruction; current_turn_instruction += temp_instruction;
current_turn_instruction += "-"; current_turn_instruction += "-";
temp_instruction = cast::integral_to_string(round_about.leave_at_exit + 1); temp_instruction = cast::integral_to_string(round_about.leave_at_exit + 1);
@ -341,7 +342,8 @@ template <class DataFacadeT> class JSONDescriptor final : public BaseDescriptor<
} }
else else
{ {
temp_instruction = cast::integral_to_string(cast::enum_to_underlying(current_instruction)); temp_instruction =
cast::integral_to_string(cast::enum_to_underlying(current_instruction));
current_turn_instruction += temp_instruction; current_turn_instruction += temp_instruction;
} }
json_instruction_row.values.push_back(current_turn_instruction); json_instruction_row.values.push_back(current_turn_instruction);
@ -354,14 +356,13 @@ template <class DataFacadeT> class JSONDescriptor final : public BaseDescriptor<
json_instruction_row.values.push_back( json_instruction_row.values.push_back(
cast::integral_to_string(static_cast<unsigned>(segment.length)) + "m"); cast::integral_to_string(static_cast<unsigned>(segment.length)) + "m");
const double bearing_value = (segment.bearing / 10.); const double bearing_value = (segment.bearing / 10.);
json_instruction_row.values.push_back(Bearing::Get(bearing_value)); json_instruction_row.values.push_back(bearing::get(bearing_value));
json_instruction_row.values.push_back( json_instruction_row.values.push_back(
static_cast<unsigned>(round(bearing_value))); static_cast<unsigned>(round(bearing_value)));
json_instruction_row.values.push_back(segment.travel_mode); json_instruction_row.values.push_back(segment.travel_mode);
route_segments_list.emplace_back( route_segments_list.emplace_back(
segment.name_id, segment.name_id, static_cast<int>(segment.length),
static_cast<int>(segment.length),
static_cast<unsigned>(route_segments_list.size())); static_cast<unsigned>(route_segments_list.size()));
json_instruction_array.values.push_back(json_instruction_row); json_instruction_array.values.push_back(json_instruction_row);
} }
@ -377,14 +378,15 @@ template <class DataFacadeT> class JSONDescriptor final : public BaseDescriptor<
} }
JSON::Array json_last_instruction_row; JSON::Array json_last_instruction_row;
temp_instruction = cast::integral_to_string(cast::enum_to_underlying(TurnInstruction::ReachedYourDestination)); temp_instruction = cast::integral_to_string(
cast::enum_to_underlying(TurnInstruction::ReachedYourDestination));
json_last_instruction_row.values.push_back(temp_instruction); json_last_instruction_row.values.push_back(temp_instruction);
json_last_instruction_row.values.push_back(""); json_last_instruction_row.values.push_back("");
json_last_instruction_row.values.push_back(0); json_last_instruction_row.values.push_back(0);
json_last_instruction_row.values.push_back(necessary_segments_running_index - 1); json_last_instruction_row.values.push_back(necessary_segments_running_index - 1);
json_last_instruction_row.values.push_back(0); json_last_instruction_row.values.push_back(0);
json_last_instruction_row.values.push_back("0m"); json_last_instruction_row.values.push_back("0m");
json_last_instruction_row.values.push_back(Bearing::Get(0.0)); json_last_instruction_row.values.push_back(bearing::get(0.0));
json_last_instruction_row.values.push_back(0.); json_last_instruction_row.values.push_back(0.);
json_instruction_array.values.push_back(json_last_instruction_row); json_instruction_array.values.push_back(json_last_instruction_row);
} }