|
SUMO - Simulation of Urban MObility
|
00001 /************************************************************************ 00002 * * 00003 * Copyright 2004, Brown University, Providence, RI * 00004 * * 00005 * Permission to use and modify this software and its documentation * 00006 * for any purpose other than its incorporation into a commercial * 00007 * product is hereby granted without fee. Recipient agrees not to * 00008 * re-distribute this software or any modifications of this * 00009 * software without the permission of Brown University. Brown * 00010 * University makes no representations or warrantees about the * 00011 * suitability of this software for any purpose. It is provided * 00012 * "as is" without express or implied warranty. Brown University * 00013 * requests notification of any modifications to this software or * 00014 * its documentation. Notice should be sent to: * 00015 * * 00016 * To: * 00017 * Software Librarian * 00018 * Laboratory for Engineering Man/Machine Systems, * 00019 * Division of Engineering, Box D, * 00020 * Brown University * 00021 * Providence, RI 02912 * 00022 * Software_Librarian@lems.brown.edu * 00023 * * 00024 * We will acknowledge all electronic notifications. * 00025 * * 00026 ************************************************************************/ 00027 00028 #ifndef POINTS_H 00029 #define POINTS_H 00030 00031 //#include <vcl_iostream.h> 00032 #include <iostream> 00033 #include <cmath> 00034 #include <cassert> 00035 00036 template <class coord_type> 00037 class Point2D 00038 { 00039 protected: 00040 coord_type _x, _y; 00041 public: 00042 Point2D() 00043 { 00044 _x=_y=-1; 00045 } 00046 00047 inline Point2D(coord_type x0, coord_type y0) 00048 { 00049 _x = x0; 00050 _y = y0; 00051 } 00052 00053 template <class point_type> 00054 inline Point2D<coord_type>(const Point2D<point_type> &old) 00055 { 00056 _x = old.getX(); 00057 _y = old.getY(); 00058 } 00059 00060 template <class point_type> 00061 inline Point2D<coord_type>& operator=(const Point2D<point_type> &old) 00062 { 00063 _x = old.getX(); 00064 _y = old.getY(); 00065 00066 return *this; 00067 } 00068 00069 //TODO : The following function is a "specialization" of the previous 00070 //function. Basically this is supposed to kick in when the two points 00071 //have the same type. This is here to improve speed, vcl_since 00072 //the values of x and y can be accessed directly without the need 00073 //for the get. functions. 00074 // 00075 //Check if this thing works. 00076 00077 inline Point2D<coord_type>(const Point2D<coord_type> &old) 00078 { 00079 _x = old._x; 00080 _y = old._y; 00081 } 00082 00083 inline Point2D<coord_type>& operator=(const Point2D<coord_type> &old) 00084 { 00085 _x = old._x; 00086 _y = old._y; 00087 return *this; 00088 } 00089 00090 ~Point2D() 00091 { 00092 } 00093 00094 coord_type getX() const { return _x; }; 00095 coord_type getY() const { return _y; }; 00096 coord_type x() const { return _x; }; 00097 coord_type y() const { return _y; }; 00098 00099 void setX(coord_type nx) { _x = nx; } 00100 void setY(coord_type ny) { _y = ny; } 00101 void set(coord_type nx, coord_type ny) { setX(nx); setY(ny); } 00102 00103 coord_type operator[](int i) const { 00104 switch(i) { 00105 case 0: return _x; 00106 case 1: return _y; 00107 case 2: return 1; 00108 default: assert(NULL == "Point2D::operator[] index out of range"); 00109 return 0; 00110 } 00111 } 00112 00113 template <class point_type> 00114 bool operator==(const Point2D<point_type> &old) const 00115 { 00116 if ((_x == old._x)&&(_y==old._y)) 00117 return true; 00118 else 00119 return false; 00120 } 00121 00122 template <class point_type> 00123 bool operator!=(const Point2D<point_type> &old) const 00124 { 00125 if ((_x == old._x)&&(_y==old._y)) 00126 return false; 00127 else 00128 return true; 00129 } 00130 00131 00132 template <class point_type> 00133 Point2D<coord_type>& operator+=(const Point2D<point_type> &old) 00134 { 00135 _x += old._x; 00136 _y += old._y; 00137 00138 return *this; 00139 } 00140 00141 template <class point_type> 00142 Point2D<coord_type>& operator-=(const Point2D<point_type> &old) 00143 { 00144 _x -= old._x; 00145 _y -= old._y; 00146 00147 return *this; 00148 } 00149 00153 void rotate(double angle) 00154 { 00155 coord_type rot_x=0; 00156 coord_type rot_y=0; 00157 00158 rot_x = ( _x*cos(angle)+_y*sin(angle)); 00159 rot_y = (-_x*sin(angle)+_y*cos(angle)); 00160 00161 _x = rot_x; 00162 _y = rot_y; 00163 } 00164 00165 void swap() 00166 { 00167 std::swap(_x,_y); 00168 } 00169 00170 double magnitude() const 00171 { 00172 return (sqrt(_x*_x+_y*_y)); 00173 } 00174 }; 00175 00176 00177 /*--------------------- Multiplication --------------------------------*/ 00178 00179 template <class mul_type, class point_type> 00180 inline Point2D<point_type> operator*(mul_type val, const Point2D<point_type> pt) 00181 { 00182 return Point2D<point_type>(val*pt.getX(), val*pt.getY()); 00183 } 00184 00185 00186 template <class point_type, class mul_type> 00187 inline Point2D<point_type> operator*(const Point2D<point_type> pt, mul_type val) 00188 { 00189 return val*pt; 00190 } 00191 00192 00193 00194 00195 /*--------------------- Division --------------------------------*/ 00196 00197 template <class point_type, class div_type> 00198 inline Point2D<point_type> operator/(const Point2D<point_type> pt, div_type val) 00199 { 00200 if (val ==0) 00201 std::cout<<" Error: <Point2D operator/> Division by 0"<<std::endl; 00202 return Point2D<point_type>(pt.getX()/val, pt.getY()/val); 00203 } 00204 00205 00206 00207 /*-------------------------------------------------------------*/ 00208 00209 /* 00210 * Note: Return type is same as the first point. Not the smartest thing to do. 00211 * But no other way out. Can try typelists. 00212 * */ 00213 00214 template <class point1_type, class point2_type> 00215 inline Point2D<double> operator+(const Point2D<point1_type> &pt1, const Point2D<point2_type> &pt2) 00216 { 00217 return Point2D<double>(pt1.getX()+pt2.getX(), pt1.getY()+pt2.getY()); 00218 } 00219 00220 /* 00221 template<class coord_type> 00222 inline Point2D<coord_type> operator+(const Point2D<coord_type> &pt1,const Point2D<coord_type> &pt2) 00223 { 00224 return Point2D<coord_type>(pt1.getX()+pt2.getX(), pt1.getY()+pt2.getY()); 00225 } 00226 */ 00227 template <class point1_type, class point2_type> 00228 inline Point2D<point1_type> operator-(Point2D<point1_type> pt1, Point2D<point2_type> pt2) 00229 { 00230 return Point2D<point1_type>(pt1.getX()-pt2.getX(), pt1.getY()-pt2.getY()); 00231 } 00232 00233 /*------------------------------------------------------------*/ 00234 /* 00235 template <class point_type> 00236 inline vcl_ostream & operator<< (vcl_ostream & os, const Point2D<point_type> pt) 00237 { 00238 os<<" ["<<pt.getX()<<", "<<pt.getY()<<"] "; 00239 return os; 00240 } 00241 */ 00242 /*------------------------------------------------------------*/ 00243 00244 template <class point_type1, class point_type2> 00245 double euc_distance(Point2D<point_type1> pt1, Point2D<point_type2> pt2) 00246 { 00247 double x_dist, y_dist,dist=0; 00248 00249 x_dist = (pt1.getX()-pt2.getX())*(pt1.getX()-pt2.getX()); 00250 y_dist = (pt1.getY()-pt2.getY())*(pt1.getY()-pt2.getY()); 00251 00252 dist = sqrt(x_dist+y_dist); 00253 return dist; 00254 } 00255 00256 #endif /*POINTS_H*/