int read_point (FILE *fp, fastf_t *c_p, int c_len, int normalize, struct bu_vls *tail) { char *cp = NULL; fastf_t sum; int i; int return_code = 1; static int line_nm = 0; struct bu_vls *bp; for (bp = bu_vls_vlsinit();; bu_vls_trunc(bp, 0)) { if (bu_vls_gets(bp, fp) == -1) { return_code = EOF; goto wrap_up; } ++line_nm; cp = bu_vls_addr(bp); while ((*cp == ' ') || (*cp == '\t')) ++cp; if ((*cp == '#') || (*cp == '\0')) continue; for (i = 0; i < c_len; ++i) { char *endp; c_p[i] = strtod(cp, &endp); if (endp == cp) bu_exit (1, "Illegal input at line %d: '%s'\n", line_nm, bu_vls_addr(bp)); cp = endp; } if (normalize) { sum = 0.0; for (i = 0; i < c_len; ++i) sum += c_p[i]; for (i = 0; i < c_len; ++i) c_p[i] /= sum; } goto wrap_up; } wrap_up: if ((return_code == 1) && (tail != 0)) { bu_vls_trunc(tail, 0); bu_vls_strcat(tail, cp); } bu_vls_vlsfree(bp); return (return_code); }
/** * returns the next available/unused name, using a consistent naming * convention specific to combinations/regions and solids. * state->incr is used for each number level increase. */ static struct bu_vls * get_name(struct db_i *_dbip, struct directory *dp, struct clone_state *state, int iter) { struct bu_vls *newname; char prefix[CLONE_BUFSIZE] = {0}, suffix[CLONE_BUFSIZE] = {0}, buf[CLONE_BUFSIZE] = {0}, suffix2[CLONE_BUFSIZE] = {0}; int num = 0, i = 1, j = 0; newname = bu_vls_vlsinit(); /* Ugh. This needs much repair/cleanup. */ if (state->updpos == 0) { sscanf(dp->d_namep, "%[!-/,:-~]%d%[!-/,:-~]%512s", prefix, &num, suffix, suffix2); /* CLONE_BUFSIZE */ snprintf(suffix, CLONE_BUFSIZE, "%s", suffix2); } else if (state->updpos == 1) { int num2 = 0; sscanf(dp->d_namep, "%[!-/,:-~]%d%[!-/,:-~]%d%[!-/,:-~]", prefix, &num2, suffix2, &num, suffix); snprintf(prefix, CLONE_BUFSIZE, "%s%d%s", prefix, num2, suffix2); } else bu_exit(EXIT_FAILURE, "multiple -c options not supported yet."); do { /* choke the name back to the prefix */ bu_vls_trunc(newname, 0); bu_vls_strcpy(newname, prefix); if ((dp->d_flags & RT_DIR_SOLID) || (dp->d_flags & RT_DIR_REGION)) { /* primitives and regions */ if (suffix[0] != 0) if ((i == 1) && is_in_list(obj_list, buf)) { j = index_in_list(obj_list, buf); snprintf(buf, CLONE_BUFSIZE, "%s%d", prefix, num); /* save the name for the next pass */ /* clear and set the name */ bu_vls_trunc(newname, 0); bu_vls_printf(newname, "%V%s", obj_list.names[j].dest[iter], suffix); } else bu_vls_printf(newname, "%zu%s", num+i*state->incr, suffix); else bu_vls_printf(newname, "%zu", num + i*state->incr); } else /* non-region combinations */ bu_vls_printf(newname, "%d", (num==0)?i+1:i+num); i++; } while (db_lookup(_dbip, bu_vls_addr(newname), LOOKUP_QUIET) != NULL); return newname; }
HIDDEN void create_boxes(genptr_t callBackData, int x, int y, int z, const char *UNUSED(a), fastf_t fill) { fastf_t min[3], max[3]; struct bu_vls *vp; char bufx[50], bufy[50], bufz[50]; char *nameDestination; struct voxelizeData *dataValues = (struct voxelizeData *)callBackData; sprintf(bufx, "%d", x); sprintf(bufy, "%d", y); sprintf(bufz, "%d", z); if(dataValues->threshold <= fill) { vp = bu_vls_vlsinit(); bu_vls_strcat(vp, dataValues->newname); bu_vls_strcat(vp, ".x"); bu_vls_strcat(vp, bufx); bu_vls_strcat(vp, "y"); bu_vls_strcat(vp, bufy); bu_vls_strcat(vp, "z"); bu_vls_strcat(vp, bufz); bu_vls_strcat(vp, ".s"); min[0] = (dataValues->bbMin)[0] + (x * (dataValues->sizeVoxel)[0]); min[1] = (dataValues->bbMin)[1] + (y * (dataValues->sizeVoxel)[1]); min[2] = (dataValues->bbMin)[2] + (z * (dataValues->sizeVoxel)[2]); max[0] = (dataValues->bbMin)[0] + ( (x + 1.0) * (dataValues->sizeVoxel)[0]); max[1] = (dataValues->bbMin)[1] + ( (y + 1.0) * (dataValues->sizeVoxel)[1]); max[2] = (dataValues->bbMin)[2] + ( (z + 1.0) * (dataValues->sizeVoxel)[2]); nameDestination = bu_vls_strgrab(vp); mk_rpp(dataValues->wdbp,nameDestination, min, max); mk_addmember(nameDestination, &dataValues->content.l, 0, WMOP_UNION); } }
int ged_lc(struct ged *gedp, int argc, const char *argv[]) { char *file_name = NULL; int file_name_flag_cnt = 0; int sort_column = 1; int sort_column_flag_cnt = 0; int find_duplicates_flag = 0; int skip_special_duplicates_flag = 0; int skip_subtracted_regions_flag = 0; int descending_sort_flag = 0; int unrecognized_flag_cnt = 0; int orig_argc; const char **orig_argv; static const char *usage = "[-d|-s] [-r] [-z] [-0|-1|-2|-3|-4|-5] [-f {FileName}] {GroupName}"; int c; int error_cnt = 0; FILE *outfile = NULL; struct bu_vls *output; const char *group_name; size_t i,j; struct bu_ptbl results1 = BU_PTBL_INIT_ZERO; struct bu_ptbl results2 = BU_PTBL_INIT_ZERO; char *path; char *plan; struct db_full_path root; int matches; struct region_record *regions; size_t ignored_cnt = 0; /* The defaults are the minimum widths to accommodate column headers */ size_t region_id_len_max = 2; size_t material_id_len_max = 3; size_t los_len_max = 3; size_t obj_len_max = 6; /* For the output at the end */ size_t start, end, incr; /* initialize result */ bu_vls_trunc(gedp->ged_result_str, 0); if (argc == 1) { bu_vls_printf(gedp->ged_result_str, "Usage: %s\n", usage); return GED_HELP; } GED_CHECK_DATABASE_OPEN(gedp, GED_ERROR); GED_CHECK_ARGC_GT_0(gedp, argc, GED_ERROR); bu_optind = 1; /* re-init bu_getopt() */ while ((c = bu_getopt(argc, (char * const *)argv, "dsrz012345f:")) != -1) { switch (c) { case '0': case '1': case '2': case '3': case '4': case '5': sort_column_flag_cnt++; sort_column = c - '0'; break; case 'f': file_name_flag_cnt++; file_name = bu_optarg; break; case 's': skip_special_duplicates_flag = 1; /* FALLTHROUGH */ case 'd': find_duplicates_flag = 1; break; case 'r': skip_subtracted_regions_flag = 1; break; case 'z': descending_sort_flag = 1; break; default: unrecognized_flag_cnt++; } } orig_argc = argc; orig_argv = argv; argc -= (bu_optind - 1); argv += (bu_optind - 1); /* Attempt to recreate the exact error messages from the original lc.tcl */ if (file_name_flag_cnt > 1) { bu_vls_printf(gedp->ged_result_str, "Error: '-f' used more than once.\n"); error_cnt++; } if (sort_column_flag_cnt > 1) { bu_vls_printf(gedp->ged_result_str, "Error: Sort column defined more than once.\n"); error_cnt++; } if (file_name_flag_cnt + argc + unrecognized_flag_cnt > 3) { bu_vls_printf(gedp->ged_result_str, "Error: More than one group name and/or file name was specified.\n"); error_cnt++; } else if (argc < 2) { if (file_name_flag_cnt && !file_name) { bu_vls_printf(gedp->ged_result_str, "Error: Group name and file name not specified\n"); } else { bu_vls_printf(gedp->ged_result_str, "Error: Group name not specified.\n"); } error_cnt++; } else if (argc + unrecognized_flag_cnt > 2) { bu_vls_printf(gedp->ged_result_str, "Error: More than one group name was specified.\n"); error_cnt++; } else if (file_name_flag_cnt && !file_name) { bu_vls_printf(gedp->ged_result_str, "Error: File name not specified.\n"); error_cnt++; } if (file_name) { char *norm_name; norm_name = bu_realpath(file_name, NULL); if (file_name[0] == '-') { bu_vls_printf(gedp->ged_result_str, "Error: File name can not start with '-'.\n"); error_cnt++; } else if (bu_file_exists(file_name, NULL)) { bu_vls_printf(gedp->ged_result_str, "Error: Output file %s already exists.\n",norm_name); error_cnt++; } else { outfile = fopen(file_name, "w"); if (!outfile) { bu_vls_printf(gedp->ged_result_str, "Error: %d\n", errno); error_cnt++; } } bu_vls_printf(gedp->ged_result_str, "Output filename: %s\n", norm_name); bu_free(norm_name, "ged_lc"); output = bu_vls_vlsinit(); } else { output = gedp->ged_result_str; } if (error_cnt > 0) { return GED_ERROR; } group_name = argv[1]; /* The 7 is for the "-name" and '\0' */ plan = (char *) bu_malloc(sizeof(char) * (strlen(group_name) + 7), "ged_lc"); sprintf(plan, "-name %s", group_name); matches = db_search(&results1, DB_SEARCH_TREE, plan, 0, NULL, gedp->ged_wdbp->dbip); if (matches < 1) { bu_vls_printf(gedp->ged_result_str, "Error: Group '%s' does not exist.\n", group_name); return GED_ERROR; } bu_free(plan, "ged_lc"); db_search_free(&results1); if (skip_subtracted_regions_flag) { plan = "-type region ! -bool -"; } else { plan = "-type region"; } path = (char *) bu_malloc(sizeof(char) * (strlen(group_name) + 2), "ged_lc"); sprintf(path, "/%s", group_name); db_string_to_path(&root, gedp->ged_wdbp->dbip, path); matches = db_search(&results2, DB_SEARCH_TREE, plan, root.fp_len, root.fp_names, gedp->ged_wdbp->dbip); bu_free(path, "ged_lc"); if (matches < 1) { return GED_ERROR; } regions = (struct region_record *) bu_malloc(sizeof(struct region_record) * BU_PTBL_LEN(&results2), "ged_lc"); for (i = 0; i < BU_PTBL_LEN(&results2); i++) { struct db_full_path *entry = (struct db_full_path *)BU_PTBL_GET(&results2, i); struct directory *dp_curr_dir = DB_FULL_PATH_CUR_DIR(entry); struct bu_attribute_value_set avs; j = BU_PTBL_LEN(&results2) - i - 1 ; regions[j].ignore = 0; bu_avs_init_empty(&avs); db5_get_attributes(gedp->ged_wdbp->dbip, &avs, dp_curr_dir); regions[j].region_id = get_attr(&avs, "region_id"); V_MAX(region_id_len_max, strlen(regions[j].region_id)); regions[j].material_id = get_attr(&avs, "material_id"); V_MAX(material_id_len_max, strlen(regions[j].material_id)); regions[j].los = get_attr(&avs, "los"); V_MAX(los_len_max, strlen(regions[j].los)); regions[j].obj_name = dp_curr_dir->d_namep; V_MAX(obj_len_max, strlen(regions[j].obj_name)); if (entry->fp_len > 1) { struct directory *dp_parent = DB_FULL_PATH_GET(entry, entry->fp_len - 2); regions[j].obj_parent = dp_parent->d_namep; } else { regions[j].obj_parent = "--"; } } if (find_duplicates_flag) { bu_sort((void *) regions, BU_PTBL_LEN(&results2), sizeof(struct region_record), cmp_regions, NULL); for (i = 1; i < BU_PTBL_LEN(&results2); i++) { int same; if (skip_special_duplicates_flag) { same = !cmp_regions((void *)&(regions[i - 1]), (void *)&(regions[i]), NULL); } else { same = !bu_strcmp(regions[i - 1].region_id, regions[i].region_id); } if (same) { regions[i].ignore = 1; ignored_cnt++; } } if (ignored_cnt == BU_PTBL_LEN(&results2)) { if (file_name) { print_cmd_args(output, orig_argc, orig_argv); bu_vls_printf(output, "No duplicate region_id\n"); bu_vls_fwrite(outfile, output); fclose(outfile); } bu_vls_printf(gedp->ged_result_str, "No duplicate region_id\n"); bu_vls_printf(gedp->ged_result_str, "Done.\n"); bu_free(regions, "ged_lc"); return GED_ERROR; } } if (sort_column > 0) { bu_sort((void *) regions, BU_PTBL_LEN(&results2), sizeof(struct region_record), sort_regions, (void *)&sort_column); } if (file_name) { print_cmd_args(output, orig_argc, orig_argv); } bu_vls_printf(output, "List length: %d\n", BU_PTBL_LEN(&results2) - ignored_cnt); bu_vls_printf(output, "%-*s %-*s %-*s %-*s %s\n", region_id_len_max + 1, "ID", material_id_len_max + 1, "MAT", los_len_max, "LOS", obj_len_max, "REGION", "PARENT"); end = BU_PTBL_LEN(&results2); if (descending_sort_flag) { start = end - 1; end = -1; incr = -1; } else { start = 0; incr = 1; } for (i = start; i != end; i += incr) { if (regions[i].ignore) { continue; } bu_vls_printf(output, "%-*s %-*s %-*s %-*s %s\n", region_id_len_max + 1, regions[i].region_id, material_id_len_max + 1, regions[i].material_id, los_len_max, regions[i].los, obj_len_max, regions[i].obj_name, regions[i].obj_parent); } bu_vls_printf(gedp->ged_result_str, "Done.\n"); if (file_name) { bu_vls_fwrite(outfile, output); fclose(outfile); } bu_free(regions, "ged_lc"); return GED_OK; }
int main (int argc, char **argv) { char *inf_name; int ch; int i; int nm_sites; int normalize = 0; /* Make all weights sum to one? */ fastf_t *coeff; fastf_t x, y, z; FILE *infp; struct bu_list site_list; struct bu_vls *tail_buf = 0; struct site *sp; BU_LIST_INIT(&site_list); while ((ch = bu_getopt(argc, argv, OPT_STRING)) != EOF) switch (ch) { case 'n': normalize = 1; break; case 's': if (sscanf(bu_optarg, "%lf %lf %lf", &x, &y, &z) != 3) { bu_log("Illegal site: '%s'\n", bu_optarg); print_usage(); } enqueue_site(&site_list, x, y, z); break; case 't': if (tail_buf == 0) /* Only initialize it once */ tail_buf = bu_vls_vlsinit(); break; case '?': default: print_usage(); } switch (argc - bu_optind) { case 0: inf_name = "stdin"; infp = stdin; break; case 1: inf_name = argv[bu_optind++]; if ((infp = fopen(inf_name, "r")) == NULL) bu_exit (1, "Cannot open file '%s'\n", inf_name); break; default: print_usage(); } if (BU_LIST_IS_EMPTY(&site_list)) { enqueue_site(&site_list, (fastf_t) 1.0, (fastf_t) 0.0, (fastf_t) 0.0); enqueue_site(&site_list, (fastf_t) 0.0, (fastf_t) 1.0, (fastf_t) 0.0); enqueue_site(&site_list, (fastf_t) 0.0, (fastf_t) 0.0, (fastf_t) 1.0); } nm_sites = 0; for (BU_LIST_FOR(sp, site, &site_list)) ++nm_sites; coeff = (fastf_t *) bu_malloc(nm_sites * sizeof(fastf_t), "coefficient array"); while (read_point(infp, coeff, nm_sites, normalize, tail_buf) != EOF) { x = y = z = 0.0; i = 0; for (BU_LIST_FOR(sp, site, &site_list)) { x += sp -> s_x * coeff[i]; y += sp -> s_y * coeff[i]; z += sp -> s_z * coeff[i]; ++i; } bu_flog(stdout, "%g %g %g", x, y, z); if (tail_buf) bu_flog(stdout, "%s", bu_vls_addr(tail_buf)); bu_flog(stdout, "\n"); } return 0; }
void dump_bitv(const struct bu_bitv *b) { const size_t len = b->nbits; size_t x; size_t i, j, k; size_t bit, ipad = 0, jpad = 0; size_t word_count; size_t chunksize = 0; volatile size_t BVS = sizeof(bitv_t); /* should be 1 byte as defined in bu.h */ size_t bytes; struct bu_vls *v = bu_vls_vlsinit(); bytes = len / BITS_PER_BYTE; /* eight digits per byte */ word_count = bytes / BVS; chunksize = bytes % BVS; if (chunksize == 0) { chunksize = BVS; } else { /* handle partial chunk before using chunksize == BVS */ word_count++; } while (word_count--) { while (chunksize--) { /* get the appropriate bits in the bit vector */ unsigned long lval = (unsigned long)((b->bits[word_count] & ((bitv_t)(0xff)<<(chunksize * BITS_PER_BYTE))) >> (chunksize * BITS_PER_BYTE)) & (bitv_t)0xff; /* get next eight binary digits from bitv */ int ii; for (ii = 7; ii >= 0; --ii) { unsigned long uval = lval >> ii & 0x1; bu_vls_printf(v, "%1lx", uval); } } chunksize = BVS; } /* print out one set of data per X bits */ x = 16; /* X number of bits per line */ x = x > len ? len : x; /* we want even output lines (lengths in multiples of BITS_PER_BYTE) */ if (len % x) { ipad = jpad = x - (len % x); }; if (ipad > 0) i = j = -ipad; else i = j = 0; bit = len - 1; bu_log("\n====================================================================="); bu_log("\nbitv dump:"); bu_log("\n nbits = %zu", len); NEXT: k = i + x; bu_log("\n---------------------------------------------------------------------"); bu_log("\n bit "); for (; i < k; ++i, --ipad) { if (ipad > 0) bu_log(" %3s", " "); else bu_log(" %3d", bit--); } bu_log("\n val "); /* the actual values are a little tricky due to the actual structure and number of words */ for (; j < k; ++j, --jpad) { if (jpad > 0) bu_log(" %3s", " "); else bu_log(" %c", bu_vls_cstr(v)[j]); } if (i < len - 1) { goto NEXT; } bu_log("\n---------------------------------------------------------------------"); bu_log("\n====================================================================="); bu_vls_free(v); }