Go to the documentation of this file.00001 #ifndef OPENTISSUE_DYNAMICS_MBD_UTIL_MBD_GET_EXTERNAL_FORCE_VECTOR_H
00002 #define OPENTISSUE_DYNAMICS_MBD_UTIL_MBD_GET_EXTERNAL_FORCE_VECTOR_H
00003
00004
00005
00006
00007
00008
00009
00010 #include <OpenTissue/configuration.h>
00011
00012 #include <OpenTissue/core/math/math_is_number.h>
00013
00014 namespace OpenTissue
00015 {
00016 namespace mbd
00017 {
00033 template<typename indirect_body_iterator,typename vector_type>
00034 void get_external_force_vector(indirect_body_iterator begin, indirect_body_iterator end, vector_type & f_ext, bool compute_velocity_forces)
00035 {
00036 typedef typename indirect_body_iterator::value_type body_type;
00037 typedef typename body_type::math_policy math_policy;
00038 typedef typename body_type::value_traits value_traits;
00039 typedef typename body_type::vector3_type vector3_type;
00040 typedef typename body_type::matrix3x3_type matrix3x3_type;
00041 typedef typename body_type::quaternion_type quaternion_type;
00042 typedef typename vector_type::size_type size_type;
00043
00044 vector3_type velocity_forces;
00045 vector3_type force;
00046 vector3_type torque;
00047 vector3_type omega;
00048 matrix3x3_type invI;
00049
00050 size_type n = std::distance(begin,end);
00051
00052 math_policy::resize( f_ext, 6*n);
00053
00054 typename vector_type::iterator fval = f_ext.begin();
00055
00056 for(indirect_body_iterator body = begin;body!=end;++body)
00057 {
00058 assert(body->is_active() || !"get_external_force_vector(): body was not active");
00059
00060 if(body->is_fixed() || body->is_scripted())
00061 {
00062 *fval++ = value_traits::zero();
00063 *fval++ = value_traits::zero();
00064 *fval++ = value_traits::zero();
00065 *fval++ = value_traits::zero();
00066 *fval++ = value_traits::zero();
00067 *fval++ = value_traits::zero();
00068 continue;
00069 }
00070 body->compute_forces_and_torques(force,torque);
00071 assert(is_number(force(0)) || !"get_external_force_vector(): non number encountered");
00072 assert(is_number(force(1)) || !"get_external_force_vector(): non number encountered");
00073 assert(is_number(force(2)) || !"get_external_force_vector(): non number encountered");
00074 assert(is_number(torque(0)) || !"get_external_force_vector(): non number encountered");
00075 assert(is_number(torque(1)) || !"get_external_force_vector(): non number encountered");
00076 assert(is_number(torque(2)) || !"get_external_force_vector(): non number encountered");
00077
00078 *fval++ = force(0);
00079 *fval++ = force(1);
00080 *fval++ = force(2);
00081
00082 velocity_forces.clear();
00083 if(compute_velocity_forces)
00084 {
00085 body->get_spin(omega);
00086 assert(is_number(omega(0))|| !"get_external_force_vector(): non number encountered");
00087 assert(is_number(omega(1))|| !"get_external_force_vector(): non number encountered");
00088 assert(is_number(omega(2))|| !"get_external_force_vector(): non number encountered");
00089 body->get_inertia_wcs(invI);
00090 velocity_forces = cross( omega , invI*omega);
00091 assert(is_number(velocity_forces(0)) || !"get_external_force_vector(): non number encountered");
00092 assert(is_number(velocity_forces(1)) || !"get_external_force_vector(): non number encountered");
00093 assert(is_number(velocity_forces(2)) || !"get_external_force_vector(): non number encountered");
00094 *fval++ = torque(0) - velocity_forces(0);
00095 *fval++ = torque(1) - velocity_forces(1);
00096 *fval++ = torque(2) - velocity_forces(2);
00097 continue;
00098 }
00099 *fval++ = torque(0);
00100 *fval++ = torque(1);
00101 *fval++ = torque(2);
00102 }
00103 }
00104
00105 template<typename group_type,typename vector_type>
00106 void get_external_force_vector(group_type const & group, vector_type & f_ext, bool compute_velocity_forces )
00107 {
00108 get_external_force_vector(group.body_begin(),group.body_end(),f_ext,compute_velocity_forces);
00109 }
00110
00111 }
00112 }
00113
00114 #endif