# coding=utf-8 #必須指定 # 21-02-07 cube3.py # 21-02-09 cube5.py # 21-02-16 p3drect.py v1.01 #!/usr/bin/env python import numpy as np import sys , traceback , os import pdb from collections import deque import math from struct import * import copy import random if sys.version[0] == '2': import Tkinter as tk else: import tkinter as tk import random debug_flag = False #debug_flag = True class Cube(): def __init__(self): argvs = sys.argv argc = len(argvs) if '-m' in argvs: self.manual = True else: self.manual = False if '-d' in argvs: self.div = int( argvs[ argvs.index('-d')+1]) else: self.div = 3 if '-s' in argvs: self.selecthighlight = True else: self.selecthighlight = False self.size = 99.0 # size of dice # self.div = 2 # number of divides self.edge = 2.0 # self.cellsize = int(self.size / self.div) self.inside = int(self.cellsize - 2 * self.edge) # 33 - 2*2 = 29 # self.axval = [ [-50.0,-16.2],[-16.0,16.0],[16.2,50.0] ] self.axval = [] for i in range(self.div): self.axval.append([i*self.cellsize - self.size/2.0, i*self.cellsize - self.size/2.0 + (self.inside - self.edge/10) + self.edge*2]) self.axval[len(self.axval)-1][1]=self.size/2.0 print ("axval= ",self.axval) # for automode self.moveseq = [] self.automode = 1 # 0:stay 1:record 2:playback self.startflag = False self.roll90 = False self.roll90command = False self.autopos = 0 self.make_cube() self.getcoor() self.rm3d = Room3d() # create instance of class rm3d self.rm3d.createroom() # create screen canvas self.rm3d.c0.bind("",self.quit) self.rm3d.c0.bind("",self.initmx) self.rm3d.c0.bind("",self.viewval) self.rm3d.c0.bind("" ,self.keyup) self.rm3d.c0.bind("" ,self.keydown) self.rm3d.c0.bind("" ,self.keyleft) self.rm3d.c0.bind("" ,self.keyright) self.rm3d.c0.bind("",self.selectx) self.rm3d.c0.bind("",self.selecty) self.rm3d.c0.bind("",self.selectz) self.rm3d.c0.bind("",self.select1) self.rm3d.c0.bind("",self.select2) self.rm3d.c0.bind("",self.select3) self.rm3d.c0.bind("",self.rotf) self.rm3d.c0.bind("",self.rotb) self.rm3d.c0.bind("",self.manual_seq_back) self.rm3d.c0.bind("",self.check_coor) self.rm3d.c0.bind("",self.align_coor) self.rm3d.c0.bind("",self.startstop) self.rm3d.c0.bind("",self.roundsw) self.round_flag = True self.s4 = tk.StringVar() self.labelc8a = tk.Label(self.rm3d.c0,textvariable=self.s4, font=("Monospace",12,""),bg='blue',fg='white',justify="left",anchor ='nw') self.labelc8a.grid(row=0,column=0,sticky="wens") self.rm3d.c2.create_window(30,20,window = self.labelc8a,width = 300,anchor ='nw') self.s4.set('[selected box]') if self.manual: pass valtext = "Manual mode\nkey assign:\n1,2,3,x,y,z:selevt boxes\nr,R:rotate\n" else: valtext = "Auto mode\nkey assign:\n , view point height\n , distance\n" self.rm3d.s2.set(valtext) self.rm3d.selecthighlight = self.selecthighlight self.run() def roundsw(self,event=None): # '' if self.round_flag: self.round_flag = False else: self.round_flag = True def startstop(self,event=None): if self.startflag : self.startflag = False else: self.startflag = True def check_coor(self,event=None): self.chkcoor() def align_coor(self,event=None): self.aligncoor() # rotate def rotf(self,event=None): # 'r' if self.manual: dr = 0 self.cursol[3] = dr self.rotcount = 90 self.moveseq.append(copy.deepcopy(self.cursol)) def rotb(self,event=None): # 'R' if self.manual: dr = 1 self.cursol[3] = dr self.rotcount = 90 self.moveseq.append(copy.deepcopy(self.cursol)) # manual sequence back def manual_seq_back(self,event=None): print ("seq remain ",len(self.moveseq)) self.autopos = len(self.moveseq) - 1 if self.rotcount == 0: self.cursol = copy.deepcopy(self.moveseq[self.autopos]) if self.cursol[3] == 1: self.cursol[3] = 0 else: self.cursol[3] = 1 self.selectbox() self.rotcount = 90 if self.autopos <= 0: self.moveseq = [] # self.autopos -= 1 else: self.moveseq.pop(-1) pass # select box def selectx(self,event=None): if self.manual: self.cursol[0] = 0 # self.selectbox() self.printax() def selecty(self,event=None): if self.manual: self.cursol[0] = 1 # self.selectbox() self.printax() def selectz(self,event=None): if self.manual: self.cursol[0] = 2 # self.selectbox() self.printax() def select1(self,event=None): if self.manual: vl = 0 self.vl = vl ly = self.cursol[0] self.cursol[1] = self.axval[vl][0] + self.boxoffset[ly] self.cursol[2] = self.axval[vl][1] + self.boxoffset[ly] self.selectbox() self.printax() def select2(self,event=None): if self.manual: vl = 1 self.vl = vl ly = self.cursol[0] self.cursol[1] = self.axval[vl][0] + self.boxoffset[ly] self.cursol[2] = self.axval[vl][1] + self.boxoffset[ly] self.selectbox() self.printax() def select3(self,event=None): if self.manual: vl = 2 self.vl = vl ly = self.cursol[0] self.cursol[1] = self.axval[vl][0] + self.boxoffset[ly] self.cursol[2] = self.axval[vl][1] + self.boxoffset[ly] self.selectbox() self.printax() def printax(self): axp = ['X','Y','Z'] axv = ['1','2','3','4','5','6','7','8','9','10','11'] self.s4.set(axp[self.cursol[0]]+':'+axv[self.vl]) def rot(self,clist): xx = float(clist[0]) yy = float(clist[1]) zz = float(clist[2]) # rotate in z axis yst = - (np.sin(self.rz) * xx) + (np.cos(self.rz) * yy) xst = + (np.cos(self.rz) * xx) + (np.sin(self.rz) * yy) # x axis rotate yst2 = - np.sin(self.rx)*zz + np.cos(self.rx) * yst zst = + np.cos(self.rx)*zz + np.sin(self.rx) * yst # rotete in y axis xst2 = - np.sin(self.ry)*zst + np.cos(self.ry) * xst zst2 = + np.cos(self.ry)*zst + np.sin(self.ry) * xst return [xst2,yst2,zst2 ] def aligncoor(self): if True: pl = 0 for plane in self.cubeplane: bx = 0 for box in plane: ap = 0 for apec in box[0:4]: for i in range(3): for al in self.qtstart[i]: if (al * 0.99 < apec[i]) and (apec[i] < al * 1.01) and (al != apec[i]): self.cubeplane[pl][bx][ap][i] = copy.deepcopy(al) break if (al * 0.99 > apec[i]) and (apec[i] > al * 1.01) and (al != apec[i]): self.cubeplane[pl][bx][ap][i] = copy.deepcopy(al) break ap += 1 bx += 1 pl += 1 def chkcoor(self): if True: self.qtstart2 = [[],[],[]] self.qtend2 = [] pl = 0 for plane in self.cubeplane: bx = 0 for box in plane: ap = 0 for apec in box[0:4]: for i in range(3): if apec[i] in self.qtstart2[i]: pass else: self.qtstart2[i].append(apec[i]) ap += 1 bx += 1 pl += 1 if debug_flag: for vallist in self.qtstart2: print ("qtstart =",sorted(vallist)) print ("qtend =",self.qtend2) def getcoor(self): if True: self.qtstart = [[],[],[]] self.qtend = [] pl = 0 for plane in self.cubeplane: bx = 0 for box in plane: ap = 0 for apec in box[0:4]: for i in range(3): if apec[i] in self.qtstart[i]: pass else: self.qtstart[i].append(apec[i]) ap += 1 bx += 1 pl += 1 # for vallist in self.qtstart: # print ("qtstart =",sorted(vallist)) # print ("qtend =",self.qtend) def rotbox(self): # incube[ [apec0],[apec1],[apec2],[apec3],[t],[color],[status] ] if 0 < self.rotcount <= 90: # self.curol[0] if self.cursol[0] == 0: if self.cursol[3] == 1: self.rx = self.rotanginc else: self.rx = -self.rotanginc self.ry = 0 self.rz = 0 elif self.cursol[0] == 1: self.rx = 0 if self.cursol[3] == 1: self.ry = self.rotanginc else: self.ry = -self.rotanginc self.rz = 0 elif self.cursol[0] == 2: self.rx = 0 self.ry = 0 if self.cursol[3] == 1: self.rz = self.rotanginc else: self.rz = -self.rotanginc pl = 0 for plane in self.cubeplane: bx = 0 for box in plane: ap = 0 rotst = self.cubeplane[pl][bx][6][0] if rotst == 1: for apec in box[0:4]: apectemp = [apec[0] - self.boxoffset[0],apec[1] - self.boxoffset[1],apec[2] - self.boxoffset[2]] temp2 = self.rot(apectemp[0:3]) temp = [temp2[0]+self.boxoffset[0],temp2[1]+self.boxoffset[1],temp2[2]+self.boxoffset[2]] self.cubeplane[pl][bx][ap][0:3] = copy.deepcopy(temp) pass ap += 1 bx += 1 pl += 1 if self.rotcount == int(self.sp): # on last turn,then align box self.aligncoor() self.chkcoor() self.aligncoor() if self.manual: pass else: self.unselectbox() if self.roll90: self.selectboxall() self.rotcount = 90 self.roll90command = True self.rotcount -= int(self.sp) def rotboxall(self): # incube[ [apec0],[apec1],[apec2],[apec3],[t],[color],[status] ] if 0 < self.rotcount <= 90: # self.curol[0] if True: self.rx = self.rotanginc self.ry = 0 self.rz = 0 pl = 0 for plane in self.cubeplane: bx = 0 for box in plane: ap = 0 rotst = self.cubeplane[pl][bx][6][0] if rotst == 1: for apec in box[0:4]: temp = self.rot(apec[0:3]) self.cubeplane[pl][bx][ap][0:3] = copy.deepcopy(temp) pass ap += 1 bx += 1 pl += 1 if self.rotcount == int(self.sp): # on last turn,then align box self.aligncoor() self.chkcoor() self.aligncoor() self.unselectbox() self.roll90 = False self.roll90command = False self.rotcount -= int(self.sp) def autorun(self): # self.automode 0:stay 1:record 2:playback # self.moveseq if self.automode == 1 and self.startflag: # record if self.rotcount == 0: ly = random.randint(0,2) # 軸 x,y,z vl = random.randint(0,self.div-1) # 層 0,1,2 dr = random.randint(0,1) self.cursol[0] = ly self.cursol[1] = self.axval[vl][0] + self.boxoffset[ly] self.cursol[2] = self.axval[vl][1] + self.boxoffset[ly] self.cursol[3] = dr self.moveseq.append(copy.deepcopy(self.cursol)) if len(self.moveseq) >= 16: self.automode = 2 self.autopos = len(self.moveseq) - 1 self.selectbox() self.rotcount = 90 elif self.automode == 2 and self.startflag: if self.rotcount == 0: self.cursol = copy.deepcopy(self.moveseq[self.autopos]) if self.cursol[3] == 1: self.cursol[3] = 0 else: self.cursol[3] = 1 # print ("playback = ",self.cursol,self.autopos) self.selectbox() self.rotcount = 90 if self.autopos <= 0: self.automode = 1 self.moveseq = [] # self.roll90 = True self.autopos -= 1 def run(self): inc = 01.0 pl = 0 self.rl = 200.0 self.za = -100.0 self.sp = 2.0 self.rotanginc = (np.pi/180.0) * self.sp self.rotdir = 0 # 0:stay 1:inc 2:dec self.runstep = 0 while True: xa = 0.0 xa2 = 0.0 self.rotang = 0.0 while True: self.rm3d.dest = [ 100,100,0] self.rm3d.cposx = np.sin(xa)*self.rl + self.rm3d.dest[0] self.rm3d.cposy = np.cos(xa)*self.rl + self.rm3d.dest[1] self.rm3d.cposz = self.za self.rm3d.cpos = [ self.rm3d.cposx , self.rm3d.cposy , self.rm3d.cposz] hoge = [] # if self.manual: # self.selectbox() if self.roll90command: self.rotboxall() else: self.rotbox() for pl in range(6): doge = self.rm3d.solve(self.cubeplane[pl]) for box in doge: hoge.append(box) doge = self.rm3d.reorder(hoge,[]) boge = self.rm3d.shift2center(doge) self.rm3d.drawwall(boge) # if not self.manual: if self.round_flag: xa2 += (2*np.pi/360.0) xa = xa2 % 2*np.pi if not self.manual: self.runstep += 1 if self.runstep > 360: self.startflag = True if not self.manual: self.autorun() def selectbox(self): # incube[ [apec0],[apec1],[apec2],[apec3],[t],[color],[status] ] self.aligncoor() pl = 0 for plane in self.cubeplane: bx = 0 for box in plane: hogetemp = self.cubeplane[pl][bx][0] if self.cursol[1] <= hogetemp[self.cursol[0]] <= self.cursol[2]: self.cubeplane[pl][bx][6][0] = 1 # print ("select ",self.cubeplane[pl][bx]) # print ("cursol ",self.cursol) else: self.cubeplane[pl][bx][6][0] = 0 bx += 1 pl += 1 def unselectbox(self): # incube[ [apec0],[apec1],[apec2],[apec3],[t],[color],[status] ] self.aligncoor() pl = 0 for plane in self.cubeplane: bx = 0 for box in plane: self.cubeplane[pl][bx][6][0] = 0 bx += 1 pl += 1 def selectboxall(self): # incube[ [apec0],[apec1],[apec2],[apec3],[t],[color],[status] ] self.aligncoor() pl = 0 for plane in self.cubeplane: bx = 0 for box in plane: self.cubeplane[pl][bx][6][0] = 1 bx += 1 pl += 1 def make_cube(self): self.col = [ '#ff0000', '#00ff00', '#0000ff', '#ff00ff', '#ffff00', '#00ffff'] boxoffset = [ 100,100,0] self.boxoffset = boxoffset self.rotcount = 0 self.cursol = [0,-50,-40,0] # [0:x 1:y 2:z , min ,max]] self.stbox = [0,2,4,6] self.stpl = [ 5 ] self.boxst = 0 self.cellsize = int(self.size / self.div) self.inside = int(self.cellsize - 2 * self.edge) if True: self.plane = [] for xi in range(int(self.div)): for yi in range(int(self.div)): x = -self.size/2.0 + self.edge + xi * self.cellsize y = -self.size/2.0 + self.edge + yi * self.cellsize self.plane.append([[x,y],[x+self.inside,y],[x+self.inside,y+self.inside],[x,y+self.inside]]) pass # print ("self.plane ",self.plane) self.cubeplane = [] cc = 0 cellno = 0 for p in range(6): self.cubeplane.append([]) if p == 0: # top cd = 0 for cell in self.plane: self.cubeplane[cc].append([]) for item in cell: self.cubeplane[cc][cd].append([item[0]+boxoffset[0],item[1]+boxoffset[1],-self.size/2.0]) self.cubeplane[cc][cd].append([0]) # t self.cubeplane[cc][cd].append([self.col[p]]) # color self.cubeplane[cc][cd].append([self.boxst]) self.cubeplane[cc][cd].append([cellno]) cd += 1 cellno += 1 elif p == 1: # bottom cd = 0 for cell in self.plane: self.cubeplane[cc].append([]) for item in cell: self.cubeplane[cc][cd].append([item[0]+boxoffset[0],item[1]+boxoffset[1],+self.size/2.0]) self.cubeplane[cc][cd].append([0]) # t self.cubeplane[cc][cd].append([self.col[p]]) # color self.cubeplane[cc][cd].append([self.boxst]) self.cubeplane[cc][cd].append([cellno]) cd += 1 cellno += 1 elif p == 2: # front cd = 0 for cell in self.plane: self.cubeplane[cc].append([]) for item in cell: self.cubeplane[cc][cd].append([item[0]+boxoffset[0],+self.size/2.0+boxoffset[1],item[1]]) self.cubeplane[cc][cd].append([0]) # t self.cubeplane[cc][cd].append([self.col[p]]) # color self.cubeplane[cc][cd].append([self.boxst]) self.cubeplane[cc][cd].append([cellno]) cd += 1 cellno += 1 elif p == 3: # back cd = 0 for cell in self.plane: self.cubeplane[cc].append([]) for item in cell: self.cubeplane[cc][cd].append([item[0]+boxoffset[0],-self.size/2.0+boxoffset[1],item[1]]) self.cubeplane[cc][cd].append([0]) # t self.cubeplane[cc][cd].append([self.col[p]]) # color self.cubeplane[cc][cd].append([self.boxst]) self.cubeplane[cc][cd].append([cellno]) cd += 1 cellno += 1 elif p == 4: # left cd = 0 for cell in self.plane: self.cubeplane[cc].append([]) for item in cell: self.cubeplane[cc][cd].append([+self.size/2.0+boxoffset[0],item[0]+boxoffset[1],item[1]]) self.cubeplane[cc][cd].append([0]) # t self.cubeplane[cc][cd].append([self.col[p]]) # color self.cubeplane[cc][cd].append([self.boxst]) self.cubeplane[cc][cd].append([cellno]) cd += 1 cellno += 1 elif p == 5: # right cd = 0 for cell in self.plane: self.cubeplane[cc].append([]) for item in cell: self.cubeplane[cc][cd].append([-self.size/2.0+boxoffset[0],item[0]+boxoffset[1],item[1]]) self.cubeplane[cc][cd].append([0]) # t self.cubeplane[cc][cd].append([self.col[p]]) # color self.cubeplane[cc][cd].append([self.boxst]) self.cubeplane[cc][cd].append([cellno]) cd += 1 cellno += 1 cc += 1 # print ("cubeplae=",self.cubeplane) # pdb.set_trace() def viewval(self,event=None): self.rm3d.dispwall(self.cubeplane) def keyup(self,event=None): self.za += 10 print ("za=",self.za) def keydown(self,event=None): self.za -= 10 print ("za=",self.za) def keyleft(self,event=None): self.rl += 10 print ("rl=",self.rl) def keyright(self,event=None): self.rl -= 10 print ("rl=",self.rl) def initmx(self,event=None): self.rm3d.initmx = True def quit(self,event=None): print ("quit") self.rm3d.c0.destroy() sys.exit() class Room3d: # 3D screen display module def __init__(self): print ('3D module \n(c)2021 Kodera ippei \n 21-02-23 version 1.02') xwin , ywin = 400,250 self.c0 = tk.Tk() self.c0.title('Rotate cube version 1.02') geo = str(xwin)+'x'+str(ywin)+'+380+150' self.c0.geometry(geo) self.c2 = tk.Canvas(self.c0 , width=xwin - 10 , height=ywin-10,bg='gray') self.c2.place(x=0, y=0) self.c2.create_rectangle(10,10,xwin -10 ,ywin - 10,fill='blue') self.c4 = tk.Toplevel() self.c4.title('Monitor Screen') geo = str(xwin)+'x'+str(ywin)+'+860+150' self.c4.geometry(geo) self.c6 = tk.Canvas(self.c4 , width=xwin - 10 , height=ywin-10,bg='gray') self.c6.place(x=0, y=0) self.c6.create_rectangle(10,10,xwin -10 ,ywin - 10,fill='blue') self.s1 = tk.StringVar() self.s2 = tk.StringVar() self.s3 = tk.StringVar() self.s1.set(sys.version) self.labelc6a = tk.Label(self.c4,textvariable=self.s1,font=("Monospace",10,""),bg='blue',fg='white',justify="left",anchor ='nw') self.labelc6a.grid(row=0,column=0,sticky="wens") self.c6.create_window(30,40,window = self.labelc6a,width = 300,anchor ='nw') self.labelc6b = tk.Label(self.c4,textvariable=self.s2,font=("Monospace",10,""),bg='blue',fg='white',justify="left",anchor ='nw') self.labelc6b.grid(row=0,column=0,sticky="wens") self.c6.create_window(30,80,window = self.labelc6b,width = 300,anchor ='nw') self.labelc6c = tk.Label(self.c4,textvariable=self.s3,font=("Monospace",10,""),bg='blue',fg='white',justify="left",anchor ='nw') self.labelc6c.grid(row=0,column=0,sticky="wens") self.c6.create_window(30,180,window = self.labelc6c,width = 300,anchor ='nw') self.c4.update() self.threth = 1.2 self.initmx = True self.width = 3 self.objval = 0 # self.outer = outer_instance # 親インスタンス self.wx = float(xwin) self.wy = float(ywin) self.first = True self.he = 120.0 self.stepflag = False self.focus = 10 def createroom(self): pass def dispwall(self,dd): for di in dd: print ("---") for di2 in di: print (di2) def shift2center(self,inwall): objmax = [0,0] objmin = [9999,9999] for obj in inwall: for apec in obj[0:4]: if apec[0] > objmax[0]: objmax[0] = apec[0] if apec[0] < objmin[0]: objmin[0] = apec[0] if apec[1] > objmax[1]: objmax[1] = apec[1] if apec[1] < objmin[1]: objmin[1] = apec[1] pass xwidth = objmax[0] - objmin[0] ywidth = objmax[1] - objmin[1] xcenter = objmin[0] + xwidth/2.0 ycenter = objmin[1] + ywidth/2.0 if debug_flag: print ("xwidth %4d, ywidth %4d" % (int(xwidth),int(ywidth)) ) print ("max %4d, max %4d" % (objmax[0],objmax[1]) ) print ("min %4d, min %4d" % (objmin[0],objmin[1]) ) print ("wx %4d, wy %4d" % (self.wx,self.wy) ) # self.wx : size of window # self.wy : size of window if self.initmx: self.mx = self.wx * 0.5/ xwidth self.my = self.wy * 0.5/ ywidth self.xcenter = objmin[0] + xwidth/2.0 self.ycenter = objmin[1] + ywidth/2.0 self.initmx = False outwall = [] cc = 0 for obj in inwall: for apec in obj[0:4]: outwall.append([]) outwall[cc].append( [ ((apec[0]-self.xcenter) * self.mx+self.wx/2.0) , ((apec[1]-self.ycenter) * self.my+self.wy/2.0) , apec[2] ] ) outwall[cc].append([obj[4]]) outwall[cc].append([obj[5]]) outwall[cc].append([obj[6]]) cc += 1 return outwall def drawaux(self,wall): self.c2.delete("aux") objid = [] cc = 0 for obj in wall: if len(obj) == 4: self.c2.create_polygon( obj[0][0] , obj[0][1] , obj[1][0] , obj[1][1] , obj[2][0] , obj[2][1] , obj[3][0] , obj[3][1] , outline="orange",width=self.width,fill ='', tags="wall") elif len(obj) == 6: boxcolor = obj[5][0][0][0] self.c2.create_polygon( obj[0][0] , obj[0][1] , obj[1][0] , obj[1][1] , obj[2][0] , obj[2][1] , obj[3][0] , obj[3][1] , outline="black",width=self.width,fill = boxcolor ,tags="wall") elif len(obj) == 7: boxcolor = obj[5][0][0][0] boxstatus = obj[6][0][0][0] if boxstatus == 0: self.c2.create_polygon( obj[0][0] , obj[0][1] , obj[1][0] , obj[1][1] , obj[2][0] , obj[2][1] , obj[3][0] , obj[3][1] , outline="black",width=self.width,fill = boxcolor ,tags="aux") else: self.c2.create_polygon( obj[0][0] , obj[0][1] , obj[1][0] , obj[1][1] , obj[2][0] , obj[2][1] , obj[3][0] , obj[3][1] , outline="black",width=self.width,fill = boxcolor ,tags="wall") cc += 1 self.c2.update() def drawwall(self,wall): deleted_wall_flag = False # self.c2.delete("wall") self.walllen = len(wall) if self.objval == 0: self.objvalx = 0 else: self.objvalx = self.objval objid = [] cc = 0 for obj in wall[self.objvalx:]: if self.stepflag: print ("t=",obj[4]) raw_input() if len(obj) == 4: self.c2.create_polygon( obj[0][0] , obj[0][1] , obj[1][0] , obj[1][1] , obj[2][0] , obj[2][1] , obj[3][0] , obj[3][1] , outline="orange",width=self.width,fill ='', tags="wall") elif len(obj) == 6: boxcolor = obj[5][0][0][0] self.c2.create_polygon( obj[0][0] , obj[0][1] , obj[1][0] , obj[1][1] , obj[2][0] , obj[2][1] , obj[3][0] , obj[3][1] , outline="black",width=self.width,fill = boxcolor ,tags="wall") elif len(obj) >= 7: if not deleted_wall_flag: self.c2.delete("wall") self.c2.delete("payload") deleted_wall_flag = True boxcolor = obj[5][0][0][0] boxstatus = obj[6][0][0][0] boxavtp = obj[4][0][0] if boxavtp > 0 and boxstatus in [0]: self.c2.create_polygon( obj[0][0] , obj[0][1] , obj[1][0] , obj[1][1] , obj[2][0] , obj[2][1] , obj[3][0] , obj[3][1] , outline="black",width=self.width, fill = boxcolor ,tags="wall") if boxavtp > 0 and boxstatus in [1]: if self.selecthighlight: outlinecolor = "#dddddd" else: outlinecolor = "black" self.c2.create_polygon( obj[0][0] , obj[0][1] , obj[1][0] , obj[1][1] , obj[2][0] , obj[2][1] , obj[3][0] , obj[3][1] , outline=outlinecolor,width=self.width, fill = boxcolor ,tags="wall") # packman elif boxavtp > 0 and boxstatus == 8: self.c2.create_polygon( obj[0][0] , obj[0][1] , obj[1][0] , obj[1][1] , obj[2][0] , obj[2][1] , obj[3][0] , obj[3][1] , outline="black",width=self.width, fill = boxcolor ,tags="wall",smooth=True) # ball elif boxavtp > 0 and boxstatus == 2: self.c2.delete("ball") self.radius = abs(obj[0][0] - obj[2][0]) self.c2.create_oval ( obj[0][0] , obj[0][1] , obj[0][0] + self.radius, obj[0][1] + self.radius , outline="black",width=1, fill = boxcolor ,tags="ball") # sohkoban payload elif boxavtp > 0 and boxstatus == 13: self.c2.create_polygon( obj[0][0] , obj[0][1] , obj[1][0] , obj[1][1] , obj[2][0] , obj[2][1] , obj[3][0] , obj[3][1] , outline="black",width=self.width, fill = boxcolor ,tags="payload") cc += 1 self.c2.update() if debug_flag: print ("drawwall: draw {:3d}".format(cc) ) def reorder(self,inwall,splite): # Hidden surface treatment cc = 0 reo = [] if len(splite) > 0: inwall += splite for obj in inwall: if len(obj) > 3: if 0 < obj[4][0] < self.threth: reo.append(obj) # reo2 = sorted(reo,key=lambda h:h[4][1],reverse=False) reo2 = sorted(reo,key=lambda h:h[4][0],reverse=False) return reo2 def retina(self,pl,ev,ob): # pl : plane[a,b,c,d] 法線ベクトル(a,b,c) ,d # ev : eye[x0,y0,z0] 焦点位置 # ob :object[x1,y1,z1] 物体の位置 # return [x2,y2,z2] mapping to the retina # a,b,c,d = pl[0],pl[1],pl[2],pl[3] x0,y0,z0 = ev[0],ev[1],ev[2] x1,y1,z1 = ob[0],ob[1],ob[2] tap = ( a * (x1 - x0) + b * (y1 - y0) + c * (z1 - z0) ) if tap == 0: return [0,0,0,0] t = (-d -(a * x0 + b * y0 + c * z0) ) / tap if t<0: if debug_flag: print ("retina: minus",t) x2 = x0 + t * (x1 - x0) y2 = y0 + t * (y1 - y0) z2 = z0 + t * (z1 - z0) slinelength = np.sqrt((x1 - x0)**2 + (y1 - y0)**2 + (z1 - z0)**2) treal = (self.pl_lengthxx / slinelength) * t return [x2 , y2 , z2 , slinelength , treal ] def rel_retina(self,pl,point): # 水平ベクトル ph2 = [ -pl[1] , pl[0] , 0 ] ph_length = np.sqrt(ph2[0]**2 + ph2[1]**2 + ph2[2]**2) ph = [ ph2[0]/ph_length , ph2[1]/ph_length , ph2[2]/ph_length ] # 垂直ベクトル if pl[2] < 0: po = -1.0 else: po = 1.0 pv2 = [ pl[0] , pl[1] , -( pl[0]*pl[0] + pl[1]*pl[1] )/pl[2] ] pv_length = np.sqrt(pv2[0]**2 + pv2[1]**2 + pv2[2]**2) * po pv = [ pv2[0]/pv_length , pv2[1]/pv_length , pv2[2]/pv_length ] xd = point[0]*ph[0] + point[1]*ph[1] + point[2]*ph[2] yd = point[0]*pv[0] + point[1]*pv[1] + point[2]*pv[2] return [xd , yd*10.0 , point[3] , point[4]] def subvector(self,k1,k2): k3 = [0,0,0] k3 = [k1[0] - k2[0],k1[1] - k2[1],k1[2] - k2[2]] return k3 def addvector(self,k1,k2): k3 = [0,0,0] k3 = [k1[0] + k2[0] , k1[1] + k2[1] , k1[2] + k2[2] ] return k3 def solve(self,inwalldummy): inwall = copy.deepcopy(inwalldummy) # self.cposx |view point # self.cposy | # self.cposz | # self.outer.artemp direction of view # self.unitx size of one cell # self.unity self.distance = 2.5 # plane1 pl = [ self.cposx - self.dest[0] , self.cposy - self.dest[1] , self.cposz - self.dest[2] , 0] ####################### self.cposx2 = self.cposx - self.dest[0] self.cposy2 = self.cposy - self.dest[1] self.cposz2 = self.cposz - self.dest[2] self.cpos2 = [ self.cposx2,self.cposy2,self.cposz2 ] self.dest2 = [0,0,0] ####################### # pl_length = np.sqrt( (self.cposx - self.dest[0])**2 + # (self.cposy - self.dest[1])**2 + # (self.cposz - self.dest[2])**2) pl_length = np.sqrt(self.cposx2**2 + self.cposy2**2 + self.cposz2**2) self.pl_lengthxx = pl_length plunit = [pl[0]/pl_length , pl[1]/pl_length , pl[2]/pl_length] focus = self.focus plpoint = [ self.cposx - plunit[0]*focus - self.dest[0], self.cposy - plunit[1]*focus - self.dest[1], self.cposz - plunit[2]*focus - self.dest[2] ] d2 = -(pl[0]*plpoint[0] + pl[1]*plpoint[1] + pl[2]*plpoint[2]) pl[3] = d2 ev = [self.cposx2 , self.cposy2 , self.cposz2] # get retina center self.retina_center = self.retina(pl,self.cpos2,self.dest2) if debug_flag: print ("dest =",self.dest ) print ("cpos =",self.cpos ) print (" ",(th) ) print ("(a,b,c)=",pl ) outwall = [] cc = 0 for obj in inwall: outwall.append([]) # avt = 0 avtp = 0 avtplist = [] for i in range(4): apec = obj[ i%4 ] apec2 = self.subvector(apec , self.dest) tpoint = self.retina(pl,ev,apec2) temp = self.rel_retina(pl,tpoint) outwall[cc].append( temp ) avtplist.append(temp[3]) # outwall[cc].append([ min(avtplist), np.average(avtplist) ]) # t outwall[cc].append([ min(avtplist), 0 ]) # t if avtp < 0: pass outwall[cc].append([['#ff0000']]) # color outwall[cc].append([obj[6]]) # status else: outwall[cc].append([obj[5]]) # color outwall[cc].append([obj[6]]) # status cc += 1 return outwall def solve_pack(self,inwalldummy,packoffset): inwall = copy.deepcopy(inwalldummy) # self.cposx |view point # self.cposy | # self.cposz | # self.outer.artemp direction of view # self.unitx size of one cell # self.unity self.distance = 2.5 # plane1 pl = [ self.cposx - self.dest[0] , self.cposy - self.dest[1] , self.cposz - self.dest[2] , 0] ####################### pl_length = np.sqrt( (self.cposx - self.dest[0])**2 + (self.cposy - self.dest[1])**2 + (self.cposz - self.dest[2])**2) self.pl_lengthxx = pl_length plunit = [pl[0]/pl_length , pl[1]/pl_length , pl[2]/pl_length] focus = self.focus plpoint = [ self.cposx - plunit[0]*focus - self.dest[0], self.cposy - plunit[1]*focus - self.dest[1], self.cposz - plunit[2]*focus - self.dest[2] ] d2 = -(pl[0]*plpoint[0] + pl[1]*plpoint[1] + pl[2]*plpoint[2]) ####################### self.cposx2 = self.cposx - self.dest[0] self.cposy2 = self.cposy - self.dest[1] self.cposz2 = self.cposz - self.dest[2] self.cpos2 = [ self.cposx2,self.cposy2,self.cposz2 ] self.dest2 = [0,0,0] pl[3] = d2 ev = [self.cposx2 , self.cposy2 , self.cposz2] #print pl # get retina center self.retina_center = self.retina(pl,self.cpos2,self.dest2) if debug_flag: print ("dest =",self.dest ) print ("cpos =",self.cpos ) print (" ",(th) ) print ("(a,b,c)=",pl ) outwall = [] cc = 0 for obj in inwall: outwall.append([]) avt = 0 avtp = 0 avtplist = [] for i in range(4): apec = obj[ i%4 ] apec2 = self.addvector(apec,packoffset) apec3 = self.subvector(apec2 , self.dest) tpoint = self.retina(pl,ev,apec3) temp = self.rel_retina(pl,tpoint) outwall[cc].append( temp ) avtplist.append(temp[3]) outwall[cc].append([ min(avtplist), np.average(avtplist) ]) # t if avtp < 0: outwall[cc].append([['#ff0000']]) # color outwall[cc].append([obj[6]]) # status else: outwall[cc].append([obj[5]]) # color outwall[cc].append([obj[6]]) # status cc += 1 return outwall def HelloWorld(): print ("Hello World!") ra = Cube() tk.mainloop() if __name__=="__main__": HelloWorld()