Choreonoid  1.5
ExtraBodyStateAccessor.h
Go to the documentation of this file.
1 
6 #ifndef CNOID_BODY_EXTRA_BODY_STATE_ACCESSOR_H
7 #define CNOID_BODY_EXTRA_BODY_STATE_ACCESSOR_H
8 
9 #include <cnoid/Referenced>
10 #include <cnoid/Array2D>
11 #include <cnoid/EigenTypes>
12 #include <cnoid/Signal>
13 #include <boost/variant.hpp>
14 #include <string>
15 #include "exportdecl.h"
16 
17 namespace cnoid {
18 
20 {
21  static int elementSizes[];
22  Signal<void()> sigStateChanged_;
23 
24 public:
25 
26  class Angle {
27  double angle;
28  public:
29  Angle(double rad) : angle(rad) { }
30  double value() const { return angle; }
31  //operator double() const { return angle; }
32  };
33 
34  struct None { } none;
35 
36  enum Attribute {
37  NORMAL = 0,
38  WARNING = 1 << 0,
39  STRONG_WARNING = WARNING | 1 << 1
40  };
41 
42  enum { BOOL, INT, DOUBLE, ANGLE, STRING, VECTOR3, VECTORX, NONE };
43 
44  class Value {
49  boost::variant<bool, int, double, Angle, std::string, Vector3f, VectorX, None> value;
50  int attr;
51  public:
52  Value() : attr(0) { }
53  Value& operator=(const Value& rhs) { value = rhs.value; attr = rhs.attr; return *this; }
54  template<typename T> Value& operator=(const T& rhs) { value = rhs; return *this; }
55  bool getBool() const { return boost::get<bool>(value); }
56  int getInt() const { return boost::get<int>(value); }
57  double getDouble() const { return boost::get<double>(value); }
58  double getAngle() const { return boost::get<Angle>(value).value(); }
59  void setAngle(double rad) { value = Angle(rad); }
60  const std::string& getString() const { return boost::get<std::string>(value); }
61  const Vector3f& getVector3f() const { return boost::get<Vector3f>(value); }
62  Vector3 getVector3() const { return boost::get<Vector3f>(value).cast<Vector3::Scalar>(); }
63  const VectorX& getVectorX() const { return boost::get<VectorX>(value); }
64 
65  int which() const { return value.which(); }
66  bool empty() const { return value.empty(); }
67  void setAttribute(int attribute) { attr = attribute; }
68  int attribute() const { return attr; }
69  };
70 
73  virtual ~ExtraBodyStateAccessor();
74 
75  static int getNumValueElements(const Value& v){
76  int n = elementSizes[v.which()];
77  if(n < 0){
78  n = v.getVectorX().size();
79  }
80  return n;
81  }
82 
83  virtual int getNumStateItems() const = 0;
84  virtual int getNumJointStateItems() const = 0;
85  virtual const char* getStateItemName(int stateIndex) const = 0;
87  virtual const char* getStateItemLabel(int stateIndex) const = 0;
88  virtual const char* getJointStateItemName(int jointStateIndex) const = 0;
90  virtual const char* getJointStateItemLabel(int jointStateIndex) const = 0;
91 
92  virtual void getState(std::vector<Value>& out_state) const = 0;
93 
99  virtual bool setState(const std::vector<Value>& state) const;
100 
104  virtual void getJointState(Array2D<Value>& out_jointState) const = 0;
105 
111  virtual bool setJointState(const Array2D<Value>& jointState) const;
112 
113  SignalProxy<void()> sigStateChanged() { return sigStateChanged_; }
114  void notifyStateChange() { sigStateChanged_(); }
115 };
116 
117 template<> inline ExtraBodyStateAccessor::Value&
118 ExtraBodyStateAccessor::Value::operator=(const Vector3& rhs) { value = Vector3f(rhs.cast<float>()); return *this; }
119 
121 };
122 
123 #endif
double value() const
Definition: ExtraBodyStateAccessor.h:30
Vector3 getVector3() const
Definition: ExtraBodyStateAccessor.h:62
Eigen::VectorXd VectorX
Definition: EigenTypes.h:62
Value & operator=(const T &rhs)
Definition: ExtraBodyStateAccessor.h:54
const VectorX & getVectorX() const
Definition: ExtraBodyStateAccessor.h:63
Angle(double rad)
Definition: ExtraBodyStateAccessor.h:29
Definition: Referenced.h:67
static int getNumValueElements(const Value &v)
Definition: ExtraBodyStateAccessor.h:75
double getDouble() const
Definition: ExtraBodyStateAccessor.h:57
void setAngle(double rad)
Definition: ExtraBodyStateAccessor.h:59
void setAttribute(int attribute)
Definition: ExtraBodyStateAccessor.h:67
Definition: Array2D.h:15
Definition: Referenced.h:128
int attribute() const
Definition: ExtraBodyStateAccessor.h:68
int getInt() const
Definition: ExtraBodyStateAccessor.h:56
Attribute
Definition: ExtraBodyStateAccessor.h:36
Definition: ExtraBodyStateAccessor.h:44
void notifyStateChange()
Definition: ExtraBodyStateAccessor.h:114
Defines the minimum processing for performing pasing file for STL.
Definition: AbstractSceneLoader.h:9
Value & operator=(const Value &rhs)
Definition: ExtraBodyStateAccessor.h:53
Value()
Definition: ExtraBodyStateAccessor.h:52
Definition: ExtraBodyStateAccessor.h:26
int which() const
Definition: ExtraBodyStateAccessor.h:65
Eigen::Vector3d Vector3
Definition: EigenTypes.h:58
Definition: ExtraBodyStateAccessor.h:19
SignalProxy< void()> sigStateChanged()
Definition: ExtraBodyStateAccessor.h:113
#define CNOID_EXPORT
Definition: Util/exportdecl.h:37
Definition: ExtraBodyStateAccessor.h:42
double getAngle() const
Definition: ExtraBodyStateAccessor.h:58
Definition: Signal.h:380
const Vector3f & getVector3f() const
Definition: ExtraBodyStateAccessor.h:61
bool empty() const
Definition: ExtraBodyStateAccessor.h:66
bool getBool() const
Definition: ExtraBodyStateAccessor.h:55
ref_ptr< ExtraBodyStateAccessor > ExtraBodyStateAccessorPtr
Definition: ExtraBodyStateAccessor.h:120
Definition: ExtraBodyStateAccessor.h:34
const std::string & getString() const
Definition: ExtraBodyStateAccessor.h:60