39 #define HEAP_LIMIT 10000
40 #define HEAP_DELTA 10000
41 #define PI 3.14159265958979323846
75 return (
int)(p_pnt_right - p_pnt_left);
85 if ((left == NULL) && (right == NULL))
111 p_pnt_left = _p_pnt_left;
112 p_pnt_right = _p_pnt_right;
113 bbox_diam = bbox_diam_proj = -1;
122 return bbox_diam_proj;
145 return p_pnt_left + (rand() % (p_pnt_right - p_pnt_left + 1));
175 for (
int ind = 0; ind < size; ind++)
177 arr[ ind ] = _arr[ ind ];
193 node->left = node->right = NULL;
220 for (
int ind = a_lo; ind <= a_hi; ind++)
221 for (
int jnd = b_lo; jnd <= b_hi; jnd++)
233 for (
int ind = a_lo; ind <= a_hi; ind++)
234 for (
int jnd = b_lo; jnd <= b_hi; jnd++)
247 for (
const gdiam_point* ind = a_lo; ind <= a_hi; ind++)
248 for (
const gdiam_point* jnd = b_lo; jnd <= b_hi; jnd++)
262 for (
const gdiam_point* ind = a_lo; ind <= a_hi; ind++)
263 for (
const gdiam_point* jnd = b_lo; jnd <= b_hi; jnd++)
288 assert(left <= right);
291 while ((right > left)
301 for (
const gdiam_point* ind = left; ind <= right; ind++)
303 node->bbox.
bound(*ind);
306 node->bbox_diam = node->bbox.
get_diam();
308 node->bbox.
center(node->center);
322 if ((*left)[ dim ] < val)
326 else if ((*right)[ dim ] >= val)
336 return left - start_left;
345 if (node->left != NULL)
350 GBBox& bb(node->bbox);
355 left_size =
split_array(node->p_pnt_left, node->p_pnt_right,
358 if (left_size <= 0.0)
360 printf(
"bb: %g %g\n",
362 printf(
"left: %p, right: %p\n",
363 node->p_pnt_left, node->p_pnt_right);
364 assert(left_size > 0);
367 if (left_size >= (node->p_pnt_right - node->p_pnt_left + 1))
369 printf(
"left size too large?\n");
371 assert(left_size < (node->p_pnt_right - node->p_pnt_left + 1));
375 node->p_pnt_left + left_size - 1);
376 node->right =
build_node(node->p_pnt_left + left_size,
387 ver[ 0 ] = ((vert & 0x1) != 0) ?
389 ver[ 1 ] = ((vert & 0x2) != 0) ?
391 ver[ 2 ] = ((vert & 0x4) != 0) ?
405 for (
int ind = 0; ind < 8; ind++)
410 for (
int jnd = 0; jnd < 8; jnd++)
512 printf(
"\tmax_diam; %g\n", max_diam);
517 if (left->
size() > threshold)
522 if (right->
size() > threshold)
561 if (sub_diam > (max_diam / 10.0))
566 return max_diam - sub_diam / 2.0;
574 #define UDM_BIG_SAMPLE 2
584 int update_diam_mode;
590 pair_diam.
init(arr[ 0 ]);
592 threshold_brute = 40;
593 update_diam_mode = mode;
652 assert(pHeap != NULL);
653 assert(_pCompFunc != NULL);
655 memset(pHeap, 0,
sizeof(
heap_t));
661 assert(pHeap->
pArr != NULL);
666 static void resize(
heap_t* pHeap,
int size)
671 if (size <= pHeap->max_size)
677 pTmp = (
voidPtr_t*)malloc(max_sz *
sizeof(
void*));
678 assert(pTmp != NULL);
679 memset(pTmp, 0, max_sz *
sizeof(
void*));
688 assert(pHeap != NULL);
690 if (pHeap->
pArr != NULL)
695 memset(pHeap, 0,
sizeof(
heap_t));
709 pHeap->
pArr[ ind ] = pData;
713 father = (ind - 1) / 2;
716 pHeap->
pArr[ father ]) <= 0)
721 pTmp = pHeap->
pArr[ ind ];
722 pHeap->
pArr[ ind ] = pHeap->
pArr[ father ];
723 pHeap->
pArr[ father ] = pTmp;
732 return pHeap->
pArr[ 0 ];
740 int ind, left, right, max_ind;
747 res = pHeap->
pArr[ 0 ];
757 while (ind < pHeap->curr_size)
773 pHeap->
pArr[ right ]) <= 0)
783 pHeap->
pArr[ max_ind ]) >= 0)
788 pTmp = pHeap->
pArr[ ind ];
789 pHeap->
pArr[ ind ] = pHeap->
pArr[ max_ind ];
790 pHeap->
pArr[ max_ind ] = pTmp;
801 assert(pHeap != NULL);
881 for (
int ind = 1; ind < size; ind++)
885 if (pnt[ dim ] < pp.
p[ dim ])
890 if (pnt[ dim ] > pp.
q[ dim ])
904 int heap_limit, heap_delta;
917 root_pair.
init(root, root);
919 heap.
push(root_pair);
924 while (heap.
size() > 0)
935 if ((count & 0x3ff) == 0)
937 if (((
int)heap.
size()) > heap_limit)
939 threshold_brute *= 2;
940 printf(
"threshold_brute: %d\n", threshold_brute);
941 heap_limit += heap_delta;
954 int heap_limit, heap_delta;
969 pair_diam.
init(pair_diam_x.
p, pair_diam_x.
q, proj);
973 root_pair.
init(root, root, proj, 0);
976 heap.
push(root_pair);
981 while (heap.
size() > 0)
993 if ((count & 0x3ff) == 0)
995 if (((
int)heap.
size()) > heap_limit)
997 threshold_brute *= 2;
998 printf(
"threshold_brute: %d\n", threshold_brute);
999 heap_limit += heap_delta;
1022 if ((left->
size() < 100) || (right->
size() < 100))
1035 for (
int ind = 0; ind <
UMD_SIZE; ind++)
1039 arr_left[ ind ] = p;
1040 arr_right[ ind ] =
q;
1045 for (
int ind = 0; ind <
UMD_SIZE; ind++)
1046 for (
int jnd = 0; jnd <
UMD_SIZE; jnd++)
1062 pair.
init(left, right);
1090 pair.
init(left, right, proj,
1110 bool f_is_left_splitable, f_is_right_splitable;
1129 assert(f_is_left_splitable || f_is_right_splitable);
1131 if (f_is_left_splitable)
1136 if (f_is_right_splitable)
1141 if (f_is_left_splitable
1142 && f_is_right_splitable)
1167 if (f_is_left_splitable)
1178 if (f_is_right_splitable)
1195 bool f_is_left_splitable, f_is_right_splitable;
1210 assert(f_is_left_splitable || f_is_right_splitable);
1212 if (f_is_left_splitable)
1217 if (f_is_right_splitable)
1222 if (f_is_left_splitable
1223 && f_is_right_splitable)
1244 if (f_is_left_splitable)
1255 if (f_is_right_splitable)
1306 assert(start != NULL);
1310 assert(p_arr != NULL);
1312 for (
int ind = 0; ind < size; ind++)
1314 p_arr[ ind ] = (
gdiam_point) & (start[ ind * 3 ]);
1364 dir[ 0 ] = pair.
p[ 0 ] - pair.
q[ 0 ];
1365 dir[ 1 ] = pair.
p[ 1 ] - pair.
q[ 1 ];
1366 dir[ 2 ] = pair.
p[ 2 ] - pair.
q[ 2 ];
1376 dir_2[ 0 ] = pair_2.
p[ 0 ] - pair_2.
q[ 0 ];
1377 dir_2[ 1 ] = pair_2.
p[ 1 ] - pair_2.
q[ 1 ];
1378 dir_2[ 2 ] = pair_2.
p[ 2 ] - pair_2.
q[ 2 ];
1383 dir_2[ 0 ] = dir_2[ 0 ] - prd * dir[ 0 ];
1384 dir_2[ 1 ] = dir_2[ 1 ] - prd * dir[ 1 ];
1385 dir_2[ 2 ] = dir_2[ 2 ] - prd * dir[ 2 ];
1416 bbox.
init(dir, dir_2, dir_3);
1419 for (
int ind = 0; ind < size; ind++)
1421 bbox.
bound(start[ ind ]);
1422 ap_bbox.
bound(start[ ind ]);
1438 if (p_ap_bbox != NULL)
1440 *p_ap_bbox = ap_bbox;
1494 if ((in[ 1 ] == 0.0) && (in[ 2 ] == 0.0))
1540 return sqrt((
x - pnt.
x) * (
x - pnt.
x)
1541 + (
y - pnt.
y) * (
y - pnt.
y));
1546 printf(
"(%g, %g)\n",
x,
y);
1550 return ((
x == pnt.
x) && (
y == pnt.
y));
1554 return ((fabs(
x - pnt.
x) < 1e-8)
1555 && (fabs(
y - pnt.
y) < 1e-8));
1568 double x1, y1, x2, y2, len;
1576 if ((x1 != 0.0) || (y1 != 0.0))
1578 len =
sqrt(x1 * x1 + y1 * y1);
1583 if ((x2 != 0.0) || (y2 != 0.0))
1585 len =
sqrt(x2 * x2 + y2 * y2);
1592 area = x1 * y2 - x2 * y1;
1608 area =
a.x * b.
y -
a.y * b.
x +
1609 a.y *
c.x -
a.x *
c.y +
1610 b.
x *
c.y -
c.x * b.
y;
1613 return ((area < (
ldouble)0.0) ? -1 :
1614 ((area > (
ldouble)0.0) ? 1 : 0));
1687 return (len1 > len2);
1699 for (
int ind = 0; ind < (int)in.size(); ind++)
1701 if (in[ ind ]->y < min_pnt->y)
1703 min_pnt = in[ ind ];
1706 else if ((in[ ind ]->y == min_pnt->
y)
1707 && (in[ ind ]->x < min_pnt->
x))
1709 min_pnt = in[ ind ];
1719 for (
int ind = 0; ind < (int)vec.size(); ind++)
1721 printf(
"-- %11d (%-11g, %-11g)\n",
1732 printf(
"(%g, %g)\n", pnt->
x, pnt->
y);
1743 for (
int ind = 0; ind < (int)(ch.size() - 2); ind++)
1744 assert(
Left(*(ch[ ind ]), *(ch[ ind + 1 ]),
1747 assert(
Left(*(ch[ ch.size() - 2 ]), *(ch[ ch.size() - 1 ]),
1749 assert(
Left(*(ch[ ch.size() - 1 ]), *(ch[ 0 ]),
1752 for (
int ind = 0; ind < (int)(in.size() - 2); ind++)
1754 for (
int jnd = 0; jnd < (int)(ch.size() - 1); jnd++)
1756 if (ch[ jnd ]->equal_real(*(in[ ind ])))
1761 if (ch[ jnd + 1 ]->equal(*(in[ ind ])))
1769 area =
Area(*(ch[ jnd ]), *(ch[ jnd + 1 ]),
1772 if (fabs(area) < 1e-12)
1777 printf(
"Failure in progress!\n\n");
1778 print_pnt(ch[ jnd ]);
1779 print_pnt(ch[ jnd + 1 ]);
1780 print_pnt(in[ ind ]);
1783 printf(
"ch[ jnd ]: (%g, %g)\n",
1784 ch[ jnd ]->x, ch[ jnd ]->y);
1785 printf(
"ch[ jnd + 1 ]: (%g, %g)\n",
1786 ch[ jnd + 1 ]->x, ch[ jnd + 1 ]->y);
1787 printf(
"ch[ ind ]: (%g, %g)\n",
1788 in[ ind ]->x, in[ ind ]->y);
1789 printf(
"Area: %g\n", (
double)
Area(*(ch[ jnd ]),
1792 printf(
"jnd: %d, jnd + 1: %d, ind: %d\n",
1797 assert(
Left(*(ch[ jnd ]), *(ch[ jnd + 1 ]),
1802 if (ch[ 0 ]->equal(*(in[ ind ])))
1807 if (ch[ ch.size() - 1 ]->equal(*(in[ ind ])))
1812 assert(
Left(*(ch[ ch.size() - 1 ]),
1817 printf(
"Convex hull seems to be ok!\n");
1830 while (start < (
int)in.size())
1832 if (in[ start ]->equal_real(*ptr))
1838 in[ dest++ ] = in[ start++ ];
1841 while ((
int)in.size() > dest)
1860 in[ dest++ ] = in[ pos++ ];
1862 while (pos < ((
int)in.size() - 1))
1864 if (in[ pos - 1 ]->equal_real(*(in[ pos ])))
1870 in[ dest++ ] = in[ pos++ ];
1873 in[ dest++ ] = in[ pos++ ];
1875 while ((
int)in.size() > dest)
1888 assert(in.size() > 1);
1893 remove_duplicate(in, in[ 0 ], 1);
1900 for (
int ind = 0; ind < (int)in.size(); ind++)
1902 assert(in[ ind ] != NULL);
1913 sort(in.begin() + 1, in.end(), comp);
1914 remove_consecutive_dup(in);
1933 out.push_back(in[ in.size() - 1 ]);
1934 out.push_back(in[ 0 ]);
1940 while (ind < (
int)in.size())
1942 if (out[ out.size() - 1 ]->equal_real(*(in[ ind ])))
1951 if (
Left(*(out[ last - 2 ]),
1955 if (! out[ last - 1 ]->equal(*(in[ ind ])))
1957 out.push_back(in[ ind ]);
1969 printf(
"ind: %d\n", ind);
1971 assert(out.size() > 2);
2000 printf(
"--- bbox 2d ----------------------------\n");
2002 for (
int ind = 0; ind < 4; ind++)
2003 printf(
"%d: (%g, %g)\n", ind,
vertices[ ind ][0],
2006 for (
int ind = 0; ind < 4; ind++)
2012 printf(
"dir %d: (%g, %g)\n", ind, dir[0],
2016 printf(
"\\----------------------------------\n");
2024 return (((b -
EPS) <
a)
2025 && (
a < (b +
EPS)));
2037 for (
int ind = 0; ind < (int)ch.size(); ind++)
2039 printf(
"%d: (%g, %g)\n", ind, ch[ ind ]->x,
2048 double x1, y1, x2, y2;
2050 u2 = (u + 1) % ch.size();
2057 ang = atan2(y2 - y1, x2 - x1);
2068 && (angles[ u ] < angles[ u - 1 ])
2069 &&
eq_real(angles[ u ], angles[ u - 1 ]))
2071 angles[ u ] = angles[ u - 1 ];
2091 double prev_angle, curr_angle;
2093 int ver, prev_vertex;
2097 prev_vertex = start_vertex - 1;
2099 if (prev_vertex < 0)
2101 prev_vertex = ch.size() - 1;
2104 prev_angle = angles[ prev_vertex ];
2109 curr_angle = angles[ ver ];
2111 if (prev_angle <= curr_angle)
2112 f_found = ((prev_angle < trg_angle)
2113 && (trg_angle <= curr_angle));
2115 f_found = ((prev_angle < trg_angle)
2116 || (trg_angle <= curr_angle));
2124 prev_angle = curr_angle;
2127 ver = (ver + 1) % ch.size();
2137 int b_ind,
double b_angle,
2141 double a_slope, b_slope, x_a, x_b, y_a, y_b;
2157 a_slope = tan(a_angle);
2158 b_slope = tan(b_angle);
2159 x_a = ch[ a_ind ]->x;
2160 y_a = ch[ a_ind ]->y;
2161 x_b = ch[ b_ind ]->x;
2162 y_b = ch[ b_ind ]->y;
2164 double a_const, b_const, x_out, y_out;
2170 a_const = y_a - a_slope * x_a;
2174 b_const = y_b - b_slope * x_b;
2178 x_out = - (a_const - b_const) / (a_slope - b_slope);
2179 y_out = a_slope * x_out + a_const;
2253 angle = angles[ ind ];
2258 if (angle_1 >= (2.0 *
PI))
2260 angle_1 -= 2.0 *
PI;
2268 if (angle_2 >= (2.0 *
PI))
2270 angle_2 -= 2.0 *
PI;
2275 if (angle_3 >= (2.0 *
PI))
2277 angle_3 -= 2.0 *
PI;
2290 assert(angles != NULL);
2293 for (u = 0; u < (int)ch.size(); u ++)
2313 min_bbox, min_area);
2318 for (u = 1; u < (int)ch.size(); u ++)
2328 tmp_bbox, tmp_area);
2332 if (min_area > tmp_area)
2334 min_area = tmp_area;
2335 min_bbox = tmp_bbox;
2360 for (
int ind = 0; ind < size; ind++)
2362 printf(
"%d: (%g, %g, %g)\n", ind,
2365 in_arr[ ind ][ 2 ]);
2369 #define ZERO_EPS (1e-6)
2371 static void construct_base_inner(
gdiam_point pnt1,
2430 construct_base_inner(pnt1, pnt2, pnt3);
2483 for (
int ind = 0; ind < size; ind++)
2485 arr[ ind ].
init(in_arr[ ind ], base_x, base_y);
2486 points.push_back(&(arr[ ind ]));
2495 double x1, y1, x2, y2;
2514 len =
sqrt(x1 * x1 + y1 * y1);
2522 len =
sqrt(x2 * x2 + y2 * y2);
2546 printf(
"should be all close to zero: %g, %g, %g\n",
2555 printf(
"real points:\n");
2558 printf(
"points on CH:\n");
2571 bbox.
init(base_proj, out_1, out_2);
2573 for (
int ind = 0; ind < size; ind++)
2575 bbox.
bound(in_arr[ ind ]);
2605 for (
int ind = 0; ind < times; ind++)
2611 printf(
"Dumping!\n");
2651 static int gcd2(
int a,
int b)
2653 if (
a == 0 ||
a == b)
2665 return gcd2(
a % b, b);
2669 return gcd2(
a, b %
a);
2674 static int gcd3(
int a,
int b,
int c)
2695 return gcd2(
a, gcd2(b,
c));
2709 if (gcd3(x_coef, y_coef, z_coef) > 1)
2714 if ((x_coef == 0) && (y_coef == 0)
2724 bb.
combine(new_dir, x_coef, y_coef, z_coef);
2728 pps.
init(new_dir, start, size);
2752 printf(
"1zero volume???\n");
2760 printf(
"2zero volume???\n");
2768 for (
int x_coef = -grid_size; x_coef <= grid_size; x_coef++)
2769 for (
int y_coef = -grid_size; y_coef <= grid_size; y_coef++)
2770 for (
int z_coef = 0; z_coef <= grid_size; z_coef++)
2771 try_direction(bb, bb_out, start, size,
2772 x_coef, y_coef, z_coef);
2787 int x_ind, y_ind, position;
2792 x_ind = (int)(pnt_bb[ 0 ] * grid_size);
2793 assert((-1 <= x_ind) && (x_ind <= grid_size));
2800 if (x_ind >= grid_size)
2802 x_ind = grid_size - 1;
2806 y_ind = (int)(pnt_bb[ 1 ] * grid_size);
2807 assert((-1 <= y_ind) && (y_ind <= grid_size));
2814 if (y_ind >= grid_size)
2816 y_ind = grid_size - 1;
2819 position = x_ind + y_ind * grid_size;
2821 if (tops[ position ] == NULL)
2823 tops[ position ] = bottoms[ position ] = pnt;
2829 if (pnt_top[ 2 ] < pnt_bb[ 2 ])
2831 tops[ position ] = pnt;
2836 if (pnt_bottom[ 2 ] > pnt_bb[ 2 ])
2838 bottoms[ position ] = pnt;
2855 int grid_size, mem_size, grid_entries;
2859 assert(sample_size > 1);
2864 grid_size = (int)
sqrt(sample_size / 2);
2866 grid_entries = grid_size * grid_size;
2867 mem_size = (int)(
sizeof(
gdiam_point) * grid_size * grid_size);
2873 assert(bottoms != NULL);
2874 assert(tops != NULL);
2875 assert(out_arr != NULL);
2877 for (
int ind = 0; ind < grid_entries; ind++)
2879 tops[ ind ] = bottoms[ ind ] = NULL;
2884 for (
int ind = 0; ind < size; ind++)
2885 register_point(start[ ind ], tops, bottoms,
2890 for (
int ind = 0; ind < grid_entries; ind++)
2892 if (tops[ ind ] == NULL)
2897 out_arr[ out_count++ ] = tops[ ind ];
2899 if (tops[ ind ] != bottoms[ ind ])
2901 out_arr[ out_count++ ] = bottoms[ ind ];
2907 while (out_count < sample_size)
2909 out_arr[ out_count++ ] = start[ rand() % size ];
2920 int grid_size,
int sample_size)
2925 if (sample_size >= size)
2944 for (
int ind = 0; ind < size; ind++)
2946 bb_new.
bound(start[ ind ]);
2962 int grid_size,
int sample_size)