star-3dut

Generate meshes of simple geometric shapes
git clone git://git.meso-star.fr/star-3dut.git
Log | Files | Refs | README | LICENSE

s3dut.h (11549B)


      1 /* Copyright (C) 2016, 2017, 2020, 2021, 2023 |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 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 S3DUT_H
     17 #define S3DUT_H
     18 
     19 #include <rsys/rsys.h>
     20 
     21 /* Library symbol management */
     22 #if defined(S3DUT_SHARED_BUILD) /* Build shared library */
     23   #define S3DUT_API extern EXPORT_SYM
     24 #elif defined(S3DUT_STATIC_BUILD) /* Use/build static library */
     25   #define S3DUT_API extern LOCAL_SYM
     26 #else /* Use shared library */
     27   #define S3DUT_API extern IMPORT_SYM
     28 #endif
     29 
     30 /* Helper macro that asserts if the invocation of the s3dut function `Func'
     31  * returns an error. */
     32 #ifndef NDEBUG
     33   #define S3DUT(Func) ASSERT(s3dut_##Func == RES_OK)
     34 #else
     35   #define S3DUT(Func) s3dut_##Func
     36 #endif
     37 
     38 struct mem_allocator;
     39 struct s3dut_mesh;
     40 
     41 struct s3dut_mesh_data {
     42   const double* positions;
     43   const size_t* indices;
     44   size_t nvertices; /* # vertices */
     45   size_t nprimitives; /* # primitives */
     46 };
     47 
     48 /* Defines the radius 'r' with respect the an angle 'a':
     49  * r(a) = ( |cos(M*a/4)/A)|^N1 + |sin(M*a/4)/B|^N2 )^{-1/N0} */
     50 struct s3dut_super_formula {
     51   double A;
     52   double B;
     53   double M;
     54   double N0;
     55   double N1;
     56   double N2;
     57 };
     58 #define S3DUT_SUPER_FORMULA_NULL__ {0, 0, 0, 0, 0, 0}
     59 static const struct s3dut_super_formula S3DUT_SUPER_FORMULA_NULL =
     60   S3DUT_SUPER_FORMULA_NULL__;
     61 
     62 enum s3dut_cap_flag {
     63   S3DUT_CAP_NEG_Z = BIT(0),
     64   S3DUT_CAP_POS_Z = BIT(1)
     65 };
     66 
     67 BEGIN_DECLS
     68 
     69 /*******************************************************************************
     70  * Stard-3DUT API
     71  ******************************************************************************/
     72 S3DUT_API res_T
     73 s3dut_mesh_ref_get
     74   (struct s3dut_mesh* mesh);
     75 
     76 S3DUT_API res_T
     77 s3dut_mesh_ref_put
     78   (struct s3dut_mesh* mesh);
     79 
     80 S3DUT_API res_T
     81 s3dut_mesh_get_data
     82   (const struct s3dut_mesh* mesh,
     83    struct s3dut_mesh_data* data);
     84 
     85 /* Create a triangulated UV sphere centered in 0 discretized in `nslices'
     86  * around the Z axis and `nstacks' along the Z axis. Face vertices are CCW
     87  * ordered with respect to the sphere center, i.e. they are CW ordered from the
     88  * outside point of view. */
     89 S3DUT_API res_T
     90 s3dut_create_sphere
     91   (struct mem_allocator* allocator, /* May be NULL <=> use default allocator */
     92    const double radius, /* In ]0, INF) */
     93    const unsigned nslices, /* # subdivisions around Z axis in [3, INF) */
     94    const unsigned nstacks, /* # subdivisions along Z axis in [2, INF) */
     95    struct s3dut_mesh** sphere);
     96 
     97 /* Create a triangulated cylinder centered in 0 discretized in `nslices' around
     98  * the Z axis and `nstacks' along the Z axis. The top and the bottom ends of
     99  * the cylinder are closed with triangle fans whose center is on the Z axis.
    100  * Face vertices are CCW ordered with respect to the cylinder center, i.e. they
    101  * are CW ordered from the outside point of view. */
    102 S3DUT_API res_T
    103 s3dut_create_cylinder
    104   (struct mem_allocator* allocator, /* May be NULL <=> use default allocator */
    105    const double radius, /* In ]0, INF) */
    106    const double height, /* In ]0, INF) */
    107    const unsigned nslices, /* # subdivisions around Z axis in [3, INF) */
    108    const unsigned nstacks, /* # subdivision along Z axis in [1, INF) */
    109    struct s3dut_mesh** cylinder);
    110 
    111 /* Create a triangulated cylinder centered in 0 discretized in `nslices' around
    112  * the Z axis and `nstacks' along the Z axis. The top and the bottom ends of
    113  * the cylinder can be closed or not with a triangle fan whose center is on the
    114  * Z axis, according to the `cap_mask' argument. Face vertices are CCW ordered
    115  * with respect to the cylinder center, i.e. they are CW ordered from the
    116  * outside point of view.  Similar to s3dut_create_cylinder with the ability to
    117  * close ends or not. */
    118 S3DUT_API res_T
    119 s3dut_create_thin_cylinder
    120   (struct mem_allocator* allocator, /* May be NULL <=> use default allocator */
    121    const double radius, /* In ]0, INF) */
    122    const double height, /* In ]0, INF) */
    123    const unsigned nslices, /* # subdivisions around Z axis in [3, INF) */
    124    const unsigned nstacks, /* # subdivision along Z axis in [1, INF) */
    125    const int cap_mask, /* Combination of s3dut_cap_flag */
    126    struct s3dut_mesh** cylinder);
    127 
    128 /* Create a triangulated thick cylinder centered in 0 discretized in `nslices'
    129  * around the Z axis and `nstacks' along the Z axis, with walls of thickness
    130  * `thickness'. The top and the bottom of the cylinder can be closed or not,
    131  * according to the `cap_mask' argument. Closed ends are closed by a wall of
    132  * thickness `thickness' made of 2 triangle fans centered on the Z axis. Face
    133  * vertices are CCW ordered with respect to the walls' inside, i.e.  they are
    134  * CW ordered from any point of view outside of the walls. */
    135 S3DUT_API res_T
    136 s3dut_create_thick_cylinder
    137   (struct mem_allocator* allocator, /* May be NULL <=> use default allocator */
    138    const double radius, /* In ]thickness, INF); exterior radius */
    139    const double height, /* In ]min_height, INF); min_height = 0,
    140                            tickness or 2*thickness according to cap_mask */
    141    const double thickness, /* In ]0, INF) */
    142    const unsigned nslices, /* # subdivisions around Z axis in [3, INF) */
    143    const unsigned nstacks, /* # subdivision along Z axis in [1, INF) */
    144    const int cap_mask, /* Combination of s3dut_cap_flag */
    145    struct s3dut_mesh** cylinder);
    146 
    147 /* Create a triangulated cuboid centered in 0. Face vertices are CCW ordered
    148  * with respect to the cylinder center, i.e. they are CW ordered from the
    149  * outside point of view. */
    150 S3DUT_API res_T
    151 s3dut_create_cuboid
    152   (struct mem_allocator* allocator,
    153    const double width,
    154    const double height,
    155    const double depth,
    156    struct s3dut_mesh** cuboid);
    157 
    158 /* Create a triangulated UV hemisphere oriented wrt Z axis. It discretized in
    159  * `nslices' around the Z axis and `nstacks' along the Z axis. The back of the
    160  * hemisphere is not triangulated. Face vertices are CCW ordered with respect
    161  * to the hemisphere center, i.e. they are CW ordered from the outside point of
    162  * view. */
    163 S3DUT_API res_T
    164 s3dut_create_hemisphere
    165   (struct mem_allocator* allocator,
    166    const double radius,
    167    const unsigned nslices, /* # subdivisions around Z axis in [3, INF) */
    168    const unsigned nstacks, /* # subdivisions along Z axis int [2, INF) */
    169    struct s3dut_mesh** hemisphere);
    170 
    171 /* Create a triangulated UV sphere centered in 0 discretized in `nslices'
    172  * around the Z axis and `nstacks' along the Z axis. The top and the bottom of
    173  * the sphere can be truncated at some specified z, according to the `z_range'
    174  * parameter. If truncated, the top and the bottom of the sphere can be closed
    175  * with a triangle fan whose center is on the Z axis or not, according to the
    176  * `cap_mask' argument. Face vertices are CCW ordered with respect to the
    177  * sphere center, i.e. they are CW ordered from the outside point of view. */
    178 S3DUT_API res_T
    179 s3dut_create_truncated_sphere
    180   (struct mem_allocator* allocator, /* May be NULL <=> use default allocator */
    181    const double radius, /* In ]0, INF) */
    182    const unsigned nslices, /* # subdivisions around Z axis in [3, INF) */
    183    const unsigned nstacks, /* # subdivisions along Z axis in [2, INF) */
    184    const double z_range[2], /* Clamp the sphere to z_range. NULL <=> no clamp */
    185    const int cap_mask, /* Combination of s3dut_cap_flag. Ignored if no clamp */
    186    struct s3dut_mesh** sphere);
    187 
    188 /* Create a triangulated thick UV sphere centered in 0 discretized in `nslices'
    189  * around the Z axis and `nstacks' along the Z axis, with walls of thickness
    190  * `thickness'. The top and the bottom of the sphere can be truncated at some
    191  * specified z, according to the `z_range' parameter. If truncated, the top and
    192  * the bottom of the sphere can be closed or not, according to the `cap_mask'
    193  * argument. Closed ends are closed by a wall of thickness `thickness'
    194  * made of 2 triangle fans centered on the Z axis. Face vertices are CCW
    195  * ordered with respect to the walls' inside, i.e. they are CW ordered from any
    196  * point of view outside of the walls. */
    197 S3DUT_API res_T
    198 s3dut_create_thick_truncated_sphere
    199   (struct mem_allocator* allocator, /* May be NULL <=> use default allocator */
    200    const double radius, /* In ]thickness, INF); exterior radius */
    201    const double thickness, /* In ]0, INF) */
    202    const unsigned nslices, /* # subdivisions around Z axis in [3, INF) */
    203    const unsigned nstacks, /* # subdivisions along Z axis in [2, INF) */
    204    const double z_range[2], /* Clamp the sphere to z_range. NULL <=> no clamp */
    205    const int cap_mask, /* Combination of s3dut_cap_flag. Ignored if no clamp */
    206    struct s3dut_mesh** sphere);
    207 
    208 /* Create a triangulated super shape centered in 0 and discretized in `nslices'
    209  * around the Z axis and `nstacks' along the Z axis. Face vertices are CCW
    210  * ordered with respect to the center of the super shape.
    211  *
    212  * Assuming a point with the spherical coordinates {r, theta, phi} - with r the
    213  * radius of the sphere, theta in [-PI,2PI] and phi in [-PI/2, PI/2] - the
    214  * corresponding 3D coordinates onto the super shape is obtained by evaluating
    215  * the following relations:
    216  *    x = r0(theta)*cos(theta) * r1(phi)*cos(phi)
    217  *    y = r0(theta)*sin(theta) * r1(phi)*cos(phi)
    218  *    z = r1(phi)*sin(phi)
    219  * with r0 and r1 refering to the two super formulas. */
    220 S3DUT_API res_T
    221 s3dut_create_super_shape
    222   (struct mem_allocator* allocator, /* May be NULL <=> use default allocator */
    223    const struct s3dut_super_formula* formula0,
    224    const struct s3dut_super_formula* formula1,
    225    const double radius, /* In [0, INF) */
    226    const unsigned nslices, /* # subdivisions around Z axis in [3, INF) */
    227    const unsigned nstacks, /* # subdivisions along Z axis in [2, INF) */
    228    struct s3dut_mesh** super_shape);
    229 
    230 /* Create a triangulated super shape centered in 0 and discretized in `nslices'
    231  * around the Z axis and `nstacks' along the Z axis, with walls of thickness
    232  * `thickness'. Refer to the comments of the s3dut_create_super_shape function
    233  * for informations on super shape geometry. The top and the bottom of the
    234  * super shape can be truncated at some specified z, according to the `z_range'
    235  * parameter. If truncated, the top and the bottom of the super shape can be
    236  * closed with a triangle fan whose center is on the Z axis or not, according
    237  * to the `cap_mask' argument. Face vertices are CCW ordered with from the
    238  * inside point of view. */
    239 S3DUT_API res_T
    240 s3dut_create_thick_truncated_super_shape
    241   (struct mem_allocator* allocator, /* May be NULL <=> use default allocator */
    242    const struct s3dut_super_formula* formula0,
    243    const struct s3dut_super_formula* formula1,
    244    const double radius, /* In [0, INF) */
    245    const double thickness, /* In ]0, INF) */
    246    const unsigned nslices, /* # subdivisions around Z axis in [3, INF) */
    247    const unsigned nstacks, /* # subdivisions along Z axis in [2, INF) */
    248    const double z_range[2], /* Clamp the sphere to z_range. NULL <=> no clamp */
    249    const int cap_mask, /* Combination of s3dut_cap_flag. Ignored if no clamp */
    250    struct s3dut_mesh** super_shape);
    251 
    252 END_DECLS
    253 
    254 #endif /* S3DUT_H */