Skip to content

Commit 541cba7

Browse files
authored
Merge pull request #1411 from mathics/add-pointsize-to-3d
Add pointsize to 3d
2 parents e4985e4 + b8968a0 commit 541cba7

File tree

2 files changed

+42
-20
lines changed

2 files changed

+42
-20
lines changed

mathics/builtin/drawing/graphics3d.py

Lines changed: 35 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
Expression,
1515
from_python,
1616
system_symbols_dict,
17+
Symbol,
1718
SymbolList,
1819
)
1920
from mathics.core.formatter import lookup_method
@@ -164,16 +165,24 @@ def _prepare_elements(self, leaves, options, max_width=None):
164165
if not leaves:
165166
raise BoxConstructError
166167

167-
graphics_options = self.get_option_values(leaves[1:], **options)
168-
169-
evaluation = options["evaluation"]
168+
self.graphics_options = self.get_option_values(leaves[1:], **options)
169+
background = self.graphics_options["System`Background"]
170+
if (
171+
isinstance(background, Symbol)
172+
and background.get_name() == "System`Automatic"
173+
):
174+
self.background_color = None
175+
else:
176+
self.background_color = _Color.create(background)
170177

171178
base_width, base_height, size_multiplier, size_aspect = self._get_image_size(
172-
options, graphics_options, max_width
179+
options, self.graphics_options, max_width
173180
)
174181

182+
evaluation = options["evaluation"]
183+
175184
# TODO: Handle ImageScaled[], and Scaled[]
176-
lighting_option = graphics_options["System`Lighting"]
185+
lighting_option = self.graphics_options["System`Lighting"]
177186
lighting = lighting_option.to_python() # can take symbols or strings
178187
self.lighting = []
179188

@@ -293,7 +302,7 @@ def _prepare_elements(self, leaves, options, max_width=None):
293302
evaluation.message("Graphics3D", "invlight", lighting_option)
294303

295304
# ViewPoint Option
296-
viewpoint_option = graphics_options["System`ViewPoint"]
305+
viewpoint_option = self.graphics_options["System`ViewPoint"]
297306
viewpoint = viewpoint_option.to_python(n_evaluation=evaluation)
298307

299308
if isinstance(viewpoint, list) and len(viewpoint) == 3:
@@ -323,17 +332,17 @@ def _prepare_elements(self, leaves, options, max_width=None):
323332
self.viewpoint = viewpoint
324333

325334
# TODO Aspect Ratio
326-
# aspect_ratio = graphics_options['AspectRatio'].to_python()
335+
# aspect_ratio = self.graphics_options['AspectRatio'].to_python()
327336

328-
boxratios = graphics_options["System`BoxRatios"].to_python()
337+
boxratios = self.graphics_options["System`BoxRatios"].to_python()
329338
if boxratios == "System`Automatic":
330339
boxratios = ["System`Automatic"] * 3
331340
else:
332341
boxratios = boxratios
333342
if not isinstance(boxratios, list) or len(boxratios) != 3:
334343
raise BoxConstructError
335344

336-
plot_range = graphics_options["System`PlotRange"].to_python()
345+
plot_range = self.graphics_options["System`PlotRange"].to_python()
337346
if plot_range == "System`Automatic":
338347
plot_range = ["System`Automatic"] * 3
339348
if not isinstance(plot_range, list) or len(plot_range) != 3:
@@ -425,12 +434,15 @@ def calc_dimensions(final_pass=True):
425434
for vp in self.viewpoint
426435
]
427436

428-
return xmin, xmax, ymin, ymax, zmin, zmax, boxscale
437+
w = 0 if (xmin is None or xmax is None) else xmax - xmin
438+
h = 0 if (ymin is None or ymax is None) else ymax - ymin
439+
440+
return xmin, xmax, ymin, ymax, zmin, zmax, boxscale, w, h
429441

430-
xmin, xmax, ymin, ymax, zmin, zmax, boxscale = calc_dimensions(final_pass=False)
442+
xmin, xmax, ymin, ymax, zmin, zmax, boxscale, w, h = calc_dimensions(final_pass=False)
431443

432444
axes, ticks, ticks_style = self.create_axes(
433-
elements, graphics_options, xmin, xmax, ymin, ymax, zmin, zmax, boxscale
445+
elements, self.graphics_options, xmin, xmax, ymin, ymax, zmin, zmax, boxscale
434446
)
435447

436448
return elements, axes, ticks, ticks_style, calc_dimensions, boxscale
@@ -451,7 +463,7 @@ def boxes_to_tex(self, leaves=None, **options):
451463
else:
452464
asy = elements.to_asy()
453465

454-
xmin, xmax, ymin, ymax, zmin, zmax, boxscale = calc_dimensions()
466+
xmin, xmax, ymin, ymax, zmin, zmax, boxscale, w, h = calc_dimensions()
455467

456468
# TODO: Intelligently place the axes on the longest non-middle edge.
457469
# See algorithm used by web graphics in mathics/web/media/graphics.js
@@ -643,12 +655,14 @@ def boxes_to_mathml(self, leaves=None, **options):
643655

644656
elements._apply_boxscaling(boxscale)
645657

658+
xmin, xmax, ymin, ymax, zmin, zmax, boxscale, w, h = calc_dimensions()
659+
elements.view_width = w
660+
646661
# FIXME: json is the only thing we can convert MathML into.
647662
# Handle other graphics formats.
648663
format_fn = lookup_method(elements, "json")
649-
json_repr = format_fn(elements, **options)
650664

651-
xmin, xmax, ymin, ymax, zmin, zmax, boxscale = calc_dimensions()
665+
json_repr = format_fn(elements, **options)
652666

653667
# TODO: Cubeoid (like this)
654668
# json_repr = [{'faceColor': (1, 1, 1, 1), 'position': [(0,0,0), None],
@@ -673,12 +687,12 @@ def boxes_to_mathml(self, leaves=None, **options):
673687

674688
# return "<mn>3</mn>"
675689

676-
# xml = ('<graphics3d xmin="%f" xmax="%f" ymin="%f" ymax="%f" '
690+
# mathml = ('<graphics3d xmin="%f" xmax="%f" ymin="%f" ymax="%f" '
677691
# 'zmin="%f" zmax="%f" data="%s" />') % (
678692
# xmin, xmax, ymin, ymax, zmin, zmax, json_repr)
679-
xml = '<graphics3d data="{0}" />'.format(html.escape(json_repr))
680-
xml = "<mtable><mtr><mtd>{0}</mtd></mtr></mtable>".format(xml)
681-
return xml
693+
mathml = '<graphics3d data="{0}" />'.format(html.escape(json_repr))
694+
mathml = "<mtable><mtr><mtd>{0}</mtd></mtr></mtable>".format(mathml)
695+
return mathml
682696

683697
def create_axes(
684698
self, elements, graphics_options, xmin, xmax, ymin, ymax, zmin, zmax, boxscale
@@ -795,6 +809,8 @@ def __init__(self, content, evaluation, neg_y=False):
795809
) = (
796810
self.pixel_width
797811
) = self.pixel_height = self.extent_width = self.extent_height = None
812+
self.view_width = None
813+
self.content = content
798814

799815
def extent(self, completely_visible_only=False):
800816
return total_extent_3d([element.extent() for element in self.elements])

mathics/formatter/json.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
Format a Mathics object as json
44
"""
55

6-
from mathics.builtin.graphics import RGBColor
6+
from mathics.builtin.graphics import PointSize, RGBColor
77

88
from mathics.builtin.drawing.graphics3d import (
99
Graphics3DElements,
@@ -59,12 +59,18 @@ def point_3d_box(self):
5959
if list(face_color.to_rgba()[:3]) == [1, 1, 1]:
6060
face_color = RGBColor(components=(0, 0, 0, face_color.to_rgba()[3]))
6161

62+
point_size, _ = self.style.get_style(PointSize, face_element=False)
63+
if point_size is None:
64+
point_size = PointSize(self.graphics, value=0.005)
65+
absolute_point_size = point_size.get_size()
66+
6267
for line in self.lines:
6368
data.append(
6469
{
6570
"type": "point",
6671
"coords": [coords.pos() for coords in line],
6772
"color": face_color.to_rgba(),
73+
"pointSize": absolute_point_size,
6874
}
6975
)
7076
# print("### json Point3DBox", data)

0 commit comments

Comments
 (0)