printrun-src/printrun/svg2gcode/ffgeom.py

Sat, 04 Jun 2016 12:41:32 +0200

author
mbayer
date
Sat, 04 Jun 2016 12:41:32 +0200
changeset 22
4c9bb8f93ae8
parent 16
36d478bde840
permissions
-rw-r--r--

Added the Lasercut settings to the pronterface options dialog
(default settings when used without pronterwindow)

16
36d478bde840 Implemented svg, png and hpgl compilers to pronterface
mbayer
parents:
diff changeset
1 #!/usr/bin/env python
36d478bde840 Implemented svg, png and hpgl compilers to pronterface
mbayer
parents:
diff changeset
2 """
36d478bde840 Implemented svg, png and hpgl compilers to pronterface
mbayer
parents:
diff changeset
3 ffgeom.py
36d478bde840 Implemented svg, png and hpgl compilers to pronterface
mbayer
parents:
diff changeset
4 Copyright (C) 2005 Aaron Cyril Spike, aaron@ekips.org
36d478bde840 Implemented svg, png and hpgl compilers to pronterface
mbayer
parents:
diff changeset
5
36d478bde840 Implemented svg, png and hpgl compilers to pronterface
mbayer
parents:
diff changeset
6 This file is part of FretFind 2-D.
36d478bde840 Implemented svg, png and hpgl compilers to pronterface
mbayer
parents:
diff changeset
7
36d478bde840 Implemented svg, png and hpgl compilers to pronterface
mbayer
parents:
diff changeset
8 FretFind 2-D is free software; you can redistribute it and/or modify
36d478bde840 Implemented svg, png and hpgl compilers to pronterface
mbayer
parents:
diff changeset
9 it under the terms of the GNU General Public License as published by
36d478bde840 Implemented svg, png and hpgl compilers to pronterface
mbayer
parents:
diff changeset
10 the Free Software Foundation; either version 2 of the License, or
36d478bde840 Implemented svg, png and hpgl compilers to pronterface
mbayer
parents:
diff changeset
11 (at your option) any later version.
36d478bde840 Implemented svg, png and hpgl compilers to pronterface
mbayer
parents:
diff changeset
12
36d478bde840 Implemented svg, png and hpgl compilers to pronterface
mbayer
parents:
diff changeset
13 FretFind 2-D is distributed in the hope that it will be useful,
36d478bde840 Implemented svg, png and hpgl compilers to pronterface
mbayer
parents:
diff changeset
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
36d478bde840 Implemented svg, png and hpgl compilers to pronterface
mbayer
parents:
diff changeset
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
36d478bde840 Implemented svg, png and hpgl compilers to pronterface
mbayer
parents:
diff changeset
16 GNU General Public License for more details.
36d478bde840 Implemented svg, png and hpgl compilers to pronterface
mbayer
parents:
diff changeset
17
36d478bde840 Implemented svg, png and hpgl compilers to pronterface
mbayer
parents:
diff changeset
18 You should have received a copy of the GNU General Public License
36d478bde840 Implemented svg, png and hpgl compilers to pronterface
mbayer
parents:
diff changeset
19 along with FretFind 2-D; if not, write to the Free Software
36d478bde840 Implemented svg, png and hpgl compilers to pronterface
mbayer
parents:
diff changeset
20 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
36d478bde840 Implemented svg, png and hpgl compilers to pronterface
mbayer
parents:
diff changeset
21 """
36d478bde840 Implemented svg, png and hpgl compilers to pronterface
mbayer
parents:
diff changeset
22 import math
36d478bde840 Implemented svg, png and hpgl compilers to pronterface
mbayer
parents:
diff changeset
23 try:
36d478bde840 Implemented svg, png and hpgl compilers to pronterface
mbayer
parents:
diff changeset
24 NaN = float('NaN')
36d478bde840 Implemented svg, png and hpgl compilers to pronterface
mbayer
parents:
diff changeset
25 except ValueError:
36d478bde840 Implemented svg, png and hpgl compilers to pronterface
mbayer
parents:
diff changeset
26 PosInf = 1e300000
36d478bde840 Implemented svg, png and hpgl compilers to pronterface
mbayer
parents:
diff changeset
27 NaN = PosInf/PosInf
36d478bde840 Implemented svg, png and hpgl compilers to pronterface
mbayer
parents:
diff changeset
28
36d478bde840 Implemented svg, png and hpgl compilers to pronterface
mbayer
parents:
diff changeset
29 class Point:
36d478bde840 Implemented svg, png and hpgl compilers to pronterface
mbayer
parents:
diff changeset
30 precision = 5
36d478bde840 Implemented svg, png and hpgl compilers to pronterface
mbayer
parents:
diff changeset
31 def __init__(self, x, y):
36d478bde840 Implemented svg, png and hpgl compilers to pronterface
mbayer
parents:
diff changeset
32 self.__coordinates = {'x' : float(x), 'y' : float(y)}
36d478bde840 Implemented svg, png and hpgl compilers to pronterface
mbayer
parents:
diff changeset
33 def __getitem__(self, key):
36d478bde840 Implemented svg, png and hpgl compilers to pronterface
mbayer
parents:
diff changeset
34 return self.__coordinates[key]
36d478bde840 Implemented svg, png and hpgl compilers to pronterface
mbayer
parents:
diff changeset
35 def __setitem__(self, key, value):
36d478bde840 Implemented svg, png and hpgl compilers to pronterface
mbayer
parents:
diff changeset
36 self.__coordinates[key] = float(value)
36d478bde840 Implemented svg, png and hpgl compilers to pronterface
mbayer
parents:
diff changeset
37 def __repr__(self):
36d478bde840 Implemented svg, png and hpgl compilers to pronterface
mbayer
parents:
diff changeset
38 return '(%s, %s)' % (round(self['x'],self.precision),round(self['y'],self.precision))
36d478bde840 Implemented svg, png and hpgl compilers to pronterface
mbayer
parents:
diff changeset
39 def copy(self):
36d478bde840 Implemented svg, png and hpgl compilers to pronterface
mbayer
parents:
diff changeset
40 return Point(self['x'],self['y'])
36d478bde840 Implemented svg, png and hpgl compilers to pronterface
mbayer
parents:
diff changeset
41 def translate(self, x, y):
36d478bde840 Implemented svg, png and hpgl compilers to pronterface
mbayer
parents:
diff changeset
42 self['x'] += x
36d478bde840 Implemented svg, png and hpgl compilers to pronterface
mbayer
parents:
diff changeset
43 self['y'] += y
36d478bde840 Implemented svg, png and hpgl compilers to pronterface
mbayer
parents:
diff changeset
44 def move(self, x, y):
36d478bde840 Implemented svg, png and hpgl compilers to pronterface
mbayer
parents:
diff changeset
45 self['x'] = float(x)
36d478bde840 Implemented svg, png and hpgl compilers to pronterface
mbayer
parents:
diff changeset
46 self['y'] = float(y)
36d478bde840 Implemented svg, png and hpgl compilers to pronterface
mbayer
parents:
diff changeset
47
36d478bde840 Implemented svg, png and hpgl compilers to pronterface
mbayer
parents:
diff changeset
48 class Segment:
36d478bde840 Implemented svg, png and hpgl compilers to pronterface
mbayer
parents:
diff changeset
49 def __init__(self, e0, e1):
36d478bde840 Implemented svg, png and hpgl compilers to pronterface
mbayer
parents:
diff changeset
50 self.__endpoints = [e0, e1]
36d478bde840 Implemented svg, png and hpgl compilers to pronterface
mbayer
parents:
diff changeset
51 def __getitem__(self, key):
36d478bde840 Implemented svg, png and hpgl compilers to pronterface
mbayer
parents:
diff changeset
52 return self.__endpoints[key]
36d478bde840 Implemented svg, png and hpgl compilers to pronterface
mbayer
parents:
diff changeset
53 def __setitem__(self, key, value):
36d478bde840 Implemented svg, png and hpgl compilers to pronterface
mbayer
parents:
diff changeset
54 self.__endpoints[key] = value
36d478bde840 Implemented svg, png and hpgl compilers to pronterface
mbayer
parents:
diff changeset
55 def __repr__(self):
36d478bde840 Implemented svg, png and hpgl compilers to pronterface
mbayer
parents:
diff changeset
56 return repr(self.__endpoints)
36d478bde840 Implemented svg, png and hpgl compilers to pronterface
mbayer
parents:
diff changeset
57 def copy(self):
36d478bde840 Implemented svg, png and hpgl compilers to pronterface
mbayer
parents:
diff changeset
58 return Segment(self[0],self[1])
36d478bde840 Implemented svg, png and hpgl compilers to pronterface
mbayer
parents:
diff changeset
59 def translate(self, x, y):
36d478bde840 Implemented svg, png and hpgl compilers to pronterface
mbayer
parents:
diff changeset
60 self[0].translate(x,y)
36d478bde840 Implemented svg, png and hpgl compilers to pronterface
mbayer
parents:
diff changeset
61 self[1].translate(x,y)
36d478bde840 Implemented svg, png and hpgl compilers to pronterface
mbayer
parents:
diff changeset
62 def move(self,e0,e1):
36d478bde840 Implemented svg, png and hpgl compilers to pronterface
mbayer
parents:
diff changeset
63 self[0] = e0
36d478bde840 Implemented svg, png and hpgl compilers to pronterface
mbayer
parents:
diff changeset
64 self[1] = e1
36d478bde840 Implemented svg, png and hpgl compilers to pronterface
mbayer
parents:
diff changeset
65 def delta_x(self):
36d478bde840 Implemented svg, png and hpgl compilers to pronterface
mbayer
parents:
diff changeset
66 return self[1]['x'] - self[0]['x']
36d478bde840 Implemented svg, png and hpgl compilers to pronterface
mbayer
parents:
diff changeset
67 def delta_y(self):
36d478bde840 Implemented svg, png and hpgl compilers to pronterface
mbayer
parents:
diff changeset
68 return self[1]['y'] - self[0]['y']
36d478bde840 Implemented svg, png and hpgl compilers to pronterface
mbayer
parents:
diff changeset
69 #alias functions
36d478bde840 Implemented svg, png and hpgl compilers to pronterface
mbayer
parents:
diff changeset
70 run = delta_x
36d478bde840 Implemented svg, png and hpgl compilers to pronterface
mbayer
parents:
diff changeset
71 rise = delta_y
36d478bde840 Implemented svg, png and hpgl compilers to pronterface
mbayer
parents:
diff changeset
72 def slope(self):
36d478bde840 Implemented svg, png and hpgl compilers to pronterface
mbayer
parents:
diff changeset
73 if self.delta_x() != 0:
36d478bde840 Implemented svg, png and hpgl compilers to pronterface
mbayer
parents:
diff changeset
74 return self.delta_x() / self.delta_y()
36d478bde840 Implemented svg, png and hpgl compilers to pronterface
mbayer
parents:
diff changeset
75 return NaN
36d478bde840 Implemented svg, png and hpgl compilers to pronterface
mbayer
parents:
diff changeset
76 def intercept(self):
36d478bde840 Implemented svg, png and hpgl compilers to pronterface
mbayer
parents:
diff changeset
77 if self.delta_x() != 0:
36d478bde840 Implemented svg, png and hpgl compilers to pronterface
mbayer
parents:
diff changeset
78 return self[1]['y'] - (self[0]['x'] * self.slope())
36d478bde840 Implemented svg, png and hpgl compilers to pronterface
mbayer
parents:
diff changeset
79 return NaN
36d478bde840 Implemented svg, png and hpgl compilers to pronterface
mbayer
parents:
diff changeset
80 def distanceToPoint(self, p):
36d478bde840 Implemented svg, png and hpgl compilers to pronterface
mbayer
parents:
diff changeset
81 s2 = Segment(self[0],p)
36d478bde840 Implemented svg, png and hpgl compilers to pronterface
mbayer
parents:
diff changeset
82 c1 = dot(s2,self)
36d478bde840 Implemented svg, png and hpgl compilers to pronterface
mbayer
parents:
diff changeset
83 if c1 <= 0:
36d478bde840 Implemented svg, png and hpgl compilers to pronterface
mbayer
parents:
diff changeset
84 return Segment(p,self[0]).length()
36d478bde840 Implemented svg, png and hpgl compilers to pronterface
mbayer
parents:
diff changeset
85 c2 = dot(self,self)
36d478bde840 Implemented svg, png and hpgl compilers to pronterface
mbayer
parents:
diff changeset
86 if c2 <= c1:
36d478bde840 Implemented svg, png and hpgl compilers to pronterface
mbayer
parents:
diff changeset
87 return Segment(p,self[1]).length()
36d478bde840 Implemented svg, png and hpgl compilers to pronterface
mbayer
parents:
diff changeset
88 return self.perpDistanceToPoint(p)
36d478bde840 Implemented svg, png and hpgl compilers to pronterface
mbayer
parents:
diff changeset
89 def perpDistanceToPoint(self, p):
36d478bde840 Implemented svg, png and hpgl compilers to pronterface
mbayer
parents:
diff changeset
90 len = self.length()
36d478bde840 Implemented svg, png and hpgl compilers to pronterface
mbayer
parents:
diff changeset
91 if len == 0: return NaN
36d478bde840 Implemented svg, png and hpgl compilers to pronterface
mbayer
parents:
diff changeset
92 return math.fabs(((self[1]['x'] - self[0]['x']) * (self[0]['y'] - p['y'])) - \
36d478bde840 Implemented svg, png and hpgl compilers to pronterface
mbayer
parents:
diff changeset
93 ((self[0]['x'] - p['x']) * (self[1]['y'] - self[0]['y']))) / len
36d478bde840 Implemented svg, png and hpgl compilers to pronterface
mbayer
parents:
diff changeset
94 def angle(self):
36d478bde840 Implemented svg, png and hpgl compilers to pronterface
mbayer
parents:
diff changeset
95 return math.pi * (math.atan2(self.delta_y(), self.delta_x())) / 180
36d478bde840 Implemented svg, png and hpgl compilers to pronterface
mbayer
parents:
diff changeset
96 def length(self):
36d478bde840 Implemented svg, png and hpgl compilers to pronterface
mbayer
parents:
diff changeset
97 return math.sqrt((self.delta_x() ** 2) + (self.delta_y() ** 2))
36d478bde840 Implemented svg, png and hpgl compilers to pronterface
mbayer
parents:
diff changeset
98 def pointAtLength(self, len):
36d478bde840 Implemented svg, png and hpgl compilers to pronterface
mbayer
parents:
diff changeset
99 if self.length() == 0: return Point(NaN, NaN)
36d478bde840 Implemented svg, png and hpgl compilers to pronterface
mbayer
parents:
diff changeset
100 ratio = len / self.length()
36d478bde840 Implemented svg, png and hpgl compilers to pronterface
mbayer
parents:
diff changeset
101 x = self[0]['x'] + (ratio * self.delta_x())
36d478bde840 Implemented svg, png and hpgl compilers to pronterface
mbayer
parents:
diff changeset
102 y = self[0]['y'] + (ratio * self.delta_y())
36d478bde840 Implemented svg, png and hpgl compilers to pronterface
mbayer
parents:
diff changeset
103 return Point(x, y)
36d478bde840 Implemented svg, png and hpgl compilers to pronterface
mbayer
parents:
diff changeset
104 def pointAtRatio(self, ratio):
36d478bde840 Implemented svg, png and hpgl compilers to pronterface
mbayer
parents:
diff changeset
105 if self.length() == 0: return Point(NaN, NaN)
36d478bde840 Implemented svg, png and hpgl compilers to pronterface
mbayer
parents:
diff changeset
106 x = self[0]['x'] + (ratio * self.delta_x())
36d478bde840 Implemented svg, png and hpgl compilers to pronterface
mbayer
parents:
diff changeset
107 y = self[0]['y'] + (ratio * self.delta_y())
36d478bde840 Implemented svg, png and hpgl compilers to pronterface
mbayer
parents:
diff changeset
108 return Point(x, y)
36d478bde840 Implemented svg, png and hpgl compilers to pronterface
mbayer
parents:
diff changeset
109 def createParallel(self, p):
36d478bde840 Implemented svg, png and hpgl compilers to pronterface
mbayer
parents:
diff changeset
110 return Segment(Point(p['x'] + self.delta_x(), p['y'] + self.delta_y()), p)
36d478bde840 Implemented svg, png and hpgl compilers to pronterface
mbayer
parents:
diff changeset
111 def intersect(self, s):
36d478bde840 Implemented svg, png and hpgl compilers to pronterface
mbayer
parents:
diff changeset
112 return intersectSegments(self, s)
36d478bde840 Implemented svg, png and hpgl compilers to pronterface
mbayer
parents:
diff changeset
113
36d478bde840 Implemented svg, png and hpgl compilers to pronterface
mbayer
parents:
diff changeset
114 def intersectSegments(s1, s2):
36d478bde840 Implemented svg, png and hpgl compilers to pronterface
mbayer
parents:
diff changeset
115 x1 = s1[0]['x']
36d478bde840 Implemented svg, png and hpgl compilers to pronterface
mbayer
parents:
diff changeset
116 x2 = s1[1]['x']
36d478bde840 Implemented svg, png and hpgl compilers to pronterface
mbayer
parents:
diff changeset
117 x3 = s2[0]['x']
36d478bde840 Implemented svg, png and hpgl compilers to pronterface
mbayer
parents:
diff changeset
118 x4 = s2[1]['x']
36d478bde840 Implemented svg, png and hpgl compilers to pronterface
mbayer
parents:
diff changeset
119
36d478bde840 Implemented svg, png and hpgl compilers to pronterface
mbayer
parents:
diff changeset
120 y1 = s1[0]['y']
36d478bde840 Implemented svg, png and hpgl compilers to pronterface
mbayer
parents:
diff changeset
121 y2 = s1[1]['y']
36d478bde840 Implemented svg, png and hpgl compilers to pronterface
mbayer
parents:
diff changeset
122 y3 = s2[0]['y']
36d478bde840 Implemented svg, png and hpgl compilers to pronterface
mbayer
parents:
diff changeset
123 y4 = s2[1]['y']
36d478bde840 Implemented svg, png and hpgl compilers to pronterface
mbayer
parents:
diff changeset
124
36d478bde840 Implemented svg, png and hpgl compilers to pronterface
mbayer
parents:
diff changeset
125 denom = ((y4 - y3) * (x2 - x1)) - ((x4 - x3) * (y2 - y1))
36d478bde840 Implemented svg, png and hpgl compilers to pronterface
mbayer
parents:
diff changeset
126 num1 = ((x4 - x3) * (y1 - y3)) - ((y4 - y3) * (x1 - x3))
36d478bde840 Implemented svg, png and hpgl compilers to pronterface
mbayer
parents:
diff changeset
127 num2 = ((x2 - x1) * (y1 - y3)) - ((y2 - y1) * (x1 - x3))
36d478bde840 Implemented svg, png and hpgl compilers to pronterface
mbayer
parents:
diff changeset
128
36d478bde840 Implemented svg, png and hpgl compilers to pronterface
mbayer
parents:
diff changeset
129 num = num1
36d478bde840 Implemented svg, png and hpgl compilers to pronterface
mbayer
parents:
diff changeset
130
36d478bde840 Implemented svg, png and hpgl compilers to pronterface
mbayer
parents:
diff changeset
131 if denom != 0:
36d478bde840 Implemented svg, png and hpgl compilers to pronterface
mbayer
parents:
diff changeset
132 x = x1 + ((num / denom) * (x2 - x1))
36d478bde840 Implemented svg, png and hpgl compilers to pronterface
mbayer
parents:
diff changeset
133 y = y1 + ((num / denom) * (y2 - y1))
36d478bde840 Implemented svg, png and hpgl compilers to pronterface
mbayer
parents:
diff changeset
134 return Point(x, y)
36d478bde840 Implemented svg, png and hpgl compilers to pronterface
mbayer
parents:
diff changeset
135 return Point(NaN, NaN)
36d478bde840 Implemented svg, png and hpgl compilers to pronterface
mbayer
parents:
diff changeset
136
36d478bde840 Implemented svg, png and hpgl compilers to pronterface
mbayer
parents:
diff changeset
137 def dot(s1, s2):
36d478bde840 Implemented svg, png and hpgl compilers to pronterface
mbayer
parents:
diff changeset
138 return s1.delta_x() * s2.delta_x() + s1.delta_y() * s2.delta_y()
36d478bde840 Implemented svg, png and hpgl compilers to pronterface
mbayer
parents:
diff changeset
139
36d478bde840 Implemented svg, png and hpgl compilers to pronterface
mbayer
parents:
diff changeset
140
36d478bde840 Implemented svg, png and hpgl compilers to pronterface
mbayer
parents:
diff changeset
141 # vim: expandtab shiftwidth=4 tabstop=8 softtabstop=4 fileencoding=utf-8 textwidth=99

mercurial