You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

189 lines
8.3 KiB
C++

1 month ago
/*------------------------------------------------------------------------------
* Copyright (c) 2023 by Bai Bing (seread@163.com)
* See COPYING file for copying and redistribution conditions.
*
* Alians IT Studio.
*----------------------------------------------------------------------------*/
#include <string.h>
#include "params.h"
#include "version.h"
namespace ais
{
// default options
const char *shortopts = "s:t:c:b:a:d:x:y:X:Y:i:r:f:e:w:m:o:n:u:g:l:v:h?";
char *optarg2 = NULL;
const option longopts[] = {
{"source-point-file", required_argument, 0, 's'},
{"target-area-file", required_argument, 0, 't'},
{"mock-points-count", required_argument, 0, 'c'},
{"breakline", required_argument, 0, 'b'},
{"fault", required_argument, 0, 'a'},
{"x-nodes-count", required_argument, 0, 'x'},
{"y-nodes-count", required_argument, 0, 'y'},
{"x-grid-size", required_argument, 0, 'X'},
{"y-grid-size", required_argument, 0, 'Y'},
{"max-iteration", required_argument, 0, 'i'},
{"residual", required_argument, 0, 'r'},
{"fill-value", required_argument, 0, 'f'},
{"estimate-factor", required_argument, 0, 'e'},
{"corner-weight", required_argument, 0, 'w'},
{"multi-thread", required_argument, 0, 'm'},
{"output-file", required_argument, 0, 'o'},
{"contour-step", optional_argument, 0, 'n'},
{"contour-mark-step", optional_argument, 0, 'u'},
{"insert-times", optional_argument, 0, 'g'},
{"smooth-times", optional_argument, 0, 'p'},
{"fault-edge-level", optional_argument, 0, 'l'},
{"area", optional_argument, 0, 'd'},
{"version", no_argument, 0, 'v'},
{"help", no_argument, 0, 'h'},
{"help", no_argument, 0, '?'},
{NULL, 0, 0, 0},
};
std::map<std::string, std::any> Settings::params()
{
std::map<std::string, std::any> ret;
ret["max_iteration"] = maxIteration;
ret["residual"] = residual;
ret["fill_value"] = fillValue;
ret["use_multi_threads"] = useMultiThread;
ret["fault_edge_level"] = faultEdgeLevel;
ret["estimate_factor"] = estimateFactor;
ret["corner_weight"] = cornerWeight;
return ret;
}
void usage(const char *app)
{
printf("%s: %s\n", app, ais::VERSION);
printf("Usage: %s [options...]"
"Options:\n\n"
"Source data parameters:\n"
"\t -s, --source-point-file Source random point file\n"
"\t -t, --target-area-file Target gridding point file, will use points range while not specified this optional\n"
"\t -c, --mock-points-count Use mock random points, default is 200\n"
"\t -b, --breakline Breakline file, big or complex may delayed or failed iterative processes\n"
"\t -a, --fault Fault file, big or complex may delayed or failed iterative processes\n\n"
"Iteration parameters:\n"
"\t -x, --x-nodes-count X coordinate grid nodes count, default is 101\n"
"\t -y, --y-nodes-count Y coordinate grid nodes count, default is 101\n"
"\t -X, --x-grid-size X coordinate grid size, setup up this param will update x node count automatically\n"
"\t -Y, --y-grid-size Y coordinate grid size, setup up this param will update y node count automatically\n"
"\t -i, --max-iteration Max iteration for interpolation, default is 20000\n"
"\t -r, --residual Residual for iteration, default is 0.0001\n"
"\t Eps Max = Residual * (zMax - zMin)\n"
"\t -f, --fill-value Fill value for gridding nan node, default is 0.0\n"
"\t -e, --estimate-factor Use special scale factor to estimate the grid, help to output smooth result\n"
"\t default is 0, auto detect estimate factor for surface estimate\n"
"\t -w, --corner-weight Corner weight which help to output smooth result\n"
"\t default is 64, should between 4~256\n"
"\t -m, --multi-thread Use multi-thread to iteration [on/off], default is on\n"
"\t Multi-thread can speed up iteration, but the finial result may have some gap with single thread.\n\n"
"\t -d, --area Set area\n"
"Output parameters:\n"
"\t -o, --output-file Output grid file, default is output.grd\n"
"\t -l, --fault-edge-level Added fault mark to output file, the level(0-4) will make fault edge different, default not added\n\n"
"\t -n, --contour-step Set contour step\n"
"\t -u, --contour-mark-step Set contour mark\n"
"\t -g, --insert-times Set contour insert times\n"
"\t -p, --smooth-times Set gird smooth times\n"
"Other parameters:\n"
"\t -h, --help print this help and exit\n"
"\t -?, --help print this help and exit\n"
"\t -v, --version print version and exit\n",
app);
}
// Simplified ported getopt_long
int getopt_long_2(int argc, char **argv, const char *shortopts, const option *longopts, int *longind,
char **optarg)
{
// reset the optarg, ensure the output optarg will not keep pre-results
*optarg = NULL;
const char *opName = NULL;
int opValue = 0;
if (*longind >= argc - 1)
return -1;
// get next param
char *param = argv[++(*longind)];
if (param && param[0] == '-')
{
// param with option flag '-'
if (param[1] == '-')
{
// long option param
opName = &param[2];
}
else if (param[1] != ' ' && param[1] != '\n')
{
// short option param
opValue = param[1];
}
else
{
// something error, maybe only '-', print usage
opName = "help";
}
}
else if (!param)
{
// start app by command only, run app in default setting
return -1;
}
// param not in our list, print usage
if (!opName && opValue == 0)
opName = "help";
const struct option *op = longopts;
while (op->name)
{
if (op->val == opValue || (opName && (strcmp(op->name, opName) == 0)))
{
// hit op, try get param
if (op->has_arg == required_argument && (param[0] == '-') && (*longind < argc))
{
*optarg = argv[++(*longind)];
}
else if (op->has_arg == optional_argument && (param[0] == '-'))
{
if (*longind < argc)
{
// there are some params after this optional param,
// get next and test whether it is the corresponding parameter
*optarg = argv[++(*longind)];
std::string ops{shortopts};
std::string sop = (*optarg) != NULL ? (*optarg) : "";
if (sop.length() > 1 && ((*optarg)[1] == '-' || ops.find(sop.substr(1, 1)) != ops.npos))
{
// the next argv is another one, reset and output default
*optarg = NULL;
(*longind)--;
}
}
else
{
// no more param after this, do nothing because the *optarg have been reset to null at begin
}
}
break;
}
op++;
}
if (op)
return op->val;
else
return -1;
}
} // namespace ais