KJB
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
psi_weighted_box.h
Go to the documentation of this file.
1 /* $Id: psi_weighted_box.h 10643 2011-09-29 19:51:33Z predoehl $ */
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 #ifndef KJB_PSI_WEIGHTED_BOX_H
22 #define KJB_PSI_WEIGHTED_BOX_H
23 
24 #include <m_cpp/m_vector.h>
25 #include <g_cpp/g_quaternion.h>
26 #include <psi_cpp/psi_util.h>
27 #include <psi_cpp/psi_units.h>
28 
29 #include <boost/io/ios_state.hpp>
30 
31 #ifdef KJB_HAVE_UA_CARTWHEEL
32 #include <Control/BoxState.h>
33 #endif
34 
35 namespace kjb
36 {
37 namespace psi
38 {
39 
43 class Cuboid
44 {
45 public:
46  Cuboid() :
47  center_(3, 0.0),
48  size_(3, 0.0),
49  orientation_()
50  {}
51 
53  const Vector& center,
54  const Vector& size,
55  const Quaternion& orientation) :
56  center_(center),
57  size_(size),
58  orientation_(orientation)
59  {}
60 
61  const Vector& get_center() const { return center_; }
62  const Vector& get_size() const { return size_; }
63  const Quaternion& get_orientation() const { return orientation_; }
64 
65  void set_center(const Vector& center) { center_ = center; }
66  void set_size(const Vector& size) { size_ = size; }
67  void set_orientation(const Quaternion& orientation) { orientation_ = orientation; }
68 protected:
72 };
73 
74 std::vector<Vector> get_corners(const Cuboid& c);
75 void render(const Cuboid& c);
76 
77 #ifdef KJB_HAVE_UA_CARTWHEEL
78 inline Cuboid to_cuboid(const CartWheel::BoxState& cw_box)
79 {
80  return Cuboid(
81  to_kjb(cw_box.getPosition()),
82  to_kjb(cw_box.getSize()),
83  to_kjb(cw_box.getQuaternion()));
84 }
85 
86 #endif
87 
91 struct Weighted_box : public Cuboid
92 {
93 typedef Cuboid Base;
94 public:
96  Base(Vector(0.0,0.0,0.0), Vector(0.0,0.0,0.0), Quaternion()),
97  mass_(0.0)
98  {}
99 
100  Weighted_box(const kjb::Vector& position, double rotation, const kjb::Vector& size, double mass) :
101  Base(position, size, Quaternion(Vector(0.0, 1.0, 0.0), rotation)),
102  mass_(mass)
103  {}
104 
105  double get_mass() const { return mass_; }
106  void set_mass(double mass) { mass_ = mass; }
107 
108  // return number of parameters
109  static size_t size() { return 7; }
110  static Unit_type get_units(size_t i)
111  {
112  switch(i)
113  {
114  case 0:
115  case 1:
116  return SPACIAL_UNIT;
117  case 2:
118  return ANGLE_UNIT;
119  case 3:
120  case 4:
121  case 5:
122  return LENGTH_UNIT;
123  case 6:
124  return MASS_UNIT;
125  default:
127  }
128  }
129 
132  double get(size_t i) const
133  {
134  switch(i)
135  {
136  case 0:
137  return center_[0];
138  case 1:
139  return center_[2];
140  case 2:
141  return orientation_.get_angle();
142  case 3:
143  return log(size_[0]);
144  case 4:
145  return log(size_[1]);
146  case 5:
147  return log(size_[2]);
148  case 6:
149  return log(mass_);
150  default:
152 
153  }
154  }
155 
158  void set(size_t i, double value)
159  {
160  switch(i)
161  {
162  case 0:
163  center_[0] = value;
164  return;
165  case 1:
166  center_[2] = value;
167  return;
168  case 2:
169  orientation_.set_axis_angle(Vector(0.0, 1.0, 0.0), value);
170  return;
171  case 3:
172  size_[0] = exp(value);
173  return;
174  case 4:
175  size_[1] = exp(value);
176  center_[1] = size_[1]/2.0 + 0.01;
177  return;
178  case 5:
179  size_[2] = exp(value);
180  return;
181  case 6:
182  mass_ = exp(value);
183  return;
184  default:
186 
187  }
188  }
189 
190  friend std::ostream& operator<<(std::ostream& ost, const Weighted_box& box);
191  friend std::istream& operator>>(std::istream& ist, Weighted_box& box);
192 private:
193  double mass_;
194 };
195 
196 inline std::ostream& operator<<(std::ostream& ost, const Weighted_box& box)
197 {
198  assert(box.center_.size() == 3);
199  ost << box.center_[0] << " ";
200  ost << box.center_[1] << " ";
201  ost << box.center_[2] << " ";
202 
203  ost << box.orientation_.get_angle() << " ";
204 
205  assert(box.size_.size() == 3);
206  ost << box.size_[0] << " ";
207  ost << box.size_[1] << " ";
208  ost << box.size_[2] << " ";
209 
210  ost << box.mass_;
211 
212  return ost;
213 }
214 
215 inline std::istream& operator>>(std::istream& ist, Weighted_box& box)
216 {
217  // this will restore the "skipws" state after leaving scope
218  boost::io::ios_flags_saver ifs( ist );
219  ist >> std::skipws;
220 
221  box.center_.resize(3);
222  ist >> box.center_[0];
223  ist >> box.center_[1];
224  ist >> box.center_[2];
225 
226  double angle;
227 
228  ist >> angle;
229 
230  box.orientation_.set_axis_angle(Vector(0.0, 1.0, 0.0), angle);
231 
232  box.size_.resize(3);
233  ist >> box.size_[0];
234  ist >> box.size_[1];
235  ist >> box.size_[2];
236 
237  ist >> box.mass_;
238 
239  return ist;
240 }
241 
242 inline Weighted_box parse_cli_weighted_box(const std::string& line)
243 {
244  Weighted_box box;
245 
246  std::istringstream ist(line);
247  ist >> box;
248 
249  return box;
250 }
251 
252 } // namespace psi
253 } // namespace kjb
254 #endif
Vector & resize(int new_length, Value_type pad=Value_type(0))
Resize vector, retaining previous values.
Definition: m_vector.cpp:242
friend std::ostream & operator<<(std::ostream &ost, const Weighted_box &box)
Definition: psi_weighted_box.h:196
std::ostream & operator<<(std::ostream &ost, const Action &action)
serialize an action
Definition: psi_action.cpp:333
size_type size() const
Alias to get_length(). Required to comply with stl Container concept.
Definition: m_vector.h:510
Definition: psi_units.h:38
Object thrown when an index argument exceeds the size of a container.
Definition: l_exception.h:399
std::istream & operator>>(std::istream &ist, Action &action)
unserialize an action
Definition: psi_action.cpp:346
Unit_type
Definition: psi_units.h:30
Definition: psi_weighted_box.h:43
double get_mass() const
Definition: psi_weighted_box.h:105
#define KJB_THROW(ex)
Definition: l_exception.h:46
This class implements vectors, in the linear-algebra sense, with real-valued elements.
Definition: m_vector.h:87
void set_mass(double mass)
Definition: psi_weighted_box.h:106
Definition: psi_weighted_box.h:91
Quaternion orientation_
Definition: psi_weighted_box.h:71
void set_center(const Vector &center)
Definition: psi_weighted_box.h:65
void set(size_t i, double value)
Definition: psi_weighted_box.h:158
static size_t size()
Definition: psi_weighted_box.h:109
std::vector< Vector > get_corners(const Cuboid &c)
Definition: psi_weighted_box.cpp:31
Weighted_box()
Definition: psi_weighted_box.h:95
Definition: g_quaternion.h:40
const Vector & get_center() const
Definition: psi_weighted_box.h:61
Cuboid(const Vector &center, const Vector &size, const Quaternion &orientation)
Definition: psi_weighted_box.h:52
void render(const Cuboid &c)
Definition: psi_weighted_box.cpp:56
Quaternion & set_axis_angle(const Vector &, double angle)
Definition: g_quaternion.cpp:370
static Unit_type get_units(size_t i)
Definition: psi_weighted_box.h:110
void set_orientation(const Quaternion &orientation)
Definition: psi_weighted_box.h:67
Cuboid Base
Definition: psi_weighted_box.h:93
Cuboid()
Definition: psi_weighted_box.h:46
friend std::istream & operator>>(std::istream &ist, Weighted_box &box)
Definition: psi_weighted_box.h:215
void set_size(const Vector &size)
Definition: psi_weighted_box.h:66
const Vector & get_size() const
Definition: psi_weighted_box.h:62
Definition: psi_units.h:39
double get_angle() const
Definition: g_quaternion.cpp:294
const Quaternion & get_orientation() const
Definition: psi_weighted_box.h:63
get the indices of edges in each direction for i
Definition: APPgetLargeConnectedEdges.m:48
Weighted_box(const kjb::Vector &position, double rotation, const kjb::Vector &size, double mass)
Definition: psi_weighted_box.h:100
Definition: psi_units.h:31
Vector center_
Definition: psi_weighted_box.h:69
Weighted_box parse_cli_weighted_box(const std::string &line)
Definition: psi_weighted_box.h:242
Vector size_
Definition: psi_weighted_box.h:70
Definition: psi_units.h:34
Definition for the Vector class, a thin wrapper on the KJB Vector struct and its related functionalit...