1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388
|
/*
* COPYRIGHT
*
* pcb-rnd, interactive printed circuit board design
*
* 2d drafting plugin: trim objects
* pcb-rnd Copyright (C) 2018,2019 Tibor 'Igor2' Palinkas
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program 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
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Contact:
* Project page: http://repo.hu/projects/pcb-rnd
* lead developer: http://repo.hu/projects/pcb-rnd/contact.html
* mailing list: pcb-rnd (at) list.repo.hu (send "subscribe")
*/
#include "undo_old.h"
#define REMO_INVALID -1000.0
#define remo_is_valid(v) ((v) > REMO_INVALID)
/* Move a line endpoint to a new absoltue coord in an undoable way */
static void move_lp(pcb_line_t *line, int pt_idx, rnd_coord_t x, rnd_coord_t y)
{
rnd_point_t *pt;
switch(pt_idx) {
case 1: pt = &line->Point1; break;
case 2: pt = &line->Point2; break;
default:
abort();
}
pcb_undo_add_obj_to_move(PCB_OBJ_LINE_POINT, line->parent.layer, line, pt, x - pt->X, y - pt->Y);
pcb_line_pre(line);
pt->X = x;
pt->Y = y;
pcb_line_post(line);
}
/* normallize mino and maxo: make sure they are in the [0..1] range */
#define norm_minmaxo() \
do { \
if (mino < 0.0) \
mino = 0.0; \
if (maxo > 1.0) \
mino = 1.0; \
} while(0)
static int pcb_trim_line(vtp0_t *cut_edges, pcb_line_t *line, double remo_in, rnd_coord_t rem_x, rnd_coord_t rem_y)
{
int p, n;
double io[2];
double mino = 0.0, maxo = 1.0, remo = remo_in;
rnd_coord_t x, y;
if (!remo_is_valid(remo))
remo = pcb_cline_pt_offs(line, rem_x, rem_y);
for(n = 0; n < vtp0_len(cut_edges); n++) {
pcb_any_obj_t *cut_edge = (pcb_any_obj_t *)cut_edges->array[n];
p = 0;
switch(cut_edge->type) {
case PCB_OBJ_LINE:
p = pcb_intersect_cline_cline(line, (pcb_line_t *)cut_edge, NULL, io);
break;
case PCB_OBJ_ARC:
p = pcb_intersect_cline_carc(line, (pcb_arc_t *)cut_edge, NULL, io);
break;
default: continue;
}
switch(p) {
case 0: continue; /* no intersection, skip to the next potential cutting edge */
case 2:
if (io[1] < remo) {
if (io[1] > mino) mino = io[1];
}
else {
if (io[1] < maxo) maxo = io[1];
}
case 1:
if (io[0] < remo) {
if (io[0] > mino) mino = io[0];
}
else {
if (io[0] < maxo) maxo = io[0];
}
break;
}
}
norm_minmaxo();
if ((mino == 0.0) && (maxo == 1.0))
return 0; /* nothing to be done */
if (mino == maxo)
return 0; /* refuse to end up with 0-length lines */
/* mino and maxo holds the two endpoint offsets after the cuts, in respect
to the original line. Cut/split the line using them. */
if ((mino > 0.0) && (maxo < 1.0)) { /* remove (shortest) middle section */
pcb_line_t *new_line = pcb_line_dup(line->parent.layer, line);
pcb_undo_add_obj_to_create(PCB_OBJ_LINE, new_line->parent.layer, new_line, new_line);
pcb_cline_offs(line, maxo, &x, &y);
move_lp(line, 1, x, y);
pcb_cline_offs(new_line, mino, &x, &y);
move_lp(new_line, 2, x, y);
}
else if ((mino == 0.0) && (maxo < 1.0)) { /* truncate at point1 (shortest overdue) */
pcb_cline_offs(line, maxo, &x, &y);
move_lp(line, 1, x, y);
}
else if ((mino > 0.0) && (maxo == 1.0)) { /* truncate at point2 (shortest overdue) */
pcb_cline_offs(line, mino, &x, &y);
move_lp(line, 2, x, y);
}
else
return -1;
return 1;
}
/* Move arc start or end angle to a specific absolute angle (undoable) */
static void move_arc_angs(pcb_arc_t *arc, int ep, double offs)
{
double chg = offs * arc->Delta;
pcb_undo_add_obj_to_change_angles(PCB_OBJ_ARC, arc->parent.layer, arc, arc);
pcb_arc_pre(arc);
switch(ep) {
case 1:
arc->StartAngle += chg;
arc->Delta -= chg;
break;
case 2:
arc->Delta = chg;
break;
}
pcb_arc_post(arc);
}
static int pcb_trim_arc(vtp0_t *cut_edges, pcb_arc_t *arc, double remo_in, rnd_coord_t rem_x, rnd_coord_t rem_y)
{
int p, n;
double io[2];
double mino = 0.0, maxo = 1.0, remo = remo_in;
if (!remo_is_valid(remo))
remo = pcb_carc_pt_offs(arc, rem_x, rem_y);
for(n = 0; n < vtp0_len(cut_edges); n++) {
pcb_any_obj_t *cut_edge = (pcb_any_obj_t *)cut_edges->array[n];
p = 0;
switch(cut_edge->type) {
case PCB_OBJ_LINE:
p = pcb_intersect_carc_cline(arc, (pcb_line_t *)cut_edge, NULL, io);
break;
case PCB_OBJ_ARC:
p = pcb_intersect_carc_carc(arc, (pcb_arc_t *)cut_edge, NULL, io);
break;
default: continue;
}
switch(p) {
case 0: continue; /* no intersection, skip to the next potential cutting edge */
case 2:
if (io[1] < remo) {
if (io[1] > mino) mino = io[1];
}
else {
if (io[1] < maxo) maxo = io[1];
}
case 1:
if (io[0] < remo) {
if (io[0] > mino) mino = io[0];
}
else {
if (io[0] < maxo) maxo = io[0];
}
break;
}
}
norm_minmaxo();
if ((mino == 0.0) && (maxo == 1.0))
return 0; /* nothing to be done */
if (mino == maxo)
return 0; /* refuse to end up with 0-length lines */
/* mino and maxo holds the two endpoint offsets after the cuts, in respect
to the original line. Cut/split the line using them. */
if ((mino > 0.0) && (maxo < 1.0)) { /* remove (shortest) middle section */
pcb_arc_t *new_arc = pcb_arc_dup(arc->parent.layer, arc);
pcb_undo_add_obj_to_create(PCB_OBJ_ARC, new_arc->parent.layer, new_arc, new_arc);
move_arc_angs(arc, 1, maxo);
move_arc_angs(new_arc, 2, mino);
}
else if ((mino == 0.0) && (maxo < 1.0)) { /* truncate at point1 (shortest overdue) */
move_arc_angs(arc, 1, maxo);
}
else if ((mino > 0.0) && (maxo == 1.0)) { /* truncate at point2 (shortest overdue) */
move_arc_angs(arc, 2, mino);
}
else
return -1;
return 1;
}
/* Split a line in two lines at a specific offset (undoable) */
static pcb_line_t *split_lp(pcb_line_t *line, double offs)
{
rnd_coord_t x, y;
pcb_line_t *new_line = pcb_line_dup(line->parent.layer, line);
pcb_undo_add_obj_to_create(PCB_OBJ_LINE, new_line->parent.layer, new_line, new_line);
pcb_cline_offs(line, offs, &x, &y);
move_lp(line, 2, x, y);
move_lp(new_line, 1, x, y);
return new_line;
}
/* Split an arc in two arcs at a specific offset (undoable) */
static pcb_arc_t *split_arcp(pcb_arc_t *arc, double offs)
{
pcb_arc_t *new_arc = pcb_arc_dup(arc->parent.layer, arc);
pcb_undo_add_obj_to_create(PCB_OBJ_ARC, new_arc->parent.layer, new_arc, new_arc);
move_arc_angs(arc, 2, offs);
move_arc_angs(new_arc, 1, offs);
return new_arc;
}
static int near(double v1, double v2)
{
v1 -= v2;
if (v1 < 0) v1 = -v1;
return (v1 < 0.0001);
}
static int pcb_split_line(vtp0_t *cut_edges, vtp0_t *new_objs, pcb_line_t *line, double remo_in, rnd_coord_t rem_x, rnd_coord_t rem_y)
{
int p, n, numsplt = 0, res;
double io[2];
pcb_line_t *new_line;
for(n = 0; n < vtp0_len(cut_edges); n++) {
pcb_any_obj_t *cut_edge = (pcb_any_obj_t *)cut_edges->array[n];
p = 0;
switch(cut_edge->type) {
case PCB_OBJ_LINE:
p = pcb_intersect_cline_cline(line, (pcb_line_t *)cut_edge, NULL, io);
break;
case PCB_OBJ_ARC:
p = pcb_intersect_cline_carc(line, (pcb_arc_t *)cut_edge, NULL, io);
break;
default: continue;
}
switch(p) {
case 0: continue; /* no intersection, skip to the next potential cutting edge */
case 2:
if (!near(io[1], 0.0) && !near(io[1], 1.0)) {
new_line = split_lp(line, io[1]);
numsplt++;
if (new_objs != NULL) vtp0_append(new_objs, new_line);
res = pcb_split_line(cut_edges, new_objs, line, remo_in, rem_x, rem_y);
if (res > 0) numsplt += res;
res = pcb_split_line(cut_edges, new_objs, new_line, remo_in, rem_x, rem_y);
if (res > 0) numsplt += res;
break; /* can't use the other point if we did a split here, because that changes the meaning of offsets */
}
case 1:
if (!near(io[0], 0.0) && !near(io[0], 1.0)) {
new_line = split_lp(line, io[0]);
numsplt++;
if (new_objs != NULL) vtp0_append(new_objs, new_line);
res = pcb_split_line(cut_edges, new_objs, line, remo_in, rem_x, rem_y);
if (res > 0) numsplt += res;
res = pcb_split_line(cut_edges, new_objs, new_line, remo_in, rem_x, rem_y);
if (res > 0) numsplt += res;
}
break;
}
}
return numsplt;
}
static int pcb_split_arc(vtp0_t *cut_edges, vtp0_t *new_objs, pcb_arc_t *arc, double remo_in, rnd_coord_t rem_x, rnd_coord_t rem_y)
{
int p, n, numsplt = 0, res;
double io[2];
pcb_arc_t *new_arc;
for(n = 0; n < vtp0_len(cut_edges); n++) {
pcb_any_obj_t *cut_edge = (pcb_any_obj_t *)cut_edges->array[n];
p = 0;
switch(cut_edge->type) {
case PCB_OBJ_LINE:
p = pcb_intersect_carc_cline(arc, (pcb_line_t *)cut_edge, NULL, io);
break;
case PCB_OBJ_ARC:
p = pcb_intersect_carc_carc(arc, (pcb_arc_t *)cut_edge, NULL, io);
break;
default: continue;
}
switch(p) {
case 0: continue; /* no intersection, skip to the next potential cutting edge */
case 2:
if (!near(io[1], 0.0) && !near(io[1], 1.0)) {
new_arc = split_arcp(arc, io[1]);
numsplt++;
if (new_objs != NULL) vtp0_append(new_objs, new_arc);
res = pcb_split_arc(cut_edges, new_objs, arc, remo_in, rem_x, rem_y);
if (res > 0) numsplt += res;
res = pcb_split_arc(cut_edges, new_objs, new_arc, remo_in, rem_x, rem_y);
if (res > 0) numsplt += res;
break; /* can't use the other point if we did a split here, because that changes the meaning of offsets */
}
case 1:
if (!near(io[0], 0.0) && !near(io[0], 1.0)) {
new_arc = split_arcp(arc, io[0]);
numsplt++;
if (new_objs != NULL) vtp0_append(new_objs, new_arc);
res = pcb_split_arc(cut_edges, new_objs, arc, remo_in, rem_x, rem_y);
if (res > 0) numsplt += res;
res = pcb_split_arc(cut_edges, new_objs, new_arc, remo_in, rem_x, rem_y);
if (res > 0) numsplt += res;
}
break;
}
}
return numsplt;
}
int pcb_trim_split(vtp0_t *cut_edges, vtp0_t *new_objs, pcb_any_obj_t *obj, double remo, rnd_coord_t rem_x, rnd_coord_t rem_y, int trim)
{
int res = 0;
switch(obj->type) {
case PCB_OBJ_LINE:
if (trim)
res = pcb_trim_line(cut_edges, (pcb_line_t *)obj, remo, rem_x, rem_y);
else
res = pcb_split_line(cut_edges, new_objs, (pcb_line_t *)obj, remo, rem_x, rem_y);
break;
case PCB_OBJ_ARC:
if (trim)
res = pcb_trim_arc(cut_edges, (pcb_arc_t *)obj, remo, rem_x, rem_y);
else
res = pcb_split_arc(cut_edges, new_objs, (pcb_arc_t *)obj, remo, rem_x, rem_y);
break;
default:
return -1;
}
if (res > 0)
pcb_undo_inc_serial();
return res;
}
|