CalculateFOV.cpp

Go to the documentation of this file.
00001 // -*- c-basic-offset: 4 -*-
00026 #include "CalculateFOV.h"
00027 
00028 #include <algorithm>
00029 #include <vigra/copyimage.hxx>
00030 #include <nona/RemappedPanoImage.h>
00031 
00032 namespace HuginBase {
00033 
00034 hugin_utils::FDiff2D CalculateFOV::calcFOV(const PanoramaData& panorama)
00035 {
00036     if (panorama.getNrOfImages() == 0) {
00037         // no change
00038         return hugin_utils::FDiff2D(panorama.getOptions().getHFOV(), panorama.getOptions().getVFOV());
00039     }
00040 
00041     vigra::Size2D panoSize(360*2,180*2);
00042 
00043     // remap into minature pano.
00044     PanoramaOptions opts;
00045     opts.setHFOV(360);
00046     opts.setProjection(PanoramaOptions::EQUIRECTANGULAR);
00047     opts.setWidth(panoSize.x);
00048     opts.setHeight(panoSize.y);
00049 
00050     // remap image
00051     // DGSW - make sure the type is correct
00052     vigra::BImage panoAlpha(panoSize.x, panoSize.y,static_cast< unsigned char >(0));
00053     //    vigra::BImage panoAlpha(panoSize.x, panoSize.y,0);
00054     Nona::RemappedPanoImage<vigra::BImage, vigra::BImage> remapped;
00055     UIntSet activeImgs = panorama.getActiveImages();
00056     for (UIntSet::iterator it = activeImgs.begin(); it != activeImgs.end(); ++it) {
00057         //    for (unsigned int imgNr=0; imgNr < getNrOfImages(); imgNr++) {
00058         // DGSW FIXME - Unreferenced
00059         //              const PanoImage & img = getImage(*it);
00060         remapped.setPanoImage(panorama.getSrcImage(*it), opts, vigra::Rect2D(0,0,panoSize.x,panoSize.y));
00061         //remapped.setPanoImage(*this, *it, vigra::Size2D(img.getWidth(), img.getHeight()), opts);
00062         // calculate alpha channel
00063         remapped.calcAlpha();
00064         // copy into global alpha channel.
00065         vigra::copyImageIf(vigra_ext::applyRect(remapped.boundingBox(),
00066                                                 vigra_ext::srcMaskRange(remapped)),
00067                             vigra_ext::applyRect(remapped.boundingBox(),
00068                                                 vigra_ext::srcMask(remapped)),
00069                             vigra_ext::applyRect(remapped.boundingBox(),
00070                                                 destImage(panoAlpha)));
00071         }
00072 
00073     // get field of view
00074     hugin_utils::FDiff2D ul, lr;
00075     bool found = false;
00076     ul.x = DBL_MAX;
00077     ul.y = DBL_MAX;
00078     lr.x = -DBL_MAX;
00079     lr.y = -DBL_MAX;
00080     for (int v=0; v< panoSize.y; v++) {
00081         for (int h=0; h < panoSize.x; h++) {
00082             if (panoAlpha(h,v)) {
00083                 // pixel is valid
00084                 if ( ul.x > h ) {
00085                     found=true;
00086                     ul.x = h;
00087                 }
00088                 if ( ul.y > v ) {
00089                     found=true;
00090                     ul.y = v;
00091                 }
00092                 if ( lr.x < h) {
00093                     found=true;
00094                     lr.x = h;
00095                 }
00096                 if ( lr.y < v) {
00097                     found=true;
00098                     lr.y = v;
00099                 }
00100             }
00101         }
00102     }
00103     if (!found) {
00104         // if nothing found, return current fov
00105         return hugin_utils::FDiff2D(panorama.getOptions().getHFOV(), panorama.getOptions().getVFOV());
00106     }
00107     ul=ul/2.0;
00108     lr=lr/2.0;
00109     ul.x = ul.x - 180;
00110     ul.y = ul.y - 90;
00111     lr.x = lr.x - 180;
00112     lr.y = lr.y - 90;
00113     hugin_utils::FDiff2D fov(2 * std::max(fabs(ul.x), fabs(lr.x)), 2 * std::max(fabs(ul.y), fabs(lr.y)));
00114     // the calculation above is done with a resolution of 1 deg
00115     // for small fov the calculated fov can be a little bit too small
00116     // so in this case add a small offset
00117     if(fov.x<40)
00118     {
00119         fov.x+=1;
00120     };
00121     if (fov.y < 40)
00122     {
00123         fov.y += 1;
00124     };
00125     return fov;
00126 }
00127 
00128 }

Generated on 10 Dec 2016 for Hugintrunk by  doxygen 1.4.7