Coverage for pygeodesy/vector3dBase.py: 96%

273 statements  

« prev     ^ index     » next       coverage.py v7.2.2, created at 2023-04-12 11:45 -0400

1 

2# -*- coding: utf-8 -*- 

3 

4u'''(INTERNAL) Private, 3-D vector base class C{Vector3dBase}. 

5 

6A pure Python implementation of vector-based functions by I{(C) Chris Veness 

72011-2015} published under the same MIT Licence**, see U{Vector-based geodesy 

8<https://www.Movable-Type.co.UK/scripts/latlong-vectors.html>}. 

9''' 

10 

11from pygeodesy.basics import copysign0, islistuple, isscalar, map1, _zip 

12from pygeodesy.constants import EPS, EPS0, INT0, PI, PI2, _float0, \ 

13 isnear0, isnear1, _1_0 

14from pygeodesy.errors import CrossError, _InvalidError, _IsnotError, VectorError 

15from pygeodesy.fmath import euclid_, fdot, hypot_, hypot2_ 

16from pygeodesy.fsums import fsum1_, _pos_self 

17from pygeodesy.interns import NN, _coincident_, _colinear_, _COMMASPACE_, _y_, _z_ 

18from pygeodesy.lazily import _ALL_LAZY, _ALL_MODS as _MODS, _sys, _sys_version_info2 

19from pygeodesy.named import _NamedBase, _NotImplemented, _xother3 

20from pygeodesy.namedTuples import Vector3Tuple 

21from pygeodesy.props import deprecated_method, Property, Property_RO, \ 

22 property_doc_, _update_all 

23from pygeodesy.streprs import Fmt, strs 

24from pygeodesy.units import Float, Scalar 

25from pygeodesy.utily import atan2, fabs, sincos2 

26 

27# from math import atan2, fabs # from .utily 

28 

29__all__ = _ALL_LAZY.vector3dBase 

30__version__ = '23.03.29' 

31 

32 

33class Vector3dBase(_NamedBase): # sync __methods__ with .fsums.Fsum 

34 '''(INTERNAL) Generic 3-D vector base class. 

35 

36 In a geodesy context, these may be used to represent: 

37 - n-vector representing a normal to point on earth's surface 

38 - earth-centered, earth-fixed cartesian (= spherical n-vector) 

39 - great circle normal to vector 

40 - motion vector on earth's surface 

41 - etc. 

42 ''' 

43 _crosserrors = True # un/set by .errors.crosserrors 

44 

45 _ll = None # original latlon, '_fromll' 

46 _x = INT0 # X component 

47 _y = INT0 # Y component 

48 _z = INT0 # Z component 

49 

50 def __init__(self, x_xyz, y=INT0, z=INT0, ll=None, name=NN): 

51 '''New L{Vector3d} or C{Vector3dBase} instance. 

52 

53 The vector may be normalised or use x, y, z for position and 

54 distance from earth centre or height relative to the surface 

55 of the earth' sphere or ellipsoid. 

56 

57 @arg x_xyz: X component of vector (C{scalar}) or a (3-D) vector 

58 (C{Cartesian}, L{Ecef9Tuple}, C{Nvector}, L{Vector3d}, 

59 L{Vector3Tuple}, L{Vector4Tuple} or a C{tuple} or 

60 C{list} of 3+ C{scalar} values). 

61 @kwarg y: Y component of vector (C{scalar}), ignored if B{C{x_xyz}} 

62 is not C{scalar}, otherwise same units as B{C{x_xyz}}. 

63 @kwarg z: Z component of vector (C{scalar}), ignored if B{C{x_xyz}} 

64 is not C{scalar}, otherwise same units as B{C{x_xyz}}. 

65 @kwarg ll: Optional latlon reference (C{LatLon}). 

66 @kwarg name: Optional name (C{str}). 

67 

68 @raise VectorError: Invalid B{C{x_xyz}}. 

69 ''' 

70 if isscalar(x_xyz): 

71 self._xyz(x_xyz, y, z) 

72 else: 

73 self.xyz = x_xyz 

74 if ll: 

75 self._ll = ll 

76 if name: 

77 self.name = name 

78 

79 def __abs__(self): 

80 '''Return the norm of this vector. 

81 

82 @return: Norm, unit length (C{float}); 

83 ''' 

84 return self.length 

85 

86 def __add__(self, other): 

87 '''Add this to an other vector (L{Vector3d}). 

88 

89 @return: Vectorial sum (L{Vector3d}). 

90 

91 @raise TypeError: Incompatible B{C{other}} C{type}. 

92 ''' 

93 return self.plus(other) 

94 

95 def __bool__(self): # PYCHOK PyChecker 

96 '''Is this vector non-zero? 

97 ''' 

98 return bool(self.x or self.y or self.z) 

99 

100 def __ceil__(self): # PYCHOK no cover 

101 '''Not implemented.''' 

102 return _NotImplemented(self) 

103 

104 def __cmp__(self, other): # Python 2- 

105 '''Compare this and an other vector (L{Vector3d}). 

106 

107 @return: -1, 0 or +1 (C{int}). 

108 

109 @raise TypeError: Incompatible B{C{other}} C{type}. 

110 ''' 

111 n = self.others(other).length 

112 return -1 if self.length < n else ( 

113 +1 if self.length > n else 0) 

114 

115 cmp = __cmp__ 

116 

117 def __divmod__(self, other): # PYCHOK no cover 

118 '''Not implemented.''' 

119 return _NotImplemented(self, other) 

120 

121 def __eq__(self, other): 

122 '''Is this vector equal to an other vector? 

123 

124 @arg other: The other vector (L{Vector3d}). 

125 

126 @return: C{True} if equal, C{False} otherwise. 

127 

128 @raise TypeError: Incompatible B{C{other}} C{type}. 

129 ''' 

130 return self.isequalTo(other, eps=EPS0) 

131 

132 def __float__(self): # PYCHOK no cover 

133 '''Not implemented.''' 

134 return _NotImplemented(self) 

135 

136 def __floor__(self): # PYCHOK no cover 

137 '''Not implemented.''' 

138 return _NotImplemented(self) 

139 

140 def __floordiv__(self, other): # PYCHOK no cover 

141 '''Not implemented.''' 

142 return _NotImplemented(self, other) 

143 

144 def __format__(self, *other): # PYCHOK no cover 

145 '''Not implemented.''' 

146 return _NotImplemented(self, *other) 

147 

148 def __ge__(self, other): 

149 '''Is this vector longer than or equal to an other vector? 

150 

151 @arg other: The other vector (L{Vector3d}). 

152 

153 @return: C{True} if so, C{False} otherwise. 

154 

155 @raise TypeError: Incompatible B{C{other}} C{type}. 

156 ''' 

157 return self.length >= self.others(other).length 

158 

159# def __getitem__(self, key): 

160# '''Return C{item} at index or slice C{[B{key}]}. 

161# ''' 

162# return self.xyz[key] 

163 

164 def __gt__(self, other): 

165 '''Is this vector longer than an other vector? 

166 

167 @arg other: The other vector (L{Vector3d}). 

168 

169 @return: C{True} if so, C{False} otherwise. 

170 

171 @raise TypeError: Incompatible B{C{other}} C{type}. 

172 ''' 

173 return self.length > self.others(other).length 

174 

175 def __hash__(self): # PYCHOK no cover 

176 '''Return this instance' C{hash}. 

177 ''' 

178 return hash(self.xyz) # XXX id(self)? 

179 

180 def __iadd__(self, other): 

181 '''Add this and an other vector I{in-place}, C{this += B{other}}. 

182 

183 @arg other: The other vector (L{Vector3d}). 

184 

185 @raise TypeError: Incompatible B{C{other}} C{type}. 

186 ''' 

187 return self._xyz(self.plus(other)) 

188 

189 def __ifloordiv__(self, other): # PYCHOK no cover 

190 '''Not implemented.''' 

191 return _NotImplemented(self, other) 

192 

193 def __imatmul__(self, other): # PYCHOK Python 3.5+ 

194 '''Cross multiply this and an other vector I{in-place}, C{this @= B{other}}. 

195 

196 @arg other: The other vector (L{Vector3d}). 

197 

198 @raise TypeError: Incompatible B{C{other}} C{type}. 

199 

200 @see: Luciano Ramalho, "Fluent Python", page 397-398, O'Reilly 2016. 

201 ''' 

202 return self._xyz(self.cross(other)) 

203 

204 def __imod__(self, other): # PYCHOK no cover 

205 '''Not implemented.''' 

206 return _NotImplemented(self, other) 

207 

208 def __imul__(self, scalar): 

209 '''Multiply this vector by a scalar I{in-place}, C{this *= B{scalar}}. 

210 

211 @arg scalar: Factor (C{scalar}). 

212 

213 @raise TypeError: Non-scalar B{C{scalar}}. 

214 ''' 

215 return self._xyz(self.times(scalar)) 

216 

217 def __int__(self): # PYCHOK no cover 

218 '''Not implemented.''' 

219 return _NotImplemented(self) 

220 

221 def __ipow__(self, other, *mod): # PYCHOK no cover 

222 '''Not implemented.''' 

223 return _NotImplemented(self, other, *mod) 

224 

225 def __isub__(self, other): 

226 '''Subtract an other vector from this one I{in-place}, C{this -= B{other}}. 

227 

228 @arg other: The other vector (L{Vector3d}). 

229 

230 @raise TypeError: Incompatible B{C{other}} C{type}. 

231 ''' 

232 return self._xyz(self.minus(other)) 

233 

234# def __iter__(self): 

235# '''Return an C{iter}ator over this vector's components. 

236# ''' 

237# return iter(self.xyz) 

238 

239 def __itruediv__(self, scalar): 

240 '''Divide this vector by a scalar I{in-place}, C{this /= B{scalar}}. 

241 

242 @arg scalar: The divisor (C{scalar}). 

243 

244 @raise TypeError: Non-scalar B{C{scalar}}. 

245 ''' 

246 return self._xyz(self.dividedBy(scalar)) 

247 

248 def __le__(self, other): # Python 3+ 

249 '''Is this vector shorter than or equal to an other vector? 

250 

251 @arg other: The other vector (L{Vector3d}). 

252 

253 @return: C{True} if so, C{False} otherwise. 

254 

255 @raise TypeError: Incompatible B{C{other}} C{type}. 

256 ''' 

257 return self.length <= self.others(other).length 

258 

259# def __len__(self): 

260# '''Return C{3}, always. 

261# ''' 

262# return len(self.xyz) 

263 

264 def __lt__(self, other): # Python 3+ 

265 '''Is this vector shorter than an other vector? 

266 

267 @arg other: The other vector (L{Vector3d}). 

268 

269 @return: C{True} if so, C{False} otherwise. 

270 

271 @raise TypeError: Incompatible B{C{other}} C{type}. 

272 ''' 

273 return self.length < self.others(other).length 

274 

275 def __matmul__(self, other): # PYCHOK Python 3.5+ 

276 '''Compute the cross product of this and an other vector, C{this @ B{other}}. 

277 

278 @arg other: The other vector (L{Vector3d}). 

279 

280 @return: Cross product (L{Vector3d}). 

281 

282 @raise TypeError: Incompatible B{C{other}} C{type}. 

283 ''' 

284 return self.cross(other) 

285 

286 def __mod__(self, other): # PYCHOK no cover 

287 '''Not implemented.''' 

288 return _NotImplemented(self, other) 

289 

290 def __mul__(self, scalar): 

291 '''Multiply this vector by a scalar, C{this * B{scalar}}. 

292 

293 @arg scalar: Factor (C{scalar}). 

294 

295 @return: Product (L{Vector3d}). 

296 ''' 

297 return self.times(scalar) 

298 

299 def __ne__(self, other): 

300 '''Is this vector not equal to an other vector? 

301 

302 @arg other: The other vector (L{Vector3d}). 

303 

304 @return: C{True} if so, C{False} otherwise. 

305 

306 @raise TypeError: Incompatible B{C{other}} C{type}. 

307 ''' 

308 return not self.isequalTo(other, eps=EPS0) 

309 

310 def __neg__(self): 

311 '''Return the opposite of this vector. 

312 

313 @return: This instance negated (L{Vector3d}) 

314 ''' 

315 return self.classof(-self.x, -self.y, -self.z) 

316 

317 def __pos__(self): # PYCHOK no cover 

318 '''Return this vector I{as-is} or a copy. 

319 

320 @return: This instance (L{Vector3d}) 

321 ''' 

322 return self if _pos_self else self.copy() 

323 

324 def __pow__(self, other, *mod): # PYCHOK no cover 

325 '''Not implemented.''' 

326 return _NotImplemented(self, other, *mod) 

327 

328 __radd__ = __add__ # PYCHOK no cover 

329 

330 def __rdivmod__ (self, other): # PYCHOK no cover 

331 '''Not implemented.''' 

332 return _NotImplemented(self, other) 

333 

334# def __repr__(self): 

335# '''Return the default C{repr(this)}. 

336# ''' 

337# return self.toRepr() 

338 

339 def __rfloordiv__(self, other): # PYCHOK no cover 

340 '''Not implemented.''' 

341 return _NotImplemented(self, other) 

342 

343 def __rmatmul__(self, other): # PYCHOK Python 3.5+ 

344 '''Compute the cross product of an other and this vector, C{B{other} @ this}. 

345 

346 @arg other: The other vector (L{Vector3d}). 

347 

348 @return: Cross product (L{Vector3d}). 

349 

350 @raise TypeError: Incompatible B{C{other}} C{type}. 

351 ''' 

352 return self.others(other).cross(self) 

353 

354 def __rmod__(self, other): # PYCHOK no cover 

355 '''Not implemented.''' 

356 return _NotImplemented(self, other) 

357 

358 __rmul__ = __mul__ 

359 

360 def __round__(self, ndigits=None): # PYCHOK no cover 

361 '''Not implemented.''' 

362 return _NotImplemented(self, ndigits=ndigits) 

363 

364 def __rpow__(self, other, *mod): # PYCHOK no cover 

365 '''Not implemented.''' 

366 return _NotImplemented(self, other, *mod) 

367 

368 def __rsub__(self, other): # PYCHOK no cover 

369 '''Subtract this vector from an other vector, C{B{other} - this}. 

370 

371 @arg other: The other vector (L{Vector3d}). 

372 

373 @return: Difference (L{Vector3d}). 

374 

375 @raise TypeError: Incompatible B{C{other}} C{type}. 

376 ''' 

377 return self.others(other).minus(self) 

378 

379 def __rtruediv__(self, scalar): # PYCHOK no cover 

380 '''Not implemented.''' 

381 return _NotImplemented(self, scalar) 

382 

383 def __sizeof__(self): # PYCHOK not special in Python 2- 

384 '''Return the current size of this vector in C{bytes}. 

385 ''' 

386 # self._x, self._y, self._z, self._ll, ... 

387 v = self.__dict__.values # avoid recursion 

388 return sum(map1(_sys.getsizeof, (o for o in v() if o is not self))) 

389 

390# def __str__(self): 

391# '''Return the default C{str(self)}. 

392# ''' 

393# return self.toStr() 

394 

395 def __sub__(self, other): 

396 '''Subtract an other vector from this vector, C{this - B{other}}. 

397 

398 @arg other: The other vector (L{Vector3d}). 

399 

400 @return: Difference (L{Vector3d}). 

401 

402 @raise TypeError: Incompatible B{C{other}} C{type}. 

403 ''' 

404 return self.minus(other) 

405 

406 def __truediv__(self, scalar): 

407 '''Divide this vector by a scalar, C{this / B{scalar}}. 

408 

409 @arg scalar: The divisor (C{scalar}). 

410 

411 @return: Quotient (L{Vector3d}). 

412 

413 @raise TypeError: Non-scalar B{C{scalar}}. 

414 ''' 

415 return self.dividedBy(scalar) 

416 

417 __trunc__ = __int__ 

418 

419 if _sys_version_info2 < (3, 0): # PYCHOK no cover 

420 # <https://docs.Python.org/2/library/operator.html#mapping-operators-to-functions> 

421 __div__ = __truediv__ 

422 __idiv__ = __itruediv__ 

423 __long__ = __int__ 

424 __nonzero__ = __bool__ 

425 __rdiv__ = __rtruediv__ 

426 

427 def angleTo(self, other, vSign=None, wrap=False): 

428 '''Compute the angle between this and an other vector. 

429 

430 @arg other: The other vector (L{Vector3d}). 

431 @kwarg vSign: Optional vector, if supplied (and out of the 

432 plane of this and the other), angle is signed 

433 positive if this->other is clockwise looking 

434 along vSign or negative in opposite direction, 

435 otherwise angle is unsigned. 

436 @kwarg wrap: Wrap/unroll the angle to +/-PI (C{bool}). 

437 

438 @return: Angle (C{radians}). 

439 

440 @raise TypeError: If B{C{other}} or B{C{vSign}} not a L{Vector3d}. 

441 ''' 

442 x = self.cross(other) 

443 s = x.length 

444 # use vSign as reference to set sign of s 

445 if s and vSign and x.dot(vSign) < 0: 

446 s = -s 

447 

448 a = atan2(s, self.dot(other)) 

449 if wrap and fabs(a) > PI: 

450 a -= copysign0(PI2, a) 

451 return a 

452 

453 def apply(self, fun2, other_x, *y_z, **fun2_kwds): 

454 '''Apply a 2-argument function pairwise to the components 

455 of this and an other vector. 

456 

457 @arg fun2: 2-Argument callable (C{any(scalar, scalar}), 

458 return a C{scalar} or L{INT0} result. 

459 @arg other_x: Other X component (C{scalar}) or a vector 

460 with X, Y and Z components (C{Cartesian}, 

461 L{Ecef9Tuple}, C{Nvector}, L{Vector3d}, 

462 L{Vector3Tuple} or L{Vector4Tuple}). 

463 @arg y_z: Other Y and Z components, positional (C{scalar}, C{scalar}). 

464 @kwarg fun2_kwds: Optional keyword arguments for B{C{fun2}}. 

465 

466 @return: New, applied vector (L{Vector3d}). 

467 

468 @raise ValueError: Invalid B{C{other_x}} or B{C{y_z}}. 

469 ''' 

470 if not callable(fun2): 

471 raise _IsnotError(callable.__name__, fun2=fun2) 

472 

473 if fun2_kwds: 

474 def _f2(a, b): 

475 return fun2(a, b, **fun2_kwds) 

476 else: 

477 _f2 = fun2 

478 

479 xyz = _other_x_y_z3(other_x, y_z) 

480 xyz = (_f2(a, b) for a, b in _zip(self.xyz, xyz)) # strict=True 

481 return self.classof(*xyz) 

482 

483 def cross(self, other, raiser=None, eps0=EPS): # raiser=NN 

484 '''Compute the cross product of this and an other vector. 

485 

486 @arg other: The other vector (L{Vector3d}). 

487 @kwarg raiser: Optional, L{CrossError} label if raised (C{str}, 

488 non-L{NN}). 

489 @kwarg eps0: Near-zero tolerance (C{scalar}), same units as 

490 C{x}, C{y}, and C{z}. 

491 

492 @return: Cross product (L{Vector3d}). 

493 

494 @raise CrossError: Zero or near-zero cross product and both 

495 B{C{raiser}} and L{pygeodesy.crosserrors} set. 

496 

497 @raise TypeError: Incompatible B{C{other}} C{type}. 

498 ''' 

499 other = self.others(other) 

500 

501 x = fsum1_(self.y * other.z, -self.z * other.y) 

502 y = fsum1_(self.z * other.x, -self.x * other.z) 

503 z = fsum1_(self.x * other.y, -self.y * other.x) 

504 

505 if raiser and self.crosserrors and eps0 > 0 \ 

506 and max(map1(fabs, x, y, z)) < eps0: 

507 t = self.isequalTo(other, eps=eps0) 

508 t = _coincident_ if t else _colinear_ 

509 r = other._fromll or other 

510 raise CrossError(raiser, r, txt=t) 

511 

512 return self.classof(x, y, z) 

513 

514 @property_doc_('''raise or ignore L{CrossError} exceptions (C{bool}).''') 

515 def crosserrors(self): 

516 '''Get L{CrossError} exceptions (C{bool}). 

517 ''' 

518 return self._crosserrors 

519 

520 @crosserrors.setter # PYCHOK setter! 

521 def crosserrors(self, raiser): 

522 '''Raise L{CrossError} exceptions (C{bool}). 

523 ''' 

524 self._crosserrors = bool(raiser) 

525 

526 def dividedBy(self, divisor): 

527 '''Divide this vector by a scalar. 

528 

529 @arg divisor: The divisor (C{scalar}). 

530 

531 @return: New, scaled vector (L{Vector3d}). 

532 

533 @raise TypeError: Non-scalar B{C{divisor}}. 

534 

535 @raise VectorError: Invalid or zero B{C{divisor}}. 

536 ''' 

537 d = Scalar(divisor=divisor) 

538 try: 

539 return self._times(_1_0 / d) 

540 except (ValueError, ZeroDivisionError) as x: 

541 raise VectorError(divisor=divisor, cause=x) 

542 

543 def dot(self, other): 

544 '''Compute the dot (scalar) product of this and an other vector. 

545 

546 @arg other: The other vector (L{Vector3d}). 

547 

548 @return: Dot product (C{float}). 

549 

550 @raise TypeError: Incompatible B{C{other}} C{type}. 

551 ''' 

552 return self.length2 if other is self else \ 

553 fdot(self.xyz, *self.others(other).xyz) 

554 

555 @deprecated_method 

556 def equals(self, other, units=False): # PYCHOK no cover 

557 '''DEPRECATED, use method C{isequalTo}. 

558 ''' 

559 return self.isequalTo(other, units=units) 

560 

561 @Property_RO 

562 def euclid(self): 

563 '''I{Approximate} the length (norm, magnitude) of this vector (C{Float}). 

564 

565 @see: Properties C{length} and C{length2} and function 

566 L{pygeodesy.euclid_}. 

567 ''' 

568 return Float(euclid=euclid_(self.x, self.y, self.z)) 

569 

570 def equirectangular(self, other): 

571 '''I{Approximate} the different between this and an other vector. 

572 

573 @arg other: Vector to subtract (C{Vector3dBase}). 

574 

575 @return: The lenght I{squared} of the difference (C{Float}). 

576 

577 @raise TypeError: Incompatible B{C{other}} C{type}. 

578 

579 @see: Property C{length2}. 

580 ''' 

581 d = self.minus(other) 

582 return Float(equirectangular=hypot2_(d.x, d.y, d.z)) 

583 

584 @Property 

585 def _fromll(self): 

586 '''(INTERNAL) Get the latlon reference (C{LatLon}) or C{None}. 

587 ''' 

588 return self._ll 

589 

590 @_fromll.setter # PYCHOK setter! 

591 def _fromll(self, ll): 

592 '''(INTERNAL) Set the latlon reference (C{LatLon}) or C{None}. 

593 ''' 

594 self._ll = ll or None 

595 

596 def intermediateTo(self, other, fraction, **unused): # height=None, wrap=False 

597 '''Locate the vector at a given fraction between (or along) this 

598 and an other vector. 

599 

600 @arg other: The other vector (L{Vector3d}). 

601 @arg fraction: Fraction between both vectors (C{scalar}, 

602 0.0 for this and 1.0 for the other vector). 

603 

604 @return: Intermediate vector (L{Vector3d}). 

605 

606 @raise TypeError: Incompatible B{C{other}} C{type}. 

607 ''' 

608 f = Scalar(fraction=fraction) 

609 if isnear0(f): # PYCHOK no cover 

610 r = self 

611 else: 

612 r = self.others(other) 

613 if not isnear1(f): # self * (1 - f) + r * f 

614 r = self.plus(r.minus(self)._times(f)) 

615 return r 

616 

617 def isconjugateTo(self, other, minum=1, eps=EPS): 

618 '''Determine whether this and an other vector are conjugates. 

619 

620 @arg other: The other vector (C{Cartesian}, L{Ecef9Tuple}, 

621 L{Vector3d}, C{Vector3Tuple} or C{Vector4Tuple}). 

622 @kwarg minum: Minimal number of conjugates required (C{int}, 0..3). 

623 @kwarg eps: Tolerance for equality and conjugation (C{scalar}), 

624 same units as C{x}, C{y}, and C{z}. 

625 

626 @return: C{True} if both vector's components either match 

627 or at least C{B{minum}} have opposite signs. 

628 

629 @raise TypeError: Incompatible B{C{other}} C{type}. 

630 

631 @see: Method C{isequalTo}. 

632 ''' 

633 self.others(other) 

634 n = 0 

635 for a, b in zip(self.xyz, other.xyz): 

636 if fabs(a + b) < eps and ((a < 0 and b > 0) or 

637 (a > 0 and b < 0)): 

638 n += 1 # conjugate 

639 elif fabs(a - b) > eps: 

640 return False # unequal 

641 return bool(n >= minum) 

642 

643 def isequalTo(self, other, units=False, eps=EPS): 

644 '''Check if this and an other vector are equal or equivalent. 

645 

646 @arg other: The other vector (L{Vector3d}). 

647 @kwarg units: Optionally, compare the normalized, unit 

648 version of both vectors. 

649 @kwarg eps: Tolerance for equality (C{scalar}), same units as 

650 C{x}, C{y}, and C{z}. 

651 

652 @return: C{True} if vectors are identical, C{False} otherwise. 

653 

654 @raise TypeError: Incompatible B{C{other}} C{type}. 

655 

656 @see: Method C{isconjugateTo}. 

657 ''' 

658 if units: 

659 self.others(other) 

660 d = self.unit().minus(other.unit()) 

661 else: 

662 d = self.minus(other) 

663 return max(map(fabs, d.xyz)) < eps 

664 

665 @Property_RO 

666 def length(self): # __dict__ value overwritten by Property_RO C{_united} 

667 '''Get the length (norm, magnitude) of this vector (C{Float}). 

668 

669 @see: Properties L{length2} and L{euclid}. 

670 ''' 

671 return Float(length=hypot_(self.x, self.y, self.z)) 

672 

673 @Property_RO 

674 def length2(self): # __dict__ value overwritten by Property_RO C{_united} 

675 '''Get the length I{squared} of this vector (C{Float}). 

676 

677 @see: Property L{length} and method C{equirectangular}. 

678 ''' 

679 return Float(length2=hypot2_(self.x, self.y, self.z)) 

680 

681 def minus(self, other): 

682 '''Subtract an other vector from this vector. 

683 

684 @arg other: The other vector (L{Vector3d}). 

685 

686 @return: New vector difference (L{Vector3d}). 

687 

688 @raise TypeError: Incompatible B{C{other}} C{type}. 

689 ''' 

690 self.others(other) 

691 return self._minus(other.x, other.y, other.z) 

692 

693 def _minus(self, x, y, z): 

694 '''(INTERNAL) Helper for methods C{.minus} and C{.minus_}. 

695 ''' 

696 return self.classof(self.x - x, self.y - y, self.z - z) 

697 

698 def minus_(self, other_x, *y_z): 

699 '''Subtract separate X, Y and Z components from this vector. 

700 

701 @arg other_x: X component (C{scalar}) or a vector's 

702 X, Y, and Z components (C{Cartesian}, 

703 L{Ecef9Tuple}, C{Nvector}, L{Vector3d}, 

704 L{Vector3Tuple}, L{Vector4Tuple}). 

705 @arg y_z: Y and Z components (C{scalar}, C{scalar}), 

706 ignored if B{C{other_x}} is not C{scalar}. 

707 

708 @return: New, vectiorial vector (L{Vector3d}). 

709 

710 @raise ValueError: Invalid B{C{other_x}} or B{C{y_z}}. 

711 ''' 

712 return self._minus(*_other_x_y_z3(other_x, y_z)) 

713 

714 def negate(self): 

715 '''Return this vector in opposite direction. 

716 

717 @return: New, opposite vector (L{Vector3d}). 

718 ''' 

719 return self.classof(-self.x, -self.y, -self.z) 

720 

721 __neg__ = negate # PYCHOK no cover 

722 

723 @Property_RO 

724 def _N_vector(self): 

725 '''(INTERNAL) Get the (C{nvectorBase._N_vector_}) 

726 ''' 

727 return _MODS.nvectorBase._N_vector_(*self.xyz, name=self.name) 

728 

729 def others(self, *other, **name_other_up): 

730 '''Refined class comparison. 

731 

732 @arg other: The other vector (L{Vector3d}). 

733 @kwarg name_other_up: Overriding C{name=other} and C{up=1} 

734 keyword arguments. 

735 

736 @return: The B{C{other}} if compatible. 

737 

738 @raise TypeError: Incompatible B{C{other}} C{type}. 

739 ''' 

740 other, name, up = _xother3(self, other, **name_other_up) 

741 if not isinstance(other, Vector3dBase): 

742 _NamedBase.others(self, other, name=name, up=up + 1) 

743 return other 

744 

745 def plus(self, other): 

746 '''Add this vector and an other vector. 

747 

748 @arg other: The other vector (L{Vector3d}). 

749 

750 @return: Vectorial sum (L{Vector3d}). 

751 

752 @raise TypeError: Incompatible B{C{other}} C{type}. 

753 ''' 

754 self.others(other) 

755 return self._plus(other.x, other.y, other.z) 

756 

757 sum = plus # alternate name 

758 

759 def _plus(self, x, y, z): 

760 '''(INTERNAL) Helper for methods C{.plus} and C{.plus_}. 

761 ''' 

762 return self.classof(self.x + x, self.y + y, self.z + z) 

763 

764 def plus_(self, other_x, *y_z): 

765 '''Sum of this vector and separate X, Y and Z components. 

766 

767 @arg other_x: X component (C{scalar}) or a vector's 

768 X, Y, and Z components (C{Cartesian}, 

769 L{Ecef9Tuple}, C{Nvector}, L{Vector3d}, 

770 L{Vector3Tuple}, L{Vector4Tuple}). 

771 @arg y_z: Y and Z components (C{scalar}, C{scalar}), 

772 ignored if B{C{other_x}} is not C{scalar}. 

773 

774 @return: New, vectiorial vector (L{Vector3d}). 

775 

776 @raise ValueError: Invalid B{C{other_x}} or B{C{y_z}}. 

777 ''' 

778 return self._plus(*_other_x_y_z3(other_x, y_z)) 

779 

780 def rotate(self, axis, theta): 

781 '''Rotate this vector around an axis by a specified angle. 

782 

783 @arg axis: The axis being rotated around (L{Vector3d}). 

784 @arg theta: The angle of rotation (C{radians}). 

785 

786 @return: New, rotated vector (L{Vector3d}). 

787 

788 @see: U{Rotation matrix from axis and angle<https://WikiPedia.org/wiki/ 

789 Rotation_matrix#Rotation_matrix_from_axis_and_angle>} and 

790 U{Quaternion-derived rotation matrix<https://WikiPedia.org/wiki/ 

791 Quaternions_and_spatial_rotation#Quaternion-derived_rotation_matrix>}. 

792 ''' 

793 s, c = sincos2(theta) # rotation angle 

794 d = _1_0 - c 

795 if d or s: 

796 p = self.unit().xyz # point being rotated 

797 r = self.others(axis=axis).unit() # axis being rotated around 

798 

799 ax, ay, az = r.xyz # quaternion-derived rotation matrix 

800 bx, by, bz = r.times(d).xyz 

801 sx, sy, sz = r.times(s).xyz 

802 

803 x = fdot(p, ax * bx + c, ax * by - sz, ax * bz + sy) 

804 y = fdot(p, ay * bx + sz, ay * by + c, ay * bz - sx) 

805 z = fdot(p, az * bx - sy, az * by + sx, az * bz + c) 

806 else: # unrotated 

807 x, y, z = self.xyz 

808 return self.classof(x, y, z) 

809 

810 @deprecated_method 

811 def rotateAround(self, axis, theta): # PYCHOK no cover 

812 '''DEPRECATED, use method C{rotate}.''' 

813 return self.rotate(axis, theta) 

814 

815 def times(self, factor): 

816 '''Multiply this vector by a scalar. 

817 

818 @arg factor: Scale factor (C{scalar}). 

819 

820 @return: New, scaled vector (L{Vector3d}). 

821 

822 @raise TypeError: Non-scalar B{C{factor}}. 

823 ''' 

824 return self._times(Scalar(factor=factor)) 

825 

826 def _times(self, s): 

827 '''(INTERNAL) Helper for C{.dividedBy} and C{.times}. 

828 ''' 

829 return self.classof(self.x * s, self.y * s, self.z * s) 

830 

831 def times_(self, other_x, *y_z): 

832 '''Multiply this vector's components by separate X, Y and Z factors. 

833 

834 @arg other_x: X scale factor (C{scalar}) or a vector's 

835 X, Y, and Z components as scale factors 

836 (C{Cartesian}, L{Ecef9Tuple}, C{Nvector}, 

837 L{Vector3d}, L{Vector3Tuple}, L{Vector4Tuple}). 

838 @arg y_z: Y and Z scale factors (C{scalar}, C{scalar}), 

839 ignored if B{C{other_x}} is not C{scalar}. 

840 

841 @return: New, scaled vector (L{Vector3d}). 

842 

843 @raise ValueError: Invalid B{C{other_x}} or B{C{y_z}}. 

844 ''' 

845 x, y, z = _other_x_y_z3(other_x, y_z) 

846 return self.classof(self.x * x, self.y * y, self.z * z) 

847 

848# @deprecated_method 

849# def to2ab(self): # PYCHOK no cover 

850# '''DEPRECATED, use property C{Nvector.philam}. 

851# 

852# @return: A L{PhiLam2Tuple}C{(phi, lam)}. 

853# ''' 

854# return _MODS.formy.n_xyz2philam(self.x, self.y, self.z) 

855 

856# @deprecated_method 

857# def to2ll(self): # PYCHOK no cover 

858# '''DEPRECATED, use property C{Nvector.latlon}. 

859# 

860# @return: A L{LatLon2Tuple}C{(lat, lon)}. 

861# ''' 

862# return _MODS.formy.n_xyz2latlon(self.x, self.y, self.z) 

863 

864 @deprecated_method 

865 def to3xyz(self): # PYCHOK no cover 

866 '''DEPRECATED, use property L{xyz}. 

867 ''' 

868 return self.xyz 

869 

870 def toStr(self, prec=5, fmt=Fmt.PAREN, sep=_COMMASPACE_): # PYCHOK expected 

871 '''Return a string representation of this vector. 

872 

873 @kwarg prec: Number of decimal places (C{int}). 

874 @kwarg fmt: Enclosing format to use (C{str}). 

875 @kwarg sep: Separator between components (C{str}). 

876 

877 @return: Vector as "(x, y, z)" (C{str}). 

878 ''' 

879 t = sep.join(strs(self.xyz, prec=prec)) 

880 return (fmt % (t,)) if fmt else t 

881 

882 def unit(self, ll=None): 

883 '''Normalize this vector to unit length. 

884 

885 @kwarg ll: Optional, original location (C{LatLon}). 

886 

887 @return: Normalized vector (L{Vector3d}). 

888 ''' 

889 u = self._united 

890 if ll: 

891 u._fromll = ll 

892 return u 

893 

894 @Property_RO 

895 def _united(self): # __dict__ value overwritten below 

896 '''(INTERNAL) Get normalized vector (L{Vector3d}). 

897 ''' 

898 n = self.length 

899 if n > EPS0 and fabs(n - _1_0) > EPS0: 

900 u = self._xnamed(self.dividedBy(n)) 

901 u._update(False, length=_1_0, length2=_1_0, _united=u) 

902 else: 

903 u = self.copy() 

904 u._update(False, _united=u) 

905 if self._fromll: 

906 u._fromll = self._fromll 

907 return u 

908 

909 @Property 

910 def x(self): 

911 '''Get the X component (C{float}). 

912 ''' 

913 return self._x 

914 

915 @x.setter # PYCHOK setter! 

916 def x(self, x): 

917 '''Set the X component, if different (C{float}). 

918 ''' 

919 x = Float(x=x) 

920 if self._x != x: 

921 _update_all(self) 

922 self._x = x 

923 

924 @Property 

925 def xyz(self): 

926 '''Get the X, Y and Z components (L{Vector3Tuple}C{(x, y, z)}). 

927 ''' 

928 return Vector3Tuple(self.x, self.y, self.z, name=self.name) 

929 

930 @xyz.setter # PYCHOK setter! 

931 def xyz(self, xyz): 

932 '''Set the X, Y and Z components (C{Cartesian}, L{Ecef9Tuple}, 

933 C{Nvector}, L{Vector3d}, L{Vector3Tuple}, L{Vector4Tuple} 

934 or a C{tuple} or C{list} of 3+ C{scalar} values). 

935 ''' 

936 if islistuple(xyz, 3): 

937 self._xyz(*xyz[:3]) 

938 else: 

939 self._xyz(xyz) 

940 

941 def _xyz(self, x_xyz, *y_z): 

942 '''(INTERNAL) Set the C{_x}, C{_y} and C{_z} attributes. 

943 ''' 

944 if len(self.__dict__) > 3: # any other than initial ._x, ._y and ._z attrs 

945 _update_all(self) 

946 try: 

947 self._x, \ 

948 self._y, \ 

949 self._z = map1(_float0, x_xyz, *y_z) if y_z else x_xyz.xyz 

950 except (AttributeError, TypeError, ValueError) as x: 

951 raise VectorError(cause=x, **_xyzkwds(y_z, x_xyz=x_xyz)) 

952 return self 

953 

954 @Property_RO 

955 def x2y2z2(self): 

956 '''Get the X, Y and Z components I{squared} (3-tuple C{(x**2, y**2, z**2)}). 

957 ''' 

958 return self.x**2, self.y**2, self.z**2 

959 

960 @Property 

961 def y(self): 

962 '''Get the Y component (C{float}). 

963 ''' 

964 return self._y 

965 

966 @y.setter # PYCHOK setter! 

967 def y(self, y): 

968 '''Set the Y component, if different (C{float}). 

969 ''' 

970 y = Float(y=y) 

971 if self._y != y: 

972 _update_all(self) 

973 self._y = y 

974 

975 @Property 

976 def z(self): 

977 '''Get the Z component (C{float}). 

978 ''' 

979 return self._z 

980 

981 @z.setter # PYCHOK setter! 

982 def z(self, z): 

983 '''Set the Z component, if different (C{float}). 

984 ''' 

985 z = Float(z=z) 

986 if self._z != z: 

987 _update_all(self) 

988 self._z = z 

989 

990 

991def _other_x_y_z3(other_x, y_z): 

992 '''(INTERNAL) Helper for C{Vector3dBase.apply} and C{Vector3dBase.times_}. 

993 ''' 

994 try: 

995 return map1(_float0, other_x, *y_z) if y_z else \ 

996 (other_x.x, other_x.y, other_x.z) # not .xyz! 

997 except (AttributeError, TypeError, ValueError) as x: 

998 raise _InvalidError(cause=x, **_xyzkwds(y_z, other_x=other_x)) 

999 

1000 

1001def _xyzkwds(y_z, **xyz): # PYCHOK no cover 

1002 '''(INTERANL) Helper for C{_other_x_y_z3} and C{Vector3dBase._xyz}. 

1003 ''' 

1004 if y_z: 

1005 d = dict(_zip((_y_, _z_), y_z)) # if y_z else {}, strict=True 

1006 for x in xyz.values(): 

1007 d.update(x=x) 

1008 return d 

1009 return xyz 

1010 

1011# **) MIT License 

1012# 

1013# Copyright (C) 2016-2023 -- mrJean1 at Gmail -- All Rights Reserved. 

1014# 

1015# Permission is hereby granted, free of charge, to any person obtaining a 

1016# copy of this software and associated documentation files (the "Software"), 

1017# to deal in the Software without restriction, including without limitation 

1018# the rights to use, copy, modify, merge, publish, distribute, sublicense, 

1019# and/or sell copies of the Software, and to permit persons to whom the 

1020# Software is furnished to do so, subject to the following conditions: 

1021# 

1022# The above copyright notice and this permission notice shall be included 

1023# in all copies or substantial portions of the Software. 

1024# 

1025# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS 

1026# OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 

1027# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 

1028# THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR 

1029# OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, 

1030# ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR 

1031# OTHER DEALINGS IN THE SOFTWARE.