chiark / gitweb /
Add imageToMesh tool.
[cura.git] / Cura / gui / tools / imageToMesh.py
1 __copyright__ = "Copyright (C) 2013 David Braam - Released under terms of the AGPLv3 License"
2
3 import wx
4 import numpy
5
6 from Cura.util import mesh
7
8 def supportedExtensions():
9         return ['.bmp', '.jpg', '.jpeg', '.png']
10
11 def wildcardList():
12         return ';'.join(map(lambda s: '*' + s, supportedExtensions()))
13
14 class convertImageDialog(wx.Dialog):
15         def __init__(self, parent, filename):
16                 super(convertImageDialog, self).__init__(None, title="Convert image...")
17                 wx.EVT_CLOSE(self, self.OnClose)
18                 self.parent = parent
19                 self.filename = filename
20
21                 image = wx.Image(filename)
22                 w, h = image.GetWidth() - 1, image.GetHeight() - 1
23                 self.aspectRatio = float(w) / float(h)
24
25                 p = wx.Panel(self)
26                 self.SetSizer(wx.BoxSizer())
27                 self.GetSizer().Add(p, 1, flag=wx.EXPAND)
28
29                 s = wx.GridBagSizer(2, 2)
30                 p.SetSizer(s)
31                 s.Add(wx.StaticText(p, -1, _('Height (mm)')), pos=(0, 0), flag=wx.LEFT|wx.TOP|wx.RIGHT, border=5)
32                 self.heightInput = wx.TextCtrl(p, -1, '10.0')
33                 s.Add(self.heightInput, pos=(0, 1), flag=wx.LEFT|wx.TOP|wx.RIGHT|wx.EXPAND, border=5)
34
35                 s.Add(wx.StaticText(p, -1, _('Width (mm)')), pos=(1, 0), flag=wx.LEFT|wx.TOP|wx.RIGHT, border=5)
36                 self.widthInput = wx.TextCtrl(p, -1, str(w * 0.3))
37                 s.Add(self.widthInput, pos=(1, 1), flag=wx.LEFT|wx.TOP|wx.RIGHT|wx.EXPAND, border=5)
38
39                 s.Add(wx.StaticText(p, -1, _('Depth (mm)')), pos=(2, 0), flag=wx.LEFT|wx.TOP|wx.RIGHT, border=5)
40                 self.depthInput = wx.TextCtrl(p, -1, str(h * 0.3))
41                 s.Add(self.depthInput, pos=(2, 1), flag=wx.LEFT|wx.TOP|wx.RIGHT|wx.EXPAND, border=5)
42
43                 options = ['Darker is higher', 'Lighter is higher']
44                 self.invertInput = wx.ComboBox(p, -1, options[0], choices=options, style=wx.CB_DROPDOWN|wx.CB_READONLY)
45                 s.Add(self.invertInput, pos=(3, 1), flag=wx.LEFT|wx.TOP|wx.RIGHT|wx.EXPAND, border=5)
46
47                 options = ['No smoothing', 'Light smoothing', 'Heavy smoothing']
48                 self.smoothInput = wx.ComboBox(p, -1, options[0], choices=options, style=wx.CB_DROPDOWN|wx.CB_READONLY)
49                 s.Add(self.smoothInput, pos=(4, 1), flag=wx.LEFT|wx.TOP|wx.RIGHT|wx.EXPAND, border=5)
50
51                 self.okButton = wx.Button(p, -1, 'Ok')
52                 s.Add(self.okButton, pos=(5, 1), flag=wx.ALL, border=5)
53
54                 self.okButton.Bind(wx.EVT_BUTTON, self.OnOkClick)
55                 self.widthInput.Bind(wx.EVT_TEXT, self.OnWidthEnter)
56                 self.depthInput.Bind(wx.EVT_TEXT, self.OnDepthEnter)
57
58                 self.Fit()
59                 self.Centre()
60
61         def OnClose(self, e):
62                 self.Destroy()
63
64         def OnOkClick(self, e):
65                 self.Close()
66                 height = float(self.heightInput.GetValue())
67                 width = float(self.widthInput.GetValue())
68                 blur = self.smoothInput.GetSelection()
69                 blur *= blur
70                 invert = self.invertInput.GetSelection() == 0
71
72                 obj = convertImage(self.filename, height, width, blur, invert)
73                 self.parent._scene.add(obj)
74                 self.parent._scene.centerAll()
75                 self.parent.sceneUpdated()
76
77         def OnWidthEnter(self, e):
78                 try:
79                         w = float(self.widthInput.GetValue())
80                 except ValueError:
81                         return
82                 h = w / self.aspectRatio
83                 self.depthInput.SetValue(str(h))
84
85         def OnDepthEnter(self, e):
86                 try:
87                         h = float(self.depthInput.GetValue())
88                 except ValueError:
89                         return
90                 w = h * self.aspectRatio
91                 self.widthInput.SetValue(str(w))
92
93 def convertImage(filename, height=20.0, width=100.0, blur=0, invert=False, baseHeight=1.0):
94         image = wx.Image(filename)
95         image.ConvertToGreyscale()
96         if image.GetHeight() > 512:
97                 image.Rescale(image.GetWidth() * 512 / image.GetHeight(), 512, wx.IMAGE_QUALITY_HIGH)
98         if image.GetWidth() > 512:
99                 image.Rescale(512, image.GetHeight() * 512 / image.GetWidth(), wx.IMAGE_QUALITY_HIGH)
100         if blur > 0:
101                 image = image.Blur(blur)
102         z = numpy.fromstring(image.GetData(), numpy.uint8)
103         z = numpy.array(z[::3], numpy.float32)  #Only get the R values (as we are grayscale), and convert to float values
104         pMin, pMax = numpy.min(z), numpy.max(z)
105         if pMax == pMin:
106                 pMax += 1.0
107         if invert:
108                 z = 255 - z
109         z = ((z - pMin) * height / (pMax - pMin)) + baseHeight
110
111         w, h = image.GetWidth(), image.GetHeight()
112         scale = width / (image.GetWidth() - 1)
113         n = w * h
114         y, x = numpy.mgrid[0:h,0:w]
115         x = numpy.array(x, numpy.float32, copy=False) * scale
116         y = numpy.array(y, numpy.float32, copy=False) *-scale
117         v0 = numpy.concatenate((x.reshape((n, 1)), y.reshape((n, 1)), z.reshape((n, 1))), 1)
118         v0 = v0.reshape((h, w, 3))
119         v1 = v0[0:-1,0:-1,:]
120         v2 = v0[0:-1,1:,:]
121         v3 = v0[1:,0:-1,:]
122         v4 = v0[1:,1:,:]
123
124         obj = mesh.printableObject(filename)
125         m = obj._addMesh()
126         m._prepareFaceCount((w-1) * (h-1) * 2 + 2 + (w-1)*4 + (h-1)*4)
127         m.vertexes = numpy.array(numpy.concatenate((v1,v3,v2,v2,v3,v4), 2).reshape(((w-1) * (h-1) * 6, 3)), numpy.float32, copy=False)
128         m.vertexes = numpy.concatenate((m.vertexes, numpy.zeros(((2+(w-1)*4+(h-1)*4)*3, 3), numpy.float32)))
129         m.vertexCount = (w-1) * (h-1) * 6
130         x = (w-1)* scale
131         y = (h-1)*-scale
132         m._addFace(0,0,0, x,0,0, 0,y,0)
133         m._addFace(x,y,0, 0,y,0, x,0,0)
134         for n in xrange(0, w-1):
135                 x = n* scale
136                 i = w*h-w+n
137                 m._addFace(x+scale,0,0, x,0,0, x,0,z[n])
138                 m._addFace(x+scale,0,0, x,0,z[n], x+scale,0,z[n+1])
139                 m._addFace(x+scale,y,0, x,y,z[i], x,y,0)
140                 m._addFace(x+scale,y,0, x+scale,y,z[i+1], x,y,z[i])
141
142         x = (w-1)*scale
143         for n in xrange(0, h-1):
144                 y = n*-scale
145                 i = n*w+w-1
146                 m._addFace(0,y-scale,0, 0,y,z[n*w], 0,y,0)
147                 m._addFace(0,y-scale,0, 0,y-scale,z[n*w+w], 0,y,z[n*w])
148                 m._addFace(x,y-scale,0, x,y,0, x,y,z[i])
149                 m._addFace(x,y-scale,0, x,y,z[i], x,y-scale,z[i+w])
150         obj._postProcessAfterLoad()
151         return obj