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 2001-2010 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/Position2D.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     while (angle2<-180)
00071         angle2 = 360 + angle2;
00072     return angle2;
00073 }
00074 
00075 
00076 SUMOReal
00077 NBHelpers::normRelAngle(SUMOReal angle1, SUMOReal angle2) {
00078     SUMOReal rel = relAngle(angle1, angle2);
00079     if (rel<-170||rel>170)
00080         rel = -180;
00081     return rel;
00082 }
00083 
00084 
00085 std::string
00086 NBHelpers::normalIDRepresentation(const std::string &id) {
00087     std::stringstream strm1(id);
00088     long numid;
00089     strm1 >> numid;
00090     std::stringstream strm2;
00091     strm2 << numid;
00092     return strm2.str();
00093 }
00094 
00095 
00096 SUMOReal
00097 NBHelpers::distance(NBNode *node1, NBNode *node2) {
00098     return node1->getPosition().distanceTo(node2->getPosition());
00099 }
00100 
00101 
00102 
00103 /****************************************************************************/

Generated on Wed May 5 00:06:33 2010 for Sumo - Simulation of Urban MObility by  doxygen 1.5.6