From 8aaccc428a50a7fd369c0bc3e780783982f6e39e Mon Sep 17 00:00:00 2001 From: GregoryLi0 Date: Mon, 3 Jul 2023 14:08:30 +0800 Subject: [PATCH 01/18] =?UTF-8?q?=E2=9C=A8=20feat(libbrep/libged):=20remov?= =?UTF-8?q?e=20curve?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit add "brep .. curve remove .." cmd --- include/brep/edit.h | 9 ++++++- src/libbrep/edit.cpp | 10 ++++++++ src/libged/brep/curve.cpp | 51 +++++++++++++++++++++++++++++++++++++++ 3 files changed, 69 insertions(+), 1 deletion(-) diff --git a/include/brep/edit.h b/include/brep/edit.h index a73e39bc7d7..e81139dce56 100644 --- a/include/brep/edit.h +++ b/include/brep/edit.h @@ -72,6 +72,13 @@ extern "C++" BREP_EXPORT extern int brep_curve_interpCrv(ON_Brep *brep, std::vector points); + /** + * remove a curve from brep + * @attention the index of C3 is changed after remove!!! + */ + BREP_EXPORT extern bool + brep_curve_remove(ON_Brep *brep, int curve_id); + /** * move curve along a vector. */ @@ -106,7 +113,7 @@ extern "C++" /** * Join the end of curve_id_1 to the start of curve_id_2. * return id of the new curve, delete the two old curves. - * Remark: the index of C3 is changed after join!!! + * @attention the index of C3 is changed after join!!! */ BREP_EXPORT extern int brep_curve_join(ON_Brep *brep, int curve_id_1, int curve_id_2); diff --git a/src/libbrep/edit.cpp b/src/libbrep/edit.cpp index af6cac1bb25..a9ce232d2c3 100644 --- a/src/libbrep/edit.cpp +++ b/src/libbrep/edit.cpp @@ -122,6 +122,16 @@ int brep_curve_interpCrv(ON_Brep *brep, std::vector points) return brep->AddEdgeCurve(curve); } +bool brep_curve_remove(ON_Brep *brep, int curve_id) +{ + if (curve_id < 0 || curve_id >= brep->m_C3.Count()) { + bu_log("curve_id is out of range\n"); + return false; + } + brep->m_C3.Remove(curve_id); + return true; +} + bool brep_curve_move(ON_Brep *brep, int curve_id, const ON_3dVector &point) { /// the curve could be a NURBS curve or not diff --git a/src/libged/brep/curve.cpp b/src/libged/brep/curve.cpp index d92e2d3ad4e..6feafe2cceb 100644 --- a/src/libged/brep/curve.cpp +++ b/src/libged/brep/curve.cpp @@ -236,6 +236,56 @@ _brep_cmd_curve_interp(void *bs, int argc, const char **argv) return BRLCAD_OK; } +static int +_brep_cmd_curve_remove(void *bs, int argc, const char **argv) +{ + const char *usage_string = "brep [options] curve remove "; + const char *purpose_string = "remove a NURBS curve"; + if (_brep_curve_msgs(bs, argc, argv, usage_string, purpose_string)) { + return BRLCAD_OK; + } + + struct _ged_brep_icurve *gib = (struct _ged_brep_icurve *)bs; + if (argc < 2) { + bu_vls_printf(gib->gb->gedp->ged_result_str, "not enough arguments\n"); + bu_vls_printf(gib->gb->gedp->ged_result_str, "%s\n", usage_string); + return BRLCAD_ERROR; + } + + int curve_id = atoi(argv[1]); + + if (curve_id <= 0) { + bu_vls_printf(gib->gb->gedp->ged_result_str, "invalid curve_id\n"); + return BRLCAD_ERROR; + } + + struct rt_brep_internal *b_ip = (struct rt_brep_internal *)gib->gb->intern.idb_ptr; + + bool res = brep_curve_remove(b_ip->brep, curve_id); + if (!res) { + bu_vls_printf(gib->gb->gedp->ged_result_str, "failed to remove curve\n"); + return BRLCAD_ERROR; + } + + // Delete the old object + const char *av[3]; + char *ncpy = bu_strdup(gib->gb->solid_name.c_str()); + av[0] = "kill"; + av[1] = ncpy; + av[2] = NULL; + (void)ged_exec(gib->gb->gedp, 2, (const char **)av); + bu_free(ncpy, "free name cpy"); + + // Make the new one + struct rt_wdb *wdbp = wdb_dbopen(gib->gb->gedp->dbip, RT_WDB_TYPE_DB_DEFAULT); + + if (mk_brep(wdbp, gib->gb->solid_name.c_str(), (void *)b_ip->brep)) { + return BRLCAD_ERROR; + } + bu_vls_printf(gib->gb->gedp->ged_result_str, "successful remove C3 curve! id = %d", curve_id); + return BRLCAD_OK; +} + static int _brep_cmd_curve_move(void *bs, int argc, const char **argv) { @@ -593,6 +643,7 @@ const struct bu_cmdtab _brep_curve_cmds[] = { { "create", _brep_cmd_curve_create}, { "in", _brep_cmd_curve_in}, { "inter", _brep_cmd_curve_interp}, + { "remove", _brep_cmd_curve_remove}, { "move", _brep_cmd_curve_move}, { "set_cv", _brep_cmd_curve_set_cv}, { "flip", _brep_cmd_curve_flip}, From 3ec6078f4fd8dc2f342140184116ff3af6dc7a7f Mon Sep 17 00:00:00 2001 From: GregoryLi0 Date: Mon, 3 Jul 2023 14:22:20 +0800 Subject: [PATCH 02/18] =?UTF-8?q?=E2=9C=A8=20feat(libbrep/libged):=20remov?= =?UTF-8?q?e=20surface?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit add "brep .. surface remove .." cmd --- include/brep/edit.h | 11 ++++++++-- src/libbrep/edit.cpp | 9 ++++++++ src/libged/brep/surface.cpp | 44 ++++++++++++++++++++++++++++++++++++- 3 files changed, 61 insertions(+), 3 deletions(-) diff --git a/include/brep/edit.h b/include/brep/edit.h index e81139dce56..b331ebfa6b9 100644 --- a/include/brep/edit.h +++ b/include/brep/edit.h @@ -74,7 +74,7 @@ extern "C++" /** * remove a curve from brep - * @attention the index of C3 is changed after remove!!! + * @attention the index of m_C3 is changed after remove!!! */ BREP_EXPORT extern bool brep_curve_remove(ON_Brep *brep, int curve_id); @@ -113,7 +113,7 @@ extern "C++" /** * Join the end of curve_id_1 to the start of curve_id_2. * return id of the new curve, delete the two old curves. - * @attention the index of C3 is changed after join!!! + * @attention the index of m_C3 is changed after join!!! */ BREP_EXPORT extern int brep_curve_join(ON_Brep *brep, int curve_id_1, int curve_id_2); @@ -154,6 +154,13 @@ extern "C++" */ BREP_EXPORT extern int brep_surface_create_ruled(ON_Brep *brep, int curve_id0, int curve_id1); + + /** + * remove a surface from brep + * @attention the index of m_S is changed after remove!!! + */ + BREP_EXPORT extern bool + brep_surface_remove(ON_Brep *brep, int surface_id); } /* extern C++ */ #endif diff --git a/src/libbrep/edit.cpp b/src/libbrep/edit.cpp index a9ce232d2c3..6f3f2230c7e 100644 --- a/src/libbrep/edit.cpp +++ b/src/libbrep/edit.cpp @@ -290,6 +290,15 @@ int brep_surface_create_ruled(ON_Brep *brep, int curve_id0, int curve_id1) return brep->AddSurface(surface); } +bool brep_surface_remove(ON_Brep *brep, int surface_id) +{ + if (surface_id < 0 || surface_id >= brep->m_S.Count()) { + bu_log("surface_id is out of range\n"); + return false; + } + brep->m_S.Remove(surface_id); + return true; +} std::vector calculateTangentVectors(const std::vector &points) { diff --git a/src/libged/brep/surface.cpp b/src/libged/brep/surface.cpp index 08fafbfb902..b149cb049e7 100644 --- a/src/libged/brep/surface.cpp +++ b/src/libged/brep/surface.cpp @@ -92,7 +92,6 @@ _brep_cmd_surface_create(void *bs, int argc, const char **argv) static int _brep_cmd_surface_birail(void *bs, int argc, const char **argv) { - const char *usage_string = "brep [options] surface birail "; const char *purpose_string = "create a new NURBS surface using two curves"; if (_brep_surface_msgs(bs, argc, argv, usage_string, purpose_string)) { @@ -134,6 +133,48 @@ _brep_cmd_surface_birail(void *bs, int argc, const char **argv) return BRLCAD_OK; } +static int +_brep_cmd_surface_remove(void *bs, int argc, const char **argv) +{ + const char *usage_string = "brep [options] surface remove "; + const char *purpose_string = "remove a NURBS surface"; + if (_brep_surface_msgs(bs, argc, argv, usage_string, purpose_string)) { + return BRLCAD_OK; + } + + argc--;argv++; + struct _ged_brep_isurface *gib = (struct _ged_brep_isurface *)bs; + if (argc < 1) { + bu_vls_printf(gib->gb->gedp->ged_result_str, " not enough arguments\n"); + bu_vls_printf(gib->gb->gedp->ged_result_str, "%s\n", usage_string); + return BRLCAD_ERROR; + } + + struct rt_brep_internal *b_ip = (struct rt_brep_internal *)gib->gb->intern.idb_ptr; + bool flag = brep_surface_remove(b_ip->brep, atoi(argv[0])); + if (!flag) { + bu_vls_printf(gib->gb->gedp->ged_result_str, ": failed to remove surface %s\n", argv[0]); + return BRLCAD_ERROR; + } + + // Delete the old object + const char *av[3]; + char *ncpy = bu_strdup(gib->gb->solid_name.c_str()); + av[0] = "kill"; + av[1] = ncpy; + av[2] = NULL; + (void)ged_exec(gib->gb->gedp, 2, (const char **)av); + bu_free(ncpy, "free name cpy"); + + // Make the new one + struct rt_wdb *wdbp = wdb_dbopen(gib->gb->gedp->dbip, RT_WDB_TYPE_DB_DEFAULT); + + if (mk_brep(wdbp, gib->gb->solid_name.c_str(), (void *)b_ip->brep)) { + return BRLCAD_ERROR; + } + return BRLCAD_OK; +} + static int _brep_cmd_surface_move(void *bs, int argc, const char **argv) { @@ -294,6 +335,7 @@ _brep_surface_help(struct _ged_brep_isurface *bs, int argc, const char **argv) const struct bu_cmdtab _brep_surface_cmds[] = { { "create", _brep_cmd_surface_create}, { "birail", _brep_cmd_surface_birail}, + { "remove", _brep_cmd_surface_remove}, { "move", _brep_cmd_surface_move}, { "set_cv", _brep_cmd_set_cv}, { "trim", _brep_cmd_surface_trim}, From db0309c9ca2fe001a6dc85ee8ba234bbce04d59a Mon Sep 17 00:00:00 2001 From: GregoryLi0 Date: Mon, 3 Jul 2023 15:12:48 +0800 Subject: [PATCH 03/18] =?UTF-8?q?=E2=9C=A8=20feat(libbrep/libged):=20curve?= =?UTF-8?q?=20split?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit add "brep .. curve split .." cmd --- include/brep/edit.h | 10 +++++-- src/libbrep/edit.cpp | 18 ++++++++++++ src/libged/brep/curve.cpp | 61 +++++++++++++++++++++++++++++++++++++++ 3 files changed, 87 insertions(+), 2 deletions(-) diff --git a/include/brep/edit.h b/include/brep/edit.h index b331ebfa6b9..40c57ba5824 100644 --- a/include/brep/edit.h +++ b/include/brep/edit.h @@ -92,7 +92,7 @@ extern "C++" brep_curve_set_cv(ON_Brep *brep, int curve_id, int cv_id, const ON_4dPoint &point); /** - * Reverse parameterizatrion by negating all knots + * reverse parameterizatrion by negating all knots * and reversing the order of the control vertices. */ BREP_EXPORT extern bool @@ -111,7 +111,13 @@ extern "C++" brep_curve_trim(ON_Brep *brep, int curve_id, double t0, double t1); /** - * Join the end of curve_id_1 to the start of curve_id_2. + * split a curve at a parameter. Old curve will be deleted. + */ + BREP_EXPORT extern bool + brep_curve_split(ON_Brep *brep, int curve_id, double t); + + /** + * join the end of curve_id_1 to the start of curve_id_2. * return id of the new curve, delete the two old curves. * @attention the index of m_C3 is changed after join!!! */ diff --git a/src/libbrep/edit.cpp b/src/libbrep/edit.cpp index 6f3f2230c7e..142a8b690e0 100644 --- a/src/libbrep/edit.cpp +++ b/src/libbrep/edit.cpp @@ -183,6 +183,24 @@ bool brep_curve_trim(ON_Brep *brep, int curve_id, double t0, double t1) return curve->Trim(interval); } +bool brep_curve_split(ON_Brep *brep, int curve_id, double t) +{ + ON_NurbsCurve *curve = brep_get_nurbs_curve(brep, curve_id); + if (!curve) { + return false; + } + ON_Curve *curve1 = NULL; + ON_Curve *curve2 = NULL; + bool flag = curve->Split(t, curve1, curve2); + if (flag) { + brep->m_C3.Remove(curve_id); + brep->m_C3.Append(curve1); + brep->m_C3.Append(curve2); + bu_log("old curve removed, id: %d, new curve id: %d, %d\n", curve_id, brep->m_C3.Count() - 2, brep->m_C3.Count() - 1); + } + return flag; +} + int brep_curve_join(ON_Brep *brep, int curve_id_1, int curve_id_2) { ON_NurbsCurve *curve1 = brep_get_nurbs_curve(brep, curve_id_1); diff --git a/src/libged/brep/curve.cpp b/src/libged/brep/curve.cpp index 6feafe2cceb..5d1914bcd6e 100644 --- a/src/libged/brep/curve.cpp +++ b/src/libged/brep/curve.cpp @@ -561,6 +561,66 @@ _brep_cmd_curve_trim(void *bs, int argc, const char **argv) return BRLCAD_OK; } +static int +_brep_cmd_curve_split(void *bs, int argc, const char **argv) +{ + const char *usage_string = "brep [options] split "; + const char *purpose_string = "split a NURBS curve into two at a parameter"; + if (_brep_curve_msgs(bs, argc, argv, usage_string, purpose_string)) { + return BRLCAD_OK; + } + argc--;argv++; + struct _ged_brep_icurve *gib = (struct _ged_brep_icurve *)bs; + if (argc < 2) { + bu_vls_printf(gib->gb->gedp->ged_result_str, "not enough arguments\n"); + bu_vls_printf(gib->gb->gedp->ged_result_str, "%s\n", usage_string); + return BRLCAD_ERROR; + } + + int curve_id = 0; + try { + curve_id = std::stoi(argv[0]); + } catch (const std::exception &e) { + bu_vls_printf(gib->gb->gedp->ged_result_str, "invalid curve id\n"); + bu_vls_printf(gib->gb->gedp->ged_result_str, "%s\n", e.what()); + return BRLCAD_ERROR; + } + + double param = 0; + try { + param = std::stod(argv[1]); + } catch (const std::exception &e) { + bu_vls_printf(gib->gb->gedp->ged_result_str, "invalid parameter\n"); + bu_vls_printf(gib->gb->gedp->ged_result_str, "%s\n", e.what()); + return BRLCAD_ERROR; + } + + struct rt_brep_internal *b_ip = (struct rt_brep_internal *)gib->gb->intern.idb_ptr; + bool flag = brep_curve_split(b_ip->brep, curve_id, param); + if (!flag) { + bu_vls_printf(gib->gb->gedp->ged_result_str, "failed to split curve %s\n", argv[0]); + return BRLCAD_ERROR; + } + + // Delete the old object + const char *av[3]; + char *ncpy = bu_strdup(gib->gb->solid_name.c_str()); + av[0] = "kill"; + av[1] = ncpy; + av[2] = NULL; + (void)ged_exec(gib->gb->gedp, 2, (const char **)av); + bu_free(ncpy, "free name cpy"); + + // Make the new one + struct rt_wdb *wdbp = wdb_dbopen(gib->gb->gedp->dbip, RT_WDB_TYPE_DB_DEFAULT); + + if (mk_brep(wdbp, gib->gb->solid_name.c_str(), (void *)b_ip->brep)) { + return BRLCAD_ERROR; + } + bu_vls_printf(gib->gb->gedp->ged_result_str, "split curve %s at parameter %s. Old curve removed.\n", argv[0], argv[1]); + return BRLCAD_OK; +} + static int _brep_cmd_curve_join(void *bs, int argc, const char **argv) { @@ -649,6 +709,7 @@ const struct bu_cmdtab _brep_curve_cmds[] = { { "flip", _brep_cmd_curve_flip}, { "insert_knot", _brep_cmd_curve_insert_knot}, { "trim", _brep_cmd_curve_trim}, + { "split", _brep_cmd_curve_split}, { "join", _brep_cmd_curve_join}, { (char *)NULL, NULL} }; From b3988c87794ef8fb37919e942d7ed515fac8608d Mon Sep 17 00:00:00 2001 From: GregoryLi0 Date: Mon, 3 Jul 2023 15:30:49 +0800 Subject: [PATCH 04/18] =?UTF-8?q?=E2=9C=A8=20feat(libbrep/libged):=20split?= =?UTF-8?q?=20surface?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit add "brep .. surface split .." cmd. change some code styles --- include/brep/edit.h | 6 +++++ src/libbrep/edit.cpp | 21 +++++++++++++++-- src/libged/brep/curve.cpp | 28 +++++++++++------------ src/libged/brep/surface.cpp | 45 +++++++++++++++++++++++++++++++++++++ 4 files changed, 84 insertions(+), 16 deletions(-) diff --git a/include/brep/edit.h b/include/brep/edit.h index 40c57ba5824..05246ae23ae 100644 --- a/include/brep/edit.h +++ b/include/brep/edit.h @@ -152,6 +152,12 @@ extern "C++" BREP_EXPORT extern bool brep_surface_trim(ON_Brep *brep, int surface_id, int dir, double t0, double t1); + /** + * split a surface at a parameter. Old surface will be deleted. + */ + BREP_EXPORT extern bool + brep_surface_split(ON_Brep *brep, int surface_id, int dir, double t); + /** * create a ruled surface. * The two curves must have the same NURBS form knots. diff --git a/src/libbrep/edit.cpp b/src/libbrep/edit.cpp index 142a8b690e0..94376ea310a 100644 --- a/src/libbrep/edit.cpp +++ b/src/libbrep/edit.cpp @@ -194,8 +194,8 @@ bool brep_curve_split(ON_Brep *brep, int curve_id, double t) bool flag = curve->Split(t, curve1, curve2); if (flag) { brep->m_C3.Remove(curve_id); - brep->m_C3.Append(curve1); - brep->m_C3.Append(curve2); + brep->AddEdgeCurve(curve1); + brep->AddEdgeCurve(curve2); bu_log("old curve removed, id: %d, new curve id: %d, %d\n", curve_id, brep->m_C3.Count() - 2, brep->m_C3.Count() - 1); } return flag; @@ -293,6 +293,23 @@ bool brep_surface_trim(ON_Brep *brep, int surface_id, int dir, double t0, double return surface->Trim(dir, interval); } +bool brep_surface_split(ON_Brep *brep, int surface_id, int dir, double t) +{ + ON_NurbsSurface *surface = brep_get_nurbs_surface(brep, surface_id); + if (!surface) { + return false; + } + ON_Surface *surf1=NULL, *surf2=NULL; + bool flag = surface->Split(dir, t, surf1, surf2); + if (flag) { + brep->m_S.Remove(surface_id); + brep->AddSurface(surf1); + brep->AddSurface(surf2); + bu_log("brep_surface_split: split surface %d into %d and %d\n", surface_id, brep->m_S.Count()-2, brep->m_S.Count()-1); + } + return flag; +} + int brep_surface_create_ruled(ON_Brep *brep, int curve_id0, int curve_id1) { ON_NurbsCurve *curve0 = brep_get_nurbs_curve(brep, curve_id0); diff --git a/src/libged/brep/curve.cpp b/src/libged/brep/curve.cpp index 5d1914bcd6e..86d581308e8 100644 --- a/src/libged/brep/curve.cpp +++ b/src/libged/brep/curve.cpp @@ -567,39 +567,39 @@ _brep_cmd_curve_split(void *bs, int argc, const char **argv) const char *usage_string = "brep [options] split "; const char *purpose_string = "split a NURBS curve into two at a parameter"; if (_brep_curve_msgs(bs, argc, argv, usage_string, purpose_string)) { - return BRLCAD_OK; + return BRLCAD_OK; } argc--;argv++; struct _ged_brep_icurve *gib = (struct _ged_brep_icurve *)bs; if (argc < 2) { - bu_vls_printf(gib->gb->gedp->ged_result_str, "not enough arguments\n"); - bu_vls_printf(gib->gb->gedp->ged_result_str, "%s\n", usage_string); - return BRLCAD_ERROR; + bu_vls_printf(gib->gb->gedp->ged_result_str, "not enough arguments\n"); + bu_vls_printf(gib->gb->gedp->ged_result_str, "%s\n", usage_string); + return BRLCAD_ERROR; } int curve_id = 0; try { - curve_id = std::stoi(argv[0]); + curve_id = std::stoi(argv[0]); } catch (const std::exception &e) { - bu_vls_printf(gib->gb->gedp->ged_result_str, "invalid curve id\n"); - bu_vls_printf(gib->gb->gedp->ged_result_str, "%s\n", e.what()); - return BRLCAD_ERROR; + bu_vls_printf(gib->gb->gedp->ged_result_str, "invalid curve id\n"); + bu_vls_printf(gib->gb->gedp->ged_result_str, "%s\n", e.what()); + return BRLCAD_ERROR; } double param = 0; try { - param = std::stod(argv[1]); + param = std::stod(argv[1]); } catch (const std::exception &e) { - bu_vls_printf(gib->gb->gedp->ged_result_str, "invalid parameter\n"); - bu_vls_printf(gib->gb->gedp->ged_result_str, "%s\n", e.what()); - return BRLCAD_ERROR; + bu_vls_printf(gib->gb->gedp->ged_result_str, "invalid parameter\n"); + bu_vls_printf(gib->gb->gedp->ged_result_str, "%s\n", e.what()); + return BRLCAD_ERROR; } struct rt_brep_internal *b_ip = (struct rt_brep_internal *)gib->gb->intern.idb_ptr; bool flag = brep_curve_split(b_ip->brep, curve_id, param); if (!flag) { - bu_vls_printf(gib->gb->gedp->ged_result_str, "failed to split curve %s\n", argv[0]); - return BRLCAD_ERROR; + bu_vls_printf(gib->gb->gedp->ged_result_str, "failed to split curve %s\n", argv[0]); + return BRLCAD_ERROR; } // Delete the old object diff --git a/src/libged/brep/surface.cpp b/src/libged/brep/surface.cpp index b149cb049e7..44fc9cd5a37 100644 --- a/src/libged/brep/surface.cpp +++ b/src/libged/brep/surface.cpp @@ -306,6 +306,50 @@ _brep_cmd_surface_trim(void *bs, int argc, const char **argv) return BRLCAD_OK; } +static int +_brep_cmd_surface_split(void *bs, int argc, const char **argv) +{ + const char *usage_string = "brep [options] split "; + const char *purpose_string = "split a NURBS surface into two given a parameter value."; + if (_brep_surface_msgs(bs, argc, argv, usage_string, purpose_string)) { + return BRLCAD_OK; + } + argc--;argv++; + struct _ged_brep_isurface *gib = (struct _ged_brep_isurface *)bs; + if (argc < 3) { + bu_vls_printf(gib->gb->gedp->ged_result_str, " not enough arguments\n"); + bu_vls_printf(gib->gb->gedp->ged_result_str, "%s\n", usage_string); + return BRLCAD_ERROR; + } + + int surface_id = atoi(argv[0]); + int dir = atoi(argv[1]); + double param = atof(argv[2]); + struct rt_brep_internal *b_ip = (struct rt_brep_internal *)gib->gb->intern.idb_ptr; + bool flag = brep_surface_split(b_ip->brep, surface_id, dir, param); + if (!flag) { + bu_vls_printf(gib->gb->gedp->ged_result_str, ": failed to split the surface %s\n", argv[0]); + return BRLCAD_ERROR; + } + + // Delete the old object + const char *av[3]; + char *ncpy = bu_strdup(gib->gb->solid_name.c_str()); + av[0] = "kill"; + av[1] = ncpy; + av[2] = NULL; + (void)ged_exec(gib->gb->gedp, 2, (const char **)av); + bu_free(ncpy, "free name cpy"); + + // Make the new one + struct rt_wdb *wdbp = wdb_dbopen(gib->gb->gedp->dbip, RT_WDB_TYPE_DB_DEFAULT); + + if (mk_brep(wdbp, gib->gb->solid_name.c_str(), (void *)b_ip->brep)) { + return BRLCAD_ERROR; + } + return BRLCAD_OK; +} + static void _brep_surface_help(struct _ged_brep_isurface *bs, int argc, const char **argv) { @@ -339,6 +383,7 @@ const struct bu_cmdtab _brep_surface_cmds[] = { { "move", _brep_cmd_surface_move}, { "set_cv", _brep_cmd_set_cv}, { "trim", _brep_cmd_surface_trim}, + { "split", _brep_cmd_surface_split}, { (char *)NULL, NULL} }; From b738159d0deadd011c1914d96e6bfb0a1733d099 Mon Sep 17 00:00:00 2001 From: GregoryLi0 Date: Sun, 9 Jul 2023 15:56:31 +0800 Subject: [PATCH 05/18] =?UTF-8?q?=E2=9C=A8=20feat(libbrep/libged):=20creat?= =?UTF-8?q?e=20surf=20by=20TP?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit create NURBS surface by two curve tensor product --- include/brep/edit.h | 7 ++++++ src/libbrep/edit.cpp | 19 ++++++++++++++++ src/libged/brep/surface.cpp | 45 +++++++++++++++++++++++++++++++++++++ 3 files changed, 71 insertions(+) diff --git a/include/brep/edit.h b/include/brep/edit.h index 05246ae23ae..7628788e13b 100644 --- a/include/brep/edit.h +++ b/include/brep/edit.h @@ -167,6 +167,13 @@ extern "C++" BREP_EXPORT extern int brep_surface_create_ruled(ON_Brep *brep, int curve_id0, int curve_id1); + /** + * create a surface by extruding a curve along another curve. + * return: if successful, id of the new surface; otherwise, -1. + */ + BREP_EXPORT extern int + brep_surface_tensor_product(ON_Brep *brep, int curve_id0, int curve_id1); + /** * remove a surface from brep * @attention the index of m_S is changed after remove!!! diff --git a/src/libbrep/edit.cpp b/src/libbrep/edit.cpp index 94376ea310a..d7ce0767f99 100644 --- a/src/libbrep/edit.cpp +++ b/src/libbrep/edit.cpp @@ -325,6 +325,25 @@ int brep_surface_create_ruled(ON_Brep *brep, int curve_id0, int curve_id1) return brep->AddSurface(surface); } +int brep_surface_tensor_product(ON_Brep *brep, int curve_id0, int curve_id1) +{ + ON_NurbsCurve *curve0 = brep_get_nurbs_curve(brep, curve_id0); + ON_NurbsCurve *curve1 = brep_get_nurbs_curve(brep, curve_id1); + if (!curve0 || !curve1) { + return -1; + } + ON_SumSurface *surface = ON_SumSurface::New(); + if(!surface->Create(*curve0, *curve1)) { + return -1; + } + ON_NurbsSurface *nurbs_surface = surface->NurbsSurface(); + delete surface; + if(nurbs_surface == NULL) { + return -1; + } + return brep->AddSurface(nurbs_surface); +} + bool brep_surface_remove(ON_Brep *brep, int surface_id) { if (surface_id < 0 || surface_id >= brep->m_S.Count()) { diff --git a/src/libged/brep/surface.cpp b/src/libged/brep/surface.cpp index 44fc9cd5a37..cb3d47f9ef7 100644 --- a/src/libged/brep/surface.cpp +++ b/src/libged/brep/surface.cpp @@ -350,6 +350,50 @@ _brep_cmd_surface_split(void *bs, int argc, const char **argv) return BRLCAD_OK; } +static int +_brep_cmd_surface_tensor_product(void *bs, int argc, const char **argv) +{ + const char *usage_string = "brep [options] tensor birail "; + const char *purpose_string = "create a new NURBS surface by tensor product of two curves"; + if (_brep_surface_msgs(bs, argc, argv, usage_string, purpose_string)) { + return BRLCAD_OK; + } + + struct _ged_brep_isurface *gib = (struct _ged_brep_isurface *)bs; + struct rt_brep_internal *b_ip = (struct rt_brep_internal *)gib->gb->intern.idb_ptr; + argc--;argv++; + if (argc != 2) { + bu_vls_printf(gib->gb->gedp->ged_result_str, " not enough arguments\n"); + bu_vls_printf(gib->gb->gedp->ged_result_str, "%s\n", usage_string); + return BRLCAD_ERROR; + } + int curve_id_1 = atoi(argv[0]); + int curve_id_2 = atoi(argv[1]); + int surfcode = brep_surface_tensor_product(b_ip->brep, curve_id_1, curve_id_2); + if (surfcode < 0) { + bu_vls_printf(gib->gb->gedp->ged_result_str, ": failed to create surface\n"); + return BRLCAD_ERROR; + } + + // Delete the old object + const char *av[3]; + char *ncpy = bu_strdup(gib->gb->solid_name.c_str()); + av[0] = "kill"; + av[1] = ncpy; + av[2] = NULL; + (void)ged_exec(gib->gb->gedp, 2, (const char **)av); + bu_free(ncpy, "free name cpy"); + + // Make the new one + struct rt_wdb *wdbp = wdb_dbopen(gib->gb->gedp->dbip, RT_WDB_TYPE_DB_DEFAULT); + + if (mk_brep(wdbp, gib->gb->solid_name.c_str(), (void *)b_ip->brep)) { + return BRLCAD_ERROR; + } + bu_vls_printf(gib->gb->gedp->ged_result_str, "create surface! id = %d", surfcode); + return BRLCAD_OK; +} + static void _brep_surface_help(struct _ged_brep_isurface *bs, int argc, const char **argv) { @@ -384,6 +428,7 @@ const struct bu_cmdtab _brep_surface_cmds[] = { { "set_cv", _brep_cmd_set_cv}, { "trim", _brep_cmd_surface_trim}, { "split", _brep_cmd_surface_split}, + { "tensor", _brep_cmd_surface_tensor_product}, { (char *)NULL, NULL} }; From c2941beda77dd9db4897f099fea60a2f985b54c5 Mon Sep 17 00:00:00 2001 From: GregoryLi0 Date: Sun, 9 Jul 2023 16:10:37 +0800 Subject: [PATCH 06/18] =?UTF-8?q?=F0=9F=93=83=20docs(libbrep):=20usage=20&?= =?UTF-8?q?=20purpose=20str?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/libged/brep/surface.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/libged/brep/surface.cpp b/src/libged/brep/surface.cpp index cb3d47f9ef7..600fd540794 100644 --- a/src/libged/brep/surface.cpp +++ b/src/libged/brep/surface.cpp @@ -353,8 +353,8 @@ _brep_cmd_surface_split(void *bs, int argc, const char **argv) static int _brep_cmd_surface_tensor_product(void *bs, int argc, const char **argv) { - const char *usage_string = "brep [options] tensor birail "; - const char *purpose_string = "create a new NURBS surface by tensor product of two curves"; + const char *usage_string = "brep [options] tensor "; + const char *purpose_string = "create a new NURBS surface by extruding the first curve along the second curve."; if (_brep_surface_msgs(bs, argc, argv, usage_string, purpose_string)) { return BRLCAD_OK; } From 0ec20219960ce96a17b8547ef6eef1c7651ae126 Mon Sep 17 00:00:00 2001 From: GregoryLi0 Date: Fri, 21 Jul 2023 13:23:43 +0800 Subject: [PATCH 07/18] =?UTF-8?q?=E2=9C=A8=20feat(libbrep/libged):=20creat?= =?UTF-8?q?e=20revSurface?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit add 'brep .. surface revolution ..' to create a NURBS surface by rotating a curve around an axis by an angle.. --- include/brep/edit.h | 7 ++++++ src/libbrep/edit.cpp | 26 +++++++++++++++++++ src/libged/brep/surface.cpp | 50 +++++++++++++++++++++++++++++++++++++ 3 files changed, 83 insertions(+) diff --git a/include/brep/edit.h b/include/brep/edit.h index 7628788e13b..98cd04bb21b 100644 --- a/include/brep/edit.h +++ b/include/brep/edit.h @@ -173,6 +173,13 @@ extern "C++" */ BREP_EXPORT extern int brep_surface_tensor_product(ON_Brep *brep, int curve_id0, int curve_id1); + + /** + * create a surface by rotating a curve around an axis. + * return: if successful, id of the new surface; otherwise, -1. + */ + BREP_EXPORT extern int + brep_surface_revolution(ON_Brep *brep, int curve_id0, ON_3dPoint line_start, ON_3dPoint line_end, double angle = 2 * ON_PI); /** * remove a surface from brep diff --git a/src/libbrep/edit.cpp b/src/libbrep/edit.cpp index d7ce0767f99..f6b21b5a6b4 100644 --- a/src/libbrep/edit.cpp +++ b/src/libbrep/edit.cpp @@ -344,6 +344,32 @@ int brep_surface_tensor_product(ON_Brep *brep, int curve_id0, int curve_id1) return brep->AddSurface(nurbs_surface); } +int brep_surface_revolution(ON_Brep *brep, int curve_id0, ON_3dPoint line_start, ON_3dPoint line_end, double angle) +{ + + ON_NurbsCurve *curve0 = brep_get_nurbs_curve(brep, curve_id0); + if(!curve0) { + return -1; + } + ON_RevSurface* rev_surf = ON_RevSurface::New(); + ON_Line line = ON_Line(line_start, line_end); + if(angle < ON_ZERO_TOLERANCE) { + angle = -angle; + line.Reverse(); + } + if (angle > 2 * ON_PI) { + angle = 2 * ON_PI; + } + rev_surf->m_curve = curve0; + rev_surf->m_axis = line; + rev_surf->m_angle = ON_Interval(0, angle); + + // Get the NURBS form of the surface + ON_NurbsSurface *nurbs_surface = ON_NurbsSurface::New(); + rev_surf->GetNurbForm(*nurbs_surface, 0.0); + return brep->AddSurface(nurbs_surface); +} + bool brep_surface_remove(ON_Brep *brep, int surface_id) { if (surface_id < 0 || surface_id >= brep->m_S.Count()) { diff --git a/src/libged/brep/surface.cpp b/src/libged/brep/surface.cpp index 600fd540794..9a165c03da5 100644 --- a/src/libged/brep/surface.cpp +++ b/src/libged/brep/surface.cpp @@ -394,6 +394,55 @@ _brep_cmd_surface_tensor_product(void *bs, int argc, const char **argv) return BRLCAD_OK; } +static int +_brep_cmd_surface_revolution(void *bs, int argc, const char **argv) +{ + const char *usage_string = "brep [options] revolution []"; + const char *purpose_string = "create a new NURBS surface by rotating a curve around an axis by an angle."; + if (_brep_surface_msgs(bs, argc, argv, usage_string, purpose_string)) { + return BRLCAD_OK; + } + + struct _ged_brep_isurface *gib = (struct _ged_brep_isurface *)bs; + struct rt_brep_internal *b_ip = (struct rt_brep_internal *)gib->gb->intern.idb_ptr; + argc--;argv++; + if (argc < 7) { + bu_vls_printf(gib->gb->gedp->ged_result_str, " not enough arguments\n"); + bu_vls_printf(gib->gb->gedp->ged_result_str, "%s\n", usage_string); + return BRLCAD_ERROR; + } + int curve_id_1 = atoi(argv[0]); + ON_3dPoint line_start(atof(argv[1]), atof(argv[2]), atof(argv[3])); + ON_3dPoint line_end(atof(argv[4]), atof(argv[5]), atof(argv[6])); + double angle = 2 * ON_PI; + if(argc == 8) { + angle = atof(argv[7]); + } + int surfcode = brep_surface_revolution(b_ip->brep, curve_id_1, line_start, line_end, angle); + if (surfcode < 0) { + bu_vls_printf(gib->gb->gedp->ged_result_str, "failed to create surface\n"); + return BRLCAD_ERROR; + } + + // Delete the old object + const char *av[3]; + char *ncpy = bu_strdup(gib->gb->solid_name.c_str()); + av[0] = "kill"; + av[1] = ncpy; + av[2] = NULL; + (void)ged_exec(gib->gb->gedp, 2, (const char **)av); + bu_free(ncpy, "free name cpy"); + + // Make the new one + struct rt_wdb *wdbp = wdb_dbopen(gib->gb->gedp->dbip, RT_WDB_TYPE_DB_DEFAULT); + + if (mk_brep(wdbp, gib->gb->solid_name.c_str(), (void *)b_ip->brep)) { + return BRLCAD_ERROR; + } + bu_vls_printf(gib->gb->gedp->ged_result_str, "create surface! id = %d", surfcode); + return BRLCAD_OK; +} + static void _brep_surface_help(struct _ged_brep_isurface *bs, int argc, const char **argv) { @@ -429,6 +478,7 @@ const struct bu_cmdtab _brep_surface_cmds[] = { { "trim", _brep_cmd_surface_trim}, { "split", _brep_cmd_surface_split}, { "tensor", _brep_cmd_surface_tensor_product}, + { "revolution", _brep_cmd_surface_revolution}, { (char *)NULL, NULL} }; From 128d4fe5adff0727447517e09f9f37167628c970 Mon Sep 17 00:00:00 2001 From: GregoryLi0 Date: Sun, 23 Jul 2023 09:54:37 +0800 Subject: [PATCH 08/18] =?UTF-8?q?=E2=9C=A8=20feat(libbrep/libged):=20copy?= =?UTF-8?q?=20curve?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit add 'brep .. curve copy ..' cmd --- include/brep/edit.h | 7 ++++++ src/libbrep/edit.cpp | 14 +++++++++++ src/libged/brep/curve.cpp | 51 +++++++++++++++++++++++++++++++++++++++ 3 files changed, 72 insertions(+) diff --git a/include/brep/edit.h b/include/brep/edit.h index 98cd04bb21b..da285c9fd11 100644 --- a/include/brep/edit.h +++ b/include/brep/edit.h @@ -72,6 +72,13 @@ extern "C++" BREP_EXPORT extern int brep_curve_interpCrv(ON_Brep *brep, std::vector points); + /** + * copy a curve from brep + * return id of the new curve + */ + BREP_EXPORT extern int + brep_curve_copy(ON_Brep *brep, int curve_id); + /** * remove a curve from brep * @attention the index of m_C3 is changed after remove!!! diff --git a/src/libbrep/edit.cpp b/src/libbrep/edit.cpp index f6b21b5a6b4..6efd0ee87d0 100644 --- a/src/libbrep/edit.cpp +++ b/src/libbrep/edit.cpp @@ -122,6 +122,20 @@ int brep_curve_interpCrv(ON_Brep *brep, std::vector points) return brep->AddEdgeCurve(curve); } +int brep_curve_copy(ON_Brep *brep, int curve_id) +{ + if (curve_id < 0 || curve_id >= brep->m_C3.Count()) { + bu_log("curve_id is out of range\n"); + return -1; + } + ON_Curve *curve = brep->m_C3[curve_id]; + if (!curve) { + return -1; + } + ON_Curve *curve_copy = curve->DuplicateCurve(); + return brep->AddEdgeCurve(curve_copy); +} + bool brep_curve_remove(ON_Brep *brep, int curve_id) { if (curve_id < 0 || curve_id >= brep->m_C3.Count()) { diff --git a/src/libged/brep/curve.cpp b/src/libged/brep/curve.cpp index 86d581308e8..62e74492684 100644 --- a/src/libged/brep/curve.cpp +++ b/src/libged/brep/curve.cpp @@ -236,6 +236,56 @@ _brep_cmd_curve_interp(void *bs, int argc, const char **argv) return BRLCAD_OK; } +static int +_brep_cmd_curve_copy(void *bs, int argc, const char **argv) +{ + const char *usage_string = "brep [options] curve copy "; + const char *purpose_string = "copy a NURBS curve"; + if (_brep_curve_msgs(bs, argc, argv, usage_string, purpose_string)) { + return BRLCAD_OK; + } + + struct _ged_brep_icurve *gib = (struct _ged_brep_icurve *)bs; + if (argc < 2) { + bu_vls_printf(gib->gb->gedp->ged_result_str, "not enough arguments\n"); + bu_vls_printf(gib->gb->gedp->ged_result_str, "%s\n", usage_string); + return BRLCAD_ERROR; + } + + int curve_id = atoi(argv[1]); + + if (curve_id <= 0) { + bu_vls_printf(gib->gb->gedp->ged_result_str, "invalid curve_id\n"); + return BRLCAD_ERROR; + } + + struct rt_brep_internal *b_ip = (struct rt_brep_internal *)gib->gb->intern.idb_ptr; + + int res = brep_curve_copy(b_ip->brep, curve_id); + if (!res) { + bu_vls_printf(gib->gb->gedp->ged_result_str, "failed to copy curve\n"); + return BRLCAD_ERROR; + } + + // Delete the old object + const char *av[3]; + char *ncpy = bu_strdup(gib->gb->solid_name.c_str()); + av[0] = "kill"; + av[1] = ncpy; + av[2] = NULL; + (void)ged_exec(gib->gb->gedp, 2, (const char **)av); + bu_free(ncpy, "free name cpy"); + + // Make the new one + struct rt_wdb *wdbp = wdb_dbopen(gib->gb->gedp->dbip, RT_WDB_TYPE_DB_DEFAULT); + + if (mk_brep(wdbp, gib->gb->solid_name.c_str(), (void *)b_ip->brep)) { + return BRLCAD_ERROR; + } + bu_vls_printf(gib->gb->gedp->ged_result_str, "successful copy C3 curve! new curve id = %d", res); + return BRLCAD_OK; +} + static int _brep_cmd_curve_remove(void *bs, int argc, const char **argv) { @@ -703,6 +753,7 @@ const struct bu_cmdtab _brep_curve_cmds[] = { { "create", _brep_cmd_curve_create}, { "in", _brep_cmd_curve_in}, { "inter", _brep_cmd_curve_interp}, + { "copy", _brep_cmd_curve_copy}, { "remove", _brep_cmd_curve_remove}, { "move", _brep_cmd_curve_move}, { "set_cv", _brep_cmd_curve_set_cv}, From d8fb5f2ce36212ad52f45504a51f98319c884cd2 Mon Sep 17 00:00:00 2001 From: GregoryLi0 Date: Sun, 23 Jul 2023 10:22:45 +0800 Subject: [PATCH 09/18] =?UTF-8?q?=F0=9F=90=9E=20fix(libged):=20fix=20curve?= =?UTF-8?q?=20copy?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit fix while curve id==0 --- src/libged/brep/curve.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/libged/brep/curve.cpp b/src/libged/brep/curve.cpp index 62e74492684..430f0f5d855 100644 --- a/src/libged/brep/curve.cpp +++ b/src/libged/brep/curve.cpp @@ -254,7 +254,7 @@ _brep_cmd_curve_copy(void *bs, int argc, const char **argv) int curve_id = atoi(argv[1]); - if (curve_id <= 0) { + if (curve_id < 0) { bu_vls_printf(gib->gb->gedp->ged_result_str, "invalid curve_id\n"); return BRLCAD_ERROR; } From 8d4f15e4da9aed4ef8257aeb9015afff985f8ec1 Mon Sep 17 00:00:00 2001 From: GregoryLi0 Date: Sun, 23 Jul 2023 10:23:20 +0800 Subject: [PATCH 10/18] =?UTF-8?q?=E2=9C=A8=20feat(libbrep/libged):=20copy?= =?UTF-8?q?=20surface?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit add 'brep .. surface copy ..' cmd --- include/brep/edit.h | 7 +++++ src/libbrep/edit.cpp | 20 +++++++++++--- src/libged/brep/surface.cpp | 52 ++++++++++++++++++++++++++++++++++++- 3 files changed, 75 insertions(+), 4 deletions(-) diff --git a/include/brep/edit.h b/include/brep/edit.h index da285c9fd11..ac2fc6f2b5b 100644 --- a/include/brep/edit.h +++ b/include/brep/edit.h @@ -139,6 +139,13 @@ extern "C++" BREP_EXPORT extern int brep_surface_make(ON_Brep *brep, const ON_3dPoint &position); + /** + * copy a surface from brep + * return id of the new surface + */ + BREP_EXPORT extern int + brep_surface_copy(ON_Brep *brep, int surface_id); + /** * move surface to a new position */ diff --git a/src/libbrep/edit.cpp b/src/libbrep/edit.cpp index 6efd0ee87d0..c3ba1031571 100644 --- a/src/libbrep/edit.cpp +++ b/src/libbrep/edit.cpp @@ -125,12 +125,12 @@ int brep_curve_interpCrv(ON_Brep *brep, std::vector points) int brep_curve_copy(ON_Brep *brep, int curve_id) { if (curve_id < 0 || curve_id >= brep->m_C3.Count()) { - bu_log("curve_id is out of range\n"); - return -1; + bu_log("curve_id is out of range\n"); + return -1; } ON_Curve *curve = brep->m_C3[curve_id]; if (!curve) { - return -1; + return -1; } ON_Curve *curve_copy = curve->DuplicateCurve(); return brep->AddEdgeCurve(curve_copy); @@ -274,6 +274,20 @@ int brep_surface_make(ON_Brep *brep, const ON_3dPoint &position) return brep->AddSurface(surface); } +int brep_surface_copy(ON_Brep *brep, int surface_id) +{ + if (surface_id < 0 || surface_id >= brep->m_S.Count()) { + bu_log("surface_id is out of range\n"); + return -1; + } + ON_Surface *surface = brep->m_S[surface_id]; + if (!surface) { + return -1; + } + ON_Surface *surface_copy = surface->DuplicateSurface(); + return brep->AddSurface(surface_copy); +} + bool brep_surface_move(ON_Brep *brep, int surface_id, const ON_3dVector &point) { /// the surface could be a NURBS surface or not diff --git a/src/libged/brep/surface.cpp b/src/libged/brep/surface.cpp index 9a165c03da5..169091f176f 100644 --- a/src/libged/brep/surface.cpp +++ b/src/libged/brep/surface.cpp @@ -89,6 +89,55 @@ _brep_cmd_surface_create(void *bs, int argc, const char **argv) return BRLCAD_OK; } +static int +_brep_cmd_surface_copy(void *bs, int argc, const char **argv) +{ + const char *usage_string = "brep [options] surface copy "; + const char *purpose_string = "copy a NURBS surface"; + if (_brep_surface_msgs(bs, argc, argv, usage_string, purpose_string)) { + return BRLCAD_OK; + } + + struct _ged_brep_isurface *gib = (struct _ged_brep_isurface *)bs; + if (argc < 2) { + bu_vls_printf(gib->gb->gedp->ged_result_str, "not enough arguments\n"); + bu_vls_printf(gib->gb->gedp->ged_result_str, "%s\n", usage_string); + return BRLCAD_ERROR; + } + + int surface_id = atoi(argv[1]); + + if (surface_id < 0) { + bu_vls_printf(gib->gb->gedp->ged_result_str, "invalid surface_id\n"); + return BRLCAD_ERROR; + } + + struct rt_brep_internal *b_ip = (struct rt_brep_internal *)gib->gb->intern.idb_ptr; + int res = brep_surface_copy(b_ip->brep, surface_id); + if (res < 0) { + bu_vls_printf(gib->gb->gedp->ged_result_str, ": failed to copy surface\n"); + return BRLCAD_ERROR; + } + + // Delete the old object + const char *av[3]; + char *ncpy = bu_strdup(gib->gb->solid_name.c_str()); + av[0] = "kill"; + av[1] = ncpy; + av[2] = NULL; + (void)ged_exec(gib->gb->gedp, 2, (const char **)av); + bu_free(ncpy, "free name cpy"); + + // Make the new one + struct rt_wdb *wdbp = wdb_dbopen(gib->gb->gedp->dbip, RT_WDB_TYPE_DB_DEFAULT); + + if (mk_brep(wdbp, gib->gb->solid_name.c_str(), (void *)b_ip->brep)) { + return BRLCAD_ERROR; + } + bu_vls_printf(gib->gb->gedp->ged_result_str, "successful copy surface! new surface id = %d", res); + return BRLCAD_OK; +} + static int _brep_cmd_surface_birail(void *bs, int argc, const char **argv) { @@ -110,7 +159,7 @@ _brep_cmd_surface_birail(void *bs, int argc, const char **argv) int curve_id_2 = atoi(argv[1]); int surfcode = brep_surface_create_ruled(b_ip->brep, curve_id_1, curve_id_2); if (surfcode < 0) { - bu_vls_printf(gib->gb->gedp->ged_result_str, ": failed to create surface\n"); + bu_vls_printf(gib->gb->gedp->ged_result_str, "failed to create surface\n"); return BRLCAD_ERROR; } @@ -471,6 +520,7 @@ _brep_surface_help(struct _ged_brep_isurface *bs, int argc, const char **argv) const struct bu_cmdtab _brep_surface_cmds[] = { { "create", _brep_cmd_surface_create}, + { "copy", _brep_cmd_surface_copy}, { "birail", _brep_cmd_surface_birail}, { "remove", _brep_cmd_surface_remove}, { "move", _brep_cmd_surface_move}, From 1031b1b4f4406f1fe57dd0290b72d6a54b00810a Mon Sep 17 00:00:00 2001 From: GregoryLi0 Date: Mon, 31 Jul 2023 14:14:23 +0800 Subject: [PATCH 11/18] =?UTF-8?q?=E2=9C=A8=20feat(libbrep/libged):=20surf?= =?UTF-8?q?=20interp=20knot?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Caculate knot vector while interpolating bicubic surface --- include/brep/edit.h | 7 +++ src/libbrep/edit.cpp | 109 ++++++++++++++++++++++++++++++++++++ src/libged/brep/curve.cpp | 4 +- src/libged/brep/surface.cpp | 67 ++++++++++++++++++++++ 4 files changed, 185 insertions(+), 2 deletions(-) diff --git a/include/brep/edit.h b/include/brep/edit.h index ac2fc6f2b5b..9c01fb1b74b 100644 --- a/include/brep/edit.h +++ b/include/brep/edit.h @@ -139,6 +139,13 @@ extern "C++" BREP_EXPORT extern int brep_surface_make(ON_Brep *brep, const ON_3dPoint &position); + /** + * create a bicubic nurbs surface by interpolating a set of points + * return id of the curve + */ + BREP_EXPORT extern int + brep_surface_interpCrv(ON_Brep *brep, int cv_count_x, int cv_count_y, std::vector points); + /** * copy a surface from brep * return id of the new surface diff --git a/src/libbrep/edit.cpp b/src/libbrep/edit.cpp index c3ba1031571..bb66ebd0dd2 100644 --- a/src/libbrep/edit.cpp +++ b/src/libbrep/edit.cpp @@ -288,6 +288,46 @@ int brep_surface_copy(ON_Brep *brep, int surface_id) return brep->AddSurface(surface_copy); } +int SurfMeshParams(int n, int m, std::vector points, std::vector &uk, std::vector &ul); + +int brep_surface_interpCrv(ON_Brep *brep, int cv_count_x, int cv_count_y, std::vector points) +{ + cv_count_x = cv_count_x < 2 ? 2 : cv_count_x; + cv_count_y = cv_count_y < 2 ? 2 : cv_count_y; + if (points.size() != (size_t)(cv_count_x * cv_count_y)) { + return -1; + } + + /// calculate parameter values of each point + std::vector uk, ul; + SurfMeshParams(cv_count_x - 1, cv_count_y - 1, points, uk, ul); + + /// calculate knots of the cubic B-spline surface + std::vector knots_u, knots_v; + for (size_t i = 0; i < 4; i++) + { + knots_u.push_back(0); + knots_v.push_back(0); + } + for (size_t i = 1; i < uk.size() - 1; i++) + { + knots_u.push_back(uk[i]); + } + for (size_t i = 1; i < ul.size() - 1; i++) + { + knots_v.push_back(ul[i]); + } + for (int i = 0; i < 4; i++) + { + knots_u.push_back(1); + knots_v.push_back(1); + } + if(!brep){ + return -1; + } + return -1; +} + bool brep_surface_move(ON_Brep *brep, int surface_id, const ON_3dVector &point) { /// the surface could be a NURBS surface or not @@ -504,6 +544,75 @@ void calcuBsplineCVsKnots(std::vector &cvs, std::vector &kno knots.push_back(1); } +// caculate parameters of the surface +int SurfMeshParams(int n, int m, std::vector points, std::vector &uk, std::vector &ul) +{ + std::vector cds; + n > m ? cds.resize(n + 1) : cds.resize(m + 1); + int num = m + 1; // number of nondegenerate rows + uk.resize(n + 1); + uk[0] = 0.0f; + uk[n] = 1.0f; + for (int i = 1; i < n; i++) + uk[i] = 0; + for (int i = 0; i <= m; i++) + { + double sum = 0; + for (int j = 1; j <= n; j++) + { + cds[j] = points[i * (n + 1) + j].DistanceTo(points[i * (n + 1) + j - 1]); + sum += cds[j]; + } + if (sum <= 0) + num--; + else + { + double d = 0.0f; + for (int j = 1; j < n; j++) + { + d += cds[j]; + uk[j] += d / sum; + } + } + } + if (num == 0) + return -1; + for (int i = 1; i < n; i++) + uk[i] /= num; + + num = n + 1; + ul.resize(m + 1); + ul[0] = 0.0f; + ul[m] = 1.0f; + for (int i = 1; i < m; i++) + ul[i] = 0; + for (int i = 0; i <= n; i++) + { + double sum = 0; + for (int j = 1; j <= m; j++) + { + cds[j] = points[j * (n + 1) + i].DistanceTo(points[(j - 1) * (n + 1) + i]); + sum += cds[j]; + } + if (sum <= 0) + num--; + else + { + double d = 0.0f; + for (int j = 1; j < m; j++) + { + d += cds[j]; + ul[j] += d / sum; + } + } + } + if (num == 0) + return -1; + for (int i = 1; i < m; i++) + ul[i] /= num; + return 0; +} + // Local Variables: // tab-width: 8 // mode: C++ diff --git a/src/libged/brep/curve.cpp b/src/libged/brep/curve.cpp index 430f0f5d855..f49c1520992 100644 --- a/src/libged/brep/curve.cpp +++ b/src/libged/brep/curve.cpp @@ -189,7 +189,7 @@ _brep_cmd_curve_interp(void *bs, int argc, const char **argv) int cv_count = atoi(argv[1]); if (cv_count <= 0) { - bu_vls_printf(gib->gb->gedp->ged_result_str, "invalid order or cv_count\n"); + bu_vls_printf(gib->gb->gedp->ged_result_str, "invalid cv_count\n"); return BRLCAD_ERROR; } @@ -752,7 +752,7 @@ _brep_curve_help(struct _ged_brep_icurve *bs, int argc, const char **argv) const struct bu_cmdtab _brep_curve_cmds[] = { { "create", _brep_cmd_curve_create}, { "in", _brep_cmd_curve_in}, - { "inter", _brep_cmd_curve_interp}, + { "interp", _brep_cmd_curve_interp}, { "copy", _brep_cmd_curve_copy}, { "remove", _brep_cmd_curve_remove}, { "move", _brep_cmd_curve_move}, diff --git a/src/libged/brep/surface.cpp b/src/libged/brep/surface.cpp index 169091f176f..29ba2ce2ec1 100644 --- a/src/libged/brep/surface.cpp +++ b/src/libged/brep/surface.cpp @@ -89,6 +89,72 @@ _brep_cmd_surface_create(void *bs, int argc, const char **argv) return BRLCAD_OK; } +static int +_brep_cmd_surface_interp(void *bs, int argc, const char **argv) +{ + const char *usage_string = "brep [options] surface interp ..."; + const char *purpose_string = "create a new NURBS surface"; + if (_brep_surface_msgs(bs, argc, argv, usage_string, purpose_string)) { + return BRLCAD_OK; + } + + struct _ged_brep_isurface *gib = (struct _ged_brep_isurface *)bs; + if (argc < 3) { + bu_vls_printf(gib->gb->gedp->ged_result_str, "not enough arguments\n"); + bu_vls_printf(gib->gb->gedp->ged_result_str, "%s\n", usage_string); + return BRLCAD_ERROR; + } + + argc--;argv++; + // Load and check the number of arguments + int cv_count_x = atoi(argv[0]); + int cv_count_y = atoi(argv[1]); + if (cv_count_x <= 0 || cv_count_y <= 0) { + bu_vls_printf(gib->gb->gedp->ged_result_str, "invalid cv_count\n"); + return BRLCAD_ERROR; + } + + if (argc < 2 + (cv_count_x *cv_count_y) * 3) { + bu_vls_printf(gib->gb->gedp->ged_result_str, "not enough arguments, you need to input %d more args about control vertices\n", 2 + (cv_count_x * cv_count_y) * 3 - argc); + bu_vls_printf(gib->gb->gedp->ged_result_str, "%s\n", usage_string); + return BRLCAD_ERROR; + } + + struct rt_brep_internal *b_ip = (struct rt_brep_internal *)gib->gb->intern.idb_ptr; + + std::vector points; + for (int i = 0; i < (cv_count_x * cv_count_y); i++) { + ON_3dPoint p; + p.x = atof(argv[2 + i * 3]); + p.y = atof(argv[2 + i * 3 + 1]); + p.z = atof(argv[2 + i * 3 + 2]); + points.push_back(p); + } + int surface_id = brep_surface_interpCrv(b_ip->brep, cv_count_x, cv_count_y, points); + if(surface_id < 0) { + bu_vls_printf(gib->gb->gedp->ged_result_str, "failed to create surface\n"); + return BRLCAD_ERROR; + } + + // Delete the old object + const char *av[3]; + char *ncpy = bu_strdup(gib->gb->solid_name.c_str()); + av[0] = "kill"; + av[1] = ncpy; + av[2] = NULL; + (void)ged_exec(gib->gb->gedp, 2, (const char **)av); + bu_free(ncpy, "free name cpy"); + + // Make the new one + struct rt_wdb *wdbp = wdb_dbopen(gib->gb->gedp->dbip, RT_WDB_TYPE_DB_DEFAULT); + + if (mk_brep(wdbp, gib->gb->solid_name.c_str(), (void *)b_ip->brep)) { + return BRLCAD_ERROR; + } + bu_vls_printf(gib->gb->gedp->ged_result_str, "create surface! id = %d", surface_id); + return BRLCAD_OK; +} + static int _brep_cmd_surface_copy(void *bs, int argc, const char **argv) { @@ -520,6 +586,7 @@ _brep_surface_help(struct _ged_brep_isurface *bs, int argc, const char **argv) const struct bu_cmdtab _brep_surface_cmds[] = { { "create", _brep_cmd_surface_create}, + { "interp", _brep_cmd_surface_interp}, { "copy", _brep_cmd_surface_copy}, { "birail", _brep_cmd_surface_birail}, { "remove", _brep_cmd_surface_remove}, From 6bee31f7e999f5095eecd1f1006945efeca895e7 Mon Sep 17 00:00:00 2001 From: GregoryLi0 Date: Mon, 31 Jul 2023 15:50:01 +0800 Subject: [PATCH 12/18] =?UTF-8?q?=F0=9F=8E=89=20init(libbrep/libged):=20su?= =?UTF-8?q?rf=20interp?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit implement global surface interpolation with C2 continuity --- src/libbrep/edit.cpp | 156 +++++++++++++++++++++++++++++++++++++------ 1 file changed, 136 insertions(+), 20 deletions(-) diff --git a/src/libbrep/edit.cpp b/src/libbrep/edit.cpp index bb66ebd0dd2..15c02e74af2 100644 --- a/src/libbrep/edit.cpp +++ b/src/libbrep/edit.cpp @@ -288,8 +288,18 @@ int brep_surface_copy(ON_Brep *brep, int surface_id) return brep->AddSurface(surface_copy); } +/** + * caculate parameter values of each point + */ int SurfMeshParams(int n, int m, std::vector points, std::vector &uk, std::vector &ul); +/** + * global curve interpolation + * input: n, knots, Q + * output: P + */ +void globalCurveInterp(int n, std::vector &knots, const std::vector &Q, std::vector &P); + int brep_surface_interpCrv(ON_Brep *brep, int cv_count_x, int cv_count_y, std::vector points) { cv_count_x = cv_count_x < 2 ? 2 : cv_count_x; @@ -297,35 +307,62 @@ int brep_surface_interpCrv(ON_Brep *brep, int cv_count_x, int cv_count_y, std::v if (points.size() != (size_t)(cv_count_x * cv_count_y)) { return -1; } + int n = cv_count_x - 1; + int m = cv_count_y - 1; /// calculate parameter values of each point std::vector uk, ul; - SurfMeshParams(cv_count_x - 1, cv_count_y - 1, points, uk, ul); + SurfMeshParams(n, m, points, uk, ul); /// calculate knots of the cubic B-spline surface std::vector knots_u, knots_v; - for (size_t i = 0; i < 4; i++) - { + for (size_t i = 0; i < 4; i++) { knots_u.push_back(0); knots_v.push_back(0); } - for (size_t i = 1; i < uk.size() - 1; i++) - { + for (size_t i = 1; i < uk.size() - 1; i++) { knots_u.push_back(uk[i]); } - for (size_t i = 1; i < ul.size() - 1; i++) - { + for (size_t i = 1; i < ul.size() - 1; i++) { knots_v.push_back(ul[i]); } - for (int i = 0; i < 4; i++) - { + for (int i = 0; i < 4; i++) { knots_u.push_back(1); knots_v.push_back(1); } - if(!brep){ - return -1; + + /// curve interpolation in v direction + // temporary control points + std::vector> R(n + 1, std::vector(m + 3)); + + for (int l = 0; l <= n; l++) { + std::vector Q(m + 1); + for (int k = 0; k <= m; k++) { + Q[k] = points[l * (m + 1) + k]; + } + globalCurveInterp(m, knots_v, Q, R[l]); } - return -1; + + ON_NurbsSurface *surface = ON_NurbsSurface::New(3, false, 4, 4, n + 3, m + 3); + for (int i = 0; i < m + 3; i++) { + std::vector Q(n + 1); + for (int j = 0; j < n + 1; j++) { + Q[j] = R[j][i]; + } + std::vector P(n + 3); + globalCurveInterp(n, knots_u, Q, P); + for (int j = 0; j < n + 3; j++) { + surface->SetCV(j, i, P[j]); + } + } + /// the knot vector of openNURBS is different from the NURBS book + for (size_t i = 1; i < knots_u.size() - 1; i++) { + surface->SetKnot(0, i - 1, knots_u[i]); + } + for (size_t i = 1; i < knots_v.size() - 1; i++) { + surface->SetKnot(1, i - 1, knots_v[i]); + } + return brep->AddSurface(surface); } bool brep_surface_move(ON_Brep *brep, int surface_id, const ON_3dVector &point) @@ -545,6 +582,8 @@ void calcuBsplineCVsKnots(std::vector &cvs, std::vector &kno } // caculate parameters of the surface +// input: n, m, points +// output: uk, ul int SurfMeshParams(int n, int m, std::vector points, std::vector &uk, std::vector &ul) { std::vector cds; @@ -555,11 +594,9 @@ int SurfMeshParams(int n, int m, std::vector points, std::vector points, std::vector points, std::vector U, std::vector &N) +{ + double *left = (double *)bu_calloc(p + 1, sizeof(double), "left"); + double *right = (double *)bu_calloc(p + 1, sizeof(double), "right"); + double saved, temp; + int j, r; + N[0] = 1.0; + + for (j = 1; j <= p; j++) { + left[j] = u - U[i + 1 - j]; + right[j] = U[i + j] - u; + saved = 0.0; + + for (r = 0; r < j; r++) { + temp = N[r] / (right[r + 1] + left[j - r]); + N[r] = saved + right[r + 1] * temp; + saved = left[j - r] * temp; + } + N[j] = saved; + } + bu_free(left, "left"); + bu_free(right, "right"); +} + +/** + * solve tridiagonal equation for cubic spline interpolation + */ +int solveTridiagonalint(int n, std::vector Q, std::vector U, std::vector& P) +{ + std::vector abc(4); + std::vector R(n + 1); + std::vector dd(n + 1); + double den; + int i; + + for (i = 3; i < n; i++) { + R[i] = Q[i - 1]; + } + + bsplineBasisFuns(4, U[4], 3, U, abc); + den = abc[1]; + + /* P[2] */ + P[2] = (Q[1] - abc[0] * P[1]) / den; + + for (i = 3; i < n; i++) { + dd[i] = abc[2] / den; + + bsplineBasisFuns(i + 2, U[i + 2], 3, U, abc); + den = abc[1] - abc[0] * dd[i]; + P[i] = (R[i] - abc[0] * P[i - 1]) / den; + } + + dd[n] = abc[2] / den; + + bsplineBasisFuns(n + 2, U[n + 2], 3, U, abc); + den = abc[1] - abc[0] * dd[n]; + + P[n] = (Q[n - 1] - abc[2] * P[n + 1] - abc[0] * P[n - 1]) / den; + + for (i = (n - 1); i >= 2; i--) { + P[i] = P[i] - dd[i + 1] * P[i + 1]; + } + + if (n == 2) { + P[2] /= 4.0f / 3.0f; + } + return 1; +} + +void globalCurveInterp(int n, std::vector &knots, const std::vector &Q, std::vector &P) +{ + /// initialize control points of P[0], P[1], P[n+1], P[n+2] + P.resize(n + 3); + P[0] = Q[0]; + P[1] = Q[0] + (Q[1] - Q[0]) / 3.0f * knots[4]; + P[n + 2] = Q[n]; + P[n + 1] = Q[n] - (Q[n] - Q[n - 1]) / 3.0f * (1.0f - knots[n + 2]); + + solveTridiagonalint(n, Q, knots, P); +} // Local Variables: // tab-width: 8 // mode: C++ From a932131acc0991764489bd34f712555908873626 Mon Sep 17 00:00:00 2001 From: GregoryLi0 Date: Mon, 31 Jul 2023 15:55:48 +0800 Subject: [PATCH 13/18] =?UTF-8?q?=F0=9F=93=83=20docs(libbrep/libged):=20ad?= =?UTF-8?q?d=20some=20comments?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/libbrep/edit.cpp | 1 + src/libged/brep/surface.cpp | 6 +++--- 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/src/libbrep/edit.cpp b/src/libbrep/edit.cpp index 15c02e74af2..82a756301ad 100644 --- a/src/libbrep/edit.cpp +++ b/src/libbrep/edit.cpp @@ -297,6 +297,7 @@ int SurfMeshParams(int n, int m, std::vector points, std::vector &knots, const std::vector &Q, std::vector &P); diff --git a/src/libged/brep/surface.cpp b/src/libged/brep/surface.cpp index 29ba2ce2ec1..8a3a01f49c3 100644 --- a/src/libged/brep/surface.cpp +++ b/src/libged/brep/surface.cpp @@ -109,11 +109,11 @@ _brep_cmd_surface_interp(void *bs, int argc, const char **argv) // Load and check the number of arguments int cv_count_x = atoi(argv[0]); int cv_count_y = atoi(argv[1]); - if (cv_count_x <= 0 || cv_count_y <= 0) { - bu_vls_printf(gib->gb->gedp->ged_result_str, "invalid cv_count\n"); + if (cv_count_x <= 2 || cv_count_y <= 2) { + bu_vls_printf(gib->gb->gedp->ged_result_str, "invalid cv_count, cv_count >= 3\n"); return BRLCAD_ERROR; } - + if (argc < 2 + (cv_count_x *cv_count_y) * 3) { bu_vls_printf(gib->gb->gedp->ged_result_str, "not enough arguments, you need to input %d more args about control vertices\n", 2 + (cv_count_x * cv_count_y) * 3 - argc); bu_vls_printf(gib->gb->gedp->ged_result_str, "%s\n", usage_string); From 9259b50bcc3c6ecce684704bcfe03baffc5ad5c0 Mon Sep 17 00:00:00 2001 From: GregoryLi0 Date: Tue, 1 Aug 2023 14:48:16 +0800 Subject: [PATCH 14/18] =?UTF-8?q?=F0=9F=93=83=20docs(libbrep):=20change=20?= =?UTF-8?q?comments?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- include/brep/edit.h | 4 +++- src/libbrep/edit.cpp | 3 ++- 2 files changed, 5 insertions(+), 2 deletions(-) diff --git a/include/brep/edit.h b/include/brep/edit.h index 9c01fb1b74b..e4d3cec784c 100644 --- a/include/brep/edit.h +++ b/include/brep/edit.h @@ -141,7 +141,9 @@ extern "C++" /** * create a bicubic nurbs surface by interpolating a set of points - * return id of the curve + * return id of the surface + * method: Global cubic interpolation with C2 continuity + * reference: The NURBS Book (2nd Edition), chapter 9.2.5 */ BREP_EXPORT extern int brep_surface_interpCrv(ON_Brep *brep, int cv_count_x, int cv_count_y, std::vector points); diff --git a/src/libbrep/edit.cpp b/src/libbrep/edit.cpp index 82a756301ad..86e6f11abf7 100644 --- a/src/libbrep/edit.cpp +++ b/src/libbrep/edit.cpp @@ -294,9 +294,10 @@ int brep_surface_copy(ON_Brep *brep, int surface_id) int SurfMeshParams(int n, int m, std::vector points, std::vector &uk, std::vector &ul); /** - * global curve interpolation + * global cubic curve interpolation with C2 continuity * input: n, knots, Q * output: P + * reference: The NURBS Book (2nd Edition), chapter 9.2.3 * TODO: while n <= 3, the special case should be considered */ void globalCurveInterp(int n, std::vector &knots, const std::vector &Q, std::vector &P); From de0feef3c13e05b5eb02d8b22f2c1db3520b2ffc Mon Sep 17 00:00:00 2001 From: GregoryLi0 Date: Tue, 1 Aug 2023 15:58:01 +0800 Subject: [PATCH 15/18] =?UTF-8?q?=F0=9F=90=9E=20fix(libbrep):=20fix=20get?= =?UTF-8?q?=5Fnurbs=5Fsurf?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/libbrep/edit.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/libbrep/edit.cpp b/src/libbrep/edit.cpp index 86e6f11abf7..e5f721b7f60 100644 --- a/src/libbrep/edit.cpp +++ b/src/libbrep/edit.cpp @@ -82,7 +82,7 @@ ON_NurbsCurve *brep_get_nurbs_curve(ON_Brep *brep, int curve_id) ON_NurbsSurface *brep_get_nurbs_surface(ON_Brep *brep, int surface_id) { - if (surface_id < 0 || surface_id >= brep->m_C3.Count()) { + if (surface_id < 0 || surface_id >= brep->m_S.Count()) { bu_log("surface_id is out of range\n"); return NULL; } From a41ab71f647a00d407fa92e60bd2507a4dacf0e0 Mon Sep 17 00:00:00 2001 From: GregoryLi0 Date: Fri, 11 Aug 2023 21:23:44 +0800 Subject: [PATCH 16/18] =?UTF-8?q?=F0=9F=90=9E=20fix(libged):=20remove=20'k?= =?UTF-8?q?ill'?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit no more delete old object. Functions are unaffected. Passed tests by brep_curve_edit.sh and brep_surface_edit.sh on zulip. --- src/libged/brep/curve.cpp | 99 ------------------------------------- src/libged/brep/surface.cpp | 99 ------------------------------------- 2 files changed, 198 deletions(-) diff --git a/src/libged/brep/curve.cpp b/src/libged/brep/curve.cpp index f49c1520992..74a19354591 100644 --- a/src/libged/brep/curve.cpp +++ b/src/libged/brep/curve.cpp @@ -78,15 +78,6 @@ _brep_cmd_curve_create(void *bs, int argc, const char **argv) // Create a template nurbs curve int curve_id = brep_curve_make(b_ip->brep, position); - // Delete the old object - const char *av[3]; - char *ncpy = bu_strdup(gib->gb->solid_name.c_str()); - av[0] = "kill"; - av[1] = ncpy; - av[2] = NULL; - (void)ged_exec(gib->gb->gedp, 2, (const char **)av); - bu_free(ncpy, "free name cpy"); - // Make the new one struct rt_wdb *wdbp = wdb_dbopen(gib->gb->gedp->dbip, RT_WDB_TYPE_DB_DEFAULT); @@ -151,15 +142,6 @@ _brep_cmd_curve_in(void *bs, int argc, const char **argv) return BRLCAD_ERROR; } - // Delete the old object - const char *av[3]; - char *ncpy = bu_strdup(gib->gb->solid_name.c_str()); - av[0] = "kill"; - av[1] = ncpy; - av[2] = NULL; - (void)ged_exec(gib->gb->gedp, 2, (const char **)av); - bu_free(ncpy, "free name cpy"); - // Make the new one struct rt_wdb *wdbp = wdb_dbopen(gib->gb->gedp->dbip, RT_WDB_TYPE_DB_DEFAULT); @@ -217,15 +199,6 @@ _brep_cmd_curve_interp(void *bs, int argc, const char **argv) return BRLCAD_ERROR; } - // Delete the old object - const char *av[3]; - char *ncpy = bu_strdup(gib->gb->solid_name.c_str()); - av[0] = "kill"; - av[1] = ncpy; - av[2] = NULL; - (void)ged_exec(gib->gb->gedp, 2, (const char **)av); - bu_free(ncpy, "free name cpy"); - // Make the new one struct rt_wdb *wdbp = wdb_dbopen(gib->gb->gedp->dbip, RT_WDB_TYPE_DB_DEFAULT); @@ -267,15 +240,6 @@ _brep_cmd_curve_copy(void *bs, int argc, const char **argv) return BRLCAD_ERROR; } - // Delete the old object - const char *av[3]; - char *ncpy = bu_strdup(gib->gb->solid_name.c_str()); - av[0] = "kill"; - av[1] = ncpy; - av[2] = NULL; - (void)ged_exec(gib->gb->gedp, 2, (const char **)av); - bu_free(ncpy, "free name cpy"); - // Make the new one struct rt_wdb *wdbp = wdb_dbopen(gib->gb->gedp->dbip, RT_WDB_TYPE_DB_DEFAULT); @@ -317,15 +281,6 @@ _brep_cmd_curve_remove(void *bs, int argc, const char **argv) return BRLCAD_ERROR; } - // Delete the old object - const char *av[3]; - char *ncpy = bu_strdup(gib->gb->solid_name.c_str()); - av[0] = "kill"; - av[1] = ncpy; - av[2] = NULL; - (void)ged_exec(gib->gb->gedp, 2, (const char **)av); - bu_free(ncpy, "free name cpy"); - // Make the new one struct rt_wdb *wdbp = wdb_dbopen(gib->gb->gedp->dbip, RT_WDB_TYPE_DB_DEFAULT); @@ -361,15 +316,6 @@ _brep_cmd_curve_move(void *bs, int argc, const char **argv) return BRLCAD_ERROR; } - // Delete the old object - const char *av[3]; - char *ncpy = bu_strdup(gib->gb->solid_name.c_str()); - av[0] = "kill"; - av[1] = ncpy; - av[2] = NULL; - (void)ged_exec(gib->gb->gedp, 2, (const char **)av); - bu_free(ncpy, "free name cpy"); - // Make the new one struct rt_wdb *wdbp = wdb_dbopen(gib->gb->gedp->dbip, RT_WDB_TYPE_DB_DEFAULT); @@ -405,15 +351,6 @@ _brep_cmd_curve_set_cv(void *bs, int argc, const char **argv) return BRLCAD_ERROR; } - // Delete the old object - const char *av[3]; - char *ncpy = bu_strdup(gib->gb->solid_name.c_str()); - av[0] = "kill"; - av[1] = ncpy; - av[2] = NULL; - (void)ged_exec(gib->gb->gedp, 2, (const char **)av); - bu_free(ncpy, "free name cpy"); - // Make the new one struct rt_wdb *wdbp = wdb_dbopen(gib->gb->gedp->dbip, RT_WDB_TYPE_DB_DEFAULT); @@ -456,15 +393,6 @@ _brep_cmd_curve_flip(void *bs, int argc, const char **argv) return BRLCAD_ERROR; } - // Delete the old object - const char *av[3]; - char *ncpy = bu_strdup(gib->gb->solid_name.c_str()); - av[0] = "kill"; - av[1] = ncpy; - av[2] = NULL; - (void)ged_exec(gib->gb->gedp, 2, (const char **)av); - bu_free(ncpy, "free name cpy"); - // Make the new one struct rt_wdb *wdbp = wdb_dbopen(gib->gb->gedp->dbip, RT_WDB_TYPE_DB_DEFAULT); @@ -593,15 +521,6 @@ _brep_cmd_curve_trim(void *bs, int argc, const char **argv) return BRLCAD_ERROR; } - // Delete the old object - const char *av[3]; - char *ncpy = bu_strdup(gib->gb->solid_name.c_str()); - av[0] = "kill"; - av[1] = ncpy; - av[2] = NULL; - (void)ged_exec(gib->gb->gedp, 2, (const char **)av); - bu_free(ncpy, "free name cpy"); - // Make the new one struct rt_wdb *wdbp = wdb_dbopen(gib->gb->gedp->dbip, RT_WDB_TYPE_DB_DEFAULT); @@ -652,15 +571,6 @@ _brep_cmd_curve_split(void *bs, int argc, const char **argv) return BRLCAD_ERROR; } - // Delete the old object - const char *av[3]; - char *ncpy = bu_strdup(gib->gb->solid_name.c_str()); - av[0] = "kill"; - av[1] = ncpy; - av[2] = NULL; - (void)ged_exec(gib->gb->gedp, 2, (const char **)av); - bu_free(ncpy, "free name cpy"); - // Make the new one struct rt_wdb *wdbp = wdb_dbopen(gib->gb->gedp->dbip, RT_WDB_TYPE_DB_DEFAULT); @@ -705,15 +615,6 @@ _brep_cmd_curve_join(void *bs, int argc, const char **argv) return BRLCAD_ERROR; } - // Delete the old object - const char *av[3]; - char *ncpy = bu_strdup(gib->gb->solid_name.c_str()); - av[0] = "kill"; - av[1] = ncpy; - av[2] = NULL; - (void)ged_exec(gib->gb->gedp, 2, (const char **)av); - bu_free(ncpy, "free name cpy"); - // Make the new one struct rt_wdb *wdbp = wdb_dbopen(gib->gb->gedp->dbip, RT_WDB_TYPE_DB_DEFAULT); diff --git a/src/libged/brep/surface.cpp b/src/libged/brep/surface.cpp index 8a3a01f49c3..06e976b3d5d 100644 --- a/src/libged/brep/surface.cpp +++ b/src/libged/brep/surface.cpp @@ -70,15 +70,6 @@ _brep_cmd_surface_create(void *bs, int argc, const char **argv) } int surfcode = brep_surface_make(b_ip->brep, position); - // Delete the old object - const char *av[3]; - char *ncpy = bu_strdup(gib->gb->solid_name.c_str()); - av[0] = "kill"; - av[1] = ncpy; - av[2] = NULL; - (void)ged_exec(gib->gb->gedp, 2, (const char **)av); - bu_free(ncpy, "free name cpy"); - // Make the new one struct rt_wdb *wdbp = wdb_dbopen(gib->gb->gedp->dbip, RT_WDB_TYPE_DB_DEFAULT); @@ -136,15 +127,6 @@ _brep_cmd_surface_interp(void *bs, int argc, const char **argv) return BRLCAD_ERROR; } - // Delete the old object - const char *av[3]; - char *ncpy = bu_strdup(gib->gb->solid_name.c_str()); - av[0] = "kill"; - av[1] = ncpy; - av[2] = NULL; - (void)ged_exec(gib->gb->gedp, 2, (const char **)av); - bu_free(ncpy, "free name cpy"); - // Make the new one struct rt_wdb *wdbp = wdb_dbopen(gib->gb->gedp->dbip, RT_WDB_TYPE_DB_DEFAULT); @@ -185,15 +167,6 @@ _brep_cmd_surface_copy(void *bs, int argc, const char **argv) return BRLCAD_ERROR; } - // Delete the old object - const char *av[3]; - char *ncpy = bu_strdup(gib->gb->solid_name.c_str()); - av[0] = "kill"; - av[1] = ncpy; - av[2] = NULL; - (void)ged_exec(gib->gb->gedp, 2, (const char **)av); - bu_free(ncpy, "free name cpy"); - // Make the new one struct rt_wdb *wdbp = wdb_dbopen(gib->gb->gedp->dbip, RT_WDB_TYPE_DB_DEFAULT); @@ -229,15 +202,6 @@ _brep_cmd_surface_birail(void *bs, int argc, const char **argv) return BRLCAD_ERROR; } - // Delete the old object - const char *av[3]; - char *ncpy = bu_strdup(gib->gb->solid_name.c_str()); - av[0] = "kill"; - av[1] = ncpy; - av[2] = NULL; - (void)ged_exec(gib->gb->gedp, 2, (const char **)av); - bu_free(ncpy, "free name cpy"); - // Make the new one struct rt_wdb *wdbp = wdb_dbopen(gib->gb->gedp->dbip, RT_WDB_TYPE_DB_DEFAULT); @@ -272,15 +236,6 @@ _brep_cmd_surface_remove(void *bs, int argc, const char **argv) return BRLCAD_ERROR; } - // Delete the old object - const char *av[3]; - char *ncpy = bu_strdup(gib->gb->solid_name.c_str()); - av[0] = "kill"; - av[1] = ncpy; - av[2] = NULL; - (void)ged_exec(gib->gb->gedp, 2, (const char **)av); - bu_free(ncpy, "free name cpy"); - // Make the new one struct rt_wdb *wdbp = wdb_dbopen(gib->gb->gedp->dbip, RT_WDB_TYPE_DB_DEFAULT); @@ -315,15 +270,6 @@ _brep_cmd_surface_move(void *bs, int argc, const char **argv) return BRLCAD_ERROR; } - // Delete the old object - const char *av[3]; - char *ncpy = bu_strdup(gib->gb->solid_name.c_str()); - av[0] = "kill"; - av[1] = ncpy; - av[2] = NULL; - (void)ged_exec(gib->gb->gedp, 2, (const char **)av); - bu_free(ncpy, "free name cpy"); - // Make the new one struct rt_wdb *wdbp = wdb_dbopen(gib->gb->gedp->dbip, RT_WDB_TYPE_DB_DEFAULT); @@ -358,15 +304,6 @@ _brep_cmd_set_cv(void *bs, int argc, const char **argv) return BRLCAD_ERROR; } - // Delete the old object - const char *av[3]; - char *ncpy = bu_strdup(gib->gb->solid_name.c_str()); - av[0] = "kill"; - av[1] = ncpy; - av[2] = NULL; - (void)ged_exec(gib->gb->gedp, 2, (const char **)av); - bu_free(ncpy, "free name cpy"); - // Make the new one struct rt_wdb *wdbp = wdb_dbopen(gib->gb->gedp->dbip, RT_WDB_TYPE_DB_DEFAULT); @@ -403,15 +340,6 @@ _brep_cmd_surface_trim(void *bs, int argc, const char **argv) return BRLCAD_ERROR; } - // Delete the old object - const char *av[3]; - char *ncpy = bu_strdup(gib->gb->solid_name.c_str()); - av[0] = "kill"; - av[1] = ncpy; - av[2] = NULL; - (void)ged_exec(gib->gb->gedp, 2, (const char **)av); - bu_free(ncpy, "free name cpy"); - // Make the new one struct rt_wdb *wdbp = wdb_dbopen(gib->gb->gedp->dbip, RT_WDB_TYPE_DB_DEFAULT); @@ -447,15 +375,6 @@ _brep_cmd_surface_split(void *bs, int argc, const char **argv) return BRLCAD_ERROR; } - // Delete the old object - const char *av[3]; - char *ncpy = bu_strdup(gib->gb->solid_name.c_str()); - av[0] = "kill"; - av[1] = ncpy; - av[2] = NULL; - (void)ged_exec(gib->gb->gedp, 2, (const char **)av); - bu_free(ncpy, "free name cpy"); - // Make the new one struct rt_wdb *wdbp = wdb_dbopen(gib->gb->gedp->dbip, RT_WDB_TYPE_DB_DEFAULT); @@ -490,15 +409,6 @@ _brep_cmd_surface_tensor_product(void *bs, int argc, const char **argv) return BRLCAD_ERROR; } - // Delete the old object - const char *av[3]; - char *ncpy = bu_strdup(gib->gb->solid_name.c_str()); - av[0] = "kill"; - av[1] = ncpy; - av[2] = NULL; - (void)ged_exec(gib->gb->gedp, 2, (const char **)av); - bu_free(ncpy, "free name cpy"); - // Make the new one struct rt_wdb *wdbp = wdb_dbopen(gib->gb->gedp->dbip, RT_WDB_TYPE_DB_DEFAULT); @@ -539,15 +449,6 @@ _brep_cmd_surface_revolution(void *bs, int argc, const char **argv) return BRLCAD_ERROR; } - // Delete the old object - const char *av[3]; - char *ncpy = bu_strdup(gib->gb->solid_name.c_str()); - av[0] = "kill"; - av[1] = ncpy; - av[2] = NULL; - (void)ged_exec(gib->gb->gedp, 2, (const char **)av); - bu_free(ncpy, "free name cpy"); - // Make the new one struct rt_wdb *wdbp = wdb_dbopen(gib->gb->gedp->dbip, RT_WDB_TYPE_DB_DEFAULT); From 188ea6d76663191c13c5857594ffbea92abdfed7 Mon Sep 17 00:00:00 2001 From: GregoryLi0 Date: Fri, 11 Aug 2023 21:29:23 +0800 Subject: [PATCH 17/18] =?UTF-8?q?=F0=9F=90=9E=20fix(libged):=20remove=20'k?= =?UTF-8?q?ill'?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit remove some other 'kill' --- src/libged/brep/brep.cpp | 18 ------------------ src/libged/brep/curve.cpp | 10 ---------- 2 files changed, 28 deletions(-) diff --git a/src/libged/brep/brep.cpp b/src/libged/brep/brep.cpp index 237f0b9ec5a..511602d6123 100644 --- a/src/libged/brep/brep.cpp +++ b/src/libged/brep/brep.cpp @@ -561,15 +561,6 @@ _brep_cmd_flip(void *bs, int argc, const char **argv) b_ip->brep->Flip(); - // Delete the old object - const char *av[3]; - char *ncpy = bu_strdup(gb->solid_name.c_str()); - av[0] = "kill"; - av[1] = ncpy; - av[2] = NULL; - (void)ged_exec(gb->gedp, 2, (const char **)av); - bu_free(ncpy, "free name cpy"); - // Make the new one struct rt_wdb *wdbp = wdb_dbopen(gb->gedp->dbip, RT_WDB_TYPE_DB_DEFAULT); if (mk_brep(wdbp, gb->solid_name.c_str(), (void *)b_ip->brep)) { @@ -1022,15 +1013,6 @@ _brep_cmd_shrink_surfaces(void *bs, int argc, const char **argv) b_ip->brep->ShrinkSurfaces(); - // Delete the old object - const char *av[3]; - char *ncpy = bu_strdup(gb->solid_name.c_str()); - av[0] = "kill"; - av[1] = ncpy; - av[2] = NULL; - (void)ged_exec(gb->gedp, 2, (const char **)av); - bu_free(ncpy, "free name cpy"); - // Make the new one struct rt_wdb *wdbp = wdb_dbopen(gb->gedp->dbip, RT_WDB_TYPE_DB_DEFAULT); if (mk_brep(wdbp, gb->solid_name.c_str(), (void *)b_ip->brep)) { diff --git a/src/libged/brep/curve.cpp b/src/libged/brep/curve.cpp index 74a19354591..d50a419c1c8 100644 --- a/src/libged/brep/curve.cpp +++ b/src/libged/brep/curve.cpp @@ -452,16 +452,6 @@ _brep_cmd_curve_insert_knot(void *bs, int argc, const char **argv) return BRLCAD_ERROR; } - // Delete the old object - - const char *av[3]; - char *ncpy = bu_strdup(gib->gb->solid_name.c_str()); - av[0] = "kill"; - av[1] = ncpy; - av[2] = NULL; - (void)ged_exec(gib->gb->gedp, 2, (const char **)av); - bu_free(ncpy, "free name cpy"); - // Make the new one struct rt_wdb *wdbp = wdb_dbopen(gib->gb->gedp->dbip, RT_WDB_TYPE_DB_DEFAULT); From 5ce3b02ac6a6f7a7fb44fdae367c61dc1d8e767a Mon Sep 17 00:00:00 2001 From: GregoryLi0 Date: Fri, 11 Aug 2023 21:50:03 +0800 Subject: [PATCH 18/18] =?UTF-8?q?=F0=9F=90=9E=20fix(libbrep):=20memory=20m?= =?UTF-8?q?anagement?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/libbrep/edit.cpp | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/libbrep/edit.cpp b/src/libbrep/edit.cpp index e5f721b7f60..aaef5c791d5 100644 --- a/src/libbrep/edit.cpp +++ b/src/libbrep/edit.cpp @@ -441,6 +441,7 @@ int brep_surface_tensor_product(ON_Brep *brep, int curve_id0, int curve_id1) } ON_SumSurface *surface = ON_SumSurface::New(); if(!surface->Create(*curve0, *curve1)) { + delete surface; return -1; } ON_NurbsSurface *nurbs_surface = surface->NurbsSurface(); @@ -474,6 +475,7 @@ int brep_surface_revolution(ON_Brep *brep, int curve_id0, ON_3dPoint line_start, // Get the NURBS form of the surface ON_NurbsSurface *nurbs_surface = ON_NurbsSurface::New(); rev_surf->GetNurbForm(*nurbs_surface, 0.0); + delete rev_surf; return brep->AddSurface(nurbs_surface); }