Use math.radians and math.degrees

Many places in the library were doing radian to degree conversion
via the manual calculation.  Doing timeit benchmarks on my system, I
am able to get a 4x speedup by using math.degrees or math.radians
instead
This commit is contained in:
Ogi Moore 2021-04-19 22:43:44 -07:00
parent 1138c67d45
commit b0769f4be9
11 changed files with 31 additions and 35 deletions

View File

@ -8,7 +8,6 @@ Distributed under MIT/X11 license. See license.txt for more information.
from .Qt import QtCore
from math import atan2, hypot, degrees
class Point(QtCore.QPointF):
"""Extension of QPointF which adds a few missing methods."""

View File

@ -1,5 +1,5 @@
# -*- coding: utf-8 -*-
from math import atan2, pi
from math import atan2, degrees
from .Qt import QtCore, QtGui
from .Point import Point
import numpy as np
@ -77,7 +77,7 @@ class SRTTransform(QtGui.QTransform):
self._state = {
'pos': Point(p1),
'scale': Point(dp2.length(), dp3.length() * sy),
'angle': (atan2(dp2[1], dp2[0]) * 180. / pi) + da
'angle': degrees(atan2(dp2[1], dp2[0])) + da
}
self.update()

View File

@ -1,5 +1,5 @@
# -*- coding: utf-8 -*-
from math import atan2, pi
from math import atan2, degrees
from .Qt import QtCore, QtGui
from .Vector import Vector
from .Transform3D import Transform3D
@ -165,7 +165,7 @@ class SRTTransform3D(Transform3D):
sin = (r-r.T)[rInd] / (2. * sign * axis[axisInd])
## finally, we get the complete angle from arctan(sin/cos)
self._state['angle'] = atan2(sin, cos) * 180 / pi
self._state['angle'] = degrees(atan2(sin, cos))
if self._state['angle'] == 0:
self._state['axis'] = (0,0,1)

View File

@ -4,7 +4,7 @@ Vector.py - Extension of QVector3D which adds a few missing methods.
Copyright 2010 Luke Campagnola
Distributed under MIT/X11 license. See license.txt for more information.
"""
from math import acos
from math import acos, degrees
from .Qt import QtGui, QtCore, QT_LIB
from . import functions as fn
import numpy as np
@ -89,11 +89,11 @@ class Vector(QtGui.QVector3D):
if n1 == 0. or n2 == 0.:
return None
## Probably this should be done with arctan2 instead..
ang = acos(fn.clip_scalar(QtGui.QVector3D.dotProduct(self, a) / (n1 * n2), -1.0, 1.0)) ### in radians
rads = acos(fn.clip_scalar(QtGui.QVector3D.dotProduct(self, a) / (n1 * n2), -1.0, 1.0)) ### in radians
# c = self.crossProduct(a)
# if c > 0:
# ang *= -1.
return ang * 180. / np.pi
return degrees(rads)
def __abs__(self):
return Vector(abs(self.x()), abs(self.y()), abs(self.z()))

View File

@ -432,16 +432,16 @@ def makeArrowPath(headLen=20, headWidth=None, tipAngle=20, tailLen=20, tailWidth
If *tailLen* is None, no tail will be drawn.
"""
if headWidth is None:
headWidth = headLen * math.tan(tipAngle * 0.5 * math.pi / 180.)
headWidth = headLen * math.tan(math.radians(tipAngle * 0.5))
path = QtGui.QPainterPath()
path.moveTo(0,0)
path.lineTo(headLen, -headWidth)
if tailLen is None:
innerY = headLen - headWidth * math.tan(baseAngle * math.pi / 180.)
innerY = headLen - headWidth * math.tan(math.radians(baseAngle))
path.lineTo(innerY, 0)
else:
tailWidth *= 0.5
innerY = headLen - (headWidth-tailWidth) * math.tan(baseAngle* math.pi / 180.)
innerY = headLen - (headWidth-tailWidth) * math.tan(math.radians(baseAngle))
path.lineTo(innerY, -tailWidth)
path.lineTo(headLen + tailLen, -tailWidth)
path.lineTo(headLen + tailLen, tailWidth)

View File

@ -1,4 +1,4 @@
from math import atan2, pi
from math import atan2, degrees
from ..Qt import QtGui, QtCore
from . import ArrowItem
from ..functions import clip_scalar
@ -79,10 +79,10 @@ class CurvePoint(GraphicsObject):
p1 = self.parentItem().mapToScene(QtCore.QPointF(x[i1], y[i1]))
p2 = self.parentItem().mapToScene(QtCore.QPointF(x[i2], y[i2]))
ang = atan2(p2.y()-p1.y(), p2.x()-p1.x()) ## returns radians
rads = atan2(p2.y()-p1.y(), p2.x()-p1.x()) ## returns radians
self.resetTransform()
if self._rotate:
self.setRotation(180 + ang * (180. / pi))
self.setRotation(180 + degrees(rads))
QtGui.QGraphicsItem.setPos(self, *newPos)
return True

View File

@ -1,5 +1,5 @@
# -*- coding: utf-8 -*-
from math import atan2, pi
from math import atan2, degrees
from ..Qt import QtGui, QtCore
from ..Point import Point
from .GraphicsObject import GraphicsObject
@ -360,7 +360,7 @@ class InfiniteLine(GraphicsObject):
up = tr.map(Point(left, 1))
dif = end - start
length = Point(dif).length()
angle = atan2(dif.y(), dif.x()) * 180 / pi
angle = degrees(atan2(dif.y(), dif.x()))
p.translate(start)
p.rotate(angle)

View File

@ -17,7 +17,7 @@ import numpy as np
#from numpy.linalg import norm
from ..Point import *
from ..SRTTransform import SRTTransform
from math import atan2, cos, sin, pi, sqrt, hypot
from math import atan2, cos, sin, pi, sqrt, hypot, radians, degrees
from .. import functions as fn
from .GraphicsObject import GraphicsObject
from .UIGraphicsItem import UIGraphicsItem
@ -1524,7 +1524,7 @@ class Handle(UIGraphicsItem):
devPos = dt.map(QtCore.QPointF(0,0))
tr = QtGui.QTransform()
tr.translate(devPos.x(), devPos.y())
tr.rotate(va * 180. / pi)
tr.rotate(degrees(va))
return dti.map(tr.map(self.path))
@ -1663,7 +1663,7 @@ class LineROI(ROI):
d = pos2-pos1
l = d.length()
ang = Point(1, 0).angle(d)
ra = ang * pi / 180.
ra = radians(ang if ang is not None else 0.)
c = Point(-width/2. * sin(ra), -width/2. * cos(ra))
pos1 = pos1 + c
@ -1671,9 +1671,6 @@ class LineROI(ROI):
self.addScaleRotateHandle([0, 0.5], [1, 0.5])
self.addScaleRotateHandle([1, 0.5], [0, 0.5])
self.addScaleHandle([0.5, 1], [0.5, 0.5])
class MultiRectROI(QtGui.QGraphicsObject):

View File

@ -1,4 +1,4 @@
from math import atan2, pi
from math import atan2, pi, degrees
from ..Qt import QtGui, QtCore
from ..Point import Point
@ -243,7 +243,7 @@ class TargetItem(UIGraphicsItem):
tr = QtGui.QTransform()
tr.translate(devPos.x(), devPos.y())
va = atan2(v.y(), v.x())
tr.rotate(va * 180.0 / pi)
tr.rotate(degrees(va))
tr.scale(self.scale, self.scale)
return dti.map(tr.map(self._path))

View File

@ -1,4 +1,4 @@
from math import pi, atan2
from math import atan2, degrees
from ..Qt import QtCore, QtGui
from ..Point import Point
from .. import functions as fn
@ -208,7 +208,7 @@ class TextItem(GraphicsObject):
angle = -self.angle
if self.rotateAxis is not None:
d = pt.map(self.rotateAxis) - pt.map(Point(0, 0))
a = atan2(d.y(), d.x()) * 180 / pi
a = degrees(atan2(d.y(), d.x()))
angle += a
t.rotate(angle)
self.setTransform(t)

View File

@ -5,7 +5,7 @@ import numpy as np
from .. import Vector
from .. import functions as fn
import warnings
from math import cos, sin, tan
from math import cos, sin, tan, degrees, radians
##Vector = QtGui.QVector3D
ShareWidget = None
@ -184,7 +184,7 @@ class GLViewWidget(QtWidgets.QOpenGLWidget):
nearClip = dist * 0.001
farClip = dist * 1000.
r = nearClip * tan(fov * 0.5 * np.pi / 180.)
r = nearClip * tan(0.5 * radians(fov))
t = r * h / w
## Note that X0 and width in these equations must be the values used in viewport
@ -326,8 +326,8 @@ class GLViewWidget(QtWidgets.QOpenGLWidget):
pos = center - self.opts['rotation'].rotatedVector( Vector(0,0,dist) )
else:
# using 'euler' rotation method
elev = self.opts['elevation'] * np.pi / 180
azim = self.opts['azimuth'] * np.pi / 180
elev = radians(self.opts['elevation'])
azim = radians(self.opts['azimuth'])
pos = Vector(
center.x() + dist * cos(elev) * cos(azim),
center.y() + dist * cos(elev) * sin(azim),
@ -388,7 +388,7 @@ class GLViewWidget(QtWidgets.QOpenGLWidget):
cPos = self.cameraPosition()
cVec = self.opts['center'] - cPos
dist = cVec.length() ## distance from camera to center
xDist = dist * 2. * tan(0.5 * self.opts['fov'] * np.pi / 180.) ## approx. width of view at distance of center point
xDist = dist * 2. * tan(0.5 * radians(self.opts['fov'])) ## approx. width of view at distance of center point
xScale = xDist / self.width()
zVec = QtGui.QVector3D(0,0,1)
xVec = QtGui.QVector3D.crossProduct(zVec, cVec).normalized()
@ -409,9 +409,9 @@ class GLViewWidget(QtWidgets.QOpenGLWidget):
# apply translation
self.opts['center'] += scale_factor * (xv*-dx + yv*dy + zv*dz)
else: # use default euler rotation method
elev = np.radians(self.opts['elevation'])
azim = np.radians(self.opts['azimuth'])
fov = np.radians(self.opts['fov'])
elev = radians(self.opts['elevation'])
azim = radians(self.opts['azimuth'])
fov = radians(self.opts['fov'])
dist = (self.opts['center'] - self.cameraPosition()).length()
fov_factor = tan(fov / 2) * 2
scale_factor = dist * fov_factor / self.width()
@ -435,7 +435,7 @@ class GLViewWidget(QtWidgets.QOpenGLWidget):
dist = ((pos-cam)**2).sum(axis=-1)**0.5
else:
dist = (pos-cam).length()
xDist = dist * 2. * tan(0.5 * self.opts['fov'] * np.pi / 180.)
xDist = dist * 2. * tan(0.5 * radians(self.opts['fov']))
return xDist / self.width()
def mousePressEvent(self, ev):