int rt_process_casec(struct edge_g_cnurb *trim, fastf_t u, fastf_t v) { struct edge_g_cnurb * clip; int jordan_hit; struct bu_list plist; int trim_flag = 0; int caset; /* determine if the the u, v values are on the curve */ if ( rt_nurb_uv_dist(trim, u, v) == TRIM_ON) return TRIM_IN; jordan_hit = 0; BU_LIST_INIT(&plist); if ( nurb_crv_is_bezier( trim ) ) rt_clip_cnurb(&plist, trim, u, v); else nurb_c_to_bezier( &plist, trim ); while ( BU_LIST_WHILE( clip, edge_g_cnurb, &plist ) ) { BU_LIST_DEQUEUE( &clip->l ); caset = rt_trim_case(clip, u, v); trim_flag = 0; if ( caset == CASE_B) trim_flag = rt_process_caseb(clip, u, v); if ( caset == CASE_C) trim_flag = rt_process_casec(clip, u, v); rt_nurb_free_cnurb( clip ); if ( trim_flag == TRIM_IN) jordan_hit++; if ( trim_flag == TRIM_ON) break; } while ( BU_LIST_WHILE( clip, edge_g_cnurb, &plist) ) { BU_LIST_DEQUEUE( &clip->l ); rt_nurb_free_cnurb( clip ); } if ( trim_flag == TRIM_ON) return TRIM_ON; else if ( jordan_hit & 01 ) return TRIM_IN; else return TRIM_OUT; }
static void _bu_close_files() { struct _bu_tf_list *popped; if (!_bu_tf) { return; } /* close all files, free their nodes, and unlink */ while (BU_LIST_WHILE(popped, _bu_tf_list, &(_bu_tf->l))) { BU_LIST_DEQUEUE(&(popped->l)); if (popped) { if (popped->fd != -1) { close(popped->fd); popped->fd = -1; } if (BU_VLS_IS_INITIALIZED(&popped->fn) && bu_vls_addr(&popped->fn)) { unlink(bu_vls_addr(&popped->fn)); bu_vls_free(&popped->fn); } bu_free(popped, "free bu_temp_file node"); } } /* free the head */ if (_bu_tf->fd != -1) { close(_bu_tf->fd); _bu_tf->fd = -1; } if (BU_VLS_IS_INITIALIZED(&_bu_tf->fn) && bu_vls_addr(&_bu_tf->fn)) { unlink(bu_vls_addr(&_bu_tf->fn)); bu_vls_free(&_bu_tf->fn); } bu_free(_bu_tf, "free bu_temp_file head"); }
/* * R T _ N U R B _ B E Z I E R * * Given a single snurb, if it is in Bezier form, * duplicate the snurb, and enqueue it on the bezier_hd list. * If the original snurb is NOT in Bezier form, * subdivide it a set of snurbs which are, * each of which are enqueued on the bezier_hd list. * * In either case, the original surface remains untouched. * * Returns - * 0 Surface splitting was done. * 1 Original surface was Bezier, only a copy was done. */ int rt_nurb_bezier(struct bu_list *bezier_hd, const struct face_g_snurb *orig_surf, struct resource *res) { struct face_g_snurb *s; int dir; struct bu_list todo; NMG_CK_SNURB(orig_surf); if ( (dir = rt_bez_check( orig_surf )) == -1) { s = rt_nurb_scopy( orig_surf, res ); BU_LIST_APPEND( bezier_hd, &s->l ); return 1; /* Was already Bezier, nothing done */ } BU_LIST_INIT( &todo ); rt_nurb_s_split( &todo, orig_surf, dir, res ); while ( BU_LIST_WHILE( s, face_g_snurb, &todo ) ) { if ( (dir = rt_bez_check(s)) == -1) { /* This snurb is now a Bezier */ BU_LIST_DEQUEUE( &s->l ); BU_LIST_APPEND( bezier_hd, &s->l ); } else { /* Split, and keep going */ BU_LIST_DEQUEUE( &s->l ); rt_nurb_s_split( &todo, s, dir, res ); rt_nurb_free_snurb(s, res); } } return 0; /* Bezier snurbs on bezier_hd list */ }
void Parser::parse(struct pc_pc_set *pcs) { /*Iterate through the parameter set first*/ struct pc_param *par; struct pc_constrnt *con; while (BU_LIST_WHILE(par, pc_param, &(pcs->ps->l))) { name.clear(); //std::cout<<"Parameter expression Input: "<<(char *) bu_vls_addr(&(par->name))<<std::endl; if (par->ctype == PC_DB_BYEXPR) { boost::spirit::classic::parse_info<> p_info = \ boost::spirit::classic::parse(\ (char *) bu_vls_addr(&(par->data.expression)), \ *var_gram, boost::spirit::classic::space_p); if (p_info.full) { //vcset.pushVar(); } else { std::cout << "Error during Variable expression parsing\n"; } bu_vls_free(&(par->data.expression)); } else { vcset.addParameter((char *) bu_vls_addr(&(par->name)), \ par->dtype, par->data.ptr); } bu_vls_free(&(par->name)); BU_LIST_DEQUEUE(&(par->l)); bu_free(par, "free parameter"); } while (BU_LIST_WHILE(con, pc_constrnt, &(pcs->cs->l))) { if (con->ctype == PC_DB_BYEXPR) { bu_vls_free(&(con->data.expression)); } else if (con->ctype == PC_DB_BYSTRUCT) { //std::cout << "Constraint by Struct -> \n"; vcset.addConstraint(con); bu_free(con->args, "free argument array"); } /*boost::spirit::classic::parse((char *) bu_vls_addr(&(con->name)), *con_gram, boost::spirit::space_p);*/ bu_vls_free(&(con->name)); BU_LIST_DEQUEUE(&(con->l)); bu_free(con, "free constraint"); } }
HIDDEN void wdb_free_tokens(struct bu_list *hp) { struct tokens *tok; BU_CK_LIST_HEAD(hp); while (BU_LIST_WHILE(tok, tokens, hp)) { BU_LIST_DEQUEUE(&tok->l); if (tok->type == WDB_TOK_TREE) { db_free_tree(tok->tp, &rt_uniresource); } } }
HIDDEN void gauss_free(void *cp) { register struct gauss_specific *gauss_sp = (struct gauss_specific *)cp; struct reg_db_internals *p; while (BU_LIST_WHILE(p, reg_db_internals, &gauss_sp->dbil)) { BU_LIST_DEQUEUE(&(p->l)); bu_free(p->ip.idb_ptr, "internal ptr"); bu_free((void *)p, "gauss reg_db_internals"); } BU_PUT(cp, struct gauss_specific); }
void bn_vlist_cleanup(struct bu_list *hd) { register struct bn_vlist *vp; if (!BU_LIST_IS_INITIALIZED(hd)) { BU_LIST_INIT(hd); return; } while (BU_LIST_WHILE(vp, bn_vlist, hd)) { BN_CK_VLIST(vp); BU_LIST_DEQUEUE(&(vp->l)); bu_free((char *)vp, "bn_vlist"); } }
/** * free saved rays */ void free_rays(struct fitness_state *fstate) { int i; struct part *p; for (i = 0; i < fstate->res[X] * fstate->res[Y]; i++) { if (fstate->ray[i] == NULL) continue; while (BU_LIST_WHILE(p, part, &fstate->ray[i]->l)) { BU_LIST_DEQUEUE(&p->l); bu_free(p, "part"); } bu_free(fstate->ray[i], "part"); } bu_free(fstate->ray, "fstate->ray"); }
int main(int argc, char **argv) { static long ncells; bu_debug = BU_DEBUG_MEM_CHECK | BU_DEBUG_MEM_LOG; bu_debug = 0; BU_LIST_INIT(&(gp_locs.l)); if (! pars_Argv(argc, argv)) { prnt_Usage(); return 1; } grid = (Cell *) bu_malloc(sizeof(Cell) * maxcells, "grid"); if (debug_flag & CFB_DBG_MEM) bu_log("grid = %p... %ld cells @ %lu bytes/cell\n", (void *)grid, maxcells, sizeof(Cell)); do { struct locrec *lrp; init_Globs(); if ((ncells = read_Cell_Data()) == 0) { bu_log("cell-fb: failed to read view\n"); return 1; } if (BU_LIST_NON_EMPTY(&(gp_locs.l))) { while (BU_LIST_WHILE(lrp, locrec, (&(gp_locs.l)))) { BU_LIST_DEQUEUE(&(lrp->l)); bu_log("%g %g %d %d\n", lrp->h, lrp->v, (int) H2SCRX(lrp->h), (int) V2SCRY(lrp->v)); bu_free((char *) lrp, "location record"); } } else { bu_log("Displaying %ld cells\n", ncells); if (! display_Cells(ncells)) { bu_log("cell-fb: failed to display %ld cells\n", ncells); return 1; } if (log_flag) log_Run(); } } while ((view_flag == 0) && ! feof(filep) && get_OK()); return 0; }
/** * Free the storage associated with the rt_db_internal version of this * solid. This only effects the in-memory copy. */ void rt_metaball_ifree(struct rt_db_internal *ip) { register struct rt_metaball_internal *metaball; register struct wdb_metaballpt *mbpt; RT_CK_DB_INTERNAL(ip); metaball = (struct rt_metaball_internal*)ip->idb_ptr; RT_METABALL_CK_MAGIC(metaball); if (metaball->metaball_ctrl_head.magic != 0) while (BU_LIST_WHILE(mbpt, wdb_metaballpt, &metaball->metaball_ctrl_head)) { BU_LIST_DEQUEUE(&(mbpt->l)); BU_PUT(mbpt, struct wdb_metaballpt); } bu_free(ip->idb_ptr, "metaball ifree"); ip->idb_ptr = ((void *)0); }
static void run_scripts(struct bu_list *sl, struct rt_i *rtip) { struct script_rec *srp; char *cp; FILE *fPtr; if (nirt_debug & DEBUG_SCRIPTS) show_scripts(sl, "before running them"); while (BU_LIST_WHILE(srp, script_rec, sl)) { BU_LIST_DEQUEUE(&(srp->l)); BU_CKMAG(srp, SCRIPT_REC_MAGIC, "script record"); cp = bu_vls_addr(&(srp->sr_script)); if (nirt_debug & DEBUG_SCRIPTS) { bu_log(" Attempting to run %s '%s'\n", (srp->sr_type == READING_STRING) ? "literal" : (srp->sr_type == READING_FILE) ? "file" : "???", cp); } switch (srp->sr_type) { case READING_STRING: interact(READING_STRING, cp, rtip); break; case READING_FILE: if ((fPtr = fopen(cp, "rb")) == NULL) { bu_log("Cannot open script file '%s'\n", cp); } else { interact(READING_FILE, fPtr, rtip); fclose(fPtr); } break; default: bu_exit (1, "%s:%d: script of type %d. This shouldn't happen\n", __FILE__, __LINE__, srp->sr_type); } free_script(srp); } if (nirt_debug & DEBUG_SCRIPTS) show_scripts(sl, "after running them"); }
struct rt_nurb_uv_hit * rt_nurb_intersect(const struct face_g_snurb *srf, fastf_t *plane1, fastf_t *plane2, double uv_tol, struct resource *res, struct bu_list *plist) { struct rt_nurb_uv_hit * h; struct face_g_snurb * psrf, * osrf; int dir, sub; point_t vmin, vmax; fastf_t u[2], v[2]; struct bu_list rni_plist; NMG_CK_SNURB(srf); h = (struct rt_nurb_uv_hit *) 0; if (plist == NULL) { plist = &rni_plist; BU_LIST_INIT(plist); } /* project the surface to a 2 dimensional problem */ /* NOTE that this gives a single snurb back, NOT a list */ psrf = rt_nurb_project_srf(srf, plane2, plane1, res); psrf->dir = 1; BU_LIST_APPEND(plist, &psrf->l); if (RT_G_DEBUG & DEBUG_SPLINE) rt_nurb_s_print("srf", psrf); /* This list starts out with only a single snurb, but more may be * added on as work progresses. */ while (BU_LIST_WHILE(psrf, face_g_snurb, plist)) { int flat; BU_LIST_DEQUEUE(&psrf->l); NMG_CK_SNURB(psrf); sub = 0; flat = 0; dir = psrf->dir; while (!flat) { fastf_t smin = 0.0, smax = 0.0; sub++; dir = (dir == 0)?1:0; /* change direction */ if (RT_G_DEBUG & DEBUG_SPLINE) rt_nurb_s_print("psrf", psrf); rt_nurb_pbound(psrf, vmin, vmax); /* Check for origin to be included in the bounding box */ if (!(vmin[0] <= 0.0 && vmin[1] <= 0.0 && vmax[0] >= 0.0 && vmax[1] >= 0.0)) { if (RT_G_DEBUG & DEBUG_SPLINE) bu_log("this srf doesn't include the origin\n"); flat = 1; rt_nurb_free_snurb(psrf, res); continue; } rt_nurb_clip_srf(psrf, dir, &smin, &smax); if ((smax - smin) > .8) { struct rt_nurb_uv_hit *hp; /* Split surf, requeue both sub-surfs at head */ /* New surfs will have same dir as arg, here */ if (RT_G_DEBUG & DEBUG_SPLINE) bu_log("splitting this surface\n"); rt_nurb_s_split(plist, psrf, dir, res); rt_nurb_free_snurb(psrf, res); hp = rt_nurb_intersect(srf, plane1, plane2, uv_tol, res, plist); return hp; } if (smin > 1.0 || smax < 0.0) { if (RT_G_DEBUG & DEBUG_SPLINE) bu_log("eliminating this surface (smin=%g, smax=%g)\n", smin, smax); flat = 1; rt_nurb_free_snurb(psrf, res); continue; } if (dir == RT_NURB_SPLIT_ROW) { smin = (1.0 - smin) * psrf->u.knots[0] + smin * psrf->u.knots[ psrf->u.k_size -1]; smax = (1.0 - smax) * psrf->u.knots[0] + smax * psrf->u.knots[ psrf->u.k_size -1]; } else { smin = (1.0 - smin) * psrf->v.knots[0] + smin * psrf->v.knots[ psrf->v.k_size -1]; smax = (1.0 - smax) * psrf->v.knots[0] + smax * psrf->v.knots[ psrf->v.k_size -1]; } osrf = psrf; psrf = (struct face_g_snurb *) rt_nurb_region_from_srf( osrf, dir, smin, smax, res); psrf->dir = dir; rt_nurb_free_snurb(osrf, res); if (RT_G_DEBUG & DEBUG_SPLINE) { bu_log("After call to rt_nurb_region_from_srf() (smin=%g, smax=%g)\n", smin, smax); rt_nurb_s_print("psrf", psrf); } u[0] = psrf->u.knots[0]; u[1] = psrf->u.knots[psrf->u.k_size -1]; v[0] = psrf->v.knots[0]; v[1] = psrf->v.knots[psrf->v.k_size -1]; if ((u[1] - u[0]) < uv_tol && (v[1] - v[0]) < uv_tol) { struct rt_nurb_uv_hit * hit; if (RT_G_DEBUG & DEBUG_SPLINE) { fastf_t p1[4], p2[4]; int coords; vect_t diff; coords = RT_NURB_EXTRACT_COORDS(srf->pt_type); rt_nurb_s_eval(srf, u[0], v[0], p1); rt_nurb_s_eval(srf, u[1], v[1], p2); if (RT_NURB_IS_PT_RATIONAL(srf->pt_type)) { fastf_t inv_w; inv_w = 1.0 / p1[coords-1]; VSCALE(p1, p1, inv_w); inv_w = 1.0 / p2[coords-1]; VSCALE(p2, p2, inv_w); } VSUB2(diff, p1, p2); bu_log("Precision of hit point = %g (%f %f %f) <-> (%f %f %f)\n", MAGNITUDE(diff), V3ARGS(p1), V3ARGS(p2)); } hit = (struct rt_nurb_uv_hit *) bu_malloc( sizeof(struct rt_nurb_uv_hit), "hit"); hit->next = (struct rt_nurb_uv_hit *)0; hit->sub = sub; hit->u = (u[0] + u[1])/2.0; hit->v = (v[0] + v[1])/2.0; if (h == (struct rt_nurb_uv_hit *)0) h = hit; else { hit->next = h; h = hit; } flat = 1; rt_nurb_free_snurb(psrf, res); } if ((u[1] - u[0]) > (v[1] - v[0])) dir = 1; else dir = 0; } } return (struct rt_nurb_uv_hit *)h; }
int main(int argc, char *argv[]) { struct rt_i *rtip = NULL; char db_title[TITLE_LEN+1];/* title from MGED file */ const char *tmp_str; extern char local_u_name[65]; extern double base2local; extern double local2base; FILE *fPtr; int Ch; /* Option name */ int mat_flag = 0; /* Read matrix from stdin? */ int use_of_air = 0; int print_ident_flag = 1; char ocastring[1024] = {0}; struct bu_list script_list; /* For -e and -f options */ struct script_rec *srp; extern outval ValTab[]; /* from if.c, callback functions for overlap, hit, and miss shots */ int if_overlap(struct application *, struct partition *, struct region *, struct region *, struct partition *); int if_hit(struct application *, struct partition *, struct seg *); int if_miss(struct application *); BU_LIST_INIT(&script_list); bu_setprogname(argv[0]); ocname[OVLP_RESOLVE] = "resolve"; ocname[OVLP_REBUILD_FASTGEN] = "rebuild_fastgen"; ocname[OVLP_REBUILD_ALL] = "rebuild_all"; ocname[OVLP_RETAIN] = "retain"; *ocastring = '\0'; bu_optind = 1; /* restart */ /* Handle command-line options */ while ((Ch = bu_getopt(argc, argv, OPT_STRING)) != -1) { if (bu_optopt == '?') Ch='h'; switch (Ch) { case 'A': attrib_add(bu_optarg, &need_prep); break; case 'B': rt_bot_minpieces = atoi(bu_optarg); break; case 'T': setenv("LIBRT_BOT_MINTIE", bu_optarg, 1); break; case 'b': do_backout = 1; break; case 'E': if (nirt_debug & DEBUG_SCRIPTS) show_scripts(&script_list, "before erasure"); while (BU_LIST_WHILE(srp, script_rec, &script_list)) { BU_LIST_DEQUEUE(&(srp->l)); free_script(srp); } if (nirt_debug & DEBUG_SCRIPTS) show_scripts(&script_list, "after erasure"); break; case 'e': enqueue_script(&script_list, READING_STRING, bu_optarg); if (nirt_debug & DEBUG_SCRIPTS) show_scripts(&script_list, "after enqueueing a literal"); break; case 'f': enqueue_script(&script_list, READING_FILE, bu_optarg); if (nirt_debug & DEBUG_SCRIPTS) show_scripts(&script_list, "after enqueueing a file name"); break; case 'L': listformats(); bu_exit(EXIT_SUCCESS, NULL); case 'M': mat_flag = 1; break; case 'O': sscanf(bu_optarg, "%1024s", ocastring); break; case 's': silent_flag = SILENT_YES; /* Positively yes */ break; case 'v': silent_flag = SILENT_NO; /* Positively no */ break; case 'x': sscanf(bu_optarg, "%x", (unsigned int *)&RTG.debug); break; case 'X': sscanf(bu_optarg, "%x", (unsigned int *)&nirt_debug); break; case 'u': if (sscanf(bu_optarg, "%d", &use_of_air) != 1) { (void) fprintf(stderr, "Illegal use-air specification: '%s'\n", bu_optarg); return 1; } break; case 'H': if (sscanf(bu_optarg, "%d", &print_ident_flag) != 1) { (void) fprintf(stderr, "Illegal header output option specified: '%s'\n", bu_optarg); return 1; } break; default: printusage(); bu_exit (Ch != 'h', NULL); } } /* end while getopt */ if (argc - bu_optind < 2) { printusage(); return 1; } if (isatty(0)) { if (silent_flag != SILENT_YES) silent_flag = SILENT_NO; } else { /* stdin is not a TTY */ if (silent_flag != SILENT_NO) silent_flag = SILENT_YES; } if (silent_flag != SILENT_YES && print_ident_flag) (void) fputs(brlcad_ident("Natalie's Interactive Ray Tracer"), stdout); if (use_of_air && (use_of_air != 1)) { fprintf(stderr, "Warning: useair=%d specified, will set to 1\n", use_of_air); use_of_air = 1; } switch (*ocastring) { case '\0': overlap_claims = OVLP_RESOLVE; break; case '0': case '1': case '2': case '3': if (ocastring[1] == '\0') { sscanf(ocastring, "%d", &overlap_claims); } else { fprintf(stderr, "Illegal overlap_claims specification: '%s'\n", ocastring); return 1; } break; case 'r': if (BU_STR_EQUAL(ocastring, "resolve")) overlap_claims = OVLP_RESOLVE; else if (BU_STR_EQUAL(ocastring, "rebuild_fastgen")) overlap_claims = OVLP_REBUILD_FASTGEN; else if (BU_STR_EQUAL(ocastring, "rebuild_all")) overlap_claims = OVLP_REBUILD_ALL; else if (BU_STR_EQUAL(ocastring, "retain")) overlap_claims = OVLP_RETAIN; else { fprintf(stderr, "Illegal overlap_claims specification: '%s'\n", ocastring); return 1; } break; default: fprintf(stderr, "Illegal overlap_claims specification: '%s'\n", ocastring); return 1; } db_name = argv[bu_optind]; /* build directory for target object */ if (silent_flag != SILENT_YES) { printf("Database file: '%s'\n", db_name); printf("Building the directory..."); } if ((rtip = rt_dirbuild(db_name, db_title, TITLE_LEN)) == RTI_NULL) { fflush(stdout); fprintf(stderr, "Could not load file %s\n", db_name); return 1; } rti_tab[use_of_air] = rtip; rti_tab[1 - use_of_air] = RTI_NULL; rtip->useair = use_of_air; rtip->rti_save_overlaps = (overlap_claims > 0); ++bu_optind; do_rt_gettrees(rtip, argv + bu_optind, argc - bu_optind, &need_prep); /* Initialize the table of resource structures */ rt_init_resource(&res_tab, 0, rtip); /* initialization of the application structure */ RT_APPLICATION_INIT(&ap); ap.a_hit = if_hit; /* branch to if_hit routine */ ap.a_miss = if_miss; /* branch to if_miss routine */ ap.a_overlap = if_overlap;/* branch to if_overlap routine */ ap.a_logoverlap = rt_silent_logoverlap; ap.a_onehit = 0; /* continue through shotline after hit */ ap.a_resource = &res_tab; ap.a_purpose = "NIRT ray"; ap.a_rt_i = rtip; /* rt_i pointer */ ap.a_zero1 = 0; /* sanity check, sayth raytrace.h */ ap.a_zero2 = 0; /* sanity check, sayth raytrace.h */ ap.a_uptr = (void *)a_tab.attrib; /* initialize variables */ azimuth() = 0.0; elevation() = 0.0; direct(X) = -1.0; direct(Y) = 0.0; direct(Z) = 0.0; grid(HORZ) = 0.0; grid(VERT) = 0.0; grid(DIST) = 0.0; grid2targ(); set_diameter(rtip); /* initialize the output specification */ default_ospec(); /* initialize NIRT's local units */ base2local = rtip->rti_dbip->dbi_base2local; local2base = rtip->rti_dbip->dbi_local2base; tmp_str = bu_units_string(local2base); if (tmp_str) { bu_strlcpy(local_u_name, tmp_str, sizeof(local_u_name)); } else { bu_strlcpy(local_u_name, "Unknown units", sizeof(local_u_name)); } if (silent_flag != SILENT_YES) { printf("Database title: '%s'\n", db_title); printf("Database units: '%s'\n", local_u_name); printf("model_min = (%g, %g, %g) model_max = (%g, %g, %g)\n", rtip->mdl_min[X] * base2local, rtip->mdl_min[Y] * base2local, rtip->mdl_min[Z] * base2local, rtip->mdl_max[X] * base2local, rtip->mdl_max[Y] * base2local, rtip->mdl_max[Z] * base2local); } /* Run the run-time configuration file, if it exists */ if ((fPtr = fopenrc()) != NULL) { interact(READING_FILE, fPtr, rtip); fclose(fPtr); } /* Run all scripts specified on the command line */ run_scripts(&script_list, rtip); /* Perform the user interface */ if (mat_flag) { read_mat(rtip); return 0; } else { interact(READING_FILE, stdin, rtip); } return 0; }
void pop_gop(int gop, char *parent1_id, char *parent2_id, char *child1_id, char *child2_id, struct db_i *dbi_p, struct db_i *dbi_c, struct resource *resp) { struct rt_db_internal in1, in2; struct rt_comb_internal *parent1; struct rt_comb_internal *parent2; struct directory *dp; union tree *cpoint, **cross_parent; struct node *add; int i = 0; struct node *chosen_node; int rand_node; RT_CHECK_DBI( dbi_p ); RT_CHECK_DBI( dbi_c ); RT_CK_RESOURCE( resp ); crossover_point = (union tree *)NULL; crossover_parent = (union tree **)NULL; node = (struct node*)NULL; if ( !rt_db_lookup_internal(dbi_p, parent1_id, &dp, &in1, LOOKUP_NOISY, &rt_uniresource)) bu_exit(EXIT_FAILURE, "Failed to read parent1"); shape_number =num_nodes= 0; parent1 = (struct rt_comb_internal *)in1.idb_ptr; mutate = 0; switch (gop) { case REPRODUCE: pop_functree(dbi_p, dbi_c, parent1->tree, resp, child1_id); break; case CROSSOVER: crossover = 1; /*load other parent */ if ( !rt_db_lookup_internal(dbi_p, parent2_id, &dp, &in2, LOOKUP_NOISY, resp)) bu_exit(EXIT_FAILURE, "Failed to read parent2"); parent2 = (struct rt_comb_internal *)in2.idb_ptr; BU_ALLOC(node, struct node); BU_LIST_INIT(&node->l); chosen_node = NULL; do{ num_nodes = 0; crossover_parent = &parent1->tree; crossover_node = (int)(pop_rand() * db_count_tree_nodes(parent1->tree, 0)); node_idx = 0; pop_functree(dbi_p, dbi_c, parent1->tree, resp, NULL); cross_parent = crossover_parent; cpoint = crossover_point; crossover_op = crossover_point->tr_op; #define MASK (OP_UNION | OP_XOR | OP_SUBTRACT|OP_INTERSECT) if (crossover_op & MASK)crossover_op = MASK; crossover_node = db_count_tree_nodes(crossover_point, 0); if (pop_find_nodes(parent2->tree) == crossover_node) { BU_ALLOC(add, struct node); add->s_parent = &parent2->tree; add->s_child = parent2->tree; BU_LIST_INSERT(&node->l, &add->l); ++num_nodes; } if (num_nodes > 0) { rand_node = (int)(pop_rand() * num_nodes); for (add=BU_LIST_FIRST(node, &node->l);BU_LIST_NOT_HEAD(add, &node->l) && chosen_node == NULL; add=BU_LIST_PNEXT(node, add)) { if (i++ == rand_node) { chosen_node = add; /* break cleanly...? */ } } } }while(chosen_node == NULL); /* cross trees */ *cross_parent = chosen_node->s_child; *chosen_node->s_parent =cpoint; while (BU_LIST_WHILE(add, node, &node->l)) { BU_LIST_DEQUEUE(&add->l); bu_free(add, "node"); } bu_free(node, "node"); crossover = 0; /*duplicate shapes held in trees*/ pop_functree(dbi_p, dbi_c, parent1->tree, resp, child1_id); shape_number = 0; pop_functree(dbi_p, dbi_c, parent2->tree, resp, child2_id); if ((dp = db_diradd(dbi_c, child2_id, -1, 0, dp->d_flags, (genptr_t)&dp->d_minor_type)) == RT_DIR_NULL) bu_exit(EXIT_FAILURE, "Failed to add new individual to child database"); if (rt_db_put_internal(dp, dbi_c, &in2, resp) < 0) bu_exit(EXIT_FAILURE, "Database write failure"); rt_db_free_internal(&in2); break; case MUTATE: crossover_parent = &parent1->tree; crossover_node = (int)(pop_rand() * db_count_tree_nodes(parent1->tree, 0)); node_idx = 0; mutate = 1; pop_functree(dbi_p, dbi_c, parent1->tree, resp, child1_id); mutate = 0; break; /* //random node to mutate n = (int)(pop_rand() * db_count_tree_nodes(parent1->tree, 0)); s_parent = &parent1->tree; s_node = n; node = 0; //find node pop_functree(dbi_p, dbi_c, parent1->tree, resp, NULL); */ default: bu_exit(EXIT_FAILURE, "illegal genetic operator\nfailed to execute genetic op"); } if ((dp=db_diradd(dbi_c, child1_id, -1, 0, dp->d_flags, (genptr_t)&dp->d_minor_type)) == RT_DIR_NULL) { bu_exit(EXIT_FAILURE, "Failed to add new individual to child database"); } if (rt_db_put_internal(dp, dbi_c, &in1, resp) < 0) bu_exit(EXIT_FAILURE, "Database write failure"); rt_db_free_internal(&in1); }
/* * M A I N ( ) */ int main (int argc, char **argv) { char *label[2]; /* Labels of edge endpoints */ int ch; /* Command-line character */ int i; int numeric = 0; /* Use vertex indices (vs. labels)? */ long index[2]; /* Indices of edge endpoints */ double weight; /* Edge weight */ bu_rb_tree *dictionary; /* Dictionary of vertices */ struct bridge *bp; /* The current bridge */ struct vertex *up; /* An uncivilized neighbor of vup */ struct vertex *vcp; /* The civilized vertex of bp */ struct vertex *vup; /* The uncivilized vertex of bp */ struct vertex *vertex[2]; /* The current edge */ struct neighbor *neighbor[2]; /* Their neighbors */ struct neighbor *np; /* A neighbor of vup */ int (*po[2])(); /* Priority queue order functions */ while ((ch = bu_getopt(argc, argv, OPT_STRING)) != EOF) switch (ch) { case 'n': numeric = 1; break; case '?': default: print_usage(); bu_exit (ch != '?', NULL); return(0); } /* * Initialize the dictionary */ dictionary = bu_rb_create1("Dictionary of vertices", numeric ? compare_vertex_indices : compare_vertex_labels); bu_rb_uniq_on1(dictionary); /* * Read in the graph */ while (get_edge(stdin, index, label, &weight, numeric)) { for (i = 0; i < 2; ++i) /* For each end of the edge... */ { vertex[i] = lookup_vertex(dictionary, index[i], label[i]); neighbor[i] = mk_neighbor(VERTEX_NULL, weight); BU_LIST_INSERT(&(vertex[i] -> v_neighbors), &(neighbor[i] -> l)); } neighbor[0] -> n_vertex = vertex[1]; neighbor[1] -> n_vertex = vertex[0]; } /* * Initialize the priority queue */ po[PRIOQ_INDEX] = compare_bridge_indices; po[PRIOQ_WEIGHT] = compare_bridge_weights; prioq = bu_rb_create("Priority queue of bridges", 2, po); bu_rb_walk1(dictionary, add_to_prioq, INORDER); if (debug) { print_prioq(); bu_rb_walk1(dictionary, print_vertex, INORDER); } /* * Grow a minimum spanning tree, using Prim's algorithm... * * While there exists a min-weight bridge (to a vertex v) in the queue * Dequeue the bridge * If the weight is finite * Output the bridge * Mark v as civilized * For every uncivilized neighbor u of v * if uv is cheaper than u's current bridge * dequeue u's current bridge * enqueue bridge(uv) */ weight = 0.0; while ((bp = extract_min()) != BRIDGE_NULL) { if (debug) { bu_log("extracted min-weight bridge <x%x>, leaving...\n", bp); print_prioq(); } BU_CKMAG(bp, BRIDGE_MAGIC, "bridge"); vcp = bp -> b_vert_civ; vup = bp -> b_vert_unciv; BU_CKMAG(vup, VERTEX_MAGIC, "vertex"); if (is_finite_bridge(bp)) { BU_CKMAG(vcp, VERTEX_MAGIC, "vertex"); if (numeric) (void) printf("%ld %ld %g\n", vcp -> v_index, vup -> v_index, bp -> b_weight); else (void) printf("%s %s %g\n", vcp -> v_label, vup -> v_label, bp -> b_weight); weight += bp -> b_weight; } free_bridge(bp); vup -> v_civilized = 1; if (debug) { bu_log("Looking for uncivilized neighbors of...\n"); print_vertex((void *) vup, 0); } while (BU_LIST_WHILE(np, neighbor, &(vup -> v_neighbors))) { BU_CKMAG(np, NEIGHBOR_MAGIC, "neighbor"); up = np -> n_vertex; BU_CKMAG(up, VERTEX_MAGIC, "vertex"); if (up -> v_civilized == 0) { BU_CKMAG(up -> v_bridge, BRIDGE_MAGIC, "bridge"); if (compare_weights(np -> n_weight, up -> v_bridge -> b_weight) < 0) { del_from_prioq(up); if (debug) { bu_log("After the deletion of bridge <x%x>...\n", up -> v_bridge); print_prioq(); } up -> v_bridge -> b_vert_civ = vup; up -> v_bridge -> b_weight = np -> n_weight; add_to_prioq((void *) up, 0); if (debug) { bu_log("Reduced bridge <x%x> weight to %g\n", up -> v_bridge, up -> v_bridge -> b_weight); print_prioq(); } } else if (debug) bu_log("bridge <x%x>'s weight of %g stands up\n", up -> v_bridge, up -> v_bridge -> b_weight); } else if (debug) bu_log("Skipping civilized neighbor <x%x>\n", up); BU_LIST_DEQUEUE(&(np -> l)); } } bu_log("MST weight: %g\n", weight); return 0; }
int ged_which(struct ged *gedp, int argc, const char *argv[]) { int i, j; struct directory *dp; struct rt_db_internal intern; struct rt_comb_internal *comb; struct _ged_id_to_names headIdName; struct _ged_id_to_names *itnp; struct _ged_id_names *inp; int isAir; int sflag; static const char *usageAir = "code(s)"; static const char *usageIds = "region_id(s)"; 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); if (BU_STR_EQUAL(argv[0], "whichair")) isAir = 1; else isAir = 0; /* must be wanting help */ if (argc == 1) { if (isAir) bu_vls_printf(gedp->ged_result_str, "Usage: %s %s", argv[0], usageAir); else bu_vls_printf(gedp->ged_result_str, "Usage: %s %s", argv[0], usageIds); return GED_HELP; } if (BU_STR_EQUAL(argv[1], "-s")) { --argc; ++argv; if (argc < 2) { if (isAir) bu_vls_printf(gedp->ged_result_str, "Usage: %s %s", argv[0], usageAir); else bu_vls_printf(gedp->ged_result_str, "Usage: %s %s", argv[0], usageIds); return GED_ERROR; } sflag = 1; } else { sflag = 0; } BU_LIST_INIT(&headIdName.l); /* Build list of id_to_names */ for (j = 1; j < argc; j++) { int n; int start, end; int range; int k; n = sscanf(argv[j], "%d%*[:-]%d", &start, &end); switch (n) { case 1: for (BU_LIST_FOR(itnp, _ged_id_to_names, &headIdName.l)) if (itnp->id == start) break; /* id not found */ if (BU_LIST_IS_HEAD(itnp, &headIdName.l)) { BU_GET(itnp, struct _ged_id_to_names); itnp->id = start; BU_LIST_INSERT(&headIdName.l, &itnp->l); BU_LIST_INIT(&itnp->headName.l); } break; case 2: if (start < end) range = end - start + 1; else if (end < start) { range = start - end + 1; start = end; } else range = 1; for (k = 0; k < range; ++k) { int id = start + k; for (BU_LIST_FOR(itnp, _ged_id_to_names, &headIdName.l)) if (itnp->id == id) break; /* id not found */ if (BU_LIST_IS_HEAD(itnp, &headIdName.l)) { BU_GET(itnp, struct _ged_id_to_names); itnp->id = id; BU_LIST_INSERT(&headIdName.l, &itnp->l); BU_LIST_INIT(&itnp->headName.l); } } break; } } /* Examine all COMB nodes */ for (i = 0; i < RT_DBNHASH; i++) { for (dp = gedp->ged_wdbp->dbip->dbi_Head[i]; dp != RT_DIR_NULL; dp = dp->d_forw) { if (!(dp->d_flags & RT_DIR_REGION)) continue; if (rt_db_get_internal(&intern, dp, gedp->ged_wdbp->dbip, (fastf_t *)NULL, &rt_uniresource) < 0) { bu_vls_printf(gedp->ged_result_str, "Database read error, aborting"); return GED_ERROR; } comb = (struct rt_comb_internal *)intern.idb_ptr; /* check to see if the region id or air code matches one in our list */ for (BU_LIST_FOR(itnp, _ged_id_to_names, &headIdName.l)) { if ((!isAir && comb->region_id == itnp->id) || (isAir && comb->aircode == itnp->id)) { /* add region name to our name list for this region */ BU_GET(inp, struct _ged_id_names); bu_vls_init(&inp->name); bu_vls_strcpy(&inp->name, dp->d_namep); BU_LIST_INSERT(&itnp->headName.l, &inp->l); break; } } rt_db_free_internal(&intern); } } /* place data in interp and free memory */ while (BU_LIST_WHILE(itnp, _ged_id_to_names, &headIdName.l)) { if (!sflag) { bu_vls_printf(gedp->ged_result_str, "Region[s] with %s %d:\n", isAir ? "air code" : "ident", itnp->id); } while (BU_LIST_WHILE(inp, _ged_id_names, &itnp->headName.l)) { if (sflag) bu_vls_printf(gedp->ged_result_str, " %V", &inp->name); else bu_vls_printf(gedp->ged_result_str, " %V\n", &inp->name); BU_LIST_DEQUEUE(&inp->l); bu_vls_free(&inp->name); BU_PUT(inp, struct _ged_id_names); } BU_LIST_DEQUEUE(&itnp->l); BU_PUT(itnp, struct _ged_id_to_names); } return GED_OK; }
int ged_rmap(struct ged *gedp, int argc, const char *argv[]) { int i; struct directory *dp; struct rt_db_internal intern; struct rt_comb_internal *comb; struct _ged_id_to_names headIdName; struct _ged_id_to_names *itnp; struct _ged_id_names *inp; 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); if (argc != 1) { bu_vls_printf(gedp->ged_result_str, "Usage: %s", argv[0]); return GED_ERROR; } if (db_version(gedp->ged_wdbp->dbip) < 5) { bu_vls_printf(gedp->ged_result_str, "%s is not available prior to version 5 of the .g file format\n", argv[0]); return GED_ERROR; } BU_LIST_INIT(&headIdName.l); /* For all regions not hidden */ for (i = 0; i < RT_DBNHASH; i++) { for (dp = gedp->ged_wdbp->dbip->dbi_Head[i]; dp != RT_DIR_NULL; dp = dp->d_forw) { int found = 0; if (!(dp->d_flags & RT_DIR_REGION) || (dp->d_flags & RT_DIR_HIDDEN)) continue; if (rt_db_get_internal(&intern, dp, gedp->ged_wdbp->dbip, (fastf_t *)NULL, &rt_uniresource) < 0) { bu_vls_printf(gedp->ged_result_str, "%s: Database read error, aborting", argv[0]); return GED_ERROR; } comb = (struct rt_comb_internal *)intern.idb_ptr; /* check to see if the region id or air code matches one in our list */ for (BU_LIST_FOR(itnp, _ged_id_to_names, &headIdName.l)) { if ((comb->region_id == itnp->id) || (comb->aircode != 0 && -comb->aircode == itnp->id)) { /* add region name to our name list for this region */ BU_GET(inp, struct _ged_id_names); bu_vls_init(&inp->name); bu_vls_strcpy(&inp->name, dp->d_namep); BU_LIST_INSERT(&itnp->headName.l, &inp->l); found = 1; break; } } if (!found) { /* create new id_to_names node */ BU_GET(itnp, struct _ged_id_to_names); if (0 < comb->region_id) itnp->id = comb->region_id; else itnp->id = -comb->aircode; BU_LIST_INSERT(&headIdName.l, &itnp->l); BU_LIST_INIT(&itnp->headName.l); /* add region name to our name list for this region */ BU_GET(inp, struct _ged_id_names); bu_vls_init(&inp->name); bu_vls_strcpy(&inp->name, dp->d_namep); BU_LIST_INSERT(&itnp->headName.l, &inp->l); } rt_db_free_internal(&intern); } } /* place data in the result string */ while (BU_LIST_WHILE(itnp, _ged_id_to_names, &headIdName.l)) { /* add this id to the list */ bu_vls_printf(gedp->ged_result_str, "%d {", itnp->id); /* start sublist of names associated with this id */ while (BU_LIST_WHILE(inp, _ged_id_names, &itnp->headName.l)) { /* add the this name to this sublist */ bu_vls_printf(gedp->ged_result_str, " %s", bu_vls_addr(&inp->name)); BU_LIST_DEQUEUE(&inp->l); bu_vls_free(&inp->name); BU_PUT(inp, struct _ged_id_names); } bu_vls_printf(gedp->ged_result_str, " } "); /* , itnp->id); */ BU_LIST_DEQUEUE(&itnp->l); BU_PUT(itnp, struct _ged_id_to_names); } return GED_OK; }