int write_Octree(Octree *parentp, FILE *fp) { PtList *ptp; F_Hdr_Ptlist hdr_ptlist; off_t addr = bu_ftell( fp ); if (addr < 0) { bu_log("Error: couldn't get input file's current file position.\n"); return 0; } /* Write temperature and bogus number of points for this leaf. */ hdr_ptlist.f_temp = parentp->o_temp; hdr_ptlist.f_length = 0; if ( fwrite( (char *) &hdr_ptlist, sizeof(F_Hdr_Ptlist), 1, fp ) != 1 ) { bu_log( "\"%s\"(%d) Write failed!\n", __FILE__, __LINE__ ); return 0; } /* Write out list of points. */ for ( ptp = parentp->o_points->c_next; ptp != PTLIST_NULL; ptp = ptp->c_next ) { hdr_ptlist.f_length++; if ( fwrite( (char *) ptp->c_point, sizeof(ptp->c_point), 1, fp ) != 1 ) { bu_log( "\"%s\"(%d) Write failed.\n", __FILE__, __LINE__ ); return 0; } } if ( hdr_ptlist.f_length > 0 ) { /* Go back and fudge point count. */ if ( bu_fseek( fp, addr, 0 ) ) { bu_log( "\"%s\"(%d) Fseek failed.\n", __FILE__, __LINE__ ); return 0; } if ( fwrite( (char *) &hdr_ptlist, sizeof(hdr_ptlist), 1, fp ) != 1 ) { bu_log( "\"%s\"(%d) Write failed!\n", __FILE__, __LINE__ ); return 0; } /* Re-position write pointer to end-of-file. */ if ( bu_fseek( fp, 0, 2 ) ) { bu_log( "\"%s\"(%d) Fseek failed.\n", __FILE__, __LINE__ ); return 0; } } return 1; }
static int get_IR(int x, int y, int *fahp, FILE *fp) { if (bu_fseek(fp, (y*IR_DATA_WID + x)*sizeof(int), 0) != 0) return 0; else if (fread((char *) fahp, (int) sizeof(int), 1, fp) != 1) return 0; else return 1; }
/* * Load the buffer with scan lines centered around * the given y coordinate. */ void fill_buffer(int y) { size_t ret; buf_start = y - buflines/2; if (buf_start < 0) buf_start = 0; bu_fseek(ifp, buf_start * scanlen, 0); ret = fread(buffer, scanlen, buflines, ifp); if (ret == 0) perror("fread"); }
void do_line(int xpos, int ypos, char *line) { int currx; int char_count, char_id; int len = strlen( line ); if ( font.ffdes == NULL ) { bu_log( "ERROR: do_line() called before get_font().\n" ); return; } currx = xpos; for ( char_count = 0; char_count < len; char_count++ ) { char_id = (int) line[char_count] & 0377; /* Since there is no valid space in font, skip to the right using the width of the digit zero. */ if ( char_id == ' ' ) { currx += (SWABV(font.dir['0'].width) + 2) / ir_aperture; continue; } /* locate the bitmap for the character in the file */ if ( bu_fseek( font.ffdes, SWABV(font.dir[char_id].addr)+font.offset, 0 ) == EOF ) { bu_log( "fseek() to %zd failed.\n", (SWABV(font.dir[char_id].addr) + font.offset) ); return; } /* Read in the dimensions for the character */ font.width = font.dir[char_id].right + font.dir[char_id].left; font.height = font.dir[char_id].up + font.dir[char_id].down; if ( currx + font.width > fb_getwidth( fbiop ) - 1 ) break; /* won't fit on screen */ do_char( char_id, currx, ypos ); currx += (SWABV(font.dir[char_id].width) + 2) / ir_aperture; } return; }
/* * Load the buffer with scan lines centered around * the given y coordinate. */ static void fill_buffer(int y) { size_t ret; buf_start = y - buflines/2; if (buf_start < 0) buf_start = 0; if (bu_fseek(buffp, buf_start * scanlen, 0) < 0) { fprintf(stderr, "bwscale: Can't seek to input pixel!\n"); /* bu_exit (3, NULL); */ } ret = fread(buffer, scanlen, buflines, buffp); if (ret != (size_t)buflines) perror("fread"); }
HIDDEN void reset_input(void) { int i; char *tmp; for (i=0; i < 20; i++) prev_rec[i][0] = '\0'; bu_fseek(fpin, start_off, SEEK_SET); line_count = bulk_data_start_line; tmp = bu_fgets(next_line, MAX_LINE_SIZE, fpin); while (tmp && *tmp == '$') tmp = bu_fgets(next_line, MAX_LINE_SIZE, fpin); if (tmp != (char *)NULL) input_status = INPUT_OK; else input_status = INPUT_NULL; }
static int db_corrupt_handler(struct db_i *dbip, const char *name, off_t offset, size_t size, int UNUSED(type), void *data) { struct counter *cnt = (struct counter *)data; struct bu_external ext; union record *rp = NULL; mat_t diskmat = MAT_INIT_ZERO; size_t nodecount; size_t j; int ret; RT_CK_DBI(dbip); /* get into position */ ret = bu_fseek(dbip->dbi_fp, offset, 0); if (ret) { bu_log("Database seek failure, unable to seek [%s]\n", name); return 0; } /* set up our buffer */ BU_EXTERNAL_INIT(&ext); ext.ext_nbytes = size * sizeof(union record); ext.ext_buf = (uint8_t *)bu_calloc(ext.ext_nbytes, 1, "db_corrupt"); /* read into the buffer */ j = (size_t)fread(ext.ext_buf, 1, ext.ext_nbytes, dbip->dbi_fp); if (j != ext.ext_nbytes) { bu_log("Database read failure, unable to read [%s]\n", name); bu_free_external(&ext); return 0; } rp = (union record *)ext.ext_buf; if (rp[0].u_id != ID_COMB) { /* skip non-combination */ bu_free_external(&ext); return 0; } /* iterate over combination members, looking for bad matrices */ nodecount = ext.ext_nbytes/sizeof(union record) - 1; for (j = 0; j < nodecount; j++) { /* try without flipping */ flip_mat_dbmat(diskmat, rp[j+1].M.m_mat, 0); if ((bn_mat_ck(name, diskmat) < 0) || fabs(diskmat[0]) > 1 || fabs(diskmat[1]) > 1 || fabs(diskmat[2]) > 1 || fabs(diskmat[4]) > 1 || fabs(diskmat[5]) > 1 || fabs(diskmat[6]) > 1 || fabs(diskmat[8]) > 1 || fabs(diskmat[9]) > 1 || fabs(diskmat[10]) > 1) { /* corruption detected */ cnt->found++; /* invalid, so try flipped */ flip_mat_dbmat(diskmat, rp[j+1].M.m_mat, 1); if ((bn_mat_ck(name, diskmat) < 0) || fabs(diskmat[0]) > 1 || fabs(diskmat[1]) > 1 || fabs(diskmat[2]) > 1 || fabs(diskmat[4]) > 1 || fabs(diskmat[5]) > 1 || fabs(diskmat[6]) > 1 || fabs(diskmat[8]) > 1 || fabs(diskmat[9]) > 1 || fabs(diskmat[10]) > 1) { bu_log("WARNING: Invalid matrix detected within %s\n", name); } else { /* flipping helped */ cnt->fixed++; } break; } } bu_free_external(&ext); return 0; }
int icv_rot(size_t argc, const char *argv[]) { const size_t MAXPIXELS = 16768 * 16768; /* boo hiss */ ssize_t x, y; size_t j; int ret = 0; off_t outbyte, outplace; FILE *ifp, *ofp; unsigned char *obuf; unsigned char *buffer; double angle = 0.0; ssize_t wrote = 0; ifp = stdin; ofp = stdout; bu_setprogname(argv[0]); if (!get_args(argc, argv, &ifp, &ofp, &angle)) { bu_exit(1, "Usage: %s [-rifb | -a angle] [-# bytes] [-s squaresize] [-w width] [-n height] [-o outputfile] inputfile [> outputfile]\n", argv[0]); } scanbytes = nxin * pixbytes; buflines = MAXPIXELS / nxin; if (buflines <= 0) { bu_exit(1, "ERROR: %s is not compiled to handle a scanline that long!\n", argv[0]); } if (buflines > nyin) buflines = nyin; buffer = (unsigned char *)bu_malloc(buflines * scanbytes, "buffer"); obuf = (unsigned char *)bu_malloc((nyin > nxin) ? nyin*pixbytes : nxin*pixbytes, "obuf"); /* * Break out to added arbitrary angle routine */ if (angle > 0.0) { arbrot(angle, ifp, buffer); goto done; } /* * Clear our "file pointer." We need to maintain this * In order to tell if seeking is required. ftell() always * fails on pipes, so we can't use it. */ outplace = 0; yin = 0; while (yin < nyin) { /* Fill buffer */ fill_buffer(ifp, buffer); if (!buflines) break; if (reverse) reverse_buffer(buffer); if (plus90) { for (x = 0; x < nxin; x++) { obp = obuf; bp = &buffer[ (lasty-firsty)*scanbytes + x*pixbytes ]; for (y = lasty+1; y > yin; y--) { /* firsty? */ for (j = 0; j < pixbytes; j++) *obp++ = *bp++; bp -= scanbytes + pixbytes; } yout = x; xout = (nyin - 1) - lasty; outbyte = ((yout * nyin) + xout) * pixbytes; if (outplace != outbyte) { if (bu_fseek(ofp, outbyte, SEEK_SET) < 0) { ret = 3; perror("fseek"); bu_log("ERROR: %s can't seek on output (ofp=%p, outbyte=%zd)\n", argv[0], (void *)ofp, outbyte); goto done; } outplace = outbyte; } wrote = fwrite(obuf, pixbytes, buflines, ofp); if (wrote != buflines) { ret = 4; perror("fwrite"); bu_log("ERROR: %s can't out write image data (wrote %zd of %zd)\n", argv[0], wrote, buflines); goto done; } outplace += buflines*pixbytes; } } else if (minus90) { for (x = nxin; x > 0; x--) { obp = obuf; bp = &buffer[ (x-1)*pixbytes ]; for (y = firsty+1; (ssize_t)y < lasty; y++) { for (j = 0; j < pixbytes; j++) *obp++ = *bp++; bp += scanbytes - pixbytes; } yout = (nxin - 1) - x + 1; xout = yin; outbyte = ((yout * nyin) + xout) * pixbytes; if (outplace != outbyte) { if (bu_fseek(ofp, outbyte, SEEK_SET) < 0) { ret = 3; perror("fseek"); bu_log("ERROR: %s can't seek on output (ofp=%p, outbyte=%zd)\n", argv[0], (void *)ofp, outbyte); goto done; } outplace = outbyte; } wrote = fwrite(obuf, pixbytes, buflines, ofp); if (wrote != buflines) { ret = 4; perror("fwrite"); bu_log("ERROR: %s can't out write image data (wrote %zd of %zd)\n", argv[0], wrote, buflines); goto done; } outplace += buflines*pixbytes; } } else if (invert) { for (y = lasty+1; (ssize_t)y > firsty; y--) { yout = (nyin - 1) - y + 1; outbyte = yout * scanbytes; if (outplace != outbyte) { if (bu_fseek(ofp, outbyte, SEEK_SET) < 0) { ret = 3; perror("fseek"); bu_log("ERROR: %s can't seek on output (ofp=%p, outbyte=%zd)\n", argv[0], (void *)ofp, outbyte); goto done; } outplace = outbyte; } wrote = fwrite(&buffer[(y-firsty-1)*scanbytes], 1, scanbytes, ofp); if (wrote != scanbytes) { ret = 4; perror("fwrite"); bu_log("ERROR: %s can't out write image data (wrote %zd of %zd)\n", argv[0], wrote, scanbytes); goto done; } outplace += scanbytes; } } else { /* Reverse only */ for (y = 0; y < buflines; y++) { wrote = fwrite(&buffer[y*scanbytes], 1, scanbytes, ofp); if (wrote != scanbytes) { ret = 4; perror("fwrite"); bu_log("ERROR: %s can't out write image data (wrote %zd of %zd)\n", argv[0], wrote, scanbytes); goto done; } } } yin += buflines; } done: fclose(ifp); bu_free(buffer, "buffer"); bu_free(obuf, "obuf"); return ret; }
/* * Called from db_walk_tree(). * * This routine must be prepared to run in parallel. */ union tree * do_nmg_region_end(struct db_tree_state *tsp, const struct db_full_path *pathp, union tree *curtree, void *UNUSED(client_data)) { union tree *result; struct nmgregion *r; struct bu_list vhead; struct directory *dp; int dependent; size_t i; RT_CK_TESS_TOL(tsp->ts_ttol); BN_CK_TOL(tsp->ts_tol); NMG_CK_MODEL(*tsp->ts_m); BU_LIST_INIT(&vhead); if (RT_G_DEBUG&DEBUG_TREEWALK || verbose) { char *sofar = db_path_to_string(pathp); bu_log("\ndo_nmg_region_end(%d %d%%) %s\n", regions_tried, regions_tried>0 ? (regions_done * 100) / regions_tried : 0, sofar); bu_free(sofar, "path string"); } if (curtree->tr_op == OP_NOP) return curtree; regions_tried++; if (verbose) bu_log("\ndoing boolean tree evaluate...\n"); /* evaluate the boolean */ result = process_boolean(tsp, curtree, pathp); if (result) r = result->tr_d.td_r; else r = (struct nmgregion *)NULL; if (verbose) bu_log("\nfinished boolean tree evaluate...\n"); regions_done++; if (r != NULL) { dp = DB_FULL_PATH_CUR_DIR(pathp); if (multi_file) { /* Open the output file */ if (output_file == NULL) fp_dir = stdout; else { char *multi_name; size_t len; int unique = 0; char suffix[SUFFIX_LEN+1]; /* construct a unique file name */ len = strlen(output_file) + strlen(dp->d_namep) + 6 + SUFFIX_LEN; multi_name = (char *)bu_malloc(sizeof(char)*len, "multi_name"); snprintf(multi_name, len, "%s/%s.igs", output_file, dp->d_namep); bu_strlcpy(suffix, "a", sizeof(suffix)); suffix[0]--; while (!unique) { if (bu_file_readable(multi_name)) { unique = 1; break; } /* not unique, try adding a suffix */ len = strlen(suffix); i = len - 1; suffix[i]++; while (suffix[i] > 'z' && i > 0) { suffix[i] = 'a'; i--; suffix[i]++; } if (suffix[0] > 'z' && len < SUFFIX_LEN) { for (i = 0; i <= len; i++) suffix[i] = 'a'; } else if (suffix[0] > 'z' && len >= SUFFIX_LEN) { bu_log("too many files with the same name (%s)\n", dp->d_namep); bu_exit(1, "Cannot create a unique filename, \n"); } snprintf(multi_name, len, "%s/%s%s.igs", output_file, dp->d_namep, suffix); } if ((fp_dir = fopen(multi_name, "wb")) == NULL) { perror("g-iges"); bu_exit(1, "Cannot open output file: %s\n", multi_name); } } /* Open the temporary file for the parameter section */ if ((fp_param = bu_temp_file(NULL, 0)) == NULL) { perror("g-iges"); bu_exit(1, "Cannot open temporary file\n"); } /* let the IGES routines know the selected tolerances and the database pointer */ iges_init(&tol, &ttol, verbose, DBIP); /* Write start and global sections of the IGES file */ w_start_global(fp_dir, fp_param, db_name, prog_name, output_file, __DATE__, brlcad_version()); } if (mode == FACET_MODE) { dependent = 1; for (i = 0; i < no_of_indeps; i++) { if (!bu_strncmp(dp->d_namep, independent[i], NAMESIZE+1)) { dependent = 0; break; } } dp->d_uses = (-nmgregion_to_iges(dp->d_namep, r, dependent, fp_dir, fp_param)); } else if (mode == TRIMMED_SURF_MODE) dp->d_uses = (-nmgregion_to_tsurf(dp->d_namep, r, fp_dir, fp_param)); /* NMG region is no longer necessary */ nmg_kr(r); if (multi_file) { char copy_buffer[CP_BUF_SIZE] = {0}; /* Copy the parameter section from the temporary file to the output file */ if ((bu_fseek(fp_param, 0, 0))) { perror("g-iges"); bu_exit(1, "Cannot seek to start of temporary file\n"); } while ((i = fread(copy_buffer, 1, CP_BUF_SIZE, fp_param))) if (fwrite(copy_buffer, 1, i, fp_dir) != i) { perror("g-iges"); bu_exit(1, "Error in copying parameter data to %s\n", output_file); } /* Write the terminate section */ w_terminate(fp_dir); fclose(fp_dir); fclose(fp_param); } } /* * Dispose of original tree, so that all associated dynamic * memory is released now, not at the end of all regions. * A return of TREE_NULL from this routine signals an error, * so we need to cons up an OP_NOP node to return. */ db_free_tree(curtree, &rt_uniresource); /* Does an nmg_kr() */ BU_ALLOC(curtree, union tree); RT_TREE_INIT(curtree); curtree->tr_op = OP_NOP; return curtree; }
int main(int argc, char *argv[]) { size_t i; int ret; int c; double percent; char copy_buffer[CP_BUF_SIZE] = {0}; struct directory *dp; bu_setprogname(argv[0]); bu_setlinebuf(stderr); bu_log("%s", brlcad_ident("BRL-CAD to IGES Translator")); bu_log("Please direct bug reports to <*****@*****.**>\n\n"); tree_state = rt_initial_tree_state; /* struct copy */ tree_state.ts_tol = &tol; tree_state.ts_ttol = &ttol; tree_state.ts_m = &the_model; ttol.magic = RT_TESS_TOL_MAGIC; /* Defaults, updated by command line options. */ ttol.abs = 0.0; ttol.rel = 0.01; ttol.norm = 0.0; /* FIXME: These need to be improved */ tol.magic = BN_TOL_MAGIC; tol.dist = 0.0005; tol.dist_sq = tol.dist * tol.dist; tol.perp = 1e-6; tol.para = 1 - tol.perp; the_model = nmg_mm(); BU_LIST_INIT(&RTG.rtg_vlfree); /* for vlist macros */ rt_init_resource(&rt_uniresource, 0, NULL); prog_name = argv[0]; /* Get command line arguments. */ while ((c = bu_getopt(argc, argv, "ftsmd:a:n:o:p:r:vx:P:X:")) != -1) { switch (c) { case 'f': /* Select facetized output */ mode = FACET_MODE; multi_file = 0; break; case 't': mode = TRIMMED_SURF_MODE; multi_file = 0; break; case 'm': /* multi-file mode */ multi_file = 1; mode = TRIMMED_SURF_MODE; break; case 's': /* Select NURB output */ do_nurbs = 1; break; case 'v': verbose++; break; case 'a': /* Absolute tolerance. */ ttol.abs = atof(bu_optarg); break; case 'r': /* Relative tolerance. */ ttol.rel = atof(bu_optarg); break; case 'n': /* Surface normal tolerance. */ ttol.norm = atof(bu_optarg); break; case 'd': /* distance tolerance */ tol.dist = atof(bu_optarg); tol.dist_sq = tol.dist * tol.dist; break; case 'x': sscanf(bu_optarg, "%x", (unsigned int *)&RTG.debug); break; case 'X': sscanf(bu_optarg, "%x", (unsigned int *)&RTG.NMG_debug); NMG_debug = RTG.NMG_debug; break; case 'o': /* Output file name. */ output_file = bu_optarg; break; case 'P': ncpu = atoi(bu_optarg); break; default: usage(argv[0]); break; } } if (bu_optind+1 >= argc) { usage(argv[0]); } /* Open BRL-CAD database */ argc -= bu_optind; argv += bu_optind; db_name = argv[0]; if ((DBIP = db_open(db_name, DB_OPEN_READONLY)) == DBI_NULL) { perror("g-iges"); bu_exit(1, "ERROR: unable to open geometry database file (%s)\n", db_name); } /* Scan the database */ if (db_dirbuild(DBIP)) { bu_exit(1, "db_dirbuild failed\n"); } if (!multi_file) { /* let the IGES routines know the selected tolerances and the database pointer */ iges_init(&tol, &ttol, verbose, DBIP); /* Open the output file */ if (output_file == NULL) fp_dir = stdout; else { if ((fp_dir = fopen(output_file, "wb")) == NULL) { perror(output_file); bu_exit(1, "Cannot open output file: %s\n", output_file); } } /* Open the temporary file for the parameter section */ if ((fp_param = bu_temp_file(NULL, 0)) == NULL) { perror("g-iges"); bu_exit(1, "Cannot open temporary file\n"); } /* Write start and global sections of the IGES file */ w_start_global(fp_dir, fp_param, argv[0], prog_name, output_file, __DATE__, brlcad_version()); } else { if (!bu_file_directory(output_file)) { bu_exit(1, "-o option must provide a directory, %s is not a directory\n", output_file); } } /* Count object references */ /* for (i = 1; i < argc; i++) { dp = db_lookup(DBIP, argv[i], 1); db_functree(DBIP, dp, count_refs, 0, NULL); } */ /* tree tops must have independent status, so we need to remember them */ independent = (argv+1); no_of_indeps = (size_t)argc-1; if (mode == FACET_MODE) { /* Walk indicated tree(s). Each region will be output * as a single manifold solid BREP object */ ret = db_walk_tree(DBIP, argc-1, (const char **)(argv+1), ncpu, &tree_state, 0, /* take all regions */ do_nmg_region_end, nmg_booltree_leaf_tess, (void *)NULL); /* in librt/nmg_bool.c */ if (ret) bu_exit(1, "g-iges: Could not facetize anything!"); if (!multi_file) { /* Now walk the same trees again, but only output groups */ for (i = 1; i < (size_t)argc; i++) { char *ptr; ptr = strrchr(argv[i], '/'); if (ptr != NULL) { ptr++; } else { ptr = argv[i]; } dp = db_lookup(DBIP, ptr, 1); if (!dp) { bu_log("WARNING: Unable to locate %s in %s\n, skipping\n", ptr, db_name); continue; } db_functree(DBIP, dp, csg_comb_func, 0, &rt_uniresource, NULL); } } } else if (mode == CSG_MODE) { /* Walk indicated tree(s). Each combination and solid will be output * as a CSG object, unless there is no IGES equivalent (then the * solid will be tessellated and output as a BREP object) */ for (i = 1; i < (size_t)argc; i++) { dp = db_lookup(DBIP, argv[i], 1); if (!dp) { bu_log("WARNING: Unable to locate %s in %s\n, skipping\n", argv[i], db_name); continue; } db_functree(DBIP, dp, csg_comb_func, csg_leaf_func, &rt_uniresource, NULL); } } else if (mode == TRIMMED_SURF_MODE) { /* Walk the indicated tree(s). Each region is output as a collection * of trimmed NURBS */ ret = db_walk_tree(DBIP, argc-1, (const char **)(argv+1), ncpu, &tree_state, 0, /* take all regions */ do_nmg_region_end, nmg_booltree_leaf_tess, (void *)NULL); /* in librt/nmg_bool.c */ if (ret) bu_exit(1, "g-iges: Could not facetize anything!"); } if (!multi_file) { /* Copy the parameter section from the temporary file to the output file */ if ((bu_fseek(fp_param, 0, 0))) { perror("g-iges"); bu_exit(1, "Cannot seek to start of temporary file\n"); } while ((i = fread(copy_buffer, 1, CP_BUF_SIZE, fp_param))) if (fwrite(copy_buffer, 1, i, fp_dir) != i) { perror("g-iges"); bu_exit(1, "Error in copying parameter data to %s\n", output_file); } /* Write the terminate section */ w_terminate(fp_dir); } /* Print some statistics */ Print_stats(stdout); /* report on the success rate for facetizing regions */ if (mode == FACET_MODE || mode == TRIMMED_SURF_MODE) { percent = 0; if (regions_tried>0) percent = ((double)regions_done * 100) / regions_tried; bu_log("Tried %d regions, %d converted to nmg's successfully. %g%%\n", regions_tried, regions_done, percent); } /* re-iterate warnings */ if (scale_error || solid_error || comb_error) bu_log("WARNING: the IGES file produced has errors:\n"); if (scale_error) bu_log("\t%d scaled objects found, written to IGES file without being scaled\n", scale_error); if (solid_error) bu_log("\t%d solids were not converted to IGES format\n", solid_error); if (comb_error) bu_log("\t%d combinations were not converted to IGES format\n", comb_error); return 0; }
int main(int argc, char **argv) { int c; int i; struct pshell *psh; struct pbar *pbp; struct wmember head; struct wmember all_head; char *nastran_file = "Converted from NASTRAN file (stdin)"; bu_setprogname(argv[0]); fpin = stdin; units = INCHES; /* FIXME: These need to be improved */ tol.magic = BN_TOL_MAGIC; tol.dist = 0.0005; tol.dist_sq = tol.dist * tol.dist; tol.perp = 1e-6; tol.para = 1 - tol.perp; while ((c=bu_getopt(argc, argv, "x:X:t:ni:o:mh?")) != -1) { switch (c) { case 'x': sscanf(bu_optarg, "%x", (unsigned int *)&RTG.debug); bu_printb("librt RT_G_DEBUG", RT_G_DEBUG, DEBUG_FORMAT); bu_log("\n"); break; case 'X': sscanf(bu_optarg, "%x", (unsigned int *)&RTG.NMG_debug); bu_printb("librt RTG.NMG_debug", RTG.NMG_debug, NMG_DEBUG_FORMAT); bu_log("\n"); break; case 't': /* calculational tolerance */ tol.dist = atof(bu_optarg); tol.dist_sq = tol.dist * tol.dist; break; case 'n': polysolids = 0; break; case 'm': units = MM; break; case 'i': fpin = fopen(bu_optarg, "rb"); if (fpin == (FILE *)NULL) { bu_log("Cannot open NASTRAN file (%s) for reading!\n", bu_optarg); bu_exit(1, Usage, argv[0]); } nastran_file = bu_optarg; break; case 'o': output_file = bu_optarg; break; default: bu_exit(1, Usage, argv[0]); } } fpout = wdb_fopen(output_file); if (fpout == NULL) { bu_log("Cannot open BRL-CAD file (%s) for writing!\n", output_file); bu_exit(1, Usage, argv[0]); } if (!fpin || !fpout) { bu_exit(1, Usage, argv[0]); } line = (char *)bu_malloc(MAX_LINE_SIZE, "line"); next_line = (char *)bu_malloc(MAX_LINE_SIZE, "next_line"); prev_line = (char *)bu_malloc(MAX_LINE_SIZE, "prev_line"); curr_rec = (char **)bu_calloc(NO_OF_FIELDS, sizeof(char *), "curr_rec"); for (i=0; i<NO_OF_FIELDS; i++) curr_rec[i] = (char *)bu_malloc(sizeof(char)*FIELD_LENGTH, "curr_rec[i]"); prev_rec = (char **)bu_calloc(NO_OF_FIELDS, sizeof(char *), "prev_rec"); for (i=0; i<NO_OF_FIELDS; i++) prev_rec[i] = (char *)bu_malloc(sizeof(char)*FIELD_LENGTH, "prev_rec[i]"); /* first pass, find start of NASTRAN "bulk data" */ start_off = (-1); bulk_data_start_line = 0; while (bu_fgets(line, MAX_LINE_SIZE, fpin)) { bulk_data_start_line++; if (bu_strncmp(line, "BEGIN BULK", 10)) continue; start_off = bu_ftell(fpin); break; } if (start_off < 0) { bu_log("Cannot find start of bulk data in NASTRAN file!\n"); bu_exit(1, Usage, argv[0]); } /* convert BULK data deck into something reasonable */ fptmp = bu_temp_file(NULL, 0); if (fptmp == NULL) { perror(argv[0]); bu_exit(1, "Cannot open temporary file\n"); } convert_input(); /* initialize some lists */ BU_LIST_INIT(&coord_head.l); BU_LIST_INIT(&pbar_head.l); BU_LIST_INIT(&pshell_head.l); BU_LIST_INIT(&all_head.l); nmg_model = (struct model *)NULL; /* count grid points */ bu_fseek(fptmp, 0, SEEK_SET); while (bu_fgets(line, MAX_LINE_SIZE, fptmp)) { if (!bu_strncmp(line, "GRID", 4)) grid_count++; } if (!grid_count) { bu_exit(1, "No geometry in this NASTRAN file!\n"); } /* get default values and properties */ bu_fseek(fptmp, 0, SEEK_SET); while (get_next_record(fptmp, 1, 0)) { if (!bu_strncmp(curr_rec[0], "BAROR", 5)) { /* get BAR defaults */ bar_def_pid = atoi(curr_rec[2]); } else if (!bu_strncmp(curr_rec[0], "PBAR", 4)) { struct pbar *pb; BU_ALLOC(pb, struct pbar); pb->pid = atoi(curr_rec[1]); pb->mid = atoi(curr_rec[2]); pb->area = atof(curr_rec[3]); BU_LIST_INIT(&pb->head.l); BU_LIST_INSERT(&pbar_head.l, &pb->l); } else if (!bu_strncmp(curr_rec[0], "PSHELL", 6)) { BU_ALLOC(psh, struct pshell); psh->s = (struct shell *)NULL; psh->pid = atoi(curr_rec[1]); psh->mid = atoi(curr_rec[2]); psh->thick = atof(curr_rec[3]); BU_LIST_INSERT(&pshell_head.l, &psh->l); pshell_count++; } } /* allocate storage for grid points */ g_pts = (struct grid_point *)bu_calloc(grid_count, sizeof(struct grid_point), "grid points"); /* get all grid points */ bu_fseek(fptmp, 0, SEEK_SET); while (get_next_record(fptmp, 1, 0)) { int gid; int cid; double tmp[3]; if (bu_strncmp(curr_rec[0], "GRID", 4)) continue; gid = atoi(curr_rec[1]); cid = atoi(curr_rec[2]); for (i=0; i<3; i++) { tmp[i] = atof(curr_rec[i+3]); } g_pts[grid_used].gid = gid; g_pts[grid_used].cid = cid; g_pts[grid_used].v = (struct vertex **)bu_calloc(pshell_count + 1, sizeof(struct vertex *), "g_pts vertex array"); VMOVE(g_pts[grid_used].pt, tmp); grid_used++; } /* find coordinate systems */ bu_fseek(fptmp, 0, SEEK_SET); while (get_next_record(fptmp, 1, 0)) { if (bu_strncmp(curr_rec[0], "CORD", 4)) continue; get_coord_sys(); } /* convert everything to BRL-CAD coordinate system */ i = 0; while (convert_all_cs() || convert_all_pts()) { i++; if (i > 10) { bu_exit(1, "Cannot convert to default coordinate system, check for circular definition\n"); } } mk_id(fpout, nastran_file); /* get elements */ bu_fseek(fptmp, 0, SEEK_SET); while (get_next_record(fptmp, 1, 0)) { if (!bu_strncmp(curr_rec[0], "CBAR", 4)) get_cbar(); else if (!bu_strncmp(curr_rec[0], "CROD", 4)) get_cbar(); else if (!bu_strncmp(curr_rec[0], "CTRIA3", 6)) get_ctria3(); else if (!bu_strncmp(curr_rec[0], "CQUAD4", 6)) get_cquad4(); } if (nmg_model) { nmg_rebound(nmg_model, &tol); if (polysolids) mk_bot_from_nmg(fpout, "pshell.0", nmg_shell); else mk_nmg(fpout, "pshell.0", nmg_model); } BU_LIST_INIT(&head.l); for (BU_LIST_FOR(psh, pshell, &pshell_head.l)) { struct model *m; char name[NAMESIZE+1]; if (!psh->s) continue; m = nmg_find_model(&psh->s->l.magic); nmg_rebound(m, &tol); nmg_fix_normals(psh->s, &tol); if (psh->thick > tol.dist) { nmg_model_face_fuse(m, &tol); nmg_hollow_shell(psh->s, psh->thick*conv[units], 1, &tol); } sprintf(name, "pshell.%d", psh->pid); if (polysolids) mk_bot_from_nmg(fpout, name, psh->s); else mk_nmg(fpout, name, m); mk_addmember(name, &head.l, NULL, WMOP_UNION); } if (BU_LIST_NON_EMPTY(&head.l)) { mk_lfcomb(fpout, "shells", &head, 0); mk_addmember("shells", &all_head.l, NULL, WMOP_UNION); } BU_LIST_INIT(&head.l); for (BU_LIST_FOR(pbp, pbar, &pbar_head.l)) { char name[NAMESIZE+1]; if (BU_LIST_IS_EMPTY(&pbp->head.l)) continue; sprintf(name, "pbar_group.%d", pbp->pid); mk_lfcomb(fpout, name, &pbp->head, 0); mk_addmember(name, &head.l, NULL, WMOP_UNION); } if (BU_LIST_NON_EMPTY(&head.l)) { mk_lfcomb(fpout, "pbars", &head, 0); mk_addmember("pbars", &all_head.l, NULL, WMOP_UNION); } if (BU_LIST_NON_EMPTY(&all_head.l)) { mk_lfcomb(fpout, "all", &all_head, 0); } wdb_close(fpout); return 0; }
int main(int argc, char **argv) { static const char usage[] = "Usage: bwhisteq [-v] file.bw > file.equalized\n"; int verbose = 0; FILE *fp = NULL; double havg; double hint; int i; int n; long r; size_t ret; unsigned char *bp; #define BUFSIZE 512 unsigned char buf[BUFSIZE]; unsigned char obuf[BUFSIZE]; #define BINSIZE 256 long bin[BINSIZE]; unsigned char result[BINSIZE]; int left[BINSIZE]; int right[BINSIZE]; if (argc > 1 && BU_STR_EQUAL(argv[1], "-v")) { verbose++; argc--; argv++; } if (argc != 2 || isatty(fileno(stdout))) { bu_exit(1, "%s", usage); } if ((fp = fopen(argv[1], "r")) == NULL) { bu_exit(2, "bwhisteq: Can't open \"%s\"\n", argv[1]); } /* Tally up the intensities */ havg = 0.0; while ((n = fread(buf, sizeof(*buf), BUFSIZE, fp)) > 0) { bp = &buf[0]; for (i = 0; i < n; i++) bin[ *bp++ ]++; havg += n; } havg /= (double)BINSIZE; r = 0; hint = 0; for (i = 0; i < BINSIZE; i++) { left[i] = r; hint += bin[i]; while (hint > havg) { hint -= havg; r++; } right[i] = r; result[i] = (left[i] + right[i]) / 2; } if (verbose) { for (i = 0; i < BINSIZE; i++) fprintf(stderr, "result[%d] = %d\n", i, result[i]); } bu_fseek(fp, 0, 0); while ((n = fread(buf, 1, BUFSIZE, fp)) > 0) { for (i = 0; i < n; i++) { long idx = buf[i]; if (idx < 0) idx = 0; if (idx > BINSIZE-1) idx = BINSIZE-1; if (left[idx] == right[idx]) obuf[i] = left[idx]; else { obuf[i] = result[idx]; } } ret = fwrite(obuf, 1, n, stdout); if (ret == 0) { perror("fwrite"); break; } } return 0; }
int main(int argc, char **argv) { int c; double percent; int i; bu_setprogname(argv[0]); bu_setlinebuf(stderr); RTG.debug = 0; tree_state = rt_initial_tree_state; /* struct copy */ tree_state.ts_tol = &tol; tree_state.ts_ttol = &ttol; tree_state.ts_m = &the_model; ttol.magic = RT_TESS_TOL_MAGIC; /* Defaults, updated by command line options. */ ttol.abs = 0.0; ttol.rel = 0.01; ttol.norm = 0.0; /* FIXME: These need to be improved */ tol.magic = BN_TOL_MAGIC; tol.dist = BN_TOL_DIST; tol.dist_sq = tol.dist * tol.dist; tol.perp = 1e-6; tol.para = 1 - tol.perp; the_model = nmg_mm(); BU_LIST_INIT(&RTG.rtg_vlfree); /* for vlist macros */ /* Get command line arguments. */ while ((c = bu_getopt(argc, argv, "a:n:o:r:vx:D:P:X:e:ih?")) != -1) { switch (c) { case 'a': /* Absolute tolerance. */ ttol.abs = atof(bu_optarg); ttol.rel = 0.0; break; case 'n': /* Surface normal tolerance. */ ttol.norm = atof(bu_optarg); ttol.rel = 0.0; break; case 'o': /* Output file name. */ output_file = bu_optarg; break; case 'r': /* Relative tolerance. */ ttol.rel = atof(bu_optarg); break; case 'v': verbose++; break; case 'P': ncpu = atoi(bu_optarg); break; case 'x': sscanf(bu_optarg, "%x", (unsigned int *)&RTG.debug); break; case 'D': tol.dist = atof(bu_optarg); tol.dist_sq = tol.dist * tol.dist; rt_pr_tol(&tol); break; case 'X': sscanf(bu_optarg, "%x", (unsigned int *)&RTG.NMG_debug); NMG_debug = RTG.NMG_debug; break; case 'e': /* Error file name. */ error_file = bu_optarg; break; case 'i': inches = 1; break; default: usage(argv[0]); } } if (bu_optind+1 >= argc) usage(argv[0]); if (!output_file) { fp = stdout; setmode(fileno(fp), O_BINARY); } else { /* Open output file */ if ((fp=fopen(output_file, "wb+")) == NULL) { perror(argv[0]); bu_exit(1, "Cannot open output file (%s) for writing\n", output_file); } } /* Open g-acad error log file */ if (!error_file) { fpe = stderr; setmode(fileno(fpe), O_BINARY); } else if ((fpe=fopen(error_file, "wb")) == NULL) { perror(argv[0]); bu_exit(1, "Cannot open output file (%s) for writing\n", error_file); } /* Open BRL-CAD database */ argc -= bu_optind; argv += bu_optind; if ((dbip = db_open(argv[0], DB_OPEN_READONLY)) == DBI_NULL) { perror(argv[0]); bu_exit(1, "Cannot open geometry database file (%s)\n", argv[0]); } if (db_dirbuild(dbip)) { bu_exit(1, "db_dirbuild failed\n"); } BN_CK_TOL(tree_state.ts_tol); RT_CK_TESS_TOL(tree_state.ts_ttol); fprintf(fpe, "Model: %s\n", argv[0]); fprintf(fpe, "Objects:"); for (i=1; i<argc; i++) fprintf(fpe, " %s", argv[i]); fprintf(fpe, "\nTessellation tolerances:\n\tabs = %g mm\n\trel = %g\n\tnorm = %g\n", tree_state.ts_ttol->abs, tree_state.ts_ttol->rel, tree_state.ts_ttol->norm); fprintf(fpe, "Calculational tolerances:\n\tdist = %g mm perp = %g\n", tree_state.ts_tol->dist, tree_state.ts_tol->perp); bu_log("Model: %s\n", argv[0]); bu_log("Objects:"); for (i=1; i<argc; i++) bu_log(" %s", argv[i]); bu_log("\nTessellation tolerances:\n\tabs = %g mm\n\trel = %g\n\tnorm = %g\n", tree_state.ts_ttol->abs, tree_state.ts_ttol->rel, tree_state.ts_ttol->norm); bu_log("Calculational tolerances:\n\tdist = %g mm perp = %g\n", tree_state.ts_tol->dist, tree_state.ts_tol->perp); /* Write out ACAD facet header */ if (inches) fprintf(fp, "BRL-CAD generated ACAD FACET FILE (Units in)\n"); else fprintf(fp, "BRL-CAD generated ACAD FACET FILE (Units mm)\n"); /* Generate space for number of facet entities, will write over later */ fprintf(fp, " "); /* Walk indicated tree(s). Each region will be output separately */ (void) db_walk_tree(dbip, argc-1, (const char **)(argv+1), 1, /* ncpu */ &tree_state, 0, /* take all regions */ do_region_end, nmg_booltree_leaf_tess, (void *)NULL); /* in librt/nmg_bool.c */ percent = 0; if (regions_tried>0) { percent = ((double)regions_converted * 100) / regions_tried; printf("Tried %d regions, %d converted to NMG's successfully. %g%%\n", regions_tried, regions_converted, percent); } percent = 0; if (regions_tried > 0) { percent = ((double)regions_written * 100) / regions_tried; printf(" %d triangulated successfully. %g%%\n", regions_written, percent); } bu_log("%ld triangles written\n", tot_polygons); fprintf(fpe, "%ld triangles written\n", tot_polygons); /* Write out number of facet entities to .facet file */ rewind(fp); bu_fseek(fp, 46, 0); /* Re-position pointer to 2nd line */ fprintf(fp, "%d\n", regions_written); /* Write out number of regions */ fclose(fp); /* Release dynamic storage */ nmg_km(the_model); rt_vlist_cleanup(); db_close(dbip); return 0; }
int main(int argc, char *argv[]) { int length, frame_number, number, success, maxnum; int first_frame, spread, reserve; off_t last_pos; char line[MAXLEN]; char pbuffer[MAXLEN*MAXLINES]; if (!get_args(argc, argv)) fprintf(stderr, "Get_args error\n"); /* copy any lines preceding the first "start" command */ last_pos = bu_ftell(stdin); while (bu_fgets(line, MAXLEN, stdin)!=NULL) { if (bu_strncmp(line, "start", 5)) { printf("%s", line); last_pos = bu_ftell(stdin); } else break; } /* read the frame number of the first "start" command */ sscanf(strpbrk(line, "0123456789"), "%d", &frame_number); /* find the highest frame number in the file */ maxnum = 0; while (bu_fgets(line, MAXLEN, stdin)!=NULL) { if (!bu_strncmp(line, "start", 5)) { sscanf(strpbrk(line, "0123456789"), "%d", &number); maxnum = (maxnum>number)?maxnum:number; } } length = maxnum - frame_number + 1; /* spread should initially be the smallest power of two larger than * or equal to length */ spread = 2; while (spread < length) spread = spread<<1; first_frame = frame_number; success = 1; while (length--) { number = -1; success = 0; /* tells whether or not any frames have been found which have the current frame number*/ if (incremental) { bu_fseek(stdin, 0, 0); } else { bu_fseek(stdin, last_pos, 0); } reserve = MAXLEN*MAXLINES; pbuffer[0] = '\0'; /* delete old pbuffer */ /* inner loop: search through the entire file for frames */ /* which have the current frame number */ while (!feof(stdin)) { /*read to next "start" command*/ while (bu_fgets(line, MAXLEN, stdin)!=NULL) { if (!bu_strncmp(line, "start", 5)) { sscanf(strpbrk(line, "0123456789"), "%d", &number); break; } } if (number==frame_number) { if (!success) { /*first successful match*/ printf("%s", line); if (!suppressed) printf("clean;\n"); success = 1; last_pos = bu_ftell(stdin); } /* print contents until next "end" */ while (bu_fgets(line, MAXLEN, stdin)!=NULL) { if (!bu_strncmp(line, "end;", 4)) break; else if (bu_strncmp(line, "clean", 5)) printf("%s", line); } /* save contents until next "start" */ while (bu_fgets(line, MAXLEN, stdin)!=NULL) { if (!bu_strncmp(line, "start", 5)) break; else { reserve -= strlen(line); reserve -= 1; if (reserve > 0) { bu_strlcat(pbuffer, line, reserve + strlen(line) + 1); } else { printf("ERROR: ran out of buffer space (%d characters)\n", MAXLEN*MAXLINES); } } } } } if (success) printf("end;\n"); /* print saved-up post-raytracing commands, if any */ printf("%s", pbuffer); /* get next frame number */ if (incremental) { frame_number = frame_number + 2*spread; while (frame_number > maxnum) { spread = spread>>1; frame_number = first_frame + spread; } } else { frame_number += 1; } }