commit 5b64cfdab945014ce0dd7b802b8dd133e778b3dc
parent 1c7e450338594f6c80f8f5757a3c0164a51aa314
Author: Christophe Coustet <christophe.coustet@meso-star.com>
Date: Fri, 28 Oct 2022 14:23:05 +0200
First batch of code cleanning and apdaptation to dependencies changes
Diffstat:
13 files changed, 259 insertions(+), 363 deletions(-)
diff --git a/cmake/CMakeLists.txt b/cmake/CMakeLists.txt
@@ -56,10 +56,11 @@ set(CG2_FILES_SRC
cg_parsing.c)
set(CG2_FILES_INC
+ cg.h
cg_city.h
cg_building.h
cg_building_model0.h
- cg_ground.h
+ cg_ground.h
cg_args.h
cg_parsing.h)
diff --git a/src/cg_args.c b/src/cg_args.c
@@ -17,12 +17,13 @@
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>. */
-#include <getopt.h>
-#include <rsys/rsys.h>
-
#include "cg_args.h"
+#include "cg.h"
-#define ERR(Expr) if((res = (Expr)) != RES_OK) goto error; else (void)0
+#include <rsys/rsys.h>
+#include <rsys/logger.h>
+
+#include <getopt.h>
res_T
parse_args(struct logger* logger, int argc, char** argv, struct args* args)
@@ -44,21 +45,14 @@ parse_args(struct logger* logger, int argc, char** argv, struct args* args)
while((opt = getopt(argc, argv, option_list)) != -1) {
switch (opt) {
case 'a':
- {
args->binary_export = 0;
break;
- }
- case 'b':
- {
+ case 'b':
str_set(&args->building_model_file, optarg);
break;
- }
-
- case 'c':
- {
+ case 'c':
str_set(&args->city_model_file, optarg);
break;
- }
}
}
@@ -67,14 +61,14 @@ parse_args(struct logger* logger, int argc, char** argv, struct args* args)
"Missing mandatory argument: -b <building_model_file>\n"));
res = RES_BAD_ARG;
goto error;
- }
+ }
if (str_is_empty(&args->city_model_file)) {
ERR(logger_print(logger, LOG_ERROR,
"Missing mandatory argument: -c <city_model_file>\n"));
res = RES_BAD_ARG;
goto error;
- }
+ }
exit:
return res;
@@ -85,7 +79,7 @@ error:
}
res_T
-release_args(struct args* args)
+release_args(struct args* args)
{
res_T res = RES_OK;
diff --git a/src/cg_args.h b/src/cg_args.h
@@ -17,13 +17,15 @@
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>. */
+#ifndef PARSE_ARGS_H
+#define PARSE_ARGS_H
+
#include <rsys/rsys.h>
#include <rsys/str.h>
-#include <rsys/logger.h>
-#ifndef PARSE_ARGS_H
-#define PARSE_ARGS_H
+struct logger;
+struct args;
struct args {
struct str city_model_file;
@@ -31,7 +33,6 @@ struct args {
int binary_export;
};
-
res_T
parse_args(struct logger* logger, int argc, char** argv, struct args* args);
diff --git a/src/cg_building.h b/src/cg_building.h
@@ -20,23 +20,22 @@
#ifndef BUILDING_H
#define BUILDING_H
-#include <rsys/rsys.h>
-#include <rsys/str.h>
-#include <star/scad.h>
-#include <star/scpr.h>
+#include <rsys/rsys.h>
+#include <rsys/str.h>
#include <rsys/hash_table.h>
-enum model {
+struct scpr_polygon;
+struct scad_geometry;
+
+enum model_type {
model0,
MODEL_COUNT_
};
-static char const* model_str [] = {
- "model0"
-};
+extern char const* model_str[1];
struct building_params {
- enum model model;
+ enum model_type model;
struct str name;
void* data;
};
@@ -54,7 +53,7 @@ hash_str(const struct str* a)
return hash_fnv32(str_cget(a), str_len(a));
}
-#define HTABLE_NAME building_params
+#define HTABLE_NAME building_params
#define HTABLE_DATA struct building_params
#define HTABLE_KEY struct str
#define HTABLE_KEY_FUNCTOR_INIT str_init
@@ -67,19 +66,19 @@ hash_str(const struct str* a)
/* the specific building model functors headers must be included here */
-#include "cg_building_model0.h"
+#include "cg_building_model0.h"
struct building {
/* generic building data */
size_t id;
- enum model model;
+ enum model_type model;
struct str* data_name;
double height;
- struct scpr_polygon* pg;
-
- /* specific data depending model */
- void* data;
- void* data_cad;
+ struct scpr_polygon* pg;
+
+ /* specific data depending model */
+ void* data;
+ void* data_cad;
/* functors depending model */
res_T (*init)
diff --git a/src/cg_building_model0.c b/src/cg_building_model0.c
@@ -17,64 +17,32 @@
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>. */
-#include <rsys/str.h>
-
+#include "cg.h"
#include "cg_building.h"
-#define ERR(Expr) if((res = (Expr)) != RES_OK) goto error; else (void)0
-
+#include <rsys/str.h>
+#include <star/scad.h>
+#include <star/scpr.h>
-static res_T
-polygon_to_xy
- (const struct scpr_polygon* pg, double** x, double** y, size_t* n)
+static void get_position_pg
+ (const size_t ivert, double pos[2], void* ctx)
{
- res_T res = RES_OK;
- double* xx;
- double* yy;
- size_t i;
-
- ERR(scpr_polygon_get_vertices_count(pg, 0, n));
- xx = (double*) malloc(*n * sizeof(double));
- yy = (double*) malloc(*n * sizeof(double));
-
- for (i=0; i<*n; ++i) {
- double pos[2];
- ERR(scpr_polygon_get_position(pg, 0, i, pos));
- xx[i] = pos[0];
- yy[i] = pos[1];
- }
-
- *x = xx;
- *y = yy;
-exit:
- return res;
-error:
- goto exit;
+ struct scpr_polygon* pg = ctx;
+ ASSERT(pos && pg);
+ CHK(scpr_polygon_get_position(pg, 0, ivert, pos) == RES_OK);
}
static res_T
build_floor_footprint
- (const struct scpr_polygon* pg,
- struct scad_geometry** footprint)
+ (struct scpr_polygon* pg,
+ struct scad_geometry** footprint)
{
res_T res = RES_OK;
- double* x = NULL;
- double* y = NULL;
size_t nverts;
-
- ERR(polygon_to_xy(pg, &x, &y, &nverts));
-
- ERR(scad_add_polygon
- (NULL, /* Can be NULL */
- x,
- y,
- 0,
- nverts, /* size of x and y arrays */
- footprint));
+ ERR(scpr_polygon_get_vertices_count(pg, 0, &nverts));
+ ERR(scad_add_polygon(NULL, get_position_pg, pg, 0, nverts, footprint));
exit:
- free(x);
- free(y);
return res;
error:
goto exit;
@@ -82,20 +50,20 @@ error:
static res_T
build_floor
-(const char* prefix,
- const struct scpr_polygon* pg,
- struct building* b,
- struct scad_geometry** floor)
+ (const char* prefix,
+ struct scpr_polygon* pg,
+ struct building* b,
+ struct scad_geometry** floor)
{
res_T res = RES_OK;
double e;
struct data_model0* data;
- struct scad_geometry* footprint;
+ struct scad_geometry* footprint = NULL;
double d[3] = {0, 0, 0};
char* floorname = NULL;
struct str name;
int is_init = 0;
-
+
data = (struct data_model0*)b->data;
e = data->floor;
@@ -106,15 +74,11 @@ build_floor
ERR(str_append(&name, "_floor"));
floorname = str_get(&name);
}
-
+
ERR(build_floor_footprint(pg, &footprint));
d[2] = e;
- ERR(scad_geometry_extrude
- (footprint,
- floorname,
- d,
- floor));
+ ERR(scad_geometry_extrude(footprint, floorname, d, floor));
exit:
scad_geometry_delete(footprint);
@@ -148,7 +112,7 @@ build_roof
ERR(str_append(&name, "_roof"));
roofname = str_get(&name);
}
-
+
height = b->height;
data = (struct data_model0*)b->data;
/*data_cad = (struct data_cad_model0*)b->data_cad;*/
@@ -167,54 +131,27 @@ error:
static res_T
build_wall_footprint
- (const struct scpr_polygon* pg,
- const struct scpr_polygon* pg_int,
+ (struct scpr_polygon* pg,
+ struct scpr_polygon* pg_int,
struct scad_geometry** footprint)
{
res_T res = RES_OK;
/*struct data_cad_model0* data_cad;*/
- struct scad_geometry* polygon;
- struct scad_geometry* polygon_int;
- double* x = NULL;
- double* y = NULL;
- double* x_int = NULL;
- double* y_int = NULL;
+ struct scad_geometry* polygon = NULL;
+ struct scad_geometry* polygon_int = NULL;
size_t nverts, nverts_int;
- ERR(polygon_to_xy(pg, &x, &y, &nverts));
-
- ERR(scad_add_polygon
- (NULL,
- x,
- y,
- 0,
- nverts,
- &polygon));
-
- ERR(polygon_to_xy(pg_int, &x_int, &y_int, &nverts_int));
- ERR(scad_add_polygon
- (NULL,
- x_int,
- y_int,
- 0,
- nverts_int,
- &polygon_int));
+ ERR(scpr_polygon_get_vertices_count(pg, 0, &nverts));
+ ERR(scad_add_polygon(NULL, get_position_pg, pg, 0, nverts, &polygon));
+ ERR(scpr_polygon_get_vertices_count(pg_int, 0, &nverts_int));
+ ERR(scad_add_polygon(NULL, get_position_pg, pg_int, 0, nverts_int, &polygon_int));
- ERR(scad_cut_geometries
- (NULL,
- &polygon, 1,
- &polygon_int, 1,
- footprint));
-
+ ERR(scad_cut_geometries(NULL, &polygon, 1, &polygon_int, 1, footprint));
exit:
- free(x);
- free(y);
- free(x_int);
- free(y_int);
- scad_geometry_delete(polygon);
- scad_geometry_delete(polygon_int);
+ if(polygon) scad_geometry_delete(polygon);
+ if(polygon_int) scad_geometry_delete(polygon_int);
return res;
error:
goto exit;
@@ -223,19 +160,19 @@ error:
static res_T
build_wall
(const char* prefix,
- const struct scpr_polygon* pg,
- const struct scpr_polygon* pg_int,
+ struct scpr_polygon* pg,
+ struct scpr_polygon* pg_int,
struct building* b,
struct scad_geometry** wall)
{
res_T res = RES_OK;
double height;
- struct scad_geometry* footprint;
+ struct scad_geometry* footprint = NULL;
double d[3] = {0, 0, 0};
char* wallname = NULL;
struct str name;
int is_init = 0;
-
+
height = b->height;
str_init(NULL, &name);
@@ -249,15 +186,10 @@ build_wall
ERR(build_wall_footprint(pg, pg_int, &footprint));
d[2] = height;
- ERR(scad_geometry_extrude
- (footprint,
- wallname,
- d,
- wall));
-
+ ERR(scad_geometry_extrude(footprint, wallname, d, wall));
exit:
- scad_geometry_delete(footprint);
+ if(footprint) scad_geometry_delete(footprint);
if (is_init) str_release(&name);
return res;
error:
@@ -267,7 +199,7 @@ error:
static res_T
build_cavity
(const char* prefix,
- const struct scpr_polygon* pg,
+ struct scpr_polygon* pg,
const struct building* b,
struct scad_geometry** cavity)
{
@@ -275,14 +207,12 @@ build_cavity
double e, height;
struct data_model0* data;
double d[3] = {0, 0, 0};
- struct scad_geometry* polygon;
+ struct scad_geometry* polygon = NULL;
char* cavityname = NULL;
struct str name;
int is_init = 0;
- double* x = NULL;
- double* y = NULL;
size_t nverts;
-
+
height = b->height;
data = (struct data_model0*)b->data;
e = data->floor;
@@ -295,26 +225,14 @@ build_cavity
cavityname = str_get(&name);
}
- ERR(polygon_to_xy(pg, &x, &y, &nverts));
-
- ERR(scad_add_polygon
- (NULL, /* Can be NULL */
- x,
- y,
- e,
- nverts, /* size of x and y arrays */
- &polygon));
+ ERR(scpr_polygon_get_vertices_count(pg, 0, &nverts));
+ ERR(scad_add_polygon(NULL, get_position_pg, pg, e, nverts, &polygon));
d[2] = height - e;
- ERR(scad_geometry_extrude
- (polygon,
- cavityname,
- d,
- cavity));
+ ERR(scad_geometry_extrude(polygon, cavityname, d, cavity));
+
exit:
- free(x);
- free(y);
- scad_geometry_delete(polygon);
+ if(polygon) scad_geometry_delete(polygon);
if (is_init) str_release(&name);
return res;
error:
@@ -343,12 +261,12 @@ build_connection
cname = str_get(&name);
}
- ERR(scad_geometries_common_boundaries
+ ERR(scad_geometries_common_boundaries
(cname,
&cad->cavity, 1,
&cad->floor, 1,
&cad->connection[0]));
-
+
/* cavity/wall connection */
if (prefix) {
ERR(str_set(&name, prefix));
@@ -356,12 +274,12 @@ build_connection
cname = str_get(&name);
}
- ERR(scad_geometries_common_boundaries
+ ERR(scad_geometries_common_boundaries
(cname,
&cad->cavity, 1,
&cad->wall, 1,
&cad->connection[1]));
-
+
/* cavity/roof connection */
if (prefix) {
ERR(str_set(&name, prefix));
@@ -369,7 +287,7 @@ build_connection
cname = str_get(&name);
}
- ERR(scad_geometries_common_boundaries
+ ERR(scad_geometries_common_boundaries
(cname,
&cad->cavity, 1,
&cad->roof, 1,
@@ -390,30 +308,16 @@ build_ground
res_T res = RES_OK;
struct scad_geometry* footprint = NULL;
double dir[3] = {0, 0, -1};
- double* x = NULL;
- double* y = NULL;
size_t nverts;
-
- ERR(polygon_to_xy(pg, &x, &y, &nverts));
-
- ERR(scad_add_polygon
- (NULL,
- x,
- y,
- 0,
- nverts,
- &footprint));
- ERR(scad_geometry_extrude
- (footprint,
- NULL,
- dir,
- ground));
+
+ ERR(scpr_polygon_get_vertices_count(pg, 0, &nverts));
+ ERR(scad_add_polygon(NULL, get_position_pg, pg, 0, nverts, &footprint));
+
+ ERR(scad_geometry_extrude(footprint, NULL, dir, ground));
exit:
- free(x);
- free(y);
- scad_geometry_delete(footprint);
+ if(footprint) scad_geometry_delete(footprint);
return res;
error:
goto exit;
@@ -425,9 +329,9 @@ build_boundary
struct data_cad_model0* cad)
{
res_T res = RES_OK;
- struct scad_geometry** list;
- struct scad_geometry* boundary;
- struct scad_geometry* footprint;
+ struct scad_geometry** list = NULL;
+ struct scad_geometry* boundary = NULL;
+ struct scad_geometry* footprint = NULL;
char* cname = NULL;
struct str name;
int is_init = 0;
@@ -446,26 +350,16 @@ build_boundary
cname = str_get(&name);
}
- ERR(scad_geometry_boundary
- (NULL,
- list, 4,
- &boundary));
+ ERR(scad_geometry_boundary(NULL, list, 4, &boundary));
- ERR(scad_geometries_common_boundaries
- (NULL,
- list, 4,
- &cad->ground, 1,
+ ERR(scad_geometries_common_boundaries(NULL, list, 4, &cad->ground, 1,
&footprint));
- ERR(scad_cut_geometries
- (cname,
- &boundary, 1,
- &footprint, 1,
- &cad->boundary));
+ ERR(scad_cut_geometries(cname, &boundary, 1, &footprint, 1, &cad->boundary));
exit:
- scad_geometry_delete(boundary);
- scad_geometry_delete(footprint);
+ if(boundary) scad_geometry_delete(boundary);
+ if(footprint) scad_geometry_delete(footprint);
if (list) free(list);
if (is_init) str_release(&name);
return res;
@@ -483,14 +377,14 @@ init_model0
{
res_T res = RES_OK;
struct data_model0 data;
- struct building_params* params;
+ struct building_params* params;
params = htable_building_params_find(htparams, building->data_name);
if (params == NULL) {
res = RES_BAD_ARG;
goto error;
}
-
+
if (params->model != building->model) {
res = RES_BAD_ARG;
goto error;
@@ -515,29 +409,29 @@ error:
}
res_T
-build_cad_model0(struct building* building)
+build_cad_model0(struct building* building)
{
res_T res = RES_OK;
size_t id = building->id;
- enum model model = building->model;
+ enum model_type model = building->model;
double height = building->height;
struct scpr_polygon* pg = building->pg;
- struct scpr_polygon* pg_int;
+ struct scpr_polygon* pg_int = NULL;
struct data_model0* data = (struct data_model0 *)building->data;
struct data_cad_model0* data_cad;
- double e_wall;
+ double e_wall;
struct str prefix;
int is_init = 0;
-
+
if (!building) {
res = RES_BAD_ARG;
goto error;
- }
+ }
if (height <= 0 || data->wall <= 0 || data->floor <= 0) {
res = RES_BAD_ARG;
goto error;
- }
+ }
str_init(NULL, &prefix);
is_init = 1;
@@ -549,8 +443,7 @@ build_cad_model0(struct building* building)
building->data_cad = (struct data_cad_model0*)data_cad;
e_wall = data->wall;
- ERR(scpr_polygon_create(NULL, &pg_int));
- ERR(scpr_polygon_copy(pg, pg_int));
+ ERR(scpr_polygon_create_copy(NULL, pg, &pg_int));
ERR(scpr_offset_polygon(pg_int, -e_wall, SCPR_JOIN_MITER));
/* build floor with pg_int */
@@ -577,11 +470,11 @@ build_cad_model0(struct building* building)
/* build connection cavity/floor */
ERR(build_connection(str_cget(&prefix), building->data_cad));
-
+
exit:
if (is_init) str_release(&prefix);
- scpr_polygon_ref_put(pg_int);
+ if(pg_int) scpr_polygon_ref_put(pg_int);
return res;
error:
goto exit;
@@ -589,34 +482,29 @@ error:
res_T
build_footprint_model0
-(struct building* building,
- struct scad_geometry** footprint)
+ (struct building* building,
+ struct scad_geometry** footprint)
{
res_T res = RES_OK;
struct scpr_polygon* pg = building->pg;
- struct scpr_polygon* pg_int;
+ struct scpr_polygon* pg_int = NULL;
struct data_model0* data = (struct data_model0 *)building->data;
double e_wall;
struct scad_geometry* geom[2];
e_wall = data->wall;
- ERR(scpr_polygon_create(NULL, &pg_int));
- ERR(scpr_polygon_copy(pg, pg_int));
+ ERR(scpr_polygon_create_copy(NULL, pg, &pg_int));
ERR(scpr_offset_polygon(pg_int, -e_wall, SCPR_JOIN_MITER));
-
+
ERR(build_wall_footprint(pg, pg_int, &geom[0]));
ERR(build_floor_footprint(pg, &geom[1]));
- ERR(scad_fragment_geometries
- (NULL,
- &geom[0], 1,
- &geom[1], 1,
- footprint));
+ ERR(scad_fragment_geometries(NULL, &geom[0], 1, &geom[1], 1, footprint));
exit:
scad_geometry_delete(geom[0]);
scad_geometry_delete(geom[1]);
- scpr_polygon_ref_put(pg_int);
+ if(pg_int) scpr_polygon_ref_put(pg_int);
return res;
error:
goto exit;
@@ -624,31 +512,31 @@ error:
res_T
export_stl_model0
-(const struct building* building, const int binary)
+ (const struct building* building, const int binary)
{
res_T res = RES_OK;
struct data_cad_model0* data_cad = (struct data_cad_model0 *)building->data_cad;
size_t i;
/* floor export */
- ERR(scad_stl_export(data_cad->floor, NULL, binary));
+ ERR(scad_stl_export(data_cad->floor, NULL, binary));
/* roof export */
- ERR(scad_stl_export(data_cad->roof, NULL, binary));
+ ERR(scad_stl_export(data_cad->roof, NULL, binary));
/* wall export */
- ERR(scad_stl_export(data_cad->wall, NULL, binary));
+ ERR(scad_stl_export(data_cad->wall, NULL, binary));
/* cavity export */
- ERR(scad_stl_export(data_cad->cavity, NULL, binary));
+ ERR(scad_stl_export(data_cad->cavity, NULL, binary));
/* connection export */
for (i=0; i<data_cad->n_connection; ++i) {
- ERR(scad_stl_export(data_cad->connection[i], NULL, binary));
- }
+ ERR(scad_stl_export(data_cad->connection[i], NULL, binary));
+ }
/* boundary export */
- ERR(scad_stl_export(data_cad->boundary, NULL, binary));
+ ERR(scad_stl_export(data_cad->boundary, NULL, binary));
exit:
return res;
@@ -658,17 +546,17 @@ error:
res_T
release_model0
-(struct building* building)
+ (struct building* building)
{
res_T res = RES_OK;
struct data_model0* data = (struct data_model0 *)building->data;
struct data_cad_model0* data_cad = (struct data_cad_model0 *)building->data_cad;
-
+
scpr_polygon_ref_put(building->pg);
-
+
str_release(building->data_name);
-
+
if (data_cad->connection) free(data_cad->connection);
if (data) free(data);
if (data_cad) free(data_cad);
diff --git a/src/cg_building_model0.h b/src/cg_building_model0.h
@@ -17,30 +17,31 @@
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>. */
-#include <rsys/rsys.h>
-#include <star/scad.h>
-
#ifndef BUILDING_MODEL0_H
#define BUILDING_MODEL0_H
+#include <rsys/rsys.h>
+
+struct scad_geometry;
+
struct building;
struct htable_building_params;
struct building_params;
/* specific data for model 0 */
-struct data_model0 {
+struct data_model0 {
double wall; /* wall thickness */
double floor; /* floor thickness */
};
-struct data_cad_model0 {
- struct scad_geometry* wall;
- struct scad_geometry* roof;
- struct scad_geometry* floor;
- struct scad_geometry* cavity;
- struct scad_geometry* ground;
- struct scad_geometry* boundary;
- struct scad_geometry** connection;
+struct data_cad_model0 {
+ struct scad_geometry* wall;
+ struct scad_geometry* roof;
+ struct scad_geometry* floor;
+ struct scad_geometry* cavity;
+ struct scad_geometry* ground;
+ struct scad_geometry* boundary;
+ struct scad_geometry** connection;
size_t n_connection;
};
diff --git a/src/cg_city.c b/src/cg_city.c
@@ -23,13 +23,15 @@
#include <rsys/cstr.h>
#include <rsys/stretchy_array.h>
+#include "cg.h"
#include "cg_city.h"
#include "cg_parsing.h"
-
+#include "cg_building.h"
+#include "cg_args.h"
res_T
city_init(struct logger* logger, struct city* city, struct args* args)
-{
+{
res_T res = RES_OK;
size_t i=0;
struct txtrdr* reader = NULL;
@@ -50,10 +52,10 @@ city_init(struct logger* logger, struct city* city, struct args* args)
default:
res = RES_BAD_ARG;
goto error;
- }
+ }
ERR(city->building[i].init(&city->building[i], &ht_params));
}
-
+
exit:
txtrdr_ref_put(reader);
htable_building_params_release(&ht_params);
@@ -64,7 +66,7 @@ error:
res_T
city_release(struct city* city)
-{
+{
res_T res = RES_OK;
size_t i,n;
@@ -86,7 +88,7 @@ error:
res_T
city_cad_build(struct logger* logger, struct city* city)
-{
+{
res_T res = RES_OK;
size_t i,n;
@@ -101,7 +103,7 @@ city_cad_build(struct logger* logger, struct city* city)
ERR(city->building[i].export_stl(&city->building[i], city->binary_export));
ERR(scad_scene_clear());
}
-
+
exit:
scad_finalize();
return res;
@@ -111,7 +113,7 @@ error:
res_T
city_ground_build(struct logger* logger, struct city* city)
-{
+{
res_T res = RES_OK;
size_t i,n;
@@ -120,7 +122,7 @@ city_ground_build(struct logger* logger, struct city* city)
n = city->n;
city->ground.n = n;
city->ground.footprint = malloc(n * sizeof(struct scad_geometry*));
-
+
for (i=0; i<n; ++i) {
/* create building footprint */
ERR(city->building[i].build_footprint
@@ -132,7 +134,7 @@ city_ground_build(struct logger* logger, struct city* city)
ERR(scad_scene_mesh());
ERR(ground_export_stl(&city->ground, city->binary_export));
-
+
exit:
scad_finalize();
return res;
diff --git a/src/cg_city.h b/src/cg_city.h
@@ -17,18 +17,17 @@
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>. */
-#include <rsys/logger.h>
+#ifndef CITY_H
+#define CITY_H
+
#include <star/scad.h>
-#include "cg_building.h"
#include "cg_ground.h"
-#include "cg_args.h"
+struct logger;
-#define ERR(Expr) if((res = (Expr)) != RES_OK) goto error; else (void)0
-
-#ifndef CITY_H
-#define CITY_H
+struct building;
+struct args;
static INLINE void
log_prt_fn(const char* msg, void* ctx)
@@ -60,11 +59,11 @@ log_err_fn(const char* msg, void* ctx)
struct city {
struct building* building; /* list of buildings */
size_t n;
- struct ground ground;
+ struct ground ground;
int binary_export;
};
-#define CITY_NULL__ {NULL, 0, GROUND_NULL, 0}
+#define CITY_NULL__ {NULL, 0, GROUND_NULL__, 0}
static const struct city CITY_NULL = CITY_NULL__;
res_T
diff --git a/src/cg_ground.c b/src/cg_ground.c
@@ -17,16 +17,15 @@
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>. */
-#include <rsys/str.h>
-
#include "cg_ground.h"
+#include "cg.h"
-#define ERR(Expr) if((res = (Expr)) != RES_OK) goto error; else (void)0
-
+#include <rsys/str.h>
+#include <star/scad.h>
res_T
ground_build_cad
-( struct ground* ground)
+ (struct ground* ground)
{
res_T res = RES_OK;
double origin[3];
@@ -39,12 +38,12 @@ ground_build_cad
if (!ground) {
res = RES_BAD_ARG;
goto error;
- }
+ }
origin[0] = ground->extent[0];
origin[1] = ground->extent[2];
origin[2] = -ground->depth;
-
+
extent[0] = ground->extent[1] - ground->extent[0];
extent[1] = ground->extent[3] - ground->extent[2];
extent[2] = ground->depth;
@@ -52,7 +51,7 @@ ground_build_cad
if (origin[2] > 0 || extent[0] < 0 || extent[1] < 0 || extent[2] < 0 ) {
res = RES_BAD_ARG;
goto error;
- }
+ }
str_init(NULL, &name);
is_init = 1;
@@ -69,7 +68,7 @@ ground_build_cad
&ground_box, 1,
ground->footprint, ground->n,
&ground->geometry));
-
+
ERR(scad_geometry_boundary
(NULL,
&ground->geometry, 1,
@@ -95,14 +94,13 @@ error:
goto exit;
}
-
res_T
ground_export_stl(const struct ground* ground, const int binary)
{
res_T res = RES_OK;
-
- ERR(scad_stl_export(ground->geometry, "S_ground", binary));
- ERR(scad_stl_export(ground->boundary, "B_ground", binary));
+
+ ERR(scad_stl_export(ground->geometry, "S_ground", binary));
+ ERR(scad_stl_export(ground->boundary, "B_ground", binary));
exit:
return res;
@@ -110,12 +108,11 @@ error:
goto exit;
}
-
res_T
ground_release(struct ground* ground)
{
res_T res = RES_OK;
-
+
if (ground->footprint) free(ground->footprint);
return res;
diff --git a/src/cg_ground.h b/src/cg_ground.h
@@ -17,12 +17,14 @@
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>. */
-#include <rsys/rsys.h>
-#include <star/scad.h>
-
#ifndef GROUND_H
#define GROUND_H
+#include <rsys/rsys.h>
+
+struct scad_scene;
+struct scad_geometry;
+
struct ground {
/* generic building data */
double extent[4]; /* [xmin, xmax, ymin, ymax */
@@ -30,18 +32,17 @@ struct ground {
/* cad representation */
struct scad_scene* scene;
- struct scad_geometry* geometry;
- struct scad_geometry* boundary;
- struct scad_geometry** footprint; /* list of building footprint */
- size_t n; /* number of footprint */
+ struct scad_geometry* geometry;
+ struct scad_geometry* boundary;
+ struct scad_geometry** footprint; /* list of building footprint */
+ size_t n; /* number of footprint */
};
#define GROUND_NULL__ {{0,0,0,0}, 0, NULL, NULL, NULL, NULL, 0}
static const struct ground GROUND_NULL = GROUND_NULL__;
res_T
-ground_build_cad
-(struct ground* ground);
+ground_build_cad(struct ground* ground);
res_T
ground_export_stl(const struct ground* ground, const int binary);
diff --git a/src/cg_main.c b/src/cg_main.c
@@ -17,11 +17,17 @@
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>. */
-#include <rsys/rsys.h>
-
+#include "cg.h"
#include "cg_args.h"
#include "cg_city.h"
+#include <rsys/rsys.h>
+#include <rsys/logger.h>
+
+char const* model_str [1] = {
+ "model0"
+};
+
int main
(int argc, char** argv)
{
@@ -36,13 +42,12 @@ int main
logger_set_stream(&logger, LOG_WARNING, log_warn_fn, NULL);
logger_set_stream(&logger, LOG_ERROR, log_err_fn, NULL);
-
ERR(parse_args(&logger,argc, argv, &args));
ERR(city_init(&logger, &city, &args));
ERR(city_cad_build(&logger, &city));
ERR(city_ground_build(&logger, &city));
-exit:
+exit:
city_release(&city);
release_args(&args);
logger_release(&logger);
diff --git a/src/cg_parsing.c b/src/cg_parsing.c
@@ -17,39 +17,45 @@
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>. */
-#include <rsys/cstr.h>
-#include <rsys/stretchy_array.h>
-
+#include "cg.h"
#include "cg_parsing.h"
+#include "cg_ground.h"
+#include "cg_building.h"
+#include "cg_city.h"
-#define ERR(Expr) if((res = (Expr)) != RES_OK) goto error; else (void)0
+#include <star/scpr.h>
+
+#include <rsys/cstr.h>
+#include <rsys/logger.h>
+#include <rsys/str.h>
+#include <rsys/text_reader.h>
+#include <rsys/stretchy_array.h>
/*Funtion removing spaces from string*/
static char*
remove_spaces(char *string)
{
/*non_space_count to keep the frequency of non space characters*/
- int i;
- int non_space_count = 0;
-
- /*Traverse a string and if it is non space character then, place it at
- * index non_space_count*/
- for (i = 0; string[i] != '\0'; i++)
- {
- if (string[i] != ' ')
- {
- string[non_space_count] = string[i];
- non_space_count++; /* non_space_count incremented */
- }
+ int i;
+ int non_space_count = 0;
+
+ /*Traverse a string and if it is non space character then, place it at
+ * index non_space_count*/
+ for (i = 0; string[i] != '\0'; i++) {
+ if (string[i] != ' ') {
+ string[non_space_count] = string[i];
+ non_space_count++; /* non_space_count incremented */
}
-
- /*Finally placing final character at the string end*/
- string[non_space_count] = '\0';
- return string;
+ }
+
+ /*Finally placing final character at the string end*/
+ string[non_space_count] = '\0';
+ return string;
}
static res_T
-strtolower(const char * src, char* dst ) {
+strtolower(const char * src, char* dst )
+{
res_T res = RES_OK;
if (!src || !dst) {
@@ -65,13 +71,14 @@ error:
goto exit;
}
-static enum model get_enum_value(char * val) {
- int i=0;
- for (i=0; i<MODEL_COUNT_; ++i)
- if (!strcmp(model_str[i], val))
- return i;
- return MODEL_COUNT_;
- }
+static enum model_type get_enum_value(char * val)
+{
+ int i=0;
+ for (i=0; i<MODEL_COUNT_; ++i)
+ if (!strcmp(model_str[i], val))
+ return (enum model_type)i;
+ return MODEL_COUNT_;
+}
static res_T
parse_ground
@@ -98,8 +105,9 @@ parse_ground
if (sscanf(line, "extent=%s", value) == 1 ) {
size_t sz = 0;
if (cstr_to_list_double(value, ',', ground->extent, &sz, 4)
- != RES_OK || sz != 4) {
- logger_print(logger, LOG_ERROR,
+ != RES_OK || sz != 4)
+ {
+ logger_print(logger, LOG_ERROR,
"[ground] extent value not valid: 4 values required.\n");
res = RES_BAD_ARG;
if (value) free(value);
@@ -109,24 +117,24 @@ parse_ground
if (value) free(value);
/*if new section break*/
- if (sscanf(line, "[%[^]]", value) == 1 ) {
+ if (sscanf(line, "[%[^]]", value) == 1 ) {
break;
}
- ERR(txtrdr_read_line(reader));
+ ERR(txtrdr_read_line(reader));
}
/*check depth*/
if (ground->depth <= 0) {
- logger_print(logger, LOG_ERROR,
+ logger_print(logger, LOG_ERROR,
"[ground] depth value not valid: must be > 0.\n");
res = RES_BAD_ARG;
goto error;
- }
+ }
/*check extent*/
if (ground->extent[0] >= ground->extent[1]
|| ground->extent[2] >= ground->extent[3]) {
- logger_print(logger, LOG_ERROR,
+ logger_print(logger, LOG_ERROR,
"[ground] extent value not valid: xmax must be > xmin and ymax must be > ymin.\n");
res = RES_BAD_ARG;
goto error;
@@ -156,9 +164,9 @@ get_pos(const size_t icomp, const size_t ivert, double pos[2], void* context)
{
const struct polygon_context* pg_ctx = context;
size_t i, begin = 0;
-
- for (i=0; i<icomp; ++i) {
- begin += pg_ctx->nvert[i]*2;
+
+ for (i=0; i<icomp; ++i) {
+ begin += pg_ctx->nvert[i]*2;
}
pos[0] = pg_ctx->pos[begin + ivert*2 + 0];
pos[1] = pg_ctx->pos[begin + ivert*2 + 1];
@@ -170,7 +178,7 @@ parse_polygon(const char* str, void* data)
res_T res = RES_OK;
struct polygon_context* pg_ctx = data;
double value;
-
+
ERR(cstr_to_double(str, &value));
pg_ctx->nvert[pg_ctx->ncomps] += 1;
@@ -241,11 +249,11 @@ parse_building
if (cstr_parse_list(value, ';', parse_polygon_list, &pg_ctx) != RES_OK ) {
size_t line_num;
line_num = txtrdr_get_line_num(reader);
- logger_print(logger, LOG_ERROR,
+ logger_print(logger, LOG_ERROR,
"[polygon] description not valid on line: %lu\n",
(unsigned long)line_num);
goto error;
- }
+ }
ERR(scpr_polygon_setup_indexed_vertices(
building->pg, pg_ctx.ncomps,
@@ -267,25 +275,25 @@ parse_building
str_set(building->data_name, value);
}
if (value) free(value);
-
+
/*if new section break*/
- if (sscanf(line, "[%[^]]", value) == 1 ) {
+ if (sscanf(line, "[%[^]]", value) == 1 ) {
break;
}
- ERR(txtrdr_read_line(reader));
+ ERR(txtrdr_read_line(reader));
}
/*check height*/
if (building->height <= 0) {
- logger_print(logger, LOG_ERROR,
+ logger_print(logger, LOG_ERROR,
"[building] height value not valid: must be > 0.\n");
res = RES_BAD_ARG;
goto error;
- }
-
+ }
+
/*check model*/
if (building->model == MODEL_COUNT_) {
- logger_print(logger, LOG_ERROR,
+ logger_print(logger, LOG_ERROR,
"[model] not recognized.\n");
res = RES_BAD_ARG;
goto error;
@@ -347,7 +355,7 @@ parse_building_params
struct data_model0* data;
struct building_params* params0;
(void)logger;
-
+
htable_building_params_init(NULL, ht_params);
params0 = malloc(2*sizeof(struct building_params));
@@ -402,9 +410,9 @@ release_params_building(struct building_params* params)
res = RES_BAD_ARG;
goto error;
}
-
+
printf("size params %lu \n", sa_size(params));
-
+
for (i=0; i<sa_size(params); ++i) {
free(params[i].data);
}
diff --git a/src/cg_parsing.h b/src/cg_parsing.h
@@ -23,13 +23,13 @@
#include <ctype.h>
#include <rsys/rsys.h>
-#include <rsys/text_reader.h>
-#include <rsys/logger.h>
-#include <rsys/hash_table.h>
-#include "cg_ground.h"
-#include "cg_building.h"
-#include "cg_city.h"
+struct txtrdr;
+struct logger;
+struct htable_building_params;
+
+struct city;
+struct building_params;
res_T
parse_city