OpenCOVER
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
inputdevice.h
Go to the documentation of this file.
1 /* This file is part of COVISE.
2 
3  You can use it under the terms of the GNU Lesser General Public License
4  version 2.1 or later, see lgpl-2.1.txt.
5 
6  * License: LGPL 2+ */
7 
8 /*
9  * inputdevice.h
10  *
11  * Created on: Dec 9, 2014
12  * Author: svnvlad
13  */
14 
15 #ifndef INPUT_DEVICE_H
16 #define INPUT_DEVICE_H
17 
18 #include <OpenThreads/Thread>
19 #include <OpenThreads/Mutex>
20 #include <osg/Matrix>
21 #include <osg/Vec3>
22 #include <vector>
23 #include <cover/coVRDynLib.h>
24 
25 namespace opencover
26 {
27 
37 class COVEREXPORT InputDevice : public OpenThreads::Thread
38 {
39  friend class Input;
40  friend class ButtonDevice;
41  friend class Valuator;
42  friend class TrackingBody;
43 
44 public:
45  InputDevice(const std::string &configPath);
46  virtual ~InputDevice();
47 
48  std::string configPath(const std::string &ent = "") const; //< path to config values for this device
49  virtual bool poll(); //< called regularly to retrieve new values from hardware - reimplement
50  virtual void run(); //< regularly calls poll() on another thread - reimplement if this is not appropriate
51 
52  virtual bool needsThread() const; //< whether a thread should be spawned - reimplement if not necessary
53  void stopLoop(); //< request run()/other thread to terminate
54 
55  bool isValid() const;
56  bool isVarying() const;
57  bool is6Dof() const;
58  const std::string &getName() const;
59  const osg::Matrix &getOffsetMat() const;
60  void setOffsetMat(const osg::Matrix &m);
61 
62  std::string &getCalibrationPointName(int i);
63  osg::Vec3 &getCalibrationPoint(int i);
64 
65 protected:
66  static osg::Matrix s_identity; //< identity matrix, for returning a valid reference
68  OpenThreads::Mutex m_mutex; //< protect state data structures
69 
70  const std::string m_config; //< path to config values for this device
71  std::string m_name;
72  osg::Matrix m_offsetMatrix; //< system matrix
73  bool m_isVarying; //< whether returned values can change
74  bool m_is6Dof; //< whether matrices represent position and orientation
75 
76  // state data, update during poll(), create them with the correct size
77  bool m_valid;
78  std::vector<bool> m_buttonStates;
79  std::vector<double> m_valuatorValues;
80  std::vector<std::pair<double, double> > m_valuatorRanges;
81  std::vector<bool> m_bodyMatricesValid;
82  std::vector<bool> m_bodyMatricesRelative;
83  std::vector<osg::Matrix> m_bodyMatrices;
84  osg::Vec3 m_calibrationPoints[3];
85  std::string m_calibrationPointNames[3];
86 
87  // these are called by Input
88  size_t numButtons() const
89  {
90  return m_buttonStates.size();
91  }
92  bool getButtonState(size_t idx) const;
93 
94  size_t numValuators() const
95  {
96  return m_valuatorValues.size();
97  }
98  double getValuatorValue(size_t idx) const;
99  std::pair<double, double> getValuatorRange(size_t idx) const;
100 
101  size_t numBodies() const
102  {
103  return m_bodyMatrices.size();
104  }
105  bool isBodyMatrixValid(size_t idx) const;
106  bool isBodyMatrixRelative(size_t idx) const;
107  const osg::Matrix &getBodyMatrix(size_t idx) const;
108 
109  virtual void update(); //< called by Input::update()
110 
111 private:
112  // per-frame state
113  bool m_validFrame;
114  std::vector<bool> m_buttonStatesFrame;
115  std::vector<double> m_valuatorValuesFrame;
116  std::vector<std::pair<double, double> > m_valuatorRangesFrame;
117  std::vector<bool> m_bodyMatricesValidFrame;
118  std::vector<bool> m_bodyMatricesRelativeFrame;
119  std::vector<osg::Matrix> m_bodyMatricesFrame;
120 };
121 
122 class COVEREXPORT DriverFactoryBase
123 {
124 
125  friend class Input;
126 
127 public:
128  DriverFactoryBase(const std::string &name);
129  virtual ~DriverFactoryBase();
130 
131  virtual InputDevice *newInstance(const std::string &name) = 0;
132  const std::string &name() const;
133  CO_SHLIB_HANDLE getLibHandle() const;
134 
135 private:
136  void setLibHandle(CO_SHLIB_HANDLE handle);
137 
138  std::string m_name;
139  CO_SHLIB_HANDLE m_handle;
140 };
141 
142 template <class Driver>
144 {
145 
146 public:
147  DriverFactory(const std::string &name)
148  : DriverFactoryBase(name)
149  {
150  }
151 
152  Driver *newInstance(const std::string &instanceName)
153  {
154 
155  return new Driver(instanceName);
156  }
157 };
158 }
159 
160 #define INPUT_PLUGIN(Driver) \
161  extern "C" PLUGINEXPORT opencover::DriverFactory<Driver> *newDriverFactory() \
162  { \
163  return new opencover::DriverFactory<Driver>(#Driver); \
164  }
165 
166 #endif /* INPUTHDW_H_ */
DriverFactory(const std::string &name)
Definition: inputdevice.h:147
std::string m_name
Definition: inputdevice.h:71
osg::Matrix m_offsetMatrix
Definition: inputdevice.h:72
Definition: buttondevice.h:27
bool loop_is_running
Definition: inputdevice.h:67
size_t numBodies() const
Definition: inputdevice.h:101
size_t numValuators() const
Definition: inputdevice.h:94
Definition: inputdevice.h:122
std::vector< bool > m_bodyMatricesRelative
Definition: inputdevice.h:82
Driver * newInstance(const std::string &instanceName)
Definition: inputdevice.h:152
std::vector< bool > m_bodyMatricesValid
Definition: inputdevice.h:81
dynamic library loading
The InputDevice class interacts with input hardware.
Definition: inputdevice.h:37
std::vector< bool > m_buttonStates
Definition: inputdevice.h:78
std::vector< double > m_valuatorValues
Definition: inputdevice.h:79
Definition: trackingbody.h:28
bool m_isVarying
Definition: inputdevice.h:73
const std::string m_config
Definition: inputdevice.h:70
size_t numButtons() const
Definition: inputdevice.h:88
Definition: inputdevice.h:143
void * CO_SHLIB_HANDLE
Definition: coVRDynLib.h:36
std::vector< osg::Matrix > m_bodyMatrices
Definition: inputdevice.h:83
The Input class.
Definition: input.h:38
Definition: valuator.h:27
bool m_is6Dof
Definition: inputdevice.h:74
bool m_valid
Definition: inputdevice.h:77
static osg::Matrix s_identity
Definition: inputdevice.h:66
const std::string & name() const
OpenThreads::Mutex m_mutex
If true, the main loop will run.
Definition: inputdevice.h:68
std::vector< std::pair< double, double > > m_valuatorRanges
Definition: inputdevice.h:80