Permalink
Browse files

refactor/rename vec comparators, add support for different orderings

  • Loading branch information...
postspectacular committed Jul 21, 2016
1 parent 8de7413 commit 8ad67597281208e9256fd8e5477e3770e9a75593
Showing with 108 additions and 36 deletions.
  1. +1 −11 src/geom/voronoi.c
  2. +2 −2 src/math/vec.c
  3. +16 −6 src/math/vec2.h
  4. +60 −0 src/math/vec3_common.h
  5. +24 −12 src/math/vec4_common.h
  6. +5 −5 test/vec.c
View
@@ -312,16 +312,6 @@ static int edgelist_init(CT_VOEdgeList *el, size_t num) {
return 1;
}
static int compare_vec2f(const void *a, const void *b) {
CT_Vec2f *va = (CT_Vec2f *)a;
CT_Vec2f *vb = (CT_Vec2f *)b;
if (va->y < vb->y) return -1;
if (va->y > vb->y) return 1;
if (va->x < vb->x) return -1;
if (va->x > vb->x) return 1;
return 0;
}
static CT_VOVertex *provide_next_site(CT_Voronoi *vor, const CT_Vec2f *sites,
size_t num) {
CT_VOVertex *v = NULL;
@@ -360,7 +350,7 @@ ct_export void ct_voronoi_compute(CT_Voronoi *vor, CT_Vec2f *sites, size_t num,
CT_VOHeap *heap = &vor->heap;
ct_bounds2fp((float *)sites, num, 2, &min, &max);
qsort(sites, num, sizeof(CT_Vec2f), compare_vec2f);
qsort(sites, num, sizeof(CT_Vec2f), ct_compare2fv_yx);
el->minx = min.x;
el->deltax = max.x - min.x;
heap->miny = min.y;
View
@@ -186,7 +186,7 @@ int ct_tostringfp(char *buf, int bufsz, const float *p, size_t num) {
size_t ct_convexhull2f(CT_Vec2f *points, size_t num, CT_Vec2f *hull) {
if (num < 1) return 0;
qsort(points, num, sizeof(CT_Vec2f), ct_compare2fv);
qsort(points, num, sizeof(CT_Vec2f), ct_compare2fv_xy);
size_t len = 0;
for (size_t i = 0; i < num; i++) {
while (len >= 2 &&
@@ -213,5 +213,5 @@ size_t ct_convexhull2f(CT_Vec2f *points, size_t num, CT_Vec2f *hull) {
CT_DEBUG("add hull: %f,%f (%zu)", points[i].x, points[i].y, len);
len++;
}
return len - 1 - (ct_compare2fv(&hull[0], &hull[1]) == 0);
return len - 1 - (ct_compare2fv_xy(&hull[0], &hull[1]) == 0);
}
View
@@ -88,14 +88,24 @@ ct_export ct_inline CT_Vec2f *ct_clamp2fv_imm(CT_Vec2f *a, const CT_Vec2f *min,
return a;
}
ct_export ct_inline int ct_compare2fv(const void *a, const void *b) {
ct_export ct_inline int ct_compare2fv_xy(const void *a, const void *b) {
CT_Vec2f *va = (CT_Vec2f *)a;
CT_Vec2f *vb = (CT_Vec2f *)b;
int c = COMPARE(va->x, vb->x);
if (!c) {
return COMPARE(va->y, vb->y);
}
return c;
if (va->x < vb->x) return -1;
if (va->x > vb->x) return 1;
if (va->y < vb->y) return -1;
if (va->y > vb->y) return 1;
return 0;
}
ct_export ct_inline int ct_compare2fv_yx(const void *a, const void *b) {
CT_Vec2f *va = (CT_Vec2f *)a;
CT_Vec2f *vb = (CT_Vec2f *)b;
if (va->y < vb->y) return -1;
if (va->y > vb->y) return 1;
if (va->x < vb->x) return -1;
if (va->x > vb->x) return 1;
return 0;
}
ct_export ct_inline size_t ct_deltaeq2fv(const CT_Vec2f *a, const CT_Vec2f *b,
View
@@ -24,6 +24,66 @@ ct_export ct_inline int ct_compare3fv(const void *a, const void *b) {
return c;
}
ct_export ct_inline int ct_compare3fv_xyz(const void *a, const void *b) {
CT_Vec3f *va = (CT_Vec3f *)a;
CT_Vec3f *vb = (CT_Vec3f *)b;
if (va->x < vb->x) return -1;
if (va->x > vb->x) return 1;
if (va->y < vb->y) return -1;
if (va->y > vb->y) return 1;
if (va->z < vb->z) return -1;
if (va->z > vb->z) return 1;
return 0;
}
ct_export ct_inline int ct_compare3fv_yxz(const void *a, const void *b) {
CT_Vec3f *va = (CT_Vec3f *)a;
CT_Vec3f *vb = (CT_Vec3f *)b;
if (va->y < vb->y) return -1;
if (va->y > vb->y) return 1;
if (va->x < vb->x) return -1;
if (va->x > vb->x) return 1;
if (va->z < vb->z) return -1;
if (va->z > vb->z) return 1;
return 0;
}
ct_export ct_inline int ct_compare3fv_yzx(const void *a, const void *b) {
CT_Vec3f *va = (CT_Vec3f *)a;
CT_Vec3f *vb = (CT_Vec3f *)b;
if (va->y < vb->y) return -1;
if (va->y > vb->y) return 1;
if (va->z < vb->z) return -1;
if (va->z > vb->z) return 1;
if (va->x < vb->x) return -1;
if (va->x > vb->x) return 1;
return 0;
}
ct_export ct_inline int ct_compare3fv_zyx(const void *a, const void *b) {
CT_Vec3f *va = (CT_Vec3f *)a;
CT_Vec3f *vb = (CT_Vec3f *)b;
if (va->z < vb->z) return -1;
if (va->z > vb->z) return 1;
if (va->y < vb->y) return -1;
if (va->y > vb->y) return 1;
if (va->x < vb->x) return -1;
if (va->x > vb->x) return 1;
return 0;
}
ct_export ct_inline int ct_compare3fv_zxy(const void *a, const void *b) {
CT_Vec3f *va = (CT_Vec3f *)a;
CT_Vec3f *vb = (CT_Vec3f *)b;
if (va->z < vb->z) return -1;
if (va->z > vb->z) return 1;
if (va->x < vb->x) return -1;
if (va->x > vb->x) return 1;
if (va->y < vb->y) return -1;
if (va->y > vb->y) return 1;
return 0;
}
ct_export ct_inline size_t ct_deltaeq3fv(const CT_Vec3f *a, const CT_Vec3f *b,
float eps) {
return (ct_deltaeqf(a->x, b->x, eps) && ct_deltaeqf(a->y, b->y, eps) &&
View
@@ -11,20 +11,32 @@ CT_BEGIN_DECLS
CT_Vec4f *ct_vec4f(float x, float y, float z, float w, CT_MPool *mpool);
CT_Vec4f *ct_vec4fn(float n, CT_MPool *mpool);
ct_export ct_inline int ct_compare4fv(const void *a, const void *b) {
ct_export ct_inline int ct_compare4fv_xyzw(const void *a, const void *b) {
CT_Vec4f *va = (CT_Vec4f *)a;
CT_Vec4f *vb = (CT_Vec4f *)b;
int c = COMPARE(va->x, vb->x);
if (!c) {
c = COMPARE(va->y, vb->y);
if (!c) {
c = COMPARE(va->z, vb->z);
if (!c) {
return COMPARE(va->w, vb->w);
}
}
}
return c;
if (va->x < vb->x) return -1;
if (va->x > vb->x) return 1;
if (va->y < vb->y) return -1;
if (va->y > vb->y) return 1;
if (va->z < vb->z) return -1;
if (va->z > vb->z) return 1;
if (va->w < vb->w) return -1;
if (va->w > vb->w) return 1;
return 0;
}
ct_export ct_inline int ct_compare4fv_wzyx(const void *a, const void *b) {
CT_Vec4f *va = (CT_Vec4f *)a;
CT_Vec4f *vb = (CT_Vec4f *)b;
if (va->w < vb->w) return -1;
if (va->w > vb->w) return 1;
if (va->z < vb->z) return -1;
if (va->z > vb->z) return 1;
if (va->y < vb->y) return -1;
if (va->y > vb->y) return 1;
if (va->x < vb->x) return -1;
if (va->x > vb->x) return 1;
return 0;
}
ct_export ct_inline size_t ct_deltaeq4fv(const CT_Vec4f *a, const CT_Vec4f *b,
View
@@ -45,11 +45,11 @@ int test_vec2f() {
a = ct_vec2f(1, 2, &pool);
b = ct_vec2f(1, 1, &pool);
c = ct_vec2f(2, 1, &pool);
CT_IS(1 == ct_compare2fv(a, b), "a > b");
CT_IS(0 == ct_compare2fv(a, a), "a == a");
CT_IS(-1 == ct_compare2fv(b, a), "b < a");
CT_IS(-1 == ct_compare2fv(a, c), "a < c");
CT_IS(1 == ct_compare2fv(c, a), "c > a");
CT_IS(1 == ct_compare2fv_xy(a, b), "a > b");
CT_IS(0 == ct_compare2fv_xy(a, a), "a == a");
CT_IS(-1 == ct_compare2fv_xy(b, a), "b < a");
CT_IS(-1 == ct_compare2fv_xy(a, c), "a < c");
CT_IS(1 == ct_compare2fv_xy(c, a), "c > a");
ct_mpool_free(&pool);
return 0;
}

0 comments on commit 8ad6759

Please sign in to comment.