Skip to content

Commit c83ddd2

Browse files
committed
Update doco for base, add more plots to the doco
1 parent 42a8c97 commit c83ddd2

8 files changed

Lines changed: 259 additions & 48 deletions

File tree

docs/source/2d_ellipse.rst

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
2D ellipse
2+
^^^^^^^^^^
3+
4+
.. autoclass:: spatialmath.geom2d.Ellipse
5+
:members:
6+
:undoc-members:
7+
:special-members: __init__, __str__, __len__

docs/source/2d_linesegment.rst

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
2D line segment
2+
^^^^^^^^^^^^^^^
3+
4+
.. autoclass:: spatialmath.geom2d.LineSegment2
5+
:members:
6+
:undoc-members:

docs/source/func_numeric.rst

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
Numerical utility functions
2+
===========================
3+
4+
.. automodule:: spatialmath.base.numeric
5+
:members:
6+
:undoc-members:
7+
:show-inheritance:
8+
:inherited-members:
9+
:special-members:

docs/source/functions.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ Function reference
1414
func_vector
1515
func_graphics
1616
func_args
17+
func_numeric
1718

1819

1920

docs/source/spatialmath.rst

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,4 +99,6 @@ Geometry in 2D
9999
:maxdepth: 2
100100

101101
2d_line
102-
2d_polygon
102+
2d_linesegment
103+
2d_polygon
104+
2d_ellipse

spatialmath/__init__.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
from spatialmath.pose2d import SO2, SE2
44
from spatialmath.pose3d import SO3, SE3
55
from spatialmath.baseposematrix import BasePoseMatrix
6-
from spatialmath.geom2d import Line2, Polygon2
6+
from spatialmath.geom2d import Line2, LineSegment2, Polygon2, Ellipse
77
from spatialmath.geom3d import Line3, Plane3
88
from spatialmath.twist import Twist3, Twist2
99
from spatialmath.spatialvector import (
@@ -40,7 +40,9 @@
4040
"Line3",
4141
"Plane3",
4242
"Line2",
43+
"LineSegment2",
4344
"Polygon2",
45+
"Ellipse",
4446
]
4547

4648
try:

spatialmath/base/graphics.py

Lines changed: 152 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -59,15 +59,21 @@ def plot_text(
5959
:return: the matplotlib object
6060
:rtype: list of Text instance
6161
62-
Example:
63-
64-
.. runblock:: pycon
62+
Example::
6563
6664
>>> from spatialmath.base import plotvol2, plot_text
6765
>>> plotvol2(5)
6866
>>> plot_text((1,3), 'foo')
6967
>>> plot_text((2,2), 'bar', 'b')
7068
>>> plot_text((2,2), 'baz', fontsize=14, horizontalalignment='centre')
69+
70+
.. plot::
71+
72+
from spatialmath.base import plotvol2, plot_text
73+
plotvol2(5)
74+
plot_text((1,3), 'foo')
75+
plot_text((2,2), 'bar', 'b')
76+
plot_text((2,2), 'baz', fontsize=14, horizontalalignment='centre')
7177
"""
7278

7379
defaults = {"horizontalalignment": "left", "verticalalignment": "center"}
@@ -233,14 +239,19 @@ def plot_homline(
233239
234240
If ``lines`` is a 3xN array then ``N`` lines are drawn, one per column.
235241
236-
Example:
237-
238-
.. runblock:: pycon
242+
Example::
239243
240244
>>> from spatialmath.base import plotvol2, plot_homline
241245
>>> plotvol2(5)
242246
>>> plot_homline((1, -2, 3))
243247
>>> plot_homline((1, -2, 3), 'k--') # dashed black line
248+
249+
.. plot::
250+
251+
from spatialmath.base import plotvol2, plot_homline
252+
plotvol2(5)
253+
plot_homline((1, -2, 3))
254+
plot_homline((1, -2, 3), 'k--') # dashed black line
244255
"""
245256
ax = axes_logic(ax, 2)
246257
# get plot limits from current graph
@@ -331,14 +342,19 @@ def plot_box(
331342
For plots where the y-axis is inverted (eg. for images) then top is the
332343
smaller vertical coordinate.
333344
334-
Example:
335-
336-
.. runblock:: pycon
345+
Example::
337346
338347
>>> from spatialmath.base import plotvol2, plot_box
339348
>>> plotvol2(5)
340349
>>> plot_box('r', centre=(2,3), wh=1) # w=h=1
341350
>>> plot_box(tl=(1,1), br=(0,2), filled=True, color='b')
351+
352+
.. plot::
353+
354+
from spatialmath.base import plotvol2, plot_box
355+
plotvol2(5)
356+
plot_box('r', centre=(2,3), wh=1) # w=h=1
357+
plot_box(tl=(1,1), br=(0,2), filled=True, color='b')
342358
"""
343359

344360
if wh is not None:
@@ -432,13 +448,18 @@ def plot_arrow(
432448
:type ax: Axes, optional
433449
:param kwargs: argumetns to pass to :class:`matplotlib.patches.Arrow`
434450
435-
Example:
436-
437-
.. runblock:: pycon
451+
Example::
438452
439453
>>> from spatialmath.base import plotvol2, plot_arrow
440454
>>> plotvol2(5)
441455
>>> plot_arrow((-2, 2), (3, 4), color='r', width=0.1) # red arrow
456+
457+
.. plot::
458+
459+
from spatialmath.base import plotvol2, plot_arrow
460+
plotvol2(5)
461+
plot_arrow((-2, 2), (3, 4), color='r', width=0.1) # red arrow
462+
442463
"""
443464
ax = axes_logic(ax, 2)
444465

@@ -465,14 +486,20 @@ def plot_polygon(
465486
:return: Matplotlib artist
466487
:rtype: line or patch
467488
468-
Example:
469-
470-
.. runblock:: pycon
489+
Example::
471490
472491
>>> from spatialmath.base import plotvol2, plot_polygon
473492
>>> plotvol2(5)
474493
>>> vertices = np.array([[-1, 2, -1], [1, 0, -1]])
475494
>>> plot_polygon(vertices, filled=True, facecolor='g') # green filled triangle
495+
496+
.. plot::
497+
498+
from spatialmath.base import plotvol2, plot_polygon
499+
plotvol2(5)
500+
vertices = np.array([[-1, 2, -1], [1, 0, -1]])
501+
plot_polygon(vertices, filled=True, facecolor='g') # green filled triangle
502+
476503
"""
477504

478505
if close:
@@ -567,15 +594,34 @@ def plot_circle(
567594
taken as the centre of a circle. All circles have the same radius, color
568595
etc.
569596
570-
Example:
571-
572-
.. runblock:: pycon
597+
Example::
573598
574599
>>> from spatialmath.base import plotvol2, plot_circle
575600
>>> plotvol2(5)
576601
>>> plot_circle(1, 'r') # red circle
577602
>>> plot_circle(2, 'b--') # blue dashed circle
578603
>>> plot_circle(0.5, filled=True, facecolor='y') # yellow filled circle
604+
605+
.. plot::
606+
607+
from spatialmath.base import plotvol2, plot_circle
608+
plotvol2(5)
609+
plot_circle(1, 'r') # red circle
610+
611+
612+
.. plot::
613+
614+
from spatialmath.base import plotvol2, plot_circle
615+
plotvol2(5)
616+
plot_circle(2, 'b--') # blue dashed circle
617+
618+
619+
.. plot::
620+
621+
from spatialmath.base import plotvol2, plot_circle
622+
plotvol2(5)
623+
plot_circle(0.5, filled=True, facecolor='y') # yellow filled circle
624+
579625
"""
580626
centres = smb.getmatrix(centre, (2, None))
581627

@@ -693,14 +739,32 @@ def plot_ellipse(
693739
694740
Example:
695741
696-
.. runblock:: pycon
697-
698742
>>> from spatialmath.base import plotvol2, plot_circle
699743
>>> plotvol2(5)
700-
>>> plot_ellipse(np.diag((1,2)), 'r') # red ellipse
701-
>>> plot_ellipse(np.diag((1,2)), 'b--') # blue dashed ellipse
702-
>>> plot_ellipse(np.diag((1,2)), filled=True, facecolor='y') # yellow filled ellipse
744+
>>> plot_ellipse(np.array([[1, 1], [1, 2]]), 'r') # red ellipse
745+
>>> plot_ellipse(np.array([[1, 1], [1, 2]])), 'b--') # blue dashed ellipse
746+
>>> plot_ellipse(np.array([[1, 1], [1, 2]]), filled=True, facecolor='y') # yellow filled ellipse
703747
748+
.. plot::
749+
750+
from spatialmath import Ellipse
751+
from spatialmath.base import plotvol2
752+
plotvol2(5)
753+
plot_ellipse(np.array([[1, 1], [1, 2]]), 'r') # red ellipse
754+
755+
.. plot::
756+
757+
from spatialmath import Ellipse
758+
from spatialmath.base import plotvol2
759+
plotvol2(5)
760+
plot_ellipse(np.array([[1, 1], [1, 2]])), 'b--') # blue dashed ellipse
761+
762+
.. plot::
763+
764+
from spatialmath import Ellipse
765+
from spatialmath.base import plotvol2
766+
plotvol2(5)
767+
plot_ellipse(np.array([[1, 1], [1, 2]]), filled=True, facecolor='y') # yellow filled ellipse
704768
"""
705769
# allow for centre[2] to plot ellipse in a plane in a 3D plot
706770

@@ -779,14 +843,28 @@ def plot_sphere(
779843
taken as the centre of a sphere. All spheres have the same radius, color
780844
etc.
781845
782-
Example:
783-
784-
.. runblock:: pycon
846+
Example::
785847
786848
>>> from spatialmath.base import plot_sphere
787-
>>> plot_sphere(radius=1, color='r') # red sphere wireframe
849+
>>> plot_sphere(radius=1, color="r", resolution=10) # red sphere wireframe
788850
>>> plot_sphere(radius=1, centre=(1,1,1), filled=True, facecolor='b')
789851
852+
853+
.. plot::
854+
855+
from spatialmath.base import plot_sphere, plotvol3
856+
857+
plotvol3(2)
858+
plot_sphere(radius=1, color='r', resolution=5) # red sphere wireframe
859+
860+
.. plot::
861+
862+
from spatialmath.base import plot_sphere, plotvol3
863+
864+
plotvol3(5)
865+
plot_sphere(radius=1, centre=(1,1,1), filled=True, facecolor='b')
866+
867+
790868
:seealso: :func:`~matplotlib.pyplot.plot_surface`, :func:`~matplotlib.pyplot.plot_wireframe`
791869
"""
792870
ax = axes_logic(ax, 3)
@@ -855,8 +933,8 @@ def ellipsoid(
855933

856934
x, y, z = sphere() # unit sphere
857935
e = (
858-
s * sqrtm(E) @ np.array([x.flatten(), y.flatten(), z.flatten()])
859-
+ np.c_[centre].T
936+
scale * sqrtm(E) @ np.array([x.flatten(), y.flatten(), z.flatten()])
937+
+ np.c_[centre]
860938
)
861939
return (
862940
e[0, :].reshape(x.shape),
@@ -865,7 +943,7 @@ def ellipsoid(
865943
)
866944

867945
def plot_ellipsoid(
868-
E: R2x2,
946+
E: R3x3,
869947
centre: Optional[ArrayLike3] = (0, 0, 0),
870948
scale: Optional[float] = 1,
871949
confidence: Optional[float] = None,
@@ -896,16 +974,20 @@ def plot_ellipsoid(
896974
:param stride: [description], defaults to 1
897975
:type stride: int, optional
898976
899-
``plot_ellipse(E)`` draws the ellipsoid defined by :math:`x^T \mat{E} x = 0`
977+
``plot_ellipsoid(E)`` draws the ellipsoid defined by :math:`x^T \mat{E} x = 0`
900978
on the current plot.
901979
902980
Example::
903981
904-
H = plot_ellipse(diag([1 2]), [3 4]', 'r'); % draw red ellipse
905-
plot_ellipse(diag([1 2]), [5 6]', 'alter', H); % move the ellipse
906-
plot_ellipse(diag([1 2]), [5 6]', 'alter', H, 'LineColor', 'k'); % change color
982+
>>> plot_ellipsoid(np.diag([1, 2, 3]), [1, 1, 0], color="r", resolution=10); # draw red ellipsoid
983+
984+
.. plot::
907985
908-
plot_ellipse(COVAR, 'confidence', 0.95); % draw 95% confidence ellipse
986+
from spatialmath.base import plot_ellipsoid, plotvol3
987+
import numpy as np
988+
989+
plotvol3(4)
990+
plot_ellipsoid(np.diag([1, 2, 3]), [1, 1, 0], color="r", resolution=5); # draw red ellipsoid
909991
910992
.. note::
911993
@@ -979,7 +1061,7 @@ def plot_cylinder(
9791061
:type height: float or array_like(2)
9801062
:param resolution: number of points on circumference, defaults to 50
9811063
:param centre: position of centre
982-
:param pose: pose of sphere, defaults to None
1064+
:param pose: pose of cylinder, defaults to None
9831065
:type pose: SE3, optional
9841066
:param ax: axes to draw into, defaults to None
9851067
:type ax: Axes3D, optional
@@ -996,6 +1078,18 @@ def plot_cylinder(
9961078
The cylinder can be positioned by setting ``centre``, or positioned
9971079
and orientated by setting ``pose``.
9981080
1081+
Example::
1082+
1083+
>>> plot_cylinder(radius=1, height=(1,3))
1084+
1085+
.. plot::
1086+
1087+
from spatialmath.base import plot_cylinder, plotvol3
1088+
1089+
plotvol3(5)
1090+
plot_cylinder(radius=1, height=(1,3))
1091+
1092+
9991093
:seealso: :func:`~matplotlib.pyplot.plot_surface`, :func:`~matplotlib.pyplot.plot_wireframe`
10001094
"""
10011095
if smb.isscalar(height):
@@ -1065,6 +1159,17 @@ def plot_cone(
10651159
The cylinder can be positioned by setting ``centre``, or positioned
10661160
and orientated by setting ``pose``.
10671161
1162+
Example::
1163+
1164+
>>> plot_cone(radius=1, height=2)
1165+
1166+
.. plot::
1167+
1168+
from spatialmath.base import plot_cone, plotvol3
1169+
1170+
plotvol3(5)
1171+
plot_cone(radius=1, height=2)
1172+
10681173
:seealso: :func:`~matplotlib.pyplot.plot_surface`, :func:`~matplotlib.pyplot.plot_wireframe`
10691174
"""
10701175
ax = axes_logic(ax, 3)
@@ -1126,6 +1231,17 @@ def plot_cuboid(
11261231
:return: matplotlib collection
11271232
:rtype: Line3DCollection or Poly3DCollection
11281233
1234+
Example::
1235+
1236+
>>> plot_cone(radius=1, height=2)
1237+
1238+
.. plot::
1239+
1240+
from spatialmath.base import plot_cuboid, plotvol3
1241+
1242+
plotvol3(5)
1243+
plot_cuboid(sides=(3,2,1), centre=(0,1,2))
1244+
11291245
:seealso: :func:`~matplotlib.pyplot.plot_surface`, :func:`~matplotlib.pyplot.plot_wireframe`
11301246
"""
11311247

0 commit comments

Comments
 (0)