double QFoldTest(const cv::Mat &features, const cv::Mat &targets,
             size_t folds) {
  double error = 0;
  size_t foldSize = features.rows / folds;
  for(size_t fold = 0; fold < folds; ++fold) {
    cv::Mat learnFeatures(features.rows - foldSize,
                          features.cols, CV_32FC1);
    cv::Mat learnTargets(targets.rows - foldSize, targets.cols, CV_32SC1);
    if(fold > 0) {
      CopyROI(features, cv::Range(0, fold * foldSize),
              cv::Range(0, features.cols),
              &learnFeatures, cv::Range(0, fold * foldSize),
              cv::Range(0, learnFeatures.cols));
      CopyROI(targets, cv::Range(0, fold * foldSize),
              cv::Range(0, targets.cols),
              &learnTargets, cv::Range(0, fold * foldSize),
              cv::Range(0, learnTargets.cols));

    }
    if(fold < folds) {
      CopyROI(features, cv::Range((fold + 1) * foldSize, features.rows), 
              cv::Range(0, features.cols),
              &learnFeatures, cv::Range(fold * foldSize, learnFeatures.rows), 
              cv::Range(0, learnFeatures.cols));
      CopyROI(targets, cv::Range((fold + 1) * foldSize, targets.rows),
              cv::Range(0, targets.cols),
              &learnTargets, cv::Range(fold * foldSize, learnTargets.rows),
              cv::Range(0, learnTargets.cols));
    }
    
    cv::Mat testFeatures(foldSize, features.cols, CV_32FC1);
    testFeatures = features(cv::Range(fold * foldSize, (fold + 1) * foldSize),
                            cv::Range::all());
    cv::Mat testTargets(foldSize, targets.cols, CV_32SC1);
    testTargets = targets(cv::Range(fold * foldSize, (fold + 1) * foldSize),
                          cv::Range::all());
    CvSVM svm;
    // Learn(learnFeatures, learnTargets, &svm);
    Learn(learnFeatures, learnTargets, &svm);
    error += Test(svm, testFeatures, testTargets);
  }
  return error / folds;
}
extern "C" int EXPORT_API CreateRenderScript(const char *projectFolder, const char *clipName)
{
  RD_ClipDataKey key(projectFolder, clipName);
  RD_ClipDataPtr_t pClipData = RD_ClipDataManager::instance()->object(key);
  if ( pClipData.get() == 0 )
  {
    LOGE( "Clip '%s' has not been loaded in memory!\n", clipName );
    return -1;
  }

  if ( pClipData->count() > 0 )
  {
    RD_RenderScript::Feature requestedFeatures = RD_RenderScript::ePlainFeature;
    if ( pClipData->fxEnabled() )
      requestedFeatures = RD_RenderScript::Feature(requestedFeatures | RD_RenderScript::eCutterFeature | RD_RenderScript::eDeformationFeature);

    RD_RenderScriptPtr_t pRenderScript;

    //  Plain Script
    if (  testFeatures(requestedFeatures, RD_RenderScriptPlain::supportedFeatures()) )
    {
      pRenderScript = new RD_RenderScriptPlain;
    }
    //  Fx Script
    else // if (  testFeatures(requestedFeatures, RD_RenderScriptFx::supportedFeatures()) )
    {
      pRenderScript = new RD_RenderScriptFx;
    }

    int scriptId = RD_RenderId::uniqueId();
    RD_RenderScriptManager::instance()->addObject( scriptId, pRenderScript );
    return scriptId;
  }

  return -1;
}
extern "C" int EXPORT_API UpdateRenderScript( int scriptId, const char *projectFolder, const char *clipName, const char *sheetResolution, float frame, unsigned int color, int discretizationStep)
{
  RD_RenderScriptPtr_t pRenderScript = RD_RenderScriptManager::instance()->object( scriptId );

  RD_ClipDataKey key(projectFolder, clipName);
  RD_ClipDataPtr_t pClipData = RD_ClipDataManager::instance()->object(key);
  if ( pClipData.get() == 0 )
  {
    LOGE( "Clip '%s' has not been loaded in memory!\n", clipName );
    return -1;
  }

  if ( pClipData->count() > 0 )
  {
    bool newScript = false;

    RD_RenderScript::Feature requestedFeatures = RD_RenderScript::ePlainFeature;
    if ( pClipData->fxEnabled() )
      requestedFeatures = RD_RenderScript::Feature(requestedFeatures | RD_RenderScript::eCutterFeature | RD_RenderScript::eDeformationFeature);

    //  Todo.  Implement a proper factory class to instantiate render scripts depending on
    //  requested features.
    if ( pRenderScript.isValid() )
    {
      if ( !pRenderScript->supportsFeature(requestedFeatures) )
      {
        //  Forward meta information to new script...
        RD_RenderScriptMetaPtr_t pMeta = pRenderScript->meta();

        //  Create new render script to support requested features.
        RD_RenderScriptManager::instance()->eraseObject( scriptId );

        //  Plain Script
        if ( testFeatures(requestedFeatures, RD_RenderScriptPlain::supportedFeatures()) )
        {
          pRenderScript = new RD_RenderScriptPlain(pMeta);
          newScript = true;
        }
        //  Fx Script
        else // if ( ( testFeatures(requestedFeatures, RD_RenderScriptFx::supportedFeatures()) )
        {
          pRenderScript = new RD_RenderScriptFx(pMeta);
          newScript = true;
        }
      }
    }
    else
    {
      //  Plain Script
      if (  testFeatures(requestedFeatures, RD_RenderScriptPlain::supportedFeatures()) )
      {
        pRenderScript = new RD_RenderScriptPlain;
        newScript = true;
      }
      //  Fx Script
      else // if (  testFeatures(requestedFeatures, RD_RenderScriptFx::supportedFeatures()) )
      {
        pRenderScript = new RD_RenderScriptFx;
        newScript = true;
      }
    }

    pRenderScript->update( pClipData, projectFolder, sheetResolution, frame, color, discretizationStep );

    if ( newScript )
    {
      RD_RenderScriptManager::instance()->addObject( scriptId, pRenderScript );
    }

    return scriptId;
  }

  return -1;
}
Exemple #4
0
void GeometryRenderer::updateGeometry(Geometry *geo)
{
    g = geo;
    if (!g) {
        ibo.destroy();
        vbo.destroy();
#if QT_VAO
        vao.destroy();
#endif
        vbo_size = 0;
        ibo_size = 0;
        return;
    }
    static int support_map = -1;
    if (support_map < 0) {
        static const char* ext[] = { "GL_OES_mapbuffer", NULL};
        if (OpenGLHelper::isOpenGLES()) {
            support_map = QOpenGLContext::currentContext()->format().majorVersion() > 2 ||
                    OpenGLHelper::hasExtension(ext);
        } else {
            support_map = 1;
        }
    }
    if (testFeatures(kIBO) && !ibo.isCreated()) {
        if (g->indexCount() > 0) {
            qDebug("creating IBO...");
            if (!ibo.create())
                qDebug("IBO create error");
        }
    }
    if (ibo.isCreated()) {
        ibo.bind();
        const int bs = g->indexDataSize();
        if (bs == ibo_size) {
            void * p = NULL;
            if (support_map && testFeatures(kMapBuffer))
                p = ibo.map(QOpenGLBuffer::WriteOnly);
            if (p) {
                memcpy(p, g->constIndexData(), bs);
                ibo.unmap();
            } else {
                ibo.write(0, g->constIndexData(), bs);
            }
        } else {
            ibo.allocate(g->indexData(), bs); // TODO: allocate NULL and then map or BufferSubData?
            ibo_size = bs;
        }
        ibo.release();
    }
    if (testFeatures(kVBO) && !vbo.isCreated()) {
        qDebug("creating VBO...");
        if (!vbo.create())
            qWarning("VBO create error");
    }
    if (vbo.isCreated()) {
        vbo.bind();
        const int bs = g->vertexCount()*g->stride();
        /* Notes from https://www.opengl.org/sdk/docs/man/html/glBufferSubData.xhtml
           When replacing the entire data store, consider using glBufferSubData rather than completely recreating the data store with glBufferData. This avoids the cost of reallocating the data store.
         */
        if (bs == vbo_size) { // vbo.size() error 0x501 on rpi, and query gl value can be slow
            void* p = NULL;
            if (support_map && testFeatures(kMapBuffer))
                p = vbo.map(QOpenGLBuffer::WriteOnly);
            if (p) {
                memcpy(p, g->constVertexData(), bs);
                vbo.unmap();
            } else {
                vbo.write(0, g->constVertexData(), bs);
                vbo_size = bs;
            }
        } else {
            vbo.allocate(g->vertexData(), bs);
        }
        vbo.release();
    }
#if QT_VAO
    if (stride == g->stride() && attrib == g->attributes())
        return;
    stride = g->stride();
    attrib = g->attributes();

    if (testFeatures(kVAO) && !vao.isCreated()) {
        qDebug("creating VAO...");
        if (!vao.create())
            qDebug("VAO create error");
    }
    qDebug("vao updated");
    if (vao.isCreated()) // can not use vao binder because it will create a vao if necessary
        vao.bind();
// can set data before vao bind
    if (!vao.isCreated())
        return;
    qDebug("geometry attributes changed, rebind vao...");
    // call once is enough if no feature and no geometry attribute is changed
    if (vbo.isCreated()) {
        vbo.bind();
        for (int an = 0; an < g->attributes().size(); ++an) {
            // FIXME: assume bind order is 0,1,2...
            const Attribute& a = g->attributes().at(an);
            QGLF(glVertexAttribPointer(an, a.tupleSize(), a.type(), a.normalize(), g->stride(), reinterpret_cast<const void *>(qptrdiff(a.offset())))); //TODO: in setActiveShader
            QGLF(glEnableVertexAttribArray(an));
        }
        vbo.release(); // unbind after vao unbind? http://www.zwqxin.com/archives/opengl/vao-and-vbo-stuff.html
    } // TODO: bind pointers if vbo is disabled
    // bind ibo to vao thus no bind is required later
    if (ibo.isCreated())// if not bind here, glDrawElements(...,NULL) crashes and must use ibo data ptr, why?
        ibo.bind();
    vao.release();
    if (ibo.isCreated())
        ibo.release();
#endif
    qDebug("geometry updated");
}