Ejemplo n.º 1
0
static void
calculate_forces()
{
    /* O(n^2) approach */
    for(uint32_t j = 0; j < data.num_particles; j++) {
        v2 cur_pos = data.positions[j];
        for(uint32_t i = j + 1; i < data.num_particles; i++) {
            v2 other_pos = data.positions[i];
            v2 diff = get_min_diff(cur_pos, other_pos);
            float absForce = 1 / lengthSq(diff);
            v2 force = absForce * diff;
            data.forces[j] += force;
            data.forces[i] -= force;
        }
    }
}
size_t get_min_diff(const image& img1, const image& img2) {
    if(img1.nc() < img2.nc()) return get_min_diff(img2, img1);
    size_t score = INFTY;
    for (int i = -H_TOL; i<= H_TOL; i+=STEP_SIZE)
        for(int j= -V_TOL; j<= V_TOL; j+= STEP_SIZE) {
            image copy;
            dlib::assign_image(copy, img1);
            dlib::rectangle r1 = dlib::get_rect(img1);
            dlib::rectangle r2 = dlib::get_rect(img2);
            dlib::rectangle crop1 = dlib::intersect(r1, dlib::translate_rect(r2, i, j));
            dlib::rectangle crop2 = dlib::translate_rect(crop1, -i, -j);
            auto sub1 = dlib::sub_image(copy, crop1);
            auto sub2 = dlib::sub_image(img2, crop2);
            xor_copy(sub1, sub2);
            size_t res = cnt(copy);
            score = std::min(score, res);
        }
    return score;
}
double distance_processor::calculate_one(const image& img1, const image& img2) {
    double dist = get_min_diff(img1, img2);
    int size = std::min(img1.nc(), img2.nc());
    double s = std::max(dist/log2(size), 0.0);
    return pow(2, -s);
}