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
|
/*
* $Id: spectra.c 25861 2023-10-15 14:21:00Z yeti-dn $
* Copyright (C) 2006-2018 Owain Davies, David Necas (Yeti), Petr Klapetek.
* E-mail: owain.davies@blueyonder.co.uk
* 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 <stdlib.h>
#include <string.h>
#include <glib.h>
#include <libgwyddion/gwymacros.h>
#include <libgwyddion/gwymath.h>
#include <libgwyddion/gwydebugobjects.h>
#include <libprocess/spectra.h>
#include <libprocess/linestats.h>
#include <libprocess/interpolation.h>
#include "gwyprocessinternal.h"
#define GWY_SPECTRA_TYPE_NAME "GwySpectra"
/* default number number of spectra allocated to data and coords */
#define DEFAULT_ALLOC_SIZE 5
enum {
PROP_0,
PROP_TITLE,
PROP_SPECTRUM_X_LABEL,
PROP_SPECTRUM_Y_LABEL,
PROP_LAST
};
enum {
DATA_CHANGED,
LAST_SIGNAL
};
typedef struct {
guint index;
gdouble r;
} CoordPos;
typedef struct {
gdouble x;
gdouble y;
GwyDataLine *ydata;
gboolean selected;
} GwySpectrum;
static void gwy_spectra_finalize (GObject *object);
static void gwy_spectra_serializable_init(GwySerializableIface *iface);
static GByteArray* gwy_spectra_serialize (GObject *obj,
GByteArray *buffer);
static gsize gwy_spectra_get_size (GObject *obj);
static GObject* gwy_spectra_deserialize (const guchar *buffer,
gsize size,
gsize *position);
static GObject* gwy_spectra_duplicate_real (GObject *object);
static void gwy_spectra_clone_real (GObject *source,
GObject *copy);
static void gwy_spectra_set_property (GObject *object,
guint prop_id,
const GValue *value,
GParamSpec *pspec);
static void gwy_spectra_get_property (GObject *object,
guint prop_id,
GValue *value,
GParamSpec *pspec);
static guint spectra_signals[LAST_SIGNAL] = { 0 };
G_DEFINE_TYPE_EXTENDED
(GwySpectra, gwy_spectra, G_TYPE_OBJECT, 0,
GWY_IMPLEMENT_SERIALIZABLE(gwy_spectra_serializable_init))
static void
gwy_spectra_serializable_init(GwySerializableIface *iface)
{
gwy_debug("");
iface->serialize = gwy_spectra_serialize;
iface->deserialize = gwy_spectra_deserialize;
iface->get_size = gwy_spectra_get_size;
iface->duplicate = gwy_spectra_duplicate_real;
iface->clone = gwy_spectra_clone_real;
}
static void
gwy_spectra_class_init(GwySpectraClass *klass)
{
GObjectClass *gobject_class = G_OBJECT_CLASS(klass);
gobject_class->finalize = gwy_spectra_finalize;
gobject_class->get_property = gwy_spectra_get_property;
gobject_class->set_property = gwy_spectra_set_property;
g_object_class_install_property
(gobject_class,
PROP_TITLE,
g_param_spec_string("title",
"Title",
"The spectra title",
"New spectra",
G_PARAM_READWRITE));
/**
* GwySpectra:spectrum-x-label:
*
* Label of spectrum abscissa, as it should appear on extracted graphs.
*
* Since: 2.40
**/
g_object_class_install_property
(gobject_class,
PROP_SPECTRUM_X_LABEL,
g_param_spec_string("spectrum-x-label",
"Spectrum X label",
"Label of spectrum abscissa",
NULL,
G_PARAM_READWRITE));
/**
* GwySpectra:spectrum-y-label:
*
* Label of spectrum ordinate, as it should appear on extracted graphs.
*
* Since: 2.40
**/
g_object_class_install_property
(gobject_class,
PROP_SPECTRUM_Y_LABEL,
g_param_spec_string("spectrum-y-label",
"Spectrum Y label",
"Label of spectrum ordinate",
NULL,
G_PARAM_READWRITE));
/**
* GwySpectra::data-changed:
* @gwyspectra: The #GwySpectra which received the signal.
*
* The ::data-changed signal is never emitted by the spectra itself. It
* is intended as a means to notify other spectra users they should
* update themselves.
**/
spectra_signals[DATA_CHANGED]
= g_signal_new("data-changed",
G_OBJECT_CLASS_TYPE(gobject_class),
G_SIGNAL_RUN_FIRST,
G_STRUCT_OFFSET(GwySpectraClass, data_changed),
NULL, NULL,
g_cclosure_marshal_VOID__VOID,
G_TYPE_NONE, 0);
}
static void
gwy_spectra_init(GwySpectra *spectra)
{
spectra->spectra = g_array_new(FALSE, FALSE, sizeof(GwySpectrum));
}
static void
gwy_spectra_finalize(GObject *object)
{
GwySpectra *spectra = (GwySpectra*)object;
guint i;
GWY_OBJECT_UNREF(spectra->si_unit_xy);
for (i = 0; i < spectra->spectra->len; i++) {
GwySpectrum *spec = &g_array_index(spectra->spectra, GwySpectrum, i);
GWY_OBJECT_UNREF(spec->ydata);
}
g_array_free(spectra->spectra, TRUE);
spectra->spectra = NULL;
g_free(spectra->title);
g_free(spectra->spec_xlabel);
g_free(spectra->spec_ylabel);
G_OBJECT_CLASS(gwy_spectra_parent_class)->finalize(object);
}
static void
gwy_spectra_set_property(GObject *object,
guint prop_id,
const GValue *value,
GParamSpec *pspec)
{
GwySpectra *spectra = GWY_SPECTRA(object);
switch (prop_id) {
case PROP_TITLE:
gwy_spectra_set_title(spectra, g_value_get_string(value));
break;
case PROP_SPECTRUM_X_LABEL:
gwy_spectra_set_spectrum_x_label(spectra, g_value_get_string(value));
break;
case PROP_SPECTRUM_Y_LABEL:
gwy_spectra_set_spectrum_y_label(spectra, g_value_get_string(value));
break;
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID(object, prop_id, pspec);
break;
}
}
static void
gwy_spectra_get_property(GObject*object,
guint prop_id,
GValue *value,
GParamSpec *pspec)
{
GwySpectra *spectra = GWY_SPECTRA(object);
switch (prop_id) {
case PROP_TITLE:
g_value_set_string(value, spectra->title);
break;
case PROP_SPECTRUM_X_LABEL:
g_value_set_string(value, spectra->spec_xlabel);
break;
case PROP_SPECTRUM_Y_LABEL:
g_value_set_string(value, spectra->spec_ylabel);
break;
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID(object, prop_id, pspec);
break;
}
}
/**
* gwy_spectra_new:
*
* Creates a new Spectra object containing zero spectra.
*
* Returns: A newly created spectra.
*
* Since: 2.7
**/
GwySpectra*
gwy_spectra_new(void)
{
GwySpectra *spectra;
gwy_debug("");
spectra = g_object_new(GWY_TYPE_SPECTRA, NULL);
return spectra;
}
/**
* gwy_spectra_new_alike:
* @model: A Spectra object to take units from.
*
* Creates a new Spectra object similar to an existing one, but containing zero
* spectra.
*
* Use gwy_spectra_duplicate() if you want to copy a spectra object including
* the spectra in it.
*
* Returns: A newly created Spectra object.
*
* Since: 2.7
**/
GwySpectra*
gwy_spectra_new_alike(GwySpectra *model)
{
GwySpectra *spectra;
g_return_val_if_fail(GWY_IS_SPECTRA(model), NULL);
spectra = g_object_new(GWY_TYPE_SPECTRA, NULL);
_gwy_copy_si_unit(model->si_unit_xy, &spectra->si_unit_xy);
return spectra;
}
static void
separate_arrays(GArray *spectra,
guint *ncurves,
GwyDataLine ***curves,
guint *ncoords,
gdouble **coords,
guint *nselected,
guint32 **selected)
{
guint isize, i;
*ncurves = spectra->len;
*curves = g_new(GwyDataLine*, *ncurves);
*ncoords = 2*spectra->len;
*coords = g_new(gdouble, *ncoords);
isize = 8*sizeof(guint32);
*nselected = (spectra->len + isize-1)/isize;
*selected = g_new0(guint32, *nselected);
for (i = 0; i < *ncurves; i++) {
GwySpectrum *spec = &g_array_index(spectra, GwySpectrum, i);
(*curves)[i] = spec->ydata;
(*coords)[2*i + 0] = spec->x;
(*coords)[2*i + 1] = spec->y;
if (spec->selected)
(*selected)[i/isize] |= 1 << (i % isize);
}
}
static guint
fix_serialisation_spec(GwySerializeSpec *spec)
{
/* Don't serialise spec_xlabel and spec_ylabel if they are NULL. */
if (!spec[6].value) {
if (!spec[5].value)
return 5;
return 6;
}
else if (!spec[5].value) {
GWY_SWAP(GwySerializeSpec, spec[5], spec[6]);
return 6;
}
return 7;
}
static GByteArray*
gwy_spectra_serialize(GObject *obj,
GByteArray *buffer)
{
GwySpectra *spectra;
GwyDataLine **curves;
gdouble *coords;
guint32 *selected;
guint32 ncurves, ncoords, nselected;
GByteArray *retval;
gpointer pxyunit, pxlabel, pylabel;
spectra = GWY_SPECTRA(obj);
separate_arrays(spectra->spectra,
&ncurves, &curves,
&ncoords, &coords,
&nselected, &selected);
pxyunit = unit_pointer_if_nonempty(spectra->si_unit_xy);
pxlabel = spectra->spec_xlabel ? &spectra->spec_xlabel : NULL;
pylabel = spectra->spec_ylabel ? &spectra->spec_ylabel : NULL;
{
GwySerializeSpec spec[] = {
{ 's', "title", &spectra->title, NULL, },
{ 'o', "si_unit_xy", pxyunit, NULL, },
{ 'D', "coords", &coords, &ncoords, },
{ 'I', "selected", &selected, &nselected, },
{ 'O', "data", &curves, &ncurves, },
{ 's', "spec_xlabel", pxlabel, NULL, },
{ 's', "spec_ylabel", pylabel, NULL, },
};
guint nspec = fix_serialisation_spec(spec);
retval = gwy_serialize_pack_object_struct(buffer,
GWY_SPECTRA_TYPE_NAME,
nspec, spec);
}
g_free(curves);
g_free(coords);
g_free(selected);
return retval;
}
static gsize
gwy_spectra_get_size(GObject *obj)
{
GwySpectra *spectra;
GwyDataLine **curves;
gdouble *coords;
guint32 *selected;
guint32 ncurves, ncoords, nselected;
gsize retval;
gpointer pxyunit;
spectra = GWY_SPECTRA(obj);
separate_arrays(spectra->spectra,
&ncurves, &curves,
&ncoords, &coords,
&nselected, &selected);
pxyunit = unit_pointer_if_nonempty(spectra->si_unit_xy);
{
GwySerializeSpec spec[] = {
{ 's', "title", &spectra->title, NULL, },
{ 'o', "si_unit_xy", pxyunit, NULL, },
{ 'D', "coords", &coords, &ncoords, },
{ 'I', "selected", &selected, &nselected, },
{ 'O', "data", &curves, &ncurves, },
{ 's', "spec_xlabel", &spectra->spec_xlabel, NULL, },
{ 's', "spec_ylabel", &spectra->spec_ylabel, NULL, },
};
guint nspec = fix_serialisation_spec(spec);
retval = gwy_serialize_get_struct_size(GWY_SPECTRA_TYPE_NAME,
nspec, spec);
}
g_free(curves);
g_free(coords);
g_free(selected);
return retval;
}
static GObject*
gwy_spectra_deserialize(const guchar *buffer,
gsize size,
gsize *position)
{
guint32 ncoords = 0, ncurves = 0, nselected = 0;
gdouble *coords = NULL;
guint32 *selected = NULL;
GwySIUnit *si_unit_xy = NULL;
GwyDataLine **curves = NULL;
GwySpectra *spectra;
gchar* title = NULL, *spec_xlabel = NULL, *spec_ylabel = NULL;
guint isize, i;
gwy_debug("");
g_return_val_if_fail(buffer, NULL);
{
GwySerializeSpec spec[] = {
{ 's', "title", &title, NULL, },
{ 'o', "si_unit_xy", &si_unit_xy, NULL, },
{ 'D', "coords", &coords, &ncoords, },
{ 'I', "selected", &selected, &nselected, },
{ 'O', "data", &curves, &ncurves, },
{ 's', "spec_xlabel", &spec_xlabel, NULL, },
{ 's', "spec_ylabel", &spec_ylabel, NULL, },
};
if (!gwy_serialize_unpack_object_struct(buffer, size, position,
GWY_SPECTRA_TYPE_NAME,
G_N_ELEMENTS(spec), spec)) {
for (i = 0; i < ncurves; i++)
GWY_OBJECT_UNREF(curves[i]);
g_free(curves);
g_free(coords);
g_free(selected);
g_free(title);
g_free(spec_xlabel);
g_free(spec_ylabel);
GWY_OBJECT_UNREF(si_unit_xy);
return NULL;
}
}
isize = 8*sizeof(guint32);
if (2*ncurves != ncoords
|| (nselected && (ncurves + isize-1)/isize != nselected)) {
g_critical("Serialized coordinate, data and selection array size "
"mismatch");
for (i = 0; i < ncurves; i++)
GWY_OBJECT_UNREF(curves[i]);
g_free(curves);
g_free(coords);
g_free(selected);
g_free(title);
g_free(spec_xlabel);
g_free(spec_ylabel);
GWY_OBJECT_UNREF(si_unit_xy);
return NULL;
}
spectra = gwy_spectra_new();
g_free(spectra->title);
spectra->title = title;
g_free(spectra->spec_xlabel);
spectra->spec_xlabel = spec_xlabel;
g_free(spectra->spec_ylabel);
spectra->spec_ylabel = spec_ylabel;
/* Preallocate ncurves items */
g_array_set_size(spectra->spectra, ncurves);
g_array_set_size(spectra->spectra, 0);
for (i = 0; i < ncurves; i++) {
GwySpectrum spec;
spec.x = coords[2*i + 0];
spec.y = coords[2*i + 1];
spec.ydata = curves[i];
if (nselected)
spec.selected = !!(selected[i/isize] & (1 << (i % isize)));
else
spec.selected = FALSE;
g_array_append_val(spectra->spectra, spec);
}
g_free(curves);
g_free(coords);
g_free(selected);
if (si_unit_xy) {
GWY_OBJECT_UNREF(spectra->si_unit_xy);
spectra->si_unit_xy = si_unit_xy;
}
return (GObject*)spectra;
}
static GObject*
gwy_spectra_duplicate_real(GObject *object)
{
gint i;
GwySpectra *spectra, *duplicate;
GwyDataLine *dataline;
g_return_val_if_fail(GWY_IS_SPECTRA(object), NULL);
spectra = GWY_SPECTRA(object);
duplicate = gwy_spectra_new_alike(spectra);
duplicate->title = g_strdup(spectra->title);
_gwy_copy_si_unit(spectra->si_unit_xy, &duplicate->si_unit_xy);
g_array_append_vals(duplicate->spectra, spectra->spectra->data,
spectra->spectra->len);
/* Duplicate the spectra themselves */
for (i = 0; i < duplicate->spectra->len; i++) {
dataline = g_array_index(duplicate->spectra, GwySpectrum, i).ydata;
g_array_index(duplicate->spectra, GwySpectrum, i).ydata
= gwy_data_line_duplicate(dataline);
}
return (GObject*)duplicate;
}
static void
gwy_spectra_clone_real(GObject *source, GObject *copy)
{
GwySpectra *spectra, *clone;
GwyDataLine *dataline;
guint i;
g_return_if_fail(GWY_IS_SPECTRA(source));
g_return_if_fail(GWY_IS_SPECTRA(copy));
spectra = GWY_SPECTRA(source);
clone = GWY_SPECTRA(copy);
/* Title */
gwy_assign_string(&clone->title, spectra->title);
/* Remove any existing datalines in the clone */
for (i = 0; i < clone->spectra->len; i++) {
g_object_unref(g_array_index(clone->spectra, GwySpectrum, i).ydata);
}
/* Copy the spectra to clone */
g_array_set_size(clone->spectra, 0);
g_array_append_vals(clone->spectra, spectra->spectra->data,
spectra->spectra->len);
/* Clone the spectra theselves */
for (i = 0; i < spectra->spectra->len; i++) {
dataline = g_array_index(clone->spectra, GwySpectrum, i).ydata;
g_array_index(clone->spectra, GwySpectrum, i).ydata
= gwy_data_line_duplicate(dataline);
}
_gwy_copy_si_unit(spectra->si_unit_xy, &clone->si_unit_xy);
}
/**
* gwy_spectra_data_changed:
* @spectra: A spectra object.
*
* Emits signal "data_changed" on a spectra object.
*
* Since: 2.7
**/
void
gwy_spectra_data_changed(GwySpectra *spectra)
{
g_signal_emit(spectra, spectra_signals[DATA_CHANGED], 0);
}
/**
* gwy_spectra_get_si_unit_xy:
* @spectra: A spectra.
*
* Gets SI unit used for the location co-ordinates of spectra.
*
* Returns: SI unit corresponding to the the location co-ordinates of spectra
* object. Its reference count is not incremented.
*
* Since: 2.7
**/
GwySIUnit*
gwy_spectra_get_si_unit_xy(GwySpectra *spectra)
{
g_return_val_if_fail(GWY_IS_SPECTRA(spectra), NULL);
if (!spectra->si_unit_xy)
spectra->si_unit_xy = gwy_si_unit_new(NULL);
return spectra->si_unit_xy;
}
/**
* gwy_spectra_set_si_unit_xy:
* @spectra: A Spectra object.
* @si_unit: SI unit to be set.
*
* Sets the SI unit corresponding to the location co-ordinates of the spectra
* object.
*
* It does not assume a reference on @si_unit, instead it adds its own
* reference.
*
* Since: 2.7
**/
void
gwy_spectra_set_si_unit_xy(GwySpectra *spectra,
GwySIUnit *si_unit)
{
g_return_if_fail(GWY_IS_SPECTRA(spectra));
_gwy_set_object_si_unit(si_unit, &spectra->si_unit_xy);
}
/**
* gwy_spectra_itoxy:
* @spectra: A spectra object.
* @i: Index of a spectrum.
* @x: Location to store the physical x coordinate of the spectrum.
* @y: Location to store the physical x coordinate of the spectrum.
*
* Gets the coordinates of one spectrum.
*
* Since: 2.7
**/
void
gwy_spectra_itoxy(GwySpectra *spectra,
guint i,
gdouble *x,
gdouble *y)
{
GwySpectrum *spec;
g_return_if_fail(GWY_IS_SPECTRA(spectra));
g_return_if_fail(i < spectra->spectra->len);
spec = &g_array_index(spectra->spectra, GwySpectrum, i);
if (x)
*x = spec->x;
if (y)
*y = spec->y;
}
/**
* gwy_spectra_xytoi:
* @spectra: A spectra object.
* @x: The x coordinate of the location of the spectrum.
* @y: The y coordinate of the location of the spectrum.
*
* Finds the index of the spectrum closest to the location specified by
* the coordinates x and y.
*
* Returns: The index of the nearest spectrum. If there are no curves in the
* spectra, -1 is returned.
*
* Since: 2.7
**/
gint
gwy_spectra_xytoi(GwySpectra *spectra, gdouble x, gdouble y)
{
guint i;
g_return_val_if_fail(GWY_IS_SPECTRA(spectra), -1);
if (!spectra->spectra->len)
return -1;
gwy_spectra_find_nearest(spectra, x, y, 1, &i);
return i;
}
static gint
compare_coord_pos(gconstpointer a, gconstpointer b)
{
const CoordPos *acp = (const CoordPos*)a;
const CoordPos *bcp = (const CoordPos*)b;
if (acp->r < bcp->r)
return -1;
if (acp->r > bcp->r)
return 1;
return 0;
}
/**
* gwy_spectra_find_nearest:
* @spectra: A spectra object.
* @x: Point x-coordinate.
* @y: Point y-coordinate.
* @n: Number of indices to find. Array @ilist must have at least this
* number of items.
* @ilist: Array to place the spectra indices to. They will be sorted by the
* distance from (@x, @y). Positions after the number of spectra
* in @spectra will be left untouched.
*
* Gets the list of the indices to spectra ordered by their distance from a
* given point.
*
* List positions
*
* Since: 2.7
**/
void
gwy_spectra_find_nearest(GwySpectra *spectra,
gdouble x,
gdouble y,
guint n,
guint *ilist)
{
enum { DIRECT_LIMIT = 6 };
CoordPos *items;
guint nspec, i;
g_return_if_fail(GWY_IS_SPECTRA(spectra));
g_return_if_fail(ilist || !n);
nspec = spectra->spectra->len;
n = MIN(n, nspec);
if (!n)
return;
if (n <= DIRECT_LIMIT) {
items = g_newa(CoordPos, n);
/* Initialize with sorted initial n items */
for (i = 0; i < n; i++) {
GwySpectrum *spec = &g_array_index(spectra->spectra, GwySpectrum,
i);
items[i].index = i;
items[i].r = ((spec->x - x)*(spec->x - x)
+ (spec->y - y)*(spec->y - y));
}
qsort(items, n, sizeof(CoordPos), compare_coord_pos);
/* And let the remaining items compete for positions */
for (i = n; i < nspec; i++) {
guint j, k;
gdouble r;
GwySpectrum *spec = &g_array_index(spectra->spectra, GwySpectrum,
i);
r = ((spec->x - x)*(spec->x - x)
+ (spec->y - y)*(spec->y - y));
if (r < items[n-1].r) {
for (j = 1; j < n && r < items[n-1 - j].r; j++)
;
for (k = 0; k < j-1; k++)
items[n-k] = items[n-1 - k];
items[n-j].index = i;
items[n-j].r = r;
}
}
/* Move the results to ilist */
for (i = 0; i < n; i++)
ilist[i] = items[i].index;
}
else {
/* Sort all items and take the head */
items = g_new(CoordPos, spectra->spectra->len);
for (i = 0; i < spectra->spectra->len; i++) {
GwySpectrum *spec = &g_array_index(spectra->spectra, GwySpectrum,
i);
items[i].index = i;
items[i].r = ((spec->x - x)*(spec->x - x)
+ (spec->y - y)*(spec->y - y));
}
qsort(items, nspec, sizeof(CoordPos), compare_coord_pos);
for (i = 0; i < n; i++)
ilist[i] = items[i].index;
g_free(items);
}
}
/**
* gwy_spectra_setpos:
* @spectra: A spectra object.
* @i: The index of a spectrum.
* @x: The new x coordinate of the location of the spectrum.
* @y: The new y coordinate of the location of the spectrum.
*
* Sets the location coordinates of a spectrum.
*
* Since: 2.7
**/
void
gwy_spectra_setpos(GwySpectra *spectra,
guint i,
gdouble x, gdouble y)
{
GwySpectrum *spec;
g_return_if_fail(GWY_IS_SPECTRA(spectra));
g_return_if_fail(i < spectra->spectra->len);
spec = &g_array_index(spectra->spectra, GwySpectrum, i);
spec->x = x;
spec->y = y;
}
/**
* gwy_spectra_get_spectrum:
* @spectra: A Spectra object
* @i: Index of a spectrum
*
* Gets a dataline that contains the spectrum at index i.
*
* Returns: A #GwyDataLine containing the spectrum, owned by @spectra.
*
* Since: 2.7
**/
GwyDataLine*
gwy_spectra_get_spectrum(GwySpectra *spectra, gint i)
{
GwySpectrum *spec;
g_return_val_if_fail(GWY_IS_SPECTRA(spectra), NULL);
g_return_val_if_fail(i < spectra->spectra->len, NULL);
spec = &g_array_index(spectra->spectra, GwySpectrum, i);
return spec->ydata;
}
/**
* gwy_spectra_set_spectrum:
* @spectra: A spectra object.
* @i: Index of a spectrum to replace
* @new_spectrum: A #GwyDataLine Object containing the new spectrum.
*
* Replaces the ith spectrum in the spectra object with a the
* supplied spectrum, new_spectrum. It takes its own reference
* to the New_Spectrum dataline.
*
* Since: 2.7
**/
void
gwy_spectra_set_spectrum(GwySpectra *spectra,
guint i,
GwyDataLine *new_spectrum)
{
GwySpectrum *spec;
g_return_if_fail(GWY_IS_SPECTRA(spectra));
g_return_if_fail(GWY_IS_DATA_LINE(new_spectrum));
g_return_if_fail(i < spectra->spectra->len);
spec = &g_array_index(spectra->spectra, GwySpectrum, i);
g_object_ref(new_spectrum);
g_object_unref(spec->ydata);
spec->ydata = new_spectrum;
}
/**
* gwy_spectra_set_spectrum_selected:
* @spectra: A spectra object.
* @i: Index of a spectrum.
* @selected: %TRUE to make the spectrum selected, %FALSE to deselect it.
*
* Sets selected state of a spectrum in a spectra object.
*
* Since: 2.7
**/
void
gwy_spectra_set_spectrum_selected(GwySpectra *spectra,
guint i,
gboolean selected)
{
GwySpectrum *spec;
g_return_if_fail(GWY_IS_SPECTRA(spectra));
g_return_if_fail(i < spectra->spectra->len);
spec = &g_array_index(spectra->spectra, GwySpectrum, i);
if (spec->selected != !!selected) {
spec->selected = selected;
/* TODO: Emit something or whatever. */
}
}
/**
* gwy_spectra_get_spectrum_selected:
* @spectra: A spectra object.
* @i: Index of a spectrum.
*
* Gets the selected state of a spectrum in a spectra object.
*
* Returns: %TRUE if spectrum is selected.
*
* Since: 2.7
**/
gboolean
gwy_spectra_get_spectrum_selected(GwySpectra *spectra,
guint i)
{
g_return_val_if_fail(GWY_IS_SPECTRA(spectra), FALSE);
g_return_val_if_fail(i < spectra->spectra->len, FALSE);
return g_array_index(spectra->spectra, GwySpectrum, i).selected;
}
/**
* gwy_spectra_get_n_spectra:
* @spectra: A spectra object.
*
* Gets the number of spectra in a spectra object.
*
* Returns: The number of spectra.
*
* Since: 2.7
**/
guint
gwy_spectra_get_n_spectra(GwySpectra *spectra)
{
g_return_val_if_fail(GWY_IS_SPECTRA(spectra), 0);
return spectra->spectra->len;
}
/**
* gwy_spectra_add_spectrum:
* @spectra: A spectra object.
* @new_spectrum: A GwyDataLine containing the spectrum to append.
* @x: The physical x coordinate of the location of the spectrum.
* @y: The physical y coordinate of the location of the spectrum.
*
* Adds a new spectrum to the spectra collection with a position of x, y.
*
* The spectra collection takes a reference to @new_spectrum.
*
* Since: 2.7
**/
void
gwy_spectra_add_spectrum(GwySpectra *spectra,
GwyDataLine *new_spectrum,
gdouble x, gdouble y)
{
GwySpectrum spec;
g_return_if_fail(GWY_IS_SPECTRA(spectra));
g_return_if_fail(GWY_IS_DATA_LINE(new_spectrum));
g_object_ref(new_spectrum);
spec.x = x;
spec.y = y;
spec.ydata = new_spectrum;
spec.selected = FALSE;
g_array_append_val(spectra->spectra, spec);
}
/**
* gwy_spectra_remove_spectrum:
* @spectra: A spectra object.
* @i: Index of spectrum to remove.
*
* Removes the ith spectrum from the Spectra collection. The subsequent
* spectra are moved down one place.
*
* Since: 2.7
**/
void
gwy_spectra_remove_spectrum(GwySpectra *spectra,
guint i)
{
GwySpectrum *spec;
g_return_if_fail(GWY_IS_SPECTRA(spectra));
g_return_if_fail(i < spectra->spectra->len);
spec = &g_array_index(spectra->spectra, GwySpectrum, i);
g_object_unref(spec->ydata);
g_array_remove_index(spectra->spectra, i);
}
/* FIXME: Uncomment once it does anything. */
#if 0
/* function to calculate the angle A ^B^ C
* points is an array in the format:
* Ax, Ay, Bx, By, Cx, Cy
*/
static gdouble
calc_angle(const gdouble* points)
{
gdouble Ax, Ay, Bx, By, Cx, Cy;
gdouble aa, bb, cc, beta;
Ax = points[0];
Ay = points[1];
Bx = points[2];
By = points[3];
Cx = points[4];
Cy = points[5];
aa = (Bx-Cx)*(Bx-Cx)+(By-Cy)*(By-Cy);
bb = (Ax-Cx)*(Ax-Cx)+(Ay-Cy)*(Ay-Cy);
cc = (Ax-Bx)*(Ax-Bx)+(Ay-By)*(Ay-By);
beta = acos((aa+cc-bb)/(sqrt(4*aa*cc)));
return beta;
}
static gdouble*
tri_interp_weight(gdouble x, gdouble y,
const gdouble* triangle)
{
/* TODO: Function to get the interpolation weightings
between three points*/
return NULL;
}
/**
* gwy_spectra_get_spectra_interp:
* @spectra: A spectra object.
* @x: x coordinate for spectrum
* @y: y coordinate fro spectrum
*
* In future will:
* Interpolates a spectrum at some arbitary point specified by x,y.
* If the point is within the triangle made by three measured spectra
* then these are used else it is interpolated from the nearest two or one.
* But currently:
* Returns a newly created dataline, corresponding to the nearest spectrum.
*
* Returns: A newly created dataline.
**/
GwyDataLine*
gwy_spectra_get_spectra_interp(GwySpectra *spectra,
gdouble x, gdouble y)
{
guint i;
/* TODO: Function to do a proper interpolation. */
g_return_val_if_fail(GWY_IS_SPECTRA(spectra), NULL);
if (spectra->ncurves == 0) {
g_critical("Contains no spectra");
return NULL;
}
i = gwy_spectra_xytoi(spectra, x, y);
return gwy_data_line_duplicate(gwy_spectra_get_spectrum(spectra, i));
}
#endif
/**
* gwy_spectra_get_title:
* @spectra: A spectra object.
*
* Gets the title of spectra.
*
* Returns: A pointer to the title string (owned by the spectra object).
*
* Since: 2.7
**/
const gchar*
gwy_spectra_get_title(GwySpectra *spectra)
{
g_return_val_if_fail(GWY_IS_SPECTRA(spectra), NULL);
return spectra->title;
}
/**
* gwy_spectra_set_title:
* @spectra: A spectra object.
* @title: The new title string.
*
* Sets the title of the spectra collection.
*
* Since: 2.7
**/
void
gwy_spectra_set_title(GwySpectra *spectra,
const gchar *title)
{
g_return_if_fail(GWY_IS_SPECTRA(spectra));
gwy_assign_string(&spectra->title, title);
g_object_notify(G_OBJECT(spectra), "title");
}
/**
* gwy_spectra_get_spectrum_x_label:
* @spectra: A spectra object.
*
* Gets the spectrum abscissa label of a spectra object.
*
* Returns: The abscissa label. The string is owned by @spectra and must not
* be modified nor freed.
*
* Since: 2.40
**/
const gchar*
gwy_spectra_get_spectrum_x_label(GwySpectra *spectra)
{
g_return_val_if_fail(GWY_IS_SPECTRA(spectra), NULL);
return spectra->spec_xlabel;
}
/**
* gwy_spectra_set_spectrum_x_label:
* @spectra: A spectra object.
* @label: New abscissa label.
*
* Sets the spectrum abscissa label of a spectra object.
*
* Since: 2.40
**/
void
gwy_spectra_set_spectrum_x_label(GwySpectra *spectra,
const gchar *label)
{
g_return_if_fail(GWY_IS_SPECTRA(spectra));
gwy_assign_string(&spectra->spec_xlabel, label);
}
/**
* gwy_spectra_get_spectrum_y_label:
* @spectra: A spectra object.
*
* Gets the spectrum ordinate label of a spectra object.
*
* Returns: The ordinate label. The string is owned by @spectra and must not
* be modified nor freed.
*
* Since: 2.40
**/
const gchar*
gwy_spectra_get_spectrum_y_label(GwySpectra *spectra)
{
g_return_val_if_fail(GWY_IS_SPECTRA(spectra), NULL);
return spectra->spec_ylabel;
}
/**
* gwy_spectra_set_spectrum_y_label:
* @spectra: A spectra object.
* @label: New ordinate label.
*
* Sets the spectrum ordinate label of a spectra object.
*
* Since: 2.40
**/
void
gwy_spectra_set_spectrum_y_label(GwySpectra *spectra,
const gchar *label)
{
g_return_if_fail(GWY_IS_SPECTRA(spectra));
gwy_assign_string(&spectra->spec_ylabel, label);
}
/**
* gwy_spectra_clear:
* @spectra: A spectra object.
*
* Removes all spectra from the collection.
*
* Since: 2.7
**/
void
gwy_spectra_clear(GwySpectra *spectra)
{
guint i;
g_return_if_fail(GWY_IS_SPECTRA(spectra));
for (i = 0; i < spectra->spectra->len; i++) {
GwySpectrum *spec = &g_array_index(spectra->spectra, GwySpectrum, i);
g_object_unref(spec->ydata);
}
g_array_set_size(spectra->spectra, 0);
}
/************************** Documentation ****************************/
/**
* SECTION:spectra
* @title: GwySpectra
* @short_description: Collection of dataline representing point spectra.
*
* #GwySpectra contains an array of #GwyDataLine<!-- -->s and coordinates
* representing where in a data field the spectrum was acquired.
**/
/**
* GwySpectra:
*
* The #GwySpectra struct contains private data only and should be accessed
* using the functions below.
*
* Since: 2.7
**/
/**
* gwy_spectra_duplicate:
* @spectra: A Spectra object to duplicate.
*
* Convenience macro doing gwy_serializable_duplicate() with all the necessary
* typecasting.
*
* Since: 2.7
**/
/* vim: set cin et ts=4 sw=4 cino=>1s,e0,n0,f0,{0,}0,^0,\:1s,=0,g1s,h0,t0,+1s,c3,(0,u0 : */
|