File: graph_cut.c

package info (click to toggle)
gwyddion 2.62-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 51,952 kB
  • sloc: ansic: 398,486; python: 7,877; sh: 5,492; makefile: 4,723; xml: 3,883; cpp: 1,969; pascal: 418; perl: 154; ruby: 130
file content (498 lines) | stat: -rw-r--r-- 16,274 bytes parent folder | download
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
/*
 *  $Id: graph_cut.c 24414 2021-10-22 16:05:29Z yeti-dn $
 *  Copyright (C) 2003-2016 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 CUTNESS 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/gwymacros.h>
#include <libgwyddion/gwymath.h>
#include <libgwydgets/gwygraph.h>
#include <libgwydgets/gwystock.h>
#include <libgwydgets/gwyinventorystore.h>
#include <libgwydgets/gwycombobox.h>
#include <libgwydgets/gwystock.h>
#include <libgwydgets/gwydgetutils.h>
#include <libgwymodule/gwymodule-graph.h>
#include <app/gwyapp.h>
#include <app/gwymoduleutils.h>

typedef struct {
    gint curve;
    gdouble from;
    gdouble to;
    GwyGraph *parent_graph;
    GwyGraphModel *graph_model;
    GwySIValueFormat *abscissa_vf;
    gboolean all;
} CutArgs;

typedef struct {
    CutArgs *args;
    GtkWidget *dialog;
    GtkWidget *graph;
    GtkWidget *from;
    GtkWidget *to;
    GtkWidget *curve;
    GtkWidget *all;
} CutControls;

static gboolean module_register     (void);
static void     cut                 (GwyGraph *graph);
static void     cut_dialog          (CutArgs *args);
static void     cut_fetch_entry     (CutControls *controls);
static void     curve_changed       (GtkComboBox *combo,
                                     CutControls *controls);
static void     range_changed       (GtkWidget *entry,
                                     CutControls *controls);
static void     cut_limit_selection (CutControls *controls,
                                     gboolean curve_switch);
static void     cut_get_full_x_range(CutControls *controls,
                                     gdouble *xmin,
                                     gdouble *xmax);
static void     do_cut              (CutArgs *args);
static void     graph_selected      (GwySelection* selection,
                                     gint i,
                                     CutControls *controls);
static void     all_changed         (GtkToggleButton *check,
                                     CutControls *controls);
static void     pick_curves         (CutControls *controls);
static void     update_sensitivity  (CutControls *controls);
static void     load_args           (GwyContainer *container,
                                     CutArgs *args);
static void     save_args           (GwyContainer *container,
                                     CutArgs *args);

static GwyModuleInfo module_info = {
    GWY_MODULE_ABI_VERSION,
    &module_register,
    N_("Cut graph"),
    "Petr Klapetek <klapetek@gwyddion.net>",
    "1.4",
    "David Nečas (Yeti) & Petr Klapetek",
    "2007",
};

GWY_MODULE_QUERY2(module_info, graph_cut)

static gboolean
module_register(void)
{
    gwy_graph_func_register("graph_cut",
                            (GwyGraphFunc)&cut,
                            N_("/_Basic Operations/_Cut..."),
                            GWY_STOCK_GRAPH_CUT,
                            GWY_MENU_FLAG_GRAPH_CURVE,
                            N_("Extract part of graph into new one"));

    return TRUE;
}

static void
cut(GwyGraph *graph)
{
    CutArgs args;

    gwy_clear(&args, 1);
    load_args(gwy_app_settings_get(), &args);
    args.parent_graph = graph;
    cut_dialog(&args);
    save_args(gwy_app_settings_get(), &args);
}

static void
cut_dialog(CutArgs *args)
{
    GtkWidget *label, *dialog, *hbox, *hbox2, *table;
    GwyGraphModel *gmodel;
    GwyGraphArea *area;
    GwySelection *selection;
    GwySIUnit *siunit;
    CutControls controls;
    gint response, row;
    gdouble xmin, xmax;

    controls.args = args;

    gmodel = gwy_graph_get_model(GWY_GRAPH(args->parent_graph));
    gwy_graph_model_get_x_range(gmodel, &xmin, &xmax);
    g_object_get(gmodel, "si-unit-x", &siunit, NULL);
    args->abscissa_vf
        = gwy_si_unit_get_format_with_digits(siunit,
                                             GWY_SI_UNIT_FORMAT_VFMARKUP,
                                             MAX(fabs(xmin), fabs(xmax)), 4,
                                             NULL);
    g_object_unref(siunit);

    dialog = gtk_dialog_new_with_buttons(_("Cut Graph"), NULL, 0, NULL);
    controls.dialog = dialog;
    gtk_dialog_add_button(GTK_DIALOG(dialog),
                          GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL);
    gtk_dialog_add_button(GTK_DIALOG(dialog),
                          GTK_STOCK_OK, GTK_RESPONSE_OK);
    gwy_help_add_to_graph_dialog(GTK_DIALOG(dialog), GWY_HELP_DEFAULT);
    gtk_dialog_set_default_response(GTK_DIALOG(dialog), GTK_RESPONSE_OK);

    hbox = gtk_hbox_new(FALSE, 2);
    gtk_box_pack_start(GTK_BOX(GTK_DIALOG(dialog)->vbox), hbox, TRUE, TRUE, 0);

    table = gtk_table_new(7, 2, FALSE);
    gtk_table_set_row_spacings(GTK_TABLE(table), 2);
    gtk_table_set_col_spacings(GTK_TABLE(table), 6);
    gtk_box_pack_start(GTK_BOX(hbox), table, FALSE, FALSE, 0);
    gtk_container_set_border_width(GTK_CONTAINER(table), 4);
    row = 0;

    /* Curve to cut */
    controls.curve = gwy_combo_box_graph_curve_new(G_CALLBACK(curve_changed),
                                                   &controls,
                                                   gmodel, args->curve);
    gwy_table_attach_adjbar(table, row, _("_Graph curve:"), NULL,
                            GTK_OBJECT(controls.curve),
                            GWY_HSCALE_WIDGET_NO_EXPAND);
    row++;

    controls.all = gtk_check_button_new_with_mnemonic(_("Cut _all curves"));
    gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(controls.all),
                                                   args->all);
    gtk_table_attach(GTK_TABLE(table), controls.all,
                     0, 2, row, row+1, GTK_FILL, 0, 0, 0);
    g_signal_connect(controls.all, "toggled",
                     G_CALLBACK(all_changed), &controls);
    row++;

    /* Cut area */
    gtk_table_set_row_spacing(GTK_TABLE(table), row-1, 8);
    hbox2 = gtk_hbox_new(FALSE, 6);
    gtk_table_attach(GTK_TABLE(table), hbox2,
                     0, 2, row, row+1, GTK_FILL, 0, 0, 0);
    row++;

    label = gtk_label_new(_("Range:"));
    gtk_box_pack_start(GTK_BOX(hbox2), label, FALSE, FALSE, 0);

    controls.from = gtk_entry_new();
    g_object_set_data(G_OBJECT(controls.from), "id", (gpointer)"from");
    gtk_entry_set_width_chars(GTK_ENTRY(controls.from), 8);
    gtk_box_pack_start(GTK_BOX(hbox2), controls.from, FALSE, FALSE, 0);
    g_signal_connect(controls.from, "activate",
                     G_CALLBACK(range_changed), &controls);
    gwy_widget_set_activate_on_unfocus(controls.from, TRUE);

    label = gtk_label_new(gwy_sgettext("range|to"));
    gtk_box_pack_start(GTK_BOX(hbox2), label, FALSE, FALSE, 0);

    controls.to = gtk_entry_new();
    g_object_set_data(G_OBJECT(controls.to), "id", (gpointer)"to");
    gtk_entry_set_width_chars(GTK_ENTRY(controls.to), 8);
    gtk_box_pack_start(GTK_BOX(hbox2), controls.to, FALSE, FALSE, 0);
    g_signal_connect(controls.to, "activate",
                     G_CALLBACK(range_changed), &controls);
    gwy_widget_set_activate_on_unfocus(controls.to, TRUE);

    label = gtk_label_new(NULL);
    gtk_label_set_markup(GTK_LABEL(label), args->abscissa_vf->units);
    gtk_box_pack_start(GTK_BOX(hbox2), label, FALSE, FALSE, 0);

    /* Graph */
    args->graph_model = gwy_graph_model_new_alike(gmodel);
    controls.graph = gwy_graph_new(args->graph_model);
    g_object_unref(args->graph_model);
    gtk_widget_set_size_request(controls.graph, 400, 300);

    gwy_graph_enable_user_input(GWY_GRAPH(controls.graph), FALSE);
    gtk_box_pack_start(GTK_BOX(hbox), controls.graph, TRUE, TRUE, 0);
    gwy_graph_set_status(GWY_GRAPH(controls.graph), GWY_GRAPH_STATUS_XSEL);

    area = GWY_GRAPH_AREA(gwy_graph_get_area(GWY_GRAPH(controls.graph)));
    selection = gwy_graph_area_get_selection(area, GWY_GRAPH_STATUS_XSEL);
    gwy_selection_set_max_objects(selection, 1);
    g_signal_connect(selection, "changed",
                     G_CALLBACK(graph_selected), &controls);

    gwy_graph_model_add_curve(controls.args->graph_model,
                              gwy_graph_model_get_curve(gmodel, args->curve));
    graph_selected(selection, -1, &controls);

    gtk_widget_show_all(dialog);
    pick_curves(&controls);
    update_sensitivity(&controls);

    response = gtk_dialog_run(GTK_DIALOG(dialog));
    if (response == GTK_RESPONSE_OK)
        cut_fetch_entry(&controls);
    gtk_widget_destroy(dialog);
    if (response == GTK_RESPONSE_OK)
        do_cut(args);
}

static void
do_cut(CutArgs *args)
{
    gint i, j, k, ndata, nndata, cstart, cend;
    GwyContainer *data;
    GwyGraphModel *ngmodel, *gmodel;
    GwyGraphCurveModel *gcmodel, *ngcmodel;
    const gdouble *xdata, *ydata;
    gdouble *nxdata, *nydata;

    gmodel = gwy_graph_get_model(args->parent_graph);
    ngmodel = gwy_graph_model_new_alike(gmodel);

    if (args->all) {
        cstart = 0;
        cend = gwy_graph_model_get_n_curves(gmodel);
    }
    else {
        cstart = args->curve;
        cend = args->curve + 1;
    }

    for (k = cstart; k < cend; k++) {
        gcmodel = gwy_graph_model_get_curve(gmodel, k);
        ngcmodel = gwy_graph_curve_model_duplicate(gcmodel);

        xdata = gwy_graph_curve_model_get_xdata(gcmodel);
        ydata = gwy_graph_curve_model_get_ydata(gcmodel);
        ndata = gwy_graph_curve_model_get_ndata(gcmodel);

        /*TODO this should really work differently*/
        nndata = 0;
        for (i = 0; i < ndata; i++) {
            if (xdata[i] >= args->from && xdata[i] < args->to)
                nndata++;
        }

        if (nndata == 0) {
            g_object_unref(ngcmodel);
            continue;
        }
        nxdata = g_new(gdouble, nndata);
        nydata = g_new(gdouble, nndata);

        j = 0;
        for (i = 0; i < ndata; i++) {
            if (xdata[i]>=args->from && xdata[i]<args->to) {
                nxdata[j] = xdata[i];
                nydata[j] = ydata[i];
                j++;
            }
        }
        gwy_graph_curve_model_set_data(ngcmodel, nxdata, nydata, nndata);
        g_free(nxdata);
        g_free(nydata);

        gwy_graph_model_add_curve(ngmodel, ngcmodel);
        g_object_unref(ngcmodel);
    }

    gwy_app_data_browser_get_current(GWY_APP_CONTAINER, &data, NULL);
    gwy_app_data_browser_add_graph_model(ngmodel, data, TRUE);

    g_object_unref(ngmodel);
}

static void
cut_fetch_entry(CutControls *controls)
{
    GtkWidget *entry;

    entry = gtk_window_get_focus(GTK_WINDOW(controls->dialog));
    if (entry
        && GTK_IS_ENTRY(entry)
        && g_object_get_data(G_OBJECT(entry), "id"))
        gtk_widget_activate(entry);
}

static void
pick_curves(CutControls *controls)
{
    GwyGraphModel *parent_gmodel, *graph_model;
    GwyGraphCurveModel *gcmodel;
    gint i, n;

    graph_model = controls->args->graph_model;
    parent_gmodel = gwy_graph_get_model(controls->args->parent_graph);
    gwy_graph_model_remove_all_curves(graph_model);

    if (!controls->args->all) {
        gcmodel = gwy_graph_model_get_curve(parent_gmodel,
                                            controls->args->curve);
        gwy_graph_model_add_curve(graph_model, gcmodel);
    }
    else {
        n = gwy_graph_model_get_n_curves(parent_gmodel);
        for (i = 0; i < n; i++) {
            gcmodel = gwy_graph_model_get_curve(parent_gmodel, i);
            gwy_graph_model_add_curve(graph_model, gcmodel);
        }
    }
    cut_limit_selection(controls, TRUE);
}

static void
curve_changed(GtkComboBox *combo, CutControls *controls)
{
    controls->args->curve = gwy_enum_combo_box_get_active(combo);
    pick_curves(controls);
}

static void
graph_selected(GwySelection* selection,
               gint i,
               CutControls *controls)
{
    CutArgs *args;
    gchar buffer[24];
    gdouble range[2];
    gint nselections;
    gdouble power10;

    g_return_if_fail(i <= 0);

    args = controls->args;
    nselections = gwy_selection_get_data(selection, NULL);
    gwy_selection_get_object(selection, 0, range);

    if (nselections <= 0 || range[0] == range[1])
        cut_get_full_x_range(controls, &args->from, &args->to);
    else {
        args->from = MIN(range[0], range[1]);
        args->to = MAX(range[0], range[1]);
    }
    power10 = pow10(args->abscissa_vf->precision);
    g_snprintf(buffer, sizeof(buffer), "%.*f",
               args->abscissa_vf->precision,
               floor(args->from*power10/args->abscissa_vf->magnitude)/power10);
    gtk_entry_set_text(GTK_ENTRY(controls->from), buffer);
    g_snprintf(buffer, sizeof(buffer), "%.*f",
               args->abscissa_vf->precision,
               ceil(args->to*power10/args->abscissa_vf->magnitude)/power10);
    gtk_entry_set_text(GTK_ENTRY(controls->to), buffer);

}

static void
range_changed(GtkWidget *entry,
              CutControls *controls)
{
    const gchar *id;
    gdouble *x, newval;

    id = g_object_get_data(G_OBJECT(entry), "id");
    if (gwy_strequal(id, "from"))
        x = &controls->args->from;
    else
        x = &controls->args->to;

    newval = atof(gtk_entry_get_text(GTK_ENTRY(entry)));
    newval *= controls->args->abscissa_vf->magnitude;
    if (newval == *x)
        return;
    *x = newval;
    cut_limit_selection(controls, FALSE);
}

static void
cut_limit_selection(CutControls *controls,
                    gboolean curve_switch)
{
    GwySelection *selection;
    GwyGraphArea *area;
    gdouble xmin, xmax;

    area = GWY_GRAPH_AREA(gwy_graph_get_area(GWY_GRAPH(controls->graph)));
    selection = gwy_graph_area_get_selection(area, GWY_GRAPH_STATUS_XSEL);

    if (curve_switch && !gwy_selection_get_data(selection, NULL)) {
        graph_selected(selection, -1, controls);
        return;
    }

    cut_get_full_x_range(controls, &xmin, &xmax);
    controls->args->from = CLAMP(controls->args->from, xmin, xmax);
    controls->args->to = CLAMP(controls->args->to, xmin, xmax);

    if (controls->args->from == xmin && controls->args->to == xmax)
        gwy_selection_clear(selection);
    else {
        gdouble range[2];

        range[0] = controls->args->from;
        range[1] = controls->args->to;
        gwy_selection_set_object(selection, 0, range);
    }
}

static void
cut_get_full_x_range(CutControls *controls,
                     gdouble *xmin,
                     gdouble *xmax)
{
    GwyGraphModel *gmodel;
    GwyGraphCurveModel *gcmodel;

    gmodel = gwy_graph_get_model(GWY_GRAPH(controls->graph));
    gcmodel = gwy_graph_model_get_curve(gmodel, 0);
    gwy_graph_curve_model_get_x_range(gcmodel, xmin, xmax);
}

static void
all_changed(GtkToggleButton *check,
            CutControls *controls)
{
    controls->args->all = gtk_toggle_button_get_active(check);
    pick_curves(controls);
    update_sensitivity(controls);
}

static void
update_sensitivity(CutControls *controls)
{
    gboolean csens = !controls->args->all;

    gwy_table_hscale_set_sensitive(GTK_OBJECT(controls->curve), csens);
}

static const gchar all_key[] = "/module/graph_cut/all";

static void
sanitize_args(CutArgs *args)
{
    args->all = !!args->all;
}

static void
load_args(GwyContainer *container, CutArgs *args)
{
    args->all = FALSE;

    gwy_container_gis_boolean_by_name(container, all_key, &args->all);
    sanitize_args(args);
}

static void
save_args(GwyContainer *container, CutArgs *args)
{
    gwy_container_set_boolean_by_name(container, all_key, args->all);
}

/* 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 : */