File: grain_makesel.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 (269 lines) | stat: -rw-r--r-- 10,072 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
/*
 *  $Id: grain_makesel.c 24796 2022-04-28 15:20:59Z yeti-dn $
 *  Copyright (C) 2014-2021 David Necas (Yeti), Petr Klapetek, Sven Neumann.
 *  E-mail: yeti@gwyddion.net, klapetek@gwyddion.net, neumann@jpk.com.
 *
 *  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/gwymacros.h>
#include <libgwyddion/gwymath.h>
#include <libprocess/grains.h>
#include <libgwydgets/gwystock.h>
#include <libgwymodule/gwymodule-process.h>
#include <app/gwyapp.h>

#define RUN_MODES GWY_RUN_IMMEDIATE

static gboolean module_register       (void);
static void     grain_inscribe_discs  (GwyContainer *data,
                                       GwyRunType runtype);
static void     grain_exscribe_circles(GwyContainer *data,
                                       GwyRunType runtype);
static void     grain_inscribe_rects  (GwyContainer *data,
                                       GwyRunType runtype);
static void     grain_exscribe_bboxes (GwyContainer *data,
                                       GwyRunType runtype);

static GwyModuleInfo module_info = {
    GWY_MODULE_ABI_VERSION,
    &module_register,
    N_("Creates selections visualizing grains."),
    "Yeti <yeti@gwyddion.net>",
    "1.0",
    "David Nečas (Yeti)",
    "2015",
};

GWY_MODULE_QUERY2(module_info, grain_makesel)

static gboolean
module_register(void)
{
    gwy_process_func_register("grain_inscribe_discs",
                              (GwyProcessFunc)&grain_inscribe_discs,
                              N_("/_Grains/Select _Inscribed Discs"),
                              GWY_STOCK_GRAIN_INSCRIBED_CIRCLE,
                              RUN_MODES,
                              GWY_MENU_FLAG_DATA | GWY_MENU_FLAG_DATA_MASK,
                              N_("Create a selection visualizing discs inscribed into grains"));
    gwy_process_func_register("grain_exscribe_circles",
                              (GwyProcessFunc)&grain_exscribe_circles,
                              N_("/_Grains/Select _Circumscribed Circles"),
                              GWY_STOCK_GRAIN_EXSCRIBED_CIRCLE,
                              RUN_MODES,
                              GWY_MENU_FLAG_DATA | GWY_MENU_FLAG_DATA_MASK,
                              N_("Create a selection visualizing grain circumcircles"));
    gwy_process_func_register("grain_inscribe_rects",
                              (GwyProcessFunc)&grain_inscribe_rects,
                              N_("/_Grains/Select Inscribed _Rectangles"),
                              GWY_STOCK_GRAIN_INSCRIBED_BOX,
                              RUN_MODES,
                              GWY_MENU_FLAG_DATA | GWY_MENU_FLAG_DATA_MASK,
                              N_("Create a selection visualizing rectangles inscribed into grains"));
    gwy_process_func_register("grain_exscribe_bboxes",
                              (GwyProcessFunc)&grain_exscribe_bboxes,
                              N_("/_Grains/Select _Bounding Boxes"),
                              GWY_STOCK_GRAIN_BOUNDING_BOX,
                              RUN_MODES,
                              GWY_MENU_FLAG_DATA | GWY_MENU_FLAG_DATA_MASK,
                              N_("Create a selection visualizing grain bounding boxes"));

    return TRUE;
}

static GwySelection*
create_selection(const gchar *typename, guint *ngrains)
{
    GParamSpecInt *pspec;
    GObjectClass *klass;
    GType type;

    type = g_type_from_name(typename);
    g_return_val_if_fail(type, NULL);

    klass = g_type_class_ref(type);
    pspec = (GParamSpecInt*)g_object_class_find_property(klass, "max-objects");
    g_return_val_if_fail(G_IS_PARAM_SPEC_UINT(pspec), NULL);

    if ((gint)*ngrains > pspec->maximum) {
        g_warning("Too many grains for %s, only first %d will be shown.", typename, pspec->maximum);
        *ngrains = pspec->maximum;
    }
    return g_object_new(type, "max-objects", *ngrains, NULL);
}

static void
make_circles(GwyContainer *data, gint id, GwyDataField *field, gdouble **rxydata, guint ngrains)
{
    gdouble xoffset = gwy_data_field_get_xoffset(field), yoffset = gwy_data_field_get_yoffset(field);
    GwySelection *selection;
    gchar *key;
    guint i;

    selection = create_selection("GwySelectionEllipse", &ngrains);
    for (i = 1; i <= ngrains; i++) {
        gdouble r = rxydata[0][i], x = rxydata[1][i] - xoffset, y = rxydata[2][i] - yoffset;
        gdouble xy[4] = { x - r, y - r, x + r, y + r };
        gwy_selection_set_object(selection, i-1, xy);
    }

    key = g_strdup_printf("/%d/select/ellipse", id);
    gwy_container_set_object_by_name(data, key, selection);
    g_object_unref(selection);
    g_free(key);
}

/* FIXME: It would be nice to have something like that also for minimum and maximum bounding dimensions. */
static void
grain_inscribe_discs(GwyContainer *data, GwyRunType runtype)
{
    static const GwyGrainQuantity quantities[] = {
        GWY_GRAIN_VALUE_INSCRIBED_DISC_R, GWY_GRAIN_VALUE_INSCRIBED_DISC_X, GWY_GRAIN_VALUE_INSCRIBED_DISC_Y,
    };

    GwyDataField *field, *mfield;
    guint ngrains, i;
    gint *grains;
    gdouble *inscd;
    gdouble *values[3];
    gint id;

    g_return_if_fail(runtype & RUN_MODES);
    gwy_app_data_browser_get_current(GWY_APP_DATA_FIELD, &field,
                                     GWY_APP_MASK_FIELD, &mfield,
                                     GWY_APP_DATA_FIELD_ID, &id,
                                     0);

    grains = g_new0(gint, mfield->xres * mfield->yres);
    ngrains = gwy_data_field_number_grains(mfield, grains);
    inscd = g_new(gdouble, 3*(ngrains + 1));
    for (i = 0; i < 3; i++)
        values[i] = inscd + i*(ngrains + 1);

    gwy_data_field_grains_get_quantities(field, values, quantities, 3, ngrains, grains);
    g_free(grains);

    make_circles(data, id, field, values, ngrains);
    g_free(inscd);
}

static void
grain_exscribe_circles(GwyContainer *data, GwyRunType runtype)
{
    static const GwyGrainQuantity quantities[] = {
        GWY_GRAIN_VALUE_CIRCUMCIRCLE_R, GWY_GRAIN_VALUE_CIRCUMCIRCLE_X, GWY_GRAIN_VALUE_CIRCUMCIRCLE_Y,
    };

    GwyDataField *field, *mfield;
    guint ngrains, i;
    gint *grains;
    gdouble *circc;
    gdouble *values[3];
    gint id;

    g_return_if_fail(runtype & RUN_MODES);
    gwy_app_data_browser_get_current(GWY_APP_DATA_FIELD, &field,
                                     GWY_APP_MASK_FIELD, &mfield,
                                     GWY_APP_DATA_FIELD_ID, &id,
                                     0);

    grains = g_new0(gint, mfield->xres * mfield->yres);
    ngrains = gwy_data_field_number_grains(mfield, grains);
    circc = g_new(gdouble, 3*(ngrains + 1));
    for (i = 0; i < 3; i++)
        values[i] = circc + i*(ngrains + 1);

    gwy_data_field_grains_get_quantities(field, values, quantities, 3, ngrains, grains);
    g_free(grains);

    make_circles(data, id, field, values, ngrains);
    g_free(circc);
}

static void
make_boxes(GwyContainer *data, gint id, GwyDataField *field, const gint *boxes, guint ngrains)
{
    gdouble dx = gwy_data_field_get_dx(field), dy = gwy_data_field_get_dy(field);
    GwySelection *selection;
    gchar *key;
    guint i;

    selection = create_selection("GwySelectionRectangle", &ngrains);
    for (i = 1; i <= ngrains; i++) {
        const gint *bb = boxes + 4*i;
        gdouble x = dx*bb[0], y = dy*bb[1], w = dx*bb[2], h = dy*bb[3];
        gdouble xy[4] = { x, y, x + w, y + h };
        gwy_selection_set_object(selection, i-1, xy);
    }

    key = g_strdup_printf("/%d/select/rectangle", id);
    gwy_container_set_object_by_name(data, key, selection);
    g_object_unref(selection);
    g_free(key);
}

static void
grain_inscribe_rects(GwyContainer *data, GwyRunType runtype)
{
    GwyDataField *field, *mfield;
    guint ngrains;
    gint *grains, *iboxes;
    gint id;

    g_return_if_fail(runtype & RUN_MODES);
    gwy_app_data_browser_get_current(GWY_APP_DATA_FIELD, &field,
                                     GWY_APP_MASK_FIELD, &mfield,
                                     GWY_APP_DATA_FIELD_ID, &id,
                                     0);

    grains = g_new0(gint, mfield->xres * mfield->yres);
    ngrains = gwy_data_field_number_grains(mfield, grains);
    iboxes = g_new(gint, 4*(ngrains + 1));

    gwy_data_field_get_grain_inscribed_boxes(mfield, ngrains, grains, iboxes);
    g_free(grains);

    make_boxes(data, id, field, iboxes, ngrains);
    g_free(iboxes);
}

static void
grain_exscribe_bboxes(GwyContainer *data, GwyRunType runtype)
{
    GwyDataField *field, *mfield;
    guint ngrains;
    gint *grains, *bboxes;
    gint id;

    g_return_if_fail(runtype & RUN_MODES);
    gwy_app_data_browser_get_current(GWY_APP_DATA_FIELD, &field,
                                     GWY_APP_MASK_FIELD, &mfield,
                                     GWY_APP_DATA_FIELD_ID, &id,
                                     0);

    grains = g_new0(gint, mfield->xres * mfield->yres);
    ngrains = gwy_data_field_number_grains(mfield, grains);
    bboxes = g_new(gint, 4*(ngrains + 1));

    gwy_data_field_get_grain_bounding_boxes(mfield, ngrains, grains, bboxes);
    g_free(grains);

    make_boxes(data, id, field, bboxes, ngrains);
    g_free(bboxes);
}

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