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

smsh.h (4224B)


      1 /* Copyright (C) 2020-2023, 2025 |Méso|Star> (contact@meso-star.com)
      2  *
      3  * This program is free software: you can redismshbute it and/or modify
      4  * it under the terms of the GNU General Public License as published by
      5  * the Free Software Foundation, either version 3 of the License, or
      6  * (at your option) any later version.
      7  *
      8  * This program is dismshbuted in the hope that it will be useful,
      9  * but WITHOUT ANY WARRANTY; without even the implied warranty of
     10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
     11  * GNU General Public License for more details.
     12  *
     13  * You should have received a copy of the GNU General Public License
     14  * along with this program. If not, see <http://www.gnu.org/licenses/>. */
     15 
     16 #ifndef SMSH_H
     17 #define SMSH_H
     18 
     19 #include <rsys/hash.h>
     20 #include <rsys/rsys.h>
     21 
     22 /* Library symbol management */
     23 #if defined(SMSH_SHARED_BUILD) /* Build shared library */
     24   #define SMSH_API extern EXPORT_SYM
     25 #elif defined(SMSH_STATIC) /* Use/build static library */
     26   #define SMSH_API extern LOCAL_SYM
     27 #else /* Use shared library */
     28   #define SMSH_API extern IMPORT_SYM
     29 #endif
     30 
     31 /* Helper macro that asserts if the invocation of the smsh function `Func'
     32  * returns an error. One should use this macro on smsh function calls for
     33  * which no explicit error checking is performed */
     34 #ifndef NDEBUG
     35   #define SMSH(Func) ASSERT(smsh_ ## Func == RES_OK)
     36 #else
     37   #define SMSH(Func) smsh_ ## Func
     38 #endif
     39 
     40 /* Forward declaration of external data types */
     41 struct logger;
     42 struct mem_allocator;
     43 
     44 struct smsh_create_args {
     45   struct logger* logger; /* May be NULL <=> default logger */
     46   struct mem_allocator* allocator; /* NULL <=> use default allocator */
     47   int verbose; /* Verbosity level */
     48 };
     49 #define SMSH_CREATE_ARGS_DEFAULT__ {NULL, NULL, 0}
     50 static const struct smsh_create_args SMSH_CREATE_ARGS_DEFAULT =
     51   SMSH_CREATE_ARGS_DEFAULT__;
     52 
     53 struct smsh_load_args {
     54   const char* path;
     55   int memory_mapping; /* Use memory mapping instead of normal loading */
     56 };
     57 #define SMSH_LOAD_ARGS_NULL__ {NULL, 0}
     58 static const struct smsh_load_args SMSH_LOAD_ARGS_NULL =
     59   SMSH_LOAD_ARGS_NULL__;
     60 
     61 struct smsh_load_stream_args {
     62   FILE* stream;
     63   const char* name; /* Name of the stream */
     64   /* Use memory mapping instead of normal loading. Note that memory mapping
     65    * cannot be used on some stream like stdin */
     66   int memory_mapping;
     67 };
     68 #define SMSH_LOAD_STREAM_ARGS_NULL__ {NULL, "stream", 0}
     69 static const struct smsh_load_stream_args SMSH_LOAD_STREAM_ARGS_NULL =
     70   SMSH_LOAD_STREAM_ARGS_NULL__;
     71 
     72 struct smsh_desc {
     73   const double* nodes; /* List of double[dnode] */
     74   const uint64_t* cells; /* List of uint64_t[dcell] */
     75   size_t nnodes;
     76   size_t ncells;
     77   unsigned dnode; /* Dimension of a node */
     78   unsigned dcell; /* Dimension of a cell */
     79 };
     80 #define SMSH_DESC_NULL__ {NULL, NULL, 0, 0, 0, 0}
     81 static const struct smsh_desc SMSH_DESC_NULL = SMSH_DESC_NULL__;
     82 
     83 /* Forward declaration of opaque data types */
     84 struct smsh;
     85 
     86 BEGIN_DECLS
     87 
     88 /*******************************************************************************
     89  * Star-Mesh API
     90  ******************************************************************************/
     91 SMSH_API res_T
     92 smsh_create
     93   (const struct smsh_create_args* args,
     94    struct smsh** smsh);
     95 
     96 SMSH_API res_T
     97 smsh_ref_get
     98   (struct smsh* smsh);
     99 
    100 SMSH_API res_T
    101 smsh_ref_put
    102   (struct smsh* smsh);
    103 
    104 SMSH_API res_T
    105 smsh_load
    106   (struct smsh* smsh,
    107    const struct smsh_load_args* args);
    108 
    109 SMSH_API res_T
    110 smsh_load_stream
    111   (struct smsh* smsh,
    112    const struct smsh_load_stream_args* args);
    113 
    114 SMSH_API res_T
    115 smsh_get_desc
    116   (const struct smsh* smsh,
    117    struct smsh_desc* desc);
    118 
    119 SMSH_API res_T
    120 smsh_desc_compute_hash
    121   (const struct smsh_desc* desc,
    122    hash256_T hash);
    123 
    124 static INLINE const double*
    125 smsh_desc_get_node
    126   (const struct smsh_desc* desc,
    127    const size_t inode)
    128 {
    129   ASSERT(desc && inode < desc->nnodes);
    130   return desc->nodes + inode*desc->dnode;
    131 }
    132 
    133 static INLINE const uint64_t*
    134 smsh_desc_get_cell
    135   (const struct smsh_desc* desc,
    136    const size_t icell)
    137 {
    138   const uint64_t* cell;
    139   ASSERT(desc && icell < desc->ncells);
    140   cell = desc->cells + icell*desc->dcell;
    141 #ifndef NDEBUG
    142   {unsigned i; FOR_EACH(i, 0, desc->dcell) ASSERT(cell[i] < desc->nnodes);}
    143 #endif
    144   return cell;
    145 }
    146 
    147 END_DECLS
    148 
    149 #endif /* SMSH_H */