1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171
|
#!/usr/bin/env python
# coding=utf-8
#
# Copyright (C) 2005 Aaron Spike, aaron@ekips.org
#
# 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
"""
Perspective approach & math by Dmitry Platonov, shadowjack@mail.ru, 2006
"""
import inkex
from inkex.localization import inkex_gettext as _
X, Y = range(2)
try:
import numpy as np
import numpy.linalg as lin
FLOAT = np.float64
except ImportError:
np = None
class Perspective(inkex.EffectExtension):
"""Apply a perspective to a path/group of paths"""
def effect(self):
if np is None:
raise inkex.AbortExtension(
_(
"Failed to import the numpy or numpy.linalg modules. "
"These modules are required by this extension. Please install them."
" On a Debian-like system this can be done with the command, "
"sudo apt-get install python-numpy."
)
)
if len(self.svg.selection) != 2:
raise inkex.AbortExtension(
_("This extension requires two selected objects.")
)
obj, envelope = self.svg.selection
if isinstance(obj, (inkex.PathElement, inkex.Group)):
if isinstance(envelope, inkex.PathElement):
path = envelope.path.transform(
envelope.composed_transform()
).to_superpath()
if len(path) < 1 or len(path[0]) < 4:
raise inkex.AbortExtension(
_(
"This extension requires that the second path be four "
"nodes long."
)
)
dip = np.zeros((4, 2), dtype=FLOAT)
for i in range(4):
dip[i][0] = path[0][i][1][0]
dip[i][1] = path[0][i][1][1]
# Get bounding box plus any extra composed transform of parents.
bbox = obj.bounding_box(obj.getparent().composed_transform())
sip = np.array(
[
[bbox.left, bbox.bottom],
[bbox.left, bbox.top],
[bbox.right, bbox.top],
[bbox.right, bbox.bottom],
],
dtype=FLOAT,
)
else:
if isinstance(envelope, inkex.Group):
raise inkex.AbortExtension(
_(
"The second selected object is a group, not a"
" path.\nTry using Object->Ungroup."
)
)
raise inkex.AbortExtension(
_(
"The second selected object is not a path.\nTry using"
" the procedure Path->Object to Path."
)
)
else:
raise inkex.AbortExtension(
_(
"The first selected object is neither a path nor a group.\nTry "
"using the procedure Path->Object to Path."
)
)
solmatrix = np.zeros((8, 8), dtype=FLOAT)
free_term = np.zeros(8, dtype=FLOAT)
for i in (0, 1, 2, 3):
solmatrix[i][0] = sip[i][0]
solmatrix[i][1] = sip[i][1]
solmatrix[i][2] = 1
solmatrix[i][6] = -dip[i][0] * sip[i][0]
solmatrix[i][7] = -dip[i][0] * sip[i][1]
solmatrix[i + 4][3] = sip[i][0]
solmatrix[i + 4][4] = sip[i][1]
solmatrix[i + 4][5] = 1
solmatrix[i + 4][6] = -dip[i][1] * sip[i][0]
solmatrix[i + 4][7] = -dip[i][1] * sip[i][1]
free_term[i] = dip[i][0]
free_term[i + 4] = dip[i][1]
res = lin.solve(solmatrix, free_term)
projmatrix = np.array(
[[res[0], res[1], res[2]], [res[3], res[4], res[5]], [res[6], res[7], 1.0]],
dtype=FLOAT,
)
self.process_object(obj, projmatrix)
def process_object(self, obj, matrix):
if isinstance(obj, inkex.PathElement):
self.process_path(obj, matrix)
elif isinstance(obj, inkex.Group):
self.process_group(obj, matrix)
def process_group(self, group, matrix):
"""Go through all groups to process all paths inside them"""
for node in group:
self.process_object(node, matrix)
def process_path(self, element, matrix):
"""Apply the transformation to the selected path"""
point = (
element.path.to_absolute()
.transform(element.composed_transform())
.to_superpath()
)
for subs in point:
for csp in subs:
csp[0] = self.project_point(csp[0], matrix)
csp[1] = self.project_point(csp[1], matrix)
csp[2] = self.project_point(csp[2], matrix)
element.path = inkex.Path(point).transform(-element.composed_transform())
@staticmethod
def project_point(point, matrix):
"""Apply the matrix to the given point"""
return [
(point[X] * matrix[0][0] + point[Y] * matrix[0][1] + matrix[0][2])
/ (point[X] * matrix[2][0] + point[Y] * matrix[2][1] + matrix[2][2]),
(point[X] * matrix[1][0] + point[Y] * matrix[1][1] + matrix[1][2])
/ (point[X] * matrix[2][0] + point[Y] * matrix[2][1] + matrix[2][2]),
]
if __name__ == "__main__":
Perspective().run()
|