From 4a47fc463e08a29ce9ca1edea721e07394fb6061 Mon Sep 17 00:00:00 2001 From: treydinges Date: Wed, 29 Sep 2021 15:41:46 -0500 Subject: [PATCH 01/15] added material objects and db I/O functions --- include/bu/magic.h | 1 + include/rt/defines.h | 1 + include/rt/nongeom.h | 12 +++ src/libbu/magic.c | 2 + src/libged/typein/typein.c | 33 ++++++++ src/librt/material.c | 138 +++++++++++++++++++++++++++++++++ src/librt/primitives/table.cpp | 48 ++++++++++++ src/libwdb/material.c | 62 +++++++++++++++ 8 files changed, 297 insertions(+) create mode 100644 src/librt/material.c create mode 100644 src/libwdb/material.c diff --git a/include/bu/magic.h b/include/bu/magic.h index 30073f92027..e8bfa7c7c58 100644 --- a/include/bu/magic.h +++ b/include/bu/magic.h @@ -168,6 +168,7 @@ __BEGIN_DECLS #define RT_AP_MAGIC 0x4170706c /**< Appl */ #define RT_COMB_MAGIC 0x436f6d49 /**< ComI */ #define RT_CONSTRAINT_MAGIC 0x7063696d /**< pcim */ +#define RT_MATERIAL_MAGIC 0x54414d55 /**< TAMU */ #define RT_CTS_MAGIC 0x98989123 /**< ???\# */ #define RT_DB_TRAVERSE_MAGIC 0x64627472 /**< dbtr */ #define RT_DBTS_MAGIC 0x64627473 /**< dbts */ diff --git a/include/rt/defines.h b/include/rt/defines.h index 4755613e5ee..9664f9319ee 100644 --- a/include/rt/defines.h +++ b/include/rt/defines.h @@ -119,6 +119,7 @@ #define ID_BINUNIF 33 /**< @brief Uniform-array binary */ #define ID_UNUSED2 34 /**< @brief UNUSED (placeholder) */ #define ID_CONSTRAINT 39 /**< @brief Constraint object */ +#define ID_MATERIAL 47 /**< @brief Material object */ /* - ADD_BELOW_HERE - */ /* superellipsoid should be 31, but is not v5 compatible */ diff --git a/include/rt/nongeom.h b/include/rt/nongeom.h index 2b575768d5c..2c6ea2a4a8a 100644 --- a/include/rt/nongeom.h +++ b/include/rt/nongeom.h @@ -134,6 +134,18 @@ struct rt_constraint_internal { #define RT_CHECK_CONSTRAINT(_p) BU_CKMAG(_p, PC_CONSTRAINT_MAGIC, "pc_constraint_internal") #define RT_CK_CONSTRAINT(_p) PC_CHECK_CONSTRAINT(_p) +/** + * In-memory format for database "material" record + */ +struct rt_material_internal { + uint32_t magic; // ? + int id; + struct bu_vls name; + double density; +}; + +#define RT_CHECK_MATERIAL(_p) BU_CKMAG(_p, PC_MATERIAL_MAGIC, "pc_material_internal") +#define RT_CK_MATERIAL(_p) PC_CHECK_MATERIAL(_p) __END_DECLS diff --git a/src/libbu/magic.c b/src/libbu/magic.c index 5bd6a754349..b423efd4394 100644 --- a/src/libbu/magic.c +++ b/src/libbu/magic.c @@ -236,6 +236,8 @@ bu_identify_magic(register uint32_t magic) return "librt rt_comb_internal"; case RT_CONSTRAINT_MAGIC: return "librt rt_constraint_internal"; + case RT_MATERIAL_MAGIC: + return "librt rt_material_internal"; case RT_CTS_MAGIC: return "librt combined_tree_state"; case RT_DB_TRAVERSE_MAGIC: diff --git a/src/libged/typein/typein.c b/src/libged/typein/typein.c index 484acfc7f65..8575edf2daf 100644 --- a/src/libged/typein/typein.c +++ b/src/libged/typein/typein.c @@ -650,6 +650,15 @@ static const char *p_script[] = { "Enter the script type: " }; +/** + * TODO: + * add support + */ +static const char *p_material[] = { + "Enter the material name: ", + "Enter the material density: " +}; + /** * helper function that infers a boolean value from a given string * returning 0 or 1 for false and true respectively. @@ -1504,6 +1513,26 @@ sph_in(struct ged *gedp, const char **cmd_argvs, struct rt_db_internal *intern, return GED_OK; } +static int +material_in(struct ged *gedp, const char **cmd_argvs, struct rt_db_internal *intern, const char *name) +{ +// struct rt_material_internal *material; + +// intern->idb_major_type = DB5_MAJORTYPE_BRLCAD; +// intern->idb_type = ID_MATERIAL; +// intern->idb_meth = &OBJ[ID_MATERIAL]; +// BU_ALLOC(intern->idb_ptr, struct rt_material_internal); +// material = (struct rt__internal *)intern->idb_ptr; +// material->magic = RT_MATERIAL_MAGIC; + +// bu_vls_init(&material->name); +// bu_vls_strcpy(&material->name, cmd_argvs[3]); + + mk_material(gedp->ged_wdbp, name); + + return GED_OK; +} + /* * reads ell parameters from keyboard @@ -3414,6 +3443,10 @@ ged_in_core(struct ged *gedp, int argc, const char *argv[]) nvals = 1; menu = p_script; fn_in = script_in; + } else if (BU_STR_EQUAL(argv[2], "material")) { + nvals = 2; + menu = p_material; + fn_in = material_in; } else if (BU_STR_EQUAL(argv[2], "pnts")) { switch (pnts_in(gedp, argc, argv, &internal, p_pnts)) { case GED_ERROR: diff --git a/src/librt/material.c b/src/librt/material.c new file mode 100644 index 00000000000..232ffc89698 --- /dev/null +++ b/src/librt/material.c @@ -0,0 +1,138 @@ +/* M A T E R I A L . C + * BRL-CAD + * + * Copyright (c) 2008-2021 United States Government as represented by + * the U.S. Army Research Laboratory. + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public License + * version 2.1 as published by the Free Software Foundation. + * + * This library 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 + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this file; see the file named COPYING for more + * information. + */ +/** @addtogroup librt */ +/** @{ */ +/** @file librt/material.c + * + * Various functions associated with material object database I/O + * + * Todo: + * + */ + +#include "common.h" + +#include + + +#include "bn.h" +#include "rt/db4.h" +#include "pc.h" +#include "raytrace.h" + + +static const struct bu_structparse rt_material_parse[] = { + {"%d", 1, "ID", bu_offsetof(struct rt_material_internal, id), BU_STRUCTPARSE_FUNC_NULL, NULL, NULL}, + {"%V", 1, "Ex", bu_offsetof(struct rt_material_internal, name), BU_STRUCTPARSE_FUNC_NULL, NULL, NULL}, + {"%d", 1, "D", bu_offsetof(struct rt_material_internal, density), BU_STRUCTPARSE_FUNC_NULL, NULL, NULL}, + {"", 0, (char *)0, 0, BU_STRUCTPARSE_FUNC_NULL, NULL, NULL } +}; + + +/** + * Free the storage associated with the rt_db_internal version of + * material object. + */ +void +rt_material_ifree(struct rt_db_internal *ip) +{ + register struct rt_material_internal *material; + + RT_CK_DB_INTERNAL(ip); + material = (struct rt_material_internal *)ip->idb_ptr; + + if (material) { + material->magic = 0; /* sanity */ + bu_vls_free(&material->name); + bu_free((void *)material, "material ifree"); + } + ip->idb_ptr = ((void *)0); /* sanity */ +} + + +/** + * Import a material from the database format to the internal format. + */ +int +rt_material_import5(struct rt_db_internal *ip, const struct bu_external *ep, const fastf_t *UNUSED(mat), const struct db_i *UNUSED(dbip)) +{ + struct rt_material_internal *material; + unsigned char *ptr; + + BU_CK_EXTERNAL(ep); + RT_CK_DB_INTERNAL(ip); + + ip->idb_major_type = DB5_MAJORTYPE_BRLCAD; + ip->idb_type = ID_MATERIAL; + ip->idb_meth = &OBJ[ID_MATERIAL]; + BU_ALLOC(ip->idb_ptr, struct rt_material_internal); + + material = (struct rt_material_internal *)ip->idb_ptr; + BU_VLS_INIT(&material->name); + material->magic = RT_MATERIAL_MAGIC; + + ptr = ep->ext_buf; + + bu_vls_init(&material->name); + bu_vls_strncpy(&material->name, (char *)ptr, + ep->ext_nbytes - (ptr - (unsigned char *)ep->ext_buf)); + + return 0; /* OK */ +} + + +/** + * Export a material from the internal format to the database format. + */ +int +rt_material_export5(struct bu_external *ep, const struct rt_db_internal *ip, double UNUSED(local2mm), const struct db_i *UNUSED(dbip)) +{ + struct rt_material_internal *cip; + struct bu_vls str = BU_VLS_INIT_ZERO; + + RT_CK_DB_INTERNAL(ip); + + if (ip->idb_type != ID_MATERIAL) bu_bomb("rt_material_export() type not ID_MATERIAL"); + cip = (struct rt_material_internal *) ip->idb_ptr; + + BU_EXTERNAL_INIT(ep); + + bu_vls_struct_print(&str, rt_material_parse, (char *)cip); + + ep->ext_nbytes = bu_vls_strlen(&str); + ep->ext_buf = (uint8_t *)bu_calloc(1, ep->ext_nbytes, "material external"); + bu_strlcpy((char *)ep->ext_buf, bu_vls_addr(&str), ep->ext_nbytes); + + bu_vls_free(&str); + + return 0; /* OK */ +} + + +/** @} */ +/* + * Local Variables: + * mode: C + * tab-width: 8 + * indent-tabs-mode: t + * c-file-style: "stroustrup" + * End: + * ex: shiftwidth=4 tabstop=8 + */ diff --git a/src/librt/primitives/table.cpp b/src/librt/primitives/table.cpp index c296c7c6994..7f53fa0d2a8 100644 --- a/src/librt/primitives/table.cpp +++ b/src/librt/primitives/table.cpp @@ -117,6 +117,7 @@ RT_DECLARE_INTERFACE(metaball); RT_DECLARE_INTERFACE(hyp); RT_DECLARE_INTERFACE(revolve); RT_DECLARE_INTERFACE(constraint); +RT_DECLARE_INTERFACE(material); /* RT_DECLARE_INTERFACE(binunif); */ RT_DECLARE_INTERFACE(pnts); RT_DECLARE_INTERFACE(hrt); @@ -2374,6 +2375,53 @@ const struct rt_functab OBJ[] = { NULL /* label */ }, + { + /* 47 */ + RT_FUNCTAB_MAGIC, "ID_MATERIAL", "material", + 0, /* ft_use_rpp */ + NULL, /* prep */ + NULL, /* shot */ + NULL, /* print */ + NULL, /* norm */ + NULL, /* piece_shot */ + NULL, /* piece_hitsegs */ + NULL, /* uv */ + NULL, /* curve */ + NULL, /* classify */ + NULL, /* free */ + NULL, /* plot */ + NULL, /* adaptive_plot */ + NULL, /* vshot */ + NULL, /* tess */ + NULL, /* tnurb */ + NULL, /* brep */ + RTFUNCTAB_FUNC_IMPORT5_CAST(rt_material_import5), + RTFUNCTAB_FUNC_EXPORT5_CAST(rt_material_export5), + NULL, /* import4 */ + NULL, /* export4 */ + RTFUNCTAB_FUNC_IFREE_CAST(rt_material_ifree), + NULL, /* describe */ + NULL, /* xform */ + NULL, /* parse */ + 0, /* sizeof(internal) */ + 0, /* magic */ + NULL, /* get */ + NULL, /* adjust */ + NULL, /* form */ + NULL, /* make */ + NULL, /* params */ + NULL, /* bbox */ + NULL, /* volume */ + NULL, /* surf_area */ + NULL, /* centroid */ + NULL, /* oriented_bbox */ + NULL, /* find_selections */ + NULL, /* evaluate_selection */ + NULL, /* process_selection */ + NULL, /* serialize */ + NULL /* label */ + }, + { /* this entry for sanity only */ 0L, ">ID_MAXIMUM", ">id_max", diff --git a/src/libwdb/material.c b/src/libwdb/material.c new file mode 100644 index 00000000000..75c5ad0f094 --- /dev/null +++ b/src/libwdb/material.c @@ -0,0 +1,62 @@ +/* M A T E R I A L . C + * BRL-CAD + * + * Copyright (c) 2008-2021 United States Government as represented by + * the U.S. Army Research Laboratory. + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public License + * version 2.1 as published by the Free Software Foundation. + * + * This library 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 + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this file; see the file named COPYING for more + * information. + */ + +/** @file libwdb/material.c + * + * @brief + * + */ + +#include "common.h" + +#include + +#include "raytrace.h" +#include "wdb.h" + + +int +mk_material(struct rt_wdb *wdbp, const char *name) { + struct rt_db_internal intern; + struct rt_material_internal *material; + + RT_CK_WDB(wdbp); + + RT_DB_INTERNAL_INIT(&intern); + + /* Create a fresh new object for export */ + BU_ALLOC(material, struct rt_material_internal); + material->magic = RT_MATERIAL_MAGIC; + material->id = 0; + BU_VLS_INIT(&material->name); + + intern.idb_major_type = DB5_MAJORTYPE_BRLCAD; + intern.idb_type = ID_MATERIAL; + intern.idb_ptr = (void *)material; + intern.idb_meth = &OBJ[ID_MATERIAL]; + + /* Add data */ + material->id = 1; + material->name.vls_str = "TEST"; + material->density = 123.456; + + /* The internal representation will be freed */ + return wdb_put_internal(wdbp, name, &intern, mk_conv2mm); +} \ No newline at end of file From fb5bfe491489dab358e1b22b71d8bc9df31b8e2e Mon Sep 17 00:00:00 2001 From: treydinges Date: Sun, 3 Oct 2021 16:41:40 -0500 Subject: [PATCH 02/15] added basic material object implementation --- include/rt/db5.h | 1 + include/rt/defines.h | 6 ++--- include/rt/nongeom.h | 6 ++--- include/wdb.h | 3 +++ src/libged/typein/typein.c | 25 +++++++++++-------- src/librt/CMakeLists.txt | 1 + .../{ => primitives/material}/material.c | 2 +- src/librt/primitives/table.cpp | 2 +- src/libwdb/CMakeLists.txt | 1 + 9 files changed, 28 insertions(+), 19 deletions(-) rename src/librt/{ => primitives/material}/material.c (97%) diff --git a/include/rt/db5.h b/include/rt/db5.h index cc2a9d90a75..2b61267e39e 100644 --- a/include/rt/db5.h +++ b/include/rt/db5.h @@ -173,6 +173,7 @@ struct db5_ondisk_header { #define DB5_MINORTYPE_BRLCAD_HRT 43 #define DB5_MINORTYPE_BRLCAD_DATUM 44 #define DB5_MINORTYPE_BRLCAD_SCRIPT 45 +#define DB5_MINORTYPE_BRLCAD_MATERIAL 46 /* Uniform-array binary */ #define DB5_MINORTYPE_BINU_WID_MASK 0x30 diff --git a/include/rt/defines.h b/include/rt/defines.h index 9664f9319ee..87f2827d599 100644 --- a/include/rt/defines.h +++ b/include/rt/defines.h @@ -109,7 +109,7 @@ * NOTE: must update the non-geometric object id's below the * ADD_BELOW_HERE marker */ -#define ID_MAX_SOLID 46 /**< @brief Maximum defined ID_xxx for solids */ +#define ID_MAX_SOLID 47 /**< @brief Maximum defined ID_xxx for solids */ /* * Non-geometric objects @@ -119,7 +119,7 @@ #define ID_BINUNIF 33 /**< @brief Uniform-array binary */ #define ID_UNUSED2 34 /**< @brief UNUSED (placeholder) */ #define ID_CONSTRAINT 39 /**< @brief Constraint object */ -#define ID_MATERIAL 47 /**< @brief Material object */ +#define ID_MATERIAL 46 /**< @brief Material object */ /* - ADD_BELOW_HERE - */ /* superellipsoid should be 31, but is not v5 compatible */ @@ -133,7 +133,7 @@ #define ID_HRT 43 /**< @brief Heart */ #define ID_DATUM 44 /**< @brief Datum references */ #define ID_SCRIPT 45 /**< @brief Script */ -#define ID_MAXIMUM 46 /**< @brief Maximum defined ID_xxx value */ +#define ID_MAXIMUM 47 /**< @brief Maximum defined ID_xxx value */ /** * DEPRECATED: external applications should use other LIBRT API to diff --git a/include/rt/nongeom.h b/include/rt/nongeom.h index 2c6ea2a4a8a..916604ad689 100644 --- a/include/rt/nongeom.h +++ b/include/rt/nongeom.h @@ -138,14 +138,14 @@ struct rt_constraint_internal { * In-memory format for database "material" record */ struct rt_material_internal { - uint32_t magic; // ? + uint32_t magic; int id; struct bu_vls name; double density; }; -#define RT_CHECK_MATERIAL(_p) BU_CKMAG(_p, PC_MATERIAL_MAGIC, "pc_material_internal") -#define RT_CK_MATERIAL(_p) PC_CHECK_MATERIAL(_p) +#define RT_CHECK_MATERIAL(_p) BU_CKMAG(_p, RT_MATERIAL_MAGIC, "rt_material_internal") +#define RT_CK_MATERIAL(_p) RT_CHECK_MATERIAL(_p) __END_DECLS diff --git a/include/wdb.h b/include/wdb.h index d534a0470ba..402a8926534 100644 --- a/include/wdb.h +++ b/include/wdb.h @@ -347,6 +347,9 @@ WDB_EXPORT extern int mk_ars(struct rt_wdb *fp, const char *name, size_t ncurves WDB_EXPORT extern int mk_constraint(struct rt_wdb *wdbp, const char *name, const char *expr); +WDB_EXPORT extern int mk_material(struct rt_wdb *wdbp, const char *name); + + /* FIXME: are the variable-sized types actually necessary? should be * able to rely on stdint types. the file+nonfile duplication seems * silly too. diff --git a/src/libged/typein/typein.c b/src/libged/typein/typein.c index 8575edf2daf..3b7257c69b9 100644 --- a/src/libged/typein/typein.c +++ b/src/libged/typein/typein.c @@ -1514,21 +1514,24 @@ sph_in(struct ged *gedp, const char **cmd_argvs, struct rt_db_internal *intern, } static int -material_in(struct ged *gedp, const char **cmd_argvs, struct rt_db_internal *intern, const char *name) +material_in(struct ged *gedp, const char **cmd_argvs, struct rt_db_internal *intern) { -// struct rt_material_internal *material; + struct rt_material_internal *material; -// intern->idb_major_type = DB5_MAJORTYPE_BRLCAD; -// intern->idb_type = ID_MATERIAL; -// intern->idb_meth = &OBJ[ID_MATERIAL]; -// BU_ALLOC(intern->idb_ptr, struct rt_material_internal); -// material = (struct rt__internal *)intern->idb_ptr; -// material->magic = RT_MATERIAL_MAGIC; + intern->idb_major_type = DB5_MAJORTYPE_BRLCAD; + intern->idb_minor_type = DB5_MINORTYPE_BRLCAD_MATERIAL; + intern->idb_meth = &OBJ[ID_MATERIAL]; + intern->idb_magic = RT_MATERIAL_MAGIC; + BU_ALLOC(intern->idb_ptr, struct rt_material_internal); + + material = (struct rt_material_internal *)intern->idb_ptr; + material->magic = RT_MATERIAL_MAGIC; -// bu_vls_init(&material->name); -// bu_vls_strcpy(&material->name, cmd_argvs[3]); + bu_vls_init(&material->name); + bu_vls_strcpy(&material->name, cmd_argvs[3]); - mk_material(gedp->ged_wdbp, name); + material->density = atof(cmd_argvs[4]) * gedp->dbip->dbi_local2base; + // mk_material(gedp->ged_wdbp, material->name.vls_str); return GED_OK; } diff --git a/src/librt/CMakeLists.txt b/src/librt/CMakeLists.txt index f31cf21c8ba..a3afed5ae6b 100644 --- a/src/librt/CMakeLists.txt +++ b/src/librt/CMakeLists.txt @@ -140,6 +140,7 @@ set(LIBRT_SOURCES primitives/joint/joint.c primitives/joint/joint_brep.cpp primitives/joint/joint_mirror.c + primitives/material/material.c primitives/metaball/metaball.c primitives/metaball/metaball_tri.c primitives/mirror.c diff --git a/src/librt/material.c b/src/librt/primitives/material/material.c similarity index 97% rename from src/librt/material.c rename to src/librt/primitives/material/material.c index 232ffc89698..9e4d7b3d428 100644 --- a/src/librt/material.c +++ b/src/librt/primitives/material/material.c @@ -40,7 +40,7 @@ static const struct bu_structparse rt_material_parse[] = { {"%d", 1, "ID", bu_offsetof(struct rt_material_internal, id), BU_STRUCTPARSE_FUNC_NULL, NULL, NULL}, - {"%V", 1, "Ex", bu_offsetof(struct rt_material_internal, name), BU_STRUCTPARSE_FUNC_NULL, NULL, NULL}, + {"%V", 1, "N", bu_offsetof(struct rt_material_internal, name), BU_STRUCTPARSE_FUNC_NULL, NULL, NULL}, {"%d", 1, "D", bu_offsetof(struct rt_material_internal, density), BU_STRUCTPARSE_FUNC_NULL, NULL, NULL}, {"", 0, (char *)0, 0, BU_STRUCTPARSE_FUNC_NULL, NULL, NULL } }; diff --git a/src/librt/primitives/table.cpp b/src/librt/primitives/table.cpp index 7f53fa0d2a8..3368cdad94e 100644 --- a/src/librt/primitives/table.cpp +++ b/src/librt/primitives/table.cpp @@ -2376,7 +2376,7 @@ const struct rt_functab OBJ[] = { }, { - /* 47 */ + /* 46 */ RT_FUNCTAB_MAGIC, "ID_MATERIAL", "material", 0, /* ft_use_rpp */ NULL, /* prep */ diff --git a/src/libwdb/CMakeLists.txt b/src/libwdb/CMakeLists.txt index e49ccb93800..b426899cb21 100644 --- a/src/libwdb/CMakeLists.txt +++ b/src/libwdb/CMakeLists.txt @@ -20,6 +20,7 @@ set(LIBWDB_SOURCES brep.cpp cline.c constraint.c + material.c datum.c dsp.c dynamic_geometry.c From 389b2dafdc25253e3715df2a434b4fd4ea85ac20 Mon Sep 17 00:00:00 2001 From: treydinges Date: Mon, 4 Oct 2021 10:36:04 -0500 Subject: [PATCH 03/15] working material object implementation - incomplete property fields --- src/libged/typein/typein.c | 2 +- src/librt/CMakeLists.txt | 2 +- src/librt/{primitives/material => }/material.c | 0 3 files changed, 2 insertions(+), 2 deletions(-) rename src/librt/{primitives/material => }/material.c (100%) diff --git a/src/libged/typein/typein.c b/src/libged/typein/typein.c index 3b7257c69b9..aa37842fb4f 100644 --- a/src/libged/typein/typein.c +++ b/src/libged/typein/typein.c @@ -1521,7 +1521,7 @@ material_in(struct ged *gedp, const char **cmd_argvs, struct rt_db_internal *int intern->idb_major_type = DB5_MAJORTYPE_BRLCAD; intern->idb_minor_type = DB5_MINORTYPE_BRLCAD_MATERIAL; intern->idb_meth = &OBJ[ID_MATERIAL]; - intern->idb_magic = RT_MATERIAL_MAGIC; + // intern->idb_magic = RT_MATERIAL_MAGIC; BU_ALLOC(intern->idb_ptr, struct rt_material_internal); material = (struct rt_material_internal *)intern->idb_ptr; diff --git a/src/librt/CMakeLists.txt b/src/librt/CMakeLists.txt index a3afed5ae6b..5c9000eddf6 100644 --- a/src/librt/CMakeLists.txt +++ b/src/librt/CMakeLists.txt @@ -41,6 +41,7 @@ set(LIBRT_SOURCES comb/comb_mirror.c comb/db_comb.c constraint.c + material.c cut.c cut_hlbvh.c db5_alloc.c @@ -140,7 +141,6 @@ set(LIBRT_SOURCES primitives/joint/joint.c primitives/joint/joint_brep.cpp primitives/joint/joint_mirror.c - primitives/material/material.c primitives/metaball/metaball.c primitives/metaball/metaball_tri.c primitives/mirror.c diff --git a/src/librt/primitives/material/material.c b/src/librt/material.c similarity index 100% rename from src/librt/primitives/material/material.c rename to src/librt/material.c From bcea23cae68416c91258f31cb38676c72fd1a1e9 Mon Sep 17 00:00:00 2001 From: treydinges Date: Tue, 12 Oct 2021 13:07:12 -0500 Subject: [PATCH 04/15] Name for the material is now picked up from the in command 'name'field --- src/libged/typein/typein.c | 19 ++++++----- src/librt/material.c | 60 ++++++++++++++++++++++++++-------- src/librt/primitives/table.cpp | 2 +- src/libwdb/material.c | 5 ++- 4 files changed, 59 insertions(+), 27 deletions(-) diff --git a/src/libged/typein/typein.c b/src/libged/typein/typein.c index aa37842fb4f..a6238789043 100644 --- a/src/libged/typein/typein.c +++ b/src/libged/typein/typein.c @@ -655,7 +655,7 @@ static const char *p_script[] = { * add support */ static const char *p_material[] = { - "Enter the material name: ", + "Enter the material ID: ", "Enter the material density: " }; @@ -1516,22 +1516,23 @@ sph_in(struct ged *gedp, const char **cmd_argvs, struct rt_db_internal *intern, static int material_in(struct ged *gedp, const char **cmd_argvs, struct rt_db_internal *intern) { - struct rt_material_internal *material; + struct rt_material_internal *material_ip; intern->idb_major_type = DB5_MAJORTYPE_BRLCAD; intern->idb_minor_type = DB5_MINORTYPE_BRLCAD_MATERIAL; intern->idb_meth = &OBJ[ID_MATERIAL]; - // intern->idb_magic = RT_MATERIAL_MAGIC; BU_ALLOC(intern->idb_ptr, struct rt_material_internal); - material = (struct rt_material_internal *)intern->idb_ptr; - material->magic = RT_MATERIAL_MAGIC; + material_ip = (struct rt_material_internal *)intern->idb_ptr; + material_ip->magic = RT_MATERIAL_MAGIC; - bu_vls_init(&material->name); - bu_vls_strcpy(&material->name, cmd_argvs[3]); - material->density = atof(cmd_argvs[4]) * gedp->dbip->dbi_local2base; - // mk_material(gedp->ged_wdbp, material->name.vls_str); + material_ip->id = atoi(cmd_argvs[3]); + + bu_vls_init(&material_ip->name); + bu_vls_strcpy(&material_ip->name, cmd_argvs[1]); + + material_ip->density = atof(cmd_argvs[4]) * gedp->dbip->dbi_local2base; return GED_OK; } diff --git a/src/librt/material.c b/src/librt/material.c index 9e4d7b3d428..a49027af794 100644 --- a/src/librt/material.c +++ b/src/librt/material.c @@ -40,8 +40,8 @@ static const struct bu_structparse rt_material_parse[] = { {"%d", 1, "ID", bu_offsetof(struct rt_material_internal, id), BU_STRUCTPARSE_FUNC_NULL, NULL, NULL}, - {"%V", 1, "N", bu_offsetof(struct rt_material_internal, name), BU_STRUCTPARSE_FUNC_NULL, NULL, NULL}, - {"%d", 1, "D", bu_offsetof(struct rt_material_internal, density), BU_STRUCTPARSE_FUNC_NULL, NULL, NULL}, + {"%V", 1, "Name", bu_offsetof(struct rt_material_internal, name), BU_STRUCTPARSE_FUNC_NULL, NULL, NULL}, + {"%f", 1, "Density", bu_offsetof(struct rt_material_internal, density), BU_STRUCTPARSE_FUNC_NULL, NULL, NULL}, {"", 0, (char *)0, 0, BU_STRUCTPARSE_FUNC_NULL, NULL, NULL } }; @@ -73,8 +73,7 @@ rt_material_ifree(struct rt_db_internal *ip) int rt_material_import5(struct rt_db_internal *ip, const struct bu_external *ep, const fastf_t *UNUSED(mat), const struct db_i *UNUSED(dbip)) { - struct rt_material_internal *material; - unsigned char *ptr; + struct rt_material_internal *material_ip; BU_CK_EXTERNAL(ep); RT_CK_DB_INTERNAL(ip); @@ -84,15 +83,16 @@ rt_material_import5(struct rt_db_internal *ip, const struct bu_external *ep, con ip->idb_meth = &OBJ[ID_MATERIAL]; BU_ALLOC(ip->idb_ptr, struct rt_material_internal); - material = (struct rt_material_internal *)ip->idb_ptr; - BU_VLS_INIT(&material->name); - material->magic = RT_MATERIAL_MAGIC; + material_ip = (struct rt_material_internal *)ip->idb_ptr; + BU_VLS_INIT(&material_ip->name); + material_ip->magic = RT_MATERIAL_MAGIC; - ptr = ep->ext_buf; + struct bu_vls str = BU_VLS_INIT_ZERO; + unsigned char *ptr = ep->ext_buf; + bu_vls_init(&str); + bu_vls_strncpy(&str, (char *)ptr, ep->ext_nbytes - (ptr - (unsigned char *)ep->ext_buf)); - bu_vls_init(&material->name); - bu_vls_strncpy(&material->name, (char *)ptr, - ep->ext_nbytes - (ptr - (unsigned char *)ep->ext_buf)); + bu_struct_parse(&str, rt_material_parse, (const char *)material_ip, material_ip); return 0; /* OK */ } @@ -104,17 +104,17 @@ rt_material_import5(struct rt_db_internal *ip, const struct bu_external *ep, con int rt_material_export5(struct bu_external *ep, const struct rt_db_internal *ip, double UNUSED(local2mm), const struct db_i *UNUSED(dbip)) { - struct rt_material_internal *cip; + struct rt_material_internal *material_ip; struct bu_vls str = BU_VLS_INIT_ZERO; RT_CK_DB_INTERNAL(ip); if (ip->idb_type != ID_MATERIAL) bu_bomb("rt_material_export() type not ID_MATERIAL"); - cip = (struct rt_material_internal *) ip->idb_ptr; + material_ip = (struct rt_material_internal *) ip->idb_ptr; BU_EXTERNAL_INIT(ep); - bu_vls_struct_print(&str, rt_material_parse, (char *)cip); + bu_vls_struct_print(&str, rt_material_parse, (char *)material_ip); ep->ext_nbytes = bu_vls_strlen(&str); ep->ext_buf = (uint8_t *)bu_calloc(1, ep->ext_nbytes, "material external"); @@ -125,6 +125,38 @@ rt_material_export5(struct bu_external *ep, const struct rt_db_internal *ip, dou return 0; /* OK */ } +/** + * Make human-readable formatted presentation of this object. First + * line describes type of object. Additional lines are indented one + * tab, and give parameter values. + */ +int +rt_material_describe(struct bu_vls *str, const struct rt_db_internal *ip, int verbose, double mm2local) +{ + register struct rt_material_internal *material_ip = (struct rt_material_internal *)ip->idb_ptr; + + char buf[256]; + + RT_CHECK_MATERIAL(material_ip); + bu_vls_strcat(str, "material (MATERIAL)\n"); + + sprintf(buf, "\tID: %d\n", + material_ip->id); + bu_vls_strcat(str, buf); + + sprintf(buf, "\tName: %s\n", + material_ip->name.vls_str); + bu_vls_strcat(str, buf); + + sprintf(buf, "\tDensity: %f\n", + INTCLAMP(material_ip->density * mm2local)); + bu_vls_strcat(str, buf); + + if (!verbose) return 0; + + return 0; +} + /** @} */ /* diff --git a/src/librt/primitives/table.cpp b/src/librt/primitives/table.cpp index 3368cdad94e..d59bc372276 100644 --- a/src/librt/primitives/table.cpp +++ b/src/librt/primitives/table.cpp @@ -2400,7 +2400,7 @@ const struct rt_functab OBJ[] = { NULL, /* import4 */ NULL, /* export4 */ RTFUNCTAB_FUNC_IFREE_CAST(rt_material_ifree), - NULL, /* describe */ + RTFUNCTAB_FUNC_DESCRIBE_CAST(rt_material_describe), NULL, /* xform */ NULL, /* parse */ 0, /* sizeof(internal) */ diff --git a/src/libwdb/material.c b/src/libwdb/material.c index 75c5ad0f094..efddde624b2 100644 --- a/src/libwdb/material.c +++ b/src/libwdb/material.c @@ -44,7 +44,6 @@ mk_material(struct rt_wdb *wdbp, const char *name) { /* Create a fresh new object for export */ BU_ALLOC(material, struct rt_material_internal); material->magic = RT_MATERIAL_MAGIC; - material->id = 0; BU_VLS_INIT(&material->name); intern.idb_major_type = DB5_MAJORTYPE_BRLCAD; @@ -53,8 +52,8 @@ mk_material(struct rt_wdb *wdbp, const char *name) { intern.idb_meth = &OBJ[ID_MATERIAL]; /* Add data */ - material->id = 1; - material->name.vls_str = "TEST"; + material->id = 4; + bu_vls_strcpy(&material->name, "TEST"); material->density = 123.456; /* The internal representation will be freed */ From cf8b083507666ad2cc5610d03aa3558a317d4a2a Mon Sep 17 00:00:00 2001 From: "Cole D. Connelly" Date: Thu, 14 Oct 2021 12:13:47 -0500 Subject: [PATCH 05/15] Add AVS' for materials (WIP). --- .vscode/c_cpp_properties.json | 16 ++++++++++++++++ include/rt/nongeom.h | 5 +++++ src/libged/typein/typein.c | 6 ++++++ 3 files changed, 27 insertions(+) create mode 100644 .vscode/c_cpp_properties.json diff --git a/.vscode/c_cpp_properties.json b/.vscode/c_cpp_properties.json new file mode 100644 index 00000000000..18a515732f6 --- /dev/null +++ b/.vscode/c_cpp_properties.json @@ -0,0 +1,16 @@ +{ + "configurations": [ + { + "name": "Linux", + "includePath": [ + "${default}", + "${workspaceFolder}/include" + ], + "compilerPath": "/usr/bin/gcc", + "cStandard": "gnu17", + "cppStandard": "gnu++14", + "intelliSenseMode": "linux-gcc-x64" + } + ], + "version": 4 +} \ No newline at end of file diff --git a/include/rt/nongeom.h b/include/rt/nongeom.h index 916604ad689..98744118240 100644 --- a/include/rt/nongeom.h +++ b/include/rt/nongeom.h @@ -142,6 +142,11 @@ struct rt_material_internal { int id; struct bu_vls name; double density; + + struct bu_attribute_value_set physicalProperties; + struct bu_attribute_value_set mechanicalProperties; + struct bu_attribute_value_set opticalProperties; + struct bu_attribute_value_set thermalProperties; }; #define RT_CHECK_MATERIAL(_p) BU_CKMAG(_p, RT_MATERIAL_MAGIC, "rt_material_internal") diff --git a/src/libged/typein/typein.c b/src/libged/typein/typein.c index aa37842fb4f..54b08b7b235 100644 --- a/src/libged/typein/typein.c +++ b/src/libged/typein/typein.c @@ -1533,6 +1533,12 @@ material_in(struct ged *gedp, const char **cmd_argvs, struct rt_db_internal *int material->density = atof(cmd_argvs[4]) * gedp->dbip->dbi_local2base; // mk_material(gedp->ged_wdbp, material->name.vls_str); + // Intialize AVS' + material->physicalProperties = BU_AVS_INIT_ZERO; + material->mechanicalProperties = BU_AVS_INIT_ZERO; + material->opticalProperties = BU_AVS_INIT_ZERO; + material->thermalProperties = BU_AVS_INIT_ZERO; + return GED_OK; } From 5432f24ffa7b9ecf7510ff86b9db65607205a534 Mon Sep 17 00:00:00 2001 From: treydinges Date: Fri, 15 Oct 2021 01:03:44 -0500 Subject: [PATCH 06/15] attempting to parse material avs stores --- .vscode/c_cpp_properties.json | 16 ---------------- include/bu/avs.h | 7 +++++++ src/libbu/avs.c | 31 +++++++++++++++++++++++++++++++ src/libbu/parse.c | 17 ++++++++++++++++- src/libged/typein/typein.c | 20 +++++++++++++++----- src/librt/material.c | 29 +++++++++++++++++++++++------ 6 files changed, 92 insertions(+), 28 deletions(-) delete mode 100644 .vscode/c_cpp_properties.json diff --git a/.vscode/c_cpp_properties.json b/.vscode/c_cpp_properties.json deleted file mode 100644 index 18a515732f6..00000000000 --- a/.vscode/c_cpp_properties.json +++ /dev/null @@ -1,16 +0,0 @@ -{ - "configurations": [ - { - "name": "Linux", - "includePath": [ - "${default}", - "${workspaceFolder}/include" - ], - "compilerPath": "/usr/bin/gcc", - "cStandard": "gnu17", - "cppStandard": "gnu++14", - "intelliSenseMode": "linux-gcc-x64" - } - ], - "version": 4 -} \ No newline at end of file diff --git a/include/bu/avs.h b/include/bu/avs.h index b30ab51d43a..5001371fac8 100644 --- a/include/bu/avs.h +++ b/include/bu/avs.h @@ -222,6 +222,13 @@ BU_EXPORT extern void bu_avs_merge(struct bu_attribute_value_set *dest, BU_EXPORT extern const char *bu_avs_get(const struct bu_attribute_value_set *avp, const char *attribute); +/** + * gets all attributes in an attribute set in "name = value" form, + * using the provided title. + */ +BU_EXPORT extern const char *bu_avs_get_all(const struct bu_attribute_value_set *avp, + const char *title); + /** * Remove all occurrences of an attribute from the provided attribute * set. diff --git a/src/libbu/avs.c b/src/libbu/avs.c index 9e6f4809c08..dea655341b6 100644 --- a/src/libbu/avs.c +++ b/src/libbu/avs.c @@ -198,6 +198,37 @@ bu_avs_get(const struct bu_attribute_value_set *avsp, const char *name) return NULL; } +const char * +bu_avs_get_all(const struct bu_attribute_value_set *avsp, const char *title) { + struct bu_attribute_value_pair *avpp; + size_t i; + struct bu_vls str = BU_VLS_INIT_ZERO; + + BU_CK_AVS(avsp); + + bu_vls_init(&str); + if (title) { + bu_vls_strcat(&str, title); + bu_vls_strcat(&str, "=\t"); + } + + avpp = avsp->avp; + for (i = 0; i < avsp->count; i++, avpp++) { + if (i > 0) { + bu_vls_strcat(&str, ", "); + } + bu_vls_strcat(&str, "("); + bu_vls_strcat(&str, avpp->name ? avpp->name : "NULL"); + bu_vls_strcat(&str, " : "); + bu_vls_strcat(&str, avpp->value ? avpp->value : "NULL"); + bu_vls_strcat(&str, ")"); + } + + const char * attributes = bu_vls_strgrab(&str); + + return attributes; +} + int bu_avs_remove(struct bu_attribute_value_set *avsp, const char *name) diff --git a/src/libbu/parse.c b/src/libbu/parse.c index 024eeb66f5c..e7f4b321482 100644 --- a/src/libbu/parse.c +++ b/src/libbu/parse.c @@ -29,6 +29,7 @@ #include #include "vmath.h" +#include "bu/avs.h" #include "bu/cv.h" #include "bu/log.h" #include "bu/assert.h" @@ -1487,6 +1488,19 @@ bu_vls_struct_print(struct bu_vls *vls, register const struct bu_structparse *sd bu_vls_printf(vls, "%s%s=\"%s\"", (bu_vls_strlen(vls)?" ":""), sdp->sp_name, bu_vls_addr(vls_p)); } break; + case 'v': + { + struct bu_attribute_value_set *avs_p = (struct bu_attribute_value_set *)loc; + const char *keyValuePairs = bu_avs_get_all(avs_p, NULL); + struct bu_vls tmpstr = BU_VLS_INIT_ZERO; + + bu_vls_init(&tmpstr); + bu_vls_strcat(&tmpstr, keyValuePairs); + printf("%s", tmpstr.vls_str); + bu_vls_printf(vls, "%s%s=\"%s\"", (bu_vls_strlen(vls)?" ":""), sdp->sp_name, bu_vls_addr(&tmpstr)); + bu_vls_free(&tmpstr); + } + break; case 'i': { register size_t i = sdp->sp_count; @@ -2412,7 +2426,8 @@ bu_structparse_get_terse_form(struct bu_vls *logstr, const struct bu_structparse /* These types are specified by lengths, e.g. %80s */ if (BU_STR_EQUAL(sp->sp_fmt, "%c") || BU_STR_EQUAL(sp->sp_fmt, "%s") || - BU_STR_EQUAL(sp->sp_fmt, "%V")) { + BU_STR_EQUAL(sp->sp_fmt, "%V") || + BU_STR_EQUAL(sp->sp_fmt, "%v")) { if (sp->sp_count > 1) { /* Make them all look like %###s */ bu_vls_printf(logstr, "%%%zus", sp->sp_count); diff --git a/src/libged/typein/typein.c b/src/libged/typein/typein.c index 089dedb2d18..3a569e3ce3e 100644 --- a/src/libged/typein/typein.c +++ b/src/libged/typein/typein.c @@ -1517,6 +1517,7 @@ static int material_in(struct ged *gedp, const char **cmd_argvs, struct rt_db_internal *intern) { struct rt_material_internal *material_ip; + struct bu_vls expression = BU_VLS_INIT_ZERO; intern->idb_major_type = DB5_MAJORTYPE_BRLCAD; intern->idb_minor_type = DB5_MINORTYPE_BRLCAD_MATERIAL; @@ -1534,11 +1535,20 @@ material_in(struct ged *gedp, const char **cmd_argvs, struct rt_db_internal *int material_ip->density = atof(cmd_argvs[4]) * gedp->dbip->dbi_local2base; - // Intialize AVS' - material->physicalProperties = BU_AVS_INIT_ZERO; - material->mechanicalProperties = BU_AVS_INIT_ZERO; - material->opticalProperties = BU_AVS_INIT_ZERO; - material->thermalProperties = BU_AVS_INIT_ZERO; + // Intialize AVS + bu_avs_init_empty(&material_ip->physicalProperties); + // bu_avs_init_empty(&material_ip->mechanicalProperties); + // bu_avs_init_empty(&material_ip->opticalProperties); + // bu_avs_init_empty(&material_ip->thermalProperties); + + // bu_vls_strcat(struct bu_vls *vp, const char *s); + // bu_vls_vlscat(struct bu_vls *dest, const struct bu_vls *src); + + bu_vls_from_argv(&expression, 1, &cmd_argvs[6]); + (void)bu_avs_add(&material_ip->physicalProperties, cmd_argvs[5], bu_vls_addr(&expression)); + bu_vls_free(&expression); + + bu_avs_print(&material_ip->physicalProperties, "MaterialStore"); return GED_OK; } diff --git a/src/librt/material.c b/src/librt/material.c index a49027af794..efb7730a227 100644 --- a/src/librt/material.c +++ b/src/librt/material.c @@ -42,6 +42,7 @@ static const struct bu_structparse rt_material_parse[] = { {"%d", 1, "ID", bu_offsetof(struct rt_material_internal, id), BU_STRUCTPARSE_FUNC_NULL, NULL, NULL}, {"%V", 1, "Name", bu_offsetof(struct rt_material_internal, name), BU_STRUCTPARSE_FUNC_NULL, NULL, NULL}, {"%f", 1, "Density", bu_offsetof(struct rt_material_internal, density), BU_STRUCTPARSE_FUNC_NULL, NULL, NULL}, + {"%v", 1, "physicalProperties", bu_offsetof(struct rt_material_internal, physicalProperties), BU_STRUCTPARSE_FUNC_NULL, NULL, NULL}, {"", 0, (char *)0, 0, BU_STRUCTPARSE_FUNC_NULL, NULL, NULL } }; @@ -114,8 +115,23 @@ rt_material_export5(struct bu_external *ep, const struct rt_db_internal *ip, dou BU_EXTERNAL_INIT(ep); + bu_vls_init(&str); bu_vls_struct_print(&str, rt_material_parse, (char *)material_ip); + // const char *physicalProperties = bu_avs_get_all(&material_ip->physicalProperties, "physicalProperties"); + // bu_vls_strcat(&str, physicalProperties); + + // const char *mechanicalProperties = bu_avs_get_all(&material_ip->mechanicalProperties, "mechanicalProperties"); + // bu_vls_strcat(&str, mechanicalProperties); + + // const char *opticalProperties = bu_avs_get_all(&material_ip->opticalProperties, "opticalProperties"); + // bu_vls_strcat(&str, opticalProperties); + + // const char *thermalProperties = bu_avs_get_all(&material_ip->thermalProperties, "thermalProperties"); + // bu_vls_strcat(&str, thermalProperties); + + // printf("Total vls string in export: %s", str.vls_str); + ep->ext_nbytes = bu_vls_strlen(&str); ep->ext_buf = (uint8_t *)bu_calloc(1, ep->ext_nbytes, "material external"); bu_strlcpy((char *)ep->ext_buf, bu_vls_addr(&str), ep->ext_nbytes); @@ -140,16 +156,17 @@ rt_material_describe(struct bu_vls *str, const struct rt_db_internal *ip, int ve RT_CHECK_MATERIAL(material_ip); bu_vls_strcat(str, "material (MATERIAL)\n"); - sprintf(buf, "\tID: %d\n", - material_ip->id); + sprintf(buf, "\tID: %d\n", material_ip->id); + bu_vls_strcat(str, buf); + + sprintf(buf, "\tName: %s\n", material_ip->name.vls_str); bu_vls_strcat(str, buf); - sprintf(buf, "\tName: %s\n", - material_ip->name.vls_str); + sprintf(buf, "\tDensity: %f\n", INTCLAMP(material_ip->density * mm2local)); bu_vls_strcat(str, buf); - sprintf(buf, "\tDensity: %f\n", - INTCLAMP(material_ip->density * mm2local)); + const char *physicalProperties = bu_avs_get_all(&material_ip->physicalProperties, NULL); + sprintf(buf, "\tphysicalProperties: %s\n", physicalProperties); bu_vls_strcat(str, buf); if (!verbose) return 0; From 39d30a7ccbe5e1275de873e3168116843f7174c2 Mon Sep 17 00:00:00 2001 From: volivarez60 <57726285+volivarez60@users.noreply.github.com> Date: Mon, 18 Oct 2021 20:41:30 -0500 Subject: [PATCH 07/15] Create gltf_read.cpp --- src/libgcv/plugins/gltf/gltf_read.cpp | 1 + 1 file changed, 1 insertion(+) create mode 100644 src/libgcv/plugins/gltf/gltf_read.cpp diff --git a/src/libgcv/plugins/gltf/gltf_read.cpp b/src/libgcv/plugins/gltf/gltf_read.cpp new file mode 100644 index 00000000000..cbeb52dc3b5 --- /dev/null +++ b/src/libgcv/plugins/gltf/gltf_read.cpp @@ -0,0 +1 @@ +// insert code here From 1e50b5c0a4fc7f86d7ee5ec16ba3419c94957283 Mon Sep 17 00:00:00 2001 From: treydinges Date: Tue, 19 Oct 2021 01:01:32 -0500 Subject: [PATCH 08/15] Material object with AVS store import and export --- include/rt/nongeom.h | 4 +- src/libbu/avs.c | 10 ++- src/libbu/parse.c | 17 +--- src/libged/typein/typein.c | 17 ++-- src/librt/material.c | 168 +++++++++++++++++++++++++++---------- src/libwdb/material.c | 4 +- 6 files changed, 146 insertions(+), 74 deletions(-) diff --git a/include/rt/nongeom.h b/include/rt/nongeom.h index ff313ac5f99..85f826c0088 100644 --- a/include/rt/nongeom.h +++ b/include/rt/nongeom.h @@ -139,9 +139,9 @@ struct rt_constraint_internal { */ struct rt_material_internal { uint32_t magic; - int id; struct bu_vls name; - double density; + struct bu_vls parent; + struct bu_vls source; struct bu_attribute_value_set physicalProperties; struct bu_attribute_value_set mechanicalProperties; diff --git a/src/libbu/avs.c b/src/libbu/avs.c index dea655341b6..67760a8e6b7 100644 --- a/src/libbu/avs.c +++ b/src/libbu/avs.c @@ -209,21 +209,25 @@ bu_avs_get_all(const struct bu_attribute_value_set *avsp, const char *title) { bu_vls_init(&str); if (title) { bu_vls_strcat(&str, title); - bu_vls_strcat(&str, "=\t"); + bu_vls_strcat(&str, "=\""); } avpp = avsp->avp; for (i = 0; i < avsp->count; i++, avpp++) { if (i > 0) { - bu_vls_strcat(&str, ", "); + bu_vls_strcat(&str, ","); } bu_vls_strcat(&str, "("); bu_vls_strcat(&str, avpp->name ? avpp->name : "NULL"); - bu_vls_strcat(&str, " : "); + bu_vls_strcat(&str, ":"); bu_vls_strcat(&str, avpp->value ? avpp->value : "NULL"); bu_vls_strcat(&str, ")"); } + if (title) { + bu_vls_strcat(&str, "\""); + } + const char * attributes = bu_vls_strgrab(&str); return attributes; diff --git a/src/libbu/parse.c b/src/libbu/parse.c index e7f4b321482..024eeb66f5c 100644 --- a/src/libbu/parse.c +++ b/src/libbu/parse.c @@ -29,7 +29,6 @@ #include #include "vmath.h" -#include "bu/avs.h" #include "bu/cv.h" #include "bu/log.h" #include "bu/assert.h" @@ -1488,19 +1487,6 @@ bu_vls_struct_print(struct bu_vls *vls, register const struct bu_structparse *sd bu_vls_printf(vls, "%s%s=\"%s\"", (bu_vls_strlen(vls)?" ":""), sdp->sp_name, bu_vls_addr(vls_p)); } break; - case 'v': - { - struct bu_attribute_value_set *avs_p = (struct bu_attribute_value_set *)loc; - const char *keyValuePairs = bu_avs_get_all(avs_p, NULL); - struct bu_vls tmpstr = BU_VLS_INIT_ZERO; - - bu_vls_init(&tmpstr); - bu_vls_strcat(&tmpstr, keyValuePairs); - printf("%s", tmpstr.vls_str); - bu_vls_printf(vls, "%s%s=\"%s\"", (bu_vls_strlen(vls)?" ":""), sdp->sp_name, bu_vls_addr(&tmpstr)); - bu_vls_free(&tmpstr); - } - break; case 'i': { register size_t i = sdp->sp_count; @@ -2426,8 +2412,7 @@ bu_structparse_get_terse_form(struct bu_vls *logstr, const struct bu_structparse /* These types are specified by lengths, e.g. %80s */ if (BU_STR_EQUAL(sp->sp_fmt, "%c") || BU_STR_EQUAL(sp->sp_fmt, "%s") || - BU_STR_EQUAL(sp->sp_fmt, "%V") || - BU_STR_EQUAL(sp->sp_fmt, "%v")) { + BU_STR_EQUAL(sp->sp_fmt, "%V")) { if (sp->sp_count > 1) { /* Make them all look like %###s */ bu_vls_printf(logstr, "%%%zus", sp->sp_count); diff --git a/src/libged/typein/typein.c b/src/libged/typein/typein.c index 3a569e3ce3e..dbebd7b6b38 100644 --- a/src/libged/typein/typein.c +++ b/src/libged/typein/typein.c @@ -655,8 +655,9 @@ static const char *p_script[] = { * add support */ static const char *p_material[] = { - "Enter the material ID: ", - "Enter the material density: " + "Please enter the key value pairs" + // "Enter the material ID: ", + // "Enter the material density: " }; /** @@ -1514,7 +1515,7 @@ sph_in(struct ged *gedp, const char **cmd_argvs, struct rt_db_internal *intern, } static int -material_in(struct ged *gedp, const char **cmd_argvs, struct rt_db_internal *intern) +material_in(struct ged *UNUSED(gedp), const char **cmd_argvs, struct rt_db_internal *intern) { struct rt_material_internal *material_ip; struct bu_vls expression = BU_VLS_INIT_ZERO; @@ -1528,18 +1529,18 @@ material_in(struct ged *gedp, const char **cmd_argvs, struct rt_db_internal *int material_ip->magic = RT_MATERIAL_MAGIC; - material_ip->id = atoi(cmd_argvs[3]); + // material_ip->id = atoi(cmd_argvs[3]); bu_vls_init(&material_ip->name); bu_vls_strcpy(&material_ip->name, cmd_argvs[1]); - material_ip->density = atof(cmd_argvs[4]) * gedp->dbip->dbi_local2base; + // material_ip->density = atof(cmd_argvs[4]) * gedp->dbip->dbi_local2base; // Intialize AVS bu_avs_init_empty(&material_ip->physicalProperties); - // bu_avs_init_empty(&material_ip->mechanicalProperties); - // bu_avs_init_empty(&material_ip->opticalProperties); - // bu_avs_init_empty(&material_ip->thermalProperties); + bu_avs_init_empty(&material_ip->mechanicalProperties); + bu_avs_init_empty(&material_ip->opticalProperties); + bu_avs_init_empty(&material_ip->thermalProperties); // bu_vls_strcat(struct bu_vls *vp, const char *s); // bu_vls_vlscat(struct bu_vls *dest, const struct bu_vls *src); diff --git a/src/librt/material.c b/src/librt/material.c index efb7730a227..71c85333ed0 100644 --- a/src/librt/material.c +++ b/src/librt/material.c @@ -30,7 +30,7 @@ #include "common.h" #include - +#include #include "bn.h" #include "rt/db4.h" @@ -38,15 +38,6 @@ #include "raytrace.h" -static const struct bu_structparse rt_material_parse[] = { - {"%d", 1, "ID", bu_offsetof(struct rt_material_internal, id), BU_STRUCTPARSE_FUNC_NULL, NULL, NULL}, - {"%V", 1, "Name", bu_offsetof(struct rt_material_internal, name), BU_STRUCTPARSE_FUNC_NULL, NULL, NULL}, - {"%f", 1, "Density", bu_offsetof(struct rt_material_internal, density), BU_STRUCTPARSE_FUNC_NULL, NULL, NULL}, - {"%v", 1, "physicalProperties", bu_offsetof(struct rt_material_internal, physicalProperties), BU_STRUCTPARSE_FUNC_NULL, NULL, NULL}, - {"", 0, (char *)0, 0, BU_STRUCTPARSE_FUNC_NULL, NULL, NULL } -}; - - /** * Free the storage associated with the rt_db_internal version of * material object. @@ -88,12 +79,69 @@ rt_material_import5(struct rt_db_internal *ip, const struct bu_external *ep, con BU_VLS_INIT(&material_ip->name); material_ip->magic = RT_MATERIAL_MAGIC; - struct bu_vls str = BU_VLS_INIT_ZERO; - unsigned char *ptr = ep->ext_buf; - bu_vls_init(&str); - bu_vls_strncpy(&str, (char *)ptr, ep->ext_nbytes - (ptr - (unsigned char *)ep->ext_buf)); - - bu_struct_parse(&str, rt_material_parse, (const char *)material_ip, material_ip); + struct bu_vls name = BU_VLS_INIT_ZERO; + unsigned char *cp = ep->ext_buf; + + // copy name to internal format + bu_vls_strcat(&name, (char *)cp); + bu_vls_vlscat(&material_ip->name, &name); + cp += strlen((const char *)cp) + 1; + + // copy physical properties + uint32_t size = ntohl(*(uint32_t *)cp); + cp += SIZEOF_NETWORK_LONG; + + struct bu_external physical_ep = BU_EXTERNAL_INIT_ZERO; + physical_ep.ext_nbytes = size; + physical_ep.ext_buf = cp; + if (size > 0) { + db5_import_attributes(&material_ip->physicalProperties, &physical_ep); + } else { + bu_avs_init_empty(&material_ip->physicalProperties); + } + cp += size; + + // copy mechanical properties + size = ntohl(*(uint32_t *)cp); + cp += SIZEOF_NETWORK_LONG; + + struct bu_external mechanical_ep = BU_EXTERNAL_INIT_ZERO; + mechanical_ep.ext_nbytes = size; + mechanical_ep.ext_buf = cp; + if (size > 0) { + db5_import_attributes(&material_ip->mechanicalProperties, &mechanical_ep); + } else { + bu_avs_init_empty(&material_ip->mechanicalProperties); + } + cp += size; + + // copy optical properties + size = ntohl(*(uint32_t *)cp); + cp += SIZEOF_NETWORK_LONG; + + struct bu_external optical_ep = BU_EXTERNAL_INIT_ZERO; + optical_ep.ext_nbytes = size; + optical_ep.ext_buf = cp; + if (size > 0) { + db5_import_attributes(&material_ip->opticalProperties, &optical_ep); + } else { + bu_avs_init_empty(&material_ip->opticalProperties); + } + cp += size; + + // copy thermal properties + size = ntohl(*(uint32_t *)cp); + cp += SIZEOF_NETWORK_LONG; + + struct bu_external thermal_ep = BU_EXTERNAL_INIT_ZERO; + thermal_ep.ext_nbytes = size; + thermal_ep.ext_buf = cp; + if (size > 0) { + db5_import_attributes(&material_ip->thermalProperties, &thermal_ep); + } else { + bu_avs_init_empty(&material_ip->thermalProperties); + } + cp += size; return 0; /* OK */ } @@ -106,37 +154,59 @@ int rt_material_export5(struct bu_external *ep, const struct rt_db_internal *ip, double UNUSED(local2mm), const struct db_i *UNUSED(dbip)) { struct rt_material_internal *material_ip; - struct bu_vls str = BU_VLS_INIT_ZERO; + struct bu_vls name = BU_VLS_INIT_ZERO; + struct bu_external physical_ep = BU_EXTERNAL_INIT_ZERO; + struct bu_external mechanical_ep = BU_EXTERNAL_INIT_ZERO; + struct bu_external optical_ep = BU_EXTERNAL_INIT_ZERO; + struct bu_external thermal_ep = BU_EXTERNAL_INIT_ZERO; RT_CK_DB_INTERNAL(ip); if (ip->idb_type != ID_MATERIAL) bu_bomb("rt_material_export() type not ID_MATERIAL"); - material_ip = (struct rt_material_internal *) ip->idb_ptr; + material_ip = (struct rt_material_internal *)ip->idb_ptr; BU_EXTERNAL_INIT(ep); - bu_vls_init(&str); - bu_vls_struct_print(&str, rt_material_parse, (char *)material_ip); - - // const char *physicalProperties = bu_avs_get_all(&material_ip->physicalProperties, "physicalProperties"); - // bu_vls_strcat(&str, physicalProperties); - - // const char *mechanicalProperties = bu_avs_get_all(&material_ip->mechanicalProperties, "mechanicalProperties"); - // bu_vls_strcat(&str, mechanicalProperties); - - // const char *opticalProperties = bu_avs_get_all(&material_ip->opticalProperties, "opticalProperties"); - // bu_vls_strcat(&str, opticalProperties); - - // const char *thermalProperties = bu_avs_get_all(&material_ip->thermalProperties, "thermalProperties"); - // bu_vls_strcat(&str, thermalProperties); + db5_export_attributes(&physical_ep, &material_ip->physicalProperties); + db5_export_attributes(&mechanical_ep, &material_ip->mechanicalProperties); + db5_export_attributes(&optical_ep, &material_ip->opticalProperties); + db5_export_attributes(&thermal_ep, &material_ip->thermalProperties); - // printf("Total vls string in export: %s", str.vls_str); - - ep->ext_nbytes = bu_vls_strlen(&str); + // initialize entire buffer + ep->ext_nbytes = bu_vls_strlen(&material_ip->name) + 1 + (4 * SIZEOF_NETWORK_LONG) + physical_ep.ext_nbytes + mechanical_ep.ext_nbytes + optical_ep.ext_nbytes + thermal_ep.ext_nbytes; + // ep->ext_nbytes = bu_vls_strlen(&material_ip->name) + 1 + SIZEOF_NETWORK_LONG + physical_ep.ext_nbytes; ep->ext_buf = (uint8_t *)bu_calloc(1, ep->ext_nbytes, "material external"); - bu_strlcpy((char *)ep->ext_buf, bu_vls_addr(&str), ep->ext_nbytes); - - bu_vls_free(&str); + unsigned char *cp = ep->ext_buf; + + // copy over the name to buffer + bu_vls_vlscat(&name, &material_ip->name); + bu_strlcpy((char *)cp, bu_vls_cstr(&name), bu_vls_strlen(&name) + 1); + cp += bu_vls_strlen(&name) + 1; + bu_vls_free(&name); + + // copy physical properties + *(uint32_t *)cp = htonl(physical_ep.ext_nbytes); + cp += SIZEOF_NETWORK_LONG; + memcpy(cp, physical_ep.ext_buf, physical_ep.ext_nbytes); + cp += physical_ep.ext_nbytes; + + // copy mechanical properties + *(uint32_t *)cp = htonl(mechanical_ep.ext_nbytes); + cp += SIZEOF_NETWORK_LONG; + memcpy(cp, mechanical_ep.ext_buf, mechanical_ep.ext_nbytes); + cp += mechanical_ep.ext_nbytes; + + // copy optical properties + *(uint32_t *)cp = htonl(optical_ep.ext_nbytes); + cp += SIZEOF_NETWORK_LONG; + memcpy(cp, optical_ep.ext_buf, optical_ep.ext_nbytes); + cp += optical_ep.ext_nbytes; + + // copy thermal properties + *(uint32_t *)cp = htonl(thermal_ep.ext_nbytes); + cp += SIZEOF_NETWORK_LONG; + memcpy(cp, thermal_ep.ext_buf, thermal_ep.ext_nbytes); + cp += thermal_ep.ext_nbytes; return 0; /* OK */ } @@ -147,7 +217,7 @@ rt_material_export5(struct bu_external *ep, const struct rt_db_internal *ip, dou * tab, and give parameter values. */ int -rt_material_describe(struct bu_vls *str, const struct rt_db_internal *ip, int verbose, double mm2local) +rt_material_describe(struct bu_vls *str, const struct rt_db_internal *ip, int verbose, double UNUSED(mm2local)) { register struct rt_material_internal *material_ip = (struct rt_material_internal *)ip->idb_ptr; @@ -156,20 +226,32 @@ rt_material_describe(struct bu_vls *str, const struct rt_db_internal *ip, int ve RT_CHECK_MATERIAL(material_ip); bu_vls_strcat(str, "material (MATERIAL)\n"); - sprintf(buf, "\tID: %d\n", material_ip->id); + sprintf(buf, "\tName: %s\n", material_ip->name.vls_str); bu_vls_strcat(str, buf); - sprintf(buf, "\tName: %s\n", material_ip->name.vls_str); + sprintf(buf, "\tParent: %s\n", material_ip->parent.vls_str); bu_vls_strcat(str, buf); - sprintf(buf, "\tDensity: %f\n", INTCLAMP(material_ip->density * mm2local)); + sprintf(buf, "\tSource: %s\n", material_ip->source.vls_str); bu_vls_strcat(str, buf); + if (!verbose) return 0; + const char *physicalProperties = bu_avs_get_all(&material_ip->physicalProperties, NULL); sprintf(buf, "\tphysicalProperties: %s\n", physicalProperties); bu_vls_strcat(str, buf); - if (!verbose) return 0; + const char *mechanicalProperties = bu_avs_get_all(&material_ip->mechanicalProperties, NULL); + sprintf(buf, "\tmechanicalProperties: %s\n", mechanicalProperties); + bu_vls_strcat(str, buf); + + const char *opticalProperties = bu_avs_get_all(&material_ip->opticalProperties, NULL); + sprintf(buf, "\topticalProperties: %s\n", opticalProperties); + bu_vls_strcat(str, buf); + + const char *thermalProperties = bu_avs_get_all(&material_ip->thermalProperties, NULL); + sprintf(buf, "\tthermalProperties: %s\n", thermalProperties); + bu_vls_strcat(str, buf); return 0; } diff --git a/src/libwdb/material.c b/src/libwdb/material.c index efddde624b2..5d611e0f906 100644 --- a/src/libwdb/material.c +++ b/src/libwdb/material.c @@ -52,9 +52,9 @@ mk_material(struct rt_wdb *wdbp, const char *name) { intern.idb_meth = &OBJ[ID_MATERIAL]; /* Add data */ - material->id = 4; + // material->id = 4; bu_vls_strcpy(&material->name, "TEST"); - material->density = 123.456; + // material->density = 123.456; /* The internal representation will be freed */ return wdb_put_internal(wdbp, name, &intern, mk_conv2mm); From 574ee714e2e1c38c45160874927508a058f6870f Mon Sep 17 00:00:00 2001 From: treydinges Date: Tue, 19 Oct 2021 12:27:53 -0500 Subject: [PATCH 09/15] Completed AVS store implementation with in command --- src/libbu/avs.c | 9 +- src/libged/typein/typein.c | 199 +++++++++++++++++++++++++++++++++---- src/librt/material.c | 70 ++++++++----- 3 files changed, 229 insertions(+), 49 deletions(-) diff --git a/src/libbu/avs.c b/src/libbu/avs.c index 67760a8e6b7..49f76b3946f 100644 --- a/src/libbu/avs.c +++ b/src/libbu/avs.c @@ -214,14 +214,11 @@ bu_avs_get_all(const struct bu_attribute_value_set *avsp, const char *title) { avpp = avsp->avp; for (i = 0; i < avsp->count; i++, avpp++) { - if (i > 0) { - bu_vls_strcat(&str, ","); - } - bu_vls_strcat(&str, "("); + bu_vls_strcat(&str, "\t\t("); bu_vls_strcat(&str, avpp->name ? avpp->name : "NULL"); - bu_vls_strcat(&str, ":"); + bu_vls_strcat(&str, " : "); bu_vls_strcat(&str, avpp->value ? avpp->value : "NULL"); - bu_vls_strcat(&str, ")"); + bu_vls_strcat(&str, ")\n"); } if (title) { diff --git a/src/libged/typein/typein.c b/src/libged/typein/typein.c index dbebd7b6b38..8ad7c95a311 100644 --- a/src/libged/typein/typein.c +++ b/src/libged/typein/typein.c @@ -655,9 +655,13 @@ static const char *p_script[] = { * add support */ static const char *p_material[] = { - "Please enter the key value pairs" - // "Enter the material ID: ", - // "Enter the material density: " + "Enter the material name: ", + "Enter the parent material name: ", + "Enter the material source: ", + "Enter key value pairs for physical properties as separate arguments (end or skip with .): ", + "Enter key value pairs for mechanical properties as separate arguments (end or skip with .): ", + "Enter key value pairs for optical properties as separate arguments (end or skip with .): ", + "Enter key value pairs for thermal properties as separate arguments (end or skip with .): ", }; /** @@ -1515,10 +1519,9 @@ sph_in(struct ged *gedp, const char **cmd_argvs, struct rt_db_internal *intern, } static int -material_in(struct ged *UNUSED(gedp), const char **cmd_argvs, struct rt_db_internal *intern) +material_in(struct ged *gedp, int argc, const char **cmd_argvs, struct rt_db_internal *intern) { struct rt_material_internal *material_ip; - struct bu_vls expression = BU_VLS_INIT_ZERO; intern->idb_major_type = DB5_MAJORTYPE_BRLCAD; intern->idb_minor_type = DB5_MINORTYPE_BRLCAD_MATERIAL; @@ -1528,28 +1531,175 @@ material_in(struct ged *UNUSED(gedp), const char **cmd_argvs, struct rt_db_inter material_ip = (struct rt_material_internal *)intern->idb_ptr; material_ip->magic = RT_MATERIAL_MAGIC; - - // material_ip->id = atoi(cmd_argvs[3]); - + // name, parent, and source bu_vls_init(&material_ip->name); - bu_vls_strcpy(&material_ip->name, cmd_argvs[1]); + bu_vls_strcpy(&material_ip->name, cmd_argvs[3]); - // material_ip->density = atof(cmd_argvs[4]) * gedp->dbip->dbi_local2base; + bu_vls_init(&material_ip->parent); + bu_vls_strcpy(&material_ip->parent, cmd_argvs[4]); - // Intialize AVS + bu_vls_init(&material_ip->source); + bu_vls_strcpy(&material_ip->source, cmd_argvs[5]); + + // Intialize AVS stores bu_avs_init_empty(&material_ip->physicalProperties); bu_avs_init_empty(&material_ip->mechanicalProperties); bu_avs_init_empty(&material_ip->opticalProperties); bu_avs_init_empty(&material_ip->thermalProperties); - // bu_vls_strcat(struct bu_vls *vp, const char *s); - // bu_vls_vlscat(struct bu_vls *dest, const struct bu_vls *src); - - bu_vls_from_argv(&expression, 1, &cmd_argvs[6]); - (void)bu_avs_add(&material_ip->physicalProperties, cmd_argvs[5], bu_vls_addr(&expression)); - bu_vls_free(&expression); + if (argc % 2 != 0) { + bu_vls_printf(gedp->ged_result_str, "ERROR, key value pairs entered incorrectly!\n"); + return GED_ERROR; + } - bu_avs_print(&material_ip->physicalProperties, "MaterialStore"); + int arg_idx = 6; + int arg_ptr = 7; + while (1) { + if (argc < arg_ptr) { + bu_vls_printf(gedp->ged_result_str, "ERROR, not enough arguments!\n"); + return GED_ERROR; + } + + if (BU_STR_EQUAL(".", cmd_argvs[arg_idx]) && arg_ptr % 2 == 0) { + bu_vls_printf(gedp->ged_result_str, "ERROR, key value pairs entered incorrectly!\n"); + return GED_ERROR; + } + + if (BU_STR_EQUAL(".", cmd_argvs[arg_idx])) { + // increment counters to get key values in next loop + arg_idx += 1; + arg_ptr += 1; + break; + } + + // have to check the next arg after we know it is not '.' + if (argc < arg_ptr + 1) { + bu_vls_printf(gedp->ged_result_str, "ERROR, not enough arguments!\n"); + return GED_ERROR; + } + + if (BU_STR_EQUAL(".", cmd_argvs[arg_idx + 1]) && (arg_ptr + 1) % 2 != 0) { + bu_vls_printf(gedp->ged_result_str, "ERROR, key value pairs entered incorrectly!\n"); + return GED_ERROR; + } + + // if we make it here we have a valid key value set for argv[arg_idx] and argv[arg_idx + 1] + (void)bu_avs_add(&material_ip->physicalProperties, cmd_argvs[arg_idx], cmd_argvs[arg_idx + 1]); + + // increment the counters by two so we can get the next pair + arg_idx += 2; + arg_ptr += 2; + } + + while (1) { + if (argc < arg_ptr) { + bu_vls_printf(gedp->ged_result_str, "ERROR, not enough arguments!\n"); + return GED_ERROR; + } + + if (BU_STR_EQUAL(".", cmd_argvs[arg_idx]) && arg_ptr % 2 != 0) { + bu_vls_printf(gedp->ged_result_str, "ERROR, key value pairs entered incorrectly!\n"); + return GED_ERROR; + } + + if (BU_STR_EQUAL(".", cmd_argvs[arg_idx])) { + // increment counters to get key values in next loop + arg_idx += 1; + arg_ptr += 1; + break; + } + + // have to check the next arg after we know it is not '.' + if (argc < arg_ptr + 1) { + bu_vls_printf(gedp->ged_result_str, "ERROR, not enough arguments!\n"); + return GED_ERROR; + } + + if (BU_STR_EQUAL(".", cmd_argvs[arg_idx + 1]) && (arg_ptr + 1) % 2 == 0) { + bu_vls_printf(gedp->ged_result_str, "ERROR, key value pairs entered incorrectly!\n"); + return GED_ERROR; + } + + // if we make it here we have a valid key value set for argv[arg_idx] and argv[arg_idx + 1] + (void)bu_avs_add(&material_ip->mechanicalProperties, cmd_argvs[arg_idx], cmd_argvs[arg_idx + 1]); + + // increment the counters by two so we can get the next pair + arg_idx += 2; + arg_ptr += 2; + } + + while (1) { + if (argc < arg_ptr) { + bu_vls_printf(gedp->ged_result_str, "ERROR, not enough arguments!\n"); + return GED_ERROR; + } + + if (BU_STR_EQUAL(".", cmd_argvs[arg_idx]) && arg_ptr % 2 == 0) { + bu_vls_printf(gedp->ged_result_str, "ERROR, key value pairs entered incorrectly!\n"); + return GED_ERROR; + } + + if (BU_STR_EQUAL(".", cmd_argvs[arg_idx])) { + // increment counters to get key values in next loop + arg_idx += 1; + arg_ptr += 1; + break; + } + + // have to check the next arg after we know it is not '.' + if (argc < arg_ptr + 1) { + bu_vls_printf(gedp->ged_result_str, "ERROR, not enough arguments!\n"); + return GED_ERROR; + } + + if (BU_STR_EQUAL(".", cmd_argvs[arg_idx + 1]) && (arg_ptr + 1) % 2 != 0) { + bu_vls_printf(gedp->ged_result_str, "ERROR, key value pairs entered incorrectly!\n"); + return GED_ERROR; + } + + // if we make it here we have a valid key value set for argv[arg_idx] and argv[arg_idx + 1] + (void)bu_avs_add(&material_ip->opticalProperties, cmd_argvs[arg_idx], cmd_argvs[arg_idx + 1]); + + // increment the counters by two so we can get the next pair + arg_idx += 2; + arg_ptr += 2; + } + + while (1) { + if (argc < arg_ptr) { + bu_vls_printf(gedp->ged_result_str, "ERROR, not enough arguments!\n"); + return GED_ERROR; + } + + if (BU_STR_EQUAL(".", cmd_argvs[arg_idx]) && arg_ptr % 2 != 0) { + bu_vls_printf(gedp->ged_result_str, "ERROR, key value pairs entered incorrectly!\n"); + return GED_ERROR; + } + + if (BU_STR_EQUAL(".", cmd_argvs[arg_idx])) { + arg_idx += 1; + arg_ptr += 1; + break; + } + + // have to check the next arg after we know it is not '.' + if (argc < arg_ptr + 1) { + bu_vls_printf(gedp->ged_result_str, "ERROR, not enough arguments!\n"); + return GED_ERROR; + } + + if (BU_STR_EQUAL(".", cmd_argvs[arg_idx + 1]) && (arg_ptr + 1) % 2 == 0) { + bu_vls_printf(gedp->ged_result_str, "ERROR, key value pairs entered incorrectly!\n"); + return GED_ERROR; + } + + // if we make it here we have a valid key value set for argv[arg_idx] and argv[arg_idx + 1] + (void)bu_avs_add(&material_ip->thermalProperties, cmd_argvs[arg_idx], cmd_argvs[arg_idx + 1]); + + // increment the counters by two so we can get the next pair + arg_idx += 2; + arg_ptr += 2; + } return GED_OK; } @@ -3465,9 +3615,18 @@ ged_in_core(struct ged *gedp, int argc, const char *argv[]) menu = p_script; fn_in = script_in; } else if (BU_STR_EQUAL(argv[2], "material")) { - nvals = 2; + nvals = 7; menu = p_material; - fn_in = material_in; + switch (material_in(gedp, argc, argv, &internal)) { + case GED_ERROR: + bu_vls_printf(gedp->ged_result_str, "%s: ERROR, material not made!\n", argv[0]); + rt_db_free_internal(&internal); + return GED_ERROR; + case GED_MORE: + return GED_MORE; + } + + goto do_new_update; } else if (BU_STR_EQUAL(argv[2], "pnts")) { switch (pnts_in(gedp, argc, argv, &internal, p_pnts)) { case GED_ERROR: diff --git a/src/librt/material.c b/src/librt/material.c index 71c85333ed0..0533a31d3a7 100644 --- a/src/librt/material.c +++ b/src/librt/material.c @@ -76,10 +76,14 @@ rt_material_import5(struct rt_db_internal *ip, const struct bu_external *ep, con BU_ALLOC(ip->idb_ptr, struct rt_material_internal); material_ip = (struct rt_material_internal *)ip->idb_ptr; - BU_VLS_INIT(&material_ip->name); material_ip->magic = RT_MATERIAL_MAGIC; + BU_VLS_INIT(&material_ip->name); + BU_VLS_INIT(&material_ip->parent); + BU_VLS_INIT(&material_ip->source); struct bu_vls name = BU_VLS_INIT_ZERO; + struct bu_vls parent = BU_VLS_INIT_ZERO; + struct bu_vls source = BU_VLS_INIT_ZERO; unsigned char *cp = ep->ext_buf; // copy name to internal format @@ -87,6 +91,16 @@ rt_material_import5(struct rt_db_internal *ip, const struct bu_external *ep, con bu_vls_vlscat(&material_ip->name, &name); cp += strlen((const char *)cp) + 1; + // copy parent to internal format + bu_vls_strcat(&parent, (char *)cp); + bu_vls_vlscat(&material_ip->parent, &parent); + cp += strlen((const char *)cp) + 1; + + // copy source to internal format + bu_vls_strcat(&source, (char *)cp); + bu_vls_vlscat(&material_ip->source, &source); + cp += strlen((const char *)cp) + 1; + // copy physical properties uint32_t size = ntohl(*(uint32_t *)cp); cp += SIZEOF_NETWORK_LONG; @@ -155,6 +169,8 @@ rt_material_export5(struct bu_external *ep, const struct rt_db_internal *ip, dou { struct rt_material_internal *material_ip; struct bu_vls name = BU_VLS_INIT_ZERO; + struct bu_vls parent = BU_VLS_INIT_ZERO; + struct bu_vls source = BU_VLS_INIT_ZERO; struct bu_external physical_ep = BU_EXTERNAL_INIT_ZERO; struct bu_external mechanical_ep = BU_EXTERNAL_INIT_ZERO; struct bu_external optical_ep = BU_EXTERNAL_INIT_ZERO; @@ -173,8 +189,14 @@ rt_material_export5(struct bu_external *ep, const struct rt_db_internal *ip, dou db5_export_attributes(&thermal_ep, &material_ip->thermalProperties); // initialize entire buffer - ep->ext_nbytes = bu_vls_strlen(&material_ip->name) + 1 + (4 * SIZEOF_NETWORK_LONG) + physical_ep.ext_nbytes + mechanical_ep.ext_nbytes + optical_ep.ext_nbytes + thermal_ep.ext_nbytes; - // ep->ext_nbytes = bu_vls_strlen(&material_ip->name) + 1 + SIZEOF_NETWORK_LONG + physical_ep.ext_nbytes; + ep->ext_nbytes = bu_vls_strlen(&material_ip->name) + 1 + + bu_vls_strlen(&material_ip->parent) + 1 + + bu_vls_strlen(&material_ip->source) + 1 + + (4 * SIZEOF_NETWORK_LONG) + + physical_ep.ext_nbytes + + mechanical_ep.ext_nbytes + + optical_ep.ext_nbytes + + thermal_ep.ext_nbytes; ep->ext_buf = (uint8_t *)bu_calloc(1, ep->ext_nbytes, "material external"); unsigned char *cp = ep->ext_buf; @@ -184,6 +206,18 @@ rt_material_export5(struct bu_external *ep, const struct rt_db_internal *ip, dou cp += bu_vls_strlen(&name) + 1; bu_vls_free(&name); + // copy over the parent to buffer + bu_vls_vlscat(&parent, &material_ip->parent); + bu_strlcpy((char *)cp, bu_vls_cstr(&parent), bu_vls_strlen(&parent) + 1); + cp += bu_vls_strlen(&parent) + 1; + bu_vls_free(&parent); + + // copy over the source to buffer + bu_vls_vlscat(&source, &material_ip->source); + bu_strlcpy((char *)cp, bu_vls_cstr(&source), bu_vls_strlen(&source) + 1); + cp += bu_vls_strlen(&source) + 1; + bu_vls_free(&source); + // copy physical properties *(uint32_t *)cp = htonl(physical_ep.ext_nbytes); cp += SIZEOF_NETWORK_LONG; @@ -221,37 +255,27 @@ rt_material_describe(struct bu_vls *str, const struct rt_db_internal *ip, int ve { register struct rt_material_internal *material_ip = (struct rt_material_internal *)ip->idb_ptr; - char buf[256]; + struct bu_vls buf = BU_VLS_INIT_ZERO; RT_CHECK_MATERIAL(material_ip); bu_vls_strcat(str, "material (MATERIAL)\n"); - sprintf(buf, "\tName: %s\n", material_ip->name.vls_str); - bu_vls_strcat(str, buf); - - sprintf(buf, "\tParent: %s\n", material_ip->parent.vls_str); - bu_vls_strcat(str, buf); - - sprintf(buf, "\tSource: %s\n", material_ip->source.vls_str); - bu_vls_strcat(str, buf); + bu_vls_printf(&buf, "\tName: %s\n", material_ip->name.vls_str); + bu_vls_printf(&buf, "\tParent: %s\n", material_ip->parent.vls_str); + bu_vls_printf(&buf, "\tSource: %s\n", material_ip->source.vls_str); if (!verbose) return 0; const char *physicalProperties = bu_avs_get_all(&material_ip->physicalProperties, NULL); - sprintf(buf, "\tphysicalProperties: %s\n", physicalProperties); - bu_vls_strcat(str, buf); - const char *mechanicalProperties = bu_avs_get_all(&material_ip->mechanicalProperties, NULL); - sprintf(buf, "\tmechanicalProperties: %s\n", mechanicalProperties); - bu_vls_strcat(str, buf); - const char *opticalProperties = bu_avs_get_all(&material_ip->opticalProperties, NULL); - sprintf(buf, "\topticalProperties: %s\n", opticalProperties); - bu_vls_strcat(str, buf); - const char *thermalProperties = bu_avs_get_all(&material_ip->thermalProperties, NULL); - sprintf(buf, "\tthermalProperties: %s\n", thermalProperties); - bu_vls_strcat(str, buf); + + bu_vls_printf(&buf, "\tphysicalProperties: \n%s", physicalProperties); + bu_vls_printf(&buf, "\topticalProperties: \n%s", opticalProperties); + bu_vls_printf(&buf, "\tmechanicalProperties: \n%s", mechanicalProperties); + bu_vls_printf(&buf, "\tthermalProperties: \n%s", thermalProperties); + bu_vls_vlscat(str, &buf); return 0; } From 04e8548b2f113e83a07e9725bc0d85fe48e8d893 Mon Sep 17 00:00:00 2001 From: treydinges Date: Tue, 19 Oct 2021 17:27:06 -0500 Subject: [PATCH 10/15] mk_material implementation --- include/wdb.h | 10 +++++++++- src/libwdb/material.c | 45 +++++++++++++++++++++++++++++-------------- 2 files changed, 40 insertions(+), 15 deletions(-) diff --git a/include/wdb.h b/include/wdb.h index 402a8926534..c0ccd236f00 100644 --- a/include/wdb.h +++ b/include/wdb.h @@ -347,7 +347,15 @@ WDB_EXPORT extern int mk_ars(struct rt_wdb *fp, const char *name, size_t ncurves WDB_EXPORT extern int mk_constraint(struct rt_wdb *wdbp, const char *name, const char *expr); -WDB_EXPORT extern int mk_material(struct rt_wdb *wdbp, const char *name); +WDB_EXPORT extern int mk_material(struct rt_wdb *wdbp, + const char *db_name, + const char *name, + const char *parent, + const char *source, + struct bu_attribute_value_set *physicalProperties, + struct bu_attribute_value_set *mechanicalProperties, + struct bu_attribute_value_set *opticalProperties, + struct bu_attribute_value_set *thermalProperties); /* FIXME: are the variable-sized types actually necessary? should be diff --git a/src/libwdb/material.c b/src/libwdb/material.c index 5d611e0f906..791b49464c4 100644 --- a/src/libwdb/material.c +++ b/src/libwdb/material.c @@ -33,29 +33,46 @@ int -mk_material(struct rt_wdb *wdbp, const char *name) { - struct rt_db_internal intern; - struct rt_material_internal *material; - +mk_material(struct rt_wdb *wdbp, + const char *db_name, + const char *name, + const char *parent, + const char *source, + struct bu_attribute_value_set *physicalProperties, + struct bu_attribute_value_set *mechanicalProperties, + struct bu_attribute_value_set *opticalProperties, + struct bu_attribute_value_set *thermalProperties) { RT_CK_WDB(wdbp); - RT_DB_INTERNAL_INIT(&intern); - /* Create a fresh new object for export */ - BU_ALLOC(material, struct rt_material_internal); - material->magic = RT_MATERIAL_MAGIC; - BU_VLS_INIT(&material->name); + struct rt_material_internal *material_ip; + BU_ALLOC(material_ip, struct rt_material_internal); + material_ip->magic = RT_MATERIAL_MAGIC; + BU_VLS_INIT(&material_ip->name); + BU_VLS_INIT(&material_ip->parent); + BU_VLS_INIT(&material_ip->source); + bu_avs_init_empty(&material_ip->physicalProperties); + bu_avs_init_empty(&material_ip->mechanicalProperties); + bu_avs_init_empty(&material_ip->opticalProperties); + bu_avs_init_empty(&material_ip->thermalProperties); + struct rt_db_internal intern; + RT_DB_INTERNAL_INIT(&intern); intern.idb_major_type = DB5_MAJORTYPE_BRLCAD; intern.idb_type = ID_MATERIAL; - intern.idb_ptr = (void *)material; + intern.idb_ptr = (void *)material_ip; intern.idb_meth = &OBJ[ID_MATERIAL]; /* Add data */ - // material->id = 4; - bu_vls_strcpy(&material->name, "TEST"); - // material->density = 123.456; + bu_vls_strcpy(&material_ip->name, name); + bu_vls_strcpy(&material_ip->parent, parent); + bu_vls_strcpy(&material_ip->source, source); + + bu_avs_merge(&material_ip->physicalProperties, physicalProperties); + bu_avs_merge(&material_ip->mechanicalProperties, mechanicalProperties); + bu_avs_merge(&material_ip->opticalProperties, opticalProperties); + bu_avs_merge(&material_ip->thermalProperties, thermalProperties); /* The internal representation will be freed */ - return wdb_put_internal(wdbp, name, &intern, mk_conv2mm); + return wdb_put_internal(wdbp, db_name, &intern, mk_conv2mm); } \ No newline at end of file From cf4c0b868b2de1bfbea1158a65e56576b16f59a9 Mon Sep 17 00:00:00 2001 From: treydinges Date: Wed, 20 Oct 2021 13:44:50 -0500 Subject: [PATCH 11/15] Revert "Create gltf_read.cpp" This reverts commit 39d30a7ccbe5e1275de873e3168116843f7174c2. --- src/libgcv/plugins/gltf/gltf_read.cpp | 1 - 1 file changed, 1 deletion(-) delete mode 100644 src/libgcv/plugins/gltf/gltf_read.cpp diff --git a/src/libgcv/plugins/gltf/gltf_read.cpp b/src/libgcv/plugins/gltf/gltf_read.cpp deleted file mode 100644 index cbeb52dc3b5..00000000000 --- a/src/libgcv/plugins/gltf/gltf_read.cpp +++ /dev/null @@ -1 +0,0 @@ -// insert code here From 60ac20cf59ef4cc159d82d2caf4605006501410f Mon Sep 17 00:00:00 2001 From: treydinges Date: Tue, 26 Oct 2021 12:07:30 -0500 Subject: [PATCH 12/15] typein command updated for material objects --- src/libged/typein/typein.c | 42 +++++++++++++++++++++----------------- src/librt/material.c | 2 +- 2 files changed, 24 insertions(+), 20 deletions(-) diff --git a/src/libged/typein/typein.c b/src/libged/typein/typein.c index 8ad7c95a311..c2b3a1f6e53 100644 --- a/src/libged/typein/typein.c +++ b/src/libged/typein/typein.c @@ -658,10 +658,11 @@ static const char *p_material[] = { "Enter the material name: ", "Enter the parent material name: ", "Enter the material source: ", - "Enter key value pairs for physical properties as separate arguments (end or skip with .): ", - "Enter key value pairs for mechanical properties as separate arguments (end or skip with .): ", - "Enter key value pairs for optical properties as separate arguments (end or skip with .): ", - "Enter key value pairs for thermal properties as separate arguments (end or skip with .): ", + "Enter key value pairs for the following properties as follows:\n \ + \tPhysical properties (end or skip with .)\n\ + \tMechanical properties (end or skip with .)\n\ + \tOptical properties (end or skip with .)\n\ + \tThermal properties (end or skip with .)\nExample: key val key val . key val . . key val .\n" }; /** @@ -3349,6 +3350,7 @@ ged_in_core(struct ged *gedp, int argc, const char *argv[]) int nvals; int (*fn_in)(struct ged *, const char **, struct rt_db_internal *) = NULL; int (*fn_in_2)(struct ged *, const char **, struct rt_db_internal *, const char *) = NULL; + int (*fn_in_3)(struct ged *, int, const char **, struct rt_db_internal *) = NULL; GED_CHECK_DATABASE_OPEN(gedp, GED_ERROR); GED_CHECK_READ_ONLY(gedp, GED_ERROR); @@ -3357,9 +3359,9 @@ ged_in_core(struct ged *gedp, int argc, const char *argv[]) /* initialize result */ bu_vls_trunc(gedp->ged_result_str, 0); - /* Get the name of the solid to be created */ + /* Get the name of the object to be created */ if (argc < 2) { - bu_vls_printf(gedp->ged_result_str, "Enter name of solid: "); + bu_vls_printf(gedp->ged_result_str, "Enter name of object: "); return GED_MORE; } if (db_lookup(gedp->dbip, argv[1], LOOKUP_QUIET) != RT_DIR_NULL) { @@ -3370,12 +3372,12 @@ ged_in_core(struct ged *gedp, int argc, const char *argv[]) bu_vls_printf(gedp->ged_result_str, "%s: ERROR, v4 names are limited to %d characters\n", argv[0], NAMESIZE); return GED_ERROR; } - /* Save the solid name */ + /* Save the object name */ name = (char *)argv[1]; - /* Get the solid type to be created and make it */ + /* Get the object type to be created and make it */ if (argc < 3) { - bu_vls_printf(gedp->ged_result_str, "Enter solid type: "); + bu_vls_printf(gedp->ged_result_str, "Enter object type: "); return GED_MORE; } @@ -3617,16 +3619,7 @@ ged_in_core(struct ged *gedp, int argc, const char *argv[]) } else if (BU_STR_EQUAL(argv[2], "material")) { nvals = 7; menu = p_material; - switch (material_in(gedp, argc, argv, &internal)) { - case GED_ERROR: - bu_vls_printf(gedp->ged_result_str, "%s: ERROR, material not made!\n", argv[0]); - rt_db_free_internal(&internal); - return GED_ERROR; - case GED_MORE: - return GED_MORE; - } - - goto do_new_update; + fn_in_3 = material_in; } else if (BU_STR_EQUAL(argv[2], "pnts")) { switch (pnts_in(gedp, argc, argv, &internal, p_pnts)) { case GED_ERROR: @@ -3689,6 +3682,17 @@ ged_in_core(struct ged *gedp, int argc, const char *argv[]) } return GED_ERROR; } + } else if (fn_in_3) { + if (fn_in_3(gedp, argc, argv, &internal) != 0) { + bu_vls_printf(gedp->ged_result_str, "%s: ERROR %s not made!\n", argv[0], argv[2]); + if (internal.idb_ptr) { + /* a few input functions do not use the internal pointer + * only free it, if it has been used + */ + rt_db_free_internal(&internal); + } + return GED_ERROR; + } } else { bu_vls_printf(gedp->ged_result_str, "%s: ERROR %s not made!\n", argv[0], argv[2]); return GED_ERROR; diff --git a/src/librt/material.c b/src/librt/material.c index 0533a31d3a7..9e72a8e1919 100644 --- a/src/librt/material.c +++ b/src/librt/material.c @@ -272,8 +272,8 @@ rt_material_describe(struct bu_vls *str, const struct rt_db_internal *ip, int ve const char *thermalProperties = bu_avs_get_all(&material_ip->thermalProperties, NULL); bu_vls_printf(&buf, "\tphysicalProperties: \n%s", physicalProperties); - bu_vls_printf(&buf, "\topticalProperties: \n%s", opticalProperties); bu_vls_printf(&buf, "\tmechanicalProperties: \n%s", mechanicalProperties); + bu_vls_printf(&buf, "\topticalProperties: \n%s", opticalProperties); bu_vls_printf(&buf, "\tthermalProperties: \n%s", thermalProperties); bu_vls_vlscat(str, &buf); From 561377c70165afaf653014ac7f70a5108c269f0f Mon Sep 17 00:00:00 2001 From: treydinges Date: Tue, 26 Oct 2021 12:57:28 -0500 Subject: [PATCH 13/15] Memory leaks?? --- src/librt/material.c | 30 ++++++++++++++++++++++++++---- src/libwdb/material.c | 1 + 2 files changed, 27 insertions(+), 4 deletions(-) diff --git a/src/librt/material.c b/src/librt/material.c index 9e72a8e1919..7fa3880b665 100644 --- a/src/librt/material.c +++ b/src/librt/material.c @@ -53,6 +53,13 @@ rt_material_ifree(struct rt_db_internal *ip) if (material) { material->magic = 0; /* sanity */ bu_vls_free(&material->name); + bu_vls_free(&material->parent); + bu_vls_free(&material->source); + + bu_avs_free(&material->physicalProperties); + bu_avs_free(&material->mechanicalProperties); + bu_avs_free(&material->opticalProperties); + bu_avs_free(&material->thermalProperties); bu_free((void *)material, "material ifree"); } ip->idb_ptr = ((void *)0); /* sanity */ @@ -84,28 +91,35 @@ rt_material_import5(struct rt_db_internal *ip, const struct bu_external *ep, con struct bu_vls name = BU_VLS_INIT_ZERO; struct bu_vls parent = BU_VLS_INIT_ZERO; struct bu_vls source = BU_VLS_INIT_ZERO; + + struct bu_external physical_ep = BU_EXTERNAL_INIT_ZERO; + struct bu_external mechanical_ep = BU_EXTERNAL_INIT_ZERO; + struct bu_external optical_ep = BU_EXTERNAL_INIT_ZERO; + struct bu_external thermal_ep = BU_EXTERNAL_INIT_ZERO; unsigned char *cp = ep->ext_buf; // copy name to internal format bu_vls_strcat(&name, (char *)cp); bu_vls_vlscat(&material_ip->name, &name); cp += strlen((const char *)cp) + 1; + bu_vls_free(&name); // copy parent to internal format bu_vls_strcat(&parent, (char *)cp); bu_vls_vlscat(&material_ip->parent, &parent); cp += strlen((const char *)cp) + 1; + bu_vls_free(&parent); // copy source to internal format bu_vls_strcat(&source, (char *)cp); bu_vls_vlscat(&material_ip->source, &source); cp += strlen((const char *)cp) + 1; + bu_vls_free(&source); // copy physical properties uint32_t size = ntohl(*(uint32_t *)cp); cp += SIZEOF_NETWORK_LONG; - struct bu_external physical_ep = BU_EXTERNAL_INIT_ZERO; physical_ep.ext_nbytes = size; physical_ep.ext_buf = cp; if (size > 0) { @@ -114,12 +128,12 @@ rt_material_import5(struct rt_db_internal *ip, const struct bu_external *ep, con bu_avs_init_empty(&material_ip->physicalProperties); } cp += size; + bu_free_external(&physical_ep); // copy mechanical properties size = ntohl(*(uint32_t *)cp); cp += SIZEOF_NETWORK_LONG; - struct bu_external mechanical_ep = BU_EXTERNAL_INIT_ZERO; mechanical_ep.ext_nbytes = size; mechanical_ep.ext_buf = cp; if (size > 0) { @@ -128,12 +142,12 @@ rt_material_import5(struct rt_db_internal *ip, const struct bu_external *ep, con bu_avs_init_empty(&material_ip->mechanicalProperties); } cp += size; + bu_free_external(&mechanical_ep); // copy optical properties size = ntohl(*(uint32_t *)cp); cp += SIZEOF_NETWORK_LONG; - struct bu_external optical_ep = BU_EXTERNAL_INIT_ZERO; optical_ep.ext_nbytes = size; optical_ep.ext_buf = cp; if (size > 0) { @@ -142,12 +156,12 @@ rt_material_import5(struct rt_db_internal *ip, const struct bu_external *ep, con bu_avs_init_empty(&material_ip->opticalProperties); } cp += size; + bu_free_external(&optical_ep); // copy thermal properties size = ntohl(*(uint32_t *)cp); cp += SIZEOF_NETWORK_LONG; - struct bu_external thermal_ep = BU_EXTERNAL_INIT_ZERO; thermal_ep.ext_nbytes = size; thermal_ep.ext_buf = cp; if (size > 0) { @@ -156,6 +170,7 @@ rt_material_import5(struct rt_db_internal *ip, const struct bu_external *ep, con bu_avs_init_empty(&material_ip->thermalProperties); } cp += size; + bu_free_external(&thermal_ep); return 0; /* OK */ } @@ -171,6 +186,7 @@ rt_material_export5(struct bu_external *ep, const struct rt_db_internal *ip, dou struct bu_vls name = BU_VLS_INIT_ZERO; struct bu_vls parent = BU_VLS_INIT_ZERO; struct bu_vls source = BU_VLS_INIT_ZERO; + struct bu_external physical_ep = BU_EXTERNAL_INIT_ZERO; struct bu_external mechanical_ep = BU_EXTERNAL_INIT_ZERO; struct bu_external optical_ep = BU_EXTERNAL_INIT_ZERO; @@ -223,24 +239,28 @@ rt_material_export5(struct bu_external *ep, const struct rt_db_internal *ip, dou cp += SIZEOF_NETWORK_LONG; memcpy(cp, physical_ep.ext_buf, physical_ep.ext_nbytes); cp += physical_ep.ext_nbytes; + bu_free_external(&physical_ep); // copy mechanical properties *(uint32_t *)cp = htonl(mechanical_ep.ext_nbytes); cp += SIZEOF_NETWORK_LONG; memcpy(cp, mechanical_ep.ext_buf, mechanical_ep.ext_nbytes); cp += mechanical_ep.ext_nbytes; + bu_free_external(&mechanical_ep); // copy optical properties *(uint32_t *)cp = htonl(optical_ep.ext_nbytes); cp += SIZEOF_NETWORK_LONG; memcpy(cp, optical_ep.ext_buf, optical_ep.ext_nbytes); cp += optical_ep.ext_nbytes; + bu_free_external(&optical_ep); // copy thermal properties *(uint32_t *)cp = htonl(thermal_ep.ext_nbytes); cp += SIZEOF_NETWORK_LONG; memcpy(cp, thermal_ep.ext_buf, thermal_ep.ext_nbytes); cp += thermal_ep.ext_nbytes; + bu_free_external(&thermal_ep); return 0; /* OK */ } @@ -275,7 +295,9 @@ rt_material_describe(struct bu_vls *str, const struct rt_db_internal *ip, int ve bu_vls_printf(&buf, "\tmechanicalProperties: \n%s", mechanicalProperties); bu_vls_printf(&buf, "\topticalProperties: \n%s", opticalProperties); bu_vls_printf(&buf, "\tthermalProperties: \n%s", thermalProperties); + bu_vls_vlscat(str, &buf); + bu_vls_free(&buf); return 0; } diff --git a/src/libwdb/material.c b/src/libwdb/material.c index 791b49464c4..3dd0c27c555 100644 --- a/src/libwdb/material.c +++ b/src/libwdb/material.c @@ -51,6 +51,7 @@ mk_material(struct rt_wdb *wdbp, BU_VLS_INIT(&material_ip->name); BU_VLS_INIT(&material_ip->parent); BU_VLS_INIT(&material_ip->source); + bu_avs_init_empty(&material_ip->physicalProperties); bu_avs_init_empty(&material_ip->mechanicalProperties); bu_avs_init_empty(&material_ip->opticalProperties); From 823a70b0ad7358d07f0f7a88789694935287d204 Mon Sep 17 00:00:00 2001 From: treydinges Date: Tue, 26 Oct 2021 18:08:28 -0500 Subject: [PATCH 14/15] Material object representation ready for main --- src/libbu/avs.c | 5 ++--- src/librt/material.c | 24 ++++++++++++------------ src/libwdb/material.c | 2 +- 3 files changed, 15 insertions(+), 16 deletions(-) diff --git a/src/libbu/avs.c b/src/libbu/avs.c index 49f76b3946f..4985c75eb0b 100644 --- a/src/libbu/avs.c +++ b/src/libbu/avs.c @@ -200,13 +200,12 @@ bu_avs_get(const struct bu_attribute_value_set *avsp, const char *name) const char * bu_avs_get_all(const struct bu_attribute_value_set *avsp, const char *title) { + BU_CK_AVS(avsp); + struct bu_attribute_value_pair *avpp; size_t i; struct bu_vls str = BU_VLS_INIT_ZERO; - BU_CK_AVS(avsp); - - bu_vls_init(&str); if (title) { bu_vls_strcat(&str, title); bu_vls_strcat(&str, "=\""); diff --git a/src/librt/material.c b/src/librt/material.c index 7fa3880b665..aecfab92878 100644 --- a/src/librt/material.c +++ b/src/librt/material.c @@ -128,7 +128,6 @@ rt_material_import5(struct rt_db_internal *ip, const struct bu_external *ep, con bu_avs_init_empty(&material_ip->physicalProperties); } cp += size; - bu_free_external(&physical_ep); // copy mechanical properties size = ntohl(*(uint32_t *)cp); @@ -142,7 +141,6 @@ rt_material_import5(struct rt_db_internal *ip, const struct bu_external *ep, con bu_avs_init_empty(&material_ip->mechanicalProperties); } cp += size; - bu_free_external(&mechanical_ep); // copy optical properties size = ntohl(*(uint32_t *)cp); @@ -156,7 +154,6 @@ rt_material_import5(struct rt_db_internal *ip, const struct bu_external *ep, con bu_avs_init_empty(&material_ip->opticalProperties); } cp += size; - bu_free_external(&optical_ep); // copy thermal properties size = ntohl(*(uint32_t *)cp); @@ -170,7 +167,6 @@ rt_material_import5(struct rt_db_internal *ip, const struct bu_external *ep, con bu_avs_init_empty(&material_ip->thermalProperties); } cp += size; - bu_free_external(&thermal_ep); return 0; /* OK */ } @@ -183,6 +179,11 @@ int rt_material_export5(struct bu_external *ep, const struct rt_db_internal *ip, double UNUSED(local2mm), const struct db_i *UNUSED(dbip)) { struct rt_material_internal *material_ip; + RT_CK_DB_INTERNAL(ip); + + if (ip->idb_type != ID_MATERIAL) bu_bomb("rt_material_export() type not ID_MATERIAL"); + material_ip = (struct rt_material_internal *)ip->idb_ptr; + struct bu_vls name = BU_VLS_INIT_ZERO; struct bu_vls parent = BU_VLS_INIT_ZERO; struct bu_vls source = BU_VLS_INIT_ZERO; @@ -192,11 +193,6 @@ rt_material_export5(struct bu_external *ep, const struct rt_db_internal *ip, dou struct bu_external optical_ep = BU_EXTERNAL_INIT_ZERO; struct bu_external thermal_ep = BU_EXTERNAL_INIT_ZERO; - RT_CK_DB_INTERNAL(ip); - - if (ip->idb_type != ID_MATERIAL) bu_bomb("rt_material_export() type not ID_MATERIAL"); - material_ip = (struct rt_material_internal *)ip->idb_ptr; - BU_EXTERNAL_INIT(ep); db5_export_attributes(&physical_ep, &material_ip->physicalProperties); @@ -275,16 +271,20 @@ rt_material_describe(struct bu_vls *str, const struct rt_db_internal *ip, int ve { register struct rt_material_internal *material_ip = (struct rt_material_internal *)ip->idb_ptr; - struct bu_vls buf = BU_VLS_INIT_ZERO; - RT_CHECK_MATERIAL(material_ip); + + struct bu_vls buf = BU_VLS_INIT_ZERO; bu_vls_strcat(str, "material (MATERIAL)\n"); bu_vls_printf(&buf, "\tName: %s\n", material_ip->name.vls_str); bu_vls_printf(&buf, "\tParent: %s\n", material_ip->parent.vls_str); bu_vls_printf(&buf, "\tSource: %s\n", material_ip->source.vls_str); - if (!verbose) return 0; + if (!verbose) { + bu_vls_vlscat(str, &buf); + bu_vls_free(&buf); + return 0; + } const char *physicalProperties = bu_avs_get_all(&material_ip->physicalProperties, NULL); const char *mechanicalProperties = bu_avs_get_all(&material_ip->mechanicalProperties, NULL); diff --git a/src/libwdb/material.c b/src/libwdb/material.c index 3dd0c27c555..99a54bd6068 100644 --- a/src/libwdb/material.c +++ b/src/libwdb/material.c @@ -76,4 +76,4 @@ mk_material(struct rt_wdb *wdbp, /* The internal representation will be freed */ return wdb_put_internal(wdbp, db_name, &intern, mk_conv2mm); -} \ No newline at end of file +} From 3e220cc1eb2906b864b5146300049bc72aa3ad5f Mon Sep 17 00:00:00 2001 From: treydinges Date: Thu, 11 Nov 2021 14:47:57 -0600 Subject: [PATCH 15/15] fixes for material branch code style/implementation --- include/bu/avs.h | 7 -- src/libbu/avs.c | 31 ------ src/librt/CMakeLists.txt | 2 +- src/librt/{ => material}/material.c | 151 +++++++++++++++++----------- 4 files changed, 94 insertions(+), 97 deletions(-) rename src/librt/{ => material}/material.c (69%) diff --git a/include/bu/avs.h b/include/bu/avs.h index 5001371fac8..b30ab51d43a 100644 --- a/include/bu/avs.h +++ b/include/bu/avs.h @@ -222,13 +222,6 @@ BU_EXPORT extern void bu_avs_merge(struct bu_attribute_value_set *dest, BU_EXPORT extern const char *bu_avs_get(const struct bu_attribute_value_set *avp, const char *attribute); -/** - * gets all attributes in an attribute set in "name = value" form, - * using the provided title. - */ -BU_EXPORT extern const char *bu_avs_get_all(const struct bu_attribute_value_set *avp, - const char *title); - /** * Remove all occurrences of an attribute from the provided attribute * set. diff --git a/src/libbu/avs.c b/src/libbu/avs.c index 4985c75eb0b..9e6f4809c08 100644 --- a/src/libbu/avs.c +++ b/src/libbu/avs.c @@ -198,37 +198,6 @@ bu_avs_get(const struct bu_attribute_value_set *avsp, const char *name) return NULL; } -const char * -bu_avs_get_all(const struct bu_attribute_value_set *avsp, const char *title) { - BU_CK_AVS(avsp); - - struct bu_attribute_value_pair *avpp; - size_t i; - struct bu_vls str = BU_VLS_INIT_ZERO; - - if (title) { - bu_vls_strcat(&str, title); - bu_vls_strcat(&str, "=\""); - } - - avpp = avsp->avp; - for (i = 0; i < avsp->count; i++, avpp++) { - bu_vls_strcat(&str, "\t\t("); - bu_vls_strcat(&str, avpp->name ? avpp->name : "NULL"); - bu_vls_strcat(&str, " : "); - bu_vls_strcat(&str, avpp->value ? avpp->value : "NULL"); - bu_vls_strcat(&str, ")\n"); - } - - if (title) { - bu_vls_strcat(&str, "\""); - } - - const char * attributes = bu_vls_strgrab(&str); - - return attributes; -} - int bu_avs_remove(struct bu_attribute_value_set *avsp, const char *name) diff --git a/src/librt/CMakeLists.txt b/src/librt/CMakeLists.txt index 5c9000eddf6..c5dca068988 100644 --- a/src/librt/CMakeLists.txt +++ b/src/librt/CMakeLists.txt @@ -40,8 +40,8 @@ set(LIBRT_SOURCES comb/comb_brep.cpp comb/comb_mirror.c comb/db_comb.c + material/material.c constraint.c - material.c cut.c cut_hlbvh.c db5_alloc.c diff --git a/src/librt/material.c b/src/librt/material/material.c similarity index 69% rename from src/librt/material.c rename to src/librt/material/material.c index aecfab92878..f8b354ae741 100644 --- a/src/librt/material.c +++ b/src/librt/material/material.c @@ -37,40 +37,72 @@ #include "pc.h" #include "raytrace.h" +const char * +get_all_avs_keys(const struct bu_attribute_value_set *avsp, const char *title) +{ + BU_CK_AVS(avsp); + + struct bu_attribute_value_pair *avpp; + size_t i; + struct bu_vls str = BU_VLS_INIT_ZERO; + + if (title) + { + bu_vls_strcat(&str, title); + bu_vls_strcat(&str, "=\""); + } + + avpp = avsp->avp; + for (i = 0; i < avsp->count; i++, avpp++) + { + bu_vls_strcat(&str, " "); + bu_vls_strcat(&str, avpp->name ? avpp->name : "NULL"); + bu_vls_strcat(&str, ":"); + bu_vls_strcat(&str, avpp->value ? avpp->value : "NULL"); + bu_vls_strcat(&str, "\n"); + } + + if (title) + { + bu_vls_strcat(&str, "\""); + } + + const char *attributes = bu_vls_strgrab(&str); + + return attributes; +} /** * Free the storage associated with the rt_db_internal version of * material object. */ -void -rt_material_ifree(struct rt_db_internal *ip) +void rt_material_ifree(struct rt_db_internal *ip) { register struct rt_material_internal *material; RT_CK_DB_INTERNAL(ip); material = (struct rt_material_internal *)ip->idb_ptr; - if (material) { - material->magic = 0; /* sanity */ - bu_vls_free(&material->name); - bu_vls_free(&material->parent); - bu_vls_free(&material->source); - - bu_avs_free(&material->physicalProperties); - bu_avs_free(&material->mechanicalProperties); - bu_avs_free(&material->opticalProperties); - bu_avs_free(&material->thermalProperties); - bu_free((void *)material, "material ifree"); + if (material) + { + material->magic = 0; /* sanity */ + bu_vls_free(&material->name); + bu_vls_free(&material->parent); + bu_vls_free(&material->source); + + bu_avs_free(&material->physicalProperties); + bu_avs_free(&material->mechanicalProperties); + bu_avs_free(&material->opticalProperties); + bu_avs_free(&material->thermalProperties); + bu_free((void *)material, "material ifree"); } - ip->idb_ptr = ((void *)0); /* sanity */ + ip->idb_ptr = ((void *)0); /* sanity */ } - /** * Import a material from the database format to the internal format. */ -int -rt_material_import5(struct rt_db_internal *ip, const struct bu_external *ep, const fastf_t *UNUSED(mat), const struct db_i *UNUSED(dbip)) +int rt_material_import5(struct rt_db_internal *ip, const struct bu_external *ep, const fastf_t *UNUSED(mat), const struct db_i *UNUSED(dbip)) { struct rt_material_internal *material_ip; @@ -122,9 +154,12 @@ rt_material_import5(struct rt_db_internal *ip, const struct bu_external *ep, con physical_ep.ext_nbytes = size; physical_ep.ext_buf = cp; - if (size > 0) { + if (size > 0) + { db5_import_attributes(&material_ip->physicalProperties, &physical_ep); - } else { + } + else + { bu_avs_init_empty(&material_ip->physicalProperties); } cp += size; @@ -135,9 +170,12 @@ rt_material_import5(struct rt_db_internal *ip, const struct bu_external *ep, con mechanical_ep.ext_nbytes = size; mechanical_ep.ext_buf = cp; - if (size > 0) { + if (size > 0) + { db5_import_attributes(&material_ip->mechanicalProperties, &mechanical_ep); - } else { + } + else + { bu_avs_init_empty(&material_ip->mechanicalProperties); } cp += size; @@ -148,9 +186,12 @@ rt_material_import5(struct rt_db_internal *ip, const struct bu_external *ep, con optical_ep.ext_nbytes = size; optical_ep.ext_buf = cp; - if (size > 0) { + if (size > 0) + { db5_import_attributes(&material_ip->opticalProperties, &optical_ep); - } else { + } + else + { bu_avs_init_empty(&material_ip->opticalProperties); } cp += size; @@ -161,27 +202,29 @@ rt_material_import5(struct rt_db_internal *ip, const struct bu_external *ep, con thermal_ep.ext_nbytes = size; thermal_ep.ext_buf = cp; - if (size > 0) { + if (size > 0) + { db5_import_attributes(&material_ip->thermalProperties, &thermal_ep); - } else { + } + else + { bu_avs_init_empty(&material_ip->thermalProperties); } cp += size; - return 0; /* OK */ + return 0; /* OK */ } - /** * Export a material from the internal format to the database format. */ -int -rt_material_export5(struct bu_external *ep, const struct rt_db_internal *ip, double UNUSED(local2mm), const struct db_i *UNUSED(dbip)) +int rt_material_export5(struct bu_external *ep, const struct rt_db_internal *ip, double UNUSED(local2mm), const struct db_i *UNUSED(dbip)) { struct rt_material_internal *material_ip; RT_CK_DB_INTERNAL(ip); - if (ip->idb_type != ID_MATERIAL) bu_bomb("rt_material_export() type not ID_MATERIAL"); + if (ip->idb_type != ID_MATERIAL) + bu_bomb("rt_material_export() type not ID_MATERIAL"); material_ip = (struct rt_material_internal *)ip->idb_ptr; struct bu_vls name = BU_VLS_INIT_ZERO; @@ -201,14 +244,7 @@ rt_material_export5(struct bu_external *ep, const struct rt_db_internal *ip, dou db5_export_attributes(&thermal_ep, &material_ip->thermalProperties); // initialize entire buffer - ep->ext_nbytes = bu_vls_strlen(&material_ip->name) + 1 - + bu_vls_strlen(&material_ip->parent) + 1 - + bu_vls_strlen(&material_ip->source) + 1 - + (4 * SIZEOF_NETWORK_LONG) - + physical_ep.ext_nbytes - + mechanical_ep.ext_nbytes - + optical_ep.ext_nbytes - + thermal_ep.ext_nbytes; + ep->ext_nbytes = bu_vls_strlen(&material_ip->name) + 1 + bu_vls_strlen(&material_ip->parent) + 1 + bu_vls_strlen(&material_ip->source) + 1 + (4 * SIZEOF_NETWORK_LONG) + physical_ep.ext_nbytes + mechanical_ep.ext_nbytes + optical_ep.ext_nbytes + thermal_ep.ext_nbytes; ep->ext_buf = (uint8_t *)bu_calloc(1, ep->ext_nbytes, "material external"); unsigned char *cp = ep->ext_buf; @@ -234,31 +270,31 @@ rt_material_export5(struct bu_external *ep, const struct rt_db_internal *ip, dou *(uint32_t *)cp = htonl(physical_ep.ext_nbytes); cp += SIZEOF_NETWORK_LONG; memcpy(cp, physical_ep.ext_buf, physical_ep.ext_nbytes); - cp += physical_ep.ext_nbytes; + cp += physical_ep.ext_nbytes; bu_free_external(&physical_ep); // copy mechanical properties *(uint32_t *)cp = htonl(mechanical_ep.ext_nbytes); cp += SIZEOF_NETWORK_LONG; memcpy(cp, mechanical_ep.ext_buf, mechanical_ep.ext_nbytes); - cp += mechanical_ep.ext_nbytes; + cp += mechanical_ep.ext_nbytes; bu_free_external(&mechanical_ep); // copy optical properties *(uint32_t *)cp = htonl(optical_ep.ext_nbytes); cp += SIZEOF_NETWORK_LONG; memcpy(cp, optical_ep.ext_buf, optical_ep.ext_nbytes); - cp += optical_ep.ext_nbytes; + cp += optical_ep.ext_nbytes; bu_free_external(&optical_ep); // copy thermal properties *(uint32_t *)cp = htonl(thermal_ep.ext_nbytes); cp += SIZEOF_NETWORK_LONG; memcpy(cp, thermal_ep.ext_buf, thermal_ep.ext_nbytes); - cp += thermal_ep.ext_nbytes; + cp += thermal_ep.ext_nbytes; bu_free_external(&thermal_ep); - return 0; /* OK */ + return 0; /* OK */ } /** @@ -266,8 +302,7 @@ rt_material_export5(struct bu_external *ep, const struct rt_db_internal *ip, dou * line describes type of object. Additional lines are indented one * tab, and give parameter values. */ -int -rt_material_describe(struct bu_vls *str, const struct rt_db_internal *ip, int verbose, double UNUSED(mm2local)) +int rt_material_describe(struct bu_vls *str, const struct rt_db_internal *ip, int verbose, double UNUSED(mm2local)) { register struct rt_material_internal *material_ip = (struct rt_material_internal *)ip->idb_ptr; @@ -276,25 +311,26 @@ rt_material_describe(struct bu_vls *str, const struct rt_db_internal *ip, int ve struct bu_vls buf = BU_VLS_INIT_ZERO; bu_vls_strcat(str, "material (MATERIAL)\n"); - bu_vls_printf(&buf, "\tName: %s\n", material_ip->name.vls_str); - bu_vls_printf(&buf, "\tParent: %s\n", material_ip->parent.vls_str); - bu_vls_printf(&buf, "\tSource: %s\n", material_ip->source.vls_str); + bu_vls_printf(&buf, " name: %s\n", material_ip->name.vls_str); + bu_vls_printf(&buf, " parent: %s\n", material_ip->parent.vls_str); + bu_vls_printf(&buf, " source: %s\n", material_ip->source.vls_str); - if (!verbose) { + if (!verbose) + { bu_vls_vlscat(str, &buf); bu_vls_free(&buf); return 0; } - const char *physicalProperties = bu_avs_get_all(&material_ip->physicalProperties, NULL); - const char *mechanicalProperties = bu_avs_get_all(&material_ip->mechanicalProperties, NULL); - const char *opticalProperties = bu_avs_get_all(&material_ip->opticalProperties, NULL); - const char *thermalProperties = bu_avs_get_all(&material_ip->thermalProperties, NULL); + const char *physicalProperties = get_all_avs_keys(&material_ip->physicalProperties, NULL); + const char *mechanicalProperties = get_all_avs_keys(&material_ip->mechanicalProperties, NULL); + const char *opticalProperties = get_all_avs_keys(&material_ip->opticalProperties, NULL); + const char *thermalProperties = get_all_avs_keys(&material_ip->thermalProperties, NULL); - bu_vls_printf(&buf, "\tphysicalProperties: \n%s", physicalProperties); - bu_vls_printf(&buf, "\tmechanicalProperties: \n%s", mechanicalProperties); - bu_vls_printf(&buf, "\topticalProperties: \n%s", opticalProperties); - bu_vls_printf(&buf, "\tthermalProperties: \n%s", thermalProperties); + bu_vls_printf(&buf, " physical properties\n%s", physicalProperties); + bu_vls_printf(&buf, " mechanical properties\n%s", mechanicalProperties); + bu_vls_printf(&buf, " optical properties\n%s", opticalProperties); + bu_vls_printf(&buf, " thermal properties\n%s", thermalProperties); bu_vls_vlscat(str, &buf); bu_vls_free(&buf); @@ -302,7 +338,6 @@ rt_material_describe(struct bu_vls *str, const struct rt_db_internal *ip, int ve return 0; } - /** @} */ /* * Local Variables: