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 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468
|
/*
* Clutter.
*
* An OpenGL based 'interactive canvas' library.
*
* Authored By Matthew Allum <mallum@openedhand.com>
*
* Copyright (C) 2006 OpenedHand
*
* 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 2 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/>.
*/
/**
* SECTION:clutter-geometric-types
* @Title: Base geometric types
* @Short_Description: Common geometric data types used by Clutter
*
* Clutter defines a set of geometric data structures that are commonly used
* across the whole API.
*/
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include "clutter-types.h"
#include "clutter-private.h"
#include <math.h>
#define FLOAT_EPSILON (1e-15)
/*
* ClutterGeometry
*/
static ClutterGeometry*
clutter_geometry_copy (const ClutterGeometry *geometry)
{
return g_slice_dup (ClutterGeometry, geometry);
}
static void
clutter_geometry_free (ClutterGeometry *geometry)
{
if (G_LIKELY (geometry != NULL))
g_slice_free (ClutterGeometry, geometry);
}
/**
* clutter_geometry_union:
* @geometry_a: a #ClutterGeometry
* @geometry_b: another #ClutterGeometry
* @result: (out): location to store the result
*
* Find the union of two rectangles represented as #ClutterGeometry.
*
* Since: 1.4
*
* Deprecated: 1.16: Use #ClutterRect and clutter_rect_union()
*/
void
clutter_geometry_union (const ClutterGeometry *geometry_a,
const ClutterGeometry *geometry_b,
ClutterGeometry *result)
{
/* We don't try to handle rectangles that can't be represented
* as a signed integer box */
gint x_1 = MIN (geometry_a->x, geometry_b->x);
gint y_1 = MIN (geometry_a->y, geometry_b->y);
gint x_2 = MAX (geometry_a->x + (gint)geometry_a->width,
geometry_b->x + (gint)geometry_b->width);
gint y_2 = MAX (geometry_a->y + (gint)geometry_a->height,
geometry_b->y + (gint)geometry_b->height);
result->x = x_1;
result->y = y_1;
result->width = x_2 - x_1;
result->height = y_2 - y_1;
}
/**
* clutter_geometry_intersects:
* @geometry0: The first geometry to test
* @geometry1: The second geometry to test
*
* Determines if @geometry0 and geometry1 intersect returning %TRUE if
* they do else %FALSE.
*
* Return value: %TRUE of @geometry0 and geometry1 intersect else
* %FALSE.
*
* Since: 1.4
*
* Deprecated: 1.16: Use #ClutterRect and clutter_rect_intersection()
*/
gboolean
clutter_geometry_intersects (const ClutterGeometry *geometry0,
const ClutterGeometry *geometry1)
{
if (geometry1->x >= (geometry0->x + (gint)geometry0->width) ||
geometry1->y >= (geometry0->y + (gint)geometry0->height) ||
(geometry1->x + (gint)geometry1->width) <= geometry0->x ||
(geometry1->y + (gint)geometry1->height) <= geometry0->y)
return FALSE;
else
return TRUE;
}
static gboolean
clutter_geometry_progress (const GValue *a,
const GValue *b,
gdouble progress,
GValue *retval)
{
const ClutterGeometry *a_geom = g_value_get_boxed (a);
const ClutterGeometry *b_geom = g_value_get_boxed (b);
ClutterGeometry res = { 0, };
gint a_width = a_geom->width;
gint b_width = b_geom->width;
gint a_height = a_geom->height;
gint b_height = b_geom->height;
res.x = a_geom->x + (b_geom->x - a_geom->x) * progress;
res.y = a_geom->y + (b_geom->y - a_geom->y) * progress;
res.width = a_width + (b_width - a_width) * progress;
res.height = a_height + (b_height - a_height) * progress;
g_value_set_boxed (retval, &res);
return TRUE;
}
G_DEFINE_BOXED_TYPE_WITH_CODE (ClutterGeometry, clutter_geometry,
clutter_geometry_copy,
clutter_geometry_free,
CLUTTER_REGISTER_INTERVAL_PROGRESS (clutter_geometry_progress));
/*
* ClutterVertices
*/
/**
* clutter_vertex_new:
* @x: X coordinate
* @y: Y coordinate
* @z: Z coordinate
*
* Creates a new #ClutterVertex for the point in 3D space
* identified by the 3 coordinates @x, @y, @z.
*
* This function is the logical equivalent of:
*
* |[
* clutter_vertex_init (clutter_vertex_alloc (), x, y, z);
* ]|
*
* Return value: (transfer full): the newly allocated #ClutterVertex.
* Use clutter_vertex_free() to free the resources
*
* Since: 1.0
*/
ClutterVertex *
clutter_vertex_new (gfloat x,
gfloat y,
gfloat z)
{
return clutter_vertex_init (clutter_vertex_alloc (), x, y, z);
}
/**
* clutter_vertex_alloc: (constructor)
*
* Allocates a new, empty #ClutterVertex.
*
* Return value: (transfer full): the newly allocated #ClutterVertex.
* Use clutter_vertex_free() to free its resources
*
* Since: 1.12
*/
ClutterVertex *
clutter_vertex_alloc (void)
{
return g_slice_new0 (ClutterVertex);
}
/**
* clutter_vertex_init:
* @vertex: a #ClutterVertex
* @x: X coordinate
* @y: Y coordinate
* @z: Z coordinate
*
* Initializes @vertex with the given coordinates.
*
* Return value: (transfer none): the initialized #ClutterVertex
*
* Since: 1.10
*/
ClutterVertex *
clutter_vertex_init (ClutterVertex *vertex,
gfloat x,
gfloat y,
gfloat z)
{
g_return_val_if_fail (vertex != NULL, NULL);
vertex->x = x;
vertex->y = y;
vertex->z = z;
return vertex;
}
/**
* clutter_vertex_copy:
* @vertex: a #ClutterVertex
*
* Copies @vertex
*
* Return value: (transfer full): a newly allocated copy of #ClutterVertex.
* Use clutter_vertex_free() to free the allocated resources
*
* Since: 1.0
*/
ClutterVertex *
clutter_vertex_copy (const ClutterVertex *vertex)
{
if (G_LIKELY (vertex != NULL))
return g_slice_dup (ClutterVertex, vertex);
return NULL;
}
/**
* clutter_vertex_free:
* @vertex: a #ClutterVertex
*
* Frees a #ClutterVertex allocated using clutter_vertex_alloc() or
* clutter_vertex_copy().
*
* Since: 1.0
*/
void
clutter_vertex_free (ClutterVertex *vertex)
{
if (G_UNLIKELY (vertex != NULL))
g_slice_free (ClutterVertex, vertex);
}
/**
* clutter_vertex_equal:
* @vertex_a: a #ClutterVertex
* @vertex_b: a #ClutterVertex
*
* Compares @vertex_a and @vertex_b for equality
*
* Return value: %TRUE if the passed #ClutterVertex are equal
*
* Since: 1.0
*/
gboolean
clutter_vertex_equal (const ClutterVertex *vertex_a,
const ClutterVertex *vertex_b)
{
g_return_val_if_fail (vertex_a != NULL && vertex_b != NULL, FALSE);
if (vertex_a == vertex_b)
return TRUE;
return fabsf (vertex_a->x - vertex_b->x) < FLOAT_EPSILON &&
fabsf (vertex_a->y - vertex_b->y) < FLOAT_EPSILON &&
fabsf (vertex_a->z - vertex_b->z) < FLOAT_EPSILON;
}
static void
clutter_vertex_interpolate (const ClutterVertex *a,
const ClutterVertex *b,
double progress,
ClutterVertex *res)
{
res->x = a->x + (b->x - a->x) * progress;
res->y = a->y + (b->y - a->y) * progress;
res->z = a->z + (b->z - a->z) * progress;
}
static gboolean
clutter_vertex_progress (const GValue *a,
const GValue *b,
gdouble progress,
GValue *retval)
{
const ClutterVertex *av = g_value_get_boxed (a);
const ClutterVertex *bv = g_value_get_boxed (b);
ClutterVertex res;
clutter_vertex_interpolate (av, bv, progress, &res);
g_value_set_boxed (retval, &res);
return TRUE;
}
G_DEFINE_BOXED_TYPE_WITH_CODE (ClutterVertex, clutter_vertex,
clutter_vertex_copy,
clutter_vertex_free,
CLUTTER_REGISTER_INTERVAL_PROGRESS (clutter_vertex_progress));
/*
* ClutterMargin
*/
/**
* clutter_margin_new:
*
* Creates a new #ClutterMargin.
*
* Return value: (transfer full): a newly allocated #ClutterMargin. Use
* clutter_margin_free() to free the resources associated with it when
* done.
*
* Since: 1.10
*/
ClutterMargin *
clutter_margin_new (void)
{
return g_slice_new0 (ClutterMargin);
}
/**
* clutter_margin_copy:
* @margin_: a #ClutterMargin
*
* Creates a new #ClutterMargin and copies the contents of @margin_ into
* the newly created structure.
*
* Return value: (transfer full): a copy of the #ClutterMargin.
*
* Since: 1.10
*/
ClutterMargin *
clutter_margin_copy (const ClutterMargin *margin_)
{
if (G_LIKELY (margin_ != NULL))
return g_slice_dup (ClutterMargin, margin_);
return NULL;
}
/**
* clutter_margin_free:
* @margin_: a #ClutterMargin
*
* Frees the resources allocated by clutter_margin_new() and
* clutter_margin_copy().
*
* Since: 1.10
*/
void
clutter_margin_free (ClutterMargin *margin_)
{
if (G_LIKELY (margin_ != NULL))
g_slice_free (ClutterMargin, margin_);
}
G_DEFINE_BOXED_TYPE (ClutterMargin, clutter_margin,
clutter_margin_copy,
clutter_margin_free)
/*
* ClutterPoint
*/
static const ClutterPoint _clutter_point_zero = CLUTTER_POINT_INIT_ZERO;
/**
* clutter_point_zero:
*
* A point centered at (0, 0).
*
* The returned value can be used as a guard.
*
* Return value: a point centered in (0, 0); the returned #ClutterPoint
* is owned by Clutter and it should not be modified or freed.
*
* Since: 1.12
*/
const ClutterPoint *
clutter_point_zero (void)
{
return &_clutter_point_zero;
}
/**
* clutter_point_alloc: (constructor)
*
* Allocates a new #ClutterPoint.
*
* Return value: (transfer full): the newly allocated #ClutterPoint.
* Use clutter_point_free() to free its resources.
*
* Since: 1.12
*/
ClutterPoint *
clutter_point_alloc (void)
{
return g_slice_new0 (ClutterPoint);
}
/**
* clutter_point_init:
* @point: a #ClutterPoint
* @x: the X coordinate of the point
* @y: the Y coordinate of the point
*
* Initializes @point with the given coordinates.
*
* Return value: (transfer none): the initialized #ClutterPoint
*
* Since: 1.12
*/
ClutterPoint *
clutter_point_init (ClutterPoint *point,
float x,
float y)
{
g_return_val_if_fail (point != NULL, NULL);
point->x = x;
point->y = y;
return point;
}
/**
* clutter_point_copy:
* @point: a #ClutterPoint
*
* Creates a new #ClutterPoint with the same coordinates of @point.
*
* Return value: (transfer full): a newly allocated #ClutterPoint.
* Use clutter_point_free() to free its resources.
*
* Since: 1.12
*/
ClutterPoint *
clutter_point_copy (const ClutterPoint *point)
{
return g_slice_dup (ClutterPoint, point);
}
/**
* clutter_point_free:
* @point: a #ClutterPoint
*
* Frees the resources allocated for @point.
*
* Since: 1.12
*/
void
clutter_point_free (ClutterPoint *point)
{
if (point != NULL && point != &_clutter_point_zero)
g_slice_free (ClutterPoint, point);
}
/**
* clutter_point_equals:
* @a: the first #ClutterPoint to compare
* @b: the second #ClutterPoint to compare
*
* Compares two #ClutterPoint for equality.
*
* Return value: %TRUE if the #ClutterPoints are equal
*
* Since: 1.12
*/
gboolean
clutter_point_equals (const ClutterPoint *a,
const ClutterPoint *b)
{
if (a == b)
return TRUE;
if (a == NULL || b == NULL)
return FALSE;
return fabsf (a->x - b->x) < FLOAT_EPSILON &&
fabsf (a->y - b->y) < FLOAT_EPSILON;
}
/**
* clutter_point_distance:
* @a: a #ClutterPoint
* @b: a #ClutterPoint
* @x_distance: (out) (allow-none): return location for the horizontal
* distance between the points
* @y_distance: (out) (allow-none): return location for the vertical
* distance between the points
*
* Computes the distance between two #ClutterPoint.
*
* Return value: the distance between the points.
*
* Since: 1.12
*/
float
clutter_point_distance (const ClutterPoint *a,
const ClutterPoint *b,
float *x_distance,
float *y_distance)
{
float x_d, y_d;
g_return_val_if_fail (a != NULL, 0.f);
g_return_val_if_fail (b != NULL, 0.f);
if (clutter_point_equals (a, b))
return 0.f;
x_d = (a->x - b->x);
y_d = (a->y - b->y);
if (x_distance != NULL)
*x_distance = fabsf (x_d);
if (y_distance != NULL)
*y_distance = fabsf (y_d);
return sqrt ((x_d * x_d) + (y_d * y_d));
}
static gboolean
clutter_point_progress (const GValue *a,
const GValue *b,
gdouble progress,
GValue *retval)
{
const ClutterPoint *ap = g_value_get_boxed (a);
const ClutterPoint *bp = g_value_get_boxed (b);
ClutterPoint res = CLUTTER_POINT_INIT (0, 0);
res.x = ap->x + (bp->x - ap->x) * progress;
res.y = ap->y + (bp->y - ap->y) * progress;
g_value_set_boxed (retval, &res);
return TRUE;
}
G_DEFINE_BOXED_TYPE_WITH_CODE (ClutterPoint, clutter_point,
clutter_point_copy,
clutter_point_free,
CLUTTER_REGISTER_INTERVAL_PROGRESS (clutter_point_progress))
/*
* ClutterSize
*/
/**
* clutter_size_alloc: (constructor)
*
* Allocates a new #ClutterSize.
*
* Return value: (transfer full): the newly allocated #ClutterSize.
* Use clutter_size_free() to free its resources.
*
* Since: 1.12
*/
ClutterSize *
clutter_size_alloc (void)
{
return g_slice_new0 (ClutterSize);
}
/**
* clutter_size_init:
* @size: a #ClutterSize
* @width: the width
* @height: the height
*
* Initializes a #ClutterSize with the given dimensions.
*
* Return value: (transfer none): the initialized #ClutterSize
*
* Since: 1.12
*/
ClutterSize *
clutter_size_init (ClutterSize *size,
float width,
float height)
{
g_return_val_if_fail (size != NULL, NULL);
size->width = width;
size->height = height;
return size;
}
/**
* clutter_size_copy:
* @size: a #ClutterSize
*
* Creates a new #ClutterSize and duplicates @size.
*
* Return value: (transfer full): the newly allocated #ClutterSize.
* Use clutter_size_free() to free its resources.
*
* Since: 1.12
*/
ClutterSize *
clutter_size_copy (const ClutterSize *size)
{
return g_slice_dup (ClutterSize, size);
}
/**
* clutter_size_free:
* @size: a #ClutterSize
*
* Frees the resources allocated for @size.
*
* Since: 1.12
*/
void
clutter_size_free (ClutterSize *size)
{
if (size != NULL)
g_slice_free (ClutterSize, size);
}
/**
* clutter_size_equals:
* @a: a #ClutterSize to compare
* @b: a #ClutterSize to compare
*
* Compares two #ClutterSize for equality.
*
* Return value: %TRUE if the two #ClutterSize are equal
*
* Since: 1.12
*/
gboolean
clutter_size_equals (const ClutterSize *a,
const ClutterSize *b)
{
if (a == b)
return TRUE;
if (a == NULL || b == NULL)
return FALSE;
return fabsf (a->width - b->width) < FLOAT_EPSILON &&
fabsf (a->height - b->height) < FLOAT_EPSILON;
}
static gboolean
clutter_size_progress (const GValue *a,
const GValue *b,
gdouble progress,
GValue *retval)
{
const ClutterSize *as = g_value_get_boxed (a);
const ClutterSize *bs = g_value_get_boxed (b);
ClutterSize res = CLUTTER_SIZE_INIT (0, 0);
res.width = as->width + (bs->width - as->width) * progress;
res.height = as->height + (bs->height - as->height) * progress;
g_value_set_boxed (retval, &res);
return TRUE;
}
G_DEFINE_BOXED_TYPE_WITH_CODE (ClutterSize, clutter_size,
clutter_size_copy,
clutter_size_free,
CLUTTER_REGISTER_INTERVAL_PROGRESS (clutter_size_progress))
/*
* ClutterRect
*/
static const ClutterRect _clutter_rect_zero = CLUTTER_RECT_INIT_ZERO;
static gboolean clutter_rect_progress (const GValue *a,
const GValue *b,
gdouble progress,
GValue *res);
G_DEFINE_BOXED_TYPE_WITH_CODE (ClutterRect, clutter_rect,
clutter_rect_copy,
clutter_rect_free,
CLUTTER_REGISTER_INTERVAL_PROGRESS (clutter_rect_progress))
static inline void
clutter_rect_normalize_internal (ClutterRect *rect)
{
if (rect->size.width >= 0.f && rect->size.height >= 0.f)
return;
if (rect->size.width < 0.f)
{
float size = fabsf (rect->size.width);
rect->origin.x -= size;
rect->size.width = size;
}
if (rect->size.height < 0.f)
{
float size = fabsf (rect->size.height);
rect->origin.y -= size;
rect->size.height = size;
}
}
/**
* clutter_rect_zero:
*
* A #ClutterRect with #ClutterRect.origin set at (0, 0) and a size
* of 0.
*
* The returned value can be used as a guard.
*
* Return value: a rectangle with origin in (0, 0) and a size of 0.
* The returned #ClutterRect is owned by Clutter and it should not
* be modified or freed.
*
* Since: 1.12
*/
const ClutterRect *
clutter_rect_zero (void)
{
return &_clutter_rect_zero;
}
/**
* clutter_rect_alloc: (constructor)
*
* Creates a new, empty #ClutterRect.
*
* You can use clutter_rect_init() to initialize the returned rectangle,
* for instance:
*
* |[
* rect = clutter_rect_init (clutter_rect_alloc (), x, y, width, height);
* ]|
*
* Return value: (transfer full): the newly allocated #ClutterRect.
* Use clutter_rect_free() to free its resources
*
* Since: 1.12
*/
ClutterRect *
clutter_rect_alloc (void)
{
return g_slice_new0 (ClutterRect);
}
/**
* clutter_rect_init:
* @rect: a #ClutterRect
* @x: X coordinate of the origin
* @y: Y coordinate of the origin
* @width: width of the rectangle
* @height: height of the rectangle
*
* Initializes a #ClutterRect with the given origin and size.
*
* Return value: (transfer none): the updated rectangle
*
* Since: 1.12
*/
ClutterRect *
clutter_rect_init (ClutterRect *rect,
float x,
float y,
float width,
float height)
{
g_return_val_if_fail (rect != NULL, NULL);
rect->origin.x = x;
rect->origin.y = y;
rect->size.width = width;
rect->size.height = height;
return rect;
}
/**
* clutter_rect_copy:
* @rect: a #ClutterRect
*
* Copies @rect into a new #ClutterRect instance.
*
* Return value: (transfer full): the newly allocate copy of @rect.
* Use clutter_rect_free() to free the associated resources
*
* Since: 1.12
*/
ClutterRect *
clutter_rect_copy (const ClutterRect *rect)
{
if (rect != NULL)
{
ClutterRect *res;
res = g_slice_dup (ClutterRect, rect);
clutter_rect_normalize_internal (res);
return res;
}
return NULL;
}
/**
* clutter_rect_free:
* @rect: a #ClutterRect
*
* Frees the resources allocated by @rect.
*
* Since: 1.12
*/
void
clutter_rect_free (ClutterRect *rect)
{
if (rect != NULL && rect != &_clutter_rect_zero)
g_slice_free (ClutterRect, rect);
}
/**
* clutter_rect_equals:
* @a: a #ClutterRect
* @b: a #ClutterRect
*
* Checks whether @a and @b are equals.
*
* This function will normalize both @a and @b before comparing
* their origin and size.
*
* Return value: %TRUE if the rectangles match in origin and size.
*
* Since: 1.12
*/
gboolean
clutter_rect_equals (ClutterRect *a,
ClutterRect *b)
{
if (a == b)
return TRUE;
if (a == NULL || b == NULL)
return FALSE;
clutter_rect_normalize_internal (a);
clutter_rect_normalize_internal (b);
return clutter_point_equals (&a->origin, &b->origin) &&
clutter_size_equals (&a->size, &b->size);
}
/**
* clutter_rect_normalize:
* @rect: a #ClutterRect
*
* Normalizes a #ClutterRect.
*
* A #ClutterRect is defined by the area covered by its size; this means
* that a #ClutterRect with #ClutterRect.origin in [ 0, 0 ] and a
* #ClutterRect.size of [ 10, 10 ] is equivalent to a #ClutterRect with
* #ClutterRect.origin in [ 10, 10 ] and a #ClutterRect.size of [ -10, -10 ].
*
* This function is useful to ensure that a rectangle has positive width
* and height; it will modify the passed @rect and normalize its size.
*
* Since: 1.12
*/
ClutterRect *
clutter_rect_normalize (ClutterRect *rect)
{
g_return_val_if_fail (rect != NULL, NULL);
clutter_rect_normalize_internal (rect);
return rect;
}
/**
* clutter_rect_get_center:
* @rect: a #ClutterRect
* @center: (out caller-allocates): a #ClutterPoint
*
* Retrieves the center of @rect, after normalizing the rectangle,
* and updates @center with the correct coordinates.
*
* Since: 1.12
*/
void
clutter_rect_get_center (ClutterRect *rect,
ClutterPoint *center)
{
g_return_if_fail (rect != NULL);
g_return_if_fail (center != NULL);
clutter_rect_normalize_internal (rect);
center->x = rect->origin.x + (rect->size.width / 2.0f);
center->y = rect->origin.y + (rect->size.height / 2.0f);
}
/**
* clutter_rect_contains_point:
* @rect: a #ClutterRect
* @point: the point to check
*
* Checks whether @point is contained by @rect, after normalizing the
* rectangle.
*
* Return value: %TRUE if the @point is contained by @rect.
*
* Since: 1.12
*/
gboolean
clutter_rect_contains_point (ClutterRect *rect,
ClutterPoint *point)
{
g_return_val_if_fail (rect != NULL, FALSE);
g_return_val_if_fail (point != NULL, FALSE);
clutter_rect_normalize_internal (rect);
return (point->x >= rect->origin.x) &&
(point->y >= rect->origin.y) &&
(point->x <= (rect->origin.x + rect->size.width)) &&
(point->y <= (rect->origin.y + rect->size.height));
}
/**
* clutter_rect_contains_rect:
* @a: a #ClutterRect
* @b: a #ClutterRect
*
* Checks whether @a contains @b.
*
* The first rectangle contains the second if the union of the
* two #ClutterRect is equal to the first rectangle.
*
* Return value: %TRUE if the first rectangle contains the second.
*
* Since: 1.12
*/
gboolean
clutter_rect_contains_rect (ClutterRect *a,
ClutterRect *b)
{
ClutterRect res;
g_return_val_if_fail (a != NULL, FALSE);
g_return_val_if_fail (b != NULL, FALSE);
clutter_rect_union (a, b, &res);
return clutter_rect_equals (a, &res);
}
/**
* clutter_rect_union:
* @a: a #ClutterRect
* @b: a #ClutterRect
* @res: (out caller-allocates): a #ClutterRect
*
* Computes the smallest possible rectangle capable of fully containing
* both @a and @b, and places it into @res.
*
* This function will normalize both @a and @b prior to computing their
* union.
*
* Since: 1.12
*/
void
clutter_rect_union (ClutterRect *a,
ClutterRect *b,
ClutterRect *res)
{
g_return_if_fail (a != NULL);
g_return_if_fail (b != NULL);
g_return_if_fail (res != NULL);
clutter_rect_normalize_internal (a);
clutter_rect_normalize_internal (b);
res->origin.x = MIN (a->origin.x, b->origin.x);
res->origin.y = MIN (a->origin.y, b->origin.y);
res->size.width = MAX (a->size.width, b->size.width);
res->size.height = MAX (a->size.height, b->size.height);
}
/**
* clutter_rect_intersection:
* @a: a #ClutterRect
* @b: a #ClutterRect
* @res: (out caller-allocates) (allow-none): a #ClutterRect, or %NULL
*
* Computes the intersection of @a and @b, and places it in @res, if @res
* is not %NULL.
*
* This function will normalize both @a and @b prior to computing their
* intersection.
*
* This function can be used to simply check if the intersection of @a and @b
* is not empty, by using %NULL for @res.
*
* Return value: %TRUE if the intersection of @a and @b is not empty
*
* Since: 1.12
*/
gboolean
clutter_rect_intersection (ClutterRect *a,
ClutterRect *b,
ClutterRect *res)
{
float x_1, y_1, x_2, y_2;
g_return_val_if_fail (a != NULL, FALSE);
g_return_val_if_fail (b != NULL, FALSE);
clutter_rect_normalize_internal (a);
clutter_rect_normalize_internal (b);
x_1 = MAX (a->origin.x, b->origin.x);
y_1 = MAX (a->origin.y, b->origin.y);
x_2 = MIN (a->origin.x + a->size.width, b->origin.x + b->size.width);
y_2 = MIN (a->origin.y + a->size.height, b->origin.y + b->size.height);
if (x_1 >= x_2 || y_1 >= y_2)
{
if (res != NULL)
clutter_rect_init (res, 0.f, 0.f, 0.f, 0.f);
return FALSE;
}
if (res != NULL)
clutter_rect_init (res, x_1, y_1, x_2 - x_1, y_2 - y_1);
return TRUE;
}
/**
* clutter_rect_offset:
* @rect: a #ClutterRect
* @d_x: the horizontal offset value
* @d_y: the vertical offset value
*
* Offsets the origin of @rect by the given values, after normalizing
* the rectangle.
*
* Since: 1.12
*/
void
clutter_rect_offset (ClutterRect *rect,
float d_x,
float d_y)
{
g_return_if_fail (rect != NULL);
clutter_rect_normalize_internal (rect);
rect->origin.x += d_x;
rect->origin.y += d_y;
}
/**
* clutter_rect_inset:
* @rect: a #ClutterRect
* @d_x: an horizontal value; a positive @d_x will create an inset rectangle,
* and a negative value will create a larger rectangle
* @d_y: a vertical value; a positive @d_x will create an inset rectangle,
* and a negative value will create a larger rectangle
*
* Normalizes the @rect and offsets its origin by the @d_x and @d_y values;
* the size is adjusted by (2 * @d_x, 2 * @d_y).
*
* If @d_x and @d_y are positive the size of the rectangle is decreased; if
* the values are negative, the size of the rectangle is increased.
*
* If the resulting rectangle has a negative width or height, the size is
* set to 0.
*
* Since: 1.12
*/
void
clutter_rect_inset (ClutterRect *rect,
float d_x,
float d_y)
{
g_return_if_fail (rect != NULL);
clutter_rect_normalize_internal (rect);
rect->origin.x += d_x;
rect->origin.y += d_y;
if (d_x >= 0.f)
rect->size.width -= (d_x * 2.f);
else
rect->size.width += (d_x * -2.f);
if (d_y >= 0.f)
rect->size.height -= (d_y * 2.f);
else
rect->size.height += (d_y * -2.f);
if (rect->size.width < 0.f)
rect->size.width = 0.f;
if (rect->size.height < 0.f)
rect->size.height = 0.f;
}
/**
* clutter_rect_clamp_to_pixel:
* @rect: a #ClutterRect
*
* Rounds the origin of @rect downwards to the nearest integer, and rounds
* the size of @rect upwards to the nearest integer, so that @rect is
* updated to the smallest rectangle capable of fully containing the
* original, fractional rectangle.
*
* Since: 1.12
*/
void
clutter_rect_clamp_to_pixel (ClutterRect *rect)
{
g_return_if_fail (rect != NULL);
clutter_rect_normalize_internal (rect);
rect->origin.x = floorf (rect->origin.x);
rect->origin.y = floorf (rect->origin.y);
rect->size.width = ceilf (rect->size.width);
rect->size.height = ceilf (rect->size.height);
}
/**
* clutter_rect_get_x:
* @rect: a #ClutterRect
*
* Retrieves the X coordinate of the origin of @rect.
*
* Return value: the X coordinate of the origin of the rectangle
*
* Since: 1.12
*/
float
clutter_rect_get_x (ClutterRect *rect)
{
g_return_val_if_fail (rect != NULL, 0.f);
clutter_rect_normalize_internal (rect);
return rect->origin.x;
}
/**
* clutter_rect_get_y:
* @rect: a #ClutterRect
*
* Retrieves the Y coordinate of the origin of @rect.
*
* Return value: the Y coordinate of the origin of the rectangle
*
* Since: 1.12
*/
float
clutter_rect_get_y (ClutterRect *rect)
{
g_return_val_if_fail (rect != NULL, 0.f);
clutter_rect_normalize_internal (rect);
return rect->origin.y;
}
/**
* clutter_rect_get_width:
* @rect: a #ClutterRect
*
* Retrieves the width of @rect.
*
* Return value: the width of the rectangle
*
* Since: 1.12
*/
float
clutter_rect_get_width (ClutterRect *rect)
{
g_return_val_if_fail (rect != NULL, 0.f);
clutter_rect_normalize_internal (rect);
return rect->size.width;
}
/**
* clutter_rect_get_height:
* @rect: a #ClutterRect
*
* Retrieves the height of @rect.
*
* Return value: the height of the rectangle
*
* Since: 1.12
*/
float
clutter_rect_get_height (ClutterRect *rect)
{
g_return_val_if_fail (rect != NULL, 0.f);
clutter_rect_normalize_internal (rect);
return rect->size.height;
}
static gboolean
clutter_rect_progress (const GValue *a,
const GValue *b,
gdouble progress,
GValue *retval)
{
const ClutterRect *rect_a = g_value_get_boxed (a);
const ClutterRect *rect_b = g_value_get_boxed (b);
ClutterRect res = CLUTTER_RECT_INIT_ZERO;
#define INTERPOLATE(r_a,r_b,member,field,factor) ((r_a)->member.field + (((r_b)->member.field - ((r_a)->member.field)) * (factor)))
res.origin.x = INTERPOLATE (rect_a, rect_b, origin, x, progress);
res.origin.y = INTERPOLATE (rect_a, rect_b, origin, y, progress);
res.size.width = INTERPOLATE (rect_a, rect_b, size, width, progress);
res.size.height = INTERPOLATE (rect_a, rect_b, size, height, progress);
#undef INTERPOLATE
g_value_set_boxed (retval, &res);
return TRUE;
}
/**
* ClutterMatrix:
*
* A type representing a 4x4 matrix.
*
* It is identicaly to #CoglMatrix.
*
* Since: 1.12
*/
static gpointer
clutter_matrix_copy (gpointer data)
{
return cogl_matrix_copy (data);
}
static gboolean
clutter_matrix_progress (const GValue *a,
const GValue *b,
gdouble progress,
GValue *retval)
{
const ClutterMatrix *matrix1 = g_value_get_boxed (a);
const ClutterMatrix *matrix2 = g_value_get_boxed (b);
ClutterVertex scale1 = CLUTTER_VERTEX_INIT (1.f, 1.f, 1.f);
float shear1[3] = { 0.f, 0.f, 0.f };
ClutterVertex rotate1 = CLUTTER_VERTEX_INIT_ZERO;
ClutterVertex translate1 = CLUTTER_VERTEX_INIT_ZERO;
ClutterVertex4 perspective1 = { 0.f, 0.f, 0.f, 0.f };
ClutterVertex scale2 = CLUTTER_VERTEX_INIT (1.f, 1.f, 1.f);
float shear2[3] = { 0.f, 0.f, 0.f };
ClutterVertex rotate2 = CLUTTER_VERTEX_INIT_ZERO;
ClutterVertex translate2 = CLUTTER_VERTEX_INIT_ZERO;
ClutterVertex4 perspective2 = { 0.f, 0.f, 0.f, 0.f };
ClutterVertex scale_res = CLUTTER_VERTEX_INIT (1.f, 1.f, 1.f);
float shear_res = 0.f;
ClutterVertex rotate_res = CLUTTER_VERTEX_INIT_ZERO;
ClutterVertex translate_res = CLUTTER_VERTEX_INIT_ZERO;
ClutterVertex4 perspective_res = { 0.f, 0.f, 0.f, 0.f };
ClutterMatrix res;
clutter_matrix_init_identity (&res);
_clutter_util_matrix_decompose (matrix1,
&scale1, shear1, &rotate1, &translate1,
&perspective1);
_clutter_util_matrix_decompose (matrix2,
&scale2, shear2, &rotate2, &translate2,
&perspective2);
/* perspective */
_clutter_util_vertex4_interpolate (&perspective1, &perspective2, progress, &perspective_res);
res.wx = perspective_res.x;
res.wy = perspective_res.y;
res.wz = perspective_res.z;
res.ww = perspective_res.w;
/* translation */
clutter_vertex_interpolate (&translate1, &translate2, progress, &translate_res);
cogl_matrix_translate (&res, translate_res.x, translate_res.y, translate_res.z);
/* rotation */
clutter_vertex_interpolate (&rotate1, &rotate2, progress, &rotate_res);
cogl_matrix_rotate (&res, rotate_res.x, 1.0f, 0.0f, 0.0f);
cogl_matrix_rotate (&res, rotate_res.y, 0.0f, 1.0f, 0.0f);
cogl_matrix_rotate (&res, rotate_res.z, 0.0f, 0.0f, 1.0f);
/* skew */
shear_res = shear1[2] + (shear2[2] - shear1[2]) * progress; /* YZ */
if (shear_res != 0.f)
_clutter_util_matrix_skew_yz (&res, shear_res);
shear_res = shear1[1] + (shear2[1] - shear1[1]) * progress; /* XZ */
if (shear_res != 0.f)
_clutter_util_matrix_skew_xz (&res, shear_res);
shear_res = shear1[0] + (shear2[0] - shear1[0]) * progress; /* XY */
if (shear_res != 0.f)
_clutter_util_matrix_skew_xy (&res, shear_res);
/* scale */
clutter_vertex_interpolate (&scale1, &scale2, progress, &scale_res);
cogl_matrix_scale (&res, scale_res.x, scale_res.y, scale_res.z);
g_value_set_boxed (retval, &res);
return TRUE;
}
G_DEFINE_BOXED_TYPE_WITH_CODE (ClutterMatrix, clutter_matrix,
clutter_matrix_copy,
clutter_matrix_free,
CLUTTER_REGISTER_INTERVAL_PROGRESS (clutter_matrix_progress))
/**
* clutter_matrix_alloc:
*
* Allocates enough memory to hold a #ClutterMatrix.
*
* Return value: (transfer full): the newly allocated #ClutterMatrix
*
* Since: 1.12
*/
ClutterMatrix *
clutter_matrix_alloc (void)
{
return g_new0 (ClutterMatrix, 1);
}
/**
* clutter_matrix_free:
* @matrix: (allow-none): a #ClutterMatrix
*
* Frees the memory allocated by clutter_matrix_alloc().
*
* Since: 1.12
*/
void
clutter_matrix_free (ClutterMatrix *matrix)
{
cogl_matrix_free (matrix);
}
/**
* clutter_matrix_init_identity:
* @matrix: a #ClutterMatrix
*
* Initializes @matrix with the identity matrix, i.e.:
*
* |[
* .xx = 1.0, .xy = 0.0, .xz = 0.0, .xw = 0.0
* .yx = 0.0, .yy = 1.0, .yz = 0.0, .yw = 0.0
* .zx = 0.0, .zy = 0.0, .zz = 1.0, .zw = 0.0
* .wx = 0.0, .wy = 0.0, .wz = 0.0, .ww = 1.0
* ]|
*
* Return value: (transfer none): the initialized #ClutterMatrix
*
* Since: 1.12
*/
ClutterMatrix *
clutter_matrix_init_identity (ClutterMatrix *matrix)
{
cogl_matrix_init_identity (matrix);
return matrix;
}
/**
* clutter_matrix_init_from_array:
* @matrix: a #ClutterMatrix
* @values: (array fixed-size=16): a C array of 16 floating point values,
* representing a 4x4 matrix, with column-major order
*
* Initializes @matrix with the contents of a C array of floating point
* values.
*
* Return value: (transfer none): the initialzed #ClutterMatrix
*
* Since: 1.12
*/
ClutterMatrix *
clutter_matrix_init_from_array (ClutterMatrix *matrix,
const float values[16])
{
cogl_matrix_init_from_array (matrix, values);
return matrix;
}
/**
* clutter_matrix_init_from_matrix:
* @a: the #ClutterMatrix to initialize
* @b: the #ClutterMatrix to copy
*
* Initializes the #ClutterMatrix @a with the contents of the
* #ClutterMatrix @b.
*
* Return value: (transfer none): the initialized #ClutterMatrix
*
* Since: 1.12
*/
ClutterMatrix *
clutter_matrix_init_from_matrix (ClutterMatrix *a,
const ClutterMatrix *b)
{
return memcpy (a, b, sizeof (ClutterMatrix));
}
|