Revision: 76830
http://sourceforge.net/p/brlcad/code/76830
Author: starseeker
Date: 2020-08-18 15:57:34 +0000 (Tue, 18 Aug 2020)
Log Message:
-----------
Add the point inside/outside test to cg
This supports two operations - intersection and subtraction - between
a point cloud on the left and a volumetric object on the right. It
will find the subset of the points in the left hand object that are
either inside and on (i.e. intersecting) or outside the test volume.
Command form is:
cg eval "pnts.s + sph.s" inside.pnts
cg eval "pnts.s - sph.s" outside.pnts
Modified Paths:
--------------
brlcad/trunk/src/libged/cg/CMakeLists.txt
brlcad/trunk/src/libged/cg/cg.cpp
Added Paths:
-----------
brlcad/trunk/src/libged/cg/eval_pnts.cpp
brlcad/trunk/src/libged/cg/ged_cg.h
Modified: brlcad/trunk/src/libged/cg/CMakeLists.txt
===================================================================
--- brlcad/trunk/src/libged/cg/CMakeLists.txt 2020-08-18 14:43:01 UTC (rev
76829)
+++ brlcad/trunk/src/libged/cg/CMakeLists.txt 2020-08-18 15:57:34 UTC (rev
76830)
@@ -6,8 +6,13 @@
${GED_INCLUDE_DIRS}
)
+set(cg_srcs
+ cg.cpp
+ eval_pnts.cpp
+ )
+
add_definitions(-DGED_PLUGIN)
-ged_plugin_library(ged-cg SHARED cg.cpp)
+ged_plugin_library(ged-cg SHARED ${cg_srcs})
target_link_libraries(ged-cg libged libbu)
set_property(TARGET ged-cg APPEND PROPERTY COMPILE_DEFINITIONS BRLCADBUILD
HAVE_CONFIG_H)
VALIDATE_STYLE(ged-cg cg.cpp)
@@ -14,8 +19,9 @@
PLUGIN_SETUP(ged-cg ged)
CMAKEFILES(
+ ${cg_srcs}
CMakeLists.txt
- cg.cpp
+ ged_cg.h
)
# Local Variables:
Modified: brlcad/trunk/src/libged/cg/cg.cpp
===================================================================
--- brlcad/trunk/src/libged/cg/cg.cpp 2020-08-18 14:43:01 UTC (rev 76829)
+++ brlcad/trunk/src/libged/cg/cg.cpp 2020-08-18 15:57:34 UTC (rev 76830)
@@ -37,6 +37,7 @@
#include "bu/cmd.h"
#include "bu/opt.h"
#include "../ged_private.h"
+#include "./ged_cg.h"
}
#define HELPFLAG "--print-help"
@@ -65,7 +66,99 @@
return 0;
}
+static int
+_cg_eval(struct ged *gedp, const char *expr, const char *outname)
+{
+ if (!gedp || !expr || !outname)
+ return GED_ERROR;
+ if (!strlen(expr)) {
+ bu_vls_printf(gedp->ged_result_str, "Empty expression supplied for
evaluation\n");
+ return GED_ERROR;
+ }
+ if (!strlen(outname)) {
+ bu_vls_printf(gedp->ged_result_str, "Empty string supplied for output
name\n");
+ return GED_ERROR;
+ }
+ // Eventually we need more sophisticated parsing - for a first cut, just
chop into argv
+ // and assume an infix expression with one op and two objects
+ struct bu_vls expr_vls = BU_VLS_INIT_ZERO;
+ bu_vls_sprintf(&expr_vls, "%s", expr);
+ char **eav = (char **)bu_calloc(bu_vls_strlen(&expr_vls), sizeof(char *),
"expression argv");
+ int eac = bu_argv_from_string(eav, bu_vls_strlen(&expr_vls),
bu_vls_addr(&expr_vls));
+
+ if (eac != 3) {
+ bu_free(eav, "argv array");
+ bu_vls_free(&expr_vls);
+ bu_vls_printf(gedp->ged_result_str, "TODO: currently unsupported
expression \"%s\"\n", expr);
+ return GED_ERROR;
+ }
+
+ db_op_t op = db_str2op(eav[1]);
+ if (op == DB_OP_NULL || op == DB_OP_UNION) {
+ bu_free(eav, "argv array");
+ bu_vls_free(&expr_vls);
+ bu_vls_printf(gedp->ged_result_str, "TODO: currently unsupported
expression \"%s\"\n", expr);
+ return GED_ERROR;
+ }
+
+ struct directory *ldp = db_lookup(gedp->ged_wdbp->dbip, eav[0],
LOOKUP_QUIET);
+ if (ldp == RT_DIR_NULL) {
+ bu_free(eav, "argv array");
+ bu_vls_free(&expr_vls);
+ bu_vls_printf(gedp->ged_result_str, "could not find object \"%s\"\n",
eav[0]);
+ return GED_ERROR;
+ }
+
+ struct directory *rdp = db_lookup(gedp->ged_wdbp->dbip, eav[2],
LOOKUP_QUIET);
+ if (rdp == RT_DIR_NULL) {
+ bu_free(eav, "argv array");
+ bu_vls_free(&expr_vls);
+ bu_vls_printf(gedp->ged_result_str, "could not find object \"%s\"\n",
eav[2]);
+ return GED_ERROR;
+ }
+
+ struct rt_db_internal tpnts_intern;
+ GED_DB_GET_INTERNAL(gedp, &tpnts_intern, ldp, bn_mat_identity,
&rt_uniresource, GED_ERROR);
+ struct rt_db_internal v_intern;
+ GED_DB_GET_INTERNAL(gedp, &v_intern, rdp, bn_mat_identity,
&rt_uniresource, GED_ERROR);
+
+ if (tpnts_intern.idb_major_type != DB5_MAJORTYPE_BRLCAD ||
v_intern.idb_major_type != DB5_MAJORTYPE_BRLCAD) {
+ bu_vls_printf(gedp->ged_result_str, "One or more unsupported objects
specified in expression");
+ bu_free(eav, "argv array");
+ bu_vls_free(&expr_vls);
+ rt_db_free_internal(&tpnts_intern);
+ return GED_ERROR;
+ }
+
+ /* Currently the lefthand object must be a pnts object */
+ if (tpnts_intern.idb_minor_type != DB5_MINORTYPE_BRLCAD_PNTS) {
+ bu_vls_printf(gedp->ged_result_str, "TODO: left hand object %s is not a
pnts object, currently unsupported", eav[0]);
+ bu_free(eav, "argv array");
+ bu_vls_free(&expr_vls);
+ rt_db_free_internal(&tpnts_intern);
+ return GED_ERROR;
+ }
+
+ /* For the righthand object, we need to raytrace it */
+ if (v_intern.idb_minor_type == DB5_MINORTYPE_BRLCAD_PNTS) {
+ bu_vls_printf(gedp->ged_result_str, "TODO: right hand object %s is a
pnts object, currently unsupported", eav[2]);
+ bu_free(eav, "argv array");
+ bu_vls_free(&expr_vls);
+ rt_db_free_internal(&tpnts_intern);
+ return GED_ERROR;
+ }
+
+ int ret = pnts_eval_op(gedp, op, &tpnts_intern, eav[2], outname);
+
+ bu_free(eav, "argv array");
+ bu_vls_free(&expr_vls);
+ rt_db_free_internal(&tpnts_intern);
+
+ return ret;
+
+}
+
extern "C" int
_cg_cmd_eval(void *bs, int argc, const char **argv)
{
@@ -75,13 +168,19 @@
return GED_OK;
}
-#if 0
struct _ged_cg_info *gc = (struct _ged_cg_info *)bs;
+
+ argc--; argv++;
+ if (argc != 2) {
+ bu_vls_printf(gc->gedp->ged_result_str, "%s\n", usage_string);
+ return GED_ERROR;
+ }
+
+ // Make sure the second argument doesn't exist as a database object already
struct ged *gedp = gc->gedp;
+ GED_CHECK_EXISTS(gedp, argv[1], LOOKUP_QUIET, GED_ERROR);
- argc--; argv++;
-#endif
- return GED_OK;
+ return _cg_eval(gedp, argv[0], argv[1]);
}
extern "C" int
Added: brlcad/trunk/src/libged/cg/eval_pnts.cpp
===================================================================
--- brlcad/trunk/src/libged/cg/eval_pnts.cpp (rev 0)
+++ brlcad/trunk/src/libged/cg/eval_pnts.cpp 2020-08-18 15:57:34 UTC (rev
76830)
@@ -0,0 +1,224 @@
+/* E V A L _ P N T S . C P P
+ * BRL-CAD
+ *
+ * Copyright (c) 2020 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 eval_pnts.cpp
+ *
+ * Brief description
+ *
+ */
+
+#include "common.h"
+
+extern "C" {
+#include <stdlib.h>
+#include <ctype.h>
+#include <string.h>
+}
+
+extern "C" {
+#include "rt/geom.h"
+#include "analyze.h"
+#include "../ged_private.h"
+#include "../pnts_util.h"
+#include "./ged_cg.h"
+}
+
+extern "C" int
+pnts_eval_op(
+ struct ged *gedp,
+ db_op_t op,
+ struct rt_db_internal *tpnts_intern,
+ const char *vol_obj,
+ const char *output_pnts_obj
+ )
+{
+ int ret = GED_OK;
+ struct pnt *pstd, *pstdl = NULL;
+ struct pnt_color *pc, *pcl = NULL;
+ struct pnt_scale *ps, *psl = NULL;
+ struct pnt_normal *pn, *pnl = NULL;
+ struct pnt_color_scale *pcs, *pcsl = NULL;
+ struct pnt_color_normal *pcn, *pcnl = NULL;
+ struct pnt_scale_normal *psn, *psnl = NULL;
+ struct pnt_color_scale_normal *pcsn, *pcsnl = NULL;
+ void *npnt = NULL;
+ int iostat = 0;
+ struct rt_pnts_internal *opnts = NULL;
+ struct rt_db_internal internal;
+ long pntcnt = 0;
+ struct rt_pnts_internal *pnts = (struct rt_pnts_internal
*)tpnts_intern->idb_ptr;
+ RT_PNTS_CK_MAGIC(pnts);
+
+ /* For the vol object, we need to raytrace it */
+ struct application *ap;
+ struct resource *resp;
+ struct rt_i *rtip;
+ size_t ncpus;
+ BU_GET(ap, struct application);
+ RT_APPLICATION_INIT(ap);
+ BU_GET(resp, struct resource);
+ rtip = rt_new_rti(gedp->ged_wdbp->dbip);
+ rt_init_resource(resp, 0, rtip);
+ ap->a_rt_i = rtip;
+ ap->a_resource = resp;
+ ap->a_onehit = 1;
+ ap->a_hit = NULL;
+ ap->a_miss = NULL;
+ ap->a_overlap = NULL;
+ ap->a_logoverlap = rt_silent_logoverlap;
+ if ((rt_gettree(rtip, vol_obj) < 0)) {
+ ret = GED_ERROR;
+ goto pnts_internal_memfree;
+ }
+ ncpus = bu_avail_cpus();
+ rt_prep_parallel(rtip, ncpus);
+
+ /* For the output, make a new pnts object */
+ RT_DB_INTERNAL_INIT(&internal);
+ internal.idb_major_type = DB5_MAJORTYPE_BRLCAD;
+ internal.idb_type = ID_PNTS;
+ internal.idb_meth = &OBJ[ID_PNTS];
+ BU_ALLOC(internal.idb_ptr, struct rt_pnts_internal);
+ opnts = (struct rt_pnts_internal *) internal.idb_ptr;
+ opnts->magic = RT_PNTS_INTERNAL_MAGIC;
+ opnts->scale = pnts->scale;
+ opnts->type = pnts->type;
+ opnts->point = _ged_pnts_new_pnt(pnts->type);
+ _ged_pnts_init_head_pnt(opnts);
+
+ switch(pnts->type) {
+ case RT_PNT_TYPE_PNT:
+ pstdl = (struct pnt *)pnts->point;
+ for (BU_LIST_FOR(pstd, pnt, &(pstdl->l))) {
+ iostat = analyze_pnt_in_vol(&(pstd->v), ap, 1);
+ if ((op == DB_OP_INTERSECT && iostat == -1) || (op ==
DB_OP_SUBTRACT && iostat != -1)) {
+ npnt = _ged_pnts_dup((void *)pstd, RT_PNT_TYPE_PNT);
+ _ged_pnts_add(opnts, npnt);
+ pntcnt++;
+ }
+ }
+ break;
+ case RT_PNT_TYPE_COL:
+ pcl = (struct pnt_color *)pnts->point;
+ for (BU_LIST_FOR(pc, pnt_color, &(pcl->l))) {
+ iostat = analyze_pnt_in_vol(&(pc->v), ap, 1);
+ if ((op == DB_OP_INTERSECT && iostat == -1) || (op ==
DB_OP_SUBTRACT && iostat != -1)) {
+ npnt = _ged_pnts_dup((void *)pc, RT_PNT_TYPE_COL);
+ _ged_pnts_add(opnts, npnt);
+ pntcnt++;
+ }
+ }
+ break;
+ case RT_PNT_TYPE_SCA:
+ psl = (struct pnt_scale *)pnts->point;
+ for (BU_LIST_FOR(ps, pnt_scale, &(psl->l))) {
+ iostat = analyze_pnt_in_vol(&(ps->v), ap, 1);
+ if ((op == DB_OP_INTERSECT && iostat == -1) || (op ==
DB_OP_SUBTRACT && iostat != -1)) {
+ npnt = _ged_pnts_dup((void *)ps, RT_PNT_TYPE_SCA);
+ _ged_pnts_add(opnts, npnt);
+ pntcnt++;
+ }
+ }
+ break;
+ case RT_PNT_TYPE_NRM:
+ pnl = (struct pnt_normal *)pnts->point;
+ for (BU_LIST_FOR(pn, pnt_normal, &(pnl->l))) {
+ iostat = analyze_pnt_in_vol(&(pn->v), ap, 1);
+ if ((op == DB_OP_INTERSECT && iostat == -1) || (op ==
DB_OP_SUBTRACT && iostat != -1)) {
+ npnt = _ged_pnts_dup((void *)pn, RT_PNT_TYPE_NRM);
+ _ged_pnts_add(opnts, npnt);
+ pntcnt++;
+ }
+ }
+ break;
+ case RT_PNT_TYPE_COL_SCA:
+ pcsl = (struct pnt_color_scale *)pnts->point;
+ for (BU_LIST_FOR(pcs, pnt_color_scale, &(pcsl->l))) {
+ iostat = analyze_pnt_in_vol(&(pcs->v), ap, 1);
+ if ((op == DB_OP_INTERSECT && iostat == -1) || (op ==
DB_OP_SUBTRACT && iostat != -1)) {
+ npnt = _ged_pnts_dup((void *)pcs, RT_PNT_TYPE_COL_SCA);
+ _ged_pnts_add(opnts, npnt);
+ pntcnt++;
+ }
+ }
+ break;
+ case RT_PNT_TYPE_COL_NRM:
+ pcnl = (struct pnt_color_normal *)pnts->point;
+ for (BU_LIST_FOR(pcn, pnt_color_normal, &(pcnl->l))) {
+ iostat = analyze_pnt_in_vol(&(pcn->v), ap, 1);
+ if ((op == DB_OP_INTERSECT && iostat == -1) || (op ==
DB_OP_SUBTRACT && iostat != -1)) {
+ npnt = _ged_pnts_dup((void *)pcn, RT_PNT_TYPE_COL_NRM);
+ _ged_pnts_add(opnts, npnt);
+ pntcnt++;
+ }
+ }
+ break;
+ case RT_PNT_TYPE_SCA_NRM:
+ psnl = (struct pnt_scale_normal *)pnts->point;
+ for (BU_LIST_FOR(psn, pnt_scale_normal, &(psnl->l))) {
+ iostat = analyze_pnt_in_vol(&(psn->v), ap, 1);
+ if ((op == DB_OP_INTERSECT && iostat == -1) || (op ==
DB_OP_SUBTRACT && iostat != -1)) {
+ npnt = _ged_pnts_dup((void *)psn, RT_PNT_TYPE_SCA_NRM);
+ _ged_pnts_add(opnts, npnt);
+ pntcnt++;
+ }
+ }
+ break;
+ case RT_PNT_TYPE_COL_SCA_NRM:
+ pcsnl = (struct pnt_color_scale_normal *)pnts->point;
+ for (BU_LIST_FOR(pcsn, pnt_color_scale_normal, &(pcsnl->l))) {
+ iostat = analyze_pnt_in_vol(&(pcsn->v), ap, 1);
+ if ((op == DB_OP_INTERSECT && iostat == -1) || (op ==
DB_OP_SUBTRACT && iostat != -1)) {
+ npnt = _ged_pnts_dup((void *)pcsn, RT_PNT_TYPE_COL_SCA_NRM);
+ _ged_pnts_add(opnts, npnt);
+ pntcnt++;
+ }
+ }
+ break;
+ default:
+ bu_vls_printf(gedp->ged_result_str, "Unknown point type in object,
aborting\n");
+ ret = GED_ERROR;
+ goto pnts_internal_memfree;
+ };
+
+ opnts->count = pntcnt;
+ struct directory *dp;
+ GED_DB_DIRADD(gedp, dp, output_pnts_obj, RT_DIR_PHONY_ADDR, 0,
RT_DIR_SOLID, (void *)&internal.idb_type, GED_ERROR);
+ GED_DB_PUT_INTERNAL(gedp, dp, &internal, &rt_uniresource, GED_ERROR);
+
+ bu_vls_printf(gedp->ged_result_str, "Generated pnts object %s with %ld
matching points", output_pnts_obj, pntcnt);
+
+pnts_internal_memfree:
+ rt_clean_resource(rtip, resp);
+ rt_free_rti(rtip);
+ BU_PUT(resp, struct resource);
+ BU_PUT(ap, struct appliation);
+
+ return ret;
+}
+
+
+// Local Variables:
+// tab-width: 8
+// mode: C++
+// c-basic-offset: 4
+// indent-tabs-mode: t
+// c-file-style: "stroustrup"
+// End:
+// ex: shiftwidth=4 tabstop=8
Property changes on: brlcad/trunk/src/libged/cg/eval_pnts.cpp
___________________________________________________________________
Added: svn:eol-style
## -0,0 +1 ##
+native
\ No newline at end of property
Added: svn:mime-type
## -0,0 +1 ##
+text/plain
\ No newline at end of property
Added: brlcad/trunk/src/libged/cg/ged_cg.h
===================================================================
--- brlcad/trunk/src/libged/cg/ged_cg.h (rev 0)
+++ brlcad/trunk/src/libged/cg/ged_cg.h 2020-08-18 15:57:34 UTC (rev 76830)
@@ -0,0 +1,59 @@
+/* G E D _ C G . H
+ * BRL-CAD
+ *
+ * Copyright (c) 2020 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 ged_cg.h
+ *
+ * Brief description
+ *
+ */
+
+#ifndef LIBGED_BREP_GED_PRIVATE_H
+#define LIBGED_BREP_GED_PRIVATE_H
+
+#include "common.h"
+
+#include "./rt/db_internal.h"
+#include "./rt/op.h"
+#include "./ged/defines.h"
+
+__BEGIN_DECLS
+
+GED_EXPORT extern int
+pnts_eval_op(
+ struct ged *gedp,
+ db_op_t op,
+ struct rt_db_internal *tpnts_intern,
+ const char *vol_obj,
+ const char *output_pnts_obj
+ );
+
+__END_DECLS
+
+#endif /* LIBGED_BREP_GED_PRIVATE_H */
+
+/** @} */
+/*
+ * Local Variables:
+ * mode: C
+ * tab-width: 8
+ * indent-tabs-mode: t
+ * c-file-style: "stroustrup"
+ * End:
+ * ex: shiftwidth=4 tabstop=8
+ */
Property changes on: brlcad/trunk/src/libged/cg/ged_cg.h
___________________________________________________________________
Added: svn:eol-style
## -0,0 +1 ##
+native
\ No newline at end of property
Added: svn:mime-type
## -0,0 +1 ##
+text/plain
\ No newline at end of property
This was sent by the SourceForge.net collaborative development platform, the
world's largest Open Source development site.
_______________________________________________
BRL-CAD Source Commits mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/brlcad-commits