26 os <<
"Mean: " << et.
Mean <<
", STD:" << et.
STD <<
", Median: " << et.
Median <<
", IQR: " << et.
IQR <<
", Max: " << et.
Max << std::endl;
32 this->nControlPoints = other.nControlPoints;
33 this->nDoF = other.nDoF;
34 this->dim = other.dim;
35 this->controlNet = other.controlNet;
36 this->spreadAngles = other.spreadAngles;
37 this->index = other.index;
38 this->center = other.center;
39 this->partialDerivatives = other.partialDerivatives;
48 this->spreadAngles = Vector::Constant(nDoF, spreadAngle);
49 this->nControlPoints = pow(3.0, this->nDoF);
51 this->controlNet = Matrix::Zero(this->dim, this->nControlPoints);
52 this->createIndices();
53 this->center = Vector::Zero(this->nDoF);
54 this->partialDerivatives = std::vector<Matrix>(this->nDoF, Matrix::Zero(this->dim, this->nControlPoints));
59 int nSamples = shape.cols();
63 Matrix errors = this->controlNet * this->createSLE(proprioception) - shape;
65 std::vector<Real> summed_squared_errors(nSamples, 0.0f);
67 error_values.
Mean = 0;
69 for (
int iSamples = 0; iSamples < nSamples; iSamples++)
73 for (
int iDim = 0; iDim < this->dim; iDim++)
75 sum += pow(errors(iDim, iSamples), 2);
79 summed_squared_errors[iSamples] =
sqrt(sum);
83 error_values.
Mean /= nSamples;
84 std::sort(summed_squared_errors.begin(), summed_squared_errors.end());
86 error_values.
Median = summed_squared_errors[nSamples / 2];
87 error_values.
IQR = summed_squared_errors[(nSamples * 3) / 4 ] - summed_squared_errors[nSamples / 4];
89 error_values.
Max = *std::max_element(summed_squared_errors.begin(), summed_squared_errors.end());
96 void KBM::createIndices()
99 this->index.resize(nControlPoints, nDoF);
101 for (
int iControlPoints = 0; iControlPoints < nControlPoints; iControlPoints++)
103 int decimal = iControlPoints;
106 for (
int iDoF = 0; iDoF < nDoF; iDoF++)
109 this->index(iControlPoints, iDoF) = decimal % 3;
124 assert(proprioception.rows() == nDoF);
125 int nSamples = proprioception.cols();
126 Matrix SLE(nControlPoints, nSamples);
129 Real bincoeff[] = {1, 2, 1};
131 for (
int iSamples = 0; iSamples < nSamples; iSamples++)
135 for (
int iControlPoints = 0; iControlPoints < nControlPoints; iControlPoints++)
137 SLE(iControlPoints, iSamples) = 1.0;
140 for (
int iDoF = 0; iDoF < this->nDoF; iDoF++)
142 int idx = this->index(iControlPoints, iDoF);
146 Real t = std::tan((proprioception(iDoF, iSamples) - center(iDoF)) / 2.0) / tan(this->spreadAngles[iDoF] / 2.0) / 2.0 + 0.5;
151 gamma = std::cos(this->spreadAngles[iDoF]);
159 SLE(iControlPoints, iSamples) *= gamma * bincoeff[idx] * pow(t, idx) * pow(1.0 - t, 2 - idx);
163 Real t1 = std::tan(a1 / 2.0) / tan(this->spreadAngles[iDoF] / 2.0) / 2.0 + 0.5;
164 Real t2 = std::tan(a2 / 2.0) / tan(this->spreadAngles[iDoF] / 2.0) / 2.0 + 0.5;
169 SLE(iControlPoints, iSamples) *= (1 - t1) * (1 - t2);
173 SLE(iControlPoints, iSamples) *= ((1 - t1) * t2 + (1 - t2) * t1) * std::cos(this->spreadAngles[iDoF]);
177 SLE(iControlPoints, iSamples) *= t1 * t2;
184 weight += SLE(iControlPoints, iSamples);
189 for (
int iControlPoints = 0; iControlPoints < nControlPoints; iControlPoints++)
191 SLE(iControlPoints, iSamples) /= weight;
202 assert(proprioception.cols() == shape.cols());
203 assert(proprioception.rows() == this->nDoF);
204 assert(shape.rows() == this->dim);
205 Matrix SLE = this->createSLE(proprioception);
210 this->controlNet = SLE.transpose().householderQr().solve(shape.transpose()).transpose();
214 this->controlNet =
PLS::solve(SLE, shape, threshold);
223 assert(proprioception.cols() == shape.cols());
224 assert(proprioception.rows() == this->nDoF);
230 int nSamples = proprioception.cols();
231 Matrix SLE = createSLE(proprioception);
234 for (
int iSamples = 0; iSamples < nSamples; iSamples++)
236 for (
int iDim = 0; iDim < this->dim ; iDim++)
238 Vector temp = SLE.col(iSamples);
239 Real delta = shape(iDim, iSamples) - this->controlNet.row(iDim).dot(temp);
241 update = delta * SLE.col(iSamples) * (1.0 / SLE.col(iSamples).dot(SLE.col(iSamples)));
243 this->controlNet.row(iDim) += learnRate *
update.transpose();
250 std::ifstream file(fileName.c_str());
258 std::vector<double>
values;
265 while (getline(file, line))
268 std::stringstream linestream(line);
271 while (getline(linestream,
value,
','))
283 assert(lastCols == cols);
290 assert(this->dim == rows &&
"Incompatible output dimensions");
291 assert(this->nControlPoints == lastCols &&
"Incompatible input dimensions");
293 for (
int i = 0; i < this->dim; i++)
294 for (
int j = 0; j < this->nControlPoints; j++)
296 this->controlNet(i, j) =
values[i * this->nControlPoints + j];
300 ARMARX_DEBUG_S <<
"done (rows: " << rows <<
", Cols: " << lastCols <<
", nDoF: " << this->nDoF <<
", dim: " << this->dim <<
")" << std::endl;
306 this->controlNet = Matrix::Zero(this->dim, this->nControlPoints);
321 return controlNet * createSLE(proprioception);
330 Vector o = controlNet * createSLE(proprioception);
331 Vector3 e1 = this->controlNet * (createSLE(proprioception, dim, (-1) * this->spreadAngles[dim], proprioception(dim, 0)) -
332 createSLE(proprioception, dim, this->spreadAngles[dim], proprioception(dim, 0)));
334 Vector3 e2 = this->controlNet * (createSLE(proprioception, dim, (-1) * this->spreadAngles[dim], proprioception(dim, 0) + offset) -
335 createSLE(proprioception, dim, this->spreadAngles[dim], proprioception(dim, 0) + offset));
340 Matrix result = Matrix::Zero(4, 4);
342 result.block(0, 0, 3, 1) = e1 / e1.norm();
343 result.block(0, 1, 3, 1) = e2 / e2.norm();
344 result.block(0, 2, 3, 1) = e3 / e3.norm();
345 result.block(0, 3, 3, 1) = o;
353 for (
int i = 0; i < this->nDoF; i++)
362 Vector SLEa = this->createSLE(proprioception, iDoF, this->spreadAngles[iDoF], proprioception[iDoF],
false);
363 Vector SLEb = this->createSLE(proprioception, iDoF, (-1) * this->spreadAngles[iDoF], proprioception[iDoF],
false);
364 Vector SLEc = this->createSLE(proprioception, -1, 0.0f, 0.0f,
false);
366 Vector SLE = ((SLEa - SLEb) * SLEc.sum() - (SLEa.sum() - SLEb.sum()) * SLEc) / (SLEc.sum() * SLEc.sum());
367 return this->controlNet * SLE / cos(proprioception[iDoF] / 2.0f) / cos(proprioception[iDoF] / 2.0f) / 2 / tan(this->spreadAngles[iDoF] / 2.0f);
372 Matrix Jacobian(this->dim, this->nDoF);
374 for (
int iDoF = 0; iDoF < this->nDoF; iDoF++)
384 return this->spreadAngles;
410 kbm->controlNet = controlNet;
411 kbm->spreadAngles = spreadAngles;
412 kbm->center = center;
421 std::ofstream file(fileName.c_str(), std::ofstream::trunc);
422 file << std::scientific << std::fixed << std::setprecision(std::numeric_limits<double>::digits);
424 for (
int iDim = 0; iDim < this->dim; iDim++)
426 for (
int iControlPoints = 0; iControlPoints < this->nControlPoints - 1; iControlPoints++)
428 file << this->controlNet(iDim, iControlPoints) <<
",";
432 file << this->controlNet(iDim, nControlPoints - 1) << std::endl;
446 controlNet = FoR->toLocalCoordinateSystem(chain->getTCP()->getGlobalPose()).block(0, 3, 4, 1).cast<
Real>();
450 controlNet.resize(12, 1);
451 Eigen::Transform<float, 3, Eigen::Affine> axis_x(Eigen::Translation<float, 3>(1.0, 0.0, 0.0));
452 Eigen::Transform<float, 3, Eigen::Affine> axis_y(Eigen::Translation<float, 3>(0.0, 1.0, 0.0));
453 controlNet.block(0, 0, 4, 1) = FoR->toLocalCoordinateSystem(chain->getTCP()->getGlobalPose()).block(0, 3, 4, 1).cast<
Real>();
454 controlNet.block(4, 0, 4, 1) = FoR->toLocalCoordinateSystem(chain->getTCP()->getGlobalPose() * axis_x.matrix()).block(0, 3, 4, 1).cast<
Real>();
455 controlNet.block(8, 0, 4, 1) = FoR->toLocalCoordinateSystem(chain->getTCP()->getGlobalPose() * axis_y.matrix()).block(0, 3, 4, 1).cast<
Real>();
458 for (
int i = chain->getSize() - 1; i >= 0; --i)
461 controlNet.resize(controlNet.rows(), controlNet.cols() * 3);
463 VirtualRobot::RobotNodeRevolute* node =
dynamic_cast<VirtualRobot::RobotNodeRevolute*
>(chain->getNode(i).get());
464 Vector3 axis = node->getJointRotationAxisInJointCoordSystem().cast<
Real>();
466 toLocal = node->getGlobalPose().inverse().cast<
Real>();
468 Real factor = 1.0f / cos(spreadAngles(i));
469 Eigen::Transform<Real, 3, Eigen::Affine> rotation(Eigen::AngleAxis<Real>(spreadAngles(i), axis));
470 Eigen::Transform<Real, 3, Eigen::Affine> scaling;
471 Vector3 diag = axis + factor * (Vector3::Constant(1.0f) - axis);
472 scaling = Eigen::Scaling(diag);
474 for (
int i = 0; i < temp.rows() / 4; i++)
476 controlNet.block(i * 4, 0, 4, temp.cols()) =
toGlobal * rotation.inverse() * toLocal * temp.block(i * 4, 0, 4, temp.cols());
477 controlNet.block(i * 4, temp.cols(), 4, temp.cols()) =
toGlobal * scaling * toLocal * temp.block(i * 4, 0, 4, temp.cols());
478 controlNet.block(i * 4, 2 * temp.cols(), 4, temp.cols()) =
toGlobal * rotation * toLocal * temp.block(i * 4, 0, 4, temp.cols());
483 Vector center = Vector::Constant(chain->getSize(), 0.0f);
487 return KBM::createKBM(chain->getSize(), 3, center, spreadAngles, controlNet.block(0, 0, 3, controlNet.cols()));
491 Matrix reducedControlNet(9, controlNet.cols());
492 reducedControlNet.block(0, 0, 3, controlNet.cols()) = controlNet.block(0, 0, 3, controlNet.cols());
493 reducedControlNet.block(3, 0, 3, controlNet.cols()) = controlNet.block(4, 0, 3, controlNet.cols());
494 reducedControlNet.block(6, 0, 3, controlNet.cols()) = controlNet.block(8, 0, 3, controlNet.cols());
495 return KBM::createKBM(chain->getSize(), 9, center, spreadAngles, reducedControlNet);