##############################################################################################################################################################################''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' VIZARD/PYTHON SCRIPT FOR IMMERSIVE 3D EXPERIMENTS. ALL RIGHTS RESERVED YOSHIOKA LAB, CHIBA UNIVERSITY, JAPAN.
This Script Based on using the following equipments. - WorldViz Vizard 7.0 over '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
#######################################################################################
import vizimport vizactimport vizshapeimport viztrackerimport vizproximityimport randomimport mathimport csvimport datetimeimport vizinputimport timeimport viztaskimport vizmatimport projectorimport sysimport vizcamimport vizfximport vizconnectimport vizinfo
viz.go()viz.setMultiSample(4)viz.fov(80)
viz.MainView.setPosition([-0.5,1.5,0.5])viz.MainView.setEuler([-90,0,0])#viz.MainView.collision(viz.ON)
# Add a white directional light pointing down light = vizfx.addDirectionalLight(euler=(0,90,0), color=viz.WHITE)
###########VIVE###################import steamvr### Setup SteamVR HMD#hmd = steamvr.HMD()#if not hmd.getSensor():# sys.exit('SteamVR HMD not detected')### Setup navigation node and link to main view#navigationNode = viz.addGroup()#viewLink = viz.link(navigationNode, viz.MainView)#viewLink.preMultLinkable(hmd.getSensor())
################################## グローバル変数一覧 #####################################POS_HEAD = [0] #頭位置[x,y,z]POS_HEAD[0] = 0ORI_HEAD = [0] #頭角度[r,p,y]ORI_HEAD[0] = 0
#print POS_HEAD
reset_time = [0] #時刻リセット用変数reset_time[0] = 0
flagLogging = [0] #データ記録フラグflagLogging[0] = False
###############データ出力の準備 #############EVENT_TIMER_ID = 1 #イベントチェックタイマーIDEVENT_TIMER_PERIOD = 0.1 #チェック間隔[s]PROCESS_TIMER_ID = 2 #データ保存タイマーIDPROCESS_TIMER_PERIOD = 0.016666666 #データ保存間隔[s] ≒ 60Hzoutfile = None #出力ファイルオブジェクト(内部用)outcsv = None #CSVフォーマットオブジェクト(内部用)start_time = time.time() #経過時間(内部用)
#subject = 1 #被験者識別番号、実験開始前に被験者毎に設定する##FileName='tanabe_experiment'+datetime.datetime.today().strftime("%Y%m%d")+'_Sub'+str(subject)+'.txt'#Header ='Subject'+'\t'+'Lap_time'+'\t'#Header +='PosX'+'\t'+'PosY'+'\t'+'PosZ'+'\t'+'Yaw'+'\t'+'Pitch'+'\t'+'Roll'+'\n'#expelimental_data = open(FileName,'a') #expelimental_data.write(Header)
################### 左上に検証用サブウィンドウを作成 ####################UpperLeftWindow = viz.addWindow(pos=(0,1.0),size=(0.25,0.25))UpperLeftWindow.visible(0,viz.SCREEN)BirdView = viz.addView() #Create a new viewpointUpperLeftWindow.setView(BirdView) #Attach the bird's eye view to the upper left windowBirdView.setPosition([-2,6,2]) #Move the view above the center of the roomBirdView.setEuler([90,90,90]) #Rotate the view so that it looks downUpperLeftWindow.fov(60)
#MainViewのポジションを赤いConeにリンクさせるview = viz.MainViewCone = vizshape.addCone(radius=0.1,height=0.3, axis=vizshape.AXIS_Z,color = viz.RED)Cone.renderOnlyToWindows([UpperLeftWindow])viz.link(view,Cone)
################# 軌跡レンダ ################viz.startLayer(viz.LINE_STRIP)viz.vertexColor(viz.YELLOW)lines = viz.endLayer(parent=viz.ORTHO,scene=UpperLeftWindow)
lines.dynamic()
def UpdatePath():
# Get main view position in bird eye window pixel coordinates x,y,z = UpperLeftWindow.worldToScreen(viz.MainView.getPosition(),mode=viz.WINDOW_PIXELS)
# Get position of last line vertex lx,ly,lz = lines.getVertex(-1)
# Add new vertex if current position is different from last position if x != lx or y != ly: lines.addVertex([x,y,0.0])
vizact.ontimer(0,UpdatePath)
##スペースキーで軌跡削除#vizact.onkeydown('',lines.clearVertices)
################# 地面の生成 ################ground = viz.addChild('ground.osgb')ground.setPosition([0,0,0])
################# 迷路の生成 ################wall1 = vizshape.addBox(size = [4,2,0.05])wall2 = vizshape.addBox(size = [0.05,2,4])wall3 = vizshape.addBox(size = [0.05,2,4])wall4 = vizshape.addBox(size = [4,2,0.05])wall5 = vizshape.addBox(size = [3,2,0.05])wall6 = vizshape.addBox(size = [0.05,2,2])wall7 = vizshape.addBox(size = [2,2,0.05])wall8 = vizshape.addBox(size = [1,2,0.05])wall9 = vizshape.addBox(size = [0.05,2,1])
wall1.setPosition([-2,1,0])wall2.setPosition([0,1,2])wall3.setPosition([-4,1,2])wall4.setPosition([-2,1,4])wall5.setPosition([-1.5,1,1])wall6.setPosition([-1,1,2])wall7.setPosition([-3,1,2])wall8.setPosition([-2.5,1,3])wall9.setPosition([-2,1,3.5])
################# スタート・ゴールの生成 ################start = viz.add('logo.wrl',pos=[-1,1.2,0.5],euler=[-90,0,0],scale=[0.2,0.2,0.2])start.visible(viz.OFF)
goal = viz.add('logo.wrl',pos=[-2.5,1.2,3.5],euler=[90,0,0],scale=[0.2,0.2,0.2])
####################################被験者がエリア内にいる時に矢印ナビゲーションを表示する######################################################矢印を配置##############inport skethupviz.setOption('viz.model.apply_collada_scale',1)arrow_straight1 = viz.add('arrow_straight.dae') arrow_straight1.setPosition([-1.5,0.5,0.5])arrow_straight1.setEuler([-90,0,0])#arrow_straight1.visible(viz.OFF)arrow_straight2 = arrow_straight1.clone(pos=[-0.5,0.5,2.5])arrow_straight2.setEuler([180,0,0])arrow_straight2.visible(viz.OFF)arrow_right1 = viz.add('arrow.dae')arrow_right1.setPosition([-3,0.5,0.5])arrow_right1.setEuler([-90,0,0])arrow_right1.visible(viz.OFF) arrow_right2 = arrow_right1.clone(pos=[-3.5,0.5,1])arrow_right2.visible(viz.OFF)arrow_right3 = arrow_right2.clone(pos=[-3.5,0.5,3])arrow_right3.visible(viz.OFF)
#############センサー追加準備##############Create proximity manager and set debug on. Toggle debug with d keymanager = vizproximity.Manager()manager.setDebug(viz.ON)debugEventHandle = vizact.onkeydown('d',manager.setDebug,viz.TOGGLE)
#Add main viewpoint as proximity targettarget = vizproximity.Target(viz.MainView)manager.addTarget(target)
#############右折用センサーを追加する#############sensor1 = vizproximity.Sensor( vizproximity.RectangleArea([1,1],center=[-2.5,0.5]), None )
#矢印を消すdef EnterR1(e): arrow_right1.visible(viz.ON) #矢印を出すdef ExitR1(e): arrow_right1.visible(viz.OFF)
manager.addSensor(sensor1)manager.onEnter(sensor1, EnterR1)manager.onExit(sensor1, ExitR1)
sensor2 = vizproximity.Sensor( vizproximity.RectangleArea([1,1],center=[-3.5,0.5]), None )
#矢印を消すdef EnterR2(e): arrow_right2.visible(viz.ON) #矢印を出すdef ExitR2(e): arrow_right2.visible(viz.OFF)
manager.addSensor(sensor2)manager.onEnter(sensor2, EnterR2)manager.onExit(sensor2, ExitR2)
sensor3 = vizproximity.Sensor( vizproximity.RectangleArea([1,1],center=[-3.5,2.5]), None )
#矢印を消すdef EnterR3(e): arrow_right3.visible(viz.ON) #矢印を出すdef ExitR3(e): arrow_right3.visible(viz.OFF) manager.addSensor(sensor3)manager.onEnter(sensor3, EnterR3)manager.onExit(sensor3, ExitR3)
#############直進用センサーを追加する#############sensor4 = vizproximity.Sensor( vizproximity.RectangleArea([1,1],center=[-1.5,0.5]), None )
#矢印を消すdef EnterS1(e): arrow_straight1.visible(viz.ON) #矢印を出すdef ExitS1(e): arrow_straight1.visible(viz.OFF)
manager.addSensor(sensor4)manager.onEnter(sensor4, EnterS1)manager.onExit(sensor4, ExitS1)
############################### データ出力 #####################################verts = [ [-1,4], [-1,2], [-3,2], [-3,3], [-2,3], [-2,4] ]sensor_timer = vizproximity.Sensor( vizproximity.PolygonArea(verts,offset=[0,0]), None )
#タイマーリセット#def Enter(e):# route.visible(viz.OFF)# ##タイマーリセット#def Exit(e):# route.visible(viz.ON)# #manager.addSensor(sensor)#manager.onEnter(sensor, Enter)#manager.onExit(sensor, Exit)
#def onTimer(num): # #データを取得し、ファイルへ出力&マスクを移動する# curr_time = time.clock() - reset_time[0]## # POS_HEAD[0] = viz.MainView.getPosition() #頭の位置を取得# ORI_HEAD[0] = viz.MainView.getEuler() #頭の回転角を取得# # data = str(subject)+'\t'+str(round(curr_time,3))+'\t'# data += str(round(POS_HEAD[0][0],3))+'\t'+str(round(POS_HEAD[0][1],3))+'\t'+str(round(POS_HEAD[0][2],3))+'\t'# data += str(round(ORI_HEAD[0][0],3))+'\t'+str(round(ORI_HEAD[0][1],3))+'\t'+str(round(ORI_HEAD[0][2],3))+'\n'# # print data# # expelimental_data.write(data)## prev_time = curr_time##viz.callback(viz.TIMER_EVENT, onTimer)##viz.starttimer(EVENT_TIMER_ID, EVENT_TIMER_PERIOD, viz.PERPETUAL)################################################################################