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.02147685, 0.66841416, 0.56094018, 0.75993634], [0.86332481, 0.11262973, 0.05422323, 0.90722808], [0.28295801, 0.13307141, 0.2834161 , 0.14186734]], [[0.70829906, 0.32456051, 0.98700564, 0.8064061 ], [0.71054146, 0.79704685, 0.58694827, 0.58062739], [0.53516295, 0.50215316, 0.61318775, 0.59049827]]])
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.]]), 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]: