tcElevationOptimization::loadSnapShot: simplify reliability expression
[tecorrec.git] / geo / tcIlluminantDiscontinuity.cpp
bloba2d9e8d3a7c1cd0f2bd12dff4e3a2a544122cba1
1 /***************************************************************************
2 * This file is part of Tecorrec. *
3 * Copyright 2008 James Hogan <james@albanarts.com> *
4 * *
5 * Tecorrec is free software: you can redistribute it and/or modify *
6 * it under the terms of the GNU General Public License as published by *
7 * the Free Software Foundation, either version 2 of the License, or *
8 * (at your option) any later version. *
9 * *
10 * Tecorrec is distributed in the hope that it will be useful, *
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13 * GNU General Public License for more details. *
14 * *
15 * You should have received a copy of the GNU General Public License *
16 * along with Tecorrec. If not, write to the Free Software Foundation, *
17 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. *
18 ***************************************************************************/
20 /**
21 * @file tcIlluminantDiscontinuity.cpp
22 * @brief Illuminant discontinuity in both dimentions.
25 #include "tcIlluminantDiscontinuity.h"
26 #include "tcChannel.h"
28 #include <QObject>
30 #include <cmath>
33 * Constructors + destructor
36 /// Primary constructor.
37 tcIlluminantDiscontinuity::tcIlluminantDiscontinuity(const QList<tcChannel*>& newChromaticities)
38 : tcIlluminantDirection(newChromaticities, 2, tr("Illuminant discontinuity measure"), tr(""))
39 , m_threshold(0.3f)
41 channels()[0]->setName(tr("idm.x"));
42 channels()[1]->setName(tr("idm.y"));
43 channels()[0]->setDescription(tr("Illuminant discontinuity measure in the x direction"));
44 channels()[1]->setDescription(tr("Illuminant discontinuity measure in the y direction"));
45 foreach (tcChannel* channel, chromaticities())
47 for (int i = 0; i < 2; ++i)
49 channel->addDerivitive(channels()[i]);
54 /// Destructor.
55 tcIlluminantDiscontinuity::~tcIlluminantDiscontinuity()
57 foreach (tcChannel* channel, chromaticities())
59 for (int i = 0; i < 2; ++i)
61 channel->removeDerivitive(channels()[i]);
67 * Interface for derived class to implement
70 void tcIlluminantDiscontinuity::loadPortions(const QList< Reference< tcPixelData<float> > >& chromaticities, int width, int height)
72 Reference< tcPixelData<float> > discontinuityData[2];
73 for (int i = 0; i < 2; ++i)
75 discontinuityData[i] = new tcPixelData<GLfloat>(width, height);
76 m_portions += discontinuityData[i];
79 // Go through the pixels
80 maths::VarVector<float> lastLogChromaticity(numChromaticities());
81 maths::VarVector<float> logChromaticity(numChromaticities());
82 maths::VarVector<float> temp(numChromaticities());
83 float lastDisc = 0.0f;
84 int lastSaturation = 0;
85 for (int i = 0; i < width; ++i)
87 for (int j = 0; j < height; ++j)
89 int index = j*width + i;
90 // Fill log chromaticity vector
91 int saturation = 0;
92 for (int channel = 0; channel < numChromaticities(); ++channel)
94 if (chromaticities[channel]->buffer()[index] != 0.0f)
96 ++saturation;
97 logChromaticity[channel] = logf(chromaticities[channel]->buffer()[index]);
99 else
101 logChromaticity[channel] = 0.0f;
105 temp = logChromaticity;
106 temp -= lastLogChromaticity;
107 temp *= (float)lastSaturation / numChromaticities();
109 float discontinuity = lastDisc;
110 float sqr = temp.sqr();
111 if (sqr > 0.0f)
113 float mag = sqrtf(sqr);
114 if (mag > 0.0f)
116 discontinuity = fabs(temp * illuminantDirection()) / mag;
119 lastDisc = discontinuity*0.5f;
120 lastSaturation = saturation;
122 lastLogChromaticity = logChromaticity;
124 discontinuity = (discontinuity > m_threshold ? discontinuity : 0.0f);
125 if (saturation < numChromaticities())
127 discontinuity = 0.0f;
129 //discontinuity *= (float)saturation / numChromaticities();
130 discontinuityData[0]->buffer()[index] = discontinuity;
133 lastDisc = 0.0f;
134 lastSaturation = 0;
135 for (int j = 0; j < height; ++j)
137 for (int i = 0; i < width; ++i)
139 int index = j*width + i;
140 // Fill log chromaticity vector
141 int saturation = 0;
142 for (int channel = 0; channel < numChromaticities(); ++channel)
144 if (chromaticities[channel]->buffer()[index] != 0.0f)
146 ++saturation;
147 logChromaticity[channel] = logf(chromaticities[channel]->buffer()[index]);
149 else
151 logChromaticity[channel] = 0.0f;
155 temp = logChromaticity;
156 temp -= lastLogChromaticity;
157 temp *= (float)lastSaturation / numChromaticities();
159 float discontinuity = lastDisc;
160 float sqr = temp.sqr();
161 if (sqr > 0.0f)
163 float mag = sqrtf(sqr);
164 if (mag > 0.0f)
166 discontinuity = fabs(temp * illuminantDirection()) / mag;
169 lastDisc = discontinuity*0.5f;
170 lastSaturation = saturation;
172 lastLogChromaticity = logChromaticity;
174 discontinuity = (discontinuity > m_threshold ? discontinuity : 0.0f);
175 if (saturation < numChromaticities())
177 discontinuity = 0.0f;
179 //discontinuity *= (float)saturation / numChromaticities();
180 discontinuityData[1]->buffer()[index] = discontinuity;