开源 企业版 高校版 私有云 模力方舟 AI 队友
代码拉取完成,页面将自动刷新
加入 Gitee
与超过 1400万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
已有帐号? 立即登录
文件
master
分支 (1)
master
master
分支 (1)
master
克隆/下载
克隆/下载
提示
下载代码请复制以下命令到终端执行
为确保你提交的代码身份被 Gitee 正确识别,请执行以下命令完成配置
初次使用 SSH 协议进行代码克隆、推送等操作时,需按下述提示完成 SSH 配置
1 生成 RSA 密钥
2 获取 RSA 公钥内容,并配置到 SSH公钥
在 Gitee 上使用 SVN,请访问 使用指南
使用 HTTPS 协议时,命令行会出现如下账号密码验证步骤。基于安全考虑,Gitee 建议 配置并使用私人令牌 替代登录密码进行克隆、推送等操作
Username for 'https://gitee.com': userName
Password for 'https://userName@gitee.com': # 私人令牌
master
分支 (1)
master
bx
/
src
/
math.cpp
bx
/
src
/
math.cpp
math.cpp 27.70 KB
一键复制 编辑 原始数据 按行查看 历史
Branimir Karadžić 提交于 2018年04月24日 07:22 +08:00 . Fixed atan2.
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
/*
* Copyright 2011-2018 Branimir Karadzic. All rights reserved.
* License: https://github.com/bkaradzic/bx#license-bsd-2-clause
*/
#include "bx_p.h"
#include <bx/math.h>
#include <bx/uint32_t.h>
namespace bx
{
const float kPi = 3.1415926535897932384626433832795f;
const float kPi2 = 6.2831853071795864769252867665590f;
const float kInvPi = 1.0f/kPi;
const float kPiHalf = 1.5707963267948966192313216916398f;
const float kPiQuarter = 0.7853981633974483096156608458199f;
const float kSqrt2 = 1.4142135623730950488016887242097f;
const float kLogNat10 = 2.3025850929940456840179914546844f;
const float kInvLogNat2 = 1.4426950408889634073599246810019f;
const float kLogNat2Hi = 0.6931471805599453094172321214582f;
const float kLogNat2Lo = 1.90821492927058770002e-10f;
const float kE = 2.7182818284590452353602874713527f;
const float kNearZero = 1.0f/float(1 << 28);
const float kFloatMin = 1.175494e-38f;
const float kFloatMax = 3.402823e+38f;
const float kInfinity = bitsToFloat(UINT32_C(0x7f800000) );
namespace
{
static const float kSinC2 = -0.16666667163372039794921875f;
static const float kSinC4 = 8.333347737789154052734375e-3f;
static const float kSinC6 = -1.9842604524455964565277099609375e-4f;
static const float kSinC8 = 2.760012648650445044040679931640625e-6f;
static const float kSinC10 = -2.50293279435709337121807038784027099609375e-8f;
static const float kCosC2 = -0.5f;
static const float kCosC4 = 4.166664183139801025390625e-2f;
static const float kCosC6 = -1.388833043165504932403564453125e-3f;
static const float kCosC8 = 2.47562347794882953166961669921875e-5f;
static const float kCosC10 = -2.59630184018533327616751194000244140625e-7f;
} // namespace
BX_CONST_FUNC float cos(float _a)
{
const float scaled = _a * 2.0f*kInvPi;
const float real = floor(scaled);
const float xx = _a - real * kPiHalf;
const int32_t bits = int32_t(real) & 3;
float c0, c2, c4, c6, c8, c10;
if (bits == 0
|| bits == 2)
{
c0 = 1.0f;
c2 = kCosC2;
c4 = kCosC4;
c6 = kCosC6;
c8 = kCosC8;
c10 = kCosC10;
}
else
{
c0 = xx;
c2 = kSinC2;
c4 = kSinC4;
c6 = kSinC6;
c8 = kSinC8;
c10 = kSinC10;
}
const float xsq = square(xx);
const float tmp0 = mad(c10, xsq, c8 );
const float tmp1 = mad(tmp0, xsq, c6 );
const float tmp2 = mad(tmp1, xsq, c4 );
const float tmp3 = mad(tmp2, xsq, c2 );
const float tmp4 = mad(tmp3, xsq, 1.0);
const float result = tmp4 * c0;
return bits == 1 || bits == 2
? -result
: result
;
}
namespace
{
static const float kAcosC0 = 1.5707288f;
static const float kAcosC1 = -0.2121144f;
static const float kAcosC2 = 0.0742610f;
static const float kAcosC3 = -0.0187293f;
} // namespace
BX_CONST_FUNC float acos(float _a)
{
const float absa = abs(_a);
const float tmp0 = mad(kAcosC3, absa, kAcosC2);
const float tmp1 = mad(tmp0, absa, kAcosC1);
const float tmp2 = mad(tmp1, absa, kAcosC0);
const float tmp3 = tmp2 * sqrt(1.0f - absa);
const float negate = float(_a < 0.0f);
const float tmp4 = tmp3 - 2.0f*negate*tmp3;
const float result = negate*kPi + tmp4;
return result;
}
namespace
{
static const float kAtan2C0 = -0.013480470f;
static const float kAtan2C1 = 0.057477314f;
static const float kAtan2C2 = -0.121239071f;
static const float kAtan2C3 = 0.195635925f;
static const float kAtan2C4 = -0.332994597f;
static const float kAtan2C5 = 0.999995630f;
} // namespace
BX_CONST_FUNC float atan2(float _y, float _x)
{
const float ax = abs(_x);
const float ay = abs(_y);
const float maxaxy = max(ax, ay);
const float minaxy = min(ax, ay);
if (maxaxy == 0.0f)
{
return 0.0f*sign(_y);
}
const float mxy = minaxy / maxaxy;
const float mxysq = square(mxy);
const float tmp0 = mad(kAtan2C0, mxysq, kAtan2C1);
const float tmp1 = mad(tmp0, mxysq, kAtan2C2);
const float tmp2 = mad(tmp1, mxysq, kAtan2C3);
const float tmp3 = mad(tmp2, mxysq, kAtan2C4);
const float tmp4 = mad(tmp3, mxysq, kAtan2C5);
const float tmp5 = tmp4 * mxy;
const float tmp6 = ay > ax ? kPiHalf - tmp5 : tmp5;
const float tmp7 = _x < 0.0f ? kPi - tmp6 : tmp6;
const float result = sign(_y)*tmp7;
return result;
}
BX_CONST_FUNC float ldexp(float _a, int32_t _b)
{
const uint32_t ftob = floatToBits(_a);
const uint32_t masked = uint32_and(ftob, UINT32_C(0xff800000) );
const uint32_t expsign0 = uint32_sra(masked, 23);
const uint32_t tmp = uint32_iadd(expsign0, _b);
const uint32_t expsign1 = uint32_sll(tmp, 23);
const uint32_t mantissa = uint32_and(ftob, UINT32_C(0x007fffff) );
const uint32_t bits = uint32_or(mantissa, expsign1);
const float result = bitsToFloat(bits);
return result;
}
float frexp(float _a, int32_t* _outExp)
{
const uint32_t ftob = floatToBits(_a);
const uint32_t masked0 = uint32_and(ftob, UINT32_C(0x7f800000) );
const uint32_t exp0 = uint32_srl(masked0, 23);
const uint32_t masked1 = uint32_and(ftob, UINT32_C(0x807fffff) );
const uint32_t bits = uint32_or(masked1, UINT32_C(0x3f000000) );
const float result = bitsToFloat(bits);
*_outExp = int32_t(exp0 - 0x7e);
return result;
}
namespace
{
static const float kExpC0 = 1.66666666666666019037e-01f;
static const float kExpC1 = -2.77777777770155933842e-03f;
static const float kExpC2 = 6.61375632143793436117e-05f;
static const float kExpC3 = -1.65339022054652515390e-06f;
static const float kExpC4 = 4.13813679705723846039e-08f;
} // namespace
BX_CONST_FUNC float exp(float _a)
{
if (abs(_a) <= kNearZero)
{
return _a + 1.0f;
}
const float kk = round(_a*kInvLogNat2);
const float hi = _a - kk*kLogNat2Hi;
const float lo = kk*kLogNat2Lo;
const float hml = hi - lo;
const float hmlsq = square(hml);
const float tmp0 = mad(kExpC4, hmlsq, kExpC3);
const float tmp1 = mad(tmp0, hmlsq, kExpC2);
const float tmp2 = mad(tmp1, hmlsq, kExpC1);
const float tmp3 = mad(tmp2, hmlsq, kExpC0);
const float tmp4 = hml - hmlsq * tmp3;
const float tmp5 = hml*tmp4/(2.0f-tmp4);
const float tmp6 = 1.0f - ( (lo - tmp5) - hi);
const float result = ldexp(tmp6, int32_t(kk) );
return result;
}
namespace
{
static const float kLogC0 = 6.666666666666735130e-01f;
static const float kLogC1 = 3.999999999940941908e-01f;
static const float kLogC2 = 2.857142874366239149e-01f;
static const float kLogC3 = 2.222219843214978396e-01f;
static const float kLogC4 = 1.818357216161805012e-01f;
static const float kLogC5 = 1.531383769920937332e-01f;
static const float kLogC6 = 1.479819860511658591e-01f;
} // namespace
BX_CONST_FUNC float log(float _a)
{
int32_t exp;
float ff = frexp(_a, &exp);
if (ff < kSqrt2*0.5f)
{
ff *= 2.0f;
--exp;
}
ff -= 1.0f;
const float kk = float(exp);
const float hi = kk*kLogNat2Hi;
const float lo = kk*kLogNat2Lo;
const float ss = ff / (2.0f + ff);
const float s2 = square(ss);
const float s4 = square(s2);
const float tmp0 = mad(kLogC6, s4, kLogC4);
const float tmp1 = mad(tmp0, s4, kLogC2);
const float tmp2 = mad(tmp1, s4, kLogC0);
const float t1 = s2*tmp2;
const float tmp3 = mad(kLogC5, s4, kLogC3);
const float tmp4 = mad(tmp3, s4, kLogC1);
const float t2 = s4*tmp4;
const float t12 = t1 + t2;
const float hfsq = 0.5f*square(ff);
const float result = hi - ( (hfsq - (ss*(hfsq+t12) + lo) ) - ff);
return result;
}
BX_CONST_FUNC float floor(float _a)
{
if (_a < 0.0f)
{
const float fr = fract(-_a);
const float result = -_a - fr;
return -(0.0f != fr
? result + 1.0f
: result)
;
}
return _a - fract(_a);
}
void mtxLookAtImpl(float* _result, const float* _eye, const float* _view, const float* _up)
{
float up[3] = { 0.0f, 1.0f, 0.0f };
if (NULL != _up)
{
up[0] = _up[0];
up[1] = _up[1];
up[2] = _up[2];
}
float tmp[4];
vec3Cross(tmp, up, _view);
float right[4];
vec3Norm(right, tmp);
vec3Cross(up, _view, right);
memSet(_result, 0, sizeof(float)*16);
_result[ 0] = right[0];
_result[ 1] = up[0];
_result[ 2] = _view[0];
_result[ 4] = right[1];
_result[ 5] = up[1];
_result[ 6] = _view[1];
_result[ 8] = right[2];
_result[ 9] = up[2];
_result[10] = _view[2];
_result[12] = -vec3Dot(right, _eye);
_result[13] = -vec3Dot(up, _eye);
_result[14] = -vec3Dot(_view, _eye);
_result[15] = 1.0f;
}
void mtxLookAtLh(float* _result, const float* _eye, const float* _at, const float* _up)
{
float tmp[4];
vec3Sub(tmp, _at, _eye);
float view[4];
vec3Norm(view, tmp);
mtxLookAtImpl(_result, _eye, view, _up);
}
void mtxLookAtRh(float* _result, const float* _eye, const float* _at, const float* _up)
{
float tmp[4];
vec3Sub(tmp, _eye, _at);
float view[4];
vec3Norm(view, tmp);
mtxLookAtImpl(_result, _eye, view, _up);
}
void mtxLookAt(float* _result, const float* _eye, const float* _at, const float* _up)
{
mtxLookAtLh(_result, _eye, _at, _up);
}
template<Handness::Enum HandnessT>
void mtxProjXYWH(float* _result, float _x, float _y, float _width, float _height, float _near, float _far, bool _oglNdc)
{
const float diff = _far-_near;
const float aa = _oglNdc ? ( _far+_near)/diff : _far/diff;
const float bb = _oglNdc ? (2.0f*_far*_near)/diff : _near*aa;
memSet(_result, 0, sizeof(float)*16);
_result[ 0] = _width;
_result[ 5] = _height;
_result[ 8] = (Handness::Right == HandnessT) ? _x : -_x;
_result[ 9] = (Handness::Right == HandnessT) ? _y : -_y;
_result[10] = (Handness::Right == HandnessT) ? -aa : aa;
_result[11] = (Handness::Right == HandnessT) ? -1.0f : 1.0f;
_result[14] = -bb;
}
template<Handness::Enum HandnessT>
void mtxProjImpl(float* _result, float _ut, float _dt, float _lt, float _rt, float _near, float _far, bool _oglNdc)
{
const float invDiffRl = 1.0f/(_rt - _lt);
const float invDiffUd = 1.0f/(_ut - _dt);
const float width = 2.0f*_near * invDiffRl;
const float height = 2.0f*_near * invDiffUd;
const float xx = (_rt + _lt) * invDiffRl;
const float yy = (_ut + _dt) * invDiffUd;
mtxProjXYWH<HandnessT>(_result, xx, yy, width, height, _near, _far, _oglNdc);
}
template<Handness::Enum HandnessT>
void mtxProjImpl(float* _result, const float _fov[4], float _near, float _far, bool _oglNdc)
{
mtxProjImpl<HandnessT>(_result, _fov[0], _fov[1], _fov[2], _fov[3], _near, _far, _oglNdc);
}
template<Handness::Enum HandnessT>
void mtxProjImpl(float* _result, float _fovy, float _aspect, float _near, float _far, bool _oglNdc)
{
const float height = 1.0f/tan(toRad(_fovy)*0.5f);
const float width = height * 1.0f/_aspect;
mtxProjXYWH<HandnessT>(_result, 0.0f, 0.0f, width, height, _near, _far, _oglNdc);
}
void mtxProj(float* _result, float _ut, float _dt, float _lt, float _rt, float _near, float _far, bool _oglNdc)
{
mtxProjImpl<Handness::Left>(_result, _ut, _dt, _lt, _rt, _near, _far, _oglNdc);
}
void mtxProj(float* _result, const float _fov[4], float _near, float _far, bool _oglNdc)
{
mtxProjImpl<Handness::Left>(_result, _fov, _near, _far, _oglNdc);
}
void mtxProj(float* _result, float _fovy, float _aspect, float _near, float _far, bool _oglNdc)
{
mtxProjImpl<Handness::Left>(_result, _fovy, _aspect, _near, _far, _oglNdc);
}
void mtxProjLh(float* _result, float _ut, float _dt, float _lt, float _rt, float _near, float _far, bool _oglNdc)
{
mtxProjImpl<Handness::Left>(_result, _ut, _dt, _lt, _rt, _near, _far, _oglNdc);
}
void mtxProjLh(float* _result, const float _fov[4], float _near, float _far, bool _oglNdc)
{
mtxProjImpl<Handness::Left>(_result, _fov, _near, _far, _oglNdc);
}
void mtxProjLh(float* _result, float _fovy, float _aspect, float _near, float _far, bool _oglNdc)
{
mtxProjImpl<Handness::Left>(_result, _fovy, _aspect, _near, _far, _oglNdc);
}
void mtxProjRh(float* _result, float _ut, float _dt, float _lt, float _rt, float _near, float _far, bool _oglNdc)
{
mtxProjImpl<Handness::Right>(_result, _ut, _dt, _lt, _rt, _near, _far, _oglNdc);
}
void mtxProjRh(float* _result, const float _fov[4], float _near, float _far, bool _oglNdc)
{
mtxProjImpl<Handness::Right>(_result, _fov, _near, _far, _oglNdc);
}
void mtxProjRh(float* _result, float _fovy, float _aspect, float _near, float _far, bool _oglNdc)
{
mtxProjImpl<Handness::Right>(_result, _fovy, _aspect, _near, _far, _oglNdc);
}
template<NearFar::Enum NearFarT, Handness::Enum HandnessT>
void mtxProjInfXYWH(float* _result, float _x, float _y, float _width, float _height, float _near, bool _oglNdc)
{
float aa;
float bb;
if (BX_ENABLED(NearFar::Reverse == NearFarT) )
{
aa = _oglNdc ? -1.0f : 0.0f;
bb = _oglNdc ? -2.0f*_near : -_near;
}
else
{
aa = 1.0f;
bb = _oglNdc ? 2.0f*_near : _near;
}
memSet(_result, 0, sizeof(float)*16);
_result[ 0] = _width;
_result[ 5] = _height;
_result[ 8] = (Handness::Right == HandnessT) ? _x : -_x;
_result[ 9] = (Handness::Right == HandnessT) ? _y : -_y;
_result[10] = (Handness::Right == HandnessT) ? -aa : aa;
_result[11] = (Handness::Right == HandnessT) ? -1.0f : 1.0f;
_result[14] = -bb;
}
template<NearFar::Enum NearFarT, Handness::Enum HandnessT>
void mtxProjInfImpl(float* _result, float _ut, float _dt, float _lt, float _rt, float _near, bool _oglNdc)
{
const float invDiffRl = 1.0f/(_rt - _lt);
const float invDiffUd = 1.0f/(_ut - _dt);
const float width = 2.0f*_near * invDiffRl;
const float height = 2.0f*_near * invDiffUd;
const float xx = (_rt + _lt) * invDiffRl;
const float yy = (_ut + _dt) * invDiffUd;
mtxProjInfXYWH<NearFarT,HandnessT>(_result, xx, yy, width, height, _near, _oglNdc);
}
template<NearFar::Enum NearFarT, Handness::Enum HandnessT>
void mtxProjInfImpl(float* _result, const float _fov[4], float _near, bool _oglNdc)
{
mtxProjInfImpl<NearFarT,HandnessT>(_result, _fov[0], _fov[1], _fov[2], _fov[3], _near, _oglNdc);
}
template<NearFar::Enum NearFarT, Handness::Enum HandnessT>
void mtxProjInfImpl(float* _result, float _fovy, float _aspect, float _near, bool _oglNdc)
{
const float height = 1.0f/tan(toRad(_fovy)*0.5f);
const float width = height * 1.0f/_aspect;
mtxProjInfXYWH<NearFarT,HandnessT>(_result, 0.0f, 0.0f, width, height, _near, _oglNdc);
}
void mtxProjInf(float* _result, const float _fov[4], float _near, bool _oglNdc)
{
mtxProjInfImpl<NearFar::Default,Handness::Left>(_result, _fov, _near, _oglNdc);
}
void mtxProjInf(float* _result, float _ut, float _dt, float _lt, float _rt, float _near, bool _oglNdc)
{
mtxProjInfImpl<NearFar::Default,Handness::Left>(_result, _ut, _dt, _lt, _rt, _near, _oglNdc);
}
void mtxProjInf(float* _result, float _fovy, float _aspect, float _near, bool _oglNdc)
{
mtxProjInfImpl<NearFar::Default,Handness::Left>(_result, _fovy, _aspect, _near, _oglNdc);
}
void mtxProjInfLh(float* _result, float _ut, float _dt, float _lt, float _rt, float _near, bool _oglNdc)
{
mtxProjInfImpl<NearFar::Default,Handness::Left>(_result, _ut, _dt, _lt, _rt, _near, _oglNdc);
}
void mtxProjInfLh(float* _result, const float _fov[4], float _near, bool _oglNdc)
{
mtxProjInfImpl<NearFar::Default,Handness::Left>(_result, _fov, _near, _oglNdc);
}
void mtxProjInfLh(float* _result, float _fovy, float _aspect, float _near, bool _oglNdc)
{
mtxProjInfImpl<NearFar::Default,Handness::Left>(_result, _fovy, _aspect, _near, _oglNdc);
}
void mtxProjInfRh(float* _result, float _ut, float _dt, float _lt, float _rt, float _near, bool _oglNdc)
{
mtxProjInfImpl<NearFar::Default,Handness::Right>(_result, _ut, _dt, _lt, _rt, _near, _oglNdc);
}
void mtxProjInfRh(float* _result, const float _fov[4], float _near, bool _oglNdc)
{
mtxProjInfImpl<NearFar::Default,Handness::Right>(_result, _fov, _near, _oglNdc);
}
void mtxProjInfRh(float* _result, float _fovy, float _aspect, float _near, bool _oglNdc)
{
mtxProjInfImpl<NearFar::Default,Handness::Right>(_result, _fovy, _aspect, _near, _oglNdc);
}
void mtxProjRevInfLh(float* _result, float _ut, float _dt, float _lt, float _rt, float _near, bool _oglNdc)
{
mtxProjInfImpl<NearFar::Reverse,Handness::Left>(_result, _ut, _dt, _lt, _rt, _near, _oglNdc);
}
void mtxProjRevInfLh(float* _result, const float _fov[4], float _near, bool _oglNdc)
{
mtxProjInfImpl<NearFar::Reverse,Handness::Left>(_result, _fov, _near, _oglNdc);
}
void mtxProjRevInfLh(float* _result, float _fovy, float _aspect, float _near, bool _oglNdc)
{
mtxProjInfImpl<NearFar::Reverse,Handness::Left>(_result, _fovy, _aspect, _near, _oglNdc);
}
void mtxProjRevInfRh(float* _result, float _ut, float _dt, float _lt, float _rt, float _near, bool _oglNdc)
{
mtxProjInfImpl<NearFar::Reverse,Handness::Right>(_result, _ut, _dt, _lt, _rt, _near, _oglNdc);
}
void mtxProjRevInfRh(float* _result, const float _fov[4], float _near, bool _oglNdc)
{
mtxProjInfImpl<NearFar::Reverse,Handness::Right>(_result, _fov, _near, _oglNdc);
}
void mtxProjRevInfRh(float* _result, float _fovy, float _aspect, float _near, bool _oglNdc)
{
mtxProjInfImpl<NearFar::Reverse,Handness::Right>(_result, _fovy, _aspect, _near, _oglNdc);
}
template<Handness::Enum HandnessT>
void mtxOrthoImpl(float* _result, float _left, float _right, float _bottom, float _top, float _near, float _far, float _offset, bool _oglNdc)
{
const float aa = 2.0f/(_right - _left);
const float bb = 2.0f/(_top - _bottom);
const float cc = (_oglNdc ? 2.0f : 1.0f) / (_far - _near);
const float dd = (_left + _right )/(_left - _right);
const float ee = (_top + _bottom)/(_bottom - _top );
const float ff = _oglNdc
? (_near + _far)/(_near - _far)
: _near /(_near - _far)
;
memSet(_result, 0, sizeof(float)*16);
_result[ 0] = aa;
_result[ 5] = bb;
_result[10] = (Handness::Right == HandnessT) ? -cc : cc;
_result[12] = dd + _offset;
_result[13] = ee;
_result[14] = ff;
_result[15] = 1.0f;
}
void mtxOrtho(float* _result, float _left, float _right, float _bottom, float _top, float _near, float _far, float _offset, bool _oglNdc)
{
mtxOrthoImpl<Handness::Left>(_result, _left, _right, _bottom, _top, _near, _far, _offset, _oglNdc);
}
void mtxOrthoLh(float* _result, float _left, float _right, float _bottom, float _top, float _near, float _far, float _offset, bool _oglNdc)
{
mtxOrthoImpl<Handness::Left>(_result, _left, _right, _bottom, _top, _near, _far, _offset, _oglNdc);
}
void mtxOrthoRh(float* _result, float _left, float _right, float _bottom, float _top, float _near, float _far, float _offset, bool _oglNdc)
{
mtxOrthoImpl<Handness::Right>(_result, _left, _right, _bottom, _top, _near, _far, _offset, _oglNdc);
}
void mtxRotateX(float* _result, float _ax)
{
const float sx = sin(_ax);
const float cx = cos(_ax);
memSet(_result, 0, sizeof(float)*16);
_result[ 0] = 1.0f;
_result[ 5] = cx;
_result[ 6] = -sx;
_result[ 9] = sx;
_result[10] = cx;
_result[15] = 1.0f;
}
void mtxRotateY(float* _result, float _ay)
{
const float sy = sin(_ay);
const float cy = cos(_ay);
memSet(_result, 0, sizeof(float)*16);
_result[ 0] = cy;
_result[ 2] = sy;
_result[ 5] = 1.0f;
_result[ 8] = -sy;
_result[10] = cy;
_result[15] = 1.0f;
}
void mtxRotateZ(float* _result, float _az)
{
const float sz = sin(_az);
const float cz = cos(_az);
memSet(_result, 0, sizeof(float)*16);
_result[ 0] = cz;
_result[ 1] = -sz;
_result[ 4] = sz;
_result[ 5] = cz;
_result[10] = 1.0f;
_result[15] = 1.0f;
}
void mtxRotateXY(float* _result, float _ax, float _ay)
{
const float sx = sin(_ax);
const float cx = cos(_ax);
const float sy = sin(_ay);
const float cy = cos(_ay);
memSet(_result, 0, sizeof(float)*16);
_result[ 0] = cy;
_result[ 2] = sy;
_result[ 4] = sx*sy;
_result[ 5] = cx;
_result[ 6] = -sx*cy;
_result[ 8] = -cx*sy;
_result[ 9] = sx;
_result[10] = cx*cy;
_result[15] = 1.0f;
}
void mtxRotateXYZ(float* _result, float _ax, float _ay, float _az)
{
const float sx = sin(_ax);
const float cx = cos(_ax);
const float sy = sin(_ay);
const float cy = cos(_ay);
const float sz = sin(_az);
const float cz = cos(_az);
memSet(_result, 0, sizeof(float)*16);
_result[ 0] = cy*cz;
_result[ 1] = -cy*sz;
_result[ 2] = sy;
_result[ 4] = cz*sx*sy + cx*sz;
_result[ 5] = cx*cz - sx*sy*sz;
_result[ 6] = -cy*sx;
_result[ 8] = -cx*cz*sy + sx*sz;
_result[ 9] = cz*sx + cx*sy*sz;
_result[10] = cx*cy;
_result[15] = 1.0f;
}
void mtxRotateZYX(float* _result, float _ax, float _ay, float _az)
{
const float sx = sin(_ax);
const float cx = cos(_ax);
const float sy = sin(_ay);
const float cy = cos(_ay);
const float sz = sin(_az);
const float cz = cos(_az);
memSet(_result, 0, sizeof(float)*16);
_result[ 0] = cy*cz;
_result[ 1] = cz*sx*sy-cx*sz;
_result[ 2] = cx*cz*sy+sx*sz;
_result[ 4] = cy*sz;
_result[ 5] = cx*cz + sx*sy*sz;
_result[ 6] = -cz*sx + cx*sy*sz;
_result[ 8] = -sy;
_result[ 9] = cy*sx;
_result[10] = cx*cy;
_result[15] = 1.0f;
};
void mtxSRT(float* _result, float _sx, float _sy, float _sz, float _ax, float _ay, float _az, float _tx, float _ty, float _tz)
{
const float sx = sin(_ax);
const float cx = cos(_ax);
const float sy = sin(_ay);
const float cy = cos(_ay);
const float sz = sin(_az);
const float cz = cos(_az);
const float sxsz = sx*sz;
const float cycz = cy*cz;
_result[ 0] = _sx * (cycz - sxsz*sy);
_result[ 1] = _sx * -cx*sz;
_result[ 2] = _sx * (cz*sy + cy*sxsz);
_result[ 3] = 0.0f;
_result[ 4] = _sy * (cz*sx*sy + cy*sz);
_result[ 5] = _sy * cx*cz;
_result[ 6] = _sy * (sy*sz -cycz*sx);
_result[ 7] = 0.0f;
_result[ 8] = _sz * -cx*sy;
_result[ 9] = _sz * sx;
_result[10] = _sz * cx*cy;
_result[11] = 0.0f;
_result[12] = _tx;
_result[13] = _ty;
_result[14] = _tz;
_result[15] = 1.0f;
}
void mtx3Inverse(float* _result, const float* _a)
{
float xx = _a[0];
float xy = _a[1];
float xz = _a[2];
float yx = _a[3];
float yy = _a[4];
float yz = _a[5];
float zx = _a[6];
float zy = _a[7];
float zz = _a[8];
float det = 0.0f;
det += xx * (yy*zz - yz*zy);
det -= xy * (yx*zz - yz*zx);
det += xz * (yx*zy - yy*zx);
float invDet = 1.0f/det;
_result[0] = +(yy*zz - yz*zy) * invDet;
_result[1] = -(xy*zz - xz*zy) * invDet;
_result[2] = +(xy*yz - xz*yy) * invDet;
_result[3] = -(yx*zz - yz*zx) * invDet;
_result[4] = +(xx*zz - xz*zx) * invDet;
_result[5] = -(xx*yz - xz*yx) * invDet;
_result[6] = +(yx*zy - yy*zx) * invDet;
_result[7] = -(xx*zy - xy*zx) * invDet;
_result[8] = +(xx*yy - xy*yx) * invDet;
}
void mtxInverse(float* _result, const float* _a)
{
float xx = _a[ 0];
float xy = _a[ 1];
float xz = _a[ 2];
float xw = _a[ 3];
float yx = _a[ 4];
float yy = _a[ 5];
float yz = _a[ 6];
float yw = _a[ 7];
float zx = _a[ 8];
float zy = _a[ 9];
float zz = _a[10];
float zw = _a[11];
float wx = _a[12];
float wy = _a[13];
float wz = _a[14];
float ww = _a[15];
float det = 0.0f;
det += xx * (yy*(zz*ww - zw*wz) - yz*(zy*ww - zw*wy) + yw*(zy*wz - zz*wy) );
det -= xy * (yx*(zz*ww - zw*wz) - yz*(zx*ww - zw*wx) + yw*(zx*wz - zz*wx) );
det += xz * (yx*(zy*ww - zw*wy) - yy*(zx*ww - zw*wx) + yw*(zx*wy - zy*wx) );
det -= xw * (yx*(zy*wz - zz*wy) - yy*(zx*wz - zz*wx) + yz*(zx*wy - zy*wx) );
float invDet = 1.0f/det;
_result[ 0] = +(yy*(zz*ww - wz*zw) - yz*(zy*ww - wy*zw) + yw*(zy*wz - wy*zz) ) * invDet;
_result[ 1] = -(xy*(zz*ww - wz*zw) - xz*(zy*ww - wy*zw) + xw*(zy*wz - wy*zz) ) * invDet;
_result[ 2] = +(xy*(yz*ww - wz*yw) - xz*(yy*ww - wy*yw) + xw*(yy*wz - wy*yz) ) * invDet;
_result[ 3] = -(xy*(yz*zw - zz*yw) - xz*(yy*zw - zy*yw) + xw*(yy*zz - zy*yz) ) * invDet;
_result[ 4] = -(yx*(zz*ww - wz*zw) - yz*(zx*ww - wx*zw) + yw*(zx*wz - wx*zz) ) * invDet;
_result[ 5] = +(xx*(zz*ww - wz*zw) - xz*(zx*ww - wx*zw) + xw*(zx*wz - wx*zz) ) * invDet;
_result[ 6] = -(xx*(yz*ww - wz*yw) - xz*(yx*ww - wx*yw) + xw*(yx*wz - wx*yz) ) * invDet;
_result[ 7] = +(xx*(yz*zw - zz*yw) - xz*(yx*zw - zx*yw) + xw*(yx*zz - zx*yz) ) * invDet;
_result[ 8] = +(yx*(zy*ww - wy*zw) - yy*(zx*ww - wx*zw) + yw*(zx*wy - wx*zy) ) * invDet;
_result[ 9] = -(xx*(zy*ww - wy*zw) - xy*(zx*ww - wx*zw) + xw*(zx*wy - wx*zy) ) * invDet;
_result[10] = +(xx*(yy*ww - wy*yw) - xy*(yx*ww - wx*yw) + xw*(yx*wy - wx*yy) ) * invDet;
_result[11] = -(xx*(yy*zw - zy*yw) - xy*(yx*zw - zx*yw) + xw*(yx*zy - zx*yy) ) * invDet;
_result[12] = -(yx*(zy*wz - wy*zz) - yy*(zx*wz - wx*zz) + yz*(zx*wy - wx*zy) ) * invDet;
_result[13] = +(xx*(zy*wz - wy*zz) - xy*(zx*wz - wx*zz) + xz*(zx*wy - wx*zy) ) * invDet;
_result[14] = -(xx*(yy*wz - wy*yz) - xy*(yx*wz - wx*yz) + xz*(yx*wy - wx*yy) ) * invDet;
_result[15] = +(xx*(yy*zz - zy*yz) - xy*(yx*zz - zx*yz) + xz*(yx*zy - zx*yy) ) * invDet;
}
void calcLinearFit2D(float _result[2], const void* _points, uint32_t _stride, uint32_t _numPoints)
{
float sumX = 0.0f;
float sumY = 0.0f;
float sumXX = 0.0f;
float sumXY = 0.0f;
const uint8_t* ptr = (const uint8_t*)_points;
for (uint32_t ii = 0; ii < _numPoints; ++ii, ptr += _stride)
{
const float* point = (const float*)ptr;
float xx = point[0];
float yy = point[1];
sumX += xx;
sumY += yy;
sumXX += xx*xx;
sumXY += xx*yy;
}
// [ sum(x^2) sum(x) ] [ A ] = [ sum(x*y) ]
// [ sum(x) numPoints ] [ B ] [ sum(y) ]
float det = (sumXX*_numPoints - sumX*sumX);
float invDet = 1.0f/det;
_result[0] = (-sumX * sumY + _numPoints * sumXY) * invDet;
_result[1] = (sumXX * sumY - sumX * sumXY) * invDet;
}
void calcLinearFit3D(float _result[3], const void* _points, uint32_t _stride, uint32_t _numPoints)
{
float sumX = 0.0f;
float sumY = 0.0f;
float sumZ = 0.0f;
float sumXX = 0.0f;
float sumXY = 0.0f;
float sumXZ = 0.0f;
float sumYY = 0.0f;
float sumYZ = 0.0f;
const uint8_t* ptr = (const uint8_t*)_points;
for (uint32_t ii = 0; ii < _numPoints; ++ii, ptr += _stride)
{
const float* point = (const float*)ptr;
float xx = point[0];
float yy = point[1];
float zz = point[2];
sumX += xx;
sumY += yy;
sumZ += zz;
sumXX += xx*xx;
sumXY += xx*yy;
sumXZ += xx*zz;
sumYY += yy*yy;
sumYZ += yy*zz;
}
// [ sum(x^2) sum(x*y) sum(x) ] [ A ] [ sum(x*z) ]
// [ sum(x*y) sum(y^2) sum(y) ] [ B ] = [ sum(y*z) ]
// [ sum(x) sum(y) numPoints ] [ C ] [ sum(z) ]
float mtx[9] =
{
sumXX, sumXY, sumX,
sumXY, sumYY, sumY,
sumX, sumY, float(_numPoints),
};
float invMtx[9];
mtx3Inverse(invMtx, mtx);
_result[0] = invMtx[0]*sumXZ + invMtx[1]*sumYZ + invMtx[2]*sumZ;
_result[1] = invMtx[3]*sumXZ + invMtx[4]*sumYZ + invMtx[5]*sumZ;
_result[2] = invMtx[6]*sumXZ + invMtx[7]*sumYZ + invMtx[8]*sumZ;
}
void rgbToHsv(float _hsv[3], const float _rgb[3])
{
const float rr = _rgb[0];
const float gg = _rgb[1];
const float bb = _rgb[2];
const float s0 = step(bb, gg);
const float px = lerp(bb, gg, s0);
const float py = lerp(gg, bb, s0);
const float pz = lerp(-1.0f, 0.0f, s0);
const float pw = lerp(2.0f/3.0f, -1.0f/3.0f, s0);
const float s1 = step(px, rr);
const float qx = lerp(px, rr, s1);
const float qy = py;
const float qz = lerp(pw, pz, s1);
const float qw = lerp(rr, px, s1);
const float dd = qx - min(qw, qy);
const float ee = 1.0e-10f;
_hsv[0] = abs(qz + (qw - qy) / (6.0f * dd + ee) );
_hsv[1] = dd / (qx + ee);
_hsv[2] = qx;
}
void hsvToRgb(float _rgb[3], const float _hsv[3])
{
const float hh = _hsv[0];
const float ss = _hsv[1];
const float vv = _hsv[2];
const float px = abs(fract(hh + 1.0f ) * 6.0f - 3.0f);
const float py = abs(fract(hh + 2.0f/3.0f) * 6.0f - 3.0f);
const float pz = abs(fract(hh + 1.0f/3.0f) * 6.0f - 3.0f);
_rgb[0] = vv * lerp(1.0f, clamp(px - 1.0f, 0.0f, 1.0f), ss);
_rgb[1] = vv * lerp(1.0f, clamp(py - 1.0f, 0.0f, 1.0f), ss);
_rgb[2] = vv * lerp(1.0f, clamp(pz - 1.0f, 0.0f, 1.0f), ss);
}
} // namespace bx
Loading...
举报
举报成功
我们将于2个工作日内通过站内信反馈结果给你!
请认真填写举报原因,尽可能描述详细。
请选择举报类型
取消
发送
误判申诉

此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。

如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。

取消
提交

简介

Base library used across multiple projects
暂无标签
BSD-2-Clause
使用 BSD-2-Clause 开源许可协议
取消

发行版

暂无发行版

贡献者

全部

近期动态

不能加载更多了
编辑仓库简介
简介内容
主页
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
C++
1
https://gitee.com/devpark/bx.git
git@gitee.com:devpark/bx.git
devpark
bx
bx
master
点此查找更多帮助

搜索帮助

评论
仓库举报
回到顶部
登录提示
该操作需登录 Gitee 帐号,请先登录后再操作。
立即登录
没有帐号,去注册

AltStyle によって変換されたページ (->オリジナル) /