Mask.h

Go to the documentation of this file.
00001 // -*- c-basic-offset: 4 -*-
00002 
00011 /*  This program is free software; you can redistribute it and/or
00012  *  modify it under the terms of the GNU General Public
00013  *  License as published by the Free Software Foundation; either
00014  *  version 2 of the License, or (at your option) any later version.
00015  *
00016  *  This software is distributed in the hope that it will be useful,
00017  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
00018  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00019  *  General Public License for more details.
00020  *
00021  *  You should have received a copy of the GNU General Public
00022  *  License along with this software. If not, see
00023  *  <http://www.gnu.org/licenses/>.
00024  *
00025  */
00026 
00027 #ifndef _PANODATA_MASK_H
00028 #define _PANODATA_MASK_H
00029 
00030 #include <hugin_shared.h>
00031 #include "hugin_utils/utils.h"
00032 #include "hugin_math/hugin_math.h"
00033 
00034 namespace HuginBase 
00035 {
00036 namespace PTools { class Transform; }
00037 
00039 typedef std::vector<hugin_utils::FDiff2D> VectorPolygon;
00040 
00044 const int maskOffset=100;
00045 
00052 class IMPEX MaskPolygon
00053 {
00054 public:
00056     enum MaskType 
00057     {
00058         Mask_negative=0,
00059         Mask_positive=1,
00060         Mask_Stack_negative=2,
00061         Mask_Stack_positive=3,
00062         Mask_negative_lens=4
00063     };
00065     MaskPolygon() : m_maskType(Mask_negative), m_imgNr(0), m_invert(false) {};
00067     bool isInside(const hugin_utils::FDiff2D p) const;
00069     int getWindingNumber(const hugin_utils::FDiff2D p) const;
00071     int getTotalWindingNumber() const;
00072 
00073     // access functions
00075     MaskType getMaskType() const { return m_maskType; };
00077     void setMaskType(const MaskType newType) { m_maskType=newType; };
00079     bool isPositive() const;
00081     VectorPolygon getMaskPolygon() const { return m_polygon; };
00083     void setMaskPolygon(const VectorPolygon& newMask);
00085     unsigned int getImgNr() const { return m_imgNr; };
00087     void setImgNr(const unsigned int newImgNr) { m_imgNr=newImgNr; };
00089     void setInverted(const bool inverted) { m_invert = inverted; };
00091     bool isInverted() const { return m_invert; };
00092 
00093     // polygon modifier
00095     void addPoint(const hugin_utils::FDiff2D p);
00097     void insertPoint(const unsigned int index, const hugin_utils::FDiff2D p);
00099     void removePoint(const unsigned int index);
00101     void movePointTo(const unsigned int index, const hugin_utils::FDiff2D p);
00103     void movePointBy(const unsigned int index, const hugin_utils::FDiff2D diff);
00105     void scale(const double factorx, const double factory);
00107     void scale(const double factor) { scale(factor,factor);} ;
00109     void transformPolygon(const PTools::Transform &trans);
00111     bool clipPolygon(const vigra::Rect2D rect);
00113     bool clipPolygon(const hugin_utils::FDiff2D center, const double radius);
00115     void rotate90(bool clockwise,unsigned int maskWidth,unsigned int maskHeight);
00117     void subSample(const double max_distance);
00118 
00120     unsigned int FindPointNearPos(const hugin_utils::FDiff2D p, const double tol);
00121 
00122     //operators
00124     MaskPolygon &operator=(const MaskPolygon& otherPoly);
00126     const bool operator==(const MaskPolygon& otherPoly) const;
00127 
00128     //input/output functions
00130     bool parsePolygonString(const std::string& polygonStr);
00134     void printPolygonLine(std::ostream & o, const unsigned int newImgNr) const;
00135 
00136 private:
00138     void calcBoundingBox();
00139     //variables for internal storage of Mask type, polygon and assigned image number
00140     MaskType m_maskType;
00141     VectorPolygon m_polygon;
00142     unsigned int m_imgNr;
00143     bool m_invert;
00144     vigra::Rect2D m_boundingBox;
00145 };
00146 
00147 typedef std::vector<MaskPolygon> MaskPolygonVector;
00148 
00150 IMPEX void LoadMaskFromStream(std::istream& stream, vigra::Size2D& imageSize, MaskPolygonVector &newMasks, size_t imgNr);
00152 IMPEX void SaveMaskToStream(std::ostream& stream, vigra::Size2D imageSize, MaskPolygon &maskToWrite, size_t imgNr);
00153 
00154 }; //namespace
00155 
00156 namespace vigra_ext 
00157 {
00158 
00159 template <class SrcImageIterator, class SrcAccessor>
00160 void applyMask(vigra::triple<SrcImageIterator, SrcImageIterator, SrcAccessor> img, HuginBase::MaskPolygonVector masks)
00161 {
00162     const vigra::Diff2D imgSize = img.second - img.first;
00163 
00164     if(masks.empty())
00165         return;
00166     // loop over the image and transform
00167 #pragma omp parallel for schedule(dynamic)
00168     for(int y=0; y < imgSize.y; ++y)
00169     {
00170         // create x iterators
00171         SrcImageIterator xd(img.first);
00172         xd.y += y;
00173         for(int x=0; x < imgSize.x; ++x, ++xd.x)
00174         {
00175             hugin_utils::FDiff2D newPoint(x,y);
00176             bool insideMasks=false;
00177             unsigned int i=0;
00178             while(!insideMasks && (i<masks.size()))
00179             {
00180                 insideMasks=masks[i].isInside(newPoint);
00181                 i++;
00182             };
00183             if(insideMasks)
00184                 *xd=0;
00185         }
00186     }
00187 }
00188 
00189 } //namespace
00190 #endif // _PANODATA_MASK_H

Generated on 28 Aug 2016 for Hugintrunk by  doxygen 1.4.7