tesscross.html


001| '''Tesscross - An "Unfolded" Tesseract.
001| 
001| A tesscross has 36 vertices, 68 edges, 34 faces, 8 cubes.
001| (A tesseract has 16 vertices, 32 edges, 24 faces, 8 cubes.)
001| 
001| To enable rotation, the tesscross is implemented as 8 cubes,
001| which means: 64 vertices, 96 edges, and 48 faces!
001| 
001| Points are labeled {corner}{cube} where corner is [a-h] and cube is [1-8].
001| Corner labels follow usual convention: a=[-1,+1,-1],.. h=[-1,-1,+1].
001| Cube labels: 1=outer, 2=inner, 3-4=WX, 5-6=WY, 7-8=WZ
001| 
001| classes:
001|     class Tesscross (object)
001| 
001| Developer@Sonnack.com
001| September 2018
001| '''

002| ####################################################################################################
003| from math import sqrt, sin,cos, asin,acos, pi,degrees,radians
004| from colors import *
005| from vector import vector, quaternion, V0,VX,VY,VZ
006| from logger import logger, info, debug, trace
007| ####################################################################################################
008| Log = logger('CROSS')
009| 
010| MakePointName = lambda pn,cx: '@%s%d' % (pn,cx+1)
011| 
012| IndexToPointName = lambda ix: '@%s%d' % ('abcdefgh'[ix % 8], (ix//8)+1)
013| PointNameToIndex = lambda pn: (ord(pn[1].lower())-ord('a'), int(pn[2])-1)
014| 
015| Funct = lambda a:abs(sin(radians(a % 90)))
016| 
017| ColorCode = {'X':1, 'Y':2, 'Z':3, 'W':4}
018| 
019| TesscrossVertices = (
020|     vector(-1,+1,-1), vector(+1,+1,-1), vector(+1,-1,-1), vector(-1,-1,-1),
021|     vector(-1,+1,+1), vector(+1,+1,+1), vector(+1,-1,+1), vector(-1,-1,+1),
022| )
023| TesscrossCubeVectors = (
024|     vector(+4,0,0), vector(0,0,0),
025|     vector(+2,0,0), vector(-2,0,0),
026|     vector(0,+2,0), vector(0,-2,0),
027|     vector(0,0,+2), vector(0,0,-2),
028| )
029| 
030| TesscrossEdges = (
031|     ('a', 'b', 'X'), ('d', 'c', 'X'), ('e', 'f', 'X'), ('h', 'g', 'X'),
032|     ('a', 'd', 'Y'), ('b', 'c', 'Y'), ('e', 'h', 'Y'), ('f', 'g', 'Y'),
033|     ('a', 'e', 'Z'), ('b', 'f', 'Z'), ('c', 'g', 'Z'), ('d', 'h', 'Z'),
034| 
035| )
036| TesscrossFaces = (
037|     ('a','d','h','e', 'X'), ('b','c','g','f', 'X'),
038|     ('a','b','f','e', 'Y'), ('c','d','h','g', 'Y'),
039|     ('a','b','c','d', 'Z'), ('e','f','g','h', 'Z'),
040| )
041| 
042| TesscrossXChains = {
043|     'x1':(('@a4','@b4',-1), ('@a2','@b2',-1), ('@a3','@b3',-1), ('@a1','@b1',-1)), # 0
044|     'x2':(('@d4','@c4',-1), ('@d2','@c2',-1), ('@d3','@c3',-1), ('@d1','@c1',-1)), # 1
045|     'x3':(('@e4','@f4',-1), ('@e2','@f2',-1), ('@e3','@f3',-1), ('@e1','@f1',-1)), # 2
046|     'x4':(('@h4','@g4',-1), ('@h2','@g2',-1), ('@h3','@g3',-1), ('@h1','@g1',-1)), # 3
047| 
048|     'y1':(('@a5','@d5',-1), ('@d5','@c5',-1), ('@c5','@b5',+1), ('@b5','@a5',0)), # 4
049|     'y2':(('@e5','@h5',-1), ('@h5','@g5',-1), ('@g5','@f5',+1), ('@f5','@e5',0)), # 5
050|     'y3':(('@a6','@b6',-1), ('@b6','@c6',-1), ('@c6','@d6',+1), ('@d6','@a6',0)), # 6
051|     'y4':(('@e6','@f6',-1), ('@f6','@g6',-1), ('@g6','@h6',+1), ('@h6','@e6',0)), # 7
052| 
053|     'z1':(('@a7','@b7',-1), ('@b7','@f7',-1), ('@f7','@e7',+1), ('@e7','@a7',0)), # 8
054|     'z2':(('@d7','@c7',-1), ('@c7','@g7',-1), ('@g7','@h7',+1), ('@h7','@d7',0)), # 9
055|     'z3':(('@a8','@e8',-1), ('@e8','@f8',-1), ('@f8','@b8',+1), ('@b8','@a8',0)), # 10
056|     'z4':(('@d8','@h8',-1), ('@h8','@g8',-1), ('@g8','@c8',+1), ('@c8','@d8',0)), # 11
057| 
058|     'w0':(('@b1','@b1',+2), ('@c1','@c1',+2), ('@f1','@f1',+2), ('@g1','@g1',+2)), # 12
059| }
060| 
061| TesscrossChainIndex = (
062|     #FIXME -- chain index 4...
063|     ('x1',3), ('x1',0), ('x2',0), ('x2',3), ('x3',3), ('x3',0), ('x4',0), ('x4',3), # c1 outer
064|     ('x1',1), ('x1',2), ('x2',2), ('x2',1), ('x3',1), ('x3',2), ('x4',2), ('x4',1), # c2 inner
065|     ('x1',2), ('x1',3), ('x2',3), ('x2',2), ('x3',2), ('x3',3), ('x4',3), ('x4',2), # c3 +X
066|     ('x1',0), ('x1',1), ('x2',1), ('x2',0), ('x3',0), ('x3',1), ('x4',1), ('x4',0), # c4 -X
067|     ('y1',0), ('y1',3), ('y1',2), ('y1',1), ('y2',0), ('y2',3), ('y2',2), ('y2',1), # c5 +Y
068|     ('y3',0), ('y3',1), ('y3',2), ('y3',3), ('y4',0), ('y4',1), ('y4',2), ('y4',3), # c6 -Y
069|     ('z1',0), ('z1',1), ('z2',1), ('z2',0), ('z1',3), ('z1',2), ('z2',2), ('z2',3), # c7 +Z
070|     ('z3',0), ('z3',3), ('z4',3), ('z4',0), ('z3',1), ('z3',2), ('z4',2), ('z4',1), # c8 -Z
071| )
072| 
073| 
074| ##~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~##
075| class Point (object):
076|     '''Tesscross Point.'''
077|     def __init__ (self, ix):
078|         self.ix = ix
079|         self.cx = ix // 8
080|         self.px = ix % 8
081|         self.name = IndexToPointName(self.ix)
082|         self.label = 'ABCDEFGH'[self.px]
083|         self.pt = TesscrossVertices[self.px] + TesscrossCubeVectors[self.cx]
084|         self.po = TesscrossVertices[self.px] + TesscrossCubeVectors[self.cx]
085|         self.chain = TesscrossChainIndex[ix][0]
086|         self.chainindex = TesscrossChainIndex[ix][1]
087|         self.mask = -1
088|         self.color = 'normal' if ix < 36 else 'hidden'
089| 
090|     def __call__ (self): return self.pt
091|     def __str__ (self): return '[%d.%d] "%s" %s' % (self.cx,self.px, self.label, self.pt)
092| 
093| 
094| ##~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~##
095| class Tesscross (object):
096|     '''Unfolded Tesseract class. Rotates! Standalone (not a Shape3D sub-class). 36 vertices, 68 edges, 34 faces.'''
097| 
098|     def transform (self, cb_function):
099|         for p in self.points:
100|             p.pt = cb_function(p.pt)
101| 
102|     def boost (self, vect):
103|         self.cp = self.cp + vect
104|         self.transform(lambda p: p + vect)
105| 
106|     def scale (self, percentage):
107|         self.transform(lambda p: p * percentage)
108| 
109|     def rotate (self, quat):
110|         qs = [quaternion(0, vec=p) for p in self()]
111|         rs = [quat * q * quat.recip() for q in qs]
112|         for p,r in zip(self,rs):
113|             p.pt = r.vector()
114| 
115|     def rotate_x (self, angle):
116|         self.rot = angle
117|         am = Funct(angle)
118|         ax = int((angle % 360) / 90)
119|         cxs = [ax, ((ax+1) % 4), ((ax+2) % 4), ((ax+3) % 4)]
120|         Log.trace()
121|         Log.trace('Rotate-X-Axis:')
122|         Log.trace('angle=%.3f' % angle)
123|         Log.trace('am=%.3f' % am)
124|         Log.trace('ax=%d' % ax)
125|         for p in self.points:
126|             cx = cxs[p.chainindex]
127|             chain = self.chains[p.chain][cx]
128|             #FIXME Kludge!!...
129|             if p.chain.startswith('x') and (p.label in 'BCFG') and ((angle % 90)==0):
130|                 p.pt = self.chains[p.chain][3][0] + self.chains[p.chain][3][1]
131|             else:
132|                 p.pt = chain[0] + (chain[1] * am)
133| 
134|     def draw (self, im, **kwargs):
135|         lx = kwargs['lx'] if 'lx' in kwargs else self._lx
136|         lc = kwargs['lc'] if 'lc' in kwargs else self._lc
137|         lw = kwargs['lw'] if 'lw' in kwargs else self._lw
138|         px = kwargs['px'] if 'px' in kwargs else self._px
139|         pc = kwargs['pc'] if 'pc' in kwargs else self._pc
140|         pw = kwargs['pw'] if 'pw' in kwargs else self._pw
141|         ax = kwargs['ax'] if 'ax' in kwargs else self._ax
142|         ac = kwargs['ac'] if 'ac' in kwargs else self._ac
143|         am = kwargs['am'] if 'am' in kwargs else self._am
144|         mx = kwargs['mx'] if 'mx' in kwargs else self._mx
145|         mc = kwargs['mc'] if 'mc' in kwargs else self._mc
146|         # Points...
147|         Log.trace()
148|         Log.trace('DRAW')
149|         if px:
150|             if mx:
151|                 im.circle(self.points[0].pt, pw, mc,mc)
152|             else:
153|                 for pt in self._gather_points():
154|                     im.circle(pt, pw, pc,pc)
155|         # Lines...
156|         if lx:
157|             for p0,p1,cc in list(self._gather_lines().values()):
158|                 im.line(p0, p1, c=lc, lw=lw)
159|         # Labels...
160|         if ax:
161|             for p in list(self._gather_labels().values()):
162|                 im.text(p.pt*am, p.name[1:], fx=2, c=ac, bg=ColorWhite)
163| 
164|     def writepov (self, fp, wr_vertices=True, wr_edges=True, wr_faces=True, wr_cp=True):
165|         fp.write('// %s\n' % self.name)
166|         # Points...
167|         if wr_vertices:
168|             for pt in self._gather_points():
169|                 fp.write('1,<%.8f,%.8f,%.8f>,\n' % pt)
170|             fp.write('0.0,\n')
171|         # Lines...
172|         if wr_edges:
173|             for p0,p1,lc in list(self._gather_lines().values()):
174|                 cc = ColorCode[lc]
175|                 fp.write('2,<%.8f,%.8f,%.8f>,<%.8f,%.8f,%.8f>,%d,\n' % tuple(list(p0)+list(p1)+[cc]))
176|             fp.write('0.0,\n')
177|         # Faces...
178|         if wr_faces:
179|             for cx in range(8):
180|                 for face in TesscrossFaces:
181|                     fp.write('3,')
182|                     for pn in face[0:4]:
183|                         p = getattr(self,MakePointName(pn,cx))
184|                         fp.write('<%.8f,%.8f,%.8f>,' % p)
185|                     fp.write('\n')
186|             fp.write('0.0,\n')
187|         if wr_cp:
188|             fp.write('4,<%.8f,%.8f,%.8f>,\n' % self.cp)
189|         fp.write('0.0\n')
190| 
191|     def list_points (self, cb_function=print):
192|         for p in self:
193|             cb_function(str(p))
194| 
195|     def _gather_points (self):
196|         '''Return a collection of unique points.'''
197|         pts = {}
198|         for p in self.points: pts[p.pt] = p
199|         Log.trace('Points to draw: %d' % len(pts))
200|         return pts
201| 
202|     def _gather_lines (self):
203|         '''Return a collection of unique lines.'''
204|         lns = {}
205|         for cx in range(8):
206|             for edge in TesscrossEdges:
207|                 pa,pb = MakePointName(edge[0],cx), MakePointName(edge[1],cx)
208|                 p0,p1 = getattr(self,pa), getattr(self,pb)
209|                 #FIXME UGLY CODE!
210|                 if 4.0 < (p1-p0).magnitude():
211|                     v0 = vector(int(+5.0), p0.y, p0.z)
212|                     v1 = vector(int(-3.0), p1.y, p1.z)
213|                     lns[(p0, v0)] = (p0, v0, edge[2])
214|                     lns[(v1, p1)] = (v1, p1, edge[2])
215|                     continue
216|                 lns[(p0, p1)] = (p0, p1, edge[2])
217|         Log.trace('Lines to draw: %d' % len(lns))
218|         return lns
219| 
220|     def _gather_labels (self):
221|         '''Return a collection of unique labels.'''
222|         ps = {}
223|         for p in reversed(self.points):
224|             ps[p.pt] = p
225|         Log.trace('Labels to draw: %d' % len(ps))
226|         return ps
227| 
228|     def __len__ (self): return len(self.points)
229|     def __iter__ (self): return iter(self.points)
230| 
231|     def __getitem__ (self, index): return self.points[index]
232|     def __setitem__ (self, index, value): self.points[index] = value
233| 
234|     def __delitem__ (self, index): raise RuntimeError('Deleting tesseract points is not allowed!')
235| 
236|     def __call__ (self, ix=None):
237|         return self.points[ix].pt if ix else [p.pt for p in self]
238| 
239|     def __getattr__ (self, name):
240|         if name.startswith('@'):
241|             px,cx = PointNameToIndex(name)
242|             return self.points[(cx*8)+px].pt
243|         # Who?
244|         raise AttributeError('No such Attribute: "%s"' % name)
245| 
246|     def __setattr__ (self, name, value):
247|         if name.startswith('@'):
248|             px,cx = PointNameToIndex(name)
249|             self.points[(cx*8)+px].pt = value
250|             return
251|         super(Tesscross,self).__setattr__(name, value)
252| 
253|     def __str__ (self):
254|         s = 'tesscross[%s]: @%s, a=%.2f'
255|         t = (self.name, self.cp, self.rot)
256|         return s % t
257| 
258|     def __repr__ (self):
259|         s = '{tesscross:{name:"%s", cp:%s, rot:%.2f}}'
260|         t = (self.name, self.cp, self.rot)
261|         return s % t
262| 
263|     def __init__ (self, **kwargs):
264|         self.name = 'Tesscross'
265|         self.cp = kwargs['cp'] if 'cp' in kwargs else vector(0,0,0)
266|         self.rot = kwargs['rot'] if 'rot' in kwargs else 0.0
267|         self._lx = kwargs['lx'] if 'lx' in kwargs else True
268|         self._lc = kwargs['lc'] if 'lc' in kwargs else ColorBlue
269|         self._lw = kwargs['lw'] if 'lw' in kwargs else 2
270|         self._px = kwargs['px'] if 'px' in kwargs else True
271|         self._pc = kwargs['pc'] if 'pc' in kwargs else ColorDarkBlue
272|         self._pw = kwargs['pw'] if 'pw' in kwargs else 3
273|         self._ax = kwargs['ax'] if 'ax' in kwargs else True
274|         self._ac = kwargs['ac'] if 'ac' in kwargs else ColorBlack
275|         self._am = kwargs['am'] if 'am' in kwargs else 1.1
276|         self._mx = kwargs['mx'] if 'mx' in kwargs else False
277|         self._mc = kwargs['mc'] if 'mc' in kwargs else ColorBlack
278|         if self.cp.magnitude():
279|             self.transform(lambda p: p + self.cp)
280|         # Generate vertices...
281|         Log.trace()
282|         Log.trace('Generating Points (8 cubes, 8 points each)...')
283|         self.points = []
284|         for ix in range(64):
285|             p = Point(ix)
286|             self.points.append(p)
287|             Log.trace(p)
288|         # Generate rotation chains...
289|         Log.trace()
290|         Log.trace('Generating Chains...')
291|         self.chains = {}
292|         for cn in sorted(TesscrossXChains):
293|             chain = TesscrossXChains[cn]
294|             Log.trace('Chain: "%s"' % cn)
295|             self.chains[cn] = []
296|             for link in chain:
297|                 p0 = getattr(self,link[0])
298|                 p1 = getattr(self,link[1])
299|                 t = (p0, p1-p0, link[2])
300|                 self.chains[cn].append(t)
301|                 Log.trace('> (%s, %s, %s)' % t)
302| 
303| 
304| ####################################################################################################
305| '''eof'''
306|