/
vrpn_Tracker_PhaseSpace.C
1014 lines (858 loc) · 24.8 KB
/
vrpn_Tracker_PhaseSpace.C
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
PhaseSpace, Inc 2017
vrpn_Tracker_PhaseSpace.C
ChangeLog
=================
20170802
* fixed a bug where timestamp's microsecond field was same as seconds in Windows.
20170629
* added support non-fatal errors (i.e. license warnings)
* added server version query for newer cores that support it.
* changed indentation of some debugging output.
20170201
* removed some extraneous comments
20151008
* slave mode now uses cfg specs to assign sensors to markers and rigids.
Unassigned devices will still be assigned arbitrarily.
20150818
* Update to libowl2 API for X2e device support, removed support for Impulse
* removed stylus and button support
* removed maximum rigid and marker limitations
* vrpn.cfg: x,y, and z parameters for point sensors changed to single pos
parameter
* vrpn.cfg: k1,k2,k3,k4 parameters for rigid sensors changed to single init
parameter
*/
#include "vrpn_Tracker_PhaseSpace.h"
#include <errno.h>
#include <math.h>
#include <limits>
#include <string>
#include <sstream>
#include <algorithm>
#include <map>
#define MM_TO_METERS (0.001)
#define MSGBUFSIZE 1024
#ifdef VRPN_INCLUDE_PHASESPACE
//
struct SensorInfo {
vrpn_int32 sensor_id; // vrpn sensor
int type; // owl tracker type
uint32_t tracker; // tracker_id
uint32_t id; // marker_id
std::string opt;
size_t search_hint;
SensorInfo()
{
type = -1;
tracker = 0;
id = 0;
opt = "";
search_hint = std::numeric_limits<uint32_t>::max();
}
};
//
typedef vrpn_vector<SensorInfo> Sensors;
//
bool operator<(const SensorInfo& a, const SensorInfo& b)
{
if(a.type < b.type) return true;
if(a.type > b.type) return false;
return a.id < b.id;
}
//
class vrpn_Tracker_PhaseSpace::SensorManager
{
protected:
Sensors _v;
public:
void add(const SensorInfo& s)
{
_v.push_back(s);
std::sort(_v.begin(), _v.end());
}
const SensorInfo* get_by_sensor(int sensor_id)
{
for(size_t i = 0; i < _v.size(); i++)
if(_v[i].sensor_id == sensor_id) return &_v[i];
return NULL;
}
Sensors::iterator begin()
{
return _v.begin();
}
Sensors::iterator end()
{
return _v.end();
}
};
// ctor
vrpn_Tracker_PhaseSpace::vrpn_Tracker_PhaseSpace(const char *name, vrpn_Connection *c)
: vrpn_Tracker(name ,c),
vrpn_Button_Filter(name, c),
vrpn_Clipping_Analog_Server(name, c),
debug(false),
drop_frames(false),
smgr(NULL)
{
// TODO fix
//num_buttons = vrpn_BUTTON_MAX_BUTTONS;
num_buttons = 0;
smgr = new SensorManager();
if(d_connection) {
// Register a handler for the update change callback
if (register_autodeleted_handler(update_rate_id, handle_update_rate_request, this, d_sender_id))
fprintf(stderr,"vrpn_Tracker: Can't register workspace handler\n");
}
}
// dtor
vrpn_Tracker_PhaseSpace::~vrpn_Tracker_PhaseSpace()
{
context.done();
context.close();
}
//
bool vrpn_Tracker_PhaseSpace::InitOWL()
{
printf("connecting to OWL server at %s...\n", device.c_str());
std::string init_options = "event.markers=1 event.rigids=1";
if(options.length()) init_options += " " + options;
if(drop_frames) printf("drop_frames enabled\n");
// connect to Impulse server
if(context.open(device) <= 0)
{
fprintf(stderr, "owl connect error: %s\n", context.lastError().c_str());
return false;
}
if(debug)
{
std::string coreversion = context.property<std::string>("coreversion");
printf("[debug] server version: %s\n", coreversion.length() ? coreversion.c_str() : "unknown");
std::string apiversion = context.property<std::string>("apiversion");
printf("[debug] API version: %s\n", apiversion.length() ? apiversion.c_str() : "unknown");
}
if(debug) printf("[debug] initialization parameters: %s\n", init_options.c_str());
if(context.initialize(init_options) <= 0)
{
fprintf(stderr, "owl init error: %s\n", context.lastError().c_str());
return false;
}
if(options.find("timebase=") == std::string::npos) context.timeBase(1, 1000000);
if(options.find("scale=") == std::string::npos) context.scale(MM_TO_METERS);
if(context.lastError().length())
{
fprintf(stderr, "owl error: %s\n", context.lastError().c_str());
return false;
}
int slave = context.property<int>("slave");
if(slave) printf("slave mode enabled.\n");
else if(!create_trackers()) return false;
printf("owl initialized\n");
return true;
}
//
bool vrpn_Tracker_PhaseSpace::create_trackers()
{
if(debug) printf("[debug] creating trackers...\n");
// create trackers
int nr = 0;
int nm = 0;
vrpn_vector<uint32_t> ti;
// create rigid trackers
for(Sensors::iterator s = smgr->begin(); s != smgr->end(); s++)
{
if(s->type != OWL::Type::RIGID) continue;
nr++;
if(std::find(ti.begin(), ti.end(), s->tracker) != ti.end())
{
fprintf(stderr, "rigid tracker %d already defined\n", s->tracker);
continue;
}
printf("creating rigid tracker %d\n", s->tracker);
std::string type = "rigid";
std::stringstream name; name << type << nr;
if(debug) printf("[debug] type=%s id=%d name=%s options=\'%s\'\n", type.c_str(), s->tracker, name.str().c_str(), s->opt.c_str());
if(!context.createTracker(s->tracker, type, name.str(), s->opt))
{
fprintf(stderr, "tracker creation error: %s\n", context.lastError().c_str());
return false;
}
ti.push_back(s->tracker);
}
// create point trackers
for(Sensors::iterator s = smgr->begin(); s != smgr->end(); s++)
{
if(s->type != OWL::Type::MARKER) continue;
nm++;
if(std::find(ti.begin(), ti.end(), s->tracker) == ti.end())
{
printf("creating point tracker %d\n", s->tracker);
std::string type = "point";
std::stringstream name; name << type << nm;
ti.push_back(s->tracker);
context.createTracker(s->tracker, type, name.str());
}
if(!context.assignMarker(s->tracker, s->id, "", s->opt))
{
fprintf(stderr, "marker assignment error: %s\n", context.lastError().c_str());
return false;
}
if(debug) printf("[debug] id=%d tracker=%d options=\'%s\'\n", s->id, s->tracker, s->opt.c_str());
}
if(context.lastError().length())
{
fprintf(stderr, "tracker creation error: %s\n", context.lastError().c_str());
return false;
}
return true;
}
// This function should be called each time through the main loop
// of the server code. It polls for data from the OWL server and
// sends them if available.
void vrpn_Tracker_PhaseSpace::mainloop()
{
get_report();
server_mainloop();
return;
}
//
std::string trim(char* line, int len)
{
// cut off leading whitespace
int s, e;
for(s = 0; isspace(line[s]) && s < len; s++);
// cut off comments and trailing whitespace
e = s;
for(int i = s; line[i] && i < len; i++)
{
if(line[i] == '#') break;
if(!isspace(line[i])) e = i+1;
}
return std::string(line+s, e-s);
}
//
bool read_int(const char* str, int &i)
{
if(!str) return false;
errno = 0;
char* endptr = NULL;
i = strtol(str, &endptr, 10);
if(*endptr || errno) {
fprintf(stderr, "Error, expected an integer but got token: \"%s\"\n", str);
return false;
}
return true;
}
//
bool read_uint(const char* str, uint32_t &i)
{
if(!str) return false;
errno = 0;
char* endptr = NULL;
i = strtoul(str, &endptr, 10);
if(*endptr || errno) {
fprintf(stderr, "Error, expected an unsigned integer but got token: \"%s\"\n", str);
return false;
}
return true;
}
//
bool read_bool(const char* str, bool &b)
{
if(!str) return false;
std::string s(str);
if(s == "true") {
b = true;
return true;
} else if(s == "false") {
b = false;
return true;
}
int i = 0;
bool ret = read_int(str, i);
b = i ? true : false;
return ret;
}
//
bool read_float(const char* str, float &f)
{
if(!str) return false;
errno = 0;
char* endptr = NULL;
f = (float) strtod(str, &endptr);
if(*endptr || errno) {
fprintf(stderr, "Error, expected a float but got token: \"%s\"\n", str);
return false;
}
return true;
}
// TODO: Replace all of this junk with <regex> once it is standard in gcc
class ConfigParser
{
protected:
struct Spec {
std::string key;
std::string type;
void* dest;
bool required;
};
std::map <std::string, std::string> keyvals;
std::stringstream _error;
public:
std::string error()
{
return _error.str();
}
bool contains(const std::string &key)
{
return keyvals.find(key) != keyvals.end();
}
std::string join()
{
std::stringstream s;
for(std::map<std::string, std::string>::iterator i = keyvals.begin(); i != keyvals.end(); i++)
s << (i==keyvals.begin()?"":" ") << i->first << "=" << i->second;
return s.str();
}
//
bool parse(SensorInfo &si)
{
_error.str("");
std::string spec_type;
float x=std::numeric_limits<float>::quiet_NaN();
float y=std::numeric_limits<float>::quiet_NaN();
float z=std::numeric_limits<float>::quiet_NaN();
Spec spec_marker[] = {
{ "led", "uint32_t", &si.id, true },
{ "tracker", "uint32_t", &si.tracker, false },
{ "x", "float", &x, false },
{ "y", "float", &y, false },
{ "z", "float", &z, false },
{ "", "", NULL, false } // sentinel
};
Spec spec_rigid[] = {
{ "tracker", "uint32_t", &si.tracker, true },
{ "", "", NULL, false } // sentinel
};
if(pop_int("sensor", si.sensor_id))
{
_error << "required key 'sensor' not found";
return false;
}
if(pop("type", spec_type))
{
_error << "required key 'type' not found";
return false;
}
Spec* spec = NULL;
if(spec_type == "rigid" || spec_type == "rigid_body")
{
si.type = OWL::Type::RIGID;
spec = spec_rigid;
}
else if(spec_type == "point")
{
si.type = OWL::Type::MARKER;
spec = spec_marker;
}
else {
_error << "unknown sensor type: " << spec_type;
return false;
}
for(int i = 0; spec[i].dest; i++)
{
int ret = 0;
if(spec[i].type == "string")
ret = pop(spec[i].key, *((std::string*) spec[i].dest));
else if(spec[i].type == "uint32_t")
ret = pop_uint(spec[i].key, *((uint32_t*) spec[i].dest));
else if(spec[i].type == "float")
ret = pop_float(spec[i].key, *((float*) spec[i].dest));
else
{
_error << "unknown spec type: " << spec[i].type;
return false;
}
if(ret == 1)
{
if(spec[i].required)
{
_error << "required key not found: " << spec[i].key;
return false;
}
}
else if(ret)
{
_error << "error reading value for key \'" << spec[i].key << "'";
return false;
}
}
if(si.type == OWL::Type::RIGID)
si.id = si.tracker;
//special case (legacy)
if(!isnan(x) || !isnan(y) || !isnan(z))
{
if(isnan(x) || isnan(y) || isnan(z))
{
_error << "x,y,z keys must all be specified if any are specified.";
return false;
}
if(contains("pos"))
{
_error << "pos and x,y,z keys are mutually exclusive.";
return false;
}
std::stringstream pos; pos << x << ',' << y << ',' << z;
keyvals["pos"] = pos.str();
}
si.opt = join();
return true;
}
// returns: 0 on success, 1 on key error
int pop(std::string key, std::string &val)
{
if(keyvals.find(key) == keyvals.end())
return 1;
val = keyvals[key];
keyvals.erase(key);
return 0;
}
// returns: 0 on success, 1 on key error, 2 on parse error
int pop_int(std::string key, int &n)
{
std::string v;
if(pop(key, v)) return 1;
return read_int(v.c_str(), n) ? 0 : 2;
}
// returns: 0 on success, 1 on key error, 2 on parse error
int pop_uint(std::string key, uint32_t &n)
{
std::string v;
if(pop(key, v)) return 1;
return read_uint(v.c_str(), n) ? 0 : 2;
}
// returns: 0 on success, 1 on key error, 2 on parse error
int pop_float(std::string key, float &n)
{
std::string v;
if(pop(key, v)) return 1;
return read_float(v.c_str(), n) ? 0 : 2;
}
// returns: 0 on success, 1 on key error, 2 on parse error
int pop_bool(std::string key, bool &n)
{
std::string v;
if(pop(key, v)) return 1;
return read_bool(v.c_str(), n) ? 0 : 2;
}
// return index of error, or -1 on success
int parse_kv(std::string str)
{
keyvals.clear();
_error.str("");
vrpn_vector<char> current_key;
vrpn_vector<char> current_val;
int i = 0;
while(str[i])
{
current_key.clear();
current_val.clear();
while(isspace(str[i])) i++;
while(str[i]) {
if(str[i] == '"') {
_error << "key names are not allowed to contain quotes or be contained in quotes.";
return i;
} else if(isspace(str[i])) {
_error << "unexpected whitespace.";
return i;
} else if(str[i] == '=') {
i++;
break;
} else current_key.push_back(str[i++]);
}
if(!current_key.size()) {
_error << "empty key name.";
return i;
}
bool quoted = false;
if(str[i] == '"') {
quoted = true;
i++;
}
while(str[i]) {
if(str[i] == '"') {
if(quoted) {
quoted = false;
i++;
break;
} else {
_error << "misplaced quotes.";
return i;
}
} else if(str[i] == '=') {
_error << "unexpected '=' char in value token.";
return i;
} else if(!quoted && isspace(str[i])) break;
else current_val.push_back(str[i++]);
}
if(quoted) {
_error << "unterminated quotes.";
return i;
}
if(str[i] && !isspace(str[i])) {
_error << "expected whitespace after value token.";
return i;
}
if(!current_val.size()) {
_error << "empty value string.";
return i;
}
std::string key = std::string(current_key.data(), current_key.size());
std::string val = std::string(current_val.data(), current_val.size());
if(keyvals.find(key) != keyvals.end())
{
_error << "duplicate key encountered: '" << key << "'";
return i;
}
keyvals[key] = val;
}
return -1;
}
};
//
bool vrpn_Tracker_PhaseSpace::load(FILE* file)
{
const int BUFSIZE = 1024;
char line[BUFSIZE];
bool inTag = false;
std::string ln;
bool eof = false;
while(!(eof = (fgets(line, BUFSIZE, file) == NULL)))
{
ConfigParser parser;
ln = trim(line, BUFSIZE);
// skip contentless lines
if(!ln.length()) continue;
// skip lines unless we're nested in <owl> tags
if(ln == "<owl>") {
if (inTag) {
fprintf (stderr, "Error, nested <owl> tag encountered. Aborting...\n");
return false;
} else {
inTag = true;
continue;
}
} else if (ln == "</owl>") {
if (inTag) {
if(debug)
{
printf("[debug] parsed config file:\n");
printf("[debug] %s\n", options.c_str());
for(Sensors::const_iterator s = smgr->begin(); s != smgr->end(); s++)
fprintf(stdout, "[debug] sensor=%d type=%d tracker=%d options=\'%s\'\n", s->sensor_id, s->type, s->tracker, s->opt.c_str());
}
// closing tag encountered, exit.
return true;
} else {
fprintf (stderr, "Error, </owl> tag without <owl> tag. Aborting...\n");
return false;
}
}
// parse line for key-value pairs
if(inTag) {
int e = parser.parse_kv(ln);
if(e >= 0) {
fprintf(stderr, "Error at character %d.\n", e);
break;
}
}
if(parser.contains("sensor")) {
// line is a sensor specification
SensorInfo info;
if(parser.parse(info))
{
// check duplicates
if(smgr->get_by_sensor(info.sensor_id))
{
fprintf(stderr, "duplicate sensor defined: %d\n", info.sensor_id);
break;
}
smgr->add(info);
}
else
{
fprintf(stderr, "%s\n", parser.error().c_str());
break;
}
} else {
// line is an option specification
if(parser.pop("device", device) == 2)
{
fprintf(stderr, "error reading value for key 'device'\n");
break;
}
if(parser.pop_bool("drop_frames", drop_frames) == 2)
{
fprintf(stderr, "error reading value for key 'drop_frames'\n");
break;
}
if(parser.pop_bool("debug", debug) == 2)
{
fprintf(stderr, "error reading value for key 'debug'\n");
break;
}
std::string new_options = parser.join();
if(new_options.length()) options += (options.length()?" ":"") + parser.join();
}
}
if(eof) fprintf(stderr, "Unexpected end of file.\n");
else fprintf(stderr, "Unable to parse line: \"%s\"\n", ln.c_str());
return false;
}
//
bool vrpn_Tracker_PhaseSpace::enableStreaming(bool enable)
{
if(!context.isOpen()) return false;
context.streaming(enable ? 1 : 0);
printf("streaming: %d\n", enable);
if(context.lastError().length())
{
fprintf(stderr, "owl error: %s\n", context.lastError().c_str());
return false;
}
return true;
}
//
void vrpn_Tracker_PhaseSpace::set_pose(const OWL::Rigid &r)
{
//set the position
pos[0] = r.pose[0];
pos[1] = r.pose[1];
pos[2] = r.pose[2];
//set the orientation quaternion
//OWL has the scale factor first, whereas VRPN has it last.
d_quat[0] = r.pose[4];
d_quat[1] = r.pose[5];
d_quat[2] = r.pose[6];
d_quat[3] = r.pose[3];
}
//
void vrpn_Tracker_PhaseSpace::report_marker(vrpn_int32 sensor, const OWL::Marker &m)
{
d_sensor = sensor;
if(debug)
{
int tr = context.markerInfo(m.id).tracker_id;
printf("[debug] sensor=%d type=point tracker=%d led=%d x=%f y=%f z=%f cond=%f\n", d_sensor, tr, m.id, m.x, m.y, m.z, m.cond);
}
if(m.cond <= 0) return;
pos[0] = m.x;
pos[1] = m.y;
pos[2] = m.z;
//raw positions have no rotation
d_quat[0] = 0;
d_quat[1] = 0;
d_quat[2] = 0;
d_quat[3] = 1;
send_report();
}
//
void vrpn_Tracker_PhaseSpace::report_rigid(vrpn_int32 sensor, const OWL::Rigid& r, bool is_stylus)
{
d_sensor = sensor;
if(debug)
{
printf("[debug] sensor=%d type=rigid tracker=%d x=%f y=%f z=%f w=%f a=%f b=%f c=%f cond=%f\n", d_sensor, r.id, r.pose[0], r.pose[1], r.pose[2], r.pose[3], r.pose[4], r.pose[5], r.pose[6], r.cond);
}
if(r.cond <= 0) return;
// set the position/orientation
set_pose(r);
send_report();
}
//
void vrpn_Tracker_PhaseSpace::send_report(void)
{
if(d_connection)
{
char msgbuf[MSGBUFSIZE];
int len = vrpn_Tracker::encode_to(msgbuf);
if(d_connection->pack_message(len, vrpn_Tracker::timestamp, position_m_id, d_sender_id, msgbuf,
vrpn_CONNECTION_LOW_LATENCY)) {
fprintf(stderr,"PhaseSpace: cannot write message: tossing\n");
}
}
}
//
void vrpn_Tracker_PhaseSpace::report_button(vrpn_int32 sensor, int value)
{
if(d_sensor < 0 || d_sensor >= vrpn_BUTTON_MAX_BUTTONS)
{
fprintf(stderr, "error: sensor %d exceeds max button count\n", sensor);
return;
}
d_sensor = sensor;
buttons[d_sensor] = value;
if(debug)
{
printf("[debug] button %d 0x%x\n", d_sensor, value);
}
}
//
void vrpn_Tracker_PhaseSpace::report_button_analog(vrpn_int32 sensor, int value)
{
d_sensor = sensor;
setChannelValue(d_sensor, value);
if(debug)
{
printf("[debug] analog button %d 0x%x\n", d_sensor, value);
}
}
//
template<class A>
const A* find(int id, size_t& hint, vrpn_vector<A> &data)
{
if(hint >= data.size() || id != data[hint].id)
{
for(size_t i = 0; i < data.size(); i++)
{
const A &d = data[i];
if(d.id == id)
{
hint = i;
return &d;
}
}
return NULL;
}
return &data[hint];
}
//
int vrpn_Tracker_PhaseSpace::get_report(void)
{
if(!context.isOpen()) return 0;
int maxiter = 1;
// set limit on looping
if(drop_frames) maxiter = 10000;
// read new data
const OWL::Event *event = NULL;
{
const OWL::Event *e = NULL;
int count = 0;
while(context.isOpen() && context.property<int>("initialized") && (e = context.nextEvent()))
{
if(e->type_id() == OWL::Type::FRAME) event = e;
else if(e->type_id() == OWL::Type::ERROR)
{
std::string err;
e->get(err);
fprintf(stderr, "owl error event: %s\n", err.c_str());
if(e->name() == "fatal")
{
fprintf(stderr, "stopping...\n");
context.done();
context.close();
return -1;
}
}
else if(e->type_id() == OWL::Type::BYTE)
{
std::string s; e->get(s);
printf("%s: %s\n", e->name(), s.c_str());
if(strcmp(e->name(), "done") == 0)
return 0;
}
if(maxiter && ++count >= maxiter) break;
}
}
if(!event) return 0;
// set timestamp
#if WIN32 // msvc 2008
vrpn_Tracker::timestamp.tv_sec = (long) event->time() / 1000000;
vrpn_Tracker::timestamp.tv_usec = (long) event->time() % 1000000;
#else
lldiv_t divresult = lldiv(event->time(), 1000000);
vrpn_Tracker::timestamp.tv_sec = divresult.quot;
vrpn_Tracker::timestamp.tv_usec = divresult.rem;
#endif
if(debug)
{
printf("[debug] time=%ld.%06ld\n", vrpn_Tracker::timestamp.tv_sec, vrpn_Tracker::timestamp.tv_usec);
}
OWL::Markers markers;
OWL::Rigids rigids;
for(const OWL::Event *e = event->begin(); e != event->end(); e++)
{
if(e->type_id() == OWL::Type::MARKER)
e->get(markers);
else if(e->type_id() == OWL::Type::RIGID)
e->get(rigids);
}
int slave = context.property<int>("slave");
vrpn_vector<const OWL::Marker*> reported_markers;
vrpn_vector<const OWL::Rigid*> reported_rigids;
int sensor = 0;
for(Sensors::iterator s = smgr->begin(); s != smgr->end(); s++)
{
switch(s->type)
{
case OWL::Type::MARKER:
{
const OWL::Marker *m = find<OWL::Marker>(s->id, s->search_hint, markers);
if(m) {
report_marker(s->sensor_id, *m);
if(slave) reported_markers.push_back(m);
}
}
break;
case OWL::Type::RIGID:
{
const OWL::Rigid *r = find<OWL::Rigid>(s->id, s->search_hint, rigids);
if(r) {
report_rigid(s->sensor_id, *r);
if(slave) reported_rigids.push_back(r);
}
}
break;
default:
break;
}
// TODO implement styluses
if(s->sensor_id > sensor) sensor = s->sensor_id;
}
if(slave)
{
// in slave mode, client doesn't necessarily know what the incoming data will be.
// Report the ones not specified in the cfg file
if(sensor < 1000) sensor = 1000; // start arbitrarily at 1000
for(OWL::Markers::iterator m = markers.begin(); m != markers.end(); m++)
if(std::find(reported_markers.begin(), reported_markers.end(), &*m) == reported_markers.end())
report_marker(sensor++, *m);
for(OWL::Rigids::iterator r = rigids.begin(); r != rigids.end(); r++)
if(std::find(reported_rigids.begin(), reported_rigids.end(), &*r) == reported_rigids.end())
report_rigid(sensor++, *r);
}
// finalize button and analog reports
vrpn_Button_Filter::report_changes();
vrpn_Clipping_Analog_Server::report_changes();
if(context.lastError().length())
{
printf("owl error: %s\n", context.lastError().c_str());
return -1;
}
if(!context.property<int>("initialized"))
return -1;
return (markers.size() || rigids.size()) ? 1 : 0;
}