进度条与插件

算法转化为库

在vs中新建空静态库工程,将所需的头文件和其他文件加入到其中,然后,在头文件 include 部分后面,添加如下语句:

    typedef int (*ProgressFunc)(double dfComplete, 
                                const char *pszMessage, 
                                void *pProgressArg);

以上语句用来定义回调函数,三个参数含义如下:

dfComplete 进度值,取值范围是0.0~1.0,0.0表示开始,1.0表示完成。
pszMessage 可选项,用来显示的字符串信息。通常用NULL即可。
pProgressArg 应用程序回调函数参数,是一个自定义的类(或结构体)指针。

将需要进度条的函数中添加回调函数指针和回调函数所需的结构体或自定义类 pProgressArg,例如,将

    void NSWR(double **input1, double **input2, 
                  bool *mask1, bool *mask2,int bandnum, 
                  int Height, int Width, 
                  unsigned char **output, 
                  unsigned char **output2, 
                  double **bias, double **gain, 
                  double **weight, unsigned char *mark, 
                  double tnum, int minnum);

以上的函数声明添加两个参数,修改:

    void NSWR(double **input1, double **input2, 
                  bool *mask1, bool *mask2,int bandnum, 
                  int Height, int Width, 
                  unsigned char **output, 
                  unsigned char **output2, 
                  double **bias, double **gain, 
                  double **weight, unsigned char *mark, 
                  double tnum, int minnum,
                  ProgressFunc pfnProgress,void *pProgressArg);//添加两个参数
                  //添加回调函数和回调函数最后参数

最后在函数实现中,添加进度条函数调用即可,以上例子中,对NSWR函数的实现进行修改:

    void NSWR(double **input1, double **input2, 
                      bool *mask1, bool *mask2,int bandnum, 
                      int Height, int Width, 
                      unsigned char **output, 
                      unsigned char **output2, 
                      double **bias, double **gain, 
                      double **weight, unsigned char *mark, 
                      double tnum, int minnum,
                      ProgressFunc pfnProgress,void *pProgressArg)
    {
        ...
        for (int hi=0; hi<Height; hi++)   
        {
            for (int wj=0; wj<Width; wj++)
            {
                ...
                /////////////////////////////////////////////////
                ////////////////此段为添加段///////////////////////
                /////////////////////////////////////////////////
                //process是当前进度,范围在0.0-1.0之间
                double process = ...
                if(pfnProgress)
                {
                    if(!pfnProgress(process,"",pProgressArg))
                        return; 
                }
                /////////////////////////////////////////////////
                /////////////////进度条调用完成////////////////////
                /////////////////////////////////////////////////
            }
        }
    }

插件中调用算法

插件中,include路径中,添加算法头文件所在目录 lib中,添加对生成的静态库的链接 插件调用算法,在execute函数中,添加:

    ProgressFunc func = progressFun;
    ...
    imgfill.NSWR(Inputimg1, Inputimg2, GapMask1, GapMask2, BandNum, 
                Height, Width, OutputImg1, OutputImg2, Bias, Gain, 
                Weight, mark, m_tnum, m_minnum,func ,process);

运行插件

    int parallel = std::atoi(sPlugin[4].c_str());
    //进度条
    ref_ptr<orsIProcessMsgBar> progressBar;
    progressBar = ORS_CREATE_OBJECT( orsIProcessMsgBar, "ors.process.bar.cil" );
    //运行
    orsISimpleExe *simpleExe = ORS_PTR_CAST( orsISimpleExe, exeObj );
    if( simpleExe != NULL && parallel <=1 )//simpleexe
    {
        //初始化参数列表
        if( !simpleExe->initFromProperty( jobParas ) )
        {
            std::cout<<"Job Failed : Invalid Parameters"<<std::endl;
            return 0;
        }
        //执行  暂时为local,没设置并行
        clock_t stime,etime;
        stime = clock();//get start time
        simpleExe->execute( progressBar.get() );
        etime = clock(); //get end time
        std::cout<<"Job Done,Total Time:"<<(etime-stime)/1000.0<<"s"<<std::endl;
    }
    else if( simpleExe != NULL && parallel >1 )
    {
        //simpleexe并行
        orsIParallelService *pService = 
            (orsIParallelService *)getPlatform()->getService(ORS_PARALLEL_SERVICE_NAME);
 
        ref_ptr<orsIProcessMsgBar3> progressBar;
        progressBar = ORS_CREATE_OBJECT( orsIProcessMsgBar3, "ors.process.bar3.cil3" );
 
 
        // 取自动并行化对象
        orsString peObjID;
        jobParas->getAttr( ORS_PARALLELIZE_OBJECT, peObjID );
 
        //jobParas->addAttr( ORS_PARALLEL_NUM_PROCESSES, m_nProcesses, true );
 
        // 要被并行化的对象
        jobParas->addAttr( ORS_RASTER_EXE_OBJECT, exeObj->getClassID(), true );
        if( !pService->runJob_local( peObjID, jobParas, progressBar.get() ) ) {
            return 0;
        }
    }
    else
    {
        //parallelexe 并行
        orsIParallelService *pService = 
            (orsIParallelService *)getPlatform()->getService(ORS_PARALLEL_SERVICE_NAME);
        assert(pService != NULL);//
 
        ref_ptr<orsIProcessMsgBar3> progressBar;
        progressBar = ORS_CREATE_OBJECT( orsIProcessMsgBar3, "ors.process.bar3.cil3" );
 
        if( !pService->runJob_local( exeObj->getClassID(), jobParas, progressBar.get() ) ) {
            return 0;
        }
 
    }