5 Represents a three-dimensional vector with the entries being real numbers.
8 def __init__(self, x, y = None, z = None):
13 Throws if the types of the arguments do not match the
17 This parameter may be an instance of `Vector3DReal`, in which
18 case this instance will be constructed to contain the same
19 elements as `x`, and it will be required that `y` and `z` be
21 Alternatively, `x` may be a list or tuple of length `3`, with
22 each element being either an `int` or a `float`; the element
23 `0` will be taken as the `x` value for this vector, the
24 element `1` as `y`, and the element `2` as `z`. In this case,
25 too, `y` and `z` must be `None`.
26 Alternatively, `x` may be an `int` or `float` and will serve
27 as the `x` value for this instance. In this case, `y` and `z`
30 Either `None` or the `y` value for this vector; see the
33 Either `None` or the `z` value for this vector; see the
37 if isinstance(x, Vector3DReal):
38 if y
is not None or z
is not None:
46 allowedTypes = (int, float)
48 if isinstance(x, (list, tuple)):
49 if y
is not None or z
is not None:
56 if not isinstance(x[i], allowedTypes):
66 if not isinstance(var, allowedTypes):
76 Returns the `x` value.
84 Returns the `y` value.
92 Returns the `z` value.
100 Returns the dot product of this vector with `rhs`.
103 The right-hand-side instance, which must be of type
109 return self.
x * rhs.x + self.
y * rhs.y + self.
z * rhs.z
112 def cross(self, rhs):
114 Returns the cross product of this vector with `rhs`.
117 The right-hand-side instance, which must be of type
123 cx = self.
y * rhs.z - self.
z * rhs.y;
124 cy = self.
z * rhs.x - self.
x * rhs.z;
125 cz = self.
x * rhs.y - self.
y * rhs.x;
131 Returns the square of the length of this vector.
134 return self.
dot(self)
139 Returns the length of this vector.
147 Returns this vector, but normalized, i.e. divided by its length.
150 Throws if `getLengthSquared() == 0`.
161 Normalizes this vector, i.e. divides it by its length.
164 Throws if `getLengthSquared() == 0`.
171 self.
x = self.
x * factor
172 self.
y = self.
y * factor
173 self.
z = self.
z * factor
178 Returns the projection of this vector onto the direction `rhs`.
181 Throws if `rhs.getLengthSquared() == 0`.
184 The right-hand-side instance, which must be of type
185 `Vector3DReal` and must not be of zero length.
190 if rhs.getLengthSquared() == 0:
193 normalized = rhs.getNormalized()
194 return normalized * normalized.dot(self)
199 Returns the component of this vector that is perpendicular to `rhs`.
202 Throws if `rhs.getLengthSquared() == 0`.
205 The right-hand-side instance, which must be of type
206 `Vector3DReal` and must not be of zero length.
211 if rhs.getLengthSquared() == 0:
219 Returns the this vector, rotated around the normalized vector `axis` by
220 `angle` radians in counter-clockwise direction.
223 Throws if `axis.getLengthSquared() == 0`.
226 The vector to rotate about, which must be of type
227 `Vector3DReal` and is assumed to be of unit length.
229 The amount to rotate, in radians.
234 if axis.getLengthSquared() == 0:
237 thisDotAxis = self.
dot(axis)
238 axisCrossThis = axis.cross(self)
239 projectionOntoAxis = axis * thisDotAxis
241 projectionOntoAxis + \
242 (self -projectionOntoAxis) * math.cos(angle) + \
243 axisCrossThis * math.sin(angle)
248 Sets this vector to be the result of `getRotatedAroundNormalizedAxis`,
249 called with the given arguments.
252 Throws if `axis.getLengthSquared() == 0`.
255 The vector to rotate about, which must be of type
256 `Vector3DReal` and is assumed to be of unit length.
258 The amount to rotate, in radians.
269 def isClose(self, rhs, relativeTolerance = None, absoluteTolerance = None):
271 Returns whether this instance and `rhs` are close to each other.
273 Two vectors are "close" in this sense if all of their components are
274 close in the sense of `numpy.allclose`.
277 Throws if any of the arguments have an invalid type.
279 Throws if any of the arguments have an invalid value.
282 The other instance of `Vector3DReal` to compare to.
283 @param[in] relativeTolerance
284 A non-negative `float` that is passed as the `rtol` parameter
285 of `numpy.allclose`, or `None` for its default value.
286 @param[in] absoluteTolerance
287 A non-negative `float` that is passed as the `atol` parameter
288 of `numpy.allclose`, or `None` for its default value.
293 for x
in [relativeTolerance, absoluteTolerance]:
294 if not isinstance(x, (float, type(
None))):
296 if x
is not None and x < 0:
300 a = [self.
x, self.
y, self.
z]
301 b = [rhs.x, rhs.y, rhs.z]
303 if relativeTolerance
is not None:
304 kwargs[
"rtol"] = relativeTolerance
305 if absoluteTolerance
is not None:
306 kwargs[
"atol"] = absoluteTolerance
309 return numpy.allclose(a, b, **kwargs)
314 Returns `True` if `rhs` contains the same entries as this instance, and
318 The right-hand-side instance, which must be of type
335 Returns the negation of `__eq__`.
338 The right-hand-side instance, which must be of type
342 return not self.
__eq__(rhs)
347 Returns the sum of this instance and `rhs`.
350 The right-hand-side instance, which must be of type
361 Returns the difference of this instance and `rhs`.
364 The right-hand-side instance, which must be of type
375 Returns the product of this instance and `rhs`.
378 The factor to multiply with, which must be either of type
382 allowedTypes = (int, float)
383 if not isinstance(rhs, allowedTypes):
391 Returns the ratio of this instance and `rhs`.
393 The division performed is always a floating-point division.
396 The factor to divide by with, which must be either of type
397 `int` or `float`, and must not be `0`.
405 Returns the ratio of this instance and `rhs`.
407 The division performed is always a floating-point division.
410 The factor to divide by with, which must be either of type
411 `int` or `float`, and must not be `0`.
414 allowedTypes = (int, float)
415 if not isinstance(rhs, allowedTypes):
421 if isinstance(rhs, int):
429 Returns the negative of this vector.
437 Returns the coordinate with the given index.
440 The coordinate index, which must be `0` for the `x`
441 coordinate, `1` for `y`, or `2` for `z`.
458 Returns a string representation of this instance.
461 return "(" + str(self.
x) +
", " + str(self.
y) +
", " + str(self.
z) +
")"
464 def _assertIsSameType(self, value):
466 Throws `TypeError` if `value` is not of type `Vector3DReal`.
472 if not isinstance(value, Vector3DReal):