Vector¶

Vector is base class of other vector like classes. It represents a series of vectors.

In [1]:
from py3d import Vector
Vector()
Out[1]:
Vector([], dtype=float64)
In [3]:
Vector([0, 0, 0, 0])
Out[3]:
Vector([0, 0, 0, 0])
In [5]:
Vector([1, 2, -4])
Out[5]:
Vector([ 1,  2, -4])
In [7]:
Vector([1, 2, 3]).tile(2)
Out[7]:
Vector([[1, 2, 3],
        [1, 2, 3]])
In [9]:
import py3d
py3d.Vector([1, 2, 0, 3])
Out[9]:
Vector([1, 2, 0, 3])
In [10]:
Vector([1]).tile(2)
Out[10]:
Vector([[1],
        [1]])

Get unit vector

In [12]:
import py3d
V32 = py3d.Vector([
    [1, 3],
    [2, -9],
    [0.1, -3]])
V32.U
Out[12]:
Vector([[ 0.31622777,  0.9486833 ],
        [ 0.21693046, -0.97618706],
        [ 0.03331483, -0.99944491]])

Get length of vectors

In [13]:
V32.L
Out[13]:
Vector([3.16227766, 9.21954446, 3.0016662 ])
In [15]:
from numpy import allclose
assert allclose(V32.U.L, [[1], [1], [1]])

Get homogeneous vector

In [16]:
V32.H
Out[16]:
Vector3([[ 1. ,  3. ,  1. ],
         [ 2. , -9. ,  1. ],
         [ 0.1, -3. ,  1. ]])

Get a series of random vectors

In [18]:
import py3d
py3d.rand(2, 3, 4)
Out[18]:
Vector4([[[0.63049818, 0.07885864, 0.69649669, 0.6329456 ],
          [0.05276102, 0.87389273, 0.66235171, 0.84740299],
          [0.4773944 , 0.72964287, 0.73584425, 0.62662506]],

         [[0.55094595, 0.34614607, 0.57262189, 0.0325003 ],
          [0.55465733, 0.6729481 , 0.2350295 , 0.14372498],
          [0.14530642, 0.66796871, 0.07732719, 0.93671273]]])
In [20]:
import py3d
py3d.Vector([[0, 0], [1, 2], [3, 4]]).diff()
Out[20]:
Vector([[1, 2],
        [2, 2]])

Get a flattened copy of a vector

In [22]:
import py3d
py3d.Vector([[[1,2,3]],[[4,5,6]]]).flatten()
Out[22]:
Vector([[1, 2, 3],
        [4, 5, 6]])

Fill nan elements of a Vector

In [24]:
import py3d
py3d.Vector([0, 1, float("nan")]).fillna(-1)
Out[24]:
Vector([ 0.,  1., -1.])

Drop nan elements of a Vector

In [25]:
import py3d
py3d.Vector([0, 1, float("nan")]).dropna()
Out[25]:
Vector([0., 1.])
In [26]:
import py3d
v = py3d.Vector([
    [
        [0.3, 1, float("nan")],
        [-3, 8, 5],
        [0, 2, -2],
        [float("nan"), 0.2, 9]
    ], [
        [3, 1, 5],
        [-3, 8, 5],
        [0, 6.6, -7],
        [8, 31, -4]
    ]
])
v.dropna(),v.dropna(0),v.dropna(1),v.dropna(2)
Out[26]:
(Vector([ 0.3,  1. , -3. ,  8. ,  5. ,  0. ,  2. , -2. ,  0.2,  9. ,  3. ,  1. ,  5. , -3. ,  8. ,  5. ,  0. ,  6.6, -7. ,  8. , 31. , -4. ]),
 Vector([[[ 3. ,  1. ,  5. ],
          [-3. ,  8. ,  5. ],
          [ 0. ,  6.6, -7. ],
          [ 8. , 31. , -4. ]]]),
 Vector([[[-3. ,  8. ,  5. ],
          [ 0. ,  2. , -2. ]],
 
         [[-3. ,  8. ,  5. ],
          [ 0. ,  6.6, -7. ]]]),
 Vector([[[ 1. ],
          [ 8. ],
          [ 2. ],
          [ 0.2]],
 
         [[ 1. ],
          [ 8. ],
          [ 6.6],
          [31. ]]]))
In [27]:
v.xyz.as_point(fillna_val=90)
Out[27]:

Drop duplicates

In [28]:
import py3d
py3d.Vector([
    [1,2,1],
    [2,3,4],
    [1,2,1],
    [5,3,4]
]).unique()
Out[28]:
Vector([[1, 2, 1],
        [2, 3, 4],
        [5, 3, 4]])

Read an image

In [29]:
import py3d
py3d.read_img("./20220917214012.jpg").as_image()
Out[29]:

Read a txt file storing poses

In [30]:
import py3d
poses = py3d.read_txt("poses.txt")
poses
Out[30]:
Vector([[   1.        ,   -0.38009858,   -0.92467821,   -0.02225479,  846.2467971 ,   -0.92381071,    0.38071388,   -0.04038213, 3725.075626  ,    0.04581318,    0.00521002,   -0.99893644,  115.2776042 ],
        [   9.        ,   -0.33478138,   -0.94192187,   -0.02654461,  846.339549  ,   -0.94114833,    0.33563081,   -0.03989722, 3725.29806   ,    0.04648925,    0.01162557,   -0.99885114,  115.2612811 ],
        [  14.        ,   -0.29296799,   -0.95575796,   -0.02639101,  846.4037923 ,   -0.95501915,    0.293844  ,   -0.03992654, 3725.508866  ,    0.04591495,    0.01350672,   -0.99885404,  115.2465809 ],
        [  22.        ,   -0.25582311,   -0.96643387,   -0.02366668,  846.4521737 ,   -0.96565419,    0.25661472,   -0.04075369, 3725.723345  ,    0.04545896,    0.0124281 ,   -0.9988889 ,  115.2352558 ],
        [  30.        ,   -0.22198089,   -0.97488512,   -0.01798596,  846.4980023 ,   -0.97409612,    0.22254193,   -0.04014767, 3725.955861  ,    0.043142  ,    0.00860804,   -0.99903187,  115.2302131 ],
        [  35.        ,   -0.18959113,   -0.98172114,   -0.01669745,  846.5432516 ,   -0.98076473,    0.19015581,   -0.04406021, 3726.191177  ,    0.04642996,    0.00802284,   -0.99888933,  115.2168146 ],
        [  53.        ,   -0.15254459,   -0.98823256,   -0.01124982,  846.5785177 ,   -0.98779343,    0.15282023,   -0.03016809, 3726.412478  ,    0.03153229,    0.00651051,   -0.99948153,  115.2403334 ],
        [  57.        ,   -0.1082222 ,   -0.99412667,   -0.00034593,  846.5941848 ,   -0.99364056,    0.10818015,   -0.03123283, 3726.643638  ,    0.03108681,   -0.00303635,   -0.99951208,  115.2390776 ],
        [  61.        ,   -0.05474059,   -0.99850018,   -0.00092533,  846.621932  ,   -0.99832163,    0.0547483 ,   -0.01888239, 3726.877597  ,    0.01890473,   -0.00010986,   -0.99982128,  115.2613816 ],
        [  65.        ,    0.00232874,   -0.9999955 ,    0.00189192,  846.6288611 ,   -0.99995916,   -0.00234517,   -0.00872806, 3727.13067   ,    0.00873246,   -0.00187151,   -0.99996012,  115.2742539 ]])
In [31]:
poses[:, [4,8,12]].to_txt("traj.txt", delimiter=" ")

Write csv file

In [32]:
import py3d
py3d.Vector([[1,2,3],[4,5,6]], columns=["t","x","y"]).to_csv("with_cols.csv")
py3d.Vector([[7,8.4,9],[-1,-2,-3]]).to_csv("no_cols.csv")

Read csv file

In [33]:
import py3d
a=py3d.read_csv("with_cols.csv", header=1)
a.a("t"), a.a("t","x"), a[1,2]
Out[33]:
(Vector([[1.],
         [4.]]),
 Vector([[1., 2.],
         [4., 5.]]),
 np.float64(6.0))
In [34]:
import py3d
py3d.read_csv("no_cols.csv")
Out[34]:
Vector([[ 7. ,  8.4,  9. ],
        [-1. , -2. , -3. ]])

Read pcd file

In [35]:
import py3d
py3d.read_pcd("ascii.pcd").xyz.as_point(py3d.Color(r=1,g=0.5,a=0.3))
Out[35]:
In [36]:
import py3d
pcd = py3d.read_pcd("binary.pcd")
print("min", pcd.min())
print("max", pcd.max())
pcd.xyz.as_point(colormap=pcd.w)
min -109.0828628540039
max 1223933577.0853775
Out[36]:
In [37]:
pcd.sample(10)
Out[37]:
Vector([[-4.61142397e+00,  3.45122957e+00, -1.81348097e+00,  1.20000000e+02,  1.22393358e+09],
        [ 9.67340565e+00,  1.28780260e+01, -3.32120347e+00,  6.60000000e+01,  1.22393358e+09],
        [-1.67942607e+00, -4.22090197e+00, -1.76985645e+00,  2.01000000e+02,  1.22393358e+09],
        [ 2.49860134e+01,  2.35606122e+00, -8.63356516e-02,  2.02000000e+02,  1.22393358e+09],
        [-3.33231163e+00,  3.08990526e+00, -1.77962291e+00,  1.74000000e+02,  1.22393358e+09],
        [ 4.83001113e-01,  1.33831692e+01, -3.24861121e+00,  2.31000000e+02,  1.22393358e+09],
        [ 4.72589731e+00, -6.77193403e+00, -1.90725017e+00,  1.61000000e+02,  1.22393358e+09],
        [ 6.62870884e-01,  4.55542517e+00, -2.03153372e+00,  2.14000000e+02,  1.22393358e+09],
        [-3.92011595e+00, -5.97642946e+00, -3.06805849e-01,  9.90000000e+01,  1.22393358e+09],
        [-2.56470609e+00, -5.13899994e+00, -1.75545585e+00,  1.13000000e+02,  1.22393358e+09]])

split the Vector into 3 parts

In [38]:
pcd.split([3,4])
Out[38]:
[Vector([[ -9.84686852,  25.20105171,  -2.90541101],
         [-11.15411758,  26.11715317,  -2.88157201],
         [-36.65405273,  72.06262207,   1.20190442],
         ...,
         [-10.16968441,  20.41918373,  -3.34819937],
         [ -5.50718451,  13.51468945,  -3.03626227],
         [ -6.40060711,  13.55139732,  -2.96159911]], shape=(108431, 3)),
 Vector([[164.],
         [176.],
         [215.],
         ...,
         [153.],
         [141.],
         [ 81.]], shape=(108431, 1)),
 Vector([[1.22393358e+09],
         [1.22393358e+09],
         [1.22393358e+09],
         ...,
         [1.22393358e+09],
         [1.22393358e+09],
         [1.22393358e+09]], shape=(108431, 1))]

Write npy file

In [39]:
import py3d
py3d.Vector([1,2,3]).to_npy("tmp.npy")

Read npy file

In [40]:
import py3d
py3d.read_npy("tmp.npy")
Out[40]:
Vector([1, 2, 3])

Read a real lidar point cloud from a npy file

In [41]:
import py3d
py3d.read_npy("lidar.npy").xyz.as_point()
Out[41]:

Read a depth map from a npy file

In [42]:
import py3d
py3d.read_npy("depth.npy").z.as_image()
Out[42]:

Calculate chamfer distance between two points

In [43]:
import py3d
a = py3d.Vector([[0.38902577, 0.97576158, 0.50699271, 1],
                 [0.63387973, 0.58355272, 0.12138496, 0]])
b = py3d.Vector([[0.43179828, 1.0648139, 0.58688312, 1],
                 [0.69850221, 0.67471377, 0.18104768, 1],
                 [0.99474279, 0.20892464, 0.35862561, 2],
                 [0.52789329, 0.81794455, 0.59196469, 0.5],
                 [0.78926438, 0.95307471, 0.31530688, 1]])
print(py3d.chamfer_distance(a, b))
print(py3d.chamfer_distance(a, b, 0.15))
print(py3d.chamfer_distance(a, b, 0.15, True))
print(py3d.chamfer_distance(a, b, 0.15, True, True))
0.8337985950067301
(0.8337985950067301, 0.28571428571428575)
(0.8337985950067301, array([0.12705228, 0.73322434]), array([0.12705228, 0.54096729, 1.40603869, 0.54900878, 0.44435245]), 0.28571428571428575)
(0.8337985950067301, array([0.12705228, 0.73322434]), array([0.12705228, 0.54096729, 1.40603869, 0.54900878, 0.44435245]), 0.28571428571428575, 0.5, 0.2)
In [ ]: