1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125
|
/* LIBGIMP - The GIMP Library
* Copyright (C) 1995-1997 Peter Mattis and Spencer Kimball
*
* gimpvector.c
*
* The gimp_vector* functions were taken from:
* GCK - The General Convenience Kit
* Copyright (C) 1996 Tom Bech
*
* This library is free software: you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 3 of the License, or (at your option) any later version.
*
* This library 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
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library. If not, see
* <http://www.gnu.org/licenses/>.
*/
/**********************************************/
/* A little collection of useful vector stuff */
/**********************************************/
#include "config.h"
#include <glib-object.h>
#include "gimpmath.h"
/**
* SECTION: gimpvector
* @title: GimpVector
* @short_description: Utilities to set up and manipulate vectors.
* @see_also: #GimpMatrix2, #GimpMatrix3, #GimpMatrix4
*
* Utilities to set up and manipulate vectors.
**/
/*************************/
/* Some useful constants */
/*************************/
static const GimpVector2 gimp_vector2_zero = { 0.0, 0.0 };
static const GimpVector2 gimp_vector2_unit_x = { 1.0, 0.0 };
static const GimpVector2 gimp_vector2_unit_y = { 0.0, 1.0 };
static const GimpVector3 gimp_vector3_zero = { 0.0, 0.0, 0.0 };
static const GimpVector3 gimp_vector3_unit_x = { 1.0, 0.0, 0.0 };
static const GimpVector3 gimp_vector3_unit_y = { 0.0, 1.0, 0.0 };
static const GimpVector3 gimp_vector3_unit_z = { 0.0, 0.0, 1.0 };
/**************************************/
/* Two dimensional vector functions */
/**************************************/
/**
* gimp_vector2_new:
* @x: the X coordinate.
* @y: the Y coordinate.
*
* Creates a #GimpVector2 of coordinates @x and @y.
*
* Returns: the resulting #GimpVector2.
**/
GimpVector2
gimp_vector2_new (gdouble x,
gdouble y)
{
GimpVector2 vector;
vector.x = x;
vector.y = y;
return vector;
}
/**
* gimp_vector2_set:
* @vector: a pointer to a #GimpVector2.
* @x: the X coordinate.
* @y: the Y coordinate.
*
* Sets the X and Y coordinates of @vector to @x and @y.
**/
void
gimp_vector2_set (GimpVector2 *vector,
gdouble x,
gdouble y)
{
vector->x = x;
vector->y = y;
}
/**
* gimp_vector2_length:
* @vector: a pointer to a #GimpVector2.
*
* Computes the length of a 2D vector.
*
* Returns: the length of @vector (a positive gdouble).
**/
gdouble
gimp_vector2_length (const GimpVector2 *vector)
{
return (sqrt (vector->x * vector->x + vector->y * vector->y));
}
/**
* gimp_vector2_length_val:
* @vector: a #GimpVector2.
*
* This function is identical to gimp_vector2_length() but the
* vector is passed by value rather than by reference.
*
* Returns: the length of @vector (a positive gdouble).
**/
gdouble
gimp_vector2_length_val (GimpVector2 vector)
{
return (sqrt (vector.x * vector.x + vector.y * vector.y));
}
/**
* gimp_vector2_mul:
* @vector: a pointer to a #GimpVector2.
* @factor: a scalar.
*
* Multiplies each component of the @vector by @factor. Note that this
* is equivalent to multiplying the vectors length by @factor.
**/
void
gimp_vector2_mul (GimpVector2 *vector,
gdouble factor)
{
vector->x *= factor;
vector->y *= factor;
}
/**
* gimp_vector2_mul_val:
* @vector: a #GimpVector2.
* @factor: a scalar.
*
* This function is identical to gimp_vector2_mul() but the vector is
* passed by value rather than by reference.
*
* Returns: the resulting #GimpVector2.
**/
GimpVector2
gimp_vector2_mul_val (GimpVector2 vector,
gdouble factor)
{
GimpVector2 result;
result.x = vector.x * factor;
result.y = vector.y * factor;
return result;
}
/**
* gimp_vector2_normalize:
* @vector: a pointer to a #GimpVector2.
*
* Normalizes the @vector so the length of the @vector is 1.0. The nul
* vector will not be changed.
**/
void
gimp_vector2_normalize (GimpVector2 *vector)
{
gdouble len;
len = gimp_vector2_length (vector);
if (len != 0.0)
{
len = 1.0 / len;
vector->x *= len;
vector->y *= len;
}
else
{
*vector = gimp_vector2_zero;
}
}
/**
* gimp_vector2_normalize_val:
* @vector: a #GimpVector2.
*
* This function is identical to gimp_vector2_normalize() but the
* vector is passed by value rather than by reference.
*
* Returns: a #GimpVector2 parallel to @vector, pointing in the same
* direction but with a length of 1.0.
**/
GimpVector2
gimp_vector2_normalize_val (GimpVector2 vector)
{
GimpVector2 normalized;
gdouble len;
len = gimp_vector2_length_val (vector);
if (len != 0.0)
{
len = 1.0 / len;
normalized.x = vector.x * len;
normalized.y = vector.y * len;
return normalized;
}
else
{
return gimp_vector2_zero;
}
}
/**
* gimp_vector2_neg:
* @vector: a pointer to a #GimpVector2.
*
* Negates the @vector (i.e. negate all its coordinates).
**/
void
gimp_vector2_neg (GimpVector2 *vector)
{
vector->x *= -1.0;
vector->y *= -1.0;
}
/**
* gimp_vector2_neg_val:
* @vector: a #GimpVector2.
*
* This function is identical to gimp_vector2_neg() but the vector
* is passed by value rather than by reference.
*
* Returns: the negated #GimpVector2.
**/
GimpVector2
gimp_vector2_neg_val (GimpVector2 vector)
{
GimpVector2 result;
result.x = vector.x * -1.0;
result.y = vector.y * -1.0;
return result;
}
/**
* gimp_vector2_add:
* @result: destination for the resulting #GimpVector2.
* @vector1: a pointer to the first #GimpVector2.
* @vector2: a pointer to the second #GimpVector2.
*
* Computes the sum of two 2D vectors. The resulting #GimpVector2 is
* stored in @result.
**/
void
gimp_vector2_add (GimpVector2 *result,
const GimpVector2 *vector1,
const GimpVector2 *vector2)
{
result->x = vector1->x + vector2->x;
result->y = vector1->y + vector2->y;
}
/**
* gimp_vector2_add_val:
* @vector1: the first #GimpVector2.
* @vector2: the second #GimpVector2.
*
* This function is identical to gimp_vector2_add() but the vectors
* are passed by value rather than by reference.
*
* Returns: the resulting #GimpVector2.
**/
GimpVector2
gimp_vector2_add_val (GimpVector2 vector1,
GimpVector2 vector2)
{
GimpVector2 result;
result.x = vector1.x + vector2.x;
result.y = vector1.y + vector2.y;
return result;
}
/**
* gimp_vector2_sub:
* @result: the destination for the resulting #GimpVector2.
* @vector1: a pointer to the first #GimpVector2.
* @vector2: a pointer to the second #GimpVector2.
*
* Computes the difference of two 2D vectors (@vector1 minus @vector2).
* The resulting #GimpVector2 is stored in @result.
**/
void
gimp_vector2_sub (GimpVector2 *result,
const GimpVector2 *vector1,
const GimpVector2 *vector2)
{
result->x = vector1->x - vector2->x;
result->y = vector1->y - vector2->y;
}
/**
* gimp_vector2_sub_val:
* @vector1: the first #GimpVector2.
* @vector2: the second #GimpVector2.
*
* This function is identical to gimp_vector2_sub() but the vectors
* are passed by value rather than by reference.
*
* Returns: the resulting #GimpVector2.
**/
GimpVector2
gimp_vector2_sub_val (GimpVector2 vector1,
GimpVector2 vector2)
{
GimpVector2 result;
result.x = vector1.x - vector2.x;
result.y = vector1.y - vector2.y;
return result;
}
/**
* gimp_vector2_inner_product:
* @vector1: a pointer to the first #GimpVector2.
* @vector2: a pointer to the second #GimpVector2.
*
* Computes the inner (dot) product of two 2D vectors.
* This product is zero if and only if the two vectors are orthognal.
*
* Returns: The inner product.
**/
gdouble
gimp_vector2_inner_product (const GimpVector2 *vector1,
const GimpVector2 *vector2)
{
return (vector1->x * vector2->x + vector1->y * vector2->y);
}
/**
* gimp_vector2_inner_product_val:
* @vector1: the first #GimpVector2.
* @vector2: the second #GimpVector2.
*
* This function is identical to gimp_vector2_inner_product() but the
* vectors are passed by value rather than by reference.
*
* Returns: The inner product.
**/
gdouble
gimp_vector2_inner_product_val (GimpVector2 vector1,
GimpVector2 vector2)
{
return (vector1.x * vector2.x + vector1.y * vector2.y);
}
/**
* gimp_vector2_cross_product:
* @vector1: a pointer to the first #GimpVector2.
* @vector2: a pointer to the second #GimpVector2.
*
* Compute the cross product of two vectors. The result is a
* #GimpVector2 which is orthognal to both @vector1 and @vector2. If
* @vector1 and @vector2 are parallel, the result will be the nul
* vector.
*
* Note that in 2D, this function is useful to test if two vectors are
* parallel or not, or to compute the area spawned by two vectors.
*
* Returns: The cross product.
**/
GimpVector2
gimp_vector2_cross_product (const GimpVector2 *vector1,
const GimpVector2 *vector2)
{
GimpVector2 normal;
normal.x = vector1->x * vector2->y - vector1->y * vector2->x;
normal.y = vector1->y * vector2->x - vector1->x * vector2->y;
return normal;
}
/**
* gimp_vector2_cross_product_val:
* @vector1: the first #GimpVector2.
* @vector2: the second #GimpVector2.
*
* This function is identical to gimp_vector2_cross_product() but the
* vectors are passed by value rather than by reference.
*
* Returns: The cross product.
**/
GimpVector2
gimp_vector2_cross_product_val (GimpVector2 vector1,
GimpVector2 vector2)
{
GimpVector2 normal;
normal.x = vector1.x * vector2.y - vector1.y * vector2.x;
normal.y = vector1.y * vector2.x - vector1.x * vector2.y;
return normal;
}
/**
* gimp_vector2_rotate:
* @vector: a pointer to a #GimpVector2.
* @alpha: an angle (in radians).
*
* Rotates the @vector counterclockwise by @alpha radians.
**/
void
gimp_vector2_rotate (GimpVector2 *vector,
gdouble alpha)
{
GimpVector2 result;
result.x = cos (alpha) * vector->x + sin (alpha) * vector->y;
result.y = cos (alpha) * vector->y - sin (alpha) * vector->x;
*vector = result;
}
/**
* gimp_vector2_rotate_val:
* @vector: a #GimpVector2.
* @alpha: an angle (in radians).
*
* This function is identical to gimp_vector2_rotate() but the vector
* is passed by value rather than by reference.
*
* Returns: a #GimpVector2 representing @vector rotated by @alpha
* radians.
**/
GimpVector2
gimp_vector2_rotate_val (GimpVector2 vector,
gdouble alpha)
{
GimpVector2 result;
result.x = cos (alpha) * vector.x + sin (alpha) * vector.y;
result.y = cos (alpha) * vector.y - sin (alpha) * vector.x;
return result;
}
/**
* gimp_vector2_normal:
* @vector: a pointer to a #GimpVector2.
*
* Compute a normalized perpendicular vector to @vector
*
* Returns: a #GimpVector2 perpendicular to @vector, with a length of 1.0.
*
* Since: 2.8
**/
GimpVector2
gimp_vector2_normal (GimpVector2 *vector)
{
GimpVector2 result;
result.x = - vector->y;
result.y = vector->x;
gimp_vector2_normalize (&result);
return result;
}
/**
* gimp_vector2_normal_val:
* @vector: a #GimpVector2.
*
* This function is identical to gimp_vector2_normal() but the vector
* is passed by value rather than by reference.
*
* Returns: a #GimpVector2 perpendicular to @vector, with a length of 1.0.
*
* Since: 2.8
**/
GimpVector2
gimp_vector2_normal_val (GimpVector2 vector)
{
GimpVector2 result;
result.x = - vector.y;
result.y = vector.x;
gimp_vector2_normalize (&result);
return result;
}
/**************************************/
/* Three dimensional vector functions */
/**************************************/
/**
* gimp_vector3_new:
* @x: the X coordinate.
* @y: the Y coordinate.
* @z: the Z coordinate.
*
* Creates a #GimpVector3 of coordinate @x, @y and @z.
*
* Returns: the resulting #GimpVector3.
**/
GimpVector3
gimp_vector3_new (gdouble x,
gdouble y,
gdouble z)
{
GimpVector3 vector;
vector.x = x;
vector.y = y;
vector.z = z;
return vector;
}
/**
* gimp_vector3_set:
* @vector: a pointer to a #GimpVector3.
* @x: the X coordinate.
* @y: the Y coordinate.
* @z: the Z coordinate.
*
* Sets the X, Y and Z coordinates of @vector to @x, @y and @z.
**/
void
gimp_vector3_set (GimpVector3 *vector,
gdouble x,
gdouble y,
gdouble z)
{
vector->x = x;
vector->y = y;
vector->z = z;
}
/**
* gimp_vector3_length:
* @vector: a pointer to a #GimpVector3.
*
* Computes the length of a 3D vector.
*
* Returns: the length of @vector (a positive gdouble).
**/
gdouble
gimp_vector3_length (const GimpVector3 *vector)
{
return (sqrt (vector->x * vector->x +
vector->y * vector->y +
vector->z * vector->z));
}
/**
* gimp_vector3_length_val:
* @vector: a #GimpVector3.
*
* This function is identical to gimp_vector3_length() but the vector
* is passed by value rather than by reference.
*
* Returns: the length of @vector (a positive gdouble).
**/
gdouble
gimp_vector3_length_val (GimpVector3 vector)
{
return (sqrt (vector.x * vector.x +
vector.y * vector.y +
vector.z * vector.z));
}
/**
* gimp_vector3_mul:
* @vector: a pointer to a #GimpVector3.
* @factor: a scalar.
*
* Multiplies each component of the @vector by @factor. Note that
* this is equivalent to multiplying the vectors length by @factor.
**/
void
gimp_vector3_mul (GimpVector3 *vector,
gdouble factor)
{
vector->x *= factor;
vector->y *= factor;
vector->z *= factor;
}
/**
* gimp_vector3_mul_val:
* @vector: a #GimpVector3.
* @factor: a scalar.
*
* This function is identical to gimp_vector3_mul() but the vector is
* passed by value rather than by reference.
*
* Returns: the resulting #GimpVector3.
**/
GimpVector3
gimp_vector3_mul_val (GimpVector3 vector,
gdouble factor)
{
GimpVector3 result;
result.x = vector.x * factor;
result.y = vector.y * factor;
result.z = vector.z * factor;
return result;
}
/**
* gimp_vector3_normalize:
* @vector: a pointer to a #GimpVector3.
*
* Normalizes the @vector so the length of the @vector is 1.0. The nul
* vector will not be changed.
**/
void
gimp_vector3_normalize (GimpVector3 *vector)
{
gdouble len;
len = gimp_vector3_length (vector);
if (len != 0.0)
{
len = 1.0 / len;
vector->x *= len;
vector->y *= len;
vector->z *= len;
}
else
{
*vector = gimp_vector3_zero;
}
}
/**
* gimp_vector3_normalize_val:
* @vector: a #GimpVector3.
*
* This function is identical to gimp_vector3_normalize() but the
* vector is passed by value rather than by reference.
*
* Returns: a #GimpVector3 parallel to @vector, pointing in the same
* direction but with a length of 1.0.
**/
GimpVector3
gimp_vector3_normalize_val (GimpVector3 vector)
{
GimpVector3 result;
gdouble len;
len = gimp_vector3_length_val (vector);
if (len != 0.0)
{
len = 1.0 / len;
result.x = vector.x * len;
result.y = vector.y * len;
result.z = vector.z * len;
return result;
}
else
{
return gimp_vector3_zero;
}
}
/**
* gimp_vector3_neg:
* @vector: a pointer to a #GimpVector3.
*
* Negates the @vector (i.e. negate all its coordinates).
**/
void
gimp_vector3_neg (GimpVector3 *vector)
{
vector->x *= -1.0;
vector->y *= -1.0;
vector->z *= -1.0;
}
/**
* gimp_vector3_neg_val:
* @vector: a #GimpVector3.
*
* This function is identical to gimp_vector3_neg() but the vector
* is passed by value rather than by reference.
*
* Returns: the negated #GimpVector3.
**/
GimpVector3
gimp_vector3_neg_val (GimpVector3 vector)
{
GimpVector3 result;
result.x = vector.x * -1.0;
result.y = vector.y * -1.0;
result.z = vector.z * -1.0;
return result;
}
/**
* gimp_vector3_add:
* @result: destination for the resulting #GimpVector3.
* @vector1: a pointer to the first #GimpVector3.
* @vector2: a pointer to the second #GimpVector3.
*
* Computes the sum of two 3D vectors. The resulting #GimpVector3 is
* stored in @result.
**/
void
gimp_vector3_add (GimpVector3 *result,
const GimpVector3 *vector1,
const GimpVector3 *vector2)
{
result->x = vector1->x + vector2->x;
result->y = vector1->y + vector2->y;
result->z = vector1->z + vector2->z;
}
/**
* gimp_vector3_add_val:
* @vector1: a #GimpVector3.
* @vector2: a #GimpVector3.
*
* This function is identical to gimp_vector3_add() but the vectors
* are passed by value rather than by reference.
*
* Returns: the resulting #GimpVector3.
**/
GimpVector3
gimp_vector3_add_val (GimpVector3 vector1,
GimpVector3 vector2)
{
GimpVector3 result;
result.x = vector1.x + vector2.x;
result.y = vector1.y + vector2.y;
result.z = vector1.z + vector2.z;
return result;
}
/**
* gimp_vector3_sub:
* @result: the destination for the resulting #GimpVector3.
* @vector1: a pointer to the first #GimpVector3.
* @vector2: a pointer to the second #GimpVector3.
*
* Computes the difference of two 3D vectors (@vector1 minus @vector2).
* The resulting #GimpVector3 is stored in @result.
**/
void
gimp_vector3_sub (GimpVector3 *result,
const GimpVector3 *vector1,
const GimpVector3 *vector2)
{
result->x = vector1->x - vector2->x;
result->y = vector1->y - vector2->y;
result->z = vector1->z - vector2->z;
}
/**
* gimp_vector3_sub_val:
* @vector1: a #GimpVector3.
* @vector2: a #GimpVector3.
*
* This function is identical to gimp_vector3_sub() but the vectors
* are passed by value rather than by reference.
*
* Returns: the resulting #GimpVector3.
**/
GimpVector3
gimp_vector3_sub_val (GimpVector3 vector1,
GimpVector3 vector2)
{
GimpVector3 result;
result.x = vector1.x - vector2.x;
result.y = vector1.y - vector2.y;
result.z = vector1.z - vector2.z;
return result;
}
/**
* gimp_vector3_inner_product:
* @vector1: a pointer to the first #GimpVector3.
* @vector2: a pointer to the second #GimpVector3.
*
* Computes the inner (dot) product of two 3D vectors. This product
* is zero if and only if the two vectors are orthognal.
*
* Returns: The inner product.
**/
gdouble
gimp_vector3_inner_product (const GimpVector3 *vector1,
const GimpVector3 *vector2)
{
return (vector1->x * vector2->x +
vector1->y * vector2->y +
vector1->z * vector2->z);
}
/**
* gimp_vector3_inner_product_val:
* @vector1: the first #GimpVector3.
* @vector2: the second #GimpVector3.
*
* This function is identical to gimp_vector3_inner_product() but the
* vectors are passed by value rather than by reference.
*
* Returns: The inner product.
**/
gdouble
gimp_vector3_inner_product_val (GimpVector3 vector1,
GimpVector3 vector2)
{
return (vector1.x * vector2.x +
vector1.y * vector2.y +
vector1.z * vector2.z);
}
/**
* gimp_vector3_cross_product:
* @vector1: a pointer to the first #GimpVector3.
* @vector2: a pointer to the second #GimpVector3.
*
* Compute the cross product of two vectors. The result is a
* #GimpVector3 which is orthognal to both @vector1 and @vector2. If
* @vector1 and @vector2 and parallel, the result will be the nul
* vector.
*
* This function can be used to compute the normal of the plane
* defined by @vector1 and @vector2.
*
* Returns: The cross product.
**/
GimpVector3
gimp_vector3_cross_product (const GimpVector3 *vector1,
const GimpVector3 *vector2)
{
GimpVector3 normal;
normal.x = vector1->y * vector2->z - vector1->z * vector2->y;
normal.y = vector1->z * vector2->x - vector1->x * vector2->z;
normal.z = vector1->x * vector2->y - vector1->y * vector2->x;
return normal;
}
/**
* gimp_vector3_cross_product_val:
* @vector1: the first #GimpVector3.
* @vector2: the second #GimpVector3.
*
* This function is identical to gimp_vector3_cross_product() but the
* vectors are passed by value rather than by reference.
*
* Returns: The cross product.
**/
GimpVector3
gimp_vector3_cross_product_val (GimpVector3 vector1,
GimpVector3 vector2)
{
GimpVector3 normal;
normal.x = vector1.y * vector2.z - vector1.z * vector2.y;
normal.y = vector1.z * vector2.x - vector1.x * vector2.z;
normal.z = vector1.x * vector2.y - vector1.y * vector2.x;
return normal;
}
/**
* gimp_vector3_rotate:
* @vector: a pointer to a #GimpVector3.
* @alpha: the angle (in radian) of rotation around the Z axis.
* @beta: the angle (in radian) of rotation around the Y axis.
* @gamma: the angle (in radian) of rotation around the X axis.
*
* Rotates the @vector around the three axis (Z, Y, and X) by @alpha,
* @beta and @gamma, respectively.
*
* Note that the order of the rotation is very important. If you
* expect a vector to be rotated around X, and then around Y, you will
* have to call this function twice. Also, it is often wise to call
* this function with only one of @alpha, @beta and @gamma non-zero.
**/
void
gimp_vector3_rotate (GimpVector3 *vector,
gdouble alpha,
gdouble beta,
gdouble gamma)
{
GimpVector3 s, t;
/* First we rotate it around the Z axis (XY plane).. */
/* ================================================= */
s.x = cos (alpha) * vector->x + sin (alpha) * vector->y;
s.y = cos (alpha) * vector->y - sin (alpha) * vector->x;
/* ..then around the Y axis (XZ plane).. */
/* ===================================== */
t = s;
vector->x = cos (beta) *t.x + sin (beta) * vector->z;
s.z = cos (beta) *vector->z - sin (beta) * t.x;
/* ..and at last around the X axis (YZ plane) */
/* ========================================== */
vector->y = cos (gamma) * t.y + sin (gamma) * s.z;
vector->z = cos (gamma) * s.z - sin (gamma) * t.y;
}
/**
* gimp_vector3_rotate_val:
* @vector: a #GimpVector3.
* @alpha: the angle (in radian) of rotation around the Z axis.
* @beta: the angle (in radian) of rotation around the Y axis.
* @gamma: the angle (in radian) of rotation around the X axis.
*
* This function is identical to gimp_vector3_rotate() but the vectors
* are passed by value rather than by reference.
*
* Returns: the rotated vector.
**/
GimpVector3
gimp_vector3_rotate_val (GimpVector3 vector,
gdouble alpha,
gdouble beta,
gdouble gamma)
{
GimpVector3 s, t, result;
/* First we rotate it around the Z axis (XY plane).. */
/* ================================================= */
s.x = cos (alpha) * vector.x + sin (alpha) * vector.y;
s.y = cos (alpha) * vector.y - sin (alpha) * vector.x;
/* ..then around the Y axis (XZ plane).. */
/* ===================================== */
t = s;
result.x = cos (beta) *t.x + sin (beta) * vector.z;
s.z = cos (beta) *vector.z - sin (beta) * t.x;
/* ..and at last around the X axis (YZ plane) */
/* ========================================== */
result.y = cos (gamma) * t.y + sin (gamma) * s.z;
result.z = cos (gamma) * s.z - sin (gamma) * t.y;
return result;
}
/**
* gimp_vector_2d_to_3d:
* @sx: the abscisse of the upper-left screen rectangle.
* @sy: the ordinate of the upper-left screen rectangle.
* @w: the width of the screen rectangle.
* @h: the height of the screen rectangle.
* @x: the abscisse of the point in the screen rectangle to map.
* @y: the ordinate of the point in the screen rectangle to map.
* @vp: the position of the observer.
* @p: the resulting point.
*
* \"Compute screen (sx, sy) - (sx + w, sy + h) to 3D unit square
* mapping. The plane to map to is given in the z field of p. The
* observer is located at position vp (vp->z != 0.0).\"
*
* In other words, this computes the projection of the point (@x, @y)
* to the plane z = @p->z (parallel to XY), from the @vp point of view
* through the screen (@sx, @sy)->(@sx + @w, @sy + @h)
**/
void
gimp_vector_2d_to_3d (gint sx,
gint sy,
gint w,
gint h,
gint x,
gint y,
const GimpVector3 *vp,
GimpVector3 *p)
{
gdouble t = 0.0;
if (vp->x != 0.0)
t = (p->z - vp->z) / vp->z;
if (t != 0.0)
{
p->x = vp->x + t * (vp->x - ((gdouble) (x - sx) / (gdouble) w));
p->y = vp->y + t * (vp->y - ((gdouble) (y - sy) / (gdouble) h));
}
else
{
p->x = (gdouble) (x - sx) / (gdouble) w;
p->y = (gdouble) (y - sy) / (gdouble) h;
}
}
/**
* gimp_vector_2d_to_3d_val:
* @sx: the abscisse of the upper-left screen rectangle.
* @sy: the ordinate of the upper-left screen rectangle.
* @w: the width of the screen rectangle.
* @h: the height of the screen rectangle.
* @x: the abscisse of the point in the screen rectangle to map.
* @y: the ordinate of the point in the screen rectangle to map.
* @vp: position of the observer.
* @p: the resulting point.
*
* This function is identical to gimp_vector_2d_to_3d() but the
* position of the @observer and the resulting point @p are passed by
* value rather than by reference.
*
* Returns: the computed #GimpVector3 point.
**/
GimpVector3
gimp_vector_2d_to_3d_val (gint sx,
gint sy,
gint w,
gint h,
gint x,
gint y,
GimpVector3 vp,
GimpVector3 p)
{
GimpVector3 result;
gdouble t = 0.0;
if (vp.x != 0.0)
t = (p.z - vp.z) / vp.z;
if (t != 0.0)
{
result.x = vp.x + t * (vp.x - ((gdouble) (x - sx) / (gdouble) w));
result.y = vp.y + t * (vp.y - ((gdouble) (y - sy) / (gdouble) h));
}
else
{
result.x = (gdouble) (x - sx) / (gdouble) w;
result.y = (gdouble) (y - sy) / (gdouble) h;
}
result.z = 0;
return result;
}
/**
* gimp_vector_3d_to_2d:
* @sx: the abscisse of the upper-left screen rectangle.
* @sy: the ordinate of the upper-left screen rectangle.
* @w: the width of the screen rectangle.
* @h: the height of the screen rectangle.
* @x: the abscisse of the point in the screen rectangle to map (return value).
* @y: the ordinate of the point in the screen rectangle to map (return value).
* @vp: position of the observer.
* @p: the 3D point to project to the plane.
*
* Convert the given 3D point to 2D (project it onto the viewing
* plane, (sx, sy, 0) - (sx + w, sy + h, 0). The input is assumed to
* be in the unit square (0, 0, z) - (1, 1, z). The viewpoint of the
* observer is passed in vp.
*
* This is basically the opposite of gimp_vector_2d_to_3d().
**/
void
gimp_vector_3d_to_2d (gint sx,
gint sy,
gint w,
gint h,
gdouble *x,
gdouble *y,
const GimpVector3 *vp,
const GimpVector3 *p)
{
gdouble t;
GimpVector3 dir;
gimp_vector3_sub (&dir, p, vp);
gimp_vector3_normalize (&dir);
if (dir.z != 0.0)
{
t = (-1.0 * vp->z) / dir.z;
*x = (gdouble) sx + ((vp->x + t * dir.x) * (gdouble) w);
*y = (gdouble) sy + ((vp->y + t * dir.y) * (gdouble) h);
}
else
{
*x = (gdouble) sx + (p->x * (gdouble) w);
*y = (gdouble) sy + (p->y * (gdouble) h);
}
}
|