void bn_mat_arb_rot( mat_t m, const point_t pt, const vect_t dir, const fastf_t ang) { mat_t tran1, tran2, rot; double cos_ang, sin_ang, one_m_cosang; double n1_sq, n2_sq, n3_sq; double n1_n2, n1_n3, n2_n3; if (ZERO(ang)) { MAT_IDN(m); return; } MAT_IDN(tran1); MAT_IDN(tran2); /* construct translation matrix to pt */ tran1[MDX] = (-pt[X]); tran1[MDY] = (-pt[Y]); tran1[MDZ] = (-pt[Z]); /* construct translation back from pt */ tran2[MDX] = pt[X]; tran2[MDY] = pt[Y]; tran2[MDZ] = pt[Z]; /* construct rotation matrix */ cos_ang = cos(ang); sin_ang = sin(ang); one_m_cosang = 1.0 - cos_ang; n1_sq = dir[X] * dir[X]; n2_sq = dir[Y] * dir[Y]; n3_sq = dir[Z] * dir[Z]; n1_n2 = dir[X] * dir[Y]; n1_n3 = dir[X] * dir[Z]; n2_n3 = dir[Y] * dir[Z]; MAT_IDN(rot); rot[0] = n1_sq + (1.0 - n1_sq) * cos_ang; rot[1] = n1_n2 * one_m_cosang - dir[Z] * sin_ang; rot[2] = n1_n3 * one_m_cosang + dir[Y] * sin_ang; rot[4] = n1_n2 * one_m_cosang + dir[Z] * sin_ang; rot[5] = n2_sq + (1.0 - n2_sq) * cos_ang; rot[6] = n2_n3 * one_m_cosang - dir[X] * sin_ang; rot[8] = n1_n3 * one_m_cosang - dir[Y] * sin_ang; rot[9] = n2_n3 * one_m_cosang + dir[X] * sin_ang; rot[10] = n3_sq + (1.0 - n3_sq) * cos_ang; bn_mat_mul(m, rot, tran1); bn_mat_mul2(tran2, m); }
/** * FIXME: this routine is suspect and needs investigating. if run * during view initialization, the shaders regression test fails. */ void _ged_mat_aet(struct ged_view *gvp) { mat_t tmat; fastf_t twist; fastf_t c_twist; fastf_t s_twist; bn_mat_angles(gvp->gv_rotation, 270.0 + gvp->gv_aet[1], 0.0, 270.0 - gvp->gv_aet[0]); twist = -gvp->gv_aet[2] * DEG2RAD; c_twist = cos(twist); s_twist = sin(twist); bn_mat_zrot(tmat, s_twist, c_twist); bn_mat_mul2(tmat, gvp->gv_rotation); }
HIDDEN int scloud_setup(register struct region *rp, struct bu_vls *matparm, void **dpp, const struct mfuncs *mfp, struct rt_i *rtip) /* pointer to reg_udata in *rp */ { register struct scloud_specific *scloud; struct db_full_path full_path; mat_t region_to_model; mat_t model_to_region; mat_t tmp; BU_CK_VLS(matparm); BU_GET(scloud, struct scloud_specific); *dpp = scloud; if (rp->reg_aircode == 0) { bu_log("WARNING(%s): air shader '%s' applied to non-air region.\n%s\n", rp->reg_name, mfp->mf_name, " Set air flag with \"edcodes\" in mged"); bu_bomb(""); } memcpy(scloud, &scloud_defaults, sizeof(struct scloud_specific)); if (rdebug&RDEBUG_SHADE) bu_log("scloud_setup\n"); if (bu_struct_parse(matparm, scloud_parse, (char *)scloud) < 0) return -1; if (rdebug&RDEBUG_SHADE) (void)bu_struct_print(rp->reg_name, scloud_parse, (char *)scloud); /* get transformation between world and "region" coordinates */ if (db_string_to_path(&full_path, rtip->rti_dbip, rp->reg_name)) { /* bad thing */ bu_bomb("db_string_to_path() error"); } if (! db_path_to_mat(rtip->rti_dbip, &full_path, region_to_model, 0, &rt_uniresource)) { /* bad thing */ bu_bomb("db_path_to_mat() error"); } /* get matrix to map points from model space to "region" space */ bn_mat_inv(model_to_region, region_to_model); /* add the noise-space scaling */ MAT_IDN(tmp); if (!EQUAL(scloud->scale, 1.0)) { tmp[0] = tmp[5] = tmp[10] = 1.0 / scloud->scale; } else { tmp[0] = 1.0 / (scloud->vscale[0]); tmp[5] = 1.0 / (scloud->vscale[1]); tmp[10] = 1.0 / (scloud->vscale[2]); } bn_mat_mul(scloud->mtos, tmp, model_to_region); /* add the translation within noise space */ MAT_IDN(tmp); tmp[MDX] = scloud->delta[0]; tmp[MDY] = scloud->delta[1]; tmp[MDZ] = scloud->delta[2]; bn_mat_mul2(tmp, scloud->mtos); bn_mat_inv(scloud->stom, scloud->mtos); return 1; }
/* * Returns - * -2 unknown keyword * -1 error in processing keyword * 0 OK */ int multi_words( char *words[], int nwords ) { if ( strcmp( words[0], "rot" ) == 0 ) { mat_t mat; /* Expects rotations rx, ry, rz, in degrees */ if ( nwords < 4 ) return(-1); MAT_IDN( mat ); bn_mat_angles( mat, atof( words[1] ), atof( words[2] ), atof( words[3] ) ); out_mat( mat, stdout ); return(0); } if ( strcmp( words[0], "xlate" ) == 0 ) { mat_t mat; if ( nwords < 4 ) return(-1); /* Expects translations tx, ty, tz */ MAT_IDN( mat ); MAT_DELTAS( mat, atof( words[1] ), atof( words[2] ), atof( words[3] ) ); out_mat( mat, stdout ); return(0); } if ( strcmp( words[0], "rot_at" ) == 0 ) { mat_t mat; mat_t mat1; mat_t mat2; mat_t mat3; /* JG - Expects x, y, z, rx, ry, rz */ /* Translation back to the origin by (-x, -y, -z) */ /* is done first, then the rotation, and finally */ /* back into the original position by (+x, +y, +z). */ if ( nwords < 7 ) return(-1); MAT_IDN( mat1 ); MAT_IDN( mat2 ); MAT_IDN( mat3 ); MAT_DELTAS( mat1, -atof( words[1] ), -atof( words[2] ), -atof( words[3] ) ); bn_mat_angles( mat2, atof( words[4] ), atof( words[5] ), atof( words[6] ) ); MAT_DELTAS( mat3, atof( words[1] ), atof( words[2] ), atof( words[3] ) ); bn_mat_mul( mat, mat2, mat1 ); bn_mat_mul2( mat3, mat ); out_mat( mat, stdout ); return(0); } if ( strcmp( words[0], "orient" ) == 0 ) { register int i; mat_t mat; double args[8]; /* Expects tx, ty, tz, rx, ry, rz, [scale]. */ /* All rotation is done first, then translation */ /* Note: word[0] and args[0] are the keyword */ if ( nwords < 6+1 ) return(-1); for ( i=1; i<6+1; i++ ) args[i] = 0; args[7] = 1.0; /* optional arg, default to 1 */ for ( i=1; i<nwords; i++ ) args[i] = atof( words[i] ); MAT_IDN( mat ); bn_mat_angles( mat, args[4], args[5], args[6] ); MAT_DELTAS( mat, args[1], args[2], args[3] ); if ( NEAR_ZERO( args[7], VDIVIDE_TOL ) ) { /* Nearly zero, signal error */ fprintf(stderr, "Orient scale arg is near zero ('%s')\n", words[7] ); return(-1); } else { mat[15] = 1 / args[7]; } out_mat( mat, stdout ); return(0); } if ( strcmp( words[0], "ae" ) == 0 ) { mat_t mat; fastf_t az, el; if ( nwords < 3 ) return(-1); /* Expects azimuth, elev, optional twist */ az = atof(words[1]); el = atof(words[2]); #if 0 if ( nwords == 3 ) twist = 0.0; else twist = atof(words[3]); #endif MAT_IDN( mat ); /* XXX does not take twist, for now XXX */ bn_mat_ae( mat, az, el ); out_mat( mat, stdout ); return(0); } if ( strcmp( words[0], "arb_rot_pt" ) == 0 ) { mat_t mat; point_t pt1, pt2; vect_t dir; fastf_t ang; if ( nwords < 1+3+3+1 ) return(-1); /* Expects point1, point2, angle */ VSET( pt1, atof(words[1]), atof(words[2]), atof(words[3]) ); VSET( pt2, atof(words[4]), atof(words[5]), atof(words[6]) ); ang = atof(words[7]) * bn_degtorad; VSUB2( dir, pt2, pt2 ); VUNITIZE(dir); MAT_IDN( mat ); bn_mat_arb_rot( mat, pt1, dir, ang ); out_mat( mat, stdout ); return(0); } if ( strcmp( words[0], "arb_rot_dir" ) == 0 ) { mat_t mat; point_t pt1; vect_t dir; fastf_t ang; if ( nwords < 1+3+3+1 ) return(-1); /* Expects point1, dir, angle */ VSET( pt1, atof(words[1]), atof(words[2]), atof(words[3]) ); VSET( dir, atof(words[4]), atof(words[5]), atof(words[6]) ); ang = atof(words[7]) * bn_degtorad; VUNITIZE(dir); MAT_IDN( mat ); bn_mat_arb_rot( mat, pt1, dir, ang ); out_mat( mat, stdout ); return(0); } if ( strcmp( words[0], "quat" ) == 0 ) { mat_t mat; quat_t quat; /* Usage: quat x, y, z, w */ if ( nwords < 5 ) return -1; QSET( quat, atof(words[1]), atof(words[2]), atof(words[3]), atof(words[4]) ); quat_quat2mat( mat, quat ); out_mat( mat, stdout); return 0; } if ( strcmp( words[0], "fromto" ) == 0 ) { mat_t mat; point_t cur; point_t next; vect_t from; vect_t to; /* Usage: fromto +Z cur_xyz next_xyz */ if ( nwords < 8 ) return -1; if ( strcmp( words[1], "+X" ) == 0 ) { VSET( from, 1, 0, 0 ); } else if ( strcmp( words[1], "-X" ) == 0 ) { VSET( from, -1, 0, 0 ); } else if ( strcmp( words[1], "+Y" ) == 0 ) { VSET( from, 0, 1, 0 ); } else if ( strcmp( words[1], "-Y" ) == 0 ) { VSET( from, 0, -1, 0 ); } else if ( strcmp( words[1], "+Z" ) == 0 ) { VSET( from, 0, 0, 1 ); } else if ( strcmp( words[1], "-Z" ) == 0 ) { VSET( from, 0, 0, -1 ); } else { fprintf(stderr, "fromto '%s' is not +/-XYZ\n", words[1]); return -1; } VSET( cur, atof(words[2]), atof(words[3]), atof(words[4]) ); VSET( next, atof(words[5]), atof(words[6]), atof(words[7]) ); VSUB2( to, next, cur ); VUNITIZE(to); bn_mat_fromto( mat, from, to ); /* Check to see if it worked. */ { vect_t got; MAT4X3VEC( got, mat, from ); if ( VDOT( got, to ) < 0.9 ) { bu_log("\ntabsub ERROR: At t=%s, bn_mat_fromto failed!\n", chanwords[0] ); VPRINT("\tfrom", from); VPRINT("\tto", to); VPRINT("\tgot", got); } } out_mat( mat, stdout ); return 0; } return(-2); /* Unknown keyword */ }
void Convassem() { int i, j, k, comblen, conv = 0, totass = 0; struct solid_list *root, *ptr, *ptr_tmp; struct wmember head, *wmem; int no_of_assoc = 0; int no_of_props = 0; int att_de = 0; unsigned char *rgb; struct brlcad_att brl_att; fastf_t *flt; bu_log("\nConverting solid assembly entities:\n"); ptr = NULL; root = NULL; BU_LIST_INIT(&head.l); for (i = 0; i < totentities; i++) { /* loop through all entities */ if (dir[i]->type != 184) /* This is not a solid assembly */ continue; /* Increment count of solid assemblies */ totass++; if (dir[i]->param <= pstart) { bu_log("Illegal parameter pointer for entity D%07d (%s)\n" , dir[i]->direct, dir[i]->name); continue; } Readrec(dir[i]->param); /* read first record into buffer */ Readint(&j, ""); /* read entity type */ if (j != 184) { bu_log("Incorrect entity type in Parameter section for entity %d\n", i); return; } Readint(&comblen, ""); /* read number of members in group */ /* Read pointers to group members */ for (j = 0; j < comblen; j++) { if (ptr == NULL) { root = (struct solid_list *)bu_malloc(sizeof(struct solid_list), "Convassem: root"); ptr = root; } else { ptr->next = (struct solid_list *)bu_malloc(sizeof(struct solid_list), "Convassem: ptr->next"); ptr = ptr->next; } ptr->next = NULL; /* Read pointer to an object */ Readint(&ptr->item, ""); if (ptr->item < 0) ptr->item = (-ptr->item); /* Convert pointer to a "dir" index */ ptr->item = (ptr->item-1)/2; /* Save name of object */ ptr->name = dir[ptr->item]->name; /* increment reference count */ dir[ptr->item]->referenced++; } /* Read pointer to transformation matrix for each member */ ptr = root; for (j = 0; j < comblen; j++) { ptr->matrix = 0; /* Read pointer to a transformation */ Readint(&ptr->matrix, ""); if (ptr->matrix < 0) ptr->matrix = (-ptr->matrix); /* Convert to a "dir" index */ if (ptr->matrix) ptr->matrix = (ptr->matrix-1)/2; else ptr->matrix = (-1); /* flag to indicate "none" */ ptr = ptr->next; } /* skip over the associativities */ Readint(&no_of_assoc, ""); for (k = 0; k < no_of_assoc; k++) Readint(&j, ""); /* get property entity DE's */ Readint(&no_of_props, ""); for (k = 0; k < no_of_props; k++) { Readint(&j, ""); if (dir[(j-1)/2]->type == 422 && dir[(j-1)/2]->referenced == brlcad_att_de) { /* this is one of our attribute instances */ att_de = j; } } Read_att(att_de, &brl_att); /* Make the members */ ptr = root; while (ptr != NULL) { /* copy the members original transformation matrix */ for (j = 0; j < 16; j++) ptr->rot[j] = (*dir[ptr->item]->rot)[j]; /* Apply any matrix indicated for this group member */ if (ptr->matrix > (-1)) { #if defined(USE_BN_MULT_) /* a <= a X b */ bn_mat_mul2(ptr->rot, *(dir[ptr->matrix]->rot)); #else /* a X b => o */ Matmult(ptr->rot, *(dir[ptr->matrix]->rot), ptr->rot); #endif } wmem = mk_addmember(ptr->name, &head.l, NULL, operators[Union]); flt = (fastf_t *)ptr->rot; for (j = 0; j < 16; j++) { wmem->wm_mat[j] = (*flt); flt++; } ptr = ptr->next; } /* Make the object */ if (dir[i]->colorp != 0) rgb = (unsigned char*)dir[i]->rgb; else rgb = (unsigned char *)0; mk_lrcomb(fdout , dir[i]->name, /* name */ &head, /* members */ brl_att.region_flag, /* region flag */ brl_att.material_name, /* material name */ brl_att.material_params, /* material parameters */ rgb, /* color */ brl_att.ident, /* ident */ brl_att.air_code, /* air code */ brl_att.material_code, /* GIFT material */ brl_att.los_density, /* los density */ brl_att.inherit); /* inherit */ /* Increment the count of successful conversions */ conv++; /* Free some memory */ ptr = root; while (ptr != NULL) { ptr_tmp = ptr->next; bu_free((char *)ptr, "convassem: ptr"); ptr = ptr_tmp; } } bu_log("Converted %d solid assemblies successfully out of %d total assemblies\n", conv, totass); }
int _ged_do_rot(struct ged *gedp, char coord, mat_t rmat, int (*func)()) { mat_t temp1, temp2; if (func != (int (*)())0) return (*func)(gedp, coord, gedp->ged_gvp->gv_rotate_about, rmat); switch (coord) { case 'm': /* transform model rotations into view rotations */ bn_mat_inv(temp1, gedp->ged_gvp->gv_rotation); bn_mat_mul(temp2, gedp->ged_gvp->gv_rotation, rmat); bn_mat_mul(rmat, temp2, temp1); break; case 'v': default: break; } /* Calculate new view center */ if (gedp->ged_gvp->gv_rotate_about != 'v') { point_t rot_pt; point_t new_origin; mat_t viewchg, viewchginv; point_t new_cent_view; point_t new_cent_model; switch (gedp->ged_gvp->gv_rotate_about) { case 'e': VSET(rot_pt, 0.0, 0.0, 1.0); break; case 'k': MAT4X3PNT(rot_pt, gedp->ged_gvp->gv_model2view, gedp->ged_gvp->gv_keypoint); break; case 'm': /* rotate around model center (0, 0, 0) */ VSET(new_origin, 0.0, 0.0, 0.0); MAT4X3PNT(rot_pt, gedp->ged_gvp->gv_model2view, new_origin); break; default: return GED_ERROR; } bn_mat_xform_about_pt(viewchg, rmat, rot_pt); bn_mat_inv(viewchginv, viewchg); /* Convert origin in new (viewchg) coords back to old view coords */ VSET(new_origin, 0.0, 0.0, 0.0); MAT4X3PNT(new_cent_view, viewchginv, new_origin); MAT4X3PNT(new_cent_model, gedp->ged_gvp->gv_view2model, new_cent_view); MAT_DELTAS_VEC_NEG(gedp->ged_gvp->gv_center, new_cent_model); } /* pure rotation */ bn_mat_mul2(rmat, gedp->ged_gvp->gv_rotation); ged_view_update(gedp->ged_gvp); return GED_OK; }