OpenCOVER
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
25namespace opencover
26{
27
37class COVEREXPORT InputDevice : public OpenThreads::Thread
38{
39 friend class Input;
40 friend class ButtonDevice;
41 friend class Valuator;
42 friend class TrackingBody;
43
44public:
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
65protected:
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
111private:
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
122class COVEREXPORT DriverFactoryBase
123{
124
125 friend class Input;
126
127public:
128 DriverFactoryBase(const std::string &name);
130
131 virtual InputDevice *newInstance(const std::string &name) = 0;
132 const std::string &name() const;
134
135private:
136 void setLibHandle(CO_SHLIB_HANDLE handle);
137
138 std::string m_name;
139 CO_SHLIB_HANDLE m_handle;
140};
141
142template <class Driver>
144{
145
146public:
147 DriverFactory(const std::string &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_ */
dynamic library loading
void * CO_SHLIB_HANDLE
Definition: coVRDynLib.h:36
Definition: ARToolKit.h:33
Definition: buttondevice.h:28
The Input class.
Definition: input.h:40
The InputDevice class interacts with input hardware.
Definition: inputdevice.h:38
std::vector< osg::Matrix > m_bodyMatrices
Definition: inputdevice.h:83
bool isBodyMatrixValid(size_t idx) const
std::vector< bool > m_bodyMatricesRelative
Definition: inputdevice.h:82
const osg::Matrix & getBodyMatrix(size_t idx) const
virtual void update()
bool getButtonState(size_t idx) const
void setOffsetMat(const osg::Matrix &m)
std::string m_name
Definition: inputdevice.h:71
std::vector< std::pair< double, double > > m_valuatorRanges
Definition: inputdevice.h:80
InputDevice(const std::string &configPath)
std::pair< double, double > getValuatorRange(size_t idx) const
virtual bool poll()
virtual void run()
size_t numBodies() const
Definition: inputdevice.h:101
bool m_valid
Definition: inputdevice.h:77
bool m_is6Dof
Definition: inputdevice.h:74
std::vector< double > m_valuatorValues
Definition: inputdevice.h:79
const std::string m_config
Definition: inputdevice.h:70
static osg::Matrix s_identity
Definition: inputdevice.h:66
std::vector< bool > m_bodyMatricesValid
Definition: inputdevice.h:81
size_t numValuators() const
Definition: inputdevice.h:94
std::string & getCalibrationPointName(int i)
bool m_isVarying
Definition: inputdevice.h:73
double getValuatorValue(size_t idx) const
osg::Matrix m_offsetMatrix
Definition: inputdevice.h:72
const std::string & getName() const
bool isBodyMatrixRelative(size_t idx) const
std::vector< bool > m_buttonStates
Definition: inputdevice.h:78
bool loop_is_running
Definition: inputdevice.h:67
virtual bool needsThread() const
bool isVarying() const
std::string configPath(const std::string &ent="") const
osg::Vec3 & getCalibrationPoint(int i)
size_t numButtons() const
Definition: inputdevice.h:88
OpenThreads::Mutex m_mutex
If true, the main loop will run.
Definition: inputdevice.h:68
const osg::Matrix & getOffsetMat() const
Definition: inputdevice.h:123
DriverFactoryBase(const std::string &name)
const std::string & name() const
virtual InputDevice * newInstance(const std::string &name)=0
CO_SHLIB_HANDLE getLibHandle() const
Definition: inputdevice.h:144
Driver * newInstance(const std::string &instanceName)
Definition: inputdevice.h:152
DriverFactory(const std::string &name)
Definition: inputdevice.h:147
Definition: trackingbody.h:29
Definition: valuator.h:28