SUMO - Simulation of Urban MObility

NBHelpers.cpp

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