Choreonoid  1.8
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 <cnoid/stdx/variant>
14 #include <string>
15 #include "exportdecl.h"
16 
17 namespace cnoid {
18 
19 class CNOID_EXPORT ExtraBodyStateAccessor : public Referenced
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  stdx::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 stdx::get<bool>(value); }
56  int getInt() const { return stdx::get<int>(value); }
57  double getDouble() const { return stdx::get<double>(value); }
58  double getAngle() const { return stdx::get<Angle>(value).value(); }
59  void setAngle(double rad) { value = Angle(rad); }
60  const std::string& getString() const { return stdx::get<std::string>(value); }
61  const Vector3f& getVector3f() const { return stdx::get<Vector3f>(value); }
62  Vector3 getVector3() const { return stdx::get<Vector3f>(value).cast<Vector3::Scalar>(); }
63  const VectorX& getVectorX() const { return stdx::get<VectorX>(value); }
64 
65  int which() const { return stdx::get_variant_index(value); }
66  void setAttribute(int attribute) { attr = attribute; }
67  int attribute() const { return attr; }
68  };
69 
72  virtual ~ExtraBodyStateAccessor();
73 
74  static int getNumValueElements(const Value& v){
75  int n = elementSizes[v.which()];
76  if(n < 0){
77  n = v.getVectorX().size();
78  }
79  return n;
80  }
81 
82  virtual int getNumStateItems() const = 0;
83  virtual int getNumJointStateItems() const = 0;
84  virtual const char* getStateItemName(int stateIndex) const = 0;
86  virtual const char* getStateItemLabel(int stateIndex) const = 0;
87  virtual const char* getJointStateItemName(int jointStateIndex) const = 0;
89  virtual const char* getJointStateItemLabel(int jointStateIndex) const = 0;
90 
91  virtual void getState(std::vector<Value>& out_state) const = 0;
92 
98  virtual bool setState(const std::vector<Value>& state) const;
99 
103  virtual void getJointState(Array2D<Value>& out_jointState) const = 0;
104 
110  virtual bool setJointState(const Array2D<Value>& jointState) const;
111 
112  SignalProxy<void()> sigStateChanged() { return sigStateChanged_; }
113  void notifyStateChange() { sigStateChanged_(); }
114 };
115 
116 template<> inline ExtraBodyStateAccessor::Value&
117 ExtraBodyStateAccessor::Value::operator=(const Vector3& rhs) { value = Vector3f(rhs.cast<float>()); return *this; }
118 
120 };
121 
122 #endif
cnoid::ExtraBodyStateAccessor::Value::getString
const std::string & getString() const
Definition: ExtraBodyStateAccessor.h:60
cnoid::ExtraBodyStateAccessor::Angle::Angle
Angle(double rad)
Definition: ExtraBodyStateAccessor.h:29
cnoid::Vector3
Eigen::Vector3d Vector3
Definition: EigenTypes.h:57
cnoid::ExtraBodyStateAccessor::Value::getVector3
Vector3 getVector3() const
Definition: ExtraBodyStateAccessor.h:62
cnoid::ExtraBodyStateAccessor::Value::getVectorX
const VectorX & getVectorX() const
Definition: ExtraBodyStateAccessor.h:63
cnoid::ExtraBodyStateAccessor::Value::getBool
bool getBool() const
Definition: ExtraBodyStateAccessor.h:55
cnoid::ExtraBodyStateAccessor::Angle::value
double value() const
Definition: ExtraBodyStateAccessor.h:30
cnoid::ExtraBodyStateAccessor
Definition: ExtraBodyStateAccessor.h:19
cnoid::ExtraBodyStateAccessor::Value::setAngle
void setAngle(double rad)
Definition: ExtraBodyStateAccessor.h:59
cnoid::ExtraBodyStateAccessor::Value::getDouble
double getDouble() const
Definition: ExtraBodyStateAccessor.h:57
cnoid::ExtraBodyStateAccessor::Value::setAttribute
void setAttribute(int attribute)
Definition: ExtraBodyStateAccessor.h:66
cnoid::ExtraBodyStateAccessor::Value::getInt
int getInt() const
Definition: ExtraBodyStateAccessor.h:56
cnoid::Array2D
Definition: Array2D.h:15
cnoid::ref_ptr
Definition: Referenced.h:103
cnoid::ExtraBodyStateAccessor::notifyStateChange
void notifyStateChange()
Definition: ExtraBodyStateAccessor.h:113
cnoid::ExtraBodyStateAccessor::Value::attribute
int attribute() const
Definition: ExtraBodyStateAccessor.h:67
cnoid::ExtraBodyStateAccessor::VECTORX
@ VECTORX
Definition: ExtraBodyStateAccessor.h:42
cnoid::ExtraBodyStateAccessor::Value
Definition: ExtraBodyStateAccessor.h:44
cnoid
Definition: AbstractSceneLoader.h:11
cnoid::ExtraBodyStateAccessor::Attribute
Attribute
Definition: ExtraBodyStateAccessor.h:36
cnoid::ExtraBodyStateAccessor::None
Definition: ExtraBodyStateAccessor.h:34
cnoid::ExtraBodyStateAccessor::Value::Value
Value()
Definition: ExtraBodyStateAccessor.h:52
cnoid::ExtraBodyStateAccessor::Angle
Definition: ExtraBodyStateAccessor.h:26
cnoid::ExtraBodyStateAccessor::Value::operator=
Value & operator=(const Value &rhs)
Definition: ExtraBodyStateAccessor.h:53
cnoid::Referenced
Definition: Referenced.h:54
cnoid::ExtraBodyStateAccessor::Value::getVector3f
const Vector3f & getVector3f() const
Definition: ExtraBodyStateAccessor.h:61
cnoid::ExtraBodyStateAccessor::Value::which
int which() const
Definition: ExtraBodyStateAccessor.h:65
cnoid::ExtraBodyStateAccessor::getNumValueElements
static int getNumValueElements(const Value &v)
Definition: ExtraBodyStateAccessor.h:74
cnoid::ExtraBodyStateAccessor::Value::getAngle
double getAngle() const
Definition: ExtraBodyStateAccessor.h:58
cnoid::Signal
Definition: Signal.h:165
cnoid::ExtraBodyStateAccessorPtr
ref_ptr< ExtraBodyStateAccessor > ExtraBodyStateAccessorPtr
Definition: ExtraBodyStateAccessor.h:119
cnoid::SignalProxy
Definition: Signal.h:470
cnoid::ExtraBodyStateAccessor::sigStateChanged
SignalProxy< void()> sigStateChanged()
Definition: ExtraBodyStateAccessor.h:112
cnoid::ExtraBodyStateAccessor::Value::operator=
Value & operator=(const T &rhs)
Definition: ExtraBodyStateAccessor.h:54
cnoid::VectorX
Eigen::VectorXd VectorX
Definition: EigenTypes.h:60