int rt_db_get_internal( struct rt_db_internal *ip, const struct directory *dp, const struct db_i *dbip, const mat_t mat, struct resource *resp) { struct bu_external ext; int id; int ret; RT_DB_INTERNAL_INIT(ip); if (dbip->dbi_version > 4) return rt_db_get_internal5(ip, dp, dbip, mat, resp); BU_EXTERNAL_INIT(&ext); if (db_get_external(&ext, dp, dbip) < 0) return -2; /* FAIL */ if (dp->d_flags & RT_DIR_COMB) { id = ID_COMBINATION; } else { /* As a convenience to older ft_import4 routines */ if (mat == NULL) mat = bn_mat_identity; id = rt_id_solid(&ext); } /* ip is already initialized and should not be re-initialized */ ret = -1; if (OBJ[id].ft_import4) { ret = OBJ[id].ft_import4(ip, &ext, mat, dbip, resp); } if (ret < 0) { bu_log("rt_db_get_internal(%s): import failure\n", dp->d_namep); rt_db_free_internal(ip); bu_free_external(&ext); return -1; /* FAIL */ } bu_free_external(&ext); RT_CK_DB_INTERNAL(ip); ip->idb_meth = &OBJ[id]; /* prior to version 5, there are no attributes ... */ bu_avs_init_empty(&ip->idb_avs); /* ... but this isn't the whole story: */ if (id == ID_COMBINATION) { const struct rt_comb_internal *comb = (const struct rt_comb_internal *)ip->idb_ptr; RT_CK_COMB(comb); db5_sync_comb_to_attr(&ip->idb_avs, comb); } return id; /* OK */ }
int rt_db_put_internal( struct directory *dp, struct db_i *dbip, struct rt_db_internal *ip, struct resource *resp) { struct bu_external ext; int ret; RT_CK_DB_INTERNAL(ip); if (db_version(dbip) > 4) return rt_db_put_internal5(dp, dbip, ip, resp, DB5_MAJORTYPE_BRLCAD); BU_EXTERNAL_INIT(&ext); /* Scale change on export is 1.0 -- no change */ ret = -1; if (ip->idb_meth->ft_export4) { ret = ip->idb_meth->ft_export4(&ext, ip, 1.0, dbip, resp); } if (ret < 0) { bu_log("rt_db_put_internal(%s): solid export failure\n", dp->d_namep); rt_db_free_internal(ip); bu_free_external(&ext); return -2; /* FAIL */ } rt_db_free_internal(ip); if (db_put_external(&ext, dp, dbip) < 0) { bu_free_external(&ext); return -1; /* FAIL */ } bu_free_external(&ext); return 0; /* OK */ }
int rt_fwrite_internal( FILE *fp, const char *name, const struct rt_db_internal *ip, double conv2mm) { struct bu_external ext; int ret; RT_CK_DB_INTERNAL(ip); RT_CK_FUNCTAB(ip->idb_meth); BU_EXTERNAL_INIT(&ext); ret = -1; if (ip->idb_meth->ft_export4) { ret = ip->idb_meth->ft_export4(&ext, ip, conv2mm, NULL /*dbip*/, &rt_uniresource); } if (ret < 0) { bu_log("rt_file_put_internal(%s): solid export failure\n", name); bu_free_external(&ext); return -2; /* FAIL */ } BU_CK_EXTERNAL(&ext); if (db_fwrite_external(fp, name, &ext) < 0) { bu_log("rt_fwrite_internal(%s): db_fwrite_external() error\n", name); bu_free_external(&ext); return -3; } bu_free_external(&ext); return 0; }
int ged_unhide(struct ged *gedp, int argc, const char *argv[]) { struct directory *dp; struct db_i *dbip; struct bu_external ext; struct bu_external tmp; struct db5_raw_internal raw; int i; static const char *usage = "object(s)"; GED_CHECK_DATABASE_OPEN(gedp, GED_ERROR); GED_CHECK_READ_ONLY(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", argv[0], usage); return GED_HELP; } dbip = gedp->ged_wdbp->dbip; if (db_version(dbip) < 5) { bu_vls_printf(gedp->ged_result_str, "Database was created with a previous release of BRL-CAD.\nSelect \"Tools->Upgrade Database...\" to enable support for this feature."); return GED_ERROR; } for (i = 1; i < argc; i++) { if ((dp = db_lookup(dbip, argv[i], LOOKUP_NOISY)) == RT_DIR_NULL) { continue; } RT_CK_DIR(dp); BU_EXTERNAL_INIT(&ext); if (db_get_external(&ext, dp, dbip) < 0) { bu_vls_printf(gedp->ged_result_str, "db_get_external failed for %s\n", dp->d_namep); continue; } if (db5_get_raw_internal_ptr(&raw, ext.ext_buf) == NULL) { bu_vls_printf(gedp->ged_result_str, "db5_get_raw_internal_ptr() failed for %s\n", dp->d_namep); bu_free_external(&ext); continue; } raw.h_name_hidden = (unsigned char)(0x0); BU_EXTERNAL_INIT(&tmp); db5_export_object3(&tmp, DB5HDR_HFLAGS_DLI_APPLICATION_DATA_OBJECT, dp->d_namep, raw.h_name_hidden, &raw.attributes, &raw.body, raw.major_type, raw.minor_type, raw.a_zzz, raw.b_zzz); bu_free_external(&ext); if (db_put_external(&tmp, dp, dbip)) { bu_vls_printf(gedp->ged_result_str, "db_put_external() failed for %s\n", dp->d_namep); bu_free_external(&tmp); continue; } bu_free_external(&tmp); dp->d_flags &= (~RT_DIR_HIDDEN); } return GED_OK; }
/** * Apply a 4x4 transformation matrix to the internal form of a solid. * * If "free" flag is non-zero, storage for the original solid is * released. If "os" is same as "is", storage for the original solid * is overwritten with the new, transformed solid. * * Returns - * -1 FAIL * 0 OK */ int rt_generic_xform( struct rt_db_internal *op, const mat_t mat, struct rt_db_internal *ip, int release, struct db_i *dbip, struct resource *resp) { struct bu_external ext; int id; struct bu_attribute_value_set avs; RT_CK_DB_INTERNAL(ip); RT_CK_DBI(dbip); RT_CK_RESOURCE(resp); memset(&avs, 0, sizeof(struct bu_attribute_value_set)); id = ip->idb_type; BU_EXTERNAL_INIT(&ext); /* Scale change on export is 1.0 -- no change */ switch (db_version(dbip)) { case 4: if (OBJ[id].ft_export4(&ext, ip, 1.0, dbip, resp) < 0) { bu_log("rt_generic_xform(): %s export failure\n", OBJ[id].ft_name); return -1; /* FAIL */ } if ((release || op == ip)) rt_db_free_internal(ip); RT_DB_INTERNAL_INIT(op); if (OBJ[id].ft_import4(op, &ext, mat, dbip, resp) < 0) { bu_log("rt_generic_xform(): solid import failure\n"); return -1; /* FAIL */ } break; case 5: if (OBJ[id].ft_export5(&ext, ip, 1.0, dbip, resp) < 0) { bu_log("rt_generic_xform(): %s export failure\n", OBJ[id].ft_name); return -1; /* FAIL */ } if ((release || op == ip)) { if (ip->idb_avs.magic == BU_AVS_MAGIC) { /* grab the attributes before they are lost * by rt_db_free_internal or RT_DB_INTERNAL_INIT */ bu_avs_init(&avs, ip->idb_avs.count, "avs"); bu_avs_merge(&avs, &ip->idb_avs); } rt_db_free_internal(ip); } RT_DB_INTERNAL_INIT(op); if (!release && op != ip) { /* just copy the attributes from ip to op */ if (ip->idb_avs.magic == BU_AVS_MAGIC) { bu_avs_init(&op->idb_avs, ip->idb_avs.count, "avs"); bu_avs_merge(&op->idb_avs, &ip->idb_avs); } } else if (avs.magic == BU_AVS_MAGIC) { /* put the saved attributes in the output */ bu_avs_init(&op->idb_avs, avs.count, "avs"); bu_avs_merge(&op->idb_avs, &avs); bu_avs_free(&avs); } if (OBJ[id].ft_import5(op, &ext, mat, dbip, resp) < 0) { bu_log("rt_generic_xform(): solid import failure\n"); return -1; /* FAIL */ } break; } bu_free_external(&ext); RT_CK_DB_INTERNAL(op); return 0; /* OK */ }
or poly-bot file_poly.g file_bot.g\n\ Convert polysolids to BOT solids in v4 database format only\n"; int main(int argc, char **argv) { FILE *ifp; FILE *ofp; union record record; union record *poly; long poly_limit=0; long curr_poly=0; struct bn_tol tol; int polys=0; int frees=0; int others=0; int bots=0; int i; int num_rec; int first=1; bu_setprogname(argv[0]); ifp = stdin; ofp = stdout; /* 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; if (argc >= 3) { ifp = fopen(argv[1], "rb"); if (!ifp) perror(argv[1]); ofp = fopen(argv[2], "wb"); if (!ofp) perror(argv[2]); if (ifp == NULL || ofp == NULL) { bu_exit(1, "poly-bot: can't open files."); } #if defined(_WIN32) && !defined(__CYGWIN__) } else { setmode(fileno(ifp), O_BINARY); setmode(fileno(ofp), O_BINARY); #endif } if (isatty(fileno(ifp))) { bu_exit(1, "%s", usage); } poly = (union record *)bu_malloc( POLY_BLOCK * sizeof( union record ), "poly" ); poly_limit = POLY_BLOCK; /* Read database file */ while ( fread( (char *)&record, sizeof record, 1, ifp ) == 1 && !feof(ifp) ) { top: switch ( record.u_id ) { case ID_FREE: frees++; continue; case DBID_SKETCH: num_rec = ntohl(*(uint32_t *)&record.skt.skt_count); others += num_rec + 1; if ( fwrite( &record, sizeof( union record ), 1, ofp ) < 1 ) bu_exit(1, "Write failed!\n" ); for ( i=0; i<num_rec; i++ ) { if ( fread( (char *)&record, sizeof record, 1, ifp ) != 1 ) bu_exit(1, "Unexpected EOF encountered while copying a SKETCH\n" ); if ( fwrite( &record, sizeof( union record ), 1, ofp ) < 1 ) bu_exit(1, "Write failed!\n" ); } break; case DBID_EXTR: num_rec = ntohl(*(uint32_t *)&record.extr.ex_count); others += num_rec + 1; if ( fwrite( &record, sizeof( union record ), 1, ofp ) < 1 ) bu_exit(1, "Write failed!\n" ); for ( i=0; i<num_rec; i++ ) { if ( fread( (char *)&record, sizeof record, 1, ifp ) != 1 ) bu_exit(1, "Unexpected EOF encountered while copying an EXTRUSION\n" ); if ( fwrite( &record, sizeof( union record ), 1, ofp ) < 1 ) bu_exit(1, "Write failed!\n" ); } break; case DBID_NMG: num_rec = ntohl(*(uint32_t *)&record.nmg.N_count); others += num_rec + 1; if ( fwrite( &record, sizeof( union record ), 1, ofp ) < 1 ) bu_exit(1, "Write failed!\n" ); for ( i=0; i<num_rec; i++ ) { if ( fread( (char *)&record, sizeof record, 1, ifp ) != 1 ) bu_exit(1, "Unexpected EOF encountered while copying an ARBN\n" ); if ( fwrite( &record, sizeof( union record ), 1, ofp ) < 1 ) bu_exit(1, "Write failed!\n" ); } break; case DBID_PIPE: num_rec = ntohl(*(uint32_t *)&record.pwr.pwr_count); others += num_rec + 1; if ( fwrite( &record, sizeof( union record ), 1, ofp ) < 1 ) bu_exit(1, "Write failed!\n" ); for ( i=0; i<num_rec; i++ ) { if ( fread( (char *)&record, sizeof record, 1, ifp ) != 1 ) bu_exit(1, "Unexpected EOF encountered while copying a PIPE\n" ); if ( fwrite( &record, sizeof( union record ), 1, ofp ) < 1 ) bu_exit(1, "Write failed!\n" ); } break; case DBID_ARBN: num_rec = ntohl(*(uint32_t *)&record.n.n_grans); others += num_rec + 1; if ( fwrite( &record, sizeof( union record ), 1, ofp ) < 1 ) bu_exit(1, "Write failed!\n" ); for ( i=0; i<num_rec; i++ ) { if ( fread( (char *)&record, sizeof record, 1, ifp ) != 1 ) bu_exit(1, "Unexpected EOF encountered while copying an ARBN\n" ); if ( fwrite( &record, sizeof( union record ), 1, ofp ) < 1 ) bu_exit(1, "Write failed!\n" ); } break; case DBID_STRSOL: num_rec = DB_SS_NGRAN - 1; others += num_rec + 1; if ( fwrite( &record, sizeof( union record ), 1, ofp ) < 1 ) bu_exit(1, "Write failed!\n" ); for ( i=0; i<num_rec; i++ ) { if ( fread( (char *)&record, sizeof record, 1, ifp ) != 1 ) bu_exit(1, "Unexpected EOF encountered while copying a STRSOL\n" ); if ( fwrite( &record, sizeof( union record ), 1, ofp ) < 1 ) bu_exit(1, "Write failed!\n" ); } break; case ID_BSURF: num_rec = record.d.d_nknots + record.d.d_nctls; others += num_rec + 1; if ( fwrite( &record, sizeof( union record ), 1, ofp ) < 1 ) bu_exit(1, "Write failed!\n" ); for ( i=0; i<num_rec; i++ ) { if ( fread( (char *)&record, sizeof record, 1, ifp ) != 1 ) bu_exit(1, "Unexpected EOF encountered while copying a NURB\n" ); if ( fwrite( &record, sizeof( union record ), 1, ofp ) < 1 ) bu_exit(1, "Write failed!\n" ); } break; case DBID_BOT: bots++; if ( fwrite( &record, sizeof( union record ), 1, ofp ) < 1 ) bu_exit(1, "Write failed!\n" ); num_rec = ntohl(*(uint32_t *)&record.bot.bot_nrec); for ( i=0; i<num_rec; i++ ) { if ( fread( (char *)&record, sizeof record, 1, ifp ) != 1 ) bu_exit(1, "Unexpected EOF encountered while copying a BOT\n" ); if ( fwrite( &record, sizeof( union record ), 1, ofp ) < 1 ) bu_exit(1, "Write failed!\n" ); } break; case ID_P_HEAD: { struct rt_db_internal intern; struct bu_external ext; struct bu_external ext2; polys++; curr_poly = 0; poly[curr_poly++] = record; /* struct copy */ bu_log( "Converting %s\n", poly[0].p.p_name ); while ( fread( (char *)&record, sizeof record, 1, ifp ) == 1 && !feof(ifp) && record.u_id == ID_P_DATA ) { if ( curr_poly >= poly_limit ) { poly_limit += POLY_BLOCK; poly = (union record *)bu_realloc( poly, poly_limit*sizeof( union record ), "poly realloc" ); } poly[curr_poly++] = record; /* struct copy */ } BU_EXTERNAL_INIT( &ext ); ext.ext_nbytes = curr_poly * sizeof( union record ); ext.ext_buf = (uint8_t *)poly; if ( rt_functab[ID_POLY].ft_import4( &intern, &ext, bn_mat_identity, (struct db_i *)NULL, &rt_uniresource ) ) { bu_exit(1, "Import failed for polysolid %s\n", poly[0].p.p_name ); } /* Don't free this ext buffer! */ if ( rt_pg_to_bot( &intern, &tol, &rt_uniresource ) < 0 ) { bu_exit(1, "Unable to convert polysolid %s\n", poly[0].p.p_name ); } BU_EXTERNAL_INIT( &ext2 ); if ( rt_functab[ID_POLY].ft_export4( &ext2, &intern, 1.0, (struct db_i *)NULL, &rt_uniresource ) < 0 ) { bu_exit(1, "Unable to export v4 BoT %s\n", poly[0].p.p_name ); } rt_db_free_internal(&intern); if ( db_fwrite_external( ofp, poly[0].p.p_name, &ext2 ) < 0 ) { bu_exit(1, "Unable to fwrite v4 BoT %s\n", poly[0].p.p_name ); } bu_free_external( &ext2 ); if ( feof( ifp ) ) break; goto top; } case ID_P_DATA: /* This should not happen! */ bu_log( "ERROR: Unattached polysolid data record!\n" ); continue; default: if ( first ) { if ( record.u_id != ID_IDENT ) { bu_exit(1, "This is not a BRL-CAD 'v4' database, aborting.\n" ); } first = 0; } others++; if ( fwrite( &record, sizeof( union record ), 1, ofp ) < 1 ) bu_exit(1, "Write failed!\n" ); } } bu_log( "%d polysolids converted to BOT solids\n", polys ); bu_log( "%d BOT solids copied without change\n", bots ); bu_log( "%d other records copied without change\n", others ); bu_log( "%d free records skipped\n", frees ); fclose(ofp); return 0; }
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; }