1 from __future__ import absolute_import
2 __copyright__ = "Copyright (C) 2013 David Braam - Released under terms of the AGPLv3 License"
11 def addPath(self, x, y, matrix=numpy.matrix(numpy.identity(3, numpy.float64))):
12 p = Path(x, y, matrix)
16 def saveAsHtml(self, filename):
17 f = open(filename, "w")
19 posMax = complex(-1000, -1000)
20 posMin = complex( 1000, 1000)
21 for path in self.paths:
22 points = path.getPoints()
24 if p.real > posMax.real:
25 posMax = complex(p.real, posMax.imag)
26 if p.imag > posMax.imag:
27 posMax = complex(posMax.real, p.imag)
28 if p.real < posMin.real:
29 posMin = complex(p.real, posMin.imag)
30 if p.imag < posMin.imag:
31 posMin = complex(posMin.real, p.imag)
33 f.write("<!DOCTYPE html><html><body>\n")
34 f.write("<svg xmlns=\"http://www.w3.org/2000/svg\" version=\"1.1\" style='width:%dpx;height:%dpx'>\n" % ((posMax - posMin).real, (posMax - posMin).imag))
35 f.write("<g fill-rule='evenodd' style=\"fill: gray; stroke:black;stroke-width:2\">\n")
37 for path in self.paths:
38 points = path.getPoints()
39 f.write("M %f %f " % (points[0].real - posMin.real, points[0].imag - posMin.imag))
40 for point in points[1:]:
41 f.write("L %f %f " % (point.real - posMin.real, point.imag - posMin.imag))
45 f.write("<g style=\"fill: none; stroke:red;stroke-width:1\">\n")
47 for path in self.paths:
48 f.write(path.getSVGPath())
53 f.write("</body></html>")
61 def __init__(self, x, y, matrix=numpy.matrix(numpy.identity(3, numpy.float64))):
63 self._startPoint = complex(x, y)
65 self._isClosed = False
67 def addLineTo(self, x, y):
68 self._points.append({'type': Path.LINE, 'p': complex(x, y)})
70 def addArcTo(self, x, y, rot, rx, ry, large, sweep):
75 'radius': complex(rx, ry),
80 def addCurveTo(self, x, y, cp1x, cp1y, cp2x, cp2y):
84 'cp1': complex(cp1x, cp1y),
85 'cp2': complex(cp2x, cp2y)
91 def checkClosed(self):
92 if abs(self._points[-1]['p'] - self._startPoint) < 0.001:
96 self._points.append({'type': Path.LINE, 'p': self._startPoint})
99 def getPoints(self, accuracy = 1):
100 pointList = [self._m(self._startPoint)]
101 p1 = self._startPoint
102 for p in self._points:
103 if p['type'] == Path.LINE:
105 pointList.append(self._m(p1))
106 elif p['type'] == Path.ARC:
108 rot = math.radians(p['rot'])
111 #http://www.w3.org/TR/SVG/implnote.html#ArcConversionEndpointToCenter
113 p1alt = diff #TODO: apply rot
114 p2alt = -diff #TODO: apply rot
117 x1alt2 = p1alt.real*p1alt.real
118 y1alt2 = p1alt.imag*p1alt.imag
120 f = x1alt2 / rx2 + y1alt2 / ry2
122 r *= math.sqrt(f+0.000001)
126 f = math.sqrt((rx2*ry2 - rx2*y1alt2 - ry2*x1alt2) / (rx2*y1alt2+ry2*x1alt2))
127 if p['large'] == p['sweep']:
129 cAlt = f * complex(r.real*p1alt.imag/r.imag, -r.imag*p1alt.real/r.real)
131 c = cAlt + (p1 + p2) / 2 #TODO: apply rot
133 a1 = math.atan2((p1alt.imag - cAlt.imag) / r.imag, (p1alt.real - cAlt.real) / r.real)
134 a2 = math.atan2((p2alt.imag - cAlt.imag) / r.imag, (p2alt.real - cAlt.real) / r.real)
136 large = abs(a2 - a1) > math.pi
137 if large != p['large']:
143 pCenter = self._m(c + complex(math.cos(a1 + 0.5*(a2-a1)) * r.real, math.sin(a1 + 0.5*(a2-a1)) * r.imag))
144 dist = abs(pCenter - self._m(p1)) + abs(pCenter - self._m(p2))
145 segments = int(dist / accuracy) + 1
146 for n in xrange(1, segments):
147 pointList.append(self._m(c + complex(math.cos(a1 + n*(a2-a1)/segments) * r.real, math.sin(a1 + n*(a2-a1)/segments) * r.imag)))
149 pointList.append(self._m(p2))
151 elif p['type'] == Path.CURVE:
154 cp1 = self._m(p['cp1'])
155 cp2 = self._m(p['cp2'])
157 pCenter = p1_*0.5*0.5*0.5 + cp1*3.0*0.5*0.5*0.5 + cp2*3.0*0.5*0.5*0.5 + p2*0.5*0.5*0.5
158 dist = abs(pCenter - p1_) + abs(pCenter - p2)
159 segments = int(dist / accuracy) + 1
160 for n in xrange(1, segments):
161 f = n / float(segments)
163 point = p1_*g*g*g + cp1*3.0*g*g*f + cp2*3.0*g*f*f + p2*f*f*f
164 pointList.append(point)
171 #getSVGPath returns an SVG path string. Ths path string is not perfect when matrix transformations are involved.
172 def getSVGPath(self):
173 p0 = self._m(self._startPoint)
174 ret = 'M %f %f ' % (p0.real, p0.imag)
175 for p in self._points:
176 if p['type'] == Path.LINE:
178 ret += 'L %f %f' % (p0.real, p0.imag)
179 elif p['type'] == Path.ARC:
182 ret += 'A %f %f 0 %d %d %f %f' % (radius.real, radius.imag, 1 if p['large'] else 0, 1 if p['sweep'] else 0, p0.real, p0.imag)
183 elif p['type'] == Path.CURVE:
185 cp1 = self._m(p['cp1'])
186 cp2 = self._m(p['cp2'])
187 ret += 'C %f %f %f %f %f %f' % (cp1.real, cp1.imag, cp2.real, cp2.imag, p0.real, p0.imag)
192 tmp = numpy.matrix([p.real, p.imag, 1], numpy.float64) * self._matrix
193 return complex(tmp[0,0], tmp[0,1])