| 1 |
# SPDX-License-Identifier: LGPL-2.1-or-later |
| 2 |
|
| 3 |
from Metadata import export, constmethod, sequence_protocol, class_declarations |
| 4 |
from PyObjectBase import PyObjectBase |
| 5 |
from typing import overload, Sequence, TYPE_CHECKING |
| 6 |
|
| 7 |
@export( |
| 8 |
TwinPointer="Vector3d", |
| 9 |
Include="Base/Vector3D.h", |
| 10 |
Constructor=True, |
| 11 |
Delete=True, |
| 12 |
NumberProtocol=True, |
| 13 |
RichCompare=True, |
| 14 |
) |
| 15 |
@sequence_protocol( |
| 16 |
sq_length=True, |
| 17 |
sq_concat=False, |
| 18 |
sq_repeat=False, |
| 19 |
sq_item=True, |
| 20 |
mp_subscript=True, |
| 21 |
sq_ass_item=True, |
| 22 |
mp_ass_subscript=False, |
| 23 |
sq_contains=False, |
| 24 |
sq_inplace_concat=False, |
| 25 |
sq_inplace_repeat=False, |
| 26 |
) |
| 27 |
@class_declarations( |
| 28 |
"""public: |
| 29 |
VectorPy(const Vector3d & vec, PyTypeObject *T = &Type) |
| 30 |
:PyObjectBase(new Vector3d(vec),T){} |
| 31 |
VectorPy(const Vector3f & vec, PyTypeObject *T = &Type) |
| 32 |
:PyObjectBase(new Vector3d(vec.x,vec.y,vec.z),T){} |
| 33 |
Vector3d value() const |
| 34 |
{ return *(getVectorPtr()); } |
| 35 |
private: |
| 36 |
Py::List sequence; |
| 37 |
""" |
| 38 |
) |
| 39 |
class Vector(PyObjectBase): |
| 40 |
""" |
| 41 |
Base.Vector class. |
| 42 |
|
| 43 |
This class represents a 3D float vector. |
| 44 |
Useful to represent points in the 3D space. |
| 45 |
|
| 46 |
The following constructors are supported: |
| 47 |
|
| 48 |
Vector(x=0, y=0, z=0) |
| 49 |
x : float |
| 50 |
y : float |
| 51 |
z : float |
| 52 |
|
| 53 |
Vector(vector) |
| 54 |
Copy constructor. |
| 55 |
vector : Base.Vector |
| 56 |
|
| 57 |
Vector(seq) |
| 58 |
Define from a sequence of float. |
| 59 |
seq : sequence of float. |
| 60 |
|
| 61 |
Author: Juergen Riegel ([email protected]) |
| 62 |
Licence: LGPL |
| 63 |
""" |
| 64 |
|
| 65 |
Length: float = 0.0 |
| 66 |
"""Gets or sets the length of this vector.""" |
| 67 |
|
| 68 |
x: float = 0.0 |
| 69 |
"""Gets or sets the X component of this vector.""" |
| 70 |
|
| 71 |
y: float = 0.0 |
| 72 |
"""Gets or sets the Y component of this vector.""" |
| 73 |
|
| 74 |
z: float = 0.0 |
| 75 |
"""Gets or sets the Z component of this vector.""" |
| 76 |
|
| 77 |
# fmt: off |
| 78 |
@overload |
| 79 |
def __init__(self, *, x: float = 0, y: float = 0, z: float = 0) -> None: ... |
| 80 |
@overload |
| 81 |
def __init__(self, vector: "Vector") -> None: ... |
| 82 |
@overload |
| 83 |
def __init__(self, seq: Sequence[float]) -> None: ... |
| 84 |
# fmt: on |
| 85 |
|
| 86 |
@constmethod |
| 87 |
def __reduce__(self) -> tuple: |
| 88 |
""" |
| 89 |
__reduce__() -> tuple |
| 90 |
|
| 91 |
Serialization of Vector objects. |
| 92 |
""" |
| 93 |
... |
| 94 |
|
| 95 |
@constmethod |
| 96 |
def add(self, vector2: "Vector") -> "Vector": |
| 97 |
""" |
| 98 |
add(vector2) -> Base.Vector |
| 99 |
|
| 100 |
Returns the sum of this vector and `vector2`. |
| 101 |
|
| 102 |
vector2 : Base.Vector |
| 103 |
""" |
| 104 |
... |
| 105 |
|
| 106 |
@constmethod |
| 107 |
def sub(self, vector2: "Vector") -> "Vector": |
| 108 |
""" |
| 109 |
sub(vector2) -> Base.Vector |
| 110 |
|
| 111 |
Returns the difference of this vector and `vector2`. |
| 112 |
|
| 113 |
vector2 : Base.Vector |
| 114 |
""" |
| 115 |
... |
| 116 |
|
| 117 |
@constmethod |
| 118 |
def negative(self) -> "Vector": |
| 119 |
""" |
| 120 |
negative() -> Base.Vector |
| 121 |
|
| 122 |
Returns the negative (opposite) of this vector. |
| 123 |
""" |
| 124 |
... |
| 125 |
|
| 126 |
def scale(self, x: float, y: float, z: float) -> "Vector": |
| 127 |
""" |
| 128 |
scale(x, y, z) -> Base.Vector |
| 129 |
|
| 130 |
Scales in-place this vector by the given factor in each component. |
| 131 |
|
| 132 |
x : float |
| 133 |
x-component factor scale. |
| 134 |
y : float |
| 135 |
y-component factor scale. |
| 136 |
z : float |
| 137 |
z-component factor scale. |
| 138 |
""" |
| 139 |
... |
| 140 |
|
| 141 |
def multiply(self, factor: float) -> "Vector": |
| 142 |
""" |
| 143 |
multiply(factor) -> Base.Vector |
| 144 |
|
| 145 |
Multiplies in-place each component of this vector by a single factor. |
| 146 |
Equivalent to scale(factor, factor, factor). |
| 147 |
|
| 148 |
factor : float |
| 149 |
""" |
| 150 |
... |
| 151 |
|
| 152 |
@constmethod |
| 153 |
def dot(self, vector2: "Vector") -> float: |
| 154 |
""" |
| 155 |
dot(vector2) -> float |
| 156 |
|
| 157 |
Returns the scalar product (dot product) between this vector and `vector2`. |
| 158 |
|
| 159 |
vector2 : Base.Vector |
| 160 |
""" |
| 161 |
... |
| 162 |
|
| 163 |
@constmethod |
| 164 |
def cross(self, vector2: "Vector") -> "Vector": |
| 165 |
""" |
| 166 |
cross(vector2) -> Base.Vector |
| 167 |
|
| 168 |
Returns the vector product (cross product) between this vector and `vector2`. |
| 169 |
|
| 170 |
vector2 : Base.Vector |
| 171 |
""" |
| 172 |
... |
| 173 |
|
| 174 |
@constmethod |
| 175 |
def isOnLineSegment(self, vector1: "Vector", vector2: "Vector") -> bool: |
| 176 |
""" |
| 177 |
isOnLineSegment(vector1, vector2) -> bool |
| 178 |
|
| 179 |
Checks if this vector is on the line segment generated by `vector1` and `vector2`. |
| 180 |
|
| 181 |
vector1 : Base.Vector |
| 182 |
vector2 : Base.Vector |
| 183 |
""" |
| 184 |
... |
| 185 |
|
| 186 |
@constmethod |
| 187 |
def getAngle(self, vector2: "Vector") -> float: |
| 188 |
""" |
| 189 |
getAngle(vector2) -> float |
| 190 |
|
| 191 |
Returns the angle in radians between this vector and `vector2`. |
| 192 |
|
| 193 |
vector2 : Base.Vector |
| 194 |
""" |
| 195 |
... |
| 196 |
|
| 197 |
def normalize(self) -> "Vector": |
| 198 |
""" |
| 199 |
normalize() -> Base.Vector |
| 200 |
|
| 201 |
Normalizes in-place this vector to the length of 1.0. |
| 202 |
""" |
| 203 |
... |
| 204 |
|
| 205 |
@constmethod |
| 206 |
def isEqual(self, vector2: "Vector", tol: float = 0) -> bool: |
| 207 |
""" |
| 208 |
isEqual(vector2, tol=0) -> bool |
| 209 |
|
| 210 |
Checks if the distance between the points represented by this vector |
| 211 |
and `vector2` is less or equal to the given tolerance. |
| 212 |
|
| 213 |
vector2 : Base.Vector |
| 214 |
tol : float |
| 215 |
""" |
| 216 |
... |
| 217 |
|
| 218 |
@constmethod |
| 219 |
def isParallel(self, vector2: "Vector", tol: float = 0) -> bool: |
| 220 |
""" |
| 221 |
isParallel(vector2, tol=0) -> bool |
| 222 |
|
| 223 |
Checks if this vector and `vector2` are |
| 224 |
parallel less or equal to the given tolerance. |
| 225 |
|
| 226 |
vector2 : Base.Vector |
| 227 |
tol : float |
| 228 |
""" |
| 229 |
... |
| 230 |
|
| 231 |
@constmethod |
| 232 |
def isNormal(self, vector2: "Vector", tol: float = 0) -> bool: |
| 233 |
""" |
| 234 |
isNormal(vector2, tol=0) -> bool |
| 235 |
|
| 236 |
Checks if this vector and `vector2` are |
| 237 |
normal less or equal to the given tolerance. |
| 238 |
|
| 239 |
vector2 : Base.Vector |
| 240 |
tol : float |
| 241 |
""" |
| 242 |
... |
| 243 |
|
| 244 |
def projectToLine(self, point: "Vector", dir: "Vector") -> "Vector": |
| 245 |
""" |
| 246 |
projectToLine(point, dir) -> Base.Vector |
| 247 |
|
| 248 |
Projects `point` on a line that goes through the origin with the direction `dir`. |
| 249 |
The result is the vector from `point` to the projected point. |
| 250 |
The operation is equivalent to dir_n.cross(dir_n.cross(point)), where `dir_n` is |
| 251 |
the vector `dir` normalized. |
| 252 |
The method modifies this vector instance according to result and does not |
| 253 |
depend on the vector itself. |
| 254 |
|
| 255 |
point : Base.Vector |
| 256 |
dir : Base.Vector |
| 257 |
""" |
| 258 |
... |
| 259 |
|
| 260 |
def projectToPlane(self, base: "Vector", normal: "Vector") -> "Vector": |
| 261 |
""" |
| 262 |
projectToPlane(base, normal) -> Base.Vector |
| 263 |
|
| 264 |
Projects in-place this vector on a plane defined by a base point |
| 265 |
represented by `base` and a normal defined by `normal`. |
| 266 |
|
| 267 |
base : Base.Vector |
| 268 |
normal : Base.Vector |
| 269 |
""" |
| 270 |
... |
| 271 |
|
| 272 |
@constmethod |
| 273 |
def distanceToPoint(self, point2: "Vector") -> float: |
| 274 |
""" |
| 275 |
distanceToPoint(point2) -> float |
| 276 |
|
| 277 |
Returns the distance to another point represented by `point2`. |
| 278 |
. |
| 279 |
point : Base.Vector |
| 280 |
""" |
| 281 |
... |
| 282 |
|
| 283 |
@constmethod |
| 284 |
def distanceToLine(self, base: "Vector", dir: "Vector") -> float: |
| 285 |
""" |
| 286 |
distanceToLine(base, dir) -> float |
| 287 |
|
| 288 |
Returns the distance between the point represented by this vector |
| 289 |
and a line defined by a base point represented by `base` and a |
| 290 |
direction `dir`. |
| 291 |
|
| 292 |
base : Base.Vector |
| 293 |
dir : Base.Vector |
| 294 |
""" |
| 295 |
... |
| 296 |
|
| 297 |
@constmethod |
| 298 |
def distanceToLineSegment(self, point1: "Vector", point2: "Vector") -> "Vector": |
| 299 |
""" |
| 300 |
distanceToLineSegment(point1, point2) -> Base.Vector |
| 301 |
|
| 302 |
Returns the vector between the point represented by this vector and the point |
| 303 |
on the line segment with the shortest distance. The line segment is defined by |
| 304 |
`point1` and `point2`. |
| 305 |
|
| 306 |
point1 : Base.Vector |
| 307 |
point2 : Base.Vector |
| 308 |
""" |
| 309 |
... |
| 310 |
|
| 311 |
@constmethod |
| 312 |
def distanceToPlane(self, base: "Vector", normal: "Vector") -> float: |
| 313 |
""" |
| 314 |
distanceToPlane(base, normal) -> float |
| 315 |
|
| 316 |
Returns the distance between this vector and a plane defined by a |
| 317 |
base point represented by `base` and a normal defined by `normal`. |
| 318 |
|
| 319 |
base : Base.Vector |
| 320 |
normal : Base.Vector |
| 321 |
""" |
| 322 |
... |