cubic.html


001| ####################################################################################################
002| from sys import argv, stdout, exc_info
003| from traceback import extract_tb
004| from os import path
005| from math import sqrt
006| ####################################################################################################
007| 
008| 
009| ##~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~##
010| class point (tuple):
011| 
012|     def __new__ (cls, x=0.0, y=0.0, z=0.0, w=0.0):
013|         '''Make a new point instance.'''
014|         return super().__new__(cls, [float(x),float(y),float(z),float(w)])
015| 
016|     def __init__ (self, x=0.0, y=0.0, z=0.0, w=0.0):
017|         '''Initialize new point instance.'''
018|         super().__init__()
019| 
020|     def __getattr__ (self, name):
021|         '''Read-only Properties.'''
022|         if name == 'x': return self[0] if 0<len(self) else None
023|         if name == 'y': return self[1] if 1<len(self) else None
024|         if name == 'z': return self[2] if 2<len(self) else None
025|         if name == 'w': return self[3] if 3<len(self) else None
026| 
027|     def __setattr__ (self, name, value):
028|         '''Read-only Properties!'''
029|         if name in ['x', 'y', 'z','w']:
030|             raise AttributeError('Read-Only Attribute: "%s"' % name)
031|         super().__setattr__(name, value)
032| 
033|     def __bool__ (self):
034|         return sum(self) == 0.0
035| 
036|     def __add__ (self, other):
037|         if isinstance(other,point):
038|             args = [a+b for a,b in zip(self,other)]
039|             return point(*args)
040|     def __sub__ (self, other):
041|         if isinstance(other,point):
042|             args = [a+b for a,b in zip(self,other)]
043|             return point(*args)
044|     def __mul__ (self, other):
045|         if isinstance(other,float) or isinstance(other,int):
046|             args = [a*other for a in self]
047|             return point(*args)
048|     def __truediv__ (self, other):
049|         if isinstance(other,float) or isinstance(other,int):
050|             args = [a/other for a in self]
051|             return point(*args)
052| 
053|     def __str__ (self):
054|         s = '<%.2f, %.2f, %.2f, %.2f>'
055|         t = (self.x, self.y, self.z, self.w)
056|         return s % t
057| 
058| 
059| ##~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~##
060| class extent (object):
061| 
062|     def __init__ (self, x=(0.0,0.0), y=(0.0,0.0), z=(0.0,0.0), w=(0.0,0.0)):
063|         assert len(x)==2, SyntaxError('X extent must be a 2-ple!')
064|         assert len(y)==2, SyntaxError('Y extent must be a 2-ple!')
065|         assert len(z)==2, SyntaxError('Z extent must be a 2-ple!')
066|         assert len(w)==2, SyntaxError('W extent must be a 2-ple!')
067|         self.x = x
068|         self.y = y
069|         self.z = z
070|         self.w = w
071|         self.dx = abs(self.x[1] - self.x[0])
072|         self.dy = abs(self.y[1] - self.y[0])
073|         self.dz = abs(self.z[1] - self.z[0])
074|         self.dw = abs(self.w[1] - self.w[0])
075|         self.cx = self.x[0] + (self.dx / 2.0)
076|         self.cy = self.y[0] + (self.dy / 2.0)
077|         self.cz = self.z[0] + (self.dz / 2.0)
078|         self.cw = self.w[0] + (self.dw / 2.0)
079| 
080|     def cpoint (self):
081|         return point(self.cx, self.cy, self.cz, self.cw)
082| 
083|     def delta (self):
084|         return (self.dx, self.dy, self.dz, self.dw)
085| 
086|     def volume (self):
087|         #TODO: Rename this!
088|         x = self.dx if self.dx else 1.0
089|         y = self.dy if self.dy else 1.0
090|         z = self.dz if self.dz else 1.0
091|         w = self.dw if self.dw else 1.0
092|         return (x * y * z * w)
093| 
094|     def __str__ (self):
095|         return '[%s, %s, %s, %s]' % (self.x, self.y, self.z, self.w)
096| 
097| 
098| ##~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~##
099| class edge (object):
100|     def __init__ (self, p0, p1):
101|         self.ext = extent((p0.x,p1.x), (p0.y,p1.y), (p0.z,p1.z), (p0.w,p1.w))
102| 
103|     def center (self): return self.ext.cpoint()
104|     def volume (self): return self.ext.volume()
105| 
106|     def corners (self):
107|         #ATTN: D'oh!
108|         a = point(self.x[0],self.y[0],self.z[0],self.w[0])
109|         b = point(self.x[1],self.y[1],self.z[1],self.w[1])
110|         return [a, b]
111| 
112|     def length (self):
113|         #ATTN: D'oh!
114|         x = pow(self.x[0]-self.x[1], 2)
115|         y = pow(self.y[0]-self.y[1], 2)
116|         z = pow(self.z[0]-self.z[1], 2)
117|         w = pow(self.w[0]-self.w[1], 2)
118|         return sqrt(x + y + z + w)
119| 
120|     def __str__ (self):
121|         return str(self.ext)
122| 
123| 
124| ##~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~##
125| class face (object):
126|     def __init__ (self, p0, p1):
127|         self.p0 = p0
128|         self.p1 = p1
129|         self.ext = extent((p0.x,p1.x), (p0.y,p1.y), (p0.z,p1.z), (p0.w,p1.w))
130| 
131|     def center (self): return self.ext.cpoint()
132|     def volume (self): return self.ext.volume()
133| 
134|     def corners (self):
135|         #ATTN: D'oh!
136|         a = point(self.x[0],self.y[0],self.z[0],self.w[0])
137|         b = point(self.x[0],self.y[0],self.z[0],self.w[0])
138|         c = point(self.x[0],self.y[0],self.z[0],self.w[0])
139|         d = point(self.x[0],self.y[0],self.z[0],self.w[0])
140|         return [a, b, c, d]
141| 
142|     def __str__ (self):
143|         return str(self.ext)
144| 
145| 
146| ##~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~##
147| class cube (object):
148|     def __init__ (self, p0, p1):
149|         self.p0 = p0
150|         self.p1 = p1
151|         self.ext = extent((p0.x,p1.x), (p0.y,p1.y), (p0.z,p1.z), (p0.w,p1.w))
152|         self.cp = self.ext.cpoint()
153| 
154|     def center (self): return self.ext.cpoint()
155|     def volume (self): return self.ext.volume()
156| 
157|     def corners (self):
158|         return [0]
159| 
160|     def __str__ (self):
161|         return str(self.ext)
162| 
163| 
164| ##================================================================================================##
165| def dump_extent (ext):
166|     print(ext)
167|     print(ext.cpoint())
168|     print(ext.delta())
169|     print(ext.volume())
170|     print()
171| 
172| ##================================================================================================##
173| def dump_object (obj):
174|     print(obj)
175|     print(obj.center())
176|     print(obj.ext.delta())
177|     print(obj.volume())
178|     print()
179| 
180| 
181| ##================================================================================================##
182| def demo ():
183|     e1 = extent((-1,+1))
184|     e2 = extent((-2,+2), (0,+5))
185|     dump_extent(e1)
186|     dump_extent(e2)
187|     print()
188| 
189|     g1 = edge(point(0,0), point(+4,+3))
190|     g2 = face(point(-0.25,-0.25), point(+0.75,+0.75))
191|     g3 = cube(point(-1,-1,-1), point(+1,+1,+1))
192|     dump_object(g1)
193|     dump_object(g2)
194|     dump_object(g3)
195|     print()
196|     g0 = face(point(0,0), point(3,4,5))
197|     dump_object(g0)
198| 
199| 
200| ##================================================================================================##
201| def main (*args):
202|     #dump_extent(ext)
203|     #dump_object(obj)
204|     demo()
205| ####################################################################################################
206| if __name__ == '__main__':
207|     print(argv[0])
208|     print()
209|     main(*argv[1:])
210|     print()
211| ####################################################################################################
212| '''eof'''
213|