commit 1c7e450338594f6c80f8f5757a3c0164a51aa314
parent caf98f3da043095e1fa0c37e852bd6aad6ff1f65
Author: Benjamin Piaud <benjamin.piaud@meso-star.com>
Date: Thu, 27 Oct 2022 14:27:34 +0200
Add struct building_params to prepare building data parsing
Diffstat:
7 files changed, 175 insertions(+), 22 deletions(-)
diff --git a/src/cg_building.h b/src/cg_building.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/>. */
+#ifndef BUILDING_H
+#define BUILDING_H
+
#include <rsys/rsys.h>
+#include <rsys/str.h>
#include <star/scad.h>
#include <star/scpr.h>
-
-#ifndef BUILDING_H
-#define BUILDING_H
+#include <rsys/hash_table.h>
enum model {
model0,
@@ -33,10 +35,45 @@ static char const* model_str [] = {
"model0"
};
+struct building_params {
+ enum model model;
+ struct str name;
+ void* data;
+};
+
+
+static INLINE char
+eq_str(const struct str* a, const struct str* b)
+{
+ return !strcmp(str_cget(a), str_cget(b));
+}
+
+static INLINE size_t
+hash_str(const struct str* a)
+{
+ return hash_fnv32(str_cget(a), str_len(a));
+}
+
+#define HTABLE_NAME building_params
+#define HTABLE_DATA struct building_params
+#define HTABLE_KEY struct str
+#define HTABLE_KEY_FUNCTOR_INIT str_init
+#define HTABLE_KEY_FUNCTOR_RELEASE str_release
+#define HTABLE_KEY_FUNCTOR_COPY str_copy
+#define HTABLE_KEY_FUNCTOR_COPY_AND_RELEASE str_copy_and_release
+#define HTABLE_KEY_FUNCTOR_EQ eq_str
+#define HTABLE_KEY_FUNCTOR_HASH hash_str
+#include <rsys/hash_table.h>
+
+
+/* the specific building model functors headers must be included here */
+#include "cg_building_model0.h"
+
struct building {
/* generic building data */
size_t id;
enum model model;
+ struct str* data_name;
double height;
struct scpr_polygon* pg;
@@ -45,7 +82,8 @@ struct building {
void* data_cad;
/* functors depending model */
- res_T (*init)(struct building* building);
+ res_T (*init)
+ (struct building* building, struct htable_building_params* htparams);
res_T (*build_cad)(struct building* building);
res_T (*build_footprint)
(struct building* building,
@@ -54,7 +92,4 @@ struct building {
res_T (*release)(struct building* building);
};
-/* the specific building model functors headers must be included here */
-#include "cg_building_model0.h"
-
#endif /* BUILDING_H */
diff --git a/src/cg_building_model0.c b/src/cg_building_model0.c
@@ -479,13 +479,24 @@ error:
res_T
init_model0
- (struct building* building)
+ (struct building* building, struct htable_building_params* htparams)
{
res_T res = RES_OK;
struct data_model0 data;
+ struct building_params* params;
+
+ params = htable_building_params_find(htparams, building->data_name);
+ if (params == NULL) {
+ res = RES_BAD_ARG;
+ goto error;
+ }
- data.wall = 0.2;
- data.floor = 0.3;
+ if (params->model != building->model) {
+ res = RES_BAD_ARG;
+ goto error;
+ }
+
+ data = *(struct data_model0*)params->data;
building->data = malloc(sizeof(struct data_model0));
if (!building->data) {
res = RES_MEM_ERR;
@@ -655,7 +666,9 @@ release_model0
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
@@ -23,6 +23,10 @@
#ifndef BUILDING_MODEL0_H
#define BUILDING_MODEL0_H
+struct building;
+struct htable_building_params;
+struct building_params;
+
/* specific data for model 0 */
struct data_model0 {
double wall; /* wall thickness */
@@ -41,7 +45,8 @@ struct data_cad_model0 {
};
res_T
-init_model0(struct building* building);
+init_model0
+ (struct building* building, struct htable_building_params* htparams);
res_T
build_cad_model0(struct building* building);
diff --git a/src/cg_city.c b/src/cg_city.c
@@ -26,19 +26,22 @@
#include "cg_city.h"
#include "cg_parsing.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;
+ struct htable_building_params ht_params;
city->binary_export = args->binary_export;
ERR(txtrdr_file(NULL, str_cget(&args->city_model_file), '#', &reader));
-
ERR(parse_city(logger, reader, city));
+ ERR(parse_building_params(logger, &ht_params));
+
for (i=0; i<city->n ; ++i) {
switch(city->building[i].model) {
case model0:
@@ -48,10 +51,12 @@ city_init(struct logger* logger, struct city* city, struct args* args)
res = RES_BAD_ARG;
goto error;
}
- ERR(city->building[i].init(&city->building[i]));
+ ERR(city->building[i].init(&city->building[i], &ht_params));
}
exit:
+ txtrdr_ref_put(reader);
+ htable_building_params_release(&ht_params);
return res;
error:
goto exit;
diff --git a/src/cg_city.h b/src/cg_city.h
@@ -57,7 +57,6 @@ log_err_fn(const char* msg, void* ctx)
fprintf(stderr, "\x1b[31merror:\x1b[0m %s", msg);
}
-
struct city {
struct building* building; /* list of buildings */
size_t n;
diff --git a/src/cg_parsing.c b/src/cg_parsing.c
@@ -260,6 +260,12 @@ parse_building
if (sscanf(line, "model=%s", value) == 1 ) {
building->model = get_enum_value(value);
}
+
+ if (sscanf(line, "data=%s", value) == 1 ) {
+ building->data_name = malloc(sizeof(struct str));
+ str_init(NULL, building->data_name);
+ str_set(building->data_name, value);
+ }
if (value) free(value);
/*if new section break*/
@@ -293,6 +299,10 @@ error:
goto exit;
}
+/*----------------------------------------------------------------------------*/
+/*----------------------------------------------------------------------------*/
+/*----------------------------------------------------------------------------*/
+
res_T
parse_city
(struct logger* logger,
@@ -305,18 +315,17 @@ parse_city
ERR(txtrdr_read_line(reader));
-parse:
while (txtrdr_get_line(reader)){
line = txtrdr_get_line(reader);
ERR(strtolower(line, line));
if (strcmp(line, "[building]") == 0) {
ERR(parse_building(logger, reader, &b));
sa_push(city->building, b);
- goto parse;
+ continue;
}
if (strcmp(line, "[ground]") == 0) {
ERR(parse_ground(logger, reader, &city->ground));
- goto parse;
+ continue;
}
ERR(txtrdr_read_line(reader));
}
@@ -328,3 +337,83 @@ exit:
error:
goto exit;
}
+
+res_T
+parse_building_params
+ (struct logger* logger,
+ struct htable_building_params* ht_params)
+{
+ res_T res = RES_OK;
+ struct data_model0* data;
+ struct building_params* params0;
+ (void)logger;
+
+ htable_building_params_init(NULL, ht_params);
+
+ params0 = malloc(2*sizeof(struct building_params));
+ if (!params0) {
+ res = RES_MEM_ERR;
+ goto error;
+ }
+
+ data = malloc(sizeof(struct data_model0));
+ if (!data) {
+ res = RES_MEM_ERR;
+ goto error;
+ }
+ params0[0].model = model0;
+ str_init(NULL, ¶ms0[0].name);
+ str_set(¶ms0[0].name, "b0");
+ data->wall = 0.2;
+ data->floor = 0.3;
+ params0[0].data = malloc(sizeof(struct data_model0));
+ params0[0].data = (void*)data;
+ ERR(htable_building_params_set(ht_params, ¶ms0[0].name, ¶ms0[0]));
+
+ data = malloc(sizeof(struct data_model0));
+ if (!data) {
+ res = RES_MEM_ERR;
+ goto error;
+ }
+ params0[1].model = model0;
+ str_init(NULL, ¶ms0[1].name);
+ str_set(¶ms0[1].name, "b1");
+ data->wall = 0.25;
+ data->floor = 0.35;
+ params0[1].data = malloc(sizeof(struct data_model0));
+ params0[1].data = (void*)data;
+ ERR(htable_building_params_set(ht_params, ¶ms0[1].name, ¶ms0[1]));
+
+exit:
+ free(params0);
+ return res;
+error:
+ htable_building_params_release(ht_params);
+ goto exit;
+}
+
+res_T
+release_params_building(struct building_params* params)
+{
+ res_T res = RES_OK;
+ size_t i;
+
+ if (!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);
+ }
+
+ sa_release(params);
+
+exit:
+ return res;
+error:
+ goto exit;
+}
+
diff --git a/src/cg_parsing.h b/src/cg_parsing.h
@@ -17,25 +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/>. */
+#ifndef PARSING_H
+#define PARSING_H
+
#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"
-#ifndef PARSING_H
-#define PARSING_H
-
res_T
parse_city
(struct logger* logger,
struct txtrdr* reader,
struct city* city);
+res_T
+parse_building_params
+ (struct logger* logger,
+ struct htable_building_params* ht_params);
-#endif /*PARSING_H*/
+res_T
+release_params_building(struct building_params* params);
+#endif /*PARSING_H*/