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 a3cf8bd1f5f680e8b0c633af69ff63799f14d35d
parent 9412303f91a0a5aaf1f10d88eacd67b2124f32f6
Author: Benjamin Piaud <benjamin.piaud@meso-star.com>
Date:   Thu,  4 Aug 2022 17:13:02 +0200

Begin parsing command line and files

Diffstat:
Mcmake/CMakeLists.txt | 6++++--
Asrc/cg_args.c | 81+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Asrc/cg_args.h | 37+++++++++++++++++++++++++++++++++++++
Msrc/cg_building.c | 2+-
Msrc/cg_city.c | 38+++++++++++++++++++++++++++++++-------
Msrc/cg_city.h | 37++++++++++++++++++++++++++++++++++---
Msrc/cg_ground.c | 2+-
Msrc/cg_main.c | 21+++++++++++++--------
Asrc/cg_parsing.c | 82+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Asrc/cg_parsing.h | 33+++++++++++++++++++++++++++++++++
10 files changed, 317 insertions(+), 22 deletions(-)

diff --git a/cmake/CMakeLists.txt b/cmake/CMakeLists.txt @@ -50,14 +50,16 @@ set(CG2_FILES_SRC cg_city.c cg_building.c cg_ground.c - parse_args.c + cg_args.c + cg_parsing.c polygon.c) set(CG2_FILES_INC cg_city.h cg_building.h cg_ground.h - parse_args.h + cg_args.h + cg_parsing.h polygon.h) set(CG2_FILES_DOC COPYING README.md) diff --git a/src/cg_args.c b/src/cg_args.c @@ -0,0 +1,81 @@ +/* Copyright (C) 2022 Université de Pau et des Pays de l'Adour UPPA + * Copyright (C) 2022 |Meso|Star> (contact@meso-star.com) + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * 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" + +#define ERR(Expr) if((res = (Expr)) != RES_OK) goto error; else (void)0 + +res_T +parse_args(struct logger* logger, int argc, char** argv, struct args* args) +{ + res_T res = RES_OK; + int opt = 0; + const char option_list[] = "b:c:"; + + str_init(NULL, &args->building_model_file); + str_init(NULL, &args->city_model_file); + + while((opt = getopt(argc, argv, option_list)) != -1) { + switch (opt) { + case 'b': + { + str_set(&args->building_model_file, optarg); + break; + } + + case 'c': + { + str_set(&args->city_model_file, optarg); + break; + } + } + } + + if (str_is_empty(&args->building_model_file)) { + ERR(logger_print(logger, LOG_ERROR, + "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; +error: + str_release(&args->building_model_file); + str_release(&args->city_model_file); + goto exit; +} + +res_T +release_args(struct args* args) +{ + res_T res = RES_OK; + + str_release(&args->city_model_file); + str_release(&args->building_model_file); + + return res; +} diff --git a/src/cg_args.h b/src/cg_args.h @@ -0,0 +1,37 @@ +/* Copyright (C) 2022 Université de Pau et des Pays de l'Adour UPPA + * Copyright (C) 2022 |Meso|Star> (contact@meso-star.com) + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * 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 <rsys/str.h> +#include <rsys/logger.h> + +#ifndef PARSE_ARGS_H +#define PARSE_ARGS_H + + +struct args { + struct str city_model_file; + struct str building_model_file; +}; + + +res_T +parse_args(struct logger* logger, int argc, char** argv, struct args* args); + +res_T +release_args(struct args* args); + +#endif /*PARSE_ARGS_H*/ diff --git a/src/cg_building.c b/src/cg_building.c @@ -16,7 +16,7 @@ #include <rsys/str.h> -#include "building.h" +#include "cg_building.h" #define ERR(Expr) if((res = (Expr)) != RES_OK) goto error; else (void)0 diff --git a/src/cg_city.c b/src/cg_city.c @@ -14,16 +14,40 @@ * 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 "city.h" +#include <string.h> + +#include <rsys/text_reader.h> +#include <rsys/cstr.h> + +#include "cg_city.h" +#include "cg_parsing.h" res_T -city_init(struct city* city) +city_init(struct logger* logger, struct city* city, struct args* args) { res_T res = RES_OK; + struct txtrdr* reader = NULL; + char* line = NULL; struct building* building = NULL; struct data_model0 data0, data1; /*struct ground ground;*/ + ERR(txtrdr_file(NULL, str_cget(&args->city_model_file), '#', &reader)); + + ERR(txtrdr_read_line(reader)); +parse: + while (txtrdr_get_line(reader)){ + line = txtrdr_get_line(reader); + /*if (strcmp(line, "[building]") == 0) {*/ + /*parse_building(reader);*/ + /*goto parse;*/ + if (strcmp(line, "[ground]") == 0) { + ERR(parse_ground(logger, reader, &city->ground)); + goto parse; + } + ERR(txtrdr_read_line(reader)); + } + #define NBUILD 2 building = malloc(NBUILD * sizeof(struct building)); @@ -67,11 +91,11 @@ city_init(struct city* city) city->n = NBUILD; city->building = building; - city->ground.extent[0] = -100; - city->ground.extent[1] = 100; - city->ground.extent[2] = -100; - city->ground.extent[3] = 100; - city->ground.depth = 3; + /*city->ground.extent[0] = -100; */ + /*city->ground.extent[1] = 100; */ + /*city->ground.extent[2] = -100; */ + /*city->ground.extent[3] = 100; */ + /*city->ground.depth = 3; */ ERR(scad_device_create(NULL, NULL, 1, &city->scad_device)); diff --git a/src/cg_city.h b/src/cg_city.h @@ -13,10 +13,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 <rsys/logger.h> #include <star/scad.h> -#include "building.h" -#include "ground.h" +#include "cg_building.h" +#include "cg_ground.h" +#include "cg_args.h" #define ERR(Expr) if((res = (Expr)) != RES_OK) goto error; else (void)0 @@ -24,6 +27,34 @@ #ifndef CITY_H #define CITY_H +static INLINE void +log_prt_fn(const char* msg, void* ctx) +{ + ASSERT(msg); + (void)ctx; + + fprintf(stderr, "\x1b[32moutput:\x1b[0m %s", msg); +} + +static INLINE void +log_warn_fn(const char* msg, void* ctx) +{ + ASSERT(msg); + (void)ctx; + + fprintf(stderr, "\x1b[33mwarning:\x1b[0m %s", msg); +} + +static INLINE void +log_err_fn(const char* msg, void* ctx) +{ + ASSERT(msg); + (void)ctx; + + fprintf(stderr, "\x1b[31merror:\x1b[0m %s", msg); +} + + struct city { struct scad_device* scad_device; struct building* building; /* list of buildings */ @@ -32,7 +63,7 @@ struct city { }; res_T -city_init(struct city* city); +city_init(struct logger* logger, struct city* city, struct args* arg); res_T city_cad_build(struct city* city); diff --git a/src/cg_ground.c b/src/cg_ground.c @@ -16,7 +16,7 @@ #include <rsys/str.h> -#include "ground.h" +#include "cg_ground.h" #define ERR(Expr) if((res = (Expr)) != RES_OK) goto error; else (void)0 diff --git a/src/cg_main.c b/src/cg_main.c @@ -14,30 +14,35 @@ * 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> -#include "city.h" +#include "cg_args.h" +#include "cg_city.h" int main (int argc, char** argv) { int err = EXIT_SUCCESS; res_T res = RES_OK; + struct args args; struct city city; + struct logger logger; - (void)argc; (void)argv; - + ERR(logger_init(NULL, &logger)); + logger_set_stream(&logger, LOG_OUTPUT, log_prt_fn, NULL); + logger_set_stream(&logger, LOG_WARNING, log_warn_fn, NULL); + logger_set_stream(&logger, LOG_ERROR, log_err_fn, NULL); - ERR(city_init(&city)); + ERR(parse_args(&logger,argc, argv, &args)); + ERR(city_init(&logger, &city, &args)); ERR(city_cad_build(&city)); ERR(city_ground_build(&city)); ERR(city_export_stl(&city)); - ERR(city_release(&city)); exit: + city_release(&city); + release_args(&args); + logger_release(&logger); return err; error: err = EXIT_FAILURE; diff --git a/src/cg_parsing.c b/src/cg_parsing.c @@ -0,0 +1,82 @@ +/* Copyright (C) 2022 Université de Pau et des Pays de l'Adour UPPA + * Copyright (C) 2022 |Meso|Star> (contact@meso-star.com) + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * 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 "cg_parsing.h" + +#define ERR(Expr) if((res = (Expr)) != RES_OK) goto error; else (void)0 + + /*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 */ + } + } + + /*Finally placing final character at the string end*/ + string[non_space_count] = '\0'; + return string; +} + + +res_T +parse_ground + (struct logger* logger, + struct txtrdr* reader, + struct ground* ground) +{ + res_T res = RES_OK; + char* line = NULL; + char sec[64]; + double depth = 0; + + ERR(txtrdr_read_line(reader)); + while (txtrdr_get_line(reader)){ + line = remove_spaces(txtrdr_get_line(reader)); + if (sscanf(line, "DEPTH=%lf", &depth) == 1 ) { + ground->depth = depth; + } + if (sscanf(line, "EXTENT=%s", sec) == 1 ) { + size_t sz = 0; + if (cstr_to_list_double(sec, ',', ground->extent, &sz, 4) + != RES_OK || sz != 4) { + logger_print(logger, LOG_ERROR, "[ground] EXTENT value not valid.\n"); + res = RES_BAD_ARG; + goto error; + } + } + if (sscanf(line, "[%[^]]", sec) == 1 ) goto exit; + ERR(txtrdr_read_line(reader)); + } + +exit: + return res; +error: + goto exit; +} diff --git a/src/cg_parsing.h b/src/cg_parsing.h @@ -0,0 +1,33 @@ +/* Copyright (C) 2022 Université de Pau et des Pays de l'Adour UPPA + * Copyright (C) 2022 |Meso|Star> (contact@meso-star.com) + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * 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 <rsys/text_reader.h> +#include <rsys/logger.h> + +#include "cg_ground.h" + +#ifndef PARSING_H +#define PARSING_H + +res_T +parse_ground + (struct logger* logger, + struct txtrdr* reader, + struct ground* ground); + +#endif /*PARSING_H*/ +