-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Instead of transforming the point cloud directly to a numpy buffer, we should instead return a intermediate layer, since in the underlying C++ API point cloud exposes various functions such as width and height. Making buffer releasable also created some technical debt: assertNotReleased is needed since accessing the underlying a C++ object after it has been released causes a crash. Ignored flake8's D402, since it mistakenly thought that the paranthesis in the docstring was the signature of the function.
- Loading branch information
Trym Bremnes
committed
Nov 12, 2019
1 parent
2498acc
commit 92d185e
Showing
15 changed files
with
183 additions
and
33 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
"""Contains the PointCloud class.""" | ||
import numpy | ||
|
||
import _zivid | ||
|
||
|
||
class PointCloud: | ||
"""A point cloud.""" | ||
|
||
def __init__(self, internal_point_cloud): | ||
"""Create a point cloud from an internal point cloud. | ||
Args: | ||
internal_point_cloud: a internal point cloud | ||
""" | ||
if not isinstance(internal_point_cloud, _zivid.PointCloud): | ||
raise ValueError( | ||
"Unsupported type for argument internal_point_cloud. Got {}, expected {}".format( | ||
type(internal_point_cloud), type(_zivid.PointCloud) | ||
) | ||
) | ||
self.__impl = internal_point_cloud | ||
|
||
def to_array(self): | ||
"""Convert point cloud to numpy array. | ||
Returns: | ||
a numpy array | ||
""" | ||
self.__impl.assert_not_released() | ||
return numpy.array(self.__impl) | ||
|
||
@property | ||
def height(self): | ||
"""Return height (number of rows) of point cloud. | ||
Returns: | ||
a positive integer | ||
""" | ||
return self.__impl.height() | ||
|
||
@property | ||
def width(self): | ||
"""Return width (number of columns) of point cloud. | ||
Returns: | ||
a positive integer | ||
""" | ||
return self.__impl.width() | ||
|
||
def release(self): | ||
"""Release the underlying resources.""" | ||
self.__impl.release() | ||
|
||
def __enter__(self): | ||
return self | ||
|
||
def __exit__(self, exception_type, exception_value, traceback): | ||
self.release() | ||
|
||
def __del__(self): | ||
self.release() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
#pragma once | ||
|
||
#include <Zivid/PointCloud.h> | ||
#include <ZividPython/Releasable.h> | ||
#include <ZividPython/Wrappers.h> | ||
|
||
namespace ZividPython | ||
{ | ||
class ReleasablePointCloud : public Releasable<Zivid::PointCloud> | ||
{ | ||
public: | ||
using Releasable<Zivid::PointCloud>::Releasable; | ||
|
||
ZIVID_PYTHON_FORWARD_0_ARGS(width) | ||
ZIVID_PYTHON_FORWARD_0_ARGS(height) | ||
ZIVID_PYTHON_FORWARD_0_ARGS(dataPtr) | ||
}; | ||
|
||
void wrapClass(pybind11::class_<ReleasablePointCloud> pyClass); | ||
} // namespace ZividPython |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,17 +1,71 @@ | ||
def test_point_cloud_to_array(frame): | ||
def test_point_cloud_to_array(point_cloud): | ||
import numpy as np | ||
|
||
point_cloud = frame.get_point_cloud() | ||
np_array = np.array(point_cloud) | ||
np_array = point_cloud.to_array() | ||
assert np_array is not None | ||
assert isinstance(np_array, np.ndarray) | ||
|
||
|
||
def test_to_rgb_image(frame): | ||
def test_to_rgb_image(point_cloud): | ||
import numpy as np | ||
|
||
point_cloud = frame.get_point_cloud() | ||
image = point_cloud[["r", "g", "b"]] | ||
image = np.asarray([point_cloud["r"], point_cloud["g"], point_cloud["b"]]) | ||
np_array = point_cloud.to_array() | ||
image = np_array[["r", "g", "b"]] | ||
image = np.asarray([np_array["r"], np_array["g"], np_array["b"]]) | ||
image = np.moveaxis(image, [0, 1, 2], [2, 0, 1]) | ||
image = image.astype(np.uint8) | ||
|
||
|
||
def test_height(point_cloud): | ||
height = point_cloud.height | ||
|
||
assert height is not None | ||
assert isinstance(height, int) | ||
|
||
|
||
def test_width(point_cloud): | ||
width = point_cloud.width | ||
|
||
assert width is not None | ||
assert isinstance(width, int) | ||
|
||
|
||
def test_height_context_manager(frame): | ||
import pytest | ||
|
||
with frame.get_point_cloud() as point_cloud: | ||
point_cloud.height # pylint: disable=pointless-statement | ||
with pytest.raises(RuntimeError): | ||
point_cloud.height # pylint: disable=pointless-statement | ||
|
||
|
||
def test_width_context_manager(frame): | ||
import pytest | ||
|
||
with frame.get_point_cloud() as point_cloud: | ||
point_cloud.width # pylint: disable=pointless-statement | ||
with pytest.raises(RuntimeError): | ||
point_cloud.width # pylint: disable=pointless-statement | ||
|
||
|
||
def test_to_array_context_manager(frame): | ||
import pytest | ||
|
||
with frame.get_point_cloud() as point_cloud: | ||
point_cloud.to_array() | ||
with pytest.raises(RuntimeError): | ||
point_cloud.to_array() | ||
|
||
|
||
def test_illegal_init(application): # pylint: disable=unused-argument | ||
import pytest | ||
import zivid | ||
|
||
with pytest.raises(TypeError): | ||
zivid.PointCloud() # pylint: disable=no-value-for-parameter | ||
|
||
with pytest.raises(ValueError): | ||
zivid.PointCloud("Should fail.") | ||
|
||
with pytest.raises(ValueError): | ||
zivid.PointCloud(123) |