File size: 33,361 Bytes
985c397 | 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 | // SPDX-License-Identifier: LGPL-2.1-or-later
/***************************************************************************
* Copyright (c) 2014 Victor Titov (DeepSOIC) <vv.titov@gmail.com> *
* *
* This file is part of the FreeCAD CAx development system. *
* *
* This library is free software; you can redistribute it and/or *
* modify it under the terms of the GNU Library General Public *
* License as published by the Free Software Foundation; either *
* version 2 of the License, or (at your option) any later version. *
* *
* This library 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 Library General Public License for more details. *
* *
* You should have received a copy of the GNU Library General Public *
* License along with this library; see the file COPYING.LIB. If not, *
* write to the Free Software Foundation, Inc., 59 Temple Place, *
* Suite 330, Boston, MA 02111-1307, USA *
* *
***************************************************************************/
#define DEBUG_DERIVS 0
#if DEBUG_DERIVS
#endif
#include <cassert>
#include "Geo.h"
namespace GCS
{
//----------------Point
int Point::PushOwnParams(VEC_pD& pvec) const
{
int cnt = 0;
pvec.push_back(x);
cnt++;
pvec.push_back(y);
cnt++;
return cnt;
}
void Point::ReconstructOnNewPvec(VEC_pD& pvec, int& cnt)
{
x = pvec[cnt];
cnt++;
y = pvec[cnt];
cnt++;
}
//----------------DeriVector2
DeriVector2::DeriVector2(const Point& p, const double* derivparam)
: x(*p.x)
, dx(0.0)
, y(*p.y)
, dy(0.0)
{
if (derivparam == p.x) {
dx = 1.0;
}
if (derivparam == p.y) {
dy = 1.0;
}
}
double DeriVector2::length(double& dlength) const
{
double l = length();
if (l == 0) {
dlength = 1.0;
return l;
}
dlength = (x * dx + y * dy) / l;
return l;
}
DeriVector2 DeriVector2::getNormalized() const
{
double l = length();
if (l == 0.0) {
return DeriVector2(0, 0, dx, dy);
}
DeriVector2 rtn;
rtn.x = x / l;
rtn.y = y / l;
// first, simply scale the derivative accordingly.
rtn.dx = dx / l;
rtn.dy = dy / l;
// next, remove the collinear part of dx,dy (make a projection onto a normal)
double dsc = rtn.dx * rtn.x + rtn.dy * rtn.y; // scalar product d*v
rtn.dx -= dsc * rtn.x; // subtract the projection
rtn.dy -= dsc * rtn.y;
return rtn;
}
double DeriVector2::scalarProd(const DeriVector2& v2, double* dprd) const
{
if (dprd) {
*dprd = dx * v2.x + x * v2.dx + dy * v2.y + y * v2.dy;
}
return x * v2.x + y * v2.y;
}
DeriVector2 DeriVector2::divD(double val, double dval) const
{
return {x / val, y / val, dx / val - x * dval / (val * val), dy / val - y * dval / (val * val)};
}
double DeriVector2::crossProdZ(const DeriVector2& v2, double& dprd) const
{
dprd = dx * v2.y + x * v2.dy - dy * v2.x - y * v2.dx;
return x * v2.y - y * v2.x;
}
DeriVector2 Curve::Value(double /*u*/, double /*du*/, const double* /*derivparam*/) const
{
assert(false /*Value() is not implemented*/);
return {};
}
//----------------Line
DeriVector2 Line::CalculateNormal(const Point& p, const double* derivparam) const
{
(void)p;
DeriVector2 p1v(p1, derivparam);
DeriVector2 p2v(p2, derivparam);
return p2v.subtr(p1v).rotate90ccw();
}
DeriVector2 Line::Value(double u, double du, const double* derivparam) const
{
DeriVector2 p1v(p1, derivparam);
DeriVector2 p2v(p2, derivparam);
DeriVector2 line_vec = p2v.subtr(p1v);
return p1v.sum(line_vec.multD(u, du));
}
int Line::PushOwnParams(VEC_pD& pvec)
{
int cnt = 0;
pvec.push_back(p1.x);
cnt++;
pvec.push_back(p1.y);
cnt++;
pvec.push_back(p2.x);
cnt++;
pvec.push_back(p2.y);
cnt++;
return cnt;
}
void Line::ReconstructOnNewPvec(VEC_pD& pvec, int& cnt)
{
p1.x = pvec[cnt];
cnt++;
p1.y = pvec[cnt];
cnt++;
p2.x = pvec[cnt];
cnt++;
p2.y = pvec[cnt];
cnt++;
}
Line* Line::Copy()
{
return new Line(*this);
}
//---------------circle
DeriVector2 Circle::CalculateNormal(const Point& p, const double* derivparam) const
{
DeriVector2 cv(center, derivparam);
DeriVector2 pv(p, derivparam);
return cv.subtr(pv);
}
DeriVector2 Circle::Value(double u, double du, const double* derivparam) const
{
DeriVector2 cv(center, derivparam);
double r, dr;
r = *(this->rad);
dr = (derivparam == this->rad) ? 1.0 : 0.0;
DeriVector2 ex(r, 0.0, dr, 0.0);
DeriVector2 ey = ex.rotate90ccw();
double si, dsi, co, dco;
si = std::sin(u);
dsi = du * std::cos(u);
co = std::cos(u);
dco = du * (-std::sin(u));
return cv.sum(ex.multD(co, dco).sum(ey.multD(si, dsi)));
}
int Circle::PushOwnParams(VEC_pD& pvec)
{
int cnt = 0;
pvec.push_back(center.x);
cnt++;
pvec.push_back(center.y);
cnt++;
pvec.push_back(rad);
cnt++;
return cnt;
}
void Circle::ReconstructOnNewPvec(VEC_pD& pvec, int& cnt)
{
center.x = pvec[cnt];
cnt++;
center.y = pvec[cnt];
cnt++;
rad = pvec[cnt];
cnt++;
}
Circle* Circle::Copy()
{
return new Circle(*this);
}
//------------arc
int Arc::PushOwnParams(VEC_pD& pvec)
{
int cnt = 0;
cnt += Circle::PushOwnParams(pvec);
pvec.push_back(start.x);
cnt++;
pvec.push_back(start.y);
cnt++;
pvec.push_back(end.x);
cnt++;
pvec.push_back(end.y);
cnt++;
pvec.push_back(startAngle);
cnt++;
pvec.push_back(endAngle);
cnt++;
return cnt;
}
void Arc::ReconstructOnNewPvec(VEC_pD& pvec, int& cnt)
{
Circle::ReconstructOnNewPvec(pvec, cnt);
start.x = pvec[cnt];
cnt++;
start.y = pvec[cnt];
cnt++;
end.x = pvec[cnt];
cnt++;
end.y = pvec[cnt];
cnt++;
startAngle = pvec[cnt];
cnt++;
endAngle = pvec[cnt];
cnt++;
}
Arc* Arc::Copy()
{
return new Arc(*this);
}
//--------------ellipse
// this function is exposed to allow reusing pre-filled derivectors in constraints code
double Ellipse::getRadMaj(
const DeriVector2& center,
const DeriVector2& f1,
double b,
double db,
double& ret_dRadMaj
) const
{
double cf, dcf;
cf = f1.subtr(center).length(dcf);
DeriVector2 hack(
b,
cf,
db,
dcf
); // hack = a nonsense vector to calculate major radius with derivatives, useful just
// because the calculation formula is the same as vector length formula
return hack.length(ret_dRadMaj);
}
// returns major radius. The derivative by derivparam is returned into ret_dRadMaj argument.
double Ellipse::getRadMaj(double* derivparam, double& ret_dRadMaj) const
{
DeriVector2 c(center, derivparam);
DeriVector2 f1(focus1, derivparam);
return getRadMaj(c, f1, *radmin, radmin == derivparam ? 1.0 : 0.0, ret_dRadMaj);
}
// returns the major radius (plain value, no derivatives)
double Ellipse::getRadMaj() const
{
double dradmaj; // dummy
return getRadMaj(nullptr, dradmaj);
}
DeriVector2 Ellipse::CalculateNormal(const Point& p, const double* derivparam) const
{
// fill some vectors in
DeriVector2 cv(center, derivparam);
DeriVector2 f1v(focus1, derivparam);
DeriVector2 pv(p, derivparam);
// calculation.
// focus2:
DeriVector2 f2v = cv.linCombi(2.0, f1v, -1.0); // 2*cv - f1v
// pf1, pf2 = vectors from p to focus1,focus2
DeriVector2 pf1 = f1v.subtr(pv);
DeriVector2 pf2 = f2v.subtr(pv);
// return sum of normalized pf2, pf2
return pf1.getNormalized().sum(pf2.getNormalized());
}
DeriVector2 Ellipse::Value(double u, double du, const double* derivparam) const
{
// In local coordinate system, value() of ellipse is:
//(a*cos(u), b*sin(u))
// In global, it is (vector formula):
// center + a_vec*cos(u) + b_vec*sin(u).
// That's what is being computed here.
// <construct a_vec, b_vec>
DeriVector2 c(this->center, derivparam);
DeriVector2 f1(this->focus1, derivparam);
DeriVector2 emaj = f1.subtr(c).getNormalized();
DeriVector2 emin = emaj.rotate90ccw();
double b, db;
b = *(this->radmin);
db = this->radmin == derivparam ? 1.0 : 0.0;
double a, da;
a = this->getRadMaj(c, f1, b, db, da);
DeriVector2 a_vec = emaj.multD(a, da);
DeriVector2 b_vec = emin.multD(b, db);
// </construct a_vec, b_vec>
// sin, cos with derivatives:
double co, dco, si, dsi;
co = std::cos(u);
dco = -std::sin(u) * du;
si = std::sin(u);
dsi = std::cos(u) * du;
// point of ellipse at parameter value of u, in global coordinates
return a_vec.multD(co, dco).sum(b_vec.multD(si, dsi)).sum(c);
}
int Ellipse::PushOwnParams(VEC_pD& pvec)
{
int cnt = 0;
pvec.push_back(center.x);
cnt++;
pvec.push_back(center.y);
cnt++;
pvec.push_back(focus1.x);
cnt++;
pvec.push_back(focus1.y);
cnt++;
pvec.push_back(radmin);
cnt++;
return cnt;
}
void Ellipse::ReconstructOnNewPvec(VEC_pD& pvec, int& cnt)
{
center.x = pvec[cnt];
cnt++;
center.y = pvec[cnt];
cnt++;
focus1.x = pvec[cnt];
cnt++;
focus1.y = pvec[cnt];
cnt++;
radmin = pvec[cnt];
cnt++;
}
Ellipse* Ellipse::Copy()
{
return new Ellipse(*this);
}
//---------------arc of ellipse
int ArcOfEllipse::PushOwnParams(VEC_pD& pvec)
{
int cnt = 0;
cnt += Ellipse::PushOwnParams(pvec);
pvec.push_back(start.x);
cnt++;
pvec.push_back(start.y);
cnt++;
pvec.push_back(end.x);
cnt++;
pvec.push_back(end.y);
cnt++;
pvec.push_back(startAngle);
cnt++;
pvec.push_back(endAngle);
cnt++;
return cnt;
}
void ArcOfEllipse::ReconstructOnNewPvec(VEC_pD& pvec, int& cnt)
{
Ellipse::ReconstructOnNewPvec(pvec, cnt);
start.x = pvec[cnt];
cnt++;
start.y = pvec[cnt];
cnt++;
end.x = pvec[cnt];
cnt++;
end.y = pvec[cnt];
cnt++;
startAngle = pvec[cnt];
cnt++;
endAngle = pvec[cnt];
cnt++;
}
ArcOfEllipse* ArcOfEllipse::Copy()
{
return new ArcOfEllipse(*this);
}
//---------------hyperbola
// this function is exposed to allow reusing pre-filled derivectors in constraints code
double Hyperbola::getRadMaj(
const DeriVector2& center,
const DeriVector2& f1,
double b,
double db,
double& ret_dRadMaj
) const
{
double cf, dcf;
cf = f1.subtr(center).length(dcf);
double a, da;
a = sqrt(cf * cf - b * b);
da = (dcf * cf - db * b) / a;
ret_dRadMaj = da;
return a;
}
// returns major radius. The derivative by derivparam is returned into ret_dRadMaj argument.
double Hyperbola::getRadMaj(double* derivparam, double& ret_dRadMaj) const
{
DeriVector2 c(center, derivparam);
DeriVector2 f1(focus1, derivparam);
return getRadMaj(c, f1, *radmin, radmin == derivparam ? 1.0 : 0.0, ret_dRadMaj);
}
// returns the major radius (plain value, no derivatives)
double Hyperbola::getRadMaj() const
{
double dradmaj; // dummy
return getRadMaj(nullptr, dradmaj);
}
DeriVector2 Hyperbola::CalculateNormal(const Point& p, const double* derivparam) const
{
// fill some vectors in
DeriVector2 cv(center, derivparam);
DeriVector2 f1v(focus1, derivparam);
DeriVector2 pv(p, derivparam);
// calculation.
// focus2:
DeriVector2 f2v = cv.linCombi(2.0, f1v, -1.0); // 2*cv - f1v
// pf1, pf2 = vectors from p to focus1,focus2
DeriVector2 pf1 = f1v.subtr(pv).mult(-1.0); // <--- differs from ellipse normal calculation
// code by inverting this vector
DeriVector2 pf2 = f2v.subtr(pv);
// return sum of normalized pf2, pf2
return pf1.getNormalized().sum(pf2.getNormalized());
}
DeriVector2 Hyperbola::Value(double u, double du, const double* derivparam) const
{
// In local coordinate system, value() of hyperbola is:
//(a*cosh(u), b*sinh(u))
// In global, it is (vector formula):
// center + a_vec*cosh(u) + b_vec*sinh(u).
// That's what is being computed here.
// <construct a_vec, b_vec>
DeriVector2 c(this->center, derivparam);
DeriVector2 f1(this->focus1, derivparam);
DeriVector2 emaj = f1.subtr(c).getNormalized();
DeriVector2 emin = emaj.rotate90ccw();
double b, db;
b = *(this->radmin);
db = this->radmin == derivparam ? 1.0 : 0.0;
double a, da;
a = this->getRadMaj(c, f1, b, db, da);
DeriVector2 a_vec = emaj.multD(a, da);
DeriVector2 b_vec = emin.multD(b, db);
// </construct a_vec, b_vec>
// sinh, cosh with derivatives:
double co, dco, si, dsi;
co = std::cosh(u);
dco = std::sinh(u) * du;
si = std::sinh(u);
dsi = std::cosh(u) * du;
// point of hyperbola at parameter value of u, in global coordinates
return a_vec.multD(co, dco).sum(b_vec.multD(si, dsi)).sum(c);
}
int Hyperbola::PushOwnParams(VEC_pD& pvec)
{
int cnt = 0;
pvec.push_back(center.x);
cnt++;
pvec.push_back(center.y);
cnt++;
pvec.push_back(focus1.x);
cnt++;
pvec.push_back(focus1.y);
cnt++;
pvec.push_back(radmin);
cnt++;
return cnt;
}
void Hyperbola::ReconstructOnNewPvec(VEC_pD& pvec, int& cnt)
{
center.x = pvec[cnt];
cnt++;
center.y = pvec[cnt];
cnt++;
focus1.x = pvec[cnt];
cnt++;
focus1.y = pvec[cnt];
cnt++;
radmin = pvec[cnt];
cnt++;
}
Hyperbola* Hyperbola::Copy()
{
return new Hyperbola(*this);
}
//--------------- arc of hyperbola
int ArcOfHyperbola::PushOwnParams(VEC_pD& pvec)
{
int cnt = 0;
cnt += Hyperbola::PushOwnParams(pvec);
pvec.push_back(start.x);
cnt++;
pvec.push_back(start.y);
cnt++;
pvec.push_back(end.x);
cnt++;
pvec.push_back(end.y);
cnt++;
pvec.push_back(startAngle);
cnt++;
pvec.push_back(endAngle);
cnt++;
return cnt;
}
void ArcOfHyperbola::ReconstructOnNewPvec(VEC_pD& pvec, int& cnt)
{
Hyperbola::ReconstructOnNewPvec(pvec, cnt);
start.x = pvec[cnt];
cnt++;
start.y = pvec[cnt];
cnt++;
end.x = pvec[cnt];
cnt++;
end.y = pvec[cnt];
cnt++;
startAngle = pvec[cnt];
cnt++;
endAngle = pvec[cnt];
cnt++;
}
ArcOfHyperbola* ArcOfHyperbola::Copy()
{
return new ArcOfHyperbola(*this);
}
//---------------parabola
DeriVector2 Parabola::CalculateNormal(const Point& p, const double* derivparam) const
{
// fill some vectors in
DeriVector2 cv(vertex, derivparam);
DeriVector2 f1v(focus1, derivparam);
DeriVector2 pv(p, derivparam);
// the normal is the vector from the focus to the intersection of ano thru the point p and
// direction of the symmetry axis of the parabola with the directrix. As both point to directrix
// and point to focus are of equal magnitude, we can work with unitary vectors to calculate the
// normal, substraction of those vectors.
return cv.subtr(f1v).getNormalized().subtr(f1v.subtr(pv).getNormalized());
}
DeriVector2 Parabola::Value(double u, double du, const double* derivparam) const
{
// In local coordinate system, value() of parabola is:
// P(U) = O + U*U/(4.*F)*XDir + U*YDir
DeriVector2 c(this->vertex, derivparam);
DeriVector2 f1(this->focus1, derivparam);
DeriVector2 fv = f1.subtr(c);
double f, df;
f = fv.length(df);
DeriVector2 xdir = fv.getNormalized();
DeriVector2 ydir = xdir.rotate90ccw();
DeriVector2 dirx = xdir.multD(u, du).multD(u, du).divD(4 * f, 4 * df);
DeriVector2 diry = ydir.multD(u, du);
DeriVector2 dir = dirx.sum(diry);
DeriVector2 ret; // point of parabola at parameter value of u, in global coordinates
ret = c.sum(dir);
return ret;
}
int Parabola::PushOwnParams(VEC_pD& pvec)
{
int cnt = 0;
pvec.push_back(vertex.x);
cnt++;
pvec.push_back(vertex.y);
cnt++;
pvec.push_back(focus1.x);
cnt++;
pvec.push_back(focus1.y);
cnt++;
return cnt;
}
void Parabola::ReconstructOnNewPvec(VEC_pD& pvec, int& cnt)
{
vertex.x = pvec[cnt];
cnt++;
vertex.y = pvec[cnt];
cnt++;
focus1.x = pvec[cnt];
cnt++;
focus1.y = pvec[cnt];
cnt++;
}
Parabola* Parabola::Copy()
{
return new Parabola(*this);
}
//--------------- arc of hyperbola
int ArcOfParabola::PushOwnParams(VEC_pD& pvec)
{
int cnt = 0;
cnt += Parabola::PushOwnParams(pvec);
pvec.push_back(start.x);
cnt++;
pvec.push_back(start.y);
cnt++;
pvec.push_back(end.x);
cnt++;
pvec.push_back(end.y);
cnt++;
pvec.push_back(startAngle);
cnt++;
pvec.push_back(endAngle);
cnt++;
return cnt;
}
void ArcOfParabola::ReconstructOnNewPvec(VEC_pD& pvec, int& cnt)
{
Parabola::ReconstructOnNewPvec(pvec, cnt);
start.x = pvec[cnt];
cnt++;
start.y = pvec[cnt];
cnt++;
end.x = pvec[cnt];
cnt++;
end.y = pvec[cnt];
cnt++;
startAngle = pvec[cnt];
cnt++;
endAngle = pvec[cnt];
cnt++;
}
ArcOfParabola* ArcOfParabola::Copy()
{
return new ArcOfParabola(*this);
}
// bspline
DeriVector2 BSpline::CalculateNormal(const Point& p, const double* derivparam) const
{
// even if this method is call CalculateNormal, the returned vector is not the normal strictu
// sensus but a normal vector, where the vector should point to the left when one walks along
// the curve from start to end.
//
// https://forum.freecad.org/viewtopic.php?f=10&t=26312#p209486
if (mult[0] > degree && mult[mult.size() - 1] > degree) {
// if endpoints through end poles
if (*p.x == *start.x && *p.y == *start.y) {
// and you are asking about the normal at start point
// then tangency is defined by first to second poles
DeriVector2 endpt(this->poles[1], derivparam);
DeriVector2 spt(this->poles[0], derivparam);
DeriVector2 tg = endpt.subtr(spt);
return tg.rotate90ccw();
}
if (*p.x == *end.x && *p.y == *end.y) {
// and you are asking about the normal at end point
// then tangency is defined by last to last but one poles
DeriVector2 endpt(this->poles[poles.size() - 1], derivparam);
DeriVector2 spt(this->poles[poles.size() - 2], derivparam);
DeriVector2 tg = endpt.subtr(spt);
return tg.rotate90ccw();
}
// another point and we have no clue until we implement De Boor
return {};
}
// either periodic or abnormal endpoint multiplicity, we have no clue so currently
// unsupported
return {};
}
DeriVector2 BSpline::CalculateNormal(const double* param, const double* derivparam) const
{
// TODO: is there any advantage in making this a `static`?
size_t startpole = 0;
for (size_t j = 1; j < mult.size() && *(knots[j]) <= *param; ++j) {
startpole += mult[j];
}
if (!periodic && startpole >= poles.size()) {
startpole = poles.size() - degree - 1;
}
auto polexat = [&](size_t i) {
return poles[(startpole + i) % poles.size()].x;
};
auto poleyat = [&](size_t i) {
return poles[(startpole + i) % poles.size()].y;
};
auto weightat = [&](size_t i) {
return weights[(startpole + i) % weights.size()];
};
double xsum, xslopesum;
double ysum, yslopesum;
double wsum, wslopesum;
valueHomogenous(*param, &xsum, &ysum, &wsum, &xslopesum, &yslopesum, &wslopesum);
// Tangent vector
// This should in principle be identical to error gradient wrt curve parameter in
// point-on-object
DeriVector2 result(wsum * xslopesum - wslopesum * xsum, wsum * yslopesum - wslopesum * ysum);
size_t numpoints = degree + 1;
// get dx, dy of the normal as well
for (size_t i = 0; i < numpoints; ++i) {
if (derivparam != polexat(i) && derivparam != poleyat(i) && derivparam != weightat(i)) {
continue;
}
VEC_D d(numpoints);
d[i] = 1;
double factor = splineValue(*param, startpole + degree, degree, d, flattenedknots);
VEC_D sd(numpoints - 1);
if (i > 0) {
sd[i - 1] = 1.0
/ (flattenedknots[startpole + i + degree] - flattenedknots[startpole + i]);
}
if (i < numpoints - 1) {
sd[i] = -1.0
/ (flattenedknots[startpole + i + 1 + degree] - flattenedknots[startpole + i + 1]);
}
double slopefactor = splineValue(*param, startpole + degree, degree - 1, sd, flattenedknots);
if (derivparam == polexat(i)) {
result.dx = *weightat(i) * (wsum * slopefactor - wslopesum * factor);
}
else if (derivparam == poleyat(i)) {
result.dy = *weightat(i) * (wsum * slopefactor - wslopesum * factor);
}
else if (derivparam == weightat(i)) {
result.dx = degree
* (factor * (xslopesum - wslopesum * (*polexat(i)))
- slopefactor * (xsum - wsum * (*polexat(i))));
result.dy = degree
* (factor * (yslopesum - wslopesum * (*poleyat(i)))
- slopefactor * (ysum - wsum * (*poleyat(i))));
}
break;
}
// the curve parameter being used by the constraint is not known to the geometry (there can be
// many tangent constraints on the same curve after all). Assume that this is the param
// provided.
if (derivparam != param) {
return result.rotate90ccw();
}
// derivparam == param now. Done this way just to reduce "cognitive complexity".
VEC_D sd(numpoints - 1), ssd(numpoints - 2);
for (size_t i = 1; i < numpoints; ++i) {
sd[i - 1] = (*weightat(i) - *weightat(i - 1))
/ (flattenedknots[startpole + i + degree] - flattenedknots[startpole + i]);
}
for (size_t i = 1; i < numpoints - 1; ++i) {
ssd[i - 1] = (sd[i] - sd[i - 1])
/ (flattenedknots[startpole + i + degree] - flattenedknots[startpole + i]);
}
double wslopeslopesum = degree * (degree - 1)
* BSpline::splineValue(*param, startpole + degree, degree - 2, ssd, flattenedknots);
for (size_t i = 1; i < numpoints; ++i) {
sd[i - 1] = (*polexat(i) * *weightat(i) - *polexat(i - 1) * *weightat(i - 1))
/ (flattenedknots[startpole + i + degree] - flattenedknots[startpole + i]);
}
for (size_t i = 1; i < numpoints - 1; ++i) {
ssd[i - 1] = (sd[i] - sd[i - 1])
/ (flattenedknots[startpole + i + degree] - flattenedknots[startpole + i]);
}
double xslopeslopesum = degree * (degree - 1)
* BSpline::splineValue(*param, startpole + degree, degree - 2, ssd, flattenedknots);
for (size_t i = 1; i < numpoints; ++i) {
sd[i - 1] = (*poleyat(i) * *weightat(i) - *poleyat(i - 1) * *weightat(i - 1))
/ (flattenedknots[startpole + i + degree] - flattenedknots[startpole + i]);
}
for (size_t i = 1; i < numpoints - 1; ++i) {
ssd[i - 1] = (sd[i] - sd[i - 1])
/ (flattenedknots[startpole + i + degree] - flattenedknots[startpole + i]);
}
double yslopeslopesum = degree * (degree - 1)
* BSpline::splineValue(*param, startpole + degree, degree - 2, ssd, flattenedknots);
result.dx = wsum * xslopeslopesum - wslopeslopesum * xsum;
result.dy = wsum * yslopeslopesum - wslopeslopesum * ysum;
return result.rotate90ccw();
}
DeriVector2 BSpline::Value(double u, double /*du*/, const double* /*derivparam*/) const
{
// TODO: is there any advantage in making this a `static`?
size_t startpole = 0;
for (size_t j = 1; j < mult.size() && *(knots[j]) <= u; ++j) {
startpole += mult[j];
}
if (!periodic && startpole >= poles.size()) {
startpole = poles.size() - degree - 1;
}
// double xsum = 0., xslopesum = 0.;
// double ysum = 0., yslopesum = 0.;
// double wsum = 0., wslopesum = 0.;
auto polexat = [&](size_t i) {
return poles[(startpole + i) % poles.size()].x;
};
auto poleyat = [&](size_t i) {
return poles[(startpole + i) % poles.size()].y;
};
auto weightat = [&](size_t i) {
return weights[(startpole + i) % weights.size()];
};
size_t numpoints = degree + 1;
// Tangent vector
// This should in principle be identical to error gradient wrt curve parameter in
// point-on-object
VEC_D d(numpoints);
for (size_t i = 0; i < numpoints; ++i) {
d[i] = *polexat(i) * *weightat(i);
}
double xsum = splineValue(u, startpole + degree, degree, d, flattenedknots);
for (size_t i = 0; i < numpoints; ++i) {
d[i] = *poleyat(i) * *weightat(i);
}
double ysum = splineValue(u, startpole + degree, degree, d, flattenedknots);
for (size_t i = 0; i < numpoints; ++i) {
d[i] = *weightat(i);
}
double wsum = splineValue(u, startpole + degree, degree, d, flattenedknots);
d.resize(numpoints - 1);
for (size_t i = 1; i < numpoints; ++i) {
d[i - 1] = (*polexat(i) * *weightat(i) - *polexat(i - 1) * *weightat(i - 1))
/ (flattenedknots[startpole + i + degree] - flattenedknots[startpole + i]);
}
double xslopesum = degree * splineValue(u, startpole + degree, degree - 1, d, flattenedknots);
for (size_t i = 1; i < numpoints; ++i) {
d[i - 1] = (*poleyat(i) * *weightat(i) - *poleyat(i - 1) * *weightat(i - 1))
/ (flattenedknots[startpole + i + degree] - flattenedknots[startpole + i]);
}
double yslopesum = degree * splineValue(u, startpole + degree, degree - 1, d, flattenedknots);
for (size_t i = 1; i < numpoints; ++i) {
d[i - 1] = (*weightat(i) - *weightat(i - 1))
/ (flattenedknots[startpole + i + degree] - flattenedknots[startpole + i]);
}
double wslopesum = degree * splineValue(u, startpole + degree, degree - 1, d, flattenedknots);
return {
xsum / wsum,
ysum / wsum,
(wsum * xslopesum - wslopesum * xsum) / wsum / wsum,
(wsum * yslopesum - wslopesum * ysum) / wsum / wsum
};
}
void BSpline::valueHomogenous(
const double u,
double* xw,
double* yw,
double* w,
double* dxwdu,
double* dywdu,
double* dwdu
) const
{
// TODO: is there any advantage in making this a `static`?
size_t startpole = 0;
for (size_t j = 1; j < mult.size() && *(knots[j]) <= u; ++j) {
startpole += mult[j];
}
if (!periodic && startpole >= poles.size()) {
startpole = poles.size() - degree - 1;
}
auto polexat = [&](size_t i) {
return poles[(startpole + i) % poles.size()].x;
};
auto poleyat = [&](size_t i) {
return poles[(startpole + i) % poles.size()].y;
};
auto weightat = [&](size_t i) {
return weights[(startpole + i) % weights.size()];
};
size_t numpoints = degree + 1;
VEC_D d(numpoints);
for (size_t i = 0; i < numpoints; ++i) {
d[i] = *polexat(i) * *weightat(i);
}
*xw = BSpline::splineValue(u, startpole + degree, degree, d, flattenedknots);
for (size_t i = 0; i < numpoints; ++i) {
d[i] = *poleyat(i) * *weightat(i);
}
*yw = BSpline::splineValue(u, startpole + degree, degree, d, flattenedknots);
for (size_t i = 0; i < numpoints; ++i) {
d[i] = *weightat(i);
}
*w = BSpline::splineValue(u, startpole + degree, degree, d, flattenedknots);
d.resize(numpoints - 1);
for (size_t i = 1; i < numpoints; ++i) {
d[i - 1] = (*polexat(i) * *weightat(i) - *polexat(i - 1) * *weightat(i - 1))
/ (flattenedknots[startpole + i + degree] - flattenedknots[startpole + i]);
}
*dxwdu = degree * BSpline::splineValue(u, startpole + degree, degree - 1, d, flattenedknots);
for (size_t i = 1; i < numpoints; ++i) {
d[i - 1] = (*poleyat(i) * *weightat(i) - *poleyat(i - 1) * *weightat(i - 1))
/ (flattenedknots[startpole + i + degree] - flattenedknots[startpole + i]);
}
*dywdu = degree * BSpline::splineValue(u, startpole + degree, degree - 1, d, flattenedknots);
for (size_t i = 1; i < numpoints; ++i) {
d[i - 1] = (*weightat(i) - *weightat(i - 1))
/ (flattenedknots[startpole + i + degree] - flattenedknots[startpole + i]);
}
*dwdu = degree * BSpline::splineValue(u, startpole + degree, degree - 1, d, flattenedknots);
}
int BSpline::PushOwnParams(VEC_pD& pvec)
{
std::size_t cnt = 0;
for (const auto& pole : poles) {
pvec.push_back(pole.x);
pvec.push_back(pole.y);
}
cnt = cnt + poles.size() * 2;
pvec.insert(pvec.end(), weights.begin(), weights.end());
cnt = cnt + weights.size();
pvec.insert(pvec.end(), knots.begin(), knots.end());
cnt = cnt + knots.size();
pvec.push_back(start.x);
cnt++;
pvec.push_back(start.y);
cnt++;
pvec.push_back(end.x);
cnt++;
pvec.push_back(end.y);
cnt++;
return static_cast<int>(cnt);
}
void BSpline::ReconstructOnNewPvec(VEC_pD& pvec, int& cnt)
{
for (auto& pole : poles) {
pole.x = pvec[cnt];
cnt++;
pole.y = pvec[cnt];
cnt++;
}
for (auto& weight : weights) {
weight = pvec[cnt];
cnt++;
}
for (auto& knot : knots) {
knot = pvec[cnt];
cnt++;
}
start.x = pvec[cnt];
cnt++;
start.y = pvec[cnt];
cnt++;
end.x = pvec[cnt];
cnt++;
end.y = pvec[cnt];
cnt++;
}
BSpline* BSpline::Copy()
{
return new BSpline(*this);
}
double BSpline::getLinCombFactor(double x, size_t k, size_t i, unsigned int p)
{
// Adapted to C++ from the python implementation in the Wikipedia page for de Boor algorithm
// https://en.wikipedia.org/wiki/De_Boor%27s_algorithm.
// FIXME: This should probably be guaranteed by now, and done somewhere else
// To elaborate: `flattenedknots` should be set up as soon as `knots`
// and `mult` have been defined after creating the B-spline.
// However, in the future the values of `knots` could go into the solver
// as well, when alternatives may be needed to keep `flattenedknots` updated.
// Slightly more detailed discussion here:
// https://github.com/FreeCAD/FreeCAD/pull/7484#discussion_r1020858392
if (flattenedknots.empty()) {
setupFlattenedKnots();
}
std::vector d(p + 1, 0.0);
// Ensure this is within range
int idxOfPole = static_cast<int>(i) + p - static_cast<int>(k);
if (idxOfPole < 0 || idxOfPole > static_cast<int>(p)) {
return 0.0;
}
d[idxOfPole] = 1.0;
for (size_t r = 1; r < p + 1; ++r) {
for (size_t j = p; j > r - 1; --j) {
double alpha = (x - flattenedknots[j + k - p])
/ (flattenedknots[j + 1 + k - r] - flattenedknots[j + k - p]);
d[j] = (1.0 - alpha) * d[j - 1] + alpha * d[j];
}
}
return d[p];
}
double BSpline::splineValue(double x, size_t k, unsigned int p, VEC_D& d, const VEC_D& flatknots)
{
for (size_t r = 1; r < p + 1; ++r) {
for (size_t j = p; j > r - 1; --j) {
double alpha = (x - flatknots[j + k - p])
/ (flatknots[j + 1 + k - r] - flatknots[j + k - p]);
d[j] = (1.0 - alpha) * d[j - 1] + alpha * d[j];
}
}
return p < d.size() ? d[p] : 0.0;
}
void BSpline::setupFlattenedKnots()
{
flattenedknots.clear();
for (size_t i = 0; i < knots.size(); ++i) {
flattenedknots.insert(flattenedknots.end(), mult[i], *knots[i]);
}
// Adjust for periodic: see OCC documentation for explanation
if (periodic) {
double period = *knots.back() - *knots.front();
int c = degree + 1 - mult[0]; // number of knots to pad
// Add capacity so that iterators remain valid
flattenedknots.reserve(flattenedknots.size() + 2 * c);
// get iterators first for convenience
auto frontStart = flattenedknots.end() - mult.back() - c;
auto frontEnd = flattenedknots.end() - mult.back();
auto backStart = flattenedknots.begin() + mult.front();
auto backEnd = flattenedknots.begin() + mult.front() + c;
// creating new vectors because above iterators can become invalidated upon insert
std::vector<double> frontNew(frontStart, frontEnd);
std::vector<double> backNew(backStart, backEnd);
flattenedknots.insert(flattenedknots.end(), backNew.begin(), backNew.end());
flattenedknots.insert(flattenedknots.begin(), frontNew.begin(), frontNew.end());
for (int i = 0; i < c; ++i) {
*(flattenedknots.begin() + i) -= period;
*(flattenedknots.end() - 1 - i) += period;
}
}
}
} // namespace GCS
|