star-stl

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

sstl_c.h (3433B)


      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_C_H
     17 #define SSTL_C_H
     18 
     19 #include "sstl.h"
     20 
     21 #include <rsys/float3.h>
     22 #include <rsys/hash_table.h>
     23 #include <rsys/logger.h>
     24 #include <rsys/ref_count.h>
     25 #include <rsys/str.h>
     26 #include <rsys/stretchy_array.h>
     27 
     28 #include <errno.h>
     29 #include <stdio.h>
     30 
     31 /* Helper macros for logging */
     32 #define LOG__(Dev, Lvl, Type, ...) { \
     33   if ((Dev)->verbose >= (Lvl)) \
     34     logger_print((Dev)->logger, Type, __VA_ARGS__); \
     35 } (void)0
     36 #define ERROR(Dev, ...) LOG__(Dev, 1, LOG_ERROR,   "error: "__VA_ARGS__)
     37 #define WARN(Dev, ...) LOG__(Dev, 2, LOG_WARNING, "warning: "__VA_ARGS__)
     38 #define INFO(Dev, ...) LOG__(Dev, 3, LOG_OUTPUT,  __VA_ARGS__)
     39 
     40 struct vertex { float xyz[3]; };
     41 
     42 static INLINE char
     43 eq_vertex(const struct vertex* a, const struct vertex* b)
     44 {
     45   return (char)
     46   (  a->xyz[0] == b->xyz[0]
     47   && a->xyz[1] == b->xyz[1]
     48   && a->xyz[2] == b->xyz[2]);
     49 }
     50 
     51 /* Declare the hash table that map a vertex to its index */
     52 #define HTABLE_NAME vertex
     53 #define HTABLE_DATA unsigned
     54 #define HTABLE_KEY struct vertex
     55 #define HTABLE_KEY_FUNCTOR_EQ eq_vertex
     56 #include <rsys/hash_table.h>
     57 
     58 /* Forward declarations */
     59 struct logger;
     60 struct mem_allocator;
     61 
     62 struct sstl {
     63   struct str filename;
     64   struct str name;
     65   enum sstl_type type;
     66 
     67   /* Temporary structure used to map a vertex to its id */
     68   struct htable_vertex vertex2id;
     69 
     70   float* normals;
     71   float* vertices;
     72   unsigned* indices;
     73 
     74   struct logger* logger;
     75   struct mem_allocator* allocator;
     76   int verbose;
     77   ref_T ref;
     78 };
     79 
     80 static INLINE void
     81 clear(struct sstl* sstl)
     82 {
     83   ASSERT(sstl);
     84   str_clear(&sstl->name);
     85   sa_release(sstl->indices);
     86   sa_release(sstl->vertices);
     87   sa_release(sstl->normals);
     88   sstl->indices = NULL;
     89   sstl->vertices = NULL;
     90   sstl->normals = NULL;
     91   sstl->type = SSTL_NONE__;
     92   htable_vertex_clear(&sstl->vertex2id);
     93 }
     94 
     95 static INLINE int
     96 file_is_seekable(FILE* fp)
     97 {
     98   ASSERT(fp);
     99   if(fseek(fp, 0, SEEK_CUR) >= 0) {
    100     return 1; /* File is seekable */
    101   } else {
    102     CHK(errno == ESPIPE);
    103     return 0; /* File is not seekable */
    104   }
    105 }
    106 
    107 static INLINE float*
    108 calculate_normal
    109   (float N[3],
    110    const float v0[3],
    111    const float v1[3],
    112    const float v2[3])
    113 {
    114   float E0[3], E1[3];
    115   ASSERT(N && v0 && v1 && v2);
    116 
    117   /* Vertices are CCW and the normal follows the right handed rule */
    118   f3_sub(E0, v1, v0);
    119   f3_sub(E1, v2, v0);
    120   f3_cross(N, E0, E1);
    121   f3_normalize(N, N);
    122 
    123   return N;
    124 }
    125 
    126 extern LOCAL_SYM res_T
    127 load_stream_ascii
    128   (struct sstl* sstl,
    129    FILE* stream,
    130    const char* stream_name);
    131 
    132 extern LOCAL_SYM res_T
    133 load_stream_binary
    134   (struct sstl* sstl,
    135    FILE* stream,
    136    const char* stream_name);
    137 
    138 extern LOCAL_SYM res_T
    139 register_vertex
    140   (struct sstl* sstl,
    141    const float v[3]);
    142 
    143 #endif /* SSTL_C_H */