1

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

3

4 u'''3-Point resection functions L{cassini}, L{collins5}, L{pierlot}, L{pierlotx} and

5 L{tienstra7}, survey functions L{snellius3} and L{wildberger3} and triangle functions

6 L{triAngle}, L{triAngle5}, L{triSide}, L{triSide2} and L{triSide4}.

7

8@note: Functions L{pierlot} and L{pierlotx} are transcoded to Python with permission from

9 U{triangulationPierlot<http://www.Telecom.ULg.ac.BE/triangulation/doc/total_8c.html>} and

10 U{Pierlot<http://www.Telecom.ULg.ac.BE/publi/publications/pierlot/Pierlot2014ANewThree>}.

11'''

12# make sure int/int division yields float quotient

13 from __future__ import division as _; del _ # PYCHOK semicolon

14

15 from pygeodesy.basics import map1, map2, _zip, _ALL_LAZY

16 from pygeodesy.constants import EPS, EPS0, EPS02, INT0, PI, PI2, PI_2, PI_4, \

17 _0_0, _0_5, _1_0, _N_1_0, _2_0, _N_2_0, _4_0, \

18 _16_0, _180_0, _360_0, _copysign_0_0, isnear0, \

19 _over, _umod_360

20 from pygeodesy.errors import _and, _or, TriangleError, _ValueError, _xcallable, \

21 _xkwds, _xkwds_pop2

22 from pygeodesy.fmath import favg, Fdot, fidw, fmean, hypot, hypot2_

23 from pygeodesy.fsums import Fsum, fsumf_, fsum1, fsum1f_

24 from pygeodesy.interns import _a_, _A_, _area_, _b_, _B_, _c_, _C_, _coincident_, \

25 _colinear_, _d_, _eps_, _invalid_, _negative_, _not_, \

26 _rIn_, _SPACE_

27# from pygeodesy.lazily import _ALL_LAZY # from .basics

28 from pygeodesy.named import _NamedTuple, _Pass, Fmt

29# from pygeodesy.streprs import Fmt # from .named

30 from pygeodesy.units import Degrees, Distance, Radians

31 from pygeodesy.utily import acos1, asin1, sincos2, sincos2_, sincos2d, sincos2d_

32 from pygeodesy.vector3d import _otherV3d, Vector3d

33

34 from math import cos, atan2, degrees, fabs, radians, sin, sqrt

35

36__all__ = _ALL_LAZY.resections

37__version__ = '24.04.04'

38

39_concyclic_ = 'concyclic'

40_PA_ = 'PA'

41_PB_ = 'PB'

42_PC_ = 'PC'

43_pointH_ = 'pointH'

44_pointP_ = 'pointP'

45_positive_ = 'positive'

46_radA_ = 'radA'

47_radB_ = 'radB'

48_radC_ = 'radC'

49

50

51 class Collins5Tuple(_NamedTuple):

52 '''5-Tuple C{(pointP, pointH, a, b, c)} with survey C{pointP}, auxiliary

53 C{pointH}, each an instance of B{C{pointA}}'s (sub-)class and triangle

54 sides C{a}, C{b} and C{c} in C{meter}, conventionally.

55 '''

56 _Names_ = (_pointP_, _pointH_, _a_, _b_, _c_)

57 _Units_ = (_Pass, _Pass, Distance, Distance, Distance)

58

59

60 def _F1(*xs): # class

61 '''(INTERNAL) An L{Fsum}, 1-primed.

62 '''

63 F = Fsum(_1_0, *xs)

64 F += _N_1_0

65 return F

66

67

68 class ResectionError(_ValueError):

69 '''Error raised for issues in L{pygeodesy.resections}.

70 '''

71 pass

72

73

74 class Survey3Tuple(_NamedTuple):

75 '''3-Tuple C{(PA, PB, PC)} with distance from survey point C{P} to each of

76 the triangle corners C{A}, C{B} and C{C} in C{meter}, conventionally.

77 '''

78 _Names_ = (_PA_, _PB_, _PC_)

79 _Units_ = ( Distance, Distance, Distance)

80

81

82 class Tienstra7Tuple(_NamedTuple):

83 '''7-Tuple C{(pointP, A, B, C, a, b, c)} with survey C{pointP}, interior

84 triangle angles C{A}, C{B} and C{C} in C{degrees} and triangle sides

85 C{a}, C{b} and C{c} in C{meter}, conventionally.

86 '''

87 _Names_ = (_pointP_, _A_, _B_, _C_, _a_, _b_, _c_)

88 _Units_ = (_Pass, Degrees, Degrees, Degrees, Distance, Distance, Distance)

89

90

91 class TriAngle5Tuple(_NamedTuple):

92 '''5-Tuple C{(radA, radB, radC, rIn, area)} with the interior angles at

93 triangle corners C{A}, C{B} and C{C} in C{radians}, the C{InCircle}

94 radius C{rIn} aka C{inradius} in C{meter} and the triangle C{area}

95 in C{meter} I{squared}, conventionally.

96 '''

97 _Names_ = (_radA_, _radB_, _radC_, _rIn_, _area_)

98 _Units_ = ( Radians, Radians, Radians, Distance, _Pass)

99

100

101 class TriSide2Tuple(_NamedTuple):

102 '''2-Tuple C{(a, radA)} with triangle side C{a} in C{meter}, conventionally

103 and angle C{radA} at the opposite triangle corner in C{radians}.

104 '''

105 _Names_ = (_a_, _radA_)

106 _Units_ = ( Distance, Radians)

107

108

109 class TriSide4Tuple(_NamedTuple):

110 '''4-Tuple C{(a, b, radC, d)} with interior angle C{radC} at triangle corner

111 C{C} in C{radians}with the length of triangle sides C{a} and C{b} and

112 with triangle height C{d} perpendicular to triangle side C{c}, in the

113 same units as triangle sides C{a} and C{b}.

114 '''

115 _Names_ = (_a_, _b_, _radC_, _d_)

116 _Units_ = ( Distance, Distance, Radians, Distance)

117

118

119 def _ABC3(useZ, pointA, pointB, pointC):

120 '''(INTERNAL) Helper for L{cassini} and L{tienstra7}.

121 '''

122 return (_otherV3d(useZ=useZ, pointA=pointA),

123 _otherV3d(useZ=useZ, pointB=pointB),

124 _otherV3d(useZ=useZ, pointC=pointC))

125

126

127 def _B3(useZ, point1, point2, point3):

128 '''(INTERNAL) Helper for L{pierlot} and L{pierlotx}.

129 '''

130 return (_otherV3d(useZ=useZ, point1=point1),

131 _otherV3d(useZ=useZ, point2=point2),

132 _otherV3d(useZ=useZ, point3=point3))

133

134

135 def cassini(pointA, pointB, pointC, alpha, beta, useZ=False, **Clas_and_kwds):

136 '''3-Point resection using U{Cassini<https://NL.WikiPedia.org/wiki/Achterwaartse_insnijding>}'s method.

137

138 @arg pointA: First point (C{Cartesian}, L{Vector3d}, C{Vector3Tuple},

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

140 @arg pointB: Second point (C{Cartesian}, L{Vector3d}, C{Vector3Tuple},

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

142 @arg pointC: Center point (C{Cartesian}, L{Vector3d}, C{Vector3Tuple},

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

144 @arg alpha: Angle subtended by triangle side B{C{pointA}} to B{C{pointC}}

145 (C{degrees}, non-negative).

146 @arg beta: Angle subtended by triangle side B{C{pointB}} to B{C{pointC}}

147 (C{degrees}, non-negative).

148 @kwarg useZ: If C{True}, use and interpolate the Z component, otherwise

149 force C{z=INT0} (C{bool}).

150 @kwarg Clas_and_kwds: Optional class C{B{Clas}=B{pointA}.classof} to

151 return the survey point with optionally other B{C{Clas}}

152 keyword arguments to instantiate the survey point.

153

154 @note: Typically, B{C{pointC}} is between B{C{pointA}} and B{C{pointB}}.

155

156 @return: The survey point, an instance of B{C{Clas}} or B{C{pointA}}'s

157 (sub-)class.

158

159 @raise ResectionError: Near-coincident, -colinear or -concyclic points

160 or negative or invalid B{C{alpha}} or B{C{beta}}.

161

162 @raise TypeError: Invalid B{C{pointA}}, B{C{pointB}} or B{C{pointM}}.

163

164 @see: U{Three Point Resection Problem<https://Dokumen.tips/documents/

165 three-point-resection-problem-introduction-kaestner-burkhardt-method.html>}

166 and functions L{collins5}, L{pierlot}, L{pierlotx} and L{tienstra7}.

167 '''

168

169 def _H(A, C, sa):

170 s, c = sincos2d(sa)

171 if isnear0(s):

172 raise ValueError(_or(_coincident_, _colinear_))

173 t = s, c, c

174 x = Fdot(t, A.x, C.y, -A.y).fover(s)

175 y = Fdot(t, A.y, -C.x, A.x).fover(s)

176 return x, y

177

178 A, B, C = _ABC3(useZ, pointA, pointB, pointC)

179 try:

180 sa, sb = map1(float, alpha, beta)

181 if min(sa, sb) < 0:

182 raise ValueError(_negative_)

183 if fsumf_(_360_0, -sa, -sb) < EPS0:

184 raise ValueError()

185

186 x1, y1 = _H(A, C, sa)

187 x2, y2 = _H(B, C, -sb)

188

189 x = x1 - x2

190 y = y1 - y2

191 if isnear0(x) or isnear0(y):

192 raise ValueError(_SPACE_(_concyclic_, (x, y)))

193

194 m = y / x

195 n = x / y

196 N = n + m

197 if isnear0(N):

198 raise ValueError(_SPACE_(_concyclic_, (m, n, N)))

199

200 t = n, m, _1_0, _N_1_0

201 x = Fdot(t, C.x, x1, C.y, y1).fover(N)

202 y = Fdot(t, y1, C.y, C.x, x1).fover(N)

203 z = _zidw(x, y, useZ, A, B, C)

204 return _Clas(cassini, pointA, Clas_and_kwds, x, y, z)

205

206 except (TypeError, ValueError) as x:

207 raise ResectionError(pointA=pointA, pointB=pointB, pointC=pointC,

208 alpha=alpha, beta=beta, cause=x)

209

210

211 def _Clas(where, point, Clas_and_kwds, *args):

212 '''(INTERNAL) Return a C{B{Clas}=point.classof} survey point.

213 '''

214 Clas, kwds = _xkwds_pop2(Clas_and_kwds, Clas=point.classof)

215 return Clas(*args, **_xkwds(kwds, name=where.__name__))

216

217

218 def collins5(pointA, pointB, pointC, alpha, beta, useZ=False, **Clas_and_kwds):

219 '''3-Point resection using U{Collins<https://Dokumen.tips/documents/

220 three-point-resection-problem-introduction-kaestner-burkhardt-method.html>}' method.

221

222 @arg pointA: First point (C{Cartesian}, L{Vector3d}, C{Vector3Tuple},

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

224 @arg pointB: Second point (C{Cartesian}, L{Vector3d}, C{Vector3Tuple},

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

226 @arg pointC: Center point (C{Cartesian}, L{Vector3d}, C{Vector3Tuple},

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

228 @arg alpha: Angle subtended by triangle side C{b} from B{C{pointA}} to

229 B{C{pointC}} (C{degrees}, non-negative).

230 @arg beta: Angle subtended by triangle side C{a} from B{C{pointB}} to

231 B{C{pointC}} (C{degrees}, non-negative).

232 @kwarg useZ: If C{True}, use and interpolate the Z component, otherwise

233 force C{z=INT0} (C{bool}).

234 @kwarg Clas_and_kwds: Optional class C{B{Clas}=B{pointA}.classof} to

235 return the survey point with optionally other B{C{Clas}}

236 keyword arguments to instantiate the survey point.

237

238 @note: Typically, B{C{pointC}} is between B{C{pointA}} and B{C{pointB}}.

239

240 @return: L{Collins5Tuple}C{(pointP, pointH, a, b, c)} with survey C{pointP},

241 auxiliary C{pointH}, each an instance of B{C{Clas}} or B{C{pointA}}'s

242 (sub-)class and triangle sides C{a}, C{b} and C{c} in C{meter},

243 conventionally.

244

245 @raise ResectionError: Near-coincident, -colinear or -concyclic points

246 or negative or invalid B{C{alpha}} or B{C{beta}}.

247

248 @raise TypeError: Invalid B{C{pointA}}, B{C{pointB}} or B{C{pointM}}.

249

250 @see: U{Collins' methode<https://NL.WikiPedia.org/wiki/Achterwaartse_insnijding>}

251 and functions L{cassini}, L{pierlot}, L{pierlotx} and L{tienstra7}.

252 '''

253

254 def _azi_len2(A, B, pi2=PI2):

255 v = B.minus(A)

256 r = atan2(v.x, v.y)

257 if r < 0 and pi2:

258 r += pi2

259 return r, v.length

260

261 def _xyz(d, r, A, B, C, useZ):

262 s, c = sincos2(r)

263 x = A.x + d * s

264 y = A.y + d * c

265 z = _zidw(x, y, useZ, A, B, C)

266 return x, y, z

267

268 A, B, C = _ABC3(useZ, pointA, pointB, pointC)

269 try:

270 ra, rb = radians(alpha), radians(beta)

271 if min(ra, rb) < 0:

272 raise ValueError(_negative_)

273

274 sra, srH = sin(ra), sin(ra + rb - PI) # rH = PI - ((PI - ra) + (PI - rb))

275 if isnear0(sra) or isnear0(srH):

276 raise ValueError(_or(_coincident_, _colinear_, _concyclic_))

277

278# za, a = _azi_len2(C, B)

279 zb, b = _azi_len2(C, A)

280 zc, c = _azi_len2(A, B, 0)

281

282# d = c * sin(PI - rb) / srH # B.minus(H).length

283 d = c * sin(PI - ra) / srH # A.minus(H).length

284 r = zc + PI - rb # zh = zc + (PI - rb)

285 H = _xyz(d, r, A, B, C, useZ)

286

287 zh, _ = _azi_len2(C, Vector3d(*H))

288

289# d = a * sin(za - zh) / sin(rb) # B.minus(P).length

290 d = b * sin(zb - zh) / sra # A.minus(P).length

291 r = zh - ra # zb - PI + (PI - ra - (zb - zh))

292 P = _xyz(d, r, A, B, C, useZ)

293 P = _Clas(collins5, pointA, Clas_and_kwds, *P)

294

295 H = _Clas(collins5, pointA, Clas_and_kwds, *H)

296 a = B.minus(C).length

297

298 return Collins5Tuple(P, H, a, b, c, name=collins5.__name__)

299

300 except (TypeError, ValueError) as x:

301 raise ResectionError(pointA=pointA, pointB=pointB, pointC=pointC,

302 alpha=alpha, beta=beta, cause=x)

303

304

305 def pierlot(point1, point2, point3, alpha12, alpha23, useZ=False, eps=EPS,

306 **Clas_and_kwds):

307 '''3-Point resection using U{Pierlot<http://www.Telecom.ULg.ac.BE/publi/publications/

308 pierlot/Pierlot2014ANewThree>}'s method C{ToTal} with I{approximate} limits for

309 the (pseudo-)singularities.

310

311 @arg point1: First point (C{Cartesian}, L{Vector3d}, C{Vector3Tuple},

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

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

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

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

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

317 @arg alpha12: Angle subtended from B{C{point1}} to B{C{point2}} or

318 B{C{alpha2 - alpha1}} (C{degrees}).

319 @arg alpha23: Angle subtended from B{C{point2}} to B{C{point3}} or

320 B{C{alpha3 - alpha2}}(C{degrees}).

321 @kwarg useZ: If C{True}, interpolate the survey point's Z component,

322 otherwise use C{z=INT0} (C{bool}).

323 @kwarg eps: Tolerance for C{cot} (pseudo-)singularities (C{float}).

324 @kwarg Clas_and_kwds: Optional class C{B{Clas}=B{point1}.classof} to

325 return the survey point with optionally other B{C{Clas}}

326 keyword arguments to instantiate the survey point.

327

328 @note: Typically, B{C{point1}}, B{C{point2}} and B{C{point3}} are ordered

329 by angle, modulo 360, counter-clockwise.

330

331 @return: The survey (or robot) point, an instance of B{C{Clas}} or B{C{point1}}'s

332 (sub-)class.

333

334 @raise ResectionError: Near-coincident, -colinear or -concyclic points

335 or invalid B{C{alpha12}} or B{C{alpha23}} or

336 non-positive B{C{eps}}.

337

338 @raise TypeError: Invalid B{C{point1}}, B{C{point2}} or B{C{point3}}.

339

340 @see: I{Pierlot}'s C function U{triangulationPierlot<http://www.Telecom.ULg.ac.BE/

341 triangulation/doc/total_8c_source.html>}, U{V. Pierlot, M. Van Droogenbroeck,

342 "A New Three Object Triangulation Algorithm for Mobile Robot Positioning"

343 <https://ORBi.ULiege.BE/bitstream/2268/157469/1/Pierlot2014ANewThree.pdf>},

344 U{Vincent Pierlot, Marc Van Droogenbroeck, "18 Triangulation Algorithms for 2D

345 Positioning (also known as the Resection Problem)"<http://www.Telecom.ULg.ac.BE/

346 triangulation>} and functions L{pierlotx}, L{cassini}, L{collins5} and L{tienstra7}.

347 '''

348

349 def _cot(s, c): # -eps < I{approximate} cotangent < eps

350 if eps > 0:

351 return c / (min(s, -eps) if s < 0 else max(s, eps))

352 raise ValueError(_SPACE_(_eps_, _not_, _positive_))

353

354 B1, B2, B3 = _B3(useZ, point1, point2, point3)

355 try:

356 xyz = _pierlot3(B1, B2, B3, alpha12, alpha23, useZ, _cot)

357 return _Clas(pierlot, point1, Clas_and_kwds, *xyz)

358

359 except (TypeError, ValueError) as x:

360 raise ResectionError(point1=point1, point2=point2, point3=point3,

361 alpha12=alpha12, alpha23=alpha23, eps=eps, cause=x)

362

363

364 def _pierlot3(B1, B2, B3, a12, a23, useZ, cot):

365 '''(INTERNAL) Shared L{pierlot} and L{pierlotx}.

366 '''

367 x1_, y1_, _ = B1.minus(B2).xyz

368 x3_, y3_, _ = B3.minus(B2).xyz

369

370 s12, c12, s23, c23 = sincos2d_(a12, a23)

371 # cot31 = (1 - cot12 * cot23) / (cot12 + cot32)

372 # = (1 - c12 / s12 * c23 / s23) / (c12 / s12 + c23 / s23)

373 # = (1 - (c12 * c23) / (s12 * s23)) / (c12 * s23 + s12 * c23) / (s12 * s23)

374 # = (s12 * s23 - c12 * c23) / (c12 * s23 + s12 * c23)

375 # = c31 / s31

376 cot31 = cot(fsum1f_(c12 * s23, s12 * c23), # s31

377 fsum1f_(s12 * s23, -c12 * c23)) # c31

378

379 K = _F1(x3_ * x1_, cot31 * (y3_ * x1_),

380 y3_ * y1_, -cot31 * (x3_ * y1_))

381 if K:

382 cot12 = cot(s12, c12)

383 cot23 = cot(s23, c23)

384

385 # x12 = x1_ + cot12 * y1_

386 # y12 = y1_ - cot12 * x1_

387

388 # x23 = x3_ - cot23 * y3_

389 # y23 = y3_ + cot23 * x3_

390

391 # x31 = x3_ + x1_ + cot31 * (y3_ - y1_)

392 # y31 = y3_ + y1_ - cot31 * (x3_ - x1_)

393

394 # x12 - x23 = x1_ + cot12 * y1_ - x3_ + cot23 * y3_

395 X12_23 = _F1(x1_, cot12 * y1_, -x3_, cot23 * y3_)

396 # y12 - y23 = y1_ - cot12 * x1_ - y3_ - cot23 * x3_

397 Y12_23 = _F1(y1_, -cot12 * x1_, -y3_, -cot23 * x3_)

398

399 # x31 - x23 = x3_ + x1_ + cot31 * (y3_ - y1_) - x3_ + cot23 * y3_

400 # = x1_ + cot31 * y3_ - cot31 * y1_ + cot23 * y3_

401 X31_23 = _F1(x1_, -cot31 * y1_, cot31 * y3_, cot23 * y3_)

402 # y31 - y23 = y3_ + y1_ - cot31 * (x3_ - x1_) - y3_ - cot23 * x3_

403 # = y1_ - cot31 * x3_ + cot31 * x1_ - cot23 * x3_

404 Y31_23 = _F1(y1_, cot31 * x1_, -cot31 * x3_, -cot23 * x3_)

405

406 # d = (x12 - x23) * (y23 - y31) + (x31 - x23) * (y12 - y23)

407 # = (x31 - x23) * (y12 - y23) - (x12 - x23) * (y31 - y23)

408 # x = (d * B2.x + K * Y12_23).fover(d)

409 # y = (d * B2.y - K * X12_23).fover(d)

410 x, y = _pierlotxy2(B2, -K, Y12_23, X12_23, (X31_23 * Y12_23 -

411 X12_23 * Y31_23))

412 else:

413 x, y, _ = B2.xyz

414 return x, y, _zidw(x, y, useZ, B1, B2, B3)

415

416

417 def pierlotx(point1, point2, point3, alpha1, alpha2, alpha3, useZ=False,

418 **Clas_and_kwds):

419 '''3-Point resection using U{Pierlot<http://www.Telecom.ULg.ac.BE/publi/

420 publications/pierlot/Pierlot2014ANewThree>}'s method C{ToTal} with

421 I{exact} limits for the (pseudo-)singularities.

422

423 @arg point1: First point (C{Cartesian}, L{Vector3d}, C{Vector3Tuple},

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

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

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

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

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

429 @arg alpha1: Angle at B{C{point1}} (C{degrees}, counter-clockwise).

430 @arg alpha2: Angle at B{C{point2}} (C{degrees}, counter-clockwise).

431 @arg alpha3: Angle at B{C{point3}} (C{degrees}, counter-clockwise).

432 @kwarg useZ: If C{True}, interpolate the survey point's Z component,

433 otherwise use C{z=INT0} (C{bool}).

434 @kwarg Clas_and_kwds: Optional class C{B{Clas}=B{point1}.classof} to

435 return the survey point with optionally other B{C{Clas}}

436 keyword arguments to instantiate the survey point.

437

438 @return: The survey (or robot) point, an instance of B{C{Clas}} or B{C{point1}}'s

439 (sub-)class.

440

441 @raise ResectionError: Near-coincident, -colinear or -concyclic points or

442 invalid B{C{alpha1}}, B{C{alpha2}} or B{C{alpha3}}.

443

444 @raise TypeError: Invalid B{C{point1}}, B{C{point2}} or B{C{point3}}.

445

446 @see: I{Pierlot}'s C function U{triangulationPierlot2<http://www.Telecom.ULg.ac.BE/

447 triangulation/doc/total_8c_source.html>} and function L{pierlot}, L{cassini},

448 L{collins5} and L{tienstra7}.

449 '''

450

451 def _a_z_Bs(Bs, *alphas):

452 ds = map2(_umod_360, alphas) # 0 <= alphas < 360

453 ds, Bs = zip(*sorted(_zip(ds, Bs))) # unzip

454 for p, d, B in _zip(ds, _rotate(ds), Bs):

455 d -= p # a12 = a2 - a1, ...

456 z = isnear0(fabs(d) % _180_0)

457 yield d, z, B

458

459 def _cot(s, c): # I{exact} cotangent

460 try:

461 return (c / s) if c else _copysign_0_0(s)

462 except ZeroDivisionError:

463 raise ValueError(_or(_coincident_, _colinear_))

464

465 Bs = _B3(useZ, point1, point2, point3)

466 try:

467 Cs = [0] # pseudo-global, passing the exception Case

468 xyz = _pierlotx3(_a_z_Bs(Bs, alpha1, alpha2, alpha3),

469 useZ, _cot, Cs.append)

470 return _Clas(pierlotx, point1, Clas_and_kwds, *xyz)

471

472 except (TypeError, ValueError) as x:

473 raise ResectionError(point1=point1, point2=point2, point3=point3, C=Cs.pop(),

474 alpha1=alpha1, alpha2=alpha2, alpha3=alpha3, cause=x)

475

476

477 def _pierlotx3(a_z_Bs, useZ, cot, Cs):

478 '''(INTERNAL) Core of L{pierlotx}.

479 '''

480 (a12, z12, B1), \

481 (a23, z23, B2), \

482 (a31, z31, B3) = a_z_Bs

483 if z12 and not z23:

484 Cs(1)

485 elif z23 and not z31:

486 Cs(2)

487 a23, B1, B2, B3 = a31, B2, B3, B1

488 elif z31 and not z12:

489 Cs(3)

490 a23, B2, B3 = a12, B3, B2

491 else:

492 Cs(4)

493 return _pierlot3(B1, B2, B3, a12, a23, useZ, cot)

494

495 x1_, y1_, _ = B1.minus(B3).xyz

496 x2_, y2_, _ = B2.minus(B3).xyz

497

498 K = _F1(y1_ * x2_, -x1_ * y2_)

499 if K:

500 cot23 = cot(*sincos2d(a23))

501

502 # x23 = x2_ + cot23 * y2_

503 # y23 = y2_ - cot23 * x2_

504

505 # x31 = x1_ + cot23 * y1_

506 # y31 = y1_ - cot23 * x1_

507

508 # x31 - x23 = x1_ + cot23 * y1_ - x2_ - cot23 * y2_

509 X31_23 = _F1(x1_, cot23 * y1_, -x2_, -cot23 * y2_)

510 # y31 - y23 = y1_ - cot23 * x1_ - y2_ + cot23 * x2_

511 Y31_23 = _F1(y1_, -cot23 * x1_, -y2_, cot23 * x2_)

512

513 # d = (x31 - x23) * (x2_ - x1_) + (y31 - y23) * (y2_ - y1_)

514 # x = (D * B3.x - K * Y31_23).fover(d)

515 # y = (D * B3.y + K * X31_23).fover(d)

516 x, y = _pierlotxy2(B3, K, Y31_23, X31_23, (X31_23 * _F1(x2_, -x1_) +

517 Y31_23 * _F1(y2_, -y1_)))

518 else:

519 x, y, _ = B3.xyz

520 return x, y, _zidw(x, y, useZ, B1, B2, B3)

521

522

523 def _pierlotxy2(B, K, X, Y, D):

524 '''(INTERNAL) Helper for C{_pierlot3} and C{_pierlotx3}.

525 '''

526 d = float(D)

527 if isnear0(d):

528 raise ValueError(_or(_coincident_, _colinear_, _concyclic_))

529 x = (D * B.x - K * X).fover(d)

530 y = (D * B.y + K * Y).fover(d)

531 return x, y

532

533

534 def _rotate(xs, n=1):

535 '''Rotate list or tuple C{xs} by C{n} items, right if C{n > 0} else left.

536 '''

537 return xs[n:] + xs[:n]

538

539

540 def snellius3(a, b, degC, alpha, beta):

541 '''Snellius' surveying using U{Snellius Pothenot<https://WikiPedia.org/wiki/Snellius–Pothenot_problem>}.

542

543 @arg a: Length of the triangle side between corners C{B} and C{C} and opposite of

544 triangle corner C{A} (C{scalar}, non-negative C{meter}, conventionally).

545 @arg b: Length of the triangle side between corners C{C} and C{A} and opposite of

546 triangle corner C{B} (C{scalar}, non-negative C{meter}, conventionally).

547 @arg degC: Angle at triangle corner C{C}, opposite triangle side C{c} (non-negative C{degrees}).

548 @arg alpha: Angle subtended by triangle side B{C{b}} (non-negative C{degrees}).

549 @arg beta: Angle subtended by triangle side B{C{a}} (non-negative C{degrees}).

550

551 @return: L{Survey3Tuple}C{(PA, PB, PC)} with distance from survey point C{P} to

552 each of the triangle corners C{A}, C{B} and C{C}, same units as triangle

553 sides B{C{a}}, B{C{b}} and B{C{c}}.

554

555 @raise TriangleError: Invalid B{C{a}}, B{C{b}} or B{C{degC}} or negative B{C{alpha}}

556 or B{C{beta}}.

557

558 @see: Function L{wildberger3}.

559 '''

560 try:

561 a, b, degC, alpha, beta = t = map1(float, a, b, degC, alpha, beta)

562 if min(t) < 0:

563 raise ValueError(_negative_)

564 ra, rb, rC = map1(radians, alpha, beta, degC)

565

566 r = fsum1f_(ra, rb, rC) * _0_5

567 k = PI - r

568 if min(k, r) < 0:

569 raise ValueError(_or(_coincident_, _colinear_))

570

571 sa, sb = map1(sin, ra, rb)

572 p = atan2(sa * a, sb * b)

573 sp, cp, sr, cr = sincos2_(PI_4 - p, r)

574 p = atan2(sp * sr, cp * cr)

575 pa = k + p

576 pb = k - p

577

578 if fabs(sb) > fabs(sa):

579 pc = fabs(a * sin(pb) / sb)

580 elif sa:

581 pc = fabs(b * sin(pa) / sa)

582 else:

583 raise ValueError(_or(_colinear_, _coincident_))

584

585 pa = _triSide(b, pc, fsumf_(PI, -ra, -pa))

586 pb = _triSide(a, pc, fsumf_(PI, -rb, -pb))

587 return Survey3Tuple(pa, pb, pc, name=snellius3.__name__)

588

589 except (TypeError, ValueError) as x:

590 raise TriangleError(a=a, b=b, degC=degC, alpha=alpha, beta=beta, cause=x)

591

592

593 def tienstra7(pointA, pointB, pointC, alpha, beta=None, gamma=None,

594 useZ=False, **Clas_and_kwds):

595 '''3-Point resection using U{Tienstra<https://WikiPedia.org/wiki/Tienstra_formula>}'s formula.

596

597 @arg pointA: First point (C{Cartesian}, L{Vector3d}, C{Vector3Tuple}, C{Vector4Tuple} or

598 C{Vector2Tuple} if C{B{useZ}=False}).

599 @arg pointB: Second point (C{Cartesian}, L{Vector3d}, C{Vector3Tuple}, C{Vector4Tuple} or

600 C{Vector2Tuple} if C{B{useZ}=False}).

601 @arg pointC: Third point (C{Cartesian}, L{Vector3d}, C{Vector3Tuple}, C{Vector4Tuple} or

602 C{Vector2Tuple} if C{B{useZ}=False}).

603 @arg alpha: Angle subtended by triangle side C{a} from B{C{pointB}} to B{C{pointC}}

604 (C{degrees}, non-negative).

605 @kwarg beta: Angle subtended by triangle side C{b} from B{C{pointA}} to B{C{pointC}}

606 (C{degrees}, non-negative) or C{None} if C{B{gamma} is not None}.

607 @kwarg gamma: Angle subtended by triangle side C{c} from B{C{pointA}} to B{C{pointB}}

608 (C{degrees}, non-negative) or C{None} if C{B{beta} is not None}.

609 @kwarg useZ: If C{True}, use and interpolate the Z component, otherwise force C{z=INT0}

610 (C{bool}).

611 @kwarg Clas_and_kwds: Optional class C{B{Clas}=B{pointA}.classof} to return the survey

612 point with optionally other B{C{Clas}} keyword arguments to instantiate

613 the survey point.

614

615 @note: Points B{C{pointA}}, B{C{pointB}} and B{C{pointC}} are ordered clockwise.

616

617 @return: L{Tienstra7Tuple}C{(pointP, A, B, C, a, b, c)} with survey C{pointP}, an

618 instance of B{C{Clas}} or B{C{pointA}}'s (sub-)class, with triangle angles C{A}

619 at B{C{pointA}}, C{B} at B{C{pointB}} and C{C} at B{C{pointC}} in C{degrees}

620 and with triangle sides C{a}, C{b} and C{c} in C{meter}, conventionally.

621

622 @raise ResectionError: Near-coincident, -colinear or -concyclic points or sum of

623 B{C{alpha}}, B{C{beta}} and B{C{gamma}} not C{360} or negative

624 B{C{alpha}}, B{C{beta}} or B{C{gamma}}.

625

626 @raise TypeError: Invalid B{C{pointA}}, B{C{pointB}} or B{C{pointC}}.

627

628 @see: U{3-Point Resection Solver<http://MesaMike.org/geocache/GC1B0Q9/tienstra/>},

629 U{V. Pierlot, M. Van Droogenbroeck, "A New Three Object Triangulation..."

630 <http://www.Telecom.ULg.ac.BE/publi/publications/pierlot/Pierlot2014ANewThree/>},

631 U{18 Triangulation Algorithms...<http://www.Telecom.ULg.ac.BE/triangulation/>} and

632 functions L{cassini}, L{collins5}, L{pierlot} and L{pierlotx}.

633 '''

634

635 def _deg_ks(r, s, ks, N):

636 if isnear0(fsumf_(PI, r, -s)): # r + (PI2 - s) == PI

637 raise ValueError(Fmt.PARENSPACED(concyclic=N))

638 # k = 1 / (cot(r) - cot(s))

639 # = 1 / (cos(r) / sin(r) - cos(s) / sin(s))

640 # = 1 / (cos(r) * sin(s) - cos(s) * sin(r)) / (sin(r) * sin(s))

641 # = sin(r) * sin(s) / (cos(r) * sin(s) - cos(s) * sin(r))

642 sr, cr, ss, cs = sincos2_(r, s)

643 c = fsum1f_(cr * ss, -cs * sr)

644 if isnear0(c):

645 raise ValueError(Fmt.PARENSPACED(cotan=N))

646 ks.append(sr * ss / c)

647 return Degrees(degrees(r), name=N) # C degrees

648

649 A, B, C = _ABC3(useZ, pointA, pointB, pointC)

650 try:

651 sa, sb, sc = map1(radians, alpha, (beta or 0), (gamma or 0))

652 if beta is None:

653 if gamma is None:

654 raise ValueError(_and(Fmt.EQUAL(beta=beta), Fmt.EQUAL(gamma=gamma)))

655 sb = fsumf_(PI2, -sa, -sc)

656 elif gamma is None:

657 sc = fsumf_(PI2, -sa, -sb)

658 else: # subtended angles must add to 360 degrees

659 r = fsum1f_(sa, sb, sc)

660 if fabs(r - PI2) > EPS:

661 raise ValueError(Fmt.EQUAL(sum=degrees(r)))

662 if min(sa, sb, sc) < 0:

663 raise ValueError(_negative_)

664

665 # triangle sides

666 a = B.minus(C).length

667 b = A.minus(C).length

668 c = A.minus(B).length

669

670 ks = [] # 3 Ks and triangle angles

671 dA = _deg_ks(_triAngle(b, c, a), sa, ks, _A_)

672 dB = _deg_ks(_triAngle(a, c, b), sb, ks, _B_)

673 dC = _deg_ks(_triAngle(a, b, c), sc, ks, _C_)

674

675 k = fsum1(ks, floats=True)

676 if isnear0(k):

677 raise ValueError(Fmt.EQUAL(K=k))

678 x = Fdot(ks, A.x, B.x, C.x).fover(k)

679 y = Fdot(ks, A.y, B.y, C.y).fover(k)

680 z = _zidw(x, y, useZ, A, B, C)

681

682 P = _Clas(tienstra7, pointA, Clas_and_kwds, x, y, z)

683 return Tienstra7Tuple(P, dA, dB, dC, a, b, c, name=tienstra7.__name__)

684

685 except (TypeError, ValueError) as x:

686 raise ResectionError(pointA=pointA, pointB=pointB, pointC=pointC,

687 alpha=alpha, beta=beta, gamma=gamma, cause=x)

688

689

690 def triAngle(a, b, c):

691 '''Compute one angle of a triangle.

692

693 @arg a: Adjacent triangle side length (C{scalar}, non-negative

694 C{meter}, conventionally).

695 @arg b: Adjacent triangle side length (C{scalar}, non-negative

696 C{meter}, conventionally).

697 @arg c: Opposite triangle side length (C{scalar}, non-negative

698 C{meter}, conventionally).

699

700 @return: Angle in C{radians} at triangle corner C{C}, opposite

701 triangle side B{C{c}}.

702

703 @raise TriangleError: Invalid or negative B{C{a}}, B{C{b}} or B{C{c}}.

704

705 @see: Functions L{triAngle5} and L{triSide}.

706 '''

707 try:

708 return _triAngle(a, b, c)

709 except (TypeError, ValueError) as x:

710 raise TriangleError(a=a, b=b, c=c, cause=x)

711

712

713 def _triAngle(a, b, c):

714 # (INTERNAL) To allow callers to embellish errors

715 a, b, c = map1(float, a, b, c)

716 if a < b:

717 a, b = b, a

718 if b < 0 or c < 0:

719 raise ValueError(_negative_)

720 if a < EPS0:

721 raise ValueError(_coincident_)

722 b_a = b / a

723 if b_a < EPS0:

724 raise ValueError(_coincident_)

725 t = fsumf_(_1_0, b_a**2, -(c / a)**2) / (b_a * _2_0)

726 return acos1(t)

727

728

729 def triAngle5(a, b, c):

730 '''Compute the angles of a triangle.

731

732 @arg a: Length of the triangle side opposite of triangle corner C{A}

733 (C{scalar}, non-negative C{meter}, conventionally).

734 @arg b: Length of the triangle side opposite of triangle corner C{B}

735 (C{scalar}, non-negative C{meter}, conventionally).

736 @arg c: Length of the triangle side opposite of triangle corner C{C}

737 (C{scalar}, non-negative C{meter}, conventionally).

738

739 @return: L{TriAngle5Tuple}C{(radA, radB, radC, rIn, area)} with angles

740 C{radA}, C{radB} and C{radC} at triangle corners C{A}, C{B}

741 and C{C}, all in C{radians}, the C{InCircle} radius C{rIn}

742 aka C{inradius}, same units as triangle sides B{C{a}},

743 B{C{b}} and B{C{c}} and the triangle C{area} in those same

744 units I{squared}.

745

746 @raise TriangleError: Invalid or negative B{C{a}}, B{C{b}} or B{C{c}}.

747

748 @see: Functions L{triAngle} and L{triArea}.

749 '''

750 try:

751 x, y, z = map1(float, a, b, c)

752 ab = x < y

753 if ab:

754 x, y = y, x

755 bc = y < z

756 if bc:

757 y, z = z, y

758

759 if z > EPS0: # z = min(a, b, c)

760 s = fsum1f_(z, y, x) * _0_5

761 sa, sb, r = (s - x), (s - y), (s - z)

762 r *= _over(sa * sb, s)

763 if r < EPS02:

764 raise ValueError(_coincident_)

765 r = sqrt(r)

766 rA = atan2(r, sa) * _2_0

767 rB = atan2(r, sb) * _2_0

768 rC = fsumf_(PI, -rA, -rB)

769 if min(rA, rB, rC) < 0:

770 raise ValueError(_colinear_)

771 s *= r # Heron's area

772 elif z < 0:

773 raise ValueError(_negative_)

774 else: # 0 <= c <= EPS0

775 rA = rB = PI_2

776 rC = r = s = _0_0

777

778 if bc:

779 rB, rC = rC, rB

780 if ab:

781 rA, rB = rB, rA

782 return TriAngle5Tuple(rA, rB, rC, r, s, name=triAngle5.__name__)

783

784 except (TypeError, ValueError) as x:

785 raise TriangleError(a=a, b=b, c=c, cause=x)

786

787

788 def triArea(a, b, c):

789 '''Compute the area of a triangle using U{Heron's<https://

790 WikiPedia.org/wiki/Heron%27s_formula>} C{stable} formula.

791

792 @arg a: Length of the triangle side opposite of triangle corner C{A}

793 (C{scalar}, non-negative C{meter}, conventionally).

794 @arg b: Length of the triangle side opposite of triangle corner C{B}

795 (C{scalar}, non-negative C{meter}, conventionally).

796 @arg c: Length of the triangle side opposite of triangle corner C{C}

797 (C{scalar}, non-negative C{meter}, conventionally).

798

799 @return: The triangle area (C{float}, conventionally C{meter} or

800 same units as B{C{a}}, B{C{b}} and B{C{c}} I{squared}).

801

802 @raise TriangleError: Invalid or negative B{C{a}}, B{C{b}} or B{C{c}}.

803 '''

804 try:

805 r, y, x = sorted(map1(float, a, b, c))

806 if r > 0: # r = min(a, b, c)

807 ab = x - y

808 bc = y - r

809 y += r

810 r = (x + y) * (r - ab) * (r + ab) * (x + bc)

811 if r:

812 r = sqrt(r / _16_0)

813 elif r < 0:

814 raise ValueError(_negative_)

815 return r

816

817 except (TypeError, ValueError) as x:

818 raise TriangleError(a=a, b=b, c=c, cause=x)

819

820

821 def triSide(a, b, radC):

822 '''Compute one side of a triangle.

823

824 @arg a: Adjacent triangle side length (C{scalar},

825 non-negative C{meter}, conventionally).

826 @arg b: Adjacent triangle side length (C{scalar},

827 non-negative C{meter}, conventionally).

828 @arg radC: Angle included by sides B{C{a}} and B{C{b}},

829 opposite triangle side C{c} (C{radians}).

830

831 @return: Length of triangle side C{c}, opposite triangle

832 corner C{C} and angle B{C{radC}}, same units as

833 B{C{a}} and B{C{b}}.

834

835 @raise TriangleError: Invalid B{C{a}}, B{C{b}} or B{C{radC}}.

836

837 @see: Functions L{sqrt_a}, L{triAngle}, L{triSide2} and L{triSide4}.

838 '''

839 try:

840 return _triSide(a, b, radC)

841 except (TypeError, ValueError) as x:

842 raise TriangleError(a=a, b=b, radC=radC, cause=x)

843

844

845 def _triSide(a, b, radC):

846 # (INTERNAL) To allow callers to embellish errors

847 a, b, r = t = map1(float, a, b, radC)

848 if min(t) < 0:

849 raise ValueError(_negative_)

850

851 if a < b:

852 a, b = b, a

853 if a > EPS0:

854 ba = b / a

855 c2 = fsumf_(_1_0, ba**2, _N_2_0 * ba * cos(r))

856 if c2 > EPS02:

857 return a * sqrt(c2)

858 elif c2 < 0:

859 raise ValueError(_invalid_)

860 return hypot(a, b)

861

862

863 def triSide2(b, c, radB):

864 '''Compute a side and its opposite angle of a triangle.

865

866 @arg b: Adjacent triangle side length (C{scalar},

867 non-negative C{meter}, conventionally).

868 @arg c: Adjacent triangle side length (C{scalar},

869 non-negative C{meter}, conventionally).

870 @arg radB: Angle included by sides B{C{a}} and B{C{c}},

871 opposite triangle side C{b} (C{radians}).

872

873 @return: L{TriSide2Tuple}C{(a, radA)} with triangle angle

874 C{radA} in C{radians} and length of the opposite

875 triangle side C{a}, same units as B{C{b}} and B{C{c}}.

876

877 @raise TriangleError: Invalid B{C{b}} or B{C{c}} or either

878 B{C{b}} or B{C{radB}} near zero.

879

880 @see: Functions L{sqrt_a}, L{triSide} and L{triSide4}.

881 '''

882 try:

883 return _triSide2(b, c, radB)

884 except (TypeError, ValueError) as x:

885 raise TriangleError(b=b, c=c, radB=radB, cause=x)

886

887

888 def _triSide2(b, c, radB):

889 # (INTERNAL) To allow callers to embellish errors

890 b, c, rB = map1(float, b, c, radB)

891 if min(b, c, rB) < 0:

892 raise ValueError(_negative_)

893 sB, cB = sincos2(rB)

894 if isnear0(sB):

895 if not isnear0(b):

896 raise ValueError(_invalid_)

897 a, rA = ((b + c), PI) if cB < 0 else (fabs(b - c), _0_0)

898 elif isnear0(b):

899 raise ValueError(_invalid_)

900 else:

901 rA = fsumf_(PI, -rB, -asin1(c * sB / b))

902 a = sin(rA) * b / sB

903 return TriSide2Tuple(a, rA, name=triSide2.__name__)

904

905

906 def triSide4(radA, radB, c):

907 '''Compute two sides and the height of a triangle.

908

909 @arg radA: Angle at triangle corner C{A}, opposite triangle side C{a}

910 (non-negative C{radians}).

911 @arg radB: Angle at triangle corner C{B}, opposite triangle side C{b}

912 (non-negative C{radians}).

913 @arg c: Length of triangle side between triangle corners C{A} and C{B},

914 (C{scalar}, non-negative C{meter}, conventionally).

915

916 @return: L{TriSide4Tuple}C{(a, b, radC, d)} with triangle sides C{a} and

917 C{b} and triangle height C{d} perpendicular to triangle side

918 B{C{c}}, all in the same units as B{C{c}} and interior angle

919 C{radC} in C{radians} at triangle corner C{C}, opposite

920 triangle side B{C{c}}.

921

922 @raise TriangleError: Invalid or negative B{C{radA}}, B{C{radB}} or B{C{c}}.

923

924 @see: U{Triangulation, Surveying<https://WikiPedia.org/wiki/Triangulation_(surveying)>}

925 and functions L{sqrt_a}, L{triSide} and L{triSide2}.

926 '''

927 try:

928 rA, rB, c = map1(float, radA, radB, c)

929 rC = fsumf_(PI, -rA, -rB)

930 if min(rC, rA, rB, c) < 0:

931 raise ValueError(_negative_)

932 sa, ca, sb, cb = sincos2_(rA, rB)

933 sc = fsum1f_(sa * cb, sb * ca)

934 if sc < EPS0 or min(sa, sb) < 0:

935 raise ValueError(_invalid_)

936 sc = c / sc

937 return TriSide4Tuple((sa * sc), (sb * sc), rC, (sa * sb * sc),

938 name=triSide4.__name__)

939

940 except (TypeError, ValueError) as x:

941 raise TriangleError(radA=radA, radB=radB, c=c, cause=x)

942

943

944 def wildberger3(a, b, c, alpha, beta, R3=min):

945 '''Snellius' surveying using U{Rational Trigonometry

946 <https://WikiPedia.org/wiki/Snellius–Pothenot_problem>}.

947

948 @arg a: Length of the triangle side between corners C{B} and C{C} and opposite of

949 triangle corner C{A} (C{scalar}, non-negative C{meter}, conventionally).

950 @arg b: Length of the triangle side between corners C{C} and C{A} and opposite of

951 triangle corner C{B} (C{scalar}, non-negative C{meter}, conventionally).

952 @arg c: Length of the triangle side between corners C{A} and C{B} and opposite of

953 triangle corner C{C} (C{scalar}, non-negative C{meter}, conventionally).

954 @arg alpha: Angle subtended by triangle side B{C{b}} (C{degrees}, non-negative).

955 @arg beta: Angle subtended by triangle side B{C{a}} (C{degrees}, non-negative).

956 @kwarg R3: Callable to determine C{R3} from C{(R3 - C)**2 = D}, typically standard

957 Python function C{min} or C{max}, invoked with 2 arguments.

958

959 @return: L{Survey3Tuple}C{(PA, PB, PC)} with distance from survey point C{P} to

960 each of the triangle corners C{A}, C{B} and C{C}, same units as B{C{a}},

961 B{C{b}} and B{C{c}}.

962

963 @raise TriangleError: Invalid B{C{a}}, B{C{b}} or B{C{c}} or negative B{C{alpha}} or

964 B{C{beta}} or B{C{R3}} not C{callable}.

965

966 @see: U{Wildberger, Norman J.<https://Math.Sc.Chula.ac.TH/cjm/content/

967 survey-article-greek-geometry-rational-trigonometry-and-snellius-–-pothenot-surveying>},

968 U{Devine Proportions, page 252<http://www.MS.LT/derlius/WildbergerDivineProportions.pdf>}

969 and function L{snellius3}.

970 '''

971 def _s(x):

972 return sin(x)**2

973

974 def _vpa(r3, q2, q3, s2, s3):

975 r1 = s2 * q3 / s3

976 r = r1 * r3 * _4_0

977 n = (r - _F1(r1, r3, -q2)**2).fover(s3)

978 if n < 0 or r < EPS0:

979 raise ValueError(_coincident_)

980 return sqrt((n / r) * q3) if n else _0_0

981

982 try:

983 a, b, c, da, db = q = map1(float, a, b, c, alpha, beta)

984 if min(q) < 0:

985 raise ValueError(_negative_)

986

987 q1, q2, q3 = q = a**2, b**2, c**2

988 if min(q) < EPS02:

989 raise ValueError(_coincident_)

990

991 ra, rb = map1(radians, da, db)

992 s1, s2, s3 = s = map1(_s, rb, ra, ra + rb) # rb, ra!

993 if min(s) < EPS02:

994 raise ValueError(_or(_coincident_, _colinear_))

995

996 q4 = hypot2_(*q) * _2_0 # a**4 + ...

997 Qs = _F1(*q) # == hypot2_(a, b, c)

998 d0 = (Qs**2 - q4).fmul(s1 * s2).fover(s3)

999 if d0 < 0:

1000 raise ValueError(_negative_)

1001 s += _F1(*s), # == fsum1(s),

1002 C0 = Fdot(s, q1, q2, q3, -Qs * _0_5)

1003 r3 = C0.fover(-s3) # C0 /= -s3

1004 if d0 > EPS02: # > c0

1005 _xcallable(R3=R3)

1006 d0 = sqrt(d0)

1007 r3 = R3(float(C0 + d0), float(C0 - d0)) # XXX min or max

1008

1009 pa = _vpa(r3, q2, q3, s2, s3)

1010 pb = _vpa(r3, q1, q3, s1, s3)

1011 pc = favg(_triSide2(b, pa, ra).a,

1012 _triSide2(a, pb, rb).a)

1013 return Survey3Tuple(pa, pb, pc, name=wildberger3.__name__)

1014

1015 except (TypeError, ValueError) as x:

1016 raise TriangleError(a=a, b=b, c=c, alpha=alpha, beta=beta, R3=R3, cause=x)

1017

1018

1019 def _zidw(x, y, useZ, *ABC):

1020 if useZ: # interpolate z or coplanar with A, B and C?

1021 t = tuple(_.z for _ in ABC)

1022 v = Vector3d(x, y, fmean(t))

1023 z = fidw(t, (v.minus(T).length for T in ABC))

1024 else:

1025 z = INT0

1026 return z

1027

1028# **) MIT License

1029#

1030# Copyright (C) 2016-2024 -- mrJean1 at Gmail -- All Rights Reserved.

1031#

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

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

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

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

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

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

1038#

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

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

1041#

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

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

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

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

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

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

1048# OTHER DEALINGS IN THE SOFTWARE.