optimizer_gnuplot.cpp
Go to the documentation of this file.
1 
18 #include <cstdio>
19 #include <iostream>
20 #include <cstring>
21 #include <fstream>
22 #include <optimizer_gnuplot.h>
23 
24 using namespace robotLibPbD;
25 
26 
27 bool COptimizerGnuplot::generateGnuPlotCoords(double *st, std::string filenameScript, std::string filenameData)
28 {
29  setData(examples[0]);
30  setValue(st);
31  std::string text;
32 
33  CMatrix pose;
34  std::vector<CVec> points;
35  const double len = 20.0;
36  points.push_back(CVec(0.0, 0.0, 0.0));
37  points.push_back(CVec(len, 0.0, 0.0));
38  points.push_back(CVec(0.0, 0.0, 0.0));
39  points.push_back(CVec(0.0, len, 0.0));
40  points.push_back(CVec(0.0, 0.0, 0.0));
41  points.push_back(CVec(0.0, 0.0, len));
42 
43  std::vector<int> linestyle;
44  linestyle.push_back(1);
45  linestyle.push_back(1);
46  linestyle.push_back(2);
47  linestyle.push_back(2);
48  linestyle.push_back(3);
49  linestyle.push_back(3);
50 
51  std::string plotdata;
52  for (unsigned int i=0; i<frames.getFrames().size(); i++)
53  {
54  pose = frames.getFrame(i)->getRelativeToBase();
55  for (unsigned int j=0; j<points.size(); j++)
56  plotdata += (pose * points[j]).toString() + "\n";
57  }
58 
59  std::ofstream textstream2(filenameData.c_str());
60  if (textstream2.fail())
61  return false;
62 
63  textstream2 << plotdata;
64  textstream2.close();
65 
66  std::vector<double> mins(3, 1.0e30);
67  std::vector<double> maxs(3, -1.0e30);
68  for (unsigned int i=0; i<frames.getFrames().size(); i++)
69  {
70  pose = frames.getFrame(i)->getRelativeToBase();
71  for (unsigned int k=0; k<3; k++)
72  if (pose.a[12 + k] < mins[k])
73  mins[k] = pose.a[12 + k];
74  else if (pose.a[12 + k] > maxs[k])
75  maxs[k] = pose.a[12 + k];
76 
77  for (unsigned int j=0; j<points.size(); j+=2)
78  {
79  CVec t1, t2;
80  t1 = pose * points[j];
81  t2 = pose * points[j+1];
82  text += printToString("set arrow from %f,%f,%f to %f,%f,%f linestyle %d\n",
83  t1.x, t1.y, t1.z,
84  t2.x, t2.y, t2.z,
85  linestyle[i]);
86 
87  }
88  }
89 
90  text += "splot 'plot_data' with points ps 0\n";
91  text += "pause -1\n";
92 
93 
94  std::ofstream textstream(filenameScript.c_str());
95  if (textstream.fail())
96  return false;
97 
98  textstream << text;
99  textstream.close();
100  return true;
101 }
102 
103 bool COptimizerGnuplot::generateGnuPlot(double *st, std::string filenameScript, std::string filenameData)
104 {
105  std::string text;
106  std::string plotdata;
107  CVec t1, t2;
108  std::vector<CVec> t1start(functions.size());
109  for (unsigned int i=0; i<examples.size(); i++)
110  {
111  setData(examples[i]);
112  setValue(st);
113  for (unsigned int j=0; j<functions.size(); j++)
114  {
115  functions[j]->getDistance(t1, t2);
116 
117  if (i == 0)
118  t1start[j] = t1;
119 
120  text += printToString("set arrow from %f,%f,%f to %f,%f,%f linestyle %d\n",
121  t1.x, t1.y, t1.z,
122  t2.x, t2.y, t2.z,
123  1);
124 
125  plotdata += printToString("%f %f %f\n",
126  t1.x, t1.y, t1.z);
127 
128  functions[j]->getDistance(t2);
129  text += printToString("set arrow from %f,%f,%f to %f,%f,%f linestyle %d\n",
130  t1start[j].x, t1start[j].y, t1start[j].z,
131  t1start[j].x + t2.x, t1start[j].y + t2.y, t1start[j].z + t2.z,
132  3);
133  }
134  }
135 
136  text += "splot 'plot_data' with points ps 0\n";
137  text += "pause -1\n";
138 
139  std::ofstream textstream(filenameScript.c_str());
140  if (textstream.fail())
141  return false;
142 
143  textstream << text;
144  textstream.close();
145 
146  std::ofstream textstream2(filenameData.c_str());
147  if (textstream2.fail())
148  return false;
149 
150  textstream2 << plotdata;
151  textstream2.close();
152  return true;
153 }
154 
std::vector< std::vector< double > > examples
Definition: optimizer.h:61
CFrameContainer frames
Definition: optimizer.h:58
PRECISION z
Definition: vecmath.h:60
std::string printToString(const char *format,...)
Definition: utils.cpp:149
bool generateGnuPlot(double *st, std::string filenameScript, std::string filenameData)
PRECISION a[16]
Definition: vecmath.h:190
virtual CMatrix getRelativeToBase()
Returns pose in the base (frame with no predecessor) frame.
Definition: frame.h:508
std::vector< CFrame * > getFrames()
Definition: frame.h:227
void setData(std::vector< double > &values)
Definition: optimizer.cpp:71
Homogenous vector.
Definition: vecmath.h:57
Homogenous matrix.
Definition: vecmath.h:187
std::string toString(bool round=false)
Definition: vecmath.cpp:221
bool generateGnuPlotCoords(double *st, std::string filenameScript, std::string filenameData)
void setValue(double *x)
Definition: optimizer.cpp:40
CFrame * getFrame(unsigned int id)
Definition: frame.h:224
PRECISION y
Definition: vecmath.h:60
PRECISION x
Definition: vecmath.h:60
std::vector< OptimizerGoal * > functions
Definition: optimizer.h:57


asr_kinematic_chain_optimizer
Author(s): Aumann Florian, Heller Florian, Jäkel Rainer, Wittenbeck Valerij
autogenerated on Mon Jun 10 2019 12:35:36