51 using namespace NEWMAT;
55 Spl_cubic::Spl_cubic(
const Matrix & pts)
73 nb_path = pts.Nrows()-1;
78 cerr <<
"Spl_cubic::Spl_cubic: size of time vector is zero." << endl;
84 cerr <<
"Spl_cubic::Spl_cubic: need at least 4 points to produce a cubic spline." << endl;
90 cerr <<
"Spl_cubic::Spl_cubic: No data for each points." << endl;
94 ColumnVector delta(N-1), beta(N-1);
95 tk = pts.SubMatrix(1,1,1,N);
97 for(
int i = 1; i <= N-1; i++)
99 delta(i) = pts(1,i+1) - pts(1,i);
104 cerr <<
"Spl_cubic::Spl_cubic: time between input points is zero" << endl;
107 beta(i) = 1/delta(i);
110 Matrix A(N-2, N-2); A = 0;
111 A(1,1) = 2*(delta(1)+delta(2));
113 for(
int j = 2; j <= N-2; j++)
116 A(j,j) = 2*(delta(j)+delta(j+1));
117 if( (j+1) <= A.Ncols())
118 A(j,j+1) = delta(j+1);
121 Matrix C(N-2, N); C = 0;
122 for(
int k = 1; k <= N-2; k++)
125 C(k,k+1) = -(beta(k)+beta(k+1));
126 C(k,k+2) = beta(k+1);
129 Matrix _6AiC = 6*A.i()*C;
131 ColumnVector dd_s(N);
132 dd_s(1) = dd_s(N) = 0;
134 Ak = Matrix(nb_path, N-1);
135 Bk = Matrix(nb_path, N-1);
136 Ck = Matrix(nb_path, N-1);
137 Dk = Matrix(nb_path, N-1);
139 for(
int ii = 2; ii <= nb_path+1; ii++)
141 dd_s.SubMatrix(2,N-1,1,1) = _6AiC*pts.SubMatrix(ii,ii,1,N).t();
143 for(
int jj = 1; jj < N; jj++)
146 Ak(ii-1,jj) = 1/(6.0*delta(jj))*(dd_s(jj+1) - dd_s(jj));
147 Bk(ii-1,jj) = 1/2.0*dd_s(jj);
148 Ck(ii-1,jj) = (pts(ii,jj+1)-pts(ii,jj))/delta(jj) -
149 1/6.0*delta(jj)*(dd_s(jj+1) + 2*dd_s(jj));
150 Dk(ii-1,jj) = pts(ii,jj);
160 cerr <<
"Spl_cubic::interpolating: data is not good. Problems occur in constructor." << endl;
164 for(
int i = 1; i < tk.Ncols(); i++)
165 if( (t >= tk(i)) && (t < tk(i+1)) )
167 s = Ak.SubMatrix(1,nb_path,i,i)*pow(t - tk(i), 3) +
168 Bk.SubMatrix(1,nb_path,i,i)*pow(t - tk(i),2) +
169 Ck.SubMatrix(1,nb_path,i,i)*(t - tk(i)) +
170 Dk.SubMatrix(1,nb_path,i,i);
174 cerr <<
"Spl_cubic::interpolating: t is out of range." << endl;
184 cerr <<
"Spl_cubic::first_derivative: data is not good. Problems occur in constructor." << endl;
188 for(
int i = 1; i < tk.Ncols(); i++)
189 if( (t >= tk(i)) && (t < tk(i+1)) )
191 ds = 3*Ak.SubMatrix(1,nb_path,i,i)*pow(t - tk(i), 2) +
192 2*Bk.SubMatrix(1,nb_path,i,i)*(t - tk(i)) +
193 Ck.SubMatrix(1,nb_path,i,i);
197 cerr <<
"Spl_cubic::first_derivative: t not in range." << endl;
207 cerr <<
"Spl_cubic::second_derivative: data is not good. Problems occur in constructor." << endl;
211 for(
int i = 1; i < tk.Ncols(); i++)
212 if( (t >= tk(i)) && (t < tk(i+1)) )
214 ds = 6*Ak.SubMatrix(1,nb_path,i,i)*(t - tk(i)) +
215 2*Bk.SubMatrix(1,nb_path,i,i);
219 cerr <<
"Spl_cubic::second_derivative: t not in range." << endl;
225 Spl_path::Spl_path(
const string & filename)
266 const char *ptr_filename = filename.c_str();
268 std::ifstream inpointfile(ptr_filename, std::ios::in);
278 getline(inpointfile, temp);
279 while(temp.substr(0,1) ==
"#") {
280 getline(inpointfile, temp);
283 if(temp ==
"JOINT_SPACE")
285 else if (temp ==
"CARTESIAN_SPACE")
286 type = CARTESIAN_SPACE;
289 cerr <<
"Spl_path::Spl_path: wrong selection type. Should be joint space or cartesian space." << endl;
293 getline(inpointfile, temp);
294 istringstream inputString1(temp);
295 inputString1 >> nb_path;
298 cerr <<
"Spl_path::Spl_path: number of splines should be >= 1." << endl;
301 ColumnVector p(nb_path);
304 while( !inpointfile.eof() )
306 getline(inpointfile, temp);
307 istringstream inputString2(temp);
309 if(temp.substr(0,1) ==
" ")
311 inputString2 >> time;
314 getline(inpointfile, temp);
315 istringstream inputString3(temp);
317 for(
int i = 1; i <= nb_path; i++)
318 inputString3 >> p(i);
320 pts_map.insert(point_map::value_type(time, p));
324 cerr <<
"Spl_path::Spl_path: can not open file " << filename.c_str() << endl;
328 nb_pts = pts_map.size();
329 point_map::const_iterator iter = pts_map.begin();
330 nb_path = iter->second.Nrows();
332 Matrix pts(nb_path+1, nb_pts); pts = 0;
335 for(iter = pts_map.begin(); iter != pts_map.end(); ++iter)
337 pts(1,i) = iter->first;
338 pts.SubMatrix(2, nb_path+1, i, i) = iter->second;
359 cerr <<
"Spl_path::p_pdot: problem with spline interpolating." << endl;
371 cerr <<
"Spl_path::p_pdot: problem with spline interpolating." << endl;
376 cerr <<
"Spl_path::p_pdot: problem with spline first_derivative." << endl;
384 ColumnVector & pdotdot)
389 cerr <<
"Spl_path::p_pdot_pdotdot: problem with spline interpolating." << endl;
394 cerr <<
"Spl_path::p_pdot_pdotdot: problem with spline first_derivative." << endl;
399 cerr <<
"Spl_path::p_pdot_pdotdot: problem with spline first_derivative." << endl;
408 Spl_Quaternion::Spl_Quaternion(
const string & filename)
435 const char *ptr_filename = filename.c_str();
437 std::ifstream inquatfile(ptr_filename, std::ios::in);
447 while( !inquatfile.eof() )
449 getline(inquatfile, temp);
450 while(temp.substr(0,1) ==
"#") {
451 getline(inquatfile, temp);
453 istringstream inputString(temp);
454 if(temp.substr(0,1) ==
" ")
458 getline(inquatfile, temp);
459 if( (temp.substr(0,1) ==
"r") || (temp.substr(0,1) ==
"R") )
461 getline(inquatfile, temp);
462 istringstream inputString(temp);
463 inputString >> R(1,1) >> R(1,2) >> R(1,3) >> R(2,1) >> R(2,2) >>
464 R(2,3) >> R(3,1) >> R(3,2) >> R(3,3);
467 else if( (temp.substr(0,1) ==
"q") || (temp.substr(0,1) ==
"Q") )
469 getline(inquatfile, temp);
470 istringstream inputString(temp);
471 inputString >> R(1,1) >> R(1,2) >> R(1,3) >> R(2,1);
472 q =
Quaternion(R(1,1), R(1,2), R(1,3), R(2,1));
474 else if(temp.substr(0,1) ==
"")
479 cerr <<
"Spl_Quaternion::Spl_Quaternion: format of input file "
480 << filename.c_str() <<
" is incorrect" << endl;
483 quat_data.insert( quat_map::value_type(time, q));
488 cerr <<
"Spl_Quaternion::Spl_Quaternion: can not open file "
489 << filename.c_str() << endl;
494 Spl_Quaternion::Spl_Quaternion(
const quat_map & quat)
511 quat_map::const_iterator iter1 =
quat_data.begin(), iter2;
513 if( t == iter1->first)
524 if( t < iter2->first)
526 dt = (t - iter1->first)/(iter2->first - iter1->first);
527 s =
Slerp(iter1->second, iter2->second, dt);
535 for(iter1 = iter2; iter1 != ----
quat_data.end(); ++iter1)
539 if( (t >= iter1->first) && (t < iter2->first) )
541 dt = (t - iter1->first)/(iter2->first - iter1->first);
544 qn = (++iter1)->second,
545 qn_1 = (++iter1)->second,
546 qn_2 = (++iter1)->second,
551 Quaternion an = qn*(((qn.i()*qn_1).Log() + (qn.i()*qn_).Log())/-4).
exp(),
552 an_1 = qn_1*(((qn_1.i()*qn_2).Log() + (qn_1.i()*qn).Log())/-4).
exp();
554 s =
Squad(qn, an, an_1, qn_1, dt);
560 iter2 = iter1; iter2++;
561 if( (t >= iter1->first) && (t <= iter2->first) )
563 dt = (t - iter1->first)/(iter2->first - iter1->first);
564 s =
Slerp(iter1->second, iter2->second, dt);
568 cerr <<
"Spl_Quaternion::quat_w: t not in range." << endl;
577 quat_map::const_iterator iter1 =
quat_data.begin(), iter2;
579 if( t == iter1->first)
582 w = ColumnVector(3); w = 0.0;
591 if( t < iter2->first)
593 dt = (t - iter1->first)/(iter2->first - iter1->first);
594 s =
Slerp(iter1->second, iter2->second, dt);
595 ds =
Slerp_prime(iter1->second, iter2->second, dt);
604 for(iter1 = iter2; iter1 != ----
quat_data.end(); ++iter1)
608 if( (t >= iter1->first) && (t < iter2->first) )
610 dt = (t - iter1->first)/(iter2->first - iter1->first);
613 qn = (++iter1)->second,
614 qn_1 = (++iter1)->second,
615 qn_2 = (++iter1)->second,
620 Quaternion an = qn*(((qn.i()*qn_1).Log() + (qn.i()*qn_).Log())/-4).
exp(),
621 an_1 = qn_1*(((qn_1.i()*qn_2).Log() + (qn_1.i()*qn).Log())/-4).
exp();
623 s =
Squad(qn, an, an_1, qn_1, dt);
631 iter2 = iter1; iter2++;
632 if( (t >= iter1->first) && (t <= iter2->first) )
634 dt = (t - iter1->first)/(iter2->first - iter1->first);
635 s =
Slerp(iter1->second, iter2->second, dt);
636 ds =
Slerp_prime(iter1->second, iter2->second, dt);
641 cerr <<
"Spl_Quaternion::quat_w: t not in range." << endl;
689 const char *ptr_filename = filename.c_str();
691 std::ifstream inpointfile(ptr_filename, std::ios::in);
697 getline(inpointfile, temp);
698 while(temp.substr(0,1) ==
"#") {
699 getline(inpointfile, temp);
702 if( (temp ==
"JOINT_SPACE") || (temp ==
"CARTESIAN_SPACE") )
711 type = CARTESIAN_SPACE;
716 cerr <<
"Trajectory_Select::set_trajectory: invalid input file " << filename << endl;