Line data Source code
1 : /****************************************************************************/ 2 : // Eclipse SUMO, Simulation of Urban MObility; see https://eclipse.dev/sumo 3 : // Copyright (C) 2001-2024 German Aerospace Center (DLR) and others. 4 : // This program and the accompanying materials are made available under the 5 : // terms of the Eclipse Public License 2.0 which is available at 6 : // https://www.eclipse.org/legal/epl-2.0/ 7 : // This Source Code may also be made available under the following Secondary 8 : // Licenses when the conditions for such availability set forth in the Eclipse 9 : // Public License 2.0 are satisfied: GNU General Public License, version 2 10 : // or later which is available at 11 : // https://www.gnu.org/licenses/old-licenses/gpl-2.0-standalone.html 12 : // SPDX-License-Identifier: EPL-2.0 OR GPL-2.0-or-later 13 : /****************************************************************************/ 14 : /// @file NBHelpers.cpp 15 : /// @author Daniel Krajzewicz 16 : /// @author Sascha Krieg 17 : /// @author Michael Behrisch 18 : /// @author Jakob Erdmann 19 : /// @date Tue, 20 Nov 2001 20 : /// 21 : // Some mathematical helper methods 22 : /****************************************************************************/ 23 : #include <config.h> 24 : 25 : #include <cmath> 26 : #include <string> 27 : #include <sstream> 28 : #include <iostream> 29 : #include <fstream> 30 : //#include <iomanip> 31 : #include <utils/common/StringUtils.h> 32 : #include <utils/common/StringTokenizer.h> 33 : #include <utils/common/MsgHandler.h> 34 : #include <utils/common/StringUtils.h> 35 : #include <utils/geom/Position.h> 36 : #include <utils/geom/GeomHelper.h> 37 : #include "NBNode.h" 38 : #include "NBHelpers.h" 39 : 40 : 41 : // =========================================================================== 42 : // method definitions 43 : // =========================================================================== 44 : double 45 10564250 : NBHelpers::relAngle(double angle1, double angle2) { 46 10564250 : angle2 -= angle1; 47 11341816 : while (angle2 > 180.) { 48 777566 : angle2 -= 360.; 49 : } 50 11612987 : while (angle2 < -180.) { 51 1048737 : angle2 += 360.; 52 : } 53 10564250 : return angle2; 54 : } 55 : 56 : 57 : double 58 10049336 : NBHelpers::normRelAngle(double angle1, double angle2) { 59 10049336 : double rel = relAngle(angle1, angle2); 60 10049336 : if (rel + NUMERICAL_EPS >= 180) { 61 : return -180; 62 : } else { 63 9898577 : return rel; 64 : } 65 : } 66 : 67 : 68 : std::string 69 9096 : NBHelpers::normalIDRepresentation(const std::string& id) { 70 9096 : std::stringstream strm1(id); 71 : long numid; 72 : strm1 >> numid; 73 9096 : std::stringstream strm2; 74 9096 : strm2 << numid; 75 9096 : return strm2.str(); 76 9096 : } 77 : 78 : 79 : double 80 0 : NBHelpers::distance(NBNode* node1, NBNode* node2) { 81 0 : return node1->getPosition().distanceTo(node2->getPosition()); 82 : } 83 : 84 : 85 : void 86 5 : NBHelpers::loadEdgesFromFile(const std::string& file, std::set<std::string>& into) { 87 5 : std::ifstream strm(file.c_str()); 88 5 : if (!strm.good()) { 89 0 : throw ProcessError(TLF("Could not load names of edges too keep from '%'.", file)); 90 : } 91 40 : while (strm.good()) { 92 : std::string name; 93 35 : strm >> name; 94 : into.insert(name); 95 : // maybe we're loading an edge-selection 96 70 : if (StringUtils::startsWith(name, "edge:")) { 97 28 : into.insert(name.substr(5)); 98 : } 99 : } 100 5 : } 101 : 102 : 103 : void 104 12 : NBHelpers::loadPrefixedIDsFomFile(const std::string& file, const std::string prefix, std::set<std::string>& into) { 105 12 : std::ifstream strm(file.c_str()); 106 12 : if (!strm.good()) { 107 0 : throw ProcessError(TLF("Could not load IDs from '%'.", file)); 108 : } 109 60 : while (strm.good()) { 110 : std::string prefixedID; 111 48 : strm >> prefixedID; 112 96 : if (StringUtils::startsWith(prefixedID, prefix)) { 113 20 : into.insert(prefixedID.substr(prefix.size())); 114 : } 115 : } 116 12 : } 117 : 118 : void 119 16692 : NBHelpers::interpretLaneID(const std::string& lane_id, std::string& edge_id, int& index) { 120 : // assume lane_id = edge_id + '_' + index 121 16692 : const std::string::size_type sep_index = lane_id.rfind('_'); 122 16692 : if (sep_index == std::string::npos) { 123 0 : WRITE_ERRORF(TL("Invalid lane id '%' (missing '_')."), lane_id); 124 : } 125 16692 : edge_id = lane_id.substr(0, sep_index); 126 16692 : std::string index_string = lane_id.substr(sep_index + 1); 127 : try { 128 16692 : index = StringUtils::toInt(index_string); 129 0 : } catch (NumberFormatException&) { 130 0 : WRITE_ERRORF(TL("Invalid lane index '%' for lane '%'."), index_string, lane_id); 131 0 : } 132 16692 : } 133 : 134 : 135 : /****************************************************************************/