star-3dut

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

s3dut_cuboid.c (2622B)


      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 #include "s3dut.h"
     17 #include "s3dut_mesh.h"
     18 
     19 /*******************************************************************************
     20  * Exported functions
     21  ******************************************************************************/
     22 res_T
     23 s3dut_create_cuboid
     24   (struct mem_allocator* allocator,
     25    const double width,
     26    const double height,
     27    const double depth,
     28    struct s3dut_mesh** mesh)
     29 {
     30   struct s3dut_mesh* cuboid = NULL;
     31   double x, y, z;
     32   double* coords;
     33   size_t* ids;
     34   size_t i;
     35   res_T res = RES_OK;
     36 
     37   if(width <= 0 || height <= 0 || depth <= 0 || !mesh) {
     38     res = RES_BAD_ARG;
     39     goto error;
     40   }
     41 
     42   res = mesh_create(allocator, S3DUT_MESH_CUBOID, 8, 12, &cuboid);
     43   if(res != RES_OK) goto error;
     44 
     45   coords = darray_double_data_get(&cuboid->coords);
     46   ids = darray_size_t_data_get(&cuboid->ids);
     47   x = width * 0.5;
     48   y = height * 0.5;
     49   z = depth * 0.5;
     50 
     51   i = 0;
     52   coords[i++]=-x; coords[i++]=-y; coords[i++]=-z;
     53   coords[i++]= x; coords[i++]=-y; coords[i++]=-z;
     54   coords[i++]= x; coords[i++]= y; coords[i++]=-z;
     55   coords[i++]=-x; coords[i++]= y; coords[i++]=-z;
     56   coords[i++]=-x; coords[i++]=-y; coords[i++]= z;
     57   coords[i++]= x; coords[i++]=-y; coords[i++]= z;
     58   coords[i++]= x; coords[i++]= y; coords[i++]= z;
     59   coords[i++]=-x; coords[i++]= y; coords[i++]= z;
     60 
     61   i = 0;
     62   ids[i++]=0; ids[i++]=2; ids[i++]=3;
     63   ids[i++]=0; ids[i++]=1; ids[i++]=2;
     64   ids[i++]=0; ids[i++]=3; ids[i++]=7;
     65   ids[i++]=0; ids[i++]=7; ids[i++]=4;
     66   ids[i++]=0; ids[i++]=4; ids[i++]=1;
     67   ids[i++]=4; ids[i++]=5; ids[i++]=1;
     68   ids[i++]=5; ids[i++]=6; ids[i++]=1;
     69   ids[i++]=1; ids[i++]=6; ids[i++]=2;
     70   ids[i++]=2; ids[i++]=6; ids[i++]=7;
     71   ids[i++]=2; ids[i++]=7; ids[i++]=3;
     72   ids[i++]=7; ids[i++]=6; ids[i++]=4;
     73   ids[i++]=6; ids[i++]=5; ids[i++]=4;
     74 
     75 exit:
     76   if(mesh) *mesh = cuboid;
     77   return res;
     78 error:
     79   if(cuboid) {
     80     S3DUT(mesh_ref_put(cuboid));
     81     cuboid = NULL;
     82   }
     83   goto exit;
     84 }
     85