void test_bsp__tree_can_produce_triangles_from_quads(void) { klist_t(poly) *quad = kl_init(poly); // I'll make a quad by making a triangle with a missing // vertex, then pushing the extra vertex after and recomputing float3 quad_verts[] = {{-1.0, 1.0, 0.0}, {-1.0, -1.0, 0.0}, {1.0, -1.0, 0.0}, {1.0, 1.0, 0.0}}; poly_t *poly = poly_make_triangle(quad_verts[0], quad_verts[1], quad_verts[2]); cl_assert_(poly != NULL, "Can't make triangle for test"); poly_push_vertex(poly, quad_verts[3]); cl_assert_(poly_vertex_count(poly) == 4, "Failed to add vertex into quad"); poly_update(poly); // Build a tree of the quad klist_t(poly) *lpoly = kl_init(poly); *kl_pushp(poly, lpoly) = poly; bsp_node_t *quad_bsp = bsp_build(NULL, lpoly, 1); cl_assert(quad_bsp); klist_t(poly) *tris = bsp_to_polygons(quad_bsp, 1, NULL); cl_assert_equal_i(tris->size, 2); kl_destroy(poly, quad); kl_destroy(poly, lpoly); if(tris) kl_destroy(poly, tris); }
void test_bsp__cube_bsp_can_return_poly_list_of_equal_length(void) { // We test that when we get a list of polygons from a BSP tree of a cube // and assert that we have the same number of polygons as when we started. // // A cube is nice here because no faces need to be split, so polygon // counts before and after remain the same. char cube_path[] = CLAR_FIXTURE_PATH "cube.stl"; stl_object *stl_cube = stl_read_file(cube_path, 0); klist_t(poly) *cube_polys = kl_init(poly); cl_assert(stl_cube != NULL); cl_assert(stl_cube->facet_count > 0); for(int i = 0; i < stl_cube->facet_count; i++) { stl_facet *face = &stl_cube->facets[i]; poly_t *poly = poly_make_triangle(face->vertices[0], face->vertices[1], face->vertices[2]); *kl_pushp(poly, cube_polys) = poly; cl_assert(poly); } bsp_node_t *cube_bsp = alloc_bsp_node(); cl_assert(cube_bsp != NULL); cl_assert(bsp_build(cube_bsp, cube_polys, 1) == cube_bsp); klist_t(poly) *results = bsp_to_polygons(cube_bsp, 0, NULL); cl_assert_equal_i(results->size, stl_cube->facet_count); if(stl_cube) stl_free(stl_cube); if(results) kl_destroy(poly, results); kl_destroy(poly, cube_polys); }
void test_bsp__tree_can_clip_tree(void) { klist_t(poly) *polys = kl_init(poly); float3 tr1[] = {{-0.2, 0.0, 0.0}, {0.2, 0.0, 0.0}, {0.0, 0.2, 0.0}}; float3 tr2[] = {{-0.2, 0.0, 100.0}, {0.2, 0.0, 100.0}, {0.0, 0.2, 1000.0}}; *kl_pushp(poly, polys) = poly_make_triangle(tr1[0], tr1[1], tr1[2]); *kl_pushp(poly, polys) = poly_make_triangle(tr2[0], tr2[1], tr2[2]); cl_assert_equal_i(polys->size, 2); bsp_node_t *tr_bsp = bsp_build(NULL, polys, 1); cl_assert(tr_bsp != NULL); cl_assert(bsp_clip(tr_bsp, cube_bsp) != NULL); klist_t(poly) *clipped = bsp_to_polygons(tr_bsp, 0, NULL); cl_assert_equal_i(clipped->size, 1); // Make sure we clipped the poly inside the cube, and kept // the poly outside float3 *v = &kl_val(kl_begin(clipped))->vertices[0]; cl_assert_((*v)[2] >= 99.0, "Should have kept the vertex outside the cube, not inside."); kl_destroy(poly, polys); }
// This is called when the headers are received so we can look for a message waiting for // this person, or leave them connected until one comes, or time them out after 50s maybe? void receivedHeaders(clientStatus *thisClient) { printf ("Connected by >%s<\r\n", thisClient->clientId); // Check to see if there's a message queued for this person // if so, send it and drop the connection khiter_t q = kh_get(queue, queue, (char*)thisClient->clientId); if (q != kh_end(queue)) { char *queuedMessage; kl_shift(messages, kh_value(queue,q), &queuedMessage); // Now send the message to the person and close snprintf(httpResponse, HTTP_RESPONSE_SIZE, HTTP_TEMPLATE, (int)strlen(queuedMessage), queuedMessage); // Compose the response message free(queuedMessage); write(thisClient->io.fd, httpResponse, strlen(httpResponse)); // Send it closeConnectionSkipHash((ev_io*)thisClient); // If that was the last one, free the list and remove it from the hash if (!kh_value(queue, q)->head->next) { kl_destroy(messages, kh_value(queue, q)); // Free the list free((void*)kh_key(queue, q)); // Free the key (the client id) kh_del(queue, queue, q); // Remove this client id from the hash } } else { // If there's no message, then add their client id to the hash for later int ret; khiter_t k = kh_put(clientStatuses, clientStatuses, thisClient->clientId, &ret); kh_value(clientStatuses, k) = thisClient; } }
void test_bsp__tree_can_clone(void) { bsp_node_t *cube_clone = clone_bsp_tree(cube_bsp); cl_assert(cube_clone != NULL); klist_t(poly) *clone_polys = bsp_to_polygons(cube_clone, 0, NULL); klist_t(poly) *orig_polys = bsp_to_polygons(cube_bsp, 0, NULL); cl_assert_equal_i(clone_polys->size, orig_polys->size); kl_destroy(poly, clone_polys); kl_destroy(poly, orig_polys); cl_assert(clone_polys != orig_polys); free_bsp_tree(cube_clone); }
void test_bsp__cube_can_invert(void) { float3 point = {0.0, 0.0, 0.5}; kliter_t(poly) *iter = NULL; // Make sure that all polygons consider the point in the center // behind them. klist_t(poly) *polys = bsp_to_polygons(cube_bsp, 0, NULL); for(iter = kl_begin(polys); iter < kl_end(polys); iter = kl_next(iter)) { cl_assert_equal_i(poly_classify_vertex(kl_val(iter), point), BACK); } kl_destroy(poly, polys); // INVERT cl_assert(bsp_invert(cube_bsp) == cube_bsp); // Repeat the test and expect the center to now be FRONT polys = bsp_to_polygons(cube_bsp, 0, NULL); for(iter = kl_begin(polys); iter < kl_end(polys); iter = kl_next(iter)) { cl_assert_equal_i(poly_classify_vertex(kl_val(iter), point), FRONT); } kl_destroy(poly, polys); }
void test_bsp__initialize(void) { bsp = alloc_bsp_node(); poly_t *poly = NULL; polygons = kl_init(poly); float3 fs[] = {{-0.5, 0.0, 0.0}, {0.5, 0.0, 0.0}, {0.0, 0.5, 0.0}}; poly = poly_make_triangle(fs[0], fs[1], fs[2]); cl_assert_(poly != NULL, "Out of memory"); *kl_pushp(poly, polygons) = poly; float3 fs2[] = {{-0.5, 0.0, -0.5}, {0.5, 0.0, -0.5}, {0.0, 0.5, -0.5}}; poly = poly_make_triangle(fs2[0], fs2[1], fs2[2]); cl_assert_(poly != NULL, "Out of memory"); *kl_pushp(poly, polygons) = poly; float3 fs3[] = {{-0.5, 0.0, 0.5}, {0.5, 0.0, 0.5}, {0.0, 0.5, 0.5}}; poly = poly_make_triangle(fs3[0], fs3[1], fs3[2]); cl_assert_(poly != NULL, "Out of memory"); *kl_pushp(poly, polygons) = poly; float3 fs4[] = {{0.0, 0.0, -1.0}, {0.0, 0.0, 1.0}, {0.0, 1.0, 0.0}}; poly = poly_make_triangle(fs4[0], fs4[1], fs4[2]); cl_assert_(poly != NULL, "Out of memory"); *kl_pushp(poly, polygons) = poly; cl_assert_(bsp != NULL, "Out of memory"); cl_assert_(bsp_build(bsp, polygons, 1) != NULL, "Failed to build bsp tree"); stl_object *stl_cube = stl_read_file(cube_stl_file, 1); cl_assert(stl_cube != NULL); klist_t(poly) *cube_polys = kl_init(poly); for(int i = 0; i < stl_cube->facet_count; i++) { poly_t *p = poly_make_triangle(stl_cube->facets[i].vertices[0], stl_cube->facets[i].vertices[1], stl_cube->facets[i].vertices[2]); cl_assert(p != NULL); *kl_pushp(poly, cube_polys) = p; } cube_bsp = bsp_build(NULL, cube_polys, 1); kl_destroy(poly, cube_polys); cl_assert(cube_bsp != NULL); }
static char *kl_test_update(void) { int v1 = 5; int v2 = 6; int *v; kl *klist = kl_create(); kl_insert(klist, 9, &v1); kl_insert(klist, 9, &v2); v = kl_find(klist, 9); mu_assert("updating list with one element fails", *v == v2); kl_destroy(klist); return 0; }
void loop_close(Loop *loop, bool wait) { uv_mutex_destroy(&loop->mutex); uv_close((uv_handle_t *)&loop->children_watcher, NULL); uv_close((uv_handle_t *)&loop->children_kill_timer, NULL); uv_close((uv_handle_t *)&loop->poll_timer, NULL); uv_close((uv_handle_t *)&loop->async, NULL); do { uv_run(&loop->uv, wait ? UV_RUN_DEFAULT : UV_RUN_NOWAIT); } while (uv_loop_close(&loop->uv) && wait); multiqueue_free(loop->fast_events); multiqueue_free(loop->thread_events); multiqueue_free(loop->events); kl_destroy(WatcherPtr, loop->children); }
int main(int argc, const char* argv[]) { if(kl_initialize(KL_FALSE, KL_TRUE, "example/main.lua", argc, argv) == KL_SUCCESS) { // Send the script a test event kl_script_event_t fooevt; fooevt.event.id = kl_register_script_event("TestEvent"); fooevt.event.context.as_ptr = NULL; fooevt.event.arg.as_uint32 = 42; kl_script_event_enqueue(KL_DEFAULT_SCRIPT_CONTEXT, &fooevt); while(kl_mainloop_iteration() == KL_SUCCESS) ; kl_destroy(); } #ifdef WIN32 printf("Press any key to continue..."); getchar(); #endif return 0; }
void test_polygon__cleanup(void) { if(faces) kl_destroy(poly, faces); }
static void trans_tbl_init(bam_hdr_t* out, bam_hdr_t* translate, trans_tbl_t* tbl, bool merge_rg, bool merge_pg) { tbl->n_targets = translate->n_targets; tbl->tid_trans = (int*)calloc(translate->n_targets, sizeof(int)); tbl->rg_trans = kh_init(c2c); tbl->pg_trans = kh_init(c2c); if (!tbl->tid_trans || !tbl->rg_trans || !tbl->pg_trans) { perror("out of memory"); exit(-1); } int32_t out_len = out->l_text; while (out_len > 0 && out->text[out_len-1] == '\n') {--out_len; } // strip trailing \n's kstring_t out_text = { 0, 0, NULL }; kputsn(out->text, out_len, &out_text); int i, min_tid = -1; tbl->lost_coord_sort = false; khash_t(c2i) *out_tid = kh_init(c2i); for (i = 0; i < out->n_targets; ++i) { int ret; khiter_t iter = kh_put(c2i, out_tid, out->target_name[i], &ret); if (ret <= 0) abort(); kh_value(out_tid, iter) = i; } for (i = 0; i < translate->n_targets; ++i) { khiter_t iter = kh_get(c2i, out_tid, translate->target_name[i]); if (iter == kh_end(out_tid)) { // Append missing entries to out tbl->tid_trans[i] = out->n_targets++; out->target_name = (char**)realloc(out->target_name, sizeof(char*)*out->n_targets); out->target_name[out->n_targets-1] = strdup(translate->target_name[i]); out->target_len = (uint32_t*)realloc(out->target_len, sizeof(uint32_t)*out->n_targets); out->target_len[out->n_targets-1] = translate->target_len[i]; // grep line with regex '^@SQ.*\tSN:%s(\t.*$|$)', translate->target_name[i] // from translate->text regex_t sq_id; regmatch_t* matches = (regmatch_t*)calloc(2, sizeof(regmatch_t)); if (matches == NULL) { perror("out of memory"); exit(-1); } kstring_t seq_regex = { 0, 0, NULL }; ksprintf(&seq_regex, "^@SQ.*\tSN:%s(\t.*$|$)", translate->target_name[i]); regcomp(&sq_id, seq_regex.s, REG_EXTENDED|REG_NEWLINE); free(seq_regex.s); if (regexec(&sq_id, translate->text, 1, matches, 0) != 0) { fprintf(pysamerr, "[trans_tbl_init] @SQ SN (%s) found in binary header but not text header.\n",translate->target_name[i]); exit(1); } regfree(&sq_id); // Produce our output line and append it to out_text kputc('\n', &out_text); kputsn(translate->text+matches[0].rm_so, matches[0].rm_eo-matches[0].rm_so, &out_text); free(matches); } else { tbl->tid_trans[i] = kh_value(out_tid, iter); } if (tbl->tid_trans[i] > min_tid) { min_tid = tbl->tid_trans[i]; } else { tbl->lost_coord_sort = true; } } kh_destroy(c2i, out_tid); // grep @RG id's regex_t rg_id; regmatch_t* matches = (regmatch_t*)calloc(2, sizeof(regmatch_t)); if (matches == NULL) { perror("out of memory"); exit(-1); } regcomp(&rg_id, "^@RG.*\tID:([!-)+-<>-~][ !-~]*)(\t.*$|$)", REG_EXTENDED|REG_NEWLINE); char* text = translate->text; klist_t(hdrln) *rg_list = kl_init(hdrln); while(1) { // foreach rg id in translate's header if (regexec(&rg_id, text, 2, matches, 0) != 0) break; // matches[0] is the whole @RG line; matches[1] is the ID field value kstring_t match_id = { 0, 0, NULL }; kputsn(text+matches[1].rm_so, matches[1].rm_eo-matches[1].rm_so, &match_id); // is our matched ID in our output list already regex_t rg_id_search; kstring_t rg_regex = { 0, 0, NULL }; ksprintf(&rg_regex, "^@RG.*\tID:%s(\t.*$|$)", match_id.s); regcomp(&rg_id_search, rg_regex.s, REG_EXTENDED|REG_NEWLINE|REG_NOSUB); free(rg_regex.s); kstring_t transformed_id = { 0, 0, NULL }; bool transformed_equals_match; if (regexec(&rg_id_search, out->text, 0, NULL, 0) != 0 || merge_rg) { // Not in there so can add it as 1-1 mapping kputs(match_id.s, &transformed_id); transformed_equals_match = true; } else { // It's in there so we need to transform it by appending random number to id ksprintf(&transformed_id, "%s-%0lX", match_id.s, lrand48()); transformed_equals_match = false; } regfree(&rg_id_search); // Insert it into our translation map int in_there = 0; khiter_t iter = kh_put(c2c, tbl->rg_trans, ks_release(&match_id), &in_there); char *transformed_id_s = ks_release(&transformed_id); kh_value(tbl->rg_trans,iter) = transformed_id_s; // take matched line and replace ID with transformed_id kstring_t transformed_line = { 0, 0, NULL }; if (transformed_equals_match) { kputsn(text+matches[0].rm_so, matches[0].rm_eo-matches[0].rm_so, &transformed_line); } else { kputsn(text+matches[0].rm_so, matches[1].rm_so-matches[0].rm_so, &transformed_line); kputs(transformed_id_s, &transformed_line); kputsn(text+matches[1].rm_eo, matches[0].rm_eo-matches[1].rm_eo, &transformed_line); } if (!(transformed_equals_match && merge_rg)) { // append line to linked list for PG processing char** ln = kl_pushp(hdrln, rg_list); *ln = ks_release(&transformed_line); // Give away to linked list } else free(transformed_line.s); text += matches[0].rm_eo; // next! } regfree(&rg_id); // Do same for PG id's regex_t pg_id; regcomp(&pg_id, "^@PG.*\tID:([!-)+-<>-~][ !-~]*)(\t.*$|$)", REG_EXTENDED|REG_NEWLINE); text = translate->text; klist_t(hdrln) *pg_list = kl_init(hdrln); while(1) { // foreach pg id in translate's header if (regexec(&pg_id, text, 2, matches, 0) != 0) break; kstring_t match_id = { 0, 0, NULL }; kputsn(text+matches[1].rm_so, matches[1].rm_eo-matches[1].rm_so, &match_id); // is our matched ID in our output list already regex_t pg_id_search; kstring_t pg_regex = { 0, 0, NULL }; ksprintf(&pg_regex, "^@PG.*\tID:%s(\t.*$|$)", match_id.s); regcomp(&pg_id_search, pg_regex.s, REG_EXTENDED|REG_NEWLINE|REG_NOSUB); free(pg_regex.s); kstring_t transformed_id = { 0, 0, NULL }; bool transformed_equals_match; if (regexec(&pg_id_search, out->text, 0, NULL, 0) != 0 || merge_pg) { // Not in there so can add it as 1-1 mapping kputs(match_id.s, &transformed_id); transformed_equals_match = true; } else { // It's in there so we need to transform it by appending random number to id ksprintf(&transformed_id, "%s-%0lX", match_id.s, lrand48()); transformed_equals_match = false; } regfree(&pg_id_search); // Insert it into our translation map int in_there = 0; khiter_t iter = kh_put(c2c, tbl->pg_trans, ks_release(&match_id), &in_there); char *transformed_id_s = ks_release(&transformed_id); kh_value(tbl->pg_trans,iter) = transformed_id_s; // take matched line and replace ID with transformed_id kstring_t transformed_line = { 0, 0, NULL }; if (transformed_equals_match) { kputsn(text+matches[0].rm_so, matches[0].rm_eo-matches[0].rm_so, &transformed_line); } else { kputsn(text+matches[0].rm_so, matches[1].rm_so-matches[0].rm_so, &transformed_line); kputs(transformed_id_s, &transformed_line); kputsn(text+matches[1].rm_eo, matches[0].rm_eo-matches[1].rm_eo, &transformed_line); } if (!(transformed_equals_match && merge_pg)) { // append line to linked list for PP processing char** ln = kl_pushp(hdrln, pg_list); *ln = ks_release(&transformed_line); // Give away to linked list } else free(transformed_line.s); text += matches[0].rm_eo; // next! } regfree(&pg_id); // need to translate PP's on the fly in second pass because they may not be in correct order and need complete tbl->pg_trans to do this // for each line { // with ID replaced with tranformed_id and PP's transformed using the translation table // } regex_t pg_pp; regcomp(&pg_pp, "^@PG.*\tPP:([!-)+-<>-~][!-~]*)(\t.*$|$)", REG_EXTENDED|REG_NEWLINE); kliter_t(hdrln) *iter = kl_begin(pg_list); while (iter != kl_end(pg_list)) { char* data = kl_val(iter); kstring_t transformed_line = { 0, 0, NULL }; // Find PP tag if (regexec(&pg_pp, data, 2, matches, 0) == 0) { // Lookup in hash table kstring_t pp_id = { 0, 0, NULL }; kputsn(data+matches[1].rm_so, matches[1].rm_eo-matches[1].rm_so, &pp_id); khiter_t k = kh_get(c2c, tbl->pg_trans, pp_id.s); free(pp_id.s); char* transformed_id = kh_value(tbl->pg_trans,k); // Replace kputsn(data, matches[1].rm_so-matches[0].rm_so, &transformed_line); kputs(transformed_id, &transformed_line); kputsn(data+matches[1].rm_eo, matches[0].rm_eo-matches[1].rm_eo, &transformed_line); } else { kputs(data, &transformed_line); } // Produce our output line and append it to out_text kputc('\n', &out_text); kputsn(transformed_line.s, transformed_line.l, &out_text); free(transformed_line.s); free(data); iter = kl_next(iter); } regfree(&pg_pp); // Need to also translate @RG PG's on the fly too regex_t rg_pg; regcomp(&rg_pg, "^@RG.*\tPG:([!-)+-<>-~][!-~]*)(\t.*$|$)", REG_EXTENDED|REG_NEWLINE); kliter_t(hdrln) *rg_iter = kl_begin(rg_list); while (rg_iter != kl_end(rg_list)) { char* data = kl_val(rg_iter); kstring_t transformed_line = { 0, 0, NULL }; // Find PG tag if (regexec(&rg_pg, data, 2, matches, 0) == 0) { // Lookup in hash table kstring_t pg_id = { 0, 0, NULL }; kputsn(data+matches[1].rm_so, matches[1].rm_eo-matches[1].rm_so, &pg_id); khiter_t k = kh_get(c2c, tbl->pg_trans, pg_id.s); free(pg_id.s); char* transformed_id = kh_value(tbl->pg_trans,k); // Replace kputsn(data, matches[1].rm_so-matches[0].rm_so, &transformed_line); kputs(transformed_id, &transformed_line); kputsn(data+matches[1].rm_eo, matches[0].rm_eo-matches[1].rm_eo, &transformed_line); } else { kputs(data, &transformed_line); } // Produce our output line and append it to out_text kputc('\n', &out_text); kputsn(transformed_line.s, transformed_line.l, &out_text); free(transformed_line.s); free(data); rg_iter = kl_next(rg_iter); } regfree(&rg_pg); kl_destroy(hdrln,pg_list); kl_destroy(hdrln,rg_list); free(matches); // Add trailing \n and write back to header free(out->text); kputc('\n', &out_text); out->l_text = out_text.l; out->text = ks_release(&out_text); }
void test_teardown(void) { sl_destroy(slist); kl_destroy(klist); }
static char *test_speed_compare(void) { struct _timeb start; struct _timeb finish; unsigned long sl_time_taken; unsigned long kl_time_taken; int keys[10000]; int vals[10000]; int test_keys[100]; sl *slist = sl_create(); kl *klist = kl_create(); int *v; _ftime(&start); /* generate long list (same for both) */ for(int i = 0; i < 10000; i++) { keys[i] = rand(); vals[i] = rand(); } for(int i = 0; i < 100; i++) { test_keys[i] = rand() % 10000; } /* fill lists with it (time this) */ _ftime(&start); for(int i = 0; i < 10000; i++) { kl_insert(klist, keys[i], &vals[i]); } _ftime(&finish); printf("kl: inserted 10,000 keys in %lums\n", ftime_diff(&start, &finish)); _ftime(&start); for(int i = 0; i < 10000; i++) { sl_insert(slist, keys[i], &vals[i]); } _ftime(&finish); printf("sl: inserted 10,000 keys in %lums\n", ftime_diff(&start, &finish)); /* find random values in it (same for both) n times, average time taken */ _ftime(&start); for(int i = 0; i < 1000; i++) { for(int j = 0; j < 100; j++) { v = sl_find(slist, keys[test_keys[j]]); } } _ftime(&finish); sl_time_taken = ftime_diff(&start, &finish); _ftime(&start); for(int i = 0; i < 1000; i++) { for(int j = 0; j < 100; j++) { v = kl_find(klist, keys[test_keys[j]]); } } _ftime(&finish); kl_time_taken = ftime_diff(&start, &finish); printf("sl: found 100,000 keys in %fs, %.2fkeys/s\n", sl_time_taken/1000.0, 100.0*100000/sl_time_taken); printf("kl: found 100,000 keys in %fs, %.2fkeys/s\n\n", kl_time_taken/1000.0, 100.0*100000/kl_time_taken); mu_assert("average sl find speed is slower than kl", sl_time_taken < kl_time_taken); sl_destroy(slist); kl_destroy(klist); return 0; }
void free_bsp_node(bsp_node_t *node) { if(node == NULL) return; kl_destroy(poly, node->polygons); free_poly(node->divider, 1); free(node); }