@@ -10,33 +10,32 @@ def __init__(self):
10
10
self .camera_pos = scene .camera .pos
11
11
self .camera_axes = scene .camera .axis
12
12
# Initialise a grid object
13
- self .grid_object = [None , []]
13
+ self .grid_object = [[], []]
14
+ self .__init_grid ()
14
15
15
- def draw_grid (self ):
16
+ def __init_grid (self ):
16
17
"""
17
- Display grids along the x, y, z axes.
18
-
18
+ Initialise the grid along the x, y, z axes.
19
19
"""
20
-
21
20
num_squares = 10 # Length of the grid in each direction (in units)
22
21
relative_cam = True # Whether the grid follows the camera rotation and movement
23
22
24
- the_grid = self .__create_grid (relative_cam , num_squares )
23
+ the_grid = self .__create_grid_objects (relative_cam , num_squares )
25
24
self .grid_object [0 ] = the_grid
26
25
27
26
# Update the labels instead of recreating them
28
- create_grid_numbers (self .grid_object [1 ], relative_cam , num_squares )
27
+ update_grid_numbers (self .grid_object [1 ], relative_cam , num_squares )
29
28
30
- def __create_grid (self , bool_camera_relative , num_squares ):
29
+ def __create_grid_objects (self , bool_camera_relative , num_squares ):
31
30
"""
32
31
Draw a grid along each 3D plane, that is closest to the camera.
33
32
34
33
:param bool_camera_relative: Whether to draw the axes at the camera focus point or at (0, 0, 0).
35
34
:type bool_camera_relative: bool
36
35
:param num_squares: How many unit squares to draw along the axis.
37
36
:type num_squares: int
38
- :return: Vpython compound object of the three drawn axes.
39
- :rtype: class:`vpython.compound`
37
+ :return: List of the three drawn axes.
38
+ :rtype: list
40
39
<
10000
td data-grid-cell-id="diff-76d9463d726f8ac6571151993101d33f77e3c7b82d8a11df87da360e68ae96af-40-39-2" data-line-anchor="diff-76d9463d726f8ac6571151993101d33f77e3c7b82d8a11df87da360e68ae96afR39" data-selected="false" role="gridcell" style="background-color:var(--bgColor-default);padding-right:24px" tabindex="-1" valign="top" class="focusable-grid-cell diff-text-cell right-side-diff-cell left-side"> """
41
40
42
41
# Initial conditions
@@ -120,15 +119,64 @@ def __create_grid(self, bool_camera_relative, num_squares):
120
119
xy_plane = compound (xy_lines )
121
120
yz_plane = compound (yz_lines )
122
121
123
- # Combine all into one object
124
- grid = compound ([xy_plane , xz_plane , yz_plane ])
125
-
126
- # TODO
127
- # Instead of creating a new grid everytime, reuse old, but rotate. e.g. (+) inc in x-axis pos = (+) rot about z
128
- # Other option is to not do compound, but update pos of each of the planes. (might be easier)
122
+ # Combine all into one list
123
+ grid = [xy_plane , xz_plane , yz_plane ]
129
124
130
125
return grid
131
126
127
+ def __move_grid_objects (self , bool_camera_relative , num_squares ):
128
+ camera_axes = self .camera_axes
129
+ # Locate centre of axes
130
+ if bool_camera_relative :
131
+ x_origin , y_origin , z_origin = round (scene .center .x ), round (scene .center .y ), round (scene .center .z )
132
+ else :
133
+ x_origin , y_origin , z_origin = 0 , 0 , 0
134
+
135
+ # CAMERA AXES | DISPLAYED GRID | XZ PLANE | XY PLANE | YZ PLANE
136
+ # x,y,z | x,y,z | x,z | x,y | y,z
137
+ # -------------+-----------------+----------+----------+----------
138
+ # -,-,- | +,+,+ | +,+ | +,+ | +,+
139
+ # -,-,+ | +,+,- | +,- | +,+ | +,-
140
+ # -,+,- | +,-,+ | +,+ | +,- | -,+
141
+ # -,+,+ | +,-,- | +,- | +,- | -,-
142
+ # +,-,- | -,+,+ | -,+ | -,+ | +,+
143
+ # +,-,+ | -,+,- | -,- | -,+ | +,-
144
+ # +,+,- | -,-,+ | -,+ | -,- | -,+
145
+ # +,+,+ | -,-,- | -,- | -,- | -,-
146
+ # min = -num_squares or 0, around the default position
147
+ # max = +num_squares or 0, around the default position
148
+ # e.g. at the origin, for negative axes: -10 -> 0, positive axes: 0 -> 10
149
+ min_x_coord = x_origin + int (- (num_squares / 2 ) + (sign (camera_axes .x ) * - 1 ) * (num_squares / 2 ))
150
+ max_x_coord = x_origin + int ((num_squares / 2 ) + (sign (camera_axes .x ) * - 1 ) * (num_squares / 2 ))
151
+
152
+ min_y_coord = y_origin + int (
57AE
- (num_squares / 2 ) + (sign (camera_axes .y ) * - 1 ) * (num_squares / 2 ))
153
+ max_y_coord = y_origin + int ((num_squares / 2 ) + (sign (camera_axes .y ) * - 1 ) * (num_squares / 2 ))
154
+
155
+ min_z_coord = z_origin + int (- (num_squares / 2 ) + (sign (camera_axes .z ) * - 1 ) * (num_squares / 2 ))
156
+ max_z_coord = z_origin + int ((num_squares / 2 ) + (sign (camera_axes .z ) * - 1 ) * (num_squares / 2 ))
157
+
158
+ x_middle = (max_x_coord + min_x_coord ) / 2
159
+ y_middle = (max_y_coord + min_y_coord ) / 2
160
+ z_middle = (max_z_coord + min_z_coord ) / 2
161
+
162
+ # XY Plane
163
+ if camera_axes .z < 0 :
164
+ self .grid_object [0 ][0 ].pos = vector (x_middle , y_middle , min_z_coord )
165
+ else :
166
+ self .grid_object [0 ][0 ].pos = vector (x_middle , y_middle , max_z_coord )
167
+
168
+ # XZ Plane
169
+ if camera_axes .y < 0 :
170
+ self .grid_object [0 ][1 ].pos = vector (x_middle , min_y_coord , z_middle )
171
+ else :
172
+ self .grid_object [0 ][1 ].pos = vector (x_middle , max_y_coord , z_middle )
173
+
174
+ # YZ Plane
175
+ if camera_axes .x < 0 :
176
+ self .grid_object [0 ][2 ].pos = vector (min_x_coord , y_middle , z_middle )
177
+ else :
178
+ self .grid_object [0 ][2 ].pos = vector (max_x_coord , y_middle , z_middle )
179
+
132
180
def update_grid (self ):
133
181
"""
134
182
Update the grid axes and numbers if the camera position/rotation has changed.
@@ -148,12 +196,12 @@ def update_grid(self):
148
196
self .camera_pos = new_camera_pos
149
197
self .camera_axes = new_camera_axes
150
198
151
- # Delete old grid
152
- self .set_visibility (False )
153
- self .grid_object [0 ] = None
199
+ # Update grid
200
+ num_squares = 10 # Length of the grid in each direction (in units)
201
+ relative_cam = True # Whether the grid follows the camera rotation and movement
202
+ self .__move_grid_objects (relative_cam , num_squares )
203
+ update_grid_numbers (self .grid_object [1 ], relative_cam , num_squares )
154
204
155
- # Save new grid
156
- self .draw_grid ()
157
205
# Else save current grid
158
206
else :
159
207
# Already current
@@ -166,11 +214,10 @@ def set_visibility(self, is_visible):
166
214
:param is_visible: Boolean of whether to display the grid
167
215
:type is_visible: bool
168
216
"""
169
- # If no grid, turn the objects invisible
170
- self .grid_object [0 ].visible = is_visible
171
- # TODO this will need to be uncommented once grid_object reuse is done
172
- #for number in self.grid_object[1]:
173
- # number.visible = is_visible
217
+ for plane in self .grid_object [0 ]:
218
+ plane .visible = is_visible
219
+ for number in self .grid_object [1 ]:
220
+ number .visible = is_visible
174
221
175
222
def clear_scene (self ):
176
223
"""
@@ -187,7 +234,7 @@ def clear_scene(self):
187
234
and have the user assume they are all deleted. However, all objects can be redisplayed by setting the visibility
188
235
"""
189
236
# Save current grid visibility
190
- grid_visibility = self .grid_object [0 ].visible
237
+ grid_visibility = self .grid_object [0 ][ 0 ] .visible
191
238
192
239
# Set all objects invisible
193
240
for scene_object in scene .objects :
0 commit comments