KJB
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
psi_cylinder_world.h
Go to the documentation of this file.
1 /* $Id: psi_cylinder_world.h 18331 2014-12-02 04:30:55Z ksimek $ */
2 /* {{{=========================================================================== *
3  |
4  | Copyright (c) 1994-2011 by Kobus Barnard (author)
5  |
6  | Personal and educational use of this code is granted, provided that this
7  | header is kept intact, and that the authorship is not misrepresented, that
8  | its use is acknowledged in publications, and relevant papers are cited.
9  |
10  | For other use contact the author (kobus AT cs DOT arizona DOT edu).
11  |
12  | Please note that the code in this file has not necessarily been adequately
13  | tested. Naturally, there is no guarantee of performance, support, or fitness
14  | for any particular task. Nonetheless, I am interested in hearing about
15  | problems that you encounter.
16  |
17  | Author: Kyle Simek
18  * =========================================================================== }}}*/
19 
20 // vim: tabstop=4 shiftwidth=4 foldmethod=marker
21 
22 #ifndef PSI_CYLINDER_WORLD_H
23 #define PSI_CYLINDER_WORLD_H
24 
25 /*
26  * TODO: rename this file to psi_simulator.h
27  * Rationale: this now contains cartwheel stuff too (Simulator_type)
28  */
29 
30 #include <m_cpp/m_vector.h>
31 #include <g_cpp/g_cylinder.h>
32 #include <st_cpp/st_parapiped.h>
34 #include <vector>
35 #include <algorithm>
36 
37 #include <psi_cpp/psi_action.h>
40 
41 #include <psi_cpp/psi_bbox.h>
42 #include <psi_cpp/psi_util.h>
43 
44 #include <boost/shared_ptr.hpp>
45 
46 namespace kjb
47 {
48 namespace psi
49 {
50 
52 
62 {
63 public:
64  Entity_state();
65 
66  virtual void render() const;
68  return kjb::Cylinder(
69  get_position_3d(), // base
70  get_position_3d() + kjb::Vector(0.0, 1.0, 0.0) * get_height(), // top
71  get_width()/2.0); // radius
72  }
73 
74  const kjb::Vector& get_position() const {return pos_; }
75  kjb::Vector get_position_3d() const {return kjb::Vector(pos_[0], 0.0, pos_[1]); }
76 
77  const kjb::Vector& get_direction() const { return dir_; }
78  kjb::Vector get_direction_3d() const { return kjb::Vector(dir_[0], 0.0, dir_[1]); }
79 
80  double get_heading() const {return heading_; }
81 
82  double get_height() const {return height_; }
83  double get_width() const {return width_; }
84 
85  void set_position(const kjb::Vector& p) {pos_ = p; }
86 
88  void set_heading(double heading )
89  {
90  heading_ = heading;
91 
92  double rad = heading_;
93 
94  // angle = 0 -> pointing in z direction
95  dir_[1] = cos(rad);
96 
97  // angle = epsilon -> x slightly negative
98  dir_[0] = sin(rad);
99  }
100 
101  void set_height(double h) {height_ = h; }
102  void set_width(double w) {width_ = w; }
103 
104  /*
105  * @brief Move the entity @distance away in the direction that
106  * sepcified by @dir_
107  */
108 
109  void move_forward(double distance)
110  {
111  pos_ += distance * dir_;
112  }
113 
114 private:
115  kjb::Vector pos_;
116 
117  double heading_;
118  kjb::Vector dir_;
119 
120 
121  double height_;
122  double width_;
123 };
124 
125 
127 {
128  kjb::Cylinder c = entity.get_cylinder();
129  return get_bounding_box(c, cam);
130 
131 }
132 
134 {
135 
136 private:
137  struct Action_context
138  {
139  size_t action_index;
140  size_t entity_type;
141  size_t entity_index;
142  const Action* action;
143  std::vector<double> memory;
144  };
145  typedef boost::shared_ptr<Action_context> Action_context_ptr;
146 
147 public:
149  sampling_rate_(2),
150  sampling_period_(0.5)
151  { }
152 
153  bool simulate(
154  const std::vector<std::vector<Start_state> >& start_state,
155 // const std::vector<Weighted_box>& boxes,
156  const std::vector<std::vector<std::vector<Action> > >& actions
157  ) ;
158 
159 // bool simulate(
160 // const std::vector<Start_state>& start_state,
161 // const std::vector<std::vector<Action> >& actions
162 // )
163 // {
164 // return simulate(start_state, std::vector<Weighted_box>(), actions);
165 // }
166 
167 
169  const std::vector<std::vector<std::vector<Entity_state> > >& get_entity_states() const
170  {
171  return entity_states_;
172  }
173 
174  void set_sampling_rate(double r)
175  {
176  sampling_rate_ = r;
177  sampling_period_ = 1/r;
178  }
179 
180 protected:
188 // void pad_actions(std::vector<Action>& actions, double duration) const;
189 
191  double get_actions_length(const std::vector<Action>& actions);
192 
197  std::vector<size_t> quantize_action_durations(const std::vector<Action>& actions);
198 
203  std::vector<double> quantize_walk_through_points_action(const std::vector<double>& parameters);
204 
211  void execute_action(size_t time, Action_context& context)
212  {
213  switch(context.action->type)
214  {
215  case FOLLOW_ACTION:
216  execute_follow_action(time, context);
217  break;
219  execute_walk_through_points_action(time, context);
220  break;
221  case WALK_IN_ARC_ACTION:
222  execute_walk_in_arc_action(time, context);
223  break;
224  case WALK_ACTION:
225  execute_walk_action(time, context);
226  break;
227  case NULL_ACTION:
228  execute_null_action(time, context);
229  break;
230  default:
231  KJB_THROW_2(kjb::Illegal_argument, "Unknown action type.");
232  break;
233  }
234  }
235 
240  void execute_null_action(
241  size_t time,
242  Action_context& context
243  );
244 
252  void execute_walk_action(
253  size_t time,
254  Action_context& context
255  );
256 
266  (
267  size_t time,
268  Action_context& context
269  );
270 
281  (
282  size_t time,
283  Action_context& context
284  );
285 
298  (
299  size_t time,
300  Action_context& context
301  );
302 
303 private:
304  double round(double a)
305  {
306  return std::floor(a+0.5);
307  }
308 
309  size_t duration_to_samples(double length)
310  {
311  return round(length * sampling_rate_) + 1; // add one for first state (bookends)
312  }
313 
314 private:
315  std::vector<std::vector<std::vector<Entity_state> > > entity_states_;
316 
317  double sampling_rate_;
318  double sampling_period_;
319 };
320 
321 
322 } // namespace psi
323 } // namespace kjb
324 
325 #endif
Entity_state()
Definition: psi_cylinder_world.cpp:115
void set_heading(double heading)
direction in radians
Definition: psi_cylinder_world.h:88
void set_height(double h)
Definition: psi_cylinder_world.h:101
bool simulate(const std::vector< std::vector< Start_state > > &start_state, const std::vector< std::vector< std::vector< Action > > > &actions)
Definition: psi_cylinder_world.cpp:177
void set_sampling_rate(double r)
Definition: psi_cylinder_world.h:174
Class that represents an axis-aligned 2D rectangle. It is defined in terms of its (2D) center...
Definition: gr_2D_bounding_box.h:51
double get_actions_length(const std::vector< Action > &actions)
return the length of simulation, as defined by a sequence of extended actions
Definition: psi_cylinder_world.cpp:376
kjb::Vector get_direction_3d() const
Definition: psi_cylinder_world.h:78
void execute_follow_action(size_t time, Action_context &context)
Definition: psi_cylinder_world.cpp:531
void execute_walk_action(size_t time, Action_context &context)
Definition: psi_cylinder_world.cpp:443
r
Definition: APPgetLargeConnectedEdges.m:127
Definition: psi_action.h:58
double get_width() const
Definition: psi_cylinder_world.h:83
This class implements vectors, in the linear-algebra sense, with real-valued elements.
Definition: m_vector.h:87
std::vector< double > quantize_walk_through_points_action(const std::vector< double > &parameters)
size_t length(const C &cner)
Counts the total number of elements in a 2D STL-style container.
Definition: l_util.h:17
St_perspective_camera for modeling a perspective camera using the classic Forsyth and Ponce parametri...
void execute_walk_through_points_action(size_t time, Action_context &context)
Definition: psi_cylinder_world.cpp:491
void execute_walk_in_arc_action(size_t time, Action_context &context)
Definition: psi_cylinder_world.cpp:462
Bbox get_bounding_box(const std::vector< kjb::Vector > &points_3d, const kjb::Perspective_camera &cam)
Definition: psi_bbox.cpp:172
std::vector< size_t > quantize_action_durations(const std::vector< Action > &actions)
Definition: psi_cylinder_world.cpp:396
St_perspective_camera for modeling a perspective camera using the classic Forsyth and Ponce parametri...
const kjb::Vector & get_direction() const
Definition: psi_cylinder_world.h:77
const kjb::Vector & get_position() const
Definition: psi_cylinder_world.h:74
Definition: psi_action.h:58
Definition: psi_cylinder_world.h:61
void execute_action(size_t time, Action_context &context)
Definition: psi_cylinder_world.h:211
Int_matrix floor(const Matrix &m)
Definition: m_matrix.cpp:2026
Definition: perspective_camera.h:93
Definition: psi_cylinder_world.h:133
Simple_simulator()
Definition: psi_cylinder_world.h:148
Definition: psi_action.h:59
#define KJB_THROW_2(ex, msg)
Definition: l_exception.h:48
double get_height() const
Definition: psi_cylinder_world.h:82
virtual void render() const
Definition: psi_cylinder_world.cpp:123
void set_width(double w)
Definition: psi_cylinder_world.h:102
double get_heading() const
Definition: psi_cylinder_world.h:80
Definition: psi_action.h:76
Object thrown when an argument to a function is not acceptable.
Definition: l_exception.h:377
Definition: g_cylinder.h:32
void set_position(const kjb::Vector &p)
Definition: psi_cylinder_world.h:85
kjb::Cylinder get_cylinder() const
Definition: psi_cylinder_world.h:67
Definition: psi_action.h:58
void move_forward(double distance)
Definition: psi_cylinder_world.h:109
Definition for the Vector class, a thin wrapper on the KJB Vector struct and its related functionalit...
const std::vector< std::vector< std::vector< Entity_state > > > & get_entity_states() const
returns 2d array or entity states, indexed by states[actor][frame]
Definition: psi_cylinder_world.h:169
Definition: psi_action.h:58
kjb::Vector get_position_3d() const
Definition: psi_cylinder_world.h:75
void execute_null_action(size_t time, Action_context &context)
Definition: psi_cylinder_world.cpp:429