18 double (* restrict vertices)[3],
size_t num_vertices,
19 double * restrict middle_point) {
25 for (
size_t i = 0; i < num_vertices; ++i) {
27 middle_point[0] += vertices[i][0];
28 middle_point[1] += vertices[i][1];
29 middle_point[2] += vertices[i][2];
32 double scale = 1.0 / sqrt(middle_point[0] * middle_point[0] +
33 middle_point[1] * middle_point[1] +
34 middle_point[2] * middle_point[2]);
36 middle_point[0] *= scale;
37 middle_point[1] *= scale;
38 middle_point[2] *= scale;
100 double * vertices,
size_t num_vertices,
double triangle[][3],
108 double * x_furthest =
127 rotate_vector(x_middle, -(2.0 * M_PI) / 3.0, x_t[0], x_t[2]);
132 double min_cos_angle = -2.0;
133 double best_x_t[3][3] = {{0.0,0.0,0.0},{0.0,0.0,0.0},{0.0,0.0,0.0}};
134 double best_a[2] = {0.0, 0.0};
136 double * a =
xmalloc(3 * num_vertices * 3 *
sizeof(*a));
141 double d_rot_angle = M_PI / (double)(3 * num_tests);
142 for (
size_t test_idx = 0; test_idx < num_tests; ++test_idx) {
145 double curr_rot_angle = (double)test_idx * d_rot_angle;
150 for (
size_t i = 0; i < 3; ++i) {
152 memcpy(a + i*3*num_vertices, vertices, 3*num_vertices *
sizeof(*a));
175 lapack_int n = 3, nrhs = (lapack_int) num_vertices, lda = n, ldx = n, ipiv[3];
176 A[0][0] = x_middle[0], A[0][1] = x_middle[1], A[0][2] = x_middle[2];
177 A[1][0] = x_t_[i][0], A[1][1] = x_t_[i][1], A[1][2] = x_t_[i][2];
184 LAPACK_COL_MAJOR, n, nrhs, &A[0][0], lda,
185 &ipiv[0], a + i * 3 * num_vertices, ldx),
186 "ERROR: internal error (could not solve linear 3x3 system)")
195 double curr_min_cos_angle[2] = {2.0, 2.0};
196 double * curr_x_furthest[2] = {NULL, NULL};
197 double curr_best_a[2][2];
200 for (
size_t i = 0; i < 3 * num_vertices; ++i) {
202 int flag = a[1+3*i] < 0.0;
203 double scale = 1.0 / sqrt(a[0+3*i]*a[0+3*i] + a[1+3*i]*a[1+3*i]);
204 double cos_angle = a[0+3*i] * scale;
206 if (cos_angle < curr_min_cos_angle[flag]) {
207 curr_min_cos_angle[flag] = cos_angle;
208 curr_x_furthest[flag] = vertices + 3 * (i%num_vertices);
209 curr_best_a[flag][0] = cos_angle;
210 curr_best_a[flag][1] = a[1+3*i]*scale;
214 int flag = curr_min_cos_angle[0] < curr_min_cos_angle[1];
218 if (curr_x_furthest[flag] != NULL) {
219 double x_triangle[3][3];
222 print_bnd_triangle(vertices, num_vertices, &x_triangle[0][0], test_idx);
227 if ((curr_x_furthest[flag] != NULL) &&
228 (curr_min_cos_angle[flag] > min_cos_angle)) {
229 memcpy(&best_x_t[0][0], &x_t_[0][0], 9 *
sizeof(x_t_[0][0]));
230 min_cos_angle = curr_min_cos_angle[flag];
231 x_furthest = curr_x_furthest[flag];
232 best_a[0] = curr_best_a[flag][0];
233 best_a[1] = curr_best_a[flag][1];
240 min_cos_angle > -2.0,
241 "ERROR: internal error (could not fine any matching triangle)")