52 #include <osgUtil/Optimizer>
53 #include <osgDB/ReadFile>
55 #include <osgViewer/Viewer>
56 #include <osgViewer/CompositeViewer>
57 #include <osgViewer/ViewerEventHandlers>
59 #include <osgGA/TrackballManipulator>
60 #include <osgGA/StateSetManipulator>
63 #include <osg/Material>
65 #include <osg/BlendFunc>
67 #include <osg/PolygonOffset>
68 #include <osg/MatrixTransform>
70 #include <osg/RenderInfo>
71 #include <osg/LineStipple>
73 #include <osgDB/WriteFile>
75 #include <osgText/Text>
86 bu_exit(1,
"ERROR: Unable to get internal representation of %s\n", dp->
d_namep);
91 if (
rt_obj_plot(plot_segments, &intern, &rttol, &tol) < 0) {
92 bu_exit(1,
"ERROR: Unable to get plot segment list from %s\n", dp->
d_namep);
101 std::vector<int>startIndexes;
102 std::vector<int>stopIndexes;
103 osg::Vec3Array *vertArray = (osg::Vec3Array *)geom->getVertexArray();
107 osg::ref_ptr<osg::Vec3Array> new_vertArray =
new osg::Vec3Array();
108 geom->setVertexArray(new_vertArray);
109 vertArray = (osg::Vec3Array *)geom->getVertexArray();
111 int currentVertex = vertArray->getNumElements();
113 geom->setUseDisplayList(
true);
120 point_t *
pt = vp->
pt;
122 for (j=0 ; j <
nused ; j++, cmd++, pt++, currentVertex++)
130 if (startIndexes.size() > 0)
133 stopIndexes.push_back(currentVertex-1);
136 startIndexes.push_back(currentVertex);
139 vertArray->push_back(osg::Vec3((
float)((*pt)[
X]), (
float)((*pt)[
Y]),(
float)(*pt)[
Z]));
146 vertArray->push_back(osg::Vec3((
float)((*pt)[X]), (
float)((*pt)[Y]),(
float)(*pt)[Z]));
154 if (startIndexes.size() > 0)
155 stopIndexes.push_back(--currentVertex);
158 for (
unsigned j=0 ; j < startIndexes.size() ; j++) {
159 geom->addPrimitiveSet(
new osg::DrawArrays(osg::PrimitiveSet::LINE_STRIP, startIndexes[j], (stopIndexes[j]-startIndexes[j])+1 ));
164 osg::ref_ptr<osg::Group>
166 std::map<
const struct directory *, osg::ref_ptr<osg::Group> > *osg_nodes,
169 osg::ref_ptr<osg::Group> wrapper;
170 if ((*osg_nodes).find(dp) == (*osg_nodes).end()) {
171 wrapper =
new osg::Group;
172 (*osg_nodes)[dp] = wrapper.get();
174 wrapper = (*osg_nodes)[dp];
178 unsigned int children_cnt = wrapper->getNumChildren();
179 wrapper->removeChildren(0, children_cnt);
182 osg::ref_ptr<osg::Geode> geode =
new osg::Geode;
183 osg::ref_ptr<osg::Geometry> geom =
new osg::Geometry;
187 geode->addDrawable(geom);
188 osg::StateSet* state = geode->getOrCreateStateSet();
189 state->setMode(GL_LIGHTING, osg::StateAttribute::OFF);
190 wrapper->addChild(geode);
209 const char *solid_search =
"! -type comb";
213 for (
int i = (
int)
BU_PTBL_LEN(&solids) - 1; i >= 0; i--) {
216 if ((*osg_nodes).find(curr_dp) == (*osg_nodes).end()) {
217 osg::ref_ptr<osg::Group> node =
solid_node(osg_nodes, curr_dp, dbip);
218 (*osg_nodes)[curr_dp] = node.get();
225 osg::ref_ptr<osg::Group>
227 std::map<
const struct directory *, osg::ref_ptr<osg::Group> > *osg_nodes,
230 osg::ref_ptr<osg::Group> comb;
231 if ((*osg_nodes).find(dp) == (*osg_nodes).end()) {
232 comb =
new osg::Group;
233 (*osg_nodes)[dp] = comb.get();
235 comb = (*osg_nodes)[dp];
244 float color[3] = {-1.0, -1.0, -1.0};
245 if (comb_internal->rgb_valid) {
246 VSCALE(color, comb_internal->rgb, (1.0/255.0) );
248 struct mater *mp = MATER_NULL;
250 if (comb_internal->region_id <= mp->mt_high && comb_internal->region_id >= mp->mt_low) {
251 color[0] = mp->mt_r/255.0;
252 color[1] = mp->mt_g/255.0;
253 color[2] = mp->mt_b/255.0;
259 osg::ref_ptr<osg::Material> mtl =
new osg::Material;
260 mtl->setDiffuse(osg::Material::FRONT_AND_BACK, osg::Vec4(color[0], color[1], color[2], 1.0f));
261 osg::StateSet* state = comb->getOrCreateStateSet();
262 if (comb_internal->inherit || comb_internal->region_flag) {
263 state->setAttributeAndModes(mtl.get(), osg::StateAttribute::OVERRIDE|osg::StateAttribute::ON);
265 state->setAttributeAndModes(mtl.get(), osg::StateAttribute::ON);
277 for (
unsigned int i = 0; i < path->
fp_len; i++) {
278 if(path->
fp_bool[i] == 4) ret = 1;
286 mat_t m = MAT_INIT_IDN;
287 mat_t m_idn = MAT_INIT_IDN;
291 mat_t mt = MAT_INIT_IDN;
292 MAT_TRANSPOSE(mt, m);
293 osg::Matrixd osgMat(mt);
294 osg::ref_ptr<osg::MatrixTransform> osgmt =
new osg::MatrixTransform(osgMat);
295 osgmt->addChild(child);
296 comb->addChild(osgmt);
300 osg::StateSet* state = osgmt->getOrCreateStateSet();
301 osg::ref_ptr<osg::LineStipple> ls =
new osg::LineStipple();
302 ls->setPattern(0xCF33);
304 state->setAttributeAndModes(ls.get());
308 osg::ref_ptr<osg::Group> property_wrapper =
new osg::Group;
310 osg::StateSet* state = property_wrapper->getOrCreateStateSet();
311 osg::ref_ptr<osg::LineStipple> ls =
new osg::LineStipple();
312 ls->setPattern(0xCF33);
314 state->setAttributeAndModes(ls.get());
315 property_wrapper->addChild(child);
316 comb->addChild(property_wrapper);
318 comb->addChild(child);
326 osg::ref_ptr<osg::Group>
328 std::map<
const struct directory *, osg::ref_ptr<osg::Group> > *osg_nodes,
331 osg::ref_ptr<osg::Group> comb =
bare_comb_node(osg_nodes, dp, dbip);
334 unsigned int children_cnt = comb->getNumChildren();
335 comb->removeChildren(0, children_cnt);
337 const char *comb_search =
"-type comb";
340 for (
int i = (
int)
BU_PTBL_LEN(&combs) - 1; i >= 0; i--) {
342 osg::ref_ptr<osg::Group> sub_comb =
bare_comb_node(osg_nodes, curr_dp, dbip);
343 const char *comb_children_search =
"-mindepth 1 -maxdepth 1";
346 for (
int j = (
int)
BU_PTBL_LEN(&comb_children) - 1; j >= 0; j--) {
350 osg::ref_ptr<osg::Group> new_node =
bare_comb_node(osg_nodes, curr_child_dp, dbip);
351 (*osg_nodes)[curr_child_dp] = new_node.get();
354 osg::ref_ptr<osg::Group> new_node =
solid_node(osg_nodes, curr_child_dp, dbip);
355 (*osg_nodes)[curr_child_dp] = new_node.get();
357 add_comb_child(sub_comb, (*osg_nodes)[curr_child_dp], curr_path, dbip);
366 osg::ref_ptr<osg::Group>
368 std::map<
const struct directory *, osg::ref_ptr<osg::Group> > *osg_nodes,
374 unsigned int children_cnt = region->getNumChildren();
375 region->removeChildren(0, children_cnt);
379 osg::ref_ptr<osg::Geode> geode =
new osg::Geode;
380 osg::ref_ptr<osg::Geometry> geom =
new osg::Geometry;
383 geode->addDrawable(geom);
384 osg::StateSet* state = geode->getOrCreateStateSet();
385 state->setMode(GL_LIGHTING, osg::StateAttribute::OFF);
386 region->addChild(geode);
389 osg::ref_ptr<osg::Group> subtraction_wrapper =
new osg::Group;
390 osg::ref_ptr<osg::Geode> subtraction_geode =
new osg::Geode;
391 osg::ref_ptr<osg::Geometry> subtraction_geom =
new osg::Geometry;
392 subtraction_wrapper->setName(dp->
d_namep);
393 subtraction_geode->setName(dp->
d_namep);
394 subtraction_geom->setName(dp->
d_namep);
395 osg::StateSet* subtraction_geode_state = subtraction_geode->getOrCreateStateSet();
396 subtraction_geode_state->setMode(GL_LIGHTING, osg::StateAttribute::OFF);
397 osg::StateSet* subtraction_state = subtraction_wrapper->getOrCreateStateSet();
398 osg::ref_ptr<osg::LineStipple> ls =
new osg::LineStipple();
399 ls->setPattern(0xCF33);
401 subtraction_state->setAttributeAndModes(ls.get());
402 subtraction_geode->addDrawable(subtraction_geom);
403 subtraction_wrapper->addChild(subtraction_geode);
408 const char *region_vlist_search =
"! -type comb";
411 int have_subtraction = 0;
412 for (
int j = (
int)
BU_PTBL_LEN(®ion_vlist_contributors) - 1; j >= 0; j--) {
425 if (subtraction_path) {
438 if (have_subtraction) {
439 region->addChild(subtraction_wrapper);
449 osg::ref_ptr<osg::Group>
451 std::map<
const struct directory *, osg::ref_ptr<osg::Group> > *osg_nodes,
454 osg::ref_ptr<osg::Group> assembly =
bare_comb_node(osg_nodes, dp, dbip);
457 unsigned int children_cnt = assembly->getNumChildren();
458 assembly->removeChildren(0, children_cnt);
460 const char *assembly_search =
"-type comb ! -below -type region ! -type region";
463 for (
int i = (
int)
BU_PTBL_LEN(&assemblies) - 1; i >= 0; i--) {
465 osg::ref_ptr<osg::Group> sub_comb =
bare_comb_node(osg_nodes, curr_dp, dbip);
466 const char *comb_children_search =
"-mindepth 1 -maxdepth 1";
469 for (
int j = (
int)
BU_PTBL_LEN(&comb_children) - 1; j >= 0; j--) {
474 for (
int k = (
int)
BU_PTBL_LEN(&assemblies) - 1; k >= 0; k--) {
476 if (assembly_dp == curr_child_dp) is_assembly = 1;
478 osg::ref_ptr<osg::Group> new_node;
487 osg::ref_ptr<osg::Group> new_node =
full_region_node(osg_nodes, curr_child_dp, dbip);
488 (*osg_nodes)[curr_child_dp] = new_node.get();
491 osg::ref_ptr<osg::Group> new_node =
solid_node(osg_nodes, curr_child_dp, dbip);
492 (*osg_nodes)[curr_child_dp] = new_node.get();
494 add_comb_child(sub_comb, (*osg_nodes)[curr_child_dp], curr_path, dbip);
516 const char *is_assembly_search =
"-type region";
538 std::map<
const struct directory *, osg::ref_ptr<osg::Group> > *osg_nodes,
561 std::map<
const struct directory *, osg::ref_ptr<osg::Group> > *osg_nodes,
572 while (working_path.
fp_len > 1) {
576 osg::ref_ptr<osg::Group> comb =
bare_comb_node(osg_nodes, parent_dp, dbip);
577 osg::ref_ptr<osg::Group> child = (*osg_nodes)[curr_dp];
588 int main(
int argc,
char **argv )
590 std::map<const struct directory *, osg::ref_ptr<osg::Group> > osg_nodes;
595 if (argc != 3 || !argv) {
596 bu_exit(1,
"Error - please specify a .g file and an object\n");
599 bu_exit(1,
"Cannot stat file %s\n", argv[1]);
603 bu_exit(1,
"Cannot open geometry database file %s\n", argv[1]);
608 bu_exit(1,
"db_dirbuild failed on geometry database file %s\n", argv[1]);
613 bu_exit(1,
"ERROR: Unable to find object %s in %s\n", argv[2], argv[1]);
622 osgViewer::Viewer viewer;
623 viewer.setUpViewInWindow(0, 0, 640, 480);
626 viewer.addEventHandler(
new osgGA::StateSetManipulator(viewer.getCamera()->getOrCreateStateSet()) );
629 viewer.addEventHandler(
new osgViewer::ThreadingHandler);
632 viewer.addEventHandler(
new osgViewer::WindowSizeHandler);
635 viewer.addEventHandler(
new osgViewer::StatsHandler);
642 osgUtil::Optimizer optimizer;
643 osg::ref_ptr<osg::Group> toplevel =
new osg::Group;
645 optimizer.optimize(toplevel);
646 viewer.setSceneData(toplevel);
char * d_namep
pointer to name string
#define BU_LIST_FOR(p, structure, hp)
size_t nused
elements 0..nused active
int rt_db_get_internal(struct rt_db_internal *ip, const struct directory *dp, const struct db_i *dbip, const mat_t mat, struct resource *resp)
void db_search_free(struct bu_ptbl *search_results)
#define BU_PTBL_INIT_ZERO
void db_dup_full_path(struct db_full_path *newp, const struct db_full_path *oldp)
int cmd[BN_VLIST_CHUNK]
VL_CMD_*.
struct bu_list rtg_vlfree
head of bn_vlist freelist
#define DB_SEARCH_RETURN_UNIQ_DP
Return the set of unique directory pointers instead of full paths.
Header file for the BRL-CAD common definitions.
#define DB_FULL_PATH_CUR_DIR(_pp)
#define RT_DIR_REGION
region
#define RT_FREE_VLIST(hd)
osg::ref_ptr< osg::Group > full_region_node(std::map< const struct directory *, osg::ref_ptr< osg::Group > > *osg_nodes, struct directory *dp, struct db_i *dbip)
void db_full_path_init(struct db_full_path *pathp)
int subtracted_solid(struct db_full_path *path)
#define DB_SEARCH_TREE
Do a hierarchy-aware search. This is the default.
#define BN_VLIST_POLY_MOVE
move to first poly vertex
int rt_obj_plot(struct bu_list *vhead, struct rt_db_internal *ip, const struct rt_tess_tol *ttol, const struct bn_tol *tol)
int bn_mat_is_equal(const mat_t a, const mat_t b, const struct bn_tol *tol)
struct bu_list * obj_vlist(const struct directory *dp, const struct db_i *dbip, mat_t mat)
int * fp_bool
array of boolean flags
struct resource rt_uniresource
default. Defined in librt/globals.c
void bu_exit(int status, const char *fmt,...) _BU_ATTR_NORETURN _BU_ATTR_PRINTF23
void add_vlist_to_geom(osg::Geometry *geom, struct bu_list *plot_segments)
#define RT_DIR_SOLID
this name is a solid
int db_string_to_path(struct db_full_path *pp, const struct db_i *dbip, const char *str)
#define BU_PTBL_GET(ptbl, i)
#define RT_DB_INTERNAL_INIT(_p)
#define BN_VLIST_POLY_START
pt[] has surface normal
#define BN_VLIST_LINE_MOVE
void db_free_full_path(struct db_full_path *pp)
#define BU_GET(_ptr, _type)
osg::ref_ptr< osg::Group > full_comb_node(std::map< const struct directory *, osg::ref_ptr< osg::Group > > *osg_nodes, struct directory *dp, struct db_i *dbip)
#define BN_VLIST_LINE_DRAW
void db_close(struct db_i *dbip)
#define BN_VLIST_POLY_DRAW
subsequent poly vertex
#define BU_PUT(_ptr, _type)
Support for uniform tolerances.
struct directory ** fp_names
array of dir pointers
osg::ref_ptr< osg::Group > bare_comb_node(std::map< const struct directory *, osg::ref_ptr< osg::Group > > *osg_nodes, const struct directory *dp, const struct db_i *dbip)
#define BU_PTBL_LEN(ptbl)
struct mater * rt_material_head(void)
#define BU_LIST_INIT(_hp)
#define RT_DIR_COMB
combination
#define DB_SEARCH_QUIET
Silence all warnings.
osg::ref_ptr< osg::Group > solid_node(std::map< const struct directory *, osg::ref_ptr< osg::Group > > *osg_nodes, const struct directory *dp, const struct db_i *dbip)
void add_comb_child(osg::Group *comb, osg::Group *child, struct db_full_path *child_path, struct db_i *dbip)
#define BN_VLIST_POLY_END
last vert (repeats 1st), draw poly
struct db_i * db_open(const char *name, const char *mode)
#define DB_FULL_PATH_ROOT_DIR(_pp)
#define RT_TESS_TOL_MAGIC
int db_dirbuild(struct db_i *dbip)
void add_dp_to_scene(std::map< const struct directory *, osg::ref_ptr< osg::Group > > *osg_nodes, struct directory *dp, struct db_i *dbip)
int add_path_to_scene(std::map< const struct directory *, osg::ref_ptr< osg::Group > > *osg_nodes, const struct db_full_path *path, struct db_i *dbip)
void create_solid_nodes(std::map< const struct directory *, osg::ref_ptr< osg::Group > > *osg_nodes, struct directory *dp, struct db_i *dbip)
point_t pt[BN_VLIST_CHUNK]
associated 3-point/vect
int bu_file_exists(const char *path, int *fd)
osg::ref_ptr< osg::Group > full_assembly_node(std::map< const struct directory *, osg::ref_ptr< osg::Group > > *osg_nodes, struct directory *dp, struct db_i *dbip)
int characterize_dp(struct directory *dp, struct db_i *dbip)
void rt_db_free_internal(struct rt_db_internal *ip)
int main(int argc, char **argv)
#define DB_FULL_PATH_POP(_pp)
int db_path_to_mat(struct db_i *dbip, struct db_full_path *pathp, mat_t mat, int depth, struct resource *resp)
#define BN_VLIST_POINT_DRAW
Draw a single point.
int db_search(struct bu_ptbl *results, int flags, const char *filter, int path_c, struct directory **path_v, struct db_i *dbip)
Search for objects in a geometry database using filters.