Skip to content

Commit 95ce5a2

Browse files
committed
Updated grid to use a scale (now can show smaller distances
1 parent 2cd925f commit 95ce5a2

File tree

2 files changed

+56
-31
lines changed

2 files changed

+56
-31
lines changed

graphics/graphics_grid.py

Lines changed: 39 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
from vpython import vector, compound, mag, box
2-
from numpy import sign, ceil
2+
from numpy import sign, ceil, arange
33
from graphics.graphics_text import update_grid_numbers
44
from graphics.graphics_object2d import Marker2D
55
from spatialmath import SE2
@@ -21,6 +21,7 @@ def __init__(self, scene):
2121

2222
self.__relative_cam = True
2323
self.__num_squares = 10
24+
self.__scale = 0.5
2425

2526
# Save the current camera settings
2627
self.camera_pos = self.__scene.camera.pos
@@ -56,7 +57,8 @@ def __init_grid(self):
5657
self.grid_object[self.__planes_idx] = the_grid
5758

5859
# Update the labels instead of recreating them
59-
update_grid_numbers(self.__focal_point, self.grid_object[self.__labels_idx], self.__num_squares, self.__scene)
60+
update_grid_numbers(self.__focal_point, self.grid_object[self.__labels_idx],
61+
self.__num_squares, self.__scale, self.__scene)
6062

6163
def __create_grid_objects(self):
6264
"""
@@ -96,24 +98,34 @@ def __create_grid_objects(self):
9698
# min = -num_squares or 0, around the default position
9799
# max = +num_squares or 0, around the default position
98100
# e.g. at the origin, for negative axes: -10 -> 0, positive axes: 0 -> 10
99-
min_x_coord = x_origin + int(-(self.__num_squares / 2) + (sign(camera_axes.x) * -1) * (self.__num_squares / 2))
100-
max_x_coord = x_origin + int((self.__num_squares / 2) + (sign(camera_axes.x) * -1) * (self.__num_squares / 2))
101+
min_x_coord = x_origin + int(-(self.__num_squares / 2) +
102+
(sign(camera_axes.x) * -1) * (self.__num_squares / 2)) * self.__scale
103+
max_x_coord = x_origin + int((self.__num_squares / 2) +
104+
(sign(camera_axes.x) * -1) * (self.__num_squares / 2)) * self.__scale
101105

102-
min_y_coord = y_origin + int(-(self.__num_squares / 2) + (sign(camera_axes.y) * -1) * (self.__num_squares / 2))
103-
max_y_coord = y_origin + int((self.__num_squares / 2) + (sign(camera_axes.y) * -1) * (self.__num_squares / 2))
106+
min_y_coord = y_origin + int(-(self.__num_squares / 2) +
107+
(sign(camera_axes.y) * -1) * (self.__num_squares / 2)) * self.__scale
108+
max_y_coord = y_origin + int((self.__num_squares / 2) +
109+
(sign(camera_axes.y) * -1) * (self.__num_squares / 2)) * self.__scale
104110

105-
min_z_coord = z_origin + int(-(self.__num_squares / 2) + (sign(camera_axes.z) * -1) * (self.__num_squares / 2))
106-
max_z_coord = z_origin + int((self.__num_squares / 2) + (sign(camera_axes.z) * -1) * (self.__num_squares / 2))
111+
min_z_coord = z_origin + int(-(self.__num_squares / 2) +
112+
(sign(camera_axes.z) * -1) * (self.__num_squares / 2)) * self.__scale
113+
max_z_coord = z_origin + int((self.__num_squares / 2) +
114+
(sign(camera_axes.z) * -1) * (self.__num_squares / 2)) * self.__scale
115+
116+
x_coords = arange(min_x_coord, max_x_coord + self.__scale, self.__scale)
117+
y_coords = arange(min_y_coord, max_y_coord + self.__scale, self.__scale)
118+
z_coords = arange(min_z_coord, max_z_coord + self.__scale, self.__scale)
107119

108120
# XZ plane
109-
for x_point in range(min_x_coord, max_x_coord + 1):
121+
for x_point in x_coords:
110122
# Draw a line across for each x coord, along the same y-axis, from min to max z coord
111123
xz_lines.append(create_line(
112124
vector(x_point, y_origin, min_z_coord),
113125
vector(x_point, y_origin, max_z_coord),
114126
self.__scene
115127
))
116-
for z_point in range(min_z_coord, max_z_coord + 1):
128+
for z_point in z_coords:
117129
# Draw a line across each z coord, along the same y-axis, from min to max z coord
118130
xz_lines.append(create_line(
119131
vector(min_x_coord, y_origin, z_point),
@@ -122,14 +134,14 @@ def __create_grid_objects(self):
122134
))
123135

124136
# XY plane
125-
for x_point in range(min_x_coord, max_x_coord + 1):
137+
for x_point in x_coords:
126138
# Draw a line across each x coord, along the same z-axis, from min to max y coord
127139
xy_lines.append(create_line(
128140
vector(x_point, min_y_coord, z_origin),
129141
vector(x_point, max_y_coord, z_origin),
130142
self.__scene
131143
))
132-
for y_point in range(min_y_coord, max_y_coord + 1):
144+
for y_point in y_coords:
133145
# Draw a line across each y coord, along the same z-axis, from min to max x coord
134146
xy_lines.append(create_line(
135147
vector(min_x_coord, y_point, z_origin),
@@ -138,14 +150,14 @@ def __create_grid_objects(self):
138150
))
139151

140152
# YZ plane
141-
for y_point in range(min_y_coord, max_y_coord + 1):
153+
for y_point in y_coords:
142154
# Draw a line across each y coord, along the same x-axis, from min to max z coord
143155
yz_lines.append(create_line(
144156
vector(x_origin, y_point, min_z_coord),
145157
vector(x_origin, y_point, max_z_coord),
146158
self.__scene
147159
))
148-
for z_point in range(min_z_coord, max_z_coord + 1):
160+
for z_point in z_coords:
149161
# Draw a line across each z coord, along the same x-axis, from min to max y coord
150162
yz_lines.append(create_line(
151163
vector(x_origin, min_y_coord, z_point),
@@ -203,14 +215,20 @@ def __move_grid_objects(self):
203215
# min = -num_squares or 0, around the default position
204216
# max = +num_squares or 0, around the default position
205217
# e.g. at the origin, for negative axes: -10 -> 0, positive axes: 0 -> 10
206-
min_x_coord = x_origin + int(-(self.__num_squares / 2) + (sign(camera_axes.x) * -1) * (self.__num_squares / 2))
207-
max_x_coord = x_origin + int((self.__num_squares / 2) + (sign(camera_axes.x) * -1) * (self.__num_squares / 2))
218+
min_x_coord = x_origin + int(-(self.__num_squares / 2) +
219+
(sign(camera_axes.x) * -1) * (self.__num_squares / 2)) * self.__scale
220+
max_x_coord = x_origin + int((self.__num_squares / 2) +
221+
(sign(camera_axes.x) * -1) * (self.__num_squares / 2)) * self.__scale
208222

209-
min_y_coord = y_origin + int(-(self.__num_squares / 2) + (sign(camera_axes.y) * -1) * (self.__num_squares / 2))
210-
max_y_coord = y_origin + int((self.__num_squares / 2) + (sign(camera_axes.y) * -1) * (self.__num_squares / 2))
223+
min_y_coord = y_origin + int(-(self.__num_squares / 2) +
224+
(sign(camera_axes.y) * -1) * (self.__num_squares / 2)) * self.__scale
225+
max_y_coord = y_origin + int((self.__num_squares / 2) +
226+
(sign(camera_axes.y) * -1) * (self.__num_squares / 2)) * self.__scale
211227

212-
min_z_coord = z_origin + int(-(self.__num_squares / 2) + (sign(camera_axes.z) * -1) * (self.__num_squares / 2))
213-
max_z_coord = z_origin + int((self.__num_squares / 2) + (sign(camera_axes.z) * -1) * (self.__num_squares / 2))
228+
min_z_coord = z_origin + int(-(self.__num_squares / 2) +
229+
(sign(camera_axes.z) * -1) * (self.__num_squares / 2)) * self.__scale
230+
max_z_coord = z_origin + int((self.__num_squares / 2) +
231+
(sign(camera_axes.z) * -1) * (self.__num_squares / 2)) * self.__scale
214232

215233
# Compound origins are in the middle of the bounding boxes. Thus new pos will be between max and min.
216234
x_middle = (max_x_coord + min_x_coord) / 2
@@ -257,6 +275,7 @@ def update_grid(self):
257275
update_grid_numbers(self.__focal_point,
258276
self.grid_object[self.__labels_idx],
259277
self.__num_squares,
278+
self.__scale,
260279
self.__scene)
261280

262281
def toggle_2d_3d(self):

graphics/graphics_text.py

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
from vpython import color, label, mag, vector
2-
from numpy import sign
2+
from numpy import sign, arange
33

44

55
def draw_label(label_text, label_position, scene):
@@ -90,7 +90,7 @@ def draw_text(label_text, label_position, scene):
9090
return the_label
9191

9292

93-
def update_grid_numbers(focal_point, numbers_list, num_squares, scene):
93+
def update_grid_numbers(focal_point, numbers_list, num_squares, scale, scene):
9494
"""
9595
Draw the grid numbers along the xyz axes.
9696
@@ -100,6 +100,8 @@ def update_grid_numbers(focal_point, numbers_list, num_squares, scene):
100100
:type numbers_list: `list`
101101
:param num_squares: How many unit squares to draw along the axis.
102102
:type num_squares: `int`
103+
:param scale: The scaled length of 1 square unit
104+
:type scale: `float`
103105
:param scene: The scene in which to draw the object
104106
:type scene: class:`vpython.canvas`
105107
"""
@@ -124,22 +126,26 @@ def update_grid_numbers(focal_point, numbers_list, num_squares, scene):
124126
# min = -num_squares or 0, around the default position
125127
# max = +num_squares or 0, around the default position
126128
# e.g. at the origin, for negative axes: -10 -> 0, positive axes: 0 -> 10
127-
min_x_coord = x_origin + int(-(num_squares / 2) + (sign(camera_axes.x) * -1) * (num_squares / 2))
128-
max_x_coord = x_origin + int((num_squares / 2) + (sign(camera_axes.x) * -1) * (num_squares / 2))
129+
min_x_coord = x_origin + int(-(num_squares / 2) + (sign(camera_axes.x) * -1) * (num_squares / 2)) * scale
130+
max_x_coord = x_origin + int((num_squares / 2) + (sign(camera_axes.x) * -1) * (num_squares / 2)) * scale
129131

130-
min_y_coord = y_origin + int(-(num_squares / 2) + (sign(camera_axes.y) * -1) * (num_squares / 2))
131-
max_y_coord = y_origin + int((num_squares / 2) + (sign(camera_axes.y) * -1) * (num_squares / 2))
132+
min_y_coord = y_origin + int(-(num_squares / 2) + (sign(camera_axes.y) * -1) * (num_squares / 2)) * scale
133+
max_y_coord = y_origin + int((num_squares / 2) + (sign(camera_axes.y) * -1) * (num_squares / 2)) * scale
132134

133-
min_z_coord = z_origin + int(-(num_squares / 2) + (sign(camera_axes.z) * -1) * (num_squares / 2))
134-
max_z_coord = z_origin + int((num_squares / 2) + (sign(camera_axes.z) * -1) * (num_squares / 2))
135+
min_z_coord = z_origin + int(-(num_squares / 2) + (sign(camera_axes.z) * -1) * (num_squares / 2)) * scale
136+
max_z_coord = z_origin + int((num_squares / 2) + (sign(camera_axes.z) * -1) * (num_squares / 2)) * scale
137+
138+
x_coords = arange(min_x_coord, max_x_coord + scale, scale)
139+
y_coords = arange(min_y_coord, max_y_coord + scale, scale)
140+
z_coords = arange(min_z_coord, max_z_coord + scale, scale)
135141

136142
# If input is empty, append new, otherwise update current
137143
append = len(numbers_list) == 0
138144
# Dimensions don't change between updates, so indexing shall remain the same
139145
index = 0
140146

141147
# X plane
142-
for x_pos in range(min_x_coord, max_x_coord + 1):
148+
for x_pos in x_coords:
143149
# Draw the corresponding unit number at each x coordinate
144150
txt = str(x_pos)
145151
pos = vector(x_pos + padding, y_origin + padding, z_origin)
@@ -164,7 +170,7 @@ def update_grid_numbers(focal_point, numbers_list, num_squares, scene):
164170
index += 1
165171

166172
# Y plane
167-
for y_pos in range(min_y_coord, max_y_coord + 1):
173+
for y_pos in y_coords:
168174
# Draw the corresponding unit number at each x coordinate
169175
txt = str(y_pos)
170176
pos = vector(x_origin, y_pos + padding, z_origin + padding)
@@ -189,7 +195,7 @@ def update_grid_numbers(focal_point, numbers_list, num_squares, scene):
189195
index += 1
190196

191197
# Z plane
192-
for z_pos in range(min_z_coord, max_z_coord + 1):
198+
for z_pos in z_coords:
193199
# Draw the corresponding unit number at each x coordinate
194200
txt = str(z_pos)
195201
pos = vector(x_origin, y_origin - padding, z_pos + padding)

0 commit comments

Comments
 (0)