1 // -*- c-basic-offset: 4 -*-
2 
3 /** @file PanoToolsOptimizerWrapper.cpp
4  *
5  *  @brief wraps around PTOptimizer
6  *
7  *  !!derives from PT/PTOptimise.h
8  *
9  *  @author Pablo d'Angelo <pablo.dangelo@web.de>
10  *
11  *  $Id$
12  *
13  *  This program is free software; you can redistribute it and/or
14  *  modify it under the terms of the GNU General Public
15  *  License as published by the Free Software Foundation; either
16  *  version 2 of the License, or (at your option) any later version.
17  *
18  *  This software is distributed in the hope that it will be useful,
19  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
20  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
21  *  General Public License for more details.
22  *
23  *  You should have received a copy of the GNU General Public
24  *  License along with this software. If not, see
25  *  <http://www.gnu.org/licenses/>.
26  *
27  */
28 
29 #include <hugin_config.h>
30 
31 #include <sstream>
32 #include <hugin_utils/utils.h>
33 
34 // libpano includes ------------------------------------------------------------
35 
36 #include <stdlib.h>
37 
38 #ifdef _WIN32
39 // include windows.h with sensible defines, otherwise
40 // panotools might include with its stupid, commonly
41 // named macros all over the place.
42 #define _STLP_VERBOSE_AUTO_LINK
43 //#define _USE_MATH_DEFINES
44 #define NOMINMAX
45 #define VC_EXTRALEAN
46 #include <windows.h>
47 #undef DIFFERENCE
48 #endif
49 
50 #include "PanoToolsInterface.h"
51 #include "PanoToolsOptimizerWrapper.h"
52 
53 //------------------------------------------------------------------------------
54 
55 //#define DEBUG_WRITE_OPTIM_OUTPUT
56 //#define DEBUG_WRITE_OPTIM_OUTPUT_FILE "hugin_debug_optim_results.txt"
57 
58 namespace HuginBase { namespace PTools {
59 
optimize(PanoramaData & pano,const char * userScript)60 unsigned int optimize(PanoramaData& pano,
61                       const char * userScript)
62 {
63     char * script = 0;
64     unsigned int retval = 0;
65 
66     if (userScript == 0) {
67         std::ostringstream scriptbuf;
68         UIntSet allImg;
69         fill_set(allImg,0, unsigned(pano.getNrOfImages()-1));
70         pano.printPanoramaScript(scriptbuf, pano.getOptimizeVector(), pano.getOptions(), allImg, true);
71         script = strdup(scriptbuf.str().c_str());
72     } else {
73         script = const_cast<char *>(userScript);
74     }
75 
76     OptInfo		opt;
77 	AlignInfo	ainf;
78 
79     if (ParseScript( script, &ainf ) == 0)
80 	{
81 		if( CheckParams( &ainf ) == 0 )
82 		{
83 			ainf.fcn	= fcnPano;
84 
85 			SetGlobalPtr( &ainf );
86 
87 			opt.numVars 		= ainf.numParam;
88 			opt.numData 		= ainf.numPts;
89 			opt.SetVarsToX		= SetLMParams;
90 			opt.SetXToVars		= SetAlignParams;
91 			opt.fcn			= ainf.fcn;
92 			*opt.message		= 0;
93 
94 			RunLMOptimizer( &opt );
95 			ainf.data		= opt.message;
96             // get results from align info.
97 #ifdef DEBUG_WRITE_OPTIM_OUTPUT
98             fullPath path;
99             StringtoFullPath(&path, DEBUG_WRITE_OPTIM_OUTPUT_FILE );
100 
101 		    ainf.data		= opt.message;
102             WriteResults( script, &path, &ainf, distSquared, 0);
103 #endif
104             pano.updateVariables( GetAlignInfoVariables(ainf) );
105             pano.updateCtrlPointErrors( GetAlignInfoCtrlPoints(ainf) );
106 		} else {
107             std::cerr << "Bad params" << std::endl;
108             retval = 2;
109         }
110 		DisposeAlignInfo( &ainf );
111     } else {
112         std::cerr << "Bad params" << std::endl;
113         retval = 1;
114     }
115     if (! userScript) {
116         free(script);
117     }
118     return retval;
119 }
120 
121 }} //namespace
122