00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021 #ifndef _COORD_H
00022 #define _COORD_H
00023
00024 #include <omnetpp.h>
00025 #include "FWMath.h"
00026
00061 class Coord : public cObject
00062 {
00063 public:
00065 static const double UNDEFINED;
00066
00067 protected:
00070 double x;
00071 double y;
00072 double z;
00076 bool use2DFlag;
00077
00078 public:
00079
00084 Coord(bool use2D = false)
00085 :x(0.0), y(0.0), use2DFlag(use2D)
00086 {
00087
00088 if (use2D) {
00089 z = UNDEFINED;
00090 } else {
00091 z = 0.0;
00092 }
00093 }
00094
00096 Coord(double x, double y, double z)
00097 : x(x), y(y), z(z) , use2DFlag(false) {}
00098
00100 Coord(double x, double y)
00101 : x(x), y(y), z(UNDEFINED) , use2DFlag(true) {}
00102
00103
00105 Coord( const Coord& pos )
00106 : cObject(pos), x(pos.x), y(pos.y), z(pos.z), use2DFlag(pos.use2DFlag) {}
00107
00109 Coord( const Coord* pos )
00110 : cObject(*pos), x(pos->x), y(pos->y), z(pos->z), use2DFlag(pos->use2DFlag) {}
00111
00113 std::string info() const {
00114 std::stringstream os;
00115 if (use2DFlag) {
00116 os << "(" << x << "," << y << ")";
00117 } else {
00118 os << "(" << x << "," << y << "," << z << ")";
00119 }
00120 return os.str();
00121 }
00122
00128 friend Coord operator+(const Coord& a, const Coord& b) {
00129 Coord tmp = a;
00130 tmp += b;
00131 return tmp;
00132 }
00133
00139 friend Coord operator-(const Coord& a, const Coord& b) {
00140 Coord tmp = a;
00141 tmp -= b;
00142 return tmp;
00143 }
00144
00146 friend Coord operator*(const Coord& a, double f) {
00147 Coord tmp = a;
00148 tmp *= f;
00149 return tmp;
00150 }
00151
00153 friend Coord operator/(const Coord& a, double f) {
00154 Coord tmp = a;
00155 tmp /= f;
00156 return tmp;
00157 }
00158
00162 Coord operator*=(double f) {
00163 x *= f;
00164 y *= f;
00165 z *= f;
00166 return *this;
00167 }
00168
00172 Coord operator/=(double f) {
00173 x /= f;
00174 y /= f;
00175 z /= f;
00176 return *this;
00177 }
00178
00184 Coord operator+=(const Coord& a) {
00185 x += a.x;
00186 y += a.y;
00187 z += a.z;
00188 return *this;
00189 }
00190
00196 Coord operator=(const Coord& a) {
00197 x = a.x;
00198 y = a.y;
00199 z = a.z;
00200 use2DFlag = a.use2DFlag;
00201 return *this;
00202 }
00203
00209 Coord operator-=(const Coord& a) {
00210 x -= a.x;
00211 y -= a.y;
00212 z -= a.z;
00213 return *this;
00214 }
00215
00224 friend bool operator==(const Coord& a, const Coord& b) {
00225 return FWMath::close(a.x, b.x) && FWMath::close(a.y, b.y) && FWMath::close(a.z, b.z);
00226 }
00227
00235 friend bool operator!=(const Coord& a, const Coord& b) {
00236 return !(a==b);
00237 }
00238
00244 double distance( const Coord& a ) const {
00245 Coord dist=*this-a;
00246 return dist.length();
00247 }
00248
00254 double sqrdist( const Coord& a ) const {
00255 Coord dist=*this-a;
00256 return dist.squareLength();
00257 }
00258
00264 double sqrTorusDist(const Coord& b, const Coord& playgroundSize) const;
00265
00269 double squareLength() const
00270 {
00271 return x * x + y * y + z * z;
00272 }
00273
00277 double length() const
00278 {
00279 return sqrt(squareLength());
00280 }
00281
00285 double getX() const{ return x; }
00286
00290 void setX(double x){this->x = x;}
00291
00295 double getY() const{ return y; }
00296
00300 void setY(double y){this->y = y;}
00301
00310 double getZ() const{ return z; }
00311
00322 void setZ(double z){this->z = z;}
00323
00331 bool isValid() const {
00332 return (z == UNDEFINED) || !use2DFlag;
00333 }
00334
00338 bool is2D() const { return use2DFlag; }
00339
00343 bool is3D() const { return !use2DFlag; }
00344
00353 bool isInRectangle(const Coord& upperLeftCorner, const Coord& lowerRightCorner) const {
00354 return x >= upperLeftCorner.x && x <= lowerRightCorner.x &&
00355 y >= upperLeftCorner.y && y <= lowerRightCorner.y &&
00356 (use2DFlag || (z >= upperLeftCorner.z && z <= lowerRightCorner.z));
00357 }
00358
00368 bool isInBoundary(const Coord& lowerBound, const Coord& upperBound) const {
00369 return x >= lowerBound.x && x < upperBound.x &&
00370 y >= lowerBound.y && y < upperBound.y &&
00371 (use2DFlag || (z >= lowerBound.z && z < upperBound.z));
00372 }
00373
00377 Coord min(const Coord& a) {
00378 Coord tmp = *this;
00379 tmp.setX(this->x < a.x ? this->x : a.x);
00380 tmp.setY(this->y < a.y ? this->y : a.y);
00381 if (tmp.is3D())
00382 tmp.setZ(this->z < a.z ? this->z : a.z);
00383 return tmp;
00384 }
00385
00389 Coord max(const Coord& a) {
00390 Coord tmp = *this;
00391 tmp.setX(this->x > a.x ? this->x : a.x);
00392 tmp.setY(this->y > a.y ? this->y : a.y);
00393 if (tmp.is3D())
00394 tmp.setZ(this->z > a.z ? this->z : a.z);
00395 return tmp;
00396 }
00397
00402 friend bool operator>(const Coord& a, const Coord& b) {
00403 return (a.x > b.x
00404 && a.y > b.y
00405 && (a.is3D() ? a.z > b.z : true));
00406 }
00407
00412 friend bool operator<(const Coord& a, const Coord& b) {
00413 return (a.x < b.x
00414 && a.y < b.y
00415 && (a.is3D() ? a.z < b.z : true));
00416 }
00417 };
00418
00419 #endif
00420