Use infinity instead of None in MinMax3D to improve performance

Speed is improved by about 5%
This commit is contained in:
Eyal Soha committed 2018-07-18 16:57:42 +03:00
1 parent 59a435a787
commit cd3c8ebbe6
1 file changed
+21 -26
+21 -26
View File
@@ -134,30 +134,25 @@ class MinMax3D(object):
>>> empty = MinMax3D()
>>> empty.size == Vector3D(0.0, 0.0, 0.0)
True
>>> partial = MinMax3D()
>>> partial.record(Vector3D(2.0, None, 2.0))
>>> partial.min.x == 2.0 == partial.max.x and partial.min.y == None == partial.max.y and partial.min.z == 2.0 == partial.max.z
True
>>> partial.record(Vector3D(1.0, None, 3.0))
>>> partial.min.x == 1.0 and partial.min.y == None and partial.min.z == 2.0
True
>>> partial.max.x == 2.0 and partial.max.y == None and partial.max.z == 3.0
True
>>> partial.size == Vector3D(1.0, 0.0, 1.0)
True
"""
def __init__(self):
self.min = Vector3D(None, None, None)
self.max = Vector3D(None, None, None)
self.min = Vector3D(float("inf"), float("inf"), float("inf"))
self.max = Vector3D(-float("inf"), -float("inf"), -float("inf"))
def record(self, coordinate):
self.min.x = coordinate.x if not self.min.x else min(self.min.x, coordinate.x)
self.min.y = coordinate.y if not self.min.y else min(self.min.y, coordinate.y)
self.min.z = coordinate.z if not self.min.z else min(self.min.z, coordinate.z)
self.max.x = coordinate.x if not self.max.x else max(self.max.x, coordinate.x)
self.max.y = coordinate.y if not self.max.y else max(self.max.y, coordinate.y)
self.max.z = coordinate.z if not self.max.z else max(self.max.z, coordinate.z)
"""
Records the coordinate, storing the min and max values.
The input vector components must not be None.
"""
self.min.x = min(self.min.x, coordinate.x)
self.min.y = min(self.min.y, coordinate.y)
self.min.z = min(self.min.z, coordinate.z)
self.max.x = max(self.max.x, coordinate.x)
self.max.y = max(self.max.y, coordinate.y)
self.max.z = max(self.max.z, coordinate.z)
@property
def size(self):
@@ -165,7 +160,7 @@ class MinMax3D(object):
for c in "xyz":
min = getattr(self.min, c)
max = getattr(self.max, c)
value = abs(max - min) if min is not None and max is not None else 0.0
value = abs(max - min) if max >= min else 0.0
setattr(result, c, value)
return result
@@ -199,12 +194,12 @@ class gcode(object):
@property
def printing_area(self):
return dict(minX=self._minMax.min.x,
minY=self._minMax.min.y,
minZ=self._minMax.min.z,
maxX=self._minMax.max.x,
maxY=self._minMax.max.y,
maxZ=self._minMax.max.z)
return dict(minX=None if math.isinf(self._minMax.min.x) else self._minMax.min.x,
minY=None if math.isinf(self._minMax.min.y) else self._minMax.min.y,
minZ=None if math.isinf(self._minMax.min.z) else self._minMax.min.z,
maxX=None if math.isinf(self._minMax.max.x) else self._minMax.max.x,
maxY=None if math.isinf(self._minMax.max.y) else self._minMax.max.y,
maxZ=None if math.isinf(self._minMax.max.z) else self._minMax.max.z)
def load(self, filename, throttle=None, speedx=6000, speedy=6000, offsets=None, max_extruders=10, g90_extruder=False):
if os.path.isfile(filename):