1# ----------------------------------------------------------------------------
2# - Open3D: www.open3d.org -
3# ----------------------------------------------------------------------------
4# Copyright (c) 2018-2024 www.open3d.org
5# SPDX-License-Identifier: MIT
6# ----------------------------------------------------------------------------
7
8import copy
9import numpy as np
10import open3d as o3d
11
12if __name__ == "__main__":
13
14 print("Testing vector in open3d ...")
15
16 print("")
17 print("Testing o3d.utility.IntVector ...")
18 vi = o3d.utility.IntVector([1, 2, 3, 4, 5]) # made from python list
19 vi1 = o3d.utility.IntVector(np.asarray(
20 [1, 2, 3, 4, 5], dtype=np.int32)) # made from numpy array
21 vi2 = copy.copy(vi) # valid copy
22 vi3 = copy.deepcopy(vi) # valid copy
23 vi4 = vi[:] # valid copy
24 print(vi)
25 print(np.asarray(vi))
26 vi[0] = 10
27 np.asarray(vi)[1] = 22
28 vi1[0] *= 5
29 vi2[0] += 1
30 vi3[0:2] = o3d.utility.IntVector([40, 50])
31 print(vi)
32 print(vi1)
33 print(vi2)
34 print(vi3)
35 print(vi4)
36
37 print("")
38 print("Testing o3d.utility.DoubleVector ...")
39 vd = o3d.utility.DoubleVector([1, 2, 3])
40 vd1 = o3d.utility.DoubleVector([1.1, 1.2])
41 vd2 = o3d.utility.DoubleVector(np.asarray([0.1, 0.2]))
42 print(vd)
43 print(vd1)
44 print(vd2)
45 vd1.append(1.3)
46 vd1.extend(vd2)
47 print(vd1)
48
49 print("")
50 print("Testing o3d.utility.Vector3dVector ...")
51 vv3d = o3d.utility.Vector3dVector([[1, 2, 3], [0.1, 0.2, 0.3]])
52 vv3d1 = o3d.utility.Vector3dVector(vv3d)
53 vv3d2 = o3d.utility.Vector3dVector(np.asarray(vv3d))
54 vv3d3 = copy.deepcopy(vv3d)
55 print(vv3d)
56 print(np.asarray(vv3d))
57 vv3d[0] = [4, 5, 6]
58 print(np.asarray(vv3d))
59 # bad practice, the second [] will not support slice
60 vv3d[0][0] = -1
61 print(np.asarray(vv3d))
62 # good practice, use [] after converting to numpy.array
63 np.asarray(vv3d)[0][0] = 0
64 print(np.asarray(vv3d))
65 np.asarray(vv3d1)[:2, :2] = [[10, 11], [12, 13]]
66 print(np.asarray(vv3d1))
67 vv3d2.append([30, 31, 32])
68 print(np.asarray(vv3d2))
69 vv3d3.extend(vv3d)
70 print(np.asarray(vv3d3))
71
72 print("")
73 print("Testing o3d.utility.Vector3iVector ...")
74 vv3i = o3d.utility.Vector3iVector([[1, 2, 3], [4, 5, 6]])
75 print(vv3i)
76 print(np.asarray(vv3i))
77
78 print("")