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
|
/*
* $Id: angle_dist.c 24549 2021-11-26 17:00:39Z yeti-dn $
* Copyright (C) 2004-2021 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 <gtk/gtk.h>
#include <libgwyddion/gwymacros.h>
#include <libgwyddion/gwymath.h>
#include <libgwymodule/gwymodule-process.h>
#include <libprocess/level.h>
#include <libprocess/filters.h>
#include <libgwydgets/gwystock.h>
#include <app/gwyapp.h>
#define RUN_MODES (GWY_RUN_IMMEDIATE | GWY_RUN_INTERACTIVE)
enum {
PARAM_SIZE,
PARAM_STEPS,
PARAM_LOGSCALE,
PARAM_FIT_PLANE,
PARAM_KERNEL_SIZE,
};
typedef struct {
GwyParams *params;
GwyDataField *field;
GwyDataField *result;
} ModuleArgs;
typedef struct {
ModuleArgs *args;
GtkWidget *dialog;
GwyParamTable *table;
} ModuleGUI;
static gboolean module_register (void);
static GwyParamDef* define_module_params(void);
static void angle_dist (GwyContainer *data,
GwyRunType runtype);
static gboolean execute (ModuleArgs *args,
GtkWindow *wait_window);
static GwyDialogOutcome run_gui (ModuleArgs *args);
static void param_changed (ModuleGUI *gui,
gint id);
static void compute_slopes (GwyDataField *field,
gint kernel_size,
GwyDataField *xder,
GwyDataField *yder);
static gdouble compute_max_der2 (GwyDataField *xder,
GwyDataField *yder);
static gboolean count_angles (GwyDataField *xderfield,
GwyDataField *yderfield,
gint size,
gulong *count,
gint steps);
static GwyDataField* make_datafield (GwyDataField *old,
gint res,
gulong *count,
gdouble real,
gboolean logscale);
static GwyModuleInfo module_info = {
GWY_MODULE_ABI_VERSION,
&module_register,
N_("Calculates two-dimensional distribution of angles, that is projections of slopes to all directions."),
"Yeti <yeti@gwyddion.net>",
"2.0",
"David Nečas (Yeti) & Petr Klapetek",
"2004",
};
GWY_MODULE_QUERY2(module_info, angle_dist)
static gboolean
module_register(void)
{
gwy_process_func_register("angle_dist",
(GwyProcessFunc)&angle_dist,
N_("/_Statistics/An_gle Distribution..."),
GWY_STOCK_DISTRIBUTION_ANGLE,
RUN_MODES,
GWY_MENU_FLAG_DATA,
N_("Calculate two-dimensional angle distribution"));
return TRUE;
}
static GwyParamDef*
define_module_params(void)
{
static GwyParamDef *paramdef = NULL;
if (paramdef)
return paramdef;
paramdef = gwy_param_def_new();
gwy_param_def_set_function_name(paramdef, gwy_process_func_current());
gwy_param_def_add_int(paramdef, PARAM_SIZE, "size", _("Output size"), 1, 1024, 200);
gwy_param_def_add_int(paramdef, PARAM_STEPS, "steps", _("Number of steps"), 1, 65536, 360);
gwy_param_def_add_boolean(paramdef, PARAM_LOGSCALE, "logscale", _("_Logarithmic value scale"), FALSE);
gwy_param_def_add_boolean(paramdef, PARAM_FIT_PLANE, "fit_plane", _("Use local plane _fitting"), FALSE);
gwy_param_def_add_int(paramdef, PARAM_KERNEL_SIZE, "kernel_size", _("Plane size"), 2, 16, 5);
return paramdef;
}
static void
angle_dist(GwyContainer *data, GwyRunType runtype)
{
ModuleArgs args;
gint oldid, newid;
GwyDialogOutcome outcome = GWY_DIALOG_PROCEED;
g_return_if_fail(runtype & RUN_MODES);
gwy_clear(&args, 1);
gwy_app_data_browser_get_current(GWY_APP_DATA_FIELD, &args.field,
GWY_APP_DATA_FIELD_ID, &oldid,
NULL);
g_return_if_fail(args.field);
args.params = gwy_params_new_from_settings(define_module_params());
if (runtype == GWY_RUN_INTERACTIVE) {
outcome = run_gui(&args);
gwy_params_save_to_settings(args.params);
if (outcome == GWY_DIALOG_CANCEL)
goto end;
}
if (outcome != GWY_DIALOG_HAVE_RESULT) {
if (!execute(&args, gwy_app_find_window_for_channel(data, oldid)))
goto end;
}
newid = gwy_app_data_browser_add_data_field(args.result, data, TRUE);
gwy_app_sync_data_items(data, data, oldid, newid, FALSE,
GWY_DATA_ITEM_PALETTE,
0);
gwy_app_set_data_field_title(data, newid, _("Angle distribution"));
gwy_app_channel_log_add_proc(data, oldid, newid);
g_object_unref(args.result);
end:
g_object_unref(args.params);
}
static GwyDialogOutcome
run_gui(ModuleArgs *args)
{
ModuleGUI gui;
GwyDialog *dialog;
GwyParamTable *table;
gui.args = args;
gui.dialog = gwy_dialog_new(_("Angle Distribution"));
dialog = GWY_DIALOG(gui.dialog);
gwy_dialog_add_buttons(dialog, GWY_RESPONSE_RESET, GTK_RESPONSE_CANCEL, GTK_RESPONSE_OK, 0);
table = gui.table = gwy_param_table_new(args->params);
gwy_param_table_append_slider(table, PARAM_SIZE);
gwy_param_table_append_slider(table, PARAM_STEPS);
gwy_param_table_append_checkbox(table, PARAM_LOGSCALE);
gwy_param_table_append_separator(table);
gwy_param_table_append_checkbox(table, PARAM_FIT_PLANE);
gwy_param_table_append_slider(table, PARAM_KERNEL_SIZE);
gwy_dialog_add_param_table(dialog, table);
gwy_dialog_add_content(dialog, gwy_param_table_widget(table), FALSE, FALSE, 0);
g_signal_connect_swapped(table, "param-changed", G_CALLBACK(param_changed), &gui);
return gwy_dialog_run(dialog);
}
static void
param_changed(ModuleGUI *gui, gint id)
{
GwyParams *params = gui->args->params;
if (id < 0 || id == PARAM_FIT_PLANE)
gwy_param_table_set_sensitive(gui->table, PARAM_KERNEL_SIZE, gwy_params_get_boolean(params, PARAM_FIT_PLANE));
}
static gboolean
execute(ModuleArgs *args, GtkWindow *wait_window)
{
GwyParams *params = args->params;
gint size = gwy_params_get_int(params, PARAM_SIZE);
gint nsteps = gwy_params_get_int(params, PARAM_STEPS);
gint kernel_size = gwy_params_get_int(params, PARAM_KERNEL_SIZE);
gboolean fit_plane = gwy_params_get_boolean(params, PARAM_FIT_PLANE);
gboolean logscale = gwy_params_get_boolean(params, PARAM_LOGSCALE);
GwyDataField *field = args->field, *xder, *yder;
gint xres, yres, n;
gulong *count;
gwy_app_wait_start(wait_window, _("Computing angle distribution..."));
xres = gwy_data_field_get_xres(field);
yres = gwy_data_field_get_yres(field);
n = fit_plane ? kernel_size : 2;
n = (xres - n)*(yres - n);
xder = gwy_data_field_new_alike(field, FALSE);
yder = gwy_data_field_new_alike(field, FALSE);
compute_slopes(field, fit_plane ? kernel_size : 0, xder, yder);
count = g_new0(gulong, size*size);
if (count_angles(xder, yder, size, count, nsteps))
args->result = make_datafield(field, size, count, 2.0*G_PI, logscale);
g_free(count);
g_object_unref(yder);
g_object_unref(xder);
gwy_app_wait_finish();
return !!args->result;
}
static gdouble
compute_max_der2(GwyDataField *xder, GwyDataField *yder)
{
gint i, n = gwy_data_field_get_xres(xder)*gwy_data_field_get_yres(xder);
const gdouble *xd = gwy_data_field_get_data_const(xder);
const gdouble *yd = gwy_data_field_get_data_const(yder);
gdouble m = 0.0;
for (i = 0; i < n; i++)
m = fmax(m, xd[i]*xd[i] + yd[i]*yd[i]);
return m;
}
static void
compute_slopes(GwyDataField *field,
gint kernel_size,
GwyDataField *xder,
GwyDataField *yder)
{
GwyPlaneFitQuantity quantites[2] = { GWY_PLANE_FIT_BX, GWY_PLANE_FIT_BY };
GwyDataField *fields[2] = { xder, yder };
if (!kernel_size) {
gwy_data_field_filter_slope(field, xder, yder);
return;
}
gwy_data_field_fit_local_planes(field, kernel_size, 2, quantites, fields);
gwy_data_field_multiply(xder, 1.0/gwy_data_field_get_dx(field));
gwy_data_field_multiply(yder, 1.0/gwy_data_field_get_dy(field));
}
static gboolean
count_angles(GwyDataField *xderfield, GwyDataField *yderfield,
gint size, gulong *count,
gint steps)
{
const gdouble *xder, *yder;
gint xider, yider, i, j, n;
gdouble *ct, *st;
gdouble d, phi, max;
gboolean ok = TRUE;
max = compute_max_der2(xderfield, yderfield);
max = atan(sqrt(max));
gwy_debug("max = %g", max);
ct = g_new(gdouble, steps);
st = g_new(gdouble, steps);
for (j = 0; j < steps; j++) {
gdouble theta = 2.0*G_PI*j/steps;
ct[j] = cos(theta);
st[j] = sin(theta);
}
xder = gwy_data_field_get_data_const(xderfield);
yder = gwy_data_field_get_data_const(yderfield);
n = gwy_data_field_get_xres(xderfield)*gwy_data_field_get_yres(xderfield);
for (i = 0; i < n; i++) {
gdouble xd = xder[i], yd = yder[i];
d = atan(hypot(xd, yd));
phi = atan2(yd, xd);
for (j = 0; j < steps; j++) {
gdouble v = d*cos(2.0*G_PI*j/steps - phi);
xider = size*(v*ct[j]/(2.0*max) + 0.5);
xider = CLAMP(xider, 0, size-1);
yider = size*(v*st[j]/(2.0*max) + 0.5);
yider = CLAMP(yider, 0, size-1);
count[yider*size + xider]++;
}
if (!gwy_app_wait_set_fraction((gdouble)i/n)) {
ok = FALSE;
break;
}
}
g_free(ct);
g_free(st);
return ok;
}
static GwyDataField*
make_datafield(G_GNUC_UNUSED GwyDataField *old,
gint res, gulong *count,
gdouble real, gboolean logscale)
{
GwyDataField *field;
GwySIUnit *unit;
gdouble *d;
gint i;
field = gwy_data_field_new(res, res, real, real, FALSE);
gwy_data_field_set_xoffset(field, -gwy_data_field_get_xreal(field)/2);
gwy_data_field_set_yoffset(field, -gwy_data_field_get_yreal(field)/2);
unit = gwy_si_unit_new(NULL);
gwy_data_field_set_si_unit_z(field, unit);
g_object_unref(unit);
unit = gwy_si_unit_new(NULL);
gwy_data_field_set_si_unit_xy(field, unit);
g_object_unref(unit);
d = gwy_data_field_get_data(field);
if (logscale) {
for (i = 0; i < res*res; i++)
d[i] = count[i] ? log((gdouble)count[i]) + 1.0 : 0.0;
}
else {
for (i = 0; i < res*res; i++)
d[i] = count[i];
}
return field;
}
/* 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 : */
|