40 #define HEAP_LIMIT 10000
41 #define HEAP_DELTA 10000
42 #define PI 3.14159265958979323846
79 return (
int)(p_pnt_right - p_pnt_left);
93 if ((left == NULL) && (right == NULL))
117 p_pnt_left = _p_pnt_left;
118 p_pnt_right = _p_pnt_right;
119 bbox_diam = bbox_diam_proj = -1;
131 return bbox_diam_proj;
161 return p_pnt_left + (rand() % (p_pnt_right - p_pnt_left + 1));
193 for (
int ind = 0; ind < size; ind++)
195 arr[ind] = _arr[ind];
212 node->left = node->right = NULL;
242 for (
int ind = a_lo; ind <= a_hi; ind++)
243 for (
int jnd = b_lo; jnd <= b_hi; jnd++)
255 for (
int ind = a_lo; ind <= a_hi; ind++)
256 for (
int jnd = b_lo; jnd <= b_hi; jnd++)
271 for (
const gdiam_point* ind = a_lo; ind <= a_hi; ind++)
272 for (
const gdiam_point* jnd = b_lo; jnd <= b_hi; jnd++)
288 for (
const gdiam_point* ind = a_lo; ind <= a_hi; ind++)
289 for (
const gdiam_point* jnd = b_lo; jnd <= b_hi; jnd++)
316 assert(left <= right);
319 while ((right > left) && (
pnt_isEqual(*right, *left)))
328 for (
const gdiam_point* ind = left; ind <= right; ind++)
330 node->bbox.
bound(*ind);
333 node->bbox_diam = node->bbox.
get_diam();
335 node->bbox.
center(node->center);
348 if ((*left)[dim] < val)
352 else if ((*right)[dim] >= val)
362 return left - start_left;
371 if (node->left != NULL)
376 GBBox& bb(node->bbox);
381 left_size =
split_array(node->p_pnt_left, node->p_pnt_right, dim, split_val);
383 if (left_size <= 0.0)
386 printf(
"left: %p, right: %p\n", node->p_pnt_left, node->p_pnt_right);
387 assert(left_size > 0);
390 if (left_size >= (node->p_pnt_right - node->p_pnt_left + 1))
392 printf(
"left size too large?\n");
394 assert(left_size < (node->p_pnt_right - node->p_pnt_left + 1));
397 node->left =
build_node(node->p_pnt_left, node->p_pnt_left + left_size - 1);
398 node->right =
build_node(node->p_pnt_left + left_size, node->p_pnt_right);
420 for (
int ind = 0; ind < 8; ind++)
425 for (
int jnd = 0; jnd < 8; jnd++)
525 printf(
"\tmax_diam; %g\n", max_diam);
531 if (left->
size() > threshold)
536 if (right->
size() > threshold)
578 if (sub_diam > (max_diam / 10.0))
583 return max_diam - sub_diam / 2.0;
591 #define UDM_BIG_SAMPLE 2
601 int update_diam_mode;
608 pair_diam.
init(arr[0]);
610 threshold_brute = 40;
611 update_diam_mode = mode;
671 assert(pHeap != NULL);
672 assert(_pCompFunc != NULL);
674 memset(pHeap, 0,
sizeof(
heap_t));
679 assert(pHeap->
pArr != NULL);
684 resize(
heap_t* pHeap,
int size)
689 if (size <= pHeap->max_size)
695 pTmp = (
voidPtr_t*)malloc(max_sz *
sizeof(
void*));
696 assert(pTmp != NULL);
697 memset(pTmp, 0, max_sz *
sizeof(
void*));
706 assert(pHeap != NULL);
708 if (pHeap->
pArr != NULL)
713 memset(pHeap, 0,
sizeof(
heap_t));
727 pHeap->
pArr[ind] = pData;
731 father = (ind - 1) / 2;
738 pTmp = pHeap->
pArr[ind];
739 pHeap->
pArr[ind] = pHeap->
pArr[father];
740 pHeap->
pArr[father] = pTmp;
749 return pHeap->
pArr[0];
757 int ind, left, right, max_ind;
764 res = pHeap->
pArr[0];
774 while (ind < pHeap->curr_size)
803 pTmp = pHeap->
pArr[ind];
804 pHeap->
pArr[ind] = pHeap->
pArr[max_ind];
805 pHeap->
pArr[max_ind] = pTmp;
816 assert(pHeap != NULL);
899 for (
int ind = 1; ind < size; ind++)
903 if (pnt[dim] < pp.
p[dim])
908 if (pnt[dim] > pp.
q[dim])
921 int heap_limit, heap_delta;
932 root_pair.
init(root, root);
934 heap.
push(root_pair);
939 while (heap.
size() > 0)
950 if ((count & 0x3ff) == 0)
952 if (((
int)heap.
size()) > heap_limit)
954 threshold_brute *= 2;
955 printf(
"threshold_brute: %d\n", threshold_brute);
956 heap_limit += heap_delta;
968 int heap_limit, heap_delta;
981 pair_diam.
init(pair_diam_x.
p, pair_diam_x.
q, proj);
985 root_pair.
init(root, root, proj, 0);
988 heap.
push(root_pair);
993 while (heap.
size() > 0)
1005 if ((count & 0x3ff) == 0)
1007 if (((
int)heap.
size()) > heap_limit)
1009 threshold_brute *= 2;
1010 printf(
"threshold_brute: %d\n", threshold_brute);
1011 heap_limit += heap_delta;
1032 if ((left->
size() < 100) || (right->
size() < 100))
1044 for (
int ind = 0; ind <
UMD_SIZE; ind++)
1054 for (
int ind = 0; ind <
UMD_SIZE; ind++)
1055 for (
int jnd = 0; jnd <
UMD_SIZE; jnd++)
1056 pair_diam.
update_diam(arr_left[ind], arr_right[jnd]);
1070 pair.
init(left, right);
1115 bool f_is_left_splitable, f_is_right_splitable;
1134 assert(f_is_left_splitable || f_is_right_splitable);
1136 if (f_is_left_splitable)
1141 if (f_is_right_splitable)
1146 if (f_is_left_splitable && f_is_right_splitable)
1160 if (f_is_left_splitable)
1167 if (f_is_right_splitable)
1178 bool f_is_left_splitable, f_is_right_splitable;
1193 assert(f_is_left_splitable || f_is_right_splitable);
1195 if (f_is_left_splitable)
1200 if (f_is_right_splitable)
1205 if (f_is_left_splitable && f_is_right_splitable)
1218 if (f_is_left_splitable)
1225 if (f_is_right_splitable)
1270 assert(start != NULL);
1274 assert(p_arr != NULL);
1276 for (
int ind = 0; ind < size; ind++)
1324 dir[0] = pair.
p[0] - pair.
q[0];
1325 dir[1] = pair.
p[1] - pair.
q[1];
1326 dir[2] = pair.
p[2] - pair.
q[2];
1336 dir_2[0] = pair_2.
p[0] - pair_2.
q[0];
1337 dir_2[1] = pair_2.
p[1] - pair_2.
q[1];
1338 dir_2[2] = pair_2.
p[2] - pair_2.
q[2];
1343 dir_2[0] = dir_2[0] - prd * dir[0];
1344 dir_2[1] = dir_2[1] - prd * dir[1];
1345 dir_2[2] = dir_2[2] - prd * dir[2];
1373 bbox.
init(dir, dir_2, dir_3);
1376 for (
int ind = 0; ind < size; ind++)
1378 bbox.
bound(start[ind]);
1379 ap_bbox.
bound(start[ind]);
1395 if (p_ap_bbox != NULL)
1397 *p_ap_bbox = ap_bbox;
1449 if ((in[1] == 0.0) && (in[2] == 0.0))
1494 return sqrt((
x - pnt.
x) * (
x - pnt.
x) + (
y - pnt.
y) * (
y - pnt.
y));
1500 printf(
"(%g, %g)\n",
x,
y);
1506 return ((
x == pnt.
x) && (
y == pnt.
y));
1512 return ((fabs(
x - pnt.
x) < 1e-8) && (fabs(
y - pnt.
y) < 1e-8));
1525 double x1, y1, x2, y2, len;
1533 if ((x1 != 0.0) || (y1 != 0.0))
1535 len =
sqrt(x1 * x1 + y1 * y1);
1540 if ((x2 != 0.0) || (y2 != 0.0))
1542 len =
sqrt(x2 * x2 + y2 * y2);
1549 area = x1 * y2 - x2 * y1;
1563 area =
a.x * b.
y -
a.y * b.
x +
a.y *
c.x -
a.x *
c.y + b.
x *
c.y -
c.x * b.
y;
1566 return ((area < (
ldouble)0.0) ? -1 : ((area > (
ldouble)0.0) ? 1 : 0));
1637 return (len1 > len2);
1648 for (
int ind = 0; ind < (int)in.size(); ind++)
1650 if (in[ind]->y < min_pnt->y)
1655 else if ((in[ind]->y == min_pnt->
y) && (in[ind]->x < min_pnt->
x))
1668 for (
int ind = 0; ind < (int)vec.size(); ind++)
1670 printf(
"-- %11d (%-11g, %-11g)\n", ind, vec[ind]->x, vec[ind]->y);
1679 printf(
"(%g, %g)\n", pnt->
x, pnt->
y);
1690 for (
int ind = 0; ind < (int)(ch.size() - 2); ind++)
1691 assert(
Left(*(ch[ind]), *(ch[ind + 1]), *(ch[ind + 2])));
1693 assert(
Left(*(ch[ch.size() - 2]), *(ch[ch.size() - 1]), *(ch[0])));
1694 assert(
Left(*(ch[ch.size() - 1]), *(ch[0]), *(ch[1])));
1696 for (
int ind = 0; ind < (int)(in.size() - 2); ind++)
1698 for (
int jnd = 0; jnd < (int)(ch.size() - 1); jnd++)
1700 if (ch[jnd]->equal_real(*(in[ind])))
1705 if (ch[jnd + 1]->equal(*(in[ind])))
1712 area =
Area(*(ch[jnd]), *(ch[jnd + 1]), *(in[ind]));
1714 if (fabs(area) < 1e-12)
1719 printf(
"Failure in progress!\n\n");
1721 print_pnt(ch[jnd + 1]);
1725 printf(
"ch[ jnd ]: (%g, %g)\n", ch[jnd]->x, ch[jnd]->y);
1726 printf(
"ch[ jnd + 1 ]: (%g, %g)\n", ch[jnd + 1]->x, ch[jnd + 1]->y);
1727 printf(
"ch[ ind ]: (%g, %g)\n", in[ind]->x, in[ind]->y);
1728 printf(
"Area: %g\n", (
double)
Area(*(ch[jnd]), *(ch[jnd + 1]), *(in[ind])));
1729 printf(
"jnd: %d, jnd + 1: %d, ind: %d\n", jnd, jnd + 1, ind);
1733 assert(
Left(*(ch[jnd]), *(ch[jnd + 1]), *(in[ind])));
1737 if (ch[0]->equal(*(in[ind])))
1742 if (ch[ch.size() - 1]->equal(*(in[ind])))
1747 assert(
Left(*(ch[ch.size() - 1]), *(ch[0]), *(in[ind])));
1750 printf(
"Convex hull seems to be ok!\n");
1760 while (start < (
int)in.size())
1762 if (in[start]->equal_real(*ptr))
1768 in[dest++] = in[start++];
1771 while ((
int)in.size() > dest)
1790 in[dest++] = in[pos++];
1792 while (pos < ((
int)in.size() - 1))
1794 if (in[pos - 1]->equal_real(*(in[pos])))
1800 in[dest++] = in[pos++];
1803 in[dest++] = in[pos++];
1805 while ((
int)in.size() > dest)
1818 assert(in.size() > 1);
1823 remove_duplicate(in, in[0], 1);
1830 for (
int ind = 0; ind < (int)in.size(); ind++)
1832 assert(in[ind] != NULL);
1843 sort(in.begin() + 1, in.end(), comp);
1844 remove_consecutive_dup(in);
1863 out.push_back(in[in.size() - 1]);
1864 out.push_back(in[0]);
1870 while (ind < (
int)in.size())
1872 if (out[out.size() - 1]->equal_real(*(in[ind])))
1881 if (
Left(*(out[last - 2]), *(out[last - 1]), *(in[ind])))
1883 if (!out[last - 1]->equal(*(in[ind])))
1885 out.push_back(in[ind]);
1897 printf(
"ind: %d\n", ind);
1899 assert(out.size() > 2);
1927 printf(
"--- bbox 2d ----------------------------\n");
1929 for (
int ind = 0; ind < 4; ind++)
1932 for (
int ind = 0; ind < 4; ind++)
1938 printf(
"dir %d: (%g, %g)\n", ind, dir[0], dir[1]);
1941 printf(
"\\----------------------------------\n");
1950 return (((b -
EPS) <
a) && (
a < (b +
EPS)));
1963 for (
int ind = 0; ind < (int)ch.size(); ind++)
1965 printf(
"%d: (%g, %g)\n", ind, ch[ind]->x, ch[ind]->y);
1974 double x1, y1, x2, y2;
1976 u2 = (u + 1) % ch.size();
1983 ang = atan2(y2 - y1, x2 - x1);
1993 if ((u > 0) && (angles[u] < angles[u - 1]) &&
eq_real(angles[u], angles[u - 1]))
1995 angles[u] = angles[u - 1];
2015 double prev_angle, curr_angle;
2017 int ver, prev_vertex;
2021 prev_vertex = start_vertex - 1;
2023 if (prev_vertex < 0)
2025 prev_vertex = ch.size() - 1;
2028 prev_angle = angles[prev_vertex];
2033 curr_angle = angles[ver];
2035 if (prev_angle <= curr_angle)
2036 f_found = ((prev_angle < trg_angle) && (trg_angle <= curr_angle));
2038 f_found = ((prev_angle < trg_angle) || (trg_angle <= curr_angle));
2046 prev_angle = curr_angle;
2049 ver = (ver + 1) % ch.size();
2065 double a_slope, b_slope, x_a, x_b, y_a, y_b;
2081 a_slope = tan(a_angle);
2082 b_slope = tan(b_angle);
2088 double a_const, b_const, x_out, y_out;
2094 a_const = y_a - a_slope * x_a;
2098 b_const = y_b - b_slope * x_b;
2102 x_out = -(a_const - b_const) / (a_slope - b_slope);
2103 y_out = a_slope * x_out + a_const;
2169 angle = angles[ind];
2174 if (angle_1 >= (2.0 *
PI))
2176 angle_1 -= 2.0 *
PI;
2184 if (angle_2 >= (2.0 *
PI))
2186 angle_2 -= 2.0 *
PI;
2191 if (angle_3 >= (2.0 *
PI))
2193 angle_3 -= 2.0 *
PI;
2205 assert(angles != NULL);
2208 for (u = 0; u < (int)ch.size(); u++)
2224 get_bbox(0, angles[0],
s, ang1,
v, ang2, t, ang3, min_bbox, min_area);
2229 for (u = 1; u < (int)ch.size(); u++)
2235 get_bbox(u, angles[u],
s, ang1,
v, ang2, t, ang3, tmp_bbox, tmp_area);
2239 if (min_area > tmp_area)
2241 min_area = tmp_area;
2242 min_bbox = tmp_bbox;
2265 for (
int ind = 0; ind < size; ind++)
2267 printf(
"%d: (%g, %g, %g)\n", ind, in_arr[ind][0], in_arr[ind][1], in_arr[ind][2]);
2271 #define ZERO_EPS (1e-6)
2326 construct_base_inner(pnt1, pnt2, pnt3);
2375 for (
int ind = 0; ind < size; ind++)
2377 arr[ind].
init(in_arr[ind], base_x, base_y);
2378 points.push_back(&(arr[ind]));
2388 double x1, y1, x2, y2;
2403 len =
sqrt(x1 * x1 + y1 * y1);
2411 len =
sqrt(x2 * x2 + y2 * y2);
2431 if ((!(fabs(
pnt_dot_prod(base_proj, out_1)) < 1e-6)) ||
2435 printf(
"should be all close to zero: %g, %g, %g\n",
2444 printf(
"real points:\n");
2447 printf(
"points on CH:\n");
2460 bbox.
init(base_proj, out_1, out_2);
2462 for (
int ind = 0; ind < size; ind++)
2464 bbox.
bound(in_arr[ind]);
2497 for (
int ind = 0; ind < times; ind++)
2503 printf(
"Dumping!\n");
2544 if (
a == 0 ||
a == b)
2556 return gcd2(
a % b, b);
2560 return gcd2(
a, b %
a);
2565 gcd3(
int a,
int b,
int c)
2586 return gcd2(
a, gcd2(b,
c));
2600 if (gcd3(x_coef, y_coef, z_coef) > 1)
2605 if ((x_coef == 0) && (y_coef == 0) && (z_coef == 0))
2614 bb.
combine(new_dir, x_coef, y_coef, z_coef);
2618 pps.
init(new_dir, start, size);
2641 printf(
"1zero volume???\n");
2649 printf(
"2zero volume???\n");
2657 for (
int x_coef = -grid_size; x_coef <= grid_size; x_coef++)
2658 for (
int y_coef = -grid_size; y_coef <= grid_size; y_coef++)
2659 for (
int z_coef = 0; z_coef <= grid_size; z_coef++)
2660 try_direction(bb, bb_out, start, size, x_coef, y_coef, z_coef);
2675 int x_ind, y_ind, position;
2680 x_ind = (int)(pnt_bb[0] * grid_size);
2681 assert((-1 <= x_ind) && (x_ind <= grid_size));
2688 if (x_ind >= grid_size)
2690 x_ind = grid_size - 1;
2694 y_ind = (int)(pnt_bb[1] * grid_size);
2695 assert((-1 <= y_ind) && (y_ind <= grid_size));
2702 if (y_ind >= grid_size)
2704 y_ind = grid_size - 1;
2707 position = x_ind + y_ind * grid_size;
2709 if (tops[position] == NULL)
2711 tops[position] = bottoms[position] = pnt;
2717 if (pnt_top[2] < pnt_bb[2])
2719 tops[position] = pnt;
2724 if (pnt_bottom[2] > pnt_bb[2])
2726 bottoms[position] = pnt;
2740 int grid_size, mem_size, grid_entries;
2744 assert(sample_size > 1);
2749 grid_size = (int)
sqrt(sample_size / 2);
2751 grid_entries = grid_size * grid_size;
2752 mem_size = (int)(
sizeof(
gdiam_point) * grid_size * grid_size);
2758 assert(bottoms != NULL);
2759 assert(tops != NULL);
2760 assert(out_arr != NULL);
2762 for (
int ind = 0; ind < grid_entries; ind++)
2764 tops[ind] = bottoms[ind] = NULL;
2769 for (
int ind = 0; ind < size; ind++)
2770 register_point(start[ind], tops, bottoms, grid_size, bb);
2774 for (
int ind = 0; ind < grid_entries; ind++)
2776 if (tops[ind] == NULL)
2781 out_arr[out_count++] = tops[ind];
2783 if (tops[ind] != bottoms[ind])
2785 out_arr[out_count++] = bottoms[ind];
2791 while (out_count < sample_size)
2793 out_arr[out_count++] = start[rand() % size];
2808 if (sample_size >= size)
2827 for (
int ind = 0; ind < size; ind++)
2829 bb_new.
bound(start[ind]);