This repository has been archived on 2023-03-25. You can view files and clone it, but cannot push or open issues or pull requests.
mightyscape-0.92-deprecated/extensions/fablabchemnitz_mirror.py

94 lines
4.1 KiB
Python

#!/usr/bin/env python
"""
Derived from the "envelope" extension by Aaron Spike, aaron@ekips.org
By Apex 2011
New version Jens Kober 2017
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
"""
import inkex, cubicsuperpath, simpletransform
from ffgeom import *
class Project(inkex.Effect):
def __init__(self):
inkex.Effect.__init__(self)
def effect(self):
if len(self.options.ids) < 2:
inkex.errormsg(_("This extension requires two selected paths. \nThe second path must be exactly two nodes long."))
exit()
#trafo is selected second
obj = self.selected[self.options.ids[0]]
trafo = self.selected[self.options.ids[1]]
if trafo.tag == inkex.addNS('path','svg'):
#distil trafo into two node points
trafoPath = cubicsuperpath.parsePath(trafo.get('d'))
t = trafo.get("transform")
# if trafo has a transform, we need to translate the points to world coordinates first
if t != None:
m = simpletransform.parseTransform(t)
simpletransform.applyTransformToPath(m,trafoPath)
if len(trafoPath[0]) < 2:
inkex.errormsg(_("This extension requires that the second selected path be two nodes long."))
exit()
trafoPath = [[Point(csp[1][0],csp[1][1]) for csp in subs] for subs in trafoPath][0][:2]
#START HERE!!!
# origin of mirror line
ox = trafoPath[0]['x']
oy = trafoPath[0]['y']
# vector along mirror line
vx = trafoPath[1]['x'] - ox
vy = trafoPath[1]['y'] - oy
# the transformation first translates the origin of the mirror line to [0 0], then rotates the mirror line onto the x-axis,
# reflects everything over the x-axis, undoes the rotation, and finally undoes the translation
# alpha = atan2(vy, vx);
# [1 0 ox] [cos(alpha) -sin(alpha) 0] [1 0 0] [cos(-alpha) -sin(-alpha) 0] [1 0 -ox]
# Transformation = [0 1 oy]*[sin(alpha) cos(alpha) 0]*[0 -1 0]*[sin(-alpha) cos(-alpha) 0]*[0 1 -oy]
# [0 0 1] [ 0 0 1] [0 0 1] [ 0 0 1] [0 0 1]
# after some simplifications (or using your favorite symbolic math software):
# [(vx^2-vy^2)/(vx^2+vy^2) (2 vx vy)/(vx^2+vy^2) (2 vy (ox vy-oy vx))/(vx^2+vy^2)]
# Transformation = [ (2 vx vy)/(vx^2+vy^2) -(vx^2-vy^2)/(vx^2+vy^2) -(2 vx (ox vy-oy vx))/(vx^2+vy^2)]
# [ 0 0 1]
denom = vx**2 + vy**2
a00 = (vx**2 - vy**2) / denom
a01 = (2 * vx * vy) / denom
a02 = 2 * (ox * vy - oy * vx) / denom
mat=[[a00, a01, vy * a02], [a01, -a00, -vx * a02]]
simpletransform.applyTransformToNode(mat,obj)
else:
if trafo.tag == inkex.addNS('g','svg'):
inkex.errormsg(_("The second selected object is a group, not a path.\nTry using the procedure Object->Ungroup."))
else:
inkex.errormsg(_("The second selected object is not a path.\nTry using the procedure Path->Object to Path."))
exit()
if __name__ == '__main__':
e = Project()
e.affect()
# vim: expandtab shiftwidth=4 tabstop=8 softtabstop=4 encoding=utf-8 textwidth=99