1

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

3

4 u'''Extended 3-D vector class L{Vector3d} and functions.

5

6 Function L{intersection3d3}, L{intersections2}, L{parse3d}, L{sumOf} and

7 L{trilaterate3d2}.

8'''

9

10 from pygeodesy.constants import EPS, EPS0, EPS1, EPS4, INT0, isnear0, \

11 _0_0, _1_0, typename

12 from pygeodesy.errors import IntersectionError, _ValueError, VectorError, \

13 _xattr, _xError, _xkwds, _xkwds_get, _xkwds_item2

14 from pygeodesy.fmath import euclid, fabs, fdot, hypot, sqrt

15# from pygeodesy.fsums import fsum1_ # from _MODS

16# from pygeodesy.formy import _radical2 # _MODS

17# from pygeodesy.internals import typename # from .constants

18 from pygeodesy.interns import _COMMA_, _concentric_, _intersection_, \

19 _near_, _negative_, _no_, _too_

20 from pygeodesy.iters import PointsIter, Fmt

21 from pygeodesy.lazily import _ALL_DOCS, _ALL_LAZY, _ALL_MODS as _MODS

22 from pygeodesy.named import _name__, _name2__, _xnamed, _xotherError

23 from pygeodesy.namedTuples import Intersection3Tuple, NearestOn2Tuple, \

24 NearestOn6Tuple, _v2Cls, Vector3Tuple # Vector4Tuple

25# from pygeodesy.nvectorBase import _nsumOf # _MODS

26# from pygeodesy.streprs import Fmt # from .iters

27 from pygeodesy.units import _fi_j2, _isDegrees, Radius, Radius_

28 from pygeodesy.utily import atan2b, sincos2d

29# from pygeodesy import vector2d as _vector2d # _MODS.into

30 from pygeodesy.vector3dBase import Vector3dBase

31

32# from math import fabs, sqrt # from .fmath

33

34__all__ = _ALL_LAZY.vector3d

35__version__ = '25.04.21'

36

37_vector2d = _MODS.into(vector2d=__name__)

38

39

40 class Vector3d(Vector3dBase):

41 '''Extended 3-D vector.

42

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

44 - n-vector, the normal to a point on the earth's surface

45 - Earth-Centered, Earth-Fixed (ECEF) cartesian (== spherical n-vector)

46 - great circle normal to the vector

47 - motion vector on the earth's surface

48 - etc.

49 '''

50

51 def bearing(self, useZ=True):

52 '''Get this vector's "bearing", the angle off the +Z axis, clockwise.

53

54 @kwarg useZ: If C{True}, use the Z component, otherwise ignore the

55 Z component and consider the +Y as the +Z axis.

56

57 @return: Bearing (compass C{degrees}).

58 '''

59 x, y = self.x, self.y

60 if useZ:

61 x, y = hypot(x, y), self.z

62 return atan2b(x, y)

63

64 def circin6(self, point2, point3, eps=EPS4):

65 '''Return the radius and center of the I{inscribed} aka I{In- circle}

66 of a (3-D) triangle formed by this and two other points.

67

68 @arg point2: Second point (C{Cartesian}, L{Vector3d}, C{Vector3Tuple},

69 C{Vector4Tuple} or C{Vector2Tuple} if C{B{useZ}=False}).

70 @arg point3: Third point (C{Cartesian}, L{Vector3d}, C{Vector3Tuple},

71 C{Vector4Tuple} or C{Vector2Tuple} if C{B{useZ}=False}).

72 @kwarg eps: Tolerance for function L{pygeodesy.trilaterate3d2} if

73 C{B{useZ} is True} otherwise L{pygeodesy.trilaterate2d2}.

74

75 @return: L{Circin6Tuple}C{(radius, center, deltas, cA, cB, cC)}. The

76 C{center} and contact points C{cA}, C{cB} and C{cC}, each an

77 instance of this (sub-)class, are co-planar with this and the

78 two given points.

79

80 @raise ImportError: Package C{numpy} not found, not installed or older

81 than version 1.10.

82

83 @raise IntersectionError: Near-coincident or -colinear points or

84 a trilateration or C{numpy} issue.

85

86 @raise TypeError: Invalid B{C{point2}} or B{C{point3}}.

87

88 @see: Function L{pygeodesy.circin6}, U{Incircle

89 <https://MathWorld.Wolfram.com/Incircle.html>} and U{Contact

90 Triangle<https://MathWorld.Wolfram.com/ContactTriangle.html>}.

91 '''

92 try:

93 return _vector2d._circin6(self, point2, point3, eps=eps, useZ=True)

94 except (AssertionError, TypeError, ValueError) as x:

95 raise _xError(x, point=self, point2=point2, point3=point3)

96

97 def circum3(self, point2, point3, circum=True, eps=EPS4):

98 '''Return the radius and center of the smallest circle I{through} or

99 I{containing} this and two other (3-D) points.

100

101 @arg point2: Second point (C{Cartesian}, L{Vector3d}, C{Vector3Tuple}

102 or C{Vector4Tuple}).

103 @arg point3: Third point (C{Cartesian}, L{Vector3d}, C{Vector3Tuple}

104 or C{Vector4Tuple}).

105 @kwarg circum: If C{True}, return the C{circumradius} and C{circumcenter},

106 always, ignoring the I{Meeus}' Type I case (C{bool}).

107 @kwarg eps: Tolerance passed to function L{pygeodesy.trilaterate3d2}.

108

109 @return: A L{Circum3Tuple}C{(radius, center, deltas)}. The C{center}, an

110 instance of this (sub-)class, is co-planar with this and the two

111 given points.

112

113 @raise ImportError: Package C{numpy} not found, not installed or older than

114 version 1.10.

115

116 @raise IntersectionError: Near-concentric, -coincident or -colinear points

117 or a trilateration or C{numpy} issue.

118

119 @raise TypeError: Invalid B{C{point2}} or B{C{point3}}.

120

121 @see: Function L{pygeodesy.circum3} and methods L{circum4_} and L{meeus2}.

122 '''

123 try:

124 return _vector2d._circum3(self, point2, point3, circum=circum,

125 eps=eps, useZ=True, clas=self.classof)

126 except (AssertionError, TypeError, ValueError) as x:

127 raise _xError(x, point=self, point2=point2, point3=point3, circum=circum)

128

129 def circum4_(self, *points):

130 '''Best-fit a sphere through this and two or more other (3-D) points.

131

132 @arg points: Other points (each a C{Cartesian}, L{Vector3d}, C{Vector3Tuple}

133 or C{Vector4Tuple}).

134

135 @return: L{Circum4Tuple}C{(radius, center, rank, residuals)} with C{center}

136 an instance if this (sub-)class.

137

138 @raise ImportError: Package C{numpy} not found, not installed or

139 older than version 1.10.

140

141 @raise NumPyError: Some C{numpy} issue.

142

143 @raise PointsError: Too few B{C{points}}.

144

145 @raise TypeError: One of the B{C{points}} invalid.

146

147 @see: Function L{pygeodesy.circum4_} and methods L{circum3} and L{meeus2}.

148 '''

149 return _vector2d.circum4_(self, *points, useZ=True, Vector=self.classof)

150

151 def iscolinearWith(self, point1, point2, eps=EPS):

152 '''Check whether this and two other (3-D) points are colinear.

153

154 @arg point1: One point (C{Cartesian}, L{Vector3d}, C{Vector3Tuple}

155 or C{Vector4Tuple}).

156 @arg point2: An other point (C{Cartesian}, L{Vector3d}, C{Vector3Tuple}

157 or C{Vector4Tuple}).

158 @kwarg eps: Tolerance (C{scalar}), same units as C{x},

159 C{y}, and C{z}.

160

161 @return: C{True} if this point is colinear with B{C{point1}} and

162 B{C{point2}}, C{False} otherwise.

163

164 @raise TypeError: Invalid B{C{point1}} or B{C{point2}}.

165

166 @see: Method L{nearestOn}.

167 '''

168 v = self if self.name else _otherV3d(NN_OK=False, this=self)

169 return _vector2d._iscolinearWith(v, point1, point2, eps=eps)

170

171 def meeus2(self, point2, point3, circum=False):

172 '''Return the radius and I{Meeus}' Type of the smallest circle I{through}

173 or I{containing} this and two other (3-D) points.

174

175 @arg point2: Second point (C{Cartesian}, L{Vector3d}, C{Vector3Tuple}

176 or C{Vector4Tuple}).

177 @arg point3: Third point (C{Cartesian}, L{Vector3d}, C{Vector3Tuple}

178 or C{Vector4Tuple}).

179 @kwarg circum: If C{True}, return the C{circumradius} and C{circumcenter}

180 always, overriding I{Meeus}' Type II case (C{bool}).

181

182 @return: L{Meeus2Tuple}C{(radius, Type)}, with C{Type} the C{circumcenter}

183 iff C{B{circum}=True}.

184

185 @raise IntersectionError: Coincident or colinear points, iff C{B{circum}=True}.

186

187 @raise TypeError: Invalid B{C{point2}} or B{C{point3}}.

188

189 @see: Function L{pygeodesy.meeus2} and methods L{circum3} and L{circum4_}.

190 '''

191 try:

192 return _vector2d._meeus2(self, point2, point3, circum, clas=self.classof)

193 except (TypeError, ValueError) as x:

194 raise _xError(x, point=self, point2=point2, point3=point3, circum=circum)

195

196 def nearestOn(self, point1, point2, within=True):

197 '''Locate the point between two points closest to this point.

198

199 @arg point1: Start point (C{Cartesian}, L{Vector3d}, C{Vector3Tuple} or

200 C{Vector4Tuple}).

201 @arg point2: End point (C{Cartesian}, L{Vector3d}, C{Vector3Tuple} or

202 C{Vector4Tuple}).

203 @kwarg within: If C{True}, return the closest point between the given

204 points, otherwise the closest point on the extended

205 line through both points (C{bool}).

206

207 @return: Closest point, either B{C{point1}} or B{C{point2}} or an instance

208 of this (sub-)class.

209

210 @raise TypeError: Invalid B{C{point1}} or B{C{point2}}.

211

212 @see: Method L{sphericalTrigonometry.LatLon.nearestOn3} and U{3-D Point-Line

213 Distance<https://MathWorld.Wolfram.com/Point-LineDistance3-Dimensional.html>}.

214 '''

215 return _nearestOn2(self, point1, point2, within=within).closest

216

217 def nearestOn6(self, points, closed=False, useZ=True): # eps=EPS

218 '''Locate the point on a path or polygon closest to this point.

219

220 The closest point is either on and within the extent of a polygon

221 edge or the nearest of that edge's end points.

222

223 @arg points: The path or polygon points (C{Cartesian}, L{Vector3d},

224 C{Vector3Tuple} or C{Vector4Tuple}[]).

225 @kwarg closed: Optionally, close the path or polygon (C{bool}).

226 @kwarg useZ: If C{True}, use the Z components, otherwise force C{z=INT0} (C{bool}).

227

228 @return: A L{NearestOn6Tuple}C{(closest, distance, fi, j, start, end)}

229 with the C{closest}, the C{start} and the C{end} point each

230 an instance of this point's (sub-)class.

231

232 @raise PointsError: Insufficient number of B{C{points}}

233

234 @raise TypeError: Non-cartesian B{C{points}}.

235

236 @note: Distances measured with method L{Vector3d.equirectangular}.

237

238 @see: Function L{nearestOn6}.

239 '''

240 return nearestOn6(self, points, closed=closed, useZ=useZ) # Vector=self.classof

241

242 def parse(self, str3d, sep=_COMMA_, **name):

243 '''Parse an C{"x, y, z"} string to a L{Vector3d} instance.

244

245 @arg str3d: X, y and z string (C{str}), see function L{parse3d}.

246 @kwarg sep: Optional separator (C{str}).

247 @kwarg name: Optional instance C{B{name}=NN} (C{str}), overriding this name.

248

249 @return: The instance (L{Vector3d}).

250

251 @raise VectorError: Invalid B{C{str3d}}.

252 '''

253 return parse3d(str3d, sep=sep, Vector=self.classof, name=self._name__(name))

254

255 def radii11(self, point2, point3):

256 '''Return the radii of the C{Circum-}, C{In-}, I{Soddy} and C{Tangent}

257 circles of a (3-D) triangle.

258

259 @arg point2: Second point (C{Cartesian}, L{Vector3d}, C{Vector3Tuple},

260 C{Vector4Tuple} or C{Vector2Tuple} if C{B{useZ}=False}).

261 @arg point3: Third point (C{Cartesian}, L{Vector3d}, C{Vector3Tuple},

262 C{Vector4Tuple} or C{Vector2Tuple} if C{B{useZ}=False}).

263

264 @return: L{Radii11Tuple}C{(rA, rB, rC, cR, rIn, riS, roS, a, b, c, s)}.

265

266 @raise TriangleError: Near-coincident or -colinear points.

267

268 @raise TypeError: Invalid B{C{point2}} or B{C{point3}}.

269

270 @see: Function L{pygeodesy.radii11}, U{Incircle

271 <https://MathWorld.Wolfram.com/Incircle.html>}, U{Soddy Circles

272 <https://MathWorld.Wolfram.com/SoddyCircles.html>} and U{Tangent

273 Circles<https://MathWorld.Wolfram.com/TangentCircles.html>}.

274 '''

275 try:

276 return _vector2d._radii11ABC4(self, point2, point3, useZ=True)[0]

277 except (TypeError, ValueError) as x:

278 raise _xError(x, point=self, point2=point2, point3=point3)

279

280 def soddy4(self, point2, point3, eps=EPS4):

281 '''Return the radius and center of the C{inner} I{Soddy} circle of a

282 (3-D) triangle.

283

284 @arg point2: Second point (C{Cartesian}, L{Vector3d}, C{Vector3Tuple},

285 C{Vector4Tuple} or C{Vector2Tuple} if C{B{useZ}=False}).

286 @arg point3: Third point (C{Cartesian}, L{Vector3d}, C{Vector3Tuple},

287 C{Vector4Tuple} or C{Vector2Tuple} if C{B{useZ}=False}).

288 @kwarg eps: Tolerance for function L{pygeodesy.trilaterate3d2} if

289 C{B{useZ} is True} otherwise L{pygeodesy.trilaterate2d2}.

290

291 @return: L{Soddy4Tuple}C{(radius, center, deltas, outer)}. The C{center},

292 an instance of B{C{point1}}'s (sub-)class, is co-planar with the

293 three given points.

294

295 @raise ImportError: Package C{numpy} not found, not installed or older

296 than version 1.10.

297

298 @raise IntersectionError: Near-coincident or -colinear points or

299 a trilateration or C{numpy} issue.

300

301 @raise TypeError: Invalid B{C{point2}} or B{C{point3}}.

302

303 @see: Function L{pygeodesy.soddy4}.

304 '''

305 return _vector2d.soddy4(self, point2, point3, eps=eps, useZ=True)

306

307 def toCartesian(self, Cartesian, **Cartesian_kwds):

308 '''Return this C{Vector3d} as a C{Cartesian}.

309

310 @arg Cartesian: The C{Cartesian} class to use.

311 @kwarg Cartesian_kwds: Optional, additional C{Cartesian}

312 keyword arguments.

313

314 @return: The C{B{Cartesian}} instance.

315 '''

316 return _v2Cls(self, Cartesian, Cartesian_kwds)

317

318 def trilaterate2d2(self, radius, center2, radius2, center3, radius3, eps=EPS4, z=INT0):

319 '''Trilaterate this and two other circles, each given as a (2-D) center

320 and a radius.

321

322 @arg radius: Radius of this circle (same C{units} as this C{x} and C{y}.

323 @arg center2: Center of the 2nd circle (C{Cartesian}, L{Vector3d},

324 C{Vector2Tuple}, C{Vector3Tuple} or C{Vector4Tuple}).

325 @arg radius2: Radius of this circle (same C{units} as this C{x} and C{y}.

326 @arg center3: Center of the 3rd circle (C{Cartesian}, L{Vector3d},

327 C{Vector2Tuple}, C{Vector3Tuple} or C{Vector4Tuple}).

328 @arg radius3: Radius of the 3rd circle (same C{units} as this C{x} and C{y}.

329 @kwarg eps: Tolerance to check the trilaterated point I{delta} on all

330 3 circles (C{scalar}) or C{None} for no checking.

331 @kwarg z: Optional Z component of the trilaterated point (C{scalar}).

332

333 @return: Trilaterated point, an instance of this (sub-)class with C{z=B{z}}.

334

335 @raise IntersectionError: No intersection, near-concentric or -colinear

336 centers, trilateration failed some other way

337 or the trilaterated point is off one circle

338 by more than B{C{eps}}.

339

340 @raise TypeError: Invalid B{C{center2}} or B{C{center3}}.

341

342 @raise UnitError: Invalid B{C{radius1}}, B{C{radius2}} or B{C{radius3}}.

343

344 @see: Function L{pygeodesy.trilaterate2d2}.

345 '''

346

347 def _xyr3(r, **name_v):

348 v = _otherV3d(useZ=False, **name_v)

349 return v.x, v.y, r

350

351 try:

352 return _vector2d._trilaterate2d2(*(_xyr3(radius, center=self) +

353 _xyr3(radius2, center2=center2) +

354 _xyr3(radius3, center3=center3)),

355 eps=eps, Vector=self.classof, z=z)

356 except (AssertionError, TypeError, ValueError) as x:

357 raise _xError(x, center=self, radius=radius,

358 center2=center2, radius2=radius2,

359 center3=center3, radius3=radius3)

360

361 def trilaterate3d2(self, radius, center2, radius2, center3, radius3, eps=EPS4):

362 '''Trilaterate this and two other spheres, each given as a (3-D) center

363 and a radius.

364

365 @arg radius: Radius of this sphere (same C{units} as this C{x}, C{y}

366 and C{z}).

367 @arg center2: Center of the 2nd sphere (C{Cartesian}, L{Vector3d},

368 C{Vector3Tuple} or C{Vector4Tuple}).

369 @arg radius2: Radius of this sphere (same C{units} as this C{x}, C{y}

370 and C{z}).

371 @arg center3: Center of the 3rd sphere (C{Cartesian}, , L{Vector3d},

372 C{Vector3Tuple} or C{Vector4Tuple}).

373 @arg radius3: Radius of the 3rd sphere (same C{units} as this C{x}, C{y}

374 and C{z}).

375 @kwarg eps: Pertubation tolerance (C{scalar}), same units as C{x}, C{y}

376 and C{z} or C{None} for no pertubations.

377

378 @return: 2-Tuple with two trilaterated points, each an instance of this

379 (sub-)class. Both points are the same instance if all three

380 spheres intersect or abut in a single point.

381

382 @raise ImportError: Package C{numpy} not found, not installed or

383 older than version 1.10.

384

385 @raise IntersectionError: Near-concentric, -colinear, too distant or

386 non-intersecting spheres or C{numpy} issue.

387

388 @raise NumPyError: Some C{numpy} issue.

389

390 @raise TypeError: Invalid B{C{center2}} or B{C{center3}}.

391

392 @raise UnitError: Invalid B{C{radius}}, B{C{radius2}} or B{C{radius3}}.

393

394 @note: Package U{numpy<https://PyPI.org/project/numpy>} is required,

395 version 1.10 or later.

396

397 @see: Norrdine, A. U{I{An Algebraic Solution to the Multilateration

398 Problem}<https://www.ResearchGate.net/publication/275027725>}

399 and U{I{implementation}<https://www.ResearchGate.net/publication/288825016>}.

400 '''

401 try:

402 c1 = _otherV3d(center=self, NN_OK=False)

403 return _vector2d._trilaterate3d2(c1, Radius_(radius, low=eps),

404 center2, radius2,

405 center3, radius3,

406 eps=eps, clas=self.classof)

407 except (AssertionError, TypeError, ValueError) as x:

408 raise _xError(x, center=self, radius=radius,

409 center2=center2, radius2=radius2,

410 center3=center3, radius3=radius3)

411

412

413 def _intersect3d3(start1, end1, start2, end2, eps=EPS, useZ=False): # MCCABE 16 in .formy.intersection2, .rhumbBase

414 # (INTERNAL) Intersect two lines, see L{intersection3d3} below,

415 # separated to allow callers to embellish any exceptions

416

417 def _corners2(s1, b1, s2, useZ):

418 # Get the C{s1'} and C{e1'} corners of a right-angle

419 # triangle with the hypotenuse thru C{s1} at bearing

420 # C{b1} and the right angle at C{s2}

421 dx, dy, d = s2.minus(s1).xyz3

422 if useZ and not isnear0(d): # not supported

423 raise IntersectionError(useZ=d, bearing=b1)

424 s, c = sincos2d(b1)

425 if s and c:

426 dx *= c / s

427 dy *= s / c

428 e1 = Vector3d(s2.x, s1.y + dx, s1.z)

429 s1 = Vector3d(s1.x + dy, s2.y, s1.z)

430 else: # orthogonal

431 d = euclid(dx, dy) # hypot?

432 e1 = Vector3d(s1.x + s * d, s1.y + c * d, s1.z)

433 return s1, e1

434

435 def _outside(t, d2, o): # -o before start#, +o after end#

436 return -o if t < 0 else (o if t > d2 else 0) # XXX d2 + eps?

437

438 s1 = t = _otherV3d(useZ=useZ, start1=start1)

439 s2 = _otherV3d(useZ=useZ, start2=start2)

440 b1 = _isDegrees(end1)

441 if b1: # bearing, make an e1

442 s1, e1 = _corners2(s1, end1, s2, useZ)

443 else:

444 e1 = _otherV3d(useZ=useZ, end1=end1)

445 b2 = _isDegrees(end2)

446 if b2: # bearing, make an e2

447 s2, e2 = _corners2(s2, end2, t, useZ)

448 else:

449 e2 = _otherV3d(useZ=useZ, end2=end2)

450

451 a = e1.minus(s1)

452 b = e2.minus(s2)

453 c = s2.minus(s1)

454

455 ab = a.cross(b)

456 d = fabs(c.dot(ab))

457 e = max(EPS0, eps or _0_0)

458 if d > EPS0 and ab.length > e: # PYCHOK no cover

459 d = d / ab.length # /= chokes PyChecker

460 if d > e: # argonic, skew lines distance

461 raise IntersectionError(skew_d=d, txt=_no_(_intersection_))

462

463 # co-planar, non-skew lines

464 ab2 = ab.length2

465 if ab2 < e: # colinear, parallel or null line(s)

466 x = a.length2 > b.length2

467 if x: # make C{a} the shortest

468 a, b = b, a

469 s1, s2 = s2, s1

470 e1, e2 = e2, e1

471 b1, b2 = b2, b1

472 if b.length2 < e: # PYCHOK no cover

473 if c.length < e:

474 return s1, 0, 0

475 elif e2.minus(e1).length < e:

476 return e1, 0, 0

477 elif a.length2 < e: # null (s1, e1), non-null (s2, e2)

478 # like _nearestOn2(s1, s2, e2, within=False, eps=e)

479 t = s1.minus(s2).dot(b)

480 v = s2.plus(b.times(t / b.length2))

481 if s1.minus(v).length < e:

482 o = 0 if b2 else _outside(t, b.length2, 1 if x else 2)

483 return (v, o, 0) if x else (v, 0, o)

484 raise IntersectionError(length2=ab2, txt=_no_(_intersection_))

485

486 cb = c.cross(b)

487 t = cb.dot(ab)

488 o1 = 0 if b1 else _outside(t, ab2, 1)

489 v = s1.plus(a.times(t / ab2))

490 t = v.minus(s2).dot(b)

491 o2 = 0 if b2 else _outside(t, b.length2, 2)

492 return v, o1, o2

493

494

495 def intersection3d3(start1, end1, start2, end2, eps=EPS, useZ=True,

496 **Vector_and_kwds):

497 '''Compute the intersection point of two (2- or 3-D) lines, each defined

498 by two points or by a point and a bearing.

499

500 @arg start1: Start point of the first line (C{Cartesian}, L{Vector3d},

501 C{Vector3Tuple} or C{Vector4Tuple}).

502 @arg end1: End point of the first line (C{Cartesian}, L{Vector3d},

503 C{Vector3Tuple} or C{Vector4Tuple}) or the bearing at

504 B{C{start1}} (compass C{degrees}).

505 @arg start2: Start point of the second line (C{Cartesian}, L{Vector3d},

506 C{Vector3Tuple} or C{Vector4Tuple}).

507 @arg end2: End point of the second line (C{Cartesian}, L{Vector3d},

508 C{Vector3Tuple} or C{Vector4Tuple}) or the bearing at

509 B{C{start2}} (Ccompass C{degrees}).

510 @kwarg eps: Tolerance for skew line distance and length (C{EPS}).

511 @kwarg useZ: If C{True}, use the Z components, otherwise force C{z=INT0} (C{bool}).

512 @kwarg Vector_and_kwds: Optional class C{B{Vector}=None} to return the intersection

513 points and optional, additional B{C{Vector}} keyword arguments,

514 otherwise B{C{start1}}'s (sub-)class.

515

516 @return: An L{Intersection3Tuple}C{(point, outside1, outside2)} with C{point}

517 an instance of B{C{Vector}} or B{C{start1}}'s (sub-)class.

518

519 @note: The C{outside} values is C{0} for lines specified by point and bearing.

520

521 @raise IntersectionError: Invalid, skew, non-co-planar or otherwise non-intersecting lines.

522

523 @see: U{Line-line intersection<https://MathWorld.Wolfram.com/Line-LineIntersection.html>}

524 and U{line-line distance<https://MathWorld.Wolfram.com/Line-LineDistance.html>},

525 U{skew lines<https://MathWorld.Wolfram.com/SkewLines.html>} and U{point-line

526 distance<https://MathWorld.Wolfram.com/Point-LineDistance3-Dimensional.html>}.

527 '''

528 try:

529 v, o1, o2 = _intersect3d3(start1, end1, start2, end2, eps=eps, useZ=useZ)

530 except (TypeError, ValueError) as x:

531 raise _xError(x, start1=start1, end1=end1, start2=start2, end2=end2)

532 v = _nVc(v, **_xkwds(Vector_and_kwds, clas=start1.classof,

533 name=typename(intersection3d3)))

534 return Intersection3Tuple(v, o1, o2)

535

536

537 def intersections2(center1, radius1, center2, radius2, sphere=True, **Vector_and_kwds):

538 '''Compute the intersection of two spheres or circles, each defined by a (3-D)

539 center point and a radius.

540

541 @arg center1: Center of the first sphere or circle (C{Cartesian}, L{Vector3d},

542 C{Vector3Tuple} or C{Vector4Tuple}).

543 @arg radius1: Radius of the first sphere or circle (same units as the

544 B{C{center1}} coordinates).

545 @arg center2: Center of the second sphere or circle (C{Cartesian}, L{Vector3d},

546 C{Vector3Tuple} or C{Vector4Tuple}).

547 @arg radius2: Radius of the second sphere or circle (same units as the

548 B{C{center1}} and B{C{center2}} coordinates).

549 @kwarg sphere: If C{True}, compute the center and radius of the intersection of

550 two spheres. If C{False}, ignore the C{z}-component and compute

551 the intersection of two circles (C{bool}).

552 @kwarg Vector_and_kwds: Optional class C{B{Vector}=None} to return the intersection

553 points and optionally, additional B{C{Vector}} keyword arguments,

554 otherwise B{C{center1}}'s (sub-)class.

555

556 @return: If C{B{sphere} is True}, a 2-tuple of the C{center} and C{radius} of the

557 intersection of the I{spheres}. For abutting circles, C{radius} is C{0.0}

558 and C{center} is the I{radical center}.

559

560 If C{B{sphere} is False}, a 2-tuple with the two intersection points of the

561 I{circles}. For abutting circles, both points are the same instance, aka

562 the I{radical center}.

563

564 @raise IntersectionError: Concentric, invalid or non-intersecting spheres or circles.

565

566 @raise TypeError: Invalid B{C{center1}} or B{C{center2}}.

567

568 @raise UnitError: Invalid B{C{radius1}} or B{C{radius2}}.

569

570 @see: U{Sphere-Sphere<https://MathWorld.Wolfram.com/Sphere-SphereIntersection.html>} and

571 U{Circle-Circle<https://MathWorld.Wolfram.com/Circle-CircleIntersection.html>}

572 Intersection.

573 '''

574 try:

575 return _intersects2(center1, Radius_(radius1=radius1),

576 center2, Radius_(radius2=radius2), sphere=sphere,

577 clas=center1.classof, **Vector_and_kwds)

578 except (TypeError, ValueError) as x:

579 raise _xError(x, center1=center1, radius1=radius1, center2=center2, radius2=radius2)

580

581

582 def _intersects2(center1, r1, center2, r2, sphere=True, too_d=None, # in CartesianEllipsoidalBase.intersections2,

583 **clas_Vector_and_kwds): # .ellipsoidalBaseDI._intersections2, .formy.intersections2

584 # (INTERNAL) Intersect two spheres or circles, see L{intersections2}

585 # above, separated to allow callers to embellish any exceptions

586

587 def _nV3(x, y, z):

588 v = Vector3d(x, y, z)

589 n = typename(intersections2)

590 return _nVc(v, **_xkwds(clas_Vector_and_kwds, name=n))

591

592 def _xV3(c1, u, x, y):

593 xy1 = x, y, _1_0 # transform to original space

594 return _nV3(fdot(xy1, u.x, -u.y, c1.x),

595 fdot(xy1, u.y, u.x, c1.y), _0_0)

596

597 c1 = _otherV3d(useZ=sphere, center1=center1)

598 c2 = _otherV3d(useZ=sphere, center2=center2)

599

600 if r1 < r2: # r1, r2 == R, r

601 c1, c2 = c2, c1

602 r1, r2 = r2, r1

603

604 m = c2.minus(c1)

605 d = m.length

606 if d < max(r2 - r1, EPS):

607 raise IntersectionError(_near_(_concentric_)) # XXX ConcentricError?

608

609 o = _MODS.fsums.fsum1_(-d, r1, r2) # overlap == -(d - (r1 + r2))

610 # compute intersections with c1 at (0, 0) and c2 at (d, 0), like

611 # <https://MathWorld.Wolfram.com/Circle-CircleIntersection.html>

612 if o > EPS: # overlapping, r1, r2 == R, r

613 x = _MODS.formy._radical2(d, r1, r2).xline

614 y = _1_0 - (x / r1)**2

615 if y > EPS:

616 y = r1 * sqrt(y) # y == a / 2

617 elif y < 0: # PYCHOK no cover

618 raise IntersectionError(_negative_)

619 else: # abutting

620 y = _0_0

621 elif o < 0: # PYCHOK no cover

622 if too_d is not None:

623 d = too_d

624 raise IntersectionError(_too_(Fmt.distant(d)))

625 else: # abutting

626 x, y = r1, _0_0

627

628 u = m.unit()

629 if sphere: # sphere center and radius

630 c = c1 if x < EPS else (

631 c2 if x > EPS1 else c1.plus(u.times(x)))

632 t = _nV3(c.x, c.y, c.z), Radius(y)

633

634 elif y > 0: # intersecting circles

635 t = _xV3(c1, u, x, y), _xV3(c1, u, x, -y)

636 else: # abutting circles

637 t = _xV3(c1, u, x, 0)

638 t = t, t

639 return t

640

641

642 def iscolinearWith(point, point1, point2, eps=EPS, useZ=True):

643 '''Check whether a point is colinear with two other (2- or 3-D) points.

644

645 @arg point: The point (L{Vector3d}, C{Vector3Tuple} or C{Vector4Tuple}).

646 @arg point1: First point (L{Vector3d}, C{Vector3Tuple} or C{Vector4Tuple}).

647 @arg point2: Second point (L{Vector3d}, C{Vector3Tuple} or C{Vector4Tuple}).

648 @kwarg eps: Tolerance (C{scalar}), same units as C{x}, C{y} and C{z}.

649 @kwarg useZ: If C{True}, use the Z components, otherwise force C{z=INT0} (C{bool}).

650

651 @return: C{True} if B{C{point}} is colinear B{C{point1}} and B{C{point2}}, C{False}

652 otherwise.

653

654 @raise TypeError: Invalid B{C{point}}, B{C{point1}} or B{C{point2}}.

655

656 @see: Function L{nearestOn}.

657 '''

658 p = _otherV3d(useZ=useZ, point=point)

659 return _vector2d._iscolinearWith(p, point1, point2, eps=eps, useZ=useZ)

660

661

662 def nearestOn(point, point1, point2, within=True, useZ=True, Vector=None, **Vector_kwds):

663 '''Locate the point between two points closest to a reference (2- or 3-D).

664

665 @arg point: Reference point (C{Cartesian}, L{Vector3d}, C{Vector3Tuple}

666 or C{Vector4Tuple}).

667 @arg point1: Start point (C{Cartesian}, L{Vector3d}, C{Vector3Tuple} or

668 C{Vector4Tuple}).

669 @arg point2: End point (C{Cartesian}, L{Vector3d}, C{Vector3Tuple} or

670 C{Vector4Tuple}).

671 @kwarg within: If C{True}, return the closest point between both given

672 points, otherwise the closest point on the extended line

673 through both points (C{bool}).

674 @kwarg useZ: If C{True}, use the Z components, otherwise force C{z=INT0} (C{bool}).

675 @kwarg Vector: Class to return closest point (C{Cartesian}, L{Vector3d} or

676 C{Vector3Tuple}) or C{None}.

677 @kwarg Vector_kwds: Optional, additional B{C{Vector}} keyword arguments,

678 ignored if C{B{Vector} is None}.

679

680 @return: Closest point, either B{C{point1}} or B{C{point2}} or an instance

681 of the B{C{point}}'s (sub-)class or B{C{Vector}} if not C{None}.

682

683 @raise TypeError: Invalid B{C{point}}, B{C{point1}} or B{C{point2}}.

684

685 @see: U{3-D Point-Line Distance<https://MathWorld.Wolfram.com/Point-LineDistance3-Dimensional.html>},

686 C{Cartesian} and C{LatLon} methods C{nearestOn}, method L{sphericalTrigonometry.LatLon.nearestOn3}

687 and function L{sphericalTrigonometry.nearestOn3}.

688 '''

689 p0 = _otherV3d(useZ=useZ, point =point)

690 p1 = _otherV3d(useZ=useZ, point1=point1)

691 p2 = _otherV3d(useZ=useZ, point2=point2)

692

693 p, _ = _nearestOn2(p0, p1, p2, within=within)

694 if Vector is not None:

695 p = Vector(p.x, p.y, **_xkwds(Vector_kwds, z=p.z, name__=nearestOn))

696 elif p is p1:

697 p = point1

698 elif p is p2:

699 p = point2

700 else: # ignore Vector_kwds

701 p = point.classof(p.x, p.y, _xkwds_get(Vector_kwds, z=p.z), name__=nearestOn)

702 return p

703

704

705 def _nearestOn2(p0, p1, p2, within=True, eps=EPS):

706 # (INTERNAL) Closest point and fraction, see L{nearestOn} above,

707 # separated to allow callers to embellish any exceptions

708 p21 = p2.minus(p1)

709 d2 = p21.length2

710 if d2 < eps: # coincident

711 p = p1 # ~= p2

712 t = 0

713 else: # see comments in .points.nearestOn5

714 t = p0.minus(p1).dot(p21) / d2

715 if within and t < eps:

716 p = p1

717 t = 0

718 elif within and t > (_1_0 - eps):

719 p = p2

720 t = 1

721 else:

722 p = p1.plus(p21.times(t))

723 return NearestOn2Tuple(p, t)

724

725

726 def nearestOn6(point, points, closed=False, useZ=True, **Vector_and_kwds): # eps=EPS

727 '''Locate the point on a path or polygon closest to a reference point.

728

729 The closest point on each polygon edge is either the nearest of that

730 edge's end points or a point in between.

731

732 @arg point: Reference point (C{Cartesian}, L{Vector3d}, C{Vector3Tuple} or

733 C{Vector4Tuple}).

734 @arg points: The path or polygon points (C{Cartesian}, L{Vector3d},

735 C{Vector3Tuple} or C{Vector4Tuple}[]).

736 @kwarg closed: Optionally, close the path or polygon (C{bool}).

737 @kwarg useZ: If C{True}, use the Z components, otherwise force C{z=INT0} (C{bool}).

738 @kwarg Vector_and_kwds: Optional class C{B{Vector}=None} to return the closest

739 point and optionally, additional B{C{Vector}} keyword arguments,

740 otherwise B{C{point}}'s (sub-)class.

741

742 @return: A L{NearestOn6Tuple}C{(closest, distance, fi, j, start, end)} with the

743 C{closest}, the C{start} and the C{end} point each an instance of the

744 B{C{Vector}} keyword argument or if {B{Vector}=None} or not specified,

745 an instance of the reference B{C{point}}'s (sub-)class.

746

747 @raise PointsError: Insufficient number of B{C{points}}

748

749 @raise TypeError: Non-cartesian B{C{point}} and B{C{points}}.

750

751 @note: Distances measured with method L{Vector3d.equirectangular}. For

752 geodetic distances use function L{nearestOn5} or one of the

753 C{LatLon.nearestOn6} methods.

754 '''

755 r = _otherV3d(useZ=useZ, point=point)

756 D2 = r.equirectangular # distance squared

757

758 Ps = PointsIter(points, loop=1, name=typename(nearestOn6))

759 p1 = c = s = e = _otherV3d(useZ=useZ, i=0, points=Ps[0])

760 c2 = D2(c) # == r.minus(c).length2

761

762 f = i = 0 # p1..p2 == points[i]..[j]

763 for j, p2 in Ps.enumerate(closed=closed):

764 p2 = _otherV3d(useZ=useZ, i=j, points=p2)

765 p, t = _nearestOn2(r, p1, p2) # within=True, eps=EPS

766 d2 = D2(p) # == r.minus(p).length2

767 if d2 < c2:

768 c2, c, s, e, f = d2, p, p1, p2, (i + t)

769 p1, i = p2, j

770

771 f, j = _fi_j2(f, len(Ps)) # like .ellipsoidalBaseDI._nearestOn2_

772

773 kwds = _xkwds(Vector_and_kwds, clas=point.classof, name=Ps.name)

774 v = _nVc(c, **kwds)

775 s = _nVc(s, **kwds) if s is not c else v

776 e = _nVc(e, **kwds) if e is not c else v

777 return NearestOn6Tuple(v, sqrt(c2), f, j, s, e)

778

779

780 def _nVc(v, clas=None, Vector=None, **name_Vector_kwds): # in .vector2d

781 # return a named C{Vector} or C{clas} instance

782 name, kwds = _name2__(**name_Vector_kwds)

783 if Vector is not None:

784 v = Vector(v.x, v.y, v.z, **kwds)

785 elif clas is not None:

786 v = clas(v.x, v.y, v.z) # ignore Vector_kwds

787 return _xnamed(v, name) if name else v

788

789

790 def _otherV3d(useZ=True, NN_OK=True, i=None, **name_vector):

791 # check named vector instance, return Vector3d

792 n, v = _xkwds_item2(name_vector)

793 n = Fmt.INDEX(n, i)

794 if useZ and isinstance(v, Vector3dBase):

795 return v if NN_OK or v.name else v.copy(name=n)

796 try:

797 return Vector3d(v.x, v.y, (v.z if useZ else INT0), name=n)

798 except AttributeError: # no .x, .y or .z attr

799 pass

800 raise _xotherError(Vector3d(0, 0, 0), v, name=n, up=2)

801

802

803 def parse3d(str3d, sep=_COMMA_, Vector=Vector3d, **Vector_kwds):

804 '''Parse an C{"x, y, z"} string.

805

806 @arg str3d: X, y and z values (C{str}).

807 @kwarg sep: Optional separator (C{str}).

808 @kwarg Vector: Optional class (L{Vector3d}).

809 @kwarg Vector_kwds: Optional B{C{Vector}} keyword arguments,

810 ignored if C{B{Vector} is None}.

811

812 @return: A B{C{Vector}} instance or if C{B{Vector} is None},

813 a named L{Vector3Tuple}C{(x, y, z)}.

814

815 @raise VectorError: Invalid B{C{str3d}}.

816 '''

817 try:

818 v = [float(v.strip()) for v in str3d.split(sep)]

819 n = len(v)

820 if n != 3:

821 raise _ValueError(len=n)

822 except (TypeError, ValueError) as x:

823 raise VectorError(str3d=str3d, cause=x)

824 return _xnamed((Vector3Tuple(v) if Vector is None else # *v

825 Vector(*v, **Vector_kwds)), name__=parse3d) # .__name__

826

827

828 def sumOf(vectors, Vector=Vector3d, **Vector_kwds):

829 '''Compute the I{vectorial} sum of two oe more vectors.

830

831 @arg vectors: Vectors to be added (L{Vector3d}[]).

832 @kwarg Vector: Optional class for the vectorial sum (L{Vector3d}).

833 @kwarg Vector_kwds: Optional B{C{Vector}} keyword arguments, ignored

834 if C{B{Vector} is None}.

835

836 @return: Vectorial sum as B{C{Vector}} or if B{C{Vector} is None},

837 a named L{Vector3Tuple}C{(x, y, z)}.

838

839 @raise VectorError: No B{C{vectors}}.

840 '''

841 try:

842 t = _MODS.nvectorBase._nsumOf(vectors, 0, None, {}) # no H

843 except (TypeError, ValueError) as x:

844 raise VectorError(vectors=vectors, Vector=Vector, cause=x)

845 x, y, z = t[:3]

846 return Vector3Tuple(x, y, z, name__=sumOf) if Vector is None else \

847 Vector(x, y, z, **_xkwds(Vector_kwds, name__=sumOf)) # .__name__

848

849

850 def trilaterate3d2(center1, radius1, center2, radius2, center3, radius3,

851 eps=EPS, **Vector_and_kwds):

852 '''Trilaterate three spheres, each given as a (3-D) center and a radius.

853

854 @arg center1: Center of the 1st sphere (C{Cartesian}, L{Vector3d},

855 C{Vector3Tuple} or C{Vector4Tuple}).

856 @arg radius1: Radius of the 1st sphere (same C{units} as C{x}, C{y}

857 and C{z}).

858 @arg center2: Center of the 2nd sphere (C{Cartesian}, L{Vector3d},

859 C{Vector3Tuple} or C{Vector4Tuple}).

860 @arg radius2: Radius of this sphere (same C{units} as C{x}, C{y}

861 and C{z}).

862 @arg center3: Center of the 3rd sphere (C{Cartesian}, L{Vector3d},

863 C{Vector3Tuple} or C{Vector4Tuple}).

864 @arg radius3: Radius of the 3rd sphere (same C{units} as C{x}, C{y}

865 and C{z}).

866 @kwarg eps: Pertubation tolerance (C{scalar}), same units as C{x},

867 C{y} and C{z} or C{None} for no pertubations.

868 @kwarg Vector_and_kwds: Optional class C{B{Vector}=None} to return the

869 trilateration and optionally, additional B{C{Vector}}

870 keyword arguments, otherwise B{C{center1}}'s (sub-)class.

871

872 @return: 2-Tuple with two trilaterated points, each a B{C{Vector}}

873 instance. Both points are the same instance if all three

874 spheres abut/intersect in a single point.

875

876 @raise ImportError: Package C{numpy} not found, not installed or older

877 than version 1.10.

878

879 @raise IntersectionError: Near-concentric, -colinear, too distant or

880 non-intersecting spheres.

881

882 @raise NumPyError: Some C{numpy} issue.

883

884 @raise TypeError: Invalid B{C{center1}}, B{C{center2}} or B{C{center3}}.

885

886 @raise UnitError: Invalid B{C{radius1}}, B{C{radius2}} or B{C{radius3}}.

887

888 @see: Norrdine, A. U{I{An Algebraic Solution to the Multilateration

889 Problem}<https://www.ResearchGate.net/publication/275027725>},

890 the U{I{implementation}<https://www.ResearchGate.net/publication/

891 288825016>} and function L{pygeodesy.trilaterate2d2}.

892 '''

893 try:

894 return _vector2d._trilaterate3d2(_otherV3d(center1=center1, NN_OK=False),

895 Radius_(radius1=radius1, low=eps),

896 center2, radius2, center3, radius3, eps=eps,

897 clas=center1.classof, **Vector_and_kwds)

898 except (AssertionError, TypeError, ValueError) as x:

899 raise _xError(x, center1=center1, radius1=radius1,

900 center2=center2, radius2=radius2,

901 center3=center3, radius3=radius3)

902

903

904 def _xyzhdlln4(xyz, height, datum, ll=None, **name): # in .cartesianBase, .nvectorBase

905 '''(INTERNAL) Get a C{(h, d, ll, name)} 4-tuple.

906 '''

907 _x = _xattr

908 h = height or _x(xyz, height=None) or _x(xyz, h=None) or _x(ll, height=None)

909 d = datum or _x(xyz, datum=None) or _x(ll, datum=None)

910 return h, d, ll, _name__(name, _or_nameof=ll)

911

912

913__all__ += _ALL_DOCS(intersections2, sumOf, Vector3dBase)

914

915# **) MIT License

916#

917# Copyright (C) 2016-2025 -- mrJean1 at Gmail -- All Rights Reserved.

918#

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

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

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

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

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

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

925#

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

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

928#

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

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

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

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

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

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

935# OTHER DEALINGS IN THE SOFTWARE.