SUMO - Simulation of Urban MObility
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
points.h
Go to the documentation of this file.
1 /************************************************************************
2  * *
3  * Copyright 2004, Brown University, Providence, RI *
4  * *
5  * Permission to use and modify this software and its documentation *
6  * for any purpose other than its incorporation into a commercial *
7  * product is hereby granted without fee. Recipient agrees not to *
8  * re-distribute this software or any modifications of this *
9  * software without the permission of Brown University. Brown *
10  * University makes no representations or warrantees about the *
11  * suitability of this software for any purpose. It is provided *
12  * "as is" without express or implied warranty. Brown University *
13  * requests notification of any modifications to this software or *
14  * its documentation. Notice should be sent to: *
15  * *
16  * To: *
17  * Software Librarian *
18  * Laboratory for Engineering Man/Machine Systems, *
19  * Division of Engineering, Box D, *
20  * Brown University *
21  * Providence, RI 02912 *
22  * Software_Librarian@lems.brown.edu *
23  * *
24  * We will acknowledge all electronic notifications. *
25  * *
26  ************************************************************************/
27 
28 #ifndef POINTS_H
29 #define POINTS_H
30 
31 //#include <vcl_iostream.h>
32 #include <iostream>
33 #include <cmath>
34 #include <cassert>
35 
36 template <class coord_type>
37 class Point2D
38  {
39  protected:
40  coord_type _x, _y;
41  public:
43  {
44  _x=_y=-1;
45  }
46 
47  inline Point2D(coord_type x0, coord_type y0)
48  {
49  _x = x0;
50  _y = y0;
51  }
52 
53  template <class point_type>
55  {
56  _x = old.getX();
57  _y = old.getY();
58  }
59 
60  template <class point_type>
62  {
63  _x = old.getX();
64  _y = old.getY();
65 
66  return *this;
67  }
68 
69  //TODO : The following function is a "specialization" of the previous
70  //function. Basically this is supposed to kick in when the two points
71  //have the same type. This is here to improve speed, vcl_since
72  //the values of x and y can be accessed directly without the need
73  //for the get. functions.
74  //
75  //Check if this thing works.
76 
78  {
79  _x = old._x;
80  _y = old._y;
81  }
82 
84  {
85  _x = old._x;
86  _y = old._y;
87  return *this;
88  }
89 
91  {
92  }
93 
94  coord_type getX() const { return _x; };
95  coord_type getY() const { return _y; };
96  coord_type x() const { return _x; };
97  coord_type y() const { return _y; };
98 
99  void setX(coord_type nx) { _x = nx; }
100  void setY(coord_type ny) { _y = ny; }
101  void set(coord_type nx, coord_type ny) { setX(nx); setY(ny); }
102 
103  coord_type operator[](int i) const {
104  switch(i) {
105  case 0: return _x;
106  case 1: return _y;
107  case 2: return 1;
108  default: assert(NULL == "Point2D::operator[] index out of range");
109  return 0;
110  }
111  }
112 
113  template <class point_type>
114  bool operator==(const Point2D<point_type> &old) const
115  {
116  if ((_x == old._x)&&(_y==old._y))
117  return true;
118  else
119  return false;
120  }
121 
122  template <class point_type>
123  bool operator!=(const Point2D<point_type> &old) const
124  {
125  if ((_x == old._x)&&(_y==old._y))
126  return false;
127  else
128  return true;
129  }
130 
131 
132  template <class point_type>
134  {
135  _x += old._x;
136  _y += old._y;
137 
138  return *this;
139  }
140 
141  template <class point_type>
143  {
144  _x -= old._x;
145  _y -= old._y;
146 
147  return *this;
148  }
149 
153  void rotate(double angle)
154  {
155  coord_type rot_x=0;
156  coord_type rot_y=0;
157 
158  rot_x = ( _x*cos(angle)+_y*sin(angle));
159  rot_y = (-_x*sin(angle)+_y*cos(angle));
160 
161  _x = rot_x;
162  _y = rot_y;
163  }
164 
165  void swap()
166  {
167  std::swap(_x,_y);
168  }
169 
170  double magnitude() const
171  {
172  return (sqrt(_x*_x+_y*_y));
173  }
174  };
175 
176 
177 /*--------------------- Multiplication --------------------------------*/
178 
179  template <class mul_type, class point_type>
180 inline Point2D<point_type> operator*(mul_type val, const Point2D<point_type> pt)
181  {
182  return Point2D<point_type>(val*pt.getX(), val*pt.getY());
183  }
184 
185 
186  template <class point_type, class mul_type>
187 inline Point2D<point_type> operator*(const Point2D<point_type> pt, mul_type val)
188  {
189  return val*pt;
190  }
191 
192 
193 
194 
195 /*--------------------- Division --------------------------------*/
196 
197  template <class point_type, class div_type>
198 inline Point2D<point_type> operator/(const Point2D<point_type> pt, div_type val)
199  {
200  if (val ==0)
201  std::cout<<" Error: <Point2D operator/> Division by 0"<<std::endl;
202  return Point2D<point_type>(pt.getX()/val, pt.getY()/val);
203  }
204 
205 
206 
207 /*-------------------------------------------------------------*/
208 
209 /*
210  * Note: Return type is same as the first point. Not the smartest thing to do.
211  * But no other way out. Can try typelists.
212  * */
213 
214  template <class point1_type, class point2_type>
216  {
217  return Point2D<double>(pt1.getX()+pt2.getX(), pt1.getY()+pt2.getY());
218  }
219 
220 /*
221 template<class coord_type>
222 inline Point2D<coord_type> operator+(const Point2D<coord_type> &pt1,const Point2D<coord_type> &pt2)
223  {
224  return Point2D<coord_type>(pt1.getX()+pt2.getX(), pt1.getY()+pt2.getY());
225  }
226 */
227  template <class point1_type, class point2_type>
229  {
230  return Point2D<point1_type>(pt1.getX()-pt2.getX(), pt1.getY()-pt2.getY());
231  }
232 
233 /*------------------------------------------------------------*/
234  /*
235  template <class point_type>
236 inline vcl_ostream & operator<< (vcl_ostream & os, const Point2D<point_type> pt)
237  {
238  os<<" ["<<pt.getX()<<", "<<pt.getY()<<"] ";
239  return os;
240  }
241 */
242 /*------------------------------------------------------------*/
243 
244  template <class point_type1, class point_type2>
246  {
247  double x_dist, y_dist,dist=0;
248 
249  x_dist = (pt1.getX()-pt2.getX())*(pt1.getX()-pt2.getX());
250  y_dist = (pt1.getY()-pt2.getY())*(pt1.getY()-pt2.getY());
251 
252  dist = sqrt(x_dist+y_dist);
253  return dist;
254  }
255 
256 #endif /*POINTS_H*/
Point2D< coord_type > & operator-=(const Point2D< point_type > &old)
Definition: points.h:142
~Point2D()
Definition: points.h:90
Point2D()
Definition: points.h:42
void setY(coord_type ny)
Definition: points.h:100
coord_type _y
Definition: points.h:40
bool operator==(const Point2D< point_type > &old) const
Definition: points.h:114
Point2D< double > operator+(const Point2D< point1_type > &pt1, const Point2D< point2_type > &pt2)
Definition: points.h:215
void set(coord_type nx, coord_type ny)
Definition: points.h:101
Point2D< coord_type > & operator+=(const Point2D< point_type > &old)
Definition: points.h:133
double euc_distance(Point2D< point_type1 > pt1, Point2D< point_type2 > pt2)
Definition: points.h:245
coord_type getY() const
Definition: points.h:95
coord_type x() const
Definition: points.h:96
Definition: points.h:37
void rotate(double angle)
Definition: points.h:153
Point2D< point_type > operator/(const Point2D< point_type > pt, div_type val)
Definition: points.h:198
coord_type _x
Definition: points.h:40
Point2D< point_type > operator*(mul_type val, const Point2D< point_type > pt)
Definition: points.h:180
void setX(coord_type nx)
Definition: points.h:99
Point2D(coord_type x0, coord_type y0)
Definition: points.h:47
bool operator!=(const Point2D< point_type > &old) const
Definition: points.h:123
coord_type y() const
Definition: points.h:97
Point2D< point1_type > operator-(Point2D< point1_type > pt1, Point2D< point2_type > pt2)
Definition: points.h:228
void swap()
Definition: points.h:165
Point2D< coord_type > & operator=(const Point2D< coord_type > &old)
Definition: points.h:83
double magnitude() const
Definition: points.h:170
Point2D< coord_type > & operator=(const Point2D< point_type > &old)
Definition: points.h:61
coord_type getX() const
Definition: points.h:94
coord_type operator[](int i) const
Definition: points.h:103