Vector3¶
represents point or position, velocity and scale. Note! Angular velocity can not be represented by this type, it should be represented by Transform. It is a class inheriting numpy.ndarray, so it is also ndarray.
In [1]:
import py3d
py3d.Vector3()
Out[1]:
Vector3([0., 0., 0.])
In [3]:
py3d.Vector3(x=1.2)
Out[3]:
Vector3([1.2, 0. , 0. ])
In [5]:
py3d.Vector3(x=1, y=3, z=-2.4)
Out[5]:
Vector3([ 1. , 3. , -2.4])
Creat a Vector3 from a xy list
In [7]:
import py3d
py3d.Vector3([
[1, 2],
[3, 4]
])
Out[7]:
Vector3([[1., 2., 0.], [3., 4., 0.]])
Repeat a given vector
In [8]:
import py3d
py3d.Vector3(x=1, y=3, z=-2.4).tile(2)
Out[8]:
Vector3([[ 1. , 3. , -2.4], [ 1. , 3. , -2.4]])
In [10]:
import py3d
p = py3d.Vector3(x=range(3), y=range(2, 5)).tile(2, 3)
p
Out[10]:
Vector3([[[[0., 2., 0.], [1., 3., 0.], [2., 4., 0.]], [[0., 2., 0.], [1., 3., 0.], [2., 4., 0.]], [[0., 2., 0.], [1., 3., 0.], [2., 4., 0.]]], [[[0., 2., 0.], [1., 3., 0.], [2., 4., 0.]], [[0., 2., 0.], [1., 3., 0.], [2., 4., 0.]], [[0., 2., 0.], [1., 3., 0.], [2., 4., 0.]]]])
In [12]:
p.x
Out[12]:
Vector([[[0., 1., 2.], [0., 1., 2.], [0., 1., 2.]], [[0., 1., 2.], [0., 1., 2.], [0., 1., 2.]]])
Get first two columns of a Vector3 list
In [13]:
p.xy
Out[13]:
Vector2([[[[0., 2.], [1., 3.], [2., 4.]], [[0., 2.], [1., 3.], [2., 4.]], [[0., 2.], [1., 3.], [2., 4.]]], [[[0., 2.], [1., 3.], [2., 4.]], [[0., 2.], [1., 3.], [2., 4.]], [[0., 2.], [1., 3.], [2., 4.]]]])
In [14]:
import py3d
py3d.Vector3(y=[[1, 2], [2, 3]], x=[2, 0.1])
Out[14]:
Vector3([[[2. , 1. , 0. ], [0.1, 2. , 0. ]], [[2. , 2. , 0. ], [0.1, 3. , 0. ]]])
Grid¶
In [15]:
import py3d
py3d.Vector3.grid([-.5, .5], [-.5, .5], [-.5, .5])
Out[15]:
Vector3([[[[-0.5, -0.5, -0.5], [-0.5, -0.5, 0.5]], [[-0.5, 0.5, -0.5], [-0.5, 0.5, 0.5]]], [[[ 0.5, -0.5, -0.5], [ 0.5, -0.5, 0.5]], [[ 0.5, 0.5, -0.5], [ 0.5, 0.5, 0.5]]]])
In [16]:
import py3d
py3d.Vector3.grid(range(-5, 5), range(-5, 5), range(-5, 5)).as_point()
Out[16]:
Unit Vector¶
In [17]:
import py3d
py3d.Vector3().U
Out[17]:
Vector3([0., 0., 0.])
Get length¶
In [19]:
py3d.Vector3([1, 2, 3]).L
Out[19]:
Vector3(3.74165739)
In [20]:
py3d.Vector3([
[1, 2, 3],
[4, 5, 6]]).L
Out[20]:
Vector3([3.74165739, 8.77496439])
In [22]:
import py3d
p = py3d.rand(1000, 3)
p[p.L > 0.7].as_point()
Out[22]:
Random Vector¶
In [23]:
import py3d
py3d.rand(3)
Out[23]:
Vector3([0.16889723, 0.26027904, 0.83799991])
Get a random vector list
In [24]:
import py3d
py3d.rand(3, 3)
Out[24]:
Vector3([[0.16549828, 0.17037912, 0.90911318], [0.6483999 , 0.14024019, 0.73990579], [0.53343703, 0.2229884 , 0.14361514]])
Cross product¶
In [26]:
import py3d
py3d.Vector3(x=3).cross(py3d.Vector3(y=2))
Out[26]:
Vector3([0., 0., 6.])
In [28]:
import py3d
py3d.Vector3(x=range(3)).cross(py3d.Vector3(y=range(3)))
Out[28]:
Vector3([[0., 0., 0.], [0., 0., 1.], [0., 0., 4.]])
Dot product¶
Determine if two 2d vectors are perpendicular¶
if two 2d vectors are perpendicular, then their dot product must be zero
In [29]:
import sympy
x, y = sympy.symbols("x y")
a = sympy.Matrix([x, y, 0])
b = sympy.Matrix([-y, x, 0])
a.dot(b)
Out[29]:
$\displaystyle 0$
In [30]:
import py3d
a = py3d.Vector3([1, 2, 0])
b = py3d.Vector3([-2, 1, 0])
a.dot(b)
Out[30]:
Vector(0.)
In [32]:
import py3d
py3d.Vector3(x=range(4)).dot([1, 1, 0])
Out[32]:
Vector([0., 1., 2., 3.])
In [33]:
import py3d
py3d.Vector3(x=range(5), y=range(5)).dot(py3d.Vector3(x=range(5)))
Out[33]:
Vector([ 0., 1., 4., 9., 16.])
Cross Product¶
In [34]:
import py3d
py3d.Vector3(y=range(4)).cross([1, 0, 0])
Out[34]:
Vector3([[ 0., 0., 0.], [ 0., 0., -1.], [ 0., 0., -2.], [ 0., 0., -3.]])
Projection¶
Scalar Projection¶
In [35]:
import py3d
py3d.Vector3(x=range(5), y=range(5)).scalar_projection([1, 0, 0])
Out[35]:
Vector([0., 1., 2., 3., 4.])
In [36]:
import py3d
py3d.Vector3(x=range(1, 6), y=range(5)).scalar_projection(
py3d.Vector3(x=range(1, 6)))
Out[36]:
Vector([1., 2., 3., 4., 5.])
Vector Projection¶
In [37]:
import py3d
py3d.Vector3(x=range(5), y=range(5)).vector_projection([1, 0, 0])
Out[37]:
Vector([[0., 0., 0.], [1., 0., 0.], [2., 0., 0.], [3., 0., 0.], [4., 0., 0.]])
In [38]:
import py3d
py3d.Vector3(x=range(5), y=range(5)).vector_projection(
py3d.Vector3(x=range(1, 6)))
Out[38]:
Vector([[0., 0., 0.], [1., 0., 0.], [2., 0., 0.], [3., 0., 0.], [4., 0., 0.]])
Interpolation¶
In [39]:
import py3d
v = py3d.Vector3([
[0,0,0],
[3,4,8],
[6,13,9]
])
py3d.render(v.as_line())
i = v.lerp([0.5, 1.6, 3.1, 5.2], [0,2,4])
py3d.render(i.as_point(py3d.Color(r=1)))
Out[39]:
Distances¶
Distance to points¶
In [40]:
import py3d
import numpy
p0=py3d.Vector3([[0.9, 0.2, 0.4],
[0. , 0.4, 0.1],
[0.4, 0.8, 0.7],
[0.2, 0.5, 0.1],
[0.4, 0.7, 0.7]])
p1=[[0.4, 0.8, 0.7],
[0.5, 0.4, 0.9],
[0.4, 0.9, 0.6],
[0.3, 0.3, 0.3]]
p0.distance_to_points(p1)
Out[40]:
Vector3(0.27812143)
Visualization¶
Points¶
In [41]:
import py3d
points = (py3d.rand(1000, 3)-0.5).U * 2
points.as_point()
Out[41]:
Line¶
In [43]:
import py3d
import numpy
p = py3d.Vector3().tile(2, 10)
p.x = numpy.linspace(0, 11, 10)
p.y = numpy.sin(p.x)
p.z = [[0], [1]]
p.as_line()
Out[43]:
Polygon¶
In [44]:
import py3d
py3d.Vector3([
[5, -1, 0],
[5, 1, 0],
[-1, 1, 0],
[-1, -1, 0]
]).as_lineloop()
Out[44]:
In [45]:
import py3d
red_rect = py3d.Vector3([
[5, -1, 0],
[5, 1, 0],
[-1, 1, 0],
[-1, -1, 0]
])
gray_rect = red_rect+py3d.Vector3(x=1, y=1, z=1)
py3d.render(red_rect.as_shape().paint(py3d.Color(r=1)), gray_rect.as_shape())
Out[45]:
Circle¶
In [46]:
import py3d
py3d.Vector3.circle(2, 10).as_point()
Out[46]:
In [47]:
import py3d
py3d.Vector3.circle(2).as_shape()
Out[47]:
In [48]:
import py3d
import numpy
c = py3d.Vector3.circle(0.2)
c@=py3d.Transform.from_translation(x=1) @ py3d.Transform.from_rpy(
py3d.Vector3(z=numpy.linspace(0, numpy.pi * 2, 10)))
c.as_shape()
Out[48]:
In [49]:
import py3d
c = py3d.Vector3.circle()
c@=py3d.Transform.from_scaling(x=[1, 2, 3], y=[.5, 1.9, 1.2])
c.as_lineloop()
Out[49]:
Line segment¶
In [50]:
import py3d
import numpy
x = numpy.linspace(0, 10)
py3d.Vector3(x=x, y=numpy.sin(x)).as_linesegment()
Out[50]:
Vectors¶
In [51]:
import py3d
import numpy
a = numpy.linspace(0, 2*numpy.pi, 100)
py3d.Vector3(x=numpy.sin(a), y=numpy.cos(a)).as_vector()
Out[51]:
In [52]:
import py3d
l = py3d.Vector3([
[1, 1, 1],
[-2, -3, -1],
[0.4, -3, 2.2]
]).as_vector()
l[0].color = py3d.Color(r=1)
l[1].color = py3d.Color(b=1)
l[2].color = py3d.Color(g=1)
l
Out[52]:
In [53]:
import py3d
p = py3d.Vector3(x=range(20)).as_line()
p.paint(py3d.Color.map(p.x))
Out[53]: