def warpImage(src, theta, phi, gamma, scale, fovy):
halfFovy = fovy * 0.5
d = math.hypot(src.shape[1], src.shape[0])
sideLength = scale * d / math.cos(deg2Rad(halfFovy))
sideLength = np.int32(sideLength)
M = warpMatrix(src.shape[1], src.shape[0], theta, phi, gamma, scale, fovy)
dst = cv2.warpPerspective(src, M, (sideLength, sideLength))
mid_x = mid_y = dst.shape[0] // 2
target_x = target_y = src.shape[0] // 2
offset = (target_x % 2)
if len(dst.shape) == 3:
dst = dst[mid_y - target_y:mid_y + target_y + offset,
mid_x - target_x:mid_x + target_x + offset,
:]
else:
dst = dst[mid_y - target_y:mid_y + target_y + offset,
mid_x - target_x:mid_x + target_x + offset]
return dst
python类hypot()的实例源码
cpm_utils.py 文件源码
项目:convolutional-pose-machines-tensorflow
作者: timctho
项目源码
文件源码
阅读 36
收藏 0
点赞 0
评论 0
def gen_adj_mat(longs, lats, prob_edge=.2,
additional_length=lambda: np.random.exponential(20,1)):
'''Get an adjacency matrix for the cities whose longitudes and latitudes
are passed. Each entry will either be a number somewhat greater than the
crow-flies distance between the two cities (with probability prob_edge),
or math.inf. The matrix will consist of floats, and be symmetric. The
diagonal will be all zeroes. The "somewhat greater" is controlled by the
additional_length parameter, a function returning a random amount.'''
# Generate full nxn Bernoulli's, even though we'll only use the upper
# triangle.
edges = np.random.binomial(1, prob_edge, size=(len(longs),len(longs)))
am = np.zeros((len(longs),len(longs)))
for i in range(len(longs)):
for j in range(len(longs)):
if i==j:
am[i,i] = 0
elif i < j:
if edges[i,j] == 1:
am[i,j] = (math.hypot(longs[i]-longs[j],lats[i]-lats[j])
+ additional_length())
am[j,i] = am[i,j]
else:
am[i,j] = am[j,i] = math.inf
return np.around(am,1)
def get_camera_params(box, size, view_angle):
box_size = box.size()
size_diagonal = math.hypot(*size)
if view_angle is None:
focal_length = size_diagonal # Normal lens by default
else:
focal_length = size_diagonal / (2 * math.tan(math.radians(view_angle) / 2))
distance = focal_length * max(_zero_if_inf(box_size.x) / size[0],
_zero_if_inf(box_size.z) / size[1])
if distance == 0:
distance = 1
distance *= 1.2 # 20% margin around the object
origin = box.midpoint() - util.Vector(0, distance + _zero_if_inf(box_size.y) / 2, 0)
direction = util.Vector(0, 1, 0)
up = util.Vector(0, 0, 1)
return (origin, direction, up, focal_length)
def get_camera_params(box, size, view_angle):
box_size = box.size()
size_diagonal = math.hypot(*size)
if view_angle is None:
focal_length = size_diagonal # Normal lens by default
else:
focal_length = size_diagonal / (2 * math.tan(math.radians(view_angle) / 2))
distance = focal_length * max(_zero_if_inf(box_size.x) / size[0],
_zero_if_inf(box_size.z) / size[1])
if distance == 0:
distance = 1
distance *= 1.2 # 20% margin around the object
origin = box.midpoint() - util.Vector(0, distance + _zero_if_inf(box_size.y) / 2, 0)
direction = util.Vector(0, 1, 0)
up = util.Vector(0, 0, 1)
return (origin, direction, up, focal_length)
def drawBallVelocity(self, painter):
ballPos = self.ballOdom.pose.pose.position
ballVel = self.ballOdom.twist.twist.linear
if math.hypot(ballVel.x, ballVel.y) < 1.0:
return
angleOfSpeed = math.atan2(ballVel.y, ballVel.x)
paintDist = 10.0
velPosX = paintDist * math.cos(angleOfSpeed) + ballPos.x
velPosY = paintDist * math.sin(angleOfSpeed) + ballPos.y
ballPosPoint = self.convertToDrawWorld(ballPos.x, ballPos.y)
velPosPoint = self.convertToDrawWorld(velPosX, velPosY)
painter.setPen(QPen(QColor(102,0,255),2))
painter.drawLine(ballPosPoint, velPosPoint)
def setup_xy(self, p1_world, p2_world, p1_ucs, p2_ucs):
""" setup an UCS given by the points p1 and p2
only xy-plane, z' = z
"""
ucs_angle_to_x_axis = get_angle(p1_ucs, p2_ucs)
world_angle_to_x_axis = get_angle(p1_world, p2_world)
rotation = normalize_angle(world_angle_to_x_axis - ucs_angle_to_x_axis)
self._xaxis = (math.cos(rotation), math.sin(rotation), 0.)
self._yaxis = (math.cos(rotation+HALF_PI), math.sin(rotation+HALF_PI), 0.)
self._zaxis = (0., 0., 1.)
ucs_angle_to_x_axis = get_angle((0., 0.), p1_ucs)
world_angle_to_x_axis = rotation + ucs_angle_to_x_axis
distance_from_ucs_origin = math.hypot(p1_ucs[0], p1_ucs[1])
delta_x = distance_from_ucs_origin * math.cos(world_angle_to_x_axis)
delta_y = distance_from_ucs_origin * math.sin(world_angle_to_x_axis)
self._origin = (p1_world[0] - delta_x, p1_world[1] - delta_y, 0.)
def manipulate_frame(self, frame_image, faces, index):
# Instantiates a client
googly_eye = Image.open(self.__class__.get_os_path('overlays/eye.png'))
for face in faces:
for side in ('left', 'right'):
((lcx, lcy), (ex, ey), (rcx, rcy)) = face.get_eye_coords(side)
ew = int(1.5 * math.hypot(rcx - lcx, rcy - lcy))
pasted = googly_eye.rotate(random.randint(0, 360), Image.BICUBIC).resize((ew, ew), Image.BICUBIC)
frame_image.paste(pasted, (int(ex - ew/2), int(ey - ew/2)), pasted)
return frame_image
def cry_frame(self, frame_image, faces, index):
# Instantiates a client
tear = Image.open(self.__class__.get_os_path('overlays/tearblood.png'))
lowest = 0
for face in faces:
for side in ('left', 'right'):
((lcx, lcy), (ex, ey), (rcx, rcy)) = face.get_eye_coords(side)
ew = int(1.25 * math.hypot(rcx - lcx, rcy - lcy))
pasted = tear.resize((ew, ew), Image.BICUBIC)
left_y = int(lcy + (index * ew * 1.5) + (ew * .75))
right_y = int(rcy + (index * ew * 1.5) + (ew * .5) )
frame_image.paste(pasted, (int(lcx - ew/2), left_y), pasted)
frame_image.paste(pasted, (int(rcx - ew/2), right_y), pasted)
lowest = max(left_y, right_y)
return lowest
def cry_frame(self, frame_image, faces, index):
# Instantiates a client
tear = Image.open(self.__class__.get_os_path('overlays/tear.png'))
lowest = 0
for face in faces:
for side in ('left', 'right'):
((lcx, lcy), (ex, ey), (rcx, rcy)) = face.get_eye_coords(side)
ew = int(1.25 * math.hypot(rcx - lcx, rcy - lcy))
pasted = tear.resize((ew, ew), Image.BICUBIC).rotate(face.angles.tilt, Image.BICUBIC)
left_y = int(lcy + (index * ew * 1.5) + (ew * .5))
right_y = int(rcy + (index * ew * 1.5) + (ew * .75) )
frame_image.paste(pasted, (int(lcx - ew/2), left_y), pasted)
frame_image.paste(pasted, (int(rcx - ew/2), right_y), pasted)
lowest = max(left_y, right_y)
return lowest
def __init__(self, dot_widget, target_x, target_y):
MoveToAnimation.__init__(self, dot_widget, target_x, target_y)
self.source_zoom = dot_widget.zoom_ratio
self.target_zoom = self.source_zoom
self.extra_zoom = 0
middle_zoom = 0.5 * (self.source_zoom + self.target_zoom)
distance = math.hypot(self.source_x - self.target_x,
self.source_y - self.target_y)
rect = self.dot_widget.get_allocation()
visible = min(rect.width, rect.height) / self.dot_widget.zoom_ratio
visible *= 0.9
if distance > 0:
desired_middle_zoom = visible / distance
self.extra_zoom = min(0, 4 * (desired_middle_zoom - middle_zoom))
def arcMidPoint(self, prev_vertex, vertex, angle):
if len(prev_vertex) == 3:
[x1, y1, z1] = prev_vertex
else:
[x1, y1] = prev_vertex
if len(vertex) == 3:
[x2, y2, z2] = vertex
else:
[x2, y2] = vertex
angle = radians(angle / 2)
basic_angle = atan2(y2 - y1, x2 - x1) - pi / 2
shift = (1 - cos(angle)) * hypot(y2 - y1, x2 - x1) / 2 / sin(angle)
midpoint = [(x2 + x1) / 2 + shift * cos(basic_angle), (y2 + y1) / 2 + shift * sin(basic_angle)]
return midpoint
def __init__(self, dot_widget, target_x, target_y):
MoveToAnimation.__init__(self, dot_widget, target_x, target_y)
self.source_zoom = dot_widget.zoom_ratio
self.target_zoom = self.source_zoom
self.extra_zoom = 0
middle_zoom = 0.5 * (self.source_zoom + self.target_zoom)
distance = math.hypot(self.source_x - self.target_x,
self.source_y - self.target_y)
rect = self.dot_widget.get_allocation()
visible = min(rect.width, rect.height) / self.dot_widget.zoom_ratio
visible *= 0.9
if distance > 0:
desired_middle_zoom = visible / distance
self.extra_zoom = min(0, 4 * (desired_middle_zoom - middle_zoom))
def dft(self, data, typecode='h'):
if type(data) is str:
a = array.array(typecode, data)
for index, value in enumerate(a):
self.real_input[index] = float(value)
elif type(data) is array.array:
for index, value in enumerate(data):
self.real_input[index] = float(value)
self.fftwf_execute(self.fftwf_plan)
for i in range(len(self.amplitude)):
self.amplitude[i] = math.hypot(self.complex_output[i * 2], self.complex_output[i * 2 + 1])
# self.phase[i] = math.atan2(self.complex_output[i * 2 + 1], self.complex_output[i * 2])
return self.amplitude # , self.phase
def start(self, route=[], v=10):
r = route
self.TopSpeed = v
self.Route = r
# Calculating position and velocity vector according to route points
if len(route) > 1:
self.CurrentRoute += 1
self.Position = Vector3(r[0]['X'], r[0]['Y'], self.Position.Z)
ddash = math.hypot(self.Position.X-r[1]['X'], self.Position.Y-r[1]['Y'])
self.Velocity = Vector3((v/ddash) * (r[1]['X']-self.Position.X), (v/ddash) * (r[1]['Y']-self.Position.Y), 0)
else:
self.Position = Vector3(0, 0, self.Position.Z)
self.Velocity = Vector3(0, 0, 0)
def animate(self):
self.angle += (math.pi / 30)
xs = 200 * math.sin(self.angle) - 40 + 25
ys = 200 * math.cos(self.angle) - 40 + 25
self.m_lightSource.setPos(xs, ys)
for item in self.m_items:
effect = item.graphicsEffect()
delta = QPointF(item.x() - xs, item.y() - ys)
effect.setOffset(QPointF(delta.toPoint() / 30))
dd = math.hypot(delta.x(), delta.y())
color = effect.color()
color.setAlphaF(max(0.4, min(1 - dd / 200.0, 0.7)))
effect.setColor(color)
self.m_scene.update()
def __init__(self):
super(StickMan, self).__init__()
self.m_sticks = True
self.m_isDead = False
self.m_pixmap = QPixmap('images/head.png')
self.m_penColor = QColor(Qt.white)
self.m_fillColor = QColor(Qt.black)
# Set up start position of limbs.
self.m_nodes = []
for x, y in Coords:
node = Node(QPointF(x, y), self)
node.positionChanged.connect(self.childPositionChanged)
self.m_nodes.append(node)
self.m_perfectBoneLengths = []
for n1, n2 in Bones:
node1 = self.m_nodes[n1]
node2 = self.m_nodes[n2]
dist = node1.pos() - node2.pos()
self.m_perfectBoneLengths.append(math.hypot(dist.x(), dist.y()))
self.startTimer(10)
def stabilize(self):
threshold = 0.001
for i, (n1, n2) in enumerate(Bones):
node1 = self.m_nodes[n1]
node2 = self.m_nodes[n2]
pos1 = node1.pos()
pos2 = node2.pos()
dist = pos1 - pos2
length = math.hypot(dist.x(), dist.y())
diff = (length - self.m_perfectBoneLengths[i]) / length
p = dist * (0.5 * diff)
if p.x() > threshold and p.y() > threshold:
pos1 -= p
pos2 += p
node1.setPos(pos1)
node2.setPos(pos2)
6 code.py 文件源码
项目:computational_physics_N2014301020117
作者: yukangnineteen
项目源码
文件源码
阅读 21
收藏 0
点赞 0
评论 0
def __init__(self, initial_velocity, firing_angle, time_step, velocity_of_wind, angle_of_wind):
self.x = [0]
self.y = [0]
self.theta = firing_angle
self.alpha = angle_of_wind
self.v_x = [initial_velocity * math.cos(self.theta / 180 * math.pi)]
self.v_y = [initial_velocity * math.sin(self.theta / 180 * math.pi)]
self.v0 = initial_velocity
self.v = initial_velocity
self.v_w = velocity_of_wind
self.v_w_x = self.v_w * math.cos(self.alpha / 180 * math.pi)
self.v_w_y = self.v_w * math.sin(self.alpha / 180 * math.pi)
self.v_rel_x = self.v_x[0] - self.v_w_x
self.v_rel_y = self.v_y[0] - self.v_w_y
self.v_rel_module = math.hypot(self.v_rel_x, self.v_rel_y)
self.C = 0
self.B_2 = 0
self.g = 9.8
self.dt = time_step
6 code.py 文件源码
项目:computational_physics_N2014301020117
作者: yukangnineteen
项目源码
文件源码
阅读 24
收藏 0
点赞 0
评论 0
def calculate(self):
i = 0
while(True):
self.B_2 = 0.0039 + 0.0058 / (1 + math.exp((self.v - 35) / 5))
self.C = math.pow(1 - 0.0065 * self.y[i] / 273, 2.5)
self.x.append(self.x[i] + self.v_x[i] * self.dt)
self.y.append(self.y[i] + self.v_y[i] * self.dt)
self.v_x.append(self.v_x[i] - self.C * self.B_2 * self.v_rel_module * self.v_rel_x * self.dt)
self.v_y.append(self.v_y[i] - self.g * self.dt - self.C * self.B_2 * self.v_rel_module * self.v_rel_y * self.dt)
self.v = math.hypot(self.v_x[i + 1], self.v_y[i + 1])
self.v_rel_x = self.v_x[i + 1] - self.v_w_x
self.v_rel_y = self.v_y[i + 1] - self.v_w_y
self.v_rel_module = math.hypot(self.v_rel_x, self.v_rel_y)
i += 1
if self.y[i] <= -100:
break
self.x[-1]=((-100 - self.y[-1])*(self.x[-1] - self.x[-2]) / (self.y[-1] - self.y[-2])) + self.x[-1]
self.y[i] = -100
global a
a = self.x[-1]
5(improved) code.py 文件源码
项目:computational_physics_N2014301020117
作者: yukangnineteen
项目源码
文件源码
阅读 23
收藏 0
点赞 0
评论 0
def calculate(self):
i = 0
while(True):
self.C = 4E-2 * math.pow(1 - 6.5 * self.y[i] / 300, 2.5)
self.g.append(self.G * self.M_E / (self.R_E + self.y[i]) ** 2)
self.x.append(self.x[i] + self.v_x[i] * self.dt)
self.y.append(self.y[i] + self.v_y[i] * self.dt)
self.v_x.append(self.v_x[i] - self.C * math.hypot(self.v_x[i], self.v_y[i]) * self.v_x[i] * self.dt)
self.v_y.append(self.v_y[i] - self.g[i-1] * self.dt - self.C * math.hypot(self.v_x[i], self.v_y[i]) * self.v_y[i] * self.dt)
i += 1
if self.y[i] < 0:
break
#For the falling point
self.x[i] = - self.y[i-1] * (self.x[i] - self.x[i-1]) / (self.y[i] - self.y[i-1]) + self.x[i-1]
self.y[i] = 0
#Maxmize the range and find the corresponding firing angle
def __init__(self, dot_widget, target_x, target_y):
MoveToAnimation.__init__(self, dot_widget, target_x, target_y)
self.source_zoom = dot_widget.zoom_ratio
self.target_zoom = self.source_zoom
self.extra_zoom = 0
middle_zoom = 0.5 * (self.source_zoom + self.target_zoom)
distance = math.hypot(self.source_x - self.target_x,
self.source_y - self.target_y)
rect = self.dot_widget.get_allocation()
visible = min(rect.width, rect.height) / self.dot_widget.zoom_ratio
visible *= 0.9
if distance > 0:
desired_middle_zoom = visible / distance
self.extra_zoom = min(0, 4 * (desired_middle_zoom - middle_zoom))
def __init__(self, dot_widget, target_x, target_y):
MoveToAnimation.__init__(self, dot_widget, target_x, target_y)
self.source_zoom = dot_widget.zoom_ratio
self.target_zoom = self.source_zoom
self.extra_zoom = 0
middle_zoom = 0.5 * (self.source_zoom + self.target_zoom)
distance = math.hypot(self.source_x - self.target_x,
self.source_y - self.target_y)
rect = self.dot_widget.get_allocation()
visible = min(rect.width, rect.height) / self.dot_widget.zoom_ratio
visible *= 0.9
if distance > 0:
desired_middle_zoom = visible / distance
self.extra_zoom = min(0, 4 * (desired_middle_zoom - middle_zoom))
def animate(self):
self.angle += (math.pi / 30)
xs = 200 * math.sin(self.angle) - 40 + 25
ys = 200 * math.cos(self.angle) - 40 + 25
self.m_lightSource.setPos(xs, ys)
for item in self.m_items:
effect = item.graphicsEffect()
delta = QPointF(item.x() - xs, item.y() - ys)
effect.setOffset(QPointF(delta.toPoint() / 30))
dd = math.hypot(delta.x(), delta.y())
color = effect.color()
color.setAlphaF(max(0.4, min(1 - dd / 200.0, 0.7)))
effect.setColor(color)
self.m_scene.update()
def __init__(self):
super(StickMan, self).__init__()
self.m_sticks = True
self.m_isDead = False
self.m_pixmap = QPixmap('images/head.png')
self.m_penColor = QColor(Qt.white)
self.m_fillColor = QColor(Qt.black)
# Set up start position of limbs.
self.m_nodes = []
for x, y in Coords:
node = Node(QPointF(x, y), self)
node.positionChanged.connect(self.childPositionChanged)
self.m_nodes.append(node)
self.m_perfectBoneLengths = []
for n1, n2 in Bones:
node1 = self.m_nodes[n1]
node2 = self.m_nodes[n2]
dist = node1.pos() - node2.pos()
self.m_perfectBoneLengths.append(math.hypot(dist.x(), dist.y()))
self.startTimer(10)
def stabilize(self):
threshold = 0.001
for i, (n1, n2) in enumerate(Bones):
node1 = self.m_nodes[n1]
node2 = self.m_nodes[n2]
pos1 = node1.pos()
pos2 = node2.pos()
dist = pos1 - pos2
length = math.hypot(dist.x(), dist.y())
diff = (length - self.m_perfectBoneLengths[i]) / length
p = dist * (0.5 * diff)
if p.x() > threshold and p.y() > threshold:
pos1 -= p
pos2 += p
node1.setPos(pos1)
node2.setPos(pos2)
def _log(z):
abs_x = abs(z.real)
abs_y = abs(z.imag)
if abs_x > _LARGE_INT or abs_y > _LARGE_INT:
return complex(math.log(math.hypot(abs_x/2, abs_y/2)) + _LOG_2,
math.atan2(z.imag, z.real))
if abs_x < _DBL_MIN and abs_y < _DBL_MIN:
if abs_x > 0 or abs_y > 0:
return complex(math.log(math.hypot(math.ldexp(abs_x, _DBL_MANT_DIG),
math.ldexp(abs_y, _DBL_MANT_DIG)))
- _DBL_MANT_DIG * _LOG_2,
math.atan2(z.imag, z.real))
raise ValueError
rad, phi = polar(z)
return complex(math.log(rad), phi)
def __init__(self,x1,y1,x2,y2,x3,y3,sector,endpoint):
self.x_m=x1; self.x=x2; self.x_p=x3; self.y_m=y1; self.y=y2; self.y_p=y3;
self.length_m = math.hypot(self.x_m-self.x, self.y_m-self.y)
self.length_p = math.hypot(self.x_p-self.x, self.y_p-self.y)
self.length_secant = math.hypot(self.x_p-self.x_m, self.y_p-self.y_m)
self.length = (self.length_m+self.length_p)/2
self.sector = sector;
if endpoint:
self.curvature = 0
else:
p = (self.length_m+self.length_p+self.length_secant)/2
#print(p, self.length_m, self.length_p, self.length_secant)
try:
area = math.sqrt(p*(p-self.length_m)*(p-self.length_p)*(p-self.length_secant))
except ValueError:
area=0
self.curvature = 4*area/(self.length_m*self.length_p*self.length_secant)
def __init__(self, dot_widget, target_x, target_y):
MoveToAnimation.__init__(self, dot_widget, target_x, target_y)
self.source_zoom = dot_widget.zoom_ratio
self.target_zoom = self.source_zoom
self.extra_zoom = 0
middle_zoom = 0.5 * (self.source_zoom + self.target_zoom)
distance = math.hypot(self.source_x - self.target_x,
self.source_y - self.target_y)
rect = self.dot_widget.get_allocation()
visible = min(rect.width, rect.height) / self.dot_widget.zoom_ratio
visible *= 0.9
if distance > 0:
desired_middle_zoom = visible / distance
self.extra_zoom = min(0, 4 * (desired_middle_zoom - middle_zoom))
def CursorClosestTo(verts, allowedError = 0.025):
ratioX, ratioY = ImageRatio()
#any length that is certantly not smaller than distance of the closest
min = 1000
minV = verts[0]
for v in verts:
if v is None: continue
for area in bpy.context.screen.areas:
if area.type == 'IMAGE_EDITOR':
loc = area.spaces[0].cursor_location
hyp = hypot(loc.x/ratioX -v.uv.x, loc.y/ratioY -v.uv.y)
if (hyp < min):
min = hyp
minV = v
if min is not 1000: return minV
return None
def __init__(self, dot_widget, target_x, target_y):
MoveToAnimation.__init__(self, dot_widget, target_x, target_y)
self.source_zoom = dot_widget.zoom_ratio
self.target_zoom = self.source_zoom
self.extra_zoom = 0
middle_zoom = 0.5 * (self.source_zoom + self.target_zoom)
distance = math.hypot(self.source_x - self.target_x,
self.source_y - self.target_y)
rect = self.dot_widget.get_allocation()
visible = min(rect.width, rect.height) / self.dot_widget.zoom_ratio
visible *= 0.9
if distance > 0:
desired_middle_zoom = visible / distance
self.extra_zoom = min(0, 4 * (desired_middle_zoom - middle_zoom))