Skip to content
Snippets Groups Projects
Select Git revision
  • 4838cac374a673407e3d997b98091dfff08fca3e
  • master default protected
  • opencv4
  • custom_realsense
  • deproject
  • camera
6 results

sandboxSetup.cpp

Blame
  • sandboxSetup.cpp 5.28 KiB
    #include "../../inc/sandboxSetup.h"
    
    
    SandboxSetup::SandboxSetup(){
        projection = new Projection();
        camera = new Camera();
        beamer = new Beamer();
    }
    
    SandboxSetup::~SandboxSetup(){
        delete beamer;
        delete camera;
        delete projection;
    }
    
    //
    //  PUBLIC
    //
    
    
    // return 1 when config can't be saved in file
    int SandboxSetup::saveConfigInto(char *path){
        if(SandboxConfig::saveAdjustingMatrixInto(path, projection->getAdjustingMatrix()))
            return 1;
        
        if(SandboxConfig::saveDistanceToSandboxTop(path, projection->getDistanceTopSandbox()))
            return 1;
    
        if(SandboxConfig::saveCroppingMaskInto(path, camera->getCroppingMask()))
            return 1;
    
        if(SandboxConfig::saveBeamerResolutionInto(path, cv::Size(beamer->getWidth(), beamer->getHeight())))
            return 1;
    
        if(SandboxConfig::saveBeamerPositionInto(path, beamer->getPosition()))
            return 1;
    
        if(SandboxConfig::saveFrameProcessProfil(path, *beamer->getProfil()))
            return 1;
    
        return 0;
    }
    
    
    int SandboxSetup::saveConfig(){
        return saveConfigInto(defaultConfigFilePath);
    }
    
    int SandboxSetup::loadCroppingMask(char *path){
        return SandboxConfig::loadCroppingMaskFrom(path, camera);
    }
    
    int SandboxSetup::loadCroppingMask(){
        return loadCroppingMask(defaultConfigFilePath);
    }
    
    int SandboxSetup::loadFrameProcessProfilFrom(char *path){
        return SandboxConfig::loadFrameProcessProfil(path, beamer->getProfil());
    }
    
    int SandboxSetup::loadFrameProcessProfil(){
        return loadFrameProcessProfilFrom(defaultConfigFilePath);
    }
    
    /*
        Get the centroid of a quadrilateral
        source : http://jwilson.coe.uga.edu/EMT668/EMT668.Folders.F97/Patterson/EMT%20669/centroid%20of%20quad/Centroid.html
    */
    cv::Point2i SandboxSetup::getCenterOfQuadrilateral(std::vector<cv::Point> rectPoints){
        
        std::vector<std::vector<cv::Point2i>> triangles = getTriangles(rectPoints);
        std::vector<cv::Point2i> centroids = getCentroids(triangles);
    
        /*
            Pa = P1 + mua (P2 - P1)
            Pb = P3 + mub (P4 - P3)
        */
        cv::Point3d pa;
        cv::Point3d pb;
        double mua;
        double mub;
        beamer->LineLineIntersect( cv::Point3d(centroids.at(0).x, centroids.at(0).y, 0),
                                   cv::Point3d(centroids.at(1).x, centroids.at(1).y, 0),
                                   cv::Point3d(centroids.at(2).x, centroids.at(2).y, 0),
                                   cv::Point3d(centroids.at(3).x, centroids.at(3).y, 0),
                                   &pa, &pb, &mua, &mub );
    
        // pa and pb should be the same
        cv::Point2i center;
        center.x = (pa.x + pb.x) / 2;
        center.y = (pa.y + pb.y) / 2;
    
        return center;
    }
    
    
    /*  Assuming points positions are :
     *      pts[0] : top left
     *      pts[1] : bottom left
     *      pts[2] : bottom right
     *      pts[3] : top right
     * 
     *  center : center of the rotation in the projected frame
     */
    void SandboxSetup::setupAdjustMatrix(std::vector<cv::Point> rectPoints, cv::Point center){
    
        // Set adjusting matrix for the projection
        int widthTop = rectPoints[3].x - rectPoints[0].x;
        double angle1 = atan((double)(rectPoints[3].y - rectPoints[0].y) / widthTop);
        cv::Mat matRotation = cv::getRotationMatrix2D(center, toDegrees(angle1), 1); // adjustingMatrix
        projection->setAdjustingMatrix(matRotation);
    }
    
    
    /*  Assuming points positions are :
     *      pts[0] : top left
     *      pts[1] : bottom left
     *      pts[2] : bottom right
     *      pts[3] : top right
     */
    void SandboxSetup::setupCroppingMask(std::vector<cv::Point2i> rectPoints){
    
        // Set cropping mask
        int widthTop = rectPoints[3].x - rectPoints[0].x;
        cv::Point2i pbl = projection->rotatePixel(rectPoints[1]);
        cv::Point2i pbr = projection->rotatePixel(rectPoints[2]);
        cv::Point2i pbot = (pbl.y < pbr.y) ? pbl : pbr;
        cv::Point2i ptop = projection->rotatePixel(rectPoints[0]);
    
        cv::Size rectSize = cv::Size(widthTop, pbot.y - rectPoints[0].y);
        camera->setCroppingMask(cv::Rect(ptop, rectSize)); // croppingMask
    }
    
    
    
    //
    //  PRIVATE
    //
    
    
    /*
        Get the 4 triangles in the quadrilateral
    */
    std::vector<std::vector<cv::Point2i>> SandboxSetup::getTriangles(std::vector<cv::Point2i> rectPoints){
        
        std::vector<std::vector<cv::Point2i>> triangles;
        std::vector<cv::Point2i> A, B, C, D;
        std::vector<cv::Point2i> lst[4] = {A,B,C,D};
    
        // 4 triangles in the quadrilateral
        for (int i=0; i<4; i++){
            // corners in the triangle
            for(int j=0; j<3; j++){
                lst[i].push_back(rectPoints.at( (i+j)%rectPoints.size() ));
            }
            triangles.push_back(lst[i]);
        }
    
        return triangles;
    }
    
    /*
        Get the centroid of each of the 4 triangles
        source : https://www.khanacademy.org/math/geometry-home/triangle-properties/medians-centroids/v/triangle-medians-and-centroids
    */
    std::vector<cv::Point2i> SandboxSetup::getCentroids(std::vector<std::vector<cv::Point2i>> triangles){
        
        std::vector<cv::Point2i> centroids;
    
        // the centroid is the average of the 3 coordinates
        for(int i=0; i<(int)triangles.size(); i++){
            std::vector<cv::Point2i> tr = triangles.at(i);
            cv::Point2i center;
            center.x = (tr.at(0).x + tr.at(1).x + tr.at(2).x) / 3;
            center.y = (tr.at(0).y + tr.at(1).y + tr.at(2).y) / 3;
            centroids.push_back(center);
        }
    
        return centroids;
    }
    
    double SandboxSetup::toDegrees(double radians){
        return radians * (180.0 / M_PI);
    }