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
|
/*
* $Id: gwygraphcurvemodel.c 25077 2022-10-11 13:48:33Z yeti-dn $
* Copyright (C) 2005-2022 David Necas (Yeti), Petr Klapetek.
* E-mail: yeti@gwyddion.net, klapetek@gwyddion.net.
*
* This program 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.
*
* This program 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 this program; if not, write to the
* Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
#include "config.h"
#include <string.h>
#include <stdlib.h>
#include <gtk/gtk.h>
#include <libgwyddion/gwymath.h>
#include <libdraw/gwyrgba.h>
#include <libgwyddion/gwymacros.h>
#include <libgwyddion/gwydebugobjects.h>
#include <libprocess/dataline.h>
#include <libgwydgets/gwygraphcurvemodel.h>
#include <libgwydgets/gwydgettypes.h>
#define GWY_GRAPH_CURVE_MODEL_TYPE_NAME "GwyGraphCurveModel"
/* Cache operations */
#define CVAL(cmodel, b) ((cmodel)->cache[GWY_GRAPH_CURVE_MODEL_CACHE_##b])
#define CBIT(b) (1 << GWY_GRAPH_CURVE_MODEL_CACHE_##b)
#define CTEST(cmodel, b) ((cmodel)->cached & CBIT(b))
/* Cache2 operations, we use the same bitfield but a different array */
#define CVAL2(cmodel, b) ((cmodel)->cache2[GWY_GRAPH_CURVE_MODEL_CACHE2_##b - GWY_GRAPH_CURVE_MODEL_CACHE2_OFFSET])
#define CBIT2(b) (1 << GWY_GRAPH_CURVE_MODEL_CACHE2_##b)
#define CTEST2(cmodel, b) ((cmodel)->cached & CBIT2(b))
typedef enum {
GWY_GRAPH_CURVE_MODEL_CACHE_XMIN = 0,
GWY_GRAPH_CURVE_MODEL_CACHE_XMAX,
GWY_GRAPH_CURVE_MODEL_CACHE_YMIN,
GWY_GRAPH_CURVE_MODEL_CACHE_YMAX,
GWY_GRAPH_CURVE_MODEL_CACHE2_OFFSET,
/* X values (x > 0) */
GWY_GRAPH_CURVE_MODEL_CACHE2_XMIN_XPOS = GWY_GRAPH_CURVE_MODEL_CACHE2_OFFSET,
/* Y values (x > 0) */
GWY_GRAPH_CURVE_MODEL_CACHE2_YMIN_XPOS,
/* Absolute y values (y != 0) */
GWY_GRAPH_CURVE_MODEL_CACHE2_YAMIN,
/* Absolute y values (y != 0, x > 0) */
GWY_GRAPH_CURVE_MODEL_CACHE2_YAMIN_XPOS,
GWY_GRAPH_CURVE_MODEL_CACHE2_LAST,
} GwyGraphCurveModelCached;
static void gwy_graph_curve_model_finalize (GObject *object);
static void gwy_graph_curve_model_serializable_init(GwySerializableIface *iface);
static GByteArray* gwy_graph_curve_model_serialize (GObject *object,
GByteArray*buffer);
static gsize gwy_graph_curve_model_get_size (GObject *object);
static GObject* gwy_graph_curve_model_deserialize (const guchar *buffer,
gsize size,
gsize *position);
static GObject* gwy_graph_curve_model_duplicate_real (GObject *object);
static void gwy_graph_curve_model_clone_real (GObject *source,
GObject *copy);
static void gwy_graph_curve_model_set_property (GObject *object,
guint prop_id,
const GValue *value,
GParamSpec *pspec);
static void gwy_graph_curve_model_get_property (GObject*object,
guint prop_id,
GValue *value,
GParamSpec *pspec);
static void gwy_graph_curve_model_data_changed (GwyGraphCurveModel *gcmodel);
static gint find_out_ordering_type (GwyGraphCurveModel *gcmodel);
static void free_calibration (GwyGraphCurveModel *gcmodel);
enum {
DATA_CHANGED,
LAST_SIGNAL
};
enum {
PROP_0,
PROP_DESCRIPTION,
PROP_MODE,
PROP_POINT_TYPE,
PROP_POINT_SIZE,
PROP_LINE_STYLE,
PROP_LINE_WIDTH,
PROP_COLOR,
PROP_LAST
};
static guint graph_curve_model_signals[LAST_SIGNAL] = { 0 };
G_DEFINE_TYPE_EXTENDED
(GwyGraphCurveModel, gwy_graph_curve_model, G_TYPE_OBJECT, 0,
GWY_IMPLEMENT_SERIALIZABLE(gwy_graph_curve_model_serializable_init))
static void
gwy_graph_curve_model_serializable_init(GwySerializableIface *iface)
{
iface->serialize = gwy_graph_curve_model_serialize;
iface->deserialize = gwy_graph_curve_model_deserialize;
iface->get_size = gwy_graph_curve_model_get_size;
iface->duplicate = gwy_graph_curve_model_duplicate_real;
iface->clone = gwy_graph_curve_model_clone_real;
}
static void
gwy_graph_curve_model_class_init(GwyGraphCurveModelClass *klass)
{
GObjectClass *gobject_class = G_OBJECT_CLASS(klass);
gwy_debug("");
gobject_class->finalize = gwy_graph_curve_model_finalize;
gobject_class->set_property = gwy_graph_curve_model_set_property;
gobject_class->get_property = gwy_graph_curve_model_get_property;
/**
* GwyGraphCurveModel::data-changed:
* @gwygraphcurvemodel: The #GwyGraphCurveModel which received the signal.
*
* The ::data-changed signal is emitted whenever curve data is set with a function like
* gwy_graph_curve_model_set_data().
**/
graph_curve_model_signals[DATA_CHANGED] = g_signal_new("data-changed",
G_OBJECT_CLASS_TYPE(gobject_class),
G_SIGNAL_RUN_FIRST,
G_STRUCT_OFFSET(GwyGraphCurveModelClass, data_changed),
NULL, NULL,
g_cclosure_marshal_VOID__VOID,
G_TYPE_NONE, 0);
g_object_class_install_property(gobject_class, PROP_DESCRIPTION,
g_param_spec_string("description",
"Curve description",
"Curve description. It appears on graph key.",
"curve",
G_PARAM_READABLE | G_PARAM_WRITABLE));
g_object_class_install_property(gobject_class, PROP_MODE,
g_param_spec_enum("mode",
"Mode",
"Curve plotting mode (line, points, ...)",
GWY_TYPE_GRAPH_CURVE_TYPE,
GWY_GRAPH_CURVE_LINE,
G_PARAM_READABLE | G_PARAM_WRITABLE));
g_object_class_install_property(gobject_class, PROP_POINT_TYPE,
g_param_spec_enum("point-type",
"Point type",
"Curve point symbol type. Curve mode has to"
"include points for the symbols to be visible.",
GWY_TYPE_GRAPH_POINT_TYPE,
GWY_GRAPH_POINT_SQUARE,
G_PARAM_READABLE | G_PARAM_WRITABLE));
g_object_class_install_property(gobject_class, PROP_POINT_SIZE,
g_param_spec_int("point-size",
"Point size",
"Curve point symbol size",
0, 100,
5,
G_PARAM_READABLE | G_PARAM_WRITABLE));
g_object_class_install_property(gobject_class, PROP_LINE_STYLE,
g_param_spec_enum("line-style",
"Line style",
"Curve line style. Curve mode has to include "
"lines for the line to be visible.",
GDK_TYPE_LINE_STYLE,
GDK_LINE_SOLID,
G_PARAM_READABLE | G_PARAM_WRITABLE));
g_object_class_install_property(gobject_class, PROP_LINE_WIDTH,
g_param_spec_int("line-width",
"Line width",
"Curve line width.",
0, 100,
1,
G_PARAM_READABLE | G_PARAM_WRITABLE));
g_object_class_install_property(gobject_class, PROP_COLOR,
g_param_spec_boxed("color",
"Color",
"Curve color",
GWY_TYPE_RGBA,
G_PARAM_READABLE | G_PARAM_WRITABLE));
}
static void
gwy_graph_curve_model_set_property(GObject *object,
guint prop_id,
const GValue *value,
GParamSpec *pspec)
{
GwyGraphCurveModel *gcmodel = GWY_GRAPH_CURVE_MODEL(object);
GwyRGBA *color;
switch (prop_id) {
case PROP_DESCRIPTION:
g_string_assign(gcmodel->description, g_value_get_string(value));
break;
case PROP_MODE:
gcmodel->mode = g_value_get_enum(value);
gwy_graph_curve_model_data_changed(gcmodel);
break;
case PROP_POINT_TYPE:
gcmodel->point_type = g_value_get_enum(value);
break;
case PROP_LINE_STYLE:
gcmodel->line_style = g_value_get_enum(value);
break;
case PROP_LINE_WIDTH:
gcmodel->line_width = g_value_get_int(value);
break;
case PROP_POINT_SIZE:
gcmodel->point_size = g_value_get_int(value);
break;
case PROP_COLOR:
color = g_value_get_boxed(value);
gcmodel->color = *color;
break;
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID(object, prop_id, pspec);
break;
}
}
static void
gwy_graph_curve_model_get_property(GObject*object,
guint prop_id,
GValue *value,
GParamSpec *pspec)
{
GwyGraphCurveModel *gcmodel = GWY_GRAPH_CURVE_MODEL(object);
switch (prop_id) {
case PROP_DESCRIPTION:
g_value_set_string(value, gcmodel->description->str);
break;
case PROP_MODE:
g_value_set_enum(value, gcmodel->mode);
break;
case PROP_POINT_TYPE:
g_value_set_enum(value, gcmodel->point_type);
break;
case PROP_LINE_STYLE:
g_value_set_enum(value, gcmodel->line_style);
break;
case PROP_LINE_WIDTH:
g_value_set_int(value, gcmodel->line_width);
break;
case PROP_POINT_SIZE:
g_value_set_int(value, gcmodel->point_size);
break;
case PROP_COLOR:
g_value_set_boxed(value, &gcmodel->color);
break;
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
break;
}
}
static void
gwy_graph_curve_model_init(GwyGraphCurveModel *gcmodel)
{
gwy_debug("");
gcmodel->description = g_string_new(NULL);
gcmodel->color.r = 0;
gcmodel->color.g = 0;
gcmodel->color.b = 0;
gcmodel->color.a = 1;
gcmodel->mode = GWY_GRAPH_CURVE_LINE_POINTS;
gcmodel->point_type = GWY_GRAPH_POINT_SQUARE;
gcmodel->point_size = 8;
gcmodel->line_style = GDK_LINE_SOLID;
gcmodel->line_width = 1;
gcmodel->calibration = NULL;
}
/**
* gwy_graph_curve_model_new:
*
* Creates a new graph curve model.
*
* Returns: New empty graph curve model as a #GObject.
**/
GwyGraphCurveModel*
gwy_graph_curve_model_new(void)
{
GwyGraphCurveModel *gcmodel;
gwy_debug("");
gcmodel = (GwyGraphCurveModel*)g_object_new(GWY_TYPE_GRAPH_CURVE_MODEL, NULL);
return (gcmodel);
}
/**
* gwy_graph_curve_model_new_alike:
* @gcmodel: A graph curve model.
*
* Creates new graph curve model object that has the same settings as @gcmodel.
*
* Curve data are not duplicated.
*
* Returns: New graph curve model.
**/
GwyGraphCurveModel*
gwy_graph_curve_model_new_alike(GwyGraphCurveModel *gcmodel)
{
GwyGraphCurveModel *duplicate;
gwy_debug("");
duplicate = gwy_graph_curve_model_new();
duplicate->description = g_string_new(gcmodel->description->str);
duplicate->color = gcmodel->color;
duplicate->mode = gcmodel->mode;
duplicate->point_type = gcmodel->point_type;
duplicate->point_size = gcmodel->point_size;
duplicate->line_style = gcmodel->line_style;
duplicate->line_width = gcmodel->line_width;
return duplicate;
}
static void
gwy_graph_curve_model_finalize(GObject *object)
{
GwyGraphCurveModel *gcmodel;
gwy_debug("");
gcmodel = GWY_GRAPH_CURVE_MODEL(object);
free_calibration(gcmodel);
g_string_free(gcmodel->description, TRUE);
g_free(gcmodel->xdata);
g_free(gcmodel->ydata);
g_free(gcmodel->cache2);
G_OBJECT_CLASS(gwy_graph_curve_model_parent_class)->finalize(object);
}
static GByteArray*
gwy_graph_curve_model_serialize(GObject *object,
GByteArray *buffer)
{
GwyGraphCurveModel *gcmodel;
gwy_debug("");
g_return_val_if_fail(GWY_IS_GRAPH_CURVE_MODEL(object), NULL);
gcmodel = GWY_GRAPH_CURVE_MODEL(object);
#ifdef DEBUG
/* Ensure find_out_ordering_type() is not called with debugging disabled even with a poor compiler. */
gwy_debug("%p: %d", gcmodel, find_out_ordering_type(gcmodel));
#endif
{
GwySerializeSpec spec[] = {
{ 'D', "xdata", &gcmodel->xdata, &gcmodel->n },
{ 'D', "ydata", &gcmodel->ydata, &gcmodel->n },
{ 's', "description", &gcmodel->description->str, NULL },
{ 'd', "color.red", &gcmodel->color.r, NULL },
{ 'd', "color.green", &gcmodel->color.g, NULL },
{ 'd', "color.blue", &gcmodel->color.b, NULL },
/* XXX: Legacy */
{ 'i', "type", &gcmodel->mode, NULL },
{ 'i', "point_type", &gcmodel->point_type, NULL },
{ 'i', "point_size", &gcmodel->point_size, NULL },
{ 'i', "line_style", &gcmodel->line_style, NULL },
/* XXX: Legacy */
{ 'i', "line_size", &gcmodel->line_width, NULL },
{ 'D', "xerr", &gcmodel->calibration->xerr, &gcmodel->n },
{ 'D', "yerr", &gcmodel->calibration->yerr, &gcmodel->n },
{ 'D', "zerr", &gcmodel->calibration->zerr, &gcmodel->n },
{ 'D', "xunc", &gcmodel->calibration->xunc, &gcmodel->n },
{ 'D', "yunc", &gcmodel->calibration->yunc, &gcmodel->n },
{ 'D', "zunc", &gcmodel->calibration->zunc, &gcmodel->n },
};
return gwy_serialize_pack_object_struct(buffer, GWY_GRAPH_CURVE_MODEL_TYPE_NAME,
G_N_ELEMENTS(spec) - (gcmodel->calibration ? 0 : 6), spec);
}
}
static gsize
gwy_graph_curve_model_get_size(GObject *object)
{
GwyGraphCurveModel *gcmodel;
gwy_debug("");
g_return_val_if_fail(GWY_IS_GRAPH_CURVE_MODEL(object), 0);
gcmodel = GWY_GRAPH_CURVE_MODEL(object);
{
GwySerializeSpec spec[] = {
{ 'D', "xdata", &gcmodel->xdata, &gcmodel->n },
{ 'D', "ydata", &gcmodel->ydata, &gcmodel->n },
{ 's', "description", &gcmodel->description->str, NULL },
{ 'd', "color.red", &gcmodel->color.r, NULL },
{ 'd', "color.green", &gcmodel->color.g, NULL },
{ 'd', "color.blue", &gcmodel->color.b, NULL },
/* XXX: Legacy */
{ 'i', "type", &gcmodel->mode, NULL },
{ 'i', "point_type", &gcmodel->point_type, NULL },
{ 'i', "point_size", &gcmodel->point_size, NULL },
{ 'i', "line_style", &gcmodel->line_style, NULL },
/* XXX: Legacy */
{ 'i', "line_size", &gcmodel->line_width, NULL },
{ 'D', "xerr", &gcmodel->calibration->xerr, &gcmodel->n },
{ 'D', "yerr", &gcmodel->calibration->yerr, &gcmodel->n },
{ 'D', "zerr", &gcmodel->calibration->zerr, &gcmodel->n },
{ 'D', "xunc", &gcmodel->calibration->xunc, &gcmodel->n },
{ 'D', "yunc", &gcmodel->calibration->yunc, &gcmodel->n },
{ 'D', "zunc", &gcmodel->calibration->zunc, &gcmodel->n },
};
return gwy_serialize_get_struct_size(GWY_GRAPH_CURVE_MODEL_TYPE_NAME,
G_N_ELEMENTS(spec) - (gcmodel->calibration ? 0 : 6), spec);
}
}
static GObject*
gwy_graph_curve_model_deserialize(const guchar *buffer,
gsize size,
gsize *position)
{
GwyGraphCurveModel *gcmodel;
gdouble *xerr = NULL, *yerr = NULL, *zerr = NULL, *xunc = NULL, *yunc = NULL, *zunc = NULL;
gint nxerr = 0, nyerr = 0, nzerr = 0, nxunc = 0, nyunc = 0, nzunc = 0;
gwy_debug("");
g_return_val_if_fail(buffer, NULL);
gcmodel = gwy_graph_curve_model_new();
{
gint nxdata=0, nydata=0;
gchar *description = NULL;
GwySerializeSpec spec[] = {
{ 'D', "xdata", &gcmodel->xdata, &nxdata },
{ 'D', "ydata", &gcmodel->ydata, &nydata },
{ 's', "description", &description, NULL },
{ 'd', "color.red", &gcmodel->color.r, NULL },
{ 'd', "color.green", &gcmodel->color.g, NULL },
{ 'd', "color.blue", &gcmodel->color.b, NULL },
/* XXX: Legacy */
{ 'i', "type", &gcmodel->mode, NULL },
/* Accept mode too */
{ 'i', "mode", &gcmodel->mode, NULL },
{ 'i', "point_type", &gcmodel->point_type, NULL },
{ 'i', "point_size", &gcmodel->point_size, NULL },
{ 'i', "line_style", &gcmodel->line_style, NULL },
/* XXX: Legacy */
{ 'i', "line_size", &gcmodel->line_width, NULL },
/* Accept line_width too */
{ 'i', "line_width", &gcmodel->line_width, NULL },
{ 'D', "xerr", &xerr, &nxerr },
{ 'D', "yerr", &yerr, &nyerr },
{ 'D', "zerr", &zerr, &nzerr },
{ 'D', "xunc", &xunc, &nxunc },
{ 'D', "yunc", &yunc, &nyunc },
{ 'D', "zunc", &zunc, &nzunc },
};
if (!gwy_serialize_unpack_object_struct(buffer, size, position, GWY_GRAPH_CURVE_MODEL_TYPE_NAME,
G_N_ELEMENTS(spec), spec)) {
g_free(description);
g_object_unref(gcmodel);
return NULL;
}
if (nxdata != nydata) {
g_critical("Serialized xdata and ydata array sizes differ");
g_free(description);
g_object_unref(gcmodel);
return NULL;
}
if (description) {
g_string_assign(gcmodel->description, description);
g_free(description);
}
gcmodel->n = nxdata;
if (nxerr == nxdata && nyerr == nxdata && nzerr == nxdata
&& nxunc == nxdata && nyunc == nxdata && nzunc == nxdata) {
gcmodel->calibration = g_new(GwyCurveCalibrationData, 1);
gcmodel->calibration->n = nxdata;
gcmodel->calibration->xerr = xerr;
gcmodel->calibration->yerr = yerr;
gcmodel->calibration->zerr = zerr;
gcmodel->calibration->xunc = xunc;
gcmodel->calibration->yunc = yunc;
gcmodel->calibration->zunc = zunc;
}
else {
g_free(xerr);
g_free(yerr);
g_free(zerr);
g_free(xunc);
g_free(yunc);
g_free(zunc);
gcmodel->calibration = NULL;
}
}
#ifdef DEBUG
/* Ensure find_out_ordering_type() is not called with debugging disabled even with a poor compiler. */
gwy_debug("%p: %d", gcmodel, find_out_ordering_type(gcmodel));
#endif
return (GObject*)gcmodel;
}
static GObject*
gwy_graph_curve_model_duplicate_real(GObject *object)
{
GwyGraphCurveModel *gcmodel, *duplicate;
gwy_debug("");
g_return_val_if_fail(GWY_IS_GRAPH_CURVE_MODEL(object), NULL);
gcmodel = GWY_GRAPH_CURVE_MODEL(object);
duplicate = gwy_graph_curve_model_new_alike(gcmodel);
if ((duplicate->n = gcmodel->n)) {
duplicate->xdata = g_memdup(gcmodel->xdata, gcmodel->n*sizeof(gdouble));
duplicate->ydata = g_memdup(gcmodel->ydata, gcmodel->n*sizeof(gdouble));
gwy_graph_curve_model_set_calibration_data(duplicate, gcmodel->calibration);
}
#ifdef DEBUG
/* Ensure find_out_ordering_type() is not called with debugging disabled even with a poor compiler. */
gwy_debug("%p: %d", duplicate, find_out_ordering_type(duplicate));
#endif
return (GObject*)duplicate;
}
static void
gwy_graph_curve_model_clone_real(GObject *source,
GObject *copy)
{
GwyGraphCurveModel *gcmodel, *clone;
g_return_if_fail(GWY_IS_GRAPH_CURVE_MODEL(source));
g_return_if_fail(GWY_IS_GRAPH_CURVE_MODEL(copy));
gcmodel = GWY_GRAPH_CURVE_MODEL(source);
clone = GWY_GRAPH_CURVE_MODEL(copy);
g_object_freeze_notify(copy);
if (!gwy_strequal(clone->description->str, gcmodel->description->str)) {
g_string_assign(clone->description, gcmodel->description->str);
g_object_notify(copy, "description");
}
if (clone->mode != gcmodel->mode) {
clone->mode = gcmodel->mode;
g_object_notify(copy, "mode");
}
if (clone->point_type != gcmodel->point_type) {
clone->point_type = gcmodel->point_type;
g_object_notify(copy, "point-type");
}
if (clone->line_style != gcmodel->line_style) {
clone->line_style = gcmodel->line_style;
g_object_notify(copy, "line-style");
}
if (clone->point_size != gcmodel->point_size) {
clone->point_size = gcmodel->point_size;
g_object_notify(copy, "point-size");
}
if (clone->line_width != gcmodel->line_width) {
clone->line_width = gcmodel->line_width;
g_object_notify(copy, "line-width");
}
if (memcmp(&clone->color, &gcmodel->color, sizeof(GwyRGBA)) != 0) {
clone->color = gcmodel->color;
g_object_notify(copy, "color");
}
if (clone->n != gcmodel->n) {
clone->xdata = g_renew(gdouble, clone->xdata, gcmodel->n);
clone->ydata = g_renew(gdouble, clone->ydata, gcmodel->n);
clone->n = gcmodel->n;
}
gwy_assign(clone->xdata, gcmodel->xdata, gcmodel->n);
gwy_assign(clone->ydata, gcmodel->ydata, gcmodel->n);
g_object_thaw_notify(copy);
gwy_graph_curve_model_set_calibration_data(clone, gcmodel->calibration);
#ifdef DEBUG
/* Ensure find_out_ordering_type() is not called with debugging disabled even with a poor compiler. */
gwy_debug("%p: %d", clone, find_out_ordering_type(clone));
#endif
gwy_graph_curve_model_data_changed(clone);
}
/**
* gwy_graph_curve_model_set_data:
* @gcmodel: A graph curve model.
* @xdata: X data points (array of size @n).
* @ydata: Y data points (array of size @n).
* @n: Number of points, i.e. items in @xdata and @ydata.
*
* Sets curve model data from separated X and Y arrays.
*
* If there were calibration data in the former @gcmodel, they are removed.
*
* <warning>The points should be ordered in ascending abscissa order, meaning @xdata values ordered from smallest to
* largest. It is not enforced and you can create graphs of data the do not satisfy this condition. However, various
* graph functionality may be unavailable or degraded then. You also can use gwy_graph_curve_model_enforce_order()
* afterwards to ensure the recommended data point order.</warning>
**/
void
gwy_graph_curve_model_set_data(GwyGraphCurveModel *gcmodel,
const gdouble *xdata,
const gdouble *ydata,
gint n)
{
g_return_if_fail(GWY_IS_GRAPH_CURVE_MODEL(gcmodel));
if (gcmodel->n == n) {
gwy_assign(gcmodel->xdata, xdata, n);
gwy_assign(gcmodel->ydata, ydata, n);
}
else {
gdouble *old;
old = gcmodel->xdata;
gcmodel->xdata = g_memdup(xdata, n*sizeof(gdouble));
g_free(old);
old = gcmodel->ydata;
gcmodel->ydata = g_memdup(ydata, n*sizeof(gdouble));
g_free(old);
gcmodel->n = n;
}
#ifdef DEBUG
/* Ensure find_out_ordering_type() is not called with debugging disabled even with a poor compiler. */
gwy_debug("%p: %d", gcmodel, find_out_ordering_type(gcmodel));
#endif
free_calibration(gcmodel);
gwy_graph_curve_model_data_changed(gcmodel);
}
/**
* gwy_graph_curve_model_set_data_interleaved:
* @gcmodel: A graph curve model.
* @xydata: X and Y data points (array of size 2*@n).
* @n: Number of points, i.e. half the number of items in @xydata.
*
* Sets curve model data from an interleaved array.
*
* The array should contain interleaved abscissa and ordinate values: x0, y0, x1, y1, x2, y2, etc. You can also
* typecast an array of #GwyXY structs and pass it as @xydata.
*
* If there were calibration data in the former @gcmodel, they are removed.
*
* <warning>The points should be ordered in ascending abscissa order, meaning @xdata values ordered from smallest to
* largest. It is not enforced and you can create graphs of data the do not satisfy this condition. However, various
* graph functionality may be unavailable or degraded then. You also can use gwy_graph_curve_model_enforce_order()
* afterwards to ensure the recommended data point order.</warning>
*
* Since: 2.45
**/
void
gwy_graph_curve_model_set_data_interleaved(GwyGraphCurveModel *gcmodel,
const gdouble *xydata,
gint n)
{
gint i;
g_return_if_fail(GWY_IS_GRAPH_CURVE_MODEL(gcmodel));
if (gcmodel->n != n) {
g_free(gcmodel->xdata);
g_free(gcmodel->ydata);
gcmodel->xdata = g_new(gdouble, n);
gcmodel->ydata = g_new(gdouble, n);
}
gcmodel->n = n;
for (i = 0; i < n; i++) {
gcmodel->xdata[i] = *(xydata++);
gcmodel->ydata[i] = *(xydata++);
}
#ifdef DEBUG
/* Ensure find_out_ordering_type() is not called with debugging disabled even with a poor compiler. */
gwy_debug("%p: %d", gcmodel, find_out_ordering_type(gcmodel));
#endif
free_calibration(gcmodel);
gwy_graph_curve_model_data_changed(gcmodel);
}
/* Return positive, negative or zero for ascending, descending or no order, respectively.*/
static gint
find_out_ordering_type(GwyGraphCurveModel *gcmodel)
{
gdouble *xdata;
gboolean is_sorted = TRUE, is_revsorted = TRUE;
gint n, i;
/* Check if data are either sorted or sorted in the reverse order (which is by far the most common unwanted order
* because we get it for things like retract curves). */
n = gcmodel->n;
xdata = gcmodel->xdata;
for (i = 1; i < n; i++) {
if (is_sorted && xdata[i-1] > xdata[i]) {
is_sorted = FALSE;
if (!is_revsorted)
break;
}
if (is_revsorted && xdata[i-1] < xdata[i]) {
is_revsorted = FALSE;
if (!is_sorted)
break;
}
}
if (is_sorted)
return 1;
if (is_revsorted)
return -1;
return 0;
}
/**
* gwy_graph_curve_model_enforce_order:
* @gcmodel: A graph curve model.
*
* Ensures curve model data points are ordered by abscissa in ascending order.
*
* The function reorders the data points currently present in the model. It does not prevent functions such as
* gwy_graph_curve_model_set_data() from disrupting the order again. See its documentation for further remarks.
*
* The "data-changed" signal is emitted if the data order actually changes.
*
* Since: 2.45
**/
void
gwy_graph_curve_model_enforce_order(GwyGraphCurveModel *gcmodel)
{
gdouble *bothdata, *xdata, *ydata;
gint n, i, order;
g_return_if_fail(GWY_IS_GRAPH_CURVE_MODEL(gcmodel));
order = find_out_ordering_type(gcmodel);
gwy_debug("%p: %d", gcmodel, order);
if (order > 0)
return;
/* Revers order. */
n = gcmodel->n;
xdata = gcmodel->xdata;
ydata = gcmodel->ydata;
if (order < 0) {
for (i = 0; i < n/2; i++) {
GWY_SWAP(gdouble, xdata[i], xdata[n-1 - i]);
GWY_SWAP(gdouble, ydata[i], ydata[n-1 - i]);
}
free_calibration(gcmodel);
gwy_graph_curve_model_data_changed(gcmodel);
return;
}
/* The general case. */
bothdata = g_new(gdouble, 2*n);
for (i = 0; i < n; i++) {
bothdata[2*i + 0] = xdata[i];
bothdata[2*i + 1] = ydata[i];
}
qsort(bothdata, n, 2*sizeof(gdouble), gwy_compare_double);
for (i = 0; i < n; i++) {
xdata[i] = bothdata[2*i + 0];
ydata[i] = bothdata[2*i + 1];
}
g_free(bothdata);
free_calibration(gcmodel);
gwy_graph_curve_model_data_changed(gcmodel);
}
/**
* gwy_graph_curve_model_is_ordered:
* @gcmodel: A graph curve model.
*
* Checks if a curve model data points are ordered by abscissa in ascending order.
*
* If the curve model has less than two points it is considered ordered by abscissa. Two points with the same
* abscissa are considered correctly ordered in both orders.
*
* See gwy_graph_curve_model_enforce_order() for fixing the point order.
*
* Returns: %TRUE if the graph curve model points are sorted by abscissa, %FALSE when they are not.
*
* Since: 2.46
**/
gboolean
gwy_graph_curve_model_is_ordered(GwyGraphCurveModel *gcmodel)
{
const gdouble *xdata;
gint n, i;
g_return_val_if_fail(GWY_IS_GRAPH_CURVE_MODEL(gcmodel), FALSE);
n = gcmodel->n;
xdata = gcmodel->xdata;
for (i = 1; i < n; i++) {
if (xdata[i-1] > xdata[i])
return FALSE;
}
return TRUE;
}
/**
* gwy_graph_curve_model_get_xdata:
* @gcmodel: A graph curve model.
*
* Gets y data points of a graph curve model.
*
* The returned data are owned by the and cannot be modified nor freed. The returned pointer is valid only so long as
* the curve model exists and its data do not change.
*
* Returns: X data points, owned by the curve model.
**/
const gdouble*
gwy_graph_curve_model_get_xdata(GwyGraphCurveModel *gcmodel)
{
g_return_val_if_fail(GWY_IS_GRAPH_CURVE_MODEL(gcmodel), NULL);
return gcmodel->xdata;
}
/**
* gwy_graph_curve_model_get_ydata:
* @gcmodel: A graph curve model.
*
* Gets y data points of a graph curve model.
*
* The returned data are owned by the and cannot be modified nor freed. The returned pointer is valid only so long as
* the curve model exists and its data do not change.
*
* Returns: Y data points, owned by the curve model.
**/
const gdouble*
gwy_graph_curve_model_get_ydata(GwyGraphCurveModel *gcmodel)
{
g_return_val_if_fail(GWY_IS_GRAPH_CURVE_MODEL(gcmodel), NULL);
return gcmodel->ydata;
}
/**
* gwy_graph_curve_model_get_ndata:
* @gcmodel: A graph curve model.
*
* Gets the number of points in a graph curve model.
*
* Returns: number of data points within the curve data
**/
gint
gwy_graph_curve_model_get_ndata(GwyGraphCurveModel *gcmodel)
{
g_return_val_if_fail(GWY_IS_GRAPH_CURVE_MODEL(gcmodel), 0);
return gcmodel->n;
}
/**
* gwy_graph_curve_model_set_data_from_dataline:
* @gcmodel: A graph curve model.
* @dline: A data line.
* @from_index: Data line index where to start.
* @to_index: Data line index where to stop.
*
* Sets graph curve model data from a data line.
*
* The range of import can be modified using parameters @from_index and @to_index that are interpreted directly as
* data indices within the #GwyDataLine. In the case that @from_index == @to_index, the full #GwyDataLine is used.
*
* If there were calibration data in the former @gcmodel, they are removed.
**/
void
gwy_graph_curve_model_set_data_from_dataline(GwyGraphCurveModel *gcmodel,
GwyDataLine *dline,
gint from_index,
gint to_index)
{
gdouble *xdata;
gdouble *ydata;
const gdouble *ldata;
gint res, i;
gdouble realmin, realmax, offset;
g_return_if_fail(GWY_IS_GRAPH_CURVE_MODEL(gcmodel));
g_return_if_fail(GWY_IS_DATA_LINE(dline));
if (from_index == to_index || from_index > to_index) {
res = gwy_data_line_get_res(dline);
realmin = 0;
realmax = gwy_data_line_get_real(dline);
from_index = 0;
}
else {
res = to_index - from_index;
realmin = gwy_data_line_itor(dline, from_index);
realmax = gwy_data_line_itor(dline, to_index);
}
xdata = g_new(gdouble, res);
ydata = g_new(gdouble, res);
offset = gwy_data_line_get_offset(dline);
ldata = gwy_data_line_get_data_const(dline);
if (realmin > realmax) {
/* XXX: Data lines with negative step should not actually exist. But file modules are still prone to
* producing them for spectroscopy... */
for (i = 0; i < res; i++) {
xdata[res-1 - i] = realmin + i*(realmax - realmin)/res + offset;
ydata[res-1 - i] = ldata[i + from_index];
}
}
else {
for (i = 0; i < res; i++) {
xdata[i] = realmin + i*(realmax - realmin)/res + offset;
ydata[i] = ldata[i + from_index];
}
}
gwy_graph_curve_model_set_data(gcmodel, xdata, ydata, res);
g_free(xdata);
g_free(ydata);
}
/**
* gwy_graph_curve_model_get_x_range:
* @gcmodel: A graph curve model.
* @x_min: Location to store the minimum abscissa value, or %NULL.
* @x_max: Location to store the maximum abscissa value, or %NULL.
*
* Gets the abscissa range of a graph curve.
*
* The values are cached in the curve model therefore repeated calls to this function (with unchanged data) are cheap.
*
* If there are no data points in the curve, @x_min and @x_max are untouched and the function returns %FALSE.
*
* See also gwy_graph_curve_model_get_ranges() for a more high-level function.
*
* Returns: %TRUE if there are any data points in the curve and @x_min, @x_max were set.
**/
gboolean
gwy_graph_curve_model_get_x_range(GwyGraphCurveModel *gcmodel,
gdouble *x_min,
gdouble *x_max)
{
gdouble xmin, xmax;
gboolean must_calculate = FALSE;
gint i;
g_return_val_if_fail(GWY_IS_GRAPH_CURVE_MODEL(gcmodel), FALSE);
if (!gcmodel->n)
return FALSE;
if (x_min) {
if (CTEST(gcmodel, XMIN))
*x_min = CVAL(gcmodel, XMIN);
else
must_calculate = TRUE;
}
if (x_max) {
if (CTEST(gcmodel, XMAX))
*x_max = CVAL(gcmodel, XMAX);
else
must_calculate = TRUE;
}
if (!must_calculate)
return TRUE;
xmin = xmax = gcmodel->xdata[0];
for (i = 1; i < gcmodel->n; i++) {
if (G_UNLIKELY(gcmodel->xdata[i] < xmin))
xmin = gcmodel->xdata[i];
if (G_LIKELY(gcmodel->xdata[i] > xmax))
xmax = gcmodel->xdata[i];
}
CVAL(gcmodel, XMIN) = xmin;
CVAL(gcmodel, XMAX) = xmax;
gcmodel->cached |= CBIT(XMIN) | CBIT(XMAX);
if (x_min)
*x_min = xmin;
if (x_max)
*x_max = xmax;
return TRUE;
}
/**
* gwy_graph_curve_model_get_y_range:
* @gcmodel: A graph curve model.
* @y_min: Location to store the minimum ordinate value, or %NULL.
* @y_max: Location to store the maximum ordinate value, or %NULL.
*
* Gets the ordinate range of a graph curve.
*
* The values are cached in the curve model therefore repeated calls to this function (with unchanged data) are cheap.
*
* If there are no data points in the curve, @x_min and @x_max are untouched and the function returns %FALSE.
*
* See also gwy_graph_curve_model_get_ranges() for a more high-level function.
*
* Returns: %TRUE if there are any data points in the curve and @x_min, @x_max were set.
**/
gboolean
gwy_graph_curve_model_get_y_range(GwyGraphCurveModel *gcmodel,
gdouble *y_min,
gdouble *y_max)
{
gdouble ymin, ymax;
gboolean must_calculate = FALSE;
gint i;
g_return_val_if_fail(GWY_IS_GRAPH_CURVE_MODEL(gcmodel), FALSE);
if (!gcmodel->n)
return FALSE;
if (y_min) {
if (CTEST(gcmodel, YMIN))
*y_min = CVAL(gcmodel, YMIN);
else
must_calculate = TRUE;
}
if (y_max) {
if (CTEST(gcmodel, YMAX))
*y_max = CVAL(gcmodel, YMAX);
else
must_calculate = TRUE;
}
if (!must_calculate)
return TRUE;
ymin = ymax = gcmodel->ydata[0];
for (i = 1; i < gcmodel->n; i++) {
if (gcmodel->ydata[i] < ymin)
ymin = gcmodel->ydata[i];
if (gcmodel->ydata[i] > ymax)
ymax = gcmodel->ydata[i];
}
CVAL(gcmodel, YMIN) = ymin;
CVAL(gcmodel, YMAX) = ymax;
gcmodel->cached |= CBIT(YMIN) | CBIT(YMAX);
if (y_min)
*y_min = ymin;
if (y_max)
*y_max = ymax;
return TRUE;
}
/**
* gwy_graph_curve_model_get_ranges:
* @gcmodel: A graph curve model.
* @x_logscale: %TRUE if logarithmical scale is intended for the abscissa.
* @y_logscale: %TRUE if logarithmical scale is intended for the ordinate.
* @x_min: Location to store the minimum abscissa value to, or %NULL.
* @x_max: Location to store the maximum abscissa value to, or %NULL.
* @y_min: Location to store the minimum ordinate value to, or %NULL.
* @y_max: Location to store the maximum ordinate value to, or %NULL.
*
* Gets the log-scale suitable range minima of a graph curve.
*
* Parameters @x_logscale and @y_logscale determine which axis or axes are intended to use logarithmical scale. The
* range of displayble values for an axis generally depends on the other axis too as it acts as a filter. When both
* @x_logscale and @y_logscale are %FALSE, the returned minima are identical to those returned by
* gwy_graph_curve_model_get_x_range() and gwy_graph_curve_model_get_y_range().
*
* The return values are cached in the curve model therefore repeated calls to this function (with unchanged data) are
* cheap.
*
* If there are no data points that would be displayable with the intended logarithmical scale setup, the output
* arguments are untouched and %FALSE is returned.
*
* Returns: %TRUE if the output arguments were filled with the ranges.
*
* Since: 2.8
**/
gboolean
gwy_graph_curve_model_get_ranges(GwyGraphCurveModel *gcmodel,
gboolean x_logscale,
gboolean y_logscale,
gdouble *x_min,
gdouble *x_max,
gdouble *y_min,
gdouble *y_max)
{
gdouble xmin_xpos, ymin_xpos, ya0min, ya0min_xpos;
gdouble xret = 0.0, yret = 0.0;
gboolean ok;
gint i;
g_return_val_if_fail(GWY_IS_GRAPH_CURVE_MODEL(gcmodel), FALSE);
if (!x_logscale && !y_logscale)
return gwy_graph_curve_model_get_x_range(gcmodel, x_min, x_max)
&& gwy_graph_curve_model_get_y_range(gcmodel, y_min, y_max);
if (!gcmodel->n)
return FALSE;
if (!gcmodel->cache2)
gcmodel->cache2 = g_new0(gdouble, GWY_GRAPH_CURVE_MODEL_CACHE2_LAST - GWY_GRAPH_CURVE_MODEL_CACHE2_OFFSET);
/* We know all cache2 values are always calculated and cleared at once, so test just one of them. */
if (CTEST2(gcmodel, XMIN_XPOS)) {
xmin_xpos = CVAL2(gcmodel, XMIN_XPOS);
ymin_xpos = CVAL2(gcmodel, YMIN_XPOS);
ya0min = CVAL2(gcmodel, YAMIN);
ya0min_xpos = CVAL2(gcmodel, YAMIN_XPOS);
}
else {
xmin_xpos = ymin_xpos = ya0min = ya0min_xpos = G_MAXDOUBLE;
for (i = 0; i < gcmodel->n; i++) {
gdouble x = gcmodel->xdata[i];
gdouble y = gcmodel->ydata[i];
if (x > 0.0) {
if (x < xmin_xpos)
xmin_xpos = x;
if (y < ymin_xpos)
ymin_xpos = y;
}
y = fabs(y);
if (y > 0.0) {
if (x > 0.0 && y < ya0min_xpos)
ya0min_xpos = y;
if (y < ya0min)
ya0min = y;
}
}
/* Note we cache failures too. */
CVAL2(gcmodel, XMIN_XPOS) = xmin_xpos;
CVAL2(gcmodel, YMIN_XPOS) = ymin_xpos;
CVAL2(gcmodel, YAMIN) = ya0min;
CVAL2(gcmodel, YAMIN_XPOS) = ya0min_xpos;
gcmodel->cached |= (CBIT2(XMIN_XPOS) | CBIT2(YMIN_XPOS) | CBIT2(YAMIN) | CBIT2(YAMIN_XPOS));
}
ok = TRUE;
if (x_logscale) {
if (xmin_xpos != G_MAXDOUBLE) {
xret = xmin_xpos;
if (y_logscale) {
if (ya0min_xpos != G_MAXDOUBLE)
yret = ya0min_xpos;
else
ok = FALSE;
}
else {
if (ymin_xpos != G_MAXDOUBLE)
yret = ymin_xpos;
else
ok = FALSE;
}
}
else
ok = FALSE;
}
else {
if ((ok = gwy_graph_curve_model_get_x_range(gcmodel, &xret, NULL))) {
if (y_logscale) {
if (ya0min != G_MAXDOUBLE)
yret = ya0min;
else
ok = FALSE;
}
else
ok = ok && gwy_graph_curve_model_get_y_range(gcmodel, &yret, NULL);
}
}
if (ok) {
if (x_min)
*x_min = xret;
if (y_min)
*y_min = yret;
/* If ok is TRUE, minimum is positive so maximum is always safe. */
if (x_max)
gwy_graph_curve_model_get_x_range(gcmodel, NULL, x_max);
if (y_max) {
if (y_logscale) {
gdouble ym, yp;
gwy_graph_curve_model_get_y_range(gcmodel, &ym, &yp);
*y_max = MAX(fabs(ym), fabs(yp));
}
else
gwy_graph_curve_model_get_y_range(gcmodel, NULL, y_max);
}
}
return ok;
}
static void
gwy_graph_curve_model_data_changed(GwyGraphCurveModel *gcmodel)
{
gcmodel->cached = 0;
g_signal_emit(gcmodel, graph_curve_model_signals[DATA_CHANGED], 0);
}
/**
* gwy_graph_curve_model_get_calibration_data:
* @gcmodel: A graph curve model.
*
* Get pointer to actual calibration data for curve.
*
* Returns: Pointer to the calibration data of present curve (NULL if none).
*
* Since: 2.23
**/
GwyCurveCalibrationData*
gwy_graph_curve_model_get_calibration_data(GwyGraphCurveModel *gcmodel)
{
g_return_val_if_fail(GWY_IS_GRAPH_CURVE_MODEL(gcmodel), NULL);
return gcmodel->calibration;
}
/**
* gwy_graph_curve_model_set_calibration_data:
* @gcmodel: A graph curve model.
* @calibration: Curve calibration data
*
* Set calibration data for curve.
*
* The function makes a deep copy of @calibration.
*
* Since: 2.23
**/
void
gwy_graph_curve_model_set_calibration_data(GwyGraphCurveModel *gcmodel,
const GwyCurveCalibrationData *calibration)
{
gsize size;
g_return_if_fail(GWY_IS_GRAPH_CURVE_MODEL(gcmodel));
free_calibration(gcmodel);
if (!calibration)
return;
size = calibration->n * sizeof(gdouble);
gcmodel->calibration = g_new(GwyCurveCalibrationData, 1);
gcmodel->calibration->n = calibration->n;
gcmodel->calibration->xerr = g_memdup(calibration->xerr, size);
gcmodel->calibration->yerr = g_memdup(calibration->yerr, size);
gcmodel->calibration->zerr = g_memdup(calibration->zerr, size);
gcmodel->calibration->xunc = g_memdup(calibration->xunc, size);
gcmodel->calibration->yunc = g_memdup(calibration->yunc, size);
gcmodel->calibration->zunc = g_memdup(calibration->zunc, size);
}
static void
free_calibration(GwyGraphCurveModel *gcmodel)
{
GwyCurveCalibrationData *calibration = gcmodel->calibration;
if (!calibration)
return;
g_free(calibration->xerr);
g_free(calibration->yerr);
g_free(calibration->zerr);
g_free(calibration->xunc);
g_free(calibration->yunc);
g_free(calibration->zunc);
g_free(calibration);
gcmodel->calibration = NULL;
}
/************************** Documentation ****************************/
/**
* SECTION:gwygraphcurvemodel
* @title: GwyGraphCurveModel
* @short_description: Representation of one graph curve
*
* #GwyGraphCurveModel represents information about a graph curve necessary to fully reconstruct it.
**/
/**
* gwy_graph_curve_model_duplicate:
* @gcmodel: A graph curve model to duplicate.
*
* Convenience macro doing gwy_serializable_duplicate() with all the necessary typecasting.
**/
/* vim: set cin columns=120 tw=118 et ts=4 sw=4 cino=>1s,e0,n0,f0,{0,}0,^0,\:1s,=0,g1s,h0,t0,+1s,c3,(0,u0 : */
|