static void gwy_data_field_facet_distribution(GwyDataField *dfield, gint kernel_size, GwyContainer *container) { GwyDataField *dtheta, *dphi, *dist; GwySIUnit *siunit; gdouble *xd, *yd, *data; const gdouble *xdc, *ydc; gdouble q, max; gint res, hres, i, j, mi, mj, xres, yres; if (gwy_container_gis_object_by_name(container, "/theta", &dtheta)) g_object_ref(dtheta); else dtheta = gwy_data_field_new_alike(dfield, FALSE); if (gwy_container_gis_object_by_name(container, "/phi", &dphi)) g_object_ref(dphi); else dphi = gwy_data_field_new_alike(dfield, FALSE); compute_slopes(dfield, kernel_size, dtheta, dphi); xres = gwy_data_field_get_xres(dfield); yres = gwy_data_field_get_yres(dfield); xd = gwy_data_field_get_data(dtheta); yd = gwy_data_field_get_data(dphi); for (i = xres*yres; i; i--, xd++, yd++) { gdouble theta, phi; slopes_to_angles(*xd, *yd, &theta, &phi); *xd = theta; *yd = phi; } q = gwy_data_field_get_max(dtheta); q = MIN(q*1.05, G_PI/2.0); q = G_SQRT2/(2.0*sin(q/2.0)); if (gwy_container_gis_object_by_name(container, "/0/data", &dist)) { g_object_ref(dist); gwy_data_field_clear(dist); gwy_data_field_set_xreal(dist, 2.0*G_SQRT2/q); gwy_data_field_set_yreal(dist, 2.0*G_SQRT2/q); } else { dist = gwy_data_field_new(FDATA_RES, FDATA_RES, 2.0*G_SQRT2/q, 2.0*G_SQRT2/q, TRUE); siunit = gwy_si_unit_new(""); gwy_data_field_set_si_unit_z(dist, siunit); g_object_unref(siunit); /* FIXME */ siunit = gwy_si_unit_new(""); gwy_data_field_set_si_unit_xy(dist, siunit); g_object_unref(siunit); } res = FDATA_RES; hres = (res - 1)/2; data = gwy_data_field_get_data(dist); xdc = gwy_data_field_get_data_const(dtheta); ydc = gwy_data_field_get_data_const(dphi); for (i = xres*yres; i; i--, xdc++, ydc++) { gdouble x, y; gint xx, yy; angles_to_xy(*xdc, *ydc, &x, &y); xx = GWY_ROUND(q*x/G_SQRT2*hres) + hres; yy = GWY_ROUND(q*y/G_SQRT2*hres) + hres; data[yy*res + xx] += 1.0; } /* Find maxima */ mi = mj = hres; max = 0; for (i = 1; i+1 < res; i++) { for (j = 1; j+1 < res; j++) { gdouble z; z = data[i*res + j] + 0.3*(data[i*res + j - 1] + data[i*res + j + 1] + data[i*res - res + j] + data[i*res + res + j]) + 0.1*(data[i*res - res + j - 1] + data[i*res - res + j + 1] + data[i*res + res + j - 1] + data[i*res + res + j + 1]); if (G_UNLIKELY(z > max)) { max = z; mi = i; mj = j; } } } for (i = res*res; i; i--, data++) *data = pow(*data, 0.35); gwy_container_set_double_by_name(container, "/q", q); { gdouble x, y, theta, phi; x = (mj - hres)*G_SQRT2/(q*hres); y = (mi - hres)*G_SQRT2/(q*hres); xy_to_angles(x, y, &theta, &phi); gwy_container_set_double_by_name(container, "/theta0", theta); gwy_container_set_double_by_name(container, "/phi0", phi); } gwy_container_set_object_by_name(container, "/0/data", dist); g_object_unref(dist); gwy_container_set_object_by_name(container, "/theta", dtheta); g_object_unref(dtheta); gwy_container_set_object_by_name(container, "/phi", dphi); g_object_unref(dphi); gwy_container_set_string_by_name(container, "/0/base/palette", g_strdup(FVIEW_GRADIENT)); gwy_data_field_data_changed(dist); }
static void preview(EntropyControls *controls) { EntropyArgs *args = controls->args; GwyDataField *dfield = controls->dfield; GwyDataField *mfield = controls->mfield; GwyGraphModel *gmodel; GwyGraphCurveModel *gcmodel; GwyDataLine *ecurve; gchar buf[24]; gdouble S, s, Smax = 0.0; ecurve = gwy_data_line_new(1, 1.0, FALSE); if (args->mode == ENTROPY_VALUES) { S = gwy_data_field_area_get_entropy_at_scales(dfield, ecurve, mfield, args->masking, 0, 0, dfield->xres, dfield->yres, 0); s = gwy_data_field_area_get_rms_mask(dfield, mfield, args->masking, 0, 0, dfield->xres, dfield->yres); Smax = ENTROPY_NORMAL + log(s); } else { GwyDataField *xder = gwy_data_field_new_alike(dfield, FALSE); GwyDataField *yder = gwy_data_field_new_alike(dfield, FALSE); compute_slopes(controls->dfield, args->fit_plane ? args->kernel_size : 0, xder, yder); xder = fake_mask(xder, mfield, args->masking); yder = fake_mask(yder, mfield, args->masking); if (args->mode == ENTROPY_ANGLES) transform_to_sphere(xder, yder); S = gwy_data_field_get_entropy_2d_at_scales(xder, yder, ecurve, 0); if (args->mode == ENTROPY_SLOPES) { s = calculate_sigma2_2d(xder, yder); Smax = ENTROPY_NORMAL_2D + log(s); } g_object_unref(xder); g_object_unref(yder); } g_snprintf(buf, sizeof(buf), "%g", S); gtk_label_set_text(GTK_LABEL(controls->entropy), buf); if (args->mode != ENTROPY_ANGLES) { g_snprintf(buf, sizeof(buf), "%g", Smax - S); gtk_label_set_text(GTK_LABEL(controls->entropydef), buf); } else gtk_label_set_text(GTK_LABEL(controls->entropydef), _("N.A.")); gmodel = gwy_graph_get_model(GWY_GRAPH(controls->graph)); gwy_graph_model_remove_all_curves(gmodel); g_object_set(gmodel, "axis-label-bottom", "log h", "axis-label-left", "S", "label-position", GWY_GRAPH_LABEL_NORTHWEST, NULL); if (gwy_data_line_get_min(ecurve) > -0.5*G_MAXDOUBLE) { gcmodel = gwy_graph_curve_model_new(); g_object_set(gcmodel, "description", _("Entropy at scales"), "mode", GWY_GRAPH_CURVE_LINE_POINTS, "color", gwy_graph_get_preset_color(0), NULL); gwy_graph_curve_model_set_data_from_dataline(gcmodel, ecurve, 0, 0); gwy_graph_model_add_curve(gmodel, gcmodel); g_object_unref(gcmodel); } if (S > -0.5*G_MAXDOUBLE) { GwyDataLine *best = gwy_data_line_duplicate(ecurve); gdouble *ydata = gwy_data_line_get_data(best); guint i, res = gwy_data_line_get_res(best); for (i = 0; i < res; i++) ydata[i] = S; gcmodel = gwy_graph_curve_model_new(); g_object_set(gcmodel, "description", _("Best estimate"), "mode", GWY_GRAPH_CURVE_LINE, "color", gwy_graph_get_preset_color(1), NULL); gwy_graph_curve_model_set_data_from_dataline(gcmodel, best, 0, 0); gwy_graph_model_add_curve(gmodel, gcmodel); g_object_unref(gcmodel); g_object_unref(best); } g_object_unref(ecurve); zoom_in_changed(controls, GTK_TOGGLE_BUTTON(controls->zoom_in)); }