| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
#include "config.h" |
| 19 |
#include <glib/gi18n-lib.h> |
| 20 |
#include <locale.h> |
| 21 |
#include <string.h> |
| 22 |
#ifdef HAVE_UNISTD_H |
| 23 |
#include <unistd.h> |
| 24 |
#endif |
| 25 |
#include <glib/gstdio.h> |
| 26 |
#include <gtk/gtk.h> |
| 27 |
#include <gwy.h> |
| 28 |
|
| 29 |
#define RUN_MODES (GWY_RUN_IMMEDIATE | GWY_RUN_INTERACTIVE) |
| 30 |
|
| 31 |
enum { |
| 32 |
PARAM_STYLE, |
| 33 |
PARAM_DECIMAL_DOT, |
| 34 |
PARAM_PRECISION, |
| 35 |
}; |
| 36 |
|
| 37 |
typedef enum { |
| 38 |
VOLUME_EXPORT_VTK = 0, |
| 39 |
VOLUME_EXPORT_ZLINES = 1, |
| 40 |
VOLUME_EXPORT_LAYERS = 2, |
| 41 |
VOLUME_EXPORT_MATRICES = 3, |
| 42 |
VOLUME_EXPORT_NTYPES, |
| 43 |
} VolumeExportStyle; |
| 44 |
|
| 45 |
typedef struct { |
| 46 |
GwyParams *params; |
| 47 |
gboolean need_decimal_dot_option; |
| 48 |
GwyBrick *brick; |
| 49 |
|
| 50 |
const gchar *title; |
| 51 |
} ModuleArgs; |
| 52 |
|
| 53 |
static gboolean module_register (void); |
| 54 |
static GwyParamDef* define_module_params(void); |
| 55 |
static void module_main (GwyFile *data, |
| 56 |
GwyRunModeFlags mode); |
| 57 |
static GwyDialogOutcome run_gui (ModuleArgs *args); |
| 58 |
static gchar* export_brick (gpointer user_data, |
| 59 |
gssize *data_len); |
| 60 |
static void destroy_brick (gchar *data, |
| 61 |
gpointer user_data); |
| 62 |
|
| 63 |
static GwyModuleInfo module_info = { |
| 64 |
GWY_MODULE_ABI_VERSION, |
| 65 |
&module_register, |
| 66 |
N_("Exports volume data in simple ASCII formats."), |
| 67 |
"Yeti <[email protected]>", |
| 68 |
"2.1", |
| 69 |
"David Nečas (Yeti)", |
| 70 |
"2018", |
| 71 |
}; |
| 72 |
|
| 73 |
GWY_MODULE_QUERY2(module_info, volume_asciiexport) |
| 74 |
|
| 75 |
static gboolean |
| 76 |
module_register(void) |
| 77 |
{ |
| 78 |
gwy_volume_func_register("volume_asciiexport", |
| 79 |
module_main, |
| 80 |
N_("/_Basic Operations/Export _Text..."), |
| 81 |
NULL, |
| 82 |
RUN_MODES, |
| 83 |
GWY_MENU_FLAG_VOLUME, |
| 84 |
N_("Export volume data to a text file")); |
| 85 |
|
| 86 |
return TRUE; |
| 87 |
} |
| 88 |
|
| 89 |
static GwyParamDef* |
| 90 |
define_module_params(void) |
| 91 |
{ |
| 92 |
static const GwyEnum styles[] = { |
| 93 |
{ N_("VTK structured grid"), VOLUME_EXPORT_VTK, }, |
| 94 |
{ N_("One Z-profile per line"), VOLUME_EXPORT_ZLINES, }, |
| 95 |
{ N_("One XY-layer per line"), VOLUME_EXPORT_LAYERS, }, |
| 96 |
{ N_("Blank-line separated matrices"), VOLUME_EXPORT_MATRICES, }, |
| 97 |
}; |
| 98 |
static GwyParamDef *paramdef = NULL; |
| 99 |
|
| 100 |
if (paramdef) |
| 101 |
return paramdef; |
| 102 |
|
| 103 |
paramdef = gwy_param_def_new(); |
| 104 |
gwy_param_def_set_function_name(paramdef, gwy_volume_func_current()); |
| 105 |
gwy_param_def_add_gwyenum(paramdef, PARAM_STYLE, "style", _("Style"), |
| 106 |
styles, G_N_ELEMENTS(styles), VOLUME_EXPORT_MATRICES); |
| 107 |
gwy_param_def_add_boolean(paramdef, PARAM_DECIMAL_DOT, "decimal-dot", _("Use _dot as decimal separator"), TRUE); |
| 108 |
gwy_param_def_add_int(paramdef, PARAM_PRECISION, "precision", _("_Precision"), 0, 16, 5); |
| 109 |
return paramdef; |
| 110 |
} |
| 111 |
|
| 112 |
static void |
| 113 |
module_main(GwyFile *data, GwyRunModeFlags mode) |
| 114 |
{ |
| 115 |
ModuleArgs args; |
| 116 |
GwyDialogOutcome outcome; |
| 117 |
gint id; |
| 118 |
|
| 119 |
g_return_if_fail(mode & RUN_MODES); |
| 120 |
|
| 121 |
gwy_clear1(args); |
| 122 |
gwy_data_browser_get_current(GWY_APP_BRICK, &args.brick, |
| 123 |
GWY_APP_BRICK_ID, &id, |
| 124 |
0); |
| 125 |
g_return_if_fail(GWY_IS_BRICK(args.brick)); |
| 126 |
|
| 127 |
args.params = gwy_params_new_from_settings(define_module_params()); |
| 128 |
if (mode == GWY_RUN_INTERACTIVE) { |
| 129 |
outcome = run_gui(&args); |
| 130 |
gwy_params_save_to_settings(args.params); |
| 131 |
if (outcome == GWY_DIALOG_CANCEL) |
| 132 |
goto finish; |
| 133 |
} |
| 134 |
|
| 135 |
if (!(args.title = gwy_file_get_title(data, GWY_FILE_VOLUME, id))) |
| 136 |
args.title = _("Volume data"); |
| 137 |
|
| 138 |
gwy_save_auxiliary_with_callback(_("Export to Text File"), NULL, export_brick, destroy_brick, FALSE, &args); |
| 139 |
|
| 140 |
finish: |
| 141 |
g_object_unref(args.params); |
| 142 |
} |
| 143 |
|
| 144 |
static GwyDialogOutcome |
| 145 |
run_gui(ModuleArgs *args) |
| 146 |
{ |
| 147 |
gboolean needs_decimal_dot_option; |
| 148 |
GwyDialog *dialog; |
| 149 |
GwyParamTable *table; |
| 150 |
|
| 151 |
needs_decimal_dot_option = !gwy_strequal(gwy_get_decimal_separator(), "."); |
| 152 |
|
| 153 |
dialog = GWY_DIALOG(gwy_dialog_new(_("Export Text"))); |
| 154 |
gwy_dialog_add_stock_buttons(dialog, GWY_STOCK_CANCEL, GWY_STOCK_OK, 0); |
| 155 |
|
| 156 |
table = gwy_param_table_new(args->params); |
| 157 |
gwy_param_table_append_header(table, -1, _("Options")); |
| 158 |
gwy_param_table_append_combo(table, PARAM_STYLE); |
| 159 |
if (needs_decimal_dot_option) |
| 160 |
gwy_param_table_append_checkbox(table, PARAM_DECIMAL_DOT); |
| 161 |
gwy_param_table_append_slider(table, PARAM_PRECISION); |
| 162 |
gwy_param_table_slider_set_mapping(table, PARAM_PRECISION, GWY_SCALE_MAPPING_LINEAR); |
| 163 |
gwy_dialog_add_content(dialog, gwy_param_table_widget(table), FALSE, FALSE, 0); |
| 164 |
|
| 165 |
return gwy_dialog_run(dialog); |
| 166 |
} |
| 167 |
|
| 168 |
static gchar* |
| 169 |
export_brick(gpointer user_data, gssize *data_len) |
| 170 |
{ |
| 171 |
static const gchar vtk_header[] = |
| 172 |
"# vtk DataFile Version 2.0\n" |
| 173 |
"%s\n" |
| 174 |
"ASCII\n" |
| 175 |
"DATASET STRUCTURED_POINTS\n" |
| 176 |
"DIMENSIONS %u %u %u\n" |
| 177 |
"ASPECT_RATIO 1 1 1\n" |
| 178 |
"SPACING %g %g %g\n" |
| 179 |
"ORIGIN 0 0 0\n" |
| 180 |
"POINT_DATA %u\n" |
| 181 |
"SCALARS volume_scalars double 1\n" |
| 182 |
"LOOKUP_TABLE default\n" |
| 183 |
; |
| 184 |
|
| 185 |
ModuleArgs *args = (ModuleArgs*)user_data; |
| 186 |
GwyParams *params = args->params; |
| 187 |
gint precision = gwy_params_get_int(params, PARAM_PRECISION); |
| 188 |
VolumeExportStyle style = gwy_params_get_enum(params, PARAM_STYLE); |
| 189 |
gboolean decimal_dot = gwy_params_get_boolean(params, PARAM_DECIMAL_DOT); |
| 190 |
GwyBrick *brick = args->brick; |
| 191 |
guint xres, yres, zres, i, j, n; |
| 192 |
GString *str; |
| 193 |
const gdouble *d, *dd; |
| 194 |
|
| 195 |
xres = gwy_brick_get_xres(brick); |
| 196 |
yres = gwy_brick_get_yres(brick); |
| 197 |
zres = gwy_brick_get_zres(brick); |
| 198 |
d = gwy_brick_get_data_const(brick); |
| 199 |
|
| 200 |
str = g_string_new(NULL); |
| 201 |
if (style == VOLUME_EXPORT_VTK) { |
| 202 |
n = xres*yres*zres; |
| 203 |
g_string_append_printf(str, vtk_header, args->title, xres, yres, zres, |
| 204 |
gwy_brick_get_dx(brick), gwy_brick_get_dy(brick), gwy_brick_get_dz(brick), |
| 205 |
n); |
| 206 |
gwy_append_doubles_to_gstring(str, d, n, precision, "\n", decimal_dot); |
| 207 |
g_string_append_c(str, '\n'); |
| 208 |
} |
| 209 |
else if (style == VOLUME_EXPORT_ZLINES) { |
| 210 |
n = xres*yres; |
| 211 |
for (i = 0; i < n; i++) { |
| 212 |
dd = d + i; |
| 213 |
for (j = 0; j < zres; j++) { |
| 214 |
gwy_append_doubles_to_gstring(str, dd, 1, precision, "", decimal_dot); |
| 215 |
g_string_append_c(str, j == zres-1 ? '\n' : '\t'); |
| 216 |
dd += n; |
| 217 |
} |
| 218 |
} |
| 219 |
} |
| 220 |
else if (style == VOLUME_EXPORT_LAYERS) { |
| 221 |
n = xres*yres; |
| 222 |
for (i = 0; i < zres; i++) { |
| 223 |
dd = d + i*n; |
| 224 |
gwy_append_doubles_to_gstring(str, dd, n, precision, "\t", decimal_dot); |
| 225 |
g_string_append_c(str, '\n'); |
| 226 |
} |
| 227 |
} |
| 228 |
else if (style == VOLUME_EXPORT_MATRICES) { |
| 229 |
n = xres*yres; |
| 230 |
for (i = 0; i < zres; i++) { |
| 231 |
for (j = 0; j < yres; j++) { |
| 232 |
dd = d + i*n + j*xres; |
| 233 |
gwy_append_doubles_to_gstring(str, dd, xres, precision, "\t", decimal_dot); |
| 234 |
g_string_append_c(str, '\n'); |
| 235 |
} |
| 236 |
g_string_append_c(str, '\n'); |
| 237 |
} |
| 238 |
} |
| 239 |
else { |
| 240 |
g_assert_not_reached(); |
| 241 |
} |
| 242 |
|
| 243 |
*data_len = -1; |
| 244 |
return g_string_free(str, FALSE); |
| 245 |
} |
| 246 |
|
| 247 |
static void |
| 248 |
destroy_brick(gchar *data, G_GNUC_UNUSED gpointer user_data) |
| 249 |
{ |
| 250 |
g_free(data); |
| 251 |
} |
| 252 |
|
| 253 |
|