hpi.cpp

Go to the documentation of this file.
00001 
00017 /*  This program is free software; you can redistribute it and/or
00018  *  modify it under the terms of the GNU General Public
00019  *  License as published by the Free Software Foundation; either
00020  *  version 2 of the License, or (at your option) any later version.
00021  *
00022  *  This software is distributed in the hope that it will be useful,
00023  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
00024  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00025  *  General Public License for more details.
00026  *
00027  *  You should have received a copy of the GNU General Public
00028  *  License along with this software; if not, write to the Free Software
00029  *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00030  *
00031  *  KFJ 2011-01-18
00032  *
00033  */
00034 
00035 // uncomment this if you want diagnostics:
00036 // #define HPI_VERBOSE
00037 
00038 // first pull in the Python interface classes
00039 #include "hugin_script_interface/hpi_classes.h"
00040 
00041 // pull in the declaration of callhpi() to ensure it's consistent
00042 // with the definition below
00043 #include "hugin_script_interface/hpi.h"
00044 
00045 // callhpi() is a variadic function, so we need stdarg.h
00046 // This is old-fashioned and may become deprecated, but for
00047 // now it's very convenient.
00048 #include <stdarg.h>
00049 
00050 namespace hpi
00051 {
00053 static python_interface hpi_instance;
00054 
00055 int callhpi ( const char* plugin_name ,
00056               int argc ,
00057               ... )
00058 {
00059     va_list arglist ;
00060     va_start ( arglist , argc ) ;
00061 
00062     // activate the python interface if it's not on already
00063     if ( ! hpi_instance.activate() )
00064     {
00065         // trouble
00066         fprintf ( stderr , "HPI ERROR: failed to activate Python interface\n" ) ;
00067         va_end ( arglist ) ;
00068         return -20 ;
00069     }
00070 
00071     // we want to build a python argument list with argc + 1 arguments,
00072     // + 1 because the first argument is the plugin name:
00073     python_arglist pyarglist ( argc + 1 ) ;
00074     pyarglist.add ( plugin_name ) ;
00075 
00076     for ( int argno = 0 ; argno < argc ; argno++ )
00077     {
00078         // extract the type name
00079         const char* type_name = va_arg ( arglist , const char* ) ;
00080         // extract the pointer to argument object
00081         void* pvalue = va_arg ( arglist , void* ) ;
00082         // make a python object from this information
00083         PyObject* pyarg = pyarglist.make_hsi_object ( type_name , pvalue ) ;
00084         // try add it to the arglist
00085         if ( ! pyarglist.add ( pyarg ) )
00086         {
00087             // oops... this went wrong
00088             va_end ( arglist ) ;
00089             return -21 ;
00090         }
00091     }
00092     va_end ( arglist ) ; // we're done with that
00093 
00094     // seems we're okay. get the argument tuple out
00095     PyObject* arg_tuple = pyarglist.yield() ;
00096     if ( ! arg_tuple )
00097     {
00098         // oops... this went wrong
00099         return -22 ;
00100     }
00101 
00102     // now let's call the thing. This is done by passing the argument
00103     // tuple to hpi_dispatch, a Python function defined in hpi.py
00104     // this function interprets the first argument as a plugin name
00105     // and calls that plugin with the remaining parameters.
00106     return hpi_instance.call_hpi ( "hpi_dispatch" , arg_tuple );
00107 }
00108 
00109 } // namespace hsi
00110 
00111 // When I didn't yet have an entry point from hugin to call hpi,
00112 // I used the next bit as my main() code. For now this is left
00113 // inside, it may be handy later on.
00114 
00115 #ifdef standalone
00116 
00117 #include <fstream>
00118 
00119 using namespace std;
00120 using namespace HuginBase;
00121 using namespace hugin_utils;
00122 using namespace AppBase;
00123 
00124 // we use this function to make a panorama from a pto file
00125 // so we have something to pass to the hsi module. Later on,
00126 // this function will disappear and the calls will generate
00127 // from hugin
00128 
00129 HuginBase::Panorama* pano_open ( const char* infile )
00130 {
00131     string input ( infile ) ;
00132 
00133     ifstream prjfile(input.c_str());
00134 
00135     if (!prjfile.good())
00136     {
00137         fprintf ( stderr ,
00138                   "could not open script %s\n" ,
00139                   input.c_str() ) ;
00140         return NULL;
00141     }
00142 
00143     HuginBase::Panorama* pano = new HuginBase::Panorama ;
00144 
00145     pano->setFilePrefix ( getPathPrefix ( input ) );
00146     AppBase::DocumentData::ReadWriteError err = pano->readData(prjfile);
00147 
00148     if (err != DocumentData::SUCCESSFUL)
00149     {
00150         fprintf ( stderr ,
00151                   "%s %s\n%s %d\n" ,
00152                   "error while parsing panos tool script:" ,
00153                   input.c_str() ,
00154                   "DocumentData::ReadWriteError code:" ,
00155                   err ) ;
00156         delete pano ;
00157         return NULL ;
00158     }
00159     fprintf ( stderr , "opened pano: %p\n" , pano ) ;
00160     return pano ;
00161 }
00162 
00163 // demo-main that opens the pto named in argv[1] and passes
00164 // the Panorama pointer to a function named by argv[2]
00165 // in the hsi module
00166 
00167 int main ( int argc , char* argv[] )
00168 {
00169     HuginBase::Panorama* pano = pano_open ( argv[1] ) ;
00170     if ( ! pano )
00171     {
00172         fprintf(stderr, "Failed to open pano\n");
00173         return 1;
00174     }
00175 
00176     int success = hsi::callhpi ( argv[2] , 1 , "HuginBase::Panorama*" , pano ) ;
00177 
00178     return success ;
00179 }
00180 #endif // standalone

Generated on 24 Oct 2014 for Hugintrunk by  doxygen 1.4.7