Logo Search packages:      
Sourcecode: leptonlib version File versions  Download package

pts.c

/*====================================================================*
 -  Copyright (C) 2001 Leptonica.  All rights reserved.
 -  This software is distributed in the hope that it will be
 -  useful, but with NO WARRANTY OF ANY KIND.
 -  No author or distributor accepts responsibility to anyone for the
 -  consequences of using this software, or for whether it serves any
 -  particular purpose or works at all, unless he or she says so in
 -  writing.  Everyone is granted permission to copy, modify and
 -  redistribute this source code, for commercial or non-commercial
 -  purposes, with the following restrictions: (1) the origin of this
 -  source code must not be misrepresented; (2) modified versions must
 -  be plainly marked as such; and (3) this notice may not be removed
 -  or altered from any source or modified source distribution.
 *====================================================================*/


/*
 *   pts.c
 *
 *      Pta creation, destruction, copy, clone, empty
 *           PTA      *ptaCreate()
 *           void      ptaDestroy()
 *           PTA      *ptaCopy()
 *           PTA      *ptaClone()
 *           l_int32   ptaEmpty()
 *
 *      Pta array extension
 *           l_int32   ptaAddPt()
 *           l_int32   ptaExtendArrays()
 *
 *      Pta rearrangements
 *           l_int32   ptaJoin()
 *           PTA      *ptaReverse()
 *           PTA      *ptaCyclicPerm()
 *           PTA      *ptaSort()
 *           PTA      *ptaRemoveDuplicates()
 *
 *      Pta Accessors
 *           l_int32   ptaGetRefcount()
 *           l_int32   ptaChangeRefcount()
 *           l_int32   ptaGetCount()
 *           l_int32   ptaGetPt()
 *           l_int32   ptaGetIPt()
 *           l_int32   ptaGetArrays()
 *
 *      Ptaa creation, destruction
 *           PTAA     *ptaaCreate()
 *           void      ptaaDestroy()
 *
 *      Ptaa array extension
 *           l_int32   ptaaAddPta()
 *           l_int32   ptaaExtendArray()
 *
 *      Ptaa Accessors
 *           l_int32   ptaaGetCount()
 *           l_int32   ptaaGetPta()
 *  
 *      Pta serialized I/O
 *           l_int32   ptaWriteStream()
 *           PTA      *ptaReadStream()
 *
 *      In use
 *           PTA      *ptaFindCornerPixels()
 *           l_int32   pixPlotAlongPta()
 *           l_int32   ptaContainsPt()
 *           l_int32   ptaTestIntersection()
 *           l_int32   ptaGetLinearLSF()
 *           PTA      *ptaGetPixelsFromPix()
 */

#include <stdio.h>
#include <stdlib.h>
#include "allheaders.h"

static const l_int32  INITIAL_PTR_ARRAYSIZE = 20;   /* n'import quoi */

    /* Default spreading factor for hashing pts in a plane */
static const l_int32  DEFAULT_SPREADING_FACTOR = 7500;


/*---------------------------------------------------------------------*
 *                PTA creation, destruction, copy, clone               *
 *---------------------------------------------------------------------*/
/*!
 *  ptaCreate()
 *
 *      Input:  n  (initial array sizes)
 *      Return: pta, or null on error.
 */
PTA *
ptaCreate(l_int32  n)
{
PTA  *pta;

    PROCNAME("ptaCreate");

    if (n <= 0)
      n = INITIAL_PTR_ARRAYSIZE;

    if ((pta = (PTA *)CALLOC(1, sizeof(PTA))) == NULL)
      return (PTA *)ERROR_PTR("pta not made", procName, NULL);
    pta->n = 0;
    pta->nalloc = n;
    ptaChangeRefcount(pta, 1);  /* sets to 1 */
    
    if ((pta->x = (l_float32 *)CALLOC(n, sizeof(l_float32))) == NULL)
      return (PTA *)ERROR_PTR("x array not made", procName, NULL);
    if ((pta->y = (l_float32 *)CALLOC(n, sizeof(l_float32))) == NULL)
      return (PTA *)ERROR_PTR("y array not made", procName, NULL);

    return pta;
}


/*!
 *  ptaDestroy()
 *
 *      Input:  &pta (<to be nulled>)
 *      Return: void
 *
 *  Note:
 *      - Decrements the ref count and, if 0, destroys the pta.
 *      - Always nulls the input ptr.
 */
void
ptaDestroy(PTA  **ppta)
{
PTA  *pta;

    PROCNAME("ptaDestroy");

    if (ppta == NULL) {
        L_WARNING("ptr address is NULL!", procName);
      return;
    }

    if ((pta = *ppta) == NULL)
      return;

    ptaChangeRefcount(pta, -1);
    if (ptaGetRefcount(pta) <= 0) {
        FREE((void *)pta->x);
        FREE((void *)pta->y);
        FREE((void *)pta);
    }

    *ppta = NULL;
    return;
}


/*!
 *  ptaCopy()
 *
 *      Input:  pta
 *      Return: copy of pta, or null on error
 */
PTA *
ptaCopy(PTA  *pta)
{
l_int32    i;
l_float32  x, y;
PTA  *npta;

    PROCNAME("ptaCopy");

    if (!pta)
      return (PTA *)ERROR_PTR("pta not defined", procName, NULL);

    if ((npta = ptaCreate(pta->nalloc)) == NULL)
      return (PTA *)ERROR_PTR("npta not made", procName, NULL);

    for (i = 0; i < pta->n; i++) {
      ptaGetPt(pta, i, &x, &y);
      ptaAddPt(npta, x, y);
    }

    return npta;
}


/*!
 *  ptaClone()
 *
 *      Input:  pta
 *      Return: ptr to same pta, or null on error
 */
PTA *
ptaClone(PTA  *pta)
{
    PROCNAME("ptaClone");

    if (!pta)
      return (PTA *)ERROR_PTR("pta not defined", procName, NULL);

    ptaChangeRefcount(pta, 1);
    return pta;
}


/*!
 *  ptaEmpty()
 *
 *      Input:  pta
 *      Return: 0 if OK, 1 on error
 *
 *  Note: this only resets the "n" field, for reuse
 */
l_int32
ptaEmpty(PTA  *pta)
{
    PROCNAME("ptaEmpty");

    if (!pta)
      return ERROR_INT("ptad not defined", procName, 1);
    pta->n = 0;
    return 0;
}


/*---------------------------------------------------------------------*
 *                          PTA array extension                        *
 *---------------------------------------------------------------------*/
/*!
 *  ptaAddPt()
 *
 *      Input:  pta 
 *              x, y
 *      Return: 0 if OK, 1 on error
 */
l_int32
ptaAddPt(PTA       *pta,
         l_float32  x,
       l_float32  y)
{
l_int32  n;

    PROCNAME("ptaAddPt");

    if (!pta)
      return ERROR_INT("pta not defined", procName, 1);

    n = pta->n;
    if (n >= pta->nalloc)
      ptaExtendArrays(pta);
    pta->x[n] = x;
    pta->y[n] = y;
    pta->n++;

    return 0;
}


/*!
 *  ptaExtendArrays()
 *
 *      Input:  pta
 *      Return: 0 if OK; 1 on error
 */
l_int32
ptaExtendArrays(PTA  *pta)
{
    PROCNAME("ptaExtendArrays");

    if (!pta)
      return ERROR_INT("pta not defined", procName, 1);

    if ((pta->x = (l_float32 *)reallocNew((void **)&pta->x,
                               sizeof(l_intptr_t) * pta->nalloc,
                               2 * sizeof(l_intptr_t) * pta->nalloc)) == NULL)
      return ERROR_INT("new x array not returned", procName, 1);
    if ((pta->y = (l_float32 *)reallocNew((void **)&pta->y,
                               sizeof(l_intptr_t) * pta->nalloc,
                               2 * sizeof(l_intptr_t) * pta->nalloc)) == NULL)
      return ERROR_INT("new y array not returned", procName, 1);

    pta->nalloc = 2 * pta->nalloc;
    return 0;
}



/*---------------------------------------------------------------------*
 *                           Pta rearrangements                        *
 *---------------------------------------------------------------------*/
/*!
 *  ptaJoin()
 *
 *      Input:  ptad  (dest pta; add to this one)
 *              ptas  (source pta; add from this one)
 *              istart  (starting index in ptas)
 *              iend  (ending index in ptas; use 0 to cat all)
 *      Return: 0 if OK, 1 on error
 *
 *  Notes:
 *      (1) istart < 0 is taken to mean 'read from the start' (istart = 0)
 *      (2) iend <= 0 means 'read to the end'
 */
l_int32
ptaJoin(PTA     *ptad,
        PTA     *ptas,
        l_int32  istart,
        l_int32  iend)
{
l_int32  ns, i, x, y;

    PROCNAME("ptaJoin");

    if (!ptad)
      return ERROR_INT("ptad not defined", procName, 1);
    if (!ptas)
      return ERROR_INT("ptas not defined", procName, 1);
    ns = ptaGetCount(ptas);
    if (istart < 0)
      istart = 0;
    if (istart >= ns)
      return ERROR_INT("istart out of bounds", procName, 1);
    if (iend <= 0)
      iend = ns - 1;
    if (iend >= ns)
      return ERROR_INT("iend out of bounds", procName, 1);
    if (istart > iend)
      return ERROR_INT("istart > iend; no pts", procName, 1);

    for (i = istart; i <= iend; i++) {
      ptaGetIPt(ptas, i, &x, &y);
      ptaAddPt(ptad, x, y);
    }

    return 0;
}


/*!
 *  ptaReverse()
 *
 *      Input:  ptas
 *              type  (0 for float values; 1 for integer values)
 *      Return: ptad (reversed pta), or null on error
 */
PTA  *
ptaReverse(PTA     *ptas,
           l_int32  type)
{
l_int32    n, i, ix, iy;
l_float32  x, y;
PTA       *ptad;

    PROCNAME("ptaReverse");

    if (!ptas)
      return (PTA *)ERROR_PTR("ptas not defined", procName, NULL);

    n = ptaGetCount(ptas);
    if ((ptad = ptaCreate(n)) == NULL)
      return (PTA *)ERROR_PTR("ptad not made", procName, NULL);
    for (i = n - 1; i >= 0; i--) {
      if (type == 0) {
          ptaGetPt(ptas, i, &x, &y);
          ptaAddPt(ptad, x, y);
      }
      else {  /* type == 1 */
          ptaGetIPt(ptas, i, &ix, &iy);
          ptaAddPt(ptad, ix, iy);
      }
    }

    return ptad;
}


/*!
 *  ptaCyclicPerm()
 *
 *      Input:  ptas
 *              xs, ys  (start point; must be in ptas)
 *      Return: ptad (cyclic permutation, starting and ending at (xs, ys),
 *              or null on error
 *
 *  Note: we check to insure that ptas is a closed path where
 *        the first and last points are identical, and the
 *        resulting pta also starts and ends on the same point
 *        (which in this case is (xs, ys).
 */
PTA  *
ptaCyclicPerm(PTA     *ptas,
              l_int32  xs,
              l_int32  ys)
{
l_int32  n, i, x, y, j, index, state;
l_int32  x1, y1, x2, y2;
PTA     *ptad;

    PROCNAME("ptaCyclicPerm");

    if (!ptas)
      return (PTA *)ERROR_PTR("ptas not defined", procName, NULL);

    n = ptaGetCount(ptas);

      /* verify input data */
    ptaGetIPt(ptas, 0, &x1, &y1);
    ptaGetIPt(ptas, n - 1, &x2, &y2);
    if (x1 != x2 || y1 != y2)
      return (PTA *)ERROR_PTR("start and end pts not same", procName, NULL);
    state = L_NOT_FOUND;
    for (i = 0; i < n; i++) {
      ptaGetIPt(ptas, i, &x, &y);
      if (x == xs && y == ys) {
          state = L_FOUND;
          break;
      }
    }
    if (state == L_NOT_FOUND)
      return (PTA *)ERROR_PTR("start pt not in ptas", procName, NULL);

    if ((ptad = ptaCreate(n)) == NULL)
      return (PTA *)ERROR_PTR("ptad not made", procName, NULL);
    for (j = 0; j < n - 1; j++) {
      if (i + j < n - 1)
          index = i + j;
      else
          index = (i + j + 1) % n;
      ptaGetIPt(ptas, index, &x, &y);
      ptaAddPt(ptad, x, y);
    }
    ptaAddPt(ptad, xs, ys);

    return ptad;
}


/*!
 *  ptaSort()
 * 
 *      Input:  ptas
 *              sorttype (L_SORT_BY_X, L_SORT_BY_Y)
 *              sortorder  (L_SORT_INCREASING, L_SORT_DECREASING)
 *              &naindex (<optional return> index of sorted order into
 *                        original array)
 *      Return: ptad (sorted version of ptas), or null on error
 */
PTA *
ptaSort(PTA     *ptas,
        l_int32  sorttype,
        l_int32  sortorder,
        NUMA   **pnaindex)
{
l_int32    i, index, n;
l_float32  x, y;
PTA       *ptad;
NUMA      *na, *naindex;

    PROCNAME("ptaSort");

    if (!ptas)
      return (PTA *)ERROR_PTR("ptas not defined", procName, NULL);
    if (sorttype != L_SORT_BY_X && sorttype != L_SORT_BY_Y)
      return (PTA *)ERROR_PTR("invalid sort type", procName, NULL);
    if (sortorder != L_SORT_INCREASING && sortorder != L_SORT_DECREASING)
      return (PTA *)ERROR_PTR("invalid sort order", procName, NULL);

        /* Build up numa of specific data */
    n = ptaGetCount(ptas);
    if ((na = numaCreate(n)) == NULL)
      return (PTA *)ERROR_PTR("na not made", procName, NULL);
    for (i = 0; i < n; i++) {
        ptaGetPt(ptas, i, &x, &y);
      if (sorttype == L_SORT_BY_X)
          numaAddNumber(na, x);
        else
          numaAddNumber(na, y);
    }

        /* Get the sort index for data array */
    if ((naindex = numaSortIndex(na, sortorder)) == NULL)
      return (PTA *)ERROR_PTR("naindex not made", procName, NULL);

        /* Build up sorted pta using sort index */
    if ((ptad = ptaCreate(n)) == NULL)
      return (PTA *)ERROR_PTR("ptad not made", procName, NULL);
    for (i = 0; i < n; i++) {
        numaGetIValue(naindex, i, &index);
        ptaGetPt(ptas, index, &x, &y);
      ptaAddPt(ptad, x, y);
    }

    if (pnaindex)
        *pnaindex = naindex;
    else
        numaDestroy(&naindex);
    numaDestroy(&na);
    return ptad;
}


/*!
 *  ptaRemoveDuplicates()
 * 
 *      Input:  ptas (assumed to be integer values)
 *              factor (should be larger than the largest point value;
 *                      use 0 for default)
 *      Return: ptad (with duplicates removed), or null on error
 */
PTA *
ptaRemoveDuplicates(PTA      *ptas,
                    l_uint32  factor)
{
l_int32    nsize, i, j, k, index, n, nvals;
l_int32    x, y, xk, yk;
l_int32   *ia;
PTA       *ptad;
NUMA      *na;
NUMAHASH  *nahash;

    PROCNAME("ptaRemoveDuplicates");

    if (!ptas)
      return (PTA *)ERROR_PTR("ptas not defined", procName, NULL);
    if (factor == 0)
        factor = DEFAULT_SPREADING_FACTOR;

        /* Build up numaHash of indices, hashed by a key that is
         * a large linear combination of x and y values designed to
         * randomize the key. */
    nsize = 5507;  /* buckets in hash table; prime */
    nahash = numaHashCreate(nsize, 2);
    n = ptaGetCount(ptas);
    for (i = 0; i < n; i++) {
        ptaGetIPt(ptas, i, &x, &y);
        numaHashAdd(nahash, factor * x + y, (l_float32)i);
    }

    if ((ptad = ptaCreate(n)) == NULL)
      return (PTA *)ERROR_PTR("ptad not made", procName, NULL);
    for (i = 0; i < nsize; i++) {
        na = numaHashGetNuma(nahash, i);
        if (!na) continue;

        nvals = numaGetCount(na);
            /* If more than 1 pt, compare exhaustively with double loop;
             * otherwise, just enter it. */
        if (nvals > 1) {
            if ((ia = (l_int32 *)CALLOC(nvals, sizeof(l_int32))) == NULL)
                return (PTA *)ERROR_PTR("ia not made", procName, NULL);
            for (j = 0; j < nvals; j++) {
                if (ia[j] == 1) continue;
                numaGetIValue(na, j, &index);
                ptaGetIPt(ptas, index, &x, &y);
                ptaAddPt(ptad, x, y);
                for (k = j + 1; k < nvals; k++) {
                    if (ia[k] == 1) continue;
                    numaGetIValue(na, k, &index);
                    ptaGetIPt(ptas, index, &xk, &yk);
                    if (x == xk && y == yk)  /* duplicate */
                        ia[k] = 1;
                }
            }
            FREE((void *)ia);
        }
        else {
            numaGetIValue(na, 0, &index);
            ptaGetIPt(ptas, index, &x, &y);
            ptaAddPt(ptad, x, y);
        }
        numaDestroy(&na);  /* the clone */
    }

    numaHashDestroy(&nahash);
    return ptad;
}



/*---------------------------------------------------------------------*
 *                           Pta accessors                             *
 *---------------------------------------------------------------------*/
l_int32
ptaGetRefcount(PTA  *pta)
{
    PROCNAME("ptaGetRefcount");

    if (!pta)
      return ERROR_INT("pta not defined", procName, 1);
    return pta->refcount;
}


l_int32
ptaChangeRefcount(PTA     *pta,
                  l_int32  delta)
{
    PROCNAME("ptaChangeRefcount");

    if (!pta)
      return ERROR_INT("pta not defined", procName, 1);
    pta->refcount += delta;
    return 0;
}


/*!
 *  ptaGetCount()
 *
 *      Input:  pta
 *      Return: count, or 0 if no pta
 */
l_int32
ptaGetCount(PTA  *pta)
{
    PROCNAME("ptaGetCount");

    if (!pta)
      return ERROR_INT("pta not defined", procName, 0);

    return pta->n;
}


/*!
 *  ptaGetPt()
 *
 *      Input:  pta
 *              index  (into arrays)
 *              &x, &y  (<return> float values)
 *      Return: 0 if OK; 1 on error
 */
l_int32
ptaGetPt(PTA        *pta,
         l_int32     index,
         l_float32  *px,
       l_float32  *py)
{
    PROCNAME("ptaGetPt");

    if (!pta)
      return ERROR_INT("pta not defined", procName, 1);

    *px = pta->x[index];
    *py = pta->y[index];
    return 0;
}


/*!
 *  ptaGetIPt()
 *
 *      Input:  pta
 *              index  (into arrays)
 *              &x, &y  (<return> integer values)
 *      Return: 0 if OK; 1 on error
 */
l_int32
ptaGetIPt(PTA      *pta,
          l_int32   index,
          l_int32  *px,
        l_int32  *py)
{
    PROCNAME("ptaGetIPt");

    if (!pta)
      return ERROR_INT("pta not defined", procName, 1);

    *px = (l_int32)(pta->x[index] + 0.5);
    *py = (l_int32)(pta->y[index] + 0.5);
    return 0;
}


/*!
 *  ptaGetArrays()
 *
 *      Input:  pta
 *              &nax, &nay  (<return> numas of x and y arrays)
 *      Return: 0 if OK; 1 on error or if pta is empty
 *
 *  Notes:
 *      (1) This copies the internal arrays into new Numas, and returns them.
 *      (2) Manipulates internal arrays in pta and numa directly.
 */
l_int32
ptaGetArrays(PTA    *pta,
             NUMA  **pnax,
             NUMA  **pnay)
{
l_int32   i, n;
NUMA     *nax, *nay;

    PROCNAME("ptaGetArrays");

    if (!pta)
      return ERROR_INT("pta not defined", procName, 1);
    if (!pnax)
      return ERROR_INT("&nax not defined", procName, 1);
    if (!pnay)
      return ERROR_INT("&nay not defined", procName, 1);

    *pnax = *pnay = NULL;
    if ((n = ptaGetCount(pta)) == 0)
      return ERROR_INT("pta is empty", procName, 1);

    if ((nax = numaCreate(n)) == NULL)
      return ERROR_INT("nax not made", procName, 1);
    *pnax = nax;
    if ((nay = numaCreate(n)) == NULL)
      return ERROR_INT("nay not made", procName, 1);
    *pnay = nay;

        /* use arrays directly for efficiency */
    for (i = 0; i < n; i++) {
        nax->array[i] = pta->x[i];
        nay->array[i] = pta->y[i];
    }
    nax->n = nay->n = n;
        
    return 0;
}



/*---------------------------------------------------------------------*
 *                     PTAA creation, destruction                      *
 *---------------------------------------------------------------------*/
/*!
 *  ptaaCreate()
 *
 *      Input:  n  (initial number of ptrs)
 *      Return: ptaa, or null on error
 */
PTAA *
ptaaCreate(l_int32  n)
{
PTAA  *ptaa;

    PROCNAME("ptaaCreate");

    if (n <= 0)
      n = INITIAL_PTR_ARRAYSIZE;

    if ((ptaa = (PTAA *)CALLOC(1, sizeof(PTAA))) == NULL)
      return (PTAA *)ERROR_PTR("ptaa not made", procName, NULL);
    ptaa->n = 0;
    ptaa->nalloc = n;

    if ((ptaa->pta = (PTA **)CALLOC(n, sizeof(PTA *))) == NULL)
      return (PTAA *)ERROR_PTR("pta ptrs not made", procName, NULL);
    
    return ptaa;
}


/*!
 *  ptaaDestroy()
 *
 *      Input:  &ptaa <to be nulled>
 *      Return: void
 */
void
ptaaDestroy(PTAA  **pptaa)
{
l_int32  i;
PTAA    *ptaa;

    PROCNAME("ptaaDestroy");

    if (pptaa == NULL) {
        L_WARNING("ptr address is NULL!", procName);
      return;
    }

    if ((ptaa = *pptaa) == NULL)
      return;

    for (i = 0; i < ptaa->n; i++) 
      ptaDestroy(&ptaa->pta[i]);
    FREE((void *)ptaa->pta);

    FREE((void *)ptaa);
    *pptaa = NULL;
    return;
}


/*---------------------------------------------------------------------*
 *                          PTAA array extension                       *
 *---------------------------------------------------------------------*/
/*!
 *  ptaaAddPta()
 *
 *      Input:  ptaa
 *              pta  (to be added)
 *              copyflag  (L_INSERT, L_COPY, L_CLONE)
 *      Return: 0 if OK, 1 on error
 */
l_int32
ptaaAddPta(PTAA    *ptaa,
           PTA     *pta,
           l_int32  copyflag)
{
l_int32  n;
PTA     *ptac;

    PROCNAME("ptaaAddPta");

    if (!ptaa)
      return ERROR_INT("ptaa not defined", procName, 1);
    if (!pta)
      return ERROR_INT("pta not defined", procName, 1);

    if (copyflag == L_INSERT)
      ptac = pta;
    else if (copyflag == L_COPY) {
      if ((ptac = ptaCopy(pta)) == NULL)
          return ERROR_INT("ptac not made", procName, 1);
    }
    else if (copyflag == L_CLONE) {
      if ((ptac = ptaClone(pta)) == NULL)
          return ERROR_INT("pta clone not made", procName, 1);
    }
    else
      return ERROR_INT("invalid copyflag", procName, 1);

    n = ptaaGetCount(ptaa);
    if (n >= ptaa->nalloc)
      ptaaExtendArray(ptaa);
    ptaa->pta[n] = ptac;
    ptaa->n++;

    return 0;
}


/*!
 *  ptaaExtendArray()
 *
 *      Input:  ptaa
 *      Return: 0 if OK, 1 on error
 */
l_int32
ptaaExtendArray(PTAA  *ptaa)
{
    PROCNAME("ptaaExtendArray");

    if (!ptaa)
      return ERROR_INT("ptaa not defined", procName, 1);

    if ((ptaa->pta = (PTA **)reallocNew((void **)&ptaa->pta,
                             sizeof(l_intptr_t) * ptaa->nalloc,
                             2 * sizeof(l_intptr_t) * ptaa->nalloc)) == NULL)
      return ERROR_INT("new ptr array not returned", procName, 1);

    ptaa->nalloc = 2 * ptaa->nalloc;
    return 0;
}


/*---------------------------------------------------------------------*
 *                          Ptaa accessors                             *
 *---------------------------------------------------------------------*/
/*!
 *  ptaaGetCount()
 *
 *      Input:  ptaa
 *      Return: count, or 0 if no ptaa
 */
l_int32
ptaaGetCount(PTAA  *ptaa)
{
    PROCNAME("ptaaGetCount");

    if (!ptaa)
      return ERROR_INT("ptaa not defined", procName, 0);

    return ptaa->n;
}


/*!
 *  ptaaGetPta()
 *
 *      Input:  ptaa
 *              index  (to the i-th pta)
 *              accessflag  (L_COPY or L_CLONE)
 *      Return: pta, or null on error
 */
PTA *
ptaaGetPta(PTAA    *ptaa,
           l_int32  index,
         l_int32  accessflag)
{
    PROCNAME("ptaaGetPta");

    if (!ptaa)
      return (PTA *)ERROR_PTR("ptaa not defined", procName, NULL);
    if (index < 0 || index >= ptaa->n)
      return (PTA *)ERROR_PTR("index not valid", procName, NULL);

    if (accessflag == L_COPY)
      return ptaCopy(ptaa->pta[index]);
    else if (accessflag == L_CLONE)
      return ptaClone(ptaa->pta[index]);
    else
      return (PTA *)ERROR_PTR("invalid accessflag", procName, NULL);
}



/*---------------------------------------------------------------------*
 *                         Pta serialized I/O                          *
 *---------------------------------------------------------------------*/
/*!
 *  ptaWriteStream()
 *
 *      Input:  stream
 *              pta
 *              type  (0 for float values; 1 for integer values)
 *      Return: 0 if OK; 1 on error
 */
l_int32
ptaWriteStream(FILE    *fp,
               PTA     *pta,
             l_int32  type)
{
l_int32    i, n, ix, iy;
l_float32  x, y;

    PROCNAME("ptaWriteStream");

    if (!fp)
      return ERROR_INT("stream not defined", procName, 1);
    if (!pta)
      return ERROR_INT("pta not defined", procName, 1);

    n = ptaGetCount(pta);
    if (type == 0)
      fprintf(fp, "Point array with %d float points\n", n);
    else  /* type == 1 */
      fprintf(fp, "Point array with %d integer points\n", n);
    for (i = 0; i < n; i++) {
      if (type == 0) {  /* data is float */
          ptaGetPt(pta, i, &x, &y);
          fprintf(fp, "   (%f, %f)\n", x, y);
      }
      else {   /* data is integer */
          ptaGetIPt(pta, i, &ix, &iy);
          fprintf(fp, "   (%d, %d)\n", ix, iy);
      }
    }

    return 0;
}


/*!
 *  ptaReadStream()
 *
 *      Input:  stream
 *      Return: pta, or null on error
 */
PTA *
ptaReadStream(FILE  *fp)
{
char       typestr[128];
l_int32    i, n, ix, iy, type;
l_float32  x, y;
PTA       *pta;

    PROCNAME("ptaReadStream");

    if (!fp)
      return (PTA *)ERROR_PTR("stream not defined", procName, NULL);

    if (fscanf(fp, "Point array with %d %s points\n", &n, typestr) != 2)
      return (PTA *)ERROR_PTR("not a pta file", procName, NULL);
    if (strcmp(typestr, "float") == 0)
      type = 0;
    else  /* typestr is "integer" */
      type = 1;

    if ((pta = ptaCreate(n)) == NULL)
      return (PTA *)ERROR_PTR("pta not made", procName, NULL);
    for (i = 0; i < n; i++) {
      if (type == 0) {  /* data is float */
          fscanf(fp, "   (%f, %f)\n", &x, &y);
          ptaAddPt(pta, x, y);
      }
      else {   /* data is integer */
          fscanf(fp, "   (%d, %d)\n", &ix, &iy);
          ptaAddPt(pta, ix, iy);
      }
    }

    return pta;
}



/*---------------------------------------------------------------------*
 *                                In use                               *
 *---------------------------------------------------------------------*/
/*!
 *  pixFindCornerPixels()
 *
 *      Input: pixs (1 bpp)
 *      Return: pta, or null on error
 *
 *  Action: finds the 4 corner-most pixels, as defined by
 *          search inward from each corner
 */
PTA *
pixFindCornerPixels(PIX  *pixs)
{
l_int32    i, j, x, y, w, h, wpl, mindim, found;
l_uint32  *data, *line;
PTA       *pta;

    PROCNAME("pixFindCornerPixels");

    if (!pixs)
      return (PTA *)ERROR_PTR("pixs not defined", procName, NULL);
    if (pixGetDepth(pixs) != 1)
      return (PTA *)ERROR_PTR("pixs not 1 bpp", procName, NULL);

    w = pixGetWidth(pixs);
    h = pixGetHeight(pixs);
    mindim = L_MIN(w, h);
    data = pixGetData(pixs);
    wpl = pixGetWpl(pixs);

    if ((pta = ptaCreate(4)) == NULL)
      return (PTA *)ERROR_PTR("pta not made", procName, NULL);

    for (found = FALSE, i = 0; i < mindim; i++) {
      for (j = 0; j <= i; j++) {
          y = i - j;
          line = data + y * wpl;
          if (GET_DATA_BIT(line, j)) {
            ptaAddPt(pta, j, y);
            found = TRUE;
            break;
          }
      }
      if (found == TRUE)
          break;
    }

    for (found = FALSE, i = 0; i < mindim; i++) {
      for (j = 0; j <= i; j++) {
          y = i - j;
          line = data + y * wpl;
          x = w - 1 - j;
          if (GET_DATA_BIT(line, x)) {
            ptaAddPt(pta, x, y);
            found = TRUE;
            break;
          }
      }
      if (found == TRUE)
          break;
    }

    for (found = FALSE, i = 0; i < mindim; i++) {
      for (j = 0; j <= i; j++) {
          y = h - 1 - i + j;
          line = data + y * wpl;
          if (GET_DATA_BIT(line, j)) {
            ptaAddPt(pta, j, y);
            found = TRUE;
            break;
          }
      }
      if (found == TRUE)
          break;
    }

    for (found = FALSE, i = 0; i < mindim; i++) {
      for (j = 0; j <= i; j++) {
          y = h - 1 - i + j;
          line = data + y * wpl;
          x = w - 1 - j;
          if (GET_DATA_BIT(line, x)) {
            ptaAddPt(pta, x, y);
            found = TRUE;
            break;
          }
      }
      if (found == TRUE)
          break;
    }

    return pta;
}

            
/*!
 *  pixPlotAlongPta()
 *
 *      Input: pixs (any depth)
 *             pta (set of points on which to plot)
 *             outformat (GPLOT_PNG, GPLOT_PS, GPLOT_EPS, GPLOT_X11,
 *                        GPLOT_LATEX)
 *             title (<optional> for plot; can be null)
 *      Return: 0 if OK, 1 on error
 *
 *  Notes:
 *      (1) We remove any existing colormap and clip the pta to the input pixs.
 *      (2) This is a debugging function, and does not remove temporary
 *          plotting files that it generates.
 *      (3) If the image is RGB, three separate plots are generated.
 */
l_int32
pixPlotAlongPta(PIX         *pixs,
                PTA         *pta,
                l_int32      outformat,
                const char  *title)
{
char            buffer[128];
char           *rtitle, *gtitle, *btitle;
static l_int32  count = 0;  /* require separate temp files for each call */
l_int32         i, x, y, d, w, h, npts, rval, gval, bval;
l_uint32        val;
NUMA           *na, *nar, *nag, *nab;
PIX            *pixt;

    PROCNAME("pixPlotAlongLine");

    if (!pixs)
      return ERROR_INT("pixs not defined", procName, 1);
    if (!pta)
      return ERROR_INT("pta not defined", procName, 1);
    if (outformat != GPLOT_PNG && outformat != GPLOT_PS &&
        outformat != GPLOT_EPS && outformat != GPLOT_X11 &&
        outformat != GPLOT_LATEX) {
        L_WARNING("outformat invalid; using GPLOT_PNG", procName);
        outformat = GPLOT_PNG;
    }

    pixt = pixRemoveColormap(pixs, REMOVE_CMAP_BASED_ON_SRC);
    d = pixGetDepth(pixt);
    w = pixGetWidth(pixt);
    h = pixGetHeight(pixt);
    npts = ptaGetCount(pta);
    if (d == 32) {
        nar = numaCreate(npts);
        nag = numaCreate(npts);
        nab = numaCreate(npts);
        for (i = 0; i < npts; i++) {
            ptaGetIPt(pta, i, &x, &y);
            if (x < 0 || x >= w)
                continue;
            if (y < 0 || y >= h)
                continue;
            pixGetPixel(pixt, x, y, &val);
            rval = GET_DATA_BYTE(&val, COLOR_RED);
            gval = GET_DATA_BYTE(&val, COLOR_GREEN);
            bval = GET_DATA_BYTE(&val, COLOR_BLUE);
            numaAddNumber(nar, rval);
            numaAddNumber(nag, gval);
            numaAddNumber(nab, bval);
        }

        sprintf(buffer, "junkplot.%d", count++);
        rtitle = stringJoin("Red: ", title);
        gplotSimple1(nar, outformat, buffer, rtitle);
        sprintf(buffer, "junkplot.%d", count++);
        gtitle = stringJoin("Green: ", title);
        gplotSimple1(nag, outformat, buffer, gtitle);
        sprintf(buffer, "junkplot.%d", count++);
        btitle = stringJoin("Blue: ", title);
        gplotSimple1(nab, outformat, buffer, btitle);
        numaDestroy(&nar);
        numaDestroy(&nag);
        numaDestroy(&nab);
        FREE((void *)rtitle);
        FREE((void *)gtitle);
        FREE((void *)btitle);
    }
    else {
        na = numaCreate(npts);
        for (i = 0; i < npts; i++) {
            ptaGetIPt(pta, i, &x, &y);
            if (x < 0 || x >= w)
                continue;
            if (y < 0 || y >= h)
                continue;
            pixGetPixel(pixt, x, y, &val);
            numaAddNumber(na, (l_float32)val);
        }

        sprintf(buffer, "junkplot.%d", count++);
        gplotSimple1(na, outformat, buffer, title);
        numaDestroy(&na);
    }
    pixDestroy(&pixt);
    return 0;
}


/*!
 *  ptaContainsPt()
 *
 *      Input:  pta
 *              x, y  (point)
 *      Return: 1 if contained, 0 otherwise or on error
 */
l_int32
ptaContainsPt(PTA     *pta,
              l_int32  x,
              l_int32  y)
{
l_int32  i, n, ix, iy;

    PROCNAME("ptaContainsPt");

    if (!pta)
      return ERROR_INT("pta not defined", procName, 0);

    n = ptaGetCount(pta);
    for (i = 0; i < n; i++) {
      ptaGetIPt(pta, i, &ix, &iy);
      if (x == ix && y == iy)
          return 1;
    }
    return 0;
}


/*!
 *  ptaTestIntersection()
 *
 *      Input:  pta1, pta2
 *      Return: bval which is 1 if they have any elements in common; 
 *              0 otherwise or on error.
 */
l_int32
ptaTestIntersection(PTA  *pta1,
                    PTA  *pta2)
{
l_int32  i, j, n1, n2, x1, y1, x2, y2;

    PROCNAME("ptaTestIntersection");

    if (!pta1)
      return ERROR_INT("pta1 not defined", procName, 0);
    if (!pta2)
      return ERROR_INT("pta2 not defined", procName, 0);

    n1 = ptaGetCount(pta1);
    n2 = ptaGetCount(pta2);
    for (i = 0; i < n1; i++) {
      ptaGetIPt(pta1, i, &x1, &y1);
      for (j = 0; j < n2; j++) {
          ptaGetIPt(pta2, i, &x2, &y2);
          if (x1 == x2 && y1 == y2)
            return 1;
      }
    }

    return 0;
}


/*!
 *  ptaGetLinearLSF()
 *
 *      Input:  pta
 *              &a  (<optional return> slope a of least square fit: y = ax + b)
 *              &b  (<optional return> intercept b of least square fit)
 *      Return: 0 if OK, 1 on error
 *
 *  At least one of &a and &b must not be null.
 *  If both &a and &b are defined, this returns a and b that minimize:
 *
 *       sum (yi - axi -b)^2
 *        i
 *
 *  The method is simple: differentiate this expression w/rt a and b,
 *  and solve the resulting two equations for a and b in terms of
 *  various sums over the input data (xi, yi).
 *
 *  We also allow two special cases, where either a = 0 or b = 0:
 *
 *    (1) If &a is given and &b = null, find the linear LSF that
 *        goes through the origin (b = 0).
 *
 *    (2) If &b is given and &a = null, find the linear LSF with
 *        zero slope (a = 0).
 */
l_int32
ptaGetLinearLSF(PTA        *pta,
                l_float32  *pa,
            l_float32  *pb)
{
l_int32     n, i;
l_float32   factor, sx, sy, sxx, sxy;
l_float32  *xa, *ya;

    PROCNAME("ptaGetLinearLSF");

    if (!pta)
      return ERROR_INT("pta not defined", procName, 1);
    if (!pa && !pb)
      return ERROR_INT("&a and/or &b not defined", procName, 1);
    if (pa) *pa = 0.0;
    if (pb) *pb = 0.0;

    if ((n = ptaGetCount(pta)) < 2)
      return ERROR_INT("less than 2 pts not found", procName, 1);
    xa = pta->x;  /* not a copy */
    ya = pta->y;  /* not a copy */

    sx = sy = sxx = sxy = 0.;
    if (pa && pb) {
        for (i = 0; i < n; i++) {
            sx += xa[i];
            sy += ya[i];
            sxx += xa[i] * xa[i];
            sxy += xa[i] * ya[i];
        }
        factor = n * sxx - sx * sx;
        if (factor == 0.0)
            return ERROR_INT("no solution found", procName, 1);
        factor = 1. / factor;

        *pa = factor * ((l_float32)n * sxy - sx * sy);
        *pb = factor * (sxx * sy - sx * sxy);
    }
    else if (pa) {  /* line through origin */
        for (i = 0; i < n; i++) {
            sxx += xa[i] * xa[i];
            sxy += xa[i] * ya[i];
        }
        if (sxx == 0.0)
            return ERROR_INT("no solution found", procName, 1);
        *pa = sxy / sxx;
    }
    else {  /* a = 0; horizontal line */
        for (i = 0; i < n; i++)
            sy += ya[i];
        *pb = sy / (l_float32)n;
    }

    return 0;
}


/*!
 *  ptaGetPixelsFromPix()
 *
 *      Input: pixs (1 bpp)
 *      Return: pta, or null on error
 *
 *  Note: Generates a pta of fg pixels.
 */
PTA *
ptaGetPixelsFromPix(PIX  *pixs)
{
l_int32    i, j, w, h, wpl;
l_uint32  *data, *line;
PTA       *pta;

    PROCNAME("pixGetPixelsFromPix");

    if (!pixs)
      return (PTA *)ERROR_PTR("pixs not defined", procName, NULL);
    if (pixGetDepth(pixs) != 1)
      return (PTA *)ERROR_PTR("pixs not 1 bpp", procName, NULL);

    w = pixGetWidth(pixs);
    h = pixGetHeight(pixs);
    data = pixGetData(pixs);
    wpl = pixGetWpl(pixs);

    if ((pta = ptaCreate(0)) == NULL)
      return (PTA *)ERROR_PTR("pta not made", procName, NULL);
    for (i = 0; i < h; i++) {
        line = data + i * wpl;
      for (j = 0; j < w; j++) {
          if (GET_DATA_BIT(line, j))
              ptaAddPt(pta, j, i);
      }
    }

    return pta;
}


Generated by  Doxygen 1.6.0   Back to index