This source file includes following definitions.
- eps
 
- toString
 
- init
 
- compareRects
 
- process
 
- deinit
 
#include "test_precomp.hpp"
TestHypothesesFilter::TestHypothesesFilter(std::string testName_, NCVTestSourceProvider<Ncv32u> &src_,
                                           Ncv32u numDstRects_, Ncv32u minNeighbors_, Ncv32f eps_)
    :
    NCVTestProvider(testName_),
    src(src_),
    numDstRects(numDstRects_),
    minNeighbors(minNeighbors_),
    eps(eps_)
{
}
bool TestHypothesesFilter::toString(std::ofstream &strOut)
{
    strOut << "numDstRects=" << numDstRects << std::endl;
    strOut << "minNeighbors=" << minNeighbors << std::endl;
    strOut << "eps=" << eps << std::endl;
    return true;
}
bool TestHypothesesFilter::init()
{
    this->canvasWidth = 4096;
    this->canvasHeight = 4096;
    return true;
}
bool compareRects(const NcvRect32u &r1, const NcvRect32u &r2, Ncv32f eps)
{
    double delta = eps*(std::min(r1.width, r2.width) + std::min(r1.height, r2.height))*0.5;
    return std::abs((Ncv32s)r1.x - (Ncv32s)r2.x) <= delta &&
        std::abs((Ncv32s)r1.y - (Ncv32s)r2.y) <= delta &&
        std::abs((Ncv32s)r1.x + (Ncv32s)r1.width - (Ncv32s)r2.x - (Ncv32s)r2.width) <= delta &&
        std::abs((Ncv32s)r1.y + (Ncv32s)r1.height - (Ncv32s)r2.y - (Ncv32s)r2.height) <= delta;
}
inline bool operator < (const NcvRect32u &a, const NcvRect32u &b)
{
    return a.x < b.x;
}
bool TestHypothesesFilter::process()
{
    NCVStatus ncvStat;
    bool rcode = false;
    NCVVectorAlloc<Ncv32u> h_random32u(*this->allocatorCPU.get(), this->numDstRects * sizeof(NcvRect32u) / sizeof(Ncv32u));
    ncvAssertReturn(h_random32u.isMemAllocated(), false);
    Ncv32u srcSlotSize = 2 * this->minNeighbors + 1;
    NCVVectorAlloc<NcvRect32u> h_vecSrc(*this->allocatorCPU.get(), this->numDstRects*srcSlotSize);
    ncvAssertReturn(h_vecSrc.isMemAllocated(), false);
    NCVVectorAlloc<NcvRect32u> h_vecDst_groundTruth(*this->allocatorCPU.get(), this->numDstRects);
    ncvAssertReturn(h_vecDst_groundTruth.isMemAllocated(), false);
    NCV_SET_SKIP_COND(this->allocatorCPU.get()->isCounting());
    NCV_SKIP_COND_BEGIN
    ncvAssertReturn(this->src.fill(h_random32u), false);
    Ncv32u randCnt = 0;
    Ncv64f randVal;
    for (Ncv32u i=0; i<this->numDstRects; i++)
    {
        h_vecDst_groundTruth.ptr()[i].x = i * this->canvasWidth / this->numDstRects + this->canvasWidth / (this->numDstRects * 4);
        h_vecDst_groundTruth.ptr()[i].y = i * this->canvasHeight / this->numDstRects + this->canvasHeight / (this->numDstRects * 4);
        h_vecDst_groundTruth.ptr()[i].width = this->canvasWidth / (this->numDstRects * 2);
        h_vecDst_groundTruth.ptr()[i].height = this->canvasHeight / (this->numDstRects * 2);
        Ncv32u numNeighbors = this->minNeighbors + 1 + (Ncv32u)(((1.0 * h_random32u.ptr()[i]) * (this->minNeighbors + 1)) / 0xFFFFFFFF);
        numNeighbors = (numNeighbors > srcSlotSize) ? srcSlotSize : numNeighbors;
        
        for (Ncv32u j=0; j<numNeighbors; j++)
        {
            randVal = (1.0 * h_random32u.ptr()[randCnt++]) / 0xFFFFFFFF; randCnt = randCnt % h_random32u.length();
            h_vecSrc.ptr()[srcSlotSize * i + j].x =
                h_vecDst_groundTruth.ptr()[i].x +
                (Ncv32s)(h_vecDst_groundTruth.ptr()[i].width * this->eps * (randVal - 0.5));
            randVal = (1.0 * h_random32u.ptr()[randCnt++]) / 0xFFFFFFFF; randCnt = randCnt % h_random32u.length();
            h_vecSrc.ptr()[srcSlotSize * i + j].y =
                h_vecDst_groundTruth.ptr()[i].y +
                (Ncv32s)(h_vecDst_groundTruth.ptr()[i].height * this->eps * (randVal - 0.5));
            h_vecSrc.ptr()[srcSlotSize * i + j].width = h_vecDst_groundTruth.ptr()[i].width;
            h_vecSrc.ptr()[srcSlotSize * i + j].height = h_vecDst_groundTruth.ptr()[i].height;
        }
        
        for (Ncv32u j=numNeighbors; j<srcSlotSize; j++)
        {
            randVal = (1.0 * h_random32u.ptr()[randCnt++]) / 0xFFFFFFFF; randCnt = randCnt % h_random32u.length();
            h_vecSrc.ptr()[srcSlotSize * i + j].x =
                this->canvasWidth + h_vecDst_groundTruth.ptr()[i].x +
                (Ncv32s)(h_vecDst_groundTruth.ptr()[i].width * this->eps * (randVal - 0.5));
            randVal = (1.0 * h_random32u.ptr()[randCnt++]) / 0xFFFFFFFF; randCnt = randCnt % h_random32u.length();
            h_vecSrc.ptr()[srcSlotSize * i + j].y =
                this->canvasHeight + h_vecDst_groundTruth.ptr()[i].y +
                (Ncv32s)(h_vecDst_groundTruth.ptr()[i].height * this->eps * (randVal - 0.5));
            h_vecSrc.ptr()[srcSlotSize * i + j].width = h_vecDst_groundTruth.ptr()[i].width;
            h_vecSrc.ptr()[srcSlotSize * i + j].height = h_vecDst_groundTruth.ptr()[i].height;
        }
    }
    
    for (Ncv32u i=0; i<this->numDstRects*srcSlotSize-1; i++)
    {
        Ncv32u randValLocal = h_random32u.ptr()[randCnt++]; randCnt = randCnt % h_random32u.length();
        Ncv32u secondSwap = randValLocal % (this->numDstRects*srcSlotSize-1 - i);
        NcvRect32u tmp = h_vecSrc.ptr()[i + secondSwap];
        h_vecSrc.ptr()[i + secondSwap] = h_vecSrc.ptr()[i];
        h_vecSrc.ptr()[i] = tmp;
    }
    NCV_SKIP_COND_END
    Ncv32u numHypothesesSrc = static_cast<Ncv32u>(h_vecSrc.length());
    NCV_SKIP_COND_BEGIN
    ncvStat = ncvGroupRectangles_host(h_vecSrc, numHypothesesSrc, this->minNeighbors, this->eps, NULL);
    ncvAssertReturn(ncvStat == NCV_SUCCESS, false);
    NCV_SKIP_COND_END
    
    bool bLoopVirgin = true;
    NCV_SKIP_COND_BEGIN
    if (numHypothesesSrc != this->numDstRects)
    {
        bLoopVirgin = false;
    }
    else
    {
        std::vector<NcvRect32u> tmpRects(numHypothesesSrc);
        memcpy(&tmpRects[0], h_vecSrc.ptr(), numHypothesesSrc * sizeof(NcvRect32u));
        std::sort(tmpRects.begin(), tmpRects.end());
        for (Ncv32u i=0; i<numHypothesesSrc && bLoopVirgin; i++)
        {
            if (!compareRects(tmpRects[i], h_vecDst_groundTruth.ptr()[i], this->eps))
            {
                bLoopVirgin = false;
            }
        }
    }
    NCV_SKIP_COND_END
    if (bLoopVirgin)
    {
        rcode = true;
    }
    return rcode;
}
bool TestHypothesesFilter::deinit()
{
    return true;
}