frep.py 1.97 KB
Newer Older
Neil Gershenfeld's avatar
wip  
Neil Gershenfeld committed
1
2
3
#!/usr/bin/env python
#
# frep.py
Neil Gershenfeld's avatar
wip    
Neil Gershenfeld committed
4
#    functional representation solver
Neil Gershenfeld's avatar
wip  
Neil Gershenfeld committed
5
#
Neil Gershenfeld's avatar
wip    
Neil Gershenfeld committed
6
# usage:
Neil Gershenfeld's avatar
wip  
Neil Gershenfeld committed
7
#    pcb.py | frep.py [dpi [filename]]
Neil Gershenfeld's avatar
wip    
Neil Gershenfeld committed
8
#
Neil Gershenfeld's avatar
wip    
Neil Gershenfeld committed
9
10
# Neil Gershenfeld 9/30/19
# (c) Massachusetts Institute of Technology 2019
Neil Gershenfeld's avatar
wip  
Neil Gershenfeld committed
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
#
# This work may be reproduced, modified, distributed,
# performed, and displayed for any purpose, but must
# acknowledge this project. Copyright is retained and
# must be preserved. The work is provided as is; no
# warranty is provided, and users accept all liability.
#

#
# import
#

import json,sys
from numpy import *
from PIL import Image

#
# read input
#

frep = json.load(sys.stdin)

#
# check arguments
#
if (frep['type'] != 'RGB'):
   print('types other than RGB not (yet) supported')
   sys.exit()
if (len(sys.argv) == 1):
   print('output to out.png at 100 DPI')
   filename = 'out.png'
   dpi = 100
elif (len(sys.argv) == 2):
   dpi = sys.argv[1]
   filename = 'out.png'
   print('output to out.png at '+dpi+'DPI')
   dpi = int(dpi)
elif (len(sys.argv) == 3):
   dpi = sys.argv[1]
   filename = sys.argv[2]
   print('output to '+filename+' at '+dpi+' DPI')
   dpi = int(dpi)

#
# evaluate
#

Neil Gershenfeld's avatar
wip    
Neil Gershenfeld committed
58
print('evaluating')
Neil Gershenfeld's avatar
wip  
Neil Gershenfeld committed
59
60
61
62
63
64
65
66
67
68
xmin = frep['xmin']
xmax = frep['xmax']
ymin = frep['ymin']
ymax = frep['ymax']
units = float(frep['mm_per_unit'])
delta = (25.4/dpi)/units
x = arange(xmin,xmax,delta)
y = flip(arange(ymin,ymax,delta),0)
X = outer(ones(y.size),x)
Y = outer(y,ones(x.size))
Neil Gershenfeld's avatar
wip    
Neil Gershenfeld committed
69
70
if (len(frep['layers']) == 1):
   Z = frep['layers'][0]
Neil Gershenfeld's avatar
wip    
Neil Gershenfeld committed
71
   print("   z =",Z)
Neil Gershenfeld's avatar
wip    
Neil Gershenfeld committed
72
73
74
75
76
77
   f = eval(frep['function']).astype(uint32)
else:
   f = zeros((y.size,x.size),dtype=uint32)
   zmin = min(frep['layers'])
   zmax = max(frep['layers'])
   for Z in frep['layers']:
Neil Gershenfeld's avatar
wip    
Neil Gershenfeld committed
78
      print("   z =",Z)
Neil Gershenfeld's avatar
wip    
Neil Gershenfeld committed
79
80
81
      i = int(255*(Z-zmin)/(zmax-zmin)) | (255 << 8) | (255 << 16)
      flayer = i & (eval(frep['function'])).astype(uint32)
      f = f + flayer
Neil Gershenfeld's avatar
wip  
Neil Gershenfeld committed
82
83
84
85
86
87
88
89
90
91
92

#
# construct image
#

m = zeros((y.size,x.size,3),dtype=uint8)
m[:,:,0] = (f & 255)
m[:,:,1] = ((f >> 8) & 255)
m[:,:,2] = ((f >> 16) & 255)
im = Image.fromarray(m,'RGB')
im.save(filename,dpi=[dpi,dpi])