diff options
author | Campbell Barton <ideasman42@gmail.com> | 2013-09-06 02:24:12 +0400 |
---|---|---|
committer | Campbell Barton <ideasman42@gmail.com> | 2013-09-06 02:24:12 +0400 |
commit | 7a38fe97fdf6971ed3f52bba08e942bf915112c7 (patch) | |
tree | cd8569ff5a689f2b86aaae3c9b791e26b3deaf4b /source/blender/bmesh | |
parent | 6cc3aec8bc3cde3a0007fd877e40a744ee44c2e2 (diff) |
sorting utility functions for simple cases - sorting pointers by float for example.
Diffstat (limited to 'source/blender/bmesh')
-rw-r--r-- | source/blender/bmesh/intern/bmesh_construct.c | 25 | ||||
-rw-r--r-- | source/blender/bmesh/operators/bmo_join_triangles.c | 23 |
2 files changed, 13 insertions, 35 deletions
diff --git a/source/blender/bmesh/intern/bmesh_construct.c b/source/blender/bmesh/intern/bmesh_construct.c index bee5a7c7d39..60d9b62d6b9 100644 --- a/source/blender/bmesh/intern/bmesh_construct.c +++ b/source/blender/bmesh/intern/bmesh_construct.c @@ -35,6 +35,7 @@ #include "BLI_alloca.h" #include "BLI_math.h" +#include "BLI_sort_utils.h" #include "BKE_customdata.h" @@ -358,20 +359,6 @@ BMFace *BM_face_create_ngon_verts(BMesh *bm, BMVert **vert_arr, const int len, f_example, create_flag); } - -typedef struct AngleIndexPair { - float angle; - int index; -} AngleIndexPair; - -static int angle_index_pair_cmp(const void *e1, const void *e2) -{ - const AngleIndexPair *p1 = e1, *p2 = e2; - if (p1->angle > p2->angle) return 1; - else if (p1->angle < p2->angle) return -1; - else return 0; -} - /** * Makes an NGon from an un-ordered set of verts * @@ -391,7 +378,7 @@ static int angle_index_pair_cmp(const void *e1, const void *e2) BMFace *BM_face_create_ngon_vcloud(BMesh *bm, BMVert **vert_arr, int len, const BMFace *f_example, const eBMCreateFlag create_flag) { - AngleIndexPair *vang = BLI_array_alloca(vang, len); + struct SortIntByFloat *vang = BLI_array_alloca(vang, len); BMVert **vert_arr_map = BLI_array_alloca(vert_arr_map, len); BMFace *f; @@ -488,18 +475,18 @@ BMFace *BM_face_create_ngon_vcloud(BMesh *bm, BMVert **vert_arr, int len, angle = -angle; } - vang[i].angle = angle; - vang[i].index = i; + vang[i].sort_value = angle; + vang[i].data = i; } /* sort by angle and magic! - we have our ngon */ - qsort(vang, len, sizeof(AngleIndexPair), angle_index_pair_cmp); + qsort(vang, len, sizeof(*vang), BLI_sortutil_cmp_float); /* --- */ /* create edges and find the winding (if faces are attached to any existing edges) */ for (i = 0; i < len; i++) { - vert_arr_map[i] = vert_arr[vang[i].index]; + vert_arr_map[i] = vert_arr[vang[i].data]; } f = BM_face_create_ngon_verts(bm, vert_arr_map, len, f_example, create_flag, true, true); diff --git a/source/blender/bmesh/operators/bmo_join_triangles.c b/source/blender/bmesh/operators/bmo_join_triangles.c index 12065743136..6d4f257909c 100644 --- a/source/blender/bmesh/operators/bmo_join_triangles.c +++ b/source/blender/bmesh/operators/bmo_join_triangles.c @@ -34,6 +34,7 @@ #include "DNA_meshdata_types.h" #include "BLI_math.h" +#include "BLI_sort_utils.h" #include "BKE_customdata.h" @@ -181,10 +182,6 @@ static bool bm_edge_faces_cmp(BMesh *bm, BMEdge *e, const bool do_uv, const bool return true; } -typedef struct JoinEdge { - float weight; - BMEdge *e; -} JoinEdge; #define EDGE_MARK 1 #define EDGE_CHOSEN 2 @@ -192,14 +189,7 @@ typedef struct JoinEdge { #define FACE_MARK 1 #define FACE_INPUT 2 -static int fplcmp(const void *v1, const void *v2) -{ - const JoinEdge *e1 = (JoinEdge *)v1, *e2 = (JoinEdge *)v2; - if (e1->weight > e2->weight) return 1; - else if (e1->weight < e2->weight) return -1; - else return 0; -} void bmo_join_triangles_exec(BMesh *bm, BMOperator *op) { @@ -214,7 +204,8 @@ void bmo_join_triangles_exec(BMesh *bm, BMOperator *op) BMOIter siter; BMFace *f; BMEdge *e; - JoinEdge *jedges = NULL; + /* data: edge-to-join, sort_value: error weight */ + struct SortPointerByFloat *jedges; unsigned i, totedge; unsigned int totedge_tag = 0; @@ -271,19 +262,19 @@ void bmo_join_triangles_exec(BMesh *bm, BMOperator *op) measure = measure_facepair(v1->co, v2->co, v3->co, v4->co, limit); if (measure < limit) { - jedges[i].e = e; - jedges[i].weight = measure; + jedges[i].data = e; + jedges[i].sort_value = measure; i++; } } totedge = i; - qsort(jedges, totedge, sizeof(JoinEdge), fplcmp); + qsort(jedges, totedge, sizeof(*jedges), BLI_sortutil_cmp_float); for (i = 0; i < totedge; i++) { BMFace *f_a, *f_b; - e = jedges[i].e; + e = jedges[i].data; f_a = e->l->f; f_b = e->l->radial_next->f; |