SUMO - Simulation of Urban MObility
NBHelpers.cpp
Go to the documentation of this file.
00001 /****************************************************************************/
00009 // Some mathematical helper methods
00010 /****************************************************************************/
00011 // SUMO, Simulation of Urban MObility; see http://sumo.sourceforge.net/
00012 // Copyright (C) 2001-2012 DLR (http://www.dlr.de/) and contributors
00013 /****************************************************************************/
00014 //
00015 //   This file is part of SUMO.
00016 //   SUMO is free software: you can redistribute it and/or modify
00017 //   it under the terms of the GNU General Public License as published by
00018 //   the Free Software Foundation, either version 3 of the License, or
00019 //   (at your option) any later version.
00020 //
00021 /****************************************************************************/
00022 
00023 
00024 // ===========================================================================
00025 // included modules
00026 // ===========================================================================
00027 #ifdef _MSC_VER
00028 #include <windows_config.h>
00029 #else
00030 #include <config.h>
00031 #endif
00032 
00033 #include <cmath>
00034 #include <string>
00035 #include <sstream>
00036 #include "NBNode.h"
00037 #include "NBHelpers.h"
00038 #include <utils/common/StringTokenizer.h>
00039 #include <utils/geom/Position.h>
00040 #include <utils/geom/GeomHelper.h>
00041 #include <iostream>
00042 
00043 #ifdef CHECK_MEMORY_LEAKS
00044 #include <foreign/nvwa/debug_new.h>
00045 #endif // CHECK_MEMORY_LEAKS
00046 
00047 
00048 // ===========================================================================
00049 // method definitions
00050 // ===========================================================================
00051 SUMOReal
00052 NBHelpers::angle(SUMOReal x1, SUMOReal y1, SUMOReal x2, SUMOReal y2) {
00053     SUMOReal angle = (SUMOReal) atan2(x1 - x2, y1 - y2) * (SUMOReal) 180.0 / (SUMOReal) PI;
00054     if (angle < 0) {
00055         angle = 360 + angle;
00056     }
00057     return angle;
00058 }
00059 
00060 
00061 SUMOReal
00062 NBHelpers::relAngle(SUMOReal angle1, SUMOReal angle2) {
00063     angle2 -= angle1;
00064     if (angle2 > 180) {
00065         angle2 = (360 - angle2) * -1;
00066     }
00067     while (angle2 < -180) {
00068         angle2 = 360 + angle2;
00069     }
00070     return angle2;
00071 }
00072 
00073 
00074 SUMOReal
00075 NBHelpers::normRelAngle(SUMOReal angle1, SUMOReal angle2) {
00076     SUMOReal rel = relAngle(angle1, angle2);
00077     if (rel < -170 || rel > 170) {
00078         rel = -180;
00079     }
00080     return rel;
00081 }
00082 
00083 
00084 std::string
00085 NBHelpers::normalIDRepresentation(const std::string& id) {
00086     std::stringstream strm1(id);
00087     long numid;
00088     strm1 >> numid;
00089     std::stringstream strm2;
00090     strm2 << numid;
00091     return strm2.str();
00092 }
00093 
00094 
00095 SUMOReal
00096 NBHelpers::distance(NBNode* node1, NBNode* node2) {
00097     return node1->getPosition().distanceTo(node2->getPosition());
00098 }
00099 
00100 
00101 
00102 /****************************************************************************/
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Friends Defines