chiark / gitweb /
simplex wip: use gsl_vector_get for X, for abandonment
[moebius3.git] / scad.py
1
2 from __future__ import print_function
3
4 class ScadObject:
5   def __init__(so):
6     so._points = []
7     so._point_indices = {}
8     so._triangles = []
9
10   def writeout_core(so, scalefactor=1):
11     if not so._points: return
12     print('scale(%s) polyhedron(points=[' % scalefactor)
13     for p in so._points: print(p, ',')
14     print('],faces=[')
15     for t in so._triangles: print(repr(t), ',')
16     so._points = None
17     print('],convexity=10);')
18
19   def writeout(so, objname, scalefactor=1):
20     print('module %s(){' % objname)
21     so.writeout_core(scalefactor)
22     print('}')
23
24   def _point(so, p):
25     l = list(p)
26     s = repr(l)
27     try:
28       ix = so._point_indices[s]
29     except KeyError:
30       ix = len(so._points)
31       so._points.append(s)
32       so._point_indices[s] = ix
33     return ix
34
35   def triangle(so, a,b,c):
36     ''' a b c  are clockwise from inside '''
37     so._triangles.append([ so._point(p) for p in (a,b,c) ])
38
39   def quad(so, cnrs):
40     ''' cnrs[0] [1] [3] [2] are clockwise from inside '''
41     so.triangle(cnrs[0], cnrs[1], cnrs[3])
42     so.triangle(cnrs[0], cnrs[3], cnrs[2])
43
44   def rquad(so, cnrs):
45     ''' cnrs[0] [1] [3] [2] are anticlockwise from inside '''
46     so.triangle(cnrs[0], cnrs[3], cnrs[1])
47     so.triangle(cnrs[0], cnrs[2], cnrs[3])
48