Updated FSF address in all files. Fixes ticket:51
[debian/gnuradio] / gr-radar / src / lib / simulation.h
1 /* -*- c++ -*- */
2 /*
3  * Copyright 2005 Free Software Foundation, Inc.
4  * 
5  * This file is part of GNU Radio
6  * 
7  * GNU Radio is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation; either version 2, or (at your option)
10  * any later version.
11  * 
12  * GNU Radio is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU General Public License for more details.
16  * 
17  * You should have received a copy of the GNU General Public License
18  * along with GNU Radio; see the file COPYING.  If not, write to
19  * the Free Software Foundation, Inc., 51 Franklin Street,
20  * Boston, MA 02110-1301, USA.
21  */
22
23 #ifndef INCLUDED_SIMULATION_H
24 #define INCLUDED_SIMULATION_H
25
26 #include <string>
27 #include <cmath>
28 #include <vector>
29 #include <iostream>
30
31
32 class point {
33   double        d_x, d_y;
34 public:
35   point(double x = 0, double y = 0) : d_x(x), d_y(y) {}
36
37   double x() const { return d_x; }
38   double y() const { return d_y; }
39
40   void set_x(double x) { d_x = x; }
41   void set_y(double y) { d_y = y; }
42
43   static point
44   add(const point &p1, const point &p2)
45   {
46     return point(p1.x() + p2.x(),
47                  p1.y() + p2.y());
48   }
49
50   static point
51   sub(const point &p1, const point &p2)
52   {
53     return point(p1.x() - p2.x(), p1.y() - p2.y());
54   }
55
56   static double
57   distance(const point &p1, const point &p2)
58   {
59     point d = point::sub(p1, p2);
60     return std::sqrt(d.x()*d.x() + d.y()*d.y());
61   }
62
63 };
64
65
66 class dyn_object {
67   point         d_pos;
68   point         d_vel;
69   std::string   d_name;
70 public:
71   dyn_object(point pos=point(0,0), point vel=point(0,0), const std::string name="")
72     : d_pos(pos), d_vel(vel), d_name(name) {}
73
74   virtual ~dyn_object() {}
75
76   point pos() const { return d_pos; }
77   point vel() const { return d_vel; }
78   std::string name() const { return d_name; }
79
80   void set_pos(point pos) { d_pos = pos; }
81   void set_vel(point vel) { d_vel = vel; }
82
83   virtual bool update(double delta_t);
84
85   static double
86   distance(const dyn_object &o1, const dyn_object &o2)
87   {
88     return point::distance(o1.pos(), o2.pos());
89   }
90
91 };
92
93
94 class simulation {
95   double                        d_timestep;
96   double                        d_now;
97 protected:
98   std::vector<dyn_object *>     d_obj;
99
100 public:
101   simulation(double timestep = 1.0, double now = 0.0)
102     : d_timestep(timestep), d_now(now) {}
103   virtual ~simulation();
104   virtual bool update();
105   virtual bool run(long long nsteps);
106
107   void add_object(dyn_object *obj);
108   double now() const { return d_now; }
109   double timestep() const { return d_timestep; }
110 };
111
112 std::ostream& operator<<(std::ostream& out, const dyn_object& o);
113 std::ostream& operator<<(std::ostream& out, const point& p);
114
115 #endif /* INCLUDED_SIMULATION_H */
116