AstRawDark
Manage your astrophoto dark frames
 All Classes Functions Variables Enumerations Enumerator Pages
computeBestMatchCommand.cpp
1 /*
2  * AstRawDark
3  *
4  * Copyright (C) 2015 - Frédéric CORNU
5  *
6  * This program is free software: you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation, either version 3 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program. If not, see <http://www.gnu.org/licenses/>.
18  */
19 
20 #include "computeBestMatchCommand.h"
21 
22 #include "data/dataStore.h"
23 #include "processing/darkMatcher.h"
24 #include "processing/exceptions/noDarkForShootSettingsExcpetion.h"
25 #include "processing/exceptions/noDarkForTempException.h"
26 #include "commands/signalDispatcher.h"
27 
28 #ifndef QT_NO_DEBUG
29 #include <QDebug>
30 #endif
31 
32 ComputeBestMatchCommand::ComputeBestMatchCommand() {
33 
34  _description = tr("Compute best match");
35 
36  connect(this,
40 
41  connect(this,
45 }
46 
47 ComputeBestMatchCommand::~ComputeBestMatchCommand() {
48 
49 }
50 
52 
53  #ifndef QT_NO_DEBUG
54  qDebug() << "Finding best match ...";
55  #endif
56 
57  _message = tr("Finding best possible T° match...");
58  emit statusChanged(this);
59 
60  /*
61  * We will do a dichotomic search of the biggest dark set
62  * having a T° distribution that matches our lights
63  */
64 
65  int low = 0;
66  int high = DataStore::getInstance()->getScannedDarks().size();
67  int lowestBad = high;
68  QMap<int, char> lackingTemps;
69 
70  bool found = false;
71 
72  DarkMatcher matcher;
73 
74  do {
75 
76  try {
77 
78  #ifndef QT_NO_DEBUG
79  qDebug() << "BEFORE : LOW = " << low << " | HIGH = " << high;
80  #endif
81 
82  matcher.match(DataStore::getInstance()->getScannedLights(),
83  DataStore::getInstance()->getScannedDarks(),
84  high);
85 
86  low = high;
87  high += (lowestBad - high) / 2;
88 
89  #ifndef QT_NO_DEBUG
90  qDebug() << "AFTER : LOW = " << low << " | HIGH = " << high;
91  #endif
92 
93  found = true;
94 
95  } catch (NoDarkForTempException const& e) {
96 
97  lowestBad = high;
98  high -= (high - low ) / 2;
99 
100  /*
101  * if the matching failed because there was *NO* dark for the
102  * currently tested T° values, we store that T° value for
103  * detailed error report
104  */
105  if ( 0 == e.getAvailable() ) {
106 
107  lackingTemps.insert(e.getTemp(), 'X');
108  }
109 
110  #ifndef QT_NO_DEBUG
111  qDebug() << "AFTER : LOW = " << low << " | HIGH = " << high;
112  #endif
113 
114  } catch (NoDarkForShootSettingsExcpetion const& e) {
115 
116  _error = true;
117  _message = tr("Matching failed");
118  QString msg = tr("No darks match your shooting settings.");
119  _reportMessages << msg;
120  _commandReport.addSection(msg,QStringList());
121 
122  return;
123  }
124 
125  } while ( high -low > 1 );
126 
127 
128  if ( found ) {
129 
130  _message = tr("Best match found");
131  emit statusChanged(this);
132  _reportMessages << tr("OK.");
133  _commandReport.addSection(tr("Completed successfully"),QStringList());
134  emit matchFound(matcher.getMatchedDarks());
135  emit bestMatchCount(matcher.getMatchedDarks().size());
136 
137 
138  #ifndef QT_NO_DEBUG
139  qDebug() << "Best match found";
140  #endif
141 
142  } else {
143 
144  _error = true;
145  _message = tr("Matching failed");
146  QString msg = tr("Missing darks for some T° values");
147  QStringList values;
148 
149  foreach ( int temp, lackingTemps.keys() ) {
150 
151  values << QString::number(temp).append("°C");
152  }
153 
154  _reportMessages << msg;
155  _commandReport.addSection(msg, values);
156  }
157 }
158