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()
andepsilonEquals()
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}\)
- 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
- 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.
- 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