00001 #pragma ident "$Id: GeneralConstraint.cpp 2939 2011-10-23 19:55:11Z yanweignss $"
00002
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031 #include "GeneralConstraint.hpp"
00032 #include "SolverGeneral.hpp"
00033 #include <iostream>
00034 #include <iomanip>
00035 #include <string>
00036 #include <algorithm>
00037
00038 namespace gpstk
00039 {
00040 using namespace std;
00041
00042
00043 void GeneralConstraint::constraint( gnssDataMap& gdsMap )
00044 throw(InvalidConstraint)
00045 {
00046 try
00047 {
00048 realConstraint(gdsMap);
00049 }
00050 catch (...)
00051 {
00052 InvalidConstraint e("Invalid constraint.");
00053 GPSTK_THROW(e);
00054 }
00055
00056 }
00057
00058
00059
00060 void GeneralConstraint::constraint( gnssRinex& gRin )
00061 throw(InvalidConstraint)
00062 {
00063 gnssDataMap gdsMap;
00064 SourceID source( gRin.header.source );
00065 gdsMap.addGnssRinex( gRin );
00066
00067 constraint(gdsMap);
00068
00069 }
00070
00071
00072
00073 void GeneralConstraint::process( gnssRinex& gRin, GeneralEquations* gEquPtr )
00074 {
00075 if(gEquPtr)
00076 {
00077
00078 solver.setEquationSystemConstraints(
00079 gEquPtr->getConstraintSystem(gRin) );
00080
00081 DayTime time(gRin.header.epoch);
00082 updateRefSat( time,
00083 gEquPtr->getRefSatSourceMap(),
00084 gEquPtr->getSourceRefSatMap() );
00085
00086 solver.Process(gRin);
00087
00088 refsatSourceMap = gEquPtr->getRefSatSourceMap();
00089 sourceRefsatMap = gEquPtr->getSourceRefSatMap();
00090
00091 constraint(gRin);
00092 }
00093 else
00094 {
00095 solver.Process(gRin);
00096 constraint(gRin);
00097 }
00098
00099 }
00100
00101
00102
00103 void GeneralConstraint::process( gnssDataMap& gdsMap,
00104 GeneralEquations* gEquPtr )
00105 {
00106 if(gEquPtr)
00107 {
00108 solver.setEquationSystemConstraints(
00109 gEquPtr->getConstraintSystem(gdsMap) );
00110
00111 DayTime time( ( *gdsMap.begin() ).first );
00112 updateRefSat( time,
00113 gEquPtr->getRefSatSourceMap(),
00114 gEquPtr->getSourceRefSatMap() );
00115
00116 solver.Process(gdsMap);
00117
00118 refsatSourceMap = gEquPtr->getRefSatSourceMap();
00119 sourceRefsatMap = gEquPtr->getSourceRefSatMap();
00120
00121 constraint(gdsMap);
00122 }
00123 else
00124 {
00125 solver.Process(gdsMap);
00126 constraint(gdsMap);
00127 }
00128
00129 }
00130
00131
00132 int GeneralConstraint::constraintToSolver( ConstraintSystem& system,
00133 gnssDataMap& gdsMap )
00134 {
00135 try
00136 {
00137 Vector<double> meas;
00138 Matrix<double> design;
00139 Matrix<double> covariance;
00140
00141 system.constraintMatrix(getVariables(),meas,design,covariance);
00142
00143
00144
00145
00146
00147
00148
00149
00150
00151
00152
00153 if(meas.size()>0)
00154 {
00155 solver.kFilter.MeasUpdate(meas,design,covariance);
00156
00157 Vector<double> measVector = solver.getEquationSystem()
00158 .getPrefitsVector();
00159
00160 Matrix<double> designMatrix = solver.getEquationSystem()
00161 .getGeometryMatrix();
00162
00163 solver.solution = solver.kFilter.xhat;
00164 solver.covMatrix = solver.kFilter.P;
00165 solver.postfitResiduals = measVector
00166 -(designMatrix * solver.solution);
00167
00168 solver.postCompute(gdsMap);
00169 }
00170
00171 return 0;
00172 }
00173 catch (...)
00174 {
00175 return -1;
00176 }
00177
00178 }
00179
00180
00181 Matrix<double> GeneralConstraint::convertMatrix(size_t n,size_t oi,size_t ni)
00182 {
00183
00184 if( n<1 || oi<0 || ni<0 || oi>=n || ni>=n )
00185 {
00186 Exception e("Invalid input, and check it.");
00187 GPSTK_THROW(e);
00188 }
00189
00190 if(oi==ni) return ident<double>(n);
00191
00192 Matrix<double> T(n,n,0.0);
00193 for( int i = 0; i < n; i++ )
00194 {
00195 if( i != ni )
00196 {
00197 T(i,ni)= -1.0;
00198 T(i,i) = (i == oi) ? 0.0 : 1.0;
00199 }
00200 else
00201 {
00202 T(i,oi) = 1.0;
00203 }
00204 }
00205
00206 return T;
00207
00208 }
00209
00210
00211 Matrix<double> GeneralConstraint::convertMatrix(size_t n,size_t oi,size_t ni,
00212 std::vector<int> iv)
00213 {
00214
00215 bool validInput(true);
00216
00217 if( n<1 || oi<0 || ni<0 || oi>=n || ni>=n ) validInput = false;
00218
00219 for(int i=0;i<iv.size();i++)
00220 {
00221 if(iv[i]<0 || iv[i]>=n)
00222 {
00223 validInput = false;
00224 break;
00225 }
00226 }
00227
00228 if(validInput==false)
00229 {
00230 Exception e("Invalid input, and check it.");
00231 GPSTK_THROW(e);
00232 }
00233
00234 if(oi==ni) return ident<double>(n);
00235
00236 Matrix<double> T(n,n,0.0);
00237 for( int i = 0; i < n; i++ )
00238 {
00239 std::vector<int>::iterator it = find(iv.begin(),iv.end(),i);
00240 if(it==iv.end())
00241 {
00242 T(i,i) = 1.0;
00243 continue;
00244 }
00245
00246 if( i != ni )
00247 {
00248 T(i,ni)= -1.0;
00249 T(i,i) = (i == oi) ? 0.0 : 1.0;
00250 }
00251 else
00252 {
00253 T(i,oi) = 1.0;
00254 }
00255 }
00256
00257 return T;
00258
00259 }
00260
00261
00262
00263
00264 Variable GeneralConstraint::getVariable( const SourceID& source,
00265 const SatID& sat,
00266 const TypeID& type )
00267 {
00268 VariableSet vset;
00269
00270 VariableSet varSet = getVariables(source,type);
00271 for(VariableSet::iterator itv=varSet.begin();
00272 itv!=varSet.end();
00273 ++itv)
00274 {
00275 if( itv->getType()==type ) return (*itv);
00276 }
00277
00278 Exception e("The desirable variable not exist int the solver.");
00279 GPSTK_THROW(e);
00280
00281 return Variable();
00282
00283 }
00284
00285
00286 VariableSet GeneralConstraint::getVariables( const SourceID& source )
00287 {
00288 VariableSet vset;
00289
00290 VariableSet unkSet( getVariables() );
00291
00292 if(source==Variable::allSources) return unkSet;
00293
00294 for( VariableSet::const_iterator itv = unkSet.begin();
00295 itv != unkSet.end();
00296 ++itv )
00297 {
00298 if( (itv->getSource() == source) && itv->getSourceIndexed() )
00299 {
00300 vset.insert( *itv );
00301 }
00302 }
00303
00304 return vset;
00305
00306 }
00307
00308
00309 VariableSet GeneralConstraint::getVariables( const SourceID& source,
00310 const TypeID& type )
00311 {
00312 VariableSet vset;
00313
00314 VariableSet varSet = getVariables(source);
00315 for(VariableSet::iterator itv=varSet.begin();
00316 itv!=varSet.end();
00317 ++itv)
00318 {
00319 if( (itv->getType()==type) && itv->getSourceIndexed() )
00320 {
00321 vset.insert(*itv);
00322 }
00323 }
00324
00325 return vset;
00326
00327 }
00328
00329
00330 VariableSet GeneralConstraint::getVariables( const SourceID& source,
00331 const TypeIDSet& typeSet )
00332 {
00333 VariableSet vset;
00334
00335 VariableSet varSet = getVariables(source);
00336 for(VariableSet::iterator itv=varSet.begin();
00337 itv!=varSet.end();
00338 ++itv)
00339 {
00340 TypeIDSet::const_iterator it = typeSet.find(itv->getType());
00341 if( (it!=typeSet.end()) && itv->getSourceIndexed() )
00342 {
00343 vset.insert(*itv);
00344 }
00345 }
00346
00347 return vset;
00348
00349 }
00350
00351
00352 VariableSet GeneralConstraint::getVariables( const SourceIDSet& sourceSet )
00353 {
00354 VariableSet vset;
00355
00356 VariableSet unkSet( getVariables() );
00357
00358 for( VariableSet::const_iterator itv = unkSet.begin();
00359 itv != unkSet.end();
00360 ++itv )
00361 {
00362 SourceIDSet::const_iterator it = sourceSet.find( (*itv).getSource() );
00363 if( it!=sourceSet.end() ) vset.insert( *itv );
00364 }
00365
00366 return vset;
00367
00368 }
00369
00370
00371 VariableSet GeneralConstraint::getVariables( const SourceIDSet& sourceSet,
00372 const TypeID& type )
00373 {
00374 VariableSet vset;
00375
00376 VariableSet varSet = getVariables(sourceSet);
00377 for(VariableSet::iterator itv=varSet.begin();
00378 itv!=varSet.end();
00379 ++itv)
00380 {
00381 if( (itv->getType()==type) && itv->getSourceIndexed() )
00382 {
00383 vset.insert(*itv);
00384 }
00385 }
00386
00387 return vset;
00388
00389 }
00390
00391
00392 VariableSet GeneralConstraint::getVariables( const SourceIDSet& sourceSet,
00393 const TypeIDSet& typeSet )
00394 {
00395 VariableSet vset;
00396
00397 VariableSet varSet = getVariables(sourceSet);
00398 for(VariableSet::iterator itv=varSet.begin();
00399 itv!=varSet.end();
00400 ++itv)
00401 {
00402 TypeIDSet::const_iterator it = typeSet.find(itv->getType());
00403 if( (it!=typeSet.end()) && itv->getSourceIndexed() )
00404 {
00405 vset.insert(*itv);
00406 }
00407 }
00408
00409 return vset;
00410
00411 }
00412
00413
00414 VariableSet GeneralConstraint::getVariables( const SatID& sat )
00415 {
00416 VariableSet vset;
00417
00418 VariableSet unkSet( getVariables() );
00419
00420 if(sat==Variable::noSats) return vset;
00421
00422 for( VariableSet::const_iterator itv = unkSet.begin();
00423 itv != unkSet.end();
00424 ++itv )
00425 {
00426 if( !(!itv->getSourceIndexed() && itv->getSatIndexed()) )
00427 {
00428 continue;
00429 }
00430
00431 if(sat==Variable::allSats)
00432 {
00433 vset.insert(*itv);
00434 }
00435 else if(sat==Variable::allGPSSats)
00436 {
00437 if(itv->getSatellite().system==SatID::systemGPS)
00438 vset.insert(*itv);
00439 }
00440 else if(sat==Variable::allGlonassSats)
00441 {
00442 if(itv->getSatellite().system==SatID::systemGlonass)
00443 vset.insert(*itv);
00444 }
00445 else if(sat==Variable::allGalileoSats)
00446 {
00447 if(itv->getSatellite().system==SatID::systemGalileo)
00448 vset.insert(*itv);
00449 }
00450 else
00451 {
00452 if(itv->getSatellite()==sat) vset.insert(*itv);
00453 }
00454
00455 }
00456
00457 return vset;
00458
00459 }
00460
00461
00462 VariableSet GeneralConstraint::getVariables( const SatID& sat,
00463 const TypeID& type )
00464 {
00465 VariableSet vset;
00466
00467 VariableSet varSet = getVariables(sat);
00468 for(VariableSet::iterator itv=varSet.begin();
00469 itv!=varSet.end();
00470 ++itv)
00471 {
00472 if( (itv->getType()==type) ) vset.insert(*itv);
00473 }
00474
00475 return vset;
00476
00477 }
00478
00479
00480 VariableSet GeneralConstraint::getVariables( const SatID& sat,
00481 const TypeIDSet& typeSet )
00482 {
00483 VariableSet vset;
00484
00485 VariableSet varSet = getVariables(sat);
00486 for(VariableSet::iterator itv=varSet.begin();
00487 itv!=varSet.end();
00488 ++itv)
00489 {
00490 TypeIDSet::const_iterator it = typeSet.find(itv->getType());
00491 if( (it!=typeSet.end()) ) vset.insert(*itv);
00492 }
00493
00494 return vset;
00495
00496 }
00497
00498
00499 VariableSet GeneralConstraint::getVariables( const SourceID& source,
00500 const SatID& sat,
00501 const TypeID& type )
00502 {
00503 VariableSet vset;
00504
00505 VariableSet varSet = getVariables(source,type);
00506 for(VariableSet::iterator itv=varSet.begin();
00507 itv!=varSet.end();
00508 ++itv)
00509 {
00510 if( itv->getSatellite()==sat ) vset.insert(*itv);
00511 }
00512
00513 return vset;
00514
00515 }
00516
00517
00518 VariableSet GeneralConstraint::getVariables( const SourceID& source,
00519 const SatIDSet& satSet,
00520 const TypeID& type )
00521 {
00522 VariableSet vset;
00523
00524 VariableSet varSet = getVariables(source,type);
00525 for(VariableSet::iterator itv=varSet.begin();
00526 itv!=varSet.end();
00527 ++itv)
00528 {
00529 SatIDSet::const_iterator it = satSet.find(itv->getSatellite());
00530 if( it != satSet.end() ) vset.insert(*itv);
00531 }
00532
00533 return vset;
00534
00535 }
00536
00537
00538 Vector<double> GeneralConstraint::getSolution( const VariableSet& varSet )
00539 {
00540 Vector<double> solution(varSet.size(),0.0);
00541
00542 int i(0);
00543 for(VariableSet::const_iterator it=varSet.begin();
00544 it!=varSet.end();
00545 ++it)
00546 {
00547 solution[i] = solver.getSolution(*it);
00548
00549 i++;
00550 }
00551
00552 return solution;
00553
00554 }
00555
00556
00557 Matrix<double> GeneralConstraint::getCovariance( const VariableSet& varSet )
00558 {
00559 Matrix<double> covariance(varSet.size(),varSet.size(),0.0);
00560
00561 int i(0);
00562 for(VariableSet::const_iterator iti=varSet.begin();
00563 iti!=varSet.end();
00564 ++iti)
00565 {
00566 int j(0);
00567
00568 for(VariableSet::const_iterator itj=varSet.begin();
00569 itj!=varSet.end();
00570 ++itj)
00571 {
00572 covariance[i][j] = solver.getCovariance(*iti,*itj);
00573 j++;
00574 }
00575
00576 i++;
00577 }
00578
00579 return covariance;
00580
00581 }
00582
00583
00584 GeneralConstraint& GeneralConstraint::changeState(
00585 const VariableList& varList,
00586 const Matrix<double>& convertMat)
00587 {
00588 VariableSet allVariable = getCurrentUnknowns();
00589
00590
00591 int varNum(0);
00592 for(VariableList::const_iterator it = varList.begin();
00593 it!=varList.end();
00594 ++it)
00595 {
00596 if(allVariable.find(*it)==allVariable.end())
00597 {
00598 Exception e("The variable doesn't exist in the solver.");
00599 GPSTK_THROW(e);
00600 }
00601
00602 varNum++;
00603 }
00604
00605 if(varNum!=convertMat.rows() || varNum!=convertMat.cols())
00606 {
00607 Exception e("The size of input doesn't match.");
00608 GPSTK_THROW(e);
00609 }
00610
00611 const int numOfVar(varNum);
00612
00613 Vector<double> vectorOfSolution(numOfVar,0.0);
00614 Matrix<double> matrixOfCovariance(numOfVar,numOfVar,0.0);
00615
00616 int i = 0;
00617 for(VariableList::const_iterator iti = varList.begin();
00618 iti != varList.end();
00619 ++iti)
00620 {
00621 vectorOfSolution(i) = solver.getSolution(*iti);
00622
00623 VariableList tempList(varList);
00624
00625 int j(0);
00626 for(VariableList::iterator itj = tempList.begin();
00627 itj!=tempList.end();
00628 ++itj)
00629 {
00630 matrixOfCovariance(i,j) = solver.getCovariance(*iti,*itj);
00631
00632 j++;
00633 }
00634
00635 i++;
00636 }
00637
00638 Vector<double> solution = convertMat*vectorOfSolution;
00639 Matrix<double> covariance = convertMat*matrixOfCovariance
00640 *transpose(convertMat);
00641
00642 i = 0;
00643 for(VariableList::const_iterator iti=varList.begin();
00644 iti!=varList.end();
00645 ++iti)
00646 {
00647 setSolution(*iti,solution(i));
00648
00649 std::list<Variable> tempList(varList);
00650
00651 int j(0);
00652 for(std::list<Variable>::iterator itj = tempList.begin();
00653 itj!=tempList.end();
00654 ++itj)
00655 {
00656 setCovariance(*iti,*itj,covariance(i,j));
00657
00658 j++;
00659 }
00660
00661 i++;
00662 }
00663
00664 return (*this);
00665
00666 }
00667
00668
00669 int GeneralConstraint::findIndexOfSat( const SatIDSet& satSet,
00670 const SatID& sat )
00671 {
00672 int indexOfSat(-1);
00673
00674 int i(0);
00675 for(SatIDSet::const_iterator it=satSet.begin();
00676 it!=satSet.end();
00677 ++it)
00678 {
00679 if((*it)==sat) indexOfSat = i;
00680
00681 i++;
00682 }
00683
00684 return indexOfSat;
00685
00686 }
00687
00688
00689 void GeneralConstraint::stackVariables( VariableList& varList,
00690 const VariableSet& varSet )
00691 {
00692 for(VariableSet::const_iterator it= varSet.begin();
00693 it!=varSet.end();
00694 ++it)
00695 {
00696 varList.push_back(*it);
00697 }
00698
00699 }
00700
00701
00702 VariableSet GeneralConstraint::unionVariables( const VariableSet& vs1,
00703 const VariableSet& vs2 )
00704 {
00705 VariableSet tempSet(vs1);
00706 for(VariableSet::const_iterator it=vs2.begin();
00707 it!=vs2.end();
00708 ++it)
00709 {
00710 tempSet.insert(*it);
00711 }
00712
00713 return tempSet;
00714
00715 }
00716
00717
00718 VariableSet GeneralConstraint::differenceVariables( const VariableSet& vs1,
00719 const VariableSet& vs2 )
00720 {
00721 VariableSet tempSet;
00722 for(VariableSet::const_iterator it=vs1.begin();
00723 it!=vs1.end();
00724 ++it)
00725 {
00726 VariableSet::const_iterator it2 = vs2.find(*it);
00727 if(it2==vs2.end()) tempSet.insert(*it);
00728 }
00729 for(VariableSet::const_iterator it=vs2.begin();
00730 it!=vs2.end();
00731 ++it)
00732 {
00733 VariableSet::const_iterator it2 = vs1.find(*it);
00734 if(it2==vs1.end()) tempSet.insert(*it);
00735 }
00736
00737 return tempSet;
00738
00739 }
00740
00741
00742 VariableSet GeneralConstraint::intersectionVariables(const VariableSet& vs1,
00743 const VariableSet& vs2 )
00744 {
00745 VariableSet tempSet;
00746 for(VariableSet::const_iterator it=vs1.begin();
00747 it!=vs1.end();
00748 ++it)
00749 {
00750 VariableSet::const_iterator it2 = vs2.find(*it);
00751 if(it2!=vs2.end()) tempSet.insert(*it);
00752 }
00753
00754 return tempSet;
00755
00756 }
00757
00758
00759
00760 bool GeneralConstraint::isRefSat(const SatID& sat)
00761 {
00762 bool isRef(false);
00763
00764 for(SatSourceMap::iterator it = refsatSourceMap.begin();
00765 it!=refsatSourceMap.end();
00766 ++it)
00767 {
00768 if(it->first==sat)
00769 {
00770 isRef = true;
00771 break;
00772 }
00773 }
00774
00775 for(SourceSatMap::iterator it = sourceRefsatMap.begin();
00776 it!=sourceRefsatMap.end();
00777 ++it)
00778 {
00779 if(it->second==sat)
00780 {
00781 isRef = true;
00782 break;
00783 }
00784 }
00785
00786 return isRef;
00787
00788 }
00789
00790
00791
00792 }