static void test2(bor_rand_mt_t *rand, bor_nn_t *n, bor_list_t *list, size_t num) { bor_nn_el_t *nn[10]; bor_list_t *nn2[10]; el_t *el, *el2; bor_vec2_t p; size_t len, len2, i; borVec2Set(&p, borRandMT(rand, -3, 3), borRandMT(rand, -3, 3)); len = borNNNearest(n, (const bor_vec_t *)&p, num, nn); len2 = borNearestLinear(list, (void *)&p, dist2, nn2, num, NULL); assertEquals(len, num); assertEquals(len2, num); for (i = 0; i < num; i++){ el = bor_container_of(nn[i], el_t, el); el2 = bor_container_of(nn2[i], el_t, list); if (el == el2){ assertEquals(el, el2); }else{ fprintf(stderr, "%.30f %.30f [%.30f] - %.30f %.30f [%.30f]\n", borVec2X(&el->w), borVec2Y(&el->w), borVec2Dist(&el->w, &p), borVec2X(&el2->w), borVec2Y(&el2->w), borVec2Dist(&el2->w, &p)); } } }
static void testCorrect(void) { bor_nn_params_t params; bor_real_t range[4] = { -15., 15., -18., 17. }; el_t *ns, *near[3]; bor_nn_el_t *el_linear[50], *el_gug[50], *el_vp[50]; int i, j, k; int len_linear, len_gug, len_vp; int incorrect; bor_vec2_t v; borNNParamsInit(¶ms); borNNParamsSetDim(¶ms, 2); params.gug.num_cells = 0; params.gug.max_dens = 1; params.gug.expand_rate = 2.; params.gug.aabb = range; params.type = BOR_NN_LINEAR; linear = borNNNew(¶ms); params.type = BOR_NN_GUG; gug = borNNNew(¶ms); params.type = BOR_NN_VPTREE; vp = borNNNew(¶ms); ns = elsNew(arr_len); for (k = 0; k < nearest_len; k++){ incorrect = 0; for (i=0; i < loops; i++){ fprintf(stderr, "[%d] %08d / %08d\r", (int)k, (int)i, (int)loops); borVec2Set(&v, borRand(&r, -10., 10.), borRand(&r, -10, 10)); len_linear = borNNNearest(linear, (const bor_vec_t *)&v, k + 1, el_linear); len_gug = borNNNearest(gug, (const bor_vec_t *)&v, k + 1, el_gug); len_vp = borNNNearest(vp, (const bor_vec_t *)&v, k + 1, el_vp); if (len_linear != len_gug || len_linear != len_vp || len_vp != len_gug || len_linear != k + 1){ incorrect = 1; } for (j = 0; j < k + 1; j++){ near[0] = bor_container_of(el_linear[j], el_t, linear); near[1] = bor_container_of(el_gug[j], el_t, gug); near[2] = bor_container_of(el_vp[j], el_t, vp); if (near[0] != near[1] || near[0] != near[2] || near[1] != near[2]){ incorrect = 1; } } } if (incorrect){ fprintf(stderr, "[%d] %08d / %08d FAIL\n", (int)k, (int)i, (int)loops); }else{ fprintf(stderr, "[%d] %08d / %08d OK\n", (int)k, (int)i, (int)loops); } } BOR_FREE(ns); borNNDel(linear); borNNDel(gug); borNNDel(vp); }
static void bench(void) { bor_nn_params_t params; bor_real_t range[4] = { -15., 15., -18., 17. }; el_t *ns, *near; bor_nn_el_t *el[50]; int i, j, k; bor_vec2_t v; int devnull; bor_timer_t timer; devnull = open("/dev/null", O_WRONLY); if (devnull < 0){ perror("Error: "); return; } borNNParamsInit(¶ms); borNNParamsSetDim(¶ms, 2); params.gug.num_cells = 0; params.gug.max_dens = 1; params.gug.expand_rate = 2.; params.gug.aabb = range; params.type = BOR_NN_LINEAR; linear = borNNNew(¶ms); params.type = BOR_NN_GUG; gug = borNNNew(¶ms); params.type = BOR_NN_VPTREE; vp = borNNNew(¶ms); ns = elsNew(arr_len); for (k = 0; k < nearest_len; k++){ borTimerStart(&timer); for (i=0; i < loops; i++){ //fprintf(stderr, "[%d] %08d / %08d\r", (int)k, (int)i, (int)loops); borVec2Set(&v, borRand(&r, -10., 10.), borRand(&r, -10, 10)); borNNNearest(linear, (const bor_vec_t *)&v, k + 1, el); for (j = 0; j < k + 1; j++){ near = bor_container_of(el[j], el_t, linear); write(devnull, &near->v, 1); } } borTimerStop(&timer); borTimerPrintElapsed(&timer, stderr, " - [%d] - linear - \n", k); borTimerStart(&timer); for (i=0; i < loops; i++){ //fprintf(stderr, "[%d] %08d / %08d\r", (int)k, (int)i, (int)loops); borVec2Set(&v, borRand(&r, -10., 10.), borRand(&r, -10, 10)); borNNNearest(gug, (const bor_vec_t *)&v, k + 1, el); for (j = 0; j < k + 1; j++){ near = bor_container_of(el[j], el_t, gug); write(devnull, &near->v, 1); } } borTimerStop(&timer); borTimerPrintElapsed(&timer, stderr, " - [%d] - gug - \n", k); borTimerStart(&timer); for (i=0; i < loops; i++){ //fprintf(stderr, "[%d] %08d / %08d\r", (int)k, (int)i, (int)loops); borVec2Set(&v, borRand(&r, -10., 10.), borRand(&r, -10, 10)); borNNNearest(vp, (const bor_vec_t *)&v, k + 1, el); for (j = 0; j < k + 1; j++){ near = bor_container_of(el[j], el_t, vp); write(devnull, &near->v, 1); } } borTimerStop(&timer); borTimerPrintElapsed(&timer, stderr, " - [%d] - vptree - \n", k); } BOR_FREE(ns); borNNDel(linear); borNNDel(gug); borNNDel(vp); close(devnull); }