def test_constructor_and_accessors(device): dtype = o3c.Dtype.Float32 # Constructor. pcd = o3d.tgeometry.PointCloud(dtype, device) assert "points" in pcd.point assert "colors" not in pcd.point assert isinstance(pcd.point, o3d.tgeometry.TensorListMap) assert isinstance(pcd.point["points"], o3c.TensorList) # Assignment. pcd.point["points"] = o3c.TensorList(o3c.SizeVector([3]), dtype, device) pcd.point["colors"] = o3c.TensorList(o3c.SizeVector([3]), dtype, device) assert len(pcd.point["points"]) == 0 assert len(pcd.point["colors"]) == 0 one_point = o3c.Tensor.ones((3, ), dtype, device) one_color = o3c.Tensor.ones((3, ), dtype, device) pcd.point["points"].push_back(one_point) pcd.point["colors"].push_back(one_color) assert len(pcd.point["points"]) == 1 assert len(pcd.point["colors"]) == 1 # Edit and access values. pcd.point["points"][0] = o3c.Tensor([1, 2, 3], dtype, device) assert pcd.point["points"][0].allclose(o3c.Tensor([1, 2, 3], dtype, device)) # Pushback. pcd.synchronized_push_back({"points": one_point, "colors": one_color}) assert len(pcd.point["points"]) == 2 assert len(pcd.point["colors"]) == 2
def test_tensorlist_create(device): dtype = o3c.Dtype.Float32 # Construct uninitialized tensorlist. tl = o3c.TensorList(o3c.SizeVector([3, 4]), dtype, device) assert tl.size == 0 assert tl.element_shape == o3c.SizeVector([3, 4]) tl = o3c.TensorList(o3c.SizeVector([3, 4]), dtype, device=device) assert tl.size == 0 assert tl.element_shape == o3c.SizeVector([3, 4]) tl = o3c.TensorList(o3c.SizeVector([3, 4]), dtype=dtype, device=device) assert tl.size == 0 assert tl.element_shape == o3c.SizeVector([3, 4]) tl = o3c.TensorList(element_shape=o3c.SizeVector([3, 4]), dtype=dtype, device=device) assert tl.size == 0 assert tl.element_shape == o3c.SizeVector([3, 4]) # Construct from a list of tensors. t0 = o3c.Tensor.ones((2, 3), dtype, device) * 0 t1 = o3c.Tensor.ones((2, 3), dtype, device) * 1 t2 = o3c.Tensor.ones((2, 3), dtype, device) * 2 tl = o3c.TensorList([t0, t1, t2]) assert tl[0].allclose(t0) assert tl[1].allclose(t1) assert tl[2].allclose(t2) assert tl[-1].allclose(t2) assert not tl[0].issame(t0) assert not tl[1].issame(t1) assert not tl[2].issame(t2) assert not tl[-1].issame(t2) # Create from a internal tensor. t = o3c.Tensor.ones((4, 2, 3), dtype, device) tl = o3c.TensorList.from_tensor(o3c.Tensor.ones((4, 2, 3), dtype, device)) assert tl.element_shape == o3c.SizeVector([2, 3]) assert tl.size == 4 assert tl.dtype == dtype assert tl.device == device assert tl.is_resizable assert not tl.as_tensor().issame(t) # Create from a internal tensor, in-place. t = o3c.Tensor.ones((4, 2, 3), dtype, device) tl = o3c.TensorList.from_tensor(t, inplace=True) assert tl.element_shape == o3c.SizeVector([2, 3]) assert tl.size == 4 assert tl.dtype == dtype assert tl.device == device assert not tl.is_resizable assert tl.as_tensor().issame(t)
def test_tensorlistmap(device): dtype = o3c.Dtype.Float32 # Constructor. tlm = o3d.tgeometry.TensorListMap("points") # Get primary key(). assert tlm.get_primary_key() == "points" # Map member access, assignment and "contains" check. This should be the # preferrred way to construct a TensorListMap with values in python. points = o3c.TensorList(o3c.SizeVector([3]), dtype, device) colors = o3c.TensorList(o3c.SizeVector([3]), dtype, device) tlm = o3d.tgeometry.TensorListMap("points") assert "points" not in tlm tlm["points"] = points assert "points" in tlm assert "colors" not in tlm tlm["colors"] = colors assert "colors" in tlm # Constructor with tl values. tlm = o3d.tgeometry.TensorListMap("points", { "points": points, "colors": colors }) # Syncronized pushback. one_point = o3c.Tensor.ones((3,), dtype, device) one_color = o3c.Tensor.ones((3,), dtype, device) tlm.synchronized_push_back({"points": one_point, "colors": one_color}) tlm["points"].push_back(one_point) assert not tlm.is_size_synchronized() with pytest.raises(RuntimeError): tlm.assert_size_synchronized() with pytest.raises(RuntimeError): tlm.synchronized_push_back({"points": one_point, "colors": one_color})
def test_tensorlist_operation(device): dtype = o3c.Dtype.Float32 t0 = o3c.Tensor.ones((2, 3), dtype, device) * 0 t1 = o3c.Tensor.ones((2, 3), dtype, device) * 1 t2 = o3c.Tensor.ones((2, 3), dtype, device) * 2 # push_back tl = o3c.TensorList(o3c.SizeVector([2, 3]), dtype, device) assert tl.size == 0 tl.push_back(t0) assert tl.size == 1 # resize tl.resize(10) assert tl.size == 10 tl.resize(1) assert tl.size == 1 # extend tl = o3c.TensorList(o3c.SizeVector([2, 3]), dtype, device) tl.push_back(t0) tl_other = o3c.TensorList(o3c.SizeVector([2, 3]), dtype, device) tl_other.push_back(t1) tl_other.push_back(t2) tl.extend(tl_other) assert tl.size == 3 assert tl[0].allclose(t0) assert tl[1].allclose(t1) assert tl[2].allclose(t2) # += tl = o3c.TensorList(o3c.SizeVector([2, 3]), dtype, device) tl.push_back(t0) tl_other = o3c.TensorList(o3c.SizeVector([2, 3]), dtype, device) tl_other.push_back(t1) tl_other.push_back(t2) tl += tl_other assert tl.size == 3 assert tl[0].allclose(t0) assert tl[1].allclose(t1) assert tl[2].allclose(t2) # concat tl = o3c.TensorList(o3c.SizeVector([2, 3]), dtype, device) tl.push_back(t0) tl_other = o3c.TensorList(o3c.SizeVector([2, 3]), dtype, device) tl_other.push_back(t1) tl_combined = o3c.TensorList.concat(tl, tl_other) assert tl_combined.size == 2 assert tl_combined[0].allclose(t0) assert tl_combined[1].allclose(t1) # + tl = o3c.TensorList(o3c.SizeVector([2, 3]), dtype, device) tl.push_back(t0) tl_other = o3c.TensorList(o3c.SizeVector([2, 3]), dtype, device) tl_other.push_back(t1) tl_combined = tl + tl_other assert tl_combined.size == 2 assert tl_combined[0].allclose(t0) assert tl_combined[1].allclose(t1)