00001 #ifndef DH_CHAIN_H
00002 #define DH_CHAIN_H
00003
00004 #include <iostream>
00005 #include <string>
00006 #include <vector>
00007 #include <stdlib.h>
00008 #include <stdio.h>
00009
00010 #include "DH_Link.h"
00011
00012 template <class DataType, int dim>
00013 class SceneBase;
00014
00015 template <class DataType, int dim>
00016 class Scene;
00017
00021 template <class DataType, int dim>
00022 class DH_Chain {
00023 public:
00024 friend class Joint<DataType,dim>;
00025 friend class DH_Link<DataType,dim>;
00026 friend class SceneBase<DataType,dim>;
00027 friend class Scene<DataType,dim>;
00028 typedef Joint<DataType,dim> JointType;
00029 typedef DH_Link<DataType,dim> LinkType;
00030 typedef Scene<DataType,dim> SceneType;
00031
00032
00033 DH_Chain() {
00034 id = -1;
00035 joints.clear();
00036 links.clear();
00037 natives.clear();
00038 terNlinks.clear();
00039 mobile = 0;
00040 tipDHMat.resize(dim+1,dim+1);
00041 tipDHMat.assign(imatrix(dim+1));
00042 parentChainId = -1;
00043 }
00044
00045 DH_Chain(const DH_Chain& v) {
00046 id = v.id;
00047 joints = v.joints;
00048 links = v.links;
00049 natives = v.natives;
00050 terNlinks = v.terNlinks;
00051 mobile = v.mobile;
00052 tipDHMat = v.tipDHMat;
00053 parentChainId = v.parentChainId;
00054 }
00055
00056
00057 ~DH_Chain() {}
00058
00059 virtual void AddJoint( JointType * j) {
00060 j->id = joints.size();
00061 joints.push_back(j);
00062 }
00063 virtual void AddJoint( JointType& j) { AddJoint(&j); }
00064
00065 void addLink( LinkType * l ) {
00066 l->id = links.size();
00067 links.push_back(l);
00068 }
00069 void addLink( LinkType& l ) { addLink(&l); }
00070
00071 void addNativeLink( LinkType * l ) {
00072 l->id = natives.size();
00073 natives.push_back(l);
00074 }
00075 void addNativeLink( LinkType& l ) { addNativeLink(&l); }
00076 int GetNumNativeLinks() const {
00077 return ((int) natives.size());
00078 }
00079 LinkType * GetNthNative(int i) {
00080 return natives[i];
00081 }
00082
00083 void addTernLink(int link, int nativeLink) {
00084 boost::array<int,2> tmp;
00085 tmp[0] = link;
00086 tmp[1] = nativeLink;
00087 terNlinks.push_back(tmp);
00088 }
00089 int GetNumTernLinks() const {
00090 return ((int) terNlinks.size());
00091 }
00092
00093 int GetNumLinks() const {
00094 return ((int) links.size());
00095 }
00096
00097 LinkType * GetNthLink(int i) {
00098 return links[i];
00099 }
00100
00101 void calcDHMat( int baseLink, int targetLink) {
00102 LinkType * tlink;
00103 MType mtmp(4,4);
00104 tlink = links[targetLink];
00105 std::vector<int> idTag = tlink->child->CFIdTag();
00106
00107 for (register int i = baseLink; i <= targetLink; i ++) {
00108 mtmp = prod( mtmp, links[i]->DHMat );
00109 }
00110 if (idTag[idTag.size()-1] < 0) {
00111 SceneType::GetPart(idTag)->setDHMat(mtmp);
00112 }
00113 else {
00114 SceneType::GetAssembly(idTag)->setDHMat(mtmp);
00115 }
00116 }
00117
00118 void calcNativeDHMat(SceneType& TheScene, LinkType * native, int baseLink, int targetLink) {
00119 MType mtmp(4,4);
00120 std::vector<int> idTag = native->child->CFIdTag();
00121
00122 for (register int i = baseLink; i <= targetLink; i ++) {
00123 mtmp = prod( mtmp, links[i]->DHMat );
00124 }
00125
00126 if (idTag[idTag.size()-1] < 0) {
00127 TheScene.GetPart(idTag)->setDHMat( prod(mtmp, native->DHMat) );
00128 }
00129 else {
00130 TheScene.GetAssembly(idTag)->setDHMat( prod(mtmp, native->DHMat) );
00131 }
00132 }
00133
00134 void updateLinks() {
00135 for (register int i = 0; i < links.size(); i ++) {
00136 links[i]->update();
00137 }
00138 for (register int i = 0; i < natives.size(); i ++) {
00139 natives[i]->update();
00140 }
00141 }
00142
00143 void setParentChain(int val) { parentChainId = val; }
00144 int getParentChain() { return parentChainId; }
00145
00146 void print(Scene<DataType,dim>& thisScene, int level) {
00147 for (register int i = 0; i < GetNumLinks(); i++) {
00148 links[i]->print(thisScene, level+1);
00149 }
00150 for (register int i = 0; i < GetNumNativeLinks(); i++) {
00151 ( mklog() << std::string(2, '\t') << "Native\n" ).flush();
00152 natives[i]->print(thisScene, level+2);
00153 }
00154 level++;
00155 for (register int i = 0; i < GetNumTernLinks(); i++) {
00156 ( mklog() << std::string(level, '\t') << "terNlinks["<<i<<"]" << terNlinks[i][0]
00157 << " " << terNlinks[i][1] << std::endl ).flush();
00158 }
00159 }
00160
00161 virtual void Restart(std::ifstream& ifs, int& pos, double& t, double& dt) {
00162 #ifdef CHECKRESTART_DEBUG_ON
00163 ( mklog() << "\nRestarting chain " << id << std::endl ).flush();
00164 #endif
00165 ifs.seekg(pos);
00166 ifs.read((char*)&num_joints,sizeof(int));
00167 ifs.read((char*)&num_links,sizeof(int));
00168 ifs.read((char*)&num_natives,sizeof(int));
00169 ifs.read((char*)&num_terNlinks,sizeof(int));
00170 #ifdef CHECKRESTART_DEBUG_ON
00171 ( mklog() << " num_joints " << num_joints << " num_links " << num_links
00172 << " num_natives " << num_natives << " num_terNlinks " << num_terNlinks
00173 << std::endl ).flush();
00174 #endif
00175 ifs.read((char*)&tipDHMat(0,0),sizeof(DataType));
00176 ifs.read((char*)&tipDHMat(0,1),sizeof(DataType));
00177 ifs.read((char*)&tipDHMat(0,2),sizeof(DataType));
00178 ifs.read((char*)&tipDHMat(0,3),sizeof(DataType));
00179 ifs.read((char*)&tipDHMat(1,0),sizeof(DataType));
00180 ifs.read((char*)&tipDHMat(1,1),sizeof(DataType));
00181 ifs.read((char*)&tipDHMat(1,2),sizeof(DataType));
00182 ifs.read((char*)&tipDHMat(1,3),sizeof(DataType));
00183 ifs.read((char*)&tipDHMat(2,0),sizeof(DataType));
00184 ifs.read((char*)&tipDHMat(2,1),sizeof(DataType));
00185 ifs.read((char*)&tipDHMat(2,2),sizeof(DataType));
00186 ifs.read((char*)&tipDHMat(2,3),sizeof(DataType));
00187 ifs.read((char*)&tipDHMat(3,0),sizeof(DataType));
00188 ifs.read((char*)&tipDHMat(3,1),sizeof(DataType));
00189 ifs.read((char*)&tipDHMat(3,2),sizeof(DataType));
00190 ifs.read((char*)&tipDHMat(3,3),sizeof(DataType));
00191 pos = ifs.tellg();
00192
00193 for (register int i=0; i<num_joints; i++) {
00194 joints[i]->Restart(ifs,pos,t,dt);
00195 pos = ifs.tellg();
00196 }
00197 pos = ifs.tellg();
00198 for (register int i=0; i<num_links; i++) {
00199 links[i]->Restart(ifs,pos,t,dt);
00200 pos = ifs.tellg();
00201 }
00202 pos = ifs.tellg();
00203 for (register int i=0; i<num_natives; i++) {
00204 natives[i]->Restart(ifs,pos,t,dt);
00205 pos = ifs.tellg();
00206 }
00207 pos = ifs.tellg();
00208 for (register int i=0; i<num_terNlinks; i++) {
00209 ifs.read((char*)&terNlinks[i][0],sizeof(int));
00210 ifs.read((char*)&terNlinks[i][1],sizeof(int));
00211 }
00212 pos = ifs.tellg();
00213 }
00214
00215 virtual void Checkpointing(std::ofstream& ofs) {
00216 #ifdef CHECKRESTART_DEBUG_ON
00217 ( mklog() << "\nCHECKPOINTING chain " << id << std::endl ).flush();
00218 #endif
00219 num_joints = joints.size();
00220 num_links = links.size();
00221 num_natives = natives.size();
00222 num_terNlinks = terNlinks.size();
00223 ofs.write((char*)&num_joints,sizeof(int));
00224 ofs.write((char*)&num_links,sizeof(int));
00225 ofs.write((char*)&num_natives,sizeof(int));
00226 ofs.write((char*)&num_terNlinks,sizeof(int));
00227 #ifdef CHECKRESTART_DEBUG_ON
00228 ( mklog() << " num_joints " << num_joints << " num_links " << num_links
00229 << " num_natives " << num_natives << " num_terNlinks " << num_terNlinks
00230 << std::endl ).flush();
00231 #endif
00232 ofs.write((char*)&tipDHMat(0,0),sizeof(DataType));
00233 ofs.write((char*)&tipDHMat(0,1),sizeof(DataType));
00234 ofs.write((char*)&tipDHMat(0,2),sizeof(DataType));
00235 ofs.write((char*)&tipDHMat(0,3),sizeof(DataType));
00236 ofs.write((char*)&tipDHMat(1,0),sizeof(DataType));
00237 ofs.write((char*)&tipDHMat(1,1),sizeof(DataType));
00238 ofs.write((char*)&tipDHMat(1,2),sizeof(DataType));
00239 ofs.write((char*)&tipDHMat(1,3),sizeof(DataType));
00240 ofs.write((char*)&tipDHMat(2,0),sizeof(DataType));
00241 ofs.write((char*)&tipDHMat(2,1),sizeof(DataType));
00242 ofs.write((char*)&tipDHMat(2,2),sizeof(DataType));
00243 ofs.write((char*)&tipDHMat(2,3),sizeof(DataType));
00244 ofs.write((char*)&tipDHMat(3,0),sizeof(DataType));
00245 ofs.write((char*)&tipDHMat(3,1),sizeof(DataType));
00246 ofs.write((char*)&tipDHMat(3,2),sizeof(DataType));
00247 ofs.write((char*)&tipDHMat(3,3),sizeof(DataType));
00248
00249 for (register int i=0; i<num_joints; i++) {
00250 joints[i]->Checkpointing(ofs);
00251 }
00252
00253 for (register int i=0; i<num_links; i++) {
00254 links[i]->Checkpointing(ofs);
00255 }
00256
00257 for (register int i=0; i<num_natives; i++) {
00258 natives[i]->Checkpointing(ofs);
00259 }
00260
00261 for (register int i=0; i<num_terNlinks; i++) {
00262 ofs.write((char*)&terNlinks[i][0],sizeof(int));
00263 ofs.write((char*)&terNlinks[i][1],sizeof(int));
00264 }
00265 }
00266
00267 void setId(int val) { id = val; }
00268
00269 protected:
00270 int id;
00271 std::vector<JointType *> joints;
00272 std::vector<LinkType *> links, natives;
00273 std::vector<boost::array<int, 2> > terNlinks;
00277 bool mobile;
00278 int parentChainId;
00279 MType tipDHMat;
00280 int num_terNlinks, num_joints, num_links, num_natives;
00281 };
00282
00283 #endif // DH_CHAIN_H