Skip to content

Commit

Permalink
Copy supporting data to the correct position.
Browse files Browse the repository at this point in the history
To make this properly robust, should really be hashing the path and
filename of the data file in question, then using that for a new data
file name for the copy, and then updating the dsp in the working copy to
point to the hash rather than the filename.
  • Loading branch information
starseeker committed Jan 17, 2024
1 parent 215fc9c commit 57d77c7
Showing 1 changed file with 76 additions and 19 deletions.
95 changes: 76 additions & 19 deletions src/libged/facetize/tri_booleval.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,43 @@
#include "./ged_facetize.h"
#include "./subprocess.h"

void
dsp_data_cpy(struct db_i *dbip, struct rt_dsp_internal *dsp_ip, const char *dirpath)
{
if (!dbip || !dsp_ip || !dirpath)
return;

// Need to look for any local referenced data files and also make copies of
// those into wdir. An example is the terra.bin file from the example dsp
// - that is a local file reference, so the cache copy needs its own local
// version of the referenced file in the proper relative position.
if (dsp_ip->dsp_datasrc == RT_DSP_SRC_V4_FILE || dsp_ip->dsp_datasrc == RT_DSP_SRC_FILE) {
char * const *pathp = dbip->dbi_filepath;
struct bu_vls dpath = BU_VLS_INIT_ZERO;
for (; *pathp != NULL; pathp++) {
bu_vls_strcpy(&dpath , *pathp);
bu_vls_putc(&dpath, '/');
bu_vls_strcat(&dpath, bu_vls_cstr(&dsp_ip->dsp_name));
if (bu_file_exists(bu_vls_cstr(&dpath), NULL))
break;
}
if (!bu_vls_strlen(&dpath))
return;
char wpath[MAXPATHLEN];
bu_dir(wpath, MAXPATHLEN, dirpath, bu_vls_cstr(&dsp_ip->dsp_name), NULL);
std::ifstream orig_file(bu_vls_cstr(&dpath), std::ios::binary);
std::ofstream work_file(wpath, std::ios::binary);
if (!orig_file.is_open() || !work_file.is_open()) {
bu_vls_free(&dpath);
return;
}
work_file << orig_file.rdbuf();
orig_file.close();
work_file.close();
bu_vls_free(&dpath);
}
}

// Translate flags to ged_tessellate opts. These need to match the options
// used by that executable to specify algorithms.
static const char *
Expand Down Expand Up @@ -92,6 +129,8 @@ method_opt(int *method_flags)
return NULL;
}

// TODO - investigate Merge function in Manifold -
// https://manifoldcad.org/docs/html/structmanifold_1_1_mesh_g_l.html
static int
bot_to_manifold(void **out, struct db_tree_state *tsp, struct rt_db_internal *ip, int flip)
{
Expand Down Expand Up @@ -255,8 +294,8 @@ facetize_region_end(struct db_tree_state *tsp,

static int
manifold_do_bool(
union tree *tp, union tree *tl, union tree *tr,
int op, struct bu_list *UNUSED(vlfree), const struct bn_tol *UNUSED(tol), void *data)
union tree *tp, union tree *tl, union tree *tr,
int op, struct bu_list *UNUSED(vlfree), const struct bn_tol *UNUSED(tol), void *data)
{
struct _ged_facetize_state *s = (struct _ged_facetize_state *)data;
if (!s)
Expand Down Expand Up @@ -342,9 +381,9 @@ manifold_do_bool(
#if 0
manifold::Mesh lmesh = lm->GetMesh();
Mesh -> NMG
manifold::Mesh rmesh = rm->GetMesh();
manifold::Mesh rmesh = rm->GetMesh();
Mesh -> NMG
int nmg_op = NMG_BOOL_ADD;
int nmg_op = NMG_BOOL_ADD;
switch (op) {
case OP_UNION:
nmg_op = NMG_BOOL_ADD;
Expand Down Expand Up @@ -560,26 +599,25 @@ _ged_facetize_booleval(struct _ged_facetize_state *s, int argc, struct directory
// Get the root filename, so we can give the working file a relatable name
bu_path_component(&bname, rfname, BU_PATH_BASENAME);

// Hash the path to the .g file
bu_path_component(&dname, rfname, BU_PATH_DIRNAME);
unsigned long long hash_num = bu_data_hash((void *)bu_vls_cstr(&dname), bu_vls_strlen(&dname));
bu_vls_free(&dname);

// Avoid collisions between .g files with the same name in different directories by
// using both the basename and the path hash for the working filename
bu_vls_sprintf(&wfilename, "%llu_tess_%s", hash_num, bu_vls_cstr(&bname));
// Hash the path string and construct
unsigned long long hash_num = bu_data_hash((void *)bu_vls_cstr(&bname), bu_vls_strlen(&bname));
bu_vls_sprintf(&dname, "facetize_%llu", hash_num);
bu_vls_sprintf(&wfilename, "facetize_%s", bu_vls_cstr(&bname));
bu_vls_free(&bname);

// Have filename, get a location in the cache directory
char wfile[MAXPATHLEN];
bu_dir(wfile, MAXPATHLEN, BU_DIR_CACHE, bu_vls_cstr(&wfilename), NULL);
char wdir[MAXPATHLEN], wfile[MAXPATHLEN];
bu_dir(wdir, MAXPATHLEN, BU_DIR_CACHE, bu_vls_cstr(&dname), NULL);
bu_dir(wfile, MAXPATHLEN, BU_DIR_CACHE, bu_vls_cstr(&dname), bu_vls_cstr(&wfilename), NULL);
bu_vls_free(&dname);
bu_vls_free(&wfilename);

/* If we're resuming and we already have an appropriately named .g file, we
* don't overwrite it. Otherwise we need to prepare the working copy. */
if (!s->resume || (s->resume && !bu_file_exists(wfile, NULL))) {
// If we need to clear an old working copy, do it now
bu_file_delete(wfile);
bu_dirclear(wdir);
bu_mkdir(wdir);

// Populate the working copy with original .g data
// (TODO - should use the dbip's FILE pointer for this rather than
Expand All @@ -593,6 +631,24 @@ _ged_facetize_booleval(struct _ged_facetize_state *s, int argc, struct directory
work_file << orig_file.rdbuf();
orig_file.close();
work_file.close();

// Must also copy any files referenced by the .g into the proper
// relative position to the working .g copy.
for (size_t i = 0; i < BU_PTBL_LEN(&leaf_dps); i++) {
struct directory *ldp = (struct directory *)BU_PTBL_GET(&leaf_dps, i);
if (ldp->d_major_type != DB5_MAJORTYPE_BRLCAD)
continue;

if (ldp->d_minor_type == ID_DSP) {
struct rt_db_internal intern;
if (rt_db_get_internal(&intern, ldp, gedp->dbip, NULL, &rt_uniresource) < 0)
continue;
dsp_data_cpy(gedp->dbip, (struct rt_dsp_internal *)intern.idb_ptr, wdir);
rt_db_free_internal(&intern);
}

// TODO - There may be other such cases...
}
}

// Sort dp objects by d_len using a priority queue
Expand All @@ -616,6 +672,7 @@ _ged_facetize_booleval(struct _ged_facetize_state *s, int argc, struct directory

if (pq.empty() && q_dsp.empty()) {
bu_log("No viable objects for tessellation found.\n");
bu_dirclear(wdir);
return BRLCAD_OK;
}

Expand Down Expand Up @@ -710,7 +767,7 @@ _ged_facetize_booleval(struct _ged_facetize_state *s, int argc, struct directory
// materially contribute to the final object shape, but for some
// applications like visualization there are cases where "something is
// better than nothing" applies
bu_file_delete(wfile);
bu_dirclear(wdir);
return BRLCAD_ERROR;
}
}
Expand Down Expand Up @@ -752,7 +809,7 @@ _ged_facetize_booleval(struct _ged_facetize_state *s, int argc, struct directory
// materially contribute to the final object shape, but for some
// applications like visualization there are cases where "something is
// better than nothing" applies
bu_file_delete(wfile);
bu_dirclear(wdir);
return BRLCAD_ERROR;
}
}
Expand All @@ -761,7 +818,7 @@ _ged_facetize_booleval(struct _ged_facetize_state *s, int argc, struct directory
// the tree walk to set up Manifold data.
struct db_i *wdbip = db_open(wfile, DB_OPEN_READONLY);
if (!wdbip) {
bu_file_delete(wfile);
bu_dirclear(wdir);
return BRLCAD_ERROR;
}
if (db_dirbuild(wdbip) < 0) {
Expand Down Expand Up @@ -869,7 +926,7 @@ _ged_facetize_booleval(struct _ged_facetize_state *s, int argc, struct directory

tri_booleval_cleanup:
db_close(wdbip);
bu_file_delete(wfile);
bu_dirclear(wdir);
return ret;
}

Expand Down

0 comments on commit 57d77c7

Please sign in to comment.