示例#1
0
/**
 * Creates one metaball object that includes all points from an
 * existing list (in 'av') of named metaballs.  This routine creates
 * an rt_metaball_internal object directly via LIBRT given it requires
 * reading existing metaballs from the database.
 */
static void
mix_balls(struct db_i *dbip, const char *name, int ac, const char *av[])
{
    int i;
    struct directory *dp;
    struct rt_metaball_internal *newmp;

    RT_CK_DBI(dbip);

    /* allocate a struct rt_metaball_internal object that we'll
     * manually fill in with points from the other metaballs being
     * joined together.
     */
    BU_ALLOC(newmp, struct rt_metaball_internal);
    newmp->magic = RT_METABALL_INTERNAL_MAGIC;
    newmp->threshold = 1.0;
    newmp->method = 1;
    BU_LIST_INIT(&newmp->metaball_ctrl_head);

    bu_log("Combining together the following metaballs:\n");

    for (i = 0; i < ac; i++) {
	struct rt_db_internal dir;
	struct rt_metaball_internal *mp;
	struct wdb_metaballpt *mpt;

	/* get a handle on the existing database object */
	bu_log("\t%s\n", av[i]);
	dp = db_lookup(dbip, av[i], 1);
	if (!dp) {
	    bu_log("Unable to find %s\n", av[i]);
	    continue;
	}

	/* load the existing database object */
	if (rt_db_get_internal(&dir, dp, dbip, NULL, &rt_uniresource) < 0) {
	    bu_log("Unable to load %s\n", av[i]);
	    continue;
	}

	/* access the metaball-specific internal structure */
	mp = (struct rt_metaball_internal *)dir.idb_ptr;
	RT_METABALL_CK_MAGIC(mp);

	/* iterate over each point in that database object and add it
	 * to our new metaball.
	 */
	for (BU_LIST_FOR(mpt, wdb_metaballpt, &mp->metaball_ctrl_head)) {
	    bu_log("Adding point (%lf %lf %lf)\n", V3ARGS(mpt->coord));
	    rt_metaball_add_point(newmp, (const point_t *)&mpt->coord, mpt->fldstr, mpt->sweat);
	}
    }

    bu_log("Joining balls together and creating [%s] object\n", name);

    /* write out new "mega metaball" out to disk */
    dbip->dbi_wdbp = wdb_dbopen(dbip, RT_WDB_TYPE_DB_DISK);
    wdb_export(dbip->dbi_wdbp, name, newmp, ID_METABALL, 1.0);
}
示例#2
0
////////////////////////////////////////////////////////////////////////////////
// Read the BRL-CAD file
////////////////////////////////////////////////////////////////////////////////
BRLFile::BRLFile(char *filename, int v)
{
    verbose=v;

    dbip = db_open(filename, "r");
    if(dbip==DBI_NULL)
        return;

    /* build a wdbp structure for convenient read/write */
    wdbp = wdb_dbopen(dbip, RT_WDB_TYPE_DB_DISK);
    if (db_dirbuild(dbip) < 0) {        /* create directory database contents */
        bu_log("Error building directory for %s\n", filename);
        exit(-1);
    }
}
示例#3
0
文件: keep.c 项目: kanzure/brlcad
int
ged_keep(struct ged *gedp, int argc, const char *argv[])
{
    int i;
    struct keep_node_data knd;
    struct rt_wdb *keepfp;
    struct directory *dp;
    struct bu_vls title = BU_VLS_INIT_ZERO;
    struct db_i *new_dbip;
    const char *cmd = argv[0];
    static const char *usage = "[-R] file object(s)";

    int c;
    int flag_R = 0;

    GED_CHECK_DATABASE_OPEN(gedp, GED_ERROR);
    GED_CHECK_ARGC_GT_0(gedp, argc, GED_ERROR);

    /* initialize result */
    bu_vls_trunc(gedp->ged_result_str, 0);

    /* must be wanting help */
    if (argc == 1) {
	bu_vls_printf(gedp->ged_result_str, "Usage: %s %s", cmd, usage);
	return GED_HELP;
    }

    /* check for options */
    bu_optind = 1;
    while ((c = bu_getopt(argc, (char * const *)argv, "R")) != -1) {
	switch (c) {
	    case 'R':
		/* not recursively */
		flag_R = 1;
		break;
	    default:
		bu_vls_printf(gedp->ged_result_str, "Unrecognized option - %c", c);
		return GED_ERROR;
	}
    }
    /* skip options processed plus command name */
    argc -= bu_optind;
    argv += bu_optind;

    if (argc < 2) {
	bu_vls_printf(gedp->ged_result_str, "ERROR: missing file or object names\n");
	bu_vls_printf(gedp->ged_result_str, "Usage: %s %s", cmd, usage);
	return GED_ERROR;
    }

    /* First, clear any existing counts */
    for (i = 0; i < RT_DBNHASH; i++) {
	for (dp = gedp->ged_wdbp->dbip->dbi_Head[i]; dp != RT_DIR_NULL; dp = dp->d_forw)
	    dp->d_nref = 0;
    }

    /* Alert user if named file already exists */

    new_dbip = db_open(argv[0], DB_OPEN_READWRITE);

    if (new_dbip != DBI_NULL) {
	if (db_version(new_dbip) != db_version(gedp->ged_wdbp->dbip)) {
	    bu_vls_printf(gedp->ged_result_str, "%s: File format mismatch between '%s' and '%s'\n",
			  cmd, argv[0], gedp->ged_wdbp->dbip->dbi_filename);
	    return GED_ERROR;
	}

	if ((keepfp = wdb_dbopen(new_dbip, RT_WDB_TYPE_DB_DISK)) == NULL) {
	    bu_vls_printf(gedp->ged_result_str, "%s:  Error opening '%s'\n", cmd, argv[0]);
	    return GED_ERROR;
	} else {
	    bu_vls_printf(gedp->ged_result_str, "%s:  Appending to '%s'\n", cmd, argv[0]);

	    /* --- Scan geometry database and build in-memory directory --- */
	    db_dirbuild(new_dbip);
	}
    } else {
	/* Create a new database */
	keepfp = wdb_fopen_v(argv[0], db_version(gedp->ged_wdbp->dbip));

	if (keepfp == NULL) {
	    perror(argv[0]);
	    return GED_ERROR;
	}
    }

    knd.wdbp = keepfp;
    knd.gedp = gedp;

    /* ident record */
    if (bu_strncmp(gedp->ged_wdbp->dbip->dbi_title, "Parts of: ", 10) != 0) {
	bu_vls_strcat(&title, "Parts of: ");
    }
    bu_vls_strcat(&title, gedp->ged_wdbp->dbip->dbi_title);

    if (db_update_ident(keepfp->dbip, bu_vls_addr(&title), gedp->ged_wdbp->dbip->dbi_local2base) < 0) {
	perror("fwrite");
	bu_vls_printf(gedp->ged_result_str, "db_update_ident() failed\n");
	wdb_close(keepfp);
	bu_vls_free(&title);
	return GED_ERROR;
    }
    bu_vls_free(&title);

    for (i = 1; i < argc; i++) {
	if ((dp = db_lookup(gedp->ged_wdbp->dbip, argv[i], LOOKUP_NOISY)) == RT_DIR_NULL)
	    continue;

	if (!flag_R) {
	    /* recursively keep objects */
	    db_functree(gedp->ged_wdbp->dbip, dp, node_write, node_write, &rt_uniresource, (void *)&knd);
	} else {
	    /* keep just this object */
	    node_write(gedp->ged_wdbp->dbip, dp, (void *)&knd);
	}
    }

    wdb_close(keepfp);
    return GED_OK;
}
int
main(int argc, char *argv[])
{
    struct db_i *dbip;
    struct directory *dp;
    struct rt_db_internal intern;
    struct rt_bot_internal *bot_ip = NULL;
    struct rt_wdb *wdbp;
    struct bu_vls name;
    struct bu_vls bname;
    struct Mesh_Info *prev_mesh = NULL;
    struct Mesh_Info *mesh = NULL;

    bu_vls_init(&name);

    if (argc != 3) {
	bu_exit(1, "Usage: %s file.g object", argv[0]);
    }

    dbip = db_open(argv[1], DB_OPEN_READWRITE);
    if (dbip == DBI_NULL) {
	bu_exit(1, "ERROR: Unable to read from geometry database file %s\n", argv[1]);
    }

    if (db_dirbuild(dbip) < 0)
	bu_exit(1, "ERROR: Unable to read from %s\n", argv[1]);

    dp = db_lookup(dbip, argv[2], LOOKUP_QUIET);
    if (dp == RT_DIR_NULL) {
	bu_exit(1, "ERROR: Unable to look up object %s\n", argv[2]);
    }

    RT_DB_INTERNAL_INIT(&intern)
	if (rt_db_get_internal(&intern, dp, dbip, NULL, &rt_uniresource) < 0) {
	    bu_exit(1, "ERROR: Unable to get internal representation of %s\n", argv[2]);
	}

    if (intern.idb_minor_type != DB5_MINORTYPE_BRLCAD_BOT) {
	bu_exit(1, "ERROR: object %s does not appear to be of type BoT\n", argv[2]);
    } else {
	bot_ip = (struct rt_bot_internal *)intern.idb_ptr;
    }
    RT_BOT_CK_MAGIC(bot_ip);

    for (size_t i_cnt = 1; i_cnt < 3; i_cnt++) {
	mesh = iterate(bot_ip, prev_mesh);
	prev_mesh = mesh;

	// Plot results
	struct bu_vls fname;
	bu_vls_init(&fname);
	bu_vls_printf(&fname, "root3_%d.pl", i_cnt);
	FILE* plot_file = fopen(bu_vls_addr(&fname), "w");
	std::map<size_t, std::vector<size_t> >::iterator f_it;
	std::vector<size_t>::iterator l_it;
	int r = int(256*drand48() + 1.0);
	int g = int(256*drand48() + 1.0);
	int b = int(256*drand48() + 1.0);
	for (f_it = mesh->face_pts.begin(); f_it != mesh->face_pts.end(); f_it++) {
	    l_it = (*f_it).second.begin();
	    plot_face(&mesh->points_p0[(int)(*l_it)], &mesh->points_p0[(int)(*(l_it+1))], &mesh->points_p0[(int)(*(l_it+2))], r, g , b, plot_file);
	}
	fclose(plot_file);
    }

    // When constructing the final BoT, use the limit points for all
    // vertices
    ON_3dPointArray points_inf;
    for (size_t v = 0; v < (size_t)mesh->points_p0.Count(); v++) {
        points_inf.Append(*mesh->points_p0.At((int)v));
	//point_inf(v, mesh, &points_inf);
    }
    // The subdivision process shrinks the bot relative to its original
    // vertex positions - to better approximate the original surface,
    // average the change in position of the original vertices to get a
    // scaling factor and apply it to all points in the final mesh.
    fastf_t scale = 0.0;
    for (size_t pcnt = 0; pcnt < bot_ip->num_vertices; pcnt++) {
	ON_3dVector v1(ON_3dPoint(&bot_ip->vertices[pcnt*3]));
	ON_3dVector v2(*points_inf.At((int)pcnt));
	scale += 1 + (v1.Length() - v2.Length())/v1.Length();
    }
    scale = scale / bot_ip->num_vertices;
    for (size_t pcnt = 0; pcnt < (size_t)points_inf.Count(); pcnt++) {
	ON_3dPoint p0(*points_inf.At((int)pcnt));
	ON_3dPoint p1 = p0 * scale;
	*points_inf.At((int)pcnt) = p1;
    }

    wdbp = wdb_dbopen(dbip, RT_WDB_TYPE_DB_DISK);

    fastf_t *vertices = (fastf_t *)bu_malloc(sizeof(fastf_t) * points_inf.Count() * 3, "new verts");
    int *faces = (int *)bu_malloc(sizeof(int) * mesh->face_pts.size() * 3, "new faces");
    for (size_t v = 0; v < (size_t)points_inf.Count(); v++) {
	vertices[v*3] = points_inf[(int)v].x;
	vertices[v*3+1] = points_inf[(int)v].y;
	vertices[v*3+2] = points_inf[(int)v].z;
    }
    std::map<size_t, std::vector<size_t> >::iterator f_it;
    std::vector<size_t>::iterator l_it;
    for (f_it = mesh->face_pts.begin(); f_it != mesh->face_pts.end(); f_it++) {
	l_it = (*f_it).second.begin();
	faces[(*f_it).first*3] = (*l_it);
	faces[(*f_it).first*3+1] = (*(l_it + 1));
	faces[(*f_it).first*3+2] = (*(l_it + 2));
    }

    bu_vls_init(&bname);
    bu_vls_sprintf(&bname, "%s_subd", argv[2]);
    mk_bot(wdbp, bu_vls_addr(&bname), RT_BOT_SOLID, RT_BOT_UNORIENTED, 0, points_inf.Count(), mesh->face_pts.size(), vertices, faces, (fastf_t *)NULL, (struct bu_bitv *)NULL);
    wdb_close(wdbp);
    bu_vls_free(&bname);

    bu_free(vertices, "free subdivision BoT vertices");
    bu_free(faces, "free subdivision BoT faces");

    return 0;
}
示例#5
0
文件: g-step.cpp 项目: kanzure/brlcad
int
main(int argc, char *argv[])
{
    int ret = 0;
    int convert_tops_list = 0;
    struct directory **paths;
    int path_cnt = 0;
    AP203_Contents *sc = new AP203_Contents;

    // process command line arguments
    int c;
    char *output_file = (char *)NULL;
    while ((c = bu_getopt(argc, argv, "o:")) != -1) {
        switch (c) {
        case 'o':
            output_file = bu_optarg;
            break;
        default:
            usage();
            bu_exit(1, NULL);
            break;
        }
    }

    if (bu_optind >= argc || output_file == (char *)NULL) {
        usage();
        bu_exit(1, NULL);
    }

    argc -= bu_optind;
    argv += bu_optind;

    /* check our inputs/outputs */
    if (bu_file_exists(output_file, NULL)) {
        bu_exit(1, "ERROR: refusing to overwrite existing output file:\"%s\". Please remove file or change output file name and try again.", output_file);
    }

    if (!bu_file_exists(argv[0], NULL) && !BU_STR_EQUAL(argv[0], "-")) {
        bu_exit(2, "ERROR: unable to read input \"%s\" .g file", argv[0]);
    }

    if (argc < 2) {
        convert_tops_list = 1;
    }


    std::string iflnm = argv[0];
    std::string oflnm = output_file;

    /* load .g file */
    BRLCADWrapper *dotg  = new BRLCADWrapper();
    if (!dotg) {
        std::cerr << "ERROR: unable to create BRL-CAD instance" << std::endl;
        return 3;
    }
    if (!dotg->load(iflnm)) {
        std::cerr << "ERROR: unable to open BRL-CAD input file [" << oflnm << "]" << std::endl;
        delete dotg;
        return 2;
    }

    struct db_i *dbip = dotg->GetDBIP();
    struct rt_wdb *wdbp = wdb_dbopen(dbip, RT_WDB_TYPE_DB_DISK);

    if (convert_tops_list) {
        /* Need db_update_nref for DB_LS_TOPS to work */
        db_update_nref(dbip, &rt_uniresource);
        path_cnt = db_ls(dbip, DB_LS_TOPS, &paths);
        if (!path_cnt) {
            std::cerr << "ERROR: no objects found in .g file" << "\n" << std::endl;
            delete dotg;
            return 1;
        }
    } else {
        int i = 1;
        paths = (struct directory **)bu_malloc(sizeof(struct directory *) * argc, "dp array");
        while (i < argc) {
            bu_log("%d: %s\n", i, argv[i]);
            struct directory *dp = db_lookup(dbip, argv[i], LOOKUP_QUIET);
            if (dp == RT_DIR_NULL) {
                std::cerr << "ERROR: cannot find " << argv[i] << "\n" << std::endl;
                delete dotg;
                bu_free(paths, "free path memory");
                return 1;
            } else {
                paths[i-1] = dp;
                path_cnt++;
                i++;
            }
        }
        paths[i-1] = RT_DIR_NULL;
    }


    struct bu_vls scratch_string;
    bu_vls_init(&scratch_string);

    Registry *registry = new Registry(SchemaInit);
    InstMgr instance_list;
    STEPfile *sfile = new STEPfile(*registry, instance_list);

    registry->ResetSchemas();
    registry->ResetEntities();

    /* Populate the header instances */
    InstMgr *header_instances = sfile->HeaderInstances();

    /* 1 - Populate File_Name */
    SdaiFile_name * fn = (SdaiFile_name *)sfile->HeaderDefaultFileName();
    bu_vls_sprintf(&scratch_string, "'%s'", output_file);
    fn->name_(bu_vls_addr(&scratch_string));
    fn->time_stamp_("");
    StringAggregate_ptr author_tmp = new StringAggregate;
    author_tmp->AddNode(new StringNode("''"));
    fn->author_(author_tmp);
    StringAggregate_ptr org_tmp = new StringAggregate;
    org_tmp->AddNode(new StringNode("''"));
    fn->organization_(org_tmp);
    fn->preprocessor_version_("'BRL-CAD g-step exporter'");
    fn->originating_system_("''");
    fn->authorization_("''");
    header_instances->Append((SDAI_Application_instance *)fn, completeSE);

    /* 2 - Populate File_Description */
    SdaiFile_description * fd = (SdaiFile_description *)sfile->HeaderDefaultFileDescription();
    StringAggregate_ptr description_tmp = new StringAggregate;
    description_tmp->AddNode(new StringNode("''"));
    fd->description_(description_tmp);
    fd->implementation_level_("'2;1'");
    header_instances->Append((SDAI_Application_instance *)fd, completeSE);

    /* 3 - Populate File_Schema */
    SdaiFile_schema *fs = (SdaiFile_schema *)sfile->HeaderDefaultFileSchema();
    StringAggregate_ptr schema_tmp = new StringAggregate;
    schema_tmp->AddNode(new StringNode("'CONFIG_CONTROL_DESIGN'"));
    fs->schema_identifiers_(schema_tmp);
    header_instances->Append((SDAI_Application_instance *)fs, completeSE);

    sc->registry = registry;
    sc->instance_list = &instance_list;

    sc->default_context = Add_Default_Geometric_Context(sc);

    sc->application_context = (SdaiApplication_context *)sc->registry->ObjCreate("APPLICATION_CONTEXT");
    sc->instance_list->Append((STEPentity *)sc->application_context, completeSE);
    sc->application_context->application_("'CONFIGURATION CONTROLLED 3D DESIGNS OF MECHANICAL PARTS AND ASSEMBLIES'");

    sc->design_context = (SdaiDesign_context *)sc->registry->ObjCreate("DESIGN_CONTEXT");
    sc->instance_list->Append((STEPentity *)sc->design_context, completeSE);
    sc->design_context->name_("''");
    sc->design_context->life_cycle_stage_("'design'");
    sc->design_context->frame_of_reference_(sc->application_context);

    sc->solid_to_step = new std::map<struct directory *, STEPentity *>;
    sc->solid_to_step_shape = new std::map<struct directory *, STEPentity *>;
    sc->solid_to_step_manifold = new std::map<struct directory *, STEPentity *>;
    sc->comb_to_step = new std::map<struct directory *, STEPentity *>;
    sc->comb_to_step_shape = new std::map<struct directory *, STEPentity *>;
    sc->comb_to_step_manifold = new std::map<struct directory *, STEPentity *>;

    for (int i = 0; i < path_cnt; i++) {
        /* Now, add actual DATA */
        struct directory *dp = paths[i];
        struct rt_db_internal intern;
        rt_db_get_internal(&intern, dp, dbip, bn_mat_identity, &rt_uniresource);
        RT_CK_DB_INTERNAL(&intern);
        Object_To_STEP(dp, &intern, wdbp, sc);
        rt_db_free_internal(&intern);
    }

    /* Write STEP file */
    if (!bu_file_exists(output_file, NULL)) {
        std::ofstream stepout(output_file);
        sfile->WriteExchangeFile(stepout);
    }

    /* Free memory */
    header_instances->DeleteInstances();
    instance_list.DeleteInstances();
    delete dotg;
    delete registry;
    delete sfile;
    delete sc->solid_to_step;
    delete sc->solid_to_step_shape;
    delete sc->solid_to_step_manifold;
    delete sc->comb_to_step;
    delete sc->comb_to_step_shape;
    delete sc->comb_to_step_manifold;
    delete sc;
    bu_vls_free(&scratch_string);
    bu_free(paths, "free dp list");

    return ret;
}
示例#6
0
/**
 *	P O P _ S P A W N --- spawn a new population
 *	TODO: generalize/modularize somehow to allow adding more shapes and primitives
 *	also use variable/defined rates, intersection with bounding box, etc...
 */
void
pop_spawn (struct population *p)
{
    int i, j;
    point_t p1/*, p2, p3*/;
    struct wmember wm_hd;
    double r1/*, r2, r3*/;

    char shape[256];

    p->db_p = db_create("gen000", 5);
    p->db_p->dbi_wdbp = wdb_dbopen(p->db_p, RT_WDB_TYPE_DB_DISK);

    for (i = 0; i < p->size; i++) {
	p->name[i] = bu_malloc(sizeof(char) * 256, "name");
	snprintf(p->name[i], 256, "ind%.3d", i);

	BU_LIST_INIT(&wm_hd.l);
	/*
	  VSET(p1, -5, -5, -5);
	  VSET(p2, 5, 5, 5);
	  r1 = r2 = 2.5;
	*/
	for (j = 0; j < 6; j++) {
	    /* VSETALL(p1, -10+pop_rand()*10); */
	    p1[0] = -10*pop_rand()*10;
	    p1[1] = -10*pop_rand()*10;
	    p1[2] = -10*pop_rand()*10;
	    r1 = 1+3*pop_rand();
	    snprintf(shape, 256, "ind%.3d-%.3d", i, j);
	    mk_sph(p->db_p->dbi_wdbp, shape, p1, r1);
	    mk_addmember(shape, &wm_hd.l, NULL, WMOP_UNION);
	}


	p->parent[i].fitness = 0.0;
	p->parent[i].id = i;
	/*

	snprintf(shape, 256, "ind%.3d-%.3d", i, 0);
	mk_sph(p->db_p->dbi_wdbp, shape, p1, r1);
	mk_addmember(shape, &wm_hd.l, NULL, WMOP_UNION);


	snprintf(shape, 256, "ind%.3d-%.3d", i, 1);
	mk_sph(p->db_p->dbi_wdbp, shape, p2, r2);
	mk_addmember(shape, &wm_hd.l, NULL, WMOP_UNION);

	snprintf(shape, 256, "gen%.3dind%.3d-%.3d", 0, i, 2);
	mk_sph(p->db_p->dbi_wdbp, shape, p3, r3);
	mk_addmember(shape, &wm_hd.l, NULL, WMOP_UNION);
	*/
	mk_lcomb(p->db_p->dbi_wdbp, NL_P(p->parent[i].id), &wm_hd, 1, NULL, NULL, NULL, 0);
    }

/*
 * reload the db so we dont
 * have to do any extra checks
 * in the main loop
 */
    wdb_close(p->db_p->dbi_wdbp);
    if ((p->db_p = db_open("gen000", "r")) == DBI_NULL)
	bu_exit(EXIT_FAILURE, "Failed to re-open initial population");
    if (db_dirbuild(p->db_p) < 0)
	bu_exit(EXIT_FAILURE, "Failed to load initial database");
}