chiark / gitweb /
genscad: Lift out ScadObject (nfc)
authorIan Jackson <ijackson@chiark.greenend.org.uk>
Sun, 5 Nov 2017 17:09:10 +0000 (17:09 +0000)
committerIan Jackson <ijackson@chiark.greenend.org.uk>
Sun, 5 Nov 2017 17:09:10 +0000 (17:09 +0000)
Signed-off-by: Ian Jackson <ijackson@chiark.greenend.org.uk>
genscad

diff --git a/genscad b/genscad
index a9c3dc4b22da7e51753b1661a2d8814ce7eb5ae0..b2b83bfc4981d86c69e0ca0d0013464d4f0570e5 100755 (executable)
--- a/genscad
+++ b/genscad
@@ -16,56 +16,64 @@ nw = 80
 m = Moebius(nv, nw)
 
 
-stl_points = []
-stl_point_indices = {}
-stl_triangles = []
-
-def stl_writeout():
-  print('module MoebiusCore(){ scale(%s) polyhedron(points=[' % nomsize)
-  for p in stl_points: print(p, ',')
-  print('],faces=[')
-  for t in stl_triangles: print(repr(t), ',')
-  print('],convexity=10); }')
-  print('moebiuscore_nomsize=%s;' % repr(nomsize))
-
-def stl_point(p):
-  l = list(p)
-  s = repr(l)
-  try:
-    ix = stl_point_indices[s]
-  except KeyError:
-    ix = len(stl_points)
-    stl_points.append(s)
-    stl_point_indices[s] = ix
-  return ix
-
-def stl_triangle(a,b,c):
-  ''' a b c  are clockwise from inside '''
-  stl_triangles.append([ stl_point(p) for p in (a,b,c) ])
-
-def stl_quad(cnrs):
-  ''' cnrs[0] [1] [3] [2] are clockwise from inside '''
-  stl_triangle(cnrs[0], cnrs[1], cnrs[3])
-  stl_triangle(cnrs[0], cnrs[3], cnrs[2])
-
-def stl_rquad(cnrs):
-  ''' cnrs[0] [1] [3] [2] are anticlockwise from inside '''
-  stl_triangle(cnrs[0], cnrs[3], cnrs[1])
-  stl_triangle(cnrs[0], cnrs[2], cnrs[3])
+class ScadObject:
+  def __init__(so):
+    so._points = []
+    so._point_indices = {}
+    so._triangles = []
+
+  def writeout(so, objname, scalefactor=1):
+    print('module %s(){ scale(%s) polyhedron(points=[' %
+          (objname, scalefactor))
+    for p in so._points: print(p, ',')
+    print('],faces=[')
+    for t in so._triangles: print(repr(t), ',')
+    print('],convexity=10); }')
+    so._points = None
+
+  def _point(so, p):
+    l = list(p)
+    s = repr(l)
+    try:
+      ix = so._point_indices[s]
+    except KeyError:
+      ix = len(so._points)
+      so._points.append(s)
+      so._point_indices[s] = ix
+    return ix
+
+  def triangle(so, a,b,c):
+    ''' a b c  are clockwise from inside '''
+    so._triangles.append([ so._point(p) for p in (a,b,c) ])
+
+  def quad(so, cnrs):
+    ''' cnrs[0] [1] [3] [2] are clockwise from inside '''
+    so.triangle(cnrs[0], cnrs[1], cnrs[3])
+    so.triangle(cnrs[0], cnrs[3], cnrs[2])
+
+  def rquad(so, cnrs):
+    ''' cnrs[0] [1] [3] [2] are anticlockwise from inside '''
+    so.triangle(cnrs[0], cnrs[3], cnrs[1])
+    so.triangle(cnrs[0], cnrs[2], cnrs[3])
 
 relthick = thick/(nomsize*2)
 
-for v in range(0, nv):
-  for w in range(0, nw):
-    stl_quad([ m.point_offset(v+a, w+b,  relthick)
-               for a in (0, 1)
-               for b in (0, 1) ])
-    stl_rquad([ m.point_offset(v+a, w+b, -relthick)
-               for a in (1, 0)
-               for b in (1, 0) ])
-  for q, w in ((stl_quad, 0), (stl_rquad, nw)):
-    q([ m.point_offset(v+a, w,  b*relthick)
-        for a in (0, 1)
-        for b in (-1, +1) ])
-
-stl_writeout()
+def make_moebius(objname):
+  so = ScadObject()
+  for v in range(0, nv):
+    for w in range(0, nw):
+      so.quad([ m.point_offset(v+a, w+b,  relthick)
+                for a in (0, 1)
+                for b in (0, 1) ])
+      so.rquad([ m.point_offset(v+a, w+b, -relthick)
+                 for a in (1, 0)
+                 for b in (1, 0) ])
+    for q, w in ((so.quad, 0), (so.rquad, nw)):
+      q([ m.point_offset(v+a, w,  b*relthick)
+          for a in (0, 1)
+          for b in (-1, +1) ])
+  so.writeout(objname, nomsize)
+
+make_moebius('MoebiusCore')
+
+print('moebiuscore_nomsize=%s;' % repr(nomsize))