city_generator2

Generated conformal 3D meshes representing a city
git clone git://git.meso-star.fr/city_generator2.git
Log | Files | Refs | README | LICENSE

commit e660798b6a7edb3da15a3d536cd75a763c9241a8
parent 4521aafeb01cddae0dcb325174ea89d5d726396c
Author: Christophe Coustet <christophe.coustet@meso-star.com>
Date:   Mon,  2 Jan 2023 16:38:01 +0100

Improve function args checking

Diffstat:
Msrc/cg_constructive_mode_0.c | 79++++++++++++++++++++++++++++++++++++++++++++++++++++++-------------------------
Msrc/cg_constructive_mode_1.c | 139++++++++++++++++++++++++++-----------------------------------------------------
2 files changed, 100 insertions(+), 118 deletions(-)

diff --git a/src/cg_constructive_mode_0.c b/src/cg_constructive_mode_0.c @@ -34,6 +34,8 @@ build_floor_footprint { res_T res = RES_OK; size_t nverts; + + ASSERT(pg && footprint); ERR(scpr_polygon_get_vertices_count(pg, 0, &nverts)); ERR(scad_add_polygon(NULL, get_position_pg, pg, 0, nverts, footprint)); @@ -59,6 +61,8 @@ build_floor struct str name; int is_init = 0; + ASSERT(pg && b && floor); + data = (struct data_set_cmode_0*)b->data; e = data->floor; @@ -99,6 +103,8 @@ build_roof struct str name; int is_init = 0; + ASSERT(b && floor && roof); + str_init(NULL, &name); is_init = 1; if (prefix) { @@ -133,6 +139,8 @@ build_wall_footprint struct scad_geometry* polygon_int = NULL; size_t nverts, nverts_int; + ASSERT(pg && pg_int && footprint); + ERR(scpr_polygon_get_vertices_count(pg, 0, &nverts)); ERR(scad_add_polygon(NULL, get_position_pg, pg, 0, nverts, &polygon)); @@ -165,6 +173,8 @@ build_wall struct str name; int is_init = 0; + ASSERT(pg && pg_int && b && wall); + height = b->height; str_init(NULL, &name); @@ -205,6 +215,8 @@ build_cavity int is_init = 0; size_t nverts; + ASSERT(pg && b && cavity); + height = b->height; data = (struct data_set_cmode_0*)b->data; e = data->floor; @@ -242,6 +254,8 @@ build_connection struct str name; int is_init = 0; + ASSERT(allocator && cad); + cad->connection = MEM_CALLOC(allocator, 3, sizeof(struct scad_geometry*)); if(!cad->connection) { res = RES_MEM_ERR; @@ -258,10 +272,7 @@ build_connection cname = str_get(&name); } - ERR(scad_geometries_common_boundaries - (cname, - &cad->cavity, 1, - &cad->floor, 1, + ERR(scad_geometries_common_boundaries(cname, &cad->cavity, 1, &cad->floor, 1, &cad->connection[0])); /* cavity/wall connection */ @@ -271,10 +282,7 @@ build_connection cname = str_get(&name); } - ERR(scad_geometries_common_boundaries - (cname, - &cad->cavity, 1, - &cad->wall, 1, + ERR(scad_geometries_common_boundaries(cname, &cad->cavity, 1, &cad->wall, 1, &cad->connection[1])); /* cavity/roof connection */ @@ -284,10 +292,7 @@ build_connection cname = str_get(&name); } - ERR(scad_geometries_common_boundaries - (cname, - &cad->cavity, 1, - &cad->roof, 1, + ERR(scad_geometries_common_boundaries(cname, &cad->cavity, 1, &cad->roof, 1, &cad->connection[2])); exit: @@ -310,6 +315,8 @@ build_boundary struct str name; int is_init = 0; + ASSERT(allocator && cad); + list = MEM_ALLOC(allocator, 4 * sizeof(struct scad_geometry*)); if(!list) { res = RES_MEM_ERR; @@ -356,6 +363,8 @@ building_ground_connection struct str name; int is_init = 0; + ASSERT(pg && e > 0 && connection); + if (prefix) { str_init(NULL, &name); is_init = 1; @@ -454,19 +463,23 @@ build_cad_model0 void** cad) { res_T res = RES_OK; - double height = building->height; - struct scpr_polygon* pg = building->pg; + double height; + struct scpr_polygon* pg = NULL; struct scpr_polygon* pg_int = NULL; - struct data_set_cmode_0* data = (struct data_set_cmode_0 *)building->data; + struct data_set_cmode_0* data = NULL; struct data_cad_cmode_0* data_cad = NULL; double e_wall; const char* name; - if (!building) { + if (!building || !allocator || !cad) { res = RES_BAD_ARG; goto error; } + height = building->height; + pg = building->pg; + data = (struct data_set_cmode_0 *)building->data; + if (height <= 0 || data->wall <= 0 || data->floor <= 0) { res = RES_BAD_ARG; goto error; @@ -511,7 +524,7 @@ exit: *(struct data_cad_cmode_0**)cad = data_cad; return res; error: - CHK(RES_OK == release_cad_model0(allocator, data_cad)); + if(data_cad) CHK(RES_OK == release_cad_model0(allocator, data_cad)); data_cad = NULL; goto exit; } @@ -528,6 +541,11 @@ build_footprint_model0 double e_wall; (void)allocator; + if(!building || ! footprint) { + res = RES_BAD_ARG; + goto error; + } + e_wall = data->wall; ERR(building_ground_connection(NULL, pg, e_wall, footprint)); @@ -546,6 +564,11 @@ export_stl_model0 struct data_cad_cmode_0* data_cad = (struct data_cad_cmode_0*)cad; size_t i; + if(!cad) { + res = RES_BAD_ARG; + goto error; + } + /* floor export */ ERR(scad_stl_export(data_cad->floor, NULL, binary)); @@ -582,16 +605,22 @@ release_cad_model0 { res_T res = RES_OK; struct data_cad_cmode_0* data_cad = (struct data_cad_cmode_0 *)cad; + size_t i; - if(data_cad) { - size_t i; - if(data_cad->boundary) SCAD(geometry_delete(data_cad->boundary)); - for(i = 0; i < data_cad->n_connection; i++) { - SCAD(geometry_delete(data_cad->connection[i])); - } - MEM_RM(allocator, data_cad->connection); - MEM_RM(allocator, data_cad); + if(!allocator || ! cad) { + res = RES_BAD_ARG; + goto error; + } + + if(data_cad->boundary) SCAD(geometry_delete(data_cad->boundary)); + for(i = 0; i < data_cad->n_connection; i++) { + SCAD(geometry_delete(data_cad->connection[i])); } + MEM_RM(allocator, data_cad->connection); + MEM_RM(allocator, data_cad); +exit: return res; +error: + goto exit; } diff --git a/src/cg_constructive_mode_1.c b/src/cg_constructive_mode_1.c @@ -49,10 +49,7 @@ build_floor struct str name; int is_init = 0; - if (!pg || !data || !floor) { - res = RES_BAD_ARG; - goto error; - } + ASSERT(allocator && pg && data && floor); if (prefix) { str_init(allocator, &name); @@ -107,10 +104,7 @@ build_wall struct str name; int is_init = 0; - if (!pg || !data || !wall) { - res = RES_BAD_ARG; - goto error; - } + ASSERT(allocator && pg && data && wall); if (prefix) { str_init(allocator, &name); @@ -188,10 +182,7 @@ build_int_insulation struct str name; int is_init = 0; - if (!pg || !data || !insulation) { - res = RES_BAD_ARG; - goto error; - } + ASSERT(allocator && pg && data && insulation); if (prefix) { str_init(allocator, &name); @@ -215,23 +206,19 @@ build_int_insulation NULL, get_position_pg, pg_ext, 0, nverts, &footprint_ext)); ERR(scpr_polygon_get_vertices_count(pg_int, 0, &nverts)); - ERR(scad_add_polygon( - NULL, get_position_pg, pg_int, 0, nverts, &footprint_int)); + ERR(scad_add_polygon(NULL, get_position_pg, pg_int, 0, nverts, &footprint_int)); - ERR(scad_cut_geometries( - NULL, &footprint_ext, 1, &footprint_int, 1, &footprint)); + ERR(scad_cut_geometries(NULL, &footprint_ext, 1, &footprint_int, 1, &footprint)); d[2] = height - e_roof - attic - e_roof_insulation; ERR(scad_geometry_extrude(footprint, NULL, d, &geom)); if (inter_floor) { - ERR(scad_cut_geometries( - insulationname, &geom, 1, &inter_floor, 1, insulation)); + ERR(scad_cut_geometries(insulationname, &geom, 1, &inter_floor, 1, insulation)); } else { ERR(scad_geometry_copy(geom, insulationname, insulation)); } - exit: SCAD(geometry_delete(footprint)); SCAD(geometry_delete(footprint_int)); @@ -268,10 +255,7 @@ build_roof struct str name; int is_init = 0; - if (!pg || !data || !roof) { - res = RES_BAD_ARG; - goto error; - } + ASSERT(allocator && pg && data && roof); if (prefix) { str_init(allocator, &name); @@ -327,10 +311,7 @@ build_roof_insulation struct str name; int is_init = 0; - if (!pg || !data || !insulation) { - res = RES_BAD_ARG; - goto error; - } + ASSERT(allocator && pg && data && insulation); if (prefix) { str_init(allocator, &name); @@ -385,10 +366,7 @@ build_floor_insulation struct str name; int is_init = 0; - if (!pg || !data || !insulation) { - res = RES_BAD_ARG; - goto error; - } + ASSERT(allocator && pg && data && insulation); if (prefix) { str_init(allocator, &name); @@ -419,6 +397,7 @@ exit: error: goto exit; } + static res_T build_inter_floor (const char* prefix, @@ -448,10 +427,7 @@ build_inter_floor struct str name; int is_init = 0; - if (!pg || !data || !inter_floor) { - res = RES_BAD_ARG; - goto error; - } + ASSERT(allocator && pg && data && inter_floor); if (prefix) { str_init(allocator, &name); @@ -500,6 +476,7 @@ exit: error: goto exit; } + static res_T build_ext_insulation (const char* prefix, @@ -523,10 +500,7 @@ build_ext_insulation struct str name; int is_init = 0; - if (!pg || !data || !insulation) { - res = RES_BAD_ARG; - goto error; - } + ASSERT(allocator && pg && data && insulation); if (prefix) { str_init(allocator, &name); @@ -592,10 +566,7 @@ build_crawlspace struct str name; int is_init = 0; - if (!pg || !data || !crawlspace) { - res = RES_BAD_ARG; - goto error; - } + ASSERT(allocator && pg && data && crawlspace); if (prefix) { str_init(allocator, &name); @@ -654,10 +625,7 @@ build_habitable struct str name; int is_init = 0; - if (!pg || !data || !cavity) { - res = RES_BAD_ARG; - goto error; - } + ASSERT(allocator && pg && data && cavity); if (prefix) { str_init(allocator, &name); @@ -719,10 +687,7 @@ build_attic struct str name; int is_init = 0; - if (!pg || !data || !attic) { - res = RES_BAD_ARG; - goto error; - } + ASSERT(allocator && pg && data && attic); if (prefix) { str_init(allocator, &name); @@ -778,10 +743,7 @@ build_windows struct str gname; int is_init = 0; - if (!data || !data_cad) { - res = RES_BAD_ARG; - goto error; - } + ASSERT(allocator && data && data_cad); scale[0] = sqrt(data->glass_ratio); scale[1] = scale[0]; @@ -898,10 +860,7 @@ build_boundary struct str name; int is_init = 0; - if (!prefix || !data_cad || !boundary) { - res = RES_BAD_ARG; - goto error; - } + ASSERT(allocator && prefix && data_cad && boundary); str_init(allocator, &name); is_init = 1; @@ -987,10 +946,7 @@ build_connection struct str name; int is_init = 0; - if (!prefix || !data_cad || !connection) { - res = RES_BAD_ARG; - goto error; - } + ASSERT(allocator && prefix && data_cad && connection); str_init(allocator, &name); is_init = 1; @@ -1083,25 +1039,21 @@ error: static res_T build_footprint - (struct scpr_polygon* pg, struct scad_geometry** footprint) + (struct scpr_polygon* pg, + struct scad_geometry** footprint) { res_T res = RES_OK; size_t nverts = 0; - if (!pg || !footprint) { - res = RES_BAD_ARG; - goto error; - } + ASSERT(pg && footprint); ERR(scpr_polygon_get_vertices_count(pg, 0, &nverts)); - ERR(scad_add_polygon( - NULL, get_position_pg, pg, 0, nverts, footprint)); + ERR(scad_add_polygon(NULL, get_position_pg, pg, 0, nverts, footprint)); exit: return res; error: goto exit; - } static res_T @@ -1118,10 +1070,7 @@ build_fake_ground struct scad_geometry* footprint = NULL; struct scad_geometry* geom = NULL; - if (!cad || !pg || !ground ) { - res = RES_BAD_ARG; - goto error; - } + ASSERT(cad && pg && ground ); if (cad->foundation) sa_push(list, cad->foundation); if (cad->attic_cavity) sa_push(list, cad->attic_cavity); @@ -1148,7 +1097,6 @@ error: goto exit; } - static res_T building_ground_connection (const char* prefix, @@ -1164,10 +1112,7 @@ building_ground_connection struct scad_geometry* list_boundary = NULL; struct scad_geometry* footprint = NULL; - if (!prefix || !cad || !connection) { - res = RES_BAD_ARG; - goto error; - } + ASSERT(prefix && allocator && cad && connection); str_init(allocator, &name); is_init = 1; @@ -1274,7 +1219,7 @@ build_cad_model1 struct data_cad_cmode_1* data_cad = NULL; const char* name; - if (!building) { + if (!building || !allocator || !cad) { res = RES_BAD_ARG; goto error; } @@ -1379,7 +1324,7 @@ exit: *(struct data_cad_cmode_1**)cad = data_cad; return res; error: - CHK(RES_OK == release_cad_model1(allocator, data_cad)); + if(data_cad) CHK(RES_OK == release_cad_model1(allocator, data_cad)); data_cad = NULL; goto exit; } @@ -1415,6 +1360,11 @@ export_stl_model1 struct data_cad_cmode_1* data_cad = (struct data_cad_cmode_1 *)cad; size_t i = 0; + if (!cad) { + res = RES_BAD_ARG; + goto error; + } + /* floor export */ ERR(scad_stl_export(data_cad->floor, NULL, binary)); @@ -1498,19 +1448,22 @@ release_cad_model1 { res_T res = RES_OK; struct data_cad_cmode_1* data_cad = (struct data_cad_cmode_1 *)cad; + size_t i; - if(data_cad) { - size_t i; - for(i = 0; i < sa_size(data_cad->boundary); i++) { - ERR(scad_geometry_delete(data_cad->boundary[i])); - } - for(i = 0; i < sa_size(data_cad->connection); i++) { - ERR(scad_geometry_delete(data_cad->connection[i])); - } - sa_release(data_cad->boundary); - sa_release(data_cad->connection); - MEM_RM(allocator, data_cad); + if (!allocator || !cad) { + res = RES_BAD_ARG; + goto error; + } + + for(i = 0; i < sa_size(data_cad->boundary); i++) { + ERR(scad_geometry_delete(data_cad->boundary[i])); + } + for(i = 0; i < sa_size(data_cad->connection); i++) { + ERR(scad_geometry_delete(data_cad->connection[i])); } + sa_release(data_cad->boundary); + sa_release(data_cad->connection); + MEM_RM(allocator, data_cad); exit: return res;