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 elNew(el_t *ns, size_t len, bor_list_t *head) { size_t i; bor_real_t x, y; borListInit(head); for (i = 0; i < len; i++){ x = borRand(&r, -10., 10.); y = borRand(&r, -10., 10.); borVec2Set(&ns[i].v, x, y); borGUGElInit(&ns[i].c, (const bor_vec_t *)&ns[i].v); borListAppend(head, &ns[i].list); } }
static void _nnAddRm(uint8_t type, bor_nn_params_t *params) { bor_rand_mt_t *rand; bor_nn_t *nn; static bor_list_t els_list; static int els_len = ADD_ELS_LEN; static el_t els[ADD_ELS_LEN]; int i, j; rand = borRandMTNewAuto(); params->type = type; nn = borNNNew(params); borListInit(&els_list); for (i = 0; i < els_len; i++){ borVec2Set(&els[i].w, borRandMT(rand, -3, 3), borRandMT(rand, -3, 3)); borNNElInit(nn, &els[i].el, (const bor_vec_t *)&els[i].w); borListAppend(&els_list, &els[i].list); } for (i = 0; i < ADD_ELS_LEN; i++){ //fprintf(stdout, "%02d:\n", i); borNNAdd(nn, &els[i].el); //borNNDump(vp, stdout); } for (i = 0; i < ADD_ELS_LEN; i += 3){ borNNRemove(nn, &els[i].el); borListDel(&els[i].list); } for (i = 0; i < ADD_NUM_TESTS; i++){ for (j = 1; j <= ADD_NUM_NNS; j++){ test2(rand, nn, &els_list, j); } } borNNDel(nn); borRandMTDel(rand); }
static el_t *elsNew(size_t len) { size_t i; bor_real_t x, y; el_t *ns; ns = BOR_ALLOC_ARR(el_t, arr_len); for (i = 0; i < len; i++){ x = borRand(&r, -15., 15.); y = borRand(&r, -20., 20.); borVec2Set(&ns[i].v, x, y); borNNElInit(linear, &ns[i].linear, (const bor_vec_t *)&ns[i].v); borNNElInit(gug, &ns[i].gug, (const bor_vec_t *)&ns[i].v); borNNElInit(vp, &ns[i].vp, (const bor_vec_t *)&ns[i].v); borNNAdd(linear, &ns[i].linear); borNNAdd(gug, &ns[i].gug); borNNAdd(vp, &ns[i].vp); } return ns; }
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); }