Skip to content

Commit a7ccccb

Browse files
authored
Merge pull request #28 from BerkeleyAutomation/merge/compat
Improved python3 compatibility for points and point cloud division
2 parents 6535ba8 + 640dfc2 commit a7ccccb

File tree

2 files changed

+85
-1
lines changed

2 files changed

+85
-1
lines changed

autolab_core/points.py

Lines changed: 60 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -346,6 +346,14 @@ def __rmul__(self, mult):
346346
return self.__mul__(mult)
347347

348348
def __div__(self, div):
349+
"""A python2 compatibility wrapper."""
350+
return self.__truediv__(div)
351+
352+
def __rdiv__(self, div):
353+
"""A python2 compatibility wrapper."""
354+
return self.__rtruediv__(div)
355+
356+
def __truediv__(self, div):
349357
"""Divide the point by a scalar.
350358
351359
Parameters
@@ -367,6 +375,28 @@ def __div__(self, div):
367375
raise ValueError('Type %s not supported. Only scalar division is supported' %(type(div)))
368376
return self.__mul__(1.0 / div)
369377

378+
def __rtruediv__(self, div):
379+
"""Divide the scalar by a point.
380+
381+
Parameters
382+
----------
383+
div : float
384+
The number by which the Point is divided.
385+
386+
Returns
387+
-------
388+
:obj:`Point3D`
389+
A 3D point created by the division.
390+
391+
Raises
392+
------
393+
ValueError
394+
If div is not a scalar value.
395+
"""
396+
if isinstance(div, numbers.Number):
397+
return Point(div / self._data, self._frame)
398+
raise ValueError('Type %s not supported. Only scalar division is supported' %(type(div)))
399+
370400
@staticmethod
371401
def open(filename, frame='unspecified'):
372402
"""Create a Point from data saved in a file.
@@ -792,6 +822,14 @@ def __rmul__(self, mult):
792822
return self.__mul__(mult)
793823

794824
def __div__(self, div):
825+
"""A python2 compatibility wrapper."""
826+
return self.__truediv__(div)
827+
828+
def __rdiv__(self, div):
829+
"""A python2 compatibility wrapper."""
830+
return self.__rtruediv__(div)
831+
832+
def __truediv__(self, div):
795833
"""Divide each point in the cloud by a scalar.
796834
797835
Parameters
@@ -813,6 +851,28 @@ def __div__(self, div):
813851
raise ValueError('Type %s not supported. Only scalar division is supported' %(type(div)))
814852
return self.__mul__(1.0 / div)
815853

854+
def __rtruediv__(self, div):
855+
"""Divide a scalar by each point in the cloud.
856+
857+
Parameters
858+
----------
859+
div : float
860+
The number by which the PointCloud is divided.
861+
862+
Returns
863+
-------
864+
:obj:`PointCloud`
865+
A PointCloud created by the division.
866+
867+
Raises
868+
------
869+
ValueError
870+
If div is not a scalar value.
871+
"""
872+
if isinstance(div, numbers.Number):
873+
return PointCloud(div / self._data, self._frame)
874+
raise ValueError('Type %s not supported. Only scalar division is supported' %(type(div)))
875+
816876
@staticmethod
817877
def open(filename, frame='unspecified'):
818878
"""Create a PointCloud from data saved in a file.
@@ -1210,4 +1270,3 @@ def remove_zero_points(self):
12101270
(np.isfinite(self.normal_cloud.data[0,:])))[0]
12111271
self.point_cloud._data = self.point_cloud.data[:, points_of_interest]
12121272
self.normal_cloud._data = self.normal_cloud.data[:, points_of_interest]
1213-

tests/test_points.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,31 @@ def test_inits(self, num_points=10):
103103
except ValueError as e:
104104
caught_bad_init = True
105105
self.assertTrue(caught_bad_init, msg='Failed to catch rgb cloud init with 4xN array')
106+
107+
def test_divs(self, num_points=10):
108+
data = np.random.rand(3, num_points)
109+
p_a = PointCloud(data, 'a')
110+
p_b = Point(np.random.rand(3), 'b')
111+
112+
# div on left
113+
p_a_int = p_a / 5
114+
assert np.allclose(p_a_int._data, p_a._data / 5)
115+
p_a_float = p_a / 2.5
116+
assert np.allclose(p_a_float._data, p_a._data / 2.5)
117+
p_b_int = p_b / 5
118+
assert np.allclose(p_b_int._data, p_b._data / 5)
119+
p_b_float = p_b / 2.5
120+
assert np.allclose(p_b_float._data, p_b._data / 2.5)
121+
122+
# div on right
123+
p_a_int = 5 / p_a
124+
assert np.allclose(p_a_int._data, 5 / p_a._data)
125+
p_a_float = 2.5 / p_a
126+
assert np.allclose(p_a_float._data, 2.5 / p_a._data)
127+
p_b_int = 5 / p_b
128+
assert np.allclose(p_b_int._data, 5 / p_b._data)
129+
p_b_float = 2.5 / p_b
130+
assert np.allclose(p_b_float._data, 2.5 / p_b._data)
106131

107132
if __name__ == '__main__':
108133
unittest.main()

0 commit comments

Comments
 (0)