USGS

Isis 3.0 Application Source Code Reference

Home

CnetRefByResolution.cpp

Go to the documentation of this file.
00001 #include "Application.h"
00002 #include "ControlNet.h"
00003 #include "ControlPoint.h"
00004 #include "ControlMeasure.h"
00005 #include "PvlGroup.h"
00006 #include "Camera.h"
00007 #include "MeasureValidationResults.h"
00008 #include "Portal.h"
00009 #include "SpecialPixel.h"
00010 #include "Pvl.h"
00011 #include "SerialNumberList.h"
00012 
00013 #include "CnetRefByResolution.h"
00014 
00015 using namespace std;
00016 
00017 namespace Isis {
00018   /**
00019    * CnetRefByResolution constructor with the def file
00020    *
00021    * @author Sharmila Prasad (5/25/2010)
00022    *
00023    * @param pPvlDef         - Pvl Definition File
00024    * @param psSerialNumfile - Serial Number file attached to the ControlNet
00025    * @param peType          - Resolution Type
00026    * @param pdResValue      - Resolution value
00027    * @param pdMinRes        - Min Resolution value
00028    * @param pdMaxRes        - Max Resolution value
00029    */
00030   CnetRefByResolution::CnetRefByResolution(Pvl *pPvlDef, std::string psSerialNumfile,
00031       ResolutionType peType, double pdResValue, double pdMinRes, double pdMaxRes)
00032     : ControlNetValidMeasure(pPvlDef) {
00033     mdResValue = pdResValue;
00034     mdMinRes   = pdMinRes;
00035     mdMaxRes   = pdMaxRes;
00036     meType     = peType;
00037     ReadSerialNumbers(psSerialNumfile);
00038     SetCameraRequiredFlag(true);
00039   }
00040 
00041   /**
00042    * FindCnetRef traverses all the control points and measures in the network and checks for
00043    * valid Measure which passes the Emission Incidence Angle, DN value tests and chooses the
00044    * measure with the best Resolution criteria as the reference. Creates a new control network
00045    * with these adjustments.
00046    *
00047    * @author Sharmila Prasad (5/25/2010)
00048    * @history 2010-10-04 Sharmila Prasad - Modified for Binary CNet (Edit Lock)
00049    *
00050    * @param pNewNet   - Modified output Control Net
00051    *
00052    */
00053   void CnetRefByResolution::FindCnetRef(ControlNet &pNewNet) {
00054     // Process each existing control point in the network
00055     int iPointsModified = 0;
00056     int iMeasuresModified = 0;
00057     int iRefChanged = 0;
00058 
00059     //Status Report
00060     mStatus.SetText("Choosing Reference by Resolution...");
00061     mStatus.SetMaximumSteps(pNewNet.GetNumPoints());
00062     mStatus.CheckStatus();
00063 
00064     //mPvlLog += GetStdOptions();
00065     for (int point = 0; point < pNewNet.GetNumPoints(); ++point) {
00066       ControlPoint *newPnt = pNewNet.GetPoint(point);
00067       bool bError = false;
00068       
00069       // Create a copy of original control point
00070       const ControlPoint origPnt(*newPnt);
00071 
00072       mdResVector.clear();
00073 
00074       // Logging
00075       PvlObject pvlPointObj("PointDetails");
00076       pvlPointObj += Isis::PvlKeyword("PointId", newPnt->GetId());
00077 
00078       // Edit Lock Option
00079       bool bPntEditLock = newPnt->IsEditLocked();
00080       if (!bPntEditLock) {
00081         newPnt->SetDateTime(Application::DateTime());
00082       }
00083       else {
00084         pvlPointObj += Isis::PvlKeyword("Reference", "No Change, PointEditLock"); 
00085       }
00086 
00087       int iNumMeasuresLocked = newPnt->GetNumLockedMeasures();
00088       bool bRefLocked = newPnt->GetRefMeasure()->IsEditLocked();
00089       int numMeasures = newPnt->GetNumMeasures();
00090       
00091       int iRefIndex = -1;
00092       if (newPnt->IsReferenceExplicit())
00093         iRefIndex = newPnt->IndexOfRefMeasure();
00094       iString istrTemp;
00095 
00096       std::vector <PvlGroup> pvlGrpVector;
00097       int iBestIndex = 0;
00098 
00099       // Only perform the interest operation on points of type "Free" and
00100       // Points having atleast 1 measure and Point is not Ignored
00101       // Check for EditLock in the Measures and also verfify that
00102       // only a Reference Measure can be Locked else error
00103       if (!newPnt->IsIgnored() && newPnt->GetType() == ControlPoint::Free && numMeasures > 0 &&
00104           (iNumMeasuresLocked == 0 || (iNumMeasuresLocked > 0 && bRefLocked))) {
00105         int iNumIgnore = 0;
00106         iString istrTemp;
00107         for (int measure = 0; measure < newPnt->GetNumMeasures(); ++measure) {
00108           ControlMeasure *newMsr = newPnt->GetMeasure(measure);
00109           bool bMeasureLocked = newMsr->IsEditLocked();
00110           double dSample      = newMsr->GetSample();
00111           double dLine        = newMsr->GetLine();
00112           std::string sn      = newMsr->GetCubeSerialNumber();
00113 
00114           if (!bPntEditLock && !bMeasureLocked) {
00115             newMsr->SetDateTime(Application::DateTime());
00116             newMsr->SetChooserName("Application cnetref(Resolution)");
00117           }
00118          
00119           // Log
00120           PvlGroup pvlMeasureGrp("MeasureDetails");
00121           pvlMeasureGrp += Isis::PvlKeyword("SerialNum", sn);
00122           pvlMeasureGrp += Isis::PvlKeyword("OriginalLocation", LocationString(dSample, dLine));
00123 
00124           if (bMeasureLocked)
00125             pvlMeasureGrp += Isis::PvlKeyword("EditLock", "True");
00126 
00127           if (!newMsr->IsIgnored()) {
00128             Cube *measureCube = mCubeMgr.OpenCube(mSerialNumbers.Filename(sn));
00129             
00130             MeasureValidationResults results =
00131               ValidStandardOptions(newMsr, measureCube, &pvlMeasureGrp);
00132             if (!results.isValid()) {
00133               if (bPntEditLock) {
00134                 pvlMeasureGrp += Isis::PvlKeyword("UnIgnored", "Failed Validation Test but not Ignored as Point EditLock is True");
00135               }
00136               else if (bMeasureLocked) {
00137                 pvlMeasureGrp += Isis::PvlKeyword("UnIgnored", "Failed Validation Test but not Ignored as Measure EditLock is True");
00138               }
00139               else {
00140                 pvlMeasureGrp += Isis::PvlKeyword("Ignored", "Failed Validation Test");
00141                 newMsr->SetIgnored(true);
00142                 iNumIgnore++;
00143               }
00144             } // valid measure
00145             else {
00146               if (!bPntEditLock && !bRefLocked) {
00147                 newMsr->SetType(ControlMeasure::Candidate);
00148                 newMsr->SetIgnored(false);
00149                 mdResVector.push_back(mdResolution);
00150               }
00151             }
00152           } // Ignore == false
00153           else {
00154             pvlMeasureGrp += Isis::PvlKeyword("Ignored", "Originally Ignored");
00155             iNumIgnore++;
00156           }
00157           if (newMsr != origPnt.GetMeasure(measure)) {
00158             iMeasuresModified++;
00159           }
00160 
00161           //newPnt.UpdateMeasure(newMsr); // Redesign fixed this
00162           pvlGrpVector.push_back(pvlMeasureGrp);
00163         }// end Measure
00164 
00165         if ((newPnt->GetNumMeasures() - iNumIgnore) < 2) {
00166           if (bPntEditLock) {
00167             pvlPointObj += Isis::PvlKeyword("UnIgnored", "Good Measures less than 2 but not Ignored as Point EditLock is True");
00168           }
00169           else {
00170             newPnt->SetIgnored(true);
00171             pvlPointObj += Isis::PvlKeyword("Ignored", "Good Measures less than 2");
00172           }
00173         }
00174         // Set the Reference if the Point is unlocked and Reference measure is unlocked
00175         if (!newPnt->IsIgnored() && !bPntEditLock && !bRefLocked) {
00176           iBestIndex = GetReferenceByResolution(newPnt);
00177           if (iBestIndex >= 0 && !newPnt->GetMeasure(iBestIndex)->IsIgnored()) {
00178             newPnt->SetRefMeasure(iBestIndex);
00179             //newPnt.UpdateMeasure(cm); // Redesign fixed this
00180             pvlGrpVector[iBestIndex] += Isis::PvlKeyword("Reference", "true");
00181           }
00182           else {
00183             if (iBestIndex < 0 && meType == Range) {
00184               pvlPointObj += Isis::PvlKeyword("NOTE", "No Valid Measures within the Resolution Range. Reference defaulted to the first Measure");
00185             }
00186             iBestIndex = 0;
00187             newPnt->SetRefMeasure(iBestIndex);
00188             //newPnt.UpdateMeasure(cm); // Redesign fixed this
00189 
00190             // Log info, if Point not locked, apriori source == Reference and a new reference
00191             if (iRefIndex != iBestIndex &&
00192                 newPnt->GetAprioriSurfacePointSource() == ControlPoint::SurfacePointSource::Reference) {
00193               pvlGrpVector[iBestIndex] += Isis::PvlKeyword("AprioriSource", "Reference is the source and has changed");
00194             }
00195           }
00196         }
00197 
00198         for (int i = 0; i < newPnt->GetNumMeasures(); i++) {
00199           pvlPointObj += pvlGrpVector[i];
00200         }
00201       } // end Free
00202       else {
00203         int iComment = 0;
00204         if (numMeasures == 0) {
00205           iString sComment = "Comment";
00206           sComment += iString(++iComment);
00207           pvlPointObj += Isis::PvlKeyword(sComment, "No Measures in the Point");
00208         }
00209 
00210         if (newPnt->IsIgnored()) {
00211           iString sComment = "Comment";
00212           sComment += iString(++iComment);
00213           pvlPointObj += Isis::PvlKeyword(sComment, "Point was originally Ignored");
00214         }
00215 
00216         if (newPnt->GetType() == ControlPoint::Fixed) {
00217           iString sComment = "Comment";
00218           sComment += iString(++iComment);
00219           pvlPointObj += Isis::PvlKeyword(sComment, "Fixed Point");
00220         }
00221         else if (newPnt->GetType() == ControlPoint::Constrained) {
00222           iString sComment = "Comment";
00223           sComment += iString(++iComment);
00224           pvlPointObj += Isis::PvlKeyword(sComment, "Constrained Point");
00225         }
00226 
00227         if (iNumMeasuresLocked > 0 && !bRefLocked) {
00228           pvlPointObj += Isis::PvlKeyword("Error", "Point has a Measure with EditLock set to true "
00229                                           "but the Reference is not Locked");
00230           bError = true;
00231         }
00232         else {
00233           for (int measure = 0; measure < newPnt->GetNumMeasures(); measure++) {
00234             ControlMeasure *cm = newPnt->GetMeasure(iBestIndex);
00235             cm->SetDateTime(Application::DateTime());
00236             cm->SetChooserName("Application cnetref(Resolution)");
00237             //newPnt.UpdateMeasure(cm); // Redesign fixed this
00238           }
00239         } 
00240       }
00241 
00242       if (*newPnt != origPnt) {
00243         iPointsModified++;
00244       }
00245       
00246       if (!bError && !newPnt->IsIgnored() && newPnt->IsReferenceExplicit() && iBestIndex != iRefIndex 
00247           && !bPntEditLock && !bRefLocked) {
00248         iRefChanged++;
00249         PvlGroup pvlRefChangeGrp("ReferenceChangeDetails");
00250         if (iRefIndex >= 0) {
00251           pvlRefChangeGrp += Isis::PvlKeyword("PrevSerialNumber",
00252               origPnt.GetMeasure(iRefIndex)->GetCubeSerialNumber());
00253           pvlRefChangeGrp += Isis::PvlKeyword("PrevResolution",   mdResVector[iRefIndex]);
00254   
00255           istrTemp = iString((int)origPnt.GetMeasure(iRefIndex)->GetSample());
00256           istrTemp += ",";
00257           istrTemp += iString((int)origPnt.GetMeasure(iRefIndex)->GetLine());
00258           pvlRefChangeGrp += Isis::PvlKeyword("PrevLocation",     istrTemp);
00259         }
00260         else {
00261           pvlRefChangeGrp += Isis::PvlKeyword("PrevReference", "Not Set");
00262         }
00263         
00264         pvlRefChangeGrp += Isis::PvlKeyword("NewSerialNumber",
00265             newPnt->GetMeasure(iBestIndex)->GetCubeSerialNumber());
00266         std::string sKeyName = "NewHighestResolution";
00267         if (meType == Low) {
00268           sKeyName = "NewLeastResolution";
00269         }
00270         else if (meType == Mean) {
00271           pvlRefChangeGrp += Isis::PvlKeyword("MeanResolution",  GetMeanResolution());
00272           sKeyName = "NewResolutionNeartoMean";
00273         }
00274         else if (meType == Nearest) {
00275           sKeyName = "NewResolutionNeartoValue";
00276         }
00277         else if (meType == Range) {
00278           sKeyName = "NewResolutionInRange";
00279         }
00280         pvlRefChangeGrp += Isis::PvlKeyword(sKeyName,  mdResVector[iBestIndex]);
00281 
00282         istrTemp = iString((int)newPnt->GetMeasure(iBestIndex)->GetSample());
00283         istrTemp += ",";
00284         istrTemp += iString((int)newPnt->GetMeasure(iBestIndex)->GetLine());
00285         pvlRefChangeGrp += Isis::PvlKeyword("NewLocation",      istrTemp);
00286 
00287         pvlPointObj += pvlRefChangeGrp;
00288       }
00289       else {
00290         pvlPointObj += Isis::PvlKeyword("Reference", "No Change");
00291       }
00292 
00293       //pNewNet.UpdatePoint(newPnt); // Redesign fixed this
00294       mPvlLog += pvlPointObj;
00295       mStatus.CheckStatus();
00296     }// end Point
00297 
00298     // CnetRef Change Statistics
00299     mStatisticsGrp += Isis::PvlKeyword("PointsModified",   iPointsModified);
00300     mStatisticsGrp += Isis::PvlKeyword("ReferenceChanged", iRefChanged);
00301     mStatisticsGrp += Isis::PvlKeyword("MeasuresModified", iMeasuresModified);
00302 
00303     mPvlLog += mStatisticsGrp;
00304   }
00305 
00306   /**
00307    * GetMeanResolution - Get Mean of all the resolutions of all the measures
00308    * in the Control Point
00309    *
00310    * @author Sharmila Prasad (6/1/2010)
00311    *
00312    * @return double
00313    */
00314   double CnetRefByResolution::GetMeanResolution(void) {
00315     double dTotal = 0;
00316     for (int i = 0; i < (int)mdResVector.size(); i++) {
00317       dTotal += mdResVector[i];
00318     }
00319 
00320     return (dTotal / mdResVector.size());
00321   }
00322 
00323   /**
00324    * GetReferenceByResolution - Get the Reference for each Control Point by user defined
00325    * Resolution criteria
00326    *
00327    * @author Sharmila Prasad (6/1/2010)
00328    *
00329    * @return int
00330    */
00331   int CnetRefByResolution::GetReferenceByResolution(ControlPoint *pNewPoint) {
00332     int iBestIndex = -1;
00333     double dBestResolution = -1;
00334     double dMean = 0;
00335     if (meType == Mean) {
00336       dMean = GetMeanResolution();
00337     }
00338 
00339     for (int i = 0; i < (int)mdResVector.size(); i++) {
00340       if (pNewPoint->GetMeasure(i)->IsIgnored()) {
00341         continue;
00342       }
00343       else {
00344         double dDiff = dBestResolution - mdResVector[i];
00345         if (meType == Low) {
00346           if (dBestResolution == -1 || dDiff < 0) {
00347             dBestResolution = mdResVector[i];
00348             iBestIndex = i;
00349           }
00350         }
00351         else if (meType == High) {
00352           double dDiff = dBestResolution - mdResVector[i];
00353           if (dBestResolution == -1 || dDiff > 0) {
00354             dBestResolution = mdResVector[i];
00355             iBestIndex = i;
00356           }
00357         }
00358         else if (meType == Mean) {
00359           double dDiff = fabs(dMean - mdResVector[i]);
00360           if (dBestResolution == -1 || dDiff <  dBestResolution) {
00361             dBestResolution = dDiff;
00362             iBestIndex = i;
00363           }
00364         }
00365         else if (meType == Nearest) {
00366           double dDiff = fabs(mdResValue - mdResVector[i]);
00367           if (dBestResolution == -1 || dDiff <  dBestResolution) {
00368             dBestResolution = dDiff;
00369             iBestIndex = i;
00370           }
00371         }
00372         else if (meType == Range) {
00373           if (mdResVector[i] >=  mdMinRes && mdResVector[i] <= mdMaxRes) {
00374             iBestIndex = i;
00375             return iBestIndex;
00376           }
00377         }
00378       }
00379     }
00380     return iBestIndex;
00381   }
00382 };
00383