int sphere(int entityno) { fastf_t radius = 0.0; point_t center; fastf_t x; fastf_t y; fastf_t z; int sol_num; /* IGES solid type number */ /* Set Defaults */ x = 0.0; y = 0.0; z = 0.0; /* 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, ""); Readcnv(&radius, ""); Readcnv(&x, ""); Readcnv(&y, ""); Readcnv(&z, ""); if (radius <= 0.0) { bu_log("Illegal parameters for entity D%07d (%s)\n" , dir[entityno]->direct, dir[entityno]->name); return 0; } /* * Making the necessaries. First an id is made for the new entity, then * the x, y, z coordinates for its vertices are converted to vectors with * VSET(), and finally the libwdb routine that makes an analogous BRL-CAD * solid is called. */ VSET(center, x, y, z); mk_sph(fdout, dir[entityno]->name, center, radius); return 1; }
int Extrudcirc(int entityno, int curve, vect_t evect) /* extrusion entity number */ /* circular arc entity number */ /* extrusion vector */ { point_t base; /* center of cylinder base */ fastf_t radius; /* radius of cylinder */ fastf_t x_1, y_1; /* Start point */ fastf_t x_2, y_2; /* Terminate point */ int sol_num; /* Solid number */ /* Acquiring Data */ if (dir[curve]->param <= pstart) { bu_log("Illegal parameter pointer for entity D%07d (%s)\n" , dir[curve]->direct, dir[curve]->name); return 0; } Readrec(dir[curve]->param); Readint(&sol_num, ""); Readcnv(&base[Z], ""); Readcnv(&base[X], ""); Readcnv(&base[Y], ""); Readcnv(&x_1, ""); Readcnv(&y_1, ""); Readcnv(&x_2, ""); Readcnv(&y_2, ""); /* Check for closure */ if (!ZERO(x_1 - x_2) || !ZERO(y_1 - y_2)) { bu_log("Circular arc for extrusion is not closed:\n"); bu_log("\textrusion entity D%07d (%s)\n", dir[entityno]->direct , dir[entityno]->name); bu_log("\tarc entity D%07d (%s)\n", dir[curve]->direct, dir[curve]->name); return 0; } radius = sqrt((x_1 - base[X])*(x_1 - base[X]) + (y_1 - base[Y])*(y_1 - base[Y])); /* Make an rcc */ mk_rcc(fdout, dir[entityno]->name, base, evect, radius); return 1; }
struct edge_g_cnurb * Get_cnurb_curve(int curve_de, int *linear) { int i; int curve; struct edge_g_cnurb *crv; *linear = 0; curve = (curve_de - 1)/2; if (curve >= dirarraylen) { bu_log("Get_cnurb_curve: DE=%d is too large, dirarraylen = %d\n", curve_de, dirarraylen); return (struct edge_g_cnurb *)NULL; } switch (dir[curve]->type) { case 110: { /* line */ int pt_type; int type; point_t pt1; point_t start_pt, end_pt; Readrec(dir[curve]->param); Readint(&type, ""); if (type != dir[curve]->type) { bu_log("Error in Get_cnurb_curve, looking for curve type %d, found %d\n" , dir[curve]->type, type); return (struct edge_g_cnurb *)NULL; } /* Read first point */ for (i = 0; i < 3; i++) Readcnv(&pt1[i], ""); MAT4X3PNT(start_pt, *dir[curve]->rot, pt1); /* Read second point */ for (i = 0; i < 3; i++) Readcnv(&pt1[i], ""); MAT4X3PNT(end_pt, *dir[curve]->rot, pt1); /* pt_type for rational UVW coords */ pt_type = RT_NURB_MAKE_PT_TYPE(3, 3, 1); /* make a linear edge_g_cnurb (order=2) */ crv = rt_nurb_new_cnurb(2, 4, 2, pt_type); /* insert control mesh */ VMOVE(crv->ctl_points, start_pt); VMOVE(&crv->ctl_points[3], end_pt); /* insert knot values */ crv->k.knots[0] = 0.0; crv->k.knots[1] = 0.0; crv->k.knots[2] = 1.0; crv->k.knots[3] = 1.0; *linear = 1; return crv; } case 126: /* B-spline */ crv = Get_cnurb(curve); if (crv->order < 3) *linear = 1; return crv; default: bu_log("Not yet handling curves of type: %s\n", iges_type(dir[curve]->type)); break; } return (struct edge_g_cnurb *)NULL; }
int Getcurve(int curve, struct ptlist **curv_pts) { int type; int npts = 0; int i, j; double pi; struct ptlist *ptr, *prev; pi = atan2(0.0, -1.0); (*curv_pts) = NULL; prev = NULL; switch (dir[curve]->type) { case 110: { /* line */ point_t pt1; Readrec(dir[curve]->param); Readint(&type, ""); if (type != dir[curve]->type) { bu_log("Error in Getcurve, looking for curve type %d, found %d\n" , dir[curve]->type, type); npts = 0; break; } BU_ALLOC((*curv_pts), struct ptlist); ptr = (*curv_pts); /* Read first point */ for (i = 0; i < 3; i++) Readcnv(&pt1[i], ""); MAT4X3PNT(ptr->pt, *dir[curve]->rot, pt1); ptr->prev = NULL; prev = ptr; BU_ALLOC(ptr->next, struct ptlist); ptr = ptr->next; /* Read second point */ for (i = 0; i < 3; i++) Readcnv(&pt1[i], ""); MAT4X3PNT(ptr->pt, *dir[curve]->rot, pt1); ptr->next = NULL; ptr->prev = prev; npts = 2; break; } case 100: { /* circular arc */ point_t center, start, stop, tmp; fastf_t common_z, ang1, ang2, delta; double cosdel, sindel, rx, ry; delta = (2.0*pi)/ARCSEGS; Readrec(dir[curve]->param); Readint(&type, ""); if (type != dir[curve]->type) { bu_log("Error in Getcurve, looking for curve type %d, found %d\n" , dir[curve]->type, type); npts = 0; break; } /* Read common Z coordinate */ Readcnv(&common_z, ""); /* Read center point */ Readcnv(¢er[X], ""); Readcnv(¢er[Y], ""); center[Z] = common_z; /* Read start point */ Readcnv(&start[X], ""); Readcnv(&start[Y], ""); start[Z] = common_z; /* Read stop point */ Readcnv(&stop[X], ""); Readcnv(&stop[Y], ""); stop[Z] = common_z; ang1 = atan2(start[Y] - center[Y], start[X] - center[X]); ang2 = atan2(stop[Y] - center[Y], stop[X] - center[X]); while (ang2 <= ang1) ang2 += (2.0*pi); npts = (ang2 - ang1)/delta; npts++; V_MAX(npts, 3); delta = (ang2 - ang1)/(npts-1); cosdel = cos(delta); sindel = sin(delta); /* Calculate points on curve */ BU_ALLOC((*curv_pts), struct ptlist); ptr = (*curv_pts); prev = NULL; MAT4X3PNT(ptr->pt, *dir[curve]->rot, start); ptr->prev = prev; prev = ptr; BU_ALLOC(ptr->next, struct ptlist); ptr = ptr->next; ptr->prev = prev; VMOVE(tmp, start); for (i = 1; i < npts; i++) { rx = tmp[X] - center[X]; ry = tmp[Y] - center[Y]; tmp[X] = center[X] + rx*cosdel - ry*sindel; tmp[Y] = center[Y] + rx*sindel + ry*cosdel; MAT4X3PNT(ptr->pt, *dir[curve]->rot, tmp); prev = ptr; BU_ALLOC(ptr->next, struct ptlist); ptr = ptr->next; ptr->prev = prev; } ptr = prev; bu_free((char *)ptr->next, "Getcurve: ptr->next"); ptr->next = NULL; break; } case 106: { /* copius data */ int interpflag; /* interpretation flag 1 => x, y pairs (common z-coord) 2 => x, y, z coords 3 => x, y, z coords and i, j, k vectors */ int ntuples; /* number of points */ fastf_t common_z; /* common z-coordinate */ point_t pt1; /* temporary storage for incoming point */ Readrec(dir[curve]->param); Readint(&type, ""); if (type != dir[curve]->type) { bu_log("Error in Getcurve, looking for curve type %d, found %d\n" , dir[curve]->type, type); npts = 0; break; } Readint(&interpflag, ""); Readint(&ntuples, ""); switch (dir[curve]->form) { case 1: case 11: case 40: case 63: { /* data are coordinate pairs with common z */ if (interpflag != 1) { bu_log("Error in Getcurve for copius data entity D%07d, IP=%d, should be 1\n", dir[curve]->direct, interpflag); npts = 0; break; } Readcnv(&common_z, ""); BU_ALLOC((*curv_pts), struct ptlist); ptr = (*curv_pts); ptr->prev = NULL; for (i = 0; i < ntuples; i++) { Readcnv(&pt1[X], ""); Readcnv(&pt1[Y], ""); pt1[Z] = common_z; MAT4X3PNT(ptr->pt, *dir[curve]->rot, pt1); prev = ptr; BU_ALLOC(ptr->next, struct ptlist); ptr = ptr->next; ptr->prev = prev; ptr->next = NULL; } ptr = ptr->prev; bu_free((char *)ptr->next, "Getcurve: ptr->next"); ptr->next = NULL; npts = ntuples; break; } case 2: case 12: { /* data are coordinate triples */ if (interpflag != 2) { bu_log("Error in Getcurve for copius data entity D%07d, IP=%d, should be 2\n", dir[curve]->direct, interpflag); npts = 0; break; } BU_ALLOC((*curv_pts), struct ptlist); ptr = (*curv_pts); ptr->prev = NULL; for (i = 0; i < ntuples; i++) { Readcnv(&pt1[X], ""); Readcnv(&pt1[Y], ""); Readcnv(&pt1[Z], ""); MAT4X3PNT(ptr->pt, *dir[curve]->rot, pt1); prev = ptr; BU_ALLOC(ptr->next, struct ptlist); ptr = ptr->next; ptr->prev = prev; } ptr = ptr->prev; bu_free((char *)ptr->next, "Getcurve: ptr->next"); ptr->next = NULL; npts = ntuples; break; } default: { bu_log("Error in Getcurve for copius data entity D%07d, form %d is not a legal choice\n", dir[curve]->direct, dir[curve]->form); npts = 0; break; } } break; } case 112: { /* parametric spline */ struct spline *splroot; struct segment *seg, *seg1; vect_t tmp; double a; Readrec(dir[curve]->param); Readint(&type, ""); if (type != dir[curve]->type) { bu_log("Error in Getcurve, looking for curve type %d, found %d\n" , dir[curve]->type, type); npts = 0; break; } Readint(&i, ""); /* Skip over type */ Readint(&i, ""); /* Skip over continuity */ BU_ALLOC(splroot, struct spline); splroot->start = NULL; Readint(&splroot->ndim, ""); /* 2->planar, 3->3d */ Readint(&splroot->nsegs, ""); /* Number of segments */ Readdbl(&a, ""); /* first breakpoint */ /* start a linked list of segments */ seg = splroot->start; for (i = 0; i < splroot->nsegs; i++) { if (seg == NULL) { BU_ALLOC(seg, struct segment); splroot->start = seg; } else { BU_ALLOC(seg->next, struct segment); seg = seg->next; } seg->segno = i+1; seg->next = NULL; seg->tmin = a; /* set minimum T for this segment */ Readflt(&seg->tmax, ""); /* get maximum T for segment */ a = seg->tmax; } /* read coefficients for polynomials */ seg = splroot->start; for (i = 0; i < splroot->nsegs; i++) { for (j = 0; j < 4; j++) Readflt(&seg->cx[j], ""); /* x coeff's */ for (j = 0; j < 4; j++) Readflt(&seg->cy[j], ""); /* y coeff's */ for (j = 0; j < 4; j++) Readflt(&seg->cz[j], ""); /* z coeff's */ seg = seg->next; } /* Calculate points */ BU_ALLOC((*curv_pts), struct ptlist); ptr = (*curv_pts); prev = NULL; ptr->prev = NULL; npts = 0; seg = splroot->start; while (seg != NULL) { /* plot 9 points per segment (This should be replaced by some logic) */ for (i = 0; i < 9; i++) { a = (fastf_t)i/(8.0)*(seg->tmax-seg->tmin); tmp[0] = splinef(seg->cx, a); tmp[1] = splinef(seg->cy, a); if (splroot->ndim == 3) tmp[2] = splinef(seg->cz, a); else tmp[2] = seg->cz[0]; MAT4X3PNT(ptr->pt, *dir[curve]->rot, tmp); for (j = 0; j < 3; j++) ptr->pt[j] *= conv_factor; npts++; prev = ptr; BU_ALLOC(ptr->next, struct ptlist); ptr = ptr->next; ptr->prev = prev; } seg = seg->next; } ptr = ptr->prev; bu_free((char *)ptr->next, "Getcurve: ptr->next"); ptr->next = NULL; /* free the used memory */ seg = splroot->start; while (seg != NULL) { seg1 = seg; seg = seg->next; bu_free((char *)seg1, "Getcurve: seg1"); } bu_free((char *)splroot, "Getcurve: splroot"); splroot = NULL; break; } case 104: { /* conic arc */ double A, B, C, D, E, F, a, b, c, del, I, theta, dpi, t1, t2, xc, yc; point_t v1, v2, tmp; mat_t rot1; int num_points; Readrec(dir[curve]->param); Readint(&type, ""); if (type != dir[curve]->type) { bu_log("Error in Getcurve, looking for curve type %d, found %d\n" , dir[curve]->type, type); npts = 0; break; } /* read coefficients */ Readdbl(&A, ""); Readdbl(&B, ""); Readdbl(&C, ""); Readdbl(&D, ""); Readdbl(&E, ""); Readdbl(&F, ""); /* read common z-coordinate */ Readflt(&v1[2], ""); v2[2] = v1[2]; /* read start point */ Readflt(&v1[0], ""); Readflt(&v1[1], ""); /* read terminate point */ Readflt(&v2[0], ""); Readflt(&v2[1], ""); type = 0; if (dir[curve]->form == 1) { /* Ellipse */ if (fabs(E) < SMALL) E = 0.0; if (fabs(B) < SMALL) B = 0.0; if (fabs(D) < SMALL) D = 0.0; if (ZERO(B) && ZERO(D) && ZERO(E)) type = 1; else bu_log("Entity #%d is an incorrectly formatted ellipse\n", curve); } /* make coeff of X**2 equal to 1.0 */ a = A*C - B*B/4.0; if (fabs(a) < 1.0 && fabs(a) > TOL) { a = fabs(A); if (fabs(B) < a && !ZERO(B)) a = fabs(B); V_MIN(a, fabs(C)); A = A/a; B = B/a; C = C/a; D = D/a; E = E/a; F = F/a; a = A*C - B*B/4.0; } if (!type) { /* check for type of conic */ del = A*(C*F-E*E/4.0)-0.5*B*(B*F/2.0-D*E/4.0)+0.5*D*(B*E/4.0-C*D/2.0); I = A+C; if (ZERO(del)) { /* not a conic */ bu_log("Entity #%d, claims to be conic arc, but isn't\n", curve); break; } else if (a > 0.0 && del*I < 0.0) type = 1; /* ellipse */ else if (a < 0.0) type = 2; /* hyperbola */ else if (ZERO(a)) type = 3; /* parabola */ else { /* imaginary ellipse */ bu_log("Entity #%d is an imaginary ellipse!!\n", curve); break; } } switch (type) { double p, r1; case 3: /* parabola */ /* make A+C == 1.0 */ if (!EQUAL(A+C, 1.0)) { b = A+C; A = A/b; B = B/b; C = C/b; D = D/b; E = E/b; F = F/b; } /* theta is the angle that the parabola axis is rotated about the origin from the x-axis */ theta = 0.5*atan2(B, C-A); /* p is the distance from vertex to directrix */ p = (-E*sin(theta) - D*cos(theta))/4.0; if (fabs(p) < TOL) { bu_log("Cannot plot entity %d, p=%g\n", curve, p); break; } /* calculate vertex (xc, yc). This is based on the parametric representation: x = xc + a*t*t*cos(theta) - t*sin(theta) y = yc + a*t*t*sin(theta) + t*cos(theta) and the fact that v1 and v2 are on the curve */ a = 1.0/(4.0*p); b = ((v1[0]-v2[0])*cos(theta) + (v1[1]-v2[1])*sin(theta))/a; c = ((v1[1]-v2[1])*cos(theta) - (v1[0]-v2[0])*sin(theta)); if (fabs(c) < TOL*TOL) { bu_log("Cannot plot entity %d\n", curve); break; } b = b/c; t1 = (b + c)/2.0; /* value of 't' at v1 */ t2 = (b - c)/2.0; /* value of 't' at v2 */ xc = v1[0] - a*t1*t1*cos(theta) + t1*sin(theta); yc = v1[1] - a*t1*t1*sin(theta) - t1*cos(theta); /* Calculate points */ BU_ALLOC((*curv_pts), struct ptlist); ptr = (*curv_pts); ptr->prev = NULL; prev = NULL; npts = 0; num_points = ARCSEGS+1; dpi = (t2-t1)/(double)num_points; /* parameter increment */ /* start point */ VSET(tmp, xc, yc, v1[2]); MAT4X3PNT(ptr->pt, *dir[curve]->rot, tmp); VSCALE(ptr->pt, ptr->pt, conv_factor); npts++; prev = ptr; BU_ALLOC(ptr->next, struct ptlist); ptr = ptr->next; ptr->prev = prev; /* middle points */ b = cos(theta); c = sin(theta); for (i = 1; i < num_points-1; i++) { r1 = t1 + dpi*i; tmp[0] = xc + a*r1*r1*b - r1*c; tmp[1] = yc + a*r1*r1*c + r1*b; MAT4X3PNT(ptr->pt, *dir[curve]->rot, tmp); VSCALE(ptr->pt, ptr->pt, conv_factor); npts++; prev = ptr; BU_ALLOC(ptr->next, struct ptlist); ptr = ptr->next; ptr->prev = prev; } /* plot terminate point */ tmp[0] = v2[0]; tmp[1] = v2[1]; MAT4X3PNT(ptr->pt, *dir[curve]->rot, tmp); for (j = 0; j < 3; j++) ptr->pt[j] *= conv_factor; npts++; ptr->next = NULL; break; case 1: /* ellipse */ case 2: { /* hyperbola */ double A1, C1, F1, alpha, beta; mat_t rot2; point_t v3; /* calculate center of ellipse or hyperbola */ xc = (B*E/4.0 - D*C/2.0)/a; yc = (B*D/4.0 - A*E/2.0)/a; /* theta is angle that the curve axis is rotated about the origin from the x-axis */ if (!ZERO(B)) theta = 0.5*atan2(B, A-C); else theta = 0.0; /* calculate coeff's for same curve, but with vertex at origin and theta = 0.0 */ A1 = A + 0.5*B*tan(theta); C1 = C - 0.5*B*tan(theta); F1 = F - A*xc*xc - B*xc*yc - C*yc*yc; if (type == 2 && F1/A1 > 0.0) theta += pi/2.0; /* set-up matrix to translate and rotate the start and terminate points to match the simpler curve (A1, C1, and F1 coeff's) */ for (i = 0; i < 16; i++) rot1[i] = idn[i]; MAT_DELTAS(rot1, -xc, -yc, 0.0); MAT4X3PNT(tmp, rot1, v1); VMOVE(v1, tmp); MAT4X3PNT(tmp, rot1, v2); VMOVE(v2, tmp); MAT_DELTAS(rot1, 0.0, 0.0, 0.0); rot1[0] = cos(theta); rot1[1] = sin(theta); rot1[4] = (-rot1[1]); rot1[5] = rot1[0]; MAT4X3PNT(tmp, rot1, v1); VMOVE(v1, tmp); MAT4X3PNT(tmp, rot1, v2); VMOVE(v2, tmp); MAT_DELTAS(rot1, 0.0, 0.0, 0.0); /* calculate: alpha = start angle beta = terminate angle */ beta = 0.0; if (EQUAL(v2[0], v1[0]) && EQUAL(v2[1], v1[1])) { /* full circle */ alpha = 0.0; beta = 2.0*pi; } a = sqrt(fabs(F1/A1)); /* semi-axis length */ b = sqrt(fabs(F1/C1)); /* semi-axis length */ if (type == 1) { /* ellipse */ alpha = atan2(a*v1[1], b*v1[0]); if (ZERO(beta)) { beta = atan2(a*v2[1], b*v2[0]); beta = beta - alpha; } } else { /* hyperbola */ alpha = myarcsinh(v1[1]/b); beta = myarcsinh(v2[1]/b); if (fabs(a*cosh(beta) - v2[0]) > 0.01) a = (-a); beta = beta - alpha; } num_points = ARCSEGS; /* set-up matrix to translate and rotate the simpler curve back to the original position */ MAT_DELTAS(rot1, xc, yc, 0.0); rot1[1] = (-rot1[1]); rot1[4] = (-rot1[4]); #if defined(USE_BN_MULT_) /* o <= a X b */ bn_mat_mul(rot2, *(dir[curve]->rot), rot1); #else /* a X b => o */ Matmult(*(dir[curve]->rot), rot1, rot2); #endif /* calculate start point */ BU_ALLOC((*curv_pts), struct ptlist); ptr = (*curv_pts); prev = NULL; ptr->prev = NULL; npts = 0; VSCALE(v3, v1, conv_factor); MAT4X3PNT(ptr->pt, rot2, v3); npts++; prev = ptr; BU_ALLOC(ptr->next, struct ptlist); ptr = ptr->next; ptr->prev = prev; /* middle points */ for (i = 1; i < num_points; i++) { point_t tmp2 = {0.0, 0.0, 0.0}; theta = alpha + (double)i/(double)num_points*beta; if (type == 2) { tmp2[0] = a*cosh(theta); tmp2[1] = b*sinh(theta); } else { tmp2[0] = a*cos(theta); tmp2[1] = b*sin(theta); } VSCALE(tmp2, tmp2, conv_factor); MAT4X3PNT(ptr->pt, rot2, tmp2); npts++; prev = ptr; BU_ALLOC(ptr->next, struct ptlist); ptr = ptr->next; ptr->prev = prev; } /* terminate point */ VSCALE(v2, v2, conv_factor); MAT4X3PNT(ptr->pt, rot2, v2); npts++; ptr->next = NULL; break; } } break; } case 102: /* composite curve */ { int ncurves, *curvptr; struct ptlist *tmp_ptr; Readrec(dir[curve]->param); Readint(&type, ""); if (type != dir[curve]->type) { bu_log("Error in Getcurve, looking for curve type %d, found %d\n" , dir[curve]->type, type); npts = 0; break; } Readint(&ncurves, ""); curvptr = (int *)bu_calloc(ncurves, sizeof(int), "Getcurve: curvptr"); for (i = 0; i < ncurves; i++) { Readint(&curvptr[i], ""); curvptr[i] = (curvptr[i]-1)/2; } npts = 0; (*curv_pts) = NULL; for (i = 0; i < ncurves; i++) { npts += Getcurve(curvptr[i], &tmp_ptr); if ((*curv_pts) == NULL) (*curv_pts) = tmp_ptr; else { ptr = (*curv_pts); while (ptr->next != NULL) ptr = ptr->next; ptr->next = tmp_ptr; ptr->next->prev = ptr; if (NEAR_EQUAL(ptr->pt[X], tmp_ptr->pt[X], TOL) && NEAR_EQUAL(ptr->pt[Y], tmp_ptr->pt[Y], TOL) && NEAR_EQUAL(ptr->pt[Z], tmp_ptr->pt[Z], TOL)) { ptr->next = ptr->next->next; if (ptr->next != NULL) ptr->next->prev = ptr; bu_free((char *)tmp_ptr, "Getcurve: tmp_ptr"); npts--; } } } break; } case 126: { /* rational B-spline */ int k, m, n, a, prop1, prop2, prop3, prop4; fastf_t *t; /* knot values */ fastf_t *w; /* weights */ point_t *cntrl_pts; /* control points */ fastf_t v0, v1; /* starting and stopping parameter values */ fastf_t v; /* current parameter value */ fastf_t delv; /* parameter increment */ Readrec(dir[curve]->param); Readint(&type, ""); if (type != dir[curve]->type) { bu_log("Error in Getcurve, looking for curve type %d, found %d\n" , dir[curve]->type, type); npts = 0; break; } Readint(&k, ""); Readint(&m, ""); Readint(&prop1, ""); Readint(&prop2, ""); Readint(&prop3, ""); Readint(&prop4, ""); n = k - m + 1; a = n + 2 * m; t = (fastf_t *)bu_calloc(a+1, sizeof(fastf_t), "Getcurve: spline t"); for (i = 0; i < a+1; i++) Readflt(&t[i], ""); Knot(a+1, t); w = (fastf_t *)bu_calloc(k+1, sizeof(fastf_t), "Getcurve: spline w"); for (i = 0; i < k+1; i++) Readflt(&w[i], ""); cntrl_pts = (point_t *)bu_calloc(k+1, sizeof(point_t), "Getcurve: spline cntrl_pts"); for (i = 0; i < k+1; i++) { fastf_t tmp; for (j = 0; j < 3; j++) { Readcnv(&tmp, ""); cntrl_pts[i][j] = tmp; } } Readflt(&v0, ""); Readflt(&v1, ""); delv = (v1 - v0)/((fastf_t)(3*k)); /* Calculate points */ BU_ALLOC((*curv_pts), struct ptlist); ptr = (*curv_pts); ptr->prev = NULL; prev = NULL; npts = 0; v = v0; while (v < v1) { point_t tmp; B_spline(v, k, m+1, cntrl_pts, w, tmp); MAT4X3PNT(ptr->pt, *dir[curve]->rot, tmp); npts++; prev = ptr; BU_ALLOC(ptr->next, struct ptlist); ptr = ptr->next; ptr->prev = prev; v += delv; } VMOVE(ptr->pt, cntrl_pts[k]); npts++; ptr->next = NULL; /* Free memory */ Freeknots(); bu_free((char *)cntrl_pts, "Getcurve: spline cntrl_pts"); bu_free((char *)w, "Getcurve: spline w"); bu_free((char *)t, "Getcurve: spline t"); break; } } return npts; }
int spline(int entityno, struct face_g_snurb **b_patch) { int k1; /* upper index of first sum */ int k2; /* upper index of second sum */ int m1; /* degree of 1st set of basis functions */ int m2; /* degree of 2nd set of basis functions */ int prop1; /* !0 if closed in first direction */ int prop2; /* !0 if closed in second direction */ int prop3; /* !0 if polynomial (else rational) */ int prop4; /* !0 if periodic in first direction */ int prop5; /* !0 if periodic in second direction */ int sol_num; /* IGES solid type number */ int n1, n2; int i, j, k; int count = 0; int point_size; fastf_t min_knot; double max_wt; double scan; /* 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(&k1, ""); Readint(&k2, ""); Readint(&m1, ""); Readint(&m2, ""); Readint(&prop1, ""); Readint(&prop2, ""); Readint(&prop3, ""); Readint(&prop4, ""); Readint(&prop5, ""); n1 = k1 - m1 + 1; n2 = k2 - m2 + 1; /* spl_new: Creates a spline surface data structure * u_order (e.g. cubic = order 4) * v_order * num_u (e.g. num control points + order) * num_v * num_rows num control points in V direction * num_cols num control points in U direction * point_size number of values in a point (e.g. 3 or 4) */ if (prop3 == 0) { point_size = 4; } else { point_size = 3; } (*b_patch) = rt_nurb_new_snurb( m1+1, m2+1, n1+2*m1+1, n2+2*m2+1, k2+1, k1+1, RT_NURB_MAKE_PT_TYPE(point_size, 2, (prop3 == 0 ? RT_NURB_PT_RATIONAL : RT_NURB_PT_NONRAT)), (struct resource *)NULL); /* U knot vector */ min_knot = 0.0; for (i = 0; i <= n1+2*m1; i++) { Readdbl(&scan, ""); (*b_patch)->u.knots[i] = scan; /* double to fastf_t */ if ((*b_patch)->u.knots[i] < min_knot) min_knot = (*b_patch)->u.knots[i]; } if (min_knot < 0.0) { for (i = 0; i <= n1+2*m1; i++) { (*b_patch)->u.knots[i] -= min_knot; } } min_knot = 0.0; /* V knot vector */ for (i = 0; i <= n2+2*m2; i++) { Readdbl(&scan, ""); (*b_patch)->v.knots[i] = scan; /* double to fastf_t */ if ((*b_patch)->v.knots[i] < min_knot) min_knot = (*b_patch)->v.knots[i]; } if (min_knot < 0.0) { for (i = 0; i <= n2+2*m2; i++) { (*b_patch)->v.knots[i] -= min_knot; } } /* weights */ max_wt = 0.0; count = 0; for (i = 0; i <= k2; i++) { for (j = 0; j <= k1; j++) { if (point_size == 4) { Readdbl(&scan, ""); (*b_patch)->ctl_points[count*4 + 3] = scan; /* double to fastf_t */ if ((*b_patch)->ctl_points[count*4 + 3] > max_wt) max_wt = (*b_patch)->ctl_points[count*4 + 3]; } else { Readdbl(&max_wt, ""); } count++; } } /* control points */ count = 0; for (i = 0; i <= k2; i++) { for (j = 0; j <= k1; j++) { Readcnv(&(*b_patch)->ctl_points[count*point_size], ""); Readcnv(&(*b_patch)->ctl_points[count*point_size + 1], ""); Readcnv(&(*b_patch)->ctl_points[count*point_size + 2], ""); count++; } } if (point_size == 4) { /* apply weights */ count = 0; for (i = 0; i <= k2; i++) { for (j = 0; j <= k1; j++) { for (k = 0; k < 3; k++) (*b_patch)->ctl_points[count*4 + k] *= (*b_patch)->ctl_points[count*4 + 3]; count++; } } } return 1; }
int extrude(int entityno) { fastf_t length; /* extrusion length */ vect_t edir; /* a unit vector (direction of extrusion */ vect_t evect; /* Scaled vector for extrusion */ int sol_num; /* IGES solid type number */ int curve; /* pointer to directory entry for base curve */ struct ptlist *curv_pts; /* List of points along curve */ int i; /* Default values */ VSET(edir, 0.0, 0.0, 1.0); /* 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, ""); /* Read pointer to directory entry for curve to be extruded */ Readint(&curve, ""); /* Convert this to a "dir" index */ curve = (curve-1)/2; Readcnv(&length, ""); Readflt(&edir[X], ""); Readflt(&edir[Y], ""); Readflt(&edir[Z], ""); if (length <= 0.0) { bu_log("Illegal parameters for entity D%07d (%s)\n" , dir[entityno]->direct, dir[entityno]->name); return 0; } /* * Unitize direction vector */ VUNITIZE(edir); /* Scale vector */ VSCALE(evect, edir, length); /* Switch based on type of curve to be extruded */ switch (dir[curve]->type) { case 100: /* circular arc */ return Extrudcirc(entityno, curve, evect); case 104: /* conic arc */ return Extrudcon(entityno, curve, evect); case 102: /* composite curve */ case 106: /* copius data */ case 112: /* parametric spline */ case 126: { /* B-spline */ int npts; struct model *m; struct nmgregion *r; struct shell *s; struct faceuse *fu; struct loopuse *lu; struct edgeuse *eu; struct ptlist *pt_ptr; npts = Getcurve(curve, &curv_pts); if (npts < 3) return 0; m = nmg_mm(); r = nmg_mrsv(m); s = BU_LIST_FIRST(shell, &r->s_hd); fu = nmg_cface(s, (struct vertex **)NULL, npts-1); pt_ptr = curv_pts; lu = BU_LIST_FIRST(loopuse, &fu->lu_hd); for (BU_LIST_FOR(eu, edgeuse, &lu->down_hd)) { struct vertex *v; v = eu->vu_p->v_p; nmg_vertex_gv(v, pt_ptr->pt); pt_ptr = pt_ptr->next; } if (nmg_calc_face_g(fu)) { bu_log("Extrude: Failed to calculate face geometry\n"); nmg_km(m); bu_free((char *)curv_pts, "curve_pts"); return 0; } if (nmg_extrude_face(fu, evect, &tol)) { bu_log("Extrude: extrusion failed\n"); nmg_km(m); bu_free((char *)curv_pts, "curve_pts"); return 0; } mk_bot_from_nmg(fdout, dir[entityno]->name, s); nmg_km(m); bu_free((char *)curv_pts, "curve_pts"); return 1; } default: i = (-1); while (dir[curve]->type != typecount[++i].type && i < ntypes); bu_log("Extrusions of %s are not allowed\n", typecount[i].name); break; } return 0; }
int block(int entityno) { fastf_t xscale = 0.0; fastf_t yscale = 0.0; fastf_t zscale = 0.0; fastf_t x_1, y_1, z_1; /* First vertex components */ fastf_t x_2, y_2, z_2; /* xdir vector components */ fastf_t x_3, y_3, z_3; /* zdir vector components */ point_t v; /* the first vertex */ vect_t xdir; /* a unit vector */ vect_t xvec; /* vector along x-axis */ vect_t ydir; /* a unit vector */ vect_t yvec; /* vector along y-axis */ vect_t zdir; /* a unit vector */ vect_t zvec; /* vector along z-axis */ point_t pts[9]; /* array of points */ int sol_num; /* IGES solid type number */ /* Default values */ x_1 = 0.0; y_1 = 0.0; z_1 = 0.0; x_2 = 1.0; y_2 = 0.0; z_2 = 0.0; x_3 = 0.0; y_3 = 0.0; z_3 = 1.0; /* 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, ""); Readcnv(&xscale, ""); Readcnv(&yscale, ""); Readcnv(&zscale, ""); Readcnv(&x_1, ""); Readcnv(&y_1, ""); Readcnv(&z_1, ""); Readflt(&x_2, ""); Readflt(&y_2, ""); Readflt(&z_2, ""); Readflt(&x_3, ""); Readflt(&y_3, ""); Readflt(&z_3, ""); if (xscale <= 0.0 || yscale <= 0.0 || zscale <= 0.0) { bu_log("Illegal parameters for entity D%07d (%s)\n" , dir[entityno]->direct, dir[entityno]->name); return 0; } /* * Making the necessaries. First an id is made for the new entity. * Then the vertices for the bottom and top faces are found. Point * is located in the lower left corner of the solid, and the vertices are * counted in the counter-clockwise direction, around the bottom face. * Next these vertices are extruded to form the top face. The points * thus made are loaded into an array of points and handed off to mk_arb8(). * Make and unitize necessary vectors. */ VSET(xdir, x_2, y_2, z_2); /* Makes x-dir vector */ VUNITIZE(xdir); VSET(zdir, x_3, y_3, z_3); /* Make z-dir vector */ VUNITIZE(zdir); VCROSS(ydir, zdir, xdir); /* Make y-dir vector */ /* Scale all vectors */ VSCALE(xvec, xdir, xscale); VSCALE(zvec, zdir, zscale); VSCALE(yvec, ydir, yscale); /* Make the bottom face. */ VSET(v, x_1, y_1, z_1); /* Yields first vertex */ VMOVE(pts[0], v); /* put first vertex into array */ VADD2(pts[1], v, xvec); /* Finds second vertex */ VADD3(pts[2], v, xvec, yvec); /* Finds third vertex */ VADD2(pts[3], v, yvec); /* Finds fourth vertex */ /* Now extrude the bottom face to make the top. */ VADD2(pts[4], v, zvec); /* Finds fifth vertex */ VADD2(pts[5], pts[1], zvec); /* Finds sixth vertex */ VADD2(pts[6], pts[2], zvec); /* Finds seventh vertex */ VADD2(pts[7], pts[3], zvec); /* Find eighth vertex */ /* Now the information is handed off to mk_arb8(). */ mk_arb8(fdout, dir[entityno]->name, &pts[0][X]); return 1; }
int cyl(int entityno) { fastf_t radius = 0.0; point_t base; /* center point of base */ vect_t height; vect_t hdir; /* direction in which to grow height */ fastf_t scale_height = 0.0; fastf_t x_1; fastf_t y_1; fastf_t z_1; fastf_t x_2; fastf_t y_2; fastf_t z_2; int sol_num; /* IGES solid type number */ /* Default values */ x_1 = 0.0; y_1 = 0.0; z_1 = 0.0; x_2 = 0.0; y_2 = 0.0; z_2 = 1.0; /* 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, ""); Readcnv(&scale_height, ""); Readcnv(&radius, ""); Readcnv(&x_1, ""); Readcnv(&y_1, ""); Readcnv(&z_1, ""); Readcnv(&x_2, ""); Readcnv(&y_2, ""); Readcnv(&z_2, ""); if (radius < SMALL_FASTF || scale_height < SMALL_FASTF) { bu_log("Illegal parameters for entity D%07d (%s)\n" , dir[entityno]->direct, dir[entityno]->name); if (ZERO(radius)) { bu_log("\tradius of cylinder is zero!!!\n"); return 0; } if (ZERO(scale_height)) { bu_log("\theight of cylinder is zero!!!\n"); return 0; } if (radius < 0.0) { bu_log("\tUsing the absolute value of a negative radius\n"); radius = (-radius); } if (scale_height < 0.0) { bu_log("\tUsing absolute value of a negative height and reversing axis direction\n"); scale_height = (-scale_height); x_2 = (-x_2); y_2 = (-y_2); z_2 = (-z_2); } } /* * Making the necessaries. First an id is made for the new entity, then * the x, y, z coordinates for its vertices are converted to vectors with * VSET(), and finally the libwdb routine that makes an analogous BRL-CAD * solid is called. */ VSET(base, x_1, y_1, z_1); VSET(hdir, x_2, y_2, z_2); VUNITIZE(hdir); /* Multiply the hdir * scale_height to obtain height. */ VSCALE(height, hdir, scale_height); if (mk_rcc(fdout, dir[entityno]->name, base, height, radius) < 0) { bu_log("Unable to write entity D%07d (%s)\n" , dir[entityno]->direct, dir[entityno]->name); return 0; } return 1; }