passagemath-plot 10.6.31rc3__cp314-cp314-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Potentially problematic release.
This version of passagemath-plot might be problematic. Click here for more details.
- passagemath_plot-10.6.31rc3.dist-info/METADATA +172 -0
- passagemath_plot-10.6.31rc3.dist-info/RECORD +82 -0
- passagemath_plot-10.6.31rc3.dist-info/WHEEL +6 -0
- passagemath_plot-10.6.31rc3.dist-info/top_level.txt +2 -0
- passagemath_plot.libs/libgfortran-83c28eba.so.5.0.0 +0 -0
- passagemath_plot.libs/libgsl-cda90e79.so.28.0.0 +0 -0
- passagemath_plot.libs/libopenblasp-r0-6dcb67f9.3.29.so +0 -0
- passagemath_plot.libs/libquadmath-2284e583.so.0.0.0 +0 -0
- sage/all__sagemath_plot.py +15 -0
- sage/ext_data/threejs/animation.css +195 -0
- sage/ext_data/threejs/animation.html +85 -0
- sage/ext_data/threejs/animation.js +273 -0
- sage/ext_data/threejs/fat_lines.js +48 -0
- sage/ext_data/threejs/threejs-version.txt +1 -0
- sage/ext_data/threejs/threejs_template.html +597 -0
- sage/interfaces/all__sagemath_plot.py +1 -0
- sage/interfaces/gnuplot.py +196 -0
- sage/interfaces/jmoldata.py +208 -0
- sage/interfaces/povray.py +56 -0
- sage/plot/all.py +42 -0
- sage/plot/animate.py +1796 -0
- sage/plot/arc.py +504 -0
- sage/plot/arrow.py +671 -0
- sage/plot/bar_chart.py +205 -0
- sage/plot/bezier_path.py +400 -0
- sage/plot/circle.py +435 -0
- sage/plot/colors.py +1606 -0
- sage/plot/complex_plot.cpython-314-x86_64-linux-gnu.so +0 -0
- sage/plot/complex_plot.pyx +1446 -0
- sage/plot/contour_plot.py +1792 -0
- sage/plot/density_plot.py +318 -0
- sage/plot/disk.py +373 -0
- sage/plot/ellipse.py +375 -0
- sage/plot/graphics.py +3580 -0
- sage/plot/histogram.py +354 -0
- sage/plot/hyperbolic_arc.py +404 -0
- sage/plot/hyperbolic_polygon.py +416 -0
- sage/plot/hyperbolic_regular_polygon.py +296 -0
- sage/plot/line.py +626 -0
- sage/plot/matrix_plot.py +629 -0
- sage/plot/misc.py +509 -0
- sage/plot/multigraphics.py +1294 -0
- sage/plot/plot.py +4183 -0
- sage/plot/plot3d/all.py +23 -0
- sage/plot/plot3d/base.cpython-314-x86_64-linux-gnu.so +0 -0
- sage/plot/plot3d/base.pxd +12 -0
- sage/plot/plot3d/base.pyx +3378 -0
- sage/plot/plot3d/implicit_plot3d.py +659 -0
- sage/plot/plot3d/implicit_surface.cpython-314-x86_64-linux-gnu.so +0 -0
- sage/plot/plot3d/implicit_surface.pyx +1453 -0
- sage/plot/plot3d/index_face_set.cpython-314-x86_64-linux-gnu.so +0 -0
- sage/plot/plot3d/index_face_set.pxd +32 -0
- sage/plot/plot3d/index_face_set.pyx +1873 -0
- sage/plot/plot3d/introduction.py +131 -0
- sage/plot/plot3d/list_plot3d.py +649 -0
- sage/plot/plot3d/parametric_plot3d.py +1130 -0
- sage/plot/plot3d/parametric_surface.cpython-314-x86_64-linux-gnu.so +0 -0
- sage/plot/plot3d/parametric_surface.pxd +12 -0
- sage/plot/plot3d/parametric_surface.pyx +893 -0
- sage/plot/plot3d/platonic.py +601 -0
- sage/plot/plot3d/plot3d.py +1442 -0
- sage/plot/plot3d/plot_field3d.py +162 -0
- sage/plot/plot3d/point_c.pxi +148 -0
- sage/plot/plot3d/revolution_plot3d.py +309 -0
- sage/plot/plot3d/shapes.cpython-314-x86_64-linux-gnu.so +0 -0
- sage/plot/plot3d/shapes.pxd +22 -0
- sage/plot/plot3d/shapes.pyx +1382 -0
- sage/plot/plot3d/shapes2.py +1512 -0
- sage/plot/plot3d/tachyon.py +1779 -0
- sage/plot/plot3d/texture.py +453 -0
- sage/plot/plot3d/transform.cpython-314-x86_64-linux-gnu.so +0 -0
- sage/plot/plot3d/transform.pxd +21 -0
- sage/plot/plot3d/transform.pyx +268 -0
- sage/plot/plot3d/tri_plot.py +589 -0
- sage/plot/plot_field.py +362 -0
- sage/plot/point.py +624 -0
- sage/plot/polygon.py +562 -0
- sage/plot/primitive.py +249 -0
- sage/plot/scatter_plot.py +199 -0
- sage/plot/step.py +85 -0
- sage/plot/streamline_plot.py +328 -0
- sage/plot/text.py +432 -0
|
@@ -0,0 +1,1512 @@
|
|
|
1
|
+
# sage_setup: distribution = sagemath-plot
|
|
2
|
+
r"""
|
|
3
|
+
Classes for Lines, Frames, Rulers, Spheres, Points, Dots, and Text
|
|
4
|
+
|
|
5
|
+
AUTHORS:
|
|
6
|
+
|
|
7
|
+
- William Stein (2007-12): initial version
|
|
8
|
+
|
|
9
|
+
- William Stein and Robert Bradshaw (2008-01): Many improvements
|
|
10
|
+
"""
|
|
11
|
+
# ****************************************************************************
|
|
12
|
+
# Copyright (C) 2007 William Stein <wstein@gmail.com>
|
|
13
|
+
# Copyright (C) 2008 Robert Bradshaw <robertwb@math.washington.edu>
|
|
14
|
+
#
|
|
15
|
+
# Distributed under the terms of the GNU General Public License (GPL)
|
|
16
|
+
#
|
|
17
|
+
# This code is distributed in the hope that it will be useful,
|
|
18
|
+
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
19
|
+
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
20
|
+
# General Public License for more details.
|
|
21
|
+
#
|
|
22
|
+
# The full text of the GPL is available at:
|
|
23
|
+
#
|
|
24
|
+
# https://www.gnu.org/licenses/
|
|
25
|
+
# ****************************************************************************
|
|
26
|
+
|
|
27
|
+
import math
|
|
28
|
+
|
|
29
|
+
from . import shapes
|
|
30
|
+
|
|
31
|
+
from .base import PrimitiveObject, point_list_bounding_box
|
|
32
|
+
|
|
33
|
+
from sage.rings.real_double import RDF
|
|
34
|
+
from sage.modules.free_module_element import vector
|
|
35
|
+
from sage.misc.decorators import options, rename_keyword
|
|
36
|
+
from sage.arith.srange import srange
|
|
37
|
+
|
|
38
|
+
from .texture import Texture
|
|
39
|
+
from .shapes import Text, Sphere
|
|
40
|
+
|
|
41
|
+
TACHYON_PIXEL = 1 / 200.0
|
|
42
|
+
|
|
43
|
+
|
|
44
|
+
@rename_keyword(alpha='opacity')
|
|
45
|
+
def line3d(points, thickness=1, radius=None, arrow_head=False, **kwds):
|
|
46
|
+
r"""
|
|
47
|
+
Draw a 3d line joining a sequence of points.
|
|
48
|
+
|
|
49
|
+
One may specify either a thickness or radius. If a thickness is
|
|
50
|
+
specified, this line will have a constant diameter regardless of
|
|
51
|
+
scaling and zooming. If a radius is specified, it will behave as a
|
|
52
|
+
series of cylinders.
|
|
53
|
+
|
|
54
|
+
INPUT:
|
|
55
|
+
|
|
56
|
+
- ``points`` -- list of at least 2 points
|
|
57
|
+
|
|
58
|
+
- ``thickness`` -- (default: 1)
|
|
59
|
+
|
|
60
|
+
- ``radius`` -- (default: ``None``)
|
|
61
|
+
|
|
62
|
+
- ``arrow_head`` -- (default: ``False``)
|
|
63
|
+
|
|
64
|
+
- ``color`` -- string (``'red'``, ``'green'`` etc)
|
|
65
|
+
or a tuple (r, g, b) with r, g, b numbers between 0 and 1
|
|
66
|
+
|
|
67
|
+
- ``opacity`` -- (default: 1) if less than 1 then is
|
|
68
|
+
transparent
|
|
69
|
+
|
|
70
|
+
EXAMPLES:
|
|
71
|
+
|
|
72
|
+
A line in 3-space::
|
|
73
|
+
|
|
74
|
+
sage: line3d([(1,2,3), (1,0,-2), (3,1,4), (2,1,-2)])
|
|
75
|
+
Graphics3d Object
|
|
76
|
+
|
|
77
|
+
.. PLOT::
|
|
78
|
+
|
|
79
|
+
sphinx_plot(line3d([(1,2,3), (1,0,-2), (3,1,4), (2,1,-2)]))
|
|
80
|
+
|
|
81
|
+
The same line but red::
|
|
82
|
+
|
|
83
|
+
sage: line3d([(1,2,3), (1,0,-2), (3,1,4), (2,1,-2)], color='red')
|
|
84
|
+
Graphics3d Object
|
|
85
|
+
|
|
86
|
+
.. PLOT::
|
|
87
|
+
|
|
88
|
+
sphinx_plot(line3d([(1,2,3), (1,0,-2), (3,1,4), (2,1,-2)], color='red'))
|
|
89
|
+
|
|
90
|
+
The points of the line provided as a numpy array::
|
|
91
|
+
|
|
92
|
+
sage: import numpy
|
|
93
|
+
sage: line3d(numpy.array([(1,2,3), (1,0,-2), (3,1,4), (2,1,-2)]))
|
|
94
|
+
Graphics3d Object
|
|
95
|
+
|
|
96
|
+
.. PLOT::
|
|
97
|
+
|
|
98
|
+
import numpy
|
|
99
|
+
sphinx_plot(line3d(numpy.array([(1,2,3), (1,0,-2), (3,1,4), (2,1,-2)])))
|
|
100
|
+
|
|
101
|
+
A transparent thick green line and a little blue line::
|
|
102
|
+
|
|
103
|
+
sage: line3d([(0,0,0), (1,1,1), (1,0,2)], opacity=0.5, radius=0.1,
|
|
104
|
+
....: color='green') + line3d([(0,1,0), (1,0,2)])
|
|
105
|
+
Graphics3d Object
|
|
106
|
+
|
|
107
|
+
.. PLOT::
|
|
108
|
+
|
|
109
|
+
sphinx_plot(line3d([(0,0,0), (1,1,1), (1,0,2)], opacity=0.5, radius=0.1, color='green') + line3d([(0,1,0), (1,0,2)]))
|
|
110
|
+
|
|
111
|
+
A Dodecahedral complex of 5 tetrahedra (a more elaborate example
|
|
112
|
+
from Peter Jipsen)::
|
|
113
|
+
|
|
114
|
+
sage: def tetra(col):
|
|
115
|
+
....: return line3d([(0,0,1), (2*sqrt(2.)/3,0,-1./3), (-sqrt(2.)/3, sqrt(6.)/3,-1./3),\
|
|
116
|
+
....: (-sqrt(2.)/3,-sqrt(6.)/3,-1./3), (0,0,1), (-sqrt(2.)/3, sqrt(6.)/3,-1./3),\
|
|
117
|
+
....: (-sqrt(2.)/3,-sqrt(6.)/3,-1./3), (2*sqrt(2.)/3,0,-1./3)],\
|
|
118
|
+
....: color=col, thickness=10, aspect_ratio=[1,1,1])
|
|
119
|
+
|
|
120
|
+
sage: from math import pi
|
|
121
|
+
sage: v = (sqrt(5.)/2-5/6, 5/6*sqrt(3.)-sqrt(15.)/2, sqrt(5.)/3)
|
|
122
|
+
sage: t = acos(sqrt(5.)/3)/2
|
|
123
|
+
sage: t1 = tetra('blue').rotateZ(t)
|
|
124
|
+
sage: t2 = tetra('red').rotateZ(t).rotate(v,2*pi/5)
|
|
125
|
+
sage: t3 = tetra('green').rotateZ(t).rotate(v,4*pi/5)
|
|
126
|
+
sage: t4 = tetra('yellow').rotateZ(t).rotate(v,6*pi/5)
|
|
127
|
+
sage: t5 = tetra('orange').rotateZ(t).rotate(v,8*pi/5)
|
|
128
|
+
sage: show(t1+t2+t3+t4+t5, frame=False)
|
|
129
|
+
|
|
130
|
+
.. PLOT::
|
|
131
|
+
|
|
132
|
+
def tetra(col):
|
|
133
|
+
return line3d([(0,0,1), (2*sqrt(2.)/3,0,-1./3), (-sqrt(2.)/3, sqrt(6.)/3,-1./3),\
|
|
134
|
+
(-sqrt(2.)/3,-sqrt(6.)/3,-1./3), (0,0,1), (-sqrt(2.)/3, sqrt(6.)/3,-1./3),\
|
|
135
|
+
(-sqrt(2.)/3,-sqrt(6.)/3,-1./3), (2*sqrt(2.)/3,0,-1./3)],\
|
|
136
|
+
color=col, thickness=10, aspect_ratio=[1,1,1])
|
|
137
|
+
v = (sqrt(5.)/2-5/6, 5/6*sqrt(3.)-sqrt(15.)/2, sqrt(5.)/3)
|
|
138
|
+
t = acos(sqrt(5.)/3)/2
|
|
139
|
+
t1 = tetra('blue').rotateZ(t)
|
|
140
|
+
t2 = tetra('red').rotateZ(t).rotate(v,2*pi/5)
|
|
141
|
+
t3 = tetra('green').rotateZ(t).rotate(v,4*pi/5)
|
|
142
|
+
t4 = tetra('yellow').rotateZ(t).rotate(v,6*pi/5)
|
|
143
|
+
t5 = tetra('orange').rotateZ(t).rotate(v,8*pi/5)
|
|
144
|
+
sphinx_plot(t1+t2+t3+t4+t5)
|
|
145
|
+
|
|
146
|
+
TESTS:
|
|
147
|
+
|
|
148
|
+
Copies are made of the input list, so the input list does not change::
|
|
149
|
+
|
|
150
|
+
sage: mypoints = [vector([1,2,3]), vector([4,5,6])]
|
|
151
|
+
sage: type(mypoints[0])
|
|
152
|
+
<... 'sage.modules.vector_integer_dense.Vector_integer_dense'>
|
|
153
|
+
sage: L = line3d(mypoints)
|
|
154
|
+
sage: type(mypoints[0])
|
|
155
|
+
<... 'sage.modules.vector_integer_dense.Vector_integer_dense'>
|
|
156
|
+
|
|
157
|
+
The copies are converted to a list, so we can pass in immutable objects too::
|
|
158
|
+
|
|
159
|
+
sage: L = line3d(((0,0,0),(1,2,3)))
|
|
160
|
+
|
|
161
|
+
This function should work for anything than can be turned into a
|
|
162
|
+
list, such as iterators and such (see :issue:`10478`)::
|
|
163
|
+
|
|
164
|
+
sage: line3d(iter([(0,0,0), (sqrt(3), 2, 4)])) # needs sage.symbolic
|
|
165
|
+
Graphics3d Object
|
|
166
|
+
sage: line3d((x, x^2, x^3) for x in range(5))
|
|
167
|
+
Graphics3d Object
|
|
168
|
+
sage: from builtins import zip
|
|
169
|
+
sage: line3d(zip([2,3,5,7], [11, 13, 17, 19], [-1, -2, -3, -4]))
|
|
170
|
+
Graphics3d Object
|
|
171
|
+
"""
|
|
172
|
+
points = list(points)
|
|
173
|
+
if len(points) < 2:
|
|
174
|
+
raise ValueError("there must be at least 2 points")
|
|
175
|
+
for i in range(len(points)):
|
|
176
|
+
x, y, z = points[i]
|
|
177
|
+
points[i] = float(x), float(y), float(z)
|
|
178
|
+
if radius is None:
|
|
179
|
+
L = Line(points, thickness=thickness, arrow_head=arrow_head, **kwds)
|
|
180
|
+
L._set_extra_kwds(kwds)
|
|
181
|
+
L._extra_kwds['thickness'] = thickness # remove this line if json_repr is defined
|
|
182
|
+
return L
|
|
183
|
+
else:
|
|
184
|
+
v = []
|
|
185
|
+
if 'texture' in kwds:
|
|
186
|
+
kwds = kwds.copy()
|
|
187
|
+
texture = kwds.pop('texture')
|
|
188
|
+
else:
|
|
189
|
+
texture = Texture(kwds)
|
|
190
|
+
for i in range(len(points) - 1):
|
|
191
|
+
line = shapes.arrow3d if i == len(points)-2 and arrow_head else shapes.LineSegment
|
|
192
|
+
v.append(line(points[i], points[i+1], texture=texture, radius=radius, **kwds))
|
|
193
|
+
w = sum(v)
|
|
194
|
+
w._set_extra_kwds(kwds)
|
|
195
|
+
return w
|
|
196
|
+
|
|
197
|
+
|
|
198
|
+
@rename_keyword(alpha='opacity')
|
|
199
|
+
@options(opacity=1, color='blue', aspect_ratio=[1, 1, 1], thickness=2)
|
|
200
|
+
def bezier3d(path, **options):
|
|
201
|
+
"""
|
|
202
|
+
Draw a 3-dimensional bezier path.
|
|
203
|
+
|
|
204
|
+
Input is similar to bezier_path, but each point in the path and
|
|
205
|
+
each control point is required to have 3 coordinates.
|
|
206
|
+
|
|
207
|
+
INPUT:
|
|
208
|
+
|
|
209
|
+
- ``path`` -- list of curves, which each is a list of points. See further
|
|
210
|
+
detail below
|
|
211
|
+
|
|
212
|
+
- ``thickness`` -- (default: 2)
|
|
213
|
+
|
|
214
|
+
- ``color`` -- string (``'red'``, ``'green'`` etc)
|
|
215
|
+
or a tuple (r, g, b) with r, g, b numbers between 0 and 1
|
|
216
|
+
|
|
217
|
+
- ``opacity`` -- (default: 1) if less than 1 then is
|
|
218
|
+
transparent
|
|
219
|
+
|
|
220
|
+
- ``aspect_ratio`` -- (default: [1,1,1])
|
|
221
|
+
|
|
222
|
+
The path is a list of curves, and each curve is a list of points.
|
|
223
|
+
Each point is a tuple (x,y,z).
|
|
224
|
+
|
|
225
|
+
The first curve contains the endpoints as the first and last point
|
|
226
|
+
in the list. All other curves assume a starting point given by the
|
|
227
|
+
last entry in the preceding list, and take the last point in the list
|
|
228
|
+
as their opposite endpoint. A curve can have 0, 1 or 2 control points
|
|
229
|
+
listed between the endpoints. In the input example for path below,
|
|
230
|
+
the first and second curves have 2 control points, the third has one,
|
|
231
|
+
and the fourth has no control points::
|
|
232
|
+
|
|
233
|
+
path = [[p1, c1, c2, p2], [c3, c4, p3], [c5, p4], [p5], ...]
|
|
234
|
+
|
|
235
|
+
In the case of no control points, a straight line will be drawn
|
|
236
|
+
between the two endpoints. If one control point is supplied, then
|
|
237
|
+
the curve at each of the endpoints will be tangent to the line from
|
|
238
|
+
that endpoint to the control point. Similarly, in the case of two
|
|
239
|
+
control points, at each endpoint the curve will be tangent to the line
|
|
240
|
+
connecting that endpoint with the control point immediately after or
|
|
241
|
+
immediately preceding it in the list.
|
|
242
|
+
|
|
243
|
+
So in our example above, the curve between p1 and p2 is tangent to the
|
|
244
|
+
line through p1 and c1 at p1, and tangent to the line through p2 and c2
|
|
245
|
+
at p2. Similarly, the curve between p2 and p3 is tangent to line(p2,c3)
|
|
246
|
+
at p2 and tangent to line(p3,c4) at p3. Curve(p3,p4) is tangent to
|
|
247
|
+
line(p3,c5) at p3 and tangent to line(p4,c5) at p4. Curve(p4,p5) is a
|
|
248
|
+
straight line.
|
|
249
|
+
|
|
250
|
+
EXAMPLES::
|
|
251
|
+
|
|
252
|
+
sage: path = [[(0,0,0),(.5,.1,.2),(.75,3,-1),(1,1,0)],
|
|
253
|
+
....: [(.5,1,.2),(1,.5,0)], [(.7,.2,.5)]]
|
|
254
|
+
sage: b = bezier3d(path, color='green'); b # needs sage.symbolic
|
|
255
|
+
Graphics3d Object
|
|
256
|
+
|
|
257
|
+
.. PLOT::
|
|
258
|
+
|
|
259
|
+
path = [[(0,0,0),(.5,.1,.2),(.75,3,-1),(1,1,0)],[(.5,1,.2),(1,.5,0)],[(.7,.2,.5)]]
|
|
260
|
+
sphinx_plot(bezier3d(path, color='green'))
|
|
261
|
+
|
|
262
|
+
To construct a simple curve, create a list containing a single list::
|
|
263
|
+
|
|
264
|
+
sage: path = [[(0,0,0),(1,0,0),(0,1,0),(0,1,1)]]
|
|
265
|
+
sage: curve = bezier3d(path, thickness=5, color='blue'); curve # needs sage.symbolic
|
|
266
|
+
Graphics3d Object
|
|
267
|
+
|
|
268
|
+
.. PLOT::
|
|
269
|
+
|
|
270
|
+
path = [[(0,0,0),(1,0,0),(0,1,0),(0,1,1)]]
|
|
271
|
+
sphinx_plot(bezier3d(path, thickness=5, color='blue'))
|
|
272
|
+
|
|
273
|
+
TESTS:
|
|
274
|
+
|
|
275
|
+
Check for :issue:`31640`::
|
|
276
|
+
|
|
277
|
+
sage: p2d = [[(3,0.0),(3,0.13),(2,0.2),(2,0.3)],
|
|
278
|
+
....: [(2.7,0.4),(2.6,0.5),(2.5,0.5)], [(2.3,0.5),(2.2,0.4),(2.1,0.3)]]
|
|
279
|
+
sage: bp = bezier_path(p2d) # needs sage.symbolic
|
|
280
|
+
sage: bp.plot3d() # needs sage.symbolic
|
|
281
|
+
Graphics3d Object
|
|
282
|
+
|
|
283
|
+
sage: p3d = [[(3,0,0),(3,0.1,0),(2.9,0.2,0),(2.8,0.3,0)],
|
|
284
|
+
....: [(2.7,0.4,0),(2,0.5,0),(2.5,0.5,0)],
|
|
285
|
+
....: [(2.3,0.5,0),(2.2,0.4,0),(2.1,0.3,0)]]
|
|
286
|
+
sage: bezier3d(p3d) # needs sage.symbolic
|
|
287
|
+
Graphics3d Object
|
|
288
|
+
"""
|
|
289
|
+
from . import parametric_plot3d as P3D
|
|
290
|
+
from sage.modules.free_module_element import vector
|
|
291
|
+
from sage.symbolic.ring import SR
|
|
292
|
+
|
|
293
|
+
p0 = vector(path[0][-1])
|
|
294
|
+
t = SR.var('t')
|
|
295
|
+
if len(path[0]) > 2:
|
|
296
|
+
B = (1-t)**3*vector(path[0][0])+3*t*(1-t)**2*vector(path[0][1])+3*t**2*(1-t)*vector(path[0][-2])+t**3*p0
|
|
297
|
+
G = P3D.parametric_plot3d(list(B), (0, 1), color=options['color'], aspect_ratio=options['aspect_ratio'], thickness=options['thickness'], opacity=options['opacity'])
|
|
298
|
+
else:
|
|
299
|
+
G = line3d([path[0][0], p0], color=options['color'], thickness=options['thickness'], opacity=options['opacity'])
|
|
300
|
+
|
|
301
|
+
for curve in path[1:]:
|
|
302
|
+
if len(curve) > 1:
|
|
303
|
+
p1 = vector(curve[0])
|
|
304
|
+
p2 = vector(curve[-2])
|
|
305
|
+
p3 = vector(curve[-1])
|
|
306
|
+
B = (1-t)**3*p0+3*t*(1-t)**2*p1+3*t**2*(1-t)*p2+t**3*p3
|
|
307
|
+
G += P3D.parametric_plot3d(list(B), (0, 1), color=options['color'], aspect_ratio=options['aspect_ratio'], thickness=options['thickness'], opacity=options['opacity'])
|
|
308
|
+
else:
|
|
309
|
+
G += line3d([p0, curve[0]], color=options['color'], thickness=options['thickness'], opacity=options['opacity'])
|
|
310
|
+
p0 = vector(curve[-1])
|
|
311
|
+
return G
|
|
312
|
+
|
|
313
|
+
|
|
314
|
+
@rename_keyword(alpha='opacity')
|
|
315
|
+
@options(opacity=1, color=(0, 0, 1))
|
|
316
|
+
def polygon3d(points, **options):
|
|
317
|
+
"""
|
|
318
|
+
Draw a polygon in 3d.
|
|
319
|
+
|
|
320
|
+
INPUT:
|
|
321
|
+
|
|
322
|
+
- ``points`` -- the vertices of the polygon
|
|
323
|
+
|
|
324
|
+
Type ``polygon3d.options`` for a dictionary of the default
|
|
325
|
+
options for polygons. You can change this to change
|
|
326
|
+
the defaults for all future polygons. Use ``polygon3d.reset()``
|
|
327
|
+
to reset to the default options.
|
|
328
|
+
|
|
329
|
+
EXAMPLES:
|
|
330
|
+
|
|
331
|
+
A simple triangle::
|
|
332
|
+
|
|
333
|
+
sage: polygon3d([[0,2,0], [1.5,1,3], [3,0,0]])
|
|
334
|
+
Graphics3d Object
|
|
335
|
+
|
|
336
|
+
.. PLOT::
|
|
337
|
+
|
|
338
|
+
sphinx_plot(polygon3d([[0,2,0], [1.5,1,3], [3,0,0]]))
|
|
339
|
+
|
|
340
|
+
Some modern art -- a random polygon::
|
|
341
|
+
|
|
342
|
+
sage: v = [(randrange(-5,5), randrange(-5,5), randrange(-5, 5))
|
|
343
|
+
....: for _ in range(10)]
|
|
344
|
+
sage: polygon3d(v)
|
|
345
|
+
Graphics3d Object
|
|
346
|
+
|
|
347
|
+
.. PLOT::
|
|
348
|
+
|
|
349
|
+
v = [(randrange(-5,5), randrange(-5,5), randrange(-5, 5)) for _ in range(10)]
|
|
350
|
+
sphinx_plot(polygon3d(v))
|
|
351
|
+
|
|
352
|
+
A bent transparent green triangle::
|
|
353
|
+
|
|
354
|
+
sage: polygon3d([[1, 2, 3], [0,1,0], [1,0,1], [3,0,0]],
|
|
355
|
+
....: color=(0,1,0), opacity=0.7)
|
|
356
|
+
Graphics3d Object
|
|
357
|
+
|
|
358
|
+
.. PLOT::
|
|
359
|
+
|
|
360
|
+
sphinx_plot(polygon3d([[1, 2, 3], [0,1,0], [1,0,1], [3,0,0]], color=(0,1,0), opacity=0.7))
|
|
361
|
+
|
|
362
|
+
This is the same as using ``alpha=0.7``::
|
|
363
|
+
|
|
364
|
+
sage: polygon3d([[1, 2, 3], [0,1,0], [1,0,1], [3,0,0]],
|
|
365
|
+
....: color=(0,1,0), alpha=0.7)
|
|
366
|
+
Graphics3d Object
|
|
367
|
+
|
|
368
|
+
.. PLOT::
|
|
369
|
+
|
|
370
|
+
sphinx_plot(polygon3d([[1, 2, 3], [0,1,0], [1,0,1], [3,0,0]], color=(0,1,0), alpha=0.7))
|
|
371
|
+
"""
|
|
372
|
+
from sage.plot.plot3d.index_face_set import IndexFaceSet
|
|
373
|
+
return IndexFaceSet([range(len(points))], points, **options)
|
|
374
|
+
|
|
375
|
+
|
|
376
|
+
@rename_keyword(alpha='opacity')
|
|
377
|
+
@options(opacity=1, color=(0, 0, 1))
|
|
378
|
+
def polygons3d(faces, points, **options):
|
|
379
|
+
"""
|
|
380
|
+
Draw the union of several polygons in 3d.
|
|
381
|
+
|
|
382
|
+
Useful to plot a polyhedron as just one :class:`IndexFaceSet`.
|
|
383
|
+
|
|
384
|
+
INPUT:
|
|
385
|
+
|
|
386
|
+
- ``faces`` -- list of faces, every face given by the list
|
|
387
|
+
of indices of its vertices
|
|
388
|
+
|
|
389
|
+
- ``points`` -- coordinates of the vertices in the union
|
|
390
|
+
|
|
391
|
+
EXAMPLES:
|
|
392
|
+
|
|
393
|
+
Two adjacent triangles::
|
|
394
|
+
|
|
395
|
+
sage: f = [[0,1,2],[1,2,3]]
|
|
396
|
+
sage: v = [(-1,0,0),(0,1,1),(0,-1,1),(1,0,0)]
|
|
397
|
+
sage: polygons3d(f, v, color='red')
|
|
398
|
+
Graphics3d Object
|
|
399
|
+
|
|
400
|
+
.. PLOT::
|
|
401
|
+
|
|
402
|
+
f = [[0,1,2],[1,2,3]]
|
|
403
|
+
v = [(-1,0,0),(0,1,1),(0,-1,1),(1,0,0)]
|
|
404
|
+
sphinx_plot(polygons3d(f, v, color='red'))
|
|
405
|
+
"""
|
|
406
|
+
from sage.plot.plot3d.index_face_set import IndexFaceSet
|
|
407
|
+
return IndexFaceSet(faces, points, **options)
|
|
408
|
+
|
|
409
|
+
|
|
410
|
+
def frame3d(lower_left, upper_right, **kwds):
|
|
411
|
+
"""
|
|
412
|
+
Draw a frame in 3-D.
|
|
413
|
+
|
|
414
|
+
Primarily used as a helper function for creating frames for 3-D
|
|
415
|
+
graphics viewing.
|
|
416
|
+
|
|
417
|
+
INPUT:
|
|
418
|
+
|
|
419
|
+
- ``lower_left`` -- the lower left corner of the frame, as a
|
|
420
|
+
list, tuple, or vector
|
|
421
|
+
|
|
422
|
+
- ``upper_right`` -- the upper right corner of the frame, as a
|
|
423
|
+
list, tuple, or vector
|
|
424
|
+
|
|
425
|
+
EXAMPLES:
|
|
426
|
+
|
|
427
|
+
A frame::
|
|
428
|
+
|
|
429
|
+
sage: from sage.plot.plot3d.shapes2 import frame3d
|
|
430
|
+
sage: frame3d([1,3,2],vector([2,5,4]),color='red')
|
|
431
|
+
Graphics3d Object
|
|
432
|
+
|
|
433
|
+
This is usually used for making an actual plot::
|
|
434
|
+
|
|
435
|
+
sage: y = var('y') # needs sage.symbolic
|
|
436
|
+
sage: plot3d(sin(x^2+y^2), (x,0,pi), (y,0,pi)) # needs sage.symbolic
|
|
437
|
+
Graphics3d Object
|
|
438
|
+
"""
|
|
439
|
+
x0, y0, z0 = lower_left
|
|
440
|
+
x1, y1, z1 = upper_right
|
|
441
|
+
L1 = line3d([(x0, y0, z0), (x0, y1, z0), (x1, y1, z0),
|
|
442
|
+
(x1, y0, z0), (x0, y0, z0), # top square
|
|
443
|
+
(x0, y0, z1), (x0, y1, z1), (x1, y1, z1),
|
|
444
|
+
(x1, y0, z1), (x0, y0, z1)], # bottom square
|
|
445
|
+
**kwds)
|
|
446
|
+
# 3 additional lines joining top to bottom
|
|
447
|
+
v2 = line3d([(x0, y1, z0), (x0, y1, z1)], **kwds)
|
|
448
|
+
v3 = line3d([(x1, y0, z0), (x1, y0, z1)], **kwds)
|
|
449
|
+
v4 = line3d([(x1, y1, z0), (x1, y1, z1)], **kwds)
|
|
450
|
+
F = L1 + v2 + v3 + v4
|
|
451
|
+
F._set_extra_kwds(kwds)
|
|
452
|
+
return F
|
|
453
|
+
|
|
454
|
+
|
|
455
|
+
def frame_labels(lower_left, upper_right,
|
|
456
|
+
label_lower_left, label_upper_right, eps=1,
|
|
457
|
+
**kwds):
|
|
458
|
+
"""
|
|
459
|
+
Draw correct labels for a given frame in 3-D.
|
|
460
|
+
|
|
461
|
+
Primarily used as a helper function for creating frames for 3-D
|
|
462
|
+
graphics viewing - do not use directly unless you know what you
|
|
463
|
+
are doing!
|
|
464
|
+
|
|
465
|
+
INPUT:
|
|
466
|
+
|
|
467
|
+
- ``lower_left`` -- the lower left corner of the frame, as a
|
|
468
|
+
list, tuple, or vector
|
|
469
|
+
|
|
470
|
+
- ``upper_right`` -- the upper right corner of the frame, as a
|
|
471
|
+
list, tuple, or vector
|
|
472
|
+
|
|
473
|
+
- ``label_lower_left`` -- the label for the lower left corner
|
|
474
|
+
of the frame, as a list, tuple, or vector. This label must actually
|
|
475
|
+
have all coordinates less than the coordinates of the other label.
|
|
476
|
+
|
|
477
|
+
- ``label_upper_right`` -- the label for the upper right corner
|
|
478
|
+
of the frame, as a list, tuple, or vector. This label must actually
|
|
479
|
+
have all coordinates greater than the coordinates of the other label.
|
|
480
|
+
|
|
481
|
+
- ``eps`` -- (default: 1) a parameter for how far away from the frame
|
|
482
|
+
to put the labels
|
|
483
|
+
|
|
484
|
+
EXAMPLES:
|
|
485
|
+
|
|
486
|
+
We can use it directly::
|
|
487
|
+
|
|
488
|
+
sage: from sage.plot.plot3d.shapes2 import frame_labels
|
|
489
|
+
sage: frame_labels([1,2,3],[4,5,6],[1,2,3],[4,5,6])
|
|
490
|
+
Graphics3d Object
|
|
491
|
+
|
|
492
|
+
This is usually used for making an actual plot::
|
|
493
|
+
|
|
494
|
+
sage: # needs sage.symbolic
|
|
495
|
+
sage: y = var('y')
|
|
496
|
+
sage: P = plot3d(sin(x^2+y^2), (x,0,pi), (y,0,pi))
|
|
497
|
+
sage: a,b = P._rescale_for_frame_aspect_ratio_and_zoom(1.0,[1,1,1],1)
|
|
498
|
+
sage: F = frame_labels(a, b, *P._box_for_aspect_ratio("automatic",a,b))
|
|
499
|
+
sage: F.jmol_repr(F.default_render_params())[0]
|
|
500
|
+
[['select atomno = 1', 'color atom [76,76,76]', 'label "0.0"']]
|
|
501
|
+
|
|
502
|
+
TESTS::
|
|
503
|
+
|
|
504
|
+
sage: frame_labels([1,2,3],[4,5,6],[1,2,3],[1,3,4])
|
|
505
|
+
Traceback (most recent call last):
|
|
506
|
+
...
|
|
507
|
+
ValueError: ensure the upper right labels are above and to the right of the lower left labels
|
|
508
|
+
"""
|
|
509
|
+
x0, y0, z0 = lower_left
|
|
510
|
+
x1, y1, z1 = upper_right
|
|
511
|
+
lx0, ly0, lz0 = label_lower_left
|
|
512
|
+
lx1, ly1, lz1 = label_upper_right
|
|
513
|
+
if (lx1 - lx0) <= 0 or (ly1 - ly0) <= 0 or (lz1 - lz0) <= 0:
|
|
514
|
+
raise ValueError("ensure the upper right labels are above "
|
|
515
|
+
"and to the right of the lower left labels")
|
|
516
|
+
|
|
517
|
+
# Helper function for formatting the frame labels
|
|
518
|
+
from math import log
|
|
519
|
+
log10 = log(10)
|
|
520
|
+
|
|
521
|
+
def nd(a):
|
|
522
|
+
return int(log(a) / log10)
|
|
523
|
+
|
|
524
|
+
def fmt_string(a):
|
|
525
|
+
b = a / 2.0
|
|
526
|
+
if b >= 1:
|
|
527
|
+
return "%.1f"
|
|
528
|
+
n = max(0, 2 - nd(a/2.0))
|
|
529
|
+
return "%%.%sf" % n
|
|
530
|
+
|
|
531
|
+
# Slightly faster than mean for this situation
|
|
532
|
+
def avg(a, b):
|
|
533
|
+
return (a + b) / 2.0
|
|
534
|
+
|
|
535
|
+
color = (0.3, 0.3, 0.3)
|
|
536
|
+
|
|
537
|
+
fmt = fmt_string(lx1 - lx0)
|
|
538
|
+
T = Text(fmt % lx0, color=color).translate((x0, y0-eps, z0))
|
|
539
|
+
T += Text(fmt % avg(lx0, lx1), color=color).translate((avg(x0, x1), y0-eps, z0))
|
|
540
|
+
T += Text(fmt % lx1, color=color).translate((x1, y0-eps, z0))
|
|
541
|
+
|
|
542
|
+
fmt = fmt_string(ly1 - ly0)
|
|
543
|
+
T += Text(fmt % ly0, color=color).translate((x1+eps, y0, z0))
|
|
544
|
+
T += Text(fmt % avg(ly0, ly1), color=color).translate((x1+eps, avg(y0, y1), z0))
|
|
545
|
+
T += Text(fmt % ly1, color=color).translate((x1+eps, y1, z0))
|
|
546
|
+
|
|
547
|
+
fmt = fmt_string(lz1 - lz0)
|
|
548
|
+
T += Text(fmt % lz0, color=color).translate((x0-eps, y0, z0))
|
|
549
|
+
T += Text(fmt % avg(lz0, lz1), color=color).translate((x0-eps, y0, avg(z0, z1)))
|
|
550
|
+
T += Text(fmt % lz1, color=color).translate((x0-eps, y0, z1))
|
|
551
|
+
return T
|
|
552
|
+
|
|
553
|
+
|
|
554
|
+
def ruler(start, end, ticks=4, sub_ticks=4, absolute=False, snap=False, **kwds):
|
|
555
|
+
"""
|
|
556
|
+
Draw a ruler in 3-D, with major and minor ticks.
|
|
557
|
+
|
|
558
|
+
INPUT:
|
|
559
|
+
|
|
560
|
+
- ``start`` -- the beginning of the ruler, as a list,
|
|
561
|
+
tuple, or vector
|
|
562
|
+
|
|
563
|
+
- ``end`` -- the end of the ruler, as a list, tuple,
|
|
564
|
+
or vector
|
|
565
|
+
|
|
566
|
+
- ``ticks`` -- (default: 4) the number of major ticks
|
|
567
|
+
shown on the ruler
|
|
568
|
+
|
|
569
|
+
- ``sub_ticks`` -- (default: 4) the number of shown
|
|
570
|
+
subdivisions between each major tick
|
|
571
|
+
|
|
572
|
+
- ``absolute`` -- boolean (default: ``False``); if ``True``, makes a huge ruler
|
|
573
|
+
in the direction of an axis
|
|
574
|
+
|
|
575
|
+
- ``snap`` -- boolean (default: ``False``); if ``True``, snaps to an implied
|
|
576
|
+
grid
|
|
577
|
+
|
|
578
|
+
EXAMPLES:
|
|
579
|
+
|
|
580
|
+
A ruler::
|
|
581
|
+
|
|
582
|
+
sage: from sage.plot.plot3d.shapes2 import ruler
|
|
583
|
+
sage: R = ruler([4, 2, 1],vector([3, 3, 2])); R
|
|
584
|
+
Graphics3d Object
|
|
585
|
+
|
|
586
|
+
.. PLOT::
|
|
587
|
+
|
|
588
|
+
from sage.plot.plot3d.shapes2 import ruler
|
|
589
|
+
sphinx_plot(ruler([4, 2, 1],vector([3, 3, 2])))
|
|
590
|
+
|
|
591
|
+
A ruler with some options::
|
|
592
|
+
|
|
593
|
+
sage: R = ruler([4, 2, 1],vector([3, 3, 2]),ticks=6, sub_ticks=2, color='red'); R
|
|
594
|
+
Graphics3d Object
|
|
595
|
+
|
|
596
|
+
.. PLOT::
|
|
597
|
+
|
|
598
|
+
from sage.plot.plot3d.shapes2 import ruler
|
|
599
|
+
sphinx_plot(ruler([4, 2, 1],vector([3, 3, 2]),ticks=6, sub_ticks=2, color='red'))
|
|
600
|
+
|
|
601
|
+
The keyword ``snap`` makes the ticks not necessarily coincide
|
|
602
|
+
with the ruler::
|
|
603
|
+
|
|
604
|
+
sage: ruler([4, 2, 1],vector([3, 3, 2]),snap=True)
|
|
605
|
+
Graphics3d Object
|
|
606
|
+
|
|
607
|
+
.. PLOT::
|
|
608
|
+
|
|
609
|
+
from sage.plot.plot3d.shapes2 import ruler
|
|
610
|
+
sphinx_plot(ruler([4, 2, 1],vector([3, 3, 2]), snap=True))
|
|
611
|
+
|
|
612
|
+
The keyword ``absolute`` makes a huge ruler in one of the axis
|
|
613
|
+
directions::
|
|
614
|
+
|
|
615
|
+
sage: ruler([1,2,3],vector([1,2,4]),absolute=True)
|
|
616
|
+
Graphics3d Object
|
|
617
|
+
|
|
618
|
+
.. PLOT::
|
|
619
|
+
|
|
620
|
+
from sage.plot.plot3d.shapes2 import ruler
|
|
621
|
+
sphinx_plot(ruler([1,2,3],vector([1,2,4]), absolute=True))
|
|
622
|
+
|
|
623
|
+
TESTS::
|
|
624
|
+
|
|
625
|
+
sage: ruler([1,2,3],vector([1,3,4]),absolute=True)
|
|
626
|
+
Traceback (most recent call last):
|
|
627
|
+
...
|
|
628
|
+
ValueError: absolute rulers only valid for axis-aligned paths
|
|
629
|
+
"""
|
|
630
|
+
start = vector(RDF, start)
|
|
631
|
+
end = vector(RDF, end)
|
|
632
|
+
dir = end - start
|
|
633
|
+
dist = math.sqrt(dir.dot_product(dir))
|
|
634
|
+
dir /= dist
|
|
635
|
+
|
|
636
|
+
one_tick = dist/ticks * 1.414
|
|
637
|
+
unit = 10 ** math.floor(math.log(dist/ticks, 10))
|
|
638
|
+
if unit * 5 < one_tick:
|
|
639
|
+
unit *= 5
|
|
640
|
+
elif unit * 2 < one_tick:
|
|
641
|
+
unit *= 2
|
|
642
|
+
|
|
643
|
+
if dir[0]:
|
|
644
|
+
tick = dir.cross_product(vector(RDF, (0, 0, -dist/30)))
|
|
645
|
+
elif dir[1]:
|
|
646
|
+
tick = dir.cross_product(vector(RDF, (0, 0, dist/30)))
|
|
647
|
+
else:
|
|
648
|
+
tick = vector(RDF, (dist/30, 0, 0))
|
|
649
|
+
|
|
650
|
+
if snap:
|
|
651
|
+
for i in range(3):
|
|
652
|
+
start[i] = unit * math.floor(start[i]/unit + 1e-5)
|
|
653
|
+
end[i] = unit * math.ceil(end[i]/unit - 1e-5)
|
|
654
|
+
|
|
655
|
+
if absolute:
|
|
656
|
+
if dir[0]*dir[1] or dir[1]*dir[2] or dir[0]*dir[2]:
|
|
657
|
+
raise ValueError("absolute rulers only valid for axis-aligned paths")
|
|
658
|
+
m = max(dir[0], dir[1], dir[2])
|
|
659
|
+
if dir[0] == m:
|
|
660
|
+
off = start[0]
|
|
661
|
+
elif dir[1] == m:
|
|
662
|
+
off = start[1]
|
|
663
|
+
else:
|
|
664
|
+
off = start[2]
|
|
665
|
+
first_tick = unit * math.ceil(off/unit - 1e-5) - off
|
|
666
|
+
else:
|
|
667
|
+
off = 0
|
|
668
|
+
first_tick = 0
|
|
669
|
+
|
|
670
|
+
ruler = shapes.LineSegment(start, end, **kwds)
|
|
671
|
+
for k in range(1, int(sub_ticks * first_tick/unit)):
|
|
672
|
+
P = start + dir*(k*unit/sub_ticks)
|
|
673
|
+
ruler += shapes.LineSegment(P, P + tick/2, **kwds)
|
|
674
|
+
for d in srange(first_tick, dist + unit/(sub_ticks+1), unit):
|
|
675
|
+
P = start + dir*d
|
|
676
|
+
ruler += shapes.LineSegment(P, P + tick, **kwds)
|
|
677
|
+
ruler += shapes.Text(str(d+off), **kwds).translate(P - tick)
|
|
678
|
+
if dist - d < unit:
|
|
679
|
+
sub_ticks = int(sub_ticks * (dist - d)/unit)
|
|
680
|
+
for k in range(1, sub_ticks):
|
|
681
|
+
P += dir * (unit/sub_ticks)
|
|
682
|
+
ruler += shapes.LineSegment(P, P + tick/2, **kwds)
|
|
683
|
+
return ruler
|
|
684
|
+
|
|
685
|
+
|
|
686
|
+
def ruler_frame(lower_left, upper_right, ticks=4, sub_ticks=4, **kwds):
|
|
687
|
+
"""
|
|
688
|
+
Draw a frame made of 3-D rulers, with major and minor ticks.
|
|
689
|
+
|
|
690
|
+
INPUT:
|
|
691
|
+
|
|
692
|
+
- ``lower_left`` -- the lower left corner of the frame, as a
|
|
693
|
+
list, tuple, or vector
|
|
694
|
+
|
|
695
|
+
- ``upper_right`` -- the upper right corner of the frame, as a
|
|
696
|
+
list, tuple, or vector
|
|
697
|
+
|
|
698
|
+
- ``ticks`` -- (default: 4) the number of major ticks
|
|
699
|
+
shown on each ruler
|
|
700
|
+
|
|
701
|
+
- ``sub_ticks`` -- (default: 4) the number of shown
|
|
702
|
+
subdivisions between each major tick
|
|
703
|
+
|
|
704
|
+
EXAMPLES:
|
|
705
|
+
|
|
706
|
+
A ruler frame::
|
|
707
|
+
|
|
708
|
+
sage: from sage.plot.plot3d.shapes2 import ruler_frame
|
|
709
|
+
sage: F = ruler_frame([1,2,3],vector([2,3,4])); F
|
|
710
|
+
Graphics3d Object
|
|
711
|
+
|
|
712
|
+
.. PLOT::
|
|
713
|
+
|
|
714
|
+
from sage.plot.plot3d.shapes2 import ruler_frame
|
|
715
|
+
sphinx_plot(ruler_frame([1,2,3],vector([2,3,4])))
|
|
716
|
+
|
|
717
|
+
A ruler frame with some options::
|
|
718
|
+
|
|
719
|
+
sage: F = ruler_frame([1,2,3],vector([2,3,4]),ticks=6, sub_ticks=2, color='red'); F
|
|
720
|
+
Graphics3d Object
|
|
721
|
+
|
|
722
|
+
.. PLOT::
|
|
723
|
+
|
|
724
|
+
from sage.plot.plot3d.shapes2 import ruler_frame
|
|
725
|
+
sphinx_plot(ruler_frame([1,2,3],vector([2,3,4]),ticks=6, sub_ticks=2, color='red'))
|
|
726
|
+
"""
|
|
727
|
+
return ruler(lower_left, (upper_right[0], lower_left[1], lower_left[2]), ticks=ticks, sub_ticks=sub_ticks, absolute=True, **kwds) \
|
|
728
|
+
+ ruler(lower_left, (lower_left[0], upper_right[1], lower_left[2]), ticks=ticks, sub_ticks=sub_ticks, absolute=True, **kwds) \
|
|
729
|
+
+ ruler(lower_left, (lower_left[0], lower_left[1], upper_right[2]), ticks=ticks, sub_ticks=sub_ticks, absolute=True, **kwds)
|
|
730
|
+
|
|
731
|
+
|
|
732
|
+
###########################
|
|
733
|
+
|
|
734
|
+
@rename_keyword(alpha='opacity')
|
|
735
|
+
def sphere(center=(0, 0, 0), size=1, **kwds):
|
|
736
|
+
r"""
|
|
737
|
+
Return a plot of a sphere of radius ``size`` centered at
|
|
738
|
+
`(x,y,z)`.
|
|
739
|
+
|
|
740
|
+
INPUT:
|
|
741
|
+
|
|
742
|
+
- `(x,y,z)` -- center (default: (0,0,0))
|
|
743
|
+
|
|
744
|
+
- ``size`` -- the radius (default: 1)
|
|
745
|
+
|
|
746
|
+
EXAMPLES: A simple sphere::
|
|
747
|
+
|
|
748
|
+
sage: sphere()
|
|
749
|
+
Graphics3d Object
|
|
750
|
+
|
|
751
|
+
Two spheres touching::
|
|
752
|
+
|
|
753
|
+
sage: sphere(center=(-1,0,0)) + sphere(center=(1,0,0), aspect_ratio=[1,1,1])
|
|
754
|
+
Graphics3d Object
|
|
755
|
+
|
|
756
|
+
.. PLOT::
|
|
757
|
+
|
|
758
|
+
sphinx_plot(sphere(center=(-1,0,0)) + sphere(center=(1,0,0), aspect_ratio=[1,1,1]))
|
|
759
|
+
|
|
760
|
+
Spheres of radii 1 and 2 one stuck into the other::
|
|
761
|
+
|
|
762
|
+
sage: sphere(color='orange') + sphere(color=(0,0,0.3),
|
|
763
|
+
....: center=(0,0,-2), size=2, opacity=0.9)
|
|
764
|
+
Graphics3d Object
|
|
765
|
+
|
|
766
|
+
.. PLOT::
|
|
767
|
+
|
|
768
|
+
sphinx_plot(sphere(color='orange') + sphere(color=(0,0,0.3), center=(0,0,-2),size=2,opacity=0.9))
|
|
769
|
+
|
|
770
|
+
We draw a transparent sphere on a saddle. ::
|
|
771
|
+
|
|
772
|
+
sage: u,v = var('u v') # needs sage.symbolic
|
|
773
|
+
sage: saddle = plot3d(u^2 - v^2, (u,-2,2), (v,-2,2)) # needs sage.symbolic
|
|
774
|
+
sage: sphere((0,0,1), color='red', opacity=0.5, aspect_ratio=[1,1,1]) + saddle # needs sage.symbolic
|
|
775
|
+
Graphics3d Object
|
|
776
|
+
|
|
777
|
+
.. PLOT::
|
|
778
|
+
|
|
779
|
+
u,v = var('u v')
|
|
780
|
+
saddle = plot3d(u**2 - v**2, (u,-2,2), (v,-2,2))
|
|
781
|
+
sphinx_plot(sphere((0,0,1), color='red', opacity=0.5, aspect_ratio=[1,1,1]) + saddle)
|
|
782
|
+
|
|
783
|
+
TESTS::
|
|
784
|
+
|
|
785
|
+
sage: T = sage.plot.plot3d.texture.Texture('red')
|
|
786
|
+
sage: S = sphere(texture=T)
|
|
787
|
+
sage: T in S.texture_set()
|
|
788
|
+
True
|
|
789
|
+
"""
|
|
790
|
+
kwds['texture'] = Texture(**kwds)
|
|
791
|
+
G = Sphere(size, **kwds)
|
|
792
|
+
H = G.translate(center)
|
|
793
|
+
H._set_extra_kwds(kwds)
|
|
794
|
+
return H
|
|
795
|
+
|
|
796
|
+
|
|
797
|
+
def text3d(txt, x_y_z, **kwds):
|
|
798
|
+
r"""
|
|
799
|
+
Display 3d text.
|
|
800
|
+
|
|
801
|
+
INPUT:
|
|
802
|
+
|
|
803
|
+
- ``txt`` -- some text
|
|
804
|
+
|
|
805
|
+
- ``x_y_z`` -- position tuple `(x,y,z)`
|
|
806
|
+
|
|
807
|
+
- ``**kwds`` -- standard 3d graphics options
|
|
808
|
+
|
|
809
|
+
EXAMPLES:
|
|
810
|
+
|
|
811
|
+
We write the word Sage in red at position (1,2,3)::
|
|
812
|
+
|
|
813
|
+
sage: text3d("Sage", (1,2,3), color=(0.5,0,0))
|
|
814
|
+
Graphics3d Object
|
|
815
|
+
|
|
816
|
+
.. PLOT::
|
|
817
|
+
|
|
818
|
+
sphinx_plot(text3d("Sage", (1,2,3), color=(0.5,0,0)))
|
|
819
|
+
|
|
820
|
+
We draw a multicolor spiral of numbers::
|
|
821
|
+
|
|
822
|
+
sage: sum([text3d('%.1f'%n, (cos(n),sin(n),n), color=(n/2,1-n/2,0))
|
|
823
|
+
....: for n in [0,0.2,..,8]])
|
|
824
|
+
Graphics3d Object
|
|
825
|
+
|
|
826
|
+
.. PLOT::
|
|
827
|
+
|
|
828
|
+
import numpy
|
|
829
|
+
sphinx_plot(sum([text3d('%.1f'%n, (cos(n),sin(n),n), color=(n/2,1-n/2,0)) for n in numpy.linspace(0,8,40)]))
|
|
830
|
+
|
|
831
|
+
Another example::
|
|
832
|
+
|
|
833
|
+
sage: text3d("Sage is really neat!!",(2,12,1))
|
|
834
|
+
Graphics3d Object
|
|
835
|
+
|
|
836
|
+
.. PLOT::
|
|
837
|
+
|
|
838
|
+
sphinx_plot(text3d("Sage is really neat!!",(2,12,1)))
|
|
839
|
+
|
|
840
|
+
And in 3d in two places::
|
|
841
|
+
|
|
842
|
+
sage: text3d("Sage is...",(2,12,1), color=(1,0,0)) + text3d("quite powerful!!",(4,10,0), color=(0,0,1))
|
|
843
|
+
Graphics3d Object
|
|
844
|
+
|
|
845
|
+
.. PLOT::
|
|
846
|
+
|
|
847
|
+
sphinx_plot(text3d("Sage is...",(2,12,1), color=(1,0,0)) + text3d("quite powerful!!",(4,10,0), color=(0,0,1)))
|
|
848
|
+
|
|
849
|
+
Adjust the font size, family, style, and weight (Three.js viewer only)::
|
|
850
|
+
|
|
851
|
+
sage: t0 = text3d("Pixel size", (0, 0, 0), fontsize=20)
|
|
852
|
+
sage: t1 = text3d("Percentage size", (0, 0, 1), fontsize='300%')
|
|
853
|
+
sage: t2 = text3d("Keyword size", (0, 0, 2), fontsize='x-small')
|
|
854
|
+
sage: t3 = text3d("Single family", (0, 0, 3), fontfamily='serif')
|
|
855
|
+
sage: t4 = text3d("Family fallback", (0, 0, 4), fontfamily=['Consolas', 'Lucida Console', 'monospace'])
|
|
856
|
+
sage: t5 = text3d("Another way", (0, 0, 5), fontfamily='Consolas, Lucida Console, monospace')
|
|
857
|
+
sage: t6 = text3d("Style", (0, 0, 6), fontstyle='italic')
|
|
858
|
+
sage: t7 = text3d("Keyword weight", (0, 0, 7), fontweight='bold')
|
|
859
|
+
sage: t8 = text3d("Integer weight (1-1000)", (0, 0, 8), fontweight=800) # 'extra bold'
|
|
860
|
+
sage: sum([t0, t1, t2, t3, t4, t5, t6, t7, t8]).show(viewer='threejs', frame=False)
|
|
861
|
+
|
|
862
|
+
Adjust the text's opacity (Three.js viewer only)::
|
|
863
|
+
|
|
864
|
+
sage: def echo(o):
|
|
865
|
+
....: return text3d("Echo!", (0, 0, o), opacity=o)
|
|
866
|
+
sage: show(sum([echo(o) for o in (0.1, 0.2, .., 1)]), viewer='threejs')
|
|
867
|
+
"""
|
|
868
|
+
(x, y, z) = x_y_z
|
|
869
|
+
if 'color' not in kwds and 'rgbcolor' not in kwds:
|
|
870
|
+
kwds['color'] = (0, 0, 0)
|
|
871
|
+
G = Text(txt, **kwds).translate((x, y, z))
|
|
872
|
+
G._set_extra_kwds(kwds)
|
|
873
|
+
|
|
874
|
+
return G
|
|
875
|
+
|
|
876
|
+
|
|
877
|
+
class Point(PrimitiveObject):
|
|
878
|
+
"""
|
|
879
|
+
Create a position in 3-space, represented by a sphere of fixed
|
|
880
|
+
size.
|
|
881
|
+
|
|
882
|
+
INPUT:
|
|
883
|
+
|
|
884
|
+
- ``center`` -- point (3-tuple)
|
|
885
|
+
|
|
886
|
+
- ``size`` -- (default: 1)
|
|
887
|
+
|
|
888
|
+
EXAMPLES:
|
|
889
|
+
|
|
890
|
+
We normally access this via the ``point3d`` function. Note that extra
|
|
891
|
+
keywords are correctly used::
|
|
892
|
+
|
|
893
|
+
sage: point3d((4,3,2),size=2,color='red',opacity=.5)
|
|
894
|
+
Graphics3d Object
|
|
895
|
+
"""
|
|
896
|
+
def __init__(self, center, size=1, **kwds):
|
|
897
|
+
"""
|
|
898
|
+
Create the graphics primitive :class:`Point` in 3-D.
|
|
899
|
+
|
|
900
|
+
See the docstring of this class for full documentation.
|
|
901
|
+
|
|
902
|
+
EXAMPLES::
|
|
903
|
+
|
|
904
|
+
sage: from sage.plot.plot3d.shapes2 import Point
|
|
905
|
+
sage: P = Point((1,2,3),2)
|
|
906
|
+
sage: P.loc
|
|
907
|
+
(1.0, 2.0, 3.0)
|
|
908
|
+
"""
|
|
909
|
+
PrimitiveObject.__init__(self, **kwds)
|
|
910
|
+
self.loc = (float(center[0]), float(center[1]), float(center[2]))
|
|
911
|
+
self.size = size
|
|
912
|
+
self._set_extra_kwds(kwds)
|
|
913
|
+
|
|
914
|
+
def bounding_box(self):
|
|
915
|
+
"""
|
|
916
|
+
Return the lower and upper corners of a 3-D bounding box for ``self``.
|
|
917
|
+
|
|
918
|
+
This is used for rendering and ``self`` should fit entirely within this
|
|
919
|
+
box. In this case, we simply return the center of the point.
|
|
920
|
+
|
|
921
|
+
TESTS::
|
|
922
|
+
|
|
923
|
+
sage: P = point3d((-3,2,10),size=7)
|
|
924
|
+
sage: P.bounding_box()
|
|
925
|
+
((-3.0, 2.0, 10.0), (-3.0, 2.0, 10.0))
|
|
926
|
+
"""
|
|
927
|
+
return self.loc, self.loc
|
|
928
|
+
|
|
929
|
+
def tachyon_repr(self, render_params):
|
|
930
|
+
"""
|
|
931
|
+
Return representation of the point suitable for plotting
|
|
932
|
+
using the Tachyon ray tracer.
|
|
933
|
+
|
|
934
|
+
TESTS::
|
|
935
|
+
|
|
936
|
+
sage: P = point3d((1,2,3),size=3,color='purple')
|
|
937
|
+
sage: P.tachyon_repr(P.default_render_params())
|
|
938
|
+
'Sphere center 1.0 2.0 3.0 Rad 0.015 texture...'
|
|
939
|
+
"""
|
|
940
|
+
transform = render_params.transform
|
|
941
|
+
if transform is None:
|
|
942
|
+
cen = self.loc
|
|
943
|
+
else:
|
|
944
|
+
cen = transform.transform_point(self.loc)
|
|
945
|
+
|
|
946
|
+
radius = self.size * TACHYON_PIXEL
|
|
947
|
+
texture = self.texture.id
|
|
948
|
+
return (f"Sphere center {cen[0]!r} {cen[1]!r} {cen[2]!r} "
|
|
949
|
+
f"Rad {radius!r} {texture}")
|
|
950
|
+
|
|
951
|
+
def obj_repr(self, render_params):
|
|
952
|
+
"""
|
|
953
|
+
Return complete representation of the point as a sphere.
|
|
954
|
+
|
|
955
|
+
TESTS::
|
|
956
|
+
|
|
957
|
+
sage: P = point3d((1,2,3),size=3,color='purple')
|
|
958
|
+
sage: P.obj_repr(P.default_render_params())[0][0:2]
|
|
959
|
+
['g obj_1', 'usemtl texture...']
|
|
960
|
+
"""
|
|
961
|
+
T = render_params.transform
|
|
962
|
+
if T is None:
|
|
963
|
+
from . import transform
|
|
964
|
+
T = transform.Transformation()
|
|
965
|
+
render_params.push_transform(~T)
|
|
966
|
+
S = shapes.Sphere(self.size / 200.0).translate(T(self.loc))
|
|
967
|
+
cmds = S.obj_repr(render_params)
|
|
968
|
+
render_params.pop_transform()
|
|
969
|
+
return cmds
|
|
970
|
+
|
|
971
|
+
def jmol_repr(self, render_params):
|
|
972
|
+
r"""
|
|
973
|
+
Return representation of the object suitable for plotting
|
|
974
|
+
using Jmol.
|
|
975
|
+
|
|
976
|
+
TESTS::
|
|
977
|
+
|
|
978
|
+
sage: P = point3d((1,2,3),size=3,color='purple')
|
|
979
|
+
sage: P.jmol_repr(P.default_render_params())
|
|
980
|
+
['draw point_1 DIAMETER 3 {1.0 2.0 3.0}\ncolor $point_1 [128,0,128]']
|
|
981
|
+
"""
|
|
982
|
+
name = render_params.unique_name('point')
|
|
983
|
+
transform = render_params.transform
|
|
984
|
+
cen = self.loc if transform is None else transform(self.loc)
|
|
985
|
+
return ["draw {} DIAMETER {} {{{} {} {}}}\n{}".format(name, int(self.size), cen[0], cen[1], cen[2], self.texture.jmol_str('$' + name))]
|
|
986
|
+
|
|
987
|
+
def threejs_repr(self, render_params):
|
|
988
|
+
r"""
|
|
989
|
+
Return representation of the point suitable for plotting with three.js.
|
|
990
|
+
|
|
991
|
+
EXAMPLES::
|
|
992
|
+
|
|
993
|
+
sage: P = point3d((1,2,3), color=(0,1,0), opacity=0.5, size=10)
|
|
994
|
+
sage: P.threejs_repr(P.default_render_params())
|
|
995
|
+
[('point',
|
|
996
|
+
{'color': '#00ff00', 'opacity': 0.5, 'point': (1.0, 2.0, 3.0), 'size': 10.0})]
|
|
997
|
+
|
|
998
|
+
TESTS:
|
|
999
|
+
|
|
1000
|
+
Transformations apply to the point's location::
|
|
1001
|
+
|
|
1002
|
+
sage: P = point3d((1,2,3)).translate(-1, -2, -3)
|
|
1003
|
+
sage: P.threejs_repr(P.default_render_params())
|
|
1004
|
+
[('point',
|
|
1005
|
+
{'color': '#6666ff', 'opacity': 1.0, 'point': (0.0, 0.0, 0.0), 'size': 5.0})]
|
|
1006
|
+
"""
|
|
1007
|
+
transform = render_params.transform
|
|
1008
|
+
center = tuple(float(coord) for coord in self.loc)
|
|
1009
|
+
if transform is not None:
|
|
1010
|
+
center = transform(center)
|
|
1011
|
+
color = '#' + str(self.texture.hex_rgb())
|
|
1012
|
+
opacity = float(self.texture.opacity)
|
|
1013
|
+
size = float(self.size)
|
|
1014
|
+
point = {'point': center, 'size': size, 'color': color, 'opacity': opacity}
|
|
1015
|
+
return [('point', point)]
|
|
1016
|
+
|
|
1017
|
+
def stl_binary_repr(self, render_params):
|
|
1018
|
+
"""
|
|
1019
|
+
Return an empty list, as this is not useful for STL export.
|
|
1020
|
+
|
|
1021
|
+
EXAMPLES::
|
|
1022
|
+
|
|
1023
|
+
sage: P = point3d((1,2,3)).translate(-1, -2, -3)
|
|
1024
|
+
sage: P.stl_binary_repr(P.default_render_params())
|
|
1025
|
+
[]
|
|
1026
|
+
"""
|
|
1027
|
+
return []
|
|
1028
|
+
|
|
1029
|
+
|
|
1030
|
+
class Line(PrimitiveObject):
|
|
1031
|
+
r"""
|
|
1032
|
+
Draw a 3d line joining a sequence of points.
|
|
1033
|
+
|
|
1034
|
+
This line has a fixed diameter unaffected by transformations and
|
|
1035
|
+
zooming. It may be smoothed if ``corner_cutoff < 1``.
|
|
1036
|
+
|
|
1037
|
+
INPUT:
|
|
1038
|
+
|
|
1039
|
+
- ``points`` -- list of points to pass through
|
|
1040
|
+
|
|
1041
|
+
- ``thickness`` -- (default: 5) diameter of the line
|
|
1042
|
+
|
|
1043
|
+
- ``corner_cutoff`` -- (default: 0.5) threshold for
|
|
1044
|
+
smoothing (see :meth:`corners`)
|
|
1045
|
+
|
|
1046
|
+
- ``arrow_head`` -- boolean (default: ``False``); if ``True`` make
|
|
1047
|
+
this curve into an arrow
|
|
1048
|
+
|
|
1049
|
+
The parameter ``corner_cutoff`` is a bound for the cosine of the
|
|
1050
|
+
angle made by two successive segments. This angle is close to `0`
|
|
1051
|
+
(and the cosine close to 1) if the two successive segments are
|
|
1052
|
+
almost aligned and close to `\pi` (and the cosine close to -1) if
|
|
1053
|
+
the path has a strong peak. If the cosine is smaller than the
|
|
1054
|
+
bound (which means a sharper peak) then no smoothing is done.
|
|
1055
|
+
|
|
1056
|
+
EXAMPLES::
|
|
1057
|
+
|
|
1058
|
+
sage: from sage.plot.plot3d.shapes2 import Line
|
|
1059
|
+
sage: Line([(i*math.sin(i), i*math.cos(i), i/3) for i in range(30)],
|
|
1060
|
+
....: arrow_head=True)
|
|
1061
|
+
Graphics3d Object
|
|
1062
|
+
|
|
1063
|
+
Smooth angles less than 90 degrees::
|
|
1064
|
+
|
|
1065
|
+
sage: Line([(0,0,0),(1,0,0),(2,1,0),(0,1,0)], corner_cutoff=0)
|
|
1066
|
+
Graphics3d Object
|
|
1067
|
+
|
|
1068
|
+
Make sure that the ``corner_cutoff`` keyword works (:issue:`3859`)::
|
|
1069
|
+
|
|
1070
|
+
sage: N = 11
|
|
1071
|
+
sage: c = 0.4
|
|
1072
|
+
sage: sum(Line([(i,1,0), (i,0,0), (i,cos(2*pi*i/N), sin(2*pi*i/N))], # needs sage.symbolic
|
|
1073
|
+
....: corner_cutoff=c,
|
|
1074
|
+
....: color='red' if -cos(2*pi*i/N)<=c else 'blue')
|
|
1075
|
+
....: for i in range(N+1))
|
|
1076
|
+
Graphics3d Object
|
|
1077
|
+
"""
|
|
1078
|
+
def __init__(self, points, thickness=5, corner_cutoff=0.5,
|
|
1079
|
+
arrow_head=False, **kwds):
|
|
1080
|
+
"""
|
|
1081
|
+
Create the graphics primitive :class:`Line` in 3-D.
|
|
1082
|
+
|
|
1083
|
+
See the docstring of this class for full documentation.
|
|
1084
|
+
|
|
1085
|
+
EXAMPLES::
|
|
1086
|
+
|
|
1087
|
+
sage: from sage.plot.plot3d.shapes2 import Line
|
|
1088
|
+
sage: P = Line([(1,2,3),(1,2,2),(-1,2,2),(-1,3,2)],thickness=6,corner_cutoff=.2)
|
|
1089
|
+
sage: P.points, P.arrow_head
|
|
1090
|
+
([(1, 2, 3), (1, 2, 2), (-1, 2, 2), (-1, 3, 2)], False)
|
|
1091
|
+
"""
|
|
1092
|
+
if len(points) < 2:
|
|
1093
|
+
raise ValueError("there must be at least 2 points")
|
|
1094
|
+
PrimitiveObject.__init__(self, **kwds)
|
|
1095
|
+
self.points = points
|
|
1096
|
+
self.thickness = thickness
|
|
1097
|
+
self.corner_cutoff = corner_cutoff
|
|
1098
|
+
self.arrow_head = arrow_head
|
|
1099
|
+
|
|
1100
|
+
def bounding_box(self):
|
|
1101
|
+
"""
|
|
1102
|
+
Return the lower and upper corners of a 3-D bounding box for ``self``.
|
|
1103
|
+
|
|
1104
|
+
This is used for rendering and ``self`` should fit entirely within this
|
|
1105
|
+
box. In this case, we return the highest and lowest values of each
|
|
1106
|
+
coordinate among all points.
|
|
1107
|
+
|
|
1108
|
+
TESTS::
|
|
1109
|
+
|
|
1110
|
+
sage: from sage.plot.plot3d.shapes2 import Line
|
|
1111
|
+
sage: L = Line([(i, i^2 - 1, -2*ln(i)) for i in [10,20,30]]) # needs sage.symbolic
|
|
1112
|
+
sage: L.bounding_box() # needs sage.symbolic
|
|
1113
|
+
((10.0, 99.0, -6.802394763324311),
|
|
1114
|
+
(30.0, 899.0, -4.605170185988092))
|
|
1115
|
+
"""
|
|
1116
|
+
try:
|
|
1117
|
+
return self.__bounding_box
|
|
1118
|
+
except AttributeError:
|
|
1119
|
+
self.__bounding_box = point_list_bounding_box(self.points)
|
|
1120
|
+
return self.__bounding_box
|
|
1121
|
+
|
|
1122
|
+
def tachyon_repr(self, render_params):
|
|
1123
|
+
"""
|
|
1124
|
+
Return representation of the line suitable for plotting
|
|
1125
|
+
using the Tachyon ray tracer.
|
|
1126
|
+
|
|
1127
|
+
TESTS::
|
|
1128
|
+
|
|
1129
|
+
sage: L = line3d([(cos(i),sin(i),i^2) for i in srange(0,10,.01)], # needs sage.symbolic
|
|
1130
|
+
....: color='red')
|
|
1131
|
+
sage: L.tachyon_repr(L.default_render_params())[0] # needs sage.symbolic
|
|
1132
|
+
'FCylinder base 1.0 0.0 0.0 apex 0.9999500004166653 0.009999833334166664 0.0001 rad 0.005 texture...'
|
|
1133
|
+
"""
|
|
1134
|
+
T = render_params.transform
|
|
1135
|
+
cmds = []
|
|
1136
|
+
px, py, pz = self.points[0] if T is None else T(self.points[0])
|
|
1137
|
+
radius = self.thickness * TACHYON_PIXEL
|
|
1138
|
+
for P in self.points[1:]:
|
|
1139
|
+
x, y, z = P if T is None else T(P)
|
|
1140
|
+
if self.arrow_head and P is self.points[-1]:
|
|
1141
|
+
A = shapes.arrow3d((px, py, pz), (x, y, z), radius=radius, texture=self.texture)
|
|
1142
|
+
render_params.push_transform(~T)
|
|
1143
|
+
cmds.append(A.tachyon_repr(render_params))
|
|
1144
|
+
render_params.pop_transform()
|
|
1145
|
+
else:
|
|
1146
|
+
cmd = ('FCylinder base {pos[0]!r} {pos[1]!r} {pos[2]!r} '
|
|
1147
|
+
'apex {apex[0]!r} {apex[1]!r} {apex[2]!r} '
|
|
1148
|
+
'rad {radius!r} {texture}').format(
|
|
1149
|
+
pos=(px, py, pz), apex=(x, y, z), radius=radius,
|
|
1150
|
+
texture=self.texture.id)
|
|
1151
|
+
cmds.append(cmd)
|
|
1152
|
+
px, py, pz = x, y, z
|
|
1153
|
+
return cmds
|
|
1154
|
+
|
|
1155
|
+
def obj_repr(self, render_params):
|
|
1156
|
+
"""
|
|
1157
|
+
Return complete representation of the line as an object.
|
|
1158
|
+
|
|
1159
|
+
TESTS::
|
|
1160
|
+
|
|
1161
|
+
sage: from sage.plot.plot3d.shapes2 import Line
|
|
1162
|
+
sage: L = Line([(cos(i),sin(i),i^2) for i in srange(0,10,.01)], # needs sage.symbolic
|
|
1163
|
+
....: color='red')
|
|
1164
|
+
sage: L.obj_repr(L.default_render_params())[0][0][0][2][:3] # needs sage.symbolic
|
|
1165
|
+
['v 0.99995 0.00999983 0.0001',
|
|
1166
|
+
'v 1.02376 0.010195 -0.00750607',
|
|
1167
|
+
'v 1.00007 0.0102504 -0.0248984']
|
|
1168
|
+
"""
|
|
1169
|
+
T = render_params.transform
|
|
1170
|
+
if T is None:
|
|
1171
|
+
from . import transform
|
|
1172
|
+
T = transform.Transformation()
|
|
1173
|
+
render_params.push_transform(~T)
|
|
1174
|
+
L = line3d([T(P) for P in self.points], radius=self.thickness / 200.0, arrow_head=self.arrow_head, texture=self.texture)
|
|
1175
|
+
cmds = L.obj_repr(render_params)
|
|
1176
|
+
render_params.pop_transform()
|
|
1177
|
+
return cmds
|
|
1178
|
+
|
|
1179
|
+
def jmol_repr(self, render_params):
|
|
1180
|
+
r"""
|
|
1181
|
+
Return representation of the object suitable for plotting
|
|
1182
|
+
using Jmol.
|
|
1183
|
+
|
|
1184
|
+
TESTS::
|
|
1185
|
+
|
|
1186
|
+
sage: L = line3d([(cos(i),sin(i),i^2) for i in srange(0,10,.01)], # needs sage.symbolic
|
|
1187
|
+
....: color='red')
|
|
1188
|
+
sage: L.jmol_repr(L.default_render_params())[0][:42] # needs sage.symbolic
|
|
1189
|
+
'draw line_1 diameter 1 curve {1.0 0.0 0.0}'
|
|
1190
|
+
"""
|
|
1191
|
+
T = render_params.transform
|
|
1192
|
+
corners = self.corners(max_len=255) # hardcoded limit in jmol
|
|
1193
|
+
last_corner = corners[-1]
|
|
1194
|
+
corners = set(corners)
|
|
1195
|
+
cmds = []
|
|
1196
|
+
cmd = None
|
|
1197
|
+
name = ''
|
|
1198
|
+
for P in self.points:
|
|
1199
|
+
TP = P if T is None else T(P)
|
|
1200
|
+
if P in corners:
|
|
1201
|
+
if cmd:
|
|
1202
|
+
cmds.append(cmd + " {{{} {} {}}} ".format(*TP))
|
|
1203
|
+
cmds.append(self.texture.jmol_str('$' + name))
|
|
1204
|
+
type = 'arrow' if self.arrow_head and P is last_corner else 'curve'
|
|
1205
|
+
name = render_params.unique_name('line')
|
|
1206
|
+
cmd = "draw {} diameter {} {} {{{} {} {}}} ".format(name, int(self.thickness), type, TP[0], TP[1], TP[2])
|
|
1207
|
+
else:
|
|
1208
|
+
cmd += " {{{} {} {}}} ".format(*TP)
|
|
1209
|
+
cmds.append(cmd)
|
|
1210
|
+
cmds.append(self.texture.jmol_str('$' + name))
|
|
1211
|
+
return cmds
|
|
1212
|
+
|
|
1213
|
+
def corners(self, corner_cutoff=None, max_len=None):
|
|
1214
|
+
r"""
|
|
1215
|
+
Figure out where the curve turns too sharply to pretend it is
|
|
1216
|
+
smooth.
|
|
1217
|
+
|
|
1218
|
+
INPUT:
|
|
1219
|
+
|
|
1220
|
+
- ``corner_cutoff`` -- (default: ``None``) if the
|
|
1221
|
+
cosine of the angle between adjacent line segments is smaller than
|
|
1222
|
+
this bound, then there will be a sharp corner in the path.
|
|
1223
|
+
Otherwise, the path is smoothed. If ``None``,
|
|
1224
|
+
then the default value 0.5 is used.
|
|
1225
|
+
|
|
1226
|
+
- ``max_len`` -- (default: ``None``) maximum number
|
|
1227
|
+
of points allowed in a single path. If this is set, this
|
|
1228
|
+
creates corners at smooth points in order to break the path
|
|
1229
|
+
into smaller pieces.
|
|
1230
|
+
|
|
1231
|
+
The parameter ``corner_cutoff`` is a bound for the cosine of the
|
|
1232
|
+
angle made by two successive segments. This angle is close to `0`
|
|
1233
|
+
(and the cosine close to 1) if the two successive segments are
|
|
1234
|
+
almost aligned and close to `\pi` (and the cosine close to -1) if
|
|
1235
|
+
the path has a strong peak. If the cosine is smaller than the
|
|
1236
|
+
bound (which means a sharper peak) then there must be a corner.
|
|
1237
|
+
|
|
1238
|
+
OUTPUT:
|
|
1239
|
+
|
|
1240
|
+
List of points at which to start a new line. This always
|
|
1241
|
+
includes the first point, and never the last.
|
|
1242
|
+
|
|
1243
|
+
EXAMPLES:
|
|
1244
|
+
|
|
1245
|
+
No corners, always smooth::
|
|
1246
|
+
|
|
1247
|
+
sage: from sage.plot.plot3d.shapes2 import Line
|
|
1248
|
+
sage: Line([(0,0,0),(1,0,0),(2,1,0),(0,1,0)], corner_cutoff=-1).corners()
|
|
1249
|
+
[(0, 0, 0)]
|
|
1250
|
+
|
|
1251
|
+
Smooth if the angle is greater than 90 degrees::
|
|
1252
|
+
|
|
1253
|
+
sage: Line([(0,0,0),(1,0,0),(2,1,0),(0,1,0)], corner_cutoff=0).corners()
|
|
1254
|
+
[(0, 0, 0), (2, 1, 0)]
|
|
1255
|
+
|
|
1256
|
+
Every point (corners everywhere)::
|
|
1257
|
+
|
|
1258
|
+
sage: Line([(0,0,0),(1,0,0),(2,1,0),(0,1,0)], corner_cutoff=1).corners()
|
|
1259
|
+
[(0, 0, 0), (1, 0, 0), (2, 1, 0)]
|
|
1260
|
+
"""
|
|
1261
|
+
if corner_cutoff is None:
|
|
1262
|
+
corner_cutoff = self.corner_cutoff
|
|
1263
|
+
|
|
1264
|
+
if corner_cutoff >= 1:
|
|
1265
|
+
# corners everywhere
|
|
1266
|
+
return self.points[:-1]
|
|
1267
|
+
|
|
1268
|
+
elif corner_cutoff <= -1:
|
|
1269
|
+
# no corners
|
|
1270
|
+
if max_len is not None:
|
|
1271
|
+
# forced by the maximal number of consecutive smooth points
|
|
1272
|
+
return self.points[:-1][::max_len - 1]
|
|
1273
|
+
else:
|
|
1274
|
+
return [self.points[0]]
|
|
1275
|
+
|
|
1276
|
+
else:
|
|
1277
|
+
if max_len is None:
|
|
1278
|
+
max_len = len(self.points) + 1
|
|
1279
|
+
count = 2
|
|
1280
|
+
# ... -- prev -- cur -- next -- ...
|
|
1281
|
+
cur = self.points[0]
|
|
1282
|
+
next = self.points[1]
|
|
1283
|
+
next_dir = [next[i] - cur[i] for i in range(3)]
|
|
1284
|
+
corners = [cur]
|
|
1285
|
+
cur, prev_dir = next, next_dir
|
|
1286
|
+
|
|
1287
|
+
# quicker than making them vectors first
|
|
1288
|
+
def dot(x0_y0_z0, x1_y1_z1):
|
|
1289
|
+
(x0, y0, z0) = x0_y0_z0
|
|
1290
|
+
(x1, y1, z1) = x1_y1_z1
|
|
1291
|
+
return x0 * x1 + y0 * y1 + z0 * z1
|
|
1292
|
+
|
|
1293
|
+
for next in self.points[2:]:
|
|
1294
|
+
if next == cur:
|
|
1295
|
+
corners.append(cur)
|
|
1296
|
+
cur = next
|
|
1297
|
+
count = 1
|
|
1298
|
+
continue
|
|
1299
|
+
next_dir = [next[i] - cur[i] for i in range(3)]
|
|
1300
|
+
cos_angle = (dot(prev_dir, next_dir) /
|
|
1301
|
+
math.sqrt(dot(prev_dir, prev_dir) *
|
|
1302
|
+
dot(next_dir, next_dir)))
|
|
1303
|
+
if cos_angle <= corner_cutoff or count > max_len - 1:
|
|
1304
|
+
corners.append(cur)
|
|
1305
|
+
count = 1
|
|
1306
|
+
cur, prev_dir = next, next_dir
|
|
1307
|
+
count += 1
|
|
1308
|
+
return corners
|
|
1309
|
+
|
|
1310
|
+
def threejs_repr(self, render_params):
|
|
1311
|
+
r"""
|
|
1312
|
+
Return representation of the line suitable for plotting with three.js.
|
|
1313
|
+
|
|
1314
|
+
EXAMPLES::
|
|
1315
|
+
|
|
1316
|
+
sage: L = line3d([(1,2,3), (4,5,6)], thickness=10, color=(1,0,0), opacity=0.5)
|
|
1317
|
+
sage: L.threejs_repr(L.default_render_params())
|
|
1318
|
+
[('line',
|
|
1319
|
+
{'color': '#ff0000',
|
|
1320
|
+
'linewidth': 10.0,
|
|
1321
|
+
'opacity': 0.5,
|
|
1322
|
+
'points': [(1.0, 2.0, 3.0), (4.0, 5.0, 6.0)]})]
|
|
1323
|
+
|
|
1324
|
+
TESTS:
|
|
1325
|
+
|
|
1326
|
+
Transformations apply to the line's vertices::
|
|
1327
|
+
|
|
1328
|
+
sage: L = line3d([(1,2,3), (4,5,6)]).translate(-1, -2, -3)
|
|
1329
|
+
sage: L.threejs_repr(L.default_render_params())
|
|
1330
|
+
[('line',
|
|
1331
|
+
{'color': '#6666ff',
|
|
1332
|
+
'linewidth': 1.0,
|
|
1333
|
+
'opacity': 1.0,
|
|
1334
|
+
'points': [(0.0, 0.0, 0.0), (3.0, 3.0, 3.0)]})]
|
|
1335
|
+
|
|
1336
|
+
When setting ``arrow_head=True``, the last line segment is replaced by
|
|
1337
|
+
an arrow with a width half the thickness of the line::
|
|
1338
|
+
|
|
1339
|
+
sage: L = line3d([(0,0,0), (1,1,1), (2,2,2)], thickness=4, arrow_head=True)
|
|
1340
|
+
sage: L_repr = L.threejs_repr(L.default_render_params())
|
|
1341
|
+
sage: L_repr[-1]
|
|
1342
|
+
('line',
|
|
1343
|
+
{'color': '#6666ff',
|
|
1344
|
+
'linewidth': 4.0,
|
|
1345
|
+
'opacity': 1.0,
|
|
1346
|
+
'points': [(0.0, 0.0, 0.0), (1.0, 1.0, 1.0)]})
|
|
1347
|
+
sage: A = arrow3d((1,1,1), (2,2,2), width=2)
|
|
1348
|
+
sage: A_repr = A.threejs_repr(A.default_render_params())
|
|
1349
|
+
sage: A_repr == L_repr[:-1]
|
|
1350
|
+
True
|
|
1351
|
+
|
|
1352
|
+
The arrow shares the transformation, color, and opacity of the line::
|
|
1353
|
+
|
|
1354
|
+
sage: L = line3d([(0,0,0), (1,1,1), (2,2,2)], thickness=4,
|
|
1355
|
+
....: arrow_head=True, color=(1,0,0), opacity=0.5)
|
|
1356
|
+
sage: L = L.translate(-1, -1, -1)
|
|
1357
|
+
sage: L_repr = L.threejs_repr(L.default_render_params())
|
|
1358
|
+
sage: L_repr[-1]
|
|
1359
|
+
('line',
|
|
1360
|
+
{'color': '#ff0000',
|
|
1361
|
+
'linewidth': 4.0,
|
|
1362
|
+
'opacity': 0.5,
|
|
1363
|
+
'points': [(-1.0, -1.0, -1.0), (0.0, 0.0, 0.0)]})
|
|
1364
|
+
sage: A = arrow3d((1,1,1), (2,2,2), width=2, color=(1,0,0), opacity=0.5)
|
|
1365
|
+
sage: A = A.translate(-1, -1, -1)
|
|
1366
|
+
sage: A_repr = A.threejs_repr(A.default_render_params())
|
|
1367
|
+
sage: A_repr == L_repr[:-1]
|
|
1368
|
+
True
|
|
1369
|
+
|
|
1370
|
+
If there were only two points to begin with, only the arrow head's
|
|
1371
|
+
representation is returned::
|
|
1372
|
+
|
|
1373
|
+
sage: L = line3d([(0,0,0), (1,1,1)], thickness=2, arrow_head=True)
|
|
1374
|
+
sage: L_repr = L.threejs_repr(L.default_render_params())
|
|
1375
|
+
sage: A = arrow3d((0,0,0), (1,1,1), width=1)
|
|
1376
|
+
sage: A_repr = A.threejs_repr(A.default_render_params())
|
|
1377
|
+
sage: A_repr == L_repr
|
|
1378
|
+
True
|
|
1379
|
+
"""
|
|
1380
|
+
reprs = []
|
|
1381
|
+
points = [tuple(float(coord) for coord in p) for p in self.points]
|
|
1382
|
+
color = '#' + str(self.texture.hex_rgb())
|
|
1383
|
+
opacity = float(self.texture.opacity)
|
|
1384
|
+
thickness = float(self.thickness)
|
|
1385
|
+
if self.arrow_head:
|
|
1386
|
+
width = thickness / 2.0
|
|
1387
|
+
arrow = shapes.arrow3d(start=points[-2], end=points[-1], width=width,
|
|
1388
|
+
color=color, opacity=opacity)
|
|
1389
|
+
reprs += arrow.threejs_repr(render_params)
|
|
1390
|
+
points = points[:-1] # The arrow replaces the last line segment.
|
|
1391
|
+
if len(points) > 1:
|
|
1392
|
+
transform = render_params.transform
|
|
1393
|
+
if transform is not None:
|
|
1394
|
+
points = [transform(p) for p in points]
|
|
1395
|
+
line = {'points': points, 'color': color, 'opacity': opacity, 'linewidth': thickness}
|
|
1396
|
+
reprs.append(('line', line))
|
|
1397
|
+
return reprs
|
|
1398
|
+
|
|
1399
|
+
def stl_binary_repr(self, render_params):
|
|
1400
|
+
"""
|
|
1401
|
+
Return an empty list, as this is not useful for STL export.
|
|
1402
|
+
|
|
1403
|
+
EXAMPLES::
|
|
1404
|
+
|
|
1405
|
+
sage: L = line3d([(1,2,3), (4,5,6)]).translate(-1, -2, -3)
|
|
1406
|
+
sage: L.stl_binary_repr(L.default_render_params())
|
|
1407
|
+
[]
|
|
1408
|
+
"""
|
|
1409
|
+
return []
|
|
1410
|
+
|
|
1411
|
+
|
|
1412
|
+
@rename_keyword(alpha='opacity')
|
|
1413
|
+
def point3d(v, size=5, **kwds):
|
|
1414
|
+
"""
|
|
1415
|
+
Plot a point or list of points in 3d space.
|
|
1416
|
+
|
|
1417
|
+
INPUT:
|
|
1418
|
+
|
|
1419
|
+
- ``v`` -- a point or list of points
|
|
1420
|
+
|
|
1421
|
+
- ``size`` -- (default: 5) size of the point (or
|
|
1422
|
+
points)
|
|
1423
|
+
|
|
1424
|
+
- ``color`` -- string (``'red'``, ``'green'`` etc)
|
|
1425
|
+
or a tuple (r, g, b) with r, g, b numbers between 0 and 1
|
|
1426
|
+
|
|
1427
|
+
- ``opacity`` -- (default: 1) if less than 1 then is
|
|
1428
|
+
transparent
|
|
1429
|
+
|
|
1430
|
+
EXAMPLES::
|
|
1431
|
+
|
|
1432
|
+
sage: sum(point3d((i,i^2,i^3), size=5) for i in range(10))
|
|
1433
|
+
Graphics3d Object
|
|
1434
|
+
|
|
1435
|
+
.. PLOT::
|
|
1436
|
+
|
|
1437
|
+
sphinx_plot(sum([point3d((i,i^2,i^3), size=5) for i in range(10)]))
|
|
1438
|
+
|
|
1439
|
+
We check to make sure this works with vectors and other iterables::
|
|
1440
|
+
|
|
1441
|
+
sage: pl = point3d([vector(ZZ,(1, 0, 0)), vector(ZZ,(0, 1, 0)), (-1, -1, 0)])
|
|
1442
|
+
sage: print(point(vector((2,3,4))))
|
|
1443
|
+
Graphics3d Object
|
|
1444
|
+
|
|
1445
|
+
sage: c = polytopes.hypercube(3) # needs sage.geometry.polyhedron
|
|
1446
|
+
sage: v = c.vertices()[0]; v # needs sage.geometry.polyhedron
|
|
1447
|
+
A vertex at (1, -1, -1)
|
|
1448
|
+
sage: print(point(v)) # needs sage.geometry.polyhedron
|
|
1449
|
+
Graphics3d Object
|
|
1450
|
+
|
|
1451
|
+
We check to make sure the options work::
|
|
1452
|
+
|
|
1453
|
+
sage: point3d((4,3,2), size=20, color='red', opacity=.5)
|
|
1454
|
+
Graphics3d Object
|
|
1455
|
+
|
|
1456
|
+
.. PLOT::
|
|
1457
|
+
|
|
1458
|
+
sphinx_plot(point3d((4,3,2),size=20,color='red',opacity=.5))
|
|
1459
|
+
|
|
1460
|
+
numpy arrays can be provided as input::
|
|
1461
|
+
|
|
1462
|
+
sage: import numpy
|
|
1463
|
+
sage: point3d(numpy.array([1,2,3]))
|
|
1464
|
+
Graphics3d Object
|
|
1465
|
+
|
|
1466
|
+
.. PLOT::
|
|
1467
|
+
|
|
1468
|
+
import numpy
|
|
1469
|
+
sphinx_plot(point3d(numpy.array([1,2,3])))
|
|
1470
|
+
|
|
1471
|
+
::
|
|
1472
|
+
|
|
1473
|
+
sage: point3d(numpy.array([[1,2,3], [4,5,6], [7,8,9]]))
|
|
1474
|
+
Graphics3d Object
|
|
1475
|
+
|
|
1476
|
+
.. PLOT::
|
|
1477
|
+
|
|
1478
|
+
import numpy
|
|
1479
|
+
sphinx_plot(point3d(numpy.array([[1,2,3], [4,5,6], [7,8,9]])))
|
|
1480
|
+
|
|
1481
|
+
We check that iterators of points are accepted (:issue:`13890`)::
|
|
1482
|
+
|
|
1483
|
+
sage: point3d(iter([(1,1,2),(2,3,4),(3,5,8)]), size=20, color='red')
|
|
1484
|
+
Graphics3d Object
|
|
1485
|
+
|
|
1486
|
+
TESTS::
|
|
1487
|
+
|
|
1488
|
+
sage: point3d([])
|
|
1489
|
+
Graphics3d Object
|
|
1490
|
+
"""
|
|
1491
|
+
try:
|
|
1492
|
+
l = len(v)
|
|
1493
|
+
except TypeError:
|
|
1494
|
+
# argument is an iterator
|
|
1495
|
+
v = list(v)
|
|
1496
|
+
l = len(v)
|
|
1497
|
+
|
|
1498
|
+
if l == 0:
|
|
1499
|
+
from sage.plot.plot3d.base import Graphics3d
|
|
1500
|
+
return Graphics3d()
|
|
1501
|
+
|
|
1502
|
+
if l == 3:
|
|
1503
|
+
try:
|
|
1504
|
+
# check if the first element can be changed to a float
|
|
1505
|
+
RDF(v[0])
|
|
1506
|
+
return Point(v, size, **kwds)
|
|
1507
|
+
except TypeError:
|
|
1508
|
+
pass
|
|
1509
|
+
|
|
1510
|
+
A = sum([Point(z, size, **kwds) for z in v])
|
|
1511
|
+
A._set_extra_kwds(kwds)
|
|
1512
|
+
return A
|