star-enclosures-3d

Extract enclosures from 3D geometry
git clone git://git.meso-star.fr/star-enclosures-3d.git
Log | Files | Refs | README | LICENSE

commit c8288c8522f4fe073ed2586eef8e5928548f16fb
parent d4c1d724c858050e398c65e0916e64d005a5f927
Author: Christophe Coustet <christophe.coustet@meso-star.com>
Date:   Mon, 26 Feb 2018 13:22:38 +0100

Some cleaning: remove non-openmp code.

Diffstat:
Msrc/senc_scene_analyze.c | 289+++----------------------------------------------------------------------------
1 file changed, 11 insertions(+), 278 deletions(-)

diff --git a/src/senc_scene_analyze.c b/src/senc_scene_analyze.c @@ -661,93 +661,6 @@ error: } static res_T -scan_edges - (struct senc_scene* scn, - struct darray_neighbourhood* neighbourhood_by_edge, - struct darray_triangle_tmp* triangles_tmp_array) -{ - trg_id_t t; - const struct triangle_in *triangles_in; - struct triangle_tmp *triangles_tmp; - edge_id_t max_nbedges; - /* Htable used to give an id to edges */ - struct htable_edge_id edge_ids; - res_T res = RES_OK; - - ASSERT(scn && neighbourhood_by_edge && triangles_tmp_array); - ASSERT((size_t)scn->nuverts + (size_t)scn->nutris + 2 <= EDGE_MAX__); - - /* Make some room for edges. */ - max_nbedges = (edge_id_t)(scn->nuverts + scn->nutris + 4); - htable_edge_id_init(scn->dev->allocator, &edge_ids); - OK(htable_edge_id_reserve(&edge_ids, max_nbedges)); - OK(darray_neighbourhood_reserve(neighbourhood_by_edge, max_nbedges)); - OK(darray_triangle_tmp_resize(triangles_tmp_array, scn->nutris)); - - /* Loop on triangles to register edges. */ - triangles_in = darray_triangle_in_cdata_get(&scn->triangles_in); - triangles_tmp = darray_triangle_tmp_data_get(triangles_tmp_array); - ASSERT(scn->nutris == darray_triangle_tmp_size_get(triangles_tmp_array)); - FOR_EACH(t, 0, scn->nutris) { - struct trg_edge edge; - char e; - FOR_EACH(e, 0, 3) { - edge_id_t* p_id; - /* Create edge. */ - set_edge(triangles_in[t].vertice_id[e], - triangles_in[t].vertice_id[(e + 1) % 3], &edge, &triangles_tmp[t].reversed_edge[e]); - /* Find edge id; create it if not already done. */ - p_id = htable_edge_id_find(&edge_ids, &edge); - - if(p_id) { - struct edge_neighbourhood* n = - darray_neighbourhood_data_get(neighbourhood_by_edge) + *p_id; - struct neighbour_info* info; - size_t sz; - /* Add neighbour info to existing edge's neighbour list */ - ASSERT(n->edge.vrtx0 == edge.vrtx0 && n->edge.vrtx1 == edge.vrtx1); - sz = darray_neighbour_size_get(&n->neighbours); - OK(darray_neighbour_resize(&n->neighbours, 1 + sz)); - info = darray_neighbour_data_get(&n->neighbours) + sz; - info->trg_id = t; - info->edge_rank = e; - } else { - /* Create id */ - edge_id_t id; - struct edge_neighbourhood* n; - struct neighbour_info* info; - size_t sz = htable_edge_id_size_get(&edge_ids); - ASSERT(sz <= EDGE_MAX__); - id = (edge_id_t) sz; - ASSERT(htable_edge_id_size_get(&edge_ids) - == darray_neighbourhood_size_get(neighbourhood_by_edge)); - OK(htable_edge_id_set(&edge_ids, &edge, &id)); - OK(darray_neighbourhood_resize(neighbourhood_by_edge, 1 + sz)); - n = darray_neighbourhood_data_get(neighbourhood_by_edge) + sz; - /* Add neighbour info to a newly created edge's neighbour list */ - n->edge = edge; - ASSERT(darray_neighbour_size_get(&n->neighbours) == 0); - OK(darray_neighbour_reserve(&n->neighbours, 2)); - OK(darray_neighbour_resize(&n->neighbours, 1)); - info = darray_neighbour_data_get(&n->neighbours); - info->trg_id = t; - info->edge_rank = e; - } - } - } - - - printf("Collected %zu edges.\n", htable_edge_id_size_get(&edge_ids)); - - -exit: - htable_edge_id_release(&edge_ids); - return res; -error: - goto exit; -} - -static res_T collect_and_link_neighbours (struct senc_scene* scn, struct trgside* trgsides, @@ -842,12 +755,6 @@ collect_and_link_neighbours tmp = darray_neighbourhood_size_get(&neighbourhood_by_edge); ASSERT(tmp <= EDGE_MAX__); edge_count = (edge_id_t)tmp; - - - printf("Thread %d/%d collected %zu edges.\n", rank, num_thread, - htable_edge_id_size_get(&edge_ids)); - - FOR_EACH(e, 0, edge_count) { double edge[3], common_edge[3], basis[9], norm, mz, mz_edge; vrtx_id_t v0, v1, v2; @@ -962,138 +869,6 @@ error: } static res_T -link_neighbours - (struct senc_scene* scn, - struct trgside* trgsides, - struct darray_neighbourhood* neighbourhood_by_edge, - struct darray_triangle_tmp* triangles_tmp_array) -{ - edge_id_t e, edge_count; - const struct triangle_in *triangles_in; - struct triangle_tmp *triangles_tmp; - const union double3* vertices; - size_t tmp; - res_T res = RES_OK; - - ASSERT(scn && trgsides && neighbourhood_by_edge && triangles_tmp_array); - - /* Loop on edges. - * For each edge sort triangle sides by rotation angle - * and connect neighbours. */ - triangles_in = darray_triangle_in_cdata_get(&scn->triangles_in); - triangles_tmp = darray_triangle_tmp_data_get(triangles_tmp_array); - vertices = darray_position_cdata_get(&scn->vertices); - tmp = darray_neighbourhood_size_get(neighbourhood_by_edge); - ASSERT(tmp <= EDGE_MAX__); - edge_count = (edge_id_t)tmp; - - FOR_EACH(e, 0, edge_count) { - double edge[3], common_edge[3], basis[9], norm, mz, mz_edge; - vrtx_id_t v0, v1, v2; - struct edge_neighbourhood* neighbourhood - = darray_neighbourhood_data_get(neighbourhood_by_edge) + e; - struct darray_neighbour* neighbour_list = &neighbourhood->neighbours; - side_id_t i, neighbour_count; - char mz_vid, mz_vid_edge; - tmp = darray_neighbour_size_get(neighbour_list); - ASSERT(tmp <= SIDE_MAX__); - neighbour_count = (side_id_t)tmp; - ASSERT(neighbour_count); - v0 = neighbourhood->edge.vrtx0; - v1 = neighbourhood->edge.vrtx1; - d3_sub(common_edge, vertices[v1].vec, vertices[v0].vec); - if(vertices[v0].pos.z > vertices[v1].pos.z) { - mz_edge = vertices[v0].pos.z; - mz_vid_edge = 0; - } else { - mz_edge = vertices[v1].pos.z; - mz_vid_edge = 1; - } - norm = d3_normalize(common_edge, common_edge); - ASSERT(norm); - d33_basis(basis, common_edge); - d33_inverse(basis, basis); - FOR_EACH(i, 0, neighbour_count) { - struct neighbour_info* neighbour_info - = darray_neighbour_data_get(neighbour_list) + i; - const struct triangle_in *trg_in = triangles_in + neighbour_info->trg_id; - struct triangle_tmp *neighbour = triangles_tmp + neighbour_info->trg_id; - char actual_vid; - v2 = trg_in->vertice_id[(neighbour_info->edge_rank + 2) % 3]; - if(vertices[v2].pos.z > mz_edge) { - mz = vertices[v2].pos.z; - mz_vid = 2; - } else { - mz = mz_edge; - mz_vid = mz_vid_edge; - } - /* Compute the actual vertex id - * as vertices are not in the v0 v1 v2 order in the actual triangle */ - if(mz_vid == 2) { - actual_vid = (2 + neighbour_info->edge_rank) % 3; - } else { - int is_r = neighbour->reversed_edge[neighbour_info->edge_rank]; - ASSERT(mz_vid == 0 || mz_vid == 1); - actual_vid = ((is_r ? 1 - mz_vid : mz_vid) + neighbour_info->edge_rank) % 3; - } - - ASSERT(neighbour->max_z <= mz); - neighbour->max_z = mz; - ASSERT(0 <= actual_vid && actual_vid <= 2); - neighbour->max_z_vrtx_id = actual_vid; - /* Compute rotation angle around common edge */ - d3_sub(edge, vertices[v2].vec, vertices[v0].vec); - d33_muld3(edge, basis, edge); - ASSERT(d3_len(edge) && (edge[0] || edge[1])); - neighbour_info->angle = atan2(edge[1], edge[0]); - if(neighbour_info->angle < 0) neighbour_info->angle += 2 * PI; - /* Due to catastrophic cancelation, -eps+2pi translates to 2pi */ - ASSERT(0 <= neighbour_info->angle && neighbour_info->angle <= 2 * PI); - } - /* Sort triangles by rotation angle */ - qsort(darray_neighbour_data_get(neighbour_list), neighbour_count, - sizeof(struct neighbour_info), neighbour_cmp); - /* Link sides. - * Create cycles of sides by neighbourhood around common edge. */ - FOR_EACH(i, 0, neighbour_count) { - /* Neighbourhood info for current pair of triangles */ - const struct neighbour_info* current - = darray_neighbour_cdata_get(neighbour_list) + i; - const struct neighbour_info* ccw_neighbour - = darray_neighbour_cdata_get(neighbour_list) + (i + 1) % neighbour_count; - /* Rank of the edge of interest in triangles */ - const char crt_edge = current->edge_rank; - const char ccw_edge = ccw_neighbour->edge_rank; - /* User id of current triangles */ - const trg_id_t crt_id = current->trg_id; - const trg_id_t ccw_id = ccw_neighbour->trg_id; - /* Facing sides of triangles */ - const enum side_id crt_side - = triangles_tmp[crt_id].reversed_edge[crt_edge] ? SIDE_BACK : SIDE_FRONT; - const enum side_id ccw_side - = triangles_tmp[ccw_id].reversed_edge[ccw_edge] ? SIDE_FRONT : SIDE_BACK; - /* Index of sides in trgsides */ - const side_id_t crt_side_idx = TRGIDxSIDE_2_TRGSIDE(crt_id, crt_side); - const side_id_t ccw_side_idx = TRGIDxSIDE_2_TRGSIDE(ccw_id, ccw_side); - /* Side ptrs */ - struct trgside* const p_crt_side = trgsides + crt_side_idx; - struct trgside* const p_ccw_side = trgsides + ccw_side_idx; - /* Link sides */ - p_crt_side->facing_side_id[crt_edge] = ccw_side_idx; - p_ccw_side->facing_side_id[ccw_edge] = crt_side_idx; - /* Record media */ - p_crt_side->medium = triangles_in[crt_id].medium[crt_side]; - p_ccw_side->medium = triangles_in[ccw_id].medium[ccw_side]; - ASSERT(p_crt_side->medium < scn->nmeds); - ASSERT(p_ccw_side->medium < scn->nmeds); - p_crt_side->list_id = FLAG_LIST_SIDE_LIST; - p_ccw_side->list_id = FLAG_LIST_SIDE_LIST; - } - } - return res; -} - -static res_T build_result (struct senc_descriptor* desc, struct darray_cc_descriptor* connex_components) @@ -1302,10 +1077,6 @@ senc_scene_analyze(struct senc_scene* scn, struct senc_descriptor** out_desc) darray_triangle_tmp_init(scn->dev->allocator, &triangles_tmp); triangles_tmp_initialized = 1; - - -#ifndef PARALLEL_EDGES - OK(darray_triangle_tmp_resize(&triangles_tmp, scn->nutris)); trgsides = MEM_CALLOC(scn->dev->allocator, 2 * scn->nutris, sizeof(struct trgside)); @@ -1313,65 +1084,27 @@ senc_scene_analyze(struct senc_scene* scn, struct senc_descriptor** out_desc) res = RES_MEM_ERR; goto error; } + + /* Step 1: build neighbourhoods */ #pragma omp parallel { - collect_and_link_neighbours(scn, trgsides, &triangles_tmp); - } - - - - time_sub(&t0, time_current(&t1), &t0); - time_dump(&t0, TIME_MSEC | TIME_SEC | TIME_MIN, NULL, dump, sizeof(dump)); - printf("collect_and_link_neighbours: %s\n", dump); - time_current(&t0); - -#else - - darray_neighbourhood_init(scn->dev->allocator, &neighbourhood_by_edge); - neighbourhood_by_edge_initialized = 1; - - /* Step 1: list edges and connect triangles to edges */ - res = scan_edges(scn, &neighbourhood_by_edge, &triangles_tmp); - if(res != RES_OK) { - log_err(scn->dev, "%s: could not scan edges.\n", FUNC_NAME); - goto error; + res_T tmp_res = + collect_and_link_neighbours(scn, trgsides, &triangles_tmp); + if(tmp_res != RES_OK) res = tmp_res; } - - trgsides - = MEM_CALLOC(scn->dev->allocator, 2 * scn->nutris, sizeof(struct trgside)); - if(!trgsides) { - res = RES_MEM_ERR; - goto error; - } - - - - time_sub(&t0, time_current(&t1), &t0); - time_dump(&t0, TIME_MSEC | TIME_SEC | TIME_MIN, NULL, dump, sizeof(dump)); - printf("scan_edges: %s\n", dump); - time_current(&t0); - - - /* Step 2: link triangles by neighbourhood */ - res = link_neighbours(scn, trgsides, &neighbourhood_by_edge, - &triangles_tmp); - if(res != RES_OK) { - log_err(scn->dev, "%s: could not link neighbours.\n", FUNC_NAME); + if (res != RES_OK) { + log_err(scn->dev, + "%s: could not build neighbourhoods from scene.\n", FUNC_NAME); goto error; } - darray_neighbourhood_release(&neighbourhood_by_edge); - neighbourhood_by_edge_initialized = 0; - - time_sub(&t0, time_current(&t1), &t0); time_dump(&t0, TIME_MSEC | TIME_SEC | TIME_MIN, NULL, dump, sizeof(dump)); - printf("link_neighbours: %s\n", dump); + printf("collect_and_link_neighbours: %s\n", dump); time_current(&t0); -#endif darray_cc_descriptor_init(scn->dev->allocator, &connex_components); connex_components_initialized = 1; @@ -1379,7 +1112,7 @@ senc_scene_analyze(struct senc_scene* scn, struct senc_descriptor** out_desc) triangles_comp_initialized = 1; OK(darray_triangle_comp_resize(&triangles_comp, scn->nutris)); - /* Step 3: extract triangle connex components */ + /* Step 2: extract triangle connex components */ res = extract_connex_components(desc, trgsides, &connex_components, &triangles_tmp, &triangles_comp); if(res != RES_OK) { @@ -1400,7 +1133,7 @@ senc_scene_analyze(struct senc_scene* scn, struct senc_descriptor** out_desc) - /* Step 4: group components */ + /* Step 3: group components */ res = group_connex_components(desc, trgsides, &triangles_comp, &connex_components); if(res != RES_OK) {