Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
Q = 1e-8 # total charges
# dq = 1e-8 / 6 # define charge of electrons
dq = Q / num_charge
charges = []
space_between = 2 * view_space_length / (num_charge + 1) # evenly divide space between each electrons
for x in arange(-view_space_length + space_between, view_space_length, space_between):
q = ElectricBall(pos=vector(x, 0, 0), radius=1, color=color.red, charge=dq)
charges.append(q)
# creat arrow around electric balls
arrows = []
observe_points_dis = 0.5 * view_space_length
for x in arange(-1.5 * view_space_length, 1.51 * view_space_length, observe_points_dis):
for y in arange(-1.0 * view_space_length, 1.01 * view_space_length, observe_points_dis):
for z in arange(-1.0 * view_space_length, 1.01 * view_space_length, observe_points_dis):
pointer = arrow(pos=vector(x, y, z), color=color.blue, opacity=0.0)
electrical_vector = vector(0, 0, 0)
# infinity large arrows will be ignored
infinity = False
# each arrow is affected by all of the charges
for q in charges:
direction_vector = pointer.pos - q.pos
if direction_vector.mag == 0:
infinity = True
break
# calculate electric field affected by each electric ball
E = (k * dq / direction_vector.mag ** 2) * direction_vector.norm()
# sum electric field at the each point
electrical_vector += E
if t in tr:
if tr[t] not in rings[t]:
rings[t].append(tr[t])
tr3d[i].append(rings[t].index(tr[t]))
else:
rings[t].append(("unk", -1))
tr3d[i].append(len(rings[t]) - 1)
for t in rings:
r = rings[t]
tmp_len = len(r)
for i in range(tmp_len):
if r[i][1] == -1:
col = vpython.vec(0.75, 0.75, 0.75)
elif r[i][1]:
col = vpython.color.green
else:
col = vpython.color.blue
s = IPsphere(pos=vpython.vec((tmp_len - 1) * vpython.cos(2 * i * vpython.pi / tmp_len), (tmp_len - 1) * vpython.sin(2 * i * vpython.pi / tmp_len), 2 * t), # noqa: E501
ip=r[i][0],
color=col)
for trlst in six.itervalues(tr3d):
if t <= len(trlst):
if trlst[t - 1] == i:
trlst[t - 1] = s
forecol = colgen(0.625, 0.4375, 0.25, 0.125)
for trlst in six.itervalues(tr3d):
col = vpython.vec(*next(forecol))
start = vpython.vec(0, 0, 0)
for ip in trlst:
vpython.cylinder(pos=start, axis=ip.pos - start, color=col, radius=0.2) # noqa: E501
def addFinitePlane(self, plane, colour=None, opacity=1.):
if not Visualiser.VISUALISER_ON:
return
if isinstance(plane, FinitePlane):
if colour == None:
colour = visual.color.blue
# visual doesn't support planes, so we draw a very thin box
H = .001
pos = (plane.length/2, plane.width/2, H/2)
pos = transform_point(pos, plane.transform)
size = (plane.length, plane.width, H)
axis = transform_direction((0,0,1), plane.transform)
visual.box(pos=visual.vec(pos), size=size, color=colour, opacity=opacity)
vel = np.array([state[7], state[8], state[9]]).flatten()
current_quat = Quaternion(att)
x_axis = current_quat.rotation_matrix.dot(np.array([1.0, 0.0, 0.0]))
y_axis = current_quat.rotation_matrix.dot(np.array([0.0, 1.0, 0.0]))
z_axis = current_quat.rotation_matrix.dot(np.array([0.0, 0.0, 1.0]))
if self.viewer is None:
self.viewer = canvas(title='Quadrotor 3D', width=640, height=480, center=vector(0, 0, 2), forward=vector(1, 1, -0.5), up=vector(0, 0, 1), background=color.white, range=4.0, autoscale = False)
self.render_quad1 = box(canvas = self.viewer, pos=vector(pos[0],pos[1],0), axis=vector(x_axis[0],x_axis[1],x_axis[2]), length=0.2, height=0.05, width=0.05)
self.render_quad2 = box(canvas = self.viewer, pos=vector(pos[0],pos[1],0), axis=vector(y_axis[0],y_axis[1],y_axis[2]), length=0.2, height=0.05, width=0.05)
self.render_rotor1 = cylinder(canvas = self.viewer, pos=vector(pos[0],pos[1],0), axis=vector(0.01*z_axis[0],0.01*z_axis[1],0.01*z_axis[2]), radius=0.2, color=color.cyan, opacity=0.5)
self.render_rotor2 = cylinder(canvas = self.viewer, pos=vector(pos[0],pos[1],0), axis=vector(0.01*z_axis[0],0.01*z_axis[1],0.01*z_axis[2]), radius=0.2, color=color.cyan, opacity=0.5)
self.render_rotor3 = cylinder(canvas = self.viewer, pos=vector(pos[0],pos[1],0), axis=vector(0.01*z_axis[0],0.01*z_axis[1],0.01*z_axis[2]), radius=0.2, color=color.cyan, opacity=0.5)
self.render_rotor4 = cylinder(canvas = self.viewer, pos=vector(pos[0],pos[1],0), axis=vector(0.01*z_axis[0],0.01*z_axis[1],0.01*z_axis[2]), radius=0.2, color=color.cyan, opacity=0.5)
self.render_velocity = pointer = arrow(pos=vector(pos[0],pos[1],0), axis=vector(vel[0],vel[1],vel[2]), shaftwidth=0.05, color=color.green)
self.render_ref = sphere(canvas = self.viewer, pos=vector(ref_pos[0], ref_pos[1], ref_pos[2]), radius=0.02, color=color.blue, make_trail = True)
grid_xy = make_grid(5, 100)
if self.state is None: return None
self.render_quad1.pos.x = pos[0]
self.render_quad1.pos.y = pos[1]
self.render_quad1.pos.z = pos[2]
self.render_quad2.pos.x = pos[0]
self.render_quad2.pos.y = pos[1]
self.render_quad2.pos.z = pos[2]
rotor_pos = 0.5*x_axis
self.render_rotor1.pos.x = pos[0] + rotor_pos[0]
self.render_rotor1.pos.y = pos[1] + rotor_pos[1]
self.render_rotor1.pos.z = pos[2] + rotor_pos[2]
rotor_pos = (-0.5)*x_axis
state = self.state
ref_pos = self.ref_pos
ref_vel = self.ref_vel
pos = np.array([state[0], state[1], state[2]]).flatten()
att = np.array([state[3], state[4], state[5], state[6]]).flatten()
vel = np.array([state[7], state[8], state[9]]).flatten()
current_quat = Quaternion(att)
x_axis = current_quat.rotation_matrix.dot(np.array([1.0, 0.0, 0.0]))
y_axis = current_quat.rotation_matrix.dot(np.array([0.0, 1.0, 0.0]))
z_axis = current_quat.rotation_matrix.dot(np.array([0.0, 0.0, 1.0]))
if self.viewer is None:
self.viewer = canvas(title='Quadrotor 3D', width=640, height=480, center=vector(0, 0, 2), forward=vector(1, 1, -0.5), up=vector(0, 0, 1), background=color.white, range=4.0, autoscale = False)
self.render_quad1 = box(canvas = self.viewer, pos=vector(pos[0],pos[1],0), axis=vector(x_axis[0],x_axis[1],x_axis[2]), length=0.2, height=0.05, width=0.05)
self.render_quad2 = box(canvas = self.viewer, pos=vector(pos[0],pos[1],0), axis=vector(y_axis[0],y_axis[1],y_axis[2]), length=0.2, height=0.05, width=0.05)
self.render_rotor1 = cylinder(canvas = self.viewer, pos=vector(pos[0],pos[1],0), axis=vector(0.01*z_axis[0],0.01*z_axis[1],0.01*z_axis[2]), radius=0.2, color=color.cyan, opacity=0.5)
self.render_rotor2 = cylinder(canvas = self.viewer, pos=vector(pos[0],pos[1],0), axis=vector(0.01*z_axis[0],0.01*z_axis[1],0.01*z_axis[2]), radius=0.2, color=color.cyan, opacity=0.5)
self.render_rotor3 = cylinder(canvas = self.viewer, pos=vector(pos[0],pos[1],0), axis=vector(0.01*z_axis[0],0.01*z_axis[1],0.01*z_axis[2]), radius=0.2, color=color.cyan, opacity=0.5)
self.render_rotor4 = cylinder(canvas = self.viewer, pos=vector(pos[0],pos[1],0), axis=vector(0.01*z_axis[0],0.01*z_axis[1],0.01*z_axis[2]), radius=0.2, color=color.cyan, opacity=0.5)
self.render_velocity = pointer = arrow(pos=vector(pos[0],pos[1],0), axis=vector(vel[0],vel[1],vel[2]), shaftwidth=0.05, color=color.green)
self.render_ref = sphere(canvas = self.viewer, pos=vector(ref_pos[0], ref_pos[1], ref_pos[2]), radius=0.02, color=color.blue, make_trail = True)
grid_xy = make_grid(5, 100)
if self.state is None: return None
self.render_quad1.pos.x = pos[0]
self.render_quad1.pos.y = pos[1]
self.render_quad1.pos.z = pos[2]
self.render_quad2.pos.x = pos[0]
self.render_quad2.pos.y = pos[1]
:type label_text: `str`
:param label_position: 3D vector position to draw the label at.
:type label_position: class:`vpython.vector`
:param scene: The scene in which to draw the object
:type scene: class:`vpython.canvas`
:return: The created label object.
:rtype: class:`vpython.label`
"""
# Custom settings for the label
label_height = 10
label_xoffset = 0
label_yoffset = 50
label_space = 20
label_font = 'serif'
label_text_colour = color.black
label_line_color = color.black
the_label = label(
canvas=scene,
pos=label_position,
text=label_text,
height=label_height,
xoffset=label_xoffset,
yoffset=label_yoffset,
space=label_space,
font=label_font,
color=label_text_colour,
linecolor=label_line_color
)
return the_label
def addCSGvoxel(self, box, colour, opacity=1.):
"""
16/03/10: To visualise CSG objects
"""
if colour == None:
colour = visual.color.red
org = box.origin
ext = box.extent
size = np.abs(ext - org)
pos = org + 0.5*size
visual.box(pos=pos, size=size, color=colour, opacity=opacity)
:rtype: class:`vpython.label`
"""
# Distance of camera from focus point to determine text size
distance_from_center = mag(scene.center - scene.camera.pos)
# Far away = smaller text, closer = larger text (up to a min (20) and max (40))
# Typically 5->20 units away
# (eqn and limits modified to suit display better) = -1.3333 * distance_from_center + 46.6667
label_height = -1.3333 * distance_from_center + 36.6667 # Calculate label height
label_height = max(min(label_height, 35), 10) # Limit to 10->35
label_xoffset = 0
label_yoffset = 0
label_space = 0
label_font = 'serif'
label_text_colour = color.black
label_line_color = color.white
label_bg_opacity = 0
label_linewidth = 0.1
the_label = label(
canvas=scene,
pos=label_position,
text=label_text,
height=label_height,
xoffset=label_xoffset,
yoffset=label_yoffset,
space=label_space,
font=label_font,
color=label_text_colour,
linecolor=label_line_color,
opacity=label_bg_opacity,
def __init__(self, background=(0,0,0), ambient=1.):
super(Visualiser, self).__init__()
if not Visualiser.VISUALISER_ON:
return
#self.display = visual.display(title='pvtrace', x=0, y=0, width=800, height=600, background=(0.957, 0.957, 1), ambient=0.5)
#self.display.exit = False
visual.curve(pos=[visual.vec(0,0,0), visual.vec(.2,0,0)], radius=0.001, color=visual.color.red)
visual.curve(pos=[visual.vec(0,0,0), visual.vec(0,.2,0)], radius=0.001, color=visual.color.green)
visual.curve(pos=[visual.vec(0,0,0), visual.vec(0,0,.2)], radius=0.001, color=visual.color.blue)
visual.label(pos=visual.vec(.22, 0, 0), text='X', linecolor=visual.color.red)
visual.label(pos=visual.vec(0, .22, 0), text='Y', linecolor=visual.color.green)
visual.label(pos=visual.vec(0, 0, .22), text='Z', linecolor=visual.color.blue)