Work at SourceForge, help us to make it a better place! We have an immediate need for a Support Technician in our San Francisco or Denver office.

Close

[d5c5e9]: src / hugin1 / hugin / AutoCtrlPointCreator.cpp Maximize Restore History

Download this file

AutoCtrlPointCreator.cpp    436 lines (382 with data), 15.7 kB

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
// -*- c-basic-offset: 4 -*-
/** @file AutoCtrlPointCreator.cpp
*
* @brief implementation of AutoCtrlPointCreator Class
*
* @author Pablo d'Angelo <pablo.dangelo@web.de>
*
* $Id$
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This software is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
* You should have received a copy of the GNU General Public
* License along with this software; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*
*/
#include <config.h>
#include "panoinc_WX.h"
#include "panoinc.h"
#include <fstream>
#include "PT/Panorama.h"
#include "hugin/huginApp.h"
#include "hugin/config_defaults.h"
#include "hugin/AutoCtrlPointCreator.h"
#include "hugin/CommandHistory.h"
#include "base_wx/MyExternalCmdExecDialog.h"
#include "base_wx/platform.h"
#include "common/wxPlatform.h"
#include <wx/utils.h>
using namespace std;
using namespace PT;
using namespace utils;
CPVector AutoCtrlPointCreator::readUpdatedControlPoints(const std::string & file,
PT::Panorama & pano)
{
ifstream stream(file.c_str());
if (! stream.is_open()) {
DEBUG_ERROR("Could not open autopano output: " << file);
return CPVector();
}
Panorama tmpp;
PanoramaMemento newPano;
newPano.loadPTScript(stream, "");
tmpp.setMemento(newPano);
// create mapping between the panorama images.
map<unsigned int, unsigned int> imgMapping;
for (unsigned int ni = 0; ni < tmpp.getNrOfImages(); ni++) {
std::string nname = stripPath(tmpp.getImage(ni).getFilename());
for (unsigned int oi=0; oi < pano.getNrOfImages(); oi++) {
std::string oname = stripPath(pano.getImage(oi).getFilename());
if (nname == oname) {
// insert image
imgMapping[ni] = oi;
break;
}
}
if (! set_contains(imgMapping, ni)) {
DEBUG_ERROR("Could not find image " << ni << ", name: " << tmpp.getImage(ni).getFilename() << " in autopano output");
return CPVector();
}
}
// get control points
CPVector ctrlPoints = tmpp.getCtrlPoints();
// make sure they are in correct order
for (CPVector::iterator it= ctrlPoints.begin(); it != ctrlPoints.end(); ++it) {
(*it).image1Nr = imgMapping[(*it).image1Nr];
(*it).image2Nr = imgMapping[(*it).image2Nr];
}
return ctrlPoints;
}
CPVector AutoCtrlPointCreator::automatch(Panorama & pano,
const UIntSet & imgs,
int nFeatures)
{
CPVector cps;
int t = wxConfigBase::Get()->Read(wxT("/AutoPano/Type"),HUGIN_AP_TYPE);
if (t < 0) {
wxString tmp[2];
tmp[0] = _("Autopano (version 1.03 or greater), from http://autopano.kolor.com");
tmp[1] = _("Autopano-Sift, from http://user.cs.tu-berlin.de/~nowozin/autopano-sift/");
// determine autopano type
wxSingleChoiceDialog d(NULL, _("Choose which autopano program should be used\n"), _("Select autopano type"),
2, tmp, NULL);
if (d.ShowModal() == wxID_OK) {
t = d.GetSelection();
} else {
return cps;
}
}
#ifdef __WXMAC__
if(t==0)
{
if(wxMessageBox(_("Autopano from http://autopano.kolor.com is not available for OSX"),
_("Would you like to use Autopano-Sift instead?"),
wxOK|wxCANCEL|wxICON_EXCLAMATION)
== wxOK) t=1;
else return cps;
}
#endif
switch (t) {
case 0:
{
// autopano@kolor
AutoPanoKolor matcher;
cps = matcher.automatch(pano, imgs, nFeatures);
break;
}
case 1:
{
// autopano-sift
AutoPanoSift matcher;
cps = matcher.automatch(pano, imgs, nFeatures);
break;
}
default:
DEBUG_ERROR("Invalid autopano type");
}
wxConfigBase::Get()->Write(wxT("/AutoPano/Type"),t);
return cps;
}
CPVector AutoPanoSift::automatch(Panorama & pano, const UIntSet & imgs,
int nFeatures)
{
CPVector cps;
// create suitable command line..
#ifdef __WXMSW__
wxString autopanoExe = wxConfigBase::Get()->Read(wxT("/AutoPanoSift/AutopanoExe"), wxT(HUGIN_APSIFT_EXE));
if (!wxFile::Exists(autopanoExe)){
wxFileDialog dlg(0,_("Select autopano program / frontend script"),
wxT(""), wxT("autopano-win32.exe"),
_("Executables (*.exe)|*.exe"),
wxOPEN, wxDefaultPosition);
if (dlg.ShowModal() == wxID_OK) {
autopanoExe = dlg.GetPath();
wxConfigBase::Get()->Write(wxT("/AutopanoSift/AutopanoExe"),autopanoExe);
} else {
wxLogError(_("No autopano selected"));
return cps;
}
}
#elif (defined __WXMAC__) && defined HUGIN_OSX_BUNDLED
wxString autopanoExe = wxConfigBase::Get()->Read(wxT("/AutoPanoSift/AutopanoExe"), wxT(HUGIN_APSIFT_EXE));
//if the autopano-sift front end specified in preference does not exist:
if (autopanoExe == wxT(HUGIN_APSIFT_EXE))
{
autopanoExe = MacGetPathTOBundledResourceFile(CFSTR("autopano-complete-mac.sh"));
//if the script exists inside the bundle (which should), then check if there is autopano-sift files inside bundle:
if(autopanoExe != wxT(""))
{
wxString autopanoExeDir = MacGetPathTOBundledResourceFile(CFSTR("autopano-sift"));
//if they are not in bundle, then do not use the script included in the bundle
if( autopanoExeDir == wxT("")
|| !wxFileExists(autopanoExeDir+wxT("/autopano.exe"))
|| !wxFileExists(autopanoExeDir+wxT("/generatekeys-sd.exe"))
|| !wxFileExists(autopanoExeDir+wxT("/libsift.dll")) )
{
wxMessageBox(wxT(""), _("Autopano-SIFT is not installed."));
return cps;
}
}
} else if(!wxFileExists(autopanoExe)) {
wxFileDialog dlg(0,_("Select autopano frontend script"),
wxT(""), wxT(""),
_("Shell Scripts (*.sh)|*.sh"),
wxOPEN, wxDefaultPosition);
if (dlg.ShowModal() == wxID_OK) {
autopanoExe = dlg.GetPath();
wxConfigBase::Get()->Write(wxT("/AutopanoSift/AutopanoExe"), autopanoExe);
} else {
wxLogError(_("No autopano selected"));
return cps;
}
}
#else
// autopano should be in the path on linux
wxString autopanoExe = wxConfigBase::Get()->Read(wxT("/AutoPanoSift/AutopanoExe"),wxT(HUGIN_APSIFT_EXE));
#endif
wxString autopanoArgs = wxConfigBase::Get()->Read(wxT("/AutoPanoSift/Args"),
wxT(HUGIN_APSIFT_ARGS));
#ifdef __WXMSW__
// remember cwd.
wxString cwd = wxGetCwd();
wxString apDir = wxPathOnly(autopanoExe);
if (apDir.Length() > 0) {
wxSetWorkingDirectory(apDir);
}
#endif
// TODO: create a secure temporary filename here
wxString ptofile = wxFileName::CreateTempFileName(wxT("ap_res"));
autopanoArgs.Replace(wxT("%o"), ptofile);
wxString tmp;
tmp.Printf(wxT("%d"), nFeatures);
autopanoArgs.Replace(wxT("%p"), tmp);
// build a list of all image files, and a corrosponding connection map.
// local img nr -> global (panorama) img number
std::map<int,int> imgMapping;
long idx = autopanoArgs.Find(wxT("%namefile")) ;
DEBUG_DEBUG("find %namefile in '"<< autopanoArgs.mb_str() << "' returned: " << idx);
bool use_namefile = idx >=0;
idx = autopanoArgs.Find(wxT("%i"));
DEBUG_DEBUG("find %i in '"<< autopanoArgs.mb_str() << "' returned: " << idx);
bool use_params = idx >=0;
if (use_namefile && use_params) {
wxMessageBox(_("Please use either %namefile or %i in the autopano-sift command line."),
_("Error in Autopano command"), wxOK | wxICON_ERROR);
return cps;
}
if ((! use_namefile) && (! use_params)) {
wxMessageBox(_("Please use %namefile or %i to specify the input files for autopano-sift"),
_("Error in Autopano command"), wxOK | wxICON_ERROR);
return cps;
}
wxFile namefile;
wxString namefile_name;
if (use_namefile) {
// create temporary file with image names.
namefile_name = wxFileName::CreateTempFileName(wxT("ap_imgnames"), &namefile);
DEBUG_DEBUG("before replace %namefile: " << autopanoArgs.mb_str());
autopanoArgs.Replace(wxT("%namefile"), namefile_name);
DEBUG_DEBUG("after replace %namefile: " << autopanoArgs.mb_str());
int imgNr=0;
for(UIntSet::const_iterator it = imgs.begin(); it != imgs.end(); it++)
{
imgMapping[imgNr] = *it;
namefile.Write(wxString(pano.getImage(*it).getFilename().c_str(), *wxConvCurrent));
namefile.Write(wxT("\r\n"));
imgNr++;
}
// close namefile
if (namefile_name != wxString(wxT(""))) {
namefile.Close();
}
} else {
string imgFiles;
int imgNr=0;
for(UIntSet::const_iterator it = imgs.begin(); it != imgs.end(); it++)
{
imgMapping[imgNr] = *it;
imgFiles.append(" ").append(quoteFilename(pano.getImage(*it).getFilename()));
imgNr++;
}
autopanoArgs.Replace(wxT("%i"), wxString (imgFiles.c_str(), *wxConvCurrent));
}
#ifdef __WXMSW__
if (autopanoArgs.size() > 32000) {
wxMessageBox(_("autopano command line too long.\nThis is a windows limitation\nPlease select less images, or place the images in a folder with\na shorter pathname"),
_("Too many images selected"),
wxCANCEL | wxICON_ERROR );
return cps;
}
#endif
wxString cmd = autopanoExe + wxT(" ") + autopanoArgs;
DEBUG_DEBUG("Executing: " << autopanoExe.mb_str() << " " << autopanoArgs.mb_str());
int ret = 0;
// use MyExternalCmdExecDialog
ret = MyExecuteCommandOnDialog(autopanoExe, autopanoArgs, 0, _("finding control points"));
if (ret == -1) {
wxMessageBox( _("Could not execute command: " + cmd), _("wxExecute Error"), wxOK | wxICON_ERROR);
return cps;
} else if (ret > 0) {
wxMessageBox(_("command: ") + cmd +
_("\nfailed with error code: ") + wxString::Format(wxT("%d"),ret),
_("wxExecute Error"),
wxOK | wxICON_ERROR);
return cps;
}
if (! wxFileExists(ptofile.c_str())) {
wxMessageBox(wxString(_("Could not open ")) + ptofile + _(" for reading\nThis is an indicator that the autopano call failed,\nor wrong command line parameters have been used.\n\nAutopano command: ")
+ cmd, _("autopano failure"), wxOK | wxICON_ERROR );
return cps;
}
// read and update control points
cps = readUpdatedControlPoints((const char *)ptofile.mb_str(), pano);
#ifdef __WXMSW__
// set old cwd.
wxSetWorkingDirectory(cwd);
#endif
if (namefile_name != wxString(wxT(""))) {
namefile.Close();
wxRemoveFile(namefile_name);
}
if (!wxRemoveFile(ptofile)) {
DEBUG_DEBUG("could not remove temporary file: " << ptofile.c_str());
}
return cps;
}
CPVector AutoPanoKolor::automatch(Panorama & pano, const UIntSet & imgs,
int nFeatures)
{
CPVector cps;
#ifdef __WXMSW__
wxString autopanoExe = wxConfigBase::Get()->Read(wxT("/AutoPanoKolor/AutopanoExe"), wxT(HUGIN_APKOLOR_EXE));
if (!wxFile::Exists(autopanoExe)){
wxFileDialog dlg(0,_("Select autopano program"),
wxT(""), wxT("autopano.exe"),
_("Executables (*.exe)|*.exe"),
wxOPEN, wxDefaultPosition);
if (dlg.ShowModal() == wxID_OK) {
autopanoExe = dlg.GetPath();
wxConfigBase::Get()->Write(wxT("/AutoPanoKolor/AutopanoExe"),autopanoExe);
} else {
wxLogError(_("No autopano selected"));
return cps;
}
}
#else
// todo: selection of autopano on linux..
wxString autopanoExe = wxConfigBase::Get()->Read(wxT("/AutoPanoKolor/AutopanoExe"),wxT(HUGIN_APKOLOR_EXE));
#endif
// write default autopano.kolor.com flags
wxString autopanoArgs = wxConfigBase::Get()->Read(wxT("/AutoPanoKolor/Args"),
wxT(HUGIN_APKOLOR_ARGS));
// build a list of all image files, and a corrosponding connection map.
// local img nr -> global (panorama) img number
std::map<int,int> imgMapping;
string imgFiles;
int imgNr=0;
for(UIntSet::const_iterator it = imgs.begin(); it != imgs.end(); it++)
{
imgMapping[imgNr] = *it;
imgFiles.append(" ").append(quoteFilename(pano.getImage(*it).getFilename()));
imgNr++;
}
wxString ptofilepath = wxFileName::CreateTempFileName(wxT("ap_res"));
wxFileName ptofn(ptofilepath);
wxString ptofile = ptofn.GetFullName();
autopanoArgs.Replace(wxT("%o"), ptofile);
wxString tmp;
tmp.Printf(wxT("%d"), nFeatures);
autopanoArgs.Replace(wxT("%p"), tmp);
autopanoArgs.Replace(wxT("%i"), wxString (imgFiles.c_str(), *wxConvCurrent));
wxString tempdir = ptofn.GetPath();
autopanoArgs.Replace(wxT("%d"), ptofn.GetPath());
wxString cmd;
cmd.Printf(wxT("%s %s"), utils::wxQuoteFilename(autopanoExe).c_str(), autopanoArgs.c_str());
#ifdef __WXMSW__
if (cmd.size() > 32766) {
wxMessageBox(_("autopano command line too long.\nThis is a windows limitation\nPlease select less images, or place the images in a folder with\na shorter pathname"),
_("Too many images selected"),
wxCANCEL );
return cps;
}
#endif
DEBUG_DEBUG("Executing: " << cmd.c_str());
int ret = 0;
// use MyExternalCmdExecDialog
ret = MyExecuteCommandOnDialog(autopanoExe, autopanoArgs, 0, _("finding control points"));
if (ret == -1) {
wxMessageBox( _("Could not execute command: " + cmd), _("wxExecute Error"),
wxOK | wxICON_ERROR);
return cps;
} else if (ret > 0) {
wxMessageBox(_("command: ") + cmd +
_("\nfailed with error code: ") + wxString::Format(wxT("%d"),ret),
_("wxExecute Error"),
wxOK | wxICON_ERROR);
return cps;
}
ptofile = ptofn.GetFullPath();
ptofile.append(wxT("0.oto"));
if (! wxFileExists(ptofile.c_str()) ) {
wxMessageBox(wxString(_("Could not open ")) + ptofile + _(" for reading\nThis is an indicator that the autopano call failed,\nor wrong command line parameters have been used.\n\nAutopano command: ")
+ cmd + _("\n current directory:") +
wxGetCwd(),
_("autopano failure"), wxCANCEL );
return cps;
}
// read and update control points
cps = readUpdatedControlPoints((const char *)ptofile.mb_str(), pano);
if (!wxRemoveFile(ptofile)) {
DEBUG_DEBUG("could not remove temporary file: " << ptofile.c_str());
}
return cps;
}