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
|
/*---------------------------------------------------------------------------*
| PDFlib - A library for generating PDF on the fly |
+---------------------------------------------------------------------------+
| Copyright (c) 1997-2006 Thomas Merz and PDFlib GmbH. All rights reserved. |
+---------------------------------------------------------------------------+
| |
| This software is subject to the PDFlib license. It is NOT in the |
| public domain. Extended versions and commercial licenses are |
| available, please check http://www.pdflib.com. |
| |
*---------------------------------------------------------------------------*/
/* $Id: pc_geom.c,v 1.2 2009/10/20 18:12:26 scuri Exp $
*
* Various geometry routines
*
*/
#include "pc_util.h"
#include "pc_geom.h"
/* ---------------------- matrix functions ----------------------------- */
pdc_bool
pdc_is_identity_matrix(pdc_matrix *m)
{
return PDC_FLOAT_ISNULL(m->a - 1) &&
PDC_FLOAT_ISNULL(m->b) &&
PDC_FLOAT_ISNULL(m->c) &&
PDC_FLOAT_ISNULL(m->d - 1) &&
PDC_FLOAT_ISNULL(m->e) &&
PDC_FLOAT_ISNULL(m->f);
}
/* identity matrix */
void
pdc_identity_matrix(pdc_matrix *M)
{
M->a = 1;
M->b = 0;
M->c = 0;
M->d = 1;
M->e = 0;
M->f = 0;
}
/* translation matrix */
void
pdc_translation_matrix(pdc_scalar tx, pdc_scalar ty, pdc_matrix *M)
{
M->a = 1;
M->b = 0;
M->c = 0;
M->d = 1;
M->e = tx;
M->f = ty;
}
/* scale matrix */
void
pdc_scale_matrix(pdc_scalar sx, pdc_scalar sy, pdc_matrix *M)
{
M->a = sx;
M->b = 0;
M->c = 0;
M->d = sy;
M->e = 0;
M->f = 0;
}
/* rotation matrix */
void
pdc_rotation_matrix(pdc_scalar alpha, pdc_matrix *M)
{
pdc_scalar phi, c, s;
phi = alpha * PDC_DEG2RAD;
c = cos(phi);
s = sin(phi);
M->a = c;
M->b = s;
M->c = -s;
M->d = c;
M->e = 0;
M->f = 0;
}
/* skew matrix */
void
pdc_skew_matrix(pdc_scalar alpha, pdc_scalar beta, pdc_matrix *M)
{
M->a = 1;
M->b = tan(alpha * PDC_DEG2RAD);
M->c = tan(beta * PDC_DEG2RAD);
M->d = 1;
M->e = 0;
M->f = 0;
}
/* N = M * N */
void
pdc_multiply_matrix(const pdc_matrix *M, pdc_matrix *N)
{
pdc_matrix result;
result.a = M->a * N->a + M->b * N->c;
result.b = M->a * N->b + M->b * N->d;
result.c = M->c * N->a + M->d * N->c;
result.d = M->c * N->b + M->d * N->d;
result.e = M->e * N->a + M->f * N->c + N->e;
result.f = M->e * N->b + M->f * N->d + N->f;
*N = result;
}
/* L = M * N */
void
pdc_multiply_matrix3(pdc_matrix *L, const pdc_matrix *M, const pdc_matrix *N)
{
L->a = M->a * N->a + M->b * N->c;
L->b = M->a * N->b + M->b * N->d;
L->c = M->c * N->a + M->d * N->c;
L->d = M->c * N->b + M->d * N->d;
L->e = M->e * N->a + M->f * N->c + N->e;
L->f = M->e * N->b + M->f * N->d + N->f;
}
/* M = [a b c d e f] * M; */
void
pdc_multiply_6s_matrix(pdc_matrix *M, pdc_scalar a, pdc_scalar b, pdc_scalar c,
pdc_scalar d, pdc_scalar e, pdc_scalar f)
{
pdc_matrix result;
result.a = a * M->a + b * M->c;
result.b = a * M->b + b * M->d;
result.c = c * M->a + d * M->c;
result.d = c * M->b + d * M->d;
result.e = e * M->a + f * M->c + M->e;
result.f = e * M->b + f * M->d + M->f;
*M = result;
}
/* invert M and store the result in N */
void
pdc_invert_matrix(pdc_core *pdc, pdc_matrix *N, pdc_matrix *M)
{
pdc_scalar det = M->a * M->d - M->b * M->c;
if (fabs(det) < PDC_SMALLREAL * PDC_SMALLREAL)
pdc_error(pdc, PDC_E_INT_INVMATRIX,
pdc_errprintf(pdc, "%f %f %f %f %f %f",
M->a, M->b, M->c, M->d, M->e, M->f),
0, 0, 0);
N->a = M->d/det;
N->b = -M->b/det;
N->c = -M->c/det;
N->d = M->a/det;
N->e = -(M->e * N->a + M->f * N->c);
N->f = -(M->e * N->b + M->f * N->d);
}
/* debug print */
void
pdc_print_matrix(const char *name, const pdc_matrix *M)
{
printf("%s: a=%g, b=%g, c=%g, d=%g, e=%g, f=%g\n",
name, M->a, M->b, M->c, M->d, M->e, M->f);
}
/* transform scalar */
pdc_scalar
pdc_transform_scalar(const pdc_matrix *M, pdc_scalar s)
{
pdc_scalar det = M->a * M->d - M->b * M->c;
return sqrt(fabs(det)) * s;
}
/* transform point */
void
pdc_transform_point(const pdc_matrix *M, pdc_scalar x, pdc_scalar y,
pdc_scalar *tx, pdc_scalar *ty)
{
*tx = M->a * x + M->c * y + M->e;
*ty = M->b * x + M->d * y + M->f;
}
/* transform vector */
void
pdc_transform_vector(const pdc_matrix *M, pdc_vector *v, pdc_vector *tv)
{
pdc_scalar tx = M->a * v->x + M->c * v->y + M->e;
pdc_scalar ty = M->b * v->x + M->d * v->y + M->f;
if (tv)
{
tv->x = tx;
tv->y = ty;
}
else
{
v->x = tx;
v->y = ty;
}
}
/* transform relative vector */
void
pdc_transform_rvector(const pdc_matrix *M, pdc_vector *v, pdc_vector *tv)
{
pdc_scalar tx = M->a * v->x + M->c * v->y;
pdc_scalar ty = M->b * v->x + M->d * v->y;
if (tv)
{
tv->x = tx;
tv->y = ty;
}
else
{
v->x = tx;
v->y = ty;
}
}
/* get length of vector */
pdc_scalar
pdc_get_vector_length(pdc_vector *start, pdc_vector *end)
{
pdc_scalar dx = end->x - start->x;
pdc_scalar dy = end->y - start->y;
return sqrt(dx * dx + dy * dy);
}
/* ---------------------- utility functions ----------------------------- */
void
pdc_place_element(pdc_fitmethod method, pdc_scalar minfscale,
const pdc_box *fitbox, const pdc_vector *fitrelpos,
const pdc_vector *elemsize, const pdc_vector *elemrelpos,
pdc_box *elembox, pdc_vector *scale)
{
pdc_vector refpos;
pdc_scalar width, height, det, fscale = 1.0;
pdc_bool xscaling = pdc_false;
/* reference position in fitbox */
width = fitbox->ur.x - fitbox->ll.x;
height = fitbox->ur.y - fitbox->ll.y;
refpos.x = fitbox->ll.x + fitrelpos->x * width;
refpos.y = fitbox->ll.y + fitrelpos->y * height;
/* first check */
switch (method)
{
case pdc_entire:
case pdc_slice:
case pdc_meet:
case pdc_tauto:
if (fabs(width) > PDC_FLOAT_PREC && fabs(height) > PDC_FLOAT_PREC)
{
if (method != pdc_entire)
{
det = elemsize->x * height - elemsize->y * width;
xscaling = (method == pdc_slice && det <= 0) ||
((method == pdc_meet || method == pdc_tauto) &&
det > 0) ? pdc_true : pdc_false;
if (xscaling)
fscale = width / elemsize->x;
else
fscale = height / elemsize->y;
}
if (method == pdc_tauto)
{
if(fscale >= 1.0)
{
method = pdc_nofit;
}
else if (fscale < minfscale)
{
method = pdc_meet;
}
}
}
else
{
method = pdc_nofit;
}
break;
default:
break;
}
/* calculation */
switch (method)
{
/* entire box is covered by entire element */
case pdc_entire:
*elembox = *fitbox;
scale->x = width / elemsize->x;
scale->y = height / elemsize->y;
return;
/* fit into and preserve aspect ratio */
case pdc_slice:
case pdc_meet:
if (xscaling)
height = fscale * elemsize->y;
else
width = fscale * elemsize->x;
scale->x = fscale;
scale->y = fscale;
break;
/* fit into and doesn't preserve aspect ratio */
case pdc_tauto:
if (xscaling)
{
height = elemsize->y;
scale->x = fscale;
scale->y = 1.0;
}
else
{
width = elemsize->x;
scale->x = 1.0;
scale->y = fscale;
}
break;
/* only positioning */
case pdc_nofit:
case pdc_clip:
width = elemsize->x;
height = elemsize->y;
scale->x = 1.0;
scale->y = 1.0;
break;
}
/* placed element box */
elembox->ll.x = refpos.x - elemrelpos->x * width;
elembox->ll.y = refpos.y - elemrelpos->y * height;
elembox->ur.x = refpos.x + (1.0 - elemrelpos->x) * width;
elembox->ur.y = refpos.y + (1.0 - elemrelpos->y) * height;
}
void
pdc_box2polyline(const pdc_matrix *M, const pdc_box *box, pdc_vector *polyline)
{
pdc_scalar x[4], y[4];
/* counterclockwise order */
if (M != NULL)
{
pdc_transform_point(M, box->ll.x, box->ll.y, &x[0], &y[0]);
pdc_transform_point(M, box->ur.x, box->ll.y, &x[1], &y[1]);
pdc_transform_point(M, box->ur.x, box->ur.y, &x[2], &y[2]);
pdc_transform_point(M, box->ll.x, box->ur.y, &x[3], &y[3]);
polyline[0].x = x[0];
polyline[0].y = y[0];
polyline[1].x = x[1];
polyline[1].y = y[1];
polyline[2].x = x[2];
polyline[2].y = y[2];
polyline[3].x = x[3];
polyline[3].y = y[3];
polyline[4] = polyline[0];
}
else
{
polyline[0].x = box->ll.x;
polyline[0].y = box->ll.y;
polyline[1].x = box->ur.x;
polyline[1].y = box->ll.y;
polyline[2].x = box->ur.x;
polyline[2].y = box->ur.y;
polyline[3].x = box->ll.x;
polyline[3].y = box->ur.y;
polyline[4] = polyline[0];
}
}
void *
pdc_delete_polylinelist(pdc_core *pdc, pdc_polyline *polylinelist, int nplines)
{
int i;
if (polylinelist != NULL)
{
for (i = 0; i < nplines; i++)
pdc_free(pdc, polylinelist[i].p);
pdc_free(pdc, polylinelist);
}
return NULL;
}
void
pdc_init_box(pdc_box *box)
{
box->ll.x = PDC_FLOAT_MAX;
box->ll.y = PDC_FLOAT_MAX;
box->ur.x = PDC_FLOAT_MIN;
box->ur.y = PDC_FLOAT_MIN;
}
void
pdc_adapt_box(pdc_box *box, const pdc_vector *v)
{
if (v->x < box->ll.x)
box->ll.x = v->x;
if (v->y < box->ll.y)
box->ll.y = v->y;
if (v->x > box->ur.x)
box->ur.x = v->x;
if (v->y > box->ur.y)
box->ur.y = v->y;
}
void
pdc_normalize_box(pdc_box *box, pdc_scalar ydir)
{
pdc_scalar sxy;
if (box->ll.x > box->ur.x)
{
sxy = box->ll.x;
box->ll.x = box->ur.x;
box->ur.x = sxy;
}
if (ydir * box->ll.y > ydir * box->ur.y)
{
sxy = box->ll.y;
box->ll.y = box->ur.y;
box->ur.y = sxy;
}
}
void
pdc_transform_box(const pdc_matrix *M, pdc_box *box, pdc_box *tbox)
{
pdc_vector polyline[5];
pdc_box tmpbox;
int i;
pdc_box2polyline(NULL, box, polyline);
pdc_init_box(&tmpbox);
for (i = 0; i < 4; i++)
{
pdc_transform_vector(M, &polyline[i], NULL);
pdc_adapt_box(&tmpbox, &polyline[i]);
}
if (tbox)
*tbox = tmpbox;
else
*box = tmpbox;
}
/* --------------------------- rectangles --------------------------- */
pdc_bool
pdc_rect_isnull(const pdc_rectangle *r)
{
if (!r)
return pdc_true;
return
(r->llx == 0 && r->lly == 0 &&
r->urx == 0 && r->ury == 0);
}
pdc_bool
pdc_rect_contains(const pdc_rectangle *r1, const pdc_rectangle *r2)
{
return
(r1->llx <= r2->llx && r1->lly <= r2->lly &&
r1->urx >= r2->urx && r1->ury >= r2->ury);
}
void
pdc_rect_copy(pdc_rectangle *r1, const pdc_rectangle *r2)
{
r1->llx = r2->llx;
r1->lly = r2->lly;
r1->urx = r2->urx;
r1->ury = r2->ury;
}
void
pdc_rect_init(pdc_rectangle *r, pdc_scalar llx, pdc_scalar lly,
pdc_scalar urx, pdc_scalar ury)
{
r->llx = llx;
r->lly = lly;
r->urx = urx;
r->ury = ury;
}
pdc_bool
pdc_rect_intersect(
pdc_rectangle *result,
const pdc_rectangle *r1,
const pdc_rectangle *r2)
{
if (r1->urx <= r2->llx ||
r2->urx <= r1->llx ||
r1->ury <= r2->lly ||
r2->ury <= r1->lly)
{
if (result)
{
result->llx = result->lly = result->urx = result->ury = 0;
}
return pdc_false;
}
if (result)
{
result->llx = MAX(r1->llx, r2->llx);
result->urx = MIN(r1->urx, r2->urx);
result->lly = MAX(r1->lly, r2->lly);
result->ury = MIN(r1->ury, r2->ury);
}
return pdc_true;
}
void
pdc_rect_transform(const pdc_matrix *M, const pdc_rectangle *r1,
pdc_rectangle *r2)
{
pdc_scalar x[4], y[4];
int i;
pdc_transform_point(M, r1->llx, r1->lly, &x[0], &y[0]);
pdc_transform_point(M, r1->urx, r1->lly, &x[1], &y[1]);
pdc_transform_point(M, r1->urx, r1->ury, &x[2], &y[2]);
pdc_transform_point(M, r1->llx, r1->ury, &x[3], &y[3]);
pdc_rect_init(r2, PDC_FLOAT_MAX, PDC_FLOAT_MAX,
PDC_FLOAT_MIN, PDC_FLOAT_MIN);
for (i = 0; i < 4; i++)
{
if (x[i] < r2->llx)
r2->llx = x[i];
if (y[i] < r2->lly)
r2->lly = y[i];
if (x[i] > r2->urx)
r2->urx = x[i];
if (y[i] > r2->ury)
r2->ury = y[i];
}
}
void pdc_rect_normalize(pdc_rectangle *r)
{
double aux;
if (r->urx < r->llx)
{
aux = r->llx; r->llx = r->urx; r->urx = aux;
}
if (r->ury < r->lly)
{
aux = r->lly; r->lly = r->ury; r->ury = aux;
}
}
void pdc_rect_normalize2(pdc_rectangle *dst, const pdc_rectangle *src)
{
if (src->llx < src->urx)
{
dst->llx = src->llx;
dst->urx = src->urx;
}
else
{
dst->llx = src->urx;
dst->urx = src->llx;
}
if (src->lly < src->ury)
{
dst->lly = src->lly;
dst->ury = src->ury;
}
else
{
dst->lly = src->ury;
dst->ury = src->lly;
}
}
void
pdc_polyline2rect(const pdc_vector *polyline, int np, pdc_rectangle *r)
{
int i;
pdc_rect_init(r, PDC_FLOAT_MAX, PDC_FLOAT_MAX,
PDC_FLOAT_MIN, PDC_FLOAT_MIN);
for (i = 0; i < np; i++)
{
if (polyline[i].x < r->llx)
r->llx = polyline[i].x;
if (polyline[i].y < r->lly)
r->lly = polyline[i].y;
if (polyline[i].x > r->urx)
r->urx = polyline[i].x;
if (polyline[i].y > r->ury)
r->ury = polyline[i].y;
}
}
void
pdc_rect2polyline(const pdc_matrix *M, const pdc_rectangle *r,
pdc_vector *polyline)
{
pdc_scalar x[4], y[4];
/* counterclockwise order */
if (M != NULL)
{
pdc_transform_point(M, r->llx, r->lly, &x[0], &y[0]);
pdc_transform_point(M, r->urx, r->lly, &x[1], &y[1]);
pdc_transform_point(M, r->urx, r->ury, &x[2], &y[2]);
pdc_transform_point(M, r->llx, r->ury, &x[3], &y[3]);
polyline[0].x = x[0];
polyline[0].y = y[0];
polyline[1].x = x[1];
polyline[1].y = y[1];
polyline[2].x = x[2];
polyline[2].y = y[2];
polyline[3].x = x[3];
polyline[3].y = y[3];
polyline[4] = polyline[0];
}
else
{
polyline[0].x = r->llx;
polyline[0].y = r->lly;
polyline[1].x = r->urx;
polyline[1].y = r->lly;
polyline[2].x = r->urx;
polyline[2].y = r->ury;
polyline[3].x = r->llx;
polyline[3].y = r->ury;
polyline[4] = polyline[0];
}
}
/* debug print */
void
pdc_print_rectangle(const char *name, const pdc_rectangle *r)
{
printf("%s: llx=%g, lly=%g, urx=%g, ury=%g\n",
name, r->llx, r->lly, r->urx, r->ury);
}
|