1#!/usr/local/bin/python3.8 2# coding=utf-8 3# 4# Copyright (C) 2007 Tavmjong Bah, tavmjong@free.fr 5# Copyright (C) 2006 Georg Wiora, xorx@quarkbox.de 6# Copyright (C) 2006 Johan Engelen, johan@shouraizou.nl 7# Copyright (C) 2005 Aaron Spike, aaron@ekips.org 8# 9# This program is free software; you can redistribute it and/or modify 10# it under the terms of the GNU General Public License as published by 11# the Free Software Foundation; either version 2 of the License, or 12# (at your option) any later version. 13# 14# This program is distributed in the hope that it will be useful, 15# but WITHOUT ANY WARRANTY; without even the implied warranty of 16# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 17# GNU General Public License for more details. 18# 19# You should have received a copy of the GNU General Public License 20# along with this program; if not, write to the Free Software 21# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. 22# 23# Changes: 24# * This program is a modified version of wavy.py by Aaron Spike. 25# * 22-Dec-2006: Wiora : Added axis and isotropic scaling 26# * 21-Jun-2007: Tavmjong: Added polar coordinates 27# 28from math import cos, pi, sin 29 30import inkex 31from inkex import ClipPath, Rectangle 32from inkex.utils import math_eval 33 34def drawfunction(xstart, xend, ybottom, ytop, samples, width, height, left, bottom, 35 fx="sin(x)", fpx="cos(x)", fponum=True, times2pi=False, polar=False, isoscale=True, drawaxis=True, endpts=False): 36 if times2pi: 37 xstart = 2 * pi * xstart 38 xend = 2 * pi * xend 39 40 # coords and scales based on the source rect 41 if xstart == xend: 42 inkex.errormsg("x-interval cannot be zero. Please modify 'Start X value' or 'End X value'") 43 return [] 44 scalex = width / (xend - xstart) 45 xoff = left 46 coordx = lambda x: (x - xstart) * scalex + xoff # convert x-value to coordinate 47 if polar: # Set scale so that left side of rectangle is -1, right side is +1. 48 # (We can't use xscale for both range and scale.) 49 centerx = left + width / 2.0 50 polar_scalex = width / 2.0 51 coordx = lambda x: x * polar_scalex + centerx # convert x-value to coordinate 52 53 if ytop == ybottom: 54 inkex.errormsg("y-interval cannot be zero. Please modify 'Y value of rectangle's top' or 'Y value of rectangle's bottom'") 55 return [] 56 scaley = height / (ytop - ybottom) 57 yoff = bottom 58 coordy = lambda y: (ybottom - y) * scaley + yoff # convert y-value to coordinate 59 60 # Check for isotropic scaling and use smaller of the two scales, correct ranges 61 if isoscale and not polar: 62 if scaley < scalex: 63 # compute zero location 64 xzero = coordx(0) 65 # set scale 66 scalex = scaley 67 # correct x-offset 68 xstart = (left - xzero) / scalex 69 xend = (left + width - xzero) / scalex 70 else: 71 # compute zero location 72 yzero = coordy(0) 73 # set scale 74 scaley = scalex 75 # correct x-offset 76 ybottom = (yzero - bottom) / scaley 77 ytop = (bottom + height - yzero) / scaley 78 79 f = math_eval(fx) 80 fp = math_eval(fpx) 81 if (f is None or (fp is None and not(fponum))): 82 raise inkex.AbortExtension(_("Invalid function specification")) 83 84 # step is the distance between nodes on x 85 step = (xend - xstart) / (samples - 1) 86 third = step / 3.0 87 ds = step * 0.001 # Step used in calculating derivatives 88 89 a = [] # path array 90 # add axis 91 if drawaxis: 92 # check for visibility of x-axis 93 if ybottom <= 0 <= ytop: 94 # xaxis 95 a.append(['M', [left, coordy(0)]]) 96 a.append(['l', [width, 0]]) 97 # check for visibility of y-axis 98 if xstart <= 0 <= xend: 99 # xaxis 100 a.append(['M', [coordx(0), bottom]]) 101 a.append(['l', [0, -height]]) 102 103 # initialize function and derivative for 0; 104 # they are carried over from one iteration to the next, to avoid extra function calculations. 105 x0 = xstart 106 y0 = f(xstart) 107 if polar: 108 xp0 = y0 * cos(x0) 109 yp0 = y0 * sin(x0) 110 x0 = xp0 111 y0 = yp0 112 if fponum or polar: # numerical derivative, using 0.001*step as the small differential 113 x1 = xstart + ds # Second point AFTER first point (Good for first point) 114 y1 = f(x1) 115 if polar: 116 xp1 = y1 * cos(x1) 117 yp1 = y1 * sin(x1) 118 x1 = xp1 119 y1 = yp1 120 dx0 = (x1 - x0) / ds 121 dy0 = (y1 - y0) / ds 122 else: # derivative given by the user 123 dx0 = 1 # Only works for rectangular coordinates 124 dy0 = fp(xstart) 125 126 # Start curve 127 if endpts: 128 a.append(['M', [left, coordy(0)]]) 129 a.append(['L', [coordx(x0), coordy(y0)]]) 130 else: 131 a.append(['M', [coordx(x0), coordy(y0)]]) # initial moveto 132 133 for i in range(int(samples - 1)): 134 x1 = (i + 1) * step + xstart 135 x2 = x1 - ds # Second point BEFORE first point (Good for last point) 136 y1 = f(x1) 137 y2 = f(x2) 138 if polar: 139 xp1 = y1 * cos(x1) 140 yp1 = y1 * sin(x1) 141 xp2 = y2 * cos(x2) 142 yp2 = y2 * sin(x2) 143 x1 = xp1 144 y1 = yp1 145 x2 = xp2 146 y2 = yp2 147 if fponum or polar: # numerical derivative 148 dx1 = (x1 - x2) / ds 149 dy1 = (y1 - y2) / ds 150 else: # derivative given by the user 151 dx1 = 1 # Only works for rectangular coordinates 152 dy1 = fp(x1) 153 # create curve 154 a.append(['C', 155 [coordx(x0 + (dx0 * third)), coordy(y0 + (dy0 * third)), 156 coordx(x1 - (dx1 * third)), coordy(y1 - (dy1 * third)), 157 coordx(x1), coordy(y1)] 158 ]) 159 x0 = x1 # Next segment's start is this segments end 160 y0 = y1 161 dx0 = dx1 # Assume the function is smooth everywhere, so carry over the derivative too 162 dy0 = dy1 163 if endpts: 164 a.append(['L', [left + width, coordy(0)]]) 165 return a 166 167 168class FuncPlot(inkex.EffectExtension): 169 def add_arguments(self, pars): 170 pars.add_argument("--tab") 171 pars.add_argument("--xstart", type=float, default=0.0, help="Start x-value") 172 pars.add_argument("--xend", type=float, default=1.0, help="End x-value") 173 pars.add_argument("--times2pi", type=inkex.Boolean, default=True, help="* x-range by 2*pi") 174 pars.add_argument("--polar", type=inkex.Boolean, default=False, help="Use polar coords") 175 pars.add_argument("--ybottom", type=float, default=-1.0, help="y-value of rect's bottom") 176 pars.add_argument("--ytop", type=float, default=1.0, help="y-value of rectangle's top") 177 pars.add_argument("--samples", type=int, default=8, help="Samples") 178 pars.add_argument("--fofx", default="sin(x)", help="f(x) for plotting") 179 pars.add_argument("--fponum", type=inkex.Boolean, default=True, help="Numerical 1st deriv") 180 pars.add_argument("--fpofx", default="cos(x)", help="f'(x) for plotting") 181 pars.add_argument("--clip", type=inkex.Boolean, default=False, help="Clip with source rect") 182 pars.add_argument("--remove", type=inkex.Boolean, default=True, help="Remove source rect") 183 pars.add_argument("--isoscale", type=inkex.Boolean, default=True, help="Isotropic scaling") 184 pars.add_argument("--drawaxis", type=inkex.Boolean, default=True, help="Draw axis") 185 pars.add_argument("--endpts", type=inkex.Boolean, default=False, help="Add end points") 186 187 def effect(self): 188 newpath = None 189 for node in self.svg.selected.values(): 190 if isinstance(node, Rectangle): 191 # create new path with basic dimensions of selected rectangle 192 newpath = inkex.PathElement() 193 x = float(node.get('x')) 194 y = float(node.get('y')) 195 w = float(node.get('width')) 196 h = float(node.get('height')) 197 198 # copy attributes of rect 199 newpath.style = node.style 200 newpath.transform = node.transform 201 202 # top and bottom were exchanged 203 newpath.path = \ 204 drawfunction(self.options.xstart, 205 self.options.xend, 206 self.options.ybottom, 207 self.options.ytop, 208 self.options.samples, 209 w, h, x, y + h, 210 self.options.fofx, 211 self.options.fpofx, 212 self.options.fponum, 213 self.options.times2pi, 214 self.options.polar, 215 self.options.isoscale, 216 self.options.drawaxis, 217 self.options.endpts) 218 newpath.set('title', self.options.fofx) 219 220 # add path into SVG structure 221 node.getparent().append(newpath) 222 # option whether to clip the path with rect or not. 223 if self.options.clip: 224 clip = self.svg.defs.add(ClipPath()) 225 clip.set_random_id() 226 clip.append(node.copy()) 227 newpath.set('clip-path', clip.get_id(as_url=2)) 228 # option whether to remove the rectangle or not. 229 if self.options.remove: 230 node.getparent().remove(node) 231 if newpath is None: 232 raise inkex.AbortExtension(_("Please select a rectangle")) 233 234 235if __name__ == '__main__': 236 FuncPlot().run() 237