~ubuntu-branches/ubuntu/precise/koffice/precise

« back to all changes in this revision

Viewing changes to krita/plugins/extensions/panorama/imagealignment/homographyimagematchmodel_p.h

  • Committer: Bazaar Package Importer
  • Author(s): Jonathan Riddell
  • Date: 2010-09-21 15:36:35 UTC
  • mfrom: (1.4.1 upstream) (60.2.11 maverick)
  • Revision ID: james.westby@ubuntu.com-20100921153635-6tejqkiro2u21ydi
Tags: 1:2.2.2-0ubuntu3
Add kubuntu_03_fix-crash-on-closing-sqlite-connection-2.2.2.diff and
kubuntu_04_support-large-memo-values-for-msaccess-2.2.2.diff as
recommended by upstream http://kexi-
project.org/wiki/wikiview/index.php@Kexi2.2_Patches.html#sqlite_stab
ility

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/*
2
 
 * Copyright (c) 2007 Cyrille Berger (cberger@cberger.net)
3
 
 *
4
 
 *  This program is free software; you can redistribute it and/or modify
5
 
 *  it under the terms of the GNU Lesser General Public License as published by
6
 
 *  the Free Software Foundation; version 2 of the License.
7
 
 *
8
 
 *  This program is distributed in the hope that it will be useful,
9
 
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
10
 
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11
 
 *  GNU General Public License for more details.
12
 
 *
13
 
 *  You should have received a copy of the GNU Lesser General Public License
14
 
 *  along with this program; if not, write to the Free Software
15
 
 *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */
16
 
 
17
 
#ifndef _HOMOGRAPHY_IMAGE_MATCH_MODEL_P_H_
18
 
#define _HOMOGRAPHY_IMAGE_MATCH_MODEL_P_H_
19
 
 
20
 
 
21
 
class HomographyImageMatchModel
22
 
{
23
 
public:
24
 
    struct Params {
25
 
        inline Params(int w, int h, double thres)
26
 
                : width(w), height(h), threshold(thres) {}
27
 
        int width, height;
28
 
        double threshold;
29
 
    };
30
 
public:
31
 
    inline HomographyImageMatchModel(std::vector<KisMatch> samples, Params* params) : m_isValid(false), m_fitComputed(false), m_matches(samples), m_width(params->width), m_height(params->height), m_threshold(params->threshold) {
32
 
    }
33
 
    /**
34
 
     * @return the minimal number of points needed for a fit
35
 
     */
36
 
    inline static uint nbFit() {
37
 
        return 6;
38
 
    }
39
 
    inline bool isValid() {
40
 
        if (not m_fitComputed) {
41
 
            computeFitting();
42
 
        }
43
 
        return m_isValid;
44
 
    }
45
 
    inline void addData(std::vector<KisMatch>::iterator begin, std::vector<KisMatch>::iterator end) {
46
 
        for (std::vector<KisMatch>::iterator it = begin; it != end; it++) {
47
 
            m_matches.push_back(*it);
48
 
        }
49
 
        m_fitComputed = false;
50
 
    }
51
 
    inline double fittingErrorSum() {
52
 
        if (not m_fitComputed) {
53
 
            computeFitting();
54
 
        }
55
 
        return m_fittingErrorSum;
56
 
    }
57
 
    inline double threshold() const {
58
 
        return m_threshold;
59
 
    }
60
 
    inline double fittingError(const KisMatch& m) {
61
 
        int indx[HomographySameDistortionFunction::SIZEINDEXES];
62
 
        for (int i = 0; i < HomographySameDistortionFunction::SIZEINDEXES; i++) {
63
 
            indx[i] = i;
64
 
        }
65
 
        double norm(4.0 / (m_width * m_width + m_height * m_height));
66
 
        HomographySameDistortionFunction hsdf(indx, m_width * 0.5, m_height * 0.5, norm, m.ref->x(), m.ref->y(), m.match->x(), m.match->y());
67
 
        double f1, f2;
68
 
        hsdf.f(parameters(), f1, f2);
69
 
        return (f1 + f2) * 0.5;
70
 
    }
71
 
    inline const std::vector<double>& parameters() const {
72
 
        return m_parameters;
73
 
    }
74
 
    inline const std::vector<KisMatch>& matches() const {
75
 
        return m_matches;
76
 
    }
77
 
private:
78
 
    void computeFitting() {
79
 
        KisControlPoints cps(2);
80
 
        cps.addMatches(m_matches, 0, 1);
81
 
        PanoptimFunction<HomographySameDistortionFunction, HomographySameDistortionFunction::SIZEINDEXES> f(cps, m_width * 0.5, m_height * 0.5, m_width, m_height);
82
 
        m_parameters.resize(HomographySameDistortionFunction::SIZEINDEXES);
83
 
        m_parameters[HomographySameDistortionFunction::INDX_a] = 0.0;
84
 
        m_parameters[HomographySameDistortionFunction::INDX_b] = 0.0;
85
 
        m_parameters[HomographySameDistortionFunction::INDX_c] = 0.0;
86
 
        m_parameters[HomographySameDistortionFunction::INDX_h11] = 1.0;
87
 
        m_parameters[HomographySameDistortionFunction::INDX_h21] = 0.0;
88
 
        m_parameters[HomographySameDistortionFunction::INDX_h31] = m_matches[0].ref->x() - m_matches[0].match->x();
89
 
        m_parameters[HomographySameDistortionFunction::INDX_h12] = 0.0;
90
 
        m_parameters[HomographySameDistortionFunction::INDX_h22] = 1.0;
91
 
        m_parameters[HomographySameDistortionFunction::INDX_h32] = m_matches[0].ref->y() - m_matches[0].match->y();
92
 
        m_parameters[HomographySameDistortionFunction::INDX_h13] = 0.0;
93
 
        m_parameters[HomographySameDistortionFunction::INDX_h23] = 0.0;
94
 
        m_fittingErrorSum = Optimization::Algorithms::levenbergMarquardt(&f, m_parameters, 100, 1e-12, 0.01, 10.0);
95
 
        m_isValid = true;
96
 
        m_fitComputed = true;
97
 
        for (uint i = 0; i < m_parameters.size(); i++) {
98
 
            dbgPlugins << "m_parameters[" << i << "]=" << m_parameters[i];
99
 
        }
100
 
    }
101
 
private:
102
 
    bool m_isValid;
103
 
    bool m_fitComputed;
104
 
    double m_fittingErrorSum;
105
 
    lMatches m_matches;
106
 
    std::vector<double> m_parameters;
107
 
    int m_width, m_height;
108
 
    double m_threshold;
109
 
};
110
 
 
111
 
#endif