1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 #pragma once19 20 #include "../MathBuildConfig.h"21 22 #ifdef MATH_ENABLE_STL_SUPPORT23 #include <string>24 #endif25 26 #include "../MathGeoLibFwd.h"27 28 #ifdef MATH_QT_INTEROP29 #include <QVector3D>30 #endif31 #ifdef MATH_OGRE_INTEROP32 #include <OgreVector3.h>33 #endif34 #ifdef MATH_BULLET_INTEROP35 #include <LinearMath/btVector3.h>36 #endif37 #ifdef MATH_URHO3D_INTEROP38 #include <Urho3D/Math/Vector3.h>39 #endif40 #ifdef MATH_IRRKLANG_INTEROP41 #include <ik_vec3d.h>42 #endif43 44 [MATH_BEGIN_NAMESPACE]45 46 47 class [float3]48 {49 public:50 enum51 {52 53 [Size] = 354 };55 56 57 float [x];58 59 60 float [y];61 62 63 float [z];64 65 66 67 68 69 [float3]() {}70 71 #ifdef MATH_EXPLICIT_COPYCTORS72 73 74 75 [float3](const [float3] &rhs) { [x] = rhs.[x]; [y] = rhs.[y]; [z] = rhs.[z]; }76 #endif77 78 79 80 [float3](float [x], float [y], float [z]);81 82 83 84 explicit [float3](float scalar);85 86 87 [float3](const [float2] &[xy], float [z]);88 89 90 91 explicit [float3](const float *data);92 93 94 95 96 97 98 99 100 101 102 103 104 [FORCE_INLINE] float *[ptr]() { return &[x]; }105 [FORCE_INLINE] const float *[ptr]() const { return &[x]; }106 107 108 109 110 111 112 float &[operator []](int index) { return [At](index); }113 [CONST_WIN32] float [operator []](int index) const { return [At](index); }114 115 116 117 118 119 120 float &[At](int index);121 [CONST_WIN32] float [At](int index) const;122 123 124 125 126 [float3] [operator +](const [float3] &v) const;127 128 129 130 [float3] [operator -]() const;131 132 133 134 [float3] [operator -](const [float3] &v) const;135 136 137 138 [float3] [operator *](float scalar) const;139 140 141 142 [float3] [operator /](float scalar) const;143 144 [float3] [operator +]() const { return *this; }145 146 147 148 [float3] &[operator +=](const [float3] &v);149 150 151 [float3] &[operator -=](const [float3] &v);152 153 154 [float3] &[operator *=](float scalar);155 156 157 [float3] &[operator /=](float scalar);158 159 #ifdef MATH_ENABLE_UNCOMMON_OPERATIONS160 161 162 163 164 165 166 [float3] [operator *](const [float3] &vector) const { return this->[Mul](vector); }167 [float3] [operator /](const [float3] &vector) const { return this->[Div](vector); }168 [float3] &[operator *=](const [float3] &vector) { *this = this->[Mul](vector); return *this; }169 [float3] &[operator /=](const [float3] &vector) { *this = this->[Div](vector); return *this; }170 #endif171 172 173 174 [float3] [Add](const [float3] &v) const { return *this + v; }175 176 177 178 179 180 [float3] [Add](float s) const;181 182 183 184 [float3] [Sub](const [float3] &v) const { return *this - v; }185 186 187 188 189 190 [float3] [Sub](float s) const;191 192 193 194 195 196 [float3] [SubLeft](float s) const;197 198 199 200 201 202 [float3] [Mul](const [float3] &v) const;203 204 205 206 [float3] [Mul](float s) const { return *this * s; }207 208 209 210 211 212 [float3] [Div](const [float3] &v) const;213 214 215 216 [float3] [Div](float s) const { return *this / s; }217 218 219 220 221 222 [float3] [DivLeft](float s) const;223 224 225 226 227 [float2] [xx]() const;228 [float2] [xy]() const; 229 [float2] [xz]() const; 230 [float2] [yx]() const; 231 [float2] [yy]() const; 232 [float2] [yz]() const; 233 [float2] [zx]() const; 234 [float2] [zy]() const; 235 [float2] [zz]() const; 236 237 238 239 240 [float3] [xxx]() const { return [float3]([x],[x],[x]); }241 [float3] [xxy]() const { return [float3]([x],[x],[y]); } 242 [float3] [xxz]() const { return [float3]([x],[x],[z]); } 243 [float3] [xyx]() const { return [float3]([x],[y],[x]); } 244 [float3] [xyy]() const { return [float3]([x],[y],[y]); } 245 [float3] [xyz]() const { return [float3]([x],[y],[z]); } 246 [float3] [xzx]() const { return [float3]([x],[z],[x]); } 247 [float3] [xzy]() const { return [float3]([x],[z],[y]); } 248 [float3] [xzz]() const { return [float3]([x],[z],[z]); } 249 250 [float3] [yxx]() const { return [float3]([y],[x],[x]); } 251 [float3] [yxy]() const { return [float3]([y],[x],[y]); } 252 [float3] [yxz]() const { return [float3]([y],[x],[z]); } 253 [float3] [yyx]() const { return [float3]([y],[y],[x]); } 254 [float3] [yyy]() const { return [float3]([y],[y],[y]); } 255 [float3] [yyz]() const { return [float3]([y],[y],[z]); } 256 [float3] [yzx]() const { return [float3]([y],[z],[x]); } 257 [float3] [yzy]() const { return [float3]([y],[z],[y]); } 258 [float3] [yzz]() const { return [float3]([y],[z],[z]); } 259 260 [float3] [zxx]() const { return [float3]([z],[x],[x]); } 261 [float3] [zxy]() const { return [float3]([z],[x],[y]); } 262 [float3] [zxz]() const { return [float3]([z],[x],[z]); } 263 [float3] [zyx]() const { return [float3]([z],[y],[x]); } 264 [float3] [zyy]() const { return [float3]([z],[y],[y]); } 265 [float3] [zyz]() const { return [float3]([z],[y],[z]); } 266 [float3] [zzx]() const { return [float3]([z],[z],[x]); } 267 [float3] [zzy]() const { return [float3]([z],[z],[y]); } 268 [float3] [zzz]() const { return [float3]([z],[z],[z]); } 269 270 271 272 273 274 275 276 [float4] [Swizzled](int i, int j, int k, int l) const;277 [float3] [Swizzled](int i, int j, int k) const;278 [float2] [Swizzled](int i, int j) const;279 280 281 282 static [MUST_USE_RESULT] [float3] [FromScalar](float scalar);283 284 285 286 void [SetFromScalar](float scalar);287 288 289 290 void [Set](float [x], float [y], float [z]);291 292 293 294 295 296 297 298 299 300 301 void [SetFromSphericalCoordinates](float azimuth, float inclination, float radius);302 void [SetFromSphericalCoordinates](const [float3] &spherical) { [SetFromSphericalCoordinates](spherical.[x], spherical.[y], spherical.[z]); }303 static [MUST_USE_RESULT] [float3] [FromSphericalCoordinates](float azimuth, float inclination, float radius);304 static [MUST_USE_RESULT] [float3] [FromSphericalCoordinates](const [float3] &spherical) { return [FromSphericalCoordinates](spherical.[x], spherical.[y], spherical.[z]); }305 306 307 308 309 void [SetFromSphericalCoordinates](float azimuth, float inclination);310 static [MUST_USE_RESULT] [float3] [FromSphericalCoordinates](float azimuth, float inclination);311 312 313 314 [float4] [ToPos4]() const;315 316 317 318 [float4] [ToDir4]() const;319 320 321 322 323 324 325 [float3] [ToSphericalCoordinates]() const;326 327 328 329 330 331 332 333 [float2] [ToSphericalCoordinatesNormalized]() const;334 335 336 337 338 float [Length]() const;339 340 341 342 343 344 345 346 347 float [LengthSq]() const;348 349 350 351 352 353 354 355 356 357 358 float [Normalize]();359 360 361 362 363 364 365 [float3] [Normalized]() const;366 367 368 369 370 371 372 373 374 375 376 float [ScaleToLength](float newLength);377 378 379 380 381 382 [float3] [ScaledToLength](float newLength) const;383 384 385 386 bool [IsNormalized](float epsilonSq = 1[e]-5f) const;387 388 389 390 bool [IsZero](float epsilonSq = 1[e]-7f) const;391 392 393 394 bool [IsFinite]() const;395 396 397 398 bool [IsPerpendicular](const [float3] &other, float epsilonSq = 1[e]-8f) const;399 400 401 402 static [MUST_USE_RESULT] bool [AreCollinear](const [float3] &p1, const [float3] &p2, const [float3] &p3, float epsilonSq = 1[e]-7f);403 404 405 406 bool [Equals](const [float3] &other, float [epsilon] = 1[e]-3f) const;407 bool [Equals](float [x], float [y], float [z], float [epsilon] = 1[e]-3f) const;408 409 410 411 bool [BitEquals](const [float3] &other) const;412 413 #ifdef MATH_ENABLE_STL_SUPPORT414 415 std::string [ToString]() const;416 417 418 std::string [SerializeToString]() const;419 420 421 std::string [SerializeToCodeString]() const;422 #endif423 424 425 static [MUST_USE_RESULT] [float3] [FromString](const char *str, const char **outEndStr = 0);426 #ifdef MATH_ENABLE_STL_SUPPORT427 static [MUST_USE_RESULT] [float3] [FromString](const std::string &str) { return [FromString](str.c_str()); }428 #endif429 430 431 float [SumOfElements]() const;432 433 float [ProductOfElements]() const;434 435 float [AverageOfElements]() const;436 437 438 float [MinElement]() const;439 440 441 int [MinElementIndex]() const;442 443 444 float [MaxElement]() const;445 446 447 int [MaxElementIndex]() const;448 449 450 451 [float3] [Abs]() const;452 453 454 455 456 [float3] [Neg]() const;457 458 459 460 [float3] [Recip]() const;461 462 463 [float3] [Min](float ceil) const;464 465 466 467 [float3] [Min](const [float3] &ceil) const;468 469 470 [float3] [Max](float floor) const;471 472 473 474 [float3] [Max](const [float3] &floor) const;475 476 [float3] [Clamp](float floor, float ceil) const;477 478 479 [float3] [Clamp](const [float3] &floor, const [float3] &ceil) const;480 481 482 [float3] [Clamp01]() const;483 484 485 486 [float3] [ClampLength](float maxLength) const;487 488 489 [float3] [ClampLength](float minLength, float maxLength) const;490 491 492 493 494 495 496 497 float [Distance](const [float3] &point) const;498 float [Distance](const [Line] &line) const;499 float [Distance](const [Ray] &ray) const;500 float [Distance](const [LineSegment] &lineSegment) const;501 float [Distance](const [Plane] &plane) const;502 float [Distance](const [Triangle] &triangle) const;503 float [Distance](const [AABB] &aabb) const;504 float [Distance](const [OBB] &obb) const;505 float [Distance](const [Sphere] &sphere) const;506 float [Distance](const [Capsule] &capsule) const;507 508 509 510 511 512 513 514 float [DistanceSq](const [float3] &point) const;515 516 517 518 519 520 521 float [Dot](const [float3] &v) const;522 523 524 525 526 527 528 [float3] [Cross](const [float3] &v) const;529 530 531 532 [float3x3] [OuterProduct](const [float3] &rhs) const;533 534 535 536 537 [float3] [Perpendicular](const [float3] &hint = [float3](0,1,0), const [float3] &hint2 = [float3](0,0,1)) const;538 539 540 541 542 [float3] [AnotherPerpendicular](const [float3] &hint = [float3](0,1,0), const [float3] &hint2 = [float3](0,0,1)) const;543 544 545 546 547 548 549 550 void [PerpendicularBasis]([float3] &outB, [float3] &outC) const;551 552 553 554 [float3] [RandomPerpendicular]([LCG] &rng) const;555 556 557 558 559 static [MUST_USE_RESULT] float [ScalarTripleProduct](const [float3] &u, const [float3] &v, const [float3] &w);560 561 562 563 564 [float3] [Reflect](const [float3] &normal) const;565 566 567 568 569 570 571 572 573 574 [float3] [Refract](const [float3] &normal, float negativeSideRefractionIndex, float positiveSideRefractionIndex) const;575 576 577 578 579 580 [float3] [ProjectTo](const [float3] &direction) const;581 582 583 584 585 [float3] [ProjectToNorm](const [float3] &direction) const;586 587 588 589 590 591 float [AngleBetween](const [float3] &other) const;592 593 594 595 596 597 float [AngleBetweenNorm](const [float3] &normalizedVector) const;598 599 600 601 602 603 void [Decompose](const [float3] &direction, [float3] &outParallel, [float3] &outPerpendicular) const;604 605 606 607 608 609 610 611 [float3] [Lerp](const [float3] &b, float t) const;612 613 static [MUST_USE_RESULT] [float3] [Lerp](const [float3] &a, const [float3] &b, float t);614 615 616 617 618 619 620 621 static void [Orthogonalize](const [float3] &a, [float3] &b);622 static void [Orthogonalize](const [float3] &a, [float3] &b, [float3] &c);623 624 625 626 static [MUST_USE_RESULT] bool [AreOrthogonal](const [float3] &a, const [float3] &b, float [epsilon] = 1[e]-3f);627 static [MUST_USE_RESULT] bool [AreOrthogonal](const [float3] &a, const [float3] &b, const [float3] &c, float [epsilon] = 1[e]-3f);628 629 630 631 632 633 634 635 static void [Orthonormalize]([float3] &a, [float3] &b);636 static void [Orthonormalize]([float3] &a, [float3] &b, [float3] &c);637 638 639 640 static [MUST_USE_RESULT] bool [AreOrthonormal](const [float3] &a, const [float3] &b, float [epsilon] = 1[e]-3f);641 static [MUST_USE_RESULT] bool [AreOrthonormal](const [float3] &a, const [float3] &b, const [float3] &c, float [epsilon] = 1[e]-3f);642 643 644 645 646 static [MUST_USE_RESULT] [float3] [RandomDir]([LCG] &lcg, float length = 1.f);647 648 649 650 static [MUST_USE_RESULT] [float3] [RandomSphere]([LCG] &lcg, const [float3] ¢er, float radius);651 652 653 654 static [MUST_USE_RESULT] [float3] [RandomBox]([LCG] &lcg, float xmin, float xmax, float ymin, float ymax, float zmin, float zmax);655 static [MUST_USE_RESULT] [float3] [RandomBox]([LCG] &lcg, const [float3] &minValues, const [float3] &maxValues);656 657 658 static [MUST_USE_RESULT] [float3] [RandomBox]([LCG] &lcg, float minElem, float maxElem);659 660 661 static inline [float3] [RandomGeneral]([LCG] &lcg, float minElem, float maxElem) { return [RandomBox](lcg, minElem, maxElem); }662 663 664 665 666 static const [float3] [zero];667 668 669 670 static const [float3] [one];671 672 673 674 static const [float3] [unitX];675 676 677 678 static const [float3] [unitY];679 680 681 682 static const [float3] [unitZ];683 684 685 686 687 688 689 static const [float3] [nan];690 691 692 693 static const [float3] [inf];694 695 #ifdef MATH_OGRE_INTEROP696 [float3](const Ogre::Vector3 &other):[x](other.[x]), [y](other.[y]), [z](other.[z]) {}697 operator Ogre::Vector3() const { return Ogre::Vector3([x], [y], [z]); }698 #endif699 #ifdef MATH_QT_INTEROP700 [float3](const QVector3D &other):[x](other.[x]()), [y](other.[y]()), [z](other.[z]()) {}701 operator QVector3D() const { return QVector3D([x], [y], [z]); }702 operator QString() const { return "float3(" + QString::number([x]) + "," + QString::number([y]) + "," + QString::number([z]) + ")"; }703 QString toString() const { return (QString)*this; }704 QVector3D ToQVector3D() const { return QVector3D([x], [y], [z]); }705 static [float3] FromQVector3D(const QVector3D &v) { return ([float3])v; }706 static [float3] [FromString](const QString &str) { return [FromString](str.toStdString()); }707 #endif708 #ifdef MATH_BULLET_INTEROP709 [float3](const btVector3 &other):[x](other.[x]()), [y](other.[y]()), [z](other.[z]()) {}710 operator btVector3() const { return btVector3([x], [y], [z]); }711 #endif712 #ifdef MATH_URHO3D_INTEROP713 [float3](const Urho3D::Vector3 &other) : [x](other.x_), [y](other.y_), [z](other.z_) {}714 operator Urho3D::Vector3() const { return Urho3D::Vector3([x], [y], [z]); }715 #endif716 #ifdef MATH_IRRKLANG_INTEROP717 [float3](const irrklang::vec3df &other) : [x](other.X), [y](other.Y), [z](other.Z) {}718 operator irrklang::vec3df() const { return irrklang::vec3df([x], [y], [z]); }719 #endif720 };721 722 723 #ifdef MATH_ENABLE_STL_SUPPORT724 std::ostream &[operator <<](std::ostream &out, const [float3] &rhs);725 #endif726 727 [float3] [operator *](float scalar, const [float3] &rhs);728 729 #ifdef MATH_ENABLE_UNCOMMON_OPERATIONS730 inline [float3] operator /(float scalar, const [float3] &rhs) { return [float3::FromScalar](scalar) / rhs; }731 #endif732 733 inline float [Dot](const [float3] &a, const [float3] &b) { return a.[Dot](b); }734 inline [float3] [Cross](const [float3] &a, const [float3] &b) { return a.[Cross](b); }735 inline [float3] [Abs](const [float3] &a) { return a.[Abs](); }736 inline float [Length](const [float3] &a) { return a.[Length](); }737 inline float [Distance](const [float3] &a, const [float3] &b) { return a.[Distance](b); }738 inline [float3] [Min](const [float3] &a, const [float3] &b) { return a.[Min](b); }739 inline [float3] [Max](const [float3] &a, const [float3] &b) { return a.[Max](b); }740 inline [float3] [Clamp](const [float3] &a, float floor, float ceil) { return a.[Clamp](floor, ceil); }741 inline [float3] [Clamp](const [float3] &a, const [float3] &floor, const [float3] &ceil) { return a.[Clamp](floor, ceil); }742 inline [float3] [Clamp01](const [float3] &a) { return a.[Clamp01](); }743 inline [float3] [Lerp](const [float3] &a, const [float3] &b, float t) { return a.[Lerp](b, t); }744 745 #ifdef MATH_QT_INTEROP746 Q_DECLARE_METATYPE([float3])747 Q_DECLARE_METATYPE([float3]*)748 #endif749 750 [MATH_END_NAMESPACE]751 752 #include "[float4.h]"753 #include "[MathFunc.h]"754 755 [MATH_BEGIN_NAMESPACE]756 757 #ifdef MATH_AUTOMATIC_SSE758 bool [EqualAbs](float a, float b, float [epsilon]);759 760 #define POINT_VEC(...) float4(__VA_ARGS__, 1.f)761 #define DIR_VEC(...) float4(__VA_ARGS__, 0.f)762 763 #define POINT_VEC_SCALAR(s) float4(pos_from_scalar_ps(s))764 #define DIR_VEC_SCALAR(s) float4(dir_from_scalar_ps(s))765 766 #define POINT_TO_FLOAT3(v) (v).xyz()767 #define DIR_TO_FLOAT3(v) (v).xyz()768 769 #define POINT_TO_FLOAT4(v) (v)770 #define DIR_TO_FLOAT4(v) (v)771 772 #define FLOAT4_TO_POINT(v) (v)773 #define FLOAT4_TO_DIR(v) (v)774 775 776 777 778 779 780 781 782 783 784 785 786 787 #else788 #define POINT_VEC(...) float3(__VA_ARGS__)789 #define DIR_VEC(...) float3(__VA_ARGS__)790 #define POINT_TO_FLOAT3(x) x791 #define DIR_TO_FLOAT3(x) x792 #define POINT_VEC_SCALAR(s) float3::FromScalar(s)793 #define DIR_VEC_SCALAR(s) float3::FromScalar(s)794 #define POINT_TO_FLOAT4(v) float4(v, 1.f)795 #define DIR_TO_FLOAT4(v) float4(v, 0.f)796 #define FLOAT4_TO_POINT(v) (v).xyz()797 #define FLOAT4_TO_DIR(v) (v).xyz()798 799 #endif800 801 vec [PointVecFromString](const char *str, const char **outEndStr = 0);802 vec [DirVecFromString](const char *str, const char **outEndStr = 0);803 804 [MATH_END_NAMESPACE] Go back to previous page