22 , _a(_geod.EquatorialRadius())
23 , _f(_geod.Flattening())
24 , _rR(sqrt(_geod.EllipsoidArea() / (4 *
Math::pi())))
25 , _d(_rR *
Math::pi())
26 , _eps(3 * numeric_limits<real>::epsilon())
27 , _tol(_d * pow(numeric_limits<real>::epsilon(), 3/real(4)))
28 , _delta(_d * pow(numeric_limits<real>::epsilon(), 1/real(5)))
36 _t1 = _t4 = _a * (1 - _f) *
Math::pi();
37 _t2 = 2 * distpolar(90);
38 _geod.
Inverse(0, 0, 90, 0, _t5); _t5 *= 2;
50 if (! (_d1 < _d3 && _d2 < _d3 && _d2 < 2 * _t1) )
66 XPoint p = ClosestInt(lineX, lineY, XPoint(p0));
76 int& segmode,
int* c)
const {
85 XPoint p = SegmentInt(lineX, lineY, segmode);
100 XPoint p = NextInt(lineX, lineY);
105 std::vector<Intersect::Point>
114 std::vector<Intersect::Point>
124 std::vector<Intersect::Point>
128 return AllInternal(lineX, lineY, maxdist, p0, c,
false);
131 std::vector<Intersect::Point>
135 return AllInternal(lineX, lineY, maxdist, p0, c,
true);
140 const Intersect::XPoint& p)
const {
143 real latX, lonX, aziX, latY, lonY, aziY;
144 lineX.
Position(p.x , latX, lonX, aziX);
145 lineY.
Position(p.y, latY, lonY, aziY);
146 real z, aziXa, aziYa;
147 _geod.
Inverse(latX, lonX, latY, lonY, z, aziXa, aziYa);
148 real sinz = sin(z/_rR), cosz = cos(z/_rR);
153 real s = copysign(
real(1), XY + (dXY + dY - dX));
164 if (z <= _eps * _rR) {
167 if (fabs(sinX - sinY) <= _eps && fabs(cosX - cosY) <= _eps)
169 else if (fabs(sinX + sinY) <= _eps && fabs(cosX + cosY) <= _eps)
173 }
else if (fabs(sinX) <= _eps && fabs(sinY) <= _eps) {
174 c = cosX * cosY > 0 ? 1 : -1;
176 sX = cosX * z/2; sY = -cosY * z/2;
184 sX = _rR * atan2(sinY * sinz, sinY * cosX * cosz - cosY * sinX);
185 sY = _rR * atan2(sinX * sinz, -sinX * cosY * cosz + cosX * sinY);
188 return XPoint(sX, sY, c);
192 Intersect::Basic(
const GeodesicLine& lineX,
const GeodesicLine& lineY,
193 const Intersect::XPoint& p0)
const {
201 XPoint dq = Spherical(lineX, lineY, q);
203 if (q.c || !(dq.Dist() > _tol))
break;
209 Intersect::ClosestInt(
const GeodesicLine& lineX,
const GeodesicLine& lineY,
210 const Intersect::XPoint& p0)
const {
212 const int ix[num] = { 0, 1, -1, 0, 0 };
213 const int iy[num] = { 0, 0, 0, 1, -1 };
214 bool skip[num] = { 0, 0, 0, 0, 0 };
216 for (
int n = 0; n < num; ++n) {
217 if (skip[n])
continue;
218 XPoint qx = Basic(lineX, lineY, p0 + XPoint(ix[n] * _d1, iy[n] * _d1));
219 qx = fixcoincident(p0, qx);
220 if (_comp.eq(q, qx))
continue;
221 if (qx.Dist(p0) < _t1) { q = qx; ++_cnt2;
break; }
222 if (n == 0 || qx.Dist(p0) < q.Dist(p0)) { q = qx; ++_cnt2; }
223 for (
int m = n + 1; m < num; ++m)
225 qx.Dist(p0 + XPoint(ix[m]*_d1, iy[m]*_d1)) < 2*_t1 - _d1 - _delta;
231 Intersect::NextInt(
const GeodesicLine& lineX,
const GeodesicLine& lineY)
234 const int ix[num] = { -1, -1, 1, 1, -2, 0, 2, 0 };
235 const int iy[num] = { -1, 1, -1, 1, 0, 2, 0, -2 };
236 bool skip[num] = { 0, 0, 0, 0, 0, 0, 0, 0 };
239 for (
int n = 0; n < num; ++n) {
240 if (skip[n])
continue;
241 XPoint qx = Basic(lineX, lineY, XPoint(ix[n] * _d2, iy[n] * _d2));
242 qx = fixcoincident(z, qx);
243 bool zerop = _comp.eq(z, qx);
244 if (qx.c == 0 && zerop)
continue;
246 for (
int sgn = -1; sgn <= 1; sgn+=2) {
247 real s = ConjugateDist(lineX, sgn * _d,
false);
248 XPoint qa(s, qx.c*s, qx.c);
249 if (qa.Dist() < q.Dist()) { q = qa; ++_cnt2; }
252 if (qx.Dist() < q.Dist()) { q = qx; ++_cnt2; }
254 for (
int sgn = -1; sgn <= 1; ++sgn) {
257 if ((qx.c == 0 && sgn != 0) || (zerop && sgn == 0))
continue;
258 XPoint qy = qx.c ? qx +
Point(sgn * _d2, qx.c * sgn *_d2) : qx;
259 for (
int m = n + 1; m < num; ++m)
261 qy.Dist(XPoint(ix[m]*_d2, iy[m]*_d2)) < 2*_t1 - _d2 - _delta;
268 Intersect::SegmentInt(
const GeodesicLine& lineX,
const GeodesicLine& lineY,
269 int& segmode)
const {
273 const bool conjectureproved =
false;
274 real sx = lineX.Distance(), sy = lineY.Distance();
276 XPoint p0 = XPoint(sx/2, sy/2), q = ClosestInt(lineX, lineY, p0);
277 q = fixsegment(sx, sy, q);
278 segmode = segmentmode(sx, sy, q);
280 if (!conjectureproved && segmode != 0 && p0.Dist() >= p0.Dist(q)) {
284 for (
int ix = 0; ix < 2 && segmodex != 0; ++ix) {
285 for (
int iy = 0; iy < 2 && segmodex != 0; ++iy) {
286 XPoint t(ix * sx, iy * sy);
288 if (q.Dist(t) >= 2 * _t1) {
290 qx = Basic(lineX, lineY, t);
293 qx = fixcoincident(t, qx);
296 segmodex = segmentmode(sx, sy, qx);
300 if (segmodex == 0) { ++_cnt4; segmode = 0; q = qx; }
305 std::vector<Intersect::XPoint>
306 Intersect::AllInt0(
const GeodesicLine& lineX,
307 const GeodesicLine& lineY,
309 real maxdistx = maxdist + _delta;
310 const int m = int(ceil(maxdistx / _d3)),
311 m2 = m*m + (m - 1) % 2,
313 real d3 = maxdistx/m;
314 vector<XPoint> start(m2);
315 vector<bool> skip(m2,
false);
318 for (
int i = -n; i <= n; i += 2)
319 for (
int j = -n; j <= n; j += 2) {
320 if (!(i == 0 && j == 0))
321 start[h++] = p0 + XPoint( d3 * (i + j) / 2, d3 * (i - j) / 2);
324 set<XPoint, SetComp> r(_comp);
325 set<XPoint, SetComp> c(_comp);
326 vector<XPoint> added;
327 for (
int k = 0; k < m2; ++k) {
328 if (skip[k])
continue;
329 XPoint q = Basic(lineX, lineY, start[k]);
330 if (r.find(q) != r.end()
332 || (c0 != 0 && c.find(fixcoincident(p0, q)) != c.end()))
340 q = fixcoincident(p0, q);
344 for (
auto qp = r.begin(); qp != r.end(); ) {
345 if (_comp.eq(fixcoincident(p0, *qp, c0), q)) {
353 real t, m12, M12, M21;
354 lineX.GenPosition(
false, s0,
357 t, t, t, t, m12, M12, M21, t);
359 for (
int sgn = -1; sgn <= 1; sgn += 2) {
362 sa = ConjugateDist(lineX, s0 + sa + sgn*_d,
false, m12, M12, M21)
364 qc = q + XPoint(sa, c0*sa);
367 }
while (qc.Dist(p0) <= maxdistx);
372 for (
auto qp = added.cbegin(); qp != added.cend(); ++qp) {
373 for (
int l = k + 1; l < m2; ++l)
374 skip[l] = skip[l] || qp->Dist(start[l]) < 2*_t1 - d3 - _delta;
378 for (
auto qp = r.begin(); qp != r.end(); ) {
379 if (!(qp->Dist(p0) <= maxdist))
384 vector<XPoint> v(r.size());
386 for (
auto p = r.cbegin(); p != r.cend(); ++p)
388 sort(v.begin(), v.end(), RankPoint(p0));
392 std::vector<Intersect::Point>
393 Intersect::AllInternal(
const GeodesicLine& lineX,
const GeodesicLine& lineY,
395 std::vector<int>& c,
bool cp)
const {
397 v = AllInt0(lineX, lineY, fmax(
real(0), maxdist), XPoint(p0));
398 int i = int(v.size());
401 for (
int j = 0; j < i; ++j) {
403 if (cp) c[j] = v[j].c;
410 GeodesicLine line = _geod.
Line(lat1, 0, 0,
414 real s = ConjugateDist(line, (1 + _f/2) * _a *
Math::pi() / 2,
true);
418 *lat2, t, t, t, t, t, t, t);
425 if (lata) *lata = 64;
426 if (latb) *latb = 90-64;
430 lat0 = 63, s0 = distpolar(lat0),
431 lat1 = 65, s1 = distpolar(lat1),
432 lat2 = 64, s2 = distpolar(lat2),
433 latx = lat2, sx = s2;
435 for (
int i = 0; i < 10; ++i) {
436 real den = (lat1-lat0)*s2 + (lat0-lat2)*s1 + (lat2-lat1)*s0;
437 if (!(den < 0 || den > 0))
break;
438 real latn = ((lat1-lat0)*(lat1+lat0)*s2 + (lat0-lat2)*(lat0+lat2)*s1 +
439 (lat2-lat1)*(lat2+lat1)*s0) / (2*den);
440 lat0 = lat1; s0 = s1;
441 lat1 = lat2; s1 = s2;
442 lat2 = latn; s2 = distpolar(lat2);
443 if (_f < 0 ? (s2 < sx) : (s2 > sx)) {
448 if (lata) *lata = latx;
449 if (latb) distpolar(latx, latb);
462 for (
int i = 0; i < 100; ++i) {
463 real t, m13, M13, M31;
464 line.GenPosition(
false, s,
467 t, t, t, t, m13, M13, M31, t);
470 m23 = m13 * M12 - m12 * M13,
472 M23 = M13 * M21 + (m12 == 0 ? 0 : (1 - M12 * M21) * m13/m12),
473 M32 = M31 * M12 + (m13 == 0 ? 0 : (1 - M13 * M31) * m12/m13);
474 real ds = semi ? m23 * M23 / (1 - M23*M32) : -m23 / M32;
476 if (!(fabs(ds) > _tol))
break;
485 real s = ConjugateDist(line, _d,
false);
487 XPoint p = Basic(line, line, XPoint(s/2, -3*s/2));
490 *ds = p.Dist() - 2*s;
505 azi0 = 46, ds0, s0 = conjdist(azi0, &ds0, &sa, &sb),
506 azi1 = 44, ds1, s1 = conjdist(azi1, &ds1, &sa, &sb),
507 azix = azi1, dsx = fabs(ds1), sx = s1, sax = sa, sbx = sb;
510 for (
int i = 0; i < 10 && ds1 != ds0; ++i) {
511 real azin = (azi0*ds1-azi1*ds0)/(ds1-ds0);
512 azi0 = azi1; s0 = s1; ds0 = ds1;
513 azi1 = azin; s1 = conjdist(azi1, &ds1, &sa, &sb);
514 if (fabs(ds1) < dsx) {
515 azix = azi1, sx = s1, dsx = fabs(ds1);
520 if (azi) *azi = azix;
527 Intersect::fixcoincident(
const Intersect::XPoint& p0,
528 const Intersect::XPoint& p) {
529 return fixcoincident(p0, p, p.c);
533 Intersect::fixcoincident(
const Intersect::XPoint& p0,
534 const Intersect::XPoint& p,
int c) {
535 if (c == 0)
return p;
543 real s = ((p0.x + c * p0.y) - (p.x + c * p.y))/2;
544 return p + XPoint(s, c*s);
549 const Intersect::XPoint& p) {
550 if (p.c == 0)
return p;
561 pya = p.y - p.c * p.x, sa = -p.x,
562 pyb = p.y - p.c * (p.x-sx), sb = sx - p.x,
563 pxc = p.x - p.c * p.y, sc = p.c * -p.y,
564 pxd = p.x - p.c * (p.y-sy), sd = p.c * (sy - p.y);
566 ga = 0 <= pya && pya <= sy,
567 gb = 0 <= pyb && pyb <= sy,
568 gc = 0 <= pxc && pxc <= sx,
569 gd = 0 <= pxd && pxd <= sx;
572 if (ga && gb) s = (sa + sb) / 2;
573 else if (gc && gd) s = (sc + sd) / 2;
574 else if (ga && gc) s = (sa + sc) / 2;
575 else if (ga && gd) s = (sa + sd) / 2;
576 else if (gb && gc) s = (sb + sc) / 2;
577 else if (gb && gd) s = (sb + sd) / 2;
583 if (fabs((p.x - p.y) + sy) < fabs((p.x - p.y) - sx))
584 s = (sy - (p.x + p.y))/2;
586 s = (sx - (p.x + p.y))/2;
590 if (fabs(p.x + p.y) < fabs((p.x + p.y) - (sx + sy)))
591 s = (0 - (p.x - p.y))/2;
593 s = ((sx - sy) - (p.x - p.y))/2;
596 return p + XPoint(s, p.c*s);
GeographicLib::Math::real real
Header for GeographicLib::Intersect class.
#define GEOGRAPHICLIB_PANIC(msg)
Math::real Position(real s12, real &lat2, real &lon2, real &azi2, real &m12, real &M12, real &M21, real &S12) const
GeodesicLine InverseLine(real lat1, real lon1, real lat2, real lon2, unsigned caps=ALL) const
GeodesicLine Line(real lat1, real lon1, real azi1, unsigned caps=ALL) const
Math::real Inverse(real lat1, real lon1, real lat2, real lon2, real &s12, real &azi1, real &azi2, real &m12, real &M12, real &M21, real &S12) const
Exception handling for GeographicLib.
Point Segment(Math::real latX1, Math::real lonX1, Math::real latX2, Math::real lonX2, Math::real latY1, Math::real lonY1, Math::real latY2, Math::real lonY2, int &segmode, int *c=nullptr) const
static const unsigned LineCaps
Point Closest(Math::real latX, Math::real lonX, Math::real aziX, Math::real latY, Math::real lonY, Math::real aziY, const Point &p0=Point(0, 0), int *c=nullptr) const
std::pair< Math::real, Math::real > Point
std::vector< Point > All(Math::real latX, Math::real lonX, Math::real aziX, Math::real latY, Math::real lonY, Math::real aziY, Math::real maxdist, std::vector< int > &c, const Point &p0=Point(0, 0)) const
Point Next(Math::real latX, Math::real lonX, Math::real aziX, Math::real aziY, int *c=nullptr) const
Intersect(const Geodesic &geod)
Mathematical functions needed by GeographicLib.
static void sincosde(T x, T t, T &sinx, T &cosx)
static T AngDiff(T x, T y, T &e)
Namespace for GeographicLib.