44Three-Dimensional Graphics
55"""
66
7+ from mathics .version import __version__ # noqa used in loading to check consistency.
78
9+ import html
10+ import json
811import numbers
9- from mathics . version import __version__ # noqa used in loading to check consistency.
12+
1013from mathics .core .expression import (
1114 Expression ,
1215 from_python ,
1316 system_symbols_dict ,
1417 SymbolList ,
1518)
19+ from mathics .core .formatter import lookup_method
20+
1621from mathics .builtin .base import BoxConstructError , Builtin , InstanceableBuiltin
1722from mathics .builtin .graphics import (
1823 Graphics ,
1924 GraphicsBox ,
2025 PolygonBox ,
21- create_pens ,
2226 _Color ,
2327 LineBox ,
2428 PointBox ,
2529 Style ,
2630 RGBColor ,
2731 get_class ,
28- asy_number ,
2932 CoordinatesError ,
3033 _GraphicsElements ,
3134)
3235
33- import json
34- import html
36+ from mathics .formatter .asy_fns import asy_create_pens , asy_number
3537
3638
3739def coords3D (value ):
@@ -443,7 +445,11 @@ def boxes_to_tex(self, leaves=None, **options):
443445
444446 elements ._apply_boxscaling (boxscale )
445447
446- asy = elements .to_asy ()
448+ format_fn = lookup_method (elements , "asy" )
449+ if format_fn is not None :
450+ asy = format_fn (elements )
451+ else :
452+ asy = elements .to_asy ()
447453
448454 xmin , xmax , ymin , ymax , zmin , zmax , boxscale = calc_dimensions ()
449455
@@ -466,11 +472,11 @@ def boxes_to_tex(self, leaves=None, **options):
466472
467473 for i , line in enumerate (boundbox_lines ):
468474 if i in axes_indices :
469- pen = create_pens (
475+ pen = asy_create_pens (
470476 edge_color = RGBColor (components = (0 , 0 , 0 , 1 )), stroke_width = 1.5
471477 )
472478 else :
473- pen = create_pens (
479+ pen = asy_create_pens (
474480 edge_color = RGBColor (components = (0.4 , 0.4 , 0.4 , 1 )), stroke_width = 1
475481 )
476482
@@ -483,7 +489,7 @@ def boxes_to_tex(self, leaves=None, **options):
483489
484490 # Draw axes ticks
485491 ticklength = 0.05 * max ([xmax - xmin , ymax - ymin , zmax - zmin ])
486- pen = create_pens (
492+ pen = asy_create_pens (
487493 edge_color = RGBColor (components = (0 , 0 , 0 , 1 )), stroke_width = 1.2
488494 )
489495 for xi in axes_indices :
@@ -778,9 +784,6 @@ def __init__(self, content, evaluation, neg_y=False):
778784 def extent (self , completely_visible_only = False ):
779785 return total_extent_3d ([element .extent () for element in self .elements ])
780786
781- def to_asy (self ):
782- return "\n " .join ([element .to_asy () for element in self .elements ])
783-
784787 def _apply_boxscaling (self , boxscale ):
785788 for element in self .elements :
786789 element ._apply_boxscaling (boxscale )
@@ -821,22 +824,6 @@ def to_json(self):
821824 )
822825 return data
823826
824- def to_asy (self ):
825- face_color = self .face_color
826-
827- # Tempoary bug fix: default Point color should be black not white
828- if list (face_color .to_rgba ()[:3 ]) == [1 , 1 , 1 ]:
829- face_color = RGBColor (components = (0 , 0 , 0 , face_color .to_rgba ()[3 ]))
830-
831- pen = create_pens (face_color = face_color , is_face_element = False )
832-
833- return "" .join (
834- "path3 g={0}--cycle;dot(g, {1});" .format (
835- "--" .join ("(%.5g,%.5g,%.5g)" % coords .pos ()[0 ] for coords in line ), pen
836- )
837- for line in self .lines
838- )
839-
840827 def extent (self ):
841828 result = []
842829 for line in self .lines :
@@ -871,18 +858,6 @@ def to_json(self):
871858 )
872859 return data
873860
874- def to_asy (self ):
875- # l = self.style.get_line_width(face_element=False)
876- pen = create_pens (edge_color = self .edge_color , stroke_width = 1 )
877-
878- return "" .join (
879- "draw({0}, {1});" .format (
880- "--" .join ("({0},{1},{2})" .format (* coords .pos ()[0 ]) for coords in line ),
881- pen ,
882- )
883- for line in self .lines
884- )
885-
886861 def extent (self ):
887862 result = []
888863 for line in self .lines :
@@ -930,29 +905,6 @@ def to_json(self):
930905 )
931906 return data
932907
933- def to_asy (self ):
934- l = self .style .get_line_width (face_element = True )
935- if self .vertex_colors is None :
936- face_color = self .face_color
937- else :
938- face_color = None
939- pen = create_pens (
940- edge_color = self .edge_color ,
941- face_color = face_color ,
942- stroke_width = l ,
943- is_face_element = True ,
944- )
945-
946- asy = ""
947- for line in self .lines :
948- asy += (
949- "path3 g="
950- + "--" .join (["(%.5g,%.5g,%.5g)" % coords .pos ()[0 ] for coords in line ])
951- + "--cycle;"
952- )
953- asy += "draw(surface(g), %s);" % (pen )
954- return asy
955-
956908 def extent (self ):
957909 result = []
958910 for line in self .lines :
@@ -1146,21 +1098,6 @@ def init(self, graphics, style, item):
11461098 self .points = [Coords3D (graphics , pos = point ) for point in points ]
11471099 self .radius = item .leaves [1 ].to_python ()
11481100
1149- def to_asy (self ):
1150- # l = self.style.get_line_width(face_element=True)
1151-
1152- if self .face_color is None :
1153- face_color = (1 , 1 , 1 )
1154- else :
1155- face_color = self .face_color .to_js ()
1156-
1157- return "" .join (
1158- "draw(surface(sphere({0}, {1})), rgb({2},{3},{4}));" .format (
1159- tuple (coord .pos ()[0 ]), self .radius , * face_color [:3 ]
1160- )
1161- for coord in self .points
1162- )
1163-
11641101 def to_json (self ):
11651102 face_color = self .face_color
11661103 if face_color is not None :
0 commit comments