def get_bone_rotation(self,bone):
pose_bone = self.armature.pose.bones[bone.name]
if bone.parent != None:
local_mat = self.get_bone_transformation(bone.parent).inverted() * self.get_bone_transformation(bone)
else:
local_mat = self.get_bone_transformation(bone)
bone_euler_rot = local_mat.decompose()[1].to_euler()
degrees = round(math.degrees(bone_euler_rot.y),2)
return -math.radians(degrees)
python类radians()的实例源码
def unit_cell_volume(self):
"""
Calculates unit cell volume of a given MOF object.
"""
a = self.uc_size[0]
b = self.uc_size[1]
c = self.uc_size[2]
alp = math.radians(self.uc_angle[0])
bet = math.radians(self.uc_angle[1])
gam = math.radians(self.uc_angle[2])
volume = 1 - math.cos(alp)**2 - math.cos(bet)**2 - math.cos(gam)**2
volume += 2 * math.cos(alp) * math.cos(bet) * math.cos(gam)
volume = a * b * c * math.sqrt(volume)
frac_volume = volume / (a * b * c)
self.ucv = volume
self.frac_ucv = frac_volume
def angle_wrap(angle,radians=False):
'''
Wraps the input angle to 360.0 degrees.
if radians is True: input is assumed to be in radians, output is also in
radians
'''
if radians:
wrapped = angle % (2.0*PI)
if wrapped < 0.0:
wrapped = 2.0*PI + wrapped
else:
wrapped = angle % 360.0
if wrapped < 0.0:
wrapped = 360.0 + wrapped
return wrapped
def get_min_level(self, value):
"""Minimum cell level for given value.
Return the minimum level such that the metric is at most the given
value, or ``s2sphere.CellId.MAX_LEVEL`` if there is no such level.
For example, ``s2sphere.MAX_DIAG.get_min_level(0.1)`` returns the
minimum level such that all cell diagonal lengths are 0.1 or smaller.
The return value is always a valid level.
:param value:
Depending on whether this is used in one or two dimensions, this is
an angle in radians or a solid angle in steradians.
"""
if value <= 0:
return CellId.MAX_LEVEL
m, x = math.frexp(value / self.deriv())
level = max(0, min(CellId.MAX_LEVEL, -((x - 1) >> (self.__dim - 1))))
assert level == CellId.MAX_LEVEL or self.get_value(level) <= value
assert level == 0 or self.get_value(level - 1) > value
return level
def get_max_level(self, value):
"""Maximum cell level for given value.
Return the maximum level such that the metric is at least the given
value, or zero if there is no such level. For example,
``s2sphere.MIN_WIDTH.get_max_level(0.1)`` returns the maximum level
such that all cells have a minimum width of 0.1 or larger.
The return value is always a valid level.
:param value:
Depending on whether this is used in one or two dimensions, this is
an angle in radians or a solid angle in steradians.
"""
if value <= 0:
return CellId.MAX_LEVEL
m, x = math.frexp(self.deriv() / value)
level = max(0, min(CellId.MAX_LEVEL, (x - 1) >> (self.__dim - 1)))
assert level == 0 or self.get_value(level) >= value
assert level == CellId.MAX_LEVEL or self.get_value(level + 1) < value
return level
def __get_initial_candidates(self):
if self.__max_cells >= 4:
cap = self.__region.get_cap_bound()
level = min(CellId.min_width().get_max_level(
2 * cap.angle().radians),
min(self.__max_level, CellId.MAX_LEVEL - 1))
if self.__level_mod > 1 and level > self.__min_level:
level -= (level - self.__min_level) % self.__level_mod
if level > 0:
cell_id = CellId.from_point(cap.axis())
vertex_neighbors = cell_id.get_vertex_neighbors(level)
for neighbor in vertex_neighbors:
self.__add_candidate(self.__new_candidate(Cell(neighbor)))
return
for face in range(6):
self.__add_candidate(self.__new_candidate(FACE_CELLS[face]))
def __init__(self):
if rospy.has_param('~orientation_offset'):
# Orientation offset as quaterion q = [x,y,z,w].
self.orientation_offset = rospy.get_param('~orientation_offset')
else:
yaw_offset_deg = rospy.get_param('~yaw_offset_deg', 0.0)
self.orientation_offset = tf.quaternion_from_euler(0.0, 0.0, math.radians(yaw_offset_deg))
rospy.Subscriber(rospy.get_name() + "/imu_in", Imu, self.imu_callback)
self.pub_imu_out = rospy.Publisher(rospy.get_name() + '/imu_out',
Imu, queue_size=10)
rospy.spin()
def mitsuta_mean(self, angles_array):
# Function meant to work with degrees, covert inputs
# from radians to degrees and output from degrees to radians
D = math.degrees(angles_array[0])
mysum = D
for val in angles_array[1:]:
val = math.degrees(val)
delta = val - D
if delta < -180.0:
D = D + delta + 360.0
elif delta < 180.0:
D = D + delta
else:
D = D + delta - 360.0
mysum = mysum + D
m = mysum / len(angles_array)
avg = math.radians((m + 360.0) % 360.0)
# make sure avg is between -pi and pi
if avg > math.pi:
avg = avg - 2.0 * math.pi
elif avg < -math.pi:
avg = avg + 2.0 * math.pi
return avg
def iaga2df(iaga2002_fname, D_to_radians=True):
"""
Parser the magnetometer data record stored in the IAGA-2002 format
file *iaga2002_fname*. If *D_to_radians*, declination data (D) are
converted from degrees to radians. Return the tuple with the
:class:`DataFrame` containing the data and header information
"""
with open(iaga2002_fname) as fid:
# parse header
header, cols = parse_header(fid)
keys = ['B_' + x for x in cols]
# parse data
index = []
data_map = defaultdict(list)
for line in fid:
toks = line.split()
dt = datetime.strptime(toks[0] + ' ' + toks[1], '%Y-%m-%d %H:%M:%S.%f')
index.append(dt)
data = map(convert_float, toks[3:])
for key_i, data_i in zip(keys, data):
if key_i == 'B_D' and D_to_radians:
data_i = math.radians(data_i)
data_map[key_i].append(data_i)
df = PD.DataFrame(index=index, data=data_map)
return df, header
def get_earth_dist(pt_a, pt_b=None):
if type(pt_a) is str or pt_b is None:
return 'unkn' # No location set
log.debug("Calculating distance from {} to {}".format(pt_a, pt_b))
lat_a = radians(pt_a[0])
lng_a = radians(pt_a[1])
lat_b = radians(pt_b[0])
lng_b = radians(pt_b[1])
lat_delta = lat_b - lat_a
lng_delta = lng_b - lng_a
a = sin(lat_delta / 2) ** 2 + cos(lat_a) * cos(lat_b) * sin(lng_delta / 2) ** 2
c = 2 * atan2(sqrt(a), sqrt(1 - a))
radius = 6373000 # radius of earth in meters
if config['UNITS'] == 'imperial':
radius = 6975175 # radius of earth in yards
dist = c * radius
return dist
# Return the time as a string in different formats
def distance(self, loc):
"""
Calculate the great circle distance between two points
on the earth (specified in decimal degrees)
"""
assert type(loc) == type(self)
# convert decimal degrees to radians
lon1, lat1, lon2, lat2 = map(radians, [
self.lon,
self.lat,
loc.lon,
loc.lat,
])
# haversine formula
dlon = lon2 - lon1
dlat = lat2 - lat1
a = sin(dlat/2)**2 + cos(lat1) * cos(lat2) * sin(dlon/2)**2
c = 2 * asin(sqrt(a))
r = 6371000 # Radius of earth in meters.
return c * r
def distance(pointA, pointB):
"""
Calculate the great circle distance between two points
on the earth (specified in decimal degrees)
http://stackoverflow.com/questions/15736995/how-can-i-quickly-estimate-the-distance-between-two-latitude-longitude-points
"""
# convert decimal degrees to radians
lon1, lat1, lon2, lat2 = map(math.radians, [pointA[1], pointA[0], pointB[1], pointB[0]])
# haversine formula
dlon = lon2 - lon1
dlat = lat2 - lat1
a = math.sin(dlat/2)**2 + math.cos(lat1) * math.cos(lat2) * math.sin(dlon/2)**2
c = 2 * math.asin(math.sqrt(a))
r = 3956 # Radius of earth in miles. Use 6371 for kilometers
return c * r
def mergeRotatedScaledPage(self, page2, rotation, scale):
rotation = math.radians(rotation)
rotating = [[math.cos(rotation), math.sin(rotation),0],
[-math.sin(rotation),math.cos(rotation), 0],
[0, 0, 1]]
scaling = [[scale,0, 0],
[0, scale,0],
[0, 0, 1]]
ctm = utils.matrixMultiply(rotating, scaling)
return self.mergeTransformedPage(page2,
[ctm[0][0], ctm[0][1],
ctm[1][0], ctm[1][1],
ctm[2][0], ctm[2][1]])
##
# This is similar to mergePage, but the stream to be merged is translated
# and scaled by appling a transformation matrix.
#
# @param page2 An instance of {@link #PageObject PageObject} to be merged.
# @param scale The scaling factor
# @param tx The translation on X axis
# @param tx The translation on Y axis
def mergeRotatedScaledTranslatedPage(self, page2, rotation, scale, tx, ty):
translation = [[1, 0, 0],
[0, 1, 0],
[tx,ty,1]]
rotation = math.radians(rotation)
rotating = [[math.cos(rotation), math.sin(rotation),0],
[-math.sin(rotation),math.cos(rotation), 0],
[0, 0, 1]]
scaling = [[scale,0, 0],
[0, scale,0],
[0, 0, 1]]
ctm = utils.matrixMultiply(rotating, scaling)
ctm = utils.matrixMultiply(ctm, translation)
return self.mergeTransformedPage(page2, [ctm[0][0], ctm[0][1],
ctm[1][0], ctm[1][1],
ctm[2][0], ctm[2][1]])
##
# Applys a transformation matrix the page.
#
# @param ctm A 6 elements tuple containing the operands of the
# transformation matrix
def manual_control(self, rotation: int, velocity: float,
duration: int=1500):
"""Give a command over manual control interface."""
if rotation < -180 or rotation > 180:
raise DeviceException("Given rotation is invalid, should "
"be ]-180, 180[, was %s" % rotation)
if velocity < -0.3 or velocity > 0.3:
raise DeviceException("Given velocity is invalid, should "
"be ]-0.3, 0.3[, was: %s" % velocity)
self.manual_seqnum += 1
params = {"omega": round(math.radians(rotation), 1),
"velocity": velocity,
"duration": duration,
"seqnum": self.manual_seqnum}
self.send("app_rc_move", [params])
def rotation_matrix(theta, axis):
"""
Return the rotation matrix associated with counterclockwise rotation about
the given axis by theta radians.
Adapted from
http://stackoverflow.com/questions/6802577/python-rotation-of-3d-vector
"""
axis = axis[:3]
axis = axis/math.sqrt(np.dot(axis, axis))
a = math.cos(theta/2.0)
b, c, d = -axis*math.sin(theta/2.0)
aa, bb, cc, dd = a*a, b*b, c*c, d*d
bc, ad, ac, ab, bd, cd = b*c, a*d, a*c, a*b, b*d, c*d
return np.array([[aa+bb-cc-dd, 2*(bc+ad), 2*(bd-ac), 0],
[2*(bc-ad), aa+cc-bb-dd, 2*(cd+ab), 0],
[2*(bd+ac), 2*(cd-ab), aa+dd-bb-cc, 0],
[0, 0, 0, 1]])
def get_center_of_nodes(nodes):
"""Helper function to get center coordinates of a group of nodes
"""
x = 0
y = 0
z = 0
for node in nodes:
lat = radians(float(node.lat))
lon = radians(float(node.lon))
x += cos(lat) * cos(lon)
y += cos(lat) * sin(lon)
z += sin(lat)
x = float(x / len(nodes))
y = float(y / len(nodes))
z = float(z / len(nodes))
center_lat = degrees(atan2(z, sqrt(x * x + y * y)))
center_lon = degrees(atan2(y, x))
return center_lat, center_lon
def get_crow_fly_distance(from_tuple, to_tuple):
"""
Uses the Haversine formmula to compute distance
(https://en.wikipedia.org/wiki/Haversine_formula#The_haversine_formula)
"""
lat1, lon1 = from_tuple
lat2, lon2 = to_tuple
lat1 = float(lat1)
lat2 = float(lat2)
lon1 = float(lon1)
lon2 = float(lon2)
radius = 6371 # km
dlat = math.radians(lat2 - lat1)
dlon = math.radians(lon2 - lon1)
a = math.sin(dlat / 2) * math.sin(dlat / 2) + math.cos(math.radians(lat1)) * \
math.cos(math.radians(lat2)) * math.sin(dlon / 2) * math.sin(dlon / 2)
c = 2 * math.atan2(math.sqrt(a), math.sqrt(1 - a))
d = radius * c
return d * 1000 # meters
def mergeRotatedPage(self, page2, rotation, expand=False):
"""
This is similar to mergePage, but the stream to be merged is rotated
by appling a transformation matrix.
:param PageObject page2: the page to be merged into this one. Should be
an instance of :class:`PageObject<PageObject>`.
:param float rotation: The angle of the rotation, in degrees
:param bool expand: Whether the page should be expanded to fit the
dimensions of the page to be merged.
"""
rotation = math.radians(rotation)
return self.mergeTransformedPage(page2,
[math.cos(rotation), math.sin(rotation),
-math.sin(rotation), math.cos(rotation),
0, 0], expand)
def mergeRotatedScaledPage(self, page2, rotation, scale, expand=False):
"""
This is similar to mergePage, but the stream to be merged is rotated
and scaled by appling a transformation matrix.
:param PageObject page2: the page to be merged into this one. Should be
an instance of :class:`PageObject<PageObject>`.
:param float rotation: The angle of the rotation, in degrees
:param float scale: The scaling factor
:param bool expand: Whether the page should be expanded to fit the
dimensions of the page to be merged.
"""
rotation = math.radians(rotation)
rotating = [[math.cos(rotation), math.sin(rotation), 0],
[-math.sin(rotation), math.cos(rotation), 0],
[0, 0, 1]]
scaling = [[scale, 0, 0],
[0, scale, 0],
[0, 0, 1]]
ctm = utils.matrixMultiply(rotating, scaling)
return self.mergeTransformedPage(page2,
[ctm[0][0], ctm[0][1],
ctm[1][0], ctm[1][1],
ctm[2][0], ctm[2][1]], expand)
def create_ring_coordinates(radius, count):
""" Create coordinates arranged in a ring.
:param radius: Radius of the ring.
:type radius: float
:param count: Count of coordinates to generate.
:type count: int
:returns: Tuple of the coordinates.
:rtype: tuple
"""
pixelstep = math.radians(360/count)
pixels = []
for index in range(0, count):
pixels.append((radius * math.cos(index*pixelstep),
radius * math.sin(index*pixelstep)))
return pixels
def bearing_degrees(lat1, lon1, lat2, lon2):
"""
Convert location in bearing degrees to be able to give a direction of where the Pokemon is located.
:param lat1: user location latitude
:param lon1: user location longitude
:param lat2: pokemon location latitude
:param lon2: pokemon location longitude
:return: bearing degrees
"""
# convert decimal degrees to radians
lon1, lat1, lon2, lat2 = map(radians, [lon1, lat1, lon2, lat2])
# calculate the angle
dlon = lon2 - lon1
dlat = lat2 - lat1
x = math.sin(dlon) * math.cos(lat2)
y = math.cos(lat1) * math.sin(lat2) - (math.sin(lat1) * math.cos(lat2) * math.cos(dlon))
initial_bearing = math.atan2(x, y)
initial_bearing = math.degrees(initial_bearing)
bearing = (initial_bearing + 360) % 360
bearing = int(bearing)
return bearing
def bearing_degrees(lat1, lon1, lat2, lon2):
"""
Convert location in bearing degrees to be able to give a direction of where the Pokemon is located.
:param lat1: user location latitude
:param lon1: user location longitude
:param lat2: pokemon location latitude
:param lon2: pokemon location longitude
:return: bearing degrees
"""
# convert decimal degrees to radians
lon1, lat1, lon2, lat2 = map(radians, [lon1, lat1, lon2, lat2])
# calculate the angle
dlon = lon2 - lon1
dlat = lat2 - lat1
x = math.sin(dlon) * math.cos(lat2)
y = math.cos(lat1) * math.sin(lat2) - (math.sin(lat1) * math.cos(lat2) * math.cos(dlon))
initial_bearing = math.atan2(x, y)
initial_bearing = math.degrees(initial_bearing)
bearing = (initial_bearing + 360) % 360
bearing = int(bearing)
return bearing
def cube_map_render_pre(scene, use_force=False):
if not do_run(scene.cube_map, use_force):
return
from math import radians
camera = scene.camera
data = camera.data.copy()
data.lens_unit = 'FOV'
data.angle = radians(90)
data.type = 'PERSP'
mat = camera.matrix_world
loc = mat.to_translation()
rot = mat.to_euler()
zed = rot.z
views = bpy.cube_map_views
for view in views:
view.setCamera(data, loc, zed)
def ang_match3D(pt1, pt2, pt3, exp_ang):
ang_meas = get_line_ang_3D(pt1, pt2, pt3)
'''
print("pt1", pt1) # debug
print("pt2", pt2) # debug
print("pt3", pt3) # debug
print("exp_ang ", exp_ang) # debug
print("ang_meas ", ang_meas) # debug
'''
return flts_alm_eq(ang_meas, exp_ang)
# Calculates rotation around axis or face normal at Pivot's location.
# Takes two 3D coordinate Vectors (piv_co and mov_co), rotation angle in
# radians (ang_diff_rad), and rotation data storage object (rot_dat).
# Aligns mov_co to world origin (0, 0, 0) and rotates aligned
# mov_co (mov_aligned) around axis stored in rot_dat. After rotation,
# removes world-origin alignment.
def prep_rotation_info(ref_pts, r_dat, curr_ms_stor, new_ms_stor):
#print("curr angle", curr_ms_stor) # debug
#print("new angle", new_ms_stor) # debug
# workaround for negative angles and angles over 360 degrees
if new_ms_stor < 0 or new_ms_stor > 360:
new_ms_stor = new_ms_stor % 360
r_dat.ang_diff_d = new_ms_stor - curr_ms_stor
# fix for angles over 180 degrees
if new_ms_stor > 180:
r_dat.new_ang_r = radians(180 - (new_ms_stor % 180))
else:
r_dat.new_ang_r = radians(new_ms_stor)
r_dat.ang_diff_r = radians(r_dat.ang_diff_d)
r_dat.axis_lk = ref_pts.ax_lock
# Takes: ed_type (Editor Type), new_free_co (Vector), ref_pts (ReferencePoints),
# and rDat (RotationData) as args. Uses new_free_co to calculate the rotation
# value and then rotates the selected objects or selected vertices using
# the obtained value.
def execute(self, context):
try:
GSP = context.lamp.GeoSunProperties
dst = 1 if GSP.dst else 0
az,el = sun_calculator.geoSunData(
GSP.lat,
GSP.long,
GSP.year,
GSP.month,
GSP.day,
GSP.hour + GSP.minute/60.0,
-GSP.tz + dst
)
context.object.rotation_euler = ( math.radians(90-el), 0, math.radians(-az) )
return {'FINISHED'}
except Exception as err:
self.report({'ERROR'}, str(err))
return {'CANCELLED'}
def text(self, en, scene, name):
"""
en: dxf entity
name: ignored; exists to make separate and merged objects methods universally callable from _call_types()
Returns a new single line text object.
"""
if self.import_text:
name = en.text[:8]
d = bpy.data.curves.new(name, "FONT")
d.body = en.plain_text()
d.size = en.height
o = bpy.data.objects.new(name, d)
o.rotation_euler = Euler((0, 0, radians(en.rotation)), 'XYZ')
basepoint = self.proj(en.basepoint) if hasattr(en, "basepoint") else self.proj((0, 0, 0))
o.location = self.proj((en.insert)) + basepoint
if hasattr(en, "thickness"):
et = en.thickness / 2
d.extrude = abs(et)
if et > 0:
o.location.z += et
elif et < 0:
o.location.z -= et
return o
def create_bezier(objname, points, origin):
curvedata = bpy.data.curves.new(name=objname, type='CURVE')
curvedata.dimensions = '3D'
myobject = bpy.data.objects.new(objname, curvedata)
myobject.location = origin
myobject.rotation_euler[2] = radians(90)
bpy.context.scene.objects.link(myobject)
polyline = curvedata.splines.new('BEZIER')
polyline.bezier_points.add(len(points) - 1)
for idx, (knot, h1, h2) in enumerate(points):
point = polyline.bezier_points[idx]
point.co = knot
point.handle_left = h1
point.handle_right = h2
point.handle_left_type = 'FREE'
point.handle_right_type = 'FREE'
return myobject
def rotate_fix_armature(arm_data):
global_matrix = Matrix.Rotation(radians(90), 4, "X")
bpy.ops.object.mode_set(mode='EDIT', toggle=False)
#disconnect all bones for ease of global rotation
connectedBones = []
for bone in arm_data.edit_bones:
if bone.use_connect:
connectedBones.append(bone.name)
bone.use_connect = False
#rotate all the bones around their center
for bone in arm_data.edit_bones:
bone.transform(global_matrix)
#reconnect the bones
for bone in connectedBones:
arm_data.edit_bones[bone].use_connect = True
bpy.ops.object.mode_set(mode='OBJECT', toggle=False)
#Roughly scales the performer armature to match the enduser armature
#IN: perfromer_obj, enduser_obj, Blender objects whose .data is an armature.