def execute(self,obj):
import Part,FreeCAD
dist = obj.distribution.lower()
if dist not in ["polar","cartesian"]:
obj.distribution="polar"
print "Ray Distribution not understood, changing it to polar"
if dist == "polar":
r=5*tan(radians(obj.angle))
d=[]
for x in linspace(-obj.xSize/2,obj.xSize/2,obj.Nx):
for y in linspace(-obj.ySize/2,obj.ySize/2,obj.Ny):
d.append(Part.makeCone(0,r,5,FreeCAD.Vector(x,y,0)))
else: #Todo: Cambiar cono a piramide
r=5*tan(radians(obj.angle))
d = []
for x in linspace(-obj.xSize/2,obj.xSize/2,obj.Nx):
for y in linspace(-obj.ySize/2,obj.ySize/2,obj.Ny):
d.append(Part.makeCone(0,r,5,FreeCAD.Vector(x,y,0)))
obj.Shape = Part.makeCompound(d)
python类tan()的实例源码
def sample_transformation(self, imsz):
theta = math.radians(self.rotation[1]*n.random.randn() + self.rotation[0])
ca = math.cos(theta)
sa = math.sin(theta)
R = n.zeros((3,3))
R[0,0] = ca
R[0,1] = -sa
R[1,0] = sa
R[1,1] = ca
R[2,2] = 1
S = n.eye(3,3)
S[0,1] = math.tan(math.radians(self.skew[1]*n.random.randn() + self.skew[0]))
A = matrix_mult(R,S)
x = imsz[1]/2
y = imsz[0]/2
return (A[0,0], A[0,1], -x*A[0,0] - y*A[0,1] + x,
A[1,0], A[1,1], -x*A[1,0] - y*A[1,1] + y)
def calculate_camera_variables(eye, lookat, up, fov, aspect_ratio, fov_is_vertical=False):
import numpy as np
import math
W = np.array(lookat) - np.array(eye)
wlen = np.linalg.norm(W)
U = np.cross(W, np.array(up))
U /= np.linalg.norm(U)
V = np.cross(U, W)
V /= np.linalg.norm(V)
if fov_is_vertical:
vlen = wlen * math.tan(0.5 * fov * math.pi / 180.0)
V *= vlen
ulen = vlen * aspect_ratio
U *= ulen
else:
ulen = wlen * math.tan(0.5 * fov * math.pi / 180.0)
U *= ulen
vlen = ulen * aspect_ratio
V *= vlen
return U, V, W
def nh_steer(self, q_nearest, q_rand, epsilon):
"""
For a car like robot, where it takes two control input (u_speed, u_phi)
All possible combinations of control inputs are generated and used to find the closest q_new to q_rand
:param q_nearest:
:param q_rand:
:param epsilon:
:return:
"""
L = 20.0 # Length between midpoints of front and rear axle of the car like robot
u_speed, u_phi = [-1.0, 1.0], [-math.pi/4, 0, math.pi/4]
controls = list(itertools.product(u_speed, u_phi))
# euler = lambda t_i, q, u_s, u_p, L: (u_s*math.cos(q[2]), u_s*math.sin(q[2]), u_s/L*math.tan(u_p))
result = []
ctrls_path = {c: [] for c in controls}
for ctrl in controls:
q_new = q_nearest
for t_i in range(epsilon): # h is assumed to be 1 here for euler integration
q_new = tuple(map(add, q_new, self.euler(t_i, q_new, ctrl[0], ctrl[1], L)))
ctrls_path[ctrl].append(q_new)
result.append((ctrl[0], ctrl[1], q_new))
q_news = [x[2] for x in result]
_, _, idx = self.nearest_neighbour(q_rand, np.array(q_news))
return result[idx], ctrls_path
def process(self, im):
# if side is right flip so it becomes right
if self.side != 'left':
im = np.fliplr(im)
# slope of the perspective
slope = tan(radians(self.degrees))
(h, w, _) = im.shape
matrix_trans = np.array([[1, 0, 0],
[-slope/2, 1, slope * h / 2],
[-slope/w, 0, 1 + slope]])
trans = ProjectiveTransform(matrix_trans)
img_trans = warp(im, trans)
if self.side != 'left':
img_trans = np.fliplr(img_trans)
return img_trans
def angled_path(size, angle=0):
"""
Generates a set of lines across an image, with the given angle.
:param size: The size of the image
:param angle: The angle of the lines.
:return: A set of generators, each generator represents a line and yields a set of (x,y) coordinates.
All the lines go left to right.
"""
# y coordinates are inverted in images, so this allows for users to input more intuitive angle values.
angle = -angle
if angle % 180 == 0:
yield from horizontal_path(size)
return
if angle % 180 == 90:
yield from vertical_path(size)
return
width, height = size
slope = tan(radians(angle))
start_y = 0 if slope > 0 else height - 1
for x in range(width-1, 0, -1):
yield draw_line((x, start_y), size, slope)
for y in range(height):
yield draw_line((0, y), size, slope)
def calc_helix_points(turtle, rad, pitch):
""" calculates required points to produce helix bezier curve with given radius and pitch in direction of turtle"""
# alpha = radians(90)
# pit = pitch/(2*pi)
# a_x = rad*cos(alpha)
# a_y = rad*sin(alpha)
# a = pit*alpha*(rad - a_x)*(3*rad - a_x)/(a_y*(4*rad - a_x)*tan(alpha))
# b_0 = Vector([a_x, -a_y, -alpha*pit])
# b_1 = Vector([(4*rad - a_x)/3, -(rad - a_x)*(3*rad - a_x)/(3*a_y), -a])
# b_2 = Vector([(4*rad - a_x)/3, (rad - a_x)*(3*rad - a_x)/(3*a_y), a])
# b_3 = Vector([a_x, a_y, alpha*pit])
# axis = Vector([0, 0, 1])
# simplifies greatly for case inc_angle = 90
points = [Vector([0, -rad, -pitch / 4]),
Vector([(4 * rad) / 3, -rad, 0]),
Vector([(4 * rad) / 3, rad, 0]),
Vector([0, rad, pitch / 4])]
# align helix points to turtle direction and randomize rotation around axis
trf = turtle.dir.to_track_quat('Z', 'Y')
spin_ang = rand_in_range(0, 2 * pi)
for p in points:
p.rotate(Quaternion(Vector([0, 0, 1]), spin_ang))
p.rotate(trf)
return points[1] - points[0], points[2] - points[0], points[3] - points[0], turtle.dir.copy()
def update_projection_matrix(self):
u = self.z_near*tan(radians(self.fov))
r = u*self.width()/self.height()
self.projection_matrix = create_frustum_matrix(-r,r,-u,u,self.z_near,self.z_far)
self.projection_matrix_need_update = False
def shear_matrix(angle, direction, point, normal):
"""Return matrix to shear by angle along direction vector on shear plane.
The shear plane is defined by a point and normal vector. The direction
vector must be orthogonal to the plane's normal vector.
A point P is transformed by the shear matrix into P" such that
the vector P-P" is parallel to the direction vector and its extent is
given by the angle of P-P'-P", where P' is the orthogonal projection
of P onto the shear plane.
>>> angle = (random.random() - 0.5) * 4*math.pi
>>> direct = numpy.random.random(3) - 0.5
>>> point = numpy.random.random(3) - 0.5
>>> normal = numpy.cross(direct, numpy.random.random(3))
>>> S = shear_matrix(angle, direct, point, normal)
>>> numpy.allclose(1.0, numpy.linalg.det(S))
True
"""
normal = unit_vector(normal[:3])
direction = unit_vector(direction[:3])
if abs(numpy.dot(normal, direction)) > 1e-6:
raise ValueError("direction and normal vectors are not orthogonal")
angle = math.tan(angle)
M = numpy.identity(4)
M[:3, :3] += angle * numpy.outer(direction, normal)
M[:3, 3] = -angle * numpy.dot(point[:3], normal) * direction
return M
def skew(self, x=0, y=0):
"""Return a new transformation, skewed by x and y.
Example:
>>> import math
>>> t = Transform()
>>> t.skew(math.pi / 4)
<Transform [1 0 1 1 0 0]>
>>>
"""
import math
return self.transform((1, math.tan(y), math.tan(x), 1, 0, 0))
def findDistance(TP):
#this function finds the distance from the reflector FIX
FPw = img.shape[1]
FMw = TMw * FPw / TP[1]
#print FMw, TMw, FPw, TP[1]
distw = (FMw / 2) * math.tan(math.radians(TAN_ANGLE_HORI))
FPh = img.shape[0]
FMh = TMh * FPh / TP[0]
#print FMh, TMh, FPh, TP#[0]
disth = (FMh / 2) * math.tan(math.radians(TAN_ANGLE_VERT))
dist = (distw + disth)
#dist = disth * 8
return dist
def findDistance(TP):
"""
this function finds the distance from the reflector FIX
"""
FPw = img.shape[1]
FMw = TMw * FPw / TP[1]
distw = (FMw / 2) * math.tan(math.radians(TAN_ANGLE_HORI))
FPh = img.shape[0]
FMh = TMh * FPh / TP[0]
disth = (FMh / 2) * math.tan(math.radians(TAN_ANGLE_VERT))
dist = (distw + disth)
return dist
transformations.py 文件源码
项目:Neural-Networks-for-Inverse-Kinematics
作者: paramrajpura
项目源码
文件源码
阅读 27
收藏 0
点赞 0
评论 0
def shear_matrix(angle, direction, point, normal):
"""Return matrix to shear by angle along direction vector on shear plane.
The shear plane is defined by a point and normal vector. The direction
vector must be orthogonal to the plane's normal vector.
A point P is transformed by the shear matrix into P" such that
the vector P-P" is parallel to the direction vector and its extent is
given by the angle of P-P'-P", where P' is the orthogonal projection
of P onto the shear plane.
>>> angle = (random.random() - 0.5) * 4*math.pi
>>> direct = numpy.random.random(3) - 0.5
>>> point = numpy.random.random(3) - 0.5
>>> normal = numpy.cross(direct, numpy.random.random(3))
>>> S = shear_matrix(angle, direct, point, normal)
>>> numpy.allclose(1, numpy.linalg.det(S))
True
"""
normal = unit_vector(normal[:3])
direction = unit_vector(direction[:3])
if abs(numpy.dot(normal, direction)) > 1e-6:
raise ValueError("direction and normal vectors are not orthogonal")
angle = math.tan(angle)
M = numpy.identity(4)
M[:3, :3] += angle * numpy.outer(direction, normal)
M[:3, 3] = -angle * numpy.dot(point[:3], normal) * direction
return M
def st_to_uv(cls, s):
if cls.PROJECTION == cls.LINEAR_PROJECTION:
return 2 * s - 1
elif cls.PROJECTION == cls.TAN_PROJECTION:
s = math.tan((math.pi / 2.0) * s - math.pi / 4.0)
return s + (1.0 / (1 << 53)) * s
elif cls.PROJECTION == cls.QUADRATIC_PROJECTION:
if s >= 0.5:
return (1.0 / 3.0) * (4 * s * s - 1)
else:
return (1.0 / 3.0) * (1 - 4 * (1 - s) * (1 - s))
else:
raise ValueError('unknown projection type')
def area(a, b, c):
"""Area of the triangle (a, b, c).
see :cpp:func:`S2::Area`
"""
assert is_unit_length(a)
assert is_unit_length(b)
assert is_unit_length(c)
sa = b.angle(c)
sb = c.angle(a)
sc = a.angle(b)
s = 0.5 * (sa + sb + sc)
if s >= 3e-4:
s2 = s * s
dmin = s - max(sa, max(sb, sc))
if dmin < 1e-2 * s * s2 * s2:
area = girard_area(a, b, c)
if dmin < 2 * (0.1 * area):
return area
return 4 * math.atan(math.sqrt(
max(0.0,
math.tan(0.5 * s) *
math.tan(0.5 * (s - sa)) *
math.tan(0.5 * (s - sb)) *
math.tan(0.5 * (s - sc)))
))
def __calc(self):
"""
Perform the actual calculations for sunrise, sunset and
a number of related quantities.
The results are stored in the instance variables
sunrise_t, sunset_t and solarnoon_t
"""
timezone = self.timezone # in hours, east is positive
longitude= self.long # in decimal degrees, east is positive
latitude = self.lat # in decimal degrees, north is positive
time = self.time # percentage past midnight, i.e. noon is 0.5
day = self.day # daynumber 1=1/1/1900
Jday =day+2415018.5+time-timezone/24 # Julian day
Jcent =(Jday-2451545)/36525 # Julian century
Manom = 357.52911+Jcent*(35999.05029-0.0001537*Jcent)
Mlong = 280.46646+Jcent*(36000.76983+Jcent*0.0003032)%360
Eccent = 0.016708634-Jcent*(0.000042037+0.0001537*Jcent)
Mobliq = 23+(26+((21.448-Jcent*(46.815+Jcent*(0.00059-Jcent*0.001813))))/60)/60
obliq = Mobliq+0.00256*cos(rad(125.04-1934.136*Jcent))
vary = tan(rad(obliq/2))*tan(rad(obliq/2))
Seqcent = sin(rad(Manom))*(1.914602-Jcent*(0.004817+0.000014*Jcent))+sin(rad(2*Manom))*(0.019993-0.000101*Jcent)+sin(rad(3*Manom))*0.000289
Struelong= Mlong+Seqcent
Sapplong = Struelong-0.00569-0.00478*sin(rad(125.04-1934.136*Jcent))
declination = deg(asin(sin(rad(obliq))*sin(rad(Sapplong))))
eqtime = 4*deg(vary*sin(2*rad(Mlong))-2*Eccent*sin(rad(Manom))+4*Eccent*vary*sin(rad(Manom))*cos(2*rad(Mlong))-0.5*vary*vary*sin(4*rad(Mlong))-1.25*Eccent*Eccent*sin(2*rad(Manom)))
hourangle= deg(acos(cos(rad(90.833))/(cos(rad(latitude))*cos(rad(declination)))-tan(rad(latitude))*tan(rad(declination))))
self.solarnoon_t=(720-4*longitude-eqtime+timezone*60)/1440
self.sunrise_t =self.solarnoon_t-hourangle*4/1440
self.sunset_t =self.solarnoon_t+hourangle*4/1440
def shear_matrix(angle, direction, point, normal):
"""Return matrix to shear by angle along direction vector on shear plane.
The shear plane is defined by a point and normal vector. The direction
vector must be orthogonal to the plane's normal vector.
A point P is transformed by the shear matrix into P" such that
the vector P-P" is parallel to the direction vector and its extent is
given by the angle of P-P'-P", where P' is the orthogonal projection
of P onto the shear plane.
>>> angle = (random.random() - 0.5) * 4*math.pi
>>> direct = numpy.random.random(3) - 0.5
>>> point = numpy.random.random(3) - 0.5
>>> normal = numpy.cross(direct, numpy.random.random(3))
>>> S = shear_matrix(angle, direct, point, normal)
>>> numpy.allclose(1.0, numpy.linalg.det(S))
True
"""
normal = unit_vector(normal[:3])
direction = unit_vector(direction[:3])
if abs(numpy.dot(normal, direction)) > 1e-6:
raise ValueError("direction and normal vectors are not orthogonal")
angle = math.tan(angle)
M = numpy.identity(4)
M[:3, :3] += angle * numpy.outer(direction, normal)
M[:3, 3] = -angle * numpy.dot(point[:3], normal) * direction
return M
def __init__(self, root, tip, span, sweep=None, sweepangle=45.0):
self.root = root #: Root Chord of fin
self.tip = tip #: Tip Chord of fin
self.span = span #: Span ("height") of fin
self._length = root
if sweep is not None:
self.sweep = sweep #: Sweep length of the fin
self.sweepangle = atan(self.sweep / self.span)
"""Angle of sweep of the fin [radians]"""
else:
self.sweep = span * tan(radians(sweepangle))
self.sweepangle = radians(sweepangle)
def target_distance(target):
camera_fov_vert = 41.41
camera_pitch = 40
# units in inches
target_height = 83
camera_height = 9
# target[0][1] is [1,-1] y coord from top-bottom
y = target[0][1]
return (target_height - camera_height) / math.tan(math.radians((y * camera_fov_vert / 2) + camera_pitch))
def destinationPointVincenty(lat, lon, brng, s):
a = 6378137.0
b = 6356752.3142
f = 1.0/298.257223563
alpha1 = math.radians(brng)
sinAlpha1 = math.sin(alpha1)
cosAlpha1 = math.cos(alpha1)
tanU1 = (1.0 - f) * math.tan(math.radians(lat))
cosU1 = 1.0 / math.sqrt(1.0 + tanU1*tanU1)
sinU1 = tanU1 * cosU1
sigma1 = math.atan2(tanU1, cosAlpha1)
sinAlpha = cosU1 * sinAlpha1
cosSqAlpha = 1.0 - sinAlpha*sinAlpha
uSq = cosSqAlpha * (a*a - b*b) / (b*b)
A = 1.0 + uSq / 16384.0 * (4096.0+uSq*(-768.0+uSq*(320.0-175.0*uSq)))
B = uSq / 1024.0 * (256.0+uSq*(-128.0+uSq*(74.0-47.0*uSq)))
sigma = s / (b*A)
sigmaP = 2.0 * math.pi
while math.fabs(sigma-sigmaP) > 1e-12:
cos2SigmaM = math.cos(2.0 * sigma1 + sigma)
sinSigma = math.sin(sigma)
cosSigma = math.cos(sigma)
deltaSigma = B * sinSigma * (cos2SigmaM+B/4.0*(cosSigma*(-1.0+2.0*cos2SigmaM*cos2SigmaM) - B/6.0*cos2SigmaM*(-3.0+4.0*sinSigma*sinSigma)*(-3.0+4.0*cos2SigmaM*cos2SigmaM)))
sigmaP = sigma
sigma = s / (b*A) + deltaSigma
tmp = sinU1 * sinSigma - cosU1*cosSigma*cosAlpha1
lat2 = math.atan2(sinU1*cosSigma + cosU1*sinSigma*cosAlpha1,
(1.0 - f)*math.sqrt(sinAlpha*sinAlpha + tmp*tmp))
lambdav = math.atan2(sinSigma*sinAlpha1, cosU1*cosSigma - sinU1*sinSigma*cosAlpha1)
C = f / 16.0 * cosSqAlpha*(4.0+f*(4.0-3.0*cosSqAlpha))
L = lambdav - (1.0-C) * f * sinAlpha * (sigma + C*sinSigma*(cos2SigmaM+C*cosSigma*(-1.0+2.0*cos2SigmaM*cos2SigmaM)))
return math.degrees(lat2), lon + math.degrees(L)
# bearing is in degrees and distances are in meters