/** * Import an XXX from the database format to the internal format. * Note that the data read will be in network order. This means * Big-Endian integers and IEEE doubles for floating point. * * Apply modeling transformations as well. */ int rt_xxx_import5(struct rt_db_internal *ip, const struct bu_external *ep, const mat_t mat, const struct db_i *dbip) { struct rt_xxx_internal *xxx_ip; /* must be double for import and export */ double vv[ELEMENTS_PER_VECT*1]; RT_CK_DB_INTERNAL(ip); BU_CK_EXTERNAL(ep); if (dbip) RT_CK_DBI(dbip); BU_ASSERT_LONG(ep->ext_nbytes, ==, SIZEOF_NETWORK_DOUBLE * 3*4); /* set up the internal structure */ ip->idb_major_type = DB5_MAJORTYPE_BRLCAD; ip->idb_type = ID_XXX; ip->idb_meth = &OBJ[ID_XXX]; BU_ALLOC(ip->idb_ptr, struct rt_xxx_internal); xxx_ip = (struct rt_xxx_internal *)ip->idb_ptr; xxx_ip->magic = RT_XXX_INTERNAL_MAGIC; /* Convert the data in ep->ext_buf into internal format. Note the * conversion from network data (Big Endian ints, IEEE double * floating point) to host local data representations. */ bu_cv_ntohd((unsigned char *)&vv, (unsigned char *)ep->ext_buf, ELEMENTS_PER_VECT*1); /* Apply the modeling transformation */ if (mat == NULL) mat = bn_mat_identity; MAT4X3PNT(xxx_ip->v, mat, vv); return 0; /* OK */ }
double getdouble(FILE *fp) { double d; unsigned char buf[8]; size_t ret; ret = fread(buf, 8, 1, fp); if (ret < 1) perror("fread"); bu_cv_ntohd((unsigned char *)&d, buf, 1); return d; }
static void rt_uplot_get_args(FILE *fp, const struct uplot *up, char *carg, fastf_t *arg) { size_t ret; int i, j; int cc = 0; char inbuf[SIZEOF_NETWORK_DOUBLE] = {'\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0'}; for (i = 0; i < up->narg; i++) { switch (up->targ) { case TSHORT: arg[i] = getshort(fp); break; case TIEEE: { double scan; ret = fread(inbuf, SIZEOF_NETWORK_DOUBLE, 1, fp); if (ret != 1) bu_log("WARNING: uplot read failure\n"); bu_cv_ntohd((unsigned char *)&scan, (unsigned char *)inbuf, 1); arg[i] = scan; /* convert double to fastf_t */ break; } case TSTRING: j = 0; while (!feof(fp) && (cc = getc(fp)) != '\n') carg[j++] = cc; carg[j] = '\0'; break; case TCHAR: cc = getc(fp); if (cc == EOF) return; carg[i] = cc; arg[i] = 0; break; case TNONE: default: arg[i] = 0; /* ? */ break; } } }
void outfloat(int n) { int i; unsigned char in[8*16]; double out[16]; size_t ret; ret = fread(in, 8, n, fp); if (ret < (size_t)n) perror("fread"); bu_cv_ntohd((unsigned char *)out, in, n); for (i = 0; i < n; i++) { /*printf("%g", out[i]);*/ ; } }
/** * Import an metaball/sphere from the database format to the internal * structure. Apply modeling transformations as well. */ int rt_metaball_import5(struct rt_db_internal *ip, const struct bu_external *ep, register const fastf_t *mat, const struct db_i *dbip) { struct wdb_metaballpt *mbpt; struct rt_metaball_internal *mb; int metaball_count = 0, i; /* must be double for import and export */ double *buf; if (dbip) RT_CK_DBI(dbip); BU_CK_EXTERNAL(ep); metaball_count = ntohl(*(uint32_t *)ep->ext_buf); buf = (double *)bu_malloc((metaball_count*5+1)*SIZEOF_NETWORK_DOUBLE, "rt_metaball_import5: buf"); bu_cv_ntohd((unsigned char *)buf, (unsigned char *)ep->ext_buf+2*SIZEOF_NETWORK_LONG, metaball_count*5+1); RT_CK_DB_INTERNAL(ip); ip->idb_major_type = DB5_MAJORTYPE_BRLCAD; ip->idb_type = ID_METABALL; ip->idb_meth = &OBJ[ID_METABALL]; BU_ALLOC(ip->idb_ptr, struct rt_metaball_internal); mb = (struct rt_metaball_internal *)ip->idb_ptr; mb->magic = RT_METABALL_INTERNAL_MAGIC; mb->method = ntohl(*(uint32_t *)(ep->ext_buf + SIZEOF_NETWORK_LONG)); mb->threshold = buf[0]; BU_LIST_INIT(&mb->metaball_ctrl_head); if (mat == NULL) mat = bn_mat_identity; for (i = 1; i <= metaball_count * 5; i += 5) { /* Apply modeling transformations */ BU_GET(mbpt, struct wdb_metaballpt); mbpt->l.magic = WDB_METABALLPT_MAGIC; MAT4X3PNT(mbpt->coord, mat, &buf[i]); mbpt->fldstr = buf[i+3] / mat[15]; mbpt->sweat = buf[i+4]; BU_LIST_INSERT(&mb->metaball_ctrl_head, &mbpt->l); } bu_free((void *)buf, "rt_metaball_import5: buf"); return 0; /* OK */ }
void three_dcoord_out(FILE *fp, fastf_t *m) { unsigned char buf[3*8]; double p1[3]; double p2[3]; size_t ret; ret = fread(buf, 1, 3*8, fp); if (ret < 3*8) perror("fread"); bu_cv_ntohd((unsigned char *)p1, buf, 3); MAT4X3PNT(p2, m, p1); bu_cv_htond(buf, (unsigned char *)p2, 3); ret = fwrite(buf, 1, 3*8, stdout); if (ret < 3*8) perror("fwrite"); }
/** * Import an superellipsoid/sphere from the database format to the * internal structure. Apply modeling transformations as wsuperell. */ int rt_superell_import5(struct rt_db_internal *ip, const struct bu_external *ep, const fastf_t *mat, const struct db_i *dbip) { struct rt_superell_internal *eip; /* must be double for import and export */ double vec[ELEMENTS_PER_VECT*4 + 2]; if (dbip) RT_CK_DBI(dbip); RT_CK_DB_INTERNAL(ip); BU_CK_EXTERNAL(ep); BU_ASSERT_LONG(ep->ext_nbytes, ==, SIZEOF_NETWORK_DOUBLE * (ELEMENTS_PER_VECT*4 + 2)); ip->idb_major_type = DB5_MAJORTYPE_BRLCAD; ip->idb_type = ID_SUPERELL; ip->idb_meth = &OBJ[ID_SUPERELL]; BU_ALLOC(ip->idb_ptr, struct rt_superell_internal); eip = (struct rt_superell_internal *)ip->idb_ptr; eip->magic = RT_SUPERELL_INTERNAL_MAGIC; /* Convert from database (network) to internal (host) format */ bu_cv_ntohd((unsigned char *)vec, ep->ext_buf, ELEMENTS_PER_VECT*4 + 2); /* Apply modeling transformations */ if (mat == NULL) mat = bn_mat_identity; MAT4X3PNT(eip->v, mat, &vec[0*ELEMENTS_PER_VECT]); MAT4X3VEC(eip->a, mat, &vec[1*ELEMENTS_PER_VECT]); MAT4X3VEC(eip->b, mat, &vec[2*ELEMENTS_PER_VECT]); MAT4X3VEC(eip->c, mat, &vec[3*ELEMENTS_PER_VECT]); eip->n = vec[4*ELEMENTS_PER_VECT]; eip->e = vec[4*ELEMENTS_PER_VECT + 1]; return 0; /* OK */ }
int main (int argc, char **argv) { unsigned char *buffer; unsigned char *bp; double *value; int bufsiz; /* buffer size (in bytes) */ int l_per_b; /* buffer size (in output lines) */ int line_nm; /* number of current line */ int num; /* number of bytes read */ int i; int row, col; /* coords within input stream */ if (!get_args(argc, argv)) { print_usage(1); } /* autosize input? */ if (fileinput && autosize) { size_t w, h; if (fb_common_file_size(&w, &h, file_name, d_per_l * 8)) { file_width = (long)w; file_height = (long)h; } else bu_log("double-asc: unable to autosize\n"); } bu_log("OK, file is %ld wide and %ld high\n", file_width, file_height); /* * Choose an input-buffer size as close as possible to 64 kbytes, * while still an integral multiple of the size of an output line. */ l_per_b = ((1 << 16) / (d_per_l * 8)); bufsiz = l_per_b * (d_per_l * 8); buffer = (unsigned char *) bu_malloc(bufsiz, "char buffer"); value = (double *) bu_malloc(d_per_l * 8, "doubles"); col = row = 0; while ((num = read(infd, buffer, bufsiz)) > 0) { bp = buffer; l_per_b = num / (d_per_l * 8); for (line_nm = 0; line_nm < l_per_b; ++line_nm) { if (make_cells) printf("%d %d", col, row); bu_cv_ntohd((unsigned char *)value, bp, d_per_l); bp += d_per_l * 8; for (i = 0; i < d_per_l; ++i) printf(format, value[i]); printf("\n"); if (++col % file_width == 0) { col = 0; ++row; } } } if (num < 0) { perror("double-asc"); bu_exit (1, NULL); } return 0; }
/** * Import an REVOLVE from the database format to the internal format. * Note that the data read will be in network order. This means * Big-Endian integers and IEEE doubles for floating point. * * Apply modeling transformations as well. */ int rt_revolve_import5(struct rt_db_internal *ip, const struct bu_external *ep, const mat_t mat, const struct db_i *dbip, struct resource *resp) { struct rt_revolve_internal *rip; /* must be double for import and export */ double vv[ELEMENTS_PER_VECT*3 + 1]; char *sketch_name; unsigned char *ptr; struct directory *dp; struct rt_db_internal tmp_ip; if (dbip) RT_CK_DBI(dbip); RT_CK_DB_INTERNAL(ip); BU_CK_EXTERNAL(ep); /* set up the internal structure */ ip->idb_major_type = DB5_MAJORTYPE_BRLCAD; ip->idb_type = ID_REVOLVE; ip->idb_meth = &OBJ[ID_REVOLVE]; BU_ALLOC(ip->idb_ptr, struct rt_revolve_internal); rip = (struct rt_revolve_internal *)ip->idb_ptr; rip->magic = RT_REVOLVE_INTERNAL_MAGIC; /* Convert the data in ep->ext_buf into internal format. Note the * conversion from network data (Big Endian ints, IEEE double * floating point) to host local data representations. */ ptr = (unsigned char *)ep->ext_buf; sketch_name = (char *)ptr + (ELEMENTS_PER_VECT*3 + 1)*SIZEOF_NETWORK_DOUBLE; if (!dbip) rip->skt = (struct rt_sketch_internal *)NULL; else if ((dp=db_lookup(dbip, sketch_name, LOOKUP_NOISY)) == RT_DIR_NULL) { bu_log("ERROR: Cannot find sketch (%s) for extrusion\n", sketch_name); rip->skt = (struct rt_sketch_internal *)NULL; } else { if (rt_db_get_internal(&tmp_ip, dp, dbip, bn_mat_identity, resp) != ID_SKETCH) { bu_log("ERROR: Cannot import sketch (%s) for extrusion\n", sketch_name); bu_free(ip->idb_ptr, "extrusion"); return -1; } else rip->skt = (struct rt_sketch_internal *)tmp_ip.idb_ptr; } bu_cv_ntohd((unsigned char *)&vv, (unsigned char *)ep->ext_buf, ELEMENTS_PER_VECT*3 + 1); /* Apply the modeling transformation */ if (mat == NULL) mat = bn_mat_identity; MAT4X3PNT(rip->v3d, mat, &vv[0*3]); MAT4X3PNT(rip->axis3d, mat, &vv[1*3]); MAT4X3PNT(rip->r, mat, &vv[2*3]); rip->ang = vv[9]; /* convert name of data location */ bu_vls_init(&rip->sketch_name); bu_vls_strcpy(&rip->sketch_name, (char *)ep->ext_buf + (ELEMENTS_PER_VECT * 3 + 1) * SIZEOF_NETWORK_DOUBLE); return 0; /* OK */ }
/** * Convert from "network" doubles to machine specific. * Transform */ int rt_arbn_import5(struct rt_db_internal *ip, const struct bu_external *ep, const fastf_t *mat, const struct db_i *dbip) { struct rt_arbn_internal *aip; size_t i; unsigned long neqn; int double_count; size_t byte_count; /* must be double for import and export */ double *eqn; RT_CK_DB_INTERNAL(ip); BU_CK_EXTERNAL(ep); if (dbip) RT_CK_DBI(dbip); neqn = ntohl(*(uint32_t *)ep->ext_buf); double_count = neqn * ELEMENTS_PER_PLANE; byte_count = double_count * SIZEOF_NETWORK_DOUBLE; BU_ASSERT_LONG(ep->ext_nbytes, ==, 4+ byte_count); ip->idb_major_type = DB5_MAJORTYPE_BRLCAD; ip->idb_type = ID_ARBN; ip->idb_meth = &OBJ[ID_ARBN]; BU_ALLOC(ip->idb_ptr, struct rt_arbn_internal); aip = (struct rt_arbn_internal *)ip->idb_ptr; aip->magic = RT_ARBN_INTERNAL_MAGIC; aip->neqn = neqn; if (aip->neqn <= 0) return -1; eqn = (double *)bu_malloc(byte_count, "arbn plane eqn[] temp buf"); bu_cv_ntohd((unsigned char *)eqn, (unsigned char *)ep->ext_buf + ELEMENTS_PER_PLANE, double_count); aip->eqn = (plane_t *)bu_malloc(double_count * sizeof(fastf_t), "arbn plane eqn[]"); for (i = 0; i < aip->neqn; i++) { HMOVE(aip->eqn[i], &eqn[i*ELEMENTS_PER_PLANE]); } bu_free(eqn, "arbn plane eqn[] temp buf"); /* Transform by the matrix, if we have one that is not the identity */ if (mat && !bn_mat_is_identity(mat)) { for (i = 0; i < aip->neqn; i++) { point_t orig_pt; point_t pt; vect_t norm; fastf_t factor; /* unitize the plane equation first */ factor = 1.0 / MAGNITUDE(aip->eqn[i]); VSCALE(aip->eqn[i], aip->eqn[i], factor); aip->eqn[i][W] = aip->eqn[i][W] * factor; /* Pick a point on the original halfspace */ VSCALE(orig_pt, aip->eqn[i], aip->eqn[i][W]); /* Transform the point, and the normal */ MAT4X3VEC(norm, mat, aip->eqn[i]); MAT4X3PNT(pt, mat, orig_pt); /* Measure new distance from origin to new point */ VUNITIZE(norm); VMOVE(aip->eqn[i], norm); aip->eqn[i][W] = VDOT(pt, norm); } } return 0; }
/** * Convert from "network" doubles to machine specific. * Transform */ int rt_arbn_import4(struct rt_db_internal *ip, const struct bu_external *ep, const fastf_t *mat, const struct db_i *dbip) { union record *rp; struct rt_arbn_internal *aip; size_t i; /* must be double for import and export */ double *scan; if (dbip) RT_CK_DBI(dbip); BU_CK_EXTERNAL(ep); rp = (union record *)ep->ext_buf; if (rp->u_id != DBID_ARBN) { bu_log("rt_arbn_import4: defective record, id=x%x\n", rp->u_id); return -1; } RT_CK_DB_INTERNAL(ip); ip->idb_major_type = DB5_MAJORTYPE_BRLCAD; ip->idb_type = ID_ARBN; ip->idb_meth = &OBJ[ID_ARBN]; BU_ALLOC(ip->idb_ptr, struct rt_arbn_internal); aip = (struct rt_arbn_internal *)ip->idb_ptr; aip->magic = RT_ARBN_INTERNAL_MAGIC; aip->neqn = ntohl(*(uint32_t *)rp->n.n_neqn); if (aip->neqn <= 0) return -1; aip->eqn = (plane_t *)bu_malloc(aip->neqn*sizeof(plane_t), "arbn plane eqn[]"); scan = (double *)bu_malloc(aip->neqn*sizeof(double)*ELEMENTS_PER_PLANE, "scan array"); bu_cv_ntohd((unsigned char *)scan, (unsigned char *)(&rp[1]), aip->neqn*ELEMENTS_PER_PLANE); for (i = 0; i < aip->neqn; i++) { aip->eqn[i][X] = scan[(i*ELEMENTS_PER_PLANE)+0]; /* convert double to fastf_t */ aip->eqn[i][Y] = scan[(i*ELEMENTS_PER_PLANE)+1]; /* convert double to fastf_t */ aip->eqn[i][Z] = scan[(i*ELEMENTS_PER_PLANE)+2]; /* convert double to fastf_t */ aip->eqn[i][W] = scan[(i*ELEMENTS_PER_PLANE)+3]; /* convert double to fastf_t */ } bu_free(scan, "scan array"); /* Transform by the matrix */ if (mat == NULL) mat = bn_mat_identity; for (i = 0; i < aip->neqn; i++) { point_t orig_pt; point_t pt; vect_t norm; fastf_t factor; /* unitize the plane equation first */ factor = 1.0 / MAGNITUDE(aip->eqn[i]); VSCALE(aip->eqn[i], aip->eqn[i], factor); aip->eqn[i][W] = aip->eqn[i][W] * factor; /* Pick a point on the original halfspace */ VSCALE(orig_pt, aip->eqn[i], aip->eqn[i][W]); /* Transform the point, and the normal */ MAT4X3VEC(norm, mat, aip->eqn[i]); MAT4X3PNT(pt, mat, orig_pt); /* Measure new distance from origin to new point */ VUNITIZE(norm); VMOVE(aip->eqn[i], norm); aip->eqn[i][W] = VDOT(pt, norm); } return 0; }