1 /////////////////////////////////////////////////////////////////////////////////// 2 /// OpenGL Mathematics (glm.g-truc.net) 3 /// 4 /// Copyright (c) 2005 - 2014 G-Truc Creation (www.g-truc.net) 5 /// Permission is hereby granted, free of charge, to any person obtaining a copy 6 /// of this software and associated documentation files (the "Software"), to deal 7 /// in the Software without restriction, including without limitation the rights 8 /// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 /// copies of the Software, and to permit persons to whom the Software is 10 /// furnished to do so, subject to the following conditions: 11 /// 12 /// The above copyright notice and this permission notice shall be included in 13 /// all copies or substantial portions of the Software. 14 /// 15 /// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 /// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 /// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 /// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 /// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 /// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 21 /// THE SOFTWARE. 22 /// 23 /// @ref core 24 /// @file glm/core/type_tvec2.inl 25 /// @date 2008-08-18 / 2011-06-15 26 /// @author Christophe Riccio 27 /////////////////////////////////////////////////////////////////////////////////// 28 29 namespace glm{ 30 namespace detail 31 { 32 template <typename T, precision P> 33 GLM_FUNC_QUALIFIER GLM_CONSTEXPR length_t tvec2<T, P>::length() const 34 { 35 return 2; 36 } 37 38 ////////////////////////////////////// 39 // Accesses 40 41 template <typename T, precision P> 42 GLM_FUNC_QUALIFIER T & tvec2<T, P>::operator[](length_t i) 43 { 44 assert(i >= 0 && i < this->length()); 45 return (&x)[i]; 46 } 47 48 template <typename T, precision P> 49 GLM_FUNC_QUALIFIER T const & tvec2<T, P>::operator[](length_t i) const 50 { 51 assert(i >= 0 && i < this->length()); 52 return (&x)[i]; 53 } 54 55 ////////////////////////////////////// 56 // Implicit basic constructors 57 58 template <typename T, precision P> 59 GLM_FUNC_QUALIFIER tvec2<T, P>::tvec2() : 60 x(0), 61 y(0) 62 {} 63 64 template <typename T, precision P> 65 GLM_FUNC_QUALIFIER tvec2<T, P>::tvec2(tvec2<T, P> const & v) : 66 x(v.x), 67 y(v.y) 68 {} 69 70 template <typename T, precision P> 71 template <precision Q> 72 GLM_FUNC_QUALIFIER tvec2<T, P>::tvec2(tvec2<T, Q> const & v) : 73 x(v.x), 74 y(v.y) 75 {} 76 77 ////////////////////////////////////// 78 // Explicit basic constructors 79 80 template <typename T, precision P> 81 GLM_FUNC_QUALIFIER tvec2<T, P>::tvec2(ctor) 82 {} 83 84 template <typename T, precision P> 85 GLM_FUNC_QUALIFIER tvec2<T, P>::tvec2(T const & s) : 86 x(s), 87 y(s) 88 {} 89 90 template <typename T, precision P> 91 GLM_FUNC_QUALIFIER tvec2<T, P>::tvec2 92 ( 93 T const & s1, 94 T const & s2 95 ) : 96 x(s1), 97 y(s2) 98 {} 99 100 ////////////////////////////////////// 101 // Conversion scalar constructors 102 103 template <typename T, precision P> 104 template <typename U, typename V> 105 GLM_FUNC_QUALIFIER tvec2<T, P>::tvec2 106 ( 107 U const & a, 108 V const & b 109 ) : 110 x(static_cast<T>(a)), 111 y(static_cast<T>(b)) 112 {} 113 114 ////////////////////////////////////// 115 // Conversion vector constructors 116 117 template <typename T, precision P> 118 template <typename U, precision Q> 119 GLM_FUNC_QUALIFIER tvec2<T, P>::tvec2 120 ( 121 tvec2<U, Q> const & v 122 ) : 123 x(static_cast<T>(v.x)), 124 y(static_cast<T>(v.y)) 125 {} 126 127 template <typename T, precision P> 128 template <typename U, precision Q> 129 GLM_FUNC_QUALIFIER tvec2<T, P>::tvec2 130 ( 131 tvec3<U, Q> const & v 132 ) : 133 x(static_cast<T>(v.x)), 134 y(static_cast<T>(v.y)) 135 {} 136 137 template <typename T, precision P> 138 template <typename U, precision Q> 139 GLM_FUNC_QUALIFIER tvec2<T, P>::tvec2 140 ( 141 tvec4<U, Q> const & v 142 ) : 143 x(static_cast<T>(v.x)), 144 y(static_cast<T>(v.y)) 145 {} 146 147 ////////////////////////////////////// 148 // Unary arithmetic operators 149 150 template <typename T, precision P> 151 GLM_FUNC_QUALIFIER tvec2<T, P> & tvec2<T, P>::operator= 152 ( 153 tvec2<T, P> const & v 154 ) 155 { 156 this->x = v.x; 157 this->y = v.y; 158 return *this; 159 } 160 161 template <typename T, precision P> 162 template <typename U> 163 GLM_FUNC_QUALIFIER tvec2<T, P> & tvec2<T, P>::operator= 164 ( 165 tvec2<U, P> const & v 166 ) 167 { 168 this->x = static_cast<T>(v.x); 169 this->y = static_cast<T>(v.y); 170 return *this; 171 } 172 173 template <typename T, precision P> 174 template <typename U> 175 GLM_FUNC_QUALIFIER tvec2<T, P> & tvec2<T, P>::operator+= 176 ( 177 U s 178 ) 179 { 180 this->x += static_cast<T>(s); 181 this->y += static_cast<T>(s); 182 return *this; 183 } 184 185 template <typename T, precision P> 186 template <typename U> 187 GLM_FUNC_QUALIFIER tvec2<T, P> & tvec2<T, P>::operator+= 188 ( 189 tvec2<U, P> const & v 190 ) 191 { 192 this->x += static_cast<T>(v.x); 193 this->y += static_cast<T>(v.y); 194 return *this; 195 } 196 197 template <typename T, precision P> 198 template <typename U> 199 GLM_FUNC_QUALIFIER tvec2<T, P> & tvec2<T, P>::operator-= 200 ( 201 U s 202 ) 203 { 204 this->x -= static_cast<T>(s); 205 this->y -= static_cast<T>(s); 206 return *this; 207 } 208 209 template <typename T, precision P> 210 template <typename U> 211 GLM_FUNC_QUALIFIER tvec2<T, P> & tvec2<T, P>::operator-= 212 ( 213 tvec2<U, P> const & v 214 ) 215 { 216 this->x -= static_cast<T>(v.x); 217 this->y -= static_cast<T>(v.y); 218 return *this; 219 } 220 221 template <typename T, precision P> 222 template <typename U> 223 GLM_FUNC_QUALIFIER tvec2<T, P> & tvec2<T, P>::operator*= 224 ( 225 U s 226 ) 227 { 228 this->x *= static_cast<T>(s); 229 this->y *= static_cast<T>(s); 230 return *this; 231 } 232 233 template <typename T, precision P> 234 template <typename U> 235 GLM_FUNC_QUALIFIER tvec2<T, P> & tvec2<T, P>::operator*= 236 ( 237 tvec2<U, P> const & v 238 ) 239 { 240 this->x *= static_cast<T>(v.x); 241 this->y *= static_cast<T>(v.y); 242 return *this; 243 } 244 245 template <typename T, precision P> 246 template <typename U> 247 GLM_FUNC_QUALIFIER tvec2<T, P> & tvec2<T, P>::operator/= 248 ( 249 U s 250 ) 251 { 252 this->x /= static_cast<T>(s); 253 this->y /= static_cast<T>(s); 254 return *this; 255 } 256 257 template <typename T, precision P> 258 template <typename U> 259 GLM_FUNC_QUALIFIER tvec2<T, P> & tvec2<T, P>::operator/= 260 ( 261 tvec2<U, P> const & v 262 ) 263 { 264 this->x /= static_cast<T>(v.x); 265 this->y /= static_cast<T>(v.y); 266 return *this; 267 } 268 269 ////////////////////////////////////// 270 // Increment and decrement operators 271 272 template <typename T, precision P> 273 GLM_FUNC_QUALIFIER tvec2<T, P> & tvec2<T, P>::operator++() 274 { 275 ++this->x; 276 ++this->y; 277 return *this; 278 } 279 280 template <typename T, precision P> 281 GLM_FUNC_QUALIFIER tvec2<T, P> & tvec2<T, P>::operator--() 282 { 283 --this->x; 284 --this->y; 285 return *this; 286 } 287 288 template <typename T, precision P> 289 GLM_FUNC_QUALIFIER tvec2<T, P> tvec2<T, P>::operator++(int) 290 { 291 tvec2<T, P> Result(*this); 292 ++*this; 293 return Result; 294 } 295 296 template <typename T, precision P> 297 GLM_FUNC_QUALIFIER tvec2<T, P> tvec2<T, P>::operator--(int) 298 { 299 tvec2<T, P> Result(*this); 300 --*this; 301 return Result; 302 } 303 304 ////////////////////////////////////// 305 // Boolean operators 306 307 template <typename T, precision P> 308 GLM_FUNC_QUALIFIER bool operator== 309 ( 310 tvec2<T, P> const & v1, 311 tvec2<T, P> const & v2 312 ) 313 { 314 return (v1.x == v2.x) && (v1.y == v2.y); 315 } 316 317 template <typename T, precision P> 318 GLM_FUNC_QUALIFIER bool operator!= 319 ( 320 tvec2<T, P> const & v1, 321 tvec2<T, P> const & v2 322 ) 323 { 324 return (v1.x != v2.x) || (v1.y != v2.y); 325 } 326 327 ////////////////////////////////////// 328 // Unary bit operators 329 330 template <typename T, precision P> 331 template <typename U> 332 GLM_FUNC_QUALIFIER tvec2<T, P> & tvec2<T, P>::operator%= (U s) 333 { 334 this->x %= static_cast<T>(s); 335 this->y %= static_cast<T>(s); 336 return *this; 337 } 338 339 template <typename T, precision P> 340 template <typename U> 341 GLM_FUNC_QUALIFIER tvec2<T, P> & tvec2<T, P>::operator%= (tvec2<U, P> const & v) 342 { 343 this->x %= static_cast<T>(v.x); 344 this->y %= static_cast<T>(v.y); 345 return *this; 346 } 347 348 template <typename T, precision P> 349 template <typename U> 350 GLM_FUNC_QUALIFIER tvec2<T, P> & tvec2<T, P>::operator&= (U s) 351 { 352 this->x &= static_cast<T>(s); 353 this->y &= static_cast<T>(s); 354 return *this; 355 } 356 357 template <typename T, precision P> 358 template <typename U> 359 GLM_FUNC_QUALIFIER tvec2<T, P> & tvec2<T, P>::operator&= (tvec2<U, P> const & v) 360 { 361 this->x &= static_cast<T>(v.x); 362 this->y &= static_cast<T>(v.y); 363 return *this; 364 } 365 366 template <typename T, precision P> 367 template <typename U> 368 GLM_FUNC_QUALIFIER tvec2<T, P> & tvec2<T, P>::operator|= (U s) 369 { 370 this->x |= static_cast<T>(s); 371 this->y |= static_cast<T>(s); 372 return *this; 373 } 374 375 template <typename T, precision P> 376 template <typename U> 377 GLM_FUNC_QUALIFIER tvec2<T, P> & tvec2<T, P>::operator|= (tvec2<U, P> const & v) 378 { 379 this->x |= static_cast<T>(v.x); 380 this->y |= static_cast<T>(v.y); 381 return *this; 382 } 383 384 template <typename T, precision P> 385 template <typename U> 386 GLM_FUNC_QUALIFIER tvec2<T, P> & tvec2<T, P>::operator^= (U s) 387 { 388 this->x ^= static_cast<T>(s); 389 this->y ^= static_cast<T>(s); 390 return *this; 391 } 392 393 template <typename T, precision P> 394 template <typename U> 395 GLM_FUNC_QUALIFIER tvec2<T, P> & tvec2<T, P>::operator^= (tvec2<U, P> const & v) 396 { 397 this->x ^= static_cast<T>(v.x); 398 this->y ^= static_cast<T>(v.y); 399 return *this; 400 } 401 402 template <typename T, precision P> 403 template <typename U> 404 GLM_FUNC_QUALIFIER tvec2<T, P> & tvec2<T, P>::operator<<= (U s) 405 { 406 this->x <<= static_cast<T>(s); 407 this->y <<= static_cast<T>(s); 408 return *this; 409 } 410 411 template <typename T, precision P> 412 template <typename U> 413 GLM_FUNC_QUALIFIER tvec2<T, P> & tvec2<T, P>::operator<<= (tvec2<U, P> const & v) 414 { 415 this->x <<= static_cast<T>(v.x); 416 this->y <<= static_cast<T>(v.y); 417 return *this; 418 } 419 420 template <typename T, precision P> 421 template <typename U> 422 GLM_FUNC_QUALIFIER tvec2<T, P> & tvec2<T, P>::operator>>= (U s) 423 { 424 this->x >>= static_cast<T>(s); 425 this->y >>= static_cast<T>(s); 426 return *this; 427 } 428 429 template <typename T, precision P> 430 template <typename U> 431 GLM_FUNC_QUALIFIER tvec2<T, P> & tvec2<T, P>::operator>>= (tvec2<U, P> const & v) 432 { 433 this->x >>= static_cast<T>(v.x); 434 this->y >>= static_cast<T>(v.y); 435 return *this; 436 } 437 438 ////////////////////////////////////// 439 // Binary arithmetic operators 440 441 template <typename T, precision P> 442 GLM_FUNC_QUALIFIER tvec2<T, P> operator+ 443 ( 444 tvec2<T, P> const & v, 445 T const & s 446 ) 447 { 448 return tvec2<T, P>( 449 v.x + s, 450 v.y + s); 451 } 452 453 template <typename T, precision P> 454 GLM_FUNC_QUALIFIER tvec2<T, P> operator+ 455 ( 456 T const & s, 457 tvec2<T, P> const & v 458 ) 459 { 460 return tvec2<T, P>( 461 s + v.x, 462 s + v.y); 463 } 464 465 template <typename T, precision P> 466 GLM_FUNC_QUALIFIER tvec2<T, P> operator+ 467 ( 468 tvec2<T, P> const & v1, 469 tvec2<T, P> const & v2 470 ) 471 { 472 return tvec2<T, P>( 473 v1.x + v2.x, 474 v1.y + v2.y); 475 } 476 477 //operator- 478 template <typename T, precision P> 479 GLM_FUNC_QUALIFIER tvec2<T, P> operator- 480 ( 481 tvec2<T, P> const & v, 482 T const & s 483 ) 484 { 485 return tvec2<T, P>( 486 v.x - s, 487 v.y - s); 488 } 489 490 template <typename T, precision P> 491 GLM_FUNC_QUALIFIER tvec2<T, P> operator- 492 ( 493 T const & s, 494 tvec2<T, P> const & v 495 ) 496 { 497 return tvec2<T, P>( 498 s - v.x, 499 s - v.y); 500 } 501 502 template <typename T, precision P> 503 GLM_FUNC_QUALIFIER tvec2<T, P> operator- 504 ( 505 tvec2<T, P> const & v1, 506 tvec2<T, P> const & v2 507 ) 508 { 509 return tvec2<T, P>( 510 v1.x - v2.x, 511 v1.y - v2.y); 512 } 513 514 //operator* 515 template <typename T, precision P> 516 GLM_FUNC_QUALIFIER tvec2<T, P> operator* 517 ( 518 tvec2<T, P> const & v, 519 T const & s 520 ) 521 { 522 return tvec2<T, P>( 523 v.x * s, 524 v.y * s); 525 } 526 527 template <typename T, precision P> 528 GLM_FUNC_QUALIFIER tvec2<T, P> operator* 529 ( 530 T const & s, 531 tvec2<T, P> const & v 532 ) 533 { 534 return tvec2<T, P>( 535 s * v.x, 536 s * v.y); 537 } 538 539 template <typename T, precision P> 540 GLM_FUNC_QUALIFIER tvec2<T, P> operator* 541 ( 542 tvec2<T, P> const & v1, 543 tvec2<T, P> const & v2 544 ) 545 { 546 return tvec2<T, P>( 547 v1.x * v2.x, 548 v1.y * v2.y); 549 } 550 551 //operator/ 552 template <typename T, precision P> 553 GLM_FUNC_QUALIFIER tvec2<T, P> operator/ 554 ( 555 tvec2<T, P> const & v, 556 T const & s 557 ) 558 { 559 return tvec2<T, P>( 560 v.x / s, 561 v.y / s); 562 } 563 564 template <typename T, precision P> 565 GLM_FUNC_QUALIFIER tvec2<T, P> operator/ 566 ( 567 T const & s, 568 tvec2<T, P> const & v 569 ) 570 { 571 return tvec2<T, P>( 572 s / v.x, 573 s / v.y); 574 } 575 576 template <typename T, precision P> 577 GLM_FUNC_QUALIFIER tvec2<T, P> operator/ 578 ( 579 tvec2<T, P> const & v1, 580 tvec2<T, P> const & v2 581 ) 582 { 583 return tvec2<T, P>( 584 v1.x / v2.x, 585 v1.y / v2.y); 586 } 587 588 // Unary constant operators 589 template <typename T, precision P> 590 GLM_FUNC_QUALIFIER tvec2<T, P> operator- 591 ( 592 tvec2<T, P> const & v 593 ) 594 { 595 return tvec2<T, P>( 596 -v.x, 597 -v.y); 598 } 599 600 ////////////////////////////////////// 601 // Binary bit operators 602 603 template <typename T, precision P> 604 GLM_FUNC_QUALIFIER tvec2<T, P> operator% 605 ( 606 tvec2<T, P> const & v, 607 T const & s 608 ) 609 { 610 return tvec2<T, P>( 611 v.x % s, 612 v.y % s); 613 } 614 615 template <typename T, precision P> 616 GLM_FUNC_QUALIFIER tvec2<T, P> operator% 617 ( 618 T const & s, 619 tvec2<T, P> const & v 620 ) 621 { 622 return tvec2<T, P>( 623 s % v.x, 624 s % v.y); 625 } 626 627 template <typename T, precision P> 628 GLM_FUNC_QUALIFIER tvec2<T, P> operator% 629 ( 630 tvec2<T, P> const & v1, 631 tvec2<T, P> const & v2 632 ) 633 { 634 return tvec2<T, P>( 635 v1.x % v2.x, 636 v1.y % v2.y); 637 } 638 639 template <typename T, precision P> 640 GLM_FUNC_QUALIFIER tvec2<T, P> operator& 641 ( 642 tvec2<T, P> const & v, 643 T const & s 644 ) 645 { 646 return tvec2<T, P>( 647 v.x & s, 648 v.y & s); 649 } 650 651 template <typename T, precision P> 652 GLM_FUNC_QUALIFIER tvec2<T, P> operator& 653 ( 654 T const & s, 655 tvec2<T, P> const & v 656 ) 657 { 658 return tvec2<T, P>( 659 s & v.x, 660 s & v.y); 661 } 662 663 template <typename T, precision P> 664 GLM_FUNC_QUALIFIER tvec2<T, P> operator& 665 ( 666 tvec2<T, P> const & v1, 667 tvec2<T, P> const & v2 668 ) 669 { 670 return tvec2<T, P>( 671 v1.x & v2.x, 672 v1.y & v2.y); 673 } 674 675 template <typename T, precision P> 676 GLM_FUNC_QUALIFIER tvec2<T, P> operator| 677 ( 678 tvec2<T, P> const & v, 679 T const & s 680 ) 681 { 682 return tvec2<T, P>( 683 v.x | s, 684 v.y | s); 685 } 686 687 template <typename T, precision P> 688 GLM_FUNC_QUALIFIER tvec2<T, P> operator| 689 ( 690 T const & s, 691 tvec2<T, P> const & v 692 ) 693 { 694 return tvec2<T, P>( 695 s | v.x, 696 s | v.y); 697 } 698 699 template <typename T, precision P> 700 GLM_FUNC_QUALIFIER tvec2<T, P> operator| 701 ( 702 tvec2<T, P> const & v1, 703 tvec2<T, P> const & v2 704 ) 705 { 706 return tvec2<T, P>( 707 v1.x | v2.x, 708 v1.y | v2.y); 709 } 710 711 template <typename T, precision P> 712 GLM_FUNC_QUALIFIER tvec2<T, P> operator^ 713 ( 714 tvec2<T, P> const & v, 715 T const & s 716 ) 717 { 718 return tvec2<T, P>( 719 v.x ^ s, 720 v.y ^ s); 721 } 722 723 template <typename T, precision P> 724 GLM_FUNC_QUALIFIER tvec2<T, P> operator^ 725 ( 726 T const & s, 727 tvec2<T, P> const & v 728 ) 729 { 730 return tvec2<T, P>( 731 s ^ v.x, 732 s ^ v.y); 733 } 734 735 template <typename T, precision P> 736 GLM_FUNC_QUALIFIER tvec2<T, P> operator^ 737 ( 738 tvec2<T, P> const & v1, 739 tvec2<T, P> const & v2 740 ) 741 { 742 return tvec2<T, P>( 743 v1.x ^ v2.x, 744 v1.y ^ v2.y); 745 } 746 747 template <typename T, precision P> 748 GLM_FUNC_QUALIFIER tvec2<T, P> operator<< 749 ( 750 tvec2<T, P> const & v, 751 T const & s 752 ) 753 { 754 return tvec2<T, P>( 755 v.x << s, 756 v.y << s); 757 } 758 759 template <typename T, precision P> 760 GLM_FUNC_QUALIFIER tvec2<T, P> operator<< 761 ( 762 T const & s, 763 tvec2<T, P> const & v 764 ) 765 { 766 return tvec2<T, P>( 767 s << v.x, 768 s << v.y); 769 } 770 771 template <typename T, precision P> 772 GLM_FUNC_QUALIFIER tvec2<T, P> operator<< 773 ( 774 tvec2<T, P> const & v1, 775 tvec2<T, P> const & v2 776 ) 777 { 778 return tvec2<T, P>( 779 v1.x << v2.x, 780 v1.y << v2.y); 781 } 782 783 template <typename T, precision P> 784 GLM_FUNC_QUALIFIER tvec2<T, P> operator>> 785 ( 786 tvec2<T, P> const & v, 787 T const & s 788 ) 789 { 790 return tvec2<T, P>( 791 v.x >> s, 792 v.y >> s); 793 } 794 795 template <typename T, precision P> 796 GLM_FUNC_QUALIFIER tvec2<T, P> operator>> 797 ( 798 T const & s, 799 tvec2<T, P> const & v 800 ) 801 { 802 return tvec2<T, P>( 803 s >> v.x, 804 s >> v.y); 805 } 806 807 template <typename T, precision P> 808 GLM_FUNC_QUALIFIER tvec2<T, P> operator>> 809 ( 810 tvec2<T, P> const & v1, 811 tvec2<T, P> const & v2 812 ) 813 { 814 return tvec2<T, P>( 815 v1.x >> v2.x, 816 v1.y >> v2.y); 817 } 818 819 template <typename T, precision P> 820 GLM_FUNC_QUALIFIER tvec2<T, P> operator~ 821 ( 822 tvec2<T, P> const & v 823 ) 824 { 825 return tvec2<T, P>( 826 ~v.x, 827 ~v.y); 828 } 829 830 }//namespace detail 831 }//namespace glm 832