SUMO - Simulation of Urban MObility

Position.h

Go to the documentation of this file.
00001 /****************************************************************************/
00007 // A position in the 2D- or 3D-world
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 #ifndef Position_h
00020 #define Position_h
00021 
00022 
00023 // ===========================================================================
00024 // included modules
00025 // ===========================================================================
00026 #include <iostream>
00027 #include <cmath>
00028 
00029 #ifdef _MSC_VER
00030 #include <windows_config.h>
00031 #else
00032 #include <config.h>
00033 #endif
00034 
00035 // ===========================================================================
00036 // class definitions
00037 // ===========================================================================
00042 class Position {
00043 public:
00045     Position() : myX(0.0), myY(0.0), myZ(0.0) { }
00046 
00048     Position(SUMOReal x, SUMOReal y)
00049         : myX(x), myY(y), myZ(0) { }
00050 
00052     Position(SUMOReal x, SUMOReal y, SUMOReal z)
00053         : myX(x), myY(y), myZ(z) { }
00054 
00056     ~Position() { }
00057 
00059     SUMOReal x() const {
00060         return myX;
00061     }
00062 
00064     SUMOReal y() const {
00065         return myY;
00066     }
00067 
00069     SUMOReal z() const {
00070         return myZ;
00071     }
00072 
00074     void set(SUMOReal x, SUMOReal y) {
00075         myX = x;
00076         myY = y;
00077     }
00078 
00080     void set(SUMOReal x, SUMOReal y, SUMOReal z) {
00081         myX = x;
00082         myY = y;
00083         myZ = z;
00084     }
00085 
00087     void set(const Position& pos) {
00088         myX = pos.myX;
00089         myY = pos.myY;
00090         myZ = pos.myZ;
00091     }
00092 
00093 
00095     void mul(SUMOReal val) {
00096         myX *= val;
00097         myY *= val;
00098         myZ *= val;
00099     }
00100 
00102     void mul(SUMOReal mx, SUMOReal my) {
00103         myX *= mx;
00104         myY *= my;
00105     }
00106 
00108     void mul(SUMOReal mx, SUMOReal my, SUMOReal mz) {
00109         myX *= mx;
00110         myY *= my;
00111         myZ *= mz;
00112     }
00113 
00115     void add(const Position& pos) {
00116         myX += pos.myX;
00117         myY += pos.myY;
00118         myZ += pos.myZ;
00119     }
00120 
00122     void add(SUMOReal dx, SUMOReal dy) {
00123         myX += dx;
00124         myY += dy;
00125     }
00126 
00128     void add(SUMOReal dx, SUMOReal dy, SUMOReal dz) {
00129         myX += dx;
00130         myY += dy;
00131         myZ += dz;
00132     }
00133 
00135     void sub(SUMOReal dx, SUMOReal dy) {
00136         myX -= dx;
00137         myY -= dy;
00138     }
00139 
00141     void sub(SUMOReal dx, SUMOReal dy, SUMOReal dz) {
00142         myX -= dx;
00143         myY -= dy;
00144         myZ -= dz;
00145     }
00146 
00148     void sub(const Position& pos) {
00149         myX -= pos.myX;
00150         myY -= pos.myY;
00151         myZ -= pos.myZ;
00152     }
00153 
00154     void norm2d() {
00155         SUMOReal val = sqrt(myX*myX + myY*myY);
00156         myX = myX / val;
00157         myY = myY / val;
00158     }
00159 
00160     void reshiftRotate(SUMOReal xoff, SUMOReal yoff, SUMOReal rot) {
00161         SUMOReal x = myX * cos(rot) -myY * sin(rot) + xoff;
00162         SUMOReal y = myX * sin(rot) + yoff + myY * cos(rot);
00163         myX = x;
00164         myY = y;
00165     }
00166 
00167 
00169     friend std::ostream& operator<<(std::ostream& os, const Position& p) {
00170         os << p.x() << "," << p.y();
00171         if (p.z()!=SUMOReal(0.0)) {
00172             os << "," << p.z();
00173         }
00174         return os;
00175     }
00176 
00177     Position operator+(const Position& p2) const {
00178         return Position(myX+p2.myX,  myY+p2.myY, myZ+p2.myZ);
00179     }
00180 
00181     Position operator-(const Position& p2) const {
00182         return Position(myX-p2.myX,  myY-p2.myY, myZ-p2.myZ);
00183     }
00184 
00185     Position operator*(SUMOReal scalar) const {
00186         return Position(myX * scalar,myY * scalar, myZ *scalar);
00187     }
00188 
00189     bool operator==(const Position& p2) const {
00190         return myX==p2.myX && myY==p2.myY && myZ==p2.myZ;
00191     }
00192 
00193     bool operator!=(const Position& p2) const {
00194         return myX!=p2.myX || myY!=p2.myY || myZ!=p2.myZ;
00195     }
00196 
00197 
00198     bool almostSame(const Position& p2, SUMOReal maxDiv=POSITION_EPS) const {
00199         return fabs(myX-p2.myX)<maxDiv && fabs(myY-p2.myY)<maxDiv && fabs(myZ-p2.myZ)<maxDiv;
00200     }
00201 
00202 
00203     inline SUMOReal distanceTo(const Position& p2) const {
00204         return sqrt(distanceSquaredTo(p2));
00205     }
00206 
00207 
00208     inline SUMOReal distanceSquaredTo(const Position& p2) const {
00209         return (myX-p2.myX)*(myX-p2.myX) + (myY-p2.myY)*(myY-p2.myY) + (myZ-p2.myZ)*(myZ-p2.myZ);
00210     }
00211 
00213     Position crossProduct(const Position& pos) {
00214         return Position(
00215                    myY * pos.myZ - myZ * pos.myY,
00216                    myZ * pos.myX - myX * pos.myZ,
00217                    myX * pos.myY - myY * pos.myX);
00218     }
00219 
00221     inline SUMOReal dotProduct(const Position& pos) {
00222         return myX * pos.myX + myY * pos.myY + myZ * pos.myZ;
00223     }
00224 
00225 
00226 private:
00228     SUMOReal myX;
00229 
00231     SUMOReal myY;
00232 
00234     SUMOReal myZ;
00235 
00236 };
00237 
00238 
00239 #endif
00240 
00241 /****************************************************************************/
00242 
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Friends Defines