Skip to content

Commit

Permalink
Add a +n option to -S to prevent saving node numbers in headers.
Browse files Browse the repository at this point in the history
  • Loading branch information
joa-quim committed Oct 4, 2024
1 parent 5e68adc commit 4a0ef71
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 23 deletions.
5 changes: 3 additions & 2 deletions doc/rst/source/triangulate.rst
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ Synopsis
[ |-N| ]
[ |-Q|\ [**n**] ]
[ |SYN_OPT-R| ]
[ |-S|\ [*first*][**+z**\ [**a**\|\ **l**\|\ **m**\|\ **p**\|\ **u**]] ]
[ |-S|\ [*first*][**+z**\ [**a**\|\ **l**\|\ **m**\|\ **p**\|\ **u**]][**+n**]]
[ |-T| ]
[ |SYN_OPT-V| ]
[ |-Z| ]
Expand Down Expand Up @@ -167,9 +167,10 @@ Optional Arguments

.. _-S:

**-S**\ [*first*][**+z**\ [**a**\|\ **l**\|\ **m**\|\ **p**\|\ **u**]]
**-S**\ [*first*][**+z**\ [**a**\|\ **l**\|\ **m**\|\ **p**\|\ **u**]][**+n**]
Output triangles as polygon segments separated by a segment header
record which contains node numbers *a-b-c* and **-Z**\ *polynumber*.
However, if the **+n** flag is used, we skip writing the *polynumber* and the node numbers.
Optionally, append *first*, where *first* is an integer, to report
the polygon numbers by counting from *first* [Default starts at zero].
Incompatible with |-Q|. Optionally, add modifier
Expand Down
17 changes: 10 additions & 7 deletions src/gmt_init.c
Original file line number Diff line number Diff line change
Expand Up @@ -7272,7 +7272,8 @@ GMT_LOCAL void gmtinit_conf_modern_override (struct GMT_CTRL *GMT) {
GMT->current.setting.map_grid_pen[GMT_SECONDARY].width = GMT->session.d_NaN; /* 0.5p (thinner) */

if (error)
GMT_Report (GMT->parent, GMT_MSG_ERROR, "Unrecognized value during gmtdefaults modern initialization.\n");}
GMT_Report (GMT->parent, GMT_MSG_ERROR, "Unrecognized value during gmtdefaults modern initialization.\n");
}

/*! . */
void gmtinit_conf_modern_US (struct GMT_CTRL *GMT) {
Expand Down Expand Up @@ -20541,16 +20542,18 @@ void gmt_auto_offsets_for_colorbar (struct GMT_CTRL *GMT, double offset[], int j
GMT_Report (GMT->parent, GMT_MSG_DEBUG, "Adding label space\n");
offset[GMT_OUT] += (GMT_LETTER_HEIGHT * GMT->current.setting.font_label.size / PSL_POINTS_PER_INCH) + MAX (0.0, GMT->current.setting.map_label_offset[GMT_Y]);
}
/* Because the next call will reset frame sides i will make a copy and override the override here */
/* Because the next call will reset frame sides I will make a copy and override the override here */
gmt_M_memcpy (sides, GMT->current.map.frame.side, 5U, unsigned int);
was = GMT->current.map.frame.draw;
gmtinit_conf_modern_override (GMT); /* Reset */
(void)gmt_getdefaults (GMT, NULL);
for (opt = options; opt; opt = opt->next) {
if (opt->option != '-') continue; /* Not a parameter setting */
if ((c = strchr (opt->arg, '=')) == NULL) continue;
c[0] = '\0'; /* Remove = */
n_errors += gmtlib_setparameter (GMT, opt->arg, &c[1], false);
if (!GMT->parent->external || options->option) { /* So that externals can send a NULL ptr for options. 'Internal' is not affected */
for (opt = options; opt; opt = opt->next) {
if (opt->option != '-') continue; /* Not a parameter setting */
if ((c = strchr(opt->arg, '=')) == NULL) continue;
c[0] = '\0'; /* Remove = */
n_errors += gmtlib_setparameter(GMT, opt->arg, &c[1], false);
}
}
if (n_errors)
GMT_Report (GMT->parent, GMT_MSG_WARNING, "GMT parameter parsing failures for %d settings\n", n_errors);
Expand Down
11 changes: 5 additions & 6 deletions src/gsfml/fzanalyzer.c
Original file line number Diff line number Diff line change
Expand Up @@ -308,8 +308,7 @@ GMT_LOCAL void FZ_trend (double *x, double *y, int n, double *intercept, double
}
}

GMT_LOCAL int FZ_fit_model (struct GMT_CTRL *GMT, double *d, double *vgg, int n, double corridor, double *width, int n_widths, double *asym, int n_asym, double *comp, int n_comp, double *results, PFV *FZshape)
{
GMT_LOCAL int FZ_fit_model(struct GMT_CTRL *GMT, double *d, double *vgg, int n, double corridor, double *width, int n_widths, double *asym, int n_asym, double *comp, int n_comp, double *results, PFV *FZshape) {
/* d = distance along crossing profile in km, with d = 0 the nominal FZ location given by digitized line.
* vgg = observed (resampled) VGG along crossing profile, possibly with NaNs at end.
* n = number of points in the profile (including any NaNs)
Expand All @@ -327,7 +326,7 @@ GMT_LOCAL int FZ_fit_model (struct GMT_CTRL *GMT, double *d, double *vgg, int n,

int col0, w, m, ic, row, way, n_sing = 0, n_fits = 0, got_trough;
double *d_vgg = NULL, *res = NULL, *predicted_vgg = NULL, *vgg_comp[N_SHAPES] = {NULL, NULL, NULL};
double min_var_b, min_var_t, var_model, intercept, slope, var_data, F, par[3];
double min_var_b, min_var_t, var_model, intercept = 0.0, slope, var_data, F, par[3];

/* The algorithms used below anticipate that vgg may have NaNs and thus skip those */

Expand All @@ -338,7 +337,7 @@ GMT_LOCAL int FZ_fit_model (struct GMT_CTRL *GMT, double *d, double *vgg, int n,
for (m = 0; m < N_SHAPES; m++) vgg_comp[m] = gmt_M_memory (GMT, NULL, n, double);
predicted_vgg = gmt_M_memory (GMT, NULL, n, double);
gmt_M_memcpy (d_vgg, vgg, n, double); /* Make copy of vgg */
FZ_trend (d, d_vgg, n, &intercept, &slope, 1); /* Find and remove linear trend just for data variance calculation */
FZ_trend(d, d_vgg, n, &intercept, &slope, 1); /* Find and remove linear trend just for data variance calculation */
/* So trend = d * slope + intercept; the shift of FZ location does not change this calculation (i.e. d is original d) */

var_data = FZ_get_variance (d_vgg, n); /* Compute sum of squares for the detrended data */
Expand All @@ -352,8 +351,8 @@ GMT_LOCAL int FZ_fit_model (struct GMT_CTRL *GMT, double *d, double *vgg, int n,
for (ic = 0; ic < n_comp; ic++) { /* Search for best compression factor */
for (row = 0; row < n_asym; row++) { /* Search for optimal asymmetry parameter asym */
n_fits++;
FZ_blendmodel (vgg_comp[FZ_G0], vgg_comp[FZ_G1], vgg_comp[FZ_G2], predicted_vgg, n, asym[row], comp[ic], 1.0); /* a blend, with unit amplitude */
if (FZ_solution (GMT, d, vgg, d[col0], predicted_vgg, n, par)) { /* LS solution for trend + scaled shape */
FZ_blendmodel(vgg_comp[FZ_G0], vgg_comp[FZ_G1], vgg_comp[FZ_G2], predicted_vgg, n, asym[row], comp[ic], 1.0); /* a blend, with unit amplitude */
if (FZ_solution(GMT, d, vgg, d[col0], predicted_vgg, n, par)) { /* LS solution for trend + scaled shape */
n_sing++;
continue; /* Return 1 if singular */
}
Expand Down
28 changes: 20 additions & 8 deletions src/triangulate.c
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,8 @@ struct TRIANGULATE_CTRL {
} Q;
struct TRIANGULATE_S { /* -S[<start>][+z[a|l|m|p|u]][+g] */
bool active;
bool color; /* If +g given */
bool color; /* If +g given */
bool nopoly; /* If true, do not write polygon numbers in headers */
int64_t firstpol; /* Number of first polygon [0] */
enum triangulate_enum_stat mode; /* Compute a <value> in header depending on mode */
} S;
Expand Down Expand Up @@ -180,7 +181,7 @@ static int usage(struct GMTAPI_CTRL *API, int level) {
GMT_qi_OPT, GMT_r_OPT, GMT_s_OPT, GMT_w_OPT, GMT_colon_OPT, GMT_PAR_OPT);
#else
GMT_Usage (API, 0, "usage: %s [<table>] [-A] [-C<slopegrid>] [-Dx|y] [-E<empty>] [-G%s] [%s] [%s] [-L<indextable>[+b]] [-M] [-N] "
"[-Q] [%s] [-S[<first>][+z[a|l|m|p|u]]] [-T] [%s] [-Z] [%s] [%s] [%s] [%s] [%s] [%s] [%s] [%s] [%s] [%s] [%s] [%s]\n", name, GMT_OUTGRID, GMT_I_OPT,
"[-Q] [%s] [-S[<first>][+z[a|l|m|p|u]][+n]] [-T] [%s] [-Z] [%s] [%s] [%s] [%s] [%s] [%s] [%s] [%s] [%s] [%s] [%s] [%s]\n", name, GMT_OUTGRID, GMT_I_OPT,
GMT_J_OPT, GMT_Rgeo_OPT, GMT_V_OPT, GMT_b_OPT, GMT_d_OPT, GMT_e_OPT, GMT_f_OPT, GMT_h_OPT, GMT_i_OPT,
GMT_qi_OPT, GMT_r_OPT, GMT_s_OPT, GMT_w_OPT, GMT_colon_OPT, GMT_PAR_OPT);
#endif
Expand Down Expand Up @@ -217,7 +218,7 @@ static int usage(struct GMTAPI_CTRL *API, int level) {
GMT_Usage (API, -2, "Append n to produce closed Voronoi polygons.");
#endif
GMT_Option (API, "R");
GMT_Usage (API, 1, "\n-S[<first>][+z[a|l|m|p|u]]");
GMT_Usage (API, 1, "\n-S[<first>][+z[a|l|m|p|u]][+n]");
GMT_Usage (API, -2, "Output triangle polygons as multiple segments separated by segment headers. Append <first>, "
"an integer, to report polygon numbers counting from <first> [Default counts from zero]. Cannot be used with -Q. "
"Alternatively, compute representative value for the triplet z-values at triangle nodes via modifier +z (implies -Z). Modes are");
Expand All @@ -226,6 +227,7 @@ static int usage(struct GMTAPI_CTRL *API, int level) {
GMT_Usage (API, 3, "m: The median of triplet.");
GMT_Usage (API, 3, "p: The mode of triplet.");
GMT_Usage (API, 3, "u: The upper value of triplet.");
GMT_Usage (API, -2, "Add +n to NOT report the node number in each polygon header (this makes <first> meaningless).");
GMT_Usage (API, 1, "\n-T Output triangles or polygons even if gridding has been selected with -G. Default behavior "
"is to produce a grid based on the triangles or polygons only.");
GMT_Option (API, "V");
Expand Down Expand Up @@ -343,6 +345,12 @@ static int parse(struct GMT_CTRL *GMT, struct TRIANGULATE_CTRL *Ctrl, struct GMT
break;
case 'S':
n_errors += gmt_M_repeated_module_option (API, Ctrl->S.active);

if ((c = strstr(opt->arg, "+n"))) { /* Do not write polygon node numbers in headers */
Ctrl->S.nopoly = true;
c[0] = '\0'; /* Truncate the modifier */
}

if ((c = strstr (opt->arg, "+z"))) {
switch (c[2]) {
case 'a': case '\0': Ctrl->S.mode = TRI_MEAN; break; /* Use mean of three node z-values [Default] */
Expand All @@ -359,6 +367,7 @@ static int parse(struct GMT_CTRL *GMT, struct TRIANGULATE_CTRL *Ctrl, struct GMT
}
else
Ctrl->S.mode = TRI_POLY;

if (opt->arg[0])
Ctrl->S.firstpol = atol (opt->arg);
else
Expand Down Expand Up @@ -939,7 +948,8 @@ EXTERN_MSC int GMT_triangulate(void *V_API, int mode, void *args) {
double z_mean;
for (i = ij = 0; i < np; i++, ij += 3) {
z_mean = (zz[link[ij]] + zz[link[ij+1]] + zz[link[ij+2]]) / 3;
sprintf (record, "Polygon %d-%d-%d -Z%.8g", link[ij], link[ij+1], link[ij+2], z_mean);
(Ctrl->S.nopoly) ? sprintf(record, " -Z%.8g", z_mean) :
sprintf(record, "Polygon %d-%d-%d -Z%.8g", link[ij], link[ij+1], link[ij+2], z_mean);
if (Ctrl->A.active) { /* Compute and report area */
double area = 0.5 * ((xx[link[ij]] - xx[link[ij+2]]) * (yy[link[ij+1]] - yy[link[ij]]) - (xx[link[ij]] - xx[link[ij+1]]) * (yy[link[ij+2]] - yy[link[ij]]));
sprintf (area_txt, a_format, area);
Expand Down Expand Up @@ -979,14 +989,16 @@ EXTERN_MSC int GMT_triangulate(void *V_API, int mode, void *args) {
default: /* Just for Coverity */
break;
}
sprintf (record, "Polygon %d-%d-%d -Z%g", link[ij], link[ij+1], link[ij+2], z_triangle);
(Ctrl->S.nopoly) ? sprintf(record, " -Z%g", z_triangle) :
sprintf(record, "Polygon %d-%d-%d -Z%g", link[ij], link[ij+1], link[ij+2], z_triangle);
}
else
sprintf (record, "Polygon %d-%d-%d -Z%" PRIu64, link[ij], link[ij+1], link[ij+2], i+first);
(Ctrl->S.nopoly) ? sprintf(record, " -Z%" PRIu64, i+first) :
sprintf(record, "Polygon %d-%d-%d -Z%" PRIu64, link[ij], link[ij+1], link[ij+2], i+first);
if (Ctrl->A.active) { /* Compute and report area */
double area = 0.5 * ((xx[link[ij]] - xx[link[ij+2]]) * (yy[link[ij+1]] - yy[link[ij]]) - (xx[link[ij]] - xx[link[ij+1]]) * (yy[link[ij+2]] - yy[link[ij]]));
sprintf (area_txt, a_format, area);
strcat (record, area_txt);
sprintf(area_txt, a_format, area);
strcat(record, area_txt);
}
GMT_Put_Record (API, GMT_WRITE_SEGMENT_HEADER, record);
for (k = 0; k < 3; k++) { /* Three vertices */
Expand Down

0 comments on commit 4a0ef71

Please sign in to comment.