Changeset View
Changeset View
Standalone View
Standalone View
source/blender/bmesh/intern/bmesh_polygon.c
| Context not available. | |||||
| } | } | ||||
| /** | /** | ||||
| * Compute the tanget of the face, using the longest edge. | |||||
| */ | |||||
| void BM_face_calc_tangent_edge(const BMFace *f, float r_tangent[3]) | |||||
| { | |||||
| const BMLoop *l_long = BM_face_find_longest_loop((BMFace *)f); | |||||
| sub_v3_v3v3(r_tangent, l_long->v->co, l_long->next->v->co); | |||||
| normalize_v3(r_tangent); | |||||
| } | |||||
| /** | |||||
| * Compute the tanget of the face, using the two longest disconected edges. | |||||
| */ | |||||
| void BM_face_calc_tangent_edge_pair(const BMFace *f, float r_tangent[3]) | |||||
| { | |||||
| /* Special case because triangles don't have any disconnected edges. */ | |||||
| if (f->len == 3) { | |||||
| BMVert *verts[3]; | |||||
| float vec[3][3], lens[3]; | |||||
| int order[3] = {0, 1, 2}; | |||||
| BM_face_as_array_vert_tri((BMFace *)f, verts); | |||||
| sub_v3_v3v3(vec[0], verts[0]->co, verts[1]->co); | |||||
| sub_v3_v3v3(vec[1], verts[1]->co, verts[2]->co); | |||||
| sub_v3_v3v3(vec[2], verts[2]->co, verts[0]->co); | |||||
| lens[0] = len_squared_v3(vec[0]); | |||||
| lens[1] = len_squared_v3(vec[1]); | |||||
| lens[2] = len_squared_v3(vec[2]); | |||||
| axis_sort_v3(lens, order); | |||||
| normalize_v3(vec[order[2]]); | |||||
| normalize_v3(vec[order[1]]); | |||||
| add_v3_v3v3(r_tangent, vec[order[2]], vec[order[1]]); | |||||
| } | |||||
| /* Use longest edge pair */ | |||||
| else if (f->len == 4) { | |||||
| BMVert *verts[4]; | |||||
| float vec[3], vec_a[3], vec_b[3]; | |||||
| // BM_iter_as_array(NULL, BM_VERTS_OF_FACE, efa, (void **)verts, 4); | |||||
| BM_face_as_array_vert_quad((BMFace *)f, verts); | |||||
| sub_v3_v3v3(vec_a, verts[3]->co, verts[2]->co); | |||||
| sub_v3_v3v3(vec_b, verts[0]->co, verts[1]->co); | |||||
| add_v3_v3v3(r_tangent, vec_a, vec_b); | |||||
| sub_v3_v3v3(vec_a, verts[0]->co, verts[3]->co); | |||||
| sub_v3_v3v3(vec_b, verts[1]->co, verts[2]->co); | |||||
| add_v3_v3v3(vec, vec_a, vec_b); | |||||
| /* use the biggest edge length */ | |||||
| if (len_squared_v3(r_tangent) < len_squared_v3(vec)) { | |||||
| copy_v3_v3(r_tangent, vec); | |||||
| } | |||||
| } | |||||
| /* For ngons use two longest disconnected edges */ | |||||
| else { | |||||
| BMLoop *longest_loop = BM_face_find_longest_loop((BMFace *)f); | |||||
| BMEdge *longest_edge2 = NULL; | |||||
| float len_max_sq = 0.0f; | |||||
| float vec_a[3], vec_b[3]; | |||||
| BMLoop *l_iter = longest_loop->prev->prev; | |||||
| BMLoop *l_last = longest_loop->next; | |||||
| do { | |||||
| const float len_sq = len_squared_v3v3(l_iter->v->co, l_iter->next->v->co); | |||||
| if (len_sq >= len_max_sq) { | |||||
campbellbarton: This can be done with less checking, looping.
Keep the loop from `BM_face_find_longest_loop`… | |||||
| longest_edge2 = l_iter->e; | |||||
| len_max_sq = len_sq; | |||||
| } | |||||
| } while ((l_iter = l_iter->prev) != l_last); | |||||
| sub_v3_v3v3(vec_a, longest_loop->next->v->co, longest_loop->v->co); | |||||
| sub_v3_v3v3(vec_b, longest_edge2->v1->co, longest_edge2->v2->co); | |||||
| add_v3_v3v3(r_tangent, vec_a, vec_b); | |||||
| } | |||||
| normalize_v3(r_tangent); | |||||
| } | |||||
| /** | |||||
| * Compute the tanget of the face, using the edge farthest away from any vertex in the face. | |||||
| */ | |||||
| void BM_face_calc_tangent_edge_diagonal(const BMFace *f, float r_tangent[3]) | |||||
| { | |||||
| float cent[3]; | |||||
| float dist_max_sq = 0.0f; | |||||
| BMLoop *l_iter; | |||||
| BMLoop *l_first; | |||||
| l_iter = l_first = BM_FACE_FIRST_LOOP(f); | |||||
| do { | |||||
| BMLoop *l_iter2 = l_iter->next; | |||||
| add_v3_v3v3(cent, l_iter->v->co, l_iter->next->v->co); | |||||
| mul_v3_fl(cent, 0.5f); | |||||
| do { | |||||
| float dist_sq; | |||||
| dist_sq = len_squared_v3v3(cent, l_iter2->v->co); | |||||
| if (dist_sq >= dist_max_sq) { | |||||
| dist_max_sq = dist_sq; | |||||
| sub_v3_v3v3(r_tangent, l_iter->v->co, l_iter->next->v->co); | |||||
| } | |||||
| } while ((l_iter2 = l_iter2->next) != l_iter); | |||||
| } while ((l_iter = l_iter->next) != l_first); | |||||
| normalize_v3(r_tangent); | |||||
| } | |||||
| /** | |||||
| * Compute the tanget of the face, using longest distance between vertices on the face. | |||||
| * \note The logic is almost identical to BM_face_calc_tangent_edge_diagonal | |||||
| */ | |||||
| void BM_face_calc_tangent_vert_diagonal(const BMFace *f, float r_tangent[3]) | |||||
| { | |||||
| float vec[3]; | |||||
| float dist_max_sq = 0.0f; | |||||
| BMLoop *l_iter; | |||||
| BMLoop *l_first; | |||||
Not Done Inline ActionsAgain, this could use the current loops next/prev to avoid checking against its self. Also think it might be better to split this into 2 functions (just keep them together and note its nearly the same logic). campbellbarton: Again, this could use the current loops next/prev to avoid checking against its self.
Also… | |||||
| l_iter = l_first = BM_FACE_FIRST_LOOP(f); | |||||
| do { | |||||
| BMLoop *l_iter2 = l_iter->next; | |||||
| do { | |||||
| float dist_sq; | |||||
| sub_v3_v3v3(vec, l_iter->v->co, l_iter2->v->co); | |||||
| dist_sq = len_squared_v3(vec); | |||||
| if (dist_sq >= dist_max_sq) { | |||||
| dist_max_sq = dist_sq; | |||||
| copy_v3_v3(r_tangent, vec); | |||||
| } | |||||
| } while ((l_iter2 = l_iter2->next) != l_iter); | |||||
| } while ((l_iter = l_iter->next) != l_first); | |||||
| normalize_v3(r_tangent); | |||||
| } | |||||
| /** | |||||
| * Compute a meaningful direction along the face (use for manipulator axis). | * Compute a meaningful direction along the face (use for manipulator axis). | ||||
| * \note result isnt normalized. | * \note result isnt normalized. | ||||
| */ | */ | ||||
| Context not available. | |||||
This can be done with less checking, looping.
Keep the loop from BM_face_find_longest_loop, then walk over the adjacent loops starting at longest_loop->prev->prev, finishing when you hit longest_loop->next.
This way theres no need to check if the edges are adjacent.