22Common utilities for plotting
33"""
44
5- import itertools
65
6+ from mathics .core .atoms import NumericArray
77from mathics .core .convert .expression import to_mathics_list
88from mathics .core .expression import Expression
99from mathics .core .list import ListExpression
1010from mathics .core .systemsymbols import (
1111 SymbolGraphics ,
1212 SymbolGraphics3D ,
13+ SymbolGraphicsComplex ,
1314 SymbolLine ,
1415 SymbolPolygon ,
1516 SymbolRule ,
@@ -25,69 +26,57 @@ class GraphicsGenerator:
2526 Support for generating Graphics and Graphics3D expressions
2627 """
2728
28- # TODO: more precise types
29- # TODO: consider pre-zipping so only one for polys and one for lines
30- # TODO: consider whether we need to store these or can we just generate as we go?
31- poly_xyzs : list
32- poly_xyzs_colors : list
33- line_xyzs : list
34- line_xyzs_colors : list
29+ # TODO: more precise typing?
30+ graphics : list
3531
3632 # 2 or 3
3733 dim : int
3834
3935 def __init__ (self , dim : int ):
4036 self .dim = dim
41- self .poly_xyzs = []
42- self .poly_xyzs_colors = []
43- self .line_xyzs = []
44- self .line_xyzs_colors = []
37+ self .graphics = []
38+
39+ # add polygons and lines, optionally with vertex colors
40+ def add_thing (self , thing_symbol , things , colors ):
41+ arg = tuple (to_mathics_list (* thing ) for thing in things )
42+ arg = ListExpression (* arg ) if len (arg ) > 1 else arg [0 ]
43+ if colors :
44+ color_arg = tuple (to_mathics_list (* color ) for color in colors )
45+ color_arg = (
46+ ListExpression (* color_arg ) if len (color_arg ) > 1 else color_arg [0 ]
47+ )
48+ color_rule = Expression (SymbolRule , SymbolVertexColors , color_arg )
49+ self .graphics .append (Expression (thing_symbol , arg , color_rule ))
50+ else :
51+ self .graphics .append (Expression (thing_symbol , arg ))
4552
46- # TODO: is this correct if some polys have colors and some don't?
4753 def add_polyxyzs (self , poly_xyzs , colors = None ):
4854 """Add polygons specified by explicit xy[z] coordinates"""
49- self .poly_xyzs .append (poly_xyzs )
50- if colors :
51- self .poly_xyzs_colors .append (colors )
55+ self .add_thing (SymbolPolygon , poly_xyzs , colors )
5256
5357 def add_linexyzs (self , line_xyzs , colors = None ):
5458 """Add lines specified by explicit xy[z] coordinates"""
55- self .line_xyzs .append (line_xyzs )
56- if colors :
57- self .line_xyzs_colors .append (colors )
59+ self .add_thing (SymbolLine , line_xyzs , colors )
60+
61+ # TODO: color
62+ def add_complex (self , xyzs , lines = None , polys = None ):
63+ complex = [NumericArray (xyzs )]
64+ if polys is not None :
65+ polys_expr = Expression (SymbolPolygon , NumericArray (polys ))
66+ complex .append (polys_expr )
67+ if lines is not None :
68+ polys_expr = Expression (SymbolLines , NumericArray (lines ))
69+ complex .append (lines_expr )
70+ gc_expr = Expression (SymbolGraphicsComplex , * complex )
71+ self .graphics .append (gc_expr )
5872
5973 def generate (self , options ):
6074 """
6175 Generates Graphics[3D] expression from supplied lines, polygons (etc.)
6276 """
63-
64- # holds the elements of the final Graphics[3D] expr
65- graphics = []
66-
67- # add polygons and lines, optionally with vertex colors
68- def add_thing (thing_symbol , thingss , colorss ):
69- for things , colors in itertools .zip_longest (thingss , colorss ):
70- arg = tuple (to_mathics_list (* thing ) for thing in things )
71- arg = ListExpression (* arg ) if len (arg ) > 1 else arg [0 ]
72- if colors :
73- color_arg = tuple (to_mathics_list (* color ) for color in colors )
74- color_arg = (
75- ListExpression (* color_arg )
76- if len (color_arg ) > 1
77- else color_arg [0 ]
78- )
79- color_rule = Expression (SymbolRule , SymbolVertexColors , color_arg )
80- graphics .append (Expression (thing_symbol , arg , color_rule ))
81- else :
82- graphics .append (Expression (thing_symbol , arg ))
83-
84- add_thing (SymbolPolygon , self .poly_xyzs , self .poly_xyzs_colors )
85- add_thing (SymbolLine , self .line_xyzs , self .line_xyzs_colors )
86-
87- # generate Graphics[3D] expression
8877 graphics_expr = Expression (
8978 SymbolGraphics3D if self .dim == 3 else SymbolGraphics ,
90- ListExpression (* graphics ),
79+ ListExpression (* self . graphics ),
9180 * options ,
9281 )
9382
0 commit comments