struct bn_vlblock * bn_vlblock_init(struct bu_list *free_vlist_hd, /**< where to get/put free vlists */ int max_ent /**< maximum number of entities to get/put */) { struct bn_vlblock *vbp; size_t i; if (!BU_LIST_IS_INITIALIZED(free_vlist_hd)) BU_LIST_INIT(free_vlist_hd); BU_ALLOC(vbp, struct bn_vlblock); vbp->magic = BN_VLBLOCK_MAGIC; vbp->free_vlist_hd = free_vlist_hd; vbp->max = max_ent; vbp->head = (struct bu_list *)bu_calloc(vbp->max, sizeof(struct bu_list), "head[]"); vbp->rgb = (long *)bu_calloc(vbp->max, sizeof(long), "rgb[]"); for (i=0; i < vbp->max; i++) { vbp->rgb[i] = 0; BU_LIST_INIT(&(vbp->head[i])); } vbp->rgb[0] = 0xFFFF00L; /* Yellow, default */ vbp->rgb[1] = 0xFFFFFFL; /* White */ vbp->nused = 2; return vbp; }
void db_add_node_to_full_path(struct db_full_path *pp, struct directory *dp) { RT_CK_FULL_PATH(pp); if (pp->fp_maxlen <= 0) { pp->fp_maxlen = 32; pp->fp_names = (struct directory **)bu_malloc( pp->fp_maxlen * sizeof(struct directory *), "db_full_path array"); pp->fp_bool = (int *)bu_calloc(pp->fp_maxlen, sizeof(int), "db_full_path bool array"); pp->fp_mat = (matp_t *)bu_calloc(pp->fp_maxlen, sizeof(matp_t), "db_full_path matrices array"); } else if (pp->fp_len >= pp->fp_maxlen) { pp->fp_maxlen *= 4; pp->fp_names = (struct directory **)bu_realloc( (char *)pp->fp_names, pp->fp_maxlen * sizeof(struct directory *), "enlarged db_full_path array"); pp->fp_bool = (int *)bu_realloc( (char *)pp->fp_bool, pp->fp_maxlen * sizeof(int), "enlarged db_full_path bool array"); pp->fp_mat = (matp_t *)bu_realloc( (char *)pp->fp_mat, pp->fp_maxlen * sizeof(matp_t), "enlarged db_full_path matrices array"); } pp->fp_names[pp->fp_len++] = dp; }
struct rt_bot_internal * dup_bot( struct rt_bot_internal *bot_in ) { struct rt_bot_internal *bot; size_t i; RT_BOT_CK_MAGIC( bot_in ); BU_ALLOC(bot, struct rt_bot_internal); *bot = *bot_in; /* struct copy */ bot->faces = (int *)bu_calloc( bot_in->num_faces*3, sizeof( int ), "bot faces" ); for ( i=0; i<bot_in->num_faces*3; i++ ) bot->faces[i] = bot_in->faces[i]; bot->vertices = (fastf_t *)bu_calloc( bot_in->num_vertices*3, sizeof( fastf_t ), "bot verts" ); for ( i=0; i<bot_in->num_vertices*3; i++ ) bot->vertices[i] = bot_in->vertices[i]; if ( bot_in->thickness ) { bot->thickness = (fastf_t *)bu_calloc( bot_in->num_faces, sizeof( fastf_t ), "bot thickness" ); for ( i=0; i<bot_in->num_faces; i++ ) bot->thickness[i] = bot_in->thickness[i]; } if ( bot_in->face_mode ) { bot->face_mode = bu_bitv_dup( bot_in->face_mode ); } return bot; }
void rt_arbn_volume(fastf_t *volume, const struct rt_db_internal *ip) { struct poly_face *faces; struct rt_arbn_internal *aip = (struct rt_arbn_internal *)ip->idb_ptr; size_t i; *volume = 0.0; /* allocate array of face structs */ faces = (struct poly_face *)bu_calloc(aip->neqn, sizeof(struct poly_face), "rt_arbn_volume: faces"); for (i = 0; i < aip->neqn; i++) { /* allocate array of pt structs, max number of verts per faces = (# of faces) - 1 */ faces[i].pts = (point_t *)bu_calloc(aip->neqn - 1, sizeof(point_t), "rt_arbn_volume: pts"); } rt_arbn_faces_area(faces, aip); for (i = 0; i < aip->neqn; i++) { vect_t tmp; /* calculate volume of pyramid */ VSCALE(tmp, faces[i].plane_eqn, faces[i].area); *volume += VDOT(faces[i].pts[0], tmp)/3; } for (i = 0; i < aip->neqn; i++) { bu_free((char *)faces[i].pts, "rt_arbn_volume: pts"); } bu_free((char *)faces, "rt_arbn_volume: faces"); }
int db_argv_to_path(struct db_full_path *pp, struct db_i *dbip, int argc, const char *const *argv) { struct directory *dp; int ret = 0; int i; RT_CK_DBI(dbip); /* Make a path structure just big enough */ pp->magic = DB_FULL_PATH_MAGIC; pp->fp_maxlen = pp->fp_len = argc; pp->fp_names = (struct directory **)bu_malloc( pp->fp_maxlen * sizeof(struct directory *), "db_argv_to_path path array"); pp->fp_bool = (int *)bu_calloc(pp->fp_maxlen, sizeof(int), "db_argv_to_path bool array"); pp->fp_mat = (matp_t *)bu_calloc(pp->fp_maxlen, sizeof(matp_t), "db_string_to_path mat array"); for (i = 0; i<argc; i++) { if ((dp = db_lookup(dbip, argv[i], LOOKUP_NOISY)) == RT_DIR_NULL) { bu_log("db_argv_to_path() failed on element %d='%s'\n", i, argv[i]); ret = -1; /* FAILED */ /* Fall through, storing null dp in this location */ } pp->fp_names[i] = dp; } return ret; }
void db_dup_full_path(struct db_full_path *newp, const struct db_full_path *oldp) { unsigned int i = 0; RT_CK_FULL_PATH(newp); RT_CK_FULL_PATH(oldp); newp->fp_maxlen = oldp->fp_maxlen; newp->fp_len = oldp->fp_len; if (oldp->fp_len <= 0) { newp->fp_names = (struct directory **)0; newp->fp_bool = (int *)0; return; } newp->fp_names = (struct directory **)bu_malloc(newp->fp_maxlen * sizeof(struct directory *), "db_full_path array (duplicate)"); memcpy((char *)newp->fp_names, (char *)oldp->fp_names, newp->fp_len * sizeof(struct directory *)); newp->fp_bool = (int *)bu_malloc(newp->fp_maxlen * sizeof(int), "db_full_path bool array (duplicate)"); memcpy((char *)newp->fp_bool, (char *)oldp->fp_bool, newp->fp_len * sizeof(int)); newp->fp_mat = (matp_t *)bu_calloc(newp->fp_maxlen, sizeof(matp_t), "db_full_path mat array (duplicate)"); for (i = 0; i < newp->fp_len; i++) { if (oldp->fp_mat[i]) { newp->fp_mat[i] = (matp_t)bu_calloc(1, sizeof(mat_t), "transformation matrix"); MAT_COPY(newp->fp_mat[i], oldp->fp_mat[i]); } } }
int main(int argc, char **argv) { int i; unsigned char *scanbuf; unsigned char **rows; png_structp png_p; png_infop info_p; if ( !get_args( argc, argv ) ) { (void)fputs(usage, stderr); bu_exit ( 1, NULL ); } /* autosize input? */ if ( fileinput && autosize ) { unsigned long int w, h; if ( fb_common_file_size(&w, &h, file_name, 1) ) { file_width = (long)w; file_height = (long)h; } else { fprintf(stderr, "bw-png: unable to autosize\n"); } } png_p = png_create_write_struct( PNG_LIBPNG_VER_STRING, NULL, NULL, NULL ); if ( !png_p ) bu_exit( EXIT_FAILURE, "Could not create PNG write structure\n" ); info_p = png_create_info_struct( png_p ); if ( !info_p ) bu_exit( EXIT_FAILURE, "Could not create PNG info structure\n" ); /* allocate space for the image */ scanbuf = (unsigned char *)bu_calloc( SIZE, sizeof( unsigned char ), "scanbuf" ); /* create array of pointers to rows for libpng */ rows = (unsigned char **)bu_calloc( file_height, sizeof( unsigned char *), "rows" ); for ( i=0; i<file_height; i++ ) rows[i] = scanbuf + ((file_height-i-1)*ROWSIZE); /* read the bw file */ if ( fread( scanbuf, SIZE, 1, infp ) != 1 ) bu_exit( EXIT_FAILURE, "bw-png: Short read\n"); png_init_io( png_p, stdout ); png_set_filter( png_p, 0, PNG_FILTER_NONE ); png_set_compression_level( png_p, Z_BEST_COMPRESSION ); png_set_IHDR( png_p, info_p, file_width, file_height, 8, PNG_COLOR_TYPE_GRAY, PNG_INTERLACE_NONE, PNG_COMPRESSION_TYPE_DEFAULT, PNG_FILTER_TYPE_DEFAULT ); png_write_info( png_p, info_p ); png_write_image( png_p, rows ); png_write_end( png_p, NULL ); return 0; }
void rt_arbn_centroid(point_t *cent, const struct rt_db_internal *ip) { struct poly_face *faces; struct rt_arbn_internal *aip = (struct rt_arbn_internal *)ip->idb_ptr; size_t i; point_t arbit_point = VINIT_ZERO; fastf_t volume = 0.0; *cent[0] = 0.0; *cent[1] = 0.0; *cent[2] = 0.0; if (cent == NULL) return; /* allocate array of face structs */ faces = (struct poly_face *)bu_calloc(aip->neqn, sizeof(struct poly_face), "rt_arbn_centroid: faces"); for (i = 0; i < aip->neqn; i++) { /* allocate array of pt structs, max number of verts per faces = (# of faces) - 1 */ faces[i].pts = (point_t *)bu_calloc(aip->neqn - 1, sizeof(point_t), "rt_arbn_centroid: pts"); } rt_arbn_faces_area(faces, aip); for (i = 0; i < aip->neqn; i++) { bn_polygon_centroid(&faces[i].cent, faces[i].npts, (const point_t *) faces[i].pts); VADD2(arbit_point, arbit_point, faces[i].cent); } VSCALE(arbit_point, arbit_point, (1/aip->neqn)); for (i = 0; i < aip->neqn; i++) { vect_t tmp = VINIT_ZERO; /* calculate volume */ VSCALE(tmp, faces[i].plane_eqn, faces[i].area); faces[i].vol_pyramid = (VDOT(faces[i].pts[0], tmp)/3); volume += faces[i].vol_pyramid; /*Vector from arbit_point to centroid of face, results in h of pyramid */ VSUB2(faces[i].cent_pyramid, faces[i].cent, arbit_point); /*centroid of pyramid is 1/4 up from the bottom */ VSCALE(faces[i].cent_pyramid, faces[i].cent_pyramid, 0.75f); /* now cent_pyramid is back in the polyhedron */ VADD2(faces[i].cent_pyramid, faces[i].cent_pyramid, arbit_point); /* weight centroid of pyramid by pyramid's volume */ VSCALE(faces[i].cent_pyramid, faces[i].cent_pyramid, faces[i].vol_pyramid); /* add cent_pyramid to the centroid of the polyhedron */ VADD2(*cent, *cent, faces[i].cent_pyramid); } /* reverse the weighting */ VSCALE(*cent, *cent, (1/volume)); for (i = 0; i < aip->neqn; i++) { bu_free((char *)faces[i].pts, "rt_arbn_centroid: pts"); } bu_free((char *)faces, "rt_arbn_centroid: faces"); }
/* duplicate bot */ struct rt_bot_internal * dup_bot(struct rt_bot_internal *bot_in) { struct rt_bot_internal *bot; size_t i; RT_BOT_CK_MAGIC(bot_in); BU_ALLOC(bot, struct rt_bot_internal); bot->magic = bot_in->magic; bot->mode = bot_in->mode; bot->orientation = bot_in->orientation; bot->bot_flags = bot_in->bot_flags; bot->num_vertices = bot_in->num_vertices; bot->num_faces = bot_in->num_faces; bot->num_normals = bot_in->num_normals; bot->num_face_normals = bot_in->num_face_normals; bot->faces = (int *)bu_calloc(bot_in->num_faces * 3, sizeof(int), "bot faces"); for (i = 0; i < bot_in->num_faces * 3; i++) { bot->faces[i] = bot_in->faces[i]; } bot->vertices = (fastf_t *)bu_calloc(bot_in->num_vertices * 3, sizeof(fastf_t), "bot verts"); for (i = 0; i < bot_in->num_vertices * 3; i++) { bot->vertices[i] = bot_in->vertices[i]; } if ((bot_in->bot_flags & RT_BOT_PLATE) || (bot_in->bot_flags & RT_BOT_PLATE_NOCOS)) { if (bot_in->thickness) { bot->thickness = (fastf_t *)bu_calloc(bot_in->num_faces, sizeof(fastf_t), "bot thickness"); for (i = 0; i < bot_in->num_faces; i++) { bot->thickness[i] = bot_in->thickness[i]; } } else { bu_bomb("dup_bot(): flag should not say plate but thickness is null\n"); } } if (bot_in->face_mode) { bot->face_mode = bu_bitv_dup(bot_in->face_mode); } if (bot_in->bot_flags & RT_BOT_HAS_SURFACE_NORMALS) { bot->num_normals = bot_in->num_normals; bot->normals = (fastf_t *)bu_calloc(bot_in->num_normals * 3, sizeof(fastf_t), "BOT normals"); bot->face_normals = (int *)bu_calloc(bot_in->num_faces * 3, sizeof(int), "BOT face normals"); memcpy(bot->face_normals, bot_in->face_normals, bot_in->num_faces * 3 * sizeof(int)); } return bot; }
struct shell * Get_outer_shell(struct nmgregion *r, int entityno) { int sol_num; /* IGES solid type number */ int no_of_faces; /* Number of faces in shell */ int face_count = 0; /* Number of faces actually made */ int *face_de; /* Directory sequence numbers for faces */ int *face_orient; /* Orientation of faces */ int face; struct shell *s; /* NMG shell */ struct faceuse **fu; /* list of faceuses */ /* Acquiring Data */ if (dir[entityno]->param <= pstart) { bu_log("Illegal parameter pointer for entity D%07d (%s)\n" , dir[entityno]->direct, dir[entityno]->name); return 0; } Readrec(dir[entityno]->param); Readint(&sol_num, ""); Readint(&no_of_faces, ""); face_de = (int *)bu_calloc(no_of_faces, sizeof(int), "Get_outer_shell face DE's"); face_orient = (int *)bu_calloc(no_of_faces, sizeof(int), "Get_outer_shell orients"); fu = (struct faceuse **)bu_calloc(no_of_faces, sizeof(struct faceuse *), "Get_outer_shell faceuses "); for (face = 0; face < no_of_faces; face++) { Readint(&face_de[face], ""); Readint(&face_orient[face], ""); } s = nmg_msv(r); for (face = 0; face < no_of_faces; face++) { fu[face_count] = Add_face_to_shell(s, (face_de[face]-1)/2, face_orient[face]); if (fu[face_count] != (struct faceuse *)NULL) face_count++; } nmg_gluefaces(fu, face_count, &tol); bu_free((char *)fu, "Get_outer_shell: faceuse list"); bu_free((char *)face_de, "Get_outer_shell: face DE's"); bu_free((char *)face_orient, "Get_outer_shell: face orients"); return s; }
HIDDEN struct halfedge * generate_edge_list(const struct rt_bot_internal *bot) { const size_t num_edges = 3 * bot->num_faces; struct halfedge *edge_list; size_t face_index, edge_index; edge_list = (struct halfedge *)bu_calloc(num_edges, sizeof(struct halfedge), "edge_list"); for (face_index = 0, edge_index = 0; face_index < bot->num_faces; ++face_index) { const int *face = &bot->faces[face_index * 3]; int success = set_edge(&edge_list[edge_index++], face[0], face[1]) && set_edge(&edge_list[edge_index++], face[1], face[2]) && set_edge(&edge_list[edge_index++], face[2], face[0]); if (!success) { bu_free(edge_list, "edge_list"); return NULL; } } qsort(edge_list, num_edges, sizeof(struct halfedge), halfedge_compare); return edge_list; }
void db_alloc_directory_block(struct resource *resp) { struct directory *dp; size_t bytes; RT_CK_RESOURCE(resp); BU_CK_PTBL(&resp->re_directory_blocks); BU_ASSERT_PTR(resp->re_directory_hd, ==, NULL); /* Get a BIG block */ bytes = (size_t)bu_malloc_len_roundup(1024*sizeof(struct directory)); dp = (struct directory *)bu_calloc(1, bytes, "re_directory_blocks from db_alloc_directory_block() " BU_FLSTR); /* Record storage for later */ bu_ptbl_ins(&resp->re_directory_blocks, (long *)dp); while (bytes >= sizeof(struct directory)) { dp->d_magic = RT_DIR_MAGIC; dp->d_forw = resp->re_directory_hd; resp->re_directory_hd = dp; dp++; bytes -= sizeof(struct directory); } }
/* Test against basename UNIX tool */ void automatic_test(const char *input) { char buf_input[1000]; char *ans = NULL; char *res = (char *)bu_calloc(strlen(buf_input), sizeof(char), "automatic_test res"); #ifdef HAVE_BASENAME if (input) bu_strlcpy(buf_input, input, strlen(input)+1); /* build UNIX 'basename' command */ if (!input) ans = basename(NULL); else ans = basename(buf_input); if (!input) bu_basename(res, NULL); else bu_basename(res, buf_input); if (BU_STR_EQUAL(res, ans)) printf("%24s -> %24s [PASSED]\n", input, res); else bu_exit(EXIT_FAILURE, "%24s -> %24s (should be: %s) [FAIL]\n", input, res, ans); bu_free(res, NULL); #else printf("BASENAME not available on this platform\n"); #endif /* FIXME: this does not functionally halt */ }
int rt_superell_export4(struct bu_external *ep, const struct rt_db_internal *ip, double local2mm, const struct db_i *dbip) { struct rt_superell_internal *tip; union record *rec; if (dbip) RT_CK_DBI(dbip); RT_CK_DB_INTERNAL(ip); if (ip->idb_type != ID_SUPERELL) return -1; tip = (struct rt_superell_internal *)ip->idb_ptr; RT_SUPERELL_CK_MAGIC(tip); BU_CK_EXTERNAL(ep); ep->ext_nbytes = sizeof(union record); ep->ext_buf = (uint8_t *)bu_calloc(1, ep->ext_nbytes, "superell external"); rec = (union record *)ep->ext_buf; rec->s.s_id = ID_SOLID; rec->s.s_type = SUPERELL; /* NOTE: This also converts to dbfloat_t */ VSCALE(&rec->s.s_values[0], tip->v, local2mm); VSCALE(&rec->s.s_values[3], tip->a, local2mm); VSCALE(&rec->s.s_values[6], tip->b, local2mm); VSCALE(&rec->s.s_values[9], tip->c, local2mm); printf("SUPERELL: %g %g\n", tip->n, tip->e); rec->s.s_values[12] = tip->n; rec->s.s_values[13] = tip->e; return 0; }
/** @if no * @endif * @brief Parse command line flags. * * This routine handles parsing of all command line options. * * @param ac count of arguments * @param av array of pointers to null-terminated strings * @return index into av of first argument past options (new ac value) */ int parse_args(int ac, char *av[]) { int c; char *strrchr(); char *tmp_basename = (char *)bu_calloc(strlen(av[0]), sizeof(char), "parse_args");; /* Turn off bu_getopt's error messages */ bu_opterr = 0; /* get all the option flags from the command line */ while ((c=bu_getopt(ac, av, options)) != -1) { switch (c) { case 'd': debug = strtol(bu_optarg, NULL, 16); break; case '?': case 'h': default: bu_basename(tmp_basename, av[0]); usage(tmp_basename, "Bad or help flag specified\n"); break; } } bu_free(tmp_basename, "tmp_basename free"); return bu_optind; }
/** * Export an XXX from internal form to external format. Note that * this means converting all integers to Big-Endian format and * floating point data to IEEE double. * * Apply the transformation to mm units as well. */ int rt_xxx_export5(struct bu_external *ep, const struct rt_db_internal *ip, double local2mm, const struct db_i *dbip) { struct rt_xxx_internal *xxx_ip; /* must be double for import and export */ double vec[ELEMENTS_PER_VECT]; RT_CK_DB_INTERNAL(ip); if (ip->idb_type != ID_XXX) return -1; xxx_ip = (struct rt_xxx_internal *)ip->idb_ptr; RT_XXX_CK_MAGIC(xxx_ip); if (dbip) RT_CK_DBI(dbip); BU_CK_EXTERNAL(ep); ep->ext_nbytes = SIZEOF_NETWORK_DOUBLE * ELEMENTS_PER_VECT; ep->ext_buf = (void *)bu_calloc(1, ep->ext_nbytes, "xxx external"); /* Since libwdb users may want to operate in units other than mm, * we offer the opportunity to scale the solid (to get it into mm) * on the way out. */ VSCALE(vec, xxx_ip->v, local2mm); /* Convert from internal (host) to database (network) format */ bu_cv_htond(ep->ext_buf, (unsigned char *)vec, ELEMENTS_PER_VECT); return 0; }
/* * * Get editing string and call ged_red */ int f_red(ClientData UNUSED(clientData), Tcl_Interp *interpreter, int argc, const char *argv[]) { const char **av; struct bu_vls editstring = BU_VLS_INIT_ZERO; CHECK_DBI_NULL; if (argc != 2) { Tcl_Eval(interpreter, "help red"); return TCL_ERROR; } get_editor_string(&editstring); av = (const char **)bu_calloc(4, sizeof(char *), "f_red: av"); av[0] = argv[0]; av[1] = "-E"; av[2] = bu_vls_addr(&editstring); av[3] = argv[1]; if ( ged_red(gedp, 4, (const char **)av) == GED_ERROR ) { Tcl_AppendResult(interpreter, "Error: ", bu_vls_addr(gedp->ged_result_str), (char *)NULL); } else { Tcl_AppendResult(interpreter, bu_vls_addr(gedp->ged_result_str), (char *)NULL); } bu_vls_free(&editstring); bu_free((void *)av, "f_red: av"); return TCL_OK; }
/** * initialize the name list used for stashing destination names */ static void init_list(struct nametbl *l, size_t s) { size_t i, j; l->names = (struct name *)bu_calloc(10, sizeof(struct name), "alloc l->names"); for (i = 0; i < 10; i++) { bu_vls_init(&l->names[i].src); l->names[i].dest = (struct bu_vls *)bu_calloc(s, sizeof(struct bu_vls), "alloc l->names.dest"); for (j = 0; j < s; j++) bu_vls_init(&l->names[i].dest[j]); } l->name_size = s; l->names_len = 10; l->names_used = 0; }
int bu_b64_decode_block(char **output, const char *input, int len) { /* Calculate size of output needed and calloc the memory */ int cnt = 0; char* c; bu_b64_decodestate s; if (!output) return -1; *output = (char *)bu_calloc(((int)(3*len/4) + 4), 8, "Calloc b64 decoding buffer"); c = *output; /*---------- START DECODING ----------*/ /* initialise the decoder state */ bu_b64_init_decodestate(&s); /* decode the input data */ cnt = bu_b64_decode_block_internal(input, len, c, &s); c += cnt; /* note: there is no bu_b64_decode_blockend! */ /*---------- STOP DECODING ----------*/ /* we want to print the decoded data, so null-terminate it: */ *c = '\0'; return cnt; }
char * bu_realpath(const char *path, char *resolved_path) { if (!resolved_path) resolved_path = (char *) bu_calloc(MAXPATHLEN, sizeof(char), "resolved_path alloc"); #if defined(HAVE_WORKING_REALPATH_FUNCTION) { char *dirpath = NULL; dirpath = realpath(path, resolved_path); if (!dirpath) { /* if path lookup failed, resort to simple copy */ bu_strlcpy(resolved_path, path, (size_t)MAXPATHLEN); } } #elif defined(HAVE_GETFULLPATHNAME) /* Best solution currently available for Windows * See https://www.securecoding.cert.org/confluence/display/seccode/FIO02-C.+Canonicalize+path+names+originating+from+untrusted+sources */ GetFullPathName(path, MAXPATHLEN, resolved_path, NULL); #else /* Last resort - if NOTHING is defined, do a simple copy */ bu_strlcpy(resolved_path, path, (size_t)MAXPATHLEN); #endif return resolved_path; }
char * bu_b64_encode_block(const char *input, int len) { /* Calculate size of output needed and calloc the memory */ char *output = (char *)bu_calloc((((int)(4*len/3)) + 4), 8, "Calloc b64 buffer"); char *c = output; int cnt = 0; bu_b64_encodestate s; /*---------- START ENCODING ----------*/ /* initialise the encoder state */ bu_b64_init_encodestate(&s); /* gather data from the input and send it to the output */ cnt = bu_b64_encode_block_internal(input, len, c, &s); c += cnt; /* since we have encoded all of the input string we intend * to encode, we know that there is no more input data; * finalise the encoding */ cnt = bu_b64_encode_blockend(c, &s); c += cnt; /*---------- STOP ENCODING ----------*/ /* we want to print the encoded data, so null-terminate it: */ *c = '\0'; return output; }
/** * Call parse_args to handle command line arguments first, then * process input. */ int main(int ac, char *av[]) { /** @struct rt_i * This structure contains some global state information for librt */ struct rt_i *rtip; struct db_tree_state init_state; /* state table for the hierarchy walker */ char idbuf[1024] = {0}; /* Database title */ int arg_count; char *tmp_basename = (char *)bu_calloc(strlen(av[0]), sizeof(char), "walk_example tmp_basename"); /** @struct user_data * This is an example structure. * It contains anything you want to have available in the region/leaf processing routines */ struct user_data { int stuff; } user_data; arg_count = parse_args(ac, av); if ((ac - arg_count) < 1) { bu_basename(tmp_basename, av[0]); usage(tmp_basename, "bad argument count"); } bu_free(tmp_basename, "tmp_basename free"); /* * Build an index of what's in the database. * rt_dirbuild() returns an "instance" pointer which describes * the database. It also gives you back the * title string in the header (ID) record. */ rtip = rt_dirbuild(av[arg_count], idbuf, sizeof(idbuf)); if (rtip == RTI_NULL) { bu_exit(2, "%s: rt_dirbuild failure\n", av[0]); } arg_count++; init_state = rt_initial_tree_state; db_walk_tree(rtip->rti_dbip, /* database instance */ ac-arg_count, /* number of trees to get from the database */ (const char **)&av[arg_count], 1, /* number of cpus to use */ &init_state, region_start, region_end, leaf_func, (void *)&user_data); /* at this point you can do things with the geometry you have obtained */ return 0; }
void fast4_arg_process(const char *args) { if (!args) return; char *input = bu_strdup(args); char **argv = (char **)bu_calloc(strlen(args) + 1, sizeof(char *), "argv array"); int argc = bu_argv_from_string(argv, strlen(args), input); option::Stats stats(Fast4Usage, argc, argv); option::Option *options = (option::Option *)bu_calloc(stats.options_max, sizeof(option::Option), "options"); option::Option *buffer= (option::Option *)bu_calloc(stats.buffer_max, sizeof(option::Option), "options"); option::Parser parse(Fast4Usage, argc, argv, options, buffer); if (options[FAST4_WARN_DEFAULT_NAMES]) { bu_log("FASTGEN 4 opt: %s:%s\n", options[FAST4_WARN_DEFAULT_NAMES].name, options[FAST4_WARN_DEFAULT_NAMES].arg); } bu_free(input, "input"); bu_free(options, "free options"); bu_free(buffer, "free buffer"); }
/** * This one assumes that making all the parameters null is fine. */ void rt_generic_make(const struct rt_functab *ftp, struct rt_db_internal *intern) { intern->idb_type = ftp - OBJ; intern->idb_major_type = DB5_MAJORTYPE_BRLCAD; BU_ASSERT(&OBJ[intern->idb_type] == ftp); intern->idb_meth = ftp; intern->idb_ptr = bu_calloc(1, (unsigned int)ftp->ft_internal_size, "rt_generic_make"); *((uint32_t *)(intern->idb_ptr)) = ftp->ft_internal_magic; }
void rt_arbn_surf_area(fastf_t *area, const struct rt_db_internal *ip) { struct poly_face *faces; struct rt_arbn_internal *aip = (struct rt_arbn_internal *)ip->idb_ptr; size_t i; /* allocate array of face structs */ faces = (struct poly_face *)bu_calloc(aip->neqn, sizeof(struct poly_face), "arbn_surf_area: faces"); for (i = 0; i < aip->neqn; i++) { /* allocate array of pt structs, max number of verts per faces = (# of faces) - 1 */ faces[i].pts = (point_t *)bu_calloc(aip->neqn - 1, sizeof(point_t), "arbn_surf_area: pts"); } rt_arbn_faces_area(faces, aip); for (i = 0; i < aip->neqn; i++) { *area += faces[i].area; bu_free((char *)faces[i].pts, "rt_arbn_surf_area: pts"); } bu_free((char *)faces, "rt_arbn_surf_area: faces"); }
int ged_wcodes(struct ged *gedp, int argc, const char *argv[]) { int i; int status; FILE *fp; struct directory *dp; static const char *usage = "filename object(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); /* must be wanting help */ if (argc == 1) { bu_vls_printf(gedp->ged_result_str, "Usage: %s %s", argv[0], usage); return GED_HELP; } if (argc == 2) { bu_vls_printf(gedp->ged_result_str, "Usage: %s %s", argv[0], usage); return GED_ERROR; } if ((fp = fopen(argv[1], "w")) == NULL) { bu_vls_printf(gedp->ged_result_str, "%s: Failed to open file - %s", argv[0], argv[1]); return GED_ERROR; } path = (struct directory **)bu_calloc(PATH_STEP, sizeof(struct directory *), "alloc initial path"); path_capacity = PATH_STEP; for (i = 2; i < argc; ++i) { if ((dp = db_lookup(gedp->ged_wdbp->dbip, argv[i], LOOKUP_NOISY)) != RT_DIR_NULL) { status = wcodes_printcodes(gedp, fp, dp, 0); if (status == GED_ERROR) { (void)fclose(fp); return GED_ERROR; } } } (void)fclose(fp); bu_free(path, "dealloc path"); path = NULL; path_capacity = 0; return GED_OK; }
/** * B U _ P T B L _ I N I T * * Initialize struct & get storage for table. * Recommend 8 or 64 for initial len. */ void bu_ptbl_init(struct bu_ptbl *b, int len, const char *str) { if (bu_debug & BU_DEBUG_PTBL) bu_log("bu_ptbl_init(%8x, len=%d, %s)\n", b, len, str); BU_LIST_INIT(&b->l); b->l.magic = BU_PTBL_MAGIC; if ( len <= 0 ) len = 64; b->blen = len; b->buffer = (long **)bu_calloc(b->blen, sizeof(long *), str); b->end = 0; }
int rt_arbn_export4(struct bu_external *ep, const struct rt_db_internal *ip, double local2mm, const struct db_i *dbip) { struct rt_arbn_internal *aip; union record *rec; size_t ngrans; size_t i; /* scaling buffer must be double, not fastf_t */ double *sbuf; double *sp; if (dbip) RT_CK_DBI(dbip); RT_CK_DB_INTERNAL(ip); if (ip->idb_type != ID_ARBN) return -1; aip = (struct rt_arbn_internal *)ip->idb_ptr; RT_ARBN_CK_MAGIC(aip); if (aip->neqn <= 0) return -1; /* * The network format for a double is 8 bytes and there are 4 * doubles per plane equation. */ ngrans = (aip->neqn * 8 * ELEMENTS_PER_PLANE + sizeof(union record)-1) / sizeof(union record); BU_CK_EXTERNAL(ep); ep->ext_nbytes = (ngrans + 1) * sizeof(union record); ep->ext_buf = (uint8_t *)bu_calloc(1, ep->ext_nbytes, "arbn external"); rec = (union record *)ep->ext_buf; rec[0].n.n_id = DBID_ARBN; *(uint32_t *)rec[0].n.n_neqn = htonl(aip->neqn); *(uint32_t *)rec[0].n.n_grans = htonl(ngrans); /* Take the data from the caller, and scale it, into sbuf */ sp = sbuf = (double *)bu_malloc( aip->neqn * sizeof(double) * ELEMENTS_PER_PLANE, "arbn temp"); for (i = 0; i < aip->neqn; i++) { /* Normal is unscaled, should have unit length; d is scaled */ *sp++ = aip->eqn[i][X]; *sp++ = aip->eqn[i][Y]; *sp++ = aip->eqn[i][Z]; *sp++ = aip->eqn[i][W] * local2mm; } bu_cv_htond((unsigned char *)&rec[1], (unsigned char *)sbuf, aip->neqn * ELEMENTS_PER_PLANE); bu_free((char *)sbuf, "arbn temp"); return 0; /* OK */ }
/* * The only argument is the name of the database file. */ void ph_dirbuild(struct pkg_conn *UNUSED(pc), char *buf) { long max_argc = 0; char **argv = NULL; struct rt_i *rtip = NULL; size_t n = 0; if (debug) fprintf(stderr, "ph_dirbuild: %s\n", buf); for (n = 0; n < strlen(buf); n++) { if (isspace((int)buf[n])) max_argc++; } argv = (char **)bu_calloc(max_argc+1, sizeof(char *), "alloc argv"); if ((bu_argv_from_string(argv, max_argc, buf)) <= 0) { /* No words in input */ (void)free(buf); bu_free(argv, "free argv"); return; } if (seen_dirbuild) { bu_log("ph_dirbuild: MSG_DIRBUILD already seen, ignored\n"); (void)free(buf); bu_free(argv, "free argv"); return; } title_file = bu_strdup(argv[0]); bu_free(argv, "free argv"); /* Build directory of GED database */ if ((rtip=rt_dirbuild(title_file, idbuf, sizeof(idbuf))) == RTI_NULL) bu_exit(2, "ph_dirbuild: rt_dirbuild(%s) failure\n", title_file); APP.a_rt_i = rtip; seen_dirbuild = 1; /* * Initialize all the per-CPU memory resources. * Go for the max, as TCL interface may change npsw as we run. */ for (n=0; n < MAX_PSW; n++) { rt_init_resource(&resource[n], n, rtip); bn_rand_init(resource[n].re_randptr, n); } if (pkg_send(MSG_DIRBUILD_REPLY, idbuf, strlen(idbuf)+1, pcsrv) < 0) fprintf(stderr, "MSG_DIRBUILD_REPLY error\n"); }
HIDDEN int rt_pattern_rect_perspgrid(fastf_t **rays, size_t *ray_cnt, const point_t center_pt, const vect_t dir, const vect_t a_vec, const vect_t b_vec, const fastf_t a_theta, const fastf_t b_theta, const fastf_t a_num, const fastf_t b_num) { int count = 0; vect_t rdir; vect_t a_dir, b_dir; fastf_t x, y; fastf_t a_length = tan(a_theta); fastf_t b_length = tan(b_theta); fastf_t a_inc = 2 * a_length / (a_num - 1); fastf_t b_inc = 2 * b_length / (b_num - 1); VMOVE(a_dir, a_vec); VUNITIZE(a_dir); VMOVE(b_dir, b_vec); VUNITIZE(b_dir); /* Find out how much memory we'll need and get it */ for (y = -b_length; y <= b_length + BN_TOL_DIST; y += b_inc) { for (x = -a_length; x <= a_length + BN_TOL_DIST; x += a_inc) { count++; } } *(rays) = (fastf_t *)bu_calloc(sizeof(fastf_t) * 6, count + 1, "rays"); /* Now that we have memory, reset count so it can * be used to index into the array */ count = 0; /* This adds BN_TOL_DIST to the *_length variables in the * condition because in some cases, floating-point problems can * make extremely close numbers compare incorrectly. */ for (y = -b_length; y <= b_length + BN_TOL_DIST; y += b_inc) { for (x = -a_length; x <= a_length + BN_TOL_DIST; x += a_inc) { VJOIN2(rdir, dir, x, a_dir, y, b_dir); VUNITIZE(rdir); (*rays)[6*count] = center_pt[0]; (*rays)[6*count+1] = center_pt[1]; (*rays)[6*count+2] = center_pt[2]; (*rays)[6*count+3] = rdir[0]; (*rays)[6*count+4] = rdir[1]; (*rays)[6*count+5] = rdir[2]; count++; } } *(ray_cnt) = count; return count; }