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 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264
|
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/*************************************************************************
*
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* Copyright 2000, 2010 Oracle and/or its affiliates.
*
* OpenOffice.org - a multi-platform office productivity suite
*
* This file is part of OpenOffice.org.
*
* OpenOffice.org is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License version 3
* only, as published by the Free Software Foundation.
*
* OpenOffice.org is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License version 3 for more details
* (a copy is included in the LICENSE file that accompanied this code).
*
* You should have received a copy of the GNU Lesser General Public License
* version 3 along with OpenOffice.org. If not, see
* <http://www.openoffice.org/license.html>
* for a copy of the LGPLv3 License.
*
************************************************************************/
#include <osl/diagnose.h>
#include <basegfx/polygon/b3dpolygontools.hxx>
#include <basegfx/polygon/b3dpolygon.hxx>
#include <basegfx/numeric/ftools.hxx>
#include <basegfx/range/b3drange.hxx>
#include <basegfx/point/b2dpoint.hxx>
#include <basegfx/matrix/b3dhommatrix.hxx>
#include <basegfx/polygon/b2dpolygon.hxx>
#include <basegfx/polygon/b2dpolygontools.hxx>
#include <basegfx/tuple/b3ituple.hxx>
#include <numeric>
//////////////////////////////////////////////////////////////////////////////
namespace basegfx
{
namespace tools
{
// B3DPolygon tools
void checkClosed(B3DPolygon& rCandidate)
{
while(rCandidate.count() > 1L
&& rCandidate.getB3DPoint(0L).equal(rCandidate.getB3DPoint(rCandidate.count() - 1L)))
{
rCandidate.setClosed(true);
rCandidate.remove(rCandidate.count() - 1L);
}
}
// Get successor and predecessor indices. Returning the same index means there
// is none. Same for successor.
sal_uInt32 getIndexOfPredecessor(sal_uInt32 nIndex, const B3DPolygon& rCandidate)
{
OSL_ENSURE(nIndex < rCandidate.count(), "getIndexOfPredecessor: Access to polygon out of range (!)");
if(nIndex)
{
return nIndex - 1L;
}
else if(rCandidate.count())
{
return rCandidate.count() - 1L;
}
else
{
return nIndex;
}
}
sal_uInt32 getIndexOfSuccessor(sal_uInt32 nIndex, const B3DPolygon& rCandidate)
{
OSL_ENSURE(nIndex < rCandidate.count(), "getIndexOfPredecessor: Access to polygon out of range (!)");
if(nIndex + 1L < rCandidate.count())
{
return nIndex + 1L;
}
else
{
return 0L;
}
}
B3DRange getRange(const B3DPolygon& rCandidate)
{
B3DRange aRetval;
const sal_uInt32 nPointCount(rCandidate.count());
for(sal_uInt32 a(0L); a < nPointCount; a++)
{
const B3DPoint aTestPoint(rCandidate.getB3DPoint(a));
aRetval.expand(aTestPoint);
}
return aRetval;
}
B3DVector getNormal(const B3DPolygon& rCandidate)
{
return rCandidate.getNormal();
}
B3DVector getPositiveOrientedNormal(const B3DPolygon& rCandidate)
{
B3DVector aRetval(rCandidate.getNormal());
if(ORIENTATION_NEGATIVE == getOrientation(rCandidate))
{
aRetval = -aRetval;
}
return aRetval;
}
B2VectorOrientation getOrientation(const B3DPolygon& rCandidate)
{
B2VectorOrientation eRetval(ORIENTATION_NEUTRAL);
if(rCandidate.count() > 2L)
{
const double fSignedArea(getSignedArea(rCandidate));
if(fSignedArea > 0.0)
{
eRetval = ORIENTATION_POSITIVE;
}
else if(fSignedArea < 0.0)
{
eRetval = ORIENTATION_NEGATIVE;
}
}
return eRetval;
}
double getSignedArea(const B3DPolygon& rCandidate)
{
double fRetval(0.0);
const sal_uInt32 nPointCount(rCandidate.count());
if(nPointCount > 2)
{
const B3DVector aAbsNormal(absolute(getNormal(rCandidate)));
sal_uInt16 nCase(3); // default: ignore z
if(aAbsNormal.getX() > aAbsNormal.getY())
{
if(aAbsNormal.getX() > aAbsNormal.getZ())
{
nCase = 1; // ignore x
}
}
else if(aAbsNormal.getY() > aAbsNormal.getZ())
{
nCase = 2; // ignore y
}
B3DPoint aPreviousPoint(rCandidate.getB3DPoint(nPointCount - 1L));
for(sal_uInt32 a(0L); a < nPointCount; a++)
{
const B3DPoint aCurrentPoint(rCandidate.getB3DPoint(a));
switch(nCase)
{
case 1: // ignore x
fRetval += aPreviousPoint.getZ() * aCurrentPoint.getY();
fRetval -= aPreviousPoint.getY() * aCurrentPoint.getZ();
break;
case 2: // ignore y
fRetval += aPreviousPoint.getX() * aCurrentPoint.getZ();
fRetval -= aPreviousPoint.getZ() * aCurrentPoint.getX();
break;
case 3: // ignore z
fRetval += aPreviousPoint.getX() * aCurrentPoint.getY();
fRetval -= aPreviousPoint.getY() * aCurrentPoint.getX();
break;
}
// prepare next step
aPreviousPoint = aCurrentPoint;
}
switch(nCase)
{
case 1: // ignore x
fRetval /= 2.0 * aAbsNormal.getX();
break;
case 2: // ignore y
fRetval /= 2.0 * aAbsNormal.getY();
break;
case 3: // ignore z
fRetval /= 2.0 * aAbsNormal.getZ();
break;
}
}
return fRetval;
}
double getArea(const B3DPolygon& rCandidate)
{
double fRetval(0.0);
if(rCandidate.count() > 2)
{
fRetval = getSignedArea(rCandidate);
const double fZero(0.0);
if(fTools::less(fRetval, fZero))
{
fRetval = -fRetval;
}
}
return fRetval;
}
double getEdgeLength(const B3DPolygon& rCandidate, sal_uInt32 nIndex)
{
OSL_ENSURE(nIndex < rCandidate.count(), "getEdgeLength: Access to polygon out of range (!)");
double fRetval(0.0);
const sal_uInt32 nPointCount(rCandidate.count());
if(nIndex < nPointCount)
{
if(rCandidate.isClosed() || ((nIndex + 1L) != nPointCount))
{
const sal_uInt32 nNextIndex(getIndexOfSuccessor(nIndex, rCandidate));
const B3DPoint aCurrentPoint(rCandidate.getB3DPoint(nIndex));
const B3DPoint aNextPoint(rCandidate.getB3DPoint(nNextIndex));
const B3DVector aVector(aNextPoint - aCurrentPoint);
fRetval = aVector.getLength();
}
}
return fRetval;
}
double getLength(const B3DPolygon& rCandidate)
{
double fRetval(0.0);
const sal_uInt32 nPointCount(rCandidate.count());
if(nPointCount > 1L)
{
const sal_uInt32 nLoopCount(rCandidate.isClosed() ? nPointCount : nPointCount - 1L);
for(sal_uInt32 a(0L); a < nLoopCount; a++)
{
const sal_uInt32 nNextIndex(getIndexOfSuccessor(a, rCandidate));
const B3DPoint aCurrentPoint(rCandidate.getB3DPoint(a));
const B3DPoint aNextPoint(rCandidate.getB3DPoint(nNextIndex));
const B3DVector aVector(aNextPoint - aCurrentPoint);
fRetval += aVector.getLength();
}
}
return fRetval;
}
B3DPoint getPositionAbsolute(const B3DPolygon& rCandidate, double fDistance, double fLength)
{
B3DPoint aRetval;
const sal_uInt32 nPointCount(rCandidate.count());
if(nPointCount > 1L)
{
sal_uInt32 nIndex(0L);
bool bIndexDone(false);
const double fZero(0.0);
double fEdgeLength(fZero);
// get length if not given
if(fTools::equalZero(fLength))
{
fLength = getLength(rCandidate);
}
// handle fDistance < 0.0
if(fTools::less(fDistance, fZero))
{
if(rCandidate.isClosed())
{
// if fDistance < 0.0 increment with multiple of fLength
sal_uInt32 nCount(sal_uInt32(-fDistance / fLength));
fDistance += double(nCount + 1L) * fLength;
}
else
{
// crop to polygon start
fDistance = fZero;
bIndexDone = true;
}
}
// handle fDistance >= fLength
if(fTools::moreOrEqual(fDistance, fLength))
{
if(rCandidate.isClosed())
{
// if fDistance >= fLength decrement with multiple of fLength
sal_uInt32 nCount(sal_uInt32(fDistance / fLength));
fDistance -= (double)(nCount) * fLength;
}
else
{
// crop to polygon end
fDistance = fZero;
nIndex = nPointCount - 1L;
bIndexDone = true;
}
}
// look for correct index. fDistance is now [0.0 .. fLength[
if(!bIndexDone)
{
do
{
// get length of next edge
fEdgeLength = getEdgeLength(rCandidate, nIndex);
if(fTools::moreOrEqual(fDistance, fEdgeLength))
{
// go to next edge
fDistance -= fEdgeLength;
nIndex++;
}
else
{
// it's on this edge, stop
bIndexDone = true;
}
} while (!bIndexDone);
}
// get the point using nIndex
aRetval = rCandidate.getB3DPoint(nIndex);
// if fDistance != 0.0, move that length on the edge. The edge
// length is in fEdgeLength.
if(!fTools::equalZero(fDistance))
{
sal_uInt32 nNextIndex(getIndexOfSuccessor(nIndex, rCandidate));
const B3DPoint aNextPoint(rCandidate.getB3DPoint(nNextIndex));
double fRelative(fZero);
if(!fTools::equalZero(fEdgeLength))
{
fRelative = fDistance / fEdgeLength;
}
// add calculated average value to the return value
aRetval += interpolate(aRetval, aNextPoint, fRelative);
}
}
return aRetval;
}
B3DPoint getPositionRelative(const B3DPolygon& rCandidate, double fDistance, double fLength)
{
// get length if not given
if(fTools::equalZero(fLength))
{
fLength = getLength(rCandidate);
}
// multiply fDistance with real length to get absolute position and
// use getPositionAbsolute
return getPositionAbsolute(rCandidate, fDistance * fLength, fLength);
}
void applyLineDashing(const B3DPolygon& rCandidate, const ::std::vector<double>& rDotDashArray, B3DPolyPolygon* pLineTarget, B3DPolyPolygon* pGapTarget, double fDotDashLength)
{
const sal_uInt32 nPointCount(rCandidate.count());
const sal_uInt32 nDotDashCount(rDotDashArray.size());
if(fTools::lessOrEqual(fDotDashLength, 0.0))
{
fDotDashLength = ::std::accumulate(rDotDashArray.begin(), rDotDashArray.end(), 0.0);
}
if(fTools::more(fDotDashLength, 0.0) && (pLineTarget || pGapTarget) && nPointCount)
{
// clear targets
if(pLineTarget)
{
pLineTarget->clear();
}
if(pGapTarget)
{
pGapTarget->clear();
}
// prepare current edge's start
B3DPoint aCurrentPoint(rCandidate.getB3DPoint(0));
const sal_uInt32 nEdgeCount(rCandidate.isClosed() ? nPointCount : nPointCount - 1);
// prepare DotDashArray iteration and the line/gap switching bool
sal_uInt32 nDotDashIndex(0);
bool bIsLine(true);
double fDotDashMovingLength(rDotDashArray[0]);
B3DPolygon aSnippet;
// iterate over all edges
for(sal_uInt32 a(0); a < nEdgeCount; a++)
{
// update current edge
double fLastDotDashMovingLength(0.0);
const sal_uInt32 nNextIndex((a + 1) % nPointCount);
const B3DPoint aNextPoint(rCandidate.getB3DPoint(nNextIndex));
const double fEdgeLength(B3DVector(aNextPoint - aCurrentPoint).getLength());
while(fTools::less(fDotDashMovingLength, fEdgeLength))
{
// new split is inside edge, create and append snippet [fLastDotDashMovingLength, fDotDashMovingLength]
const bool bHandleLine(bIsLine && pLineTarget);
const bool bHandleGap(!bIsLine && pGapTarget);
if(bHandleLine || bHandleGap)
{
if(!aSnippet.count())
{
aSnippet.append(interpolate(aCurrentPoint, aNextPoint, fLastDotDashMovingLength / fEdgeLength));
}
aSnippet.append(interpolate(aCurrentPoint, aNextPoint, fDotDashMovingLength / fEdgeLength));
if(bHandleLine)
{
pLineTarget->append(aSnippet);
}
else
{
pGapTarget->append(aSnippet);
}
aSnippet.clear();
}
// prepare next DotDashArray step and flip line/gap flag
fLastDotDashMovingLength = fDotDashMovingLength;
fDotDashMovingLength += rDotDashArray[(++nDotDashIndex) % nDotDashCount];
bIsLine = !bIsLine;
}
// append snippet [fLastDotDashMovingLength, fEdgeLength]
const bool bHandleLine(bIsLine && pLineTarget);
const bool bHandleGap(!bIsLine && pGapTarget);
if(bHandleLine || bHandleGap)
{
if(!aSnippet.count())
{
aSnippet.append(interpolate(aCurrentPoint, aNextPoint, fLastDotDashMovingLength / fEdgeLength));
}
aSnippet.append(aNextPoint);
}
// prepare move to next edge
fDotDashMovingLength -= fEdgeLength;
// prepare next edge step (end point gets new start point)
aCurrentPoint = aNextPoint;
}
// append last intermediate results (if exists)
if(aSnippet.count())
{
if(bIsLine && pLineTarget)
{
pLineTarget->append(aSnippet);
}
else if(!bIsLine && pGapTarget)
{
pGapTarget->append(aSnippet);
}
}
// check if start and end polygon may be merged
if(pLineTarget)
{
const sal_uInt32 nCount(pLineTarget->count());
if(nCount > 1)
{
// these polygons were created above, there exists none with less than two points,
// thus dircet point access below is allowed
const B3DPolygon aFirst(pLineTarget->getB3DPolygon(0));
B3DPolygon aLast(pLineTarget->getB3DPolygon(nCount - 1));
if(aFirst.getB3DPoint(0).equal(aLast.getB3DPoint(aLast.count() - 1)))
{
// start of first and end of last are the same -> merge them
aLast.append(aFirst);
aLast.removeDoublePoints();
pLineTarget->setB3DPolygon(0, aLast);
pLineTarget->remove(nCount - 1);
}
}
}
if(pGapTarget)
{
const sal_uInt32 nCount(pGapTarget->count());
if(nCount > 1)
{
// these polygons were created above, there exists none with less than two points,
// thus dircet point access below is allowed
const B3DPolygon aFirst(pGapTarget->getB3DPolygon(0));
B3DPolygon aLast(pGapTarget->getB3DPolygon(nCount - 1));
if(aFirst.getB3DPoint(0).equal(aLast.getB3DPoint(aLast.count() - 1)))
{
// start of first and end of last are the same -> merge them
aLast.append(aFirst);
aLast.removeDoublePoints();
pGapTarget->setB3DPolygon(0, aLast);
pGapTarget->remove(nCount - 1);
}
}
}
}
else
{
// parameters make no sense, just add source to targets
if(pLineTarget)
{
pLineTarget->append(rCandidate);
}
if(pGapTarget)
{
pGapTarget->append(rCandidate);
}
}
}
B3DPolygon applyDefaultNormalsSphere( const B3DPolygon& rCandidate, const B3DPoint& rCenter)
{
B3DPolygon aRetval(rCandidate);
for(sal_uInt32 a(0L); a < aRetval.count(); a++)
{
B3DVector aVector(aRetval.getB3DPoint(a) - rCenter);
aVector.normalize();
aRetval.setNormal(a, aVector);
}
return aRetval;
}
B3DPolygon invertNormals( const B3DPolygon& rCandidate)
{
B3DPolygon aRetval(rCandidate);
if(aRetval.areNormalsUsed())
{
for(sal_uInt32 a(0L); a < aRetval.count(); a++)
{
aRetval.setNormal(a, -aRetval.getNormal(a));
}
}
return aRetval;
}
B3DPolygon applyDefaultTextureCoordinatesParallel( const B3DPolygon& rCandidate, const B3DRange& rRange, bool bChangeX, bool bChangeY)
{
B3DPolygon aRetval(rCandidate);
if(bChangeX || bChangeY)
{
// create projection of standard texture coordinates in (X, Y) onto
// the 3d coordinates straight
const double fWidth(rRange.getWidth());
const double fHeight(rRange.getHeight());
const bool bWidthSet(!fTools::equalZero(fWidth));
const bool bHeightSet(!fTools::equalZero(fHeight));
const double fOne(1.0);
for(sal_uInt32 a(0L); a < aRetval.count(); a++)
{
const B3DPoint aPoint(aRetval.getB3DPoint(a));
B2DPoint aTextureCoordinate(aRetval.getTextureCoordinate(a));
if(bChangeX)
{
if(bWidthSet)
{
aTextureCoordinate.setX((aPoint.getX() - rRange.getMinX()) / fWidth);
}
else
{
aTextureCoordinate.setX(0.0);
}
}
if(bChangeY)
{
if(bHeightSet)
{
aTextureCoordinate.setY(fOne - ((aPoint.getY() - rRange.getMinY()) / fHeight));
}
else
{
aTextureCoordinate.setY(fOne);
}
}
aRetval.setTextureCoordinate(a, aTextureCoordinate);
}
}
return aRetval;
}
B3DPolygon applyDefaultTextureCoordinatesSphere( const B3DPolygon& rCandidate, const B3DPoint& rCenter, bool bChangeX, bool bChangeY)
{
B3DPolygon aRetval(rCandidate);
if(bChangeX || bChangeY)
{
// create texture coordinates using sphere projection to cartesian coordinates,
// use object's center as base
const double fOne(1.0);
const sal_uInt32 nPointCount(aRetval.count());
bool bPolarPoints(false);
sal_uInt32 a;
// create center cartesian coordinates to have a possibility to decide if on boundary
// transitions which value to choose
const B3DRange aPlaneRange(getRange(rCandidate));
const B3DPoint aPlaneCenter(aPlaneRange.getCenter() - rCenter);
const double fXCenter(fOne - ((atan2(aPlaneCenter.getZ(), aPlaneCenter.getX()) + F_PI) / F_2PI));
for(a = 0L; a < nPointCount; a++)
{
const B3DVector aVector(aRetval.getB3DPoint(a) - rCenter);
const double fY(fOne - ((atan2(aVector.getY(), aVector.getXZLength()) + F_PI2) / F_PI));
B2DPoint aTexCoor(aRetval.getTextureCoordinate(a));
if(fTools::equalZero(fY))
{
// point is a north polar point, no useful X-coordinate can be created.
if(bChangeY)
{
aTexCoor.setY(0.0);
if(bChangeX)
{
bPolarPoints = true;
}
}
}
else if(fTools::equal(fY, fOne))
{
// point is a south polar point, no useful X-coordinate can be created. Set
// Y-coordinte, though
if(bChangeY)
{
aTexCoor.setY(fOne);
if(bChangeX)
{
bPolarPoints = true;
}
}
}
else
{
double fX(fOne - ((atan2(aVector.getZ(), aVector.getX()) + F_PI) / F_2PI));
// correct cartesinan point coordiante dependent from center value
if(fX > fXCenter + 0.5)
{
fX -= fOne;
}
else if(fX < fXCenter - 0.5)
{
fX += fOne;
}
if(bChangeX)
{
aTexCoor.setX(fX);
}
if(bChangeY)
{
aTexCoor.setY(fY);
}
}
aRetval.setTextureCoordinate(a, aTexCoor);
}
if(bPolarPoints)
{
// correct X-texture coordinates if polar points are contained. Those
// coordinates cannot be correct, so use prev or next X-coordinate
for(a = 0L; a < nPointCount; a++)
{
B2DPoint aTexCoor(aRetval.getTextureCoordinate(a));
if(fTools::equalZero(aTexCoor.getY()) || fTools::equal(aTexCoor.getY(), fOne))
{
// get prev, next TexCoor and test for pole
const B2DPoint aPrevTexCoor(aRetval.getTextureCoordinate(a ? a - 1L : nPointCount - 1L));
const B2DPoint aNextTexCoor(aRetval.getTextureCoordinate((a + 1L) % nPointCount));
const bool bPrevPole(fTools::equalZero(aPrevTexCoor.getY()) || fTools::equal(aPrevTexCoor.getY(), fOne));
const bool bNextPole(fTools::equalZero(aNextTexCoor.getY()) || fTools::equal(aNextTexCoor.getY(), fOne));
if(!bPrevPole && !bNextPole)
{
// both no poles, mix them
aTexCoor.setX((aPrevTexCoor.getX() + aNextTexCoor.getX()) / 2.0);
}
else if(!bNextPole)
{
// copy next
aTexCoor.setX(aNextTexCoor.getX());
}
else
{
// copy prev, even if it's a pole, hopefully it is already corrected
aTexCoor.setX(aPrevTexCoor.getX());
}
aRetval.setTextureCoordinate(a, aTexCoor);
}
}
}
}
return aRetval;
}
bool isInEpsilonRange(const B3DPoint& rEdgeStart, const B3DPoint& rEdgeEnd, const B3DPoint& rTestPosition, double fDistance)
{
// build edge vector
const B3DVector aEdge(rEdgeEnd - rEdgeStart);
bool bDoDistanceTestStart(false);
bool bDoDistanceTestEnd(false);
if(aEdge.equalZero())
{
// no edge, just a point. Do one of the distance tests.
bDoDistanceTestStart = true;
}
else
{
// calculate fCut in aEdge
const B3DVector aTestEdge(rTestPosition - rEdgeStart);
const double fScalarTestEdge(aEdge.scalar(aTestEdge));
const double fScalarStartEdge(aEdge.scalar(rEdgeStart));
const double fScalarEdge(aEdge.scalar(aEdge));
const double fCut((fScalarTestEdge - fScalarStartEdge) / fScalarEdge);
const double fZero(0.0);
const double fOne(1.0);
if(fTools::less(fCut, fZero))
{
// left of rEdgeStart
bDoDistanceTestStart = true;
}
else if(fTools::more(fCut, fOne))
{
// right of rEdgeEnd
bDoDistanceTestEnd = true;
}
else
{
// inside line [0.0 .. 1.0]
const B3DPoint aCutPoint(interpolate(rEdgeStart, rEdgeEnd, fCut));
const B3DVector aDelta(rTestPosition - aCutPoint);
const double fDistanceSquare(aDelta.scalar(aDelta));
if(fDistanceSquare <= fDistance * fDistance * fDistance)
{
return true;
}
else
{
return false;
}
}
}
if(bDoDistanceTestStart)
{
const B3DVector aDelta(rTestPosition - rEdgeStart);
const double fDistanceSquare(aDelta.scalar(aDelta));
if(fDistanceSquare <= fDistance * fDistance * fDistance)
{
return true;
}
}
else if(bDoDistanceTestEnd)
{
const B3DVector aDelta(rTestPosition - rEdgeEnd);
const double fDistanceSquare(aDelta.scalar(aDelta));
if(fDistanceSquare <= fDistance * fDistance * fDistance)
{
return true;
}
}
return false;
}
bool isInEpsilonRange(const B3DPolygon& rCandidate, const B3DPoint& rTestPosition, double fDistance)
{
const sal_uInt32 nPointCount(rCandidate.count());
if(nPointCount)
{
const sal_uInt32 nEdgeCount(rCandidate.isClosed() ? nPointCount : nPointCount - 1L);
B3DPoint aCurrent(rCandidate.getB3DPoint(0));
if(nEdgeCount)
{
// edges
for(sal_uInt32 a(0); a < nEdgeCount; a++)
{
const sal_uInt32 nNextIndex((a + 1) % nPointCount);
const B3DPoint aNext(rCandidate.getB3DPoint(nNextIndex));
if(isInEpsilonRange(aCurrent, aNext, rTestPosition, fDistance))
{
return true;
}
// prepare next step
aCurrent = aNext;
}
}
else
{
// no edges, but points -> not closed. Check single point. Just
// use isInEpsilonRange with twice the same point, it handles those well
if(isInEpsilonRange(aCurrent, aCurrent, rTestPosition, fDistance))
{
return true;
}
}
}
return false;
}
bool isInside(const B3DPolygon& rCandidate, const B3DPoint& rPoint, bool bWithBorder)
{
if(bWithBorder && isPointOnPolygon(rCandidate, rPoint, true))
{
return true;
}
else
{
bool bRetval(false);
const B3DVector aPlaneNormal(rCandidate.getNormal());
if(!aPlaneNormal.equalZero())
{
const sal_uInt32 nPointCount(rCandidate.count());
if(nPointCount)
{
B3DPoint aCurrentPoint(rCandidate.getB3DPoint(nPointCount - 1));
const double fAbsX(fabs(aPlaneNormal.getX()));
const double fAbsY(fabs(aPlaneNormal.getY()));
const double fAbsZ(fabs(aPlaneNormal.getZ()));
if(fAbsX > fAbsY && fAbsX > fAbsZ)
{
// normal points mostly in X-Direction, use YZ-Polygon projection for check
// x -> y, y -> z
for(sal_uInt32 a(0); a < nPointCount; a++)
{
const B3DPoint aPreviousPoint(aCurrentPoint);
aCurrentPoint = rCandidate.getB3DPoint(a);
// cross-over in Z?
const bool bCompZA(fTools::more(aPreviousPoint.getZ(), rPoint.getZ()));
const bool bCompZB(fTools::more(aCurrentPoint.getZ(), rPoint.getZ()));
if(bCompZA != bCompZB)
{
// cross-over in Y?
const bool bCompYA(fTools::more(aPreviousPoint.getY(), rPoint.getY()));
const bool bCompYB(fTools::more(aCurrentPoint.getY(), rPoint.getY()));
if(bCompYA == bCompYB)
{
if(bCompYA)
{
bRetval = !bRetval;
}
}
else
{
const double fCompare(
aCurrentPoint.getY() - (aCurrentPoint.getZ() - rPoint.getZ()) *
(aPreviousPoint.getY() - aCurrentPoint.getY()) /
(aPreviousPoint.getZ() - aCurrentPoint.getZ()));
if(fTools::more(fCompare, rPoint.getY()))
{
bRetval = !bRetval;
}
}
}
}
}
else if(fAbsY > fAbsX && fAbsY > fAbsZ)
{
// normal points mostly in Y-Direction, use XZ-Polygon projection for check
// x -> x, y -> z
for(sal_uInt32 a(0); a < nPointCount; a++)
{
const B3DPoint aPreviousPoint(aCurrentPoint);
aCurrentPoint = rCandidate.getB3DPoint(a);
// cross-over in Z?
const bool bCompZA(fTools::more(aPreviousPoint.getZ(), rPoint.getZ()));
const bool bCompZB(fTools::more(aCurrentPoint.getZ(), rPoint.getZ()));
if(bCompZA != bCompZB)
{
// cross-over in X?
const bool bCompXA(fTools::more(aPreviousPoint.getX(), rPoint.getX()));
const bool bCompXB(fTools::more(aCurrentPoint.getX(), rPoint.getX()));
if(bCompXA == bCompXB)
{
if(bCompXA)
{
bRetval = !bRetval;
}
}
else
{
const double fCompare(
aCurrentPoint.getX() - (aCurrentPoint.getZ() - rPoint.getZ()) *
(aPreviousPoint.getX() - aCurrentPoint.getX()) /
(aPreviousPoint.getZ() - aCurrentPoint.getZ()));
if(fTools::more(fCompare, rPoint.getX()))
{
bRetval = !bRetval;
}
}
}
}
}
else
{
// normal points mostly in Z-Direction, use XY-Polygon projection for check
// x -> x, y -> y
for(sal_uInt32 a(0); a < nPointCount; a++)
{
const B3DPoint aPreviousPoint(aCurrentPoint);
aCurrentPoint = rCandidate.getB3DPoint(a);
// cross-over in Y?
const bool bCompYA(fTools::more(aPreviousPoint.getY(), rPoint.getY()));
const bool bCompYB(fTools::more(aCurrentPoint.getY(), rPoint.getY()));
if(bCompYA != bCompYB)
{
// cross-over in X?
const bool bCompXA(fTools::more(aPreviousPoint.getX(), rPoint.getX()));
const bool bCompXB(fTools::more(aCurrentPoint.getX(), rPoint.getX()));
if(bCompXA == bCompXB)
{
if(bCompXA)
{
bRetval = !bRetval;
}
}
else
{
const double fCompare(
aCurrentPoint.getX() - (aCurrentPoint.getY() - rPoint.getY()) *
(aPreviousPoint.getX() - aCurrentPoint.getX()) /
(aPreviousPoint.getY() - aCurrentPoint.getY()));
if(fTools::more(fCompare, rPoint.getX()))
{
bRetval = !bRetval;
}
}
}
}
}
}
}
return bRetval;
}
}
bool isInside(const B3DPolygon& rCandidate, const B3DPolygon& rPolygon, bool bWithBorder)
{
const sal_uInt32 nPointCount(rPolygon.count());
for(sal_uInt32 a(0L); a < nPointCount; a++)
{
const B3DPoint aTestPoint(rPolygon.getB3DPoint(a));
if(!isInside(rCandidate, aTestPoint, bWithBorder))
{
return false;
}
}
return true;
}
bool isPointOnLine(const B3DPoint& rStart, const B3DPoint& rEnd, const B3DPoint& rCandidate, bool bWithPoints)
{
if(rCandidate.equal(rStart) || rCandidate.equal(rEnd))
{
// candidate is in epsilon around start or end -> inside
return bWithPoints;
}
else if(rStart.equal(rEnd))
{
// start and end are equal, but candidate is outside their epsilon -> outside
return false;
}
else
{
const B3DVector aEdgeVector(rEnd - rStart);
const B3DVector aTestVector(rCandidate - rStart);
if(areParallel(aEdgeVector, aTestVector))
{
const double fZero(0.0);
const double fOne(1.0);
double fParamTestOnCurr(0.0);
if(aEdgeVector.getX() > aEdgeVector.getY())
{
if(aEdgeVector.getX() > aEdgeVector.getZ())
{
// X is biggest
fParamTestOnCurr = aTestVector.getX() / aEdgeVector.getX();
}
else
{
// Z is biggest
fParamTestOnCurr = aTestVector.getZ() / aEdgeVector.getZ();
}
}
else
{
if(aEdgeVector.getY() > aEdgeVector.getZ())
{
// Y is biggest
fParamTestOnCurr = aTestVector.getY() / aEdgeVector.getY();
}
else
{
// Z is biggest
fParamTestOnCurr = aTestVector.getZ() / aEdgeVector.getZ();
}
}
if(fTools::more(fParamTestOnCurr, fZero) && fTools::less(fParamTestOnCurr, fOne))
{
return true;
}
}
return false;
}
}
bool isPointOnPolygon(const B3DPolygon& rCandidate, const B3DPoint& rPoint, bool bWithPoints)
{
const sal_uInt32 nPointCount(rCandidate.count());
if(nPointCount > 1L)
{
const sal_uInt32 nLoopCount(rCandidate.isClosed() ? nPointCount : nPointCount - 1L);
B3DPoint aCurrentPoint(rCandidate.getB3DPoint(0));
for(sal_uInt32 a(0); a < nLoopCount; a++)
{
const B3DPoint aNextPoint(rCandidate.getB3DPoint((a + 1) % nPointCount));
if(isPointOnLine(aCurrentPoint, aNextPoint, rPoint, bWithPoints))
{
return true;
}
aCurrentPoint = aNextPoint;
}
}
else if(nPointCount && bWithPoints)
{
return rPoint.equal(rCandidate.getB3DPoint(0));
}
return false;
}
bool getCutBetweenLineAndPlane(const B3DVector& rPlaneNormal, const B3DPoint& rPlanePoint, const B3DPoint& rEdgeStart, const B3DPoint& rEdgeEnd, double& fCut)
{
if(!rPlaneNormal.equalZero() && !rEdgeStart.equal(rEdgeEnd))
{
const B3DVector aTestEdge(rEdgeEnd - rEdgeStart);
const double fScalarEdge(rPlaneNormal.scalar(aTestEdge));
if(!fTools::equalZero(fScalarEdge))
{
const B3DVector aCompareEdge(rPlanePoint - rEdgeStart);
const double fScalarCompare(rPlaneNormal.scalar(aCompareEdge));
fCut = fScalarCompare / fScalarEdge;
return true;
}
}
return false;
}
bool getCutBetweenLineAndPolygon(const B3DPolygon& rCandidate, const B3DPoint& rEdgeStart, const B3DPoint& rEdgeEnd, double& fCut)
{
const sal_uInt32 nPointCount(rCandidate.count());
if(nPointCount > 2 && !rEdgeStart.equal(rEdgeEnd))
{
const B3DVector aPlaneNormal(rCandidate.getNormal());
if(!aPlaneNormal.equalZero())
{
const B3DPoint aPointOnPlane(rCandidate.getB3DPoint(0));
return getCutBetweenLineAndPlane(aPlaneNormal, aPointOnPlane, rEdgeStart, rEdgeEnd, fCut);
}
}
return false;
}
//////////////////////////////////////////////////////////////////////
// comparators with tolerance for 3D Polygons
bool equal(const B3DPolygon& rCandidateA, const B3DPolygon& rCandidateB, const double& rfSmallValue)
{
const sal_uInt32 nPointCount(rCandidateA.count());
if(nPointCount != rCandidateB.count())
return false;
const bool bClosed(rCandidateA.isClosed());
if(bClosed != rCandidateB.isClosed())
return false;
for(sal_uInt32 a(0); a < nPointCount; a++)
{
const B3DPoint aPoint(rCandidateA.getB3DPoint(a));
if(!aPoint.equal(rCandidateB.getB3DPoint(a), rfSmallValue))
return false;
}
return true;
}
bool equal(const B3DPolygon& rCandidateA, const B3DPolygon& rCandidateB)
{
const double fSmallValue(fTools::getSmallValue());
return equal(rCandidateA, rCandidateB, fSmallValue);
}
// snap points of horizontal or vertical edges to discrete values
B3DPolygon snapPointsOfHorizontalOrVerticalEdges(const B3DPolygon& rCandidate)
{
const sal_uInt32 nPointCount(rCandidate.count());
if(nPointCount > 1)
{
// Start by copying the source polygon to get a writeable copy. The closed state is
// copied by aRetval's initialisation, too, so no need to copy it in this method
B3DPolygon aRetval(rCandidate);
// prepare geometry data. Get rounded from original
B3ITuple aPrevTuple(basegfx::fround(rCandidate.getB3DPoint(nPointCount - 1)));
B3DPoint aCurrPoint(rCandidate.getB3DPoint(0));
B3ITuple aCurrTuple(basegfx::fround(aCurrPoint));
// loop over all points. This will also snap the implicit closing edge
// even when not closed, but that's no problem here
for(sal_uInt32 a(0); a < nPointCount; a++)
{
// get next point. Get rounded from original
const bool bLastRun(a + 1 == nPointCount);
const sal_uInt32 nNextIndex(bLastRun ? 0 : a + 1);
const B3DPoint aNextPoint(rCandidate.getB3DPoint(nNextIndex));
const B3ITuple aNextTuple(basegfx::fround(aNextPoint));
// get the states
const bool bPrevVertical(aPrevTuple.getX() == aCurrTuple.getX());
const bool bNextVertical(aNextTuple.getX() == aCurrTuple.getX());
const bool bPrevHorizontal(aPrevTuple.getY() == aCurrTuple.getY());
const bool bNextHorizontal(aNextTuple.getY() == aCurrTuple.getY());
const bool bSnapX(bPrevVertical || bNextVertical);
const bool bSnapY(bPrevHorizontal || bNextHorizontal);
if(bSnapX || bSnapY)
{
const B3DPoint aSnappedPoint(
bSnapX ? aCurrTuple.getX() : aCurrPoint.getX(),
bSnapY ? aCurrTuple.getY() : aCurrPoint.getY(),
aCurrPoint.getZ());
aRetval.setB3DPoint(a, aSnappedPoint);
}
// prepare next point
if(!bLastRun)
{
aPrevTuple = aCurrTuple;
aCurrPoint = aNextPoint;
aCurrTuple = aNextTuple;
}
}
return aRetval;
}
else
{
return rCandidate;
}
}
} // end of namespace tools
} // end of namespace basegfx
//////////////////////////////////////////////////////////////////////////////
// eof
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|