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; }
void Add_bot_face(int pt1, int pt2, int pt3, fastf_t thick, int pos) { if ( pt1 == pt2 || pt2 == pt3 || pt1 == pt3 ) { bu_log( "Add_bot_face: ignoring degenerate triangle in group %d component %d\n", group_id, comp_id ); return; } if ( pos == 0 ) /* use default */ pos = POS_FRONT; if ( mode == PLATE_MODE ) { if ( pos != POS_CENTER && pos != POS_FRONT ) { bu_log( "Add_bot_face: illegal postion parameter (%d), must be one or two (ignoring face for group %d component %d)\n", pos, group_id, comp_id ); return; } } if ( face_count >= face_size ) { face_size += GRID_BLOCK; if (bu_debug&BU_DEBUG_MEM_CHECK && bu_mem_barriercheck() ) bu_log( "memory corrupted before realloc of faces, thickness, and facemode\n" ); faces = (int *)bu_realloc( (void *)faces, face_size*3*sizeof( int ), "faces" ); thickness = (fastf_t *)bu_realloc( (void *)thickness, face_size*sizeof( fastf_t ), "thickness" ); facemode = (char *)bu_realloc( (void *)facemode, face_size*sizeof( char ), "facemode" ); if (bu_debug&BU_DEBUG_MEM_CHECK && bu_mem_barriercheck() ) bu_log( "memory corrupted after realloc of faces, thickness, and facemode\n" ); } faces[face_count*3] = pt1; faces[face_count*3+1] = pt2; faces[face_count*3+2] = pt3; if ( mode == PLATE_MODE ) { thickness[face_count] = thick; facemode[face_count] = pos; } else { thickness[face_count] = 0, 0; facemode[face_count] = 0; } face_count++; if (bu_debug&BU_DEBUG_MEM_CHECK && bu_mem_barriercheck() ) bu_log( "memory corrupted at end of Add_bot_face()\n" ); }
HIDDEN int under_sample(icv_image_t* bif, unsigned int factor) { double *data_p, *res_p; int x, y, widthstep; if (UNLIKELY(factor < 1)) { bu_log("Cannot shrink image to 0 factor, factor should be a positive value."); return -1; } widthstep = bif->width*bif->channels; res_p = data_p = bif->data; for (y = 0; y < bif->height; y += factor) { data_p = bif->data + widthstep*y; for (x = 0; x < bif->width; x += factor, res_p += bif->channels, data_p += factor * bif->channels) VMOVEN(res_p, data_p, bif->channels); } bif->width = (int)bif->width/factor; bif->height = (int)bif->height/factor; bif->data = (double *)bu_realloc(bif->data, (size_t)(bif->width*bif->height*bif->channels)*sizeof(double), "under_sample : Reallocation"); return 0; }
/* CSG objects are skipped. BOTs are processed but stored outside * tree. This leaf-tess function is used when we want to output * BOTs directly to the VRML file without performing a boolean * evaluation. */ union tree * leaf_tess2(struct db_tree_state *UNUSED(tsp), const struct db_full_path *UNUSED(pathp), struct rt_db_internal *ip, void *client_data) { struct rt_bot_internal *bot; struct plate_mode *pmp = (struct plate_mode *)client_data; if (ip->idb_type != ID_BOT) { return (union tree *)NULL; } bot = (struct rt_bot_internal *)ip->idb_ptr; RT_BOT_CK_MAGIC(bot); if (pmp->array_size <= pmp->num_bots) { struct rt_bot_internal **bots_tmp; pmp->array_size += 5; bots_tmp = (struct rt_bot_internal **)bu_realloc((void *)pmp->bots, pmp->array_size * sizeof(struct rt_bot_internal *), "pmp->bots"); pmp->bots = bots_tmp; } /* walk tree will free the bot, so we need a copy */ pmp->bots[pmp->num_bots] = dup_bot(bot); pmp->num_bots++; return (union tree *)NULL; }
void attrib_add(char *a, int *prep) { char *p; if (!a) { bu_log("attrib_add null arg\n"); return; /* null char ptr */ } p = strtok(a, "\t "); while (p) { /* make sure we have space */ if (!a_tab.attrib || a_tab.attrib_use >= (a_tab.attrib_cnt-1)) { a_tab.attrib_cnt += 16; a_tab.attrib = (char **)bu_realloc(a_tab.attrib, a_tab.attrib_cnt * sizeof(char *), "attrib_tab"); } /* add the attribute name(s) */ a_tab.attrib[a_tab.attrib_use] = bu_strdup(db5_standard_attribute(db5_standardize_attribute(p))); /* bu_log("attrib[%d]=\"%s\"\n", attrib_use, attrib[attrib_use]); */ a_tab.attrib[++a_tab.attrib_use] = (char *)NULL; p = strtok((char *)NULL, "\t "); *prep = 1; } }
/** * b u _ v l s _ e x t e n d * * Ensure that the provided VLS has at least 'extra' characters of * space available. */ void bu_vls_extend(register struct bu_vls *vp, unsigned int extra) { BU_CK_VLS(vp); /* increment by at least 40 bytes */ if ( extra < _VLS_ALLOC_MIN ) extra = _VLS_ALLOC_MIN; /* first time allocation */ if ( vp->vls_max <= 0 || vp->vls_str == (char *)0 ) { vp->vls_max = extra; vp->vls_str = (char *)bu_malloc( (size_t)vp->vls_max, bu_vls_message ); vp->vls_len = 0; vp->vls_offset = 0; *vp->vls_str = '\0'; return; } /* need more space? */ if ( vp->vls_offset + vp->vls_len + extra >= (size_t)vp->vls_max ) { vp->vls_max += extra; if ( vp->vls_max < _VLS_ALLOC_STEP ) { /* extend to at least this much */ vp->vls_max = _VLS_ALLOC_STEP; } vp->vls_str = (char *)bu_realloc( vp->vls_str, (size_t)vp->vls_max, bu_vls_message ); } }
void texture_stack_push(texture_t *texture, texture_t *texture_new) { texture_stack_t *td; td = (texture_stack_t *)texture->data; td->list = (texture_t **)bu_realloc(td->list, sizeof(texture_t *)*(td->num+1), "texture data"); td->list[td->num++] = texture_new; }
/** * Find a block of database storage of "count" granules. * * Returns: * 0 OK * non-0 failure */ int db_alloc(register struct db_i *dbip, register struct directory *dp, size_t count) { size_t len; union record rec; RT_CK_DBI(dbip); RT_CK_DIR(dp); if (RT_G_DEBUG&DEBUG_DB) bu_log("db_alloc(%s) %p, %p, count=%zu\n", dp->d_namep, (void *)dbip, (void *)dp, count); if (count <= 0) { bu_log("db_alloc(0)\n"); return -1; } if (dp->d_flags & RT_DIR_INMEM) { if (dp->d_un.ptr) { dp->d_un.ptr = bu_realloc(dp->d_un.ptr, count * sizeof(union record), "db_alloc() d_un.ptr"); } else { dp->d_un.ptr = bu_malloc(count * sizeof(union record), "db_alloc() d_un.ptr"); } dp->d_len = count; return 0; } if (dbip->dbi_read_only) { bu_log("db_alloc on READ-ONLY file\n"); return -1; } while (1) { len = rt_memalloc(&(dbip->dbi_freep), (unsigned)count); if (len == 0L) { /* No contiguous free block, append to file */ if ((dp->d_addr = dbip->dbi_eof) == RT_DIR_PHONY_ADDR) { bu_log("db_alloc: bad EOF\n"); return -1; } dp->d_len = count; dbip->dbi_eof += (off_t)(count * sizeof(union record)); dbip->dbi_nrec += count; break; } dp->d_addr = (off_t)(len * sizeof(union record)); dp->d_len = count; if (db_get(dbip, dp, &rec, 0, 1) < 0) return -1; if (rec.u_id != ID_FREE) { bu_log("db_alloc(): len %ld non-FREE (id %d), skipping\n", len, rec.u_id); continue; } } /* Clear out ALL the granules, for safety */ return db_zapper(dbip, dp, 0); }
/* add all hit point info to info list */ HIDDEN int add_hit_pnts(struct application *app, struct partition *partH, struct seg *UNUSED(segs)) { struct partition *pp; struct soltab *stp; /*point_t hit_pnt; vect_t hit_normal;*/ struct rt_point_container *c = (struct rt_point_container *)(app->a_uptr); struct npoints *npt; if (c->pnt_cnt > c->capacity-1) { c->capacity *= 4; c->pts = (struct npoints *)bu_realloc((char *)c->pts, c->capacity * sizeof(struct npoints), "enlarge results array"); } RT_CK_APPLICATION(app); /*struct bu_vls *fp = (struct bu_vls *)(app->a_uptr);*/ /* add all hit points */ for (pp = partH->pt_forw; pp != partH; pp = pp->pt_forw) { npt = &(c->pts[c->pnt_cnt]); /* add "in" hit point info */ stp = pp->pt_inseg->seg_stp; /* hack fix for bad tgc surfaces */ if (bu_strncmp("rec", stp->st_meth->ft_label, 3) == 0 || bu_strncmp("tgc", stp->st_meth->ft_label, 3) == 0) { /* correct invalid surface number */ if (pp->pt_inhit->hit_surfno < 1 || pp->pt_inhit->hit_surfno > 3) { pp->pt_inhit->hit_surfno = 2; } if (pp->pt_outhit->hit_surfno < 1 || pp->pt_outhit->hit_surfno > 3) { pp->pt_outhit->hit_surfno = 2; } } VJOIN1(npt->in.p, app->a_ray.r_pt, pp->pt_inhit->hit_dist, app->a_ray.r_dir); RT_HIT_NORMAL(npt->in.n, pp->pt_inhit, stp, &(app->a_ray), pp->pt_inflip); npt->in.is_set = 1; //bu_vls_printf(fp, "%f %f %f %f %f %f\n", hit_pnt[0], hit_pnt[1], hit_pnt[2], hit_normal[0], hit_normal[1], hit_normal[2]); /* add "out" hit point info (unless half-space) */ stp = pp->pt_inseg->seg_stp; if (bu_strncmp("half", stp->st_meth->ft_label, 4) != 0) { VJOIN1(npt->out.p, app->a_ray.r_pt, pp->pt_outhit->hit_dist, app->a_ray.r_dir); RT_HIT_NORMAL(npt->out.n, pp->pt_outhit, stp, &(app->a_ray), pp->pt_outflip); npt->out.is_set = 1; //bu_vls_printf(fp, "%f %f %f %f %f %f\n", hit_pnt[0], hit_pnt[1], hit_pnt[2], hit_normal[0], hit_normal[1], hit_normal[2]); } c->pnt_cnt++; } return 1; }
HIDDEN int wcodes_printcodes(struct ged *gedp, FILE *fp, struct directory *dp, size_t pathpos) { size_t i; struct rt_db_internal intern; struct rt_comb_internal *comb; int id; if (!(dp->d_flags & RT_DIR_COMB)) return GED_OK; if ((id=rt_db_get_internal(&intern, dp, gedp->ged_wdbp->dbip, (matp_t)NULL, &rt_uniresource)) < 0) { bu_vls_printf(gedp->ged_result_str, "Cannot get records for %s\n", dp->d_namep); return GED_ERROR; } if (id != ID_COMBINATION) { intern.idb_meth->ft_ifree(&intern); return GED_OK; } comb = (struct rt_comb_internal *)intern.idb_ptr; RT_CK_COMB(comb); if (comb->region_flag) { fprintf(fp, "%-6ld %-3ld %-3ld %-4ld ", comb->region_id, comb->aircode, comb->GIFTmater, comb->los); for (i =0 ; i < pathpos; i++) fprintf(fp, "/%s", path[i]->d_namep); fprintf(fp, "/%s\n", dp->d_namep); intern.idb_meth->ft_ifree(&intern); return GED_OK; } if (comb->tree) { if (pathpos >= path_capacity) { path_capacity += PATH_STEP; path = (struct directory **)bu_realloc(path, sizeof(struct directory *) * path_capacity, "realloc path bigger"); } path[pathpos] = dp; db_tree_funcleaf(gedp->ged_wdbp->dbip, comb, comb->tree, wcodes_printnode, (void *)fp, (void *)&pathpos, (void *)gedp, (void *)gedp); } intern.idb_meth->ft_ifree(&intern); return GED_OK; }
void db_extend_full_path(struct db_full_path *pathp, size_t incr) { size_t newlen; RT_CK_FULL_PATH(pathp); if (pathp->fp_maxlen <= 0) { pathp->fp_len = 0; pathp->fp_maxlen = incr; pathp->fp_names = (struct directory **)bu_malloc( pathp->fp_maxlen * sizeof(struct directory *), "empty fp_names extension"); pathp->fp_bool = (int *)bu_calloc(pathp->fp_maxlen, sizeof(int), "empty fp_bool bool extension"); pathp->fp_mat = (matp_t *)bu_calloc(pathp->fp_maxlen, sizeof(matp_t), "db_full_path matrices array"); return; } newlen = pathp->fp_len + incr; if (pathp->fp_maxlen < newlen) { pathp->fp_maxlen = newlen+1; pathp->fp_names = (struct directory **)bu_realloc( (char *)pathp->fp_names, pathp->fp_maxlen * sizeof(struct directory *), "fp_names extension"); pathp->fp_bool = (int *)bu_realloc( (char *)pathp->fp_bool, pathp->fp_maxlen * sizeof(int), "fp_names bool extension"); pathp->fp_mat = (matp_t *)bu_realloc( (char *)pathp->fp_mat, pathp->fp_maxlen * sizeof(matp_t), "enlarged db_full_path matrices array"); } }
/* routine to add a new triangle to the current part */ void add_triangle( int v[3] ) { if ( curr_tri >= max_tri ) { /* allocate more memory for triangles */ max_tri += TRI_BLOCK; part_tris = (int *)bu_realloc( part_tris, sizeof(int) * max_tri * 3, "part_tris" ); } /* fill in triangle info */ VMOVE( &part_tris[curr_tri*3], v ); /* increment count */ curr_tri++; }
/* * R T _ H T B L _ G E T * * Allocate another hit structure, extending the array if necessary. */ struct hit * rt_htbl_get(struct rt_htbl *b) { RT_CK_HTBL(b); if ( b->end >= b->blen ) { /* Increase size of array */ b->hits = (struct hit *)bu_realloc( (char *)b->hits, sizeof(struct hit) * (b->blen *= 4), "rt_htbl.hits[]" ); } /* There is sufficient room */ return &b->hits[b->end++]; }
static void Spush(struct node *ptr) { sjtop++; if (sjtop == sstklen) { sstklen += STKBLK; sstk_p = (struct node **)bu_realloc((char *)sstk_p, sstklen*sizeof(struct node *), "Spush: sstk_p"); if (sstk_p == NULL) { bu_log("Cannot reallocate stack space\n"); perror("Spush"); bu_exit(1, NULL); } } sstk_p[sjtop] = ptr; }
static struct fbm_spec * build_spec_tbl(double h_val, double lacunarity, double octaves) { struct fbm_spec *ep; double *spec_wgts; double frequency; int i; /* The right spectral weights table for these parameters has not been * pre-computed. As a result, we compute the table now and save it * with the knowledge that we'll likely want it again later. */ /* allocate storage for new tables if needed */ if (etbl_next >= etbl_size) { if (etbl_size) { etbl_size *= 2; etbl = (struct fbm_spec *)bu_realloc((char *)etbl, etbl_size*sizeof(struct fbm_spec), "spectral weights table"); } else etbl = (struct fbm_spec *)bu_calloc(etbl_size = 10, sizeof(struct fbm_spec), "spectral weights table"); if (!etbl) abort(); } /* set up the next available table */ ep = &etbl[etbl_next]; ep->magic = MAGIC_fbm_spec_wgt; ep->octaves = octaves; ep->h_val = h_val; ep->lacunarity = lacunarity; spec_wgts = ep->spec_wgts = (double *)bu_malloc( ((int)(octaves+1)) * sizeof(double), "spectral weights" ); /* precompute and store spectral weights table */ for (frequency = 1.0, i=0; i < octaves; i++) { /* compute weight for each frequency */ spec_wgts[i] = pow(frequency, -h_val); frequency *= lacunarity; } etbl_next++; /* saved for last in case we're running multi-threaded */ return ep; }
/** * B U _ P T B L _ C A T * * Catenate one table onto end of another. * There is no checking for duplication. */ void bu_ptbl_cat(struct bu_ptbl *dest, const struct bu_ptbl *src) { BU_CK_PTBL(dest); BU_CK_PTBL(src); if (bu_debug & BU_DEBUG_PTBL) bu_log("bu_ptbl_cat(%8x, %8x)\n", dest, src); if ((dest->blen - dest->end) < src->end) { dest->blen = (dest->blen + src->end) * 2 + 8; dest->buffer = (long **)bu_realloc( (char *)dest->buffer, dest->blen * sizeof(long *), "bu_ptbl.buffer[] (cat)"); } memcpy((char *)&dest->buffer[dest->end], (char *)src->buffer, src->end*sizeof(long *)); dest->end += src->end; }
void insert_id(int id) { int i; for (i = 0; i < ident_count; i++) { if (idents[i] == id) return; } if (ident_count == ident_length) { idents = (int *)bu_realloc((char *)idents, (ident_length + IDENT_BLOCK)*sizeof(int), "insert_id: idents"); ident_length += IDENT_BLOCK; } idents[ident_count] = id; ident_count++; }
HIDDEN int shrink_image(icv_image_t* bif, unsigned int factor) { double *data_p, *res_p; /**< input and output pointers */ double *p; unsigned int facsq, py, px; int x, y, c; size_t widthstep = bif->width*bif->channels; if (UNLIKELY(factor < 1)) { bu_log("Cannot shrink image to 0 factor, factor should be a positive value."); return -1; } facsq = factor*factor; res_p = bif->data; p = (double *)bu_malloc(bif->channels*sizeof(double), "shrink_image : Pixel Values Temp Buffer"); for (y = 0; y < bif->height; y += factor) for (x = 0; x < bif->width; x += factor) { for (c = 0; c < bif->channels; c++) { p[c]= 0; } for (py = 0; py < factor; py++) { data_p = bif->data + (y+py)*widthstep; for (px = 0; px < factor; px++) { for (c = 0; c < bif->channels; c++) { p[c] += *data_p++; } } } for (c = 0; c < bif->channels; c++) *res_p++ = p[c]/facsq; } bif->width = (int)bif->width/factor; bif->height = (int)bif->height/factor; bif->data = (double *)bu_realloc(bif->data, (size_t)(bif->width*bif->height*bif->channels)*sizeof(double), "shrink_image : Reallocation"); return 0; }
static void Apush(char *ptr) { int i; jtop++; if (jtop == stklen) { stklen += STKBLK; stk = (char **)bu_realloc((char *)stk, stklen*sizeof(char *), "Apush: stk"); if (stk == NULL) { bu_log("Cannot reallocate stack space\n"); perror("Apush"); bu_exit(1, NULL); } for (i = jtop; i < stklen; i++) stk[i] = NULL; } stk[jtop] = ptr; }
/** * B U _ P T B L _ C A T _ U N I Q * * Catenate one table onto end of another, * ensuring that no entry is duplicated. * Duplications between multiple items in 'src' are not caught. * The search is a nasty n**2 one. The tables are expected to be short. */ void bu_ptbl_cat_uniq(struct bu_ptbl *dest, const struct bu_ptbl *src) { register long **p; BU_CK_PTBL(dest); BU_CK_PTBL(src); if (bu_debug & BU_DEBUG_PTBL) bu_log("bu_ptbl_cat_uniq(%8x, %8x)\n", dest, src); /* Assume the worst, ensure sufficient space to add all 'src' items */ if ((dest->blen - dest->end) < src->end) { dest->buffer = (long **)bu_realloc( (char *)dest->buffer, sizeof(long *)*(dest->blen += src->blen + 8), "bu_ptbl.buffer[] (cat_uniq)"); } for ( BU_PTBL_FOR( p, (long **), src ) ) { bu_ptbl_ins_unique( dest, *p ); } }
/** * B U _ P T B L _ I N S * * Append/Insert a (long *) item to/into the table. */ int bu_ptbl_ins(struct bu_ptbl *b, long int *p) { register int i; BU_CK_PTBL(b); if (bu_debug & BU_DEBUG_PTBL) bu_log("bu_ptbl_ins(%8x, %8x)\n", b, p); if (b->blen == 0) bu_ptbl_init(b, 64, "bu_ptbl_ins() buffer"); if (b->end >= b->blen) { b->buffer = (long **)bu_realloc( (char *)b->buffer, sizeof(p)*(b->blen *= 4), "bu_ptbl.buffer[] (ins)" ); } i=b->end++; b->buffer[i] = p; return(i); }
void do_grid(char *line) { int grid_no; fastf_t x, y, z; if ( RT_G_DEBUG&DEBUG_MEM_FULL && bu_mem_barriercheck() ) bu_log( "ERROR: bu_mem_barriercheck failed at start of do_grid\n" ); bu_strlcpy(field, &line[8], sizeof(field)); grid_no = atoi( field ); if ( grid_no < 1 ) { bu_log( "ERROR: grid id number = %d\n", grid_no ); bu_bomb( "BAD GRID ID NUMBER\n" ); } bu_strlcpy(field, &line[24], sizeof(field)); x = atof( field ); bu_strlcpy(field, &line[32], sizeof(field)); y = atof( field ); bu_strlcpy(field, &line[40], sizeof(field)); z = atof( field ); while ( grid_no > grid_size - 1 ) { grid_size += GRID_BLOCK; grid_pts = (point_t *)bu_realloc( (char *)grid_pts, grid_size * sizeof( point_t ), "fast4-g: grid_pts" ); } VSET( grid_pts[grid_no], x*25.4, y*25.4, z*25.4 ); if ( grid_no > max_grid_no ) max_grid_no = grid_no; if ( RT_G_DEBUG&DEBUG_MEM_FULL && bu_mem_barriercheck() ) bu_log( "ERROR: bu_mem_barriercheck failed at end of do_grid\n" ); }
int soup_add_face_precomputed(struct soup_s *s, point_t a, point_t b , point_t c, plane_t d, uint32_t foo) { struct face_s *f; vect_t e1, e2, x; VSUB2(e1, b, a); VSUB2(e2, c, a); VCROSS(x, e1, e2); /* grow face array if needed */ if (s->nfaces >= s->maxfaces) s->faces = (struct face_s *)bu_realloc(s->faces, (s->maxfaces += faces_per_page) * sizeof(struct face_s), "bot soup faces"); f = s->faces + s->nfaces; VMOVE(f->vert[0], a); if (VDOT(x, d) <= 0) { VMOVE(f->vert[1], b); VMOVE(f->vert[2], c); } else { VMOVE(f->vert[1], c); VMOVE(f->vert[2], b); } HMOVE(f->plane, d); /* solve the bounding box (should this be VMINMAX?) */ VMOVE(f->min, f->vert[0]); VMOVE(f->max, f->vert[0]); VMIN(f->min, f->vert[1]); VMAX(f->max, f->vert[1]); VMIN(f->min, f->vert[2]); VMAX(f->max, f->vert[2]); /* fluff the bounding box for fp fuzz */ f->min[X]-=.1; f->min[Y]-=.1; f->min[Z]-=.1; f->max[X]+=.1; f->max[Y]+=.1; f->max[Z]+=.1; f->foo = foo; s->nfaces++; return 0; }
/** * add a new name to the name list */ static int add_to_list(struct nametbl *l, char *name) { size_t i, j; /* * add more slots if adding 1 more new name will fill up all the * available slots. */ if (l->names_len == (l->names_used+1)) { l->names_len += 10; l->names = (struct name *)bu_realloc(l->names, sizeof(struct name)*(l->names_len+1), "realloc l->names"); for (i = l->names_used; i < l->names_len; i++) { bu_vls_init(&l->names[i].src); l->names[i].dest = (struct bu_vls *)bu_calloc(l->name_size, sizeof(struct bu_vls), "alloc l->names.dest"); for (j = 0; j < l->name_size; j++) bu_vls_init(&l->names[i].dest[j]); } } bu_vls_strcpy(&l->names[l->names_used++].src, name); return l->names_used-1; /* return number of available slots */ }
union tree * leaf_tess(struct db_tree_state *tsp, const struct db_full_path *pathp, struct rt_db_internal *ip, void *client_data) { struct rt_bot_internal *bot; struct plate_mode *pmp = (struct plate_mode *)client_data; BARRIER_CHECK; if ( ip->idb_type != ID_BOT ) { pmp->num_nonbots++; return nmg_booltree_leaf_tess(tsp, pathp, ip, client_data); } bot = (struct rt_bot_internal *)ip->idb_ptr; RT_BOT_CK_MAGIC( bot ); if ( bot->mode == RT_BOT_PLATE || bot->mode == RT_BOT_SURFACE ) { if ( pmp->array_size <= pmp->num_bots ) { pmp->array_size += 5; pmp->bots = (struct rt_bot_internal **)bu_realloc( (char *)pmp->bots, pmp->array_size * sizeof( struct rt_bot_internal *), "pmp->bots" ); } /* walk tree will free the BOT, so we need a copy */ pmp->bots[pmp->num_bots] = dup_bot( bot ); BARRIER_CHECK; pmp->num_bots++; return (union tree *)NULL; } pmp->num_nonbots++; BARRIER_CHECK; return nmg_booltree_leaf_tess(tsp, pathp, ip, client_data); }
void process_point(point_line_t *plt) { static int code_state = INT32_MAX; static int points = 0; static point_line_t *plta = NULL; if (!plt) { printf("WARNING: Unexpected call to process_point with a NULL point structure\n"); return; } /* state change, we're either starting or ending */ if (code_state != plt->code) { if (points > 0) { process_multi_group(&plta, points, TOL); printf("END OF BLOCK %d\n", code_state); /* finish up this batch */ bu_free((genptr_t)plta, "end point_line_t group"); plta = NULL; } if (plt->type) printf("BEGIN OF BLOCK %s (%d)\n", plt->type, plt->code); /* get ready for the new batch */ code_state = plt->code; points = 0; } /* allocate room for the new point */ if (!plta) plta = (point_line_t *) bu_malloc(sizeof(point_line_t), "begin point_line_t group"); else plta = (point_line_t *) bu_realloc(plta, sizeof(point_line_t) * (points + 1), "add point_line_t"); COPY_POINT_LINE_T(plta[points], *plt); points++; }
int bu_ptbl_ins(struct bu_ptbl *b, long int *p) { register int i; BU_CK_PTBL(b); if (UNLIKELY(bu_debug & BU_DEBUG_PTBL)) bu_log("bu_ptbl_ins(%p, %p)\n", (void *)b, (void *)p); if (b->blen == 0) bu_ptbl_init(b, 64, "bu_ptbl_ins() buffer"); if ((size_t)b->end >= b->blen) { b->buffer = (long **)bu_realloc((char *)b->buffer, sizeof(long *)*(b->blen *= 4), "bu_ptbl.buffer[] (ins)"); } i = b->end++; b->buffer[i] = p; return i; }
/** * Read a polygon file and convert it to an NMG shell * * A polygon file consists of the following: * * The first line consists of two integer numbers: the number of * points (vertices) in the file, followed by the number of polygons * in the file. This line is followed by lines for each of the * vertices. Each vertex is listed on its own line, as the 3tuple "X * Y Z". After the list of vertices comes the list of polygons. * each polygon is represented by a line containing 1) the number of * vertices in the polygon, followed by 2) the indices of the * vertices that make up the polygon. * * Implicitly returns r->s_p which is a new shell containing all the * faces from the polygon file. * * XXX This is a horrible way to do this. Lee violates his own rules * about not creating fundamental structures on his own... :-) * Retired in favor of more modern tessellation strategies. */ struct shell * nmg_polytonmg(FILE *fp, struct nmgregion *r, const struct bn_tol *tol) { int i, j, num_pts, num_facets, pts_this_face, facet; int vl_len; struct vertex **v; /* list of all vertices */ struct vertex **vl; /* list of vertices for this polygon*/ point_t p; struct shell *s; struct faceuse *fu; struct loopuse *lu; struct edgeuse *eu; plane_t plane; struct model *m; s = nmg_msv(r); m = s->r_p->m_p; nmg_kvu(s->vu_p); /* get number of points & number of facets in file */ if (fscanf(fp, "%d %d", &num_pts, &num_facets) != 2) bu_bomb("polytonmg() Error in first line of poly file\n"); else if (RTG.NMG_debug & DEBUG_POLYTO) bu_log("points: %d facets: %d\n", num_pts, num_facets); v = (struct vertex **) bu_calloc(num_pts, sizeof (struct vertex *), "vertices"); /* build the vertices */ for (i = 0; i < num_pts; ++i) { GET_VERTEX(v[i], m); v[i]->magic = NMG_VERTEX_MAGIC; } /* read in the coordinates of the vertices */ for (i=0; i < num_pts; ++i) { if (fscanf(fp, "%lg %lg %lg", &p[0], &p[1], &p[2]) != 3) bu_bomb("polytonmg() Error reading point"); else if (RTG.NMG_debug & DEBUG_POLYTO) bu_log("read vertex #%d (%g %g %g)\n", i, p[0], p[1], p[2]); nmg_vertex_gv(v[i], p); } vl = (struct vertex **)bu_calloc(vl_len=8, sizeof (struct vertex *), "vertex parameter list"); for (facet = 0; facet < num_facets; ++facet) { if (fscanf(fp, "%d", &pts_this_face) != 1) bu_bomb("polytonmg() error getting pt count for this face"); if (RTG.NMG_debug & DEBUG_POLYTO) bu_log("facet %d pts in face %d\n", facet, pts_this_face); if (pts_this_face > vl_len) { while (vl_len < pts_this_face) vl_len *= 2; vl = (struct vertex **)bu_realloc((char *)vl, vl_len*sizeof(struct vertex *), "vertex parameter list (realloc)"); } for (i=0; i < pts_this_face; ++i) { if (fscanf(fp, "%d", &j) != 1) bu_bomb("polytonmg() error getting point index for v in f"); vl[i] = v[j-1]; } fu = nmg_cface(s, vl, pts_this_face); lu = BU_LIST_FIRST(loopuse, &fu->lu_hd); /* XXX should check for vertex-loop */ eu = BU_LIST_FIRST(edgeuse, &lu->down_hd); NMG_CK_EDGEUSE(eu); if (bn_mk_plane_3pts(plane, eu->vu_p->v_p->vg_p->coord, BU_LIST_PNEXT(edgeuse, eu)->vu_p->v_p->vg_p->coord, BU_LIST_PLAST(edgeuse, eu)->vu_p->v_p->vg_p->coord, tol)) { bu_log("At %d in %s\n", __LINE__, __FILE__); bu_bomb("polytonmg() cannot make plane equation\n"); } else nmg_face_g(fu, plane); } for (i=0; i < num_pts; ++i) { if (BU_LIST_IS_EMPTY(&v[i]->vu_hd)) continue; FREE_VERTEX(v[i]); } bu_free((char *)v, "vertex array"); return s; }
struct bu_list * rt_vlblock_find(struct bn_vlblock *vbp, int r, int g, int b) { long newrgb; size_t n; size_t omax; /* old max */ BN_CK_VLBLOCK(vbp); newrgb = ((r&0xFF)<<16)|((g&0xFF)<<8)|(b&0xFF); for (n=0; n < vbp->nused; n++) { if (vbp->rgb[n] == newrgb) return &(vbp->head[n]); } if (vbp->nused < vbp->max) { /* Allocate empty slot */ n = vbp->nused++; vbp->rgb[n] = newrgb; return &(vbp->head[n]); } /************** enlarge the table ****************/ omax = vbp->max; vbp->max *= 2; /* Look for empty lists and mark for use below. */ for (n=0; n < omax; n++) if (BU_LIST_IS_EMPTY(&vbp->head[n])) vbp->head[n].forw = BU_LIST_NULL; vbp->head = (struct bu_list *)bu_realloc((genptr_t)vbp->head, vbp->max * sizeof(struct bu_list), "head[]"); vbp->rgb = (long *)bu_realloc((genptr_t)vbp->rgb, vbp->max * sizeof(long), "rgb[]"); /* re-initialize pointers in lower half */ for (n=0; n < omax; n++) { /* * Check to see if list is empty * (i.e. yellow and/or white are not used). * Note - we can't use BU_LIST_IS_EMPTY here because * the addresses of the list heads have possibly changed. */ if (vbp->head[n].forw == BU_LIST_NULL) { vbp->head[n].forw = &vbp->head[n]; vbp->head[n].back = &vbp->head[n]; } else { vbp->head[n].forw->back = &vbp->head[n]; vbp->head[n].back->forw = &vbp->head[n]; } } /* initialize upper half of memory */ for (n=omax; n < vbp->max; n++) { vbp->rgb[n] = 0; BU_LIST_INIT(&vbp->head[n]); } /* here we go again */ return rt_vlblock_find(vbp, r, g, b); }
/** * handle a group of points of a particular type, with potentially * multiple sets delimited by triplicate points. */ void process_multi_group(point_line_t **plta, int count, double tolerance) { int i; point_line_t *plt = NULL; int points = 0; point_line_t *pltg = NULL; int marker = 0; point_line_t *prev_plt = NULL; if (!plta) { printf("WARNING: Unexpected call to process_multi_group with a NULL point array\n"); return; } #if PRINT_ARRAY static int print_counter = 0; if (print_counter == 0) { bu_log("--- BEFORE ---\n"); print_array(plta, count); } #endif /* remove points marked as bogus, 5-identical points in succession */ count = delete_points(plta, count, tolerance); #if PRINT_ARRAY if (print_counter == 0) { print_counter++; bu_log("--- AFTER ---\n"); print_array(plta, count); } #endif /* isolate groups and pass them on to the group processing routine */ for (i = 0; i < count; i++) { plt = &(*plta)[i]; if (!plt || !plt->type) { printf("WARNING: Unexpected NULL encountered while processing a point array (%d of %d)\n", i, count); continue; } /* if this is the first point of a group, allocate and initialize */ if (!prev_plt) { prev_plt = &(*plta)[i]; pltg = (point_line_t *) bu_malloc(sizeof(point_line_t), "begin point_line_t subgroup"); COPY_POINT_LINE_T(*pltg, *prev_plt); marker = 0; continue; } if (marker) { /* gobble up repeats points used as a marker, average new point */ if (DIST_PT_PT(prev_plt->val, plt->val) < tolerance) { prev_plt->val[X] = (prev_plt->val[X] + plt->val[X]) / 2.0; prev_plt->val[Y] = (prev_plt->val[Y] + plt->val[Y]) / 2.0; prev_plt->val[Z] = (prev_plt->val[Z] + plt->val[Z]) / 2.0; INITIALIZE_POINT_LINE_T(*plt); /* poof */ continue; } if (process_group(&pltg, points+1)) { bu_free((genptr_t)pltg, "end subgroup: point_line_t"); pltg = NULL; prev_plt = NULL; points = 0; marker = 0; --i; continue; } else { /* process_group is allowed to return non-zero if there are not enough points -- they get returned to the stack for processing again */ printf("warning, process_group returned 0\n"); } marker = 0; continue; } /* FIXME: shouldn't just average to the average, later points get weighted too much.. */ if (DIST_PT_PT(prev_plt->val, plt->val) < tolerance) { /* printf("%d: CLOSE DISTANCE of %f\n", plt->index, DIST_PT_PT(prev_plt->val, plt->val));*/ marker = points; (pltg[marker]).val[X] = (prev_plt->val[X] + plt->val[X]) / 2.0; (pltg[marker]).val[Y] = (prev_plt->val[Y] + plt->val[Y]) / 2.0; (pltg[marker]).val[Z] = (prev_plt->val[Z] + plt->val[Z]) / 2.0; continue; } if (!pltg) { printf("Blah! Error. Group array is null. Shouldn't be here!\n"); return; } pltg = (point_line_t *) bu_realloc(pltg, sizeof(point_line_t) * (points + 2), "add subgroup: point_line_t"); points++; COPY_POINT_LINE_T(pltg[points], *plt); prev_plt = plt; } printf("i: %d, count: %d", i, count); /* make sure we're not at the end of a list (i.e. no end marker, but we're at the end of this group */ if (points > 0) { if (process_group(&pltg, points+1)) { bu_free((genptr_t)pltg, "end point_line_t subgroup"); pltg = NULL; prev_plt = NULL; points = 0; marker = 0; } else { /* this one shouldn't return zero, we're at the end of a multiblock */ printf("ERROR, process_group returned 0\n"); } } }