-
Notifications
You must be signed in to change notification settings - Fork 24
/
functions.c
3083 lines (2545 loc) · 98.1 KB
/
functions.c
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
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*-------------------------------------------------------------------------*/
/* functions.c */
/* Copyright (c) 2002 Tim Edwards, Johns Hopkins University */
/*-------------------------------------------------------------------------*/
/*-------------------------------------------------------------------------*/
/* written by Tim Edwards, 8/13/93 */
/*-------------------------------------------------------------------------*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include <limits.h>
#ifndef _MSC_VER
#include <X11/Intrinsic.h>
#include <X11/StringDefs.h>
#endif
/*-------------------------------------------------------------------------*/
/* Local includes */
/*-------------------------------------------------------------------------*/
#ifdef TCL_WRAPPER
#include <tk.h>
#endif
#include "colordefs.h"
#include "xcircuit.h"
/*----------------------------------------------------------------------*/
/* Function prototype declarations */
/*----------------------------------------------------------------------*/
#include "prototypes.h"
/*-------------------------------------------------------------------------*/
/* External Variable definitions */
/*-------------------------------------------------------------------------*/
extern Display *dpy;
extern Pixmap STIPPLE[8];
extern XCWindowData *areawin;
extern Globaldata xobjs;
extern int number_colors;
extern colorindex *colorlist;
/*------------------------------------------------------------------------*/
/* find the squared length of a wire (or distance between two points in */
/* user space). */
/*------------------------------------------------------------------------*/
long sqwirelen(XPoint *userpt1, XPoint *userpt2)
{
long xdist, ydist;
xdist = (long)userpt2->x - (long)userpt1->x;
ydist = (long)userpt2->y - (long)userpt1->y;
return (xdist * xdist + ydist * ydist);
}
/*------------------------------------------------------------------------*/
/* floating-point version of the above */
/*------------------------------------------------------------------------*/
float fsqwirelen(XfPoint *userpt1, XfPoint *userpt2)
{
float xdist, ydist;
xdist = userpt2->x - userpt1->x;
ydist = userpt2->y - userpt1->y;
return (xdist * xdist + ydist * ydist);
}
/*------------------------------------------------------------------------*/
/* Find absolute distance between two points in user space */
/*------------------------------------------------------------------------*/
int wirelength(XPoint *userpt1, XPoint *userpt2)
{
u_long xdist, ydist;
xdist = (long)(userpt2->x) - (long)(userpt1->x);
ydist = (long)(userpt2->y) - (long)(userpt1->y);
return (int)sqrt((double)(xdist * xdist + ydist * ydist));
}
/*------------------------------------------------------------------------*/
/* Find the closest (squared) distance from a point to a line */
/*------------------------------------------------------------------------*/
long finddist(XPoint *linept1, XPoint *linept2, XPoint *userpt)
{
long a, b, c, frac;
float protod;
c = sqwirelen(linept1, linept2);
a = sqwirelen(linept1, userpt);
b = sqwirelen(linept2, userpt);
frac = a - b;
if (frac >= c) return b; /* "=" is important if c = 0 ! */
else if (-frac >= c) return a;
else {
protod = (float)(c + a - b);
return (a - (long)((protod * protod) / (float)(c << 2)));
}
}
/*----------------------------------------------------------------------*/
/* Decompose an arc segment into one to four bezier curves according */
/* the approximation algorithm lifted from the paper by L. Maisonobe */
/* (spaceroots.org). This decomposition is done when an arc in a path */
/* is read from an (older) xcircuit file, or when an arc is a selected */
/* item when a path is created. Because arcs are decomposed when */
/* encountered, we assume that the arc is the last element of the path. */
/*----------------------------------------------------------------------*/
void decomposearc(pathptr thepath, XPoint *startpoint)
{
float fnc, ang1, ang2;
short ncurves, i;
arcptr thearc;
genericptr *pgen;
splineptr *newspline;
polyptr *newpoly;
double nu1, nu2, lambda1, lambda2, alpha, tansq;
XfPoint E1, E2, Ep1, Ep2;
XPoint P1;
Boolean reverse = FALSE;
pgen = thepath->plist + thepath->parts - 1;
if (ELEMENTTYPE(*pgen) != ARC) return;
thearc = TOARC(pgen);
if (thearc->radius < 0) {
reverse = TRUE;
thearc->radius = -thearc->radius;
}
fnc = (thearc->angle2 - thearc->angle1) / 90.0;
ncurves = (short)fnc;
if (fnc - (float)((int)fnc) > 0.01) ncurves++;
thepath->parts--; /* Forget the arc */
for (i = 0; i < ncurves; i++) {
if (reverse) { /* arc path is reverse direction */
if (i == 0)
ang1 = thearc->angle2;
else
ang1 -= 90;
if (i == ncurves - 1)
ang2 = thearc->angle1;
else
ang2 = ang1 - 90;
}
else { /* arc path is forward direction */
if (i == 0)
ang1 = thearc->angle1;
else
ang1 += 90;
if (i == ncurves - 1)
ang2 = thearc->angle2;
else
ang2 = ang1 + 90;
}
lambda1 = (double)ang1 * RADFAC;
lambda2 = (double)ang2 * RADFAC;
nu1 = atan2(sin(lambda1) / (double)thearc->yaxis,
cos(lambda1) / (double)thearc->radius);
nu2 = atan2(sin(lambda2) / (double)thearc->yaxis,
cos(lambda2) / (double)thearc->radius);
E1.x = (float)thearc->position.x +
(float)thearc->radius * (float)cos(nu1);
E1.y = (float)thearc->position.y +
(float)thearc->yaxis * (float)sin(nu1);
E2.x = (float)thearc->position.x +
(float)thearc->radius * (float)cos(nu2);
E2.y = (float)thearc->position.y +
(float)thearc->yaxis * (float)sin(nu2);
Ep1.x = -(float)thearc->radius * (float)sin(nu1);
Ep1.y = (float)thearc->yaxis * (float)cos(nu1);
Ep2.x = -(float)thearc->radius * (float)sin(nu2);
Ep2.y = (float)thearc->yaxis * (float)cos(nu2);
P1.x = (int)(roundf(E1.x));
P1.y = (int)(roundf(E1.y));
tansq = tan((nu2 - nu1) / 2.0);
tansq *= tansq;
alpha = sin(nu2 - nu1) * 0.33333 * (sqrt(4 + (3 * tansq)) - 1);
/* If the arc 1st point is not the same as the previous path point,
* then add a straight line to the 1st arc point (mimics PostScript
* behavior).
*/
if (startpoint && (i == 0)) {
if ((startpoint->x != P1.x) || (startpoint->y != P1.y)) {
NEW_POLY(newpoly, thepath);
polydefaults(*newpoly, 2, startpoint->x, startpoint->y);
(*newpoly)->style = thearc->style;
(*newpoly)->color = thearc->color;
(*newpoly)->width = thearc->width;
(*newpoly)->points[1].x = P1.x;
(*newpoly)->points[1].y = P1.y;
}
}
NEW_SPLINE(newspline, thepath);
splinedefaults(*newspline, 0, 0);
(*newspline)->style = thearc->style;
(*newspline)->color = thearc->color;
(*newspline)->width = thearc->width;
(*newspline)->ctrl[0].x = P1.x;
(*newspline)->ctrl[0].y = P1.y;
(*newspline)->ctrl[1].x = (int)(roundf(E1.x + alpha * Ep1.x));
(*newspline)->ctrl[1].y = (int)(roundf(E1.y + alpha * Ep1.y));
(*newspline)->ctrl[2].x = (int)(roundf(E2.x - alpha * Ep2.x));
(*newspline)->ctrl[2].y = (int)(roundf(E2.y - alpha * Ep2.y));
(*newspline)->ctrl[3].x = (int)(roundf(E2.x));
(*newspline)->ctrl[3].y = (int)(roundf(E2.y));
calcspline(*newspline);
}
/* Delete the arc */
free_single((genericptr)thearc);
}
/*----------------------------------------------------------------------*/
/* Calculate points for an arc */
/*----------------------------------------------------------------------*/
void calcarc(arcptr thearc)
{
short idx;
int sarc;
float theta, delta;
/* assume that angle2 > angle1 always: must be guaranteed by other routines */
sarc = (int)(thearc->angle2 - thearc->angle1) * RSTEPS;
thearc->number = (sarc / 360) + 1;
if (sarc % 360 != 0) thearc->number++;
delta = RADFAC * ((float)(thearc->angle2 - thearc->angle1) / (thearc->number - 1));
theta = thearc->angle1 * RADFAC;
for (idx = 0; idx < thearc->number - 1; idx++) {
thearc->points[idx].x = (float)thearc->position.x +
fabs((float)thearc->radius) * cos(theta);
thearc->points[idx].y = (float)thearc->position.y +
(float)thearc->yaxis * sin(theta);
theta += delta;
}
/* place last point exactly to avoid roundoff error */
theta = thearc->angle2 * RADFAC;
thearc->points[thearc->number - 1].x = (float)thearc->position.x +
fabs((float)thearc->radius) * cos(theta);
thearc->points[thearc->number - 1].y = (float)thearc->position.y +
(float)thearc->yaxis * sin(theta);
if (thearc->radius < 0) reversefpoints(thearc->points, thearc->number);
}
/*------------------------------------------------------------------------*/
/* Create a Bezier curve approximation from control points */
/* (using PostScript formula for Bezier cubic curve) */
/*------------------------------------------------------------------------*/
float par[INTSEGS];
float parsq[INTSEGS];
float parcb[INTSEGS];
void initsplines()
{
float t;
short idx;
for (idx = 0; idx < INTSEGS; idx++) {
t = (float)(idx + 1) / (INTSEGS + 1);
par[idx] = t;
parsq[idx] = t * t;
parcb[idx] = parsq[idx] * t;
}
}
/*------------------------------------------------------------------------*/
/* Compute spline coefficients */
/*------------------------------------------------------------------------*/
void computecoeffs(splineptr thespline, float *ax, float *bx, float *cx,
float *ay, float *by, float *cy)
{
*cx = 3.0 * (float)(thespline->ctrl[1].x - thespline->ctrl[0].x);
*bx = 3.0 * (float)(thespline->ctrl[2].x - thespline->ctrl[1].x) - *cx;
*ax = (float)(thespline->ctrl[3].x - thespline->ctrl[0].x) - *cx - *bx;
*cy = 3.0 * (float)(thespline->ctrl[1].y - thespline->ctrl[0].y);
*by = 3.0 * (float)(thespline->ctrl[2].y - thespline->ctrl[1].y) - *cy;
*ay = (float)(thespline->ctrl[3].y - thespline->ctrl[0].y) - *cy - *by;
}
/*------------------------------------------------------------------------*/
void calcspline(splineptr thespline)
{
float ax, bx, cx, ay, by, cy;
short idx;
computecoeffs(thespline, &ax, &bx, &cx, &ay, &by, &cy);
for (idx = 0; idx < INTSEGS; idx++) {
thespline->points[idx].x = ax * parcb[idx] + bx * parsq[idx] +
cx * par[idx] + (float)thespline->ctrl[0].x;
thespline->points[idx].y = ay * parcb[idx] + by * parsq[idx] +
cy * par[idx] + (float)thespline->ctrl[0].y;
}
}
/*------------------------------------------------------------------------*/
/* Find the (x,y) position and tangent rotation of a point on a spline */
/*------------------------------------------------------------------------*/
void findsplinepos(splineptr thespline, float t, XPoint *retpoint, float *retrot)
{
float ax, bx, cx, ay, by, cy;
float tsq = t * t;
float tcb = tsq * t;
double dxdt, dydt;
computecoeffs(thespline, &ax, &bx, &cx, &ay, &by, &cy);
retpoint->x = (short)(ax * tcb + bx * tsq + cx * t + (float)thespline->ctrl[0].x);
retpoint->y = (short)(ay * tcb + by * tsq + cy * t + (float)thespline->ctrl[0].y);
if (retrot != NULL) {
dxdt = (double)(3 * ax * tsq + 2 * bx * t + cx);
dydt = (double)(3 * ay * tsq + 2 * by * t + cy);
*retrot = INVRFAC * atan2(dxdt, dydt); /* reversed y, x */
if (*retrot < 0) *retrot += 360;
}
}
/*------------------------------------------------------------------------*/
/* floating-point version of the above */
/*------------------------------------------------------------------------*/
void ffindsplinepos(splineptr thespline, float t, XfPoint *retpoint)
{
float ax, bx, cx, ay, by, cy;
float tsq = t * t;
float tcb = tsq * t;
computecoeffs(thespline, &ax, &bx, &cx, &ay, &by, &cy);
retpoint->x = ax * tcb + bx * tsq + cx * t + (float)thespline->ctrl[0].x;
retpoint->y = ay * tcb + by * tsq + cy * t + (float)thespline->ctrl[0].y;
}
/*------------------------------------------------------------------------*/
/* Find the closest distance between a point and a spline and return the */
/* fractional distance along the spline of this point. */
/*------------------------------------------------------------------------*/
float findsplinemin(splineptr thespline, XPoint *upoint)
{
XfPoint *spt, flpt, newspt;
float minval = 1000000, tval, hval, ndist;
short j, ival;
flpt.x = (float)(upoint->x);
flpt.y = (float)(upoint->y);
/* get estimate from precalculated spline points */
for (spt = thespline->points; spt < thespline->points + INTSEGS;
spt++) {
ndist = fsqwirelen(spt, &flpt);
if (ndist < minval) {
minval = ndist;
ival = (short)(spt - thespline->points);
}
}
tval = (float)(ival + 1) / (INTSEGS + 1);
hval = 0.5 / (INTSEGS + 1);
/* short fixed iterative loop to converge on minimum t */
for (j = 0; j < 5; j++) {
tval += hval;
ffindsplinepos(thespline, tval, &newspt);
ndist = fsqwirelen(&newspt, &flpt);
if (ndist < minval) minval = ndist;
else {
tval -= hval * 2;
ffindsplinepos(thespline, tval, &newspt);
ndist = fsqwirelen(&newspt, &flpt);
if (ndist < minval) minval = ndist;
else tval += hval;
}
hval /= 2;
}
if (tval < 0.1) {
if ((float)sqwirelen(&(thespline->ctrl[0]), upoint) < minval) tval = 0;
}
else if (tval > 0.9) {
if ((float)sqwirelen(&(thespline->ctrl[3]), upoint) < minval) tval = 1;
}
return tval;
}
/*----------------------------------------------------------------------*/
/* Convert a polygon to a Bezier curve path */
/* Curve must be selected and there must be only one selection. */
/* */
/* Note that this routine will draw inside the perimeter of a convex */
/* hull. A routine that places spline endpoints on the polygon */
/* vertices will draw outside the perimeter of a convex hull. An */
/* optimal algorithm presumably zeros the total area between the curve */
/* and the polygon (positive and negative), but I haven't worked out */
/* what that solution is. The algorithm below seems good enough for */
/* most purposes. */
/*----------------------------------------------------------------------*/
void converttocurve()
{
genericptr *ggen;
splineptr *newspline;
polyptr thispoly;
pathptr *newpath;
short *newselect;
XPoint firstpoint, lastpoint, initpoint;
int i, numpoints;
if (areawin->selects != 1) return;
thispoly = TOPOLY(topobject->plist + (*areawin->selectlist));
if (ELEMENTTYPE(thispoly) != POLYGON) return;
if (thispoly->number < 3) return; /* Will not convert */
standard_element_delete(ERASE);
if ((thispoly->style & UNCLOSED) && (thispoly->number == 3)) {
NEW_SPLINE(newspline, topobject);
splinedefaults(*newspline, 0, 0);
(*newspline)->ctrl[0] = thispoly->points[0];
(*newspline)->ctrl[1] = thispoly->points[1];
(*newspline)->ctrl[2] = thispoly->points[1];
(*newspline)->ctrl[3] = thispoly->points[2];
}
else {
numpoints = thispoly->number;
/* If the polygon is closed but the first and last points */
/* overlap, treat the last point as if it doesn't exist. */
if (!(thispoly->style & UNCLOSED))
if ((thispoly->points[0].x == thispoly->points[thispoly->number - 1].x)
&& (thispoly->points[0].y ==
thispoly->points[thispoly->number - 1].y))
numpoints--;
NEW_PATH(newpath, topobject);
pathdefaults(*newpath, 0, 0);
(*newpath)->style = thispoly->style;
if (!(thispoly->style & UNCLOSED)) {
lastpoint = thispoly->points[numpoints - 1];
initpoint.x = (lastpoint.x + thispoly->points[0].x) / 2;
initpoint.y = (lastpoint.y + thispoly->points[0].y) / 2;
firstpoint.x = (thispoly->points[0].x
+ thispoly->points[1].x) / 2;
firstpoint.y = (thispoly->points[0].y
+ thispoly->points[1].y) / 2;
NEW_SPLINE(newspline, (*newpath));
splinedefaults(*newspline, 0, 0);
(*newspline)->ctrl[0] = initpoint;
(*newspline)->ctrl[1] = thispoly->points[0];
(*newspline)->ctrl[2] = thispoly->points[0];
(*newspline)->ctrl[3] = firstpoint;
calcspline(*newspline);
}
else
firstpoint = thispoly->points[0];
for (i = 0; i < numpoints - ((!(thispoly->style & UNCLOSED)) ?
2 : 3); i++) {
lastpoint.x = (thispoly->points[i + 1].x
+ thispoly->points[i + 2].x) / 2;
lastpoint.y = (thispoly->points[i + 1].y
+ thispoly->points[i + 2].y) / 2;
NEW_SPLINE(newspline, (*newpath));
splinedefaults(*newspline, 0, 0);
(*newspline)->ctrl[0] = firstpoint;
(*newspline)->ctrl[1] = thispoly->points[i + 1];
(*newspline)->ctrl[2] = thispoly->points[i + 1];
(*newspline)->ctrl[3] = lastpoint;
firstpoint = lastpoint;
calcspline(*newspline);
}
if (!(thispoly->style & UNCLOSED))
lastpoint = initpoint;
else
lastpoint = thispoly->points[i + 2];
NEW_SPLINE(newspline, (*newpath));
splinedefaults(*newspline, 0, 0);
(*newspline)->ctrl[0] = firstpoint;
(*newspline)->ctrl[1] = thispoly->points[i + 1];
(*newspline)->ctrl[2] = thispoly->points[i + 1];
(*newspline)->ctrl[3] = lastpoint;
}
calcspline(*newspline);
calcbbox(areawin->topinstance);
setoptionmenu();
drawarea(NULL, NULL, NULL);
}
/*----------------------------------------------------------------------*/
/* Find closest point of a polygon to the cursor */
/*----------------------------------------------------------------------*/
short closepointdistance(polyptr curpoly, XPoint *cursloc, short *mindist)
{
short curdist;
XPoint *curpt, *savept;
curpt = savept = curpoly->points;
*mindist = wirelength(curpt, cursloc);
while (++curpt < curpoly->points + curpoly->number) {
curdist = wirelength(curpt, cursloc);
if (curdist < *mindist) {
*mindist = curdist;
savept = curpt;
}
}
return (short)(savept - curpoly->points);
}
/*----------------------------------------------------------------------------*/
/* Find closest point of a polygon to the cursor */
/*----------------------------------------------------------------------------*/
short closepoint(polyptr curpoly, XPoint *cursloc)
{
short mindist;
return closepointdistance(curpoly, cursloc, &mindist);
}
/*----------------------------------------------------------------------------*/
/* Find the distance to the closest point of a polygon to the cursor */
/*----------------------------------------------------------------------------*/
short closedistance(polyptr curpoly, XPoint *cursloc)
{
short mindist;
closepointdistance(curpoly, cursloc, &mindist);
return mindist;
}
/*----------------------------------------------------------------------------*/
/* Coordinate system transformations */
/*----------------------------------------------------------------------------*/
/*------------------------------------------------------------------------------*/
/* Check screen bounds: minimum, maximum scale and translation is determined */
/* by values which fit in an X11 type XPoint (short int). If the window */
/* extremes exceed type short when mapped to user space, or if the page */
/* bounds exceed type short when mapped to X11 window space, return error. */
/*------------------------------------------------------------------------------*/
short checkbounds()
{
long lval;
/* check window-to-user space */
lval = 2 * (long)((float) (areawin->width) / areawin->vscale) +
(long)areawin->pcorner.x;
if (lval != (long)((short)lval)) return -1;
lval = 2 * (long)((float) (areawin->height) / areawin->vscale) +
(long)areawin->pcorner.y;
if (lval != (long)((short)lval)) return -1;
/* check user-to-window space */
lval = (long)((float)(topobject->bbox.lowerleft.x - areawin->pcorner.x) *
areawin->vscale);
if (lval != (long)((short)lval)) return -1;
lval = (long)areawin->height - (long)((float)(topobject->bbox.lowerleft.y -
areawin->pcorner.y) * areawin->vscale);
if (lval != (long)((short)lval)) return -1;
lval = (long)((float)(topobject->bbox.lowerleft.x + topobject->bbox.width -
areawin->pcorner.x) * areawin->vscale);
if (lval != (long)((short)lval)) return -1;
lval = (long)areawin->height - (long)((float)(topobject->bbox.lowerleft.y +
topobject->bbox.height - areawin->pcorner.y) * areawin->vscale);
if (lval != (long)((short)lval)) return -1;
return 0;
}
/*------------------------------------------------------------------------*/
/* Transform X-window coordinate to xcircuit coordinate system */
/*------------------------------------------------------------------------*/
void window_to_user(short xw, short yw, XPoint *upt)
{
float tmpx, tmpy;
tmpx = (float)xw / areawin->vscale + (float)areawin->pcorner.x;
tmpy = (float)(areawin->height - yw) / areawin->vscale +
(float)areawin->pcorner.y;
tmpx += (tmpx > 0) ? 0.5 : -0.5;
tmpy += (tmpy > 0) ? 0.5 : -0.5;
upt->x = (short)tmpx;
upt->y = (short)tmpy;
}
/*------------------------------------------------------------------------*/
/* Transform xcircuit coordinate back to X-window coordinate system */
/*------------------------------------------------------------------------*/
void user_to_window(XPoint upt, XPoint *wpt)
{
float tmpx, tmpy;
tmpx = (float)(upt.x - areawin->pcorner.x) * areawin->vscale;
tmpy = (float)areawin->height - (float)(upt.y - areawin->pcorner.y)
* areawin->vscale;
tmpx += (tmpx > 0) ? 0.5 : -0.5;
tmpy += (tmpy > 0) ? 0.5 : -0.5;
wpt->x = (short)tmpx;
wpt->y = (short)tmpy;
}
/*----------------------------------------------------------------------*/
/* Transformations in the object hierarchy */
/*----------------------------------------------------------------------*/
/*----------------------------------------------------------------------*/
/* Return rotation relative to a specific CTM */
/*----------------------------------------------------------------------*/
float UGetCTMRotation(Matrix *ctm)
{
float rads = (float)atan2((double)(ctm->d), (double)(ctm->a));
return rads / RADFAC;
}
/*----------------------------------------------------------------------*/
/* Return rotation relative to the top level */
/* Note that UTopRotation() is also the rotation relative to the window */
/* since the top-level drawing page is always upright relative to the */
/* window. Thus, there is no routine UTopDrawingRotation(). */
/*----------------------------------------------------------------------*/
float UTopRotation()
{
return UGetCTMRotation(DCTM);
}
/*----------------------------------------------------------------------*/
/* Return scale relative to a specific CTM */
/*----------------------------------------------------------------------*/
float UGetCTMScale(Matrix *ctm)
{
return (float)(sqrt((double)(ctm->a * ctm->a + ctm->d * ctm->d)));
}
/*----------------------------------------------------------------------*/
/* Return scale relative to window */
/*----------------------------------------------------------------------*/
float UTopScale()
{
return UGetCTMScale(DCTM);
}
/*----------------------------------------------------------------------*/
/* Return scale multiplied by length */
/*----------------------------------------------------------------------*/
float UTopTransScale(float length)
{
return (float)(length * UTopScale());
}
/*----------------------------------------------------------------------*/
/* Return scale relative to the top-level schematic (not the window) */
/*----------------------------------------------------------------------*/
float UTopDrawingScale()
{
Matrix lctm, wctm;
UCopyCTM(DCTM, &lctm);
UResetCTM(&wctm);
UMakeWCTM(&wctm);
InvertCTM(&wctm);
UPreMultCTMbyMat(&wctm, &lctm);
return UGetCTMScale(&wctm);
}
/*----------------------------------------------------------------------*/
/* Return position offset relative to a specific CTM */
/*----------------------------------------------------------------------*/
void UGetCTMOffset(Matrix *ctm, int *offx, int *offy)
{
if (offx) *offx = (int)ctm->c;
if (offy) *offy = (int)ctm->f;
}
/*----------------------------------------------------------------------*/
/* Return position offset relative to top-level */
/*----------------------------------------------------------------------*/
void UTopOffset(int *offx, int *offy)
{
UGetCTMOffset(DCTM, offx, offy);
}
/*----------------------------------------------------------------------*/
/* Return postion relative to the top-level schematic (not the window) */
/*----------------------------------------------------------------------*/
void UTopDrawingOffset(int *offx, int *offy)
{
Matrix lctm, wctm;
UCopyCTM(DCTM, &lctm);
UResetCTM(&wctm);
UMakeWCTM(&wctm);
InvertCTM(&wctm);
UPreMultCTMbyMat(&wctm, &lctm);
UGetCTMOffset(&wctm, offx, offy);
}
/*----------------------------------------------------------------------*/
/* Get the cursor position */
/*----------------------------------------------------------------------*/
XPoint UGetCursor()
{
Window nullwin;
int nullint, xpos, ypos;
u_int nullui;
XPoint newpos;
/* Apparently this routine can get called before the display is valid */
if ((areawin->area == NULL) || (dpy == NULL)) {
newpos.x = newpos.y = 0;
return newpos;
}
#ifdef TCL_WRAPPER
/* Don't use areawin->window; if called from inside an object */
/* (e.g., "here" in a Tcl expression), areawin->window will be */
/* an off-screen pixmap, and cause a crash. */
if (Tk_WindowId(areawin->area) == (Window)NULL) {
newpos.x = newpos.y = 0;
return newpos;
}
#ifndef _MSC_VER
XQueryPointer(dpy, Tk_WindowId(areawin->area), &nullwin, &nullwin,
&nullint, &nullint, &xpos, &ypos, &nullui);
#else
XQueryPointer_TkW32(dpy, Tk_WindowId(areawin->area), &nullwin, &nullwin,
&nullint, &nullint, &xpos, &ypos, &nullui);
#endif
#else
XQueryPointer(dpy, areawin->window, &nullwin, &nullwin, &nullint,
&nullint, &xpos, &ypos, &nullui);
#endif
newpos.x = xpos;
newpos.y = ypos;
return newpos;
}
/*----------------------------------------------------------------------*/
/* Get the cursor position and translate to user coordinates */
/*----------------------------------------------------------------------*/
XPoint UGetCursorPos()
{
XPoint winpos, userpos;
if (areawin->area == NULL) {
winpos.x = winpos.y = 0;
}
else
winpos = UGetCursor();
window_to_user(winpos.x, winpos.y, &userpos);
return userpos;
}
/*----------------------------------------------------------------------*/
/* Translate a point to the nearest snap-to grid point */
/*----------------------------------------------------------------------*/
/* user coordinates to user coordinates version */
void u2u_snap(XPoint *uvalue)
{
float tmpx, tmpy;
float tmpix, tmpiy;
if (areawin->snapto) {
tmpx = (float)uvalue->x / xobjs.pagelist[areawin->page]->snapspace;
if (tmpx > 0)
tmpix = (float)((int)(tmpx + 0.5));
else
tmpix = (float)((int)(tmpx - 0.5));
tmpy = (float)uvalue->y / xobjs.pagelist[areawin->page]->snapspace;
if (tmpy > 0)
tmpiy = (float)((int)(tmpy + 0.5));
else
tmpiy = (float)((int)(tmpy - 0.5));
tmpix *= xobjs.pagelist[areawin->page]->snapspace;
tmpix += (tmpix > 0) ? 0.5 : -0.5;
tmpiy *= xobjs.pagelist[areawin->page]->snapspace;
tmpiy += (tmpiy > 0) ? 0.5 : -0.5;
uvalue->x = (int)tmpix;
uvalue->y = (int)tmpiy;
}
}
/*------------------------------------------------------------------------*/
/* window coordinates to user coordinates version */
/*------------------------------------------------------------------------*/
void snap(short valuex, short valuey, XPoint *returnpt)
{
window_to_user(valuex, valuey, returnpt);
u2u_snap(returnpt);
}
/*------------------------------------------------------------------------*/
/* Transform object coordinates through scale, translation, and rotation */
/* This routine attempts to match the PostScript definition of trans- */
/* formation matrices. */
/*------------------------------------------------------------------------*/
/*------------------------------------------------------------------------*/
/* Current transformation matrix manipulation routines */
/*------------------------------------------------------------------------*/
void UResetCTM(Matrix *ctm)
{
ctm->a = ctm->e = 1;
ctm->b = ctm->d = 0;
ctm->c = ctm->f = 0; /* 0.5 for nearest-int real->int conversion? */
#ifdef HAVE_CAIRO
if (ctm == DCTM && areawin->redraw_ongoing)
xc_cairo_set_matrix(ctm);
#endif /* HAVE_CAIRO */
}
/*------------------------------------------------------------------------*/
void InvertCTM(Matrix *ctm)
{
float det = ctm->a * ctm->e - ctm->b * ctm->d;
float tx = ctm->b * ctm->f - ctm->c * ctm->e;
float ty = ctm->d * ctm->c - ctm->a * ctm->f;
float tmpa = ctm->a;
ctm->b = -ctm->b / det;
ctm->d = -ctm->d / det;
ctm->a = ctm->e / det;
ctm->e = tmpa / det;
ctm->c = tx / det;
ctm->f = ty / det;
#ifdef HAVE_CAIRO
if (ctm == DCTM && areawin->redraw_ongoing)
xc_cairo_set_matrix(ctm);
#endif /* HAVE_CAIRO */
}
/*------------------------------------------------------------------------*/
void UCopyCTM(fctm, tctm)
Matrix *fctm, *tctm;
{
tctm->a = fctm->a;
tctm->b = fctm->b;
tctm->c = fctm->c;
tctm->d = fctm->d;
tctm->e = fctm->e;
tctm->f = fctm->f;
#ifdef HAVE_CAIRO
if (tctm == DCTM && areawin->redraw_ongoing)
xc_cairo_set_matrix(tctm);
#endif /* HAVE_CAIRO */
}
/*-------------------------------------------------------------------------*/
/* Multiply CTM by current screen position and scale to get transformation */
/* matrix from a user point to the X11 window */
/*-------------------------------------------------------------------------*/
void UMakeWCTM(Matrix *ctm)
{
ctm->a *= areawin->vscale;
ctm->b *= areawin->vscale;
ctm->c = (ctm->c - (float)areawin->pcorner.x) * areawin->vscale
+ areawin->panx;
ctm->d *= -areawin->vscale;
ctm->e *= -areawin->vscale;
ctm->f = (float)areawin->height + ((float)areawin->pcorner.y - ctm->f) *
areawin->vscale + areawin->pany;
#ifdef HAVE_CAIRO
if (ctm == DCTM && areawin->redraw_ongoing)
xc_cairo_set_matrix(ctm);
#endif /* HAVE_CAIRO */
}
/*------------------------------------------------------------------------*/
void UMultCTM(Matrix *ctm, XPoint position, float scale, float rotate)
{
float tmpa, tmpb, tmpd, tmpe, yscale;
float mata, matb, matc;
double drot = (double)rotate * RADFAC;
yscale = abs(scale); /* -scale implies flip in x direction only */
tmpa = scale * cos(drot);
tmpb = yscale * sin(drot);
tmpd = -scale * sin(drot);
tmpe = yscale * cos(drot);
mata = ctm->a * tmpa + ctm->d * tmpb;
matb = ctm->b * tmpa + ctm->e * tmpb;
matc = ctm->c * tmpa + ctm->f * tmpb + position.x;
ctm->d = ctm->d * tmpe + ctm->a * tmpd;
ctm->e = ctm->e * tmpe + ctm->b * tmpd;
ctm->f = ctm->f * tmpe + ctm->c * tmpd + position.y;
ctm->a = mata;
ctm->b = matb;
ctm->c = matc;
#ifdef HAVE_CAIRO
if (ctm == DCTM && areawin->redraw_ongoing)
xc_cairo_set_matrix(ctm);
#endif /* HAVE_CAIRO */
}
/*----------------------------------------------------------------------*/
/* Slanting function x' = x + beta * y, y' = y */
/*----------------------------------------------------------------------*/
void USlantCTM(Matrix *ctm, float beta)
{
ctm->b += ctm->a * beta;
ctm->e += ctm->d * beta;
#ifdef HAVE_CAIRO
if (ctm == DCTM && areawin->redraw_ongoing)
xc_cairo_set_matrix(ctm);
#endif /* HAVE_CAIRO */
}
#define EPS 1e-9
/*----------------------------------------------------------------------*/
/* Transform text to make it right-side up within 90 degrees of page */
/* NOTE: This is not yet resolved, as xcircuit does not agree with */