KJB
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
pt_scene_adapter.h
Go to the documentation of this file.
1 /* =========================================================================== *
2 |
3 | Copyright (c) 1994-2008 by Kobus Barnard (author).
4 |
5 | Personal and educational use of this code is granted, provided that this
6 | header is kept intact, and that the authorship is not misrepresented, that
7 | its use is acknowledged in publications, and relevant papers are cited.
8 |
9 | For other use contact the author (kobus AT cs DOT arizona DOT edu).
10 |
11 | Please note that the code in this file has not necessarily been adequately
12 | tested. Naturally, there is no guarantee of performance, support, or fitness
13 | for any particular task. Nonetheless, I am interested in hearing about
14 | problems that you encounter.
15 |
16 | Authors:
17 | Ernesto Brau
18 |
19 * =========================================================================== */
20 
21 /* $Id$ */
22 
23 #ifndef PT_SCENE_ADAPTER_H
24 #define PT_SCENE_ADAPTER_H
25 
29 #include <map>
30 
31 namespace kjb {
32 namespace pt {
33 
40 inline
42 (
43  const Scene& scene,
44  const Target& target,
45  size_t frame,
46  size_t d,
47  size_t infer_head
48 )
49 {
50  if(!infer_head) { assert(d <= 1); }
51  switch(d)
52  {
53  case 0: return target.trajectory()[frame - 1]->value.position[0];
54  case 1: return target.trajectory()[frame - 1]->value.position[2];
55  case 2: return target.trajectory()[frame - 1]->value.body_dir;
56  case 3: return target.trajectory()[frame - 1]->value.face_dir[0];
57  case 4: return target.trajectory()[frame - 1]->value.face_dir[1];
58 
59  default: KJB_THROW_2(
61  "Cannot get variable: bad index");
62  }
63 }
64 
71 inline
73 (
74  const Scene& scene,
75  const Target& target,
76  size_t frame,
77  size_t d,
78  double x,
79  bool vo,
80  bool infer_head
81 )
82 {
83  // current values
84  double px = target.trajectory()[frame - 1]->value.position[0];
85  double pz = target.trajectory()[frame - 1]->value.position[2];
86  double fd1 = 0.0;
87  double fd2 = 0.0;
88  if(infer_head)
89  {
90  double fd1 = target.trajectory()[frame - 1]->value.face_dir[0];
91  double fd2 = target.trajectory()[frame - 1]->value.face_dir[1];
92  }
93  else
94  {
95  assert(d <= 1);
96  }
97 
98  // set new values, while keeping old ones where applicable
99  switch(d)
100  {
101  case 0: set_trajectory_at_frame(
102  scene, target, frame, Vector3(x, 0.0, pz), vo, infer_head);
103  break;
104 
105  case 1: set_trajectory_at_frame(
106  scene, target, frame, Vector3(px, 0.0, x), vo, infer_head);
107  break;
108 
109  case 2: set_trajectory_dir_at_frame(scene, target, frame, x, vo);
110  break;
111 
113  scene, target, frame, Vector2(x, fd2), vo);
114  break;
115 
117  scene, target, frame, Vector2(fd1, x), vo);
118  break;
119 
120  default: KJB_THROW_2(
122  "Cannot set variable: bad index");
123  }
124 }
125 
132 inline
134 (
135  const Scene& scene,
136  const Target& target,
137  size_t frame,
138  size_t d,
139  double dx,
140  bool vo,
141  bool infer_head
142 )
143 {
144  if(!infer_head)
145  {
146  assert(d <= 1);
147  }
148  switch(d)
149  {
150  case 0: move_trajectory_at_frame(
151  scene, target, frame, Vector3(dx, 0.0, 0.0), vo, infer_head);
152  break;
153 
154  case 1: move_trajectory_at_frame(
155  scene, target, frame, Vector3(0.0, 0.0, dx), vo, infer_head);
156  break;
157 
158  case 2: move_trajectory_dir_at_frame(scene, target, frame, dx, vo);
159  break;
160 
162  scene, target, frame, Vector2(dx, 0.0), vo);
163  break;
164 
166  scene, target, frame, Vector2(0.0, dx), vo);
167  break;
168 
169  default: KJB_THROW_2(
171  "Cannot move variable: bad index");
172  }
173 }
174 
184 {
185 public:
186  Scene_adapter(bool vis_off = false, bool infer_head = true)
187  : scene_p_(0),
188  m_vis_off(vis_off),
189  m_infer_head(infer_head)
190  {}
191 
193  double get(const Scene* s, size_t i) const
194  {
195  compute_vars(s);
196  IFT(size(s) != 0, Runtime_error, "Cannot get: no changed targets");
197  return get_variable_at_frame(
198  *s, *tgt_ps_[i], frames_[i], dims_[i], m_infer_head);
199  }
200 
202  void set(Scene* s, size_t i, double x) const
203  {
204  compute_vars(s);
205  IFT(size(s) != 0, Runtime_error, "Cannot set: no changed targets");
207  *s, *tgt_ps_[i], frames_[i], dims_[i], x, m_vis_off, m_infer_head);
208  }
209 
211  void set(Scene* s, size_t i, size_t j, double x, double y) const
212  {
213  if(i == j)
214  {
215  std::cerr << "Scene_adapter::set received same index" << std::endl;
216  }
217 
218  if(j < i)
219  {
220  using std::swap;
221  swap(i, j);
222  swap(x, y);
223  }
224 
225  set(s, i, x);
226  set(s, j, y);
227  }
228 
230  void set(Scene* s, const Vector& x) const;
231 
233  size_t size(const Scene* s) const
234  {
235  compute_vars(s);
236  return tgt_ps_.size();
237  }
238 
240  void reset() const { scene_p_ = 0; }
241 
243  std::pair<const Target*, size_t>
244  target_frame(const Scene* s, size_t i) const
245  {
246  if(i >= size(s)) return std::make_pair((const Target*)0, 0);
247 
248  return std::make_pair(tgt_ps_[i], frames_[i]);
249  }
250 
252  bool infer_head() const { return m_infer_head; }
253 
254 private:
255  /* @brief Converts scene into vector. */
256  void compute_vars(const Scene* s) const;
257 
258  // members
259  mutable const Scene* scene_p_;
260  mutable std::vector<const Target*> tgt_ps_;
261  mutable std::vector<size_t> frames_;
262  mutable std::vector<size_t> dims_;
263 
264  bool m_vis_off;
265  bool m_infer_head;
266 };
267 
268 }} // namespace kjb::pt
269 
270 #endif /*PT_SCENE_ADAPTER_H */
271 
void set(Scene *s, size_t i, size_t j, double x, double y) const
Set the ith element of a scene to x.
Definition: pt_scene_adapter.h:211
void set_trajectory_face_dir_at_frame(const Scene &scene, const Target &target, size_t frame, const Vector2 &dir, bool vis_off)
Function that changes a single face direction at a single frame, and performs all the necessary updat...
Definition: pt_scene.cpp:373
std::pair< const Target *, size_t > target_frame(const Scene *s, size_t i) const
Get target and frame corresponding to variable i.
Definition: pt_scene_adapter.h:244
Adapts a Scene into a VectorModel for HMC sampling.
Definition: pt_scene_adapter.h:183
Vector_d< 3 > Vector3
Definition: g_quaternion.h:37
void set_variable_at_frame(const Scene &scene, const Target &target, size_t frame, size_t d, double x, bool vo, bool infer_head)
Sets the scene at the given variable. Here, d = 1 ... 5 represents the different parts of the traject...
Definition: pt_scene_adapter.h:73
Trajectory & trajectory() const
Get this target's current 3D positions.
Definition: pt_target.h:85
Scene_adapter(bool vis_off=false, bool infer_head=true)
Definition: pt_scene_adapter.h:186
Class that represents a full scene in the PT universe.
Definition: pt_scene.h:40
void swap(Scene &s1, Scene &s2)
Swap two scenes.
Definition: pt_scene.h:69
This class implements vectors, in the linear-algebra sense, with real-valued elements.
Definition: m_vector.h:87
void move_variable_at_frame(const Scene &scene, const Target &target, size_t frame, size_t d, double dx, bool vo, bool infer_head)
Moves the scene at the given variable. Here, d = 1 ... 5 represents the different parts of the trajec...
Definition: pt_scene_adapter.h:134
#define IFT(a, ex, msg)
Definition: l_exception.h:101
void move_trajectory_at_frame(const Scene &scene, const Target &target, size_t frame, const Vector3 &dv, bool vis_off, bool infer_head=true)
Function that moves a single trajectory at a single frame, and performs all the necessary updates...
Definition: pt_scene.h:307
double get_variable_at_frame(const Scene &scene, const Target &target, size_t frame, size_t d, size_t infer_head)
Gets the scene at the given variable. Here, d = 1 ... 5 represents the different parts of the traject...
Definition: pt_scene_adapter.h:42
void reset() const
Restart state.
Definition: pt_scene_adapter.h:240
x
Definition: APPgetLargeConnectedEdges.m:100
void set_trajectory_at_frame(const Scene &scene, const Target &target, size_t frame, const Vector3 &v, bool vis_off, bool infer_head=true)
Function that changes a single trajectory at a single frame, and performs all the necessary updates...
Definition: pt_scene.cpp:210
Class that represents a target moving through space.
Definition: pt_target.h:50
#define KJB_THROW_2(ex, msg)
Definition: l_exception.h:48
void move_trajectory_face_dir_at_frame(const Scene &scene, const Target &target, size_t frame, const Vector2 &dd, bool vis_off)
Function that moves a single face direction at a single frame, and performs all the necessary updates...
Definition: pt_scene.h:458
void swap(kjb::Gsl_Multimin_fdf &m1, kjb::Gsl_Multimin_fdf &m2)
Swap two wrapped multimin objects.
Definition: gsl_multimin.h:693
bool infer_head() const
Returns whether inferring head or not.
Definition: pt_scene_adapter.h:252
void set(Scene *s, size_t i, double x) const
Set the ith element of a scene to x.
Definition: pt_scene_adapter.h:202
void set_trajectory_dir_at_frame(const Scene &scene, const Target &target, size_t frame, double dir, bool vis_off)
Function that changes a single direction at a single frame, and performs all the necessary updates...
Definition: pt_scene.cpp:295
Object thrown when an argument to a function is not acceptable.
Definition: l_exception.h:377
size_t size(const Scene *s) const
Get the number of elements in this scene.
Definition: pt_scene_adapter.h:233
get the indices of edges in each direction for i
Definition: APPgetLargeConnectedEdges.m:48
Vector_d< 2 > Vector2
Definition: gr_opengl.h:35
Object thrown when computation fails somehow during execution.
Definition: l_exception.h:321
void move_trajectory_dir_at_frame(const Scene &scene, const Target &target, size_t frame, double dd, bool vis_off)
Function that moves a single direction at a single frame, and performs all the necessary updates...
Definition: pt_scene.h:384