IPP Software Navigation Tools IPP Links Communication Pan-STARRS Links

Ignore:
Timestamp:
Apr 21, 2005, 11:18:23 AM (21 years ago)
Author:
desonia
Message:

added psPixels functions and rewrote psVectorSortIndex.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/psLib/src/collections/psVector.c

    r3684 r3737  
    99*  @author Robert DeSonia, MHPCC
    1010*
    11 *  @version $Revision: 1.38 $ $Name: not supported by cvs2svn $
    12 *  @date $Date: 2005-04-08 17:58:57 $
     11*  @version $Revision: 1.39 $ $Name: not supported by cvs2svn $
     12*  @date $Date: 2005-04-21 21:18:23 $
    1313*
    1414*  Copyright 2004-2005 Maui High Performance Computing Center, University of Hawaii
     
    2828#include "psCollectionsErrors.h"
    2929
     30typedef struct
     31{
     32    p_psVectorData data; // need this first for psVectorSortIndex to work.
     33    int index;
     34}
     35indexedVector;
     36
    3037static void vectorFree(psVector* psVec);
    31 
    3238
    3339static void vectorFree(psVector* psVec)
     
    297303                PS_ERRORTEXT_psVector_UNSUPPORTED_TYPE,
    298304                inType);
     305        psFree(outVector);
     306        return NULL;
    299307    }
    300308
     
    305313{
    306314    psS32 N = 0;
    307     psVector* tmpVector = NULL;
    308     psVector* checkVector = NULL;
    309315    psElemType inType = 0;
    310     psU32* outVec;
    311316
    312317    if (inVector == NULL) {
     
    320325    N = inVector->n;
    321326
    322     if (outVector == NULL) {
    323         outVector = psVectorAlloc(N, PS_TYPE_U32);
    324     }
    325 
    326     // check to see if output vector needs to be resized/retyped
    327     if ( (N > outVector->nalloc) ||
    328             (outVector->type.type != PS_TYPE_U32) ) {
    329         // reshape the output vector to match the input vector's size/type.
    330         outVector = psVectorRecycle(outVector,N,PS_TYPE_U32);
    331     }
    332     outVector->n = N;
    333     outVec = outVector->data.U32;
    334 
    335327    if (N == 0) {
    336328        // no need to sort anything, as there are no elements in input vector.
     
    338330    }
    339331
    340     tmpVector = psVectorSort(tmpVector, inVector);
    341 
    342     checkVector = psVectorAlloc(N,PS_TYPE_U8);
    343     for(psS32 k=0; k<N; k++) {
    344         checkVector->data.U8[k] = 0;
    345     }
    346 
    347     #define SORT_INDICES(TYPE,absfcn,maxError) {                              \
    348         ps##TYPE* inVec = inVector->data.TYPE;                                \
    349         ps##TYPE* tmpVec = tmpVector->data.TYPE;                              \
    350         ps##TYPE  diff;                                                       \
    351         for(psS32 i=0; i<N; i++) {                                              \
    352             for(psS32 j=0; j<N; j++) {                                          \
    353                 if(checkVector->data.U8[j] == 0 ) {                                    \
    354                     diff = absfcn(tmpVec[i] - inVec[j]);                          \
    355                     if(diff < maxError) {                                         \
    356                         outVec[i] = j;                                            \
    357                         checkVector->data.U8[j] = 1; \
    358                         break;                                                    \
    359                     }                                                             \
    360                 } \
    361             }                                                                 \
    362         }                                                                     \
    363     }
    364 
    365     // Sort output vector
     332    // ok, let's create a temporary indexed vector
     333    indexedVector* idxVector = psAlloc(sizeof(indexedVector)*N);
     334    int elSize = PSELEMTYPE_SIZEOF(inType);
     335    for (int i = 0; i < N; i++) {
     336        idxVector[i].data.U8 = inVector->data.U8+i*elSize;
     337        idxVector[i].index = i;
     338    }
     339
     340    // Sort indexed vector
     341    // n.b., since first element in indexedVector is a pointer to the data,
     342    // we can use the 'Ptr' version of the standard compare functions
    366343    switch (inType) {
    367344    case PS_TYPE_U8:
    368         SORT_INDICES(U8,abs,1);
     345        qsort(idxVector, N, sizeof(indexedVector), (psCompareFcn)psCompareU8Ptr);
    369346        break;
    370347    case PS_TYPE_U16:
    371         SORT_INDICES(U16,abs,1);
     348        qsort(idxVector, N, sizeof(indexedVector), (psCompareFcn)psCompareU16Ptr);
    372349        break;
    373350    case PS_TYPE_U32:
    374         SORT_INDICES(U32,abs,1);
     351        qsort(idxVector, N, sizeof(indexedVector), (psCompareFcn)psCompareU32Ptr);
    375352        break;
    376353    case PS_TYPE_U64:
    377         SORT_INDICES(U64,abs,1);
     354        qsort(idxVector, N, sizeof(indexedVector), (psCompareFcn)psCompareU64Ptr);
    378355        break;
    379356    case PS_TYPE_S8:
    380         SORT_INDICES(S8,abs,1);
     357        qsort(idxVector, N, sizeof(indexedVector), (psCompareFcn)psCompareS8Ptr);
    381358        break;
    382359    case PS_TYPE_S16:
    383         SORT_INDICES(S16,abs,1);
     360        qsort(idxVector, N, sizeof(indexedVector), (psCompareFcn)psCompareS16Ptr);
    384361        break;
    385362    case PS_TYPE_S32:
    386         SORT_INDICES(S32,abs,1);
     363        qsort(idxVector, N, sizeof(indexedVector), (psCompareFcn)psCompareS32Ptr);
    387364        break;
    388365    case PS_TYPE_S64:
    389         SORT_INDICES(S64,abs,1);
     366        qsort(idxVector, N, sizeof(indexedVector), (psCompareFcn)psCompareS64Ptr);
    390367        break;
    391368    case PS_TYPE_F32:
    392         SORT_INDICES(F32,fabsf,FLT_EPSILON);
     369        qsort(idxVector, N, sizeof(indexedVector), (psCompareFcn)psCompareF32Ptr);
    393370        break;
    394371    case PS_TYPE_F64:
    395         SORT_INDICES(F64,fabs,DBL_EPSILON);
     372        qsort(idxVector, N, sizeof(indexedVector), (psCompareFcn)psCompareF64Ptr);
    396373        break;
    397374    default:
     
    399376                PS_ERRORTEXT_psVector_UNSUPPORTED_TYPE,
    400377                inType);
     378        psFree(idxVector);
     379        psFree(outVector);
     380        return NULL;
     381    }
     382
     383    // extract the indices to the output vector
     384    outVector = psVectorRecycle(outVector, N, PS_TYPE_U32);
     385    psU32* outData = outVector->data.U32;
     386    for (int i = 0; i < N; i++) {
     387        outData[i] = idxVector[i].index;
    401388    }
    402389
    403390    // Free temp memory
    404     psFree(tmpVector);
    405     psFree(checkVector);
     391    psFree(idxVector);
    406392
    407393    return outVector;
Note: See TracChangeset for help on using the changeset viewer.