star-mesh

Define and load a binary data format for meshes
git clone git://git.meso-star.fr/star-mesh.git
Log | Files | Refs | README | LICENSE

commit 67827f7d55a551ab3b369e0b2fee90e17dbf64c5
parent 97a5ca687751451117409c164fd852f63a11ef7a
Author: Vincent Forest <vincent.forest@meso-star.com>
Date:   Wed, 26 Feb 2025 16:54:59 +0100

Write the smsh-desc util

It prints the descriptor of a smsh file.

In this commit, the man pages are also moved to their own sub-directory,
since there are now enough of them to justify it.

Diffstat:
M.gitignore | 1+
MMakefile | 15+++++++++------
Adoc/smsh-desc.1 | 66++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Rsmsh.5 -> doc/smsh.5 | 0
Adoc/smsh2vtk.1 | 69+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Dsmsh2vtk.1 | 67-------------------------------------------------------------------
Asrc/smsh-desc.c | 209+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Msrc/smsh2vtk.c | 19++++++++++---------
8 files changed, 364 insertions(+), 82 deletions(-)

diff --git a/.gitignore b/.gitignore @@ -4,6 +4,7 @@ .gitignore *.pc smsh2vtk +smsh-desc *.so *.sw[po] tags diff --git a/Makefile b/Makefile @@ -70,11 +70,11 @@ libsmsh.o: $(OBJ) ################################################################################ # Util ################################################################################ -UTIL_SRC = src/smsh2vtk.c +UTIL_SRC = src/smsh2vtk.c src/smsh-desc.c UTIL_BIN = $(UTIL_SRC:.c=.b) UTIL_OBJ = $(UTIL_SRC:.c=.o) UTIL_DEP = $(UTIL_SRC:.c=.d) -UTIL_EXE = smsh2vtk +UTIL_EXE = smsh2vtk smsh-desc PKG_CONFIG_LOCAL = PKG_CONFIG_PATH="./:$${PKG_CONFIG_PATH}" $(PKG_CONFIG) INCS_UTIL = $$($(PKG_CONFIG_LOCAL) $(PCFLAGS) --cflags rsys smsh-local.pc) @@ -131,8 +131,9 @@ install: library utils pkg install 755 "$(DESTDIR)$(BINPREFIX)" $(UTIL_EXE); \ install 644 "$(DESTDIR)$(LIBPREFIX)/pkgconfig" smsh.pc; \ install 644 "$(DESTDIR)$(INCPREFIX)/star" src/smsh.h; \ - install 644 "$(DESTDIR)$(MANPREFIX)/man1" smsh2vtk.1; \ - install 644 "$(DESTDIR)$(MANPREFIX)/man5" smsh.5; \ + install 644 "$(DESTDIR)$(MANPREFIX)/man1" doc/smsh2vtk.1; \ + install 644 "$(DESTDIR)$(MANPREFIX)/man1" doc/smsh-desc.1; \ + install 644 "$(DESTDIR)$(MANPREFIX)/man5" doc/smsh.5; \ install 644 "$(DESTDIR)$(PREFIX)/share/doc/star-mesh" COPYING README.md uninstall: @@ -140,14 +141,16 @@ uninstall: rm -f "$(DESTDIR)$(LIBPREFIX)/pkgconfig/smsh.pc" rm -f "$(DESTDIR)$(INCPREFIX)/star/smsh.h" rm -f "$(DESTDIR)$(MANPREFIX)/man1/smsh2vtk.1" + rm -f "$(DESTDIR)$(MANPREFIX)/man1/smsh-desc.1" rm -f "$(DESTDIR)$(MANPREFIX)/man5/smsh.5" rm -f "$(DESTDIR)$(PREFIX)/share/doc/star-mesh/COPYING" rm -f "$(DESTDIR)$(PREFIX)/share/doc/star-mesh/README.md" for i in $(UTIL_EXE); do rm -f "$(DESTDIR)$(BINPREFIX)/$${i}"; done lint: - mandoc -T lint -Wbase smsh.5 - mandoc -T lint -Wbase smsh2vtk.1 + mandoc -T lint -Wbase doc/smsh.5 + mandoc -T lint -Wbase doc/smsh2vtk.1 || [ $$? -eq 1 ]; + mandoc -T lint -Wbase doc/smsh-desc.1 || [ $$? -eq 1 ]; clean: clean_test rm -f $(DEP) $(OBJ) $(LIBNAME) diff --git a/doc/smsh-desc.1 b/doc/smsh-desc.1 @@ -0,0 +1,66 @@ +.\" Copyright (C) 2020-2023, 2025 |Méso|Star> (contact@meso-star.com) +.\" +.\" 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 3 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, see <http://www.gnu.org/licenses/>. +.Dd February 26, 2025 +.\""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""" +.Dt SMSH-DESC 1 +.Os +.\""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""" +.Sh NAME +.Nm smsh-desc +.Nd +.Xr smsh 5 +file descriptor +.\""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""" +.Sh SYNOPSIS +.Nm +.Op Fl cdnpt +.Op Ar mesh +.\""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""" +.Sh DESCRIPTION +Print the pagesize on which the +.Xr smsh 5 +mesh data is aligned, as well as the number and dimension of nodes and +cells. +.Pp +The descritpion is printed on stdout with a message of the form: +.Bd -literal -offset Ds +"%zu %zy %zu %u %u %s" pagesz, #nodes, #cells, Dnode, Dcell, mesh +.Ed +.Pp +When any option is specified, +.Nm +reports only the information requested by the specified options. +.Fl q +option is set. +.Pp +The options are as follows: +.Bl -tag -width Ds +.It Fl c +print cells count. +.It Fl d +print node dimension. +.It Fl n +print node count. +.It Fl p +print data alignment. +.It Fl t +print cell dimension. +.El +.\""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""" +.Sh EXIT STATUS +.Ex -std +.\""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""" +.Sh SEE ALSO +.Xr smsh 5 diff --git a/smsh.5 b/doc/smsh.5 diff --git a/doc/smsh2vtk.1 b/doc/smsh2vtk.1 @@ -0,0 +1,69 @@ +.\" Copyright (C) 2020-2023, 2025 |Méso|Star> (contact@meso-star.com) +.\" +.\" 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 3 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, see <http://www.gnu.org/licenses/>. +.Dd February 26, 2025 +.\""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""" +.Dt SMSH2VTK 1 +.Os +.\""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""" +.Sh NAME +.Nm smsh2vtk +.Nd convert +.Xr smsh 5 +mesh to VTK format +.\""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""" +.Sh SYNOPSIS +.Nm +.Op Fl o Ar output +.Op Ar mesh +.\""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""" +.Sh DESCRIPTION +.Nm +converts a triangular or tetrahedral mesh saved in +.Xr smsh 5 +format to legacy VTK format. +.Pp +The options are as follows: +.Bl -tag -width Ds +.It Fl o Ar output +Output file. +If not defined, output data is written to standard output. +.El +.\""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""" +.Sh EXIT STATUS +.Ex -std +.\""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""" +.Sh EXAMPLES +Convert the +.Xr smsh 5 +.Pa input.smsh +mesh to VTK format and saves the results in the +.Pa output.vtk +file: +.Bd -literal -offset Ds +smsh2vtk -o output.vtk input.smsh +.Ed +.\""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""" +.Sh SEE ALSO +.Xr smsh 5 +.\""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""" +.Sh STANDARDS +.Rs +.%B The VTK User's Guide +.%O Simple Legacy Formats +.%I Kitware, Inc +.%N 11 +.%D 2010 +.%P 470--482 +.Re diff --git a/smsh2vtk.1 b/smsh2vtk.1 @@ -1,67 +0,0 @@ -.\" Copyright (C) 2020-2023, 2025 |Méso|Star> (contact@meso-star.com) -.\" -.\" 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 3 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, see <http://www.gnu.org/licenses/>. -.Dd February 26, 2025 -.\""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""" -.Dt SMSH2VTK 1 -.Os -.\""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""" -.Sh NAME -.Nm smsh2vtk -.Nd convert smsh mesh to VTK format -.\""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""" -.Sh SYNOPSIS -.Nm -.Op Fl o Ar output -.Op Ar mesh -.\""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""" -.Sh DESCRIPTION -.Nm -converts a triangular or tetrahedral mesh saved in -.Xr smsh 5 -format to legacy VTK format. -.Pp -The options are as follows: -.Bl -tag -width Ds -.It Fl o Ar output -Output file. -If not defined, output data is written to standard output. -.El -.\""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""" -.Sh EXIT STATUS -.Ex -std -.\""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""" -.Sh EXAMPLES -Convert the -.Xr smsh 5 -.Pa input.smsh -mesh to VTK format and saves the results in the -.Pa output.vtk -file: -.Bd -literal -offset Ds -smsh2vtk -o output.vtk input.smsh -.Ed -.\""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""" -.Sh SEE ALSO -.Xr smsh 5 -.\""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""" -.Sh STANDARDS -.Rs -.%B The VTK User's Guide -.%O Simple Legacy Formats -.%I Kitware, Inc -.%N 11 -.%D 2010 -.%P 470--482 -.Re diff --git a/src/smsh-desc.c b/src/smsh-desc.c @@ -0,0 +1,209 @@ +/* Copyright (C) 2020-2023, 2025 |Méso|Star> (contact@meso-star.com) + * + * 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 3 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, see <http://www.gnu.org/licenses/>. */ + +#define _POSIX_C_SOURCE 200112L /* getopt support */ + +#include "smsh.h" + +#include <rsys/cstr.h> +#include <rsys/mem_allocator.h> +#include <rsys/str.h> + +#include <errno.h> +#include <unistd.h> /* getopt */ + +enum query_flag { + QUERY_DCELL = BIT(0), + QUERY_DNODE = BIT(1), + QUERY_NCELLS = BIT(2), + QUERY_NNODES = BIT(3), + QUERY_PAGESIZE = BIT(4), + QUERY_ALL = ~0 +}; + +/* Input arguments */ +struct args { + char* mesh; + int query_mask; /* Cmbination of query flag */ +}; +static const struct args ARGS_DEFAULT = {0}; + +/* Command data */ +struct cmd { + struct args args; + struct smsh* mesh; /* Tetrahedral mesh */ + uint64_t pagesize; +}; +static const struct cmd CMD_NULL = {0}; + +/******************************************************************************* + * Helper functions + ******************************************************************************/ +static INLINE void +usage(FILE* stream) +{ + fprintf(stream, "usage: smsh-desc [-cdnpt] mesh\n"); +} + +static res_T +args_init(struct args* args, const int argc, char** argv) +{ + int opt = 0; + res_T res = RES_OK; + + *args = ARGS_DEFAULT; + + while((opt = getopt(argc, argv, "cdnpqt")) != -1) { + switch(opt) { + case 'c': args->query_mask |= QUERY_NCELLS; break; + case 'd': args->query_mask |= QUERY_DNODE; break; + case 'n': args->query_mask |= QUERY_NNODES; break; + case 'p': args->query_mask |= QUERY_PAGESIZE; break; + case 't': args->query_mask |= QUERY_DCELL; break; + default: res = RES_BAD_ARG; + } + if(res != RES_OK) goto error; + } + + if(optind < argc) args->mesh = argv[optind]; + + /* By default, query all */ + if(!args->query_mask) args->query_mask = QUERY_ALL; + + if(!args->mesh) { + fprintf(stderr, "mesh is missing\n"); + res = RES_BAD_ARG; + goto error; + } + +exit: + return res; +error: + usage(stderr); + goto exit; +} + +static res_T +setup_mesh(struct cmd* cmd, const struct args* args) +{ + struct smsh_create_args create_args = SMSH_CREATE_ARGS_DEFAULT; + struct smsh_load_args load_args = SMSH_LOAD_ARGS_NULL; + FILE* fp = NULL; + res_T res = RES_OK; + ASSERT(cmd && args); + + if(!(fp = fopen(args->mesh, "r"))) { + fprintf(stderr, "%s: %s\n", args->mesh, strerror(errno)); + res = RES_IO_ERR; + goto error; + } + if(fread(&cmd->pagesize, sizeof(uint64_t), 1, fp) != 1) { + fprintf(stderr, "%s: %s\n", args->mesh, strerror(errno)); + res = RES_IO_ERR; + goto error; + } + + create_args.verbose = 1; + if((res = smsh_create(&create_args, &cmd->mesh)) != RES_OK) goto error; + + load_args.path = args->mesh; + if((res = smsh_load(cmd->mesh, &load_args)) != RES_OK) goto error; + +exit: + if(fp) CHK(fclose(fp) == 0); + return res; +error: + if(cmd->mesh) { SMSH(ref_put(cmd->mesh)); cmd->mesh = NULL; } + goto exit; +} + +static void +cmd_release(struct cmd* cmd) +{ + ASSERT(cmd); + if(cmd->mesh) SMSH(ref_put(cmd->mesh)); +} + +static res_T +cmd_init(struct cmd* cmd, const struct args* args) +{ + res_T res = RES_OK; + ASSERT(cmd && args && args->query_mask); + + if((res = setup_mesh(cmd, args)) != RES_OK) goto error; + cmd->args = *args; + +exit: + return res; +error: + cmd_release(cmd); + goto exit; +} + +static res_T +cmd_run(struct cmd* cmd) +{ + struct smsh_desc desc = SMSH_DESC_NULL; + struct str str; + res_T res = RES_OK; + ASSERT(cmd); + + str_init(NULL, &str); + SMSH(get_desc(cmd->mesh, &desc)); + + #define APPEND(Fmt, Val) { \ + if((res = str_append_printf(&str, Fmt, (Val))) != RES_OK) goto error; \ + } (void)0 + if(cmd->args.query_mask & QUERY_PAGESIZE) APPEND("%zu ", cmd->pagesize); + if(cmd->args.query_mask & QUERY_NNODES) APPEND("%zu ", desc.nnodes); + if(cmd->args.query_mask & QUERY_NCELLS) APPEND("%zu ", desc.ncells); + if(cmd->args.query_mask & QUERY_DNODE) APPEND("%u ", desc.dnode); + if(cmd->args.query_mask & QUERY_DCELL) APPEND("%u ", desc.dcell); + APPEND("%s", cmd->args.mesh); + #undef APPEND + + printf("%s\n", str_cget(&str)); + +exit: + str_release(&str); + return res; +error: + fprintf(stderr, "error: %s\n", res_to_cstr(res)); + goto exit; +} + +/******************************************************************************* + * The program + ******************************************************************************/ +int +main(int argc, char** argv) +{ + struct args args = ARGS_DEFAULT; + struct cmd cmd = CMD_NULL; + int err = 0; + res_T res = RES_OK; + + if((res = args_init(&args, argc, argv)) != RES_OK) goto error; + if((res = cmd_init(&cmd, &args)) != RES_OK) goto error; + if((res = cmd_run(&cmd)) != RES_OK) goto error; + +exit: + cmd_release(&cmd); + CHK(mem_allocated_size() == 0); + return err; +error: + err = 1; + goto exit; +} diff --git a/src/smsh2vtk.c b/src/smsh2vtk.c @@ -98,7 +98,7 @@ check_mem_leaks(void) return RES_OK; /* No memory leak */ size_to_cstr(sz, SIZE_ALL, NULL, buffer, sizeof(buffer)); - fprintf(stderr, "Memory leaks: %s\n", buffer); + fprintf(stderr, "memory leaks: %s\n", buffer); return RES_MEM_ERR; } @@ -290,6 +290,14 @@ error: goto exit; } +static void +cmd_release(struct cmd* cmd) +{ + ASSERT(cmd); + if(cmd->mesh) SMSH(ref_put(cmd->mesh)); + if(cmd->output && cmd->output != stdout) CHK(fclose(cmd->output) == 0); +} + static res_T cmd_init(struct cmd* cmd, const struct args* args) { @@ -302,17 +310,10 @@ cmd_init(struct cmd* cmd, const struct args* args) exit: return res; error: + cmd_release(cmd); goto exit; } -static void -cmd_release(struct cmd* cmd) -{ - ASSERT(cmd); - if(cmd->mesh) SMSH(ref_put(cmd->mesh)); - if(cmd->output && cmd->output != stdout) CHK(fclose(cmd->output) == 0); -} - static res_T cmd_run(struct cmd* cmd) {