cgse-coordinates 2023.1.0__py3-none-any.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.
@@ -0,0 +1,195 @@
1
+ #!/usr/bin/env python3
2
+ # -*- coding: utf-8 -*-
3
+ """
4
+ This module defines some plotting functions for Reference Frames.
5
+
6
+ @author: Pierre Royer
7
+ """
8
+
9
+ import matplotlib.pyplot as plt
10
+ from mpl_toolkits.mplot3d import Axes3D # needed for 3d projection
11
+ import numpy as np
12
+ import egse
13
+ from egse.coordinates.referenceFrame import ReferenceFrame
14
+ from egse.coordinates.point import Point, Points
15
+
16
+
17
+ def plot_reference_frame(frame, master=None, figname=None, **kwargs):
18
+ """Plot a Reference Frame.
19
+
20
+ Args:
21
+ frame : egse.coordinates.referenceFrame.ReferenceFrame
22
+ master : master ReferenceFrame (optional)
23
+ figname: string. Name of matplotlib figure (can be pre-existing)
24
+ kwargs : passed to matplotlib.axes._subplots.Axes3DSubplot.quiver
25
+
26
+ Returns:
27
+ matplotlib.axes._subplots.Axes3DSubplot displaying the reference frame.
28
+
29
+ The three unit vectors are shown with the following colors ('RGB'):
30
+ x: Red
31
+ y: Green
32
+ z: Blue
33
+
34
+ .. note::
35
+ Use ax.set_xlim3d(min,max) to properly set the ranges of the display
36
+
37
+ """
38
+ if master is None:
39
+ tmpmaster = ReferenceFrame.createMaster()
40
+ else:
41
+ tmpmaster = master.__copy__()
42
+
43
+ f0 = frame.getOrigin()
44
+ fx = frame.getAxis('x', name='fx')
45
+ fy = frame.getAxis('y', name='fy')
46
+ fz = frame.getAxis('z', name='fz')
47
+ f0m = f0.expressIn(tmpmaster)[:3]
48
+ fxm = fx.expressIn(tmpmaster)[:3]
49
+ fym = fy.expressIn(tmpmaster)[:3]
50
+ fzm = fz.expressIn(tmpmaster)[:3]
51
+ del tmpmaster
52
+
53
+ # Origin of the X,Y and Z vectors (x = the 'x' coordinates of the origin of all 3 vectors)
54
+ # Every vector independently (--> plot in diff. colors)
55
+ x, y, z = np.array([f0m[0]]), np.array([f0m[1]]), np.array([f0m[2]])
56
+
57
+ # Orientation of the X,Y and Z vectors
58
+ vecxx, vecyx, veczx = np.array([fxm[0] - f0m[0]]), np.array([fym[0] - f0m[0]]), np.array([fzm[0] - f0m[0]])
59
+ vecxy, vecyy, veczy = np.array([fxm[1] - f0m[1]]), np.array([fym[1] - f0m[1]]), np.array([fzm[1] - f0m[1]])
60
+ vecxz, vecyz, veczz = np.array([fxm[2] - f0m[2]]), np.array([fym[2] - f0m[2]]), np.array([fzm[2] - f0m[2]])
61
+
62
+ kwargs.setdefault('length', 1)
63
+ kwargs.setdefault('normalize', True)
64
+ #kwargs.setdefault('figsize', (10,10))
65
+
66
+ fig = plt.figure(figname, figsize=plt.figaspect(1.0))
67
+ ax = fig.gca(projection='3d')
68
+ ax.quiver(x, y, z, vecxx, vecxy, vecxz, color='r', **kwargs)
69
+ ax.quiver(x, y, z, vecyx, vecyy, vecyz, color='g', **kwargs)
70
+ ax.quiver(x, y, z, veczx, veczy, veczz, color='b', **kwargs)
71
+ #ax.axis('equal')
72
+
73
+ return ax
74
+
75
+
76
+ def plot_points(points, master=None, figname=None, **kwargs):
77
+ """Plot the Points object.
78
+
79
+ Args:
80
+ points : either a (egse.coordinate.point.)Points object or a list of Point objects
81
+ master : master ReferenceFrame (optional)
82
+ figname: string. Name of matplotlib figure (can be pre-existing)
83
+ kwargs : passed to matplotlib.axes._subplots.Axes3DSubplot.scatter
84
+
85
+ Returns:
86
+ matplotlib.axes._subplots.Axes3DSubplot displaying the reference frame.
87
+
88
+ .. note::
89
+ Use ax.set_xlim3d(min,max) to properly set the ranges of the display.
90
+
91
+ """
92
+ if master is None:
93
+ tmpmaster = ReferenceFrame.createMaster()
94
+ else:
95
+ tmpmaster = master.__copy__()
96
+ #
97
+ if isinstance(points, list):
98
+ allpoints = Points(points, ref=tmpmaster)
99
+ elif isinstance(points, Points) or isinstance(points, egse.coordinates.point.Points):
100
+ allpoints = points
101
+ else:
102
+ raise ValueError("If the input is a list, all items in it must be Point objects")
103
+ #
104
+ del tmpmaster
105
+ #
106
+ coordinates = allpoints.coordinates
107
+ xs = coordinates[0, :]
108
+ ys = coordinates[1, :]
109
+ zs = coordinates[2, :]
110
+ #
111
+ kwargs.setdefault('s', 50)
112
+ kwargs.setdefault('marker', 'o')
113
+ kwargs.setdefault('color', 'k')
114
+ #
115
+ fig = plt.figure(figname)
116
+ ax = fig.gca(projection='3d')
117
+ ax.scatter(xs, ys, zs, **kwargs)
118
+
119
+ return ax
120
+
121
+
122
+ def plot_vectors(points, master=None, figname=None, fromorigin=True, **kwargs):
123
+ """Plot the Points object.
124
+
125
+ Args:
126
+ points : either a (egse.coordinate.point.)Points object or a list of Point objects
127
+ master : master ReferenceFrame (optional)
128
+ figname: string. Name of matplotlib figure (can be pre-existing)
129
+ fromorigin: bool
130
+ if True, all vectors are displayed starting from the origin
131
+ if False, all vectors go towards the origin
132
+ kwargs : passed to matplotlib.axes._subplots.Axes3DSubplot.scatter
133
+
134
+ Returns:
135
+ matplotlib.axes._subplots.Axes3DSubplot displaying the reference frame.
136
+
137
+ .. note::
138
+ Use ax.set_xlim3d(min,max) to properly set the ranges of the display.
139
+
140
+ """
141
+ #from egse.coordinates.point import Points
142
+
143
+ if master is None:
144
+ tmpmaster = ReferenceFrame.createMaster()
145
+ else:
146
+ tmpmaster = master.__copy__()
147
+ #
148
+ if isinstance(points, list):
149
+ allpoints = Points(points, ref=tmpmaster)
150
+ elif isinstance(points, Points) or isinstance(points, egse.coordinates.point.Points):
151
+ allpoints = points
152
+ else:
153
+ raise ValueError("If the input is a list, all items in it must be Point objects")
154
+ #
155
+ del tmpmaster
156
+ #
157
+
158
+ # SET DEFAULTS
159
+ kwargs.setdefault('color', 'k')
160
+ #
161
+
162
+ # PREPARE VECTOR COORDINATES
163
+ coordinates = allpoints.coordinates
164
+ xs = coordinates[0, :]
165
+ ys = coordinates[1, :]
166
+ zs = coordinates[2, :]
167
+
168
+ # Origin of the X,Y and Z vectors
169
+ # ==> x = the 'x' coordinates of the origin of all vectors)
170
+ # ==> [x,y,z] = the origin of points.ref
171
+ x, y, z = points.ref.getOrigin().coordinates[:3]
172
+ x = np.ones_like(xs) * x
173
+ y = np.ones_like(xs) * y
174
+ z = np.ones_like(xs) * z
175
+
176
+ ### PLOT
177
+
178
+ fig = plt.figure(figname)
179
+ ax = fig.gca(projection='3d')
180
+
181
+ if fromorigin == True:
182
+
183
+ ax.quiver(x, y, z, xs-x, ys-y, zs-z, **kwargs)
184
+
185
+ elif fromorigin == False:
186
+
187
+ ax.quiver(xs, ys, zs, x-xs, y-ys, z-zs, **kwargs)
188
+
189
+ else:
190
+ print("Parameter 'fromorigin' must be True or False")
191
+ print("Setting it to True by default")
192
+ ax.quiver(x, y, z, xs-x, ys-y, zs-z, **kwargs)
193
+
194
+ return ax
195
+