chiark / gitweb /
for testing
[moebius3.git] / genscad
1 #!/usr/bin/python
2
3 from __future__ import print_function
4
5 import signal
6 signal.signal(signal.SIGINT, signal.SIG_DFL)
7
8 from moebius import *
9
10 nomsize = 20
11 thick = 2.0
12
13 nv = 80
14 nw = 80
15
16 m = Moebius(nv, nw)
17
18
19 stl_points = []
20 stl_point_indices = {}
21 stl_triangles = []
22
23 def stl_writeout():
24   print('module MoebiusCore(){ scale(%s) polyhedron(points=[' % nomsize)
25   for p in stl_points: print(p, ',')
26   print('],faces=[')
27   for t in stl_triangles: print(repr(t), ',')
28   print('],convexity=10); }')
29
30 def stl_point(p):
31   l = list(p)
32   s = repr(l)
33   try:
34     ix = stl_point_indices[s]
35   except KeyError:
36     ix = len(stl_points)
37     stl_points.append(s)
38     stl_point_indices[s] = ix
39   return ix
40
41 def stl_triangle(a,b,c):
42   ''' a b c  are clockwise from inside '''
43   stl_triangles.append([ stl_point(p) for p in (a,b,c) ])
44
45 def stl_quad(cnrs):
46   ''' cnrs[0] [1] [3] [2] are clockwise from inside '''
47   stl_triangle(cnrs[0], cnrs[1], cnrs[3])
48   stl_triangle(cnrs[0], cnrs[3], cnrs[2])
49
50 def stl_rquad(cnrs):
51   ''' cnrs[0] [1] [3] [2] are anticlockwise from inside '''
52   stl_triangle(cnrs[0], cnrs[3], cnrs[1])
53   stl_triangle(cnrs[0], cnrs[2], cnrs[3])
54
55 relthick = thick/(nomsize*2)
56
57 for v in range(0, nv):
58   for w in range(0, nw):
59     stl_quad([ m.point_offset(v+a, w+b,  relthick)
60                for a in (0, 1)
61                for b in (0, 1) ])
62     stl_rquad([ m.point_offset(v+a, w+b, -relthick)
63                for a in (1, 0)
64                for b in (1, 0) ])
65   for q, w in ((stl_quad, 0), (stl_rquad, nw)):
66     q([ m.point_offset(v+a, w,  b*relthick)
67         for a in (0, 1)
68         for b in (-1, +1) ])
69
70 stl_writeout()