USGS

Isis 3.0 Application Source Code Reference

Home

cam2cam.cpp

Go to the documentation of this file.
00001 #include "Isis.h"
00002 #include "CameraFactory.h"
00003 #include "Camera.h"
00004 #include "ProcessRubberSheet.h"
00005 #include "iException.h"
00006 #include "cam2cam.h"
00007 
00008 using namespace std;
00009 using namespace Isis;
00010 
00011 // Global variables
00012 void BandChange (const int band);
00013 Camera *incam;
00014 
00015 void IsisMain() {
00016   // Open the match cube and get the camera model on it
00017   ProcessRubberSheet m;
00018   Cube *mcube = m.SetInputCube("MATCH");
00019   Cube *ocube = m.SetOutputCube ("TO");
00020 
00021   // Set up the default reference band to the middle of the cube
00022   // If we have even bands it will be close to the middle
00023   int referenceBand = ocube->Bands();
00024   referenceBand += (referenceBand % 2);
00025   referenceBand /= 2;
00026 
00027   // See if the user wants to override the reference band
00028   UserInterface &ui = Application::GetUserInterface();
00029   if (ui.WasEntered("REFBAND")) {
00030     referenceBand = ui.GetInteger("REFBAND");
00031   }
00032 
00033   // Using the Camera method out of the object opack will not work, because the
00034   // filename required by the Camera is not passed by the process class in this
00035   // case.  Use the CameraFactory to create the Camera instead to get around this
00036   // problem.
00037   Camera *outcam = CameraFactory::Create(*(mcube->Label()));
00038 
00039   // Set the reference band we want to match
00040   PvlGroup instgrp = mcube->GetGroup("Instrument");
00041   if (!outcam->IsBandIndependent()) {
00042     PvlKeyword rBand("ReferenceBand",referenceBand);
00043     rBand.AddComment("# All bands are aligned to reference band");
00044     instgrp += rBand;
00045     mcube->PutGroup(instgrp);
00046     delete outcam;
00047     outcam = NULL;
00048   }
00049 
00050   // Only recreate the output camera if it was band dependent
00051   if (outcam == NULL) outcam = CameraFactory::Create(*(mcube->Label()));
00052 
00053   // We might need the instrument group later, so get a copy before clearing the input
00054   //   cubes.
00055   m.ClearInputCubes();
00056 
00057   Cube *icube = m.SetInputCube ("FROM");
00058   incam = icube->Camera();
00059 
00060   // Set up the transform object which will simply map
00061   // output line/samps -> output lat/lons -> input line/samps
00062   Transform *transform = new cam2cam (icube->Samples(),
00063                                           icube->Lines(),
00064                                           incam,
00065                                           ocube->Samples(),
00066                                           ocube->Lines(),
00067                                           outcam);
00068 
00069 
00070   // Add the reference band to the output if necessary
00071   ocube->PutGroup(instgrp);
00072 
00073   // Set up the interpolator
00074   Interpolator *interp = NULL;
00075   if (ui.GetString("INTERP") == "NEARESTNEIGHBOR") {
00076     interp = new Interpolator(Interpolator::NearestNeighborType);
00077   }
00078   else if (ui.GetString("INTERP") == "BILINEAR") {
00079     interp = new Interpolator(Interpolator::BiLinearType);
00080   }
00081   else if (ui.GetString("INTERP") == "CUBICCONVOLUTION") {
00082     interp = new Interpolator(Interpolator::CubicConvolutionType);
00083   }
00084 
00085   // See if we need to deal with band dependent camera models
00086   if (!incam->IsBandIndependent()) {
00087     m.BandChange(BandChange);
00088   }
00089 
00090   // Warp the cube
00091   m.StartProcess(*transform, *interp);
00092   m.EndProcess();
00093 
00094   // Cleanup
00095   delete transform;
00096   delete interp;
00097 }
00098 
00099 // Transform object constructor
00100 cam2cam::cam2cam (const int inputSamples, const int inputLines,
00101                   Camera *incam, const int outputSamples,
00102                   const int outputLines, Camera *outcam) {
00103   p_inputSamples = inputSamples;
00104   p_inputLines = inputLines;
00105   p_incam = incam;
00106 
00107   p_outputSamples = outputSamples;
00108   p_outputLines = outputLines;
00109   p_outcam = outcam;
00110 }
00111 
00112 // Transform method mapping output line/samps to lat/lons to input line/samps
00113 bool cam2cam::Xform (double &inSample, double &inLine,
00114                          const double outSample, const double outLine) {
00115   // See if the output image coordinate converts to lat/lon
00116   if (!p_outcam->SetImage(outSample,outLine)) return false;
00117 
00118   // Get the universal lat/lon and see if it can be converted to input line/samp
00119   double lat = p_outcam->UniversalLatitude();
00120   double lon = p_outcam->UniversalLongitude();
00121   if (!p_incam->SetUniversalGround(lat,lon)) return false;
00122 
00123   // Make sure the point is inside the input image
00124   if (p_incam->Sample() < 0.5) return false;
00125   if (p_incam->Line() < 0.5) return false;
00126   if (p_incam->Sample() > p_inputSamples + 0.5) return false;
00127   if (p_incam->Line() > p_inputLines + 0.5) return false;
00128 
00129   // Everything is good
00130   inSample = p_incam->Sample();
00131   inLine = p_incam->Line();
00132   return true;
00133 }
00134 
00135 int cam2cam::OutputSamples () const {
00136   return p_outputSamples;
00137 }
00138 
00139 int cam2cam::OutputLines () const {
00140   return p_outputLines;
00141 }
00142 
00143 void BandChange (const int band) {
00144   incam->SetBand(band);
00145 }