8000 Modified the grid updation to reuse the assets instead of recreation · NickNgHK/robotics-toolbox-python@1a8e0c9 · GitHub
[go: up one dir, main page]

Skip to content

Commit 1a8e0c9

Browse files
committed
Modified the grid updation to reuse the assets instead of recreation
1 parent 57c3854 commit 1a8e0c9

File tree

3 files changed

+75
-29
lines changed

3 files changed

+75
-29
lines changed

graphics/graphics_canvas.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,6 @@ def init_canvas(height=500, width=1000, title='', caption='', grid=True):
3939
convert_grid_to_z_up()
4040

4141
graphics_grid = GraphicsGrid()
42-
graphics_grid.draw_grid()
4342
if not grid:
4443
graphics_grid.set_visibility(False)
4544

graphics/graphics_grid.py

Lines changed: 74 additions & 27 deletions
< 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">
"""
Original file line numberDiff line numberDiff line change
@@ -10,33 +10,32 @@ def __init__(self):
1010
self.camera_pos = scene.camera.pos
1111
self.camera_axes = scene.camera.axis
1212
# Initialise a grid object
13-
self.grid_object = [None, []]
13+
self.grid_object = [[], []]
14+
self.__init_grid()
1415

15-
def draw_grid(self):
16+
def __init_grid(self):
1617
"""
17-
Display grids along the x, y, z axes.
18-
18+
Initialise the grid along the x, y, z axes.
1919
"""
20-
2120
num_squares = 10 # Length of the grid in each direction (in units)
2221
relative_cam = True # Whether the grid follows the camera rotation and movement
2322

24-
the_grid = self.__create_grid(relative_cam, num_squares)
23+
the_grid = self.__create_grid_objects(relative_cam, num_squares)
2524
self.grid_object[0] = the_grid
2625

2726
# 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)
2928

30-
def __create_grid(self, bool_camera_relative, num_squares):
29+
def __create_grid_objects(self, bool_camera_relative, num_squares):
3130
"""
3231
Draw a grid along each 3D plane, that is closest to the camera.
3332
3433
:param bool_camera_relative: Whether to draw the axes at the camera focus point or at (0, 0, 0).
3534
:type bool_camera_relative: bool
3635
:param num_squares: How many unit squares to draw along the axis.
3736
: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
4039
4140

4241
# Initial conditions
@@ -120,15 +119,64 @@ def __create_grid(self, bool_camera_relative, num_squares):
120119
xy_plane = compound(xy_lines)
121120
yz_plane = compound(yz_lines)
122121

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]
129124

130125
return grid
131126

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+
132180
def update_grid(self):
133181
"""
134182
Update the grid axes and numbers if the camera position/rotation has changed.
@@ -148,12 +196,12 @@ def update_grid(self):
148196
self.camera_pos = new_camera_pos
149197
self.camera_axes = new_camera_axes
150198

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)
154204

155-
# Save new grid
156-
self.draw_grid()
157205
# Else save current grid
158206
else:
159207
# Already current
@@ -166,11 +214,10 @@ def set_visibility(self, is_visible):
166214
:param is_visible: Boolean of whether to display the grid
167215
:type is_visible: bool
168216
"""
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
174221

175222
def clear_scene(self):
176223
"""
@@ -187,7 +234,7 @@ def clear_scene(self):
187234
and have the user assume they are all deleted. However, all objects can be redisplayed by setting the visibility
188235
"""
189236
# Save current grid visibility
190-
grid_visibility = self.grid_object[0].visible
237+
grid_visibility = self.grid_object[0][0].visible
191238

192239
# Set all objects invisible
193240
for scene_object in scene.objects:

graphics/graphics_text.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ def draw_text(label_text, label_position):
8484
return the_label
8585

8686

87-
def create_grid_numbers(numbers_list, bool_camera_relative, num_squares):
87+
def update_grid_numbers(numbers_list, bool_camera_relative, num_squares):
8888
"""
8989
Draw the grid numbers along the xyz axes.
9090

0 commit comments

Comments
 (0)
0