16acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn/*M///////////////////////////////////////////////////////////////////////////////////////
26acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn//
36acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn//  IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING.
46acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn//
56acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn//  By downloading, copying, installing or using the software you agree to this license.
66acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn//  If you do not agree to this license, do not download, install,
76acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn//  copy or use the software.
86acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn//
96acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn//
106acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn//                        Intel License Agreement
116acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn//                For Open Source Computer Vision Library
126acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn//
136acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn// Copyright (C) 2000, Intel Corporation, all rights reserved.
146acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn// Third party copyrights are property of their respective owners.
156acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn//
166acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn// Redistribution and use in source and binary forms, with or without modification,
176acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn// are permitted provided that the following conditions are met:
186acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn//
196acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn//   * Redistribution's of source code must retain the above copyright notice,
206acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn//     this list of conditions and the following disclaimer.
216acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn//
226acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn//   * Redistribution's in binary form must reproduce the above copyright notice,
236acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn//     this list of conditions and the following disclaimer in the documentation
246acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn//     and/or other materials provided with the distribution.
256acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn//
266acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn//   * The name of Intel Corporation may not be used to endorse or promote products
276acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn//     derived from this software without specific prior written permission.
286acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn//
296acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn// This software is provided by the copyright holders and contributors "as is" and
306acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn// any express or implied warranties, including, but not limited to, the implied
316acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn// warranties of merchantability and fitness for a particular purpose are disclaimed.
326acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn// In no event shall the Intel Corporation or contributors be liable for any direct,
336acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn// indirect, incidental, special, exemplary, or consequential damages
346acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn// (including, but not limited to, procurement of substitute goods or services;
356acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn// loss of use, data, or profits; or business interruption) however caused
366acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn// and on any theory of liability, whether in contract, strict liability,
376acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn// or tort (including negligence or otherwise) arising in any way out of
386acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn// the use of this software, even if advised of the possibility of such damage.
396acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn//
406acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn//M*/
416acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn#include "_cvaux.h"
426acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn#include "_cvvm.h"
436acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn#include <stdlib.h>
446acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn#include <assert.h>
456acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn
466acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn
476acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn/*======================================================================================*/
486acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn
496acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius RennCvStatus
506acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius RennicvDynamicCorrespond( int *first,       /* first sequence of runs */
516acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn                      /* s0|w0|s1|w1|...|s(n-1)|w(n-1)|sn */
526acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn                      int first_runs,   /* number of runs */
536acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn                      int *second,      /* second sequence of runs */
546acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn                      int second_runs, int *first_corr, /* s0'|e0'|s1'|e1'|... */
556acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn                      int *second_corr )
566acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn{
576acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn
586acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn    float Pd, Fi, S;
596acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn    float Occlusion;
606acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn    float *costTable;
616acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn    uchar *matchEdges;
626acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn    int prev;
636acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn    int curr;
646acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn    int baseIndex;
656acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn    int i, j;
666acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn    int i_1, j_1;
676acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn    int n;
686acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn    int l_beg, r_beg, l_end, r_end, l_len, r_len;
696acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn    int first_curr;
706acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn    int second_curr;
716acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn    int l_color, r_color;
726acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn    int len_color;
736acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn    float cost, cost1;
746acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn    float min1, min2, min3;
756acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn    float cmin;
766acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn    uchar cpath;
776acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn    int row_size;
786acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn
796acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn    /* Test arguments for errors */
806acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn
816acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn    if( (first == 0) ||
826acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn        (first_runs < 1) ||
836acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn        (second == 0) || (second_runs < 1) || (first_corr == 0) || (second_corr == 0) )
846acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn
856acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn        return CV_BADFACTOR_ERR;
866acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn
876acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn
886acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn    Pd = 0.95f;
896acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn    Fi = (float) CV_PI;
906acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn    S = 1;
916acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn
926acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn    Occlusion = (float) log( Pd * Fi / ((1 - Pd) * sqrt( fabs( (CV_PI * 2) * (1. / S) ))));
936acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn
946acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn    costTable = (float *)cvAlloc( (first_runs + 1) * (second_runs + 1) * sizeof( float ));
956acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn
966acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn    if( costTable == 0 )
976acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn        return CV_OUTOFMEM_ERR;
986acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn
996acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn    matchEdges = (uchar *)cvAlloc( (first_runs + 1) * (second_runs + 1) * sizeof( uchar ));
1006acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn
1016acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn    if( matchEdges == 0 )
1026acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn    {
1036acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn        cvFree( &costTable );
1046acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn        return CV_OUTOFMEM_ERR;
1056acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn    }
1066acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn
1076acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn    row_size = first_runs + 1;
1086acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn
1096acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn    /* ============= Fill costTable ============= */
1106acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn
1116acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn    costTable[0] = 0.0f;
1126acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn
1136acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn    /* Fill upper line in the cost Table */
1146acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn
1156acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn    prev = first[0];
1166acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn    curr = 2;
1176acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn
1186acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn    for( n = 0; n < first_runs; n++ )
1196acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn    {
1206acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn
1216acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn        l_end = first[curr];
1226acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn        curr += 2;
1236acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn        costTable[n + 1] = costTable[n] + Occlusion * (l_end - prev);
1246acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn        prev = l_end;
1256acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn
1266acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn    }                           /* for */
1276acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn
1286acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn    /* Fill lefter line in the cost Table */
1296acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn
1306acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn    prev = second[0];
1316acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn    curr = 2;
1326acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn    baseIndex = 0;
1336acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn
1346acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn    for( n = 0; n < second_runs; n++ )
1356acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn    {
1366acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn
1376acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn        l_end = second[curr];
1386acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn        curr += 2;
1396acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn        costTable[baseIndex + row_size] = costTable[baseIndex] + Occlusion * (l_end - prev);
1406acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn        baseIndex += row_size;
1416acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn        prev = l_end;
1426acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn
1436acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn    }                           /* for */
1446acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn
1456acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn    /* Count costs in the all rest cells */
1466acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn
1476acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn    first_curr = 0;
1486acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn    second_curr = 0;
1496acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn
1506acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn    for( i = 1; i <= first_runs; i++ )
1516acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn    {
1526acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn        for( j = 1; j <= second_runs; j++ )
1536acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn        {
1546acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn
1556acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn            first_curr = (i - 1) * 2;
1566acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn            second_curr = (j - 1) * 2;
1576acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn
1586acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn            l_beg = first[first_curr];
1596acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn            first_curr++;
1606acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn            l_color = first[first_curr];
1616acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn            first_curr++;
1626acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn            l_end = first[first_curr];
1636acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn            l_len = l_end - l_beg + 1;
1646acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn
1656acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn            r_beg = second[second_curr];
1666acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn            second_curr++;
1676acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn            r_color = second[second_curr];
1686acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn            second_curr++;
1696acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn            r_end = second[second_curr];
1706acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn            r_len = r_end - r_beg + 1;
1716acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn
1726acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn            i_1 = i - 1;
1736acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn            j_1 = j - 1;
1746acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn
1756acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn            if( r_len == l_len )
1766acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn            {
1776acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn                cost = 0;
1786acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn            }
1796acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn            else
1806acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn            {
1816acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn
1826acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn                if( r_len > l_len )
1836acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn                {
1846acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn                    cost = (float) (r_len * r_len - l_len * l_len) * (1 / (r_len * l_len));
1856acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn                }
1866acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn                else
1876acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn                {
1886acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn                    cost = (float) (l_len * l_len - r_len * r_len) * (1 / (r_len * l_len));
1896acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn                }
1906acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn            }                   /* if */
1916acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn
1926acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn            len_color = r_color - l_color;
1936acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn
1946acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn            cost1 = (float) ((len_color * len_color) >> 2);
1956acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn
1966acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn            min2 = costTable[i_1 + j * row_size] + Occlusion * l_len;
1976acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn
1986acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn            min3 = costTable[i + j_1 * row_size] + Occlusion * r_len;
1996acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn
2006acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn            min1 = costTable[i_1 + j_1 * row_size] + cost + (float) cost1;
2016acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn
2026acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn            if( min1 < min2 )
2036acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn            {
2046acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn
2056acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn                if( min1 < min3 )
2066acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn                {
2076acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn                    cmin = min1;
2086acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn                    cpath = 1;
2096acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn                }
2106acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn                else
2116acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn                {
2126acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn                    cmin = min3;
2136acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn                    cpath = 3;
2146acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn                }               /* if */
2156acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn
2166acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn            }
2176acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn            else
2186acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn            {
2196acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn
2206acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn                if( min2 < min3 )
2216acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn                {
2226acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn                    cmin = min2;
2236acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn                    cpath = 2;
2246acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn                }
2256acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn                else
2266acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn                {
2276acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn                    cmin = min3;
2286acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn                    cpath = 3;
2296acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn                }               /* if */
2306acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn
2316acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn            }                   /* if */
2326acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn
2336acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn            costTable[i + j * row_size] = cmin;
2346acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn            matchEdges[i + j * row_size] = cpath;
2356acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn        }                       /* for */
2366acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn    }                           /* for */
2376acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn
2386acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn    /* =========== Reconstruct the Path =========== */
2396acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn
2406acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn    i = first_runs;
2416acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn    j = second_runs;
2426acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn
2436acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn    first_curr = i * 2 - 2;
2446acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn    second_curr = j * 2 - 2;
2456acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn
2466acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn
2476acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn    while( i > 0 && j > 0 )
2486acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn    {
2496acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn
2506acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn        /* Connect begins */
2516acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn        switch (matchEdges[i + j * row_size])
2526acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn        {
2536acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn
2546acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn        case 1:                /* to diagonal */
2556acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn
2566acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn            first_corr[first_curr] = second[second_curr];
2576acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn            first_corr[first_curr + 1] = second[second_curr + 2];
2586acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn            second_corr[second_curr] = first[first_curr];
2596acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn            second_corr[second_curr + 1] = first[first_curr + 2];
2606acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn
2616acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn            first_curr -= 2;
2626acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn            second_curr -= 2;
2636acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn            i--;
2646acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn            j--;
2656acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn
2666acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn            break;
2676acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn
2686acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn        case 2:                /* to left */
2696acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn
2706acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn            first_corr[first_curr] = second[second_curr + 2];
2716acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn            first_corr[first_curr + 1] = second[second_curr + 2];
2726acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn
2736acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn            first_curr -= 2;
2746acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn            i--;
2756acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn
2766acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn            break;
2776acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn
2786acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn        case 3:                /* to up */
2796acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn
2806acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn            second_corr[second_curr] = first[first_curr + 2];
2816acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn            second_corr[second_curr + 1] = first[first_curr + 2];
2826acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn
2836acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn            second_curr -= 2;
2846acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn            j--;
2856acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn
2866acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn            break;
2876acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn        }                       /* switch */
2886acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn    }                           /* while */
2896acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn
2906acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn    /* construct rest of horisontal path if its need */
2916acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn    while( i > 0 )
2926acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn    {
2936acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn
2946acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn        first_corr[first_curr] = second[second_curr + 2];       /* connect to begin */
2956acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn        first_corr[first_curr + 1] = second[second_curr + 2];   /* connect to begin */
2966acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn
2976acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn        first_curr -= 2;
2986acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn        i--;
2996acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn
3006acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn    }                           /* while */
3016acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn
3026acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn    /* construct rest of vertical path if its need */
3036acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn    while( j > 0 )
3046acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn    {
3056acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn
3066acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn        second_corr[second_curr] = first[first_curr + 2];
3076acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn        second_corr[second_curr + 1] = first[first_curr + 2];
3086acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn
3096acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn        second_curr -= 2;
3106acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn        j--;
3116acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn
3126acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn    }                           /* while */
3136acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn
3146acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn    cvFree( &costTable );
3156acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn    cvFree( &matchEdges );
3166acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn
3176acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn    return CV_NO_ERR;
3186acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn}                               /* icvDynamicCorrespond */
3196acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn
3206acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn
3216acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn/*======================================================================================*/
3226acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn
3236acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Rennstatic CvStatus
3246acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius RennicvDynamicCorrespondMulti( int lines,   /* number of scanlines */
3256acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn                           int *first,  /* s0|w0|s1|w1|...s(n-1)|w(n-1)|sn */
3266acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn                           int *first_runs,     /* numbers of runs */
3276acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn                           int *second, int *second_runs, int *first_corr,      /* s0'|e0'|s1'|e1'|... */
3286acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn                           int *second_corr )
3296acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn{
3306acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn    CvStatus error;
3316acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn
3326acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn    int currFirst;
3336acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn    int currSecond;
3346acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn    int currFirstCorr;
3356acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn    int currSecondCorr;
3366acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn    int n;
3376acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn
3386acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn    /* Test errors */
3396acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn
3406acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn    if( (lines < 1) ||
3416acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn        (first == 0) ||
3426acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn        (first_runs == 0) ||
3436acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn        (second == 0) || (second_runs == 0) || (first_corr == 0) || (second_corr == 0) )
3446acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn        return CV_BADFACTOR_ERR;
3456acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn
3466acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn    currFirst = 0;
3476acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn    currSecond = 0;
3486acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn    currFirstCorr = 0;
3496acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn    currSecondCorr = 0;
3506acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn
3516acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn    for( n = 0; n < lines; n++ )
3526acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn    {
3536acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn
3546acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn        error = icvDynamicCorrespond( &(first[currFirst]),
3556acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn                                      first_runs[n],
3566acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn                                      &(second[currSecond]),
3576acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn                                      second_runs[n],
3586acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn                                      &(first_corr[currFirstCorr]),
3596acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn                                      &(second_corr[currSecondCorr]) );
3606acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn
3616acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn        if( error != CV_NO_ERR )
3626acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn            return error;
3636acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn
3646acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn        currFirst += first_runs[n] * 2 + 1;
3656acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn        currSecond += second_runs[n] * 2 + 1;
3666acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn        currFirstCorr += first_runs[n] * 2;
3676acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn        currSecondCorr += second_runs[n] * 2;
3686acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn
3696acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn    }
3706acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn
3716acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn    return CV_NO_ERR;
3726acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn
3736acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn}                               /* icvDynamicCorrespondMulti */
3746acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn
3756acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn
3766acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn/*======================================================================================*/
3776acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn
3786acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn/*F///////////////////////////////////////////////////////////////////////////////////////
3796acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn//    Name: cvDynamicCorrespondMulti
3806acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn//    Purpose: The functions
3816acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn//    Context:
3826acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn//    Parameters:
3836acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn//
3846acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn//    Notes:
3856acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn//F*/
3866acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius RennCV_IMPL void
3876acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius RenncvDynamicCorrespondMulti( int lines,    /* number of scanlines */
3886acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn                          int *first,   /* s0|w0|s1|w1|...s(n-1)|w(n-1)|sn */
3896acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn                          int *first_runs,      /* numbers of runs */
3906acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn                          int *second, int *second_runs, int *first_corr,       /* s0'|e0'|s1'|e1'|... */
3916acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn                          int *second_corr )
3926acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn{
3936acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn    CV_FUNCNAME( "cvDynamicCorrespondMulti" );
3946acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn    __BEGIN__;
3956acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn
3966acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn    IPPI_CALL( icvDynamicCorrespondMulti( lines,        /* number of scanlines */
3976acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn                                          first,        /* s0|w0|s1|w1|...s(n-1)|w(n-1)|sn */
3986acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn                                          first_runs,   /* numbers of runs */
3996acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn                                          second, second_runs, first_corr,      /* s0'|e0'|s1'|e1'|... */
4006acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn                                          second_corr ));
4016acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn    __CLEANUP__;
4026acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn    __END__;
4036acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn}
404