IPP Software Navigation Tools IPP Links Communication Pan-STARRS Links

Changeset 18344


Ignore:
Timestamp:
Jun 27, 2008, 10:27:45 AM (18 years ago)
Author:
Paul Price
Message:

Allow input coordinates to be of any type.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/psLib/src/types/psTree.c

    r18335 r18344  
    368368    PS_ASSERT_TREE_NON_NULL(tree, NULL);
    369369    PS_ASSERT_VECTOR_NON_NULL(coords, NULL);
    370     PS_ASSERT_VECTOR_TYPE(coords, PS_TYPE_F64, NULL);
    371370    PS_ASSERT_VECTOR_SIZE(coords, (long)tree->dim, NULL);
    372371#endif
    373372
     373#define TREE_LEAF_CASE(TYPE) \
     374  case PS_TYPE_##TYPE: \
     375      for (; node->left; node = (coords->data.TYPE[node->divideDim] < tree->division[node->index]) ? \
     376               node->left : node->right); \
     377      break;
     378
    374379    psTreeNode *node = tree->root;      // Node of interest
    375     for (; node->left; node = coords->data.F64[node->divideDim] < tree->division[node->index] ?
    376              node->left : node->right);
     380    switch (coords->type.type) {
     381        TREE_LEAF_CASE(U8);
     382        TREE_LEAF_CASE(U16);
     383        TREE_LEAF_CASE(U32);
     384        TREE_LEAF_CASE(U64);
     385        TREE_LEAF_CASE(S8);
     386        TREE_LEAF_CASE(S16);
     387        TREE_LEAF_CASE(S32);
     388        TREE_LEAF_CASE(S64);
     389        TREE_LEAF_CASE(F32);
     390        TREE_LEAF_CASE(F64);
     391      default:
     392        psError(PS_ERR_BAD_PARAMETER_TYPE, true, "Unsupported type: %x", coords->type.type);
     393        return NULL;
     394    }
    377395    return node;
    378396}
     
    464482// This is the engine for psTreeNearest() and psTreeNearestWithin()
    465483static inline long treeNearestWithin(const psTree *tree, // Tree
    466                                      const psVector *coords, // Coordinates of interest
     484                                     const psVector *coordinates, // Coordinates of interest
    467485                                     double bestDistance // Distance (radius-squared) to best point
    468486    )
     
    470488#if 1 // Might be in a tight loop
    471489    PS_ASSERT_TREE_NON_NULL(tree, -1);
    472     PS_ASSERT_VECTOR_NON_NULL(coords, -1);
    473     PS_ASSERT_VECTOR_TYPE(coords, PS_TYPE_F64, -1);
    474     PS_ASSERT_VECTOR_SIZE(coords, (long)tree->dim, -1);
     490    PS_ASSERT_VECTOR_NON_NULL(coordinates, -1);
     491    PS_ASSERT_VECTOR_SIZE(coordinates, (long)tree->dim, -1);
    475492#endif
     493
     494    psVector *coords = (coordinates->type.type == PS_TYPE_F64 ? psMemIncrRefCounter((psVector*)coordinates) :
     495                        psVectorCopy(NULL, coordinates, PS_TYPE_F64)); // F64 version of coordinates
    476496
    477497    // Find the closest point in the leaf that contains the point of interest
     
    513533    }
    514534    psFree(work);
     535    psFree(coords);
    515536
    516537    return bestIndex;
     
    549570
    550571// Given an arbitrary point and a matching radius, return the number of points within that radius
    551 long psTreeWithin(const psTree *tree, const psVector *coords, double radius)
     572long psTreeWithin(const psTree *tree, const psVector *coordinates, double radius)
    552573{
    553574#if 1 // Might be in a tight loop
    554575    PS_ASSERT_TREE_NON_NULL(tree, -1);
    555     PS_ASSERT_VECTOR_NON_NULL(coords, -1);
    556     PS_ASSERT_VECTOR_TYPE(coords, PS_TYPE_F64, -1);
    557     PS_ASSERT_VECTOR_SIZE(coords, (long)tree->dim, -1);
     576    PS_ASSERT_VECTOR_NON_NULL(coordinates, -1);
     577    PS_ASSERT_VECTOR_SIZE(coordinates, (long)tree->dim, -1);
    558578#endif
     579
     580    psVector *coords = (coordinates->type.type == PS_TYPE_F64 ? psMemIncrRefCounter((psVector*)coordinates) :
     581                        psVectorCopy(NULL, coordinates, PS_TYPE_F64)); // F64 version of coordinates
    559582
    560583    radius *= radius;                   // We work with the radius-squared
     
    590613    }
    591614    psFree(work);
     615    psFree(coords);
    592616
    593617    return num;
     
    612636
    613637// Given an arbitrary point and a matching radius, return whether there are any points within that radius
    614 bool psTreeWithinAny(const psTree *tree, const psVector *coords, double radius)
     638bool psTreeWithinAny(const psTree *tree, const psVector *coordinates, double radius)
    615639{
    616640#if 1 // Might be in a tight loop
    617641    PS_ASSERT_TREE_NON_NULL(tree, false);
    618     PS_ASSERT_VECTOR_NON_NULL(coords, false);
    619     PS_ASSERT_VECTOR_TYPE(coords, PS_TYPE_F64, false);
    620     PS_ASSERT_VECTOR_SIZE(coords, (long)tree->dim, false);
     642    PS_ASSERT_VECTOR_NON_NULL(coordinates, false);
     643    PS_ASSERT_VECTOR_SIZE(coordinates, (long)tree->dim, false);
    621644#endif
     645
     646    psVector *coords = (coordinates->type.type == PS_TYPE_F64 ? psMemIncrRefCounter((psVector*)coordinates) :
     647                        psVectorCopy(NULL, coordinates, PS_TYPE_F64)); // F64 version of coordinates
    622648
    623649    radius *= radius;                   // We work with the radius-squared
     
    628654    psTreeNode *leaf = psTreeLeaf(tree, coords); // Leaf containing the point of interest
    629655    if (treeLeafSearchWithinAny(radius, tree, leaf, coords)) {
     656        psFree(coords);
    630657        return true;
    631658    }
     
    650677                memset(work->data, 0, (workIndex + 1) * sizeof(void));
    651678                psFree(work);
     679                psFree(coords);
    652680                return true;
    653681            }
     
    659687    }
    660688    psFree(work);
     689    psFree(coords);
    661690
    662691    return false;
Note: See TracChangeset for help on using the changeset viewer.