#include "mattoc.h" Vector3d mattocvector3d(const mxArray *v) { // Convert a matlab vector3d class into a C++ Vector3d class // see @vector3d/vector3d.m for description of the matlab vector3d class // see include/matrix3d.h for a description of the C++ vector3d class mxArray *tmp; const char * fname; double *x,*y,*z; fname = mxGetFieldNameByNumber(v,0); tmp = mxGetField(v,0,fname); #if MEXDEBUG if (!mxIsDouble(tmp) && !(mxGetM(tmp) == 1) && !(mxGetN(tmp) == 1)){ mexErrMsgTxt("Error in conversion of Vector3d."); } #endif x = mxGetPr(tmp); fname = mxGetFieldNameByNumber(v,1); tmp = mxGetField(v,0,fname); #if MEXDEBUG if (!mxIsDouble(tmp) && !(mxGetM(tmp) == 1) && !(mxGetN(tmp) == 1)){ mexErrMsgTxt("Error in conversion of Vector3d."); } #endif y = mxGetPr(tmp); fname = mxGetFieldNameByNumber(v,2); tmp = mxGetField(v,0,fname); #if MEXDEBUG if (!mxIsDouble(tmp) && !(mxGetM(tmp) == 1) && !(mxGetN(tmp) == 1)){ mexErrMsgTxt("Error in conversion of Vector3d."); } #endif z = mxGetPr(tmp); return Vector3d(x[0],y[0],z[0]); } Vector3d mattocvector3d(const mxArray *v,int idx) { // Convert a matlab vector3d class into a C++ Vector3d class // see @vector3d/vector3d.m for description of the matlab vector3d class // see include/matrix3d.h for a description of the C++ vector3d class mxArray *tmp; const char * fname; double *x,*y,*z; fname = mxGetFieldNameByNumber(v,0); tmp = mxGetField(v,idx,fname); #if MEXDEBUG if (!mxIsDouble(tmp) && !(mxGetM(tmp) == 1) && !(mxGetN(tmp) == 1)){ mexErrMsgTxt("Error in conversion of Vector3d."); } #endif x = mxGetPr(tmp); fname = mxGetFieldNameByNumber(v,1); tmp = mxGetField(v,idx,fname); #if MEXDEBUG if (!mxIsDouble(tmp) && !(mxGetM(tmp) == 1) && !(mxGetN(tmp) == 1)){ mexErrMsgTxt("Error in conversion of Vector3d."); } #endif y = mxGetPr(tmp); fname = mxGetFieldNameByNumber(v,2); tmp = mxGetField(v,idx,fname); #if MEXDEBUG if (!mxIsDouble(tmp) && !(mxGetM(tmp) == 1) && !(mxGetN(tmp) == 1)){ mexErrMsgTxt("Error in conversion of Vector3d."); } #endif z = mxGetPr(tmp); return Vector3d(x[0],y[0],z[0]); } Vector mattocvector(const mxArray *v) { // Convert a matlab vector into a C++ Vector class // matlab vectors are normal (not a class) arrays // see include/matrix.h for a description of the C++ vector class //printf("inside mattocvector\n"); double *x; //printf("inside mattocvector\n"); x = mxGetPr(v); //printf("inside mattocvector\n"); unsigned int n = mxGetN(v); //printf("mxGetN = %d\n",n); unsigned int len = mxGetN(v) * mxGetM(v); //printf("inside mattocvector\n"); if (len == 0) return Vector(); else return Vector(x,len); } void ctomatvector(const Vector &v, double *x) { // make sure memory is allocated for x correctly... for (int i = 0;i 0.0) { // first field is array of cspheres fname = mxGetFieldNameByNumber(g,0); tmp = mxGetField(g,0,fname); int ns = mxGetN(tmp); printf("number of spheres = %d\n",ns); Sphere sp; for (int i = 0;i 0.0) { // second field is array of rods fname = mxGetFieldNameByNumber(g,1); tmp = mxGetField(g,0,fname); int nr = mxGetN(tmp); printf("number of rods = %d\n",nr); Rod rd; for (int i = 0;i 0.0) { // fourth field is an array of polygons fname = mxGetFieldNameByNumber(g,3); tmp = mxGetField(g,0,fname); int npoly = mxGetN(tmp); MyPolygon poly; for (int i = 0;i 0.0) { // fifth field is an array of polyhedron! fname = mxGetFieldNameByNumber(g,4); tmp = mxGetField(g,0,fname); int npoly3d = mxGetN(tmp); Polyhedron p3d; for (int i=0;i 0.0) { out.AddShape(sp); //printf("just added a sphere radius %f\n",radius); } } // second field is array of rods fname = mxGetFieldNameByNumber(g,1); tmp = mxGetField(g,0,fname); int nr = mxGetN(tmp); //printf("number of rods = %d\n",nr); Rod rd; for (i = 0;i 0.0) { out.AddShape(rd); //printf("just added a rod radius %f\n",rradius); } } // third field is handles to matlab objects...only used in matlab // fourth field is an array of polygons fname = mxGetFieldNameByNumber(g,3); tmp = mxGetField(g,0,fname); int npoly = mxGetN(tmp); //printf("number of poly2d = %d\n",npoly); MyPolygon poly; for (i = 0;i 0.0) { poly = mattocpoly(tmp,i); out.AddShape(poly); //printf("just added a poly2d\n"); } } else { poly = mattocpoly(tmp,i); out.AddShape(poly); //printf("just added a poly2d\n"); } } // fifth field is an array of polyhedron! fname = mxGetFieldNameByNumber(g,4); tmp = mxGetField(g,0,fname); int npoly3d = mxGetN(tmp); //printf("number of poly3d = %d\n",npoly3d); Polyhedron p3d; for (i=0;i 0.0) { p3d = mattocpolyhedron(tmp,i); out.AddShape(p3d); //printf("just added a poly3d color %f\n",jcolor); } } else { p3d = mattocpolyhedron(tmp,i); //printf("trying to add a polyhedron\n"); out.AddShape(p3d); //printf("just added a poly3d\n"); } } } //end else //printf("returning a geometry in mattoc...\n"); //out.debug(); //printf("done printing in mattoc...\n"); //printf("returning from mattocgeom\n"); return out; } Polyhedron mattocpolyhedron(const mxArray *p, int idx) { //printf("trying to convert a polyhedron\n"); mxArray *tmp; const char * fname; int i = 0; // first field is an array of verteces fname = mxGetFieldNameByNumber(p,0); tmp = mxGetField(p,idx,fname); int nvert = mxGetNumberOfElements(tmp); // number of verteces Vector3d *verts; verts = new Vector3d[nvert]; for (i=0;i= 0){ l1[chld] = l1[i]->AddLink(tmp_link[chld]); chld = next_link[chld]; } } //printf("Setting outlink\n"); Link *outlink = l1[link_no-1]; delete [] tmp_link; delete [] child_link; delete [] next_link; delete [] parent_link; // delete the memory for the array of Link *, without deleting // the links themselves: the links will be deleted in the mex file // once you are done with them delete [] l1; return outlink; } Link * mattoclink(const mxArray *lnk, int link_no) { // get the number of links: int nl = mxGetN(lnk); // create a joint angle vector of zeros: Vector pos; pos.Resize(nl); int i; for (i=0;i= 0){ #if 0 if (l1[i]) l1[chld] = l1[i]->AddLink(tmp_link[chld]); else{ printf("*********************************************************\n"); printf("Reference to unitialized parent in mexfiles/mattoc.cpp, mattoclink function\n"); printf("Parents must come before children in array of links!\n"); printf("This error message brought you by the pointer police.\n"); printf("*********************************************************\n"); mexErrMsgTxt("Please fix the ordering of your Matlab array!\n"); } #else l1[chld] = l1[i]->AddLink(tmp_link[chld]); #endif //printf("added the link\n"); chld = next_link[chld]; //printf("next chld = %d\n",chld); } } //printf("Setting outlink\n"); Link *outlink = l1[link_no-1]; delete [] tmp_link; delete [] child_link; delete [] next_link; delete [] parent_link; // delete the memory for the array of Link *, without deleting // the links themselves: the links will be deleted in the mex file // once you are done with them delete [] l1; return outlink; } Link mattoclink(const mxArray *lnk, const int i, int &child_link, int &next_link, int &parent_link) { mxArray *tmp; const char * fname; // 1st is M // 2nd is frame //printf("converting the base frame\n"); // 3rd is base_frame (*) fname = mxGetFieldNameByNumber(lnk,2); tmp = mxGetField(lnk,i,fname); Matrix4d bframe = mattocmatrix4d(tmp); // 4th is tip_frame // 5th is inv_base_frame (*) //fname = mxGetFieldNameByNumber(lnk,4); //tmp = mxGetField(lnk,i,fname); //Matrix4d inv_bframe = mattocmatrix4d(tmp); // 6th is inv_frame // 7th is inv_M //printf("converting the joint screw \n"); // 8th is joint screw (*) fname = mxGetFieldNameByNumber(lnk,7); tmp = mxGetField(lnk,i,fname); se3 j_screw = mattocse3(tmp); // 9th is type //printf("converting the ap map\n"); // 10th is active/passive fname = mxGetFieldNameByNumber(lnk,9); tmp = mxGetField(lnk,i,fname); double *atv = mxGetPr(tmp); int apmap = (int)atv[0]; //printf("converting the position\n"); // 11th is joint position (*) fname = mxGetFieldNameByNumber(lnk,10); tmp = mxGetField(lnk,i,fname); double *pos = mxGetPr(tmp); //printf("converting the velocity\n"); // 12th is joint velocity (*) fname = mxGetFieldNameByNumber(lnk,11); tmp = mxGetField(lnk,i,fname); double *vel = mxGetPr(tmp); //printf("converting the acceleration\n"); // 13th is joint acceleration (*) fname = mxGetFieldNameByNumber(lnk,12); tmp = mxGetField(lnk,i,fname); double *acc = mxGetPr(tmp); // 14th is min fname = mxGetFieldNameByNumber(lnk,13); tmp = mxGetField(lnk,i,fname); double *min = mxGetPr(tmp); // 15th is max fname = mxGetFieldNameByNumber(lnk,14); tmp = mxGetField(lnk,i,fname); double *max = mxGetPr(tmp); //printf("converting the inertia\n"); // 16th is inertia (*) fname = mxGetFieldNameByNumber(lnk,15); tmp = mxGetField(lnk,i,fname); //double *in = mxGetPr(tmp); Matrix inert_mat = mattocmatrix(tmp); //printf("converting the mass matrix\n"); // 17th is general mass matrix (*) //fname = mxGetFieldNameByNumber(lnk,16); //tmp = mxGetField(lnk,i,fname); //GenMass gm = mattocgenmass(tmp); //printf("converting the mass\n"); // 18th is the mass (*) fname = mxGetFieldNameByNumber(lnk,17); tmp = mxGetField(lnk,i,fname); double *mass = mxGetPr(tmp); //printf("converting the cent_mass\n"); // 19th is center of mass (*) fname = mxGetFieldNameByNumber(lnk,18); tmp = mxGetField(lnk,i,fname); Vector3d cent_mass = mattocvector3d(tmp); #if 1 // 20th is general geometry of link fname = mxGetFieldNameByNumber(lnk,19); tmp = mxGetField(lnk,i,fname); GeneralGeometry gg = mattocgengeom(tmp); #endif // 21st is general geometry of base // 22nd is update_local // 23rd is update_global // 24th is update_inv_local // 25th is update_inv_global // 26th is child (*) fname = mxGetFieldNameByNumber(lnk,25); tmp = mxGetField(lnk,i,fname); double *cl = mxGetPr(tmp); child_link = (int)cl[0] - 1; //printf("child for link %d is %d\n",i,child_link); // 27th is next (*) fname = mxGetFieldNameByNumber(lnk,26); tmp = mxGetField(lnk,i,fname); double *nl = mxGetPr(tmp); next_link = (int)nl[0] - 1; //printf("next for link %d is %d\n",i,next_link); // 28th is parent (*) fname = mxGetFieldNameByNumber(lnk,27); tmp = mxGetField(lnk,i,fname); double *pl = mxGetPr(tmp); parent_link = (int)pl[0] - 1; //printf("parent for link %d is %d\n",i,parent_link); // 29th is baseaccel fname = mxGetFieldNameByNumber(lnk,28); tmp = mxGetField(lnk,i,fname); Vector3d base_acc = mattocvector3d(tmp); // 30th is joint torque fname = mxGetFieldNameByNumber(lnk,29); tmp = mxGetField(lnk,i,fname); double *tau = mxGetPr(tmp); //31st is applied force array fname = mxGetFieldNameByNumber(lnk,30); tmp = mxGetField(lnk,i,fname); ForceApp fa; Link l1(j_screw,pos[0]); l1.BaseFrame(bframe); l1.SetMassInfo(inert_mat, mass[0], cent_mass); l1.JointVel(vel[0]); l1.JointAcc(acc[0]); l1.BaseAcceleration(base_acc); l1.MinJointValue(min[0]); l1.MaxJointValue(max[0]); int nfa =mxGetN(tmp); // printf("nfa = %d\n",nfa); for (int j = 0; j < nfa; j++) { fa = mattocforceapp(tmp,j); // printf("Adding force = %f %f %f %f %f %f\n",fa.F.w.x,fa.F.w.y,fa.F.w.z, // fa.F.v.x, fa.F.v.y, fa.F.v.z); l1.AddForce(fa.F,fa.frame); } //printf("done with forceapp crap\n"); if (apmap == 0){ l1.IsPassive(1); l1.Torque(tau[0]); } l1.LinkGeneralGeometry(gg); return l1; } OpenChain mattocopenchain(const mxArray *oc_t) { mxArray *tmp; const char * fname; //printf("inside mattocopenchain\n"); // 1st is numjoints fname = mxGetFieldNameByNumber(oc_t,0); tmp = mxGetField(oc_t,0,fname); int numjoints; #if MEXDEBUG if (!mxIsDouble(tmp) || (mxGetM(tmp) != 1) || (mxGetN(tmp) != 1)){ mexErrMsgTxt("Error in conversion of OpenChain."); } #endif double *temp = mxGetPr(tmp); numjoints = (int)temp[0]; // 2nd is array of joints fname = mxGetFieldNameByNumber(oc_t,1); tmp = mxGetField(oc_t,0,fname); #if MEXDEBUG if (!mxIsStruct(tmp)){ mexErrMsgTxt("Error in conversion of OpenChain."); } #endif //printf("allocating\n"); Joint *cj; cj = new Joint[numjoints]; //printf("done allocating\n"); for (int i=0;i