Skip to content

Commit 3d9c856

Browse files
committed
add gmi_cap_probe and gmi_cap_load_some
- gmi_cap.h: add gmi_cap_probe and gmi_cap_load_some functions. - wrap headers and functions requiring C++ into #ifdef __cplusplus. - remove using namespace declarations. - add import with gmodel. - gmi_cap.cc: add using namespace CreateMG. - add Geometry namespace to enums (i.e. VERTEX, EDGE, FACE, REGION). - add gmodel to struct cap_model. - use reinterpret_cast instead of C-style casts. - (gmi_cap_start): handle initialization of static capstonemodule. - (gmi_cap_stop): destruct static capstone module. - (create_cre): uncomment function. - (gmi_cap_probe): add short overload to just get mesh names. - add long overload to get model and mesh contents. - (gmi_register_cap): register create_cre for gmi_load. - (owned_import): uncomment function. - (gmi_cap_load): call gmi_cap_load_some with all mesh names. - (gmi_cap_load_some): add new function to only load specific mesh names. - (gmi_import_cap): add overload to use current geometry. - replace PCU_ALWAYS_ASSERT with if statement and gmi_fail. - test/capLoadSome.cc: add utility to test gmi_cap_load_some. - test/capProbe.cc: add utility to test gmi_cap_probe. - test/CMakeLists.txt: add capLoadSome and capProbe. Signed-off-by: Aiden Woodruff <[email protected]>
1 parent 7dfb75d commit 3d9c856

File tree

5 files changed

+266
-93
lines changed

5 files changed

+266
-93
lines changed

gmi_cap/gmi_cap.cc

Lines changed: 172 additions & 80 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,43 @@
88
99
*******************************************************************************/
1010
#include "gmi_cap.h"
11+
1112
#include <stdlib.h>
12-
#include <gmi.h>
13+
#include <memory>
1314
#include <vector>
15+
1416
#include <pcu_util.h>
17+
#include <gmi.h>
1518
#include <lionPrint.h>
1619

20+
#include <CapstoneModule.h>
21+
#include <CreateMG_AppProcessor.h>
22+
#include <CreateMG_Function.h>
23+
#include <CreateMG_Reader.h>
24+
25+
using namespace CreateMG;
26+
27+
static std::unique_ptr<CapstoneModule> cs_module;
28+
29+
void gmi_cap_start(void) {
30+
if (!cs_module) {
31+
cs_module.reset(new CapstoneModule(
32+
"SCOREC/gmi_cap",
33+
"Geometry Database : SMLIB",
34+
"Mesh Database : Create",
35+
"Attribution Database : Create"
36+
));
37+
PCU_ALWAYS_ASSERT(cs_module->get_context());
38+
PCU_ALWAYS_ASSERT(cs_module->get_geometry());
39+
PCU_ALWAYS_ASSERT(cs_module->get_mesh());
40+
}
41+
}
42+
43+
void gmi_cap_stop(void) {
44+
if (!cs_module)
45+
gmi_fail("gmi_cap_stop called before gmi_cap_start");
46+
cs_module.reset();
47+
}
1748

1849
gmi_ent* toGmiEntity(M_GTopo topo)
1950
{
@@ -30,10 +61,10 @@ M_GTopo fromGmiEntity(gmi_ent* g)
3061
return topo;
3162
}
3263

33-
3464
struct cap_model {
3565
struct gmi_model model;
36-
GeometryDatabaseInterface* geomInterface;
66+
GDBI* geomInterface;
67+
M_GModel gmodel;
3768
bool owned;
3869
};
3970

@@ -42,17 +73,17 @@ static gmi_iter* begin(gmi_model* m, int dim)
4273
cap_model* cm = (cap_model*)m;
4374
M_GBRep brep;
4475
cm->geomInterface->get_brep_by_index(0, brep);
45-
GeometrySmartIterator* giter = new GeometrySmartIterator(cm->geomInterface);
76+
auto giter = new Geometry::GeometrySmartIterator(cm->geomInterface);
4677
if (dim == 0)
47-
cm->geomInterface->get_topo_iterator(brep, VERTEX, *giter);
78+
cm->geomInterface->get_topo_iterator(brep, Geometry::VERTEX, *giter);
4879
if (dim == 1)
49-
cm->geomInterface->get_topo_iterator(brep, EDGE, *giter);
80+
cm->geomInterface->get_topo_iterator(brep, Geometry::EDGE, *giter);
5081
if (dim == 2)
51-
cm->geomInterface->get_topo_iterator(brep, FACE, *giter);
82+
cm->geomInterface->get_topo_iterator(brep, Geometry::FACE, *giter);
5283
if (dim == 3)
53-
cm->geomInterface->get_topo_iterator(brep, REGION, *giter);
84+
cm->geomInterface->get_topo_iterator(brep, Geometry::REGION, *giter);
5485
cm->geomInterface->iterator_begin(*giter);
55-
return (gmi_iter*)(giter);
86+
return reinterpret_cast<gmi_iter*>(giter);
5687
}
5788

5889
/* NOTE: giter is located at the first item in the list, therefore
@@ -61,7 +92,7 @@ static gmi_iter* begin(gmi_model* m, int dim)
6192
static gmi_ent* next(gmi_model*m, gmi_iter* i)
6293
{
6394
cap_model* cm = (cap_model*)m;
64-
GeometrySmartIterator* giter = (GeometrySmartIterator*)i;
95+
auto giter = reinterpret_cast<Geometry::GeometrySmartIterator*>(i);
6596

6697
M_GTopo topo = cm->geomInterface->iterator_value(*giter);
6798

@@ -73,10 +104,8 @@ static gmi_ent* next(gmi_model*m, gmi_iter* i)
73104
return toGmiEntity(topo);
74105
}
75106

76-
static void end(gmi_model*, gmi_iter* i)
77-
{
78-
GeometrySmartIterator* giter = (GeometrySmartIterator*)i;
79-
delete giter;
107+
static void end(gmi_model*, gmi_iter* i) {
108+
delete reinterpret_cast<Geometry::GeometrySmartIterator*>(i);
80109
}
81110

82111
static int get_dim(gmi_model* m, gmi_ent* e)
@@ -108,13 +137,13 @@ static gmi_ent* find(gmi_model* m, int dim, int tag)
108137
cap_model* cm = (cap_model*)m;
109138
M_GTopo topo;
110139
if (dim == 0)
111-
topo = cm->geomInterface->get_topo_by_id(VERTEX, tag);
140+
topo = cm->geomInterface->get_topo_by_id(Geometry::VERTEX, tag);
112141
else if (dim == 1)
113-
topo = cm->geomInterface->get_topo_by_id(EDGE, tag);
142+
topo = cm->geomInterface->get_topo_by_id(Geometry::EDGE, tag);
114143
else if (dim == 2)
115-
topo = cm->geomInterface->get_topo_by_id(FACE, tag);
144+
topo = cm->geomInterface->get_topo_by_id(Geometry::FACE, tag);
116145
else if (dim == 3)
117-
topo = cm->geomInterface->get_topo_by_id(REGION, tag);
146+
topo = cm->geomInterface->get_topo_by_id(Geometry::REGION, tag);
118147
else
119148
gmi_fail("input dim is out of range.");
120149
return toGmiEntity(topo);
@@ -136,21 +165,21 @@ static gmi_set* adjacent(gmi_model* m, gmi_ent* e, int dim)
136165
int edim = gmi_dim(m, e);
137166
std::vector<M_GTopo> gtopos;
138167
if (edim == 0 && dim == 1)
139-
cm->geomInterface->get_adjacency(gtopo, EDGE, gtopos);
168+
cm->geomInterface->get_adjacency(gtopo, Geometry::EDGE, gtopos);
140169
else if (edim == 1 && dim == 0)
141-
cm->geomInterface->get_adjacency(gtopo, VERTEX, gtopos);
170+
cm->geomInterface->get_adjacency(gtopo, Geometry::VERTEX, gtopos);
142171
else if (edim == 1 && dim == 2)
143-
cm->geomInterface->get_adjacency(gtopo, FACE, gtopos);
172+
cm->geomInterface->get_adjacency(gtopo, Geometry::FACE, gtopos);
144173
else if (edim == 2 && dim == 0)
145-
cm->geomInterface->get_adjacency(gtopo, VERTEX, gtopos);
174+
cm->geomInterface->get_adjacency(gtopo, Geometry::VERTEX, gtopos);
146175
else if (edim == 2 && dim == 1)
147-
cm->geomInterface->get_adjacency(gtopo, EDGE, gtopos);
176+
cm->geomInterface->get_adjacency(gtopo, Geometry::EDGE, gtopos);
148177
else if (edim == 2 && dim == 3)
149-
cm->geomInterface->get_adjacency(gtopo, REGION, gtopos);
178+
cm->geomInterface->get_adjacency(gtopo, Geometry::REGION, gtopos);
150179
else if (edim == 1 && dim == 3)
151-
cm->geomInterface->get_adjacency(gtopo, REGION, gtopos);
180+
cm->geomInterface->get_adjacency(gtopo, Geometry::REGION, gtopos);
152181
else if (edim == 3 && dim == 2)
153-
cm->geomInterface->get_adjacency(gtopo, FACE, gtopos);
182+
cm->geomInterface->get_adjacency(gtopo, Geometry::FACE, gtopos);
154183
else
155184
gmi_fail("requested adjacency not available\n");
156185
return vector_to_set(gtopos);
@@ -285,7 +314,7 @@ static int is_in_closure_of(struct gmi_model* m, struct gmi_ent* e,
285314
cap_model* cm = (cap_model*)m;
286315
M_GTopo ce = fromGmiEntity(e);
287316
M_GTopo cet = fromGmiEntity(et);
288-
GeometryTopo ce_type;
317+
Geometry::GeometryTopo ce_type;
289318
MG_API_CALL(cm->geomInterface, get_topo_type(ce, ce_type));
290319
std::vector<M_GTopo> adj;
291320
MG_API_CALL(cm->geomInterface, get_adjacency(cet, ce_type, adj));
@@ -325,28 +354,77 @@ static int is_discrete_ent(struct gmi_model*, struct gmi_ent* e)
325354
return 0;
326355
}
327356

328-
static void destroy(gmi_model* m)
329-
{
357+
static void destroy(gmi_model* m) {
358+
lion_oprint(1, "TRACE: gmi_cap destroy called\n");
330359
cap_model* cm = (cap_model*)m;
360+
if (cm->owned) {
361+
if (!cs_module) gmi_fail(
362+
"gmi_cap destroy: called before gmi_cap_start or after gmi_cap_stop"
363+
);
364+
auto ctx = cm->geomInterface->get_context();
365+
FunctionPtr fn = get_function(ctx, "DeleteGeometryModel");
366+
set_input(fn, "Model", cm->gmodel);
367+
auto proc = get_context_processor(ctx);
368+
if (proc->execute(fn) != STATUS_OK)
369+
gmi_fail("gmi_cap destroy: failed to delete Capstone geometry model");
370+
}
331371
free(cm);
332372
}
333373

334-
/* static gmi_model* create_cre(const char* filename) */
335-
/* { */
336-
/* return gmi_cap_load(filename); */
337-
/* } */
338-
339-
static struct gmi_model_ops ops;
340-
374+
static gmi_model* create_cre(const char* filename) {
375+
return gmi_cap_load(filename);
376+
}
341377

342-
void gmi_cap_start(void)
343-
{
378+
void gmi_cap_probe(
379+
const char* creFileName, std::string& model_content,
380+
std::vector<std::string>& mesh_names, std::vector<std::string>& mesh_contents
381+
) {
382+
model_content.clear();
383+
mesh_names.clear();
384+
mesh_contents.clear();
385+
Reader *reader = get_reader(
386+
cs_module->get_context(), "Create Native Reader"
387+
);
388+
DataFileInfo info;
389+
reader->probe(creFileName, info);
390+
for (std::size_t i = 0; i < info.get_num_sections(); i++) {
391+
std::string secType, secName;
392+
info.get_section(i, secType, secName);
393+
if (secType == "mmodel") mesh_names.push_back(secName);
394+
v_string infoNames, infoValues;
395+
info.get_section_info(i, infoNames, infoValues);
396+
PCU_DEBUG_ASSERT(infoNames.size() == infoValues.size());
397+
for (std::size_t j = 0; j < infoNames.size(); ++j) {
398+
if (secType == "gmodel" && infoNames[j] == "content") {
399+
PCU_DEBUG_ASSERT(model_content.empty()); // should only be one gmodel
400+
model_content = infoValues[j];
401+
} else if (secType == "mmodel" && infoNames[j] == "content") {
402+
mesh_contents.push_back(infoValues[j]);
403+
}
404+
}
405+
}
406+
PCU_DEBUG_ASSERT(!model_content.empty());
407+
PCU_DEBUG_ASSERT(mesh_names.size() == mesh_contents.size());
344408
}
345409

346-
void gmi_cap_stop(void)
347-
{
410+
void gmi_cap_probe(
411+
const char* creFileName, std::vector<std::string>& mesh_names
412+
) {
413+
mesh_names.clear();
414+
Reader *reader = get_reader(
415+
cs_module->get_context(), "Create Native Reader"
416+
);
417+
DataFileInfo info;
418+
reader->probe(creFileName, info);
419+
for (std::size_t i = 0; i < info.get_num_sections(); i++) {
420+
std::string secType, secName;
421+
info.get_section(i, secType, secName);
422+
if (secType == "mmodel") mesh_names.push_back(secName);
423+
}
348424
}
349425

426+
static struct gmi_model_ops ops;
427+
350428
void gmi_register_cap(void)
351429
{
352430
ops.begin = begin;
@@ -368,70 +446,84 @@ void gmi_register_cap(void)
368446
ops.bbox = bbox;
369447
ops.is_discrete_ent = is_discrete_ent;
370448
ops.destroy = destroy;
371-
/* gmi_register(create_cre, "cre"); */
449+
gmi_register(create_cre, "cre");
372450
/* gmi_register(create_native, "xmt_txt"); */
373451
/* gmi_register(create_native, "x_t"); */
374452
/* gmi_register(create_native, "sat"); */
375453
}
376454

377-
/* static gmi_model* owned_import(GeometryDatabaseInterface* gi) */
378-
/* { */
379-
/* gmi_model* m = gmi_import_cap(gi); */
380-
/* ((cap_model*)m)->owned = true; */
381-
/* return m; */
382-
/* } */
383-
384-
/* struct gmi_model* gmi_cap_load(const char* creFileName) */
385-
/* { */
386-
/* if (!gmi_has_ext(creFileName, "cre")) */
387-
/* gmi_fail("gmi_cap_load: cre file must have .cre extension"); */
388-
/* const std::string gdbName("Geometry Database : SMLIB");// Switch Create with SMLIB for CAD */
389-
/* const std::string mdbName("Mesh Database : Create"); */
390-
/* const std::string adbName("Attribution Database : Create"); */
391-
392-
/* CapstoneModule cs("test", gdbName.c_str(), mdbName.c_str(), adbName.c_str()); */
393-
394-
/* GeometryDatabaseInterface *g = cs.get_geometry(); */
395-
/* MeshDatabaseInterface *m = cs.get_mesh(); */
396-
/* AppContext *c = cs.get_context(); */
397-
398-
/* PCU_ALWAYS_ASSERT(g); */
399-
/* PCU_ALWAYS_ASSERT(m); */
400-
/* PCU_ALWAYS_ASSERT(c); */
455+
static gmi_model* owned_import(GDBI* g, M_GModel gmodel) {
456+
cap_model* m = reinterpret_cast<cap_model*>(gmi_import_cap(g, gmodel));
457+
m->owned = true;
458+
return reinterpret_cast<gmi_model*>(m);
459+
}
401460

402-
/* v_string filenames; */
403-
/* filenames.push_back(creFileName); */
461+
struct gmi_model* gmi_cap_load(const char* creFileName) {
462+
if (!gmi_has_ext(creFileName, "cre"))
463+
gmi_fail("gmi_cap_load: cre file must have .cre extension");
464+
if (!cs_module) gmi_fail("gmi_cap_load called before gmi_cap_start");
404465

405-
/* M_GModel gmodel = cs.load_files(filenames); */
466+
std::vector<std::string> mesh_names;
467+
gmi_cap_probe(creFileName, mesh_names);
468+
return gmi_cap_load_some(creFileName, mesh_names);
469+
}
406470

407-
/* int numBreps; */
408-
/* g->get_num_breps(numBreps); */
409-
/* PCU_ALWAYS_ASSERT(numBreps == 1); */
471+
struct gmi_model* gmi_cap_load_some(
472+
const char* creFileName, const std::vector<std::string>& mesh_names
473+
) {
474+
if (!gmi_has_ext(creFileName, "cre"))
475+
gmi_fail("gmi_cap_load_some: CRE file must have .cre extension");
476+
if (!cs_module) gmi_fail("gmi_cap_load_some: called before gmi_cap_start");
477+
static bool called = false;
478+
if (!called) called = true;
479+
else {
480+
lion_eprint(1,
481+
"WARNING: gmi_cap_load_some called more than once. gmi_cap operations"
482+
" may fail.\n"
483+
);
484+
}
485+
auto ctx = cs_module->get_context();
486+
FunctionPtr fn(get_function(ctx, "LoadCreateData"));
487+
set_input(fn, "FileName", creFileName);
488+
set_input(fn, "Meshes", mesh_names);
489+
auto proc = get_context_processor(ctx);
490+
if (proc->execute(fn) != STATUS_OK)
491+
gmi_fail("gmi_cap_load_some: failed to read CRE file");
492+
M_GModel gmodel;
493+
get_output(fn, "Model", gmodel);
494+
MG_API_CALL(cs_module->get_geometry(), set_current_model(gmodel));
495+
return owned_import(cs_module->get_geometry(), gmodel);
496+
}
410497

411-
/* return owned_import(g); */
412-
/* } */
498+
gmi_model* gmi_import_cap(GDBI* gi) {
499+
M_GModel gmodel;
500+
MG_API_CALL(gi, get_current_model(gmodel));
501+
return gmi_import_cap(gi, gmodel);
502+
}
413503

414-
gmi_model* gmi_import_cap(GeometryDatabaseInterface* gi)
504+
gmi_model* gmi_import_cap(GDBI* gi, M_GModel gmodel)
415505
{
416506
cap_model* m;
417507
m = (cap_model*)malloc(sizeof(*m));
418508
m->model.ops = &ops;
419509
m->geomInterface = gi;
510+
m->gmodel = gmodel;
420511
M_GBRep brep;
421512
int numBreps;
422513

423514
m->geomInterface->get_num_breps(numBreps);
424-
PCU_ALWAYS_ASSERT(numBreps == 1);
515+
if (numBreps != 1)
516+
gmi_fail("gmi_import_cap: loaded CRE with numBreps != 1");
425517
m->geomInterface->get_brep_by_index(0, brep);
426-
m->geomInterface->get_num_topos(brep, VERTEX, m->model.n[0]);
427-
m->geomInterface->get_num_topos(brep, EDGE, m->model.n[1]);
428-
m->geomInterface->get_num_topos(brep, FACE, m->model.n[2]);
429-
m->geomInterface->get_num_topos(brep, REGION, m->model.n[3]);
518+
m->geomInterface->get_num_topos(brep, Geometry::VERTEX, m->model.n[0]);
519+
m->geomInterface->get_num_topos(brep, Geometry::EDGE, m->model.n[1]);
520+
m->geomInterface->get_num_topos(brep, Geometry::FACE, m->model.n[2]);
521+
m->geomInterface->get_num_topos(brep, Geometry::REGION, m->model.n[3]);
430522
m->owned = false;
431523
return &m->model;
432524
}
433525

434-
GeometryDatabaseInterface* gmi_export_cap(gmi_model* m)
526+
GDBI* gmi_export_cap(gmi_model* m)
435527
{
436528
cap_model* cm = (cap_model*)m;
437529
return cm->geomInterface;

0 commit comments

Comments
 (0)