Debian Codegraph @

p5glove/libp5glove/p5glove.h

raw symbols SCIP
1 /*
2 * $Id$
3 *
4 * Copyright (c) 2003 Jason McMullan <[email protected]>
5 *
6 * USB P5 Data Glove support
7 */
8
9 /*
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU Lesser General Public License as published by
12 * the Free Software Foundation; either version 2.1 of the License, or
13 * (at your option) any later version.
14 *
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
19 *
20 * You should have received a copy of the GNU Lesser General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23 */
24
25 #ifndef P5GLOVE_H
26 #define P5GLOVE_H
27
28 #include <stdint.h>
29
30 #ifdef __cplusplus
31 extern "C" {
32 #endif
33
34
35 typedef struct p5glove *P5Glove;
36
37 /* Button bitmasks */
38 #define P5GLOVE_BUTTON_A 1
39 #define P5GLOVE_BUTTON_B 2
40 #define P5GLOVE_BUTTON_C 4
41
42 /* IR Sensor Index */
43 #define P5GLOVE_IR_WRIST_TOP 0
44 #define P5GLOVE_IR_PINKY_R 1
45 #define P5GLOVE_IR_PINKY_L 2
46 #define P5GLOVE_IR_PALM 3
47 #define P5GLOVE_IR_INDEX 4
48 #define P5GLOVE_IR_THUMB_TOP 5
49 #define P5GLOVE_IR_WRIST_BOT 6
50 #define P5GLOVE_IR_THUMB_BOT 7
51
52 /* Finger Sensor Index */
53 #define P5GLOVE_FINGER_INDEX 0
54 #define P5GLOVE_FINGER_MIDDLE 1
55 #define P5GLOVE_FINGER_RING 2
56 #define P5GLOVE_FINGER_PINKY 3
57 #define P5GLOVE_FINGER_THUMB 4
58
59 /* p5glove_open:
60 * Open a handle to the Nth P5 Glove. Returns NULL on error,
61 * and sets errno appropriately.
62 */
63 P5Glove p5glove_open(int glove_number);
64
65
66 /* p5glove_close:
67 * Close an open handle to a P5 Glove.
68 */
69 void p5glove_close(P5Glove glove);
70
71 #define P5GLOVE_DELTA_BUTTONS 0x01
72 #define P5GLOVE_DELTA_FINGERS 0x02
73 #define P5GLOVE_DELTA_POSITION 0x04
74 #define P5GLOVE_DELTA_ROTATION 0x08
75
76 /* p5glove_sample
77 * Retrieve a sample from the P5, before 'timeout' milliseconds
78 * (timeout of < 0 means forever)
79 * Returns the 'delta mask' on success, -1 on error, and sets
80 * errno to EAGAIN if it is called faster than the refresh frequency.
81 */
82 int p5glove_sample(P5Glove glove, int timeout);
83
84
85 /* Get information about the current glove state
86 */
87
88 /* Reference LED positions (in meters)
89 */
90 int p5glove_get_led(P5Glove glove,int led,double position[3]);
91
92 /* Button states
93 */
94 int p5glove_get_buttons(P5Glove glove,uint32_t *button_mask);
95
96 /* Finger flex
97 * (0.0 - unclenched, 1.0 - clenched)
98 */
99 int p5glove_get_finger(P5Glove glove,int finger,double *clench);
100
101 /* Current glove position (in meters)
102 */
103 int p5glove_get_position(P5Glove glove,double position[3]);
104
105 /* Get Axis/Angle rotation information (angle is in degrees)
106 */
107 int p5glove_get_rotation(P5Glove glove,double *angle,double axis[3]);
108
109
110 void p5glove_begin_calibration(P5Glove glove);
111 void p5glove_end_calibration(P5Glove glove);
112
113 int p5glove_get_mouse_mode(P5Glove glove);
114 void p5glove_mouse_mode_on(P5Glove glove);
115 void p5glove_mouse_mode_off(P5Glove glove);
116
117 /* Inline helpers */
118
119 /* Distance between two points */
120 static inline double p5glove_dist(const double a[3],const double b[3])
121 {
122 double w = 0.0;
123 int i;
124
125 for (i = 0; i < 3; i++)
126 w += (a[i]-b[i])*(a[i]-b[i]);
127
128 return sqrt(w);
129 }
130
131 /* Cross product of two vectors */
132 static inline void p5glove_cross(const double v1[3],const double v2[3],double res[3])
133 {
134 res[0]=v1[1]*v2[2]-v1[2]*v2[1];
135 res[1]=v1[2]*v2[0]-v1[0]*v2[2];
136 res[2]=v1[0]*v2[1]-v1[1]*v2[0];
137 }
138
139 /* Optimized for our use. We know the last row and last column are all zeros,
140 * except for [3][3]=1.
141 */
142 static inline void p5glove_vec_mat(const double vec[3],double mat[4][4],double res[3])
143 {
144 int r,c;
145
146 for (r=0; r < 3; r++) {
147 res[r]=0.0;
148 for (c=0; c < 3; c++)
149 res[r] += vec[c]*mat[c][r];
150 }
151 }
152
153 static inline double p5glove_dot(const double a[3],const double b[3])
154 {
155 double res = 0.0;
156 int i;
157
158 for (i=0;i < 3; i++)
159 res += a[i]*b[i];
160
161 return res;
162 }
163
164 /* This is safe to call with v==res
165 */
166 static inline void p5glove_normal(const double v[3],double res[3])
167 {
168 double tmp=0.0;
169 int i;
170
171 for (i=0; i < 3; i++) {
172 tmp += v[i]*v[i];
173 }
174
175 tmp=sqrt(tmp);
176
177 for (i=0; i < 3; i++)
178 res[i] = v[i] / tmp;
179 }
180
181 /* Get the plane normal of three points */
182 static inline void p5glove_plane(const double p1[3],const double p2[3],const double p3[3],double xyz[3])
183 {
184 double v1[3],v2[3];
185
186 v1[0]=p1[0]-p2[0];
187 v1[1]=p1[1]-p2[1];
188 v1[2]=p1[2]-p2[2];
189
190 v2[0]=p3[0]-p2[0];
191 v2[1]=p3[1]-p2[1];
192 v2[2]=p3[2]-p2[2];
193
194 p5glove_cross(v1,v2,xyz);
195
196 if (xyz[0]==0.0 && xyz[1]==0.0 && xyz[2]==0.0)
197 xyz[1]=1.0;
198
199 p5glove_normal(xyz,xyz);
200 }
201
202 /* Angle of three points */
203 static inline double p5glove_angle(const double p1[3],const double p2[3],const double p3[3])
204 {
205 double v1[3],v2[3];
206
207 v1[0]=p1[0]-p2[0];
208 v1[1]=p1[1]-p2[1];
209 v1[2]=p1[2]-p2[2];
210
211 v2[0]=p3[0]-p2[0];
212 v2[1]=p3[1]-p2[1];
213 v2[2]=p3[2]-p2[2];
214
215 p5glove_normal(v1,v1);
216 p5glove_normal(v2,v2);
217
218 return acos(p5glove_dot(v1,v2));
219 }
220
221
222 #ifdef __cplusplus
223 }
224 #endif
225
226 #endif /* P5GLOVE_H */