Coverage for pygeodesy/resections.py: 97%

366 statements  

« prev     ^ index     » next       coverage.py v7.6.1, created at 2025-01-06 12:20 -0500

1 

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

3 

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

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

6L{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 

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

14 

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

16from 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, isnear0, _over, _umod_360 

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

20 _xkwds, _xkwds_pop2 

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

22from pygeodesy.fsums import _Fsumf_, fsumf_, fsum1, fsum1f_ 

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

24 _colinear_, _d_, _invalid_, _negative_, _not_, \ 

25 _rIn_, _SPACE_ 

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

27from pygeodesy.named import _NamedTuple, _Pass, Fmt 

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

29from pygeodesy.units import Degrees, Distance, Radians 

30from pygeodesy.utily import acos1, asin1, atan2, sincos2, sincos2_, \ 

31 sincos2d, sincos2d_ 

32from pygeodesy.vector3d import _otherV3d, Vector3d 

33 

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

35 

36__all__ = _ALL_LAZY.resections 

37__version__ = '24.11.27' 

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 

51class 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 

60class ResectionError(_ValueError): 

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

62 ''' 

63 pass 

64 

65 

66class Survey3Tuple(_NamedTuple): 

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

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

69 ''' 

70 _Names_ = (_PA_, _PB_, _PC_) 

71 _Units_ = ( Distance, Distance, Distance) 

72 

73 

74class Tienstra7Tuple(_NamedTuple): 

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

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

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

78 ''' 

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

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

81 

82 

83class TriAngle5Tuple(_NamedTuple): 

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

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

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

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

88 ''' 

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

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

91 

92 

93class TriSide2Tuple(_NamedTuple): 

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

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

96 ''' 

97 _Names_ = (_a_, _radA_) 

98 _Units_ = ( Distance, Radians) 

99 

100 

101class TriSide4Tuple(_NamedTuple): 

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

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

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

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

106 ''' 

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

108 _Units_ = ( Distance, Distance, Radians, Distance) 

109 

110 

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

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

113 ''' 

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

115 _otherV3d(useZ=useZ, pointB=pointB), 

116 _otherV3d(useZ=useZ, pointC=pointC)) 

117 

118 

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

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

121 ''' 

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

123 _otherV3d(useZ=useZ, point2=point2), 

124 _otherV3d(useZ=useZ, point3=point3)) 

125 

126 

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

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

129 

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

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

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

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

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

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

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

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

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

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

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

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

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

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

144 keyword arguments to instantiate the survey point. 

145 

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

147 

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

149 (sub-)class. 

150 

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

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

153 

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

155 

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

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

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

159 ''' 

160 

161 def _H(A, C, sa): 

162 s, c = sincos2d(sa) 

163 if isnear0(s): 

164 raise ValueError(_or(_coincident_, _colinear_)) 

165 t = s, c, c 

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

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

168 return x, y 

169 

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

171 try: 

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

173 if min(sa, sb) < 0: 

174 raise ValueError(_negative_) 

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

176 raise ValueError() 

177 

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

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

180 

181 x = x1 - x2 

182 y = y1 - y2 

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

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

185 

186 m = y / x 

187 n = x / y 

188 N = n + m 

189 if isnear0(N): 

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

191 

192 t = n, m, _1_0, _N_1_0 

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

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

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

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

197 

198 except (TypeError, ValueError) as x: 

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

200 alpha=alpha, beta=beta, cause=x) 

201 

202 

203def _Clas(which, point, Clas_and_kwds, *args): 

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

205 ''' 

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

207 return Clas(*args, **_xkwds(kwds, name=which.__name__)) 

208 

209 

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

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

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

213 

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

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

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

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

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

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

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

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

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

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

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

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

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

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

228 keyword arguments to instantiate the survey point. 

229 

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

231 

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

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

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

235 conventionally. 

236 

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

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

239 

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

241 

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

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

244 ''' 

245 

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

247 v = B.minus(A) 

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

249 if r < 0 and pi2: 

250 r += pi2 

251 return r, v.length 

252 

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

254 s, c = sincos2(r) 

255 x = d * s + A.x # fma(d, s, A.x) 

256 y = d * c + A.y # fma(d, c, A.y) 

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

258 return x, y, z 

259 

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

261 try: 

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

263 if min(ra, rb) < 0: 

264 raise ValueError(_negative_) 

265 

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

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

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

269 

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

271 zb, b = _azi_len2(C, A) 

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

273 

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

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

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

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

278 

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

280 

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

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

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

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

285 

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

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

288 a = B.minus(C).length 

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

290 

291 except (TypeError, ValueError) as x: 

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

293 alpha=alpha, beta=beta, cause=x) 

294 

295 

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

297 **Clas_and_kwds): 

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

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

300 the (pseudo-)singularities. 

301 

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

317 keyword arguments to instantiate the survey point. 

318 

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

320 by angle, modulo 360, counter-clockwise. 

321 

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

323 (sub-)class. 

324 

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

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

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

328 

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

330 

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

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

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

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

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

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

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

338 ''' 

339 

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

341 if eps > 0: 

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

343 t = Fmt.PARENSPACED(eps=eps) 

344 raise ValueError(_SPACE_(t, _not_, _positive_)) 

345 

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

347 try: 

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

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

350 

351 except (TypeError, ValueError) as x: 

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

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

354 

355 

356def _pierlot3(B1, B2, B3, a12, a23, useZ, _cot): 

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

358 ''' 

359 x1_, y1_, _ = B1.minus(B2).xyz3 

360 x3_, y3_, _ = B3.minus(B2).xyz3 

361 

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

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

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

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

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

367 # = c31 / s31 

368 cot31 = _cot(fsum1f_(c12 * s23, s12 * c23), # s31 

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

370 

371 K = _Fsumf_(x3_ * x1_, cot31 * (y3_ * x1_), 

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

373 if K: 

374 cot12 = _cot(s12, c12) 

375 cot23 = _cot(s23, c23) 

376 

377 # x12 = x1_ + cot12 * y1_ 

378 # y12 = y1_ - cot12 * x1_ 

379 

380 # x23 = x3_ - cot23 * y3_ 

381 # y23 = y3_ + cot23 * x3_ 

382 

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

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

385 

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

387 X12_23 = _Fsumf_(x1_, cot12 * y1_, -x3_, cot23 * y3_) 

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

389 Y12_23 = _Fsumf_(y1_, -cot12 * x1_, -y3_, -cot23 * x3_) 

390 

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

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

393 X31_23 = _Fsumf_(x1_, -cot31 * y1_, cot31 * y3_, cot23 * y3_) 

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

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

396 Y31_23 = _Fsumf_(y1_, cot31 * x1_, -cot31 * x3_, -cot23 * x3_) 

397 

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

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

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

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

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

403 X12_23 * Y31_23)) 

404 else: 

405 x, y, _ = B2.xyz3 

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

407 

408 

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

410 **Clas_and_kwds): 

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

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

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

414 

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

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

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

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

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

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

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

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

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

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

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

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

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

428 keyword arguments to instantiate the survey point. 

429 

430 @return: The survey (or robot) point, an instance of B{C{Clas}} or 

431 B{C{point1}}'s (sub-)class. 

432 

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

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

435 

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

437 

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

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

440 L{collins5} and L{tienstra7}. 

441 ''' 

442 

443 def _a_z_Bs(Bs, *alphas): 

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

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

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

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

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

449 yield d, z, B 

450 

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

452 try: 

453 return (c / s) # if c else _copysign_0_0(s) 

454 except ZeroDivisionError: 

455 raise ValueError(_or(_coincident_, _colinear_)) 

456 

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

458 try: 

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

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

461 useZ, _cot, Cs.append) 

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

463 

464 except (TypeError, ValueError) as x: 

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

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

467 

468 

469def _pierlotx3(a_z_Bs, useZ, _cot, Cs): 

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

471 ''' 

472 (a12, z12, B1), \ 

473 (a23, z23, B2), \ 

474 (a31, z31, B3) = a_z_Bs 

475 if z12 and not z23: 

476 Cs(1) 

477 elif z23 and not z31: 

478 Cs(2) 

479 a23, B1, B2, B3 = a31, B2, B3, B1 

480 elif z31 and not z12: 

481 Cs(3) 

482 a23, B2, B3 = a12, B3, B2 

483 else: 

484 Cs(4) 

485 return _pierlot3(B1, B2, B3, a12, a23, useZ, _cot) 

486 

487 x1_, y1_, _ = B1.minus(B3).xyz3 

488 x2_, y2_, _ = B2.minus(B3).xyz3 

489 

490 K = _Fsumf_(y1_ * x2_, -x1_ * y2_) 

491 if K: 

492 cot23 = _cot(*sincos2d(a23)) 

493 

494 # x23 = x2_ + cot23 * y2_ 

495 # y23 = y2_ - cot23 * x2_ 

496 

497 # x31 = x1_ + cot23 * y1_ 

498 # y31 = y1_ - cot23 * x1_ 

499 

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

501 X31_23 = _Fsumf_(x1_, cot23 * y1_, -x2_, -cot23 * y2_) 

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

503 Y31_23 = _Fsumf_(y1_, -cot23 * x1_, -y2_, cot23 * x2_) 

504 

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

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

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

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

509 Y31_23 * _Fsumf_(y2_, -y1_))) 

510 else: 

511 x, y, _ = B3.xyz3 

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

513 

514 

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

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

517 ''' 

518 d = float(D) 

519 if isnear0(d): 

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

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

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

523 return x, y 

524 

525 

526def _rotate(xs, n=1): 

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

528 ''' 

529 return xs[n:] + xs[:n] 

530 

531 

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

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

534 

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

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

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

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

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

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

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

542 

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

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

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

546 

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

548 or B{C{beta}}. 

549 

550 @see: Function L{wildberger3}. 

551 ''' 

552 try: 

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

554 if min(t) < 0: 

555 raise ValueError(_negative_) 

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

557 

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

559 k = PI - r 

560 if min(k, r) < 0: 

561 raise ValueError(_or(_coincident_, _colinear_)) 

562 

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

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

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

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

567 pa = k + p 

568 pb = k - p 

569 

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

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

572 elif sa: 

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

574 else: 

575 raise ValueError(_or(_colinear_, _coincident_)) 

576 

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

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

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

580 

581 except (TypeError, ValueError) as x: 

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

583 

584 

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

586 useZ=False, **Clas_and_kwds): 

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

588 

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

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

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

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

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

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

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

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

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

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

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

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

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

602 (C{bool}). 

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

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

605 the survey point. 

606 

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

608 

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

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

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

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

613 

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

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

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

617 

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

619 

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

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

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

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

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

625 ''' 

626 

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

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

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

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

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

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

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

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

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

636 if isnear0(c): 

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

638 ks.append(sr * ss / c) 

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

640 

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

642 try: 

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

644 if beta is None: 

645 if gamma is None: 

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

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

648 elif gamma is None: 

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

650 else: # subtended angles must add to 360 degrees 

651 r = fsum1f_(sa, sb, sc) 

652 if fabs(r - PI2) > EPS: 

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

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

655 raise ValueError(_negative_) 

656 

657 # triangle sides 

658 a = B.minus(C).length 

659 b = A.minus(C).length 

660 c = A.minus(B).length 

661 

662 ks = [] # 3 Ks and triangle angles 

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

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

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

666 

667 k = fsum1(ks) 

668 if isnear0(k): 

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

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

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

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

673 

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

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

676 

677 except (TypeError, ValueError) as x: 

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

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

680 

681 

682def triAngle(a, b, c): 

683 '''Compute one angle of a triangle. 

684 

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

686 C{meter}, conventionally). 

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

688 C{meter}, conventionally). 

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

690 C{meter}, conventionally). 

691 

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

693 triangle side B{C{c}}. 

694 

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

696 

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

698 ''' 

699 try: 

700 return _triAngle(a, b, c) 

701 except (TypeError, ValueError) as x: 

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

703 

704 

705def _triAngle(a, b, c): 

706 # (INTERNAL) To allow callers to embellish errors 

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

708 if a < b: 

709 a, b = b, a 

710 if b < 0 or c < 0: 

711 raise ValueError(_negative_) 

712 if a < EPS0: 

713 raise ValueError(_coincident_) 

714 b_a = b / a 

715 if b_a < EPS0: 

716 raise ValueError(_coincident_) 

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

718 return acos1(t) 

719 

720 

721def triAngle5(a, b, c): 

722 '''Compute the angles of a triangle. 

723 

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

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

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

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

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

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

730 

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

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

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

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

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

736 units I{squared}. 

737 

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

739 

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

741 ''' 

742 try: 

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

744 ab = x < y 

745 if ab: 

746 x, y = y, x 

747 bc = y < z 

748 if bc: 

749 y, z = z, y 

750 

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

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

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

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

755 if r < EPS02: 

756 raise ValueError(_coincident_) 

757 r = sqrt(r) 

758 rA = atan2(r, sa) * _2_0 

759 rB = atan2(r, sb) * _2_0 

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

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

762 raise ValueError(_colinear_) 

763 s *= r # Heron's area 

764 elif z < 0: 

765 raise ValueError(_negative_) 

766 else: # 0 <= c <= EPS0 

767 rA = rB = PI_2 

768 rC = r = s = _0_0 

769 

770 if bc: 

771 rB, rC = rC, rB 

772 if ab: 

773 rA, rB = rB, rA 

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

775 

776 except (TypeError, ValueError) as x: 

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

778 

779 

780def triArea(a, b, c): 

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

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

783 

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

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

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

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

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

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

790 

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

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

793 

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

795 ''' 

796 try: 

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

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

799 ab = x - y 

800 bc = y - r 

801 y += r 

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

803 if r: 

804 r = sqrt(r / _16_0) 

805 elif r < 0: 

806 raise ValueError(_negative_) 

807 return r 

808 

809 except (TypeError, ValueError) as x: 

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

811 

812 

813def triSide(a, b, radC): 

814 '''Compute one side of a triangle. 

815 

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

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

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

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

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

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

822 

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

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

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

826 

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

828 

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

830 ''' 

831 try: 

832 return _triSide(a, b, radC) 

833 except (TypeError, ValueError) as x: 

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

835 

836 

837def _triSide(a, b, radC): 

838 # (INTERNAL) To allow callers to embellish errors 

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

840 if min(t) < 0: 

841 raise ValueError(_negative_) 

842 

843 if a < b: 

844 a, b = b, a 

845 if a > EPS0: 

846 ba = b / a 

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

848 if c2 > EPS02: 

849 return a * sqrt(c2) 

850 elif c2 < 0: 

851 raise ValueError(_invalid_) 

852 return hypot(a, b) 

853 

854 

855def triSide2(b, c, radB): 

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

857 

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

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

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

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

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

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

864 

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

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

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

868 

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

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

871 

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

873 ''' 

874 try: 

875 return _triSide2(b, c, radB) 

876 except (TypeError, ValueError) as x: 

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

878 

879 

880def _triSide2(b, c, radB): 

881 # (INTERNAL) To allow callers to embellish errors 

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

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

884 raise ValueError(_negative_) 

885 sB, cB = sincos2(rB) 

886 if isnear0(sB): 

887 if not isnear0(b): 

888 raise ValueError(_invalid_) 

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

890 elif isnear0(b): 

891 raise ValueError(_invalid_) 

892 else: 

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

894 a = sin(rA) * b / sB 

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

896 

897 

898def triSide4(radA, radB, c): 

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

900 

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

902 (non-negative C{radians}). 

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

904 (non-negative C{radians}). 

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

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

907 

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

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

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

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

912 triangle side B{C{c}}. 

913 

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

915 

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

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

918 ''' 

919 try: 

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

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

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

923 raise ValueError(_negative_) 

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

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

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

927 raise ValueError(_invalid_) 

928 sc = c / sc 

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

930 name=triSide4.__name__) 

931 

932 except (TypeError, ValueError) as x: 

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

934 

935 

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

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

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

939 

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

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

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

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

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

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

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

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

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

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

950 

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

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

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

954 

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

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

957 

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

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

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

961 and function L{snellius3}. 

962 ''' 

963 def _s(x): 

964 return sin(x)**2 

965 

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

967 r1 = s2 * q3 / s3 

968 r = r1 * r3 * _4_0 

969 n = (r - _Fsumf_(r1, r3, -q2)**2).fover(s3) 

970 if n < 0 or r < EPS0: 

971 raise ValueError(_coincident_) 

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

973 

974 try: 

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

976 if min(q) < 0: 

977 raise ValueError(_negative_) 

978 

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

980 if min(q) < EPS02: 

981 raise ValueError(_coincident_) 

982 

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

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

985 if min(s) < EPS02: 

986 raise ValueError(_or(_coincident_, _colinear_)) 

987 

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

989 Qs = _Fsumf_(*q) # == hypot2_(a, b, c) 

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

991 if d0 < 0: 

992 raise ValueError(_negative_) 

993 s += _Fsumf_(*s), # == fsum1(s), 

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

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

996 if d0 > EPS02: # > c0 

997 _xcallable(R3=R3) 

998 d0 = sqrt(d0) 

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

1000 

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

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

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

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

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

1006 

1007 except (TypeError, ValueError) as x: 

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

1009 

1010 

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

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

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

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

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

1016 else: 

1017 z = INT0 

1018 return z 

1019 

1020# **) MIT License 

1021# 

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

1023# 

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

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

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

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

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

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

1030# 

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

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

1033# 

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

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

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

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

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

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

1040# OTHER DEALINGS IN THE SOFTWARE.