Revision: 47088 http://brlcad.svn.sourceforge.net/brlcad/?rev=47088&view=rev Author: abhi2011 Date: 2011-10-05 11:01:29 +0000 (Wed, 05 Oct 2011) Log Message: ----------- Added code to display the normals generated at each manifold
Modified Paths: -------------- brlcad/trunk/src/libged/simulate/simulate.c Modified: brlcad/trunk/src/libged/simulate/simulate.c =================================================================== --- brlcad/trunk/src/libged/simulate/simulate.c 2011-10-05 04:07:52 UTC (rev 47087) +++ brlcad/trunk/src/libged/simulate/simulate.c 2011-10-05 11:01:29 UTC (rev 47088) @@ -45,7 +45,14 @@ /* The C++ simulation function */ extern int run_simulation(struct simulation_params *sim_params); +/* Some constants used to declare arrays */ +#define MAX_FLOATING_POINT_STRLEN 20 +#define NORMAL_SCALE_FACTOR 1 +#define ARROW_WIDTH 0.02 +#define ARROW_BASE_RADIUS 0.02 +#define ARROW_TIP_RADIUS 0.001 + /** * How to use simulate.Blissfully simple interface, more options will be added soon */ @@ -111,13 +118,19 @@ for (current_manifold = rb->first_manifold; current_manifold != NULL; current_manifold = current_manifold->next) { - for (i=0; i<current_manifold->num_contacts; i++) { - bu_log("contact %d of %d, (%f, %f, %f):(%f, %f, %f), n(%f, %f, %f)\n", - i+1, current_manifold->num_contacts, + + bu_log("--Manifold between %s & %s has %d contacts--\n", + current_manifold->rbA->rb_namep, + current_manifold->rbB->rb_namep, + current_manifold->num_contacts); + + for (i=0; i<current_manifold->num_contacts; i++) { + bu_log("%d, (%f, %f, %f):(%f, %f, %f), n(%f, %f, %f)\n", + i+1, V3ARGS(current_manifold->rb_contacts[i].ptA), V3ARGS(current_manifold->rb_contacts[i].ptB), V3ARGS(current_manifold->rb_contacts[i].normalWorldOnB)); - } + } } } @@ -131,7 +144,7 @@ int i; char buffer[500] = ""; for (i=0; i<num_args; i++) { - sprintf(buffer, "%s %s", buffer, cmd_args[i]); + sprintf(buffer, "%s %s", buffer, cmd_args[i]); } bu_log(buffer); @@ -172,15 +185,15 @@ /* Check if the duplicate already exists, and kill it if so */ if (db_lookup(gedp->ged_wdbp->dbip, name, LOOKUP_QUIET) != RT_DIR_NULL) { - bu_log("kill: WARNING \"%s\" exists, deleting it\n", name); - cmd_args[0] = "kill"; - cmd_args[1] = name; - cmd_args[2] = (char *)0; + bu_log("kill: WARNING \"%s\" exists, deleting it\n", name); + cmd_args[0] = "kill"; + cmd_args[1] = name; + cmd_args[2] = (char *)0; - if (ged_kill(gedp, 2, (const char **)cmd_args) != GED_OK) { + if (ged_kill(gedp, 2, (const char **)cmd_args) != GED_OK) { bu_log("kill: ERROR Could not delete existing \"%s\"\n", name); return GED_ERROR; - } + } } return GED_OK; @@ -199,8 +212,8 @@ int rv; if (kill(gedp, new_name) != GED_OK) { - bu_log("kill_copy: ERROR Could not delete existing \"%s\"\n", new_name); - return GED_ERROR; + bu_log("kill_copy: ERROR Could not delete existing \"%s\"\n", new_name); + return GED_ERROR; } /* Copy the passed prim/comb */ @@ -210,9 +223,9 @@ cmd_args[3] = (char *)0; rv = ged_copy(gedp, 3, (const char **)cmd_args); if (rv != GED_OK) { - bu_log("kill_copy: ERROR Could not copy \"%s\" to \"%s\"\n", dp->d_namep, + bu_log("kill_copy: ERROR Could not copy \"%s\" to \"%s\"\n", dp->d_namep, new_name); - return GED_ERROR; + return GED_ERROR; } return GED_OK; @@ -237,16 +250,227 @@ cmd_args[4] = (char *)0; rv = ged_comb(gedp, 4, (const char **)cmd_args); if (rv != GED_OK) { - bu_log("add_to_comb: ERROR Could not add \"%s\" to the combination \"%s\"\n", - target, add); + bu_log("add_to_comb: ERROR Could not add \"%s\" to the combination \"%s\"\n", + add, target); + return GED_ERROR; + } + + return GED_OK; +} + + +/** + * Draws an arrow from, to using the BOT primitive & SPH + * Used to draw manifold normals + * TODO: surely there is a simpler way! + */ +int +arrow(struct ged *gedp, char* name, point_t from, point_t to) +{ + char *cmd_args[20]; + int rv; + char buffer_str[MAX_FLOATING_POINT_STRLEN]; + char *prefix_arrow_line = "arrow_line_"; + char *prefix_arrow_head = "arrow_head_"; + struct bu_vls arrow_line_vls = BU_VLS_INIT_ZERO, arrow_head_vls = BU_VLS_INIT_ZERO; + char *prefixed_arrow_line, *prefixed_arrow_head; + vect_t v; + + /* Arrow line primitive name */ + bu_vls_sprintf(&arrow_line_vls, "%s%s", prefix_arrow_line, name); + prefixed_arrow_line = bu_vls_addr(&arrow_line_vls); + + /* Arrow line primitive name */ + bu_vls_sprintf(&arrow_head_vls, "%s%s", prefix_arrow_head, name); + prefixed_arrow_head = bu_vls_addr(&arrow_head_vls); + + if (kill(gedp, prefixed_arrow_line) != GED_OK) { + bu_log("line: ERROR Could not delete existing \"%s\"\n", prefixed_arrow_line); return GED_ERROR; } + if (kill(gedp, prefixed_arrow_head) != GED_OK) { + bu_log("line: ERROR Could not delete existing \"%s\"\n", prefixed_arrow_head); + return GED_ERROR; + } + + cmd_args[0] = "in"; + cmd_args[1] = prefixed_arrow_line; + cmd_args[2] = "bot"; + cmd_args[3] = "3"; + cmd_args[4] = "1"; + cmd_args[5] = "1"; + cmd_args[6] = "1"; + + sprintf(buffer_str, "%f", from[0]); cmd_args[7] = bu_strdup(buffer_str); + sprintf(buffer_str, "%f", from[1]); cmd_args[8] = bu_strdup(buffer_str); + sprintf(buffer_str, "%f", from[2]); cmd_args[9] = bu_strdup(buffer_str); + + sprintf(buffer_str, "%f", to[0]); cmd_args[10] = bu_strdup(buffer_str); + sprintf(buffer_str, "%f", to[1]); cmd_args[11] = bu_strdup(buffer_str); + sprintf(buffer_str, "%f", to[2]); cmd_args[12] = bu_strdup(buffer_str); + + sprintf(buffer_str, "%f", from[0]); cmd_args[13] = bu_strdup(buffer_str); + sprintf(buffer_str, "%f", from[1]); cmd_args[14] = bu_strdup(buffer_str); + sprintf(buffer_str, "%f", from[2]); cmd_args[15] = bu_strdup(buffer_str); + + cmd_args[16] = "0"; + cmd_args[17] = "1"; + cmd_args[18] = "2"; + + cmd_args[19] = (char *)0; + + print_command(cmd_args, 19); + + rv = ged_in(gedp, 19, (const char **)cmd_args); + if (rv != GED_OK) { + bu_log("line: ERROR Could not draw arrow line \"%s\" (%f,%f,%f)-(%f,%f,%f) \n", + prefixed_arrow_line, V3ARGS(from), V3ARGS(to)); + return GED_ERROR; + } + + add_to_comb(gedp, name, prefixed_arrow_line); + + VSUB2(v, to, from); + VUNITIZE(v); + VSCALE(v, v, 0.1); + bu_log("line: Unit vector (%f,%f,%f)\n", V3ARGS(v)); + + cmd_args[0] = "in"; + cmd_args[1] = prefixed_arrow_head; + cmd_args[2] = "trc"; + + sprintf(buffer_str, "%f", to[0]); cmd_args[3] = bu_strdup(buffer_str); + sprintf(buffer_str, "%f", to[1]); cmd_args[4] = bu_strdup(buffer_str); + sprintf(buffer_str, "%f", to[2]); cmd_args[5] = bu_strdup(buffer_str); + + sprintf(buffer_str, "%f", v[0]); cmd_args[6] = bu_strdup(buffer_str); + sprintf(buffer_str, "%f", v[1]); cmd_args[7] = bu_strdup(buffer_str); + sprintf(buffer_str, "%f", v[2]); cmd_args[8] = bu_strdup(buffer_str); + + + sprintf(buffer_str, "%f", ARROW_BASE_RADIUS); cmd_args[9] = bu_strdup(buffer_str); + sprintf(buffer_str, "%f", ARROW_TIP_RADIUS); cmd_args[10] = bu_strdup(buffer_str); + + cmd_args[11] = (char *)0; + + print_command(cmd_args, 11); + + rv = ged_in(gedp, 11, (const char **)cmd_args); + if (rv != GED_OK) { + bu_log("line: ERROR Could not draw arrow head \"%s\" (%f,%f,%f)-(%f,%f,%f) \n", + prefixed_arrow_head, V3ARGS(from), V3ARGS(to)); + return GED_ERROR; + } + + add_to_comb(gedp, name, prefixed_arrow_head); + return GED_OK; } +/** + * Applies a material to passed comb using libged + * TODO: lower to librt + */ int +apply_material(struct ged *gedp, + char* comb, + char* material, + unsigned char r, + unsigned char g, + unsigned char b) +{ + int rv; + char buffer_str[MAX_FLOATING_POINT_STRLEN]; + char* cmd_args[28]; + + cmd_args[0] = "mater"; + cmd_args[1] = comb; + cmd_args[2] = material; + + sprintf(buffer_str, "%d", r); cmd_args[3] = bu_strdup(buffer_str); + sprintf(buffer_str, "%d", g); cmd_args[4] = bu_strdup(buffer_str); + sprintf(buffer_str, "%d", b); cmd_args[5] = bu_strdup(buffer_str); + + cmd_args[6] = "0"; + cmd_args[7] = (char *)0; + + rv = ged_mater(gedp, 7, (const char **)cmd_args); + if (rv != GED_OK) { + bu_log("apply_material: WARNING Could not adjust the material to %s(%d, %d, %d) for \"%s\"\n", + material, r, g, b, comb); + return GED_ERROR; + } + + return GED_OK; +} + + +/** + * This function colors the passed comb. It's for showing the current + * state of the object inside the physics engine. + * + * TODO : this should be used with a debugging flag + */ +int +apply_color(struct ged *gedp, + char* name, + unsigned char r, + unsigned char g, + unsigned char b) +{ + struct directory *dp = NULL; + struct rt_comb_internal *comb = NULL; + struct rt_db_internal intern; + struct bu_attribute_value_set avs; + + /* Look up directory pointer for the passed comb name */ + GED_DB_LOOKUP(gedp, dp, name, LOOKUP_NOISY, GED_ERROR); + GED_CHECK_COMB(gedp, dp, GED_ERROR); + GED_DB_GET_INTERNAL(gedp, &intern, dp, (fastf_t *)NULL, &rt_uniresource, GED_ERROR); + + /* Get a comb from the internal format */ + comb = (struct rt_comb_internal *)intern.idb_ptr; + RT_CK_COMB(comb); + + /* Set the color related members */ + comb->rgb[0] = r; + comb->rgb[1] = g; + comb->rgb[2] = b; + comb->rgb_valid = 1; + comb->inherit = 0; + + /* Get the current attribute set of the comb from the db */ + bu_avs_init_empty(&avs); + if (db5_get_attributes(gedp->ged_wdbp->dbip, &avs, dp)) { + bu_vls_printf(gedp->ged_result_str, "apply_transforms: ERROR Cannot get attributes for object %s\n", dp->d_namep); + bu_avs_free(&avs); + return GED_ERROR; + } + + /* Sync the changed attributes with the old ones */ + db5_standardize_avs(&avs); + db5_sync_comb_to_attr(&avs, comb); + db5_standardize_avs(&avs); + + /* Put back in db to allow drawing */ + GED_DB_PUT_INTERNAL(gedp, dp, &intern, &rt_uniresource, GED_ERROR); + if (db5_update_attributes(dp, &avs, gedp->ged_wdbp->dbip)) { + bu_vls_printf(gedp->ged_result_str, "apply_transforms: ERROR failed to update attributes\n"); + bu_avs_free(&avs); + return GED_ERROR; + } + + bu_avs_free(&avs); + return GED_OK; +} + + +/** + * Adds physics specific attributes, will be used to add some more properties later + */ +int add_physics_attribs(struct rigid_body *current_node) { @@ -350,6 +574,9 @@ } +/** + * Gets the bounding box of all the combs which will participate in the simulation + */ int get_bb(struct ged *gedp, struct simulation_params *sim_params) { @@ -405,7 +632,7 @@ insert_AABB(struct ged *gedp, struct simulation_params *sim_params, struct rigid_body *current_node) { char* cmd_args[28]; - char buffer[20]; + char buffer[MAX_FLOATING_POINT_STRLEN]; int rv; char *prefix = "bb_"; char *prefix_reg = "bb_reg_"; @@ -516,20 +743,9 @@ add_to_comb(gedp, prefixed_reg_name, prefixed_name); /* Adjust the material for region to be almost transparent */ - cmd_args[0] = "mater"; - cmd_args[1] = bu_strdup(prefixed_reg_name); - cmd_args[2] = "plastic tr 0.9"; - cmd_args[3] = "210"; - cmd_args[4] = "0"; - cmd_args[5] = "100"; - cmd_args[6] = "0"; - cmd_args[7] = (char *)0; - rv = ged_mater(gedp, 7, (const char **)cmd_args); - if (rv != GED_OK) { - bu_log("insertAABB: WARNING Could not adjust the material for \"%s\"\n", - prefixed_reg_name); - } + apply_material(gedp, prefixed_reg_name, "plastic tr 0.9", 210, 0, 100); + /* Add the region to the result of the sim so it will be drawn too */ add_to_comb(gedp, sim_params->sim_comb_name, prefixed_reg_name); @@ -553,12 +769,14 @@ char* cmd_args[28]; char buffer[20]; int rv, num_args; - char *prefixed_name, *prefixed_reg_name; + char *prefixed_name, *prefixed_reg_name, *prefixed_normal_name; char *prefix = "mf_"; char *prefix_reg = "mf_reg_"; + char *prefix_normal = "normal_"; struct bu_vls buffer_vls = BU_VLS_INIT_ZERO; char *name; - + vect_t scaled_normal; + point_t from, to; struct sim_manifold *current_manifold; int i; @@ -579,18 +797,21 @@ /* Prepare prefixed manifold region name */ prefixed_reg_name = prefix_name(prefix_reg, name); + /* Prepare prefixed manifold region name */ + prefixed_normal_name = prefix_name(prefix_normal, name); + /* Delete existing manifold prim and region */ rv = kill(gedp, prefixed_name); if (rv != GED_OK) { bu_log("insert_manifolds: ERROR Could not delete existing bounding box arb8 : %s \ - so NOT attempting to add new bounding box\n", prefixed_name); + so NOT attempting to add new bounding box\n", prefixed_name); return GED_ERROR; } rv = kill(gedp, prefixed_reg_name); if (rv != GED_OK) { bu_log("insert_manifolds: ERROR Could not delete existing bounding box region : %s \ - so NOT attempting to add new region\n", prefixed_reg_name); + so NOT attempting to add new region\n", prefixed_reg_name); return GED_ERROR; } @@ -637,6 +858,9 @@ cmd_args[15] = (char *)0; num_args = 15; + + VADD2SCALE(from, current_manifold->rb_contacts[0].ptA, + current_manifold->rb_contacts[1].ptB, 0.5); break; case 3: @@ -672,6 +896,9 @@ } cmd_args[27] = (char *)0; num_args = 27; + + VADD2SCALE(from, current_manifold->rb_contacts[0].ptA, + current_manifold->rb_contacts[2].ptB, 0.5); break; default: @@ -692,29 +919,30 @@ /* Make the region for the manifold primitive */ add_to_comb(gedp, prefixed_reg_name, prefixed_name); - /* Adjust the material for region to be almost transparent */ - cmd_args[0] = "mater"; - cmd_args[1] = bu_strdup(prefixed_reg_name); - cmd_args[2] = "plastic tr 0.9"; - cmd_args[3] = "210"; - cmd_args[4] = "210"; - cmd_args[5] = "0"; - cmd_args[6] = "0"; - cmd_args[7] = (char *)0; - rv = ged_mater(gedp, 7, (const char **)cmd_args); - if (rv != GED_OK) { - bu_log("insert_manifolds: WARNING Could not adjust the material for \"%s\"\n", - prefixed_reg_name); - } + /* Adjust the material for region to be visible */ + apply_material(gedp, prefixed_reg_name, "plastic tr 0.9", 210, 210, 0); /* Add the region to the result of the sim so it will be drawn too */ add_to_comb(gedp, sim_params->sim_comb_name, prefixed_reg_name); - } /* if-num_args */ + /* Finally draw the normal */ + VSCALE(scaled_normal, current_manifold->rb_contacts[0].normalWorldOnB, NORMAL_SCALE_FACTOR); + VADD2(to, scaled_normal, from); + + bu_log("insert_manifolds: line (%f,%f,%f)-> (%f,%f,%f)-> (%f,%f,%f) \n", + V3ARGS(current_manifold->rb_contacts[0].normalWorldOnB), + V3ARGS(to), + V3ARGS(scaled_normal)); + + arrow(gedp, prefixed_normal_name, from, to); + add_to_comb(gedp, sim_params->sim_comb_name, prefixed_normal_name); + + }/* if-num_args */ + bu_free(prefixed_name, "simulate : prefixed_name"); bu_free(prefixed_reg_name, "simulate : prefixed_reg_name"); - } /* if-num_contacts */ + }/* if-num_contacts */ } /* end for-manifold */ @@ -725,66 +953,6 @@ /** - * This function colors the passed comb. It's for showing the current - * state of the object inside the physics engine. - * - * TODO : this should be used with a debugging flag - */ -int -apply_color(struct ged *gedp, - char* rb_namep, - unsigned char r, - unsigned char g, - unsigned char b) -{ - struct directory *dp = NULL; - struct rt_comb_internal *comb = NULL; - struct rt_db_internal intern; - struct bu_attribute_value_set avs; - - /* Look up directory pointer for the passed comb name */ - GED_DB_LOOKUP(gedp, dp, rb_namep, LOOKUP_NOISY, GED_ERROR); - GED_CHECK_COMB(gedp, dp, GED_ERROR); - GED_DB_GET_INTERNAL(gedp, &intern, dp, (fastf_t *)NULL, &rt_uniresource, GED_ERROR); - - /* Get a comb from the internal format */ - comb = (struct rt_comb_internal *)intern.idb_ptr; - RT_CK_COMB(comb); - - /* Set the color related members */ - comb->rgb[0] = r; - comb->rgb[1] = g; - comb->rgb[2] = b; - comb->rgb_valid = 1; - comb->inherit = 0; - - /* Get the current attribute set of the comb from the db */ - bu_avs_init_empty(&avs); - if (db5_get_attributes(gedp->ged_wdbp->dbip, &avs, dp)) { - bu_vls_printf(gedp->ged_result_str, "apply_transforms: ERROR Cannot get attributes for object %s\n", dp->d_namep); - bu_avs_free(&avs); - return GED_ERROR; - } - - /* Sync the changed attributes with the old ones */ - db5_standardize_avs(&avs); - db5_sync_comb_to_attr(&avs, comb); - db5_standardize_avs(&avs); - - /* Put back in db to allow drawing */ - GED_DB_PUT_INTERNAL(gedp, dp, &intern, &rt_uniresource, GED_ERROR); - if (db5_update_attributes(dp, &avs, gedp->ged_wdbp->dbip)) { - bu_vls_printf(gedp->ged_result_str, "apply_transforms: ERROR failed to update attributes\n"); - bu_avs_free(&avs); - return GED_ERROR; - } - - bu_avs_free(&avs); - return GED_OK; -} - - -/** * This function takes the transforms present in the current node and * applies them in 3 steps : translate to origin, apply the rotation, * then translate to final position with respect to origin(as obtained This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. ------------------------------------------------------------------------------ All the data continuously generated in your IT infrastructure contains a definitive record of customers, application performance, security threats, fraudulent activity and more. Splunk takes this data and makes sense of it. Business sense. IT sense. Common sense. http://p.sf.net/sfu/splunk-d2dcopy1 _______________________________________________ BRL-CAD Source Commits mailing list brlcad-commits@lists.sourceforge.net https://lists.sourceforge.net/lists/listinfo/brlcad-commits