rituals_math.cpp 20.5 KB
Newer Older
1 2 3 4 5 6 7 8 9 10
/* 
Copyright (c) 2016 William Bundy

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/

William Bundy's avatar
William Bundy committed
11 12 13 14 15
/*
 * rituals_math.cpp
 * where all the math and geometry 
 */ 

William Bundy's avatar
William Bundy committed
16

William Bundy's avatar
William Bundy committed
17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33
inline real clamp(real x, real min, real max)
{
	if(x < min) x = min;
	else if(x > max) x = max;
	return x;
}

real clamp_01(real x)
{
	return clamp(x, 0.0f, 1.0f);	
}

real lerp(real a, real b, real t)
{
	return (1.0f - t) * a + t * b;
}

William Bundy's avatar
William Bundy committed
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


#ifndef REFLECTED
union Vec2 
{
	struct {
		real x, y;
	};
	real e[2];
};

union Vec3
{
	struct {
		real x, y, z;
	};
	real e[3];
};

union Vec4
{
	struct {
		real x, y, z, w;
	};
	real e[4];
	
};

union Vec2i
{
	struct {
		integer x, y;
	};
	integer e[2];
};

union Vec3i
{
	struct {
		integer x, y, z;
	};
	integer e[3];
};

union Vec4i
{
	struct {
		integer x, y, z, w;
	};
	integer e[4];
	
};

#endif

/* v* functions */

static inline Vec2 v2(real x, real y)
{
	return Vec2{x, y};
}

static inline Vec3 v3(real x, real y, real z)
{
	return Vec3{
		x, y, z
	};
}

static inline Vec4 v4(real x, real y, real z, real w)
{
	return Vec4{
		x, y, z, w
	};
}

static inline Vec2i v2i(integer x, integer y)
{
	return Vec2i {
		x, y
	};
}

static inline Vec3i v3i(integer x, integer y, integer z)
{
	return Vec3i{
		x, y, z
	};
}

static inline Vec4i v4i(integer x, integer y, integer z, integer w)
{
	return Vec4i{
		x, y, z, w
	};
}

#ifdef __cplusplus

static inline Vec2 v2(Vec2i vi)
{
	return v2(vi.x, vi.y);
}

static inline Vec2 v2(Vec3 vi)
{
	return v2(vi.x, vi.y);
}

static inline Vec3 v3(Vec3i vi)
{
    return v3(vi.x, vi.y, vi.z);
}

static inline Vec3 v3(Vec2 vi)
{
    return v3(vi.x, vi.y, 0);
}

static inline Vec3 v3(Vec2 vi, real z)
{
    return v3(vi.x, vi.y, z);
}

static inline Vec4 v4(Vec4i vi)
{
    return v4(vi.x, vi.y, vi.z, vi.w);
}

static inline Vec4 v4(Vec2 vi)
{
    return v4(vi.x, vi.y, 0, 0);
}

static inline Vec4 v4(Vec2 vi, real z, real w)
{
    return v4(vi.x, vi.y, z, w);
}

static inline Vec4 v4(Vec3 vi)
{
    return v4(vi.x, vi.y, vi.z, 0);
}

static inline Vec4 v4(Vec3 vi, real w)
{
    return v4(vi.x, vi.y, vi.z, w);
}

static inline Vec2i v2i(Vec2 vi)
{
    return v2i((integer)vi.x, (integer)vi.y);
}

static inline Vec3i v3i(Vec3 vi)
{
    return v3i((integer)vi.x, (integer)vi.y, (integer)vi.z);
}

static inline Vec4i v4i(Vec4 vi)
{
    return v4i((integer)vi.x, (integer)vi.y, (integer)vi.z, (integer)vi.w);
}
#endif


/* Vector operators */


// Defined operators:
// Negation (-)
// Addition, subtraction (+, -)
// Scalar multiplication (*)
// Hadamard Product / Piecewise multiplication (*)
// Scalar division (*)

// Overloaded: Assignment versions (+=, -=, *=, /=, *=(v))

/* Vec2 */

static inline Vec2 v2_negate(Vec2 a)
{
	return Vec2{-a.x, -a.y};
}

static inline Vec2 v2_sub(Vec2 a, Vec2 b)
{
	return Vec2{a.x - b.x, a.y - b.y};
}

static inline Vec2 v2_add(Vec2 a, Vec2 b)
{
	return Vec2{a.x + b.x, a.y + b.y};
}

static inline Vec2 v2_add_scaled(Vec2 a, Vec2 b, real scale)
{
	return Vec2{a.x + b.x * scale, a.y + b.y * scale};
}


static inline Vec2 v2_mul(Vec2 a, real s)
{
	return Vec2{a.x * s, a.y * s};
}

static inline Vec2 v2_div(Vec2 a, real s)
{
	return v2_mul(a, 1.0f / s);
}

static inline Vec2 v2_component_mul(Vec2 a, Vec2 b)
{
	return Vec2{a.x * b.x, a.y * b.y};
}

/* Vec3 */

static inline Vec3 v3_negate(Vec3 a)
{
	return Vec3{-a.x, -a.y, -a.z};
}

static inline Vec3 v3_sub(Vec3 a, Vec3 b)
{
	return Vec3{a.x - b.x, a.y - b.y, a.z - b.z};
}

static inline Vec3 v3_add(Vec3 a, Vec3 b)
{
	return Vec3{a.x + b.x, a.y + b.y, a.z + b.z};
}

static inline Vec3 v3_add_scaled(Vec3 a, Vec3 b, real scale)
{
	return Vec3{a.x + b.x * scale, a.y + b.y * scale, a.z + b.z * scale};
}

static inline Vec3 v3_mul(Vec3 a, real s)
{
	return Vec3{a.x * s, a.y * s, a.z * s};
}

static inline Vec3 v3_div(Vec3 a, real s)
{
	return v3_mul(a, 1.0f/s);
}

static inline Vec3 v3_component_mul(Vec3 a, Vec3 b)
{
	return Vec3{a.x * b.x, a.y * b.y, a.z * b.z};
}

/* Vec4 */

static inline Vec4 v4_negate(Vec4 a)
{
    return Vec4{-a.x, -a.y, -a.z, -a.w};
}

static inline Vec4 v4_add(Vec4 a, Vec4 b)
{
	return Vec4{a.x + b.x, a.y + b.y, a.z + b.z, a.w + b.w};
}

static inline Vec4 v4_add_scaled(Vec4 a, Vec4 b, real scale)
{
	return Vec4{a.x + b.x * scale, a.y + b.y * scale, a.z + b.z * scale, a.w + b.w * scale};
}

static inline Vec4 v4_sub(Vec4 a, Vec4 b)
{
	return Vec4{a.x - b.x, a.y - b.y, a.z - b.z, a.w - b.w};
}

static inline Vec4 v4_compenent_mul(Vec4 a, Vec4 b)
{
	return Vec4{a.x * b.x, a.y * b.y, a.z * b.z, a.w * b.w};
}

static inline Vec4 v4_mul(Vec4 a, real b)
{
	return Vec4{a.x * b, a.y * b, a.z * b, a.w * b};
}

static inline Vec4 v4_div(Vec4 a, real b)
{
	return  v4_mul(a, 1.0f/b);
}

/* Vec2i */

static inline Vec2i v2i_negate(Vec2i a)
{
	return Vec2i{-a.x, -a.y};
}

static inline Vec2i v2i_sub(Vec2i a, Vec2i b)
{
	return Vec2i{a.x - b.x, a.y - b.y};
}

static inline Vec2i v2i_add(Vec2i a, Vec2i b)
{
	return Vec2i{a.x + b.x, a.y + b.y};
}

static inline Vec2i v2i_add_scaled(Vec2i a, Vec2i b, real scale)
{
	return Vec2i{a.x + (integer)(b.x * scale), a.y + (integer)(b.y * scale)};
}
static inline Vec2i v2i_addi_scaled(Vec2i a, Vec2i b, integer scale)
{
	return Vec2i{a.x + (integer)(b.x * scale), a.y + (integer)(b.y * scale)};
}

static inline Vec2i v2i_mul(Vec2i a, real s)
{
	return Vec2i{(integer)(a.x * s), (integer)(a.y * s)};
}

static inline Vec2i v2i_muli(Vec2i a, integer s)
{
	return Vec2i{(integer)(a.x * s), (integer)(a.y * s)};
}

static inline Vec2i v2i_div(Vec2i a, real s)
{
	return v2i_mul(a, 1.0f / s);
}

static inline Vec2i v2i_divi(Vec2i a, integer s)
{
	return Vec2i{a.x / s, a.y / s};
}


static inline Vec2i v2i_component_mul(Vec2i a, Vec2i b)
{
	return Vec2i{a.x * b.x, a.y * b.y};
}

/* Vec3i */

static inline Vec3i v3i_negate(Vec3i a)
{
	return Vec3i{-a.x, -a.y, -a.z};
}

static inline Vec3i v3i_sub(Vec3i a, Vec3i b)
{
	return Vec3i{a.x - b.x, a.y - b.y, a.z - b.z};
}

static inline Vec3i v3i_add(Vec3i a, Vec3i b)
{
	return Vec3i{a.x + b.x, a.y + b.y, a.z + b.z};
}

static inline Vec3i v3i_add_scaled(Vec3i a, Vec3i b, real scale)
{
	return Vec3i{a.x + (integer)(b.x * scale), a.y + (integer)(b.y * scale), a.z + (integer)(b.z * scale)};
}

static inline Vec3i v3i_addi_scaled(Vec3i a, Vec3i b, integer scale)
{
	return Vec3i{a.x + (integer)(b.x * scale), a.y + (integer)(b.y * scale), a.z + (integer)(b.z * scale)};
}

static inline Vec3i v3i_mul(Vec3i a, real s)
{
	return Vec3i{(integer)(a.x * s), (integer)(a.y * s), (integer)(a.z * s)};
}

static inline Vec3i v3i_muli(Vec3i a, integer s)
{
	return Vec3i{a.x * s, a.y * s, a.z * s};
}

static inline Vec3i v3i_div(Vec3i a, real s)
{
	return v3i_mul(a, 1.0f/s);
}

static inline Vec3i v3i_divi(Vec3i a, integer s)
{
	return Vec3i{a.x / s, a.y / s, a.z / s};
}


static inline Vec3i v3i_component_mul(Vec3i a, Vec3i b)
{
	return Vec3i{a.x * b.x, a.y * b.y, a.z * b.z};
}

/* Vec4i */

static inline Vec4i v4i_negate(Vec4i a)
{
    return Vec4i{-a.x, -a.y, -a.z, -a.w};
}

static inline Vec4i v4i_add(Vec4i a, Vec4i b)
{
	return Vec4i{a.x + b.x, a.y + b.y, a.z + b.z, a.w + b.w};
}

static inline Vec4i v4i_add_scaled(Vec4i a, Vec4i b, real scale)
{
	return Vec4i{a.x + (integer)(b.x * scale), a.y + (integer)(b.y * scale), a.z + (integer)(b.z * scale), a.w + (integer)(b.w * scale)};
}

static inline Vec4i v4i_addi_scaled(Vec4i a, Vec4i b, integer scale)
{
	return Vec4i{a.x + (integer)(b.x * scale), a.y + (integer)(b.y * scale), a.z + (integer)(b.z * scale), a.w + (integer)(b.w * scale)};
}

static inline Vec4i v4i_sub(Vec4i a, Vec4i b)
{
	return Vec4i{a.x - b.x, a.y - b.y, a.z - b.z, a.w - b.w};
}

static inline Vec4i v4i_compenent_mul(Vec4i a, Vec4i b)
{
	return Vec4i{a.x * b.x, a.y * b.y, a.z * b.z, a.w * b.w};
}

static inline Vec4i v4i_mul(Vec4i a, real b)
{
	return Vec4i{(integer)(a.x * b), (integer)(a.y * b), (integer)(a.z * b), (integer)(a.w * b)};
}
static inline Vec4i v4i_muli(Vec4i a, integer b)
{
	return Vec4i{(integer)(a.x * b), (integer)(a.y * b), (integer)(a.z * b), (integer)(a.w * b)};
}

static inline Vec4i v4i_divi(Vec4i a, real b)
{
	return Vec4i{(integer)(a.x / b), (integer)(a.y / b), (integer)(a.z / b), (integer)(a.w / b)};
}

static inline Vec4i v4i_div(Vec4i a, real b)
{
	return  v4i_mul(a, 1.0f/b);
}

#ifdef __cplusplus
/* Operator Overloads */

/* Vec2 */

static inline Vec2 operator-(Vec2 a)
{
	return Vec2{-a.x, -a.y};
}

static inline Vec2 operator-(Vec2 a, Vec2 b)
{
	return Vec2{a.x - b.x, a.y - b.y};
}

static inline Vec2 operator+(Vec2 a, Vec2 b)
{
	return Vec2{a.x + b.x, a.y + b.y};
}

static inline Vec2 operator*(Vec2 a, real s)
{
	return Vec2{a.x * s, a.y * s};
}

static inline Vec2 operator*(real s, Vec2 a)
{
	return Vec2{a.x * s, a.y * s};
}

static inline Vec2 operator/(Vec2 a, real s)
{
	return a * (1.0f / s);
}

static inline Vec2 operator*(Vec2 a, Vec2 b)
{
	return Vec2{a.x * b.x, a.y * b.y};
}

static inline Vec2& operator+=(Vec2& a, Vec2 b)
{
	a = a + b;
	return a;
}

static inline Vec2& operator-=(Vec2& a, Vec2 b)
{
	a = a - b;
	return a;
}

static inline Vec2& operator*=(Vec2& a, real b)
{
	a = a * b;
	return a;
}

static inline Vec2& operator*=(Vec2& a, Vec2 b)
{
	a = a * b;
	return a;
}

static inline Vec2& operator/=(Vec2& a, real b)
{
	a = a / b;
	return a;
}

/* Vec3 */

static inline Vec3 operator-(Vec3 a)
{
	return Vec3{-a.x, -a.y, -a.z};
}

static inline Vec3 operator-(Vec3 a, Vec3 b)
{
	return Vec3{a.x - b.x, a.y - b.y, a.z - b.z};
}

static inline Vec3 operator+(Vec3 a, Vec3 b)
{
	return Vec3{a.x + b.x, a.y + b.y, a.z + b.z};
}

static inline Vec3 operator*(Vec3 a, real s)
{
	return Vec3{a.x * s, a.y * s, a.z * s};
}

static inline Vec3 operator*(real s, Vec3 a)
{
	return Vec3{a.x * s, a.y * s, a.z * s};
}

static inline Vec3 operator/(Vec3 a, real s)
{
	return a * (1.0f / s);
}

static inline Vec3 operator*(Vec3 a, Vec3 b)
{
	return Vec3{a.x * b.x, a.y * b.y, a.z * b.z};
}

static inline Vec3& operator+=(Vec3& a, Vec3 b)
{
	a = a + b;
	return a;
}

static inline Vec3& operator-=(Vec3& a, Vec3 b)
{
	a = a - b;
	return a;
}

static inline Vec3& operator*=(Vec3& a, real b)
{
	a = a * b;
	return a;
}

static inline Vec3& operator*=(Vec3& a, Vec3 b)
{
	a = a * b;
	return a;
}

static inline Vec3& operator/=(Vec3& a, real b)
{
	a = a / b;
	return a;
}


/* Vec4 */

static inline Vec4 operator-(Vec4 a)
{
    return Vec4{-a.x, -a.y, -a.z, -a.w};
}

static inline Vec4 operator+(Vec4 a, Vec4 b)
{
	return Vec4{a.x + b.x, a.y + b.y, a.z + b.z, a.w + b.w};
}

static inline Vec4 operator-(Vec4 a, Vec4 b)
{
	return Vec4{a.x - b.x, a.y - b.y, a.z - b.z, a.w - b.w};
}

static inline Vec4 operator*(Vec4 a, Vec4 b)
{
	return Vec4{a.x * b.x, a.y * b.y, a.z * b.z, a.w * b.w};
}

static inline Vec4 operator*(Vec4 a, real b)
{
	return Vec4{a.x * b, a.y * b, a.z * b, a.w * b};
}
static inline Vec4 operator*(real b, Vec4 a) 
{
	return a * b;
}
static inline Vec4 operator/(Vec4 a, real b)
{
	return a * (1.0f/b);
}

static inline Vec4 operator+=(Vec4& a, Vec4 b)
{
    a = a + b;
    return a;
}

static inline Vec4 operator-=(Vec4& a, Vec4 b)
{
    a = a - b;
    return a;
}

static inline Vec4 operator*=(Vec4& a, real b)
{
	a = a * b;
	return a;
}

static inline Vec4 operator/=(Vec4& a, real b)
{
	a = a / b;
	return a;
}


static inline Vec4 operator*=(Vec4& a, Vec4 b)
{
    a = a * b;
    return a;
}

/* Vec2i */

static inline Vec2i operator-(Vec2i a)
{
	return Vec2i{-a.x, -a.y};
}

static inline Vec2i operator-(Vec2i a, Vec2i b)
{
	return Vec2i{a.x - b.x, a.y - b.y};
}

static inline Vec2i operator+(Vec2i a, Vec2i b)
{
	return Vec2i{a.x + b.x, a.y + b.y};
}

static inline Vec2i operator*(Vec2i a, integer s)
{
	return Vec2i{a.x * s, a.y * s};
}

static inline Vec2i operator*(integer s, Vec2i a)
{
	return Vec2i{a.x * s, a.y * s};
}

static inline Vec2i operator/(Vec2i a, integer s)
{
	return a * (1.0f / s);
}

static inline Vec2i operator*(Vec2i a, Vec2i b)
{
	return Vec2i{a.x * b.x, a.y * b.y};
}

static inline Vec2i& operator+=(Vec2i& a, Vec2i b)
{
	a = a + b;
	return a;
}

static inline Vec2i& operator-=(Vec2i& a, Vec2i b)
{
	a = a - b;
	return a;
}

static inline Vec2i& operator*=(Vec2i& a, integer b)
{
	a = a * b;
	return a;
}

static inline Vec2i& operator*=(Vec2i& a, Vec2i b)
{
	a = a * b;
	return a;
}

static inline Vec2i& operator/=(Vec2i& a, integer b)
William Bundy's avatar
William Bundy committed
746
{
William Bundy's avatar
William Bundy committed
747 748 749
	a = a / b;
	return a;
}
William Bundy's avatar
William Bundy committed
750

William Bundy's avatar
William Bundy committed
751
/* Vec3i */
William Bundy's avatar
William Bundy committed
752

William Bundy's avatar
William Bundy committed
753
static inline Vec3i operator-(Vec3i a)
William Bundy's avatar
William Bundy committed
754
{
William Bundy's avatar
William Bundy committed
755
	return Vec3i{-a.x, -a.y, -a.z};
William Bundy's avatar
William Bundy committed
756 757
}

William Bundy's avatar
William Bundy committed
758
static inline Vec3i operator-(Vec3i a, Vec3i b)
William Bundy's avatar
William Bundy committed
759
{
William Bundy's avatar
William Bundy committed
760
	return Vec3i{a.x - b.x, a.y - b.y, a.z - b.z};
William Bundy's avatar
William Bundy committed
761 762
}

William Bundy's avatar
William Bundy committed
763
static inline Vec3i operator+(Vec3i a, Vec3i b)
William Bundy's avatar
William Bundy committed
764
{
William Bundy's avatar
William Bundy committed
765
	return Vec3i{a.x + b.x, a.y + b.y, a.z + b.z};
William Bundy's avatar
William Bundy committed
766 767
}

William Bundy's avatar
William Bundy committed
768
static inline Vec3i operator*(Vec3i a, integer s)
William Bundy's avatar
William Bundy committed
769
{
William Bundy's avatar
William Bundy committed
770
	return Vec3i{a.x * s, a.y * s, a.z * s};
William Bundy's avatar
William Bundy committed
771 772
}

William Bundy's avatar
William Bundy committed
773
static inline Vec3i operator*(integer s, Vec3i a)
William Bundy's avatar
William Bundy committed
774
{
William Bundy's avatar
William Bundy committed
775
	return Vec3i{a.x * s, a.y * s, a.z * s};
William Bundy's avatar
William Bundy committed
776 777
}

William Bundy's avatar
William Bundy committed
778
static inline Vec3i operator/(Vec3i a, integer s)
William Bundy's avatar
William Bundy committed
779
{
William Bundy's avatar
William Bundy committed
780
	return a * (1.0f / s);
William Bundy's avatar
William Bundy committed
781 782
}

William Bundy's avatar
William Bundy committed
783
static inline Vec3i operator*(Vec3i a, Vec3i b)
William Bundy's avatar
William Bundy committed
784
{
William Bundy's avatar
William Bundy committed
785
	return Vec3i{a.x * b.x, a.y * b.y, a.z * b.z};
William Bundy's avatar
William Bundy committed
786 787
}

William Bundy's avatar
William Bundy committed
788
static inline Vec3i& operator+=(Vec3i& a, Vec3i b)
William Bundy's avatar
William Bundy committed
789 790 791 792 793
{
	a = a + b;
	return a;
}

William Bundy's avatar
William Bundy committed
794
static inline Vec3i& operator-=(Vec3i& a, Vec3i b)
William Bundy's avatar
William Bundy committed
795 796 797 798 799
{
	a = a - b;
	return a;
}

William Bundy's avatar
William Bundy committed
800
static inline Vec3i& operator*=(Vec3i& a, integer b)
William Bundy's avatar
William Bundy committed
801 802 803 804 805
{
	a = a * b;
	return a;
}

William Bundy's avatar
William Bundy committed
806
static inline Vec3i& operator*=(Vec3i& a, Vec3i b)
William Bundy's avatar
William Bundy committed
807
{
William Bundy's avatar
William Bundy committed
808 809
	a = a * b;
	return a;
William Bundy's avatar
William Bundy committed
810 811
}

William Bundy's avatar
William Bundy committed
812
static inline Vec3i& operator/=(Vec3i& a, integer b)
William Bundy's avatar
William Bundy committed
813
{
William Bundy's avatar
William Bundy committed
814 815
	a = a / b;
	return a;
William Bundy's avatar
William Bundy committed
816 817
}

William Bundy's avatar
William Bundy committed
818 819 820
/* Vec4i */

static inline Vec4i operator-(Vec4i a)
William Bundy's avatar
William Bundy committed
821
{
William Bundy's avatar
William Bundy committed
822
    return Vec4i{-a.x, -a.y, -a.z, -a.w};
William Bundy's avatar
William Bundy committed
823 824
}

William Bundy's avatar
William Bundy committed
825
static inline Vec4i operator+(Vec4i a, Vec4i b)
William Bundy's avatar
William Bundy committed
826
{
William Bundy's avatar
William Bundy committed
827
	return Vec4i{a.x + b.x, a.y + b.y, a.z + b.z, a.w + b.w};
William Bundy's avatar
William Bundy committed
828 829
}

William Bundy's avatar
William Bundy committed
830
static inline Vec4i operator-(Vec4i a, Vec4i b)
William Bundy's avatar
William Bundy committed
831
{
William Bundy's avatar
William Bundy committed
832
	return Vec4i{a.x - b.x, a.y - b.y, a.z - b.z, a.w - b.w};
William Bundy's avatar
William Bundy committed
833 834
}

William Bundy's avatar
William Bundy committed
835
static inline Vec4i operator*(Vec4i a, Vec4i b)
William Bundy's avatar
William Bundy committed
836
{
William Bundy's avatar
William Bundy committed
837
	return Vec4i{a.x * b.x, a.y * b.y, a.z * b.z, a.w * b.w};
William Bundy's avatar
William Bundy committed
838 839
}

William Bundy's avatar
William Bundy committed
840
static inline Vec4i operator*(Vec4i a, integer b)
William Bundy's avatar
William Bundy committed
841
{
William Bundy's avatar
William Bundy committed
842
	return Vec4i{a.x * b, a.y * b, a.z * b, a.w * b};
William Bundy's avatar
William Bundy committed
843 844
}

William Bundy's avatar
William Bundy committed
845
static inline Vec4i operator*(integer b, Vec4i a) 
William Bundy's avatar
William Bundy committed
846
{
William Bundy's avatar
William Bundy committed
847
	return a * b;
William Bundy's avatar
William Bundy committed
848
}
William Bundy's avatar
William Bundy committed
849 850

static inline Vec4i operator/(Vec4i a, integer b)
William Bundy's avatar
William Bundy committed
851
{
William Bundy's avatar
William Bundy committed
852
	return a * (1.0f/b);
William Bundy's avatar
William Bundy committed
853 854
}

William Bundy's avatar
William Bundy committed
855
static inline Vec4i operator+=(Vec4i& a, Vec4i b)
William Bundy's avatar
William Bundy committed
856
{
William Bundy's avatar
William Bundy committed
857 858
    a = a + b;
    return a;
William Bundy's avatar
William Bundy committed
859 860
}

William Bundy's avatar
William Bundy committed
861
static inline Vec4i operator-=(Vec4i& a, Vec4i b)
William Bundy's avatar
William Bundy committed
862
{
William Bundy's avatar
William Bundy committed
863 864
    a = a - b;
    return a;
William Bundy's avatar
William Bundy committed
865 866
}

William Bundy's avatar
William Bundy committed
867
static inline Vec4i operator*=(Vec4i& a, integer b)
William Bundy's avatar
William Bundy committed
868
{
William Bundy's avatar
William Bundy committed
869
	a = a * b;
William Bundy's avatar
William Bundy committed
870 871 872
	return a;
}

William Bundy's avatar
William Bundy committed
873
static inline Vec4i operator/=(Vec4i& a, integer b)
William Bundy's avatar
William Bundy committed
874
{
William Bundy's avatar
William Bundy committed
875
	a = a / b;
William Bundy's avatar
William Bundy committed
876 877 878
	return a;
}

William Bundy's avatar
William Bundy committed
879
static inline Vec4i operator*=(Vec4i& a, Vec4i b)
William Bundy's avatar
William Bundy committed
880
{
William Bundy's avatar
William Bundy committed
881 882
    a = a * b;
    return a;
William Bundy's avatar
William Bundy committed
883 884
}

William Bundy's avatar
William Bundy committed
885
#endif
William Bundy's avatar
William Bundy committed
886 887 888



William Bundy's avatar
William Bundy committed
889
static inline Vec2 v2_perpendicular(Vec2 v)
William Bundy's avatar
William Bundy committed
890
{
William Bundy's avatar
William Bundy committed
891 892 893
	return Vec2 {
		-v.y, v.x
	};
William Bundy's avatar
William Bundy committed
894 895
}

William Bundy's avatar
William Bundy committed
896
static inline real v2_dot(Vec2 a, Vec2 b)
897
{
William Bundy's avatar
William Bundy committed
898
	return a.x * b.x + a.y * b.y;
899
}
William Bundy's avatar
William Bundy committed
900 901

static inline real v2_len_sq(Vec2 a)
902
{
William Bundy's avatar
William Bundy committed
903
	return a.x * a.x + a.y * a.y;
904
}
William Bundy's avatar
William Bundy committed
905

William Bundy's avatar
William Bundy committed
906
static inline real v2_len(Vec2 a)
907
{
William Bundy's avatar
William Bundy committed
908
    return sqrtf(v2_len_sq(a));
909
}
William Bundy's avatar
William Bundy committed
910 911

static inline real v2_cross(Vec2 a, Vec2 b)
912
{
William Bundy's avatar
William Bundy committed
913
	return a.x * b.y - a.y * b.x;
914
}
William Bundy's avatar
William Bundy committed
915

William Bundy's avatar
William Bundy committed
916
static inline Vec2 v2_from_angle(real normal)
William Bundy's avatar
William Bundy committed
917
{
William Bundy's avatar
William Bundy committed
918 919 920
	return Vec2 {
		cosf(normal), sinf(normal)
	};
William Bundy's avatar
William Bundy committed
921 922
}

William Bundy's avatar
William Bundy committed
923
static inline real v2_to_angle(Vec2 v)
924
{
William Bundy's avatar
William Bundy committed
925
	return atan2f(v.y, v.x);
926 927
}

William Bundy's avatar
William Bundy committed
928
static inline Vec2 v2_normalize(Vec2 v)
William Bundy's avatar
William Bundy committed
929
{
William Bundy's avatar
William Bundy committed
930 931
	real mag = sqrt(v.x*v.x + v.y * v.y);
	return v2(v.x / mag, v.y / mag);
William Bundy's avatar
William Bundy committed
932 933 934
}


William Bundy's avatar
William Bundy committed
935
static inline real v3_dot(Vec3 a, Vec3 b)
William Bundy's avatar
William Bundy committed
936
{
William Bundy's avatar
William Bundy committed
937
	return a.x * b.x + a.y * b.y + a.z * b.z;
William Bundy's avatar
William Bundy committed
938
}
William Bundy's avatar
William Bundy committed
939 940

static inline real v3_len_sq(Vec3 a)
William Bundy's avatar
William Bundy committed
941
{
William Bundy's avatar
William Bundy committed
942
	return a.x * a.x + a.y * a.y + a.z * a.z;
William Bundy's avatar
William Bundy committed
943 944
}

William Bundy's avatar
William Bundy committed
945
static inline real v3_len(Vec3 a)
946
{
William Bundy's avatar
William Bundy committed
947
	return sqrtf(v3_len_sq(a));
948 949
}

William Bundy's avatar
William Bundy committed
950 951

static inline Vec3 v3_cross(Vec3 a, Vec3 b)
952
{
William Bundy's avatar
William Bundy committed
953 954 955 956 957 958
    Vec3 v;
    v.x = (a.y * b.z - b.y * a.z);
    v.y = (a.x * b.z - b.x * a.z);
	v.z = (a.x * b.y - a.y * b.x);

    return v;
959
}
William Bundy's avatar
William Bundy committed
960

William Bundy's avatar
William Bundy committed
961 962 963 964 965
static inline Vec3 v3_normalize(Vec3 v)
{
	real mag = v3_len(v);
	return v3(v.x / mag, v.y / mag, v.z / mag);
}
William Bundy's avatar
William Bundy committed
966

William Bundy's avatar
William Bundy committed
967 968 969 970
static inline real v4_dot(Vec4 a, Vec4 b)
{
	return a.x * b.x + a.y * b.y + a.z * b.z + a.w * b.w;
}
William Bundy's avatar
William Bundy committed
971

William Bundy's avatar
William Bundy committed
972
static inline real v4_len_sq(Vec4 a)
William Bundy's avatar
William Bundy committed
973
{
William Bundy's avatar
William Bundy committed
974
	return a.x * a.x + a.y * a.y + a.z * a.z + a.w * a.w;
William Bundy's avatar
William Bundy committed
975 976
}

William Bundy's avatar
William Bundy committed
977
static inline real v4_len(Vec4 a)
William Bundy's avatar
William Bundy committed
978
{
William Bundy's avatar
William Bundy committed
979
	return sqrtf(v4_len_sq(a));
William Bundy's avatar
William Bundy committed
980 981
}

William Bundy's avatar
William Bundy committed
982
static inline Vec4 v4_normalize(Vec4 v)
William Bundy's avatar
William Bundy committed
983
{
William Bundy's avatar
William Bundy committed
984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003
	real mag = v4_len(v);
	return v4(v.x / mag, v.y / mag, v.z / mag, v.w / mag);
}

static inline Vec4 hex_to_v4(uint32 rgba)
{
#define _c(n) (uint8)((rgba >> (n)) & 0xFF)
	uint8 r = _c(24);
	uint8 g = _c(16);
	uint8 b = _c(8);
	uint8 a = _c(0);
#undef _c
	Vec4 color;
#define _f(n) ((real)(n)/255.0f)
	color.x = _f(r);
	color.y = _f(g);
	color.z = _f(b);
	color.w = _f(a);
#undef _f
	return color;
William Bundy's avatar
William Bundy committed
1004 1005
}

William Bundy's avatar
William Bundy committed
1006 1007 1008 1009 1010



#ifndef REFLECTED
union Rect2
William Bundy's avatar
William Bundy committed
1011
{
William Bundy's avatar
William Bundy committed
1012 1013
	struct {
		real x, y, w, h;
William Bundy's avatar
William Bundy committed
1014
	};
William Bundy's avatar
William Bundy committed
1015 1016 1017 1018 1019
	struct {
		Vec2 position, size;
	};
	real e[4];
};
William Bundy's avatar
William Bundy committed
1020

William Bundy's avatar
William Bundy committed
1021
struct Rect2_Clip_Info
William Bundy's avatar
William Bundy committed
1022
{
William Bundy's avatar
William Bundy committed
1023 1024 1025 1026 1027
	Vec2 rp1;
	Vec2 rp2;
	Vec2 diff1;
	Vec2 diff2;
};
William Bundy's avatar
William Bundy committed
1028

William Bundy's avatar
William Bundy committed
1029
struct Rect2i
William Bundy's avatar
William Bundy committed
1030
{
William Bundy's avatar
William Bundy committed
1031 1032
	integer x, y, w, h;
};
William Bundy's avatar
William Bundy committed
1033

William Bundy's avatar
William Bundy committed
1034
union AABB
William Bundy's avatar
William Bundy committed
1035
{
William Bundy's avatar
William Bundy committed
1036 1037 1038 1039 1040 1041 1042 1043 1044
	struct {
		Vec2 center;
		union {
			struct {
				real hw, hh;
			};
			struct {
				Vec2 hext;
			};
1045 1046
		};
	};
William Bundy's avatar
William Bundy committed
1047
	real e[4];
William Bundy's avatar
William Bundy committed
1048
};
William Bundy's avatar
William Bundy committed
1049
#endif
William Bundy's avatar
William Bundy committed
1050

1051 1052 1053 1054 1055 1056
#define AABB_x1(b) (b.center.x - b.hw)
#define AABB_x2(b) (b.center.x + b.hw)
#define AABB_y1(b) (b.center.y - b.hh)
#define AABB_y2(b) (b.center.y + b.hh)


William Bundy's avatar
William Bundy committed
1057 1058 1059 1060 1061
static inline bool operator==(Rect2 a, Rect2 b)
{
	return (a.x == b.x) && (a.y == b.y) && (a.w == b.w) && (a.h == b.h);
}

William Bundy's avatar
William Bundy committed
1062 1063 1064 1065 1066 1067
static inline AABB aabb(Vec2 c, real hw, real hh)
{
	return AABB{
		c, hw, hh
	};
}
William Bundy's avatar
William Bundy committed
1068

William Bundy's avatar
William Bundy committed
1069 1070 1071 1072 1073 1074
static inline AABB aabb(real x, real y, real hw, real hh)
{
	return AABB{
		v2(x, y), hw, hh
	};
}
William Bundy's avatar
William Bundy committed
1075

William Bundy's avatar
William Bundy committed
1076
static inline Rect2 rect2(real x, real y, real w, real h)
William Bundy's avatar
William Bundy committed
1077
{
William Bundy's avatar
William Bundy committed
1078 1079 1080 1081
	return Rect2{
		x, y, w, h
	};
}
William Bundy's avatar
William Bundy committed
1082

William Bundy's avatar
William Bundy committed
1083 1084 1085 1086 1087 1088
static inline Rect2 rect2(Vec2 topleft, Vec2 size)
{
	return Rect2{
		topleft.x, topleft.y, size.x, size.y
	};
}
William Bundy's avatar
William Bundy committed
1089

William Bundy's avatar
William Bundy committed
1090 1091 1092 1093 1094 1095
static inline Rect2i rect2i(integer x, integer y, integer w, integer h)
{
	return Rect2i {
		x, y, w, h
	};
}
William Bundy's avatar
William Bundy committed
1096 1097 1098

static inline Rect2_Clip_Info rect2_clip(Rect2 r, Rect2 clip)
{
William Bundy's avatar
William Bundy committed
1099 1100 1101 1102
	Vec2 rp1 = r.position;
	Vec2 rp2 = rp1 + r.size;
	Vec2 cp1 = clip.position;
	Vec2 cp2 = cp1 + clip.size;
William Bundy's avatar
William Bundy committed
1103 1104 1105 1106 1107 1108 1109 1110 1111
	Vec2 diff1 = v2(0, 0);
	Vec2 diff2 = v2(0, 0);

	if(rp1.x < cp1.x) {
		diff1.x = cp1.x - rp1.x;
		rp1.x = cp1.x;
	}

	if(rp2.x > cp2.x) {
William Bundy's avatar
William Bundy committed
1112
		diff2.x = rp2.x - cp2.x;
William Bundy's avatar
William Bundy committed
1113 1114 1115 1116 1117 1118 1119 1120
		rp2.x = cp2.x;
	}
	if(rp1.y < cp1.y) {
		diff1.y = cp1.y - rp1.y;
		rp1.y = cp1.y;
	}

	if(rp2.y > cp2.y) {
William Bundy's avatar
William Bundy committed
1121
		diff2.y = rp2.y - cp2.y;
William Bundy's avatar
William Bundy committed
1122 1123 1124
		rp2.y = cp2.y;
	}

William Bundy's avatar
William Bundy committed
1125
	return Rect2_Clip_Info{
William Bundy's avatar
William Bundy committed
1126
		rp1, rp2, diff1, diff2
William Bundy's avatar
William Bundy committed
1127
	};
William Bundy's avatar
William Bundy committed
1128 1129
}

William Bundy's avatar
William Bundy committed
1130
static inline bool rect_contains_point(Rect2 r, Vec2 p)
1131
{
William Bundy's avatar
William Bundy committed
1132 1133
	return (p.x > r.x) && (p.y > r.y) &&
		   (p.x < (r.x+r.w)) && (p.y < (r.y + r.h));
1134 1135
}

William Bundy's avatar
William Bundy committed
1136
static inline bool aabb_intersect_f(real dx, real w, real dy, real h)
William Bundy's avatar
William Bundy committed
1137
{
William Bundy's avatar
William Bundy committed
1138 1139
	if(fabsf(dx) > (w)) return false;
	if(fabsf(dy) > (h)) return false;
William Bundy's avatar
William Bundy committed
1140 1141 1142
	return true;
}

1143 1144 1145 1146 1147 1148 1149
static inline bool aabb_intersect(AABB a, AABB b)
{
	if(fabsf(b.center.x - a.center.x) > (b.hw + a.hw)) return false;
	if(fabsf(b.center.y - a.center.y) > (b.hh + a.hh)) return false;
	return true;
}

William Bundy's avatar
William Bundy committed
1150
static inline Vec2 aabb_overlap_v2(AABB a, AABB b)
1151
{
William Bundy's avatar
William Bundy committed
1152 1153 1154 1155 1156 1157
	real sx = (a.hw + b.hw) - fabsf(b.center.x - a.center.x);
	real sy = (a.hh + b.hh) - fabsf(b.center.y - a.center.y);
	if(sx > sy) {
		sx = 0;
		if(a.center.y > b.center.y) {
			sy *= -1;
1158 1159
		}
	} else {
William Bundy's avatar
William Bundy committed
1160 1161 1162
		sy = 0;
		if(a.center.x > b.center.x) {
			sx *= -1;
1163 1164
		}
	}
William Bundy's avatar
William Bundy committed
1165
    return v2(sx, sy);
1166 1167
}

William Bundy's avatar
William Bundy committed
1168
static inline void aabb_overlap(AABB a, AABB b, Vec2* s)
William Bundy's avatar
William Bundy committed
1169
{
William Bundy's avatar
William Bundy committed
1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184
	real sx = (a.hw + b.hw) - fabsf(b.center.x - a.center.x);
	real sy = (a.hh + b.hh) - fabsf(b.center.y - a.center.y);
	if(sx > sy) {
		sx = 0;
		if(a.center.y > b.center.y) {
			sy *= -1;
		}
	} else {
		sy = 0;
		if(a.center.x > b.center.x) {
			sx *= -1;
		}
	}
    s->x = sx;
    s->y = sy;
William Bundy's avatar
William Bundy committed
1185 1186
}

William Bundy's avatar
William Bundy committed
1187
static inline AABB rect_to_aabb(Rect2 r)
William Bundy's avatar
William Bundy committed
1188 1189
{
	AABB b = {0};
William Bundy's avatar
William Bundy committed
1190 1191 1192
	b.hw = r.w / 2;
	b.hh = r.h / 2;
	b.center = v2(r.x + b.hw, r.y + b.hh);
William Bundy's avatar
William Bundy committed
1193 1194 1195
	return b;
}

William Bundy's avatar
William Bundy committed
1196
static inline Rect2 aabb_to_rect(AABB b)
William Bundy's avatar
William Bundy committed
1197 1198
{
	Rect2 r = {0};
William Bundy's avatar
William Bundy committed
1199 1200 1201 1202
	r.x = b.center.x - b.hw;
	r.y = b.center.y - b.hh;
	r.w = b.hw * 2;
	r.h = b.hh * 2;
William Bundy's avatar
William Bundy committed
1203 1204 1205
	return r;
}

1206 1207 1208 1209 1210 1211 1212
static inline isize modulus(isize a, isize b)
{
	if(b == 0) return -1;
	if(a == -b) return 0;
	else if(a < 0) a = b + (a % b);
	return a % b;
}
William Bundy's avatar
William Bundy committed
1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224

static inline AABB extents_to_aabb(Vec2 tl, Vec2 br)
{
	AABB a;
	a.hext = (br - tl) * 0.5f;
	a.center = tl + a.hext;
	return a;
}
static inline AABB extents_to_aabb(real left, real top, real right, real bottom)
{
	return extents_to_aabb(v2(left, top), v2(right, bottom));	
}