2018-07-27 21:00:30 +02:00
|
|
|
/*
|
|
|
|
* GDSII-Converter
|
|
|
|
* Copyright (C) 2018 Mario Hüttel <mario.huettel@gmx.net>
|
|
|
|
*
|
|
|
|
* This file is part of GDSII-Converter.
|
|
|
|
*
|
|
|
|
* GDSII-Converter is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License version 2 as
|
|
|
|
* published by the Free Software Foundation.
|
|
|
|
*
|
|
|
|
* GDSII-Converter is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
2019-11-17 15:52:28 +01:00
|
|
|
* along with GDSII-Converter. If not, see <http://www.gnu.org/licenses/>.
|
2018-07-27 21:00:30 +02:00
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @file bounding-box.c
|
|
|
|
* @brief Calculation of bounding boxes
|
|
|
|
* @author Mario Hüttel <mario.huettel@gmx.net>
|
|
|
|
*/
|
|
|
|
|
2018-12-10 19:36:00 +01:00
|
|
|
/**
|
2019-03-26 19:57:19 +01:00
|
|
|
* @addtogroup geometric
|
2018-12-10 19:36:00 +01:00
|
|
|
* @{
|
|
|
|
*/
|
|
|
|
|
2018-07-27 21:00:30 +02:00
|
|
|
#include <stdio.h>
|
2018-08-24 16:15:36 +02:00
|
|
|
#include <math.h>
|
2018-07-27 21:00:30 +02:00
|
|
|
|
2019-03-26 19:57:19 +01:00
|
|
|
#include <gds-render/geometric/bounding-box.h>
|
|
|
|
|
2019-10-28 23:57:41 +01:00
|
|
|
#define MIN(a, b) (((a) < (b)) ? (a) : (b)) /**< @brief Return smaller number */
|
|
|
|
#define MAX(a, b) (((a) > (b)) ? (a) : (b)) /**< @brief Return bigger number */
|
2018-08-24 16:15:36 +02:00
|
|
|
#define ABS_DBL(a) ((a) < 0 ? -(a) : (a))
|
2018-07-27 21:00:30 +02:00
|
|
|
|
|
|
|
void bounding_box_calculate_polygon(GList *vertices, conv_generic_to_vector_2d_t conv_func, union bounding_box *box)
|
|
|
|
{
|
2019-02-28 21:17:54 +01:00
|
|
|
double xmin = DBL_MAX, xmax = -DBL_MAX, ymin = DBL_MAX, ymax = -DBL_MAX;
|
2018-07-27 21:00:30 +02:00
|
|
|
struct vector_2d temp_vec;
|
|
|
|
GList *list_item;
|
|
|
|
|
|
|
|
/* Check for errors */
|
|
|
|
if (!conv_func || !box || !vertices)
|
|
|
|
return;
|
|
|
|
|
|
|
|
for (list_item = vertices; list_item != NULL; list_item = g_list_next(list_item)) {
|
|
|
|
/* Convert generic vertex to vector_2d */
|
2018-08-24 16:15:36 +02:00
|
|
|
if (conv_func)
|
|
|
|
conv_func((void *)list_item->data, &temp_vec);
|
|
|
|
else
|
|
|
|
vector_2d_copy(&temp_vec, (struct vector_2d *)list_item->data);
|
2018-07-27 21:00:30 +02:00
|
|
|
|
|
|
|
/* Update bounding coordinates with vertex */
|
|
|
|
xmin = MIN(xmin, temp_vec.x);
|
|
|
|
xmax = MAX(xmax, temp_vec.x);
|
|
|
|
ymin = MIN(ymin, temp_vec.y);
|
|
|
|
ymax = MAX(ymax, temp_vec.y);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Fill bounding box with results */
|
|
|
|
box->vectors.lower_left.x = xmin;
|
|
|
|
box->vectors.lower_left.y = ymin;
|
|
|
|
box->vectors.upper_right.x = xmax;
|
|
|
|
box->vectors.upper_right.y = ymax;
|
|
|
|
}
|
2018-07-27 21:08:45 +02:00
|
|
|
|
|
|
|
void bounding_box_update_box(union bounding_box *destination, union bounding_box *update)
|
|
|
|
{
|
|
|
|
if (!destination || !update)
|
|
|
|
return;
|
|
|
|
|
|
|
|
destination->vectors.lower_left.x = MIN(destination->vectors.lower_left.x,
|
|
|
|
update->vectors.lower_left.x);
|
|
|
|
destination->vectors.lower_left.y = MIN(destination->vectors.lower_left.y,
|
|
|
|
update->vectors.lower_left.y);
|
|
|
|
destination->vectors.upper_right.x = MAX(destination->vectors.upper_right.x,
|
|
|
|
update->vectors.upper_right.x);
|
|
|
|
destination->vectors.upper_right.y = MAX(destination->vectors.upper_right.y,
|
|
|
|
update->vectors.upper_right.y);
|
|
|
|
}
|
|
|
|
|
|
|
|
void bounding_box_prepare_empty(union bounding_box *box)
|
|
|
|
{
|
|
|
|
box->vectors.lower_left.x = DBL_MAX;
|
|
|
|
box->vectors.lower_left.y = DBL_MAX;
|
2019-02-28 21:17:54 +01:00
|
|
|
box->vectors.upper_right.x = -DBL_MAX;
|
|
|
|
box->vectors.upper_right.y = -DBL_MAX;
|
2018-07-27 21:08:45 +02:00
|
|
|
}
|
2018-08-24 16:15:36 +02:00
|
|
|
|
2019-08-26 20:01:32 +02:00
|
|
|
/**
|
|
|
|
* @brief Calculate path miter points for a pathwith a \p width and the anchors \p a \p b \p c.
|
|
|
|
* @param[in] a
|
|
|
|
* @param[in] b
|
|
|
|
* @param[in] c
|
|
|
|
* @param[out] m1
|
|
|
|
* @param[out] m2
|
|
|
|
* @param[in] width
|
|
|
|
* @return Miter points in \p m1 and \p m2
|
|
|
|
* @note This function is currently unused (and untested). Ignore any compiler warning regarding this function.
|
|
|
|
*/
|
2018-08-24 16:15:36 +02:00
|
|
|
static void calculate_path_miter_points(struct vector_2d *a, struct vector_2d *b, struct vector_2d *c,
|
|
|
|
struct vector_2d *m1, struct vector_2d *m2, double width)
|
|
|
|
{
|
|
|
|
double angle, angle_sin, u;
|
|
|
|
struct vector_2d ba, bc, u_vec, v_vec, ba_norm;
|
|
|
|
|
|
|
|
if (!a || !b || !c || !m1 || !m2)
|
|
|
|
return;
|
|
|
|
|
|
|
|
vector_2d_subtract(&ba, a, b);
|
|
|
|
vector_2d_subtract(&bc, c, b);
|
|
|
|
|
|
|
|
angle = vector_2d_calculate_angle_between(&ba, &bc);
|
|
|
|
|
|
|
|
if (ABS_DBL(angle) < 0.05 || ABS_DBL(angle - M_PI) < 0.1) {
|
|
|
|
/* Specail cases Don*/
|
|
|
|
vector_2d_copy(&ba_norm, &ba);
|
|
|
|
vector_2d_rotate(&ba_norm, DEG2RAD(90));
|
|
|
|
vector_2d_normalize(&ba_norm);
|
|
|
|
vector_2d_scale(&ba_norm, width/2.0);
|
|
|
|
vector_2d_add(m1, b, &ba_norm);
|
|
|
|
vector_2d_subtract(m2, b, &ba_norm);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
angle_sin = sin(angle);
|
|
|
|
u = width/(2*angle_sin);
|
|
|
|
|
|
|
|
vector_2d_copy(&u_vec, &ba);
|
|
|
|
vector_2d_copy(&v_vec, &bc);
|
|
|
|
vector_2d_normalize(&u_vec);
|
|
|
|
vector_2d_normalize(&v_vec);
|
|
|
|
vector_2d_scale(&u_vec, u);
|
|
|
|
vector_2d_scale(&v_vec, u);
|
|
|
|
|
|
|
|
vector_2d_copy(m1, b);
|
|
|
|
vector_2d_add(m1, m1, &u_vec);
|
|
|
|
vector_2d_add(m1, m1, &v_vec);
|
|
|
|
|
|
|
|
vector_2d_copy(m2, b);
|
|
|
|
vector_2d_subtract(m2, m2, &u_vec);
|
|
|
|
vector_2d_subtract(m2, m2, &v_vec);
|
|
|
|
}
|
|
|
|
|
2019-11-16 15:54:56 +01:00
|
|
|
void bounding_box_update_with_path(GList *vertices, double thickness,
|
2019-02-27 21:30:41 +01:00
|
|
|
conv_generic_to_vector_2d_t conv_func, union bounding_box *box)
|
2018-08-24 16:15:36 +02:00
|
|
|
{
|
2019-02-27 21:30:41 +01:00
|
|
|
GList *vertex_iterator;
|
|
|
|
struct vector_2d pt;
|
|
|
|
|
2019-11-09 01:50:37 +01:00
|
|
|
/* printf("Warning! Function %s not yet implemented correctly!\n", __func__); */
|
2019-02-27 21:30:41 +01:00
|
|
|
|
|
|
|
if (!vertices || !box)
|
|
|
|
return;
|
|
|
|
|
|
|
|
for (vertex_iterator = vertices; vertex_iterator != NULL; vertex_iterator = g_list_next(vertex_iterator)) {
|
|
|
|
|
|
|
|
if (conv_func != NULL)
|
|
|
|
conv_func(vertex_iterator->data, &pt);
|
|
|
|
else
|
|
|
|
(void)vector_2d_copy(&pt, (struct vector_2d *)vertex_iterator->data);
|
|
|
|
|
|
|
|
/* These are approximations.
|
|
|
|
* Used as long as miter point calculation is not fully implemented
|
|
|
|
*/
|
|
|
|
box->vectors.lower_left.x = MIN(box->vectors.lower_left.x, pt.x - thickness/2);
|
|
|
|
box->vectors.lower_left.y = MIN(box->vectors.lower_left.y, pt.y - thickness/2);
|
|
|
|
box->vectors.upper_right.x = MAX(box->vectors.upper_right.x, pt.x + thickness/2);
|
|
|
|
box->vectors.upper_right.y = MAX(box->vectors.upper_right.y, pt.y + thickness/2);
|
|
|
|
}
|
2018-08-24 16:15:36 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void bounding_box_update_point(union bounding_box *destination, conv_generic_to_vector_2d_t conv_func, void *pt)
|
|
|
|
{
|
2018-12-22 19:31:36 +01:00
|
|
|
struct vector_2d point;
|
|
|
|
|
2019-03-04 19:35:25 +01:00
|
|
|
if (!destination || !pt)
|
2018-12-22 19:31:36 +01:00
|
|
|
return;
|
|
|
|
|
2019-03-04 19:35:04 +01:00
|
|
|
if (conv_func)
|
2018-12-22 19:31:36 +01:00
|
|
|
conv_func(pt, &point);
|
|
|
|
else
|
|
|
|
(void)vector_2d_copy(&point, (struct vector_2d *)pt);
|
2018-08-24 16:15:36 +02:00
|
|
|
|
2018-12-22 19:31:36 +01:00
|
|
|
destination->vectors.lower_left.x = MIN(destination->vectors.lower_left.x, point.x);
|
|
|
|
destination->vectors.lower_left.y = MIN(destination->vectors.lower_left.y, point.y);
|
|
|
|
destination->vectors.upper_right.x = MAX(destination->vectors.upper_right.x, point.x);
|
|
|
|
destination->vectors.upper_right.y = MAX(destination->vectors.upper_right.y, point.y);
|
2018-08-24 16:15:36 +02:00
|
|
|
}
|
2018-12-10 19:36:00 +01:00
|
|
|
|
2019-11-16 15:54:56 +01:00
|
|
|
void bounding_box_get_all_points(struct vector_2d *points, union bounding_box *box)
|
|
|
|
{
|
|
|
|
if (!points || !box)
|
|
|
|
return;
|
|
|
|
|
|
|
|
points[0].x = box->vectors.lower_left.x;
|
|
|
|
points[0].y = box->vectors.lower_left.y;
|
|
|
|
points[1].x = box->vectors.upper_right.x;
|
|
|
|
points[1].y = box->vectors.lower_left.y;
|
|
|
|
points[2].x = box->vectors.upper_right.x;
|
|
|
|
points[2].y = box->vectors.upper_right.y;
|
|
|
|
points[3].x = box->vectors.lower_left.x;
|
|
|
|
points[3].y = box->vectors.upper_right.y;
|
|
|
|
}
|
|
|
|
|
2019-02-27 21:30:41 +01:00
|
|
|
void bounding_box_apply_transform(double scale, double rotation_deg, bool flip_at_x, union bounding_box *box)
|
2018-12-29 01:13:33 +01:00
|
|
|
{
|
2019-02-27 21:30:41 +01:00
|
|
|
int i;
|
2019-11-16 15:54:56 +01:00
|
|
|
struct vector_2d input_points[4];
|
|
|
|
|
|
|
|
if (!box)
|
|
|
|
return;
|
|
|
|
|
|
|
|
bounding_box_get_all_points(input_points, box);
|
|
|
|
|
|
|
|
/* Reset box */
|
|
|
|
bounding_box_prepare_empty(box);
|
|
|
|
|
|
|
|
for (i = 0; i < 4; i++) {
|
|
|
|
input_points[i].y *= (flip_at_x ? -1 : 1);
|
|
|
|
vector_2d_rotate(&input_points[i], rotation_deg * M_PI / 180.0);
|
|
|
|
vector_2d_scale(&input_points[i], scale);
|
2019-02-27 21:30:41 +01:00
|
|
|
|
2019-11-16 15:54:56 +01:00
|
|
|
bounding_box_update_point(box, NULL, &input_points[i]);
|
2019-02-27 21:30:41 +01:00
|
|
|
}
|
2018-12-29 01:13:33 +01:00
|
|
|
}
|
|
|
|
|
2018-12-10 19:36:00 +01:00
|
|
|
/** @} */
|