Skip to content

Commit

Permalink
Merge pull request #147 from zivid/ZIVID-4528_add_support_for_normals
Browse files Browse the repository at this point in the history
Add support for normals
  • Loading branch information
PiotrWiercinski authored Oct 19, 2021
2 parents 04c4fd8 + c2e4074 commit e21effd
Show file tree
Hide file tree
Showing 8 changed files with 35 additions and 2 deletions.
2 changes: 1 addition & 1 deletion continuous-integration/deployment/expected-version.txt
Original file line number Diff line number Diff line change
@@ -1 +1 @@
2.2.0.2.5.0
2.3.0.2.5.0
1 change: 1 addition & 0 deletions modules/_zivid/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
__version__,
Application,
Array2DColorRGBA,
Array2DNormalXYZ,
Array2DPointXYZ,
Array2DPointXYZColorRGBA,
Array2DPointXYZW,
Expand Down
2 changes: 2 additions & 0 deletions modules/zivid/point_cloud.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ def copy_data(self, data_format):
xyzw: ndarray(Height,Width,4) of float
z: ndarray(Height,Width) of float
rgba: ndarray(Height,Width,4) of uint8
normals: ndarray(Height,Width,3) of float
snr: ndarray(Height,Width) of float
xyzrgba: ndarray(Height,Width) of composite dtype (accessed with e.g. arr["x"])
Expand All @@ -80,6 +81,7 @@ def copy_data(self, data_format):
"xyzw": _zivid.Array2DPointXYZW,
"z": _zivid.Array2DPointZ,
"rgba": _zivid.Array2DColorRGBA,
"normals": _zivid.Array2DNormalXYZ,
"snr": _zivid.Array2DSNR,
"xyzrgba": _zivid.Array2DPointXYZColorRGBA,
}
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

# To be replaced by: from setuptools_scm import get_version
def get_version():
return "2.2.0"
return "2.3.0"


def _zivid_sdk_version():
Expand Down
12 changes: 12 additions & 0 deletions src/ReleasableArray2D.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,18 @@ namespace ZividPython
.def_buffer(pointCloudDataBuffer<NativeType, WrapperType, 4>);
}

void wrapClass(pybind11::class_<ReleasableArray2D<Zivid::NormalXYZ>> pyClass)
{
using WrapperType = float;
using NativeType = Zivid::NormalXYZ;
static_assert(std::is_same_v<WrapperType, decltype(NativeType::x)>);
static_assert(std::is_same_v<WrapperType, decltype(NativeType::y)>);
static_assert(std::is_same_v<WrapperType, decltype(NativeType::z)>);

pyClass.def(py::init<>(&pointCloudDataCopier<NativeType>))
.def_buffer(pointCloudDataBuffer<NativeType, WrapperType, 3>);
}

void wrapClass(pybind11::class_<ReleasableArray2D<Zivid::PointXYZ>> pyClass)
{
using WrapperType = float;
Expand Down
1 change: 1 addition & 0 deletions src/Wrapper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ ZIVID_PYTHON_MODULE // NOLINT
ZIVID_PYTHON_WRAP_CLASS_BUFFER_AS_RELEASABLE(module, PointCloud);

ZIVID_PYTHON_WRAP_ARRAY2D_BUFFER_AS_RELEASABLE(module, ColorRGBA);
ZIVID_PYTHON_WRAP_ARRAY2D_BUFFER_AS_RELEASABLE(module, NormalXYZ);
ZIVID_PYTHON_WRAP_ARRAY2D_BUFFER_AS_RELEASABLE(module, PointXYZ);
ZIVID_PYTHON_WRAP_ARRAY2D_BUFFER_AS_RELEASABLE(module, PointXYZW);
ZIVID_PYTHON_WRAP_ARRAY2D_BUFFER_AS_RELEASABLE(module, PointZ);
Expand Down
1 change: 1 addition & 0 deletions src/include/ZividPython/ReleasableArray2D.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ namespace ZividPython

void wrapClass(pybind11::class_<ReleasableArray2D<Zivid::SNR>> pyClass);
void wrapClass(pybind11::class_<ReleasableArray2D<Zivid::ColorRGBA>> pyClass);
void wrapClass(pybind11::class_<ReleasableArray2D<Zivid::NormalXYZ>> pyClass);
void wrapClass(pybind11::class_<ReleasableArray2D<Zivid::PointXYZ>> pyClass);
void wrapClass(pybind11::class_<ReleasableArray2D<Zivid::PointXYZW>> pyClass);
void wrapClass(pybind11::class_<ReleasableArray2D<Zivid::PointZ>> pyClass);
Expand Down
16 changes: 16 additions & 0 deletions test/test_point_cloud.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,14 @@ def test_point_cloud_copy_data(point_cloud):
xyzw = point_cloud.copy_data("xyzw")
xyzrgba = point_cloud.copy_data("xyzrgba")
rgba = point_cloud.copy_data("rgba")
normals = point_cloud.copy_data("normals")
depth = point_cloud.copy_data("z")
snr = point_cloud.copy_data("snr")
assert isinstance(xyz, np.ndarray)
assert isinstance(xyzw, np.ndarray)
assert isinstance(xyzrgba, np.ndarray)
assert isinstance(rgba, np.ndarray)
assert isinstance(normals, np.ndarray)
assert isinstance(depth, np.ndarray)
assert isinstance(snr, np.ndarray)

Expand Down Expand Up @@ -67,6 +69,20 @@ def test_point_cloud_rgba(point_cloud):
np.testing.assert_array_equal(rgba[:, :, 3], xyzrgba["a"])


def test_point_cloud_normals(point_cloud):
import numpy as np

normals = point_cloud.copy_data("normals")

assert normals.shape == (point_cloud.height, point_cloud.width, 3)
assert normals.dtype == np.float32

normals_flat = normals.reshape(normals.shape[0] * normals.shape[1], 3)
non_nan_normals = normals_flat[~np.isnan(normals_flat[:, 0])]
vector_lengths = np.linalg.norm(non_nan_normals, axis=1)
np.testing.assert_array_almost_equal(vector_lengths, 1.0)


def test_point_cloud_snr(point_cloud):
import numpy as np

Expand Down

0 comments on commit e21effd

Please sign in to comment.