forked from jdhoek/inkscape-isometric-projection
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathisometric_projection.py
163 lines (133 loc) · 5.9 KB
/
isometric_projection.py
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
#!/usr/bin/env python3
import math
import inkex
from inkex import Transform
inkex.localization.localize()
class IsometricProjectionTools(inkex.Effect):
"""
Convert a flat 2D projection to one of the three visible sides in an
isometric projection, and vice versa.
"""
attrTransformCenterX = inkex.addNS('transform-center-x', 'inkscape')
attrTransformCenterY = inkex.addNS('transform-center-y', 'inkscape')
# Precomputed values for sine, cosine, and tangent of 30°.
rad_30 = math.radians(30)
cos_30 = math.cos(rad_30)
sin_30 = 0.5 # No point in using math.sin for 30°.
tan_30 = math.tan(rad_30)
# Combined affine transformation matrices. The bottom row of these 3×3
# matrices is omitted; it is always [0, 0, 1].
transformations = {
# From 2D to isometric top down view:
# * scale vertically by cos(30°)
# * shear horizontally by -30°
# * rotate clock-wise 30°
'to_top': [[cos_30, -cos_30, 0],
[sin_30, sin_30, 0]],
# From 2D to isometric left-hand side view:
# * scale horizontally by cos(30°)
# * shear vertically by -30°
'to_left': [[cos_30, 0, 0],
[sin_30, 1, 0]],
# From 2D to isometric right-hand side view:
# * scale horizontally by cos(30°)
# * shear vertically by 30°
'to_right': [[cos_30, 0, 0],
[-sin_30, 1, 0]],
# From isometric top down view to 2D:
# * rotate counter-clock-wise 30°
# * shear horizontally by 30°
# * scale vertically by 1 / cos(30°)
'from_top': [[tan_30, 1, 0],
[-tan_30, 1, 0]],
# From isometric left-hand side view to 2D:
# * shear vertically by 30°
# * scale horizontally by 1 / cos(30°)
'from_left': [[1 / cos_30, 0, 0],
[-tan_30, 1, 0]],
# From isometric right-hand side view to 2D:
# * shear vertically by -30°
# * scale horizontally by 1 / cos(30°)
'from_right': [[1 / cos_30, 0, 0],
[tan_30, 1, 0]]
}
def __init__(self):
inkex.Effect.__init__(self)
self.arg_parser.add_argument('--conversion', default='top', help='Conversion to perform: (top|left|right)')
self.arg_parser.add_argument('--reverse', default="false", help='Reverse the transformation from isometric projection to flat 2D')
def getTransformCenter(self, midpoint, node):
"""
Find the transformation center of an object. If the user set it
manually by dragging it in Inkscape, those coordinates are used.
Otherwise, an attempt is made to find the center of the object's
bounding box.
"""
c_x = node.get(self.attrTransformCenterX)
c_y = node.get(self.attrTransformCenterY)
# Default to dead-center.
if c_x is None:
c_x = 0.0
else:
c_x = float(c_x)
if c_y is None:
c_y = 0.0
else:
c_y = float(c_y)
x = midpoint[0] + c_x
y = midpoint[1] - c_y
return [x, y]
def translateBetweenPoints(self, tr, here, there):
"""
Add a translation to a matrix that moves between two points.
"""
x = there[0] - here[0]
y = there[1] - here[1]
tr.add_translate(x, y)
def moveTransformationCenter(self, node, midpoint, center_new):
"""
If a transformation center is manually set on the node, move it to
match the transformation performed on the node.
"""
c_x = node.get(self.attrTransformCenterX)
c_y = node.get(self.attrTransformCenterY)
if c_x is not None:
x = str(center_new[0] - midpoint[0])
node.set(self.attrTransformCenterX, x)
if c_y is not None:
y = str(midpoint[1] - center_new[1])
node.set(self.attrTransformCenterY, y)
def effect(self):
if self.options.reverse == "true":
conversion = "from_" + self.options.conversion
else:
conversion = "to_" + self.options.conversion
if len(self.svg.selected) == 0:
inkex.errormsg(_("Please select an object to perform the " +
"isometric projection transformation on."))
return
# Default to the flat 2D to isometric top down view conversion if an
# invalid identifier is passed.
effect_matrix = self.transformations.get(
conversion, self.transformations.get('to_top'))
for id, node in self.svg.selected.items():
bbox = node.bounding_box()
midpoint = [bbox.center_x, bbox.center_y]
center_old = self.getTransformCenter(midpoint, node)
transform = node.get("transform")
# Combine our transformation matrix with any pre-existing
# transform.
tr = Transform(transform) * Transform(effect_matrix)
# Compute the location of the transformation center after applying
# the transformation matrix.
center_new = center_old[:]
#Transform(matrix).apply_to_point(center_new)
tr.apply_to_point(center_new)
tr.apply_to_point(midpoint)
# Add a translation transformation that will move the object to
# keep its transformation center in the same place.
self.translateBetweenPoints(tr, center_new, center_old)
node.set('transform', str(tr))
# Adjust the transformation center.
self.moveTransformationCenter(node, midpoint, center_new)
# Create effect instance and apply it.
IsometricProjectionTools().run()