forked from MapServer/MapServer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmapprimitive.c
2340 lines (2002 loc) · 76.1 KB
/
mapprimitive.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
/******************************************************************************
* $Id$
*
* Project: MapServer
* Purpose: Implementations for rectObj, pointObj, lineObj, shapeObj, etc.
* Author: Steve Lime and the MapServer team.
*
******************************************************************************
* Copyright (c) 1996-2008 Regents of the University of Minnesota.
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies of this Software or works derived from this Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
* OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
* DEALINGS IN THE SOFTWARE.
****************************************************************************/
#include "mapserver.h"
#include "mapprimitive.h"
#include <assert.h>
#include <locale.h>
#include "fontcache.h"
typedef enum {CLIP_LEFT, CLIP_MIDDLE, CLIP_RIGHT} CLIP_STATE;
#define CLIP_CHECK(min, a, max) ((a) < (min) ? CLIP_LEFT : ((a) > (max) ? CLIP_RIGHT : CLIP_MIDDLE));
#define ROUND(a) ( (a) + 0.5 )
#define SWAP( a, b, t) ( (t) = (a), (a) = (b), (b) = (t) )
#define EDGE_CHECK( x0, x, x1) ((x) < MS_MIN( (x0), (x1)) ? CLIP_LEFT : ((x) > MS_MAX( (x0), (x1)) ? CLIP_RIGHT : CLIP_MIDDLE ))
#ifndef INFINITY
#define INFINITY (1.0e+30)
#endif
#define NEARZERO (1.0e-30) /* 1/INFINITY */
void msPrintShape(shapeObj *p)
{
int i,j;
msDebug("Shape contains %d parts.\n", p->numlines);
for (i=0; i<p->numlines; i++) {
msDebug("\tPart %d contains %d points.\n", i, p->line[i].numpoints);
for (j=0; j<p->line[i].numpoints; j++) {
msDebug("\t\t%d: (%f, %f)\n", j, p->line[i].point[j].x, p->line[i].point[j].y);
}
}
}
shapeObj *msShapeFromWKT(const char *string)
{
#ifdef USE_GEOS
return msGEOSShapeFromWKT(string);
#else
return msOGRShapeFromWKT(string);
#endif
}
char *msShapeToWKT(shapeObj *shape)
{
#ifdef USE_GEOS
char* pszGEOSStr;
char* pszStr;
pszGEOSStr = msGEOSShapeToWKT(shape);
pszStr = (pszGEOSStr) ? msStrdup(pszGEOSStr) : NULL;
msGEOSFreeWKT(pszGEOSStr);
return pszStr;
#else
return msOGRShapeToWKT(shape);
#endif
}
void msInitShape(shapeObj *shape)
{
/* spatial component */
shape->line = NULL;
shape->numlines = 0;
shape->type = MS_SHAPE_NULL;
shape->bounds.minx = shape->bounds.miny = -1;
shape->bounds.maxx = shape->bounds.maxy = -1;
/* attribute component */
shape->values = NULL;
shape->numvalues = 0;
shape->geometry = NULL;
shape->renderer_cache = NULL;
/* annotation component */
shape->text = NULL;
/* bookkeeping component */
shape->classindex = 0; /* default class */
shape->tileindex = shape->index = shape->resultindex = -1;
shape->scratch = MS_FALSE; /* not a temporary/scratch shape */
}
int msCopyShape(const shapeObj *from, shapeObj *to)
{
int i;
if(!from || !to) return(-1);
for(i=0; i<from->numlines; i++)
msAddLine(to, &(from->line[i])); /* copy each line */
to->type = from->type;
to->bounds.minx = from->bounds.minx;
to->bounds.miny = from->bounds.miny;
to->bounds.maxx = from->bounds.maxx;
to->bounds.maxy = from->bounds.maxy;
if(from->text) to->text = msStrdup(from->text);
to->classindex = from->classindex;
to->index = from->index;
to->tileindex = from->tileindex;
to->resultindex = from->resultindex;
if(from->values) {
if (to->values) msFreeCharArray(to->values, to->numvalues);
to->values = (char **)msSmallMalloc(sizeof(char *)*from->numvalues);
for(i=0; i<from->numvalues; i++)
to->values[i] = msStrdup(from->values[i]);
to->numvalues = from->numvalues;
}
to->geometry = NULL; /* GEOS code will build automatically if necessary */
to->scratch = from->scratch;
return(0);
}
void msFreeShape(shapeObj *shape)
{
int c;
if(!shape) return; /* for safety */
for (c= 0; c < shape->numlines; c++)
free(shape->line[c].point);
if (shape->line) free(shape->line);
if(shape->values) msFreeCharArray(shape->values, shape->numvalues);
if(shape->text) free(shape->text);
#ifdef USE_GEOS
msGEOSFreeGeometry(shape);
#endif
msInitShape(shape); /* now reset */
}
int msGetShapeRAMSize(shapeObj* shape)
{
int i;
int size = 0;
size += sizeof(shapeObj);
size += shape->numlines * sizeof(lineObj);
for (i = 0; i < shape->numlines; i++)
{
size += shape->line[i].numpoints * sizeof(pointObj);
}
size += shape->numvalues * sizeof(char*);
for( i = 0; i < shape->numvalues; i++ )
{
if( shape->values[i] )
size += strlen( shape->values[i] ) + 1;
}
if( shape->text )
size += strlen( shape->text ) + 1;
return size;
}
void msFreeLabelPathObj(labelPathObj *path)
{
msFreeShape(&(path->bounds));
msFree(path->path.point);
msFree(path->angles);
msFree(path);
}
void msShapeDeleteLine( shapeObj *shape, int line )
{
if( line < 0 || line >= shape->numlines ) {
assert( 0 );
return;
}
free( shape->line[line].point );
if( line < shape->numlines - 1 ) {
memmove( shape->line + line,
shape->line + line + 1,
sizeof(lineObj) * (shape->numlines - line - 1) );
}
shape->numlines--;
}
void msComputeBounds(shapeObj *shape)
{
int i, j;
if(shape->numlines <= 0) return;
for(i=0; i<shape->numlines; i++) {
if(shape->line[i].numpoints > 0) {
shape->bounds.minx = shape->bounds.maxx = shape->line[i].point[0].x;
shape->bounds.miny = shape->bounds.maxy = shape->line[i].point[0].y;
break;
}
}
if(i == shape->numlines) return;
for( i=0; i<shape->numlines; i++ ) {
for( j=0; j<shape->line[i].numpoints; j++ ) {
shape->bounds.minx = MS_MIN(shape->bounds.minx, shape->line[i].point[j].x);
shape->bounds.maxx = MS_MAX(shape->bounds.maxx, shape->line[i].point[j].x);
shape->bounds.miny = MS_MIN(shape->bounds.miny, shape->line[i].point[j].y);
shape->bounds.maxy = MS_MAX(shape->bounds.maxy, shape->line[i].point[j].y);
}
}
}
/* checks to see if ring r is an outer ring of shape */
int msIsOuterRing(shapeObj *shape, int r)
{
int i, status=MS_TRUE;
int result1, result2;
if(shape->numlines == 1) return(MS_TRUE);
for(i=0; i<shape->numlines; i++) {
if(i == r) continue;
/*
** We have to test 2, or perhaps 3 points on the shape against the ring because
** it is possible that at most one point could touch the ring and the function
** msPointInPolygon() is indeterminite in that case. (bug #2434)
*/
result1 = msPointInPolygon(&(shape->line[r].point[0]), &(shape->line[i]));
result2 = msPointInPolygon(&(shape->line[r].point[1]), &(shape->line[i]));
if(result1 == result2) { /* same result twice, neither point was on the edge */
if(result1 == MS_TRUE) status = !status;
} else { /* one of the first 2 points were on the edge of the ring, the next one isn't */
if(msPointInPolygon(&(shape->line[r].point[2]), &(shape->line[i])) == MS_TRUE)
status = !status;
}
}
return(status);
}
/*
** Returns a list of outer rings for shape (the list has one entry for each ring,
** MS_TRUE for outer rings).
*/
int *msGetOuterList(shapeObj *shape)
{
int i;
int *list;
list = (int *)malloc(sizeof(int)*shape->numlines);
MS_CHECK_ALLOC(list, sizeof(int)*shape->numlines, NULL);
for(i=0; i<shape->numlines; i++)
list[i] = msIsOuterRing(shape, i);
return(list);
}
/*
** Returns a list of inner rings for ring r in shape (given a list of outer rings).
*/
int *msGetInnerList(shapeObj *shape, int r, int *outerlist)
{
int i;
int *list;
list = (int *)malloc(sizeof(int)*shape->numlines);
MS_CHECK_ALLOC(list, sizeof(int)*shape->numlines, NULL);
for(i=0; i<shape->numlines; i++) { /* test all rings against the ring */
if(outerlist[i] == MS_TRUE) { /* ring is an outer and can't be an inner */
list[i] = MS_FALSE;
continue;
}
/* A valid inner ring may touch its outer ring at most one point. */
/* In the case the first point matches a vertex of an outer ring, */
/* msPointInPolygon() might return 0 or 1 (depending on coordinate values, */
/* see msGetOuterList()), so test a second point if the first test */
/* returned that the point is not inside the outer ring. */
/* Fixes #5299 */
/* Of course all of this assumes that the geometries are indeed valid in */
/* OGC terms, otherwise all logic of msIsOuterRing(), msGetOuterList(), */
/* and msGetInnerList() has undefined behaviour. */
list[i] = msPointInPolygon(&(shape->line[i].point[0]), &(shape->line[r])) ||
msPointInPolygon(&(shape->line[i].point[1]), &(shape->line[r]));
}
return(list);
}
/*
** Add point to a line object.
**
** Note that reallocating the point array larger for each point can
** be pretty inefficient, so use this function sparingly. Mostly
** geometries creators should create their own working lineObj and
** then call msAddLine() to add it to a shape.
*/
int msAddPointToLine(lineObj *line, pointObj *point )
{
line->numpoints += 1;
line->point = (pointObj *) msSmallRealloc(line->point, sizeof(pointObj) * line->numpoints);
line->point[line->numpoints-1] = *point;
return MS_SUCCESS;
}
int msAddLine(shapeObj *p, const lineObj *new_line)
{
lineObj lineCopy;
lineCopy.numpoints = new_line->numpoints;
lineCopy.point = (pointObj *) malloc(new_line->numpoints*sizeof(pointObj));
MS_CHECK_ALLOC(lineCopy.point, new_line->numpoints*sizeof(pointObj), MS_FAILURE);
if( new_line->point )
memcpy( lineCopy.point, new_line->point, sizeof(pointObj) * new_line->numpoints );
// cppcheck-suppress memleak
return msAddLineDirectly( p, &lineCopy );
}
/*
** Same as msAddLine(), except that this version "seizes" the points
** array from the passed in line and uses it instead of copying it.
*/
int msAddLineDirectly(shapeObj *p, lineObj *new_line)
{
int c;
if( p->numlines == 0 ) {
p->line = (lineObj *) malloc(sizeof(lineObj));
} else {
lineObj* newline = (lineObj *) realloc(p->line, (p->numlines+1)*sizeof(lineObj));
if( !newline ) {
free(p->line);
}
p->line = newline;
}
if( !p->line )
{
free(new_line->point );
new_line->point = NULL;
new_line->numpoints = 0;
}
MS_CHECK_ALLOC(p->line, (p->numlines+1)*sizeof(lineObj), MS_FAILURE);
/* Copy the new line onto the end of the extended line array */
c= p->numlines;
p->line[c].numpoints = new_line->numpoints;
p->line[c].point = new_line->point;
/* strip points reference off the passed in lineObj */
new_line->point = NULL;
new_line->numpoints = 0;
/* Update the polygon information */
p->numlines++;
return(MS_SUCCESS);
}
/*
** Converts a rect array to a shapeObj structure. Note order is CW assuming y origin
** is in the lower left corner (normal cartesian coordinate system). Also polygon is
** is closed (i.e. first=last). This conforms to the shapefile specification. For image
** coordinate systems (i.e. GD) this is back-ass-ward, which is fine cause the function
** that calculates direction assumes min y = lower left, this way it'll still work. Drawing
** functions are independent of direction. Orientation problems can cause some nasty bugs.
*/
void msRectToPolygon(rectObj rect, shapeObj *poly)
{
lineObj line= {0,NULL};
line.point = (pointObj *)msSmallMalloc(sizeof(pointObj)*5);
line.point[0].x = rect.minx;
line.point[0].y = rect.miny;
line.point[1].x = rect.minx;
line.point[1].y = rect.maxy;
line.point[2].x = rect.maxx;
line.point[2].y = rect.maxy;
line.point[3].x = rect.maxx;
line.point[3].y = rect.miny;
line.point[4].x = line.point[0].x;
line.point[4].y = line.point[0].y;
line.numpoints = 5;
msAddLine(poly, &line);
if(poly->numlines == 1) { /* poly was empty to begin with */
poly->type = MS_SHAPE_POLYGON;
poly->bounds = rect;
} else
msMergeRect(&poly->bounds, &rect);
free(line.point);
}
/*
** Private implementation of the Sutherland-Cohen algorithm. Inspired by
** "Getting Graphic: Programming Fundamentals in C and C++" by Mark Finlay
** and John Petritis. (pages 179-182)
*/
static int clipLine(double *x1, double *y1, double *x2, double *y2, rectObj rect)
{
double x, y;
double slope;
CLIP_STATE check1, check2;
if(*x1 < rect.minx && *x2 < rect.minx)
return(MS_FALSE);
if(*x1 > rect.maxx && *x2 > rect.maxx)
return(MS_FALSE);
check1 = CLIP_CHECK(rect.minx, *x1, rect.maxx);
check2 = CLIP_CHECK(rect.minx, *x2, rect.maxx);
if(check1 == CLIP_LEFT || check2 == CLIP_LEFT) {
slope = (*y2 - *y1)/(*x2 - *x1);
y = *y1 + (rect.minx - *x1)*slope;
if(check1 == CLIP_LEFT) {
*x1 = rect.minx;
*y1 = y;
} else {
*x2 = rect.minx;
*y2 = y;
}
}
if(check1 == CLIP_RIGHT || check2 == CLIP_RIGHT) {
slope = (*y2 - *y1)/(*x2 - *x1);
y = *y1 + (rect.maxx - *x1)*slope;
if(check1 == CLIP_RIGHT) {
*x1 = rect.maxx;
*y1 = y;
} else {
*x2 = rect.maxx;
*y2 = y;
}
}
if(*y1 < rect.miny && *y2 < rect.miny)
return(MS_FALSE);
if(*y1 > rect.maxy && *y2 > rect.maxy)
return(MS_FALSE);
check1 = CLIP_CHECK(rect.miny, *y1, rect.maxy);
check2 = CLIP_CHECK(rect.miny, *y2, rect.maxy);
if(check1 == CLIP_LEFT || check2 == CLIP_LEFT) {
slope = (*x2 - *x1)/(*y2 - *y1);
x = *x1 + (rect.miny - *y1)*slope;
if(check1 == CLIP_LEFT) {
*x1 = x;
*y1 = rect.miny;
} else {
*x2 = x;
*y2 = rect.miny;
}
}
if(check1 == CLIP_RIGHT || check2 == CLIP_RIGHT) {
slope = (*x2 - *x1)/(*y2 - *y1);
x = *x1 + (rect.maxy - *y1)*slope;
if(check1 == CLIP_RIGHT) {
*x1 = x;
*y1 = rect.maxy;
} else {
*x2 = x;
*y2 = rect.maxy;
}
}
return(MS_TRUE);
}
/*
** Routine for clipping a polyline, stored in a shapeObj struct, to a
** rectangle. Uses clipLine() function to create a new shapeObj.
*/
void msClipPolylineRect(shapeObj *shape, rectObj rect)
{
int i,j;
lineObj line= {0,NULL};
double x1, x2, y1, y2;
shapeObj tmp;
memset( &tmp, 0, sizeof(shapeObj) );
if(shape->numlines == 0) /* nothing to clip */
return;
/*
** Don't do any clip processing of shapes completely within the
** clip rectangle based on a comparison of bounds. We could do
** something similar for completely outside, but that rarely occurs
** since the spatial query at the layer read level has generally already
** discarded all shapes completely outside the rect.
*/
if( shape->bounds.maxx <= rect.maxx
&& shape->bounds.minx >= rect.minx
&& shape->bounds.maxy <= rect.maxy
&& shape->bounds.miny >= rect.miny ) {
return;
}
for(i=0; i<shape->numlines; i++) {
line.point = (pointObj *)msSmallMalloc(sizeof(pointObj)*shape->line[i].numpoints);
line.numpoints = 0;
x1 = shape->line[i].point[0].x;
y1 = shape->line[i].point[0].y;
for(j=1; j<shape->line[i].numpoints; j++) {
x2 = shape->line[i].point[j].x;
y2 = shape->line[i].point[j].y;
if(clipLine(&x1,&y1,&x2,&y2,rect) == MS_TRUE) {
if(line.numpoints == 0) { /* first segment, add both points */
line.point[0].x = x1;
line.point[0].y = y1;
line.point[1].x = x2;
line.point[1].y = y2;
line.numpoints = 2;
} else { /* add just the last point */
line.point[line.numpoints].x = x2;
line.point[line.numpoints].y = y2;
line.numpoints++;
}
if((x2 != shape->line[i].point[j].x) || (y2 != shape->line[i].point[j].y)) {
msAddLine(&tmp, &line);
line.numpoints = 0; /* new line */
}
}
x1 = shape->line[i].point[j].x;
y1 = shape->line[i].point[j].y;
}
if(line.numpoints > 0) {
msAddLineDirectly(&tmp, &line);
} else {
free(line.point);
line.numpoints = 0; /* new line */
}
}
for (i=0; i<shape->numlines; i++) free(shape->line[i].point);
free(shape->line);
shape->line = tmp.line;
shape->numlines = tmp.numlines;
msComputeBounds(shape);
}
/*
** Slightly modified version of the Liang-Barsky polygon clipping algorithm
*/
void msClipPolygonRect(shapeObj *shape, rectObj rect)
{
int i, j;
double deltax, deltay, xin,xout, yin,yout;
double tinx,tiny, toutx,touty, tin1, tin2, tout;
double x1,y1, x2,y2;
shapeObj tmp;
lineObj line= {0,NULL};
msInitShape(&tmp);
if(shape->numlines == 0) /* nothing to clip */
return;
/*
** Don't do any clip processing of shapes completely within the
** clip rectangle based on a comparison of bounds. We could do
** something similar for completely outside, but that rarely occurs
** since the spatial query at the layer read level has generally already
** discarded all shapes completely outside the rect.
*/
if( shape->bounds.maxx <= rect.maxx
&& shape->bounds.minx >= rect.minx
&& shape->bounds.maxy <= rect.maxy
&& shape->bounds.miny >= rect.miny ) {
return;
}
for(j=0; j<shape->numlines; j++) {
line.point = (pointObj *)msSmallMalloc(sizeof(pointObj)*2*shape->line[j].numpoints+1); /* worst case scenario, +1 allows us to duplicate the 1st and last point */
line.numpoints = 0;
for (i = 0; i < shape->line[j].numpoints-1; i++) {
x1 = shape->line[j].point[i].x;
y1 = shape->line[j].point[i].y;
x2 = shape->line[j].point[i+1].x;
y2 = shape->line[j].point[i+1].y;
deltax = x2-x1;
if (deltax == 0) { /* bump off of the vertical */
deltax = (x1 > rect.minx) ? -NEARZERO : NEARZERO ;
}
deltay = y2-y1;
if (deltay == 0) { /* bump off of the horizontal */
deltay = (y1 > rect.miny) ? -NEARZERO : NEARZERO ;
}
if (deltax > 0) { /* points to right */
xin = rect.minx;
xout = rect.maxx;
} else {
xin = rect.maxx;
xout = rect.minx;
}
if (deltay > 0) { /* points up */
yin = rect.miny;
yout = rect.maxy;
} else {
yin = rect.maxy;
yout = rect.miny;
}
tinx = (xin - x1)/deltax;
tiny = (yin - y1)/deltay;
if (tinx < tiny) { /* hits x first */
tin1 = tinx;
tin2 = tiny;
} else { /* hits y first */
tin1 = tiny;
tin2 = tinx;
}
if (1 >= tin1) {
if (0 < tin1) {
line.point[line.numpoints].x = xin;
line.point[line.numpoints].y = yin;
line.numpoints++;
}
if (1 >= tin2) {
toutx = (xout - x1)/deltax;
touty = (yout - y1)/deltay;
tout = (toutx < touty) ? toutx : touty ;
if (0 < tin2 || 0 < tout) {
if (tin2 <= tout) {
if (0 < tin2) {
if (tinx > tiny) {
line.point[line.numpoints].x = xin;
line.point[line.numpoints].y = y1 + tinx*deltay;
line.numpoints++;
} else {
line.point[line.numpoints].x = x1 + tiny*deltax;
line.point[line.numpoints].y = yin;
line.numpoints++;
}
}
if (1 > tout) {
if (toutx < touty) {
line.point[line.numpoints].x = xout;
line.point[line.numpoints].y = y1 + toutx*deltay;
line.numpoints++;
} else {
line.point[line.numpoints].x = x1 + touty*deltax;
line.point[line.numpoints].y = yout;
line.numpoints++;
}
} else {
line.point[line.numpoints].x = x2;
line.point[line.numpoints].y = y2;
line.numpoints++;
}
} else {
if (tinx > tiny) {
line.point[line.numpoints].x = xin;
line.point[line.numpoints].y = yout;
line.numpoints++;
} else {
line.point[line.numpoints].x = xout;
line.point[line.numpoints].y = yin;
line.numpoints++;
}
}
}
}
}
}
if(line.numpoints > 0) {
line.point[line.numpoints].x = line.point[0].x; /* force closure */
line.point[line.numpoints].y = line.point[0].y;
line.numpoints++;
msAddLineDirectly(&tmp, &line);
} else {
free(line.point);
}
} /* next line */
for (i=0; i<shape->numlines; i++) free(shape->line[i].point);
free(shape->line);
shape->line = tmp.line;
shape->numlines = tmp.numlines;
msComputeBounds(shape);
return;
}
/*
** offsets a point relative to an image position
*/
void msOffsetPointRelativeTo(pointObj *point, layerObj *layer)
{
double x=0, y=0;
if ( msCheckParentPointer(layer->map,"map")==MS_FAILURE )
return;
if(layer->transform == MS_TRUE) return; /* nothing to do */
if(layer->units == MS_PERCENTAGES) {
point->x *= (layer->map->width-1);
point->y *= (layer->map->height-1);
}
if(layer->transform == MS_FALSE || layer->transform == MS_UL) return; /* done */
switch(layer->transform) {
case MS_UC:
x = (layer->map->width-1)/2;
y = 0;
break;
case MS_UR:
x = layer->map->width-1;
y = 0;
break;
case MS_CL:
x = 0;
y = layer->map->height/2;
break;
case MS_CC:
x = layer->map->width/2;
y = layer->map->height/2;
break;
case MS_CR:
x = layer->map->width-1;
y = layer->map->height/2;
break;
case MS_LL:
x = 0;
y = layer->map->height-1;
break;
case MS_LC:
x = layer->map->width/2;
y = layer->map->height-1;
break;
case MS_LR:
x = layer->map->width-1;
y = layer->map->height-1;
break;
}
point->x += x;
point->y += y;
return;
}
/*
** offsets a shape relative to an image position
*/
void msOffsetShapeRelativeTo(shapeObj *shape, layerObj *layer)
{
int i, j;
double x=0, y=0;
if(layer->transform == MS_TRUE) return; /* nothing to do */
if ( msCheckParentPointer(layer->map,"map")==MS_FAILURE )
return;
if(layer->units == MS_PERCENTAGES) {
for (i=0; i<shape->numlines; i++) {
for (j=0; j<shape->line[i].numpoints; j++) {
shape->line[i].point[j].x *= (layer->map->width-1);
shape->line[i].point[j].y *= (layer->map->height-1);
}
}
}
if(layer->transform == MS_FALSE || layer->transform == MS_UL) return; /* done */
switch(layer->transform) {
case MS_UC:
x = (layer->map->width-1)/2;
y = 0;
break;
case MS_UR:
x = layer->map->width-1;
y = 0;
break;
case MS_CL:
x = 0;
y = layer->map->height/2;
break;
case MS_CC:
x = layer->map->width/2;
y = layer->map->height/2;
break;
case MS_CR:
x = layer->map->width-1;
y = layer->map->height/2;
break;
case MS_LL:
x = 0;
y = layer->map->height-1;
break;
case MS_LC:
x = layer->map->width/2;
y = layer->map->height-1;
break;
case MS_LR:
x = layer->map->width-1;
y = layer->map->height-1;
break;
}
for (i=0; i<shape->numlines; i++) {
for (j=0; j<shape->line[i].numpoints; j++) {
shape->line[i].point[j].x += x;
shape->line[i].point[j].y += y;
}
}
return;
}
void msTransformShapeSimplify(shapeObj *shape, rectObj extent, double cellsize)
{
int i,j,k,beforelast; /* loop counters */
double dx,dy;
pointObj *point;
double inv_cs = 1.0 / cellsize; /* invert and multiply much faster */
int ok = 0;
if(shape->numlines == 0) return; /* nothing to transform */
if(shape->type == MS_SHAPE_LINE) {
/*
* loop through the shape's lines, and do naive simplification
* to discard the points that are too close to one another.
* currently the threshold is to discard points if they fall
* less than a pixel away from their predecessor.
* the simplified line is guaranteed to contain at
* least its first and last point
*/
for(i=0; i<shape->numlines; i++) { /* for each part */
if(shape->line[i].numpoints<2) {
shape->line[i].numpoints=0;
continue; /*skip degenerate lines*/
}
point=shape->line[i].point;
/*always keep first point*/
point[0].x = MS_MAP2IMAGE_X_IC_DBL(point[0].x, extent.minx, inv_cs);
point[0].y = MS_MAP2IMAGE_Y_IC_DBL(point[0].y, extent.maxy, inv_cs);
beforelast=shape->line[i].numpoints-1;
for(j=1,k=1; j < beforelast; j++ ) { /*loop from second point to first-before-last point*/
point[k].x = MS_MAP2IMAGE_X_IC_DBL(point[j].x, extent.minx, inv_cs);
point[k].y = MS_MAP2IMAGE_Y_IC_DBL(point[j].y, extent.maxy, inv_cs);
dx=(point[k].x-point[k-1].x);
dy=(point[k].y-point[k-1].y);
if(dx*dx+dy*dy>1)
k++;
}
/* try to keep last point */
point[k].x = MS_MAP2IMAGE_X_IC_DBL(point[j].x, extent.minx, inv_cs);
point[k].y = MS_MAP2IMAGE_Y_IC_DBL(point[j].y, extent.maxy, inv_cs);
/* discard last point if equal to the one before it */
if(point[k].x!=point[k-1].x || point[k].y!=point[k-1].y) {
shape->line[i].numpoints=k+1;
} else {
shape->line[i].numpoints=k;
}
/* skip degenerate line once more */
if(shape->line[i].numpoints<2) {
shape->line[i].numpoints=0;
} else {
ok = 1; /* we have at least one line with more than two points */
}
}
} else if(shape->type == MS_SHAPE_POLYGON) {
/*
* loop through the shape's lines, and do naive simplification
* to discard the points that are too close to one another.
* currently the threshold is to discard points if they fall
* less than a pixel away from their predecessor.
* the simplified polygon is guaranteed to contain at
* least its first, second and last point
*/
for(i=0; i<shape->numlines; i++) { /* for each part */
if(shape->line[i].numpoints<4) {
shape->line[i].numpoints=0;
continue; /*skip degenerate lines*/
}
point=shape->line[i].point;
/*always keep first and second point*/
point[0].x = MS_MAP2IMAGE_X_IC_DBL(point[0].x, extent.minx, inv_cs);
point[0].y = MS_MAP2IMAGE_Y_IC_DBL(point[0].y, extent.maxy, inv_cs);
point[1].x = MS_MAP2IMAGE_X_IC_DBL(point[1].x, extent.minx, inv_cs);
point[1].y = MS_MAP2IMAGE_Y_IC_DBL(point[1].y, extent.maxy, inv_cs);
beforelast=shape->line[i].numpoints-2;
for(j=2,k=2; j < beforelast; j++ ) { /*loop from second point to second-before-last point*/
point[k].x = MS_MAP2IMAGE_X_IC_DBL(point[j].x, extent.minx, inv_cs);
point[k].y = MS_MAP2IMAGE_Y_IC_DBL(point[j].y, extent.maxy, inv_cs);
dx=(point[k].x-point[k-1].x);
dy=(point[k].y-point[k-1].y);
if(dx*dx+dy*dy>1)
k++;
}
/*always keep last two points (the last point is the repetition of the
* first one */
point[k].x = MS_MAP2IMAGE_X_IC_DBL(point[j].x, extent.minx, inv_cs);
point[k].y = MS_MAP2IMAGE_Y_IC_DBL(point[j].y, extent.maxy, inv_cs);
point[k+1].x = MS_MAP2IMAGE_X_IC_DBL(point[j+1].x, extent.minx, inv_cs);
point[k+1].y = MS_MAP2IMAGE_Y_IC_DBL(point[j+1].y, extent.maxy, inv_cs);
shape->line[i].numpoints = k+2;
ok = 1;
}
} else { /* only for untyped shapes, as point layers don't go through this function */
for(i=0; i<shape->numlines; i++) {
point=shape->line[i].point;
for(j=0; j<shape->line[i].numpoints; j++) {
point[j].x = MS_MAP2IMAGE_X_IC_DBL(point[j].x, extent.minx, inv_cs);
point[j].y = MS_MAP2IMAGE_Y_IC_DBL(point[j].y, extent.maxy, inv_cs);
}
}
ok = 1;
}
if(!ok) {
for(i=0; i<shape->numlines; i++) {
free(shape->line[i].point);
}
shape->numlines = 0 ;
}
}
/**
* Generic function to transorm the shape coordinates to output coordinates
*/
void msTransformShape(shapeObj *shape, rectObj extent, double cellsize, imageObj *image)
{
if (image != NULL && MS_RENDERER_PLUGIN(image->format)) {
rendererVTableObj *renderer = MS_IMAGE_RENDERER(image);
if(renderer->transform_mode == MS_TRANSFORM_SNAPTOGRID) {
msTransformShapeToPixelSnapToGrid(shape, extent, cellsize, renderer->approximation_scale);
} else if(renderer->transform_mode == MS_TRANSFORM_SIMPLIFY) {
msTransformShapeSimplify(shape, extent, cellsize);
} else if(renderer->transform_mode == MS_TRANSFORM_ROUND) {
msTransformShapeToPixelRound(shape, extent, cellsize);
} else if(renderer->transform_mode == MS_TRANSFORM_FULLRESOLUTION) {
msTransformShapeToPixelDoublePrecision(shape,extent,cellsize);
} else if(renderer->transform_mode == MS_TRANSFORM_NONE) {
/* nothing to do */
return;
}
/* unknown, do nothing */
return;
}
msTransformShapeToPixelRound(shape, extent, cellsize);
}
void msTransformShapeToPixelSnapToGrid(shapeObj *shape, rectObj extent, double cellsize, double grid_resolution)
{
int i,j,k; /* loop counters */