Revision: 46886 http://brlcad.svn.sourceforge.net/brlcad/?rev=46886&view=rev Author: abhi2011 Date: 2011-09-23 23:16:55 +0000 (Fri, 23 Sep 2011) Log Message: ----------- Moved position transformation functions into simphysics.cpp and rearranged some code to fit in a call to rt
Modified Paths: -------------- brlcad/trunk/src/libged/simulate/simphysics.cpp brlcad/trunk/src/libged/simulate/simulate.c brlcad/trunk/src/libged/simulate/simulate.h Modified: brlcad/trunk/src/libged/simulate/simphysics.cpp =================================================================== --- brlcad/trunk/src/libged/simulate/simphysics.cpp 2011-09-23 22:14:46 UTC (rev 46885) +++ brlcad/trunk/src/libged/simulate/simphysics.cpp 2011-09-23 23:16:55 UTC (rev 46886) @@ -32,6 +32,8 @@ #include <iostream> +#include "bu.h" +#include "raytrace.h" #include "db.h" #include "vmath.h" #include "simulate.h" @@ -181,7 +183,7 @@ btAlignedObjectArray<btCollisionShape*> collision_shapes) { struct rigid_body *current_node; - btVector3 v1, v2; + btVector3 v1, v2, v3; btScalar mass; for (current_node = sim_params->head_node; current_node != NULL; current_node = current_node->next) { @@ -206,7 +208,7 @@ dynamicsWorld->addRigidBody(bb_RigidBody); collision_shapes.push_back(bb_Shape); - bu_vls_printf(sim_params->result_str, "Added static body : %s to simulation with mass %f Kg\n", + bu_log("Added static body : %s to simulation with mass %f Kg\n", current_node->rb_namep, 0.f); } @@ -250,6 +252,8 @@ case PERSIST_FORCE_IGNORE: break; + default: + ; } /* Linear Velocity */ @@ -266,12 +270,15 @@ dynamicsWorld->addRigidBody(bb_RigidBody); - bu_vls_printf(sim_params->result_str, "Added rigid body %s to simulation with mass %f Kg\n", - current_node->rb_namep, mass); + v1 = bb_RigidBody->getLinearVelocity(); + v2 = bb_RigidBody->getAngularVelocity(); + bu_log("Added rigid body %s to simulation with mass %f Kg, lv(%f,%f,%f), av(%f,%f,%f)\n", + current_node->rb_namep, mass, v1[0], v1[1], v1[2], v2[0], v2[1], v2[2]); + } else - bu_vls_printf(sim_params->result_str, "Negative mass of %f Kg detected for %s,\ + bu_log("Negative mass of %f Kg detected for %s,\ exotic forms of matter are not yet supported. Object ignored.\n", mass, current_node->rb_namep); } @@ -279,20 +286,110 @@ return 0; } + /** + * 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 from physics) + */ +int apply_transforms(struct simulation_params *sim_params) +{ + struct rt_db_internal intern; + struct rigid_body *current_node; + struct db_i *dbip = sim_params->dbip; + mat_t t, m; + struct directory *dp; + + for (current_node = sim_params->head_node; current_node != NULL; current_node = current_node->next) { + + dp = current_node->dp; + + /* Get the internal representation of the object */ + if ( !rt_db_lookup_internal(dbip, dp->d_namep, &dp, &intern, LOOKUP_NOISY, &rt_uniresource)){ + bu_log("apply_transforms: rt_db_lookup_internal(%s) failed to get the internal form", + dp->d_namep); + return SIM_ERROR; + } + + + /* Translate to origin without any rotation, before applying rotation */ + MAT_IDN(m); + m[12] = - (current_node->bb_center[0]); + m[13] = - (current_node->bb_center[1]); + m[14] = - (current_node->bb_center[2]); + MAT_TRANSPOSE(t, m); + if (rt_matrix_transform(&intern, t, &intern, 0, dbip, &rt_uniresource) < 0){ + bu_log("apply_transforms: ERROR rt_matrix_transform(%s) failed while \ + translating to origin!\n", + current_node->dp->d_namep); + return SIM_ERROR; + } + + /* Apply rotation with no translation*/ + MAT_COPY(m, current_node->m); + m[12] = 0; + m[13] = 0; + m[14] = 0; + MAT_TRANSPOSE(t, m); + if (rt_matrix_transform(&intern, t, &intern, 0, dbip, &rt_uniresource) < 0){ + bu_log("apply_transforms: ERROR rt_matrix_transform(%s) failed while \ + applying rotation\n", + current_node->dp->d_namep); + return SIM_ERROR; + } + + /* Translate again without any rotation, to apply final position */ + MAT_IDN(m); + m[12] = current_node->m[12]; + m[13] = current_node->m[13]; + m[14] = current_node->m[14]; + MAT_TRANSPOSE(t, m); + if (rt_matrix_transform(&intern, t, &intern, 0, dbip, &rt_uniresource) < 0){ + bu_log("apply_transforms: ERROR rt_matrix_transform(%s) failed while \ + translating to final position\n", + current_node->dp->d_namep); + return SIM_ERROR; + } + + /* Write the modified solid to the db so it can be redrawn at the new position & orientation by Mged */ + if (rt_db_put_internal(current_node->dp, dbip, &intern, &rt_uniresource) < 0) { + bu_log("apply_transforms: ERROR Database write error for '%s', aborting\n", + current_node->dp->d_namep); + return SIM_ERROR; + } + + } + + return SIM_OK; +} + + +/** * Step the dynamics world according to the simulation parameters just once * */ -int step_physics(btDiscreteDynamicsWorld* dynamicsWorld) +int step_physics(btDiscreteDynamicsWorld* dynamicsWorld, struct simulation_params *sim_params) { + int rv, i; + bu_log("----- Starting simulation -----\n"); - //time step of 1/60th of a second(same as internal fixedTimeStep, maxSubSteps=10 to cover 1/60th sec.) - dynamicsWorld->stepSimulation(1/60.f,10); + for (i=0 ; i < sim_params->duration ; i++) { + //time step of 1/60th of a second(same as internal fixedTimeStep, maxSubSteps=10 to cover 1/60th sec.) + dynamicsWorld->stepSimulation(1/60.f,10); + + /* Apply transforms on the participating objects, also shades objects */ + rv = apply_transforms(sim_params); + if (rv != SIM_OK){ + bu_log("step_physics: ERROR while applying transforms\n"); + return SIM_ERROR; + } + } + bu_log("----- Simulation Complete -----\n"); - return 0; + return SIM_OK; } @@ -330,8 +427,7 @@ if(current_node == NULL){ bu_vls_printf(sim_params->result_str, "get_transforms : Could not get the user pointer\n"); - continue; - + return SIM_ERROR; } //Copy the transform matrix @@ -361,7 +457,7 @@ current_node->btbb_dims[1] = current_node->btbb_max[1] - current_node->btbb_min[1]; current_node->btbb_dims[2] = current_node->btbb_max[2] - current_node->btbb_min[2]; - bu_vls_printf(sim_params->result_str, "get_transforms: Dimensions of this BB : %f %f %f\n", + bu_log("get_transforms: Dimensions of this BB : %f %f %f\n", current_node->btbb_dims[0], current_node->btbb_dims[1], current_node->btbb_dims[2]); //Get BB position in 3D space @@ -385,7 +481,7 @@ } } - return 0; + return SIM_OK; } @@ -420,7 +516,7 @@ //delete dynamics world delete dynamicsWorld; - return 0; + return SIM_OK; } @@ -471,7 +567,7 @@ dispatcher->setNearCallback((btNearCallback)nearphase_callback); //Step the physics the required number of times - step_physics(dynamicsWorld); + step_physics(dynamicsWorld, sim_params); //Get the world transforms & AABBs back into the simulation params struct get_transforms(dynamicsWorld, sim_params); Modified: brlcad/trunk/src/libged/simulate/simulate.c =================================================================== --- brlcad/trunk/src/libged/simulate/simulate.c 2011-09-23 22:14:46 UTC (rev 46885) +++ brlcad/trunk/src/libged/simulate/simulate.c 2011-09-23 23:16:55 UTC (rev 46886) @@ -657,115 +657,6 @@ /** - * 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 from physics) - */ -int apply_transforms(struct ged *gedp, struct simulation_params *sim_params) -{ - struct rt_db_internal intern; - struct rigid_body *current_node; - mat_t t , m; - /* int rv; */ - - for (current_node = sim_params->head_node; current_node != NULL; current_node = current_node->next) { - - if(strcmp(current_node->rb_namep, sim_params->ground_plane_name) == 0) - continue; - - /* Get the internal representation of the object */ - GED_DB_GET_INTERNAL(gedp, &intern, current_node->dp, bn_mat_identity, &rt_uniresource, GED_ERROR); - - /* Translate to origin without any rotation, before applying rotation */ - MAT_IDN(m); - m[12] = - (current_node->bb_center[0]); - m[13] = - (current_node->bb_center[1]); - m[14] = - (current_node->bb_center[2]); - MAT_TRANSPOSE(t, m); - if (rt_matrix_transform(&intern, t, &intern, 0, gedp->ged_wdbp->dbip, &rt_uniresource) < 0){ - bu_vls_printf(gedp->ged_result_str, "apply_transforms: ERROR rt_matrix_transform(%s) failed while \ - translating to origin!\n", - current_node->dp->d_namep); - return GED_ERROR; - } - - /* Apply rotation with no translation*/ - MAT_COPY(m, current_node->m); - m[12] = 0; - m[13] = 0; - m[14] = 0; - MAT_TRANSPOSE(t, m); - if (rt_matrix_transform(&intern, t, &intern, 0, gedp->ged_wdbp->dbip, &rt_uniresource) < 0){ - bu_vls_printf(gedp->ged_result_str, "apply_transforms: ERROR rt_matrix_transform(%s) failed while \ - applying rotation\n", - current_node->dp->d_namep); - return GED_ERROR; - } - - /* Translate again without any rotation, to apply final position */ - MAT_IDN(m); - m[12] = current_node->m[12]; - m[13] = current_node->m[13]; - m[14] = current_node->m[14]; - MAT_TRANSPOSE(t, m); - if (rt_matrix_transform(&intern, t, &intern, 0, gedp->ged_wdbp->dbip, &rt_uniresource) < 0){ - bu_vls_printf(gedp->ged_result_str, "apply_transforms: ERROR rt_matrix_transform(%s) failed while \ - translating to final position\n", - current_node->dp->d_namep); - return GED_ERROR; - } - - /* Write the modified solid to the db so it can be redrawn at the new position & orientation by Mged */ - if (rt_db_put_internal(current_node->dp, gedp->ged_wdbp->dbip, &intern, &rt_uniresource) < 0) { - bu_vls_printf(gedp->ged_result_str, "apply_transforms: ERROR Database write error for '%s', aborting\n", - current_node->dp->d_namep); - return GED_ERROR; - } - - /* Apply the proper shader to match the object state : useful for debugging */ - /* switch(current_node->state){ - case ACTIVE_TAG: - rv = apply_color(gedp, current_node->rb_namep, 255, 255, 0); - break; - - case ISLAND_SLEEPING: - rv = apply_color(gedp, current_node->rb_namep, 255, 0, 255); - break; - - case WANTS_DEACTIVATION: - rv = apply_color(gedp, current_node->rb_namep, 0, 255, 0); - break; - - case DISABLE_DEACTIVATION: - rv = apply_color(gedp, current_node->rb_namep, 255, 0, 0); - break; - - case DISABLE_SIMULATION: - rv = apply_color(gedp, current_node->rb_namep, 132, 255, 0); - break; - - default: - rv = apply_color(gedp, current_node->rb_namep, 0, 135, 233); - } - - if (rv != GED_OK){ - bu_vls_printf(gedp->ged_result_str, "apply_transforms: WARNING Could not set \ - the state color for %s\n", current_node->rb_namep); - } - */ - - /* This will be enabled by a flag later */ - /* insertAABB(gedp, sim_params, current_node); */ - - /* print_rigid_body(current_node); */ - - } - - return GED_OK; -} - - -/** * The libged physics simulation function : * Check flags, adds regions to simulation parameters, runs the simulation * applies the transforms, frees memory @@ -773,7 +664,7 @@ int ged_simulate(struct ged *gedp, int argc, const char *argv[]) { - int rv, duration, i; + int rv; struct simulation_params sim_params; static const char *sim_comb_name = "sim.c"; static const char *ground_plane_name = "sim_gp.r"; @@ -799,7 +690,8 @@ /* Make a list containing the bb and existing transforms of all the objects in the model * which will participate in the simulation */ - duration = atoi(argv[1]); + sim_params.duration = atoi(argv[1]); + sim_params.dbip = gedp->ged_wdbp->dbip; sim_params.result_str = gedp->ged_result_str; sim_params.sim_comb_name = bu_strdup(sim_comb_name); sim_params.ground_plane_name = bu_strdup(ground_plane_name); @@ -809,28 +701,13 @@ return GED_ERROR; } + /* This call will run physics for 1 step and put the resultant vel/forces into sim_params */ + rv = run_simulation(&sim_params); + if (rv != GED_OK){ + bu_vls_printf(gedp->ged_result_str, "%s: ERROR while running the simulation\n", argv[0]); + return GED_ERROR; + } - /* Run the physics simulation for duration number of times */ - for(i=0; i<duration; i++){ - - /* This call will run physics for 1 step and put the resultant vel/forces into sim_params */ - rv = run_simulation(&sim_params); - if (rv != GED_OK){ - bu_vls_printf(gedp->ged_result_str, "%s: ERROR while running the simulation\n", argv[0]); - return GED_ERROR; - } - - /* Apply transforms on the participating objects, also shades objects */ - rv = apply_transforms(gedp, &sim_params); - if (rv != GED_OK){ - bu_vls_printf(gedp->ged_result_str, "%s: ERROR while applying transforms\n", argv[0]); - return GED_ERROR; - } - - /* Now that gedp has the latest object positions, rt can be used to detect overlaps */ - - } - /* Free memory in rigid_body list */ for (current_node = sim_params.head_node; current_node != NULL; ) { next_node = current_node->next; Modified: brlcad/trunk/src/libged/simulate/simulate.h =================================================================== --- brlcad/trunk/src/libged/simulate/simulate.h 2011-09-23 22:14:46 UTC (rev 46885) +++ brlcad/trunk/src/libged/simulate/simulate.h 2011-09-23 23:16:55 UTC (rev 46886) @@ -45,6 +45,10 @@ #define PERSIST_FORCE_ALWAYS 2 #define PERSIST_FORCE_IGNORE 3 +//Force persistence over multiple steps +#define SIM_ERROR 0 +#define SIM_OK 1 + /* Contains information about a single rigid body constructed from a BRL-CAD region. * This structure is the node of a linked list containing the geometry to be added to the sim * Only the bb is currently present, but physical properties like elasticity, custom forces @@ -84,7 +88,9 @@ */ struct simulation_params { int num_bodies; /**< @brief number of rigid bodies participating in the sim */ + int duration; struct bu_vls *result_str; /**< @brief handle to the libged object to access geometry info */ + struct db_i *dbip; char *sim_comb_name; /**< @brief name of the group which contains all sim regions*/ char *ground_plane_name; /**< @brief name of the ground plane region */ struct rigid_body *head_node; /**< @brief link to first rigid body node */ This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. ------------------------------------------------------------------------------ All of the data generated in your IT infrastructure is seriously valuable. Why? It contains a definitive record of application performance, security threats, fraudulent activity, and more. Splunk takes this data and makes sense of it. IT sense. And common sense. http://p.sf.net/sfu/splunk-d2dcopy2 _______________________________________________ BRL-CAD Source Commits mailing list brlcad-commits@lists.sourceforge.net https://lists.sourceforge.net/lists/listinfo/brlcad-commits