/
mocapInterfaceNew.py
1102 lines (751 loc) · 35.4 KB
/
mocapInterfaceNew.py
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
import viz
import vizshape
import math
from OWL import *
from os import rename
# Marker ID returned by owlGetMarkers() depends on
# the ID of the rigid body to which it is attached
# and the the rigid_marker number (from zero to N, where N = num of markers attached to rigid body)
# This takes tracker num and rigid-marker id and converts.
def rigidXMarkerID(tracker, index):
## BIT SHIFT LEFT BY 12 BITS IS BINARY MULTIPLICATION BY (2 ** 12)
return ( ( tracker << 12 ) | (index) );
#fed
class rigidBody(viz.EventClass):
def __init__(self,trackerIdx,filePath,fileName,vizNodeShape,vizNodeSize,isVisible):
self.filePath = filePath
self.fileName = fileName
self.trackerIdx = trackerIdx
self.markerID_midx = []
self.markerPos_midx_localXYZ = []
self.isVisible = isVisible
self.vizNode = 0
self.physNode = 0
self.vizNodeLink = 0
self.vizNodeShape = vizNodeShape
if( type(vizNodeSize) is list and len(vizNodeSize) == 1):
self.vizNodeSize = vizNodeSize[0]
else:
self.vizNodeSize = vizNodeSize
self.serverData = []
# Create rigid tracker
self._loadDefaults()
# Create rigidTracker
owlTrackeri( self.trackerIdx, OWL_CREATE, OWL_RIGID_TRACKER );
# Populate rigidTracker
for i in range( len(self.markerID_midx) ):
# set markers
owlMarkeri( MARKER(self.trackerIdx, i), OWL_SET_LED, self.markerID_midx[i] )
# set marker positions
owlMarkerfv(MARKER(self.trackerIdx, i), OWL_SET_POSITION, self.markerPos_midx_localXYZ[i])
# Activate tracking for rigidTracker
owlTracker( self.trackerIdx, OWL_ENABLE )
# Flush requests and check for errors
if not owlGetStatus():
#owl_print_error("error in point tracker setup", owlGetError())
errorCode = owlGetError();
print("Error in point tracker setup.", errorCode)
return 0
self._createVizNode(self.vizNodeShape,self.vizNodeSize)
def _loadDefaults(self):
import os.path
openfile = [];
openfile = open( self.filePath + self.fileName, 'r' );
lineData = openfile.readlines();
parsedData = [];
markerID = [];
markerPos = [];
count = 0;
for currentLine in lineData:
tempLineDataList = currentLine.split(',');
markerID.append( int(tempLineDataList[0]) );
markerPos.append( map( float, tempLineDataList[1].split() ) );
count += 1;
#rof
self.markerID_midx = markerID
self.markerPos_midx_localXYZ = markerPos
openfile.close()
print 'Mocap: Read ' + str(count) + ' lines from the rigid body file.'
if count == 0: print 'This is likely to cause OWL.init() to fail'
def _createVizNode(self,shape,size):
if(shape == 'box' ):
print 'Making box vizNode'
if( type(size) == float or len(size) !=3):
print '**********Invalid size for ' + self.fileName
print 'Check rigidBodySizesString. Expected 3 val for box: height,width,length.'
print 'Got: ' + str(size)
import winsound
winsound.Beep(1000,250)
self.vizNode = vizshape.addBox(size,alpha = 0.7,color=viz.RED)
return
elif(shape == 'sphere'):
if( type(size) != float): # accept a float
print '**********Invalid size for ' + self.fileName
print 'Check rigidBodySizesString. Expected 1 val for sphere: radius'
print 'Got: ' + str(size)
import winsound
winsound.Beep(1000,250)
print 'Making sphere vizNode'
self.vizNode = vizshape.addSphere(radius = float(size), alpha = 0.7,color=viz.BLUE)
elif(shape == 'cylinder'):
if( type(size) == float or len(size) !=2):
print '**********Invalid size for ' + self.fileName
print 'Check rigidBodySizesString. Expected 2 val for cylinder: height,radius'
print 'Got: ' + str(size)
import winsound
winsound.Beep(1000,250)
print 'Making cylinder vizNode'
# height, rad,
self.vizNode = vizshape.addCylinder(height=size[0],radius=size[1], alpha = 0.3,color=viz.YELLOW,axis=vizshape.AXIS_X)
#self.vizNode.setAxisAngle(1,0,0,90)
if( self.vizNode ) :
self.vizNode.disable(viz.CULL_FACE)
if( self.isVisible ):
self.vizNode.visible( viz.ON ) #Make the object visible.
else:
self.vizNode.visible( viz.OFF ) #Make the object invisible.
print self.fileName + ' vizNode created, but not set as visible'
self.physNode = self.vizNode.collideMesh()
else:
print 'Problem creating viz node'
import winsound
winsound.Beep(1000,250)
def updateVizNode(self,pos_XYZ,quat_XYZW):
transformMatrix = viz.Transform()
transformMatrix.setQuat(quat_XYZW)
transformMatrix.postTrans(pos_XYZ)
transformMatrix.postAxisAngle(0,1,0,90)
self.vizNode.setMatrix(transformMatrix)
def getMarkerPositions(self,allMarkers_midx):
# Build list of updated marker positions in newPosWorldCoords
newPos_midx_GlobalXYZ = [];
for oldMarkerIdx in range( 0, len(self.markerID_midx), 1 ):
for newMarkerIdx in range( 0, len(allMarkers_midx), 1 ):
# Confused? See rigidXMarkerID macro at top!
oldRigidIDConverted = rigidXMarkerID( self.trackerIdx, oldMarkerIdx );
if( allMarkers_midx[newMarkerIdx].id == oldRigidIDConverted ):
newPos_midx_GlobalXYZ.append( [ allMarkers_midx[newMarkerIdx].x,
allMarkers_midx[newMarkerIdx].y,
allMarkers_midx[newMarkerIdx].z ] );
#fi
#rof
#rof
if( len(newPos_midx_GlobalXYZ) < len(self.markerID_midx ) ):
print "getMarkerPositions: Error. Could not see all markers."
for i in range(len(allMarkers_midx)):
print (" Visible Marker IDs: " + str(allMarkers_midx[i].id) );
return -1
#rof
else:
return newPos_midx_GlobalXYZ
def saveNewDefaults(self):
fileObject = open(self.filePath + 'temp.rb','w')#self.fileName , 'w')
# Get old marker id's and new positions
oldRigidID_midx = self.markerID_midx
newRigidPos_midx_xyz = self.markerPos_midx_localXYZ
for idx in range(len(newRigidPos_midx_xyz )):
posString = str(newRigidPos_midx_xyz[idx][0]) + ' ' + str(newRigidPos_midx_xyz[idx][1]) + ' ' + str(newRigidPos_midx_xyz[idx][2]);
newLine = str(oldRigidID_midx[idx]) + ', ' + posString + '\n'
fileObject.write(newLine);
fileObject.close();
#from shutil import move
#move(self.filePath + 'temp.rb', self.filePath + self.fileName)
from os import remove
from shutil import move
remove(self.filePath + self.fileName)
move(self.filePath + 'temp.rb', self.filePath + self.fileName)
print "Rigid body definition written to file"
def resetRigid( self, allMarkers_midx ):
#newPos_midx_GlobalXYZ = self.getMarkerPositions( allMarkers_midx )
# Build list of updated marker positions in newPosWorldCoords
newPos_midx_GlobalXYZ = [];
# Find markers that belong to the rigid tracker.
# Collect their position in newPos_midx_GlobalXYZ
for oldMarkerIdx in range( 0, len(self.markerID_midx), 1 ):
for newMarkerIdx in range( 0, len(allMarkers_midx), 1 ):
# rigidXMarkerID macro defined at top of file!
oldRigidIDConverted = rigidXMarkerID( self.trackerIdx, oldMarkerIdx );
if( allMarkers_midx[newMarkerIdx].id == oldRigidIDConverted ):
newPos_midx_GlobalXYZ.append( [ allMarkers_midx[newMarkerIdx].x,
allMarkers_midx[newMarkerIdx].y,
allMarkers_midx[newMarkerIdx].z ] );
#fi
#rof
#rof
if( len(newPos_midx_GlobalXYZ) < len(self.markerID_midx ) ):
print "ResetRigid: Error. Could not see all markers."
for i in range(len(allMarkers_midx)):
print (" Visible Marker IDs: " + str(allMarkers_midx[i].id) );
#rof
return
####################################################################################
## Here's where we set the COM / center of rotatnoi for the rigid body
if( self.fileName.find('nvis')> -1 ):
print 'mocapInterface.rigidBody.resetRigid: For '+ self.fileName + ' using mean of first and last markers as COM'
# calculate the center of the rigid body using the first and last marker
center_GlobalXYZ = [(newPos_midx_GlobalXYZ[0][0] + newPos_midx_GlobalXYZ[-1][0])/2,
(newPos_midx_GlobalXYZ[0][1] + newPos_midx_GlobalXYZ[-1][1])/2,
(newPos_midx_GlobalXYZ[0][2] + newPos_midx_GlobalXYZ[-1][2])/2]
elif( self.fileName.find('oculus')> -1 ):
print 'mocapInterface.rigidBody.resetRigid: For '+ self.fileName + ' using mean of markers 2 and 3 as COM'
# calculate the center of the rigid body using the first and last marker
# FOr the oculus, the eyes are 2.5 cm behind the front of the helmet
center_GlobalXYZ = [(newPos_midx_GlobalXYZ[3][0] + newPos_midx_GlobalXYZ[2][0])/2,
(newPos_midx_GlobalXYZ[3][1] + newPos_midx_GlobalXYZ[2][1])/2,
(-2.5+newPos_midx_GlobalXYZ[3][2] + newPos_midx_GlobalXYZ[2][2])/2]
#self.vizNode.setCenter(0,0,0)
else:
print 'mocapInterface.rigidBody.resetRigid: For '+ self.fileName + ' using mean of all markers as COM'
sum = []
center_GlobalXYZ = [0,0,0]
for dim in range(3):
sum = 0
for mIdx in range(len(newPos_midx_GlobalXYZ)):
sum = sum+newPos_midx_GlobalXYZ[mIdx][dim]
center_GlobalXYZ[dim] = sum / len(newPos_midx_GlobalXYZ)
# center_GlobalXYZ = [sum(newPos_midx_GlobalXYZ[:-1][0])/len(newPos_midx_GlobalXYZ),
# sum(newPos_midx_GlobalXYZ[:][1])/len(newPos_midx_GlobalXYZ),
# sum(newPos_midx_GlobalXYZ[:][2])/len(newPos_midx_GlobalXYZ)]
# Convert to rigid body coordinates, relative to center
newPosRigidCoords_mIdx_LocalXYZ = []
for i in range( 0, len(newPos_midx_GlobalXYZ), 1 ):
newPosRigidCoords_mIdx_LocalXYZ.append( [ newPos_midx_GlobalXYZ[i][0]-center_GlobalXYZ[0],
newPos_midx_GlobalXYZ[i][1]-center_GlobalXYZ[1],
newPos_midx_GlobalXYZ[i][2]-center_GlobalXYZ[2] ] );
#rof
# Update rigid body definition on the owl server
# and in this rigid body
owlTracker( self.trackerIdx, OWL_DISABLE );
for i in range( 0, len(newPosRigidCoords_mIdx_LocalXYZ), 1 ):
owlMarkerfv( MARKER(self.trackerIdx, i), OWL_SET_POSITION, newPosRigidCoords_mIdx_LocalXYZ[i] )
self.markerPos_midx_localXYZ[i] = newPosRigidCoords_mIdx_LocalXYZ[i];
#rof
owlTracker( self.trackerIdx, OWL_ENABLE );
if not owlGetStatus():
print ( "ResetRigid: Could not enable rigid body. OwlGetStatus returned: ", owlGetError() );
exit();
#fi
print "Rigid body definition updated on server."
def toggleVisibility(self):
if(self.isVisible):
print 'Rigid body no longer visible'
self.isVisible = 0
self.vizNode.visible( viz.OFF ) #Make the object visible.
elif self.isVisible == 0:
print 'Rigid body visible'
self.isVisible = 1
self.vizNode.visible( viz.ON ) #Make the object visible.
def rotateRigid(self,rotateByDegs_XYZ):
# create a temp vizard 3d object
# rotate, use vertices to redefine rigid object
# maybe that phasespace coordinate system does not match vizard coordinate system
viz.startLayer(viz.POINTS)
for idx in range(len(self.markerPos_midx_localXYZ)):
viz.vertex(self.markerPos_midx_localXYZ[idx][0],self.markerPos_midx_localXYZ[idx][1],self.markerPos_midx_localXYZ[idx][2])
tempRigidObject = viz.endLayer()
tempRigidObject.visible( viz.OFF ) #Make the object invisible.
# Update rigid body definition on the owl server
#tempRigidObject.setEuler( rotateByDegs_XYZ,viz.RELATIVE ) #,viz.RELATIVE)
tempRigidObject.setQuat(rotateByDegs_XYZ,viz.ABS_GLOBAL)
owlTracker(self.trackerIdx,OWL_DISABLE)
count = 0
for i in xrange(len(self.markerID_midx)):
owlMarkerfv(MARKER(self.trackerIdx, i), OWL_SET_POSITION, tempRigidObject.getVertex(i,viz.RELATIVE))
self.markerPos_midx_localXYZ[count] = tempRigidObject.getVertex(i,viz.RELATIVE)
count +=1
owlTracker(self.trackerIdx, OWL_ENABLE);
tempRigidObject.remove() #Remove the object.
class rigidHMD(rigidBody):
def __init__(self,trackerIdx,filePath,fileName,vizNodeShape,vizNodeSize,isVisible):
print 'Initializing HMD'
super(rigidHMD,self).__init__(trackerIdx,filePath,fileName,vizNodeShape,vizNodeSize,isVisible);
#self.vizNodeLink = viz.link(self.vizNode,viz.MainView)
self.showCylinders = 0
########################################################
########################################################
## Parameters of the eyes and eye-probes
self.iod = viz.MainWindow.getIPD()
self.eyeProbeDistance = .02
self.eyeOffsetDegs = self.eyeProbeDistance * math.tan(math.radians(self.iod/2));
self.eyeProbeCylLength = .6
self.eyeGridSize = .003
self.eyeProbeRadius = viz.MainWindow.getIPD()/4
self.probeBarWidthMM = .002;
# During calibration, the near plane is drawn self.planeSeperation
# mm in front of the far plane
self.planeSeperation = .01;
self.calibLineWidthDegs = 2;
self.tempAngle= 0;
self.createCalibrationCylinders()
def createCalibrationCylinders(self):
##############################################################
##############################################################
## left eye and calib grid
self.leftEyeProbeCylinder = vizshape.addCylinder(height=self.eyeProbeCylLength,radius=self.eyeProbeRadius,top=False,bottom=False,axis = vizshape.AXIS_X,color=viz.BLUE)
self.leftEyeProbeCylinder.disable(viz.CULL_FACE)
self.leftEyeProbeCylinder.visible( viz.OFF )
self.leftEyeCylinderBack = vizshape.addCircle(self.eyeProbeRadius,axis=vizshape.AXIS_X,color=viz.GREEN)
self.leftEyeCylinderBack.disable(viz.CULL_FACE)
self.leftEyeCylinderBack.setParent(self.leftEyeProbeCylinder)
self.leftEyeCylinderBack.setPosition(self.eyeProbeCylLength/2,0,0) #self.leftEyeProbeCylinder)
self.lGridVertBar = vizshape.addBox([self.probeBarWidthMM,self.eyeProbeRadius*2,self.probeBarWidthMM])
self.lGridVertBar.setParent(self.leftEyeCylinderBack)
##############################################################
##############################################################
## Draw right eye components
self.rightEyeProbeCylinder = vizshape.addCylinder(height=self.eyeProbeCylLength,radius=self.eyeProbeRadius,top=False,bottom=False,axis = vizshape.AXIS_X,color=viz.BLUE)
self.rightEyeProbeCylinder.disable(viz.CULL_FACE)
self.rightEyeProbeCylinder.visible( viz.OFF )
self.rightEyeCylinderBack = vizshape.addCircle(self.eyeProbeRadius,axis=vizshape.AXIS_X,color=viz.GREEN)
self.rightEyeCylinderBack.disable(viz.CULL_FACE)
self.rightEyeCylinderBack.setParent(self.rightEyeProbeCylinder)
self.rightEyeCylinderBack.setPosition(self.eyeProbeCylLength/2,0,0) #self.leftEyeProbeCylinder)
self.rGridHorzBar = vizshape.addBox([self.probeBarWidthMM,self.probeBarWidthMM,self.eyeProbeRadius*2])
self.rGridHorzBar.setParent(self.rightEyeCylinderBack)
# def updateVizNode(self,pos,rot):
#
# # Call the update function of parent class, rigidBody
# # This calls rigidBody.updateVizNode(pos,rot)
# super(rigidHMD,self).updateVizNode(pos,rot)
#
# # Now, if eyes are draw, change their position
# if( self.showCylinders ):
#
#
# headPos_XYZ = self.vizNode.getPosition();
#
# # move the eyes self.eyeProbeDistance up the Z axis, and
# #self.leftEyeCylinderBack.setPosition(headPos_XYZ[0]-self.iod/2,headPos_XYZ[1],headPos_XYZ[2]-self.eyeProbeDistance)
#
## self.leftEyeProbeCylinder.setPosition(headPos_XYZ[0]+self.eyeProbeDistance+self.eyeProbeCylLength/2,headPos_XYZ[1],headPos_XYZ[2]+self.iod/2)
## self.leftEyeProbeCylinder.setEuler([90,0,0])
##
## self.leftEyeCylinderBack.setPosition(0,0,self.planeSeperation)
##
## self.lGridVertBar.setPosition([0,0,0])
#
# ################################################
# ################################################
##
## # Set right plane positions
## #self.rightEyeCylinderBack.setPosition(headPos_XYZ[0]+self.iod/2,headPos_XYZ[1],headPos_XYZ[2]-self.eyeProbeDistance)
## self.rightEyeProbeCylinder.setPosition(headPos_XYZ[0]+self.eyeProbeDistance,headPos_XYZ[1],headPos_XYZ[2]-self.iod/2)
## self.rightEyeProbeCylinder.setEuler([90,0,0])
##
## self.rightEyeCylinderBack.setPosition(0,0,self.planeSeperation)
##
## self.rGridHorzBar.setPosition([0,0,0])
#
def toggleCalibrationCylinders(self):
if(self.showCylinders):
self.showCylinders = 0
self.leftEyeProbeCylinder.visible( viz.OFF ) #Make the object visible.
self.rightEyeProbeCylinder.visible( viz.OFF ) #Make the object visible.
# reset clipping plane to previous values
print 'Eye probes off, head on'
elif self.showCylinders == 0:
self.showCylinders = 1
self.leftEyeProbeCylinder.visible( viz.ON ) #Make the object visible.
self.rightEyeProbeCylinder.visible( viz.ON ) #Make the object visible.
#viz.MainWindow.clip(0.001 ,1)
print 'Eye probes on, head off'
headPos_XYZ = self.vizNode.getPosition();
self.leftEyeProbeCylinder.setPosition(0,headPos_XYZ[1],(viz.MainWindow.getIPD()/2)) #-.06/4)#headPos_XYZ[2]-self.iod/4)
self.rightEyeProbeCylinder.setPosition(0,headPos_XYZ[1],(-viz.MainWindow.getIPD()/2))
def changeIOD(self,increment):
self.iod += increment
if(self.iod <= .01):
#NOPE
self.iod -= increment
return
else:
self.iod += increment
self.eyeOffsetDegs = math.atan( math.radians( self.iod/2/self.eyeProbeDistance ))
self.leftEyeCylinderBack.setPosition(-self.iod/2,0,self.eyeProbeDistance)
self.rightEyeCylinderBack.setPosition(self.iod/2,0,self.eyeProbeDistance)
print 'Offset is: ' + str( self.eyeOffsetDegs )
def changeEyeProbeDistance(self,increment):
# Preserves IOD by allowing angle to change
self.eyeProbeDistance += increment
print 'Distance is: ' + str( self.eyeProbeDistance )
self.eyeOffsetDegs = math.atan( math.radians( self.iod/2/self.eyeProbeDistance ))
print 'Offset is: ' + str( self.eyeOffsetDegs )
#self.iod = self.eyeProbeDistance * math.tan( math.radians( self.eyeOffsetDegs ))
if( self.eyeProbeDistance > 1 ): self.eyeProbeDistance = 1;
if( self.eyeProbeDistance < -.5 ): self.eyeProbeDistance = .05;
self.leftEyeCylinderBack.setPosition(-self.iod/2,0,self.eyeProbeDistance)
self.rightEyeCylinderBack.setPosition(self.iod/2,0,self.eyeProbeDistance)
def getHeadOrientation(self):
# is server data up to date?
# is rotateRigid working correctly?
#
# Quaternion
rot = [ self.serverData.pose[4], self.serverData.pose[5], -self.serverData.pose[6], -self.serverData.pose[3] ];
#[0.002087714894559653, 0.7072209410999575, -0.0021176456789796234, 0.7069863488731505]
transformMatrix = viz.Transform()
transformMatrix.setQuat(rot)
# Not sure why I need to rotate 180 degs here.
transformMatrix.postAxisAngle(0,1,0,90)
#import vizmat
#euler_XYZ = vizmat.QuatToEuler(transformMatrix.getQuat())
quat_XYZW = transformMatrix.getQuat()
quat_XYZW[3] = -quat_XYZW[3]
print str(quat_XYZW)
self.rotateRigid(quat_XYZW)
#self.rotateRigid([-euler_XYZ[0],-euler_XYZ[1],-euler_XYZ[2]])
#self.rotateRigid([-orientation_XYZ[2],-orientation_XYZ[1],-orientation_XYZ[1]])
#newPos_midx_GlobalXYZ = self.getMarkerPositions( allMarkers_midx )
#(newPos_midx_GlobalXYZ[0] - newPos_midx_GlobalXYZ[-1])
#
# viz.startLayer(viz.POINTS)
#
# for idx in range(len(newPos_midx_GlobalXYZ)):
# #viz.vertex(newPos_midx_GlobalXYZ[idx][0],newPos_midx_GlobalXYZ[idx][1],newPos_midx_GlobalXYZ[idx][2])
#
# viz.vertex(self.markerPos_midx_localXYZ[idx][0],self.markerPos_midx_localXYZ[idx][1],self.markerPos_midx_localXYZ[idx][2])
#
# tempRigidObject = viz.endLayer()
# tempRigidObject.visible( viz.OFF ) #Make the object invisible.
# #############################
#
# # Update rigid body definition on the owl server
# # and in this rigid body
# owlTracker( self.trackerIdx, OWL_DISABLE )
#
# for i in range( 0, len(newPosRigidCoords_mIdx_LocalXYZ), 1 ):
#
# owlMarkerfv( MARKER(self.trackerIdx, i), OWL_SET_POSITION, newPosRigidCoords_mIdx_LocalXYZ[i] )
# self.markerPos_midx_localXYZ[i] = newPosRigidCoords_mIdx_LocalXYZ[i];
#
#rof
#
# owlTracker( self.trackerIdx, OWL_ENABLE );
#
# if not owlGetStatus():
#
# print ( "ResetRigid: Could not enable rigid body. OwlGetStatus returned: ", owlGetError() );
#
# exit();
class mocapInterface(viz.EventClass):
def __init__(self, phaseSpaceIP,phaseSpaceFilePath,owlParamMarkerCount,rigidFileNames_ridx,
rigidBodyShapes_ridx,rigidBodySizes_ridx,rigidBodyToggleVisibility_ridx):
viz.EventClass.__init__(self)
#self.config = config;
self.origin = []
self.scale = []
self.serverAddress = []
self.showCylinders = 0
self.rigidHMDIdx = -1
self.allRigids_ridx = [];
self.allMarkers_midx = [];
self.allTrackers_tidx = []; # A method for keeping track of tracker numbers
self.frame = 0
self.last_tick = 0
self.off = True
self.markersUsedInRigid = [];
#self.linkMainViewToHead = 0
self.mainViewLinkedToHead = 0
self.phaseSpaceFilePath = phaseSpaceFilePath #'../Resources/'
self.origin = [0,0,0];
self.scale = [0.001,0.001,0.001];
self.serverAddress = phaseSpaceIP; #'192.168.1.230';
self.rigidFileNames_ridx = rigidFileNames_ridx #['hmd-oculus.rb','paddle-hand.rb']
self.rigidBodyShapes_ridx = rigidBodyShapes_ridx# ['sphere','cylinder']
self.rigidBodySizes_ridx = rigidBodySizes_ridx #[[.1],[.03,.09]]
self.rigidBodyToggleVisibility_ridx = rigidBodyToggleVisibility_ridx #[0,1]
# Number of markers to look for
self.owlParamMarkerCount = owlParamMarkerCount #10
self.owlParamFrequ = OWL_MAX_FREQUENCY
self.owlParamInterp = 0
self.owlParamMarkerCondThresh = 100
# Init server
#if (owlInit(self.serverAddress,0) < 0):
if (owlInit(self.serverAddress, OWL_MODE3) < 0): #Adds a lot of lag!
print "Mocap: Could not connect to OWL Server"
exit()
if not owlGetStatus():
print "Mocap: could not enable OWL_STREAMING, owlGetStatus returned: ", owlGetError()
exit();
####################################################################################################################
####################################################################################################################
# Set server parameters
# set default frequency
if( self.owlParamFrequ == 0 ):
self.owlParamFrequ = OWL_MAX_FREQUENCY;
#fi
owlSetFloat(OWL_FREQUENCY, self.owlParamFrequ)
# start streaming
owlSetInteger(OWL_STREAMING, OWL_ENABLE)
owlSetInteger(OWL_INTERPOLATION, self.owlParamInterp)
#owlSetInteger(OWL_POSTPROCESS, OWL_ENABLE) Doesn't work.
# some params: OWL_POSTPROCESS, OWL_SLAVE) < 0):
######################################################################
######################################################################
## Create rigid trackers
for rigidIdx in range(len(self.rigidFileNames_ridx)):
if( self.rigidFileNames_ridx[rigidIdx].find('hmd')>-1):
# If it is the HMD rigid, create a rigidHMD object
self.allRigids_ridx.append(rigidHMD(rigidIdx,
self.phaseSpaceFilePath,
self.rigidFileNames_ridx[rigidIdx],
self.rigidBodyShapes_ridx[rigidIdx],
self.rigidBodySizes_ridx[rigidIdx],
self.rigidBodyToggleVisibility_ridx[rigidIdx]))
self.rigidHMDIdx = rigidIdx
print 'HMD RIGID CREATED'
else:
# A normal rigid body. Create a rigidbody object
self.allRigids_ridx.append(rigidBody(rigidIdx,
self.phaseSpaceFilePath,
self.rigidFileNames_ridx[rigidIdx],
self.rigidBodyShapes_ridx[rigidIdx],
self.rigidBodySizes_ridx[rigidIdx],
self.rigidBodyToggleVisibility_ridx[rigidIdx]))
### Track markers not on rigid bodies
# Fill allRigids_ridx with server data
# Count up markers on rigid bodies, while building a list
numRigidMarkers = 0;
markersUsedInRigid = []
#tempRigids = owlGetRigids();
for rIdx in range(len(self.allRigids_ridx)):
markersUsedInRigid.extend( self.allRigids_ridx[rIdx].markerID_midx );
#print markersUsedInRigid;
if( markersUsedInRigid > self.owlParamMarkerCount ): 'Mocap: More markers used by rigid bodies than in owlParamMarkerCount.'
######################################################################
######################################################################
##Create a tracker object for individual markers
##Note: we must continue indexing trackers at num rigids+1
##Note: we must not track markers already on rigid bodies
#trackerIdx = len(markersUsedInRigid)
trackerIdx = len(self.allRigids_ridx)
owlTrackeri(trackerIdx, OWL_CREATE, OWL_POINT_TRACKER)
markerCount = 0;
for i in xrange(self.owlParamMarkerCount):
# if marker not already used in rigid...
if markersUsedInRigid.count(i) == 0:
owlMarkeri(MARKER(trackerIdx,markerCount), OWL_SET_LED, i)
print 'Mocap: marker: ' + str(i) + ' is unattached, and was added to a tracker.'
markerCount += 1;
else:
pass
#print 'Mocap: marker: ' + str(i) + ' is attached to a rigid body'
owlTracker(trackerIdx, OWL_ENABLE);
######################################################################
######################################################################
if not owlGetStatus():
print "HelmetHandPhaseSpace, could not enable OWL_STREAMING, owlGetStatus returned: ", owlGetError()
exit();
# Setup a timer to update owl server
self.callback(viz.TIMER_EVENT, self.refreshMarkerPositions)
self.starttimer(0,0,viz.FOREVER)
self.turnOn()
### Link mainview to head?
if( self.rigidHMDIdx > -1 ):
self.linkMainViewToHead()
#self.vizNodeLink = viz.link(self.allRigids_ridx[self.rigidHMDIdx].vizNode,viz.MainView)
#self.mainViewLinkedToHead = 1
# end init
######################################################################
######################################################################
def getMarkerPosition(self,trackerID,markerID):
globalMarkerID = rigidXMarkerID( trackerID, markerID );
for mIdx in range(len(self.allMarkers_midx)):
if( self.allMarkers_midx[mIdx].id == globalMarkerID ):
return [self.allMarkers_midx[mIdx].x, self.allMarkers_midx[mIdx].y, self.allMarkers_midx[mIdx].z]
print 'Marker not visible'
def returnPointerToRigid(self,fileName):
# Accepts partial filenames, such as 'hmd' or 'paddle'
# Will return the first match found.
for rigidIdx in range(len(self.rigidFileNames_ridx)):
if( self.rigidFileNames_ridx[rigidIdx].find(fileName) > -1 ):
#if( self.rigidFileNames_ridx[rigidIdx] == fileName):
return self.allRigids_ridx[rigidIdx]
print 'returnPointerToRigid: Could not find ' + fileName
return 0
def checkForRigid(self,fileName):
return( self.returnPointerToRigid(fileName) )
def __del__(self):
self.quit()
def turnOff(self):
self.off = 1
self.setEnabled(False)
if self.mainViewLinkedToHead == 1:
self.mainViewLinkedToHead = 0;
self.allRigids_ridx[self.rigidHMDIdx].vizNodeLink.disable()
def turnOn(self):
self.off = 0
self.setEnabled(True)
def isOn(self):
return not self.off
def quit(self):
print "Disconnecting"
owlDone()
def getOutput(self):
return ( ' PhaseSpace: ' + `viz.MainView.getPosition()` + ' ' + `viz.MainView.getQuat()` );
#fed
def refreshMarkerPositions( self, num ):
if (num == 0):
# These are bools for whether to look for a rigid body
numMarkersSeenThisRound = 1;
markersSeen = [];
#self.allMarkers_midx = []
# There is a buffer of rigidsSeen and markersSeen
# Empty this buffer and use latest information.
while numMarkersSeenThisRound:
markersSeen = owlGetMarkers();
numMarkersSeenThisRound = len( markersSeen );
numMarkersSeenLastRound = len( self.allMarkers_midx );
if( numMarkersSeenThisRound > 0 ):
tempMarkerVector = [];
if( numMarkersSeenLastRound == 0 ):
# The first time around, populate self.allMarkers
self.allMarkers_midx = markersSeen;
print ('mocap.refreshMarkerPositions: Getting marker data for first time.');
else:
for idx in range( 0, numMarkersSeenThisRound, 1 ):
#self.allMarkers_midx = markersSeen;
currentMarkersCondition = markersSeen[idx].cond;
# Run a quality check!
if( currentMarkersCondition > 0 ):
# If the marker was seen
# The highter the .cond, the poorer the track quality
if( currentMarkersCondition < self.owlParamMarkerCondThresh ):
self.allMarkers_midx = markersSeen; # Update the marker position
#fi
#rof
#fi
if( len(tempMarkerVector) ):
self.allMarkers_midx = tempMarkerVector; # Testing this. Not usre if it will work.
#fi
#fi
#elihw
rigidsSeen = [];
numRigidSeen = 1;
while numRigidSeen:
rigidsSeen = owlGetRigids()
numRigidSeen = len(rigidsSeen);
if( numRigidSeen ):
#rigidTempContainer = rigidsSeen;
for rIdx in range(0,len(rigidsSeen)):
if( rigidsSeen[rIdx].cond > 0 and
rigidsSeen[rIdx].cond < self.owlParamMarkerCondThresh ):
self.updateRigidBody(self.allRigids_ridx[rIdx],rigidsSeen[rIdx])
#print str( rigidsSeen[rIdx].cond )
#if( rigidsSeen[rIdx].cond < self.owlParamMarkerCondThresh ):
# self.updateRigidBody(self.allRigids_ridx[rIdx],rigidsSeen[rIdx])
#else: print 'Mocap.refreshMarkerPositions(): Rigid body cond below thresh'
# if self.isOn() and rigidTempContainer:
# self.updateRigidBody(self.allRigids_ridx[0])
else:
print "HelmetHandPhaseSpace.__refreshMarkerPositions__(), YIKES, Unknown num(",num,")"
def updateRigidBody(self,rigidBodyPointer,serverData):
# Update server data.
rigidBodyPointer.serverData = serverData;
pos = [ serverData.pose[0]*self.scale[0] + self.origin[0],
serverData.pose[1]*self.scale[1] + self.origin[1],
-serverData.pose[2]*self.scale[2] + self.origin[2] ];
rot = [ serverData.pose[4], serverData.pose[5], -serverData.pose[6], -serverData.pose[3] ];
# This updates position.
rigidBodyPointer.updateVizNode( pos, rot );
def resetRigid( self, fileName ):
rigidBody = self.returnPointerToRigid( fileName );
if( rigidBody ):
rigidBody.resetRigid( self.allMarkers_midx );
else:
print ('Error: Rigid body not initialized');
#fi
#fed
def saveRigid(self,fileName):
rigidBody = self.returnPointerToRigid(fileName)
if(rigidBody):
rigidBody.saveNewDefaults()
else: print 'Error: Rigid body not initialized'
def rotateRigid(self,fileName,rotateByDegs_XYZ):
rigidBody = self.returnPointerToRigid(fileName)
if(rigidBody):
rigidBody.rotateRigid(rotateByDegs_XYZ)
else: print 'Error: Rigid body not initialized'
def toggleRigidVisibility(self,fileName):
rigidBody = self.returnPointerToRigid(fileName)
if(rigidBody):
rigidBody.toggleVisibility()
else: print 'Rigid body not initialized'
def toggleCalibrationCylinders(self):
hmdRigid = self.returnPointerToRigid('hmd')
if( hmdRigid ):
print 'Toggling eye probes'
hmdRigid.toggleCalibrationCylinders()
else:
print 'No HMD rigid body'