Utility

vector.py

 1# ----------------------------------------------------------------------------
 2# -                        Open3D: www.open3d.org                            -
 3# ----------------------------------------------------------------------------
 4# The MIT License (MIT)
 5#
 6# Copyright (c) 2018-2021 www.open3d.org
 7#
 8# Permission is hereby granted, free of charge, to any person obtaining a copy
 9# of this software and associated documentation files (the "Software"), to deal
10# in the Software without restriction, including without limitation the rights
11# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12# copies of the Software, and to permit persons to whom the Software is
13# furnished to do so, subject to the following conditions:
14#
15# The above copyright notice and this permission notice shall be included in
16# all copies or substantial portions of the Software.
17#
18# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
23# FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
24# IN THE SOFTWARE.
25# ----------------------------------------------------------------------------
26
27import copy
28import numpy as np
29import open3d as o3d
30
31if __name__ == "__main__":
32
33    print("Testing vector in open3d ...")
34
35    print("")
36    print("Testing o3d.utility.IntVector ...")
37    vi = o3d.utility.IntVector([1, 2, 3, 4, 5])  # made from python list
38    vi1 = o3d.utility.IntVector(np.asarray(
39        [1, 2, 3, 4, 5], dtype=np.int32))  # made from numpy array
40    vi2 = copy.copy(vi)  # valid copy
41    vi3 = copy.deepcopy(vi)  # valid copy
42    vi4 = vi[:]  # valid copy
43    print(vi)
44    print(np.asarray(vi))
45    vi[0] = 10
46    np.asarray(vi)[1] = 22
47    vi1[0] *= 5
48    vi2[0] += 1
49    vi3[0:2] = o3d.utility.IntVector([40, 50])
50    print(vi)
51    print(vi1)
52    print(vi2)
53    print(vi3)
54    print(vi4)
55
56    print("")
57    print("Testing o3d.utility.DoubleVector ...")
58    vd = o3d.utility.DoubleVector([1, 2, 3])
59    vd1 = o3d.utility.DoubleVector([1.1, 1.2])
60    vd2 = o3d.utility.DoubleVector(np.asarray([0.1, 0.2]))
61    print(vd)
62    print(vd1)
63    print(vd2)
64    vd1.append(1.3)
65    vd1.extend(vd2)
66    print(vd1)
67
68    print("")
69    print("Testing o3d.utility.Vector3dVector ...")
70    vv3d = o3d.utility.Vector3dVector([[1, 2, 3], [0.1, 0.2, 0.3]])
71    vv3d1 = o3d.utility.Vector3dVector(vv3d)
72    vv3d2 = o3d.utility.Vector3dVector(np.asarray(vv3d))
73    vv3d3 = copy.deepcopy(vv3d)
74    print(vv3d)
75    print(np.asarray(vv3d))
76    vv3d[0] = [4, 5, 6]
77    print(np.asarray(vv3d))
78    # bad practice, the second [] will not support slice
79    vv3d[0][0] = -1
80    print(np.asarray(vv3d))
81    # good practice, use [] after converting to numpy.array
82    np.asarray(vv3d)[0][0] = 0
83    print(np.asarray(vv3d))
84    np.asarray(vv3d1)[:2, :2] = [[10, 11], [12, 13]]
85    print(np.asarray(vv3d1))
86    vv3d2.append([30, 31, 32])
87    print(np.asarray(vv3d2))
88    vv3d3.extend(vv3d)
89    print(np.asarray(vv3d3))
90
91    print("")
92    print("Testing o3d.utility.Vector3iVector ...")
93    vv3i = o3d.utility.Vector3iVector([[1, 2, 3], [4, 5, 6]])
94    print(vv3i)
95    print(np.asarray(vv3i))
96
97    print("")