Three-dimension double-precision vector implementation

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


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.

  • 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)
>>> v2 = v.clone()
>>> id(v2) == id(v)
>>> v2.equals(v)

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

>>> print(v.x)

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])

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)


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}\)


Returns the squared length of this vector


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))

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))

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 the vector.


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]
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)
>>> v1.epsilonEquals(v2, 0.01)

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.

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

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


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

>>> v.x
>>> v.y
>>> v.z