1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935
|
/******************************************************************************
*
* Project: OpenGIS Simple Features Reference Implementation
* Purpose: The OGRCircularString geometry class.
* Author: Even Rouault, even dot rouault at spatialys dot com
*
******************************************************************************
* Copyright (c) 2010, 2014, Even Rouault <even dot rouault at spatialys dot
*com>
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included
* in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
* OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
* DEALINGS IN THE SOFTWARE.
****************************************************************************/
#include "cpl_port.h"
#include "ogr_geometry.h"
#include <cmath>
#include <cstring>
#include <algorithm>
#include <limits>
#include <vector>
#include "cpl_error.h"
#include "ogr_core.h"
#include "ogr_geometry.h"
#include "ogr_p.h"
static inline double dist(double x0, double y0, double x1, double y1)
{
return std::sqrt((x1 - x0) * (x1 - x0) + (y1 - y0) * (y1 - y0));
}
/************************************************************************/
/* OGRCircularString() */
/************************************************************************/
/**
* \brief Create an empty circular string.
*/
OGRCircularString::OGRCircularString() = default;
/************************************************************************/
/* OGRCircularString( const OGRCircularString& ) */
/************************************************************************/
/**
* \brief Copy constructor.
*
* Note: before GDAL 2.1, only the default implementation of the constructor
* existed, which could be unsafe to use.
*
* @since GDAL 2.1
*/
OGRCircularString::OGRCircularString(const OGRCircularString &) = default;
/************************************************************************/
/* ~OGRCircularString() */
/************************************************************************/
OGRCircularString::~OGRCircularString() = default;
/************************************************************************/
/* operator=( const OGRCircularString& ) */
/************************************************************************/
/**
* \brief Assignment operator.
*
* Note: before GDAL 2.1, only the default implementation of the operator
* existed, which could be unsafe to use.
*
* @since GDAL 2.1
*/
OGRCircularString &OGRCircularString::operator=(const OGRCircularString &other)
{
if (this != &other)
{
OGRSimpleCurve::operator=(other);
}
return *this;
}
/************************************************************************/
/* getGeometryType() */
/************************************************************************/
OGRwkbGeometryType OGRCircularString::getGeometryType() const
{
if ((flags & OGR_G_3D) && (flags & OGR_G_MEASURED))
return wkbCircularStringZM;
else if (flags & OGR_G_MEASURED)
return wkbCircularStringM;
else if (flags & OGR_G_3D)
return wkbCircularStringZ;
else
return wkbCircularString;
}
/************************************************************************/
/* getGeometryName() */
/************************************************************************/
const char *OGRCircularString::getGeometryName() const
{
return "CIRCULARSTRING";
}
/************************************************************************/
/* clone() */
/************************************************************************/
OGRCircularString *OGRCircularString::clone() const
{
return new (std::nothrow) OGRCircularString(*this);
}
/************************************************************************/
/* importFromWkb() */
/* */
/* Initialize from serialized stream in well known binary */
/* format. */
/************************************************************************/
OGRErr OGRCircularString::importFromWkb(const unsigned char *pabyData,
size_t nSize, OGRwkbVariant eWkbVariant,
size_t &nBytesConsumedOut)
{
OGRErr eErr = OGRSimpleCurve::importFromWkb(pabyData, nSize, eWkbVariant,
nBytesConsumedOut);
if (eErr == OGRERR_NONE)
{
if (!IsValidFast())
{
empty();
return OGRERR_CORRUPT_DATA;
}
}
return eErr;
}
/************************************************************************/
/* exportToWkb() */
/* */
/* Build a well known binary representation of this object. */
/************************************************************************/
OGRErr OGRCircularString::exportToWkb(OGRwkbByteOrder eByteOrder,
unsigned char *pabyData,
OGRwkbVariant eWkbVariant) const
{
if (!IsValidFast())
{
return OGRERR_FAILURE;
}
// Does not make sense for new geometries, so patch it.
if (eWkbVariant == wkbVariantOldOgc)
eWkbVariant = wkbVariantIso;
return OGRSimpleCurve::exportToWkb(eByteOrder, pabyData, eWkbVariant);
}
/************************************************************************/
/* importFromWkt() */
/* */
/* Instantiate from well known text format. Currently this is */
/* `CIRCULARSTRING [Z] ( x y [z], x y [z], ...)', */
/************************************************************************/
OGRErr OGRCircularString::importFromWkt(const char **ppszInput)
{
const OGRErr eErr = OGRSimpleCurve::importFromWkt(ppszInput);
if (eErr == OGRERR_NONE)
{
if (!IsValidFast())
{
empty();
return OGRERR_CORRUPT_DATA;
}
}
return eErr;
}
/************************************************************************/
/* exportToWkt() */
/************************************************************************/
std::string OGRCircularString::exportToWkt(const OGRWktOptions &opts,
OGRErr *err) const
{
if (!IsValidFast())
{
if (err)
*err = OGRERR_FAILURE;
return std::string();
}
OGRWktOptions optsModified(opts);
optsModified.variant = wkbVariantIso;
return OGRSimpleCurve::exportToWkt(optsModified, err);
}
/************************************************************************/
/* get_Length() */
/* */
/* For now we return a simple euclidean 2D distance. */
/************************************************************************/
double OGRCircularString::get_Length() const
{
double dfLength = 0.0;
for (int i = 0; i < nPointCount - 2; i += 2)
{
const double x0 = paoPoints[i].x;
const double y0 = paoPoints[i].y;
const double x1 = paoPoints[i + 1].x;
const double y1 = paoPoints[i + 1].y;
const double x2 = paoPoints[i + 2].x;
const double y2 = paoPoints[i + 2].y;
double R = 0.0;
double cx = 0.0;
double cy = 0.0;
double alpha0 = 0.0;
double alpha1 = 0.0;
double alpha2 = 0.0;
if (OGRGeometryFactory::GetCurveParameters(
x0, y0, x1, y1, x2, y2, R, cx, cy, alpha0, alpha1, alpha2))
{
dfLength += fabs(alpha2 - alpha0) * R;
}
else
{
dfLength += dist(x0, y0, x2, y2);
}
}
return dfLength;
}
/************************************************************************/
/* ExtendEnvelopeWithCircular() */
/************************************************************************/
void OGRCircularString::ExtendEnvelopeWithCircular(
OGREnvelope *psEnvelope) const
{
if (!IsValidFast() || nPointCount == 0)
return;
// Loop through circular portions and determine if they include some
// extremities of the circle.
for (int i = 0; i < nPointCount - 2; i += 2)
{
const double x0 = paoPoints[i].x;
const double y0 = paoPoints[i].y;
const double x1 = paoPoints[i + 1].x;
const double y1 = paoPoints[i + 1].y;
const double x2 = paoPoints[i + 2].x;
const double y2 = paoPoints[i + 2].y;
double R = 0.0;
double cx = 0.0;
double cy = 0.0;
double alpha0 = 0.0;
double alpha1 = 0.0;
double alpha2 = 0.0;
if (OGRGeometryFactory::GetCurveParameters(
x0, y0, x1, y1, x2, y2, R, cx, cy, alpha0, alpha1, alpha2))
{
if (CPLIsNan(alpha0) || CPLIsNan(alpha2))
{
CPLError(CE_Failure, CPLE_AppDefined,
"GetCurveParameters returned NaN");
continue;
}
int quadrantStart =
static_cast<int>(std::floor(alpha0 / (M_PI / 2)));
int quadrantEnd = static_cast<int>(std::floor(alpha2 / (M_PI / 2)));
if (quadrantStart > quadrantEnd)
{
std::swap(quadrantStart, quadrantEnd);
}
// Transition trough quadrants in counter-clock wise direction.
for (int j = quadrantStart + 1; j <= quadrantEnd; ++j)
{
switch ((j + 8) % 4)
{
case 0:
psEnvelope->MaxX = std::max(psEnvelope->MaxX, cx + R);
break;
case 1:
psEnvelope->MaxY = std::max(psEnvelope->MaxY, cy + R);
break;
case 2:
psEnvelope->MinX = std::min(psEnvelope->MinX, cx - R);
break;
case 3:
psEnvelope->MinY = std::min(psEnvelope->MaxY, cy - R);
break;
default:
CPLAssert(false);
break;
}
}
}
}
}
/************************************************************************/
/* getEnvelope() */
/************************************************************************/
void OGRCircularString::getEnvelope(OGREnvelope *psEnvelope) const
{
OGRSimpleCurve::getEnvelope(psEnvelope);
ExtendEnvelopeWithCircular(psEnvelope);
}
/************************************************************************/
/* getEnvelope() */
/************************************************************************/
void OGRCircularString::getEnvelope(OGREnvelope3D *psEnvelope) const
{
OGRSimpleCurve::getEnvelope(psEnvelope);
ExtendEnvelopeWithCircular(psEnvelope);
}
/************************************************************************/
/* OGRCircularString::segmentize() */
/************************************************************************/
void OGRCircularString::segmentize(double dfMaxLength)
{
if (!IsValidFast() || nPointCount == 0)
return;
// So as to make sure that the same line followed in both directions
// result in the same segmentized line.
if (paoPoints[0].x < paoPoints[nPointCount - 1].x ||
(paoPoints[0].x == paoPoints[nPointCount - 1].x &&
paoPoints[0].y < paoPoints[nPointCount - 1].y))
{
reversePoints();
segmentize(dfMaxLength);
reversePoints();
}
std::vector<OGRRawPoint> aoRawPoint;
std::vector<double> adfZ;
for (int i = 0; i < nPointCount - 2; i += 2)
{
const double x0 = paoPoints[i].x;
const double y0 = paoPoints[i].y;
const double x1 = paoPoints[i + 1].x;
const double y1 = paoPoints[i + 1].y;
const double x2 = paoPoints[i + 2].x;
const double y2 = paoPoints[i + 2].y;
double R = 0.0;
double cx = 0.0;
double cy = 0.0;
double alpha0 = 0.0;
double alpha1 = 0.0;
double alpha2 = 0.0;
aoRawPoint.emplace_back(x0, y0);
if (padfZ)
adfZ.emplace_back(padfZ[i]);
// We have strong constraints on the number of intermediate points
// we can add.
if (OGRGeometryFactory::GetCurveParameters(
x0, y0, x1, y1, x2, y2, R, cx, cy, alpha0, alpha1, alpha2))
{
// It is an arc circle.
const double dfSegmentLength1 = fabs(alpha1 - alpha0) * R;
const double dfSegmentLength2 = fabs(alpha2 - alpha1) * R;
if (dfSegmentLength1 > dfMaxLength ||
dfSegmentLength2 > dfMaxLength)
{
const double dfVal =
1 + 2 * std::floor(dfSegmentLength1 / dfMaxLength / 2.0);
if (dfVal >= std::numeric_limits<int>::max() || dfVal < 0.0 ||
CPLIsNan(dfVal))
{
CPLError(CE_Failure, CPLE_AppDefined,
"segmentize nIntermediatePoints invalid: %lf",
dfVal);
break;
}
const int nIntermediatePoints = static_cast<int>(dfVal);
const double dfStep =
(alpha1 - alpha0) / (nIntermediatePoints + 1);
for (int j = 1; j <= nIntermediatePoints; ++j)
{
double alpha = alpha0 + dfStep * j;
const double x = cx + R * cos(alpha);
const double y = cy + R * sin(alpha);
aoRawPoint.emplace_back(x, y);
if (padfZ)
{
const double z = padfZ[i] + (padfZ[i + 1] - padfZ[i]) *
(alpha - alpha0) /
(alpha1 - alpha0);
adfZ.emplace_back(z);
}
}
}
aoRawPoint.emplace_back(x1, y1);
if (padfZ)
adfZ.emplace_back(padfZ[i + 1]);
if (dfSegmentLength1 > dfMaxLength ||
dfSegmentLength2 > dfMaxLength)
{
const double dfVal =
1 + 2 * std::floor(dfSegmentLength2 / dfMaxLength / 2.0);
if (dfVal >= std::numeric_limits<int>::max() || dfVal < 0.0 ||
CPLIsNan(dfVal))
{
CPLError(CE_Failure, CPLE_AppDefined,
"segmentize nIntermediatePoints invalid 2: %lf",
dfVal);
break;
}
int nIntermediatePoints = static_cast<int>(dfVal);
const double dfStep =
(alpha2 - alpha1) / (nIntermediatePoints + 1);
for (int j = 1; j <= nIntermediatePoints; ++j)
{
const double alpha = alpha1 + dfStep * j;
const double x = cx + R * cos(alpha);
const double y = cy + R * sin(alpha);
aoRawPoint.emplace_back(x, y);
if (padfZ)
{
const double z =
padfZ[i + 1] + (padfZ[i + 2] - padfZ[i + 1]) *
(alpha - alpha1) /
(alpha2 - alpha1);
adfZ.emplace_back(z);
}
}
}
}
else
{
// It is a straight line.
const double dfSegmentLength1 = dist(x0, y0, x1, y1);
const double dfSegmentLength2 = dist(x1, y1, x2, y2);
if (dfSegmentLength1 > dfMaxLength ||
dfSegmentLength2 > dfMaxLength)
{
const double dfVal =
1 + 2 * std::ceil(dfSegmentLength1 / dfMaxLength / 2.0);
if (dfVal >= std::numeric_limits<int>::max() || dfVal < 0.0 ||
CPLIsNan(dfVal))
{
CPLError(CE_Failure, CPLE_AppDefined,
"segmentize nIntermediatePoints invalid 2: %lf",
dfVal);
break;
}
int nIntermediatePoints = static_cast<int>(dfVal);
for (int j = 1; j <= nIntermediatePoints; ++j)
{
aoRawPoint.emplace_back(
x0 + j * (x1 - x0) / (nIntermediatePoints + 1),
y0 + j * (y1 - y0) / (nIntermediatePoints + 1));
if (padfZ)
adfZ.emplace_back(padfZ[i] +
j * (padfZ[i + 1] - padfZ[i]) /
(nIntermediatePoints + 1));
}
}
aoRawPoint.emplace_back(x1, y1);
if (padfZ)
adfZ.emplace_back(padfZ[i + 1]);
if (dfSegmentLength1 > dfMaxLength ||
dfSegmentLength2 > dfMaxLength)
{
const double dfVal =
1 + 2 * std::ceil(dfSegmentLength2 / dfMaxLength / 2.0);
if (dfVal >= std::numeric_limits<int>::max() || dfVal < 0.0 ||
CPLIsNan(dfVal))
{
CPLError(CE_Failure, CPLE_AppDefined,
"segmentize nIntermediatePoints invalid 3: %lf",
dfVal);
break;
}
const int nIntermediatePoints = static_cast<int>(dfVal);
for (int j = 1; j <= nIntermediatePoints; ++j)
{
aoRawPoint.emplace_back(
x1 + j * (x2 - x1) / (nIntermediatePoints + 1),
y1 + j * (y2 - y1) / (nIntermediatePoints + 1));
if (padfZ)
adfZ.emplace_back(padfZ[i + 1] +
j * (padfZ[i + 2] - padfZ[i + 1]) /
(nIntermediatePoints + 1));
}
}
}
}
aoRawPoint.push_back(paoPoints[nPointCount - 1]);
if (padfZ)
adfZ.push_back(padfZ[nPointCount - 1]);
CPLAssert(aoRawPoint.empty() ||
(aoRawPoint.size() >= 3 && (aoRawPoint.size() % 2) == 1));
if (padfZ)
{
CPLAssert(adfZ.size() == aoRawPoint.size());
}
// Is there actually something to modify?
if (nPointCount < static_cast<int>(aoRawPoint.size()))
{
nPointCount = static_cast<int>(aoRawPoint.size());
paoPoints = static_cast<OGRRawPoint *>(
CPLRealloc(paoPoints, sizeof(OGRRawPoint) * nPointCount));
memcpy(paoPoints, &aoRawPoint[0], sizeof(OGRRawPoint) * nPointCount);
if (padfZ)
{
padfZ = static_cast<double *>(
CPLRealloc(padfZ, sizeof(double) * aoRawPoint.size()));
memcpy(padfZ, &adfZ[0], sizeof(double) * nPointCount);
}
}
}
/************************************************************************/
/* Value() */
/* */
/* Get an interpolated point at some distance along the curve. */
/************************************************************************/
void OGRCircularString::Value(double dfDistance, OGRPoint *poPoint) const
{
if (dfDistance < 0)
{
StartPoint(poPoint);
return;
}
double dfLength = 0;
for (int i = 0; i < nPointCount - 2; i += 2)
{
const double x0 = paoPoints[i].x;
const double y0 = paoPoints[i].y;
const double x1 = paoPoints[i + 1].x;
const double y1 = paoPoints[i + 1].y;
const double x2 = paoPoints[i + 2].x;
const double y2 = paoPoints[i + 2].y;
double R = 0.0;
double cx = 0.0;
double cy = 0.0;
double alpha0 = 0.0;
double alpha1 = 0.0;
double alpha2 = 0.0;
// We have strong constraints on the number of intermediate points
// we can add.
if (OGRGeometryFactory::GetCurveParameters(
x0, y0, x1, y1, x2, y2, R, cx, cy, alpha0, alpha1, alpha2))
{
// It is an arc circle.
const double dfSegLength = fabs(alpha2 - alpha0) * R;
if (dfSegLength > 0)
{
if ((dfLength <= dfDistance) &&
((dfLength + dfSegLength) >= dfDistance))
{
const double dfRatio =
(dfDistance - dfLength) / dfSegLength;
const double alpha =
alpha0 * (1 - dfRatio) + alpha2 * dfRatio;
const double x = cx + R * cos(alpha);
const double y = cy + R * sin(alpha);
poPoint->setX(x);
poPoint->setY(y);
if (getCoordinateDimension() == 3)
poPoint->setZ(padfZ[i] * (1 - dfRatio) +
padfZ[i + 2] * dfRatio);
return;
}
dfLength += dfSegLength;
}
}
else
{
// It is a straight line.
const double dfSegLength = dist(x0, y0, x2, y2);
if (dfSegLength > 0)
{
if ((dfLength <= dfDistance) &&
((dfLength + dfSegLength) >= dfDistance))
{
const double dfRatio =
(dfDistance - dfLength) / dfSegLength;
poPoint->setX(paoPoints[i].x * (1 - dfRatio) +
paoPoints[i + 2].x * dfRatio);
poPoint->setY(paoPoints[i].y * (1 - dfRatio) +
paoPoints[i + 2].y * dfRatio);
if (getCoordinateDimension() == 3)
poPoint->setZ(padfZ[i] * (1 - dfRatio) +
padfZ[i + 2] * dfRatio);
return;
}
dfLength += dfSegLength;
}
}
}
EndPoint(poPoint);
}
/************************************************************************/
/* CurveToLine() */
/************************************************************************/
OGRLineString *
OGRCircularString::CurveToLine(double dfMaxAngleStepSizeDegrees,
const char *const *papszOptions) const
{
OGRLineString *poLine = new OGRLineString();
poLine->assignSpatialReference(getSpatialReference());
const bool bHasZ = getCoordinateDimension() == 3;
for (int i = 0; i < nPointCount - 2; i += 2)
{
OGRLineString *poArc = OGRGeometryFactory::curveToLineString(
paoPoints[i].x, paoPoints[i].y, padfZ ? padfZ[i] : 0.0,
paoPoints[i + 1].x, paoPoints[i + 1].y, padfZ ? padfZ[i + 1] : 0.0,
paoPoints[i + 2].x, paoPoints[i + 2].y, padfZ ? padfZ[i + 2] : 0.0,
bHasZ, dfMaxAngleStepSizeDegrees, papszOptions);
poLine->addSubLineString(poArc, (i == 0) ? 0 : 1);
delete poArc;
}
return poLine;
}
/************************************************************************/
/* IsValidFast() */
/************************************************************************/
OGRBoolean OGRCircularString::IsValidFast() const
{
if (nPointCount == 1 || nPointCount == 2 ||
(nPointCount >= 3 && (nPointCount % 2) == 0))
{
CPLError(CE_Failure, CPLE_NotSupported,
"Bad number of points in circular string : %d", nPointCount);
return FALSE;
}
return TRUE;
}
/************************************************************************/
/* IsValid() */
/************************************************************************/
OGRBoolean OGRCircularString::IsValid() const
{
return IsValidFast() && OGRGeometry::IsValid();
}
/************************************************************************/
/* hasCurveGeometry() */
/************************************************************************/
OGRBoolean
OGRCircularString::hasCurveGeometry(int /* bLookForNonLinear */) const
{
return TRUE;
}
/************************************************************************/
/* getLinearGeometry() */
/************************************************************************/
OGRGeometry *
OGRCircularString::getLinearGeometry(double dfMaxAngleStepSizeDegrees,
const char *const *papszOptions) const
{
return CurveToLine(dfMaxAngleStepSizeDegrees, papszOptions);
}
//! @cond Doxygen_Suppress
/************************************************************************/
/* GetCasterToLineString() */
/************************************************************************/
static OGRLineString *CasterToLineString(OGRCurve *poGeom)
{
CPLError(CE_Failure, CPLE_AppDefined, "%s found. Conversion impossible",
poGeom->getGeometryName());
delete poGeom;
return nullptr;
}
OGRCurveCasterToLineString OGRCircularString::GetCasterToLineString() const
{
return ::CasterToLineString;
}
/************************************************************************/
/* GetCasterToLinearRing() */
/************************************************************************/
static OGRLinearRing *CasterToLinearRing(OGRCurve *poGeom)
{
CPLError(CE_Failure, CPLE_AppDefined, "%s found. Conversion impossible",
poGeom->getGeometryName());
delete poGeom;
return nullptr;
}
OGRCurveCasterToLinearRing OGRCircularString::GetCasterToLinearRing() const
{
return ::CasterToLinearRing;
}
//! @endcond
/************************************************************************/
/* IsFullCircle() */
/************************************************************************/
int OGRCircularString::IsFullCircle(double &cx, double &cy,
double &square_R) const
{
if (getNumPoints() == 3 && get_IsClosed())
{
const double x0 = getX(0);
const double y0 = getY(0);
const double x1 = getX(1);
const double y1 = getY(1);
cx = (x0 + x1) / 2;
cy = (y0 + y1) / 2;
square_R = (x1 - cx) * (x1 - cx) + (y1 - cy) * (y1 - cy);
return TRUE;
}
// Full circle defined by 2 arcs?
else if (getNumPoints() == 5 && get_IsClosed())
{
double R_1 = 0.0;
double cx_1 = 0.0;
double cy_1 = 0.0;
double alpha0_1 = 0.0;
double alpha1_1 = 0.0;
double alpha2_1 = 0.0;
double R_2 = 0.0;
double cx_2 = 0.0;
double cy_2 = 0.0;
double alpha0_2 = 0.0;
double alpha1_2 = 0.0;
double alpha2_2 = 0.0;
if (OGRGeometryFactory::GetCurveParameters(
getX(0), getY(0), getX(1), getY(1), getX(2), getY(2), R_1, cx_1,
cy_1, alpha0_1, alpha1_1, alpha2_1) &&
OGRGeometryFactory::GetCurveParameters(
getX(2), getY(2), getX(3), getY(3), getX(4), getY(4), R_2, cx_2,
cy_2, alpha0_2, alpha1_2, alpha2_2) &&
fabs(R_1 - R_2) < 1e-10 && fabs(cx_1 - cx_2) < 1e-10 &&
fabs(cy_1 - cy_2) < 1e-10 &&
(alpha2_1 - alpha0_1) * (alpha2_2 - alpha0_2) > 0)
{
cx = cx_1;
cy = cy_1;
square_R = R_1 * R_1;
return TRUE;
}
}
return FALSE;
}
/************************************************************************/
/* get_AreaOfCurveSegments() */
/************************************************************************/
//! @cond Doxygen_Suppress
double OGRCircularString::get_AreaOfCurveSegments() const
{
double dfArea = 0.0;
for (int i = 0; i < getNumPoints() - 2; i += 2)
{
const double x0 = getX(i);
const double y0 = getY(i);
const double x1 = getX(i + 1);
const double y1 = getY(i + 1);
const double x2 = getX(i + 2);
const double y2 = getY(i + 2);
double R = 0.0;
double cx = 0.0;
double cy = 0.0;
double alpha0 = 0.0;
double alpha1 = 0.0;
double alpha2 = 0.0;
if (OGRGeometryFactory::GetCurveParameters(
x0, y0, x1, y1, x2, y2, R, cx, cy, alpha0, alpha1, alpha2))
{
// Should be <= PI in absolute value.
const double delta_alpha01 = alpha1 - alpha0;
const double delta_alpha12 = alpha2 - alpha1; // Same.
// http://en.wikipedia.org/wiki/Circular_segment
dfArea += 0.5 * R * R *
fabs(delta_alpha01 - sin(delta_alpha01) + delta_alpha12 -
sin(delta_alpha12));
}
}
return dfArea;
}
//! @endcond
/************************************************************************/
/* get_Area() */
/************************************************************************/
double OGRCircularString::get_Area() const
{
if (IsEmpty() || !get_IsClosed())
return 0;
double cx = 0.0;
double cy = 0.0;
double square_R = 0.0;
if (IsFullCircle(cx, cy, square_R))
{
return M_PI * square_R;
}
// Optimization for convex rings.
if (IsConvex())
{
// Compute area of shape without the circular segments.
double dfArea = get_LinearArea();
// Add the area of the spherical segments.
dfArea += get_AreaOfCurveSegments();
return dfArea;
}
OGRLineString *poLS = CurveToLine();
const double dfArea = poLS->get_Area();
delete poLS;
return dfArea;
}
//! @cond Doxygen_Suppress
/************************************************************************/
/* ContainsPoint() */
/************************************************************************/
int OGRCircularString::ContainsPoint(const OGRPoint *p) const
{
double cx = 0.0;
double cy = 0.0;
double square_R = 0.0;
if (IsFullCircle(cx, cy, square_R))
{
const double square_dist = (p->getX() - cx) * (p->getX() - cx) +
(p->getY() - cy) * (p->getY() - cy);
return square_dist < square_R;
}
return -1;
}
/************************************************************************/
/* IntersectsPoint() */
/************************************************************************/
int OGRCircularString::IntersectsPoint(const OGRPoint *p) const
{
double cx = 0.0;
double cy = 0.0;
double square_R = 0.0;
if (IsFullCircle(cx, cy, square_R))
{
const double square_dist = (p->getX() - cx) * (p->getX() - cx) +
(p->getY() - cy) * (p->getY() - cy);
return square_dist <= square_R;
}
return -1;
}
//! @endcond
|