star-stl

Load STereo Lithography (StL) file format
git clone git://git.meso-star.fr/star-stl.git
Log | Files | Refs | README | LICENSE

sstl.h (7124B)


      1 /* Copyright (C) 2015, 2016, 2019, 2021, 2023, 2025 |Méso|Star> (contact@meso-star.com)
      2  *
      3  * This program is free software: you can redistribute 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 distributed 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 Lesser General Public License for more details.
     12  *
     13  * You should have received a copy of the GNU Lesser General Public License
     14  * along with this program. If not, see <http://www.gnu.org/licenses/>. */
     15 
     16 #ifndef SSTL_H
     17 #define SSTL_H
     18 
     19 #include <rsys/rsys.h>
     20 
     21 /* Library symbol management */
     22 #if defined(SSTL_SHARED_BUILD)
     23   #define SSTL_API extern EXPORT_SYM /* Build shared library */
     24 #elif defined(SSTL_STATUC) /* Use/build statuc library */
     25   #define SSTL_API extern LOCAL_SYM
     26 #else /* Use shared library */
     27   #define SSTL_API extern IMPORT_SYM
     28 #endif
     29 
     30 /* Helper macro that asserts if the invocation of the sstl function `Func'
     31  * returns an error. One should use this macro on sstl function calls for which
     32  * no explicit error checking is performed. */
     33 #ifndef NDEBUG
     34   #define SSTL(Func) ASSERT(sstl_ ## Func == RES_OK)
     35 #else
     36   #define SSTL(Func) sstl_ ## Func
     37 #endif
     38 
     39 /* The type of a read file */
     40 enum sstl_type {
     41   SSTL_ASCII,
     42   SSTL_BINARY,
     43   SSTL_NONE__
     44 };
     45 
     46 /* Descriptor of a loaded STL */
     47 struct sstl_desc {
     48   const char* filename;
     49   const char* solid_name; /* May be NULL <=> no name */
     50   enum sstl_type type; /* The type of the file */
     51 
     52   /* Front faces are CCW ordered and the normals follow the right handed rule */
     53   const float* vertices; /* triangles_count * 3 coordinates */
     54   const unsigned* indices; /* triangles_count * 3 indices */
     55   const float* normals; /* Per triangle normalized normal */
     56 
     57   size_t triangles_count;
     58   size_t vertices_count;
     59 };
     60 #define SSTL_DESC_NULL__ {0}
     61 static const struct sstl_desc SSTL_DESC_NULL = SSTL_DESC_NULL__;
     62 
     63 struct sstl_writer_create_args {
     64   const char* filename; /* Name of the file to write or of the provided stream */
     65   FILE* stream; /* NULL <=> write data to the file "name" */
     66 
     67   enum sstl_type type; /* Written data is either ASCII or binary */
     68 
     69   const char* solid_name; /* Can be NULL. Not used for binary StL */
     70 
     71   /* <0 <=> The number of triangles is calculated automatically.
     72    * Must be set when writing binary data to a non-searchable stream */
     73   long triangles_count;
     74 
     75   struct logger* logger; /* NULL <=> use default logger */
     76   struct mem_allocator* allocator; /* NULL <=> use default allocator */
     77   int verbose; /* verbosity level */
     78 };
     79 #define SSTL_WRITER_CREATE_ARGS_DEFAULT__ \
     80   {NULL, NULL, SSTL_ASCII, NULL, -1, NULL, NULL, 0}
     81 static const struct sstl_writer_create_args SSTL_WRITER_CREATE_ARGS_DEFAULT =
     82   SSTL_WRITER_CREATE_ARGS_DEFAULT__;
     83 
     84 struct sstl_facet {
     85   float normal[3];
     86   float vertices[3][3];
     87 };
     88 #define SSTL_FACET_NULL__ {0}
     89 static const struct sstl_facet SSTL_FACET_NULL = SSTL_FACET_NULL__;
     90 
     91 /* Forward declaration of external types */
     92 struct logger;
     93 struct mem_allocator;
     94 
     95 /* Forward declaration of opaque data types */
     96 struct sstl;
     97 struct sstl_writer;
     98 
     99 /*******************************************************************************
    100  * Star-STL API
    101  ******************************************************************************/
    102 BEGIN_DECLS
    103 
    104 SSTL_API res_T
    105 sstl_create
    106   (struct logger* logger, /* NULL <=> use default logger */
    107    struct mem_allocator* allocator, /* NULL <=> use default allocator */
    108    const int verbose, /* Verbosity level */
    109    struct sstl** sstl);
    110 
    111 SSTL_API res_T
    112 sstl_ref_get
    113   (struct sstl* sstl);
    114 
    115 SSTL_API res_T
    116 sstl_ref_put
    117   (struct sstl* sstl);
    118 
    119 /* The type of StL (ASCII or binary) is defined from the contents of the file.
    120  * The file must therefore be seekable, i.e. it cannot be a pipe, a FIFO or a
    121  * socket */
    122 SSTL_API res_T
    123 sstl_load
    124   (struct sstl* sstl,
    125    const char* filename);
    126 
    127 SSTL_API res_T
    128 sstl_load_ascii
    129   (struct sstl* sstl,
    130    const char* filename);
    131 
    132 SSTL_API res_T
    133 sstl_load_binary
    134   (struct sstl* sstl,
    135    const char* filename);
    136 
    137 /* The type of StL (Binary or ASCII) is defined from the contents of the file.
    138  * The file pointer must therefore be seekable, i.e. it cannot be a pipe, a FIFO
    139  * or a socket */
    140 SSTL_API res_T
    141 sstl_load_stream
    142   (struct sstl* sstl,
    143    FILE* stream,
    144    const char* stream_name);
    145 
    146 SSTL_API res_T
    147 sstl_load_stream_ascii
    148   (struct sstl* sstl,
    149    FILE* stream,
    150    const char* stream_name);
    151 
    152 SSTL_API res_T
    153 sstl_load_stream_binary
    154   (struct sstl* sstl,
    155    FILE* stream,
    156    const char* stream_name);
    157 
    158 /* The returned descriptor is valid until a new load process */
    159 SSTL_API res_T
    160 sstl_get_desc
    161   (struct sstl* sstl,
    162    struct sstl_desc* desc);
    163 
    164 /*******************************************************************************
    165  * Descriptor API. This is a set of help functions for retrieving mesh data from
    166  * their raw representation.
    167  ******************************************************************************/
    168 static INLINE res_T
    169 sstl_desc_get_triangle_ids
    170   (const struct sstl_desc* desc,
    171    const size_t itri,
    172    unsigned ids[3])
    173 {
    174   if(!desc || !ids || itri >= desc->triangles_count) return RES_BAD_ARG;
    175   ids[0] = desc->indices[itri*3/*#ids per triangle*/ + 0];
    176   ids[1] = desc->indices[itri*3/*#ids per triangle*/ + 1];
    177   ids[2] = desc->indices[itri*3/*#ids per triangle*/ + 2];
    178   return RES_OK;
    179 }
    180 
    181 static INLINE res_T
    182 sstl_desc_get_vertex_coords
    183   (const struct sstl_desc* desc,
    184    const size_t ivtx,
    185    float coords[3])
    186 {
    187   if(!desc || !coords || ivtx >= desc->vertices_count) return RES_BAD_ARG;
    188   coords[0] = desc->vertices[ivtx*3/*#coords per vertex*/ + 0];
    189   coords[1] = desc->vertices[ivtx*3/*#coords per vertex*/ + 1];
    190   coords[2] = desc->vertices[ivtx*3/*#coords per vertex*/ + 2];
    191   return RES_OK;
    192 }
    193 
    194 static INLINE res_T
    195 sstl_desc_get_facet
    196   (const struct sstl_desc* desc,
    197    const size_t itri,
    198    struct sstl_facet* facet)
    199 {
    200   unsigned ids[3] = {0,0,0};
    201 
    202   if(!desc || !facet || itri >= desc->triangles_count) return RES_BAD_ARG;
    203 
    204   #define CALL(Func) {res_T res; if((res = Func) != RES_OK) return res;} (void)0
    205   CALL(sstl_desc_get_triangle_ids(desc, itri, ids));
    206   CALL(sstl_desc_get_vertex_coords(desc, ids[0], facet->vertices[0]));
    207   CALL(sstl_desc_get_vertex_coords(desc, ids[1], facet->vertices[1]));
    208   CALL(sstl_desc_get_vertex_coords(desc, ids[2], facet->vertices[2]));
    209   #undef CALL
    210 
    211   return RES_OK;
    212 }
    213 
    214 /*******************************************************************************
    215  * Writer API
    216  ******************************************************************************/
    217 SSTL_API res_T
    218 sstl_writer_create
    219   (const struct sstl_writer_create_args* args,
    220    struct sstl_writer** writer);
    221 
    222 SSTL_API res_T
    223 sstl_writer_ref_get
    224   (struct sstl_writer* writer);
    225 
    226 SSTL_API res_T
    227 sstl_writer_ref_put
    228   (struct sstl_writer* writer);
    229 
    230 SSTL_API res_T
    231 sstl_write_facet
    232   (struct sstl_writer* writer,
    233    const struct sstl_facet* facet);
    234 
    235 END_DECLS
    236 
    237 #endif /* SSTL_H */