optimizer_iv.cpp
Go to the documentation of this file.
00001 
00018 #include <cstdio>
00019 #include <iostream>
00020 #include <cstring>
00021 #include <fstream> 
00022  
00023 #include <optimizer_iv.h>
00024 
00025 using namespace robotLibPbD;
00026 
00027 std::string COptimizerIv::addLine(CVec &first, CVec &second, double width)
00028 {
00029   std::vector<double> end, start;
00030   start.push_back(first.x);
00031   start.push_back(first.y);
00032   start.push_back(first.z);
00033   end.push_back(second.x);
00034   end.push_back(second.y);
00035   end.push_back(second.z);
00036 
00037    CVec diff(end.at(0) - start.at(0), end.at(1)-start.at(1), end.at(2) - start.at(2));
00038    CVec linIndep;
00039 
00040    if(diff.x == 0) 
00041    {
00042      linIndep.set(1,0,0);
00043    }   
00044    else
00045    { 
00046      linIndep.set(0,1,0);
00047    }
00048 
00049    if (diff.x != 0.0)
00050      {
00051        linIndep.y = 1.0;
00052        linIndep.z = 1.0;
00053        linIndep.x = (-linIndep.y*diff.y - linIndep.z*diff.z) / diff.x;
00054      }
00055    else if (diff.y != 0.0)
00056      {
00057        linIndep.x = 1.0;
00058        linIndep.z = 1.0;
00059        linIndep.y = (-linIndep.x*diff.x - linIndep.z*diff.z) / diff.y;
00060      }
00061    else if (diff.z != 0.0)
00062      {
00063        linIndep.x = 1.0;
00064        linIndep.y = 1.0;
00065        linIndep.z = (-linIndep.x*diff.x - linIndep.y*diff.y) / diff.z;
00066      }
00067 
00068 
00069    linIndep.normalize();
00070    diff.normalize();
00071 
00072 
00073    CVec orth(diff.y * linIndep.z - diff.z * linIndep.y, diff.z * linIndep.x - diff.x - linIndep.z, diff.x * linIndep.y - diff.y * linIndep.w);
00074 
00075    orth = linIndep ^ diff;
00076 
00077 
00078    orth.normalize();    
00079 
00080    CMatrix pose;
00081    //pose.set(linIndep.x, linIndep.y, linIndep.z, 0, diff.x, diff.y, diff.z, 0, orth.x, orth.y, orth.z, 0, 0,0,0,1);
00082    pose.set(linIndep.x, diff.x, orth.x, 0, linIndep.y, diff.y, orth.y, 0, linIndep.z, diff.z, orth.z, 0, 0, 0, 0, 1);   
00083 
00084  
00085    CVec axis;
00086    double angle;
00087 
00088    CMathLib::getRotationFromMatrix(pose, axis, angle);
00089    axis.normalize();
00090 
00091    std::string file;
00092 
00093 
00094   file += "Separator {\n";
00095   
00096   file += printToString("Transform { translation %f %f %f\n rotation %f %f %f %f\n }\n",
00097                         start.at(0) + (end.at(0) - start.at(0)) / 2, start.at(1) + (end.at(1) - start.at(1)) / 2, start.at(2) + (end.at(2) - start.at(2)) / 2,
00098                         axis.x, axis.y, axis.z, angle);
00099    
00100   file += "Material { ambientColor 0.6 0.6 0.6 diffuseColor 0.6 0.6 0.6 }\n";
00101   
00102   file += printToString("Cylinder { radius %f height %f }\n", width, sqrt(pow(end.at(0) - start.at(0),2) + pow(end.at(1) - start.at(1),2) + pow(end.at(2) - start.at(2),2)));
00103 
00104   file += "}\n";  
00105 
00106 
00107    return file;  
00108 }
00109 
00110 std::string COptimizerIv::getInventorCoordinateSystem(double scaleFactor, std::string ivCoordFilename)
00111 {
00112   std::string file;
00113   file += "Separator {\n";
00114 
00115   file += printToString("Transform { scaleFactor %f %f %f }\n", scaleFactor, scaleFactor, scaleFactor);
00116   
00117   file += printToString("File { name \"%s\" }\n", ivCoordFilename.c_str());
00118 
00119   file += "}\n";
00120   return file;
00121 }
00122  
00123 bool COptimizerIv::generateInventor(std::vector<double> values, std::string filename, std::string ivCoordFilename)
00124 {
00125   double *st = (double*)calloc(dofs,sizeof(double));
00126   for (unsigned int i=0; i<dofs && i<values.size(); i++)
00127     st[i] = values[i];
00128 
00129   std::string text;
00130 
00131   CFrame* tcpFrame;
00132   if (!frames.getFrameByName("TCP", tcpFrame))
00133     tcpFrame = NULL;
00134 
00135   CFrame* cameraFrame;
00136   if (!frames.getFrameByName("Camera", cameraFrame))
00137     cameraFrame = NULL;
00138 
00139   CVec axis;
00140   double angle;
00141 
00142   std::string file;
00143   file += "#Inventor V2.1 ascii\n";
00144   file += "Separator {\n";       
00145 
00146 
00147   file += "Separator {\n";
00148    
00149   file += getInventorCoordinateSystem(ivCoordScale, ivCoordFilename);
00150   file += "Sphere { radius 10.0 }\n";
00151   file += "}\n";
00152 
00153   CMatrix pose;
00154 
00155   if (cameraFrame != NULL)
00156     {
00157       pose = cameraFrame->getRelativeToBase();
00158       CMathLib::getRotationFromMatrix(pose, axis, angle);
00159       
00160       file += "Separator {\n";
00161       
00162       file += printToString("Transform { translation %f %f %f\n rotation %f %f %f %f\n  }\n",
00163                             pose.a[12], pose.a[13], pose.a[14],
00164                             axis.x, axis.y, axis.z, angle);
00165       
00166       file += getInventorCoordinateSystem(ivCoordScale, ivCoordFilename);
00167       
00168       file += "Material { ambientColor 0.5 0.5 0.5 diffuseColor 0.5 0.5 0.5 }\n";
00169       
00170       file += printToString("Sphere { radius %f }\n", ivSphereRadius);
00171       
00172       file += "}\n"; 
00173     }
00174 
00175   for (unsigned int i=0; i<examples.size(); i++)
00176     {  
00177       setData(examples[i]);
00178       setValue(st); 
00179 
00180       if (false && tcpFrame != NULL)
00181         {
00182       pose = tcpFrame->getRelativeToBase();
00183       CMathLib::getRotationFromMatrix(pose, axis, angle);
00184       
00185       file += "Separator {\n";
00186       
00187       file += printToString("Transform { translation %f %f %f\n rotation %f %f %f %f\n    }\n",
00188                             pose.a[12], pose.a[13], pose.a[14],
00189                             axis.x, axis.y, axis.z, angle);
00190       
00191       file += getInventorCoordinateSystem(ivCoordScale, ivCoordFilename);
00192       file += "}\n"; 
00193         }
00194       // create inventor file 
00195       
00196       for (unsigned int j=0; j<globalFunctions.size(); j++) 
00197         {
00198           CMatrix pose = globalFunctions[j]->first->getRelativeToBase();
00199           CMathLib::getRotationFromMatrix(pose, axis, angle);
00200           
00201           file += "Separator {\n";
00202  
00203           file += printToString("Transform { translation %f %f %f\n rotation %f %f %f %f\n   }\n",
00204                                 pose.a[12], pose.a[13], pose.a[14],
00205                                 axis.x, axis.y, axis.z, angle);
00206           
00207           file += getInventorCoordinateSystem(ivCoordScale, ivCoordFilename);
00208 
00209           file += "Material { ambientColor 0.0 0.0 1.0 diffuseColor 0.0 0.0 1.0 }\n";
00210 
00211           file += printToString("Sphere { radius %f }\n", ivSphereRadius);
00212 
00213           
00214 
00215           file += "}\n"; 
00216         }
00217 
00218       for (unsigned int j=0; j<functions.size(); j++) 
00219         {
00220           CMatrix pose = functions[j]->first->getRelativeToBase();
00221           CMathLib::getRotationFromMatrix(pose, axis, angle);
00222           
00223           file += "Separator {\n";
00224 
00225 
00226           file += addLine(functions[j]->first->getRelativeToBase()[3], functions[j]->second->getRelativeToBase()[3], ivLineWidth);
00227 
00228           file += printToString("Transform { translation %f %f %f\n rotation %f %f %f %f\n   }\n",
00229                                 pose.a[12], pose.a[13], pose.a[14],
00230                                 axis.x, axis.y, axis.z, angle);
00231           
00232           file += getInventorCoordinateSystem(ivCoordScale, ivCoordFilename);
00233 
00234           file += "Material { ambientColor 0.0 0.0 1.0 diffuseColor 0.0 0.0 1.0 }\n";
00235 
00236           file += printToString("Sphere { radius %f }\n", ivSphereRadius);
00237 
00238           
00239 
00240           file += "}\n"; 
00241 
00242           pose = functions[j]->second->getRelativeToBase();
00243           CMathLib::getRotationFromMatrix(pose, axis, angle);
00244           
00245           file += "Separator {\n";
00246 
00247 
00248           file += printToString("Transform { translation %f %f %f\n rotation %f %f %f %f\n   }\n",
00249                                 pose.a[12], pose.a[13], pose.a[14],
00250                                 axis.x, axis.y, axis.z, angle);
00251            
00252           file += getInventorCoordinateSystem(ivCoordScale, ivCoordFilename);
00253 
00254           file += "Material { ambientColor 0.0 1.0 0.0 diffuseColor 0.0 1.0 0.0 }\n";
00255 
00256           file += printToString("Sphere { radius %f }\n", ivSphereRadius);
00257 
00258           
00259 
00260           file += "}\n"; 
00261           }
00262       
00263 
00264       for (unsigned int j=0; j<functions.size(); j++) 
00265         {
00266           CVec fpos, spos;
00267           CMatrix pose = functions[j]->first->getRelativeToBase();
00268           pose.invert();
00269 
00270           fpos = pose * functions[j]->first->getRelativeToBase()[3];
00271           spos = pose * functions[j]->second->getRelativeToBase()[3];
00272 
00273           
00274           file += "Separator {\n";
00275 
00276           file += addLine(fpos, spos, ivLineWidth);
00277 
00278           file += "Material { ambientColor 0.0 1.0 0.0 diffuseColor 0.0 1.0 0.0 }\n";
00279 
00280           file += printToString("Sphere { radius %f }\n", ivSphereRadius);
00281 
00282           file += printToString("Transform { translation %f %f %f\n }\n",
00283                                 spos.x, spos.y, spos.z);
00284           
00285           //file += "File { name \"data/SAH-coord.iv\" }\n";
00286 
00287           
00288 
00289           file += "}\n"; 
00290           }
00291       
00292 
00293     }
00294 
00295 
00296   for (unsigned int j=0; j<globalFunctions.size(); j++) 
00297     {
00298       globalFunctions[j]->getDistance();
00299       CMatrix pose = globalFunctions[j]->first->getRelativeToBase();
00300       CMathLib::getRotationFromMatrix(pose, axis, angle);
00301       pose.a[12] = globalFunctions[j]->mean[0];
00302       pose.a[13] = globalFunctions[j]->mean[1];
00303       pose.a[14] = globalFunctions[j]->mean[2];
00304       
00305       file += "Separator {\n";
00306       
00307       file += printToString("Transform { translation %f %f %f\n rotation %f %f %f %f\n   }\n",
00308                             pose.a[12], pose.a[13], pose.a[14],
00309                             axis.x, axis.y, axis.z, angle);
00310       
00311       file += getInventorCoordinateSystem(ivCoordScale, ivCoordFilename);
00312 
00313       file += "Material { ambientColor 1.0 0.0 0.0 diffuseColor 1.0 0.0 0.0 }\n";
00314 
00315       file += printToString("Sphere { radius %f }\n", ivSphereRadius*1.01);
00316             
00317       file += "}\n"; 
00318     }
00319 
00320   file += "}\n";
00321 
00322   free(st);
00323 
00324   std::ofstream textstream(filename.c_str());
00325   if (textstream.fail())
00326     return false;
00327   
00328   textstream << file;
00329   textstream.close(); 
00330 
00331   return true;
00332 }
00333  


asr_kinematic_chain_optimizer
Author(s): Aumann Florian, Heller Florian, Jäkel Rainer, Wittenbeck Valerij
autogenerated on Sat Jun 8 2019 19:42:49