Skip to content

Commit 2cb0b4a

Browse files
authored
Merge branch 'master' into new-plot-compile-functions-2
2 parents 5ac2049 + 6a09aab commit 2cb0b4a

File tree

3 files changed

+50
-27
lines changed

3 files changed

+50
-27
lines changed

mathics/eval/drawing/plot3d_vectorized.py

Lines changed: 43 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,8 @@
1717
from .util import GraphicsGenerator
1818

1919

20-
@Timer("eval_Plot3D")
21-
def eval_Plot3D(
22-
plot_options,
23-
evaluation: Evaluation,
24-
):
25-
graphics = GraphicsGenerator(dim=3)
20+
def make_plot(plot_options, evaluation: Evaluation, dim: int):
21+
graphics = GraphicsGenerator(dim)
2622

2723
# pull out plot options
2824
_, xmin, xmax = plot_options.ranges[0]
@@ -104,17 +100,39 @@ def compute_over_grid(nx, ny):
104100
# transpose and flatten to ((nx-1)*(ny-1), 4) array, suitable for use in GraphicsComplex
105101
quads = quads.T.reshape(-1, 4)
106102

107-
# choose a color
108-
rgb = palette[i % len(palette)]
109-
rgb = [c / 255.0 for c in rgb]
110-
# graphics.add_color(SymbolRGBColor, rgb)
111-
graphics.add_directives([SymbolRGBColor, *rgb])
112-
113-
# add a GraphicsComplex displaying a surface for this function
114-
graphics.add_complex(xyzs, lines=None, polys=quads)
115-
116-
# if requested by the Mesh attribute create a mesh of lines covering the surfaces
117-
if nmesh:
103+
# Plot3D
104+
if dim == 3:
105+
# choose a color
106+
rgb = palette[i % len(palette)]
107+
rgb = [c / 255.0 for c in rgb]
108+
# graphics.add_color(SymbolRGBColor, rgb)
109+
graphics.add_directives([SymbolRGBColor, *rgb])
110+
111+
# add a GraphicsComplex displaying a surface for this function
112+
graphics.add_complex(xyzs, lines=None, polys=quads)
113+
114+
# DensityPlot
115+
elif dim == 2:
116+
# Fixed palette for now
117+
# TODO: accept color options
118+
with Timer("compute colors"):
119+
zs = xyzs[:, 2]
120+
z_min, z_max = min(zs), max(zs)
121+
zs = zs[:, np.newaxis] # allow broadcasting
122+
c_min, c_max = [0.5, 0, 0.1], [1.0, 0.9, 0.5]
123+
c_min, c_max = (
124+
np.full((len(zs), 3), c_min),
125+
np.full((len(zs), 3), c_max),
126+
)
127+
colors = ((zs - z_min) * c_max + (z_max - zs) * c_min) / (z_max - z_min)
128+
129+
# flatten the points and add the quads
130+
graphics.add_complex(xyzs[:, 0:2], lines=None, polys=quads, colors=colors)
131+
132+
# If requested by the Mesh attribute create a mesh of lines covering the surfaces
133+
# For now only for Plot3D
134+
# TODO: mesh for DensityPlot?
135+
if nmesh and dim == 3:
118136
# meshes are black for now
119137
graphics.add_directives([SymbolRGBColor, 0, 0, 0])
120138

@@ -132,15 +150,17 @@ def compute_over_grid(nx, ny):
132150
return graphics
133151

134152

135-
#
136-
#
137-
#
153+
@Timer("eval_Plot3D")
154+
def eval_Plot3D(
155+
plot_options,
156+
evaluation: Evaluation,
157+
):
158+
return make_plot(plot_options, evaluation, dim=3)
138159

139160

161+
@Timer("eval_DensityPlot")
140162
def eval_DensityPlot(
141163
plot_options,
142164
evaluation: Evaluation,
143165
):
144-
# TODO
145-
# see plot3d.eval_DensityPlot for possible info on handling colors
146-
pass
166+
return make_plot(plot_options, evaluation, dim=2)

mathics/eval/drawing/util.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
SymbolGraphicsComplex,
1515
SymbolLine,
1616
SymbolPolygon,
17+
SymbolRGBColor,
1718
SymbolRule,
1819
SymbolVertexColors,
1920
)
@@ -75,14 +76,18 @@ def cvt(d):
7576
expr = cvt(d)
7677
self.graphics.append(expr)
7778

78-
def add_complex(self, xyzs, lines=None, polys=None):
79+
def add_complex(self, xyzs, lines=None, polys=None, colors=None):
7980
complex = [NumericArray(xyzs)]
8081
if polys is not None:
8182
polys_expr = Expression(SymbolPolygon, NumericArray(polys))
8283
complex.append(polys_expr)
8384
if lines is not None:
8485
lines_expr = Expression(SymbolLine, NumericArray(lines))
8586
complex.append(lines_expr)
87+
if colors is not None:
88+
colors_expr = Expression(SymbolRGBColor, NumericArray(colors))
89+
rule_expr = Expression(SymbolRule, SymbolVertexColors, colors_expr)
90+
complex.append(rule_expr)
8691
gc_expr = Expression(SymbolGraphicsComplex, *complex)
8792
self.graphics.append(gc_expr)
8893

test/timings/test_uniform_tables.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
import os
2-
import sys
3-
import time
4-
from test.helper import check_evaluation, evaluate, session
2+
from test.helper import session
53

64
import pytest
75

0 commit comments

Comments
 (0)