<commit_before>// This file is part of the AliceVision project.
// This Source Code Form is subject to the terms of the Mozilla Public License,
// v. 2.0. If a copy of the MPL was not distributed with this file,
// You can obtain one at https://mozilla.org/MPL/2.0/.

#include "ps_depthSimMap.hpp"

#include <aliceVision/common/fileIO.hpp>
#include <aliceVision/structures/mv_geometry.hpp>
#include <aliceVision/structures/jetColorMap.hpp>
#include <aliceVision/imageIO/image.hpp>

#include <iostream>


ps_depthSimMap::ps_depthSimMap(int _rc, multiviewParams* _mp, int _scale, int _step)
{
    rc = _rc;
    mp = _mp;
    o3d = new mv_output3D(mp);
    scale = _scale;
    step = _step;
    w = mp->mip->getWidth(rc) / (scale * step);
    h = mp->mip->getHeight(rc) / (scale * step);
    dsm = new staticVector<DepthSim>(w * h);
    dsm->resize_with(w * h, DepthSim(-1.0f, 1.0f));
}

ps_depthSimMap::~ps_depthSimMap()
{
    delete o3d;
    delete dsm;
}

point3d ps_depthSimMap::get3DPtOfPixel(const pixel& pix, int pixScale, int rc)
{
    pixel pix11 = pixel(pix.x * pixScale, pix.y * pixScale);
    int i = (pix11.y / (scale * step)) * w + pix11.x / (scale * step);
    float depth = (*dsm)[i].depth;
    return mp->CArr[rc] +
           (mp->iCamArr[rc] *
            point2d((float)(pix11.x) + (float)pixScale / 2.0f, (float)pix11.y + (float)pixScale / 2.0f))
                   .normalize() *
               depth;
}

float ps_depthSimMap::getFPDepthOfPixel(const pixel& pix, int pixScale, int rc)
{
    point3d p = get3DPtOfPixel(pix, pixScale, rc);
    point3d zVect = (mp->iRArr[rc] * point3d(0.0f, 0.0f, 1.0f)).normalize();
    return pointPlaneDistance(p, mp->CArr[rc], zVect);
}

void ps_depthSimMap::add11(ps_depthSimMap* depthSimMap)
{
    if((scale != 1) || (step != 1))
    {
        printf("Warning you can add just to cale1 step1 map!\n");
        exit(1);
    }

    int k = (depthSimMap->step * depthSimMap->scale) / 2;
    int k1 = (depthSimMap->step * depthSimMap->scale) / 2;
    if((depthSimMap->step * depthSimMap->scale) % 2 == 0)
    {
        k1 -= 1;
    }

    for(int i = 0; i < depthSimMap->dsm->size(); i++)
    {
        int x = (i % depthSimMap->w) * depthSimMap->step * depthSimMap->scale;
        int y = (i / depthSimMap->w) * depthSimMap->step * depthSimMap->scale;
        DepthSim depthSim = (*depthSimMap->dsm)[i];

        if(depthSim.depth > -1.0f)
        {

            bool isBest = true;
            for(int yp = y - k; yp <= y + k1; yp++)
            {
                for(int xp = x - k; xp <= x + k1; xp++)
                {
                    if((xp >= 0) && (xp < w) && (yp >= 0) && (yp < h) && (depthSim.sim > (*dsm)[yp * w + xp].sim))
                    {
                        isBest = false;
                    }
                }
            }

            if(isBest)
            {
                for(int yp = y - k; yp <= y + k1; yp++)
                {
                    for(int xp = x - k; xp <= x + k1; xp++)
                    {
                        if((xp >= 0) && (xp < w) && (yp >= 0) && (yp < h))
                        {
                            (*dsm)[yp * w + xp] = depthSim;
                        }
                    }
                }
            }
        }
    }
}

void ps_depthSimMap::add(ps_depthSimMap* depthSimMap)
{
    if((scale != depthSimMap->scale) || (step != depthSimMap->step))
    {
        printf("Warning you can add just to the same scale and step map!\n");
        exit(1);
    }

    for(int i = 0; i < dsm->size(); i++)
    {
        DepthSim depthSim1 = (*dsm)[i];
        DepthSim depthSim2 = (*depthSimMap->dsm)[i];

        if((depthSim2.depth > -1.0f) && (depthSim2.sim < depthSim1.sim))
        {
            (*dsm)[i] = depthSim2;
        }
    }
}<commit_msg>[planeSweeping] `ps_depthSimMap` fix : correct 1px shift in depth map<commit_after>// This file is part of the AliceVision project.
// This Source Code Form is subject to the terms of the Mozilla Public License,
// v. 2.0. If a copy of the MPL was not distributed with this file,
// You can obtain one at https://mozilla.org/MPL/2.0/.

#include "ps_depthSimMap.hpp"

#include <aliceVision/common/fileIO.hpp>
#include <aliceVision/structures/mv_geometry.hpp>
#include <aliceVision/structures/jetColorMap.hpp>
#include <aliceVision/imageIO/image.hpp>

#include <iostream>


ps_depthSimMap::ps_depthSimMap(int _rc, multiviewParams* _mp, int _scale, int _step)
{
    rc = _rc;
    mp = _mp;
    o3d = new mv_output3D(mp);
    scale = _scale;
    step = _step;
    w = mp->mip->getWidth(rc) / (scale * step);
    h = mp->mip->getHeight(rc) / (scale * step);
    dsm = new staticVector<DepthSim>(w * h);
    dsm->resize_with(w * h, DepthSim(-1.0f, 1.0f));
}

ps_depthSimMap::~ps_depthSimMap()
{
    delete o3d;
    delete dsm;
}

point3d ps_depthSimMap::get3DPtOfPixel(const pixel& pix, int pixScale, int rc)
{
    pixel pix11 = pixel(pix.x * pixScale, pix.y * pixScale);
    int i = (pix11.y / (scale * step)) * w + pix11.x / (scale * step);
    float depth = (*dsm)[i].depth;
    return mp->CArr[rc] +
           (mp->iCamArr[rc] *
            point2d((float)(pix11.x) + (float)pixScale / 2.0f, (float)pix11.y + (float)pixScale / 2.0f))
                   .normalize() *
               depth;
}

float ps_depthSimMap::getFPDepthOfPixel(const pixel& pix, int pixScale, int rc)
{
    point3d p = get3DPtOfPixel(pix, pixScale, rc);
    point3d zVect = (mp->iRArr[rc] * point3d(0.0f, 0.0f, 1.0f)).normalize();
    return pointPlaneDistance(p, mp->CArr[rc], zVect);
}

void ps_depthSimMap::add11(ps_depthSimMap* depthSimMap)
{
    if((scale != 1) || (step != 1))
    {
        throw std::runtime_error("Error ps_depthSimMap: You can only add to scale1-step1 map.");
    }

    int k = (depthSimMap->step * depthSimMap->scale) / 2;
    int k1 = k;
    if((depthSimMap->step * depthSimMap->scale) % 2 == 0)
        k -= 1;

    for(int i = 0; i < depthSimMap->dsm->size(); i++)
    {
        int x = (i % depthSimMap->w) * depthSimMap->step * depthSimMap->scale;
        int y = (i / depthSimMap->w) * depthSimMap->step * depthSimMap->scale;
        DepthSim depthSim = (*depthSimMap->dsm)[i];

        if(depthSim.depth > -1.0f)
        {

            bool isBest = true;
            for(int yp = y - k; yp <= y + k1; yp++)
            {
                for(int xp = x - k; xp <= x + k1; xp++)
                {
                    if((xp >= 0) && (xp < w) && (yp >= 0) && (yp < h) && // check image borders
                       (depthSim.sim > (*dsm)[yp * w + xp].sim))
                    {
                        isBest = false;
                    }
                }
            }

            if(isBest)
            {
                for(int yp = y - k; yp <= y + k1; yp++)
                {
                    for(int xp = x - k; xp <= x + k1; xp++)
                    {
                        if((xp >= 0) && (xp < w) && (yp >= 0) && (yp < h))
                        {
                            (*dsm)[yp * w + xp] = depthSim;
                        }
                    }
                }
            }
        }
    }
}

void ps_depthSimMap::add(ps_depthSimMap* depthSimMap)
{
    if((scale != depthSimMap->scale) || (step != depthSimMap->step))
    {
        throw std::runtime_error("Error ps_depthSimMap: You can only add to the same scale and step map.");
    }

    for(int i = 0; i < dsm->size(); i++)
    {
        DepthSim depthSim1 = (*dsm)[i];
        DepthSim depthSim2 = (*depthSimMap->dsm)[i];

        if((depthSim2.depth > -1.0f) && (depthSim2.sim < depthSim1.sim))
        {
            (*dsm)[i] = depthSim2;
        }
    }
}
{'fraction_non_alphanumeric': 0.1571297629959408, 'fraction_numerical': 0.018724630090349612, 'mean_word_length': 1.8309859154929577, 'pattern_counts': {'":': 0, '<': 37, '<?xml version=': 0, '>': 83, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2005848', 'n_tokens_mistral': 2848, 'n_tokens_neox': 2714, 'n_words': 632}