From 1138c67d45197f3075efbd2a03571f7dba48e1ce Mon Sep 17 00:00:00 2001 From: Ogi Moore Date: Mon, 19 Apr 2021 22:18:59 -0700 Subject: [PATCH] Use math module trig functions for scalars This commit replaces the use of np.sin/np.cos/np.tan uses throughout the library with math.sin/math.cos/math.tan for scalar values --- pyqtgraph/Point.py | 19 ++++--------------- pyqtgraph/functions.py | 6 +++--- pyqtgraph/opengl/GLViewWidget.py | 21 +++++++++++---------- 3 files changed, 18 insertions(+), 28 deletions(-) diff --git a/pyqtgraph/Point.py b/pyqtgraph/Point.py index 4ad2c7f9..bdd8bc33 100644 --- a/pyqtgraph/Point.py +++ b/pyqtgraph/Point.py @@ -6,8 +6,7 @@ Distributed under MIT/X11 license. See license.txt for more information. """ from .Qt import QtCore -from . import functions as fn -from math import sin, acos, atan2, inf, pi, hypot +from math import atan2, hypot, degrees class Point(QtCore.QPointF): @@ -95,23 +94,14 @@ class Point(QtCore.QPointF): """Returns the vector length of this Point.""" return hypot(self[0], self[1]) # length - def norm(self): """Returns a vector in the same direction with unit length.""" return self / self.length() def angle(self, a): """Returns the angle in degrees between this vector and the vector a.""" - n1 = self.length() - n2 = a.length() - if n1 == 0. or n2 == 0.: - return None - ## Probably this should be done with arctan2 instead.. - ang = acos(fn.clip_scalar(self.dot(a) / (n1 * n2), -1.0, 1.0)) ### in radians - c = self.cross(a) - if c > 0: - ang *= -1. - return ang * 180. / pi + rads = atan2(self.y(), self.x()) - atan2(a.y(), a.x()) + return degrees(rads) def dot(self, a): """Returns the dot product of a and this Point.""" @@ -129,8 +119,7 @@ class Point(QtCore.QPointF): def __repr__(self): return "Point(%f, %f)" % (self[0], self[1]) - - + def min(self): return min(self[0], self[1]) diff --git a/pyqtgraph/functions.py b/pyqtgraph/functions.py index 2a4ef281..6fd64244 100644 --- a/pyqtgraph/functions.py +++ b/pyqtgraph/functions.py @@ -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 * np.tan(tipAngle * 0.5 * np.pi/180.) + headWidth = headLen * math.tan(tipAngle * 0.5 * math.pi / 180.) path = QtGui.QPainterPath() path.moveTo(0,0) path.lineTo(headLen, -headWidth) if tailLen is None: - innerY = headLen - headWidth * np.tan(baseAngle*np.pi/180.) + innerY = headLen - headWidth * math.tan(baseAngle * math.pi / 180.) path.lineTo(innerY, 0) else: tailWidth *= 0.5 - innerY = headLen - (headWidth-tailWidth) * np.tan(baseAngle*np.pi/180.) + innerY = headLen - (headWidth-tailWidth) * math.tan(baseAngle* math.pi / 180.) path.lineTo(innerY, -tailWidth) path.lineTo(headLen + tailLen, -tailWidth) path.lineTo(headLen + tailLen, tailWidth) diff --git a/pyqtgraph/opengl/GLViewWidget.py b/pyqtgraph/opengl/GLViewWidget.py index 66daa134..be78113e 100644 --- a/pyqtgraph/opengl/GLViewWidget.py +++ b/pyqtgraph/opengl/GLViewWidget.py @@ -5,6 +5,7 @@ import numpy as np from .. import Vector from .. import functions as fn import warnings +from math import cos, sin, tan ##Vector = QtGui.QVector3D ShareWidget = None @@ -183,7 +184,7 @@ class GLViewWidget(QtWidgets.QOpenGLWidget): nearClip = dist * 0.001 farClip = dist * 1000. - r = nearClip * np.tan(fov * 0.5 * np.pi / 180.) + r = nearClip * tan(fov * 0.5 * np.pi / 180.) t = r * h / w ## Note that X0 and width in these equations must be the values used in viewport @@ -328,9 +329,9 @@ class GLViewWidget(QtWidgets.QOpenGLWidget): elev = self.opts['elevation'] * np.pi / 180 azim = self.opts['azimuth'] * np.pi / 180 pos = Vector( - center.x() + dist * np.cos(elev) * np.cos(azim), - center.y() + dist * np.cos(elev) * np.sin(azim), - center.z() + dist * np.sin(elev) + center.x() + dist * cos(elev) * cos(azim), + center.y() + dist * cos(elev) * sin(azim), + center.z() + dist * sin(elev) ) return pos @@ -387,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. * np.tan(0.5 * self.opts['fov'] * np.pi / 180.) ## approx. width of view at distance of center point + xDist = dist * 2. * tan(0.5 * self.opts['fov'] * np.pi / 180.) ## 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() @@ -412,11 +413,11 @@ class GLViewWidget(QtWidgets.QOpenGLWidget): azim = np.radians(self.opts['azimuth']) fov = np.radians(self.opts['fov']) dist = (self.opts['center'] - self.cameraPosition()).length() - fov_factor = np.tan(fov / 2) * 2 + fov_factor = tan(fov / 2) * 2 scale_factor = dist * fov_factor / self.width() - z = scale_factor * np.cos(elev) * dy - x = scale_factor * (np.sin(azim) * dx - np.sin(elev) * np.cos(azim) * dy) - y = scale_factor * (np.cos(azim) * dx + np.sin(elev) * np.sin(azim) * dy) + z = scale_factor * cos(elev) * dy + x = scale_factor * (sin(azim) * dx - sin(elev) * cos(azim) * dy) + y = scale_factor * (cos(azim) * dx + sin(elev) * sin(azim) * dy) self.opts['center'] += QtGui.QVector3D(x, -y, z) else: raise ValueError("relative argument must be global, view, or view-upright") @@ -434,7 +435,7 @@ class GLViewWidget(QtWidgets.QOpenGLWidget): dist = ((pos-cam)**2).sum(axis=-1)**0.5 else: dist = (pos-cam).length() - xDist = dist * 2. * np.tan(0.5 * self.opts['fov'] * np.pi / 180.) + xDist = dist * 2. * tan(0.5 * self.opts['fov'] * np.pi / 180.) return xDist / self.width() def mousePressEvent(self, ev):