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
|
/*
This file is part of Warzone 2100.
Copyright (C) 2007-2010 Warzone 2100 Project
Warzone 2100 is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
Warzone 2100 is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with Warzone 2100; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
#ifndef VECTOR_H
#define VECTOR_H
#include "wzglobal.h"
#include <assert.h>
#include "fixedpoint.h"
#include "types.h"
#include "math_ext.h"
typedef struct { int x, y; } Vector2i;
typedef struct { float x, y; } Vector2f;
typedef struct { int x, y, z; } Vector3i;
typedef struct { float x, y, z; } Vector3f;
typedef struct { uint16_t x, y, z; } Vector3uw; // Only used for basedef.h BASE_ELEMENTS1.
/*!
* Create a Vector from x and y
* Needed for MSVC which doesn't support C99 struct assignments.
* \param x,y Coordinates
* \return New Vector
*/
static inline WZ_DECL_CONST Vector2i Vector2i_Init(const int x, const int y)
{
Vector2i dest = { x, y };
return dest;
}
/*!
* Convert an integer vector to float
* \param v Vector to convert
* \return Float vector
*/
static inline WZ_DECL_CONST Vector2f Vector2i_To2f(const Vector2i v)
{
Vector2f dest = { (float)v.x, (float)v.y };
return dest;
}
/*!
* \return true if both vectors are equal
*/
static inline WZ_DECL_CONST bool Vector2i_Compare(const Vector2i a, const Vector2i b)
{
return a.x == b.x && a.y == b.y;
}
/*!
* Add op2 to op1.
* \param[in] op1,op2 Operands
* \return Result
*/
static inline WZ_DECL_CONST Vector2i Vector2i_Add(const Vector2i op1, const Vector2i op2)
{
Vector2i dest = {
op1.x + op2.x,
op1.y + op2.y
};
return dest;
}
/*!
* Substract op2 from op1.
* \param op1,op2 Operands
* \return Result
*/
static inline WZ_DECL_CONST Vector2i Vector2i_Sub(const Vector2i op1, const Vector2i op2)
{
Vector2i dest = {
op1.x - op2.x,
op1.y - op2.y
};
return dest;
}
/*!
* Multiply a vector with a scalar.
* \param v Vector
* \param s Scalar
* \return Product
*/
static inline WZ_DECL_CONST Vector2i Vector2i_Mult(const Vector2i v, const int s)
{
Vector2i dest = { v.x * s, v.y * s };
return dest;
}
/*!
* Calculate the scalar product of op1 and op2.
* \param op1,op2 Operands
* \return Scalarproduct of the 2 vectors
*/
static inline WZ_DECL_CONST int Vector2i_ScalarP(const Vector2i op1, const Vector2i op2)
{
return op1.x * op2.x + op1.y * op2.y;
}
/*!
* Calculate the length of a vector.
* \param v Vector
* \return Length
*/
static inline WZ_DECL_CONST int Vector2i_Length(const Vector2i v)
{
return sqrtf( (float)Vector2i_ScalarP(v, v) );
}
/*!
* Checks to see if vector v is inside the circle whose centre is at point c
* with a radius of r.
* This function makes use of the following equation:
* (x - a)^2 + (y - b)^2 = r^2 which is used for drawing a circle of radius r
* with a centre (a, b). However we can also use it to see if a point is in a
* circle, which is the case so long as RHS > LHS.
* \param v Vector to test
* \param c Vector containing the centre of the circle
* \param r The radius of the circle
* \return If v falls within the circle
*/
static inline WZ_DECL_CONST bool Vector2i_InCircle(const Vector2i v, const Vector2i c, const unsigned int r)
{
Vector2i delta = Vector2i_Sub(v, c);
// Explictily cast to "unsigned int" because this number never can be
// negative, due to the fact that these numbers are squared. Still GCC
// warns about a comparison of a comparison between an unsigned and a
// signed integer.
return (unsigned int)((delta.x * delta.x) + (delta.y * delta.y)) < (r * r);
}
/*!
* Create a Vector from x and y
* Needed for MSVC which doesn't support C99 struct assignments.
* \param x,y Coordinates
* \return New Vector
*/
static inline WZ_DECL_CONST Vector2f Vector2f_Init(const float x, const float y)
{
Vector2f dest = { x, y };
return dest;
}
/*!
* Convert a float vector to integer
* \param v Vector to convert
* \return Float vector
*/
static inline WZ_DECL_CONST Vector2i Vector2f_To2i(const Vector2f v)
{
Vector2i dest = { (int)v.x, (int)v.y };
return dest;
}
/*!
* Add op2 to op1.
* \param op1,op2 Operands
* \return Result
*/
static inline WZ_DECL_CONST Vector2f Vector2f_Add(const Vector2f op1, const Vector2f op2)
{
Vector2f dest = {
op1.x + op2.x,
op1.y + op2.y
};
return dest;
}
/*!
* Substract op2 from op1.
* \param op1,op2 Operands
* \return Result
*/
static inline WZ_DECL_CONST Vector2f Vector2f_Sub(const Vector2f op1, const Vector2f op2)
{
Vector2f dest = {
op1.x - op2.x,
op1.y - op2.y
};
return dest;
}
/*!
* Multiply a vector with a scalar.
* \param v Vector
* \param s Scalar
* \return Product
*/
static inline WZ_DECL_CONST Vector2f Vector2f_Mult(const Vector2f v, const float s)
{
Vector2f dest = { v.x * s, v.y * s };
return dest;
}
/*!
* Calculate the scalar product of op1 and op2.
* \param op1,op2 Operands
* \return Scalarproduct of the 2 vectors
*/
static inline WZ_DECL_CONST float Vector2f_ScalarP(const Vector2f op1, const Vector2f op2)
{
return op1.x * op2.x + op1.y * op2.y;
}
/*!
* Calculate the length of a vector.
* \param v Vector
* \return Length
*/
static inline WZ_DECL_CONST float Vector2f_Length(const Vector2f v)
{
return sqrtf( Vector2f_ScalarP(v, v) );
}
/*!
* Normalise a Vector
* \param v Vector
* \return Normalised vector, nullvector when input was nullvector or very small
*/
static inline WZ_DECL_CONST Vector2f Vector2f_Normalise(const Vector2f v)
{
float length = Vector2f_Length(v);
if (length == 0.0f)
{
Vector2f dest = { 0.0f, 0.0f };
return dest;
}
else
{
Vector2f dest = { v.x / length, v.y / length };
return dest;
}
}
/*!
* Rotate v
* \param v vector to rotate
* \param degrees the amount of degrees to rotate in counterclockwise direction
* \return Result
*/
static inline WZ_DECL_CONST Vector2f Vector2f_Rotate2f(Vector2f v, float degrees)
{
Vector2f result;
float radians = (degrees / 360) * 2 * 3.14;
result.x = v.x*cos(radians) - v.y*sin(radians);
result.y = v.x*sin(radians) + v.y*cos(radians);
return result;
}
/*!
* Finds a point that lies in between two other points, a starting and ending
* point.
*
* \param from Vector representing the starting point.
* \param to Vector representing the ending point.
* \param s The distance travelled along the line between vectors \c from and
* \c to expressed as a number ranging from 0.f to 1.f.
*
* \return a Vector that's \c s along the line between \c from and \to
*/
static inline WZ_DECL_CONST Vector2i Vector2i_LinearInterpolate(const Vector2i from, const Vector2i to, const float s)
{
assert(s >= 0.f && s <= 1.f);
return Vector2i_Add(from, Vector2f_To2i(Vector2f_Mult(Vector2i_To2f(Vector2i_Sub(to, from)), s)));
}
/*!
* Finds a point that lies in between two other points, a starting and ending
* point.
*
* \param from Vector representing the starting point.
* \param to Vector representing the ending point.
* \param s The distance travelled along the line between vectors \c from and
* \c to expressed as a number ranging from 0.f to 1.f.
*
* \return a Vector that's \c s along the line between \c from and \to
*/
static inline WZ_DECL_CONST Vector2f Vector2f_LinearInterpolate(const Vector2f from, const Vector2f to, const float s)
{
assert(s >= 0.f && s <= 1.f);
return Vector2f_Add(from, Vector2f_Mult(Vector2f_Sub(to, from), s));
}
/*!
* Print a vector to stdout
*/
static inline void Vector3f_Print(const Vector3f v)
{
printf("V: x:%f, y:%f, z:%f\n", v.x, v.y, v.z);
}
/*!
* Set the vector field by field, same as v = (Vector3f){x, y, z};
* Needed for MSVC which doesn't support C99 struct assignments.
* \param x,y,z Values to set to
* \return New vector
*/
static inline WZ_DECL_CONST Vector3f Vector3f_Init(const float x, const float y, const float z)
{
Vector3f dest = { x, y, z };
return dest;
}
/*!
* Convert a float vector to integer
* \param v Vector to convert
* \return Float vector
*/
static inline WZ_DECL_CONST Vector3i Vector3f_To3i(const Vector3f v)
{
Vector3i dest = { (int)v.x, (int)v.y, (int)v.z };
return dest;
}
/*!
* Convert a float vector to short
* \param v Vector to convert
* \return Short vector
*/
static inline WZ_DECL_CONST Vector3uw Vector3f_To3uw(const Vector3f v)
{
Vector3uw dest = { (uint16_t)v.x, (uint16_t)v.y, (uint16_t)v.z };
return dest;
}
/*!
* \return true if both vectors are equal
*/
static inline WZ_DECL_CONST bool Vector3f_Compare(const Vector3f a, const Vector3f b)
{
return a.x == b.x && a.y == b.y && a.z == b.z;
}
/*!
* Add op2 to op1.
* \param op1,op2 Operands
* \return Result
*/
static inline WZ_DECL_CONST Vector3f Vector3f_Add(const Vector3f op1, const Vector3f op2)
{
Vector3f dest = {
op1.x + op2.x,
op1.y + op2.y,
op1.z + op2.z
};
return dest;
}
/*!
* Substract op2 from op1.
* \param op1,op2 Operands
* \return Result
*/
static inline WZ_DECL_CONST Vector3f Vector3f_Sub(const Vector3f op1, const Vector3f op2)
{
Vector3f dest = {
op1.x - op2.x,
op1.y - op2.y,
op1.z - op2.z
};
return dest;
}
/*!
* Multiply a vector with a scalar.
* \param v Vector
* \param s Scalar
* \return Product
*/
static inline WZ_DECL_CONST Vector3f Vector3f_Mult(const Vector3f v, const float s)
{
Vector3f dest = { v.x * s, v.y * s, v.z * s };
return dest;
}
/*!
* Calculate the scalar product of op1 and op2.
* \param op1,op2 Operands
* \return Scalarproduct of the 2 vectors
*/
static inline WZ_DECL_CONST float Vector3f_ScalarP(const Vector3f op1, const Vector3f op2)
{
return op1.x * op2.x + op1.y * op2.y + op1.z * op2.z;
}
/*!
* Calculate the crossproduct of op1 and op2.
* \param op1,op2 Operands
* \return Crossproduct
*/
static inline WZ_DECL_CONST Vector3f Vector3f_CrossP(const Vector3f op1, const Vector3f op2)
{
Vector3f dest = {
op1.y * op2.z - op1.z * op2.y,
op1.z * op2.x - op1.x * op2.z,
op1.x * op2.y - op1.y * op2.x
};
return dest;
}
/*!
* Calculate the length of a vector.
* \param v Vector
* \return Length
*/
static inline WZ_DECL_CONST float Vector3f_Length(const Vector3f v)
{
return sqrtf( Vector3f_ScalarP(v, v) );
}
/*!
* Normalise a Vector
* \param v Vector
* \return Normalised vector, nullvector when input was nullvector or very small
*/
static inline WZ_DECL_CONST Vector3f Vector3f_Normalise(const Vector3f v)
{
float length = Vector3f_Length(v);
if (length == 0.0f)
{
Vector3f dest = { 0.0f, 0.0f, 0.0f };
return dest;
}
else
{
Vector3f dest = { v.x / length, v.y / length, v.z / length };
return dest;
}
}
/*!
* Much the same as Vector2i_InCircle except that it works in 3-axis by discarding the z-component and with
* circles.
* \param v Vector to test
* \param c Vector containing the centre of the circle
* \param r The radius of the circle
* \return If v falls within the circle
*/
static inline WZ_DECL_CONST bool Vector3f_InCircle(const Vector3f v, const Vector3f c, const float r)
{
Vector3f delta = Vector3f_Sub(v, c);
// Explictily cast to "unsigned int" because this number never can be
// negative, due to the fact that these numbers are squared. Still GCC
// warns about a comparison of a comparison between an unsigned and a
// signed integer.
return (delta.x * delta.x) + (delta.y * delta.y) < (r * r);
}
/*!
* Much the same as Vector2i_InCircle except that it works in 3-axis and with
* spheres.
* The equation used is also ever so slightly different:
* (x - a)^2 + (y - b)^2 + (z - c)^2 = r^2. Notice how it is still squared and
* _not_ cubed!
* \param v Vector to test
* \param c Vector containing the centre of the sphere
* \param r The radius of the sphere
* \return If v falls within the sphere
*/
static inline WZ_DECL_CONST bool Vector3f_InSphere (const Vector3f v, const Vector3f c, const float r)
{
Vector3f delta = Vector3f_Sub(v, c);
// Explictily cast to "unsigned int" because this number never can be
// negative, due to the fact that these numbers are squared. Still GCC
// warns about a comparison of a comparison between an unsigned and a
// signed integer.
return (delta.x * delta.x) + (delta.y * delta.y) + (delta.z * delta.z) < (r * r);
}
/*!
* Compute the forward vector, a body's local Z axis, for a set of Euler angles
* (pitch, yaw and roll).
* \param v Vector containing the pitch, yaw and roll in its x, y and z members
* respectively. These rotations need to be expressed in radians.
* \return Forward vector.
*/
static inline WZ_DECL_CONST Vector3f Vector3f_EulerToForwardVector(const Vector3f v)
{
Vector3f dest = {
cosf(v.x) * sinf(v.y),
-sinf(v.x),
cosf(v.x) * cosf(v.y)
};
return dest;
}
/*!
* Compute the up vector, a body's local Y axis, for a set of Euler angles
* (pitch, yaw and roll).
* \param v Vector containing the pitch, yaw and roll in its x, y and z members
* respectively. These rotations need to be expressed in radians.
* \return Up vector.
*/
static inline WZ_DECL_CONST Vector3f Vector3f_EulerToUpVector(const Vector3f v)
{
Vector3f dest = {
sinf(v.x) * sinf(v.y) * cosf(v.z) - sinf(v.z) * cosf(v.z),
cosf(v.x) * cosf(v.z),
sinf(v.x) * cosf(v.y) * cosf(v.z) + sinf(v.y) * sinf(v.z)
};
return dest;
}
/*!
* Finds a point that lies in between two other points, a starting and ending
* point.
*
* \param from Vector representing the starting point.
* \param to Vector representing the ending point.
* \param s The distance travelled along the line between vectors \c from and
* \c to expressed as a number ranging from 0.f to 1.f.
*
* \return a Vector that's \c s along the line between \c from and \to
*/
static inline WZ_DECL_CONST Vector3f Vector3f_LinearInterpolate(const Vector3f from, const Vector3f to, const float s)
{
assert(s >= 0.f && s <= 1.f);
return Vector3f_Add(from, Vector3f_Mult(Vector3f_Sub(to, from), s));
}
/*!
* Set the vector field by field, same as v = (Vector3i){x, y, z};
* Needed for MSVC which doesn't support C99 struct assignments.
* \param x,y,z Coordinates
* \return New Vector
*/
static inline WZ_DECL_CONST Vector3i Vector3i_Init(const int x, const int y, const int z)
{
Vector3i dest = { x, y, z };
return dest;
}
/*!
* Convert an integer vector to float
* \param v Vector to convert
* \return Float vector
*/
static inline WZ_DECL_CONST Vector3f Vector3i_To3f(const Vector3i v)
{
Vector3f dest = { (float)v.x, (float)v.y, (float)v.z };
return dest;
}
/*!
* Convert an integer vector to short
* \param v Vector to convert
* \return Short vector
*/
static inline WZ_DECL_CONST Vector3uw Vector3i_To3uw(const Vector3i v)
{
Vector3uw dest = { (uint16_t)v.x, (uint16_t)v.y, (uint16_t)v.z };
return dest;
}
/*!
* Convert a vector of degree angles into radians.
* \param v Vector to convert
* \return Radian vector
*/
static inline WZ_DECL_CONST Vector3f Vector3f_ToRadians(const Vector3f v)
{
Vector3f dest = {
deg2radf(v.x),
deg2radf(v.y),
deg2radf(v.z)
};
return dest;
}
/*!
* Convert a vector of fixed-point, wannabe-floats (used on the PSX, and
* unfortunately on the PC as well), to real floats expressed in real degrees.
* \param v Rotation vector in "wannabe-float" degrees
* \return Float vector in real degrees
*/
static inline WZ_DECL_CONST Vector3f Vector3iPSX_To3fDegree(const Vector3i v)
{
return Vector3f_Mult(Vector3i_To3f(v),
// Required to multiply by this to undo the PSX fixed point fract stuff
360.f / (float)DEG_360);
}
/*!
* \return true if both vectors are equal
*/
static inline WZ_DECL_CONST bool Vector3i_Compare(const Vector3i a, const Vector3i b)
{
return a.x == b.x && a.y == b.y && a.z == b.z;
}
/*!
* Add op2 to op1.
* \param op1,op2 Operands
* \return Result
*/
static inline WZ_DECL_CONST Vector3i Vector3i_Add(const Vector3i op1, const Vector3i op2)
{
Vector3i dest = {
op1.x + op2.x,
op1.y + op2.y,
op1.z + op2.z
};
return dest;
}
/*!
* Substract op2 from op1.
* \param op1,op2 Operands
* \return Result
*/
static inline WZ_DECL_CONST Vector3i Vector3i_Sub(const Vector3i op1, const Vector3i op2)
{
Vector3i dest = {
op1.x - op2.x,
op1.y - op2.y,
op1.z - op2.z
};
return dest;
}
/*!
* Multiply a vector with a scalar.
* \param v Vector
* \param s Scalar
* \return Product
*/
static inline WZ_DECL_CONST Vector3i Vector3i_Mult(const Vector3i v, const int s)
{
Vector3i dest = { v.x * s, v.y * s, v.z * s };
return dest;
}
/*!
* Divide a vector with a scalar.
* \param v Vector
* \param s Scalar
* \return Product
*/
static inline WZ_DECL_CONST Vector3i Vector3i_Div(const Vector3i v, const int s)
{
Vector3i dest = { v.x / s, v.y / s, v.z / s };
return dest;
}
/*!
* Calculate the scalar product of op1 and op2.
* \param op1,op2 Operands
* \return Scalarproduct of the 2 vectors
*/
static inline WZ_DECL_CONST unsigned int Vector3i_ScalarP(const Vector3i op1, const Vector3i op2)
{
return op1.x * op2.x + op1.y * op2.y + op1.z * op2.z;
}
/*!
* Calculate the length of a vector.
* \param v Vector
* \return Length
*/
static inline WZ_DECL_CONST float Vector3i_Length(const Vector3i v)
{
return sqrtf( Vector3i_ScalarP(v, v) );
}
/*!
* Normalise a Vector
* \param v Vector
* \return Normalised vector, nullvector when input was nullvector or very small
*/
static inline WZ_DECL_CONST Vector3i Vector3i_Normalise(const Vector3i v)
{
float length = Vector3i_Length(v);
if (length == 0.0f)
{
Vector3i dest = { 0, 0, 0 };
return dest;
}
else
{
Vector3i dest = { v.x / length, v.y / length, v.z / length };
return dest;
}
}
/*!
* Much the same as Vector2i_InCircle except that it works in 3-axis by discarding the z-component and with
* circles.
* \param v Vector to test
* \param c Vector containing the centre of the circle
* \param r The radius of the circle
* \return If v falls within the circle
*/
static inline WZ_DECL_CONST bool Vector3i_InCircle(const Vector3i v, const Vector3i c, const unsigned int r)
{
Vector3i delta = Vector3i_Sub(v, c);
// Explictily cast to "unsigned int" because this number never can be
// negative, due to the fact that these numbers are squared. Still GCC
// warns about a comparison of a comparison between an unsigned and a
// signed integer.
return (unsigned int)((delta.x * delta.x) + (delta.y * delta.y)) < (r * r);
}
/*!
* Much the same as Vector2i_InCircle except that it works in 3-axis and with
* spheres.
* The equation used is also ever so slightly different:
* (x - a)^2 + (y - b)^2 + (z - c)^2 = r^2. Notice how it is still squared and
* _not_ cubed!
* \param v Vector to test
* \param c Vector containing the centre of the sphere
* \param r The radius of the sphere
* \return If v falls within the sphere
*/
static inline WZ_DECL_CONST bool Vector3i_InSphere (const Vector3i v, const Vector3i c, const unsigned int r)
{
Vector3i delta = Vector3i_Sub(v, c);
// Explictily cast to "unsigned int" because this number never can be
// negative, due to the fact that these numbers are squared. Still GCC
// warns about a comparison of a comparison between an unsigned and a
// signed integer.
return (unsigned int)((delta.x * delta.x) + (delta.y * delta.y) + (delta.z * delta.z)) < (r * r);
}
/*!
* Finds a point that lies in between two other points, a starting and ending
* point.
*
* \param from Vector representing the starting point.
* \param to Vector representing the ending point.
* \param s The distance travelled along the line between vectors \c from and
* \c to expressed as a number ranging from 0.f to 1.f.
*
* \return a Vector that's \c s along the line between \c from and \to
*/
static inline WZ_DECL_CONST Vector3i Vector3i_LinearInterpolate(const Vector3i from, const Vector3i to, const float s)
{
assert(s >= 0.f && s <= 1.f);
return Vector3i_Add(from, Vector3f_To3i(Vector3f_Mult(Vector3i_To3f(Vector3i_Sub(to, from)), s)));
}
/*!
* Print a vector to stdout
*/
static inline void Vector3uw_Print(const Vector3uw v)
{
printf("V: x:%u, y:%u, z:%u\n", v.x, v.y, v.z);
}
/*!
* Set the vector field by field, same as v = (Vector3uw){x, y, z};
* Needed for MSVC which doesn't support C99 struct assignments.
* \param x,y,z Coordinates
* \return New Vector
*/
static inline WZ_DECL_CONST Vector3uw Vector3uw_Init(const unsigned int x, const unsigned int y, const unsigned int z)
{
Vector3uw dest = { x, y, z };
return dest;
}
/*!
* Convert an short vector to int
* \param v Vector to convert
* \return Short vector
*/
static inline WZ_DECL_CONST Vector3i Vector3uw_To3i(const Vector3uw v)
{
Vector3i dest = { (int)v.x, (int)v.y, (int)v.z };
return dest;
}
/*!
* Convert an short vector to int
* \param v Vector to convert
* \return Short vector
*/
static inline WZ_DECL_CONST Vector3f Vector3uw_To3f(const Vector3uw v)
{
Vector3f dest = { (float)v.x, (float)v.y, (float)v.z };
return dest;
}
#endif // VECTOR_H
|