irfpy.util.vector3d

Three-dimension double-precision vector implementation

This is an implementation of Vector3d class. API is refered to Java3D.

Note

The module is left for compatibility (some other modules depend on this implementation). For developing new module, there are no reasons of using this module. Use numpy.array for normal purpose for vector manipulations

Code author: Yoshifumi Futaana

class irfpy.util.vector3d.Vector3d(x=0, y=0, z=0)[source]

Bases: object

Double precision of 3-d vector, constituting of x, y, and z.

Parameters:
  • x (float) – x component of the vector.

  • y (float) – y component of the vector.

  • z (float) – z component of the vector.

Simple to instance:

>>> v = Vector3d(5, 1, 2)
>>> print(v)
Vector3d( 5, 1, 2 )

Only float is acceptable as arguments. The clone() method deep-copies the vector. Substite is reference copy.

>>> v1 = v
>>> id(v1) == id(v)
True
>>> v2 = v.clone()
>>> id(v2) == id(v)
False
>>> v2.equals(v)
True

Based on Java3D, no accessor methods are available. Thus, cccessing the element is using .x, .y, and .z

>>> print(v.x)
5

Apart from Java3D, you can also get by index 0, 1, and 2. This enable to use this class as array-like.

>>> print(v[2])
2

But not that this index-based access is slower than the direct access by a factor of 2.

You can change the value by accessing .x, .y and .z

>>> v.y = -1
>>> print(v)
Vector3d( 5, -1, 2 )

This means that the Vector3d class is not immutable. Thus, this instance cannot be used as a key of a dictionary.

Comparison is not defined. __eq__(), __ne__(), __cmp__(), or any other comparator is not implemented. equals() and epsilonEquals() support checking equality by each component.

The API is based on Java3D API, thus methods are not intuitive. For example, operaters like \(v_1+v_2\) are not supported. This isa Java3D policy: Method should not instance, but user should instance by himself.

Constructor specifying all the argument. Deafult value is (0,0,0)

length()[source]

Returns the length of this vector.

The length of the vector \(\mathbf{v}=(v_x, v_y, v_z)\) is \(\sqrt{v_x^2+v_y^2+v_z^2}\)

lengthSquared()[source]

Returns the squared length of this vector

angle(v1)[source]

Returns the angle in radians between self vector and given vector

>>> v1 = Vector3d(1, 0, 0)
>>> v2 = Vector3d(0, 1, 0)
>>> print('%.1f' % (v1.angle(v2) * 180. / pi))
90.0

Note that there happens a rarely numerical effect that exceeds the correct range of calculation. For example, the following calculation will make “ValueError” during cosine operation, unless specially treated.

>>> x = 0.939856076502615267465046144935
>>> y = 0.105396307433879829473788447558
>>> z = 0.324903329992804690284913249343
>>> v = Vector3d(x, y, z)
>>> v1 = Vector3d(x, y, z)
>>> print(v.angle(v1))
0.0
dot(v1)[source]

Returns the inner product of this vector and the given vector

cross(v1, v2)[source]

Sets this vector to the outer product of the given vectors

normalize(v1=None)[source]

Normalize the vector.

absolute(v1=None)[source]

Set the each component of this vector to its aboslute values

add(v1, v2=None)[source]

Sets the value of this vector to the sum of v1 and v2 (or self)

sub(v1, v2=None)[source]

Substitute vectors.

Set the self vector to be self=self-v1 if v2 is omitted. Otherwise self=v1-v2 is set.

clamp(min, max)[source]

Clamps the vector to the range [low,high].

clampMax(max, v1=None)[source]
clampMin(min, v1=None)[source]
clone()[source]
epsilonEquals(v1, epsilon)[source]

Check whether the given vector is very close to self.

Returns True if L-infinite distance between this vector and v1 is less than epsilon.

>>> v1 = Vector3d(1.50, 2.80, 3.63)
>>> v2 = Vector3d(1.51, 2.82, 3.64)
>>> v1.epsilonEquals(v2, 0.1)
True
>>> v1.epsilonEquals(v2, 0.01)
False
equals(v1)[source]
getTuple()[source]

Returns the three-element python tuple.

interpolate(alpha, v1, v2=None)[source]

Returns the interplated vector.

Returns (1-alpha)*v1 + alpha*v2 or (1-alpha)*self+alpha*v1

Take care, this is different order of argument from Java3D API.

negate(v1=None)[source]
>>> v = Vector3d(1, 2, 3)
>>> v.negate()
>>> print(v.x)
-1
scale(s, v1=None)[source]
scaleAdd(s, v1, v2=None)[source]

self = self*s+v1 (v2==None) or self = v1*s+v2 (v2!=None)

setTuple(tuple)[source]

Set a tuple to self. >>> v = Vector3d() >>> id0 = id(v) >>> t = [1, 3, 5] >>> v.setTuple(t) >>> id0 == id(v) True

>>> v.x
1
>>> v.y
3
>>> v.z
5
irfpy.util.vector3d.doctests()[source]