Skip to content

Commit 819e173

Browse files
committed
Minor refactoring for performance
1 parent c36c5a9 commit 819e173

File tree

1 file changed

+37
-38
lines changed

1 file changed

+37
-38
lines changed

lib/matplotlib/backends/backend_svg.py

Lines changed: 37 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -770,7 +770,7 @@ def draw_path_collection(self, gc, master_transform, paths, all_transforms,
770770

771771
self._path_collection_id += 1
772772

773-
def _draw_gouraud_triangle(self, gc, points, colors, trans):
773+
def _draw_gouraud_triangle(self, transformed_points, colors):
774774
# This uses a method described here:
775775
#
776776
# http://www.svgopen.org/2005/papers/Converting3DFaceToSVG/index.html
@@ -782,43 +782,17 @@ def _draw_gouraud_triangle(self, gc, points, colors, trans):
782782
# opposite edge. Underlying these three gradients is a solid
783783
# triangle whose color is the average of all three points.
784784

785-
writer = self.writer
786-
if not self._has_gouraud:
787-
self._has_gouraud = True
788-
writer.start(
789-
'filter',
790-
id='colorAdd')
791-
writer.element(
792-
'feComposite',
793-
attrib={'in': 'SourceGraphic'},
794-
in2='BackgroundImage',
795-
operator='arithmetic',
796-
k2="1", k3="1")
797-
writer.end('filter')
798-
# feColorMatrix filter to correct opacity
799-
writer.start(
800-
'filter',
801-
id='colorMat')
802-
writer.element(
803-
'feColorMatrix',
804-
attrib={'type': 'matrix'},
805-
values='1 0 0 0 0 \n0 1 0 0 0 \n0 0 1 0 0' +
806-
' \n1 1 1 1 0 \n0 0 0 0 1 ')
807-
writer.end('filter')
808-
809785
avg_color = np.average(colors, axis=0)
810786
if avg_color[-1] == 0:
811787
# Skip fully-transparent triangles
812788
return
813789

814-
trans_and_flip = self._make_flip_transform(trans)
815-
tpoints = trans_and_flip.transform(points)
816-
790+
writer = self.writer
817791
writer.start('defs')
818792
for i in range(3):
819-
x1, y1 = tpoints[i]
820-
x2, y2 = tpoints[(i + 1) % 3]
821-
x3, y3 = tpoints[(i + 2) % 3]
793+
x1, y1 = transformed_points[i]
794+
x2, y2 = transformed_points[(i + 1) % 3]
795+
x3, y3 = transformed_points[(i + 2) % 3]
822796
rgba_color = colors[i]
823797

824798
if x2 == x3:
@@ -858,9 +832,9 @@ def _draw_gouraud_triangle(self, gc, points, colors, trans):
858832
writer.end('defs')
859833

860834
# triangle formation using "path"
861-
dpath = "M " + _short_float_fmt(x1)+',' + _short_float_fmt(y1)
862-
dpath += " L " + _short_float_fmt(x2) + ',' + _short_float_fmt(y2)
863-
dpath += " " + _short_float_fmt(x3) + ',' + _short_float_fmt(y3) + " Z"
835+
dpath = (f"M {_short_float_fmt(x1)},{_short_float_fmt(y1)}"
836+
f" L {_short_float_fmt(x2)},{_short_float_fmt(y2)}"
837+
f" {_short_float_fmt(x3)},{_short_float_fmt(y3)} Z")
864838

865839
writer.element(
866840
'path',
@@ -902,11 +876,36 @@ def _draw_gouraud_triangle(self, gc, points, colors, trans):
902876

903877
def draw_gouraud_triangles(self, gc, triangles_array, colors_array,
904878
transform):
905-
self.writer.start('g', **self._get_clip_attrs(gc))
879+
writer = self.writer
880+
writer.start('g', **self._get_clip_attrs(gc))
906881
transform = transform.frozen()
907-
for tri, col in zip(triangles_array, colors_array):
908-
self._draw_gouraud_triangle(gc, tri, col, transform)
909-
self.writer.end('g')
882+
trans_and_flip = self._make_flip_transform(transform)
883+
884+
if not self._has_gouraud:
885+
self._has_gouraud = True
886+
writer.start(
887+
'filter',
888+
id='colorAdd')
889+
writer.element(
890+
'feComposite',
891+
attrib={'in': 'SourceGraphic'},
892+
in2='BackgroundImage',
893+
operator='arithmetic',
894+
k2="1", k3="1")
895+
writer.end('filter')
896+
# feColorMatrix filter to correct opacity
897+
writer.start(
898+
'filter',
899+
id='colorMat')
900+
writer.element(
901+
'feColorMatrix',
902+
attrib={'type': 'matrix'},
903+
values='1 0 0 0 0 \n0 1 0 0 0 \n0 0 1 0 0 \n1 1 1 1 0 \n0 0 0 0 1 ')
904+
writer.end('filter')
905+
906+
for points, colors in zip(triangles_array, colors_array):
907+
self._draw_gouraud_triangle(trans_and_flip.transform(points), colors)
908+
writer.end('g')
910909

911910
def option_scale_image(self):
912911
# docstring inherited

0 commit comments

Comments
 (0)