static bool bpqMaxValueTest() { SPBPQueue source, source2, source3; SPListElement e1, e2, e3, e4; source = spBPQueueCreate(maxSize); ASSERT_TRUE(spBPQueueMinValue(source) == -1); // check edge case // e1, e2, e3, e4 CREATE_4_ELEMENTS() source2 = quickQ(4, e1, e2, e3, e4); source3 = quickQ(4, e4, e3, e1, e2); double max2 = spBPQueueMaxValue(source2); double max3 = spBPQueueMaxValue(source3); // check that min value is 4 in both cases ASSERT_TRUE(max2 == 4.0); ASSERT_TRUE(max3 == 4.0); // free memory spBPQueueDestroy(source); spBPQueueDestroy(source2); spBPQueueDestroy(source3); // e1, e2, e3, e4 DESTROY_4_ELEMENTS() return true; }
/** * internal help method to find the k nearest neighbors */ void spKNNSearch(SPPoint queryFeature, const SPKDTreeNode node, SPBPQueue q){ SPListElement element; int index, distance; bool distanceFlag = false; if(!node){ return; } if(node->dim==INVALID){ //** this is a leaf **// index = spPointGetIndex(node->data); distance = spPointL2SquaredDistance(queryFeature, node->data); element = spListElementCreate(index, distance); spBPQueueEnqueue(q, element); spListElementDestroy(element); return; } //** go to the left sub tree **// if(spPointGetAxisCoor(queryFeature, node->dim)<= node->val){ spKNNSearch(queryFeature, node->left, q); distance = pow((spPointGetAxisCoor(queryFeature, node->dim) - node->val),2); distanceFlag = distance < spBPQueueMaxValue(q); if(!spBPQueueIsFull(q) || distanceFlag){ spKNNSearch(queryFeature, node->right, q); } }else{ spKNNSearch(queryFeature, node->right, q); distance = pow((spPointGetAxisCoor(queryFeature, node->dim) - node->val),2); distanceFlag = distance < spBPQueueMaxValue(q); if(!spBPQueueIsFull(q) || distanceFlag){ spKNNSearch(queryFeature, node->left, q); } } return; }
void SPKDTreeKNNRecursive(SPKDTreeNode treeNode, SPPoint p, SPBPQueue bpq, SP_KDTREE_MSG* msg) { SPListElement listElement; SPPoint treePoint; bool searchedLeft; double dist; if(bpq == NULL || treeNode == NULL) { *msg = SP_KDTREE_INVALID_ARGUMENT; return; } // If treeNode is a leaf if(treeNode->left == NULL && treeNode->right == NULL) { treePoint = *(treeNode->data); listElement = spListElementCreate(spPointGetIndex(treePoint), spPointL2SquaredDistance(p, treePoint)); spBPQueueEnqueue(bpq, listElement); spListElementDestroy(listElement); *msg = SP_KDTREE_SUCCESS; return; } // Turn to search the tree that would've contain the point p (if it was in the tree) if(spPointGetAxisCoor(p, treeNode->dim) <= treeNode->val) { searchedLeft = true; SPKDTreeKNNRecursive(treeNode->left, p, bpq, msg); if (*msg != SP_KDTREE_SUCCESS) return; } else { searchedLeft = false; SPKDTreeKNNRecursive(treeNode->right, p, bpq, msg); if (*msg != SP_KDTREE_SUCCESS) return; } // dist = |treeNode.val - p[treeNode.dim]| dist = treeNode->val - spPointGetAxisCoor(p, treeNode->dim); if(dist < 0) dist *= -1; //dist *= dist; if(!spBPQueueIsFull(bpq) || dist < spBPQueueMaxValue(bpq)) { if(searchedLeft) SPKDTreeKNNRecursive(treeNode->right, p, bpq, msg); else SPKDTreeKNNRecursive(treeNode->left, p, bpq, msg); } }
/** * Given a kd-tree and a point p, the function stores the nearest neighbors of p to bpq * * @param curr - the kd-tree containing the points * @param bpq - the bounded priority queue to store the nearest neighbors in * @param p - the point to find the nearest neighbors to * * does nothing if curr == NULL or bpq == NULL or p == NULL */ void nearestNeighbors(KDTreeNode curr, SPBPQueue bpq, SPPoint p) { SPListElement node; SPPoint q; bool isLeft; double coorDis; if (curr == NULL || bpq == NULL || p == NULL ) return; q = curr->data; /* Add the current point to the BPQ. Note that this is a no-op if the * point is not as good as the points we've seen so far.*/ if (curr->dim == -1) { int index; double dis; index = spPointGetIndex(q); dis = spPointL2SquaredDistance(p, curr->data); node = spListElementCreate(index, dis); spBPQueueEnqueue(bpq, node); spListElementDestroy(node); return; } /* Recursively search the half of the tree that contains the test point. */ if (spPointGetAxisCoor(p, curr->dim) <= curr->val) { nearestNeighbors(curr->left, bpq, p); isLeft = true; } else { nearestNeighbors(curr->right, bpq, p); isLeft = false; } /* If the candidate hypersphere crosses this splitting plane, look on the * other side of the plane by examining the other subtree*/ coorDis = abs(spPointGetAxisCoor(p, curr->dim) - curr->val); if (!spBPQueueIsFull(bpq) || coorDis*coorDis < spBPQueueMaxValue(bpq)) { if (isLeft) nearestNeighbors(curr->right, bpq, p); else nearestNeighbors(curr->left, bpq, p); } }
static bool bpqEnqueueTest() { SPBPQueue source, source2; SPListElement e, e1, e2, e3, e4, e5, peek, peekLast; ASSERT_TRUE(SP_BPQUEUE_INVALID_ARGUMENT == spBPQueueEnqueue(NULL, NULL)); // check edge case CREATE_4_ELEMENTS() // e1, e2, e3, e4 e5 = spListElementCreate(5, 4.0); source = quickQ(3, e2, e1, e4); ASSERT_TRUE(SP_BPQUEUE_SUCCESS == spBPQueueEnqueue(source, e3)); // check that enqueue succeeded ASSERT_TRUE(SP_BPQUEUE_SUCCESS == spBPQueueEnqueue(source, e5)); ASSERT_TRUE(5 == spBPQueueSize(source)); // check that enqueue inserts in order peek = spBPQueuePeek(source); peekLast = spBPQueuePeekLast(source); ASSERT_TRUE(spListElementCompare(e1, peek) == 0); // make sure queue sorts by value and then by index ASSERT_TRUE(spListElementCompare(e5, peekLast) == 0); // e1, e2, e3, e4 DESTROY_4_ELEMENTS() spListElementDestroy(e5); // create new queue with maxSize source2 = spBPQueueCreate(maxSize); // insert 2*maxSize elements from lowest to highest value and check that min and max are correct for (int i = 0; i < maxSize; i++) { e = spListElementCreate(i, (double) i); ASSERT_TRUE(SP_BPQUEUE_SUCCESS == spBPQueueEnqueue(source2, e)); spListElementDestroy(e); } for (int i = maxSize; i < 2 * maxSize; i++) { e = spListElementCreate(i, (double) i); ASSERT_TRUE(SP_BPQUEUE_FULL == spBPQueueEnqueue(source2, e)); // check full when inserting more then maxSize elements spListElementDestroy(e); } ASSERT_TRUE(spBPQueueMinValue(source2) == 0.0); // check that all elements with value too high are not in the queue ASSERT_TRUE((int )spBPQueueMaxValue(source2) == maxSize - 1); spBPQueueClear(source2); // insert 2*maxSize elements from highest to lowest value and check that min and max are correct and same as before for (int i = 2 * maxSize - 1; i >= 0; i--) { e = spListElementCreate(i, (double) i); spBPQueueEnqueue(source2, e); spListElementDestroy(e); } // check min value is correct ASSERT_TRUE(spBPQueueMinValue(source2) == 0.0); ASSERT_TRUE((int )spBPQueueMaxValue(source2) == maxSize - 1); // free memory spBPQueueDestroy(source); spBPQueueDestroy(source2); spListElementDestroy(peek); spListElementDestroy(peekLast); return true; }