forked from OSGeo/gdal
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathllrasterize.cpp
713 lines (632 loc) · 25.8 KB
/
llrasterize.cpp
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
/******************************************************************************
*
* Project: GDAL
* Purpose: Vector polygon rasterization code.
* Author: Frank Warmerdam, [email protected]
*
******************************************************************************
* Copyright (c) 2000, Frank Warmerdam <[email protected]>
* Copyright (c) 2011, Even Rouault <even dot rouault at spatialys.com>
*
* SPDX-License-Identifier: MIT
****************************************************************************/
#include "cpl_port.h"
#include "gdal_alg_priv.h"
#include <cmath>
#include <cstdlib>
#include <cstring>
#include <algorithm>
#include <set>
#include <utility>
#include <vector>
#include "gdal_alg.h"
/************************************************************************/
/* dllImageFilledPolygon() */
/* */
/* Perform scanline conversion of the passed multi-ring */
/* polygon. Note the polygon does not need to be explicitly */
/* closed. The scanline function will be called with */
/* horizontal scanline chunks which may not be entirely */
/* contained within the valid raster area (in the X */
/* direction). */
/* */
/* NEW: Nodes' coordinate are kept as double in order */
/* to compute accurately the intersections with the lines */
/* */
/* A pixel is considered inside a polygon if its center */
/* falls inside the polygon. This is robust unless */
/* the nodes are placed in the center of the pixels in which */
/* case, due to numerical inaccuracies, it's hard to predict */
/* if the pixel will be considered inside or outside the shape. */
/************************************************************************/
/*
* NOTE: This code was originally adapted from the gdImageFilledPolygon()
* function in libgd.
*
* http://www.boutell.com/gd/
*
* It was later adapted for direct inclusion in GDAL and relicensed under
* the GDAL MIT license (pulled from the OpenEV distribution).
*/
void GDALdllImageFilledPolygon(int nRasterXSize, int nRasterYSize,
int nPartCount, const int *panPartSize,
const double *padfX, const double *padfY,
const double *dfVariant,
llScanlineFunc pfnScanlineFunc, void *pCBData,
bool bAvoidBurningSamePoints)
{
if (!nPartCount)
{
return;
}
int n = 0;
for (int part = 0; part < nPartCount; part++)
n += panPartSize[part];
std::vector<int> polyInts(n);
std::vector<int> polyInts2;
if (bAvoidBurningSamePoints)
polyInts2.resize(n);
double dminy = padfY[0];
double dmaxy = padfY[0];
for (int i = 1; i < n; i++)
{
if (padfY[i] < dminy)
{
dminy = padfY[i];
}
if (padfY[i] > dmaxy)
{
dmaxy = padfY[i];
}
}
int miny = static_cast<int>(dminy);
int maxy = static_cast<int>(dmaxy);
if (miny < 0)
miny = 0;
if (maxy >= nRasterYSize)
maxy = nRasterYSize - 1;
int minx = 0;
const int maxx = nRasterXSize - 1;
// Fix in 1.3: count a vertex only once.
for (int y = miny; y <= maxy; y++)
{
int partoffset = 0;
const double dy = y + 0.5; // Center height of line.
int part = 0;
int ints = 0;
int ints2 = 0;
for (int i = 0; i < n; i++)
{
if (i == partoffset + panPartSize[part])
{
partoffset += panPartSize[part];
part++;
}
int ind1 = 0;
int ind2 = 0;
if (i == partoffset)
{
ind1 = partoffset + panPartSize[part] - 1;
ind2 = partoffset;
}
else
{
ind1 = i - 1;
ind2 = i;
}
double dy1 = padfY[ind1];
double dy2 = padfY[ind2];
if ((dy1 < dy && dy2 < dy) || (dy1 > dy && dy2 > dy))
continue;
double dx1 = 0.0;
double dx2 = 0.0;
if (dy1 < dy2)
{
dx1 = padfX[ind1];
dx2 = padfX[ind2];
}
else if (dy1 > dy2)
{
std::swap(dy1, dy2);
dx2 = padfX[ind1];
dx1 = padfX[ind2];
}
else // if( fabs(dy1-dy2) < 1.e-6 )
{
// AE: DO NOT skip bottom horizontal segments
// -Fill them separately-
if (padfX[ind1] > padfX[ind2])
{
const int horizontal_x1 =
static_cast<int>(floor(padfX[ind2] + 0.5));
const int horizontal_x2 =
static_cast<int>(floor(padfX[ind1] + 0.5));
if ((horizontal_x1 > maxx) || (horizontal_x2 <= minx))
continue;
// Fill the horizontal segment (separately from the rest).
if (bAvoidBurningSamePoints)
{
polyInts2[ints2++] = horizontal_x1;
polyInts2[ints2++] = horizontal_x2;
}
else
{
pfnScanlineFunc(
pCBData, y, horizontal_x1, horizontal_x2 - 1,
dfVariant == nullptr ? 0 : dfVariant[0]);
}
}
// else: Skip top horizontal segments.
// They are already filled in the regular loop.
continue;
}
if (dy < dy2 && dy >= dy1)
{
const double intersect =
(dy - dy1) * (dx2 - dx1) / (dy2 - dy1) + dx1;
polyInts[ints++] = static_cast<int>(floor(intersect + 0.5));
}
}
std::sort(polyInts.begin(), polyInts.begin() + ints);
std::sort(polyInts2.begin(), polyInts2.begin() + ints2);
for (int i = 0; i + 1 < ints; i += 2)
{
if (polyInts[i] <= maxx && polyInts[i + 1] > minx)
{
pfnScanlineFunc(pCBData, y, polyInts[i], polyInts[i + 1] - 1,
dfVariant == nullptr ? 0 : dfVariant[0]);
}
}
for (int i2 = 0, i = 0; i2 + 1 < ints2; i2 += 2)
{
if (polyInts2[i2] <= maxx && polyInts2[i2 + 1] > minx)
{
// "synchronize" polyInts[i] with polyInts2[i2]
while (i + 1 < ints && polyInts[i] < polyInts2[i2])
i += 2;
// Only burn if we don't have a common segment between
// polyInts[] and polyInts2[]
if (i + 1 >= ints || polyInts[i] != polyInts2[i2])
{
pfnScanlineFunc(pCBData, y, polyInts2[i2],
polyInts2[i2 + 1] - 1,
dfVariant == nullptr ? 0 : dfVariant[0]);
}
}
}
}
}
/************************************************************************/
/* GDALdllImagePoint() */
/************************************************************************/
void GDALdllImagePoint(int nRasterXSize, int nRasterYSize, int nPartCount,
const int * /*panPartSize*/, const double *padfX,
const double *padfY, const double *padfVariant,
llPointFunc pfnPointFunc, void *pCBData)
{
for (int i = 0; i < nPartCount; i++)
{
const int nX = static_cast<int>(floor(padfX[i]));
const int nY = static_cast<int>(floor(padfY[i]));
double dfVariant = 0.0;
if (padfVariant != nullptr)
dfVariant = padfVariant[i];
if (0 <= nX && nX < nRasterXSize && 0 <= nY && nY < nRasterYSize)
pfnPointFunc(pCBData, nY, nX, dfVariant);
}
}
/************************************************************************/
/* GDALdllImageLine() */
/************************************************************************/
void GDALdllImageLine(int nRasterXSize, int nRasterYSize, int nPartCount,
const int *panPartSize, const double *padfX,
const double *padfY, const double *padfVariant,
llPointFunc pfnPointFunc, void *pCBData)
{
if (!nPartCount)
return;
for (int i = 0, n = 0; i < nPartCount; n += panPartSize[i++])
{
for (int j = 1; j < panPartSize[i]; j++)
{
int iX = static_cast<int>(floor(padfX[n + j - 1]));
int iY = static_cast<int>(floor(padfY[n + j - 1]));
const int iX1 = static_cast<int>(floor(padfX[n + j]));
const int iY1 = static_cast<int>(floor(padfY[n + j]));
double dfVariant = 0.0;
double dfVariant1 = 0.0;
if (padfVariant != nullptr &&
static_cast<GDALRasterizeInfo *>(pCBData)->eBurnValueSource !=
GBV_UserBurnValue)
{
dfVariant = padfVariant[n + j - 1];
dfVariant1 = padfVariant[n + j];
}
int nDeltaX = std::abs(iX1 - iX);
int nDeltaY = std::abs(iY1 - iY);
// Step direction depends on line direction.
const int nXStep = (iX > iX1) ? -1 : 1;
const int nYStep = (iY > iY1) ? -1 : 1;
// Determine the line slope.
if (nDeltaX >= nDeltaY)
{
const int nXError = nDeltaY << 1;
const int nYError = nXError - (nDeltaX << 1);
int nError = nXError - nDeltaX;
// == 0 makes clang -fcatch-undefined-behavior -ftrapv happy,
// but if it is == 0, dfDeltaVariant is not really used, so any
// value is okay.
const double dfDeltaVariant =
nDeltaX == 0 ? 0.0 : (dfVariant1 - dfVariant) / nDeltaX;
// Do not burn the end point, unless we are in the last
// segment. This is to avoid burning twice intermediate points,
// which causes artifacts in Add mode
if (j != panPartSize[i] - 1)
{
nDeltaX--;
}
while (nDeltaX-- >= 0)
{
if (0 <= iX && iX < nRasterXSize && 0 <= iY &&
iY < nRasterYSize)
pfnPointFunc(pCBData, iY, iX, dfVariant);
dfVariant += dfDeltaVariant;
iX += nXStep;
if (nError > 0)
{
iY += nYStep;
nError += nYError;
}
else
{
nError += nXError;
}
}
}
else
{
const int nXError = nDeltaX << 1;
const int nYError = nXError - (nDeltaY << 1);
int nError = nXError - nDeltaY;
// == 0 makes clang -fcatch-undefined-behavior -ftrapv happy,
// but if it is == 0, dfDeltaVariant is not really used, so any
// value is okay.
double dfDeltaVariant =
nDeltaY == 0 ? 0.0 : (dfVariant1 - dfVariant) / nDeltaY;
// Do not burn the end point, unless we are in the last
// segment. This is to avoid burning twice intermediate points,
// which causes artifacts in Add mode
if (j != panPartSize[i] - 1)
{
nDeltaY--;
}
while (nDeltaY-- >= 0)
{
if (0 <= iX && iX < nRasterXSize && 0 <= iY &&
iY < nRasterYSize)
pfnPointFunc(pCBData, iY, iX, dfVariant);
dfVariant += dfDeltaVariant;
iY += nYStep;
if (nError > 0)
{
iX += nXStep;
nError += nYError;
}
else
{
nError += nXError;
}
}
}
}
}
}
/************************************************************************/
/* GDALdllImageLineAllTouched() */
/* */
/* This alternate line drawing algorithm attempts to ensure */
/* that every pixel touched at all by the line will get set. */
/* @param padfVariant should contain the values that are to be */
/* added to the burn value. The values along the line between the */
/* points will be linearly interpolated. These values are used */
/* only if pCBData->eBurnValueSource is set to something other */
/* than GBV_UserBurnValue. If NULL is passed, a monotonous line */
/* will be drawn with the burn value. */
/************************************************************************/
void GDALdllImageLineAllTouched(int nRasterXSize, int nRasterYSize,
int nPartCount, const int *panPartSize,
const double *padfX, const double *padfY,
const double *padfVariant,
llPointFunc pfnPointFunc, void *pCBData,
bool bAvoidBurningSamePoints,
bool bIntersectOnly)
{
// This is an epsilon to detect geometries that are aligned with pixel
// coordinates. Hard to find the right value. We put it to that value
// to satisfy the scenarios of https://github.com/OSGeo/gdal/issues/7523
// and https://github.com/OSGeo/gdal/issues/6414
constexpr double EPSILON_INTERSECT_ONLY = 1e-4;
if (!nPartCount)
return;
for (int i = 0, n = 0; i < nPartCount; n += panPartSize[i++])
{
std::set<std::pair<int, int>> lastBurntPoints;
std::set<std::pair<int, int>> newBurntPoints;
for (int j = 1; j < panPartSize[i]; j++)
{
lastBurntPoints = std::move(newBurntPoints);
newBurntPoints.clear();
double dfX = padfX[n + j - 1];
double dfY = padfY[n + j - 1];
double dfXEnd = padfX[n + j];
double dfYEnd = padfY[n + j];
double dfVariant = 0.0;
double dfVariantEnd = 0.0;
if (padfVariant != nullptr &&
static_cast<GDALRasterizeInfo *>(pCBData)->eBurnValueSource !=
GBV_UserBurnValue)
{
dfVariant = padfVariant[n + j - 1];
dfVariantEnd = padfVariant[n + j];
}
// Skip segments that are off the target region.
if ((dfY < 0.0 && dfYEnd < 0.0) ||
(dfY > nRasterYSize && dfYEnd > nRasterYSize) ||
(dfX < 0.0 && dfXEnd < 0.0) ||
(dfX > nRasterXSize && dfXEnd > nRasterXSize))
continue;
// Swap if needed so we can proceed from left2right (X increasing)
if (dfX > dfXEnd)
{
std::swap(dfX, dfXEnd);
std::swap(dfY, dfYEnd);
std::swap(dfVariant, dfVariantEnd);
}
// Special case for vertical lines.
if (floor(dfX) == floor(dfXEnd) || fabs(dfX - dfXEnd) < .01)
{
if (bIntersectOnly)
{
if (std::abs(dfX - std::round(dfX)) <
EPSILON_INTERSECT_ONLY &&
std::abs(dfXEnd - std::round(dfXEnd)) <
EPSILON_INTERSECT_ONLY)
continue;
}
if (dfYEnd < dfY)
{
std::swap(dfY, dfYEnd);
std::swap(dfVariant, dfVariantEnd);
}
const int iX = static_cast<int>(floor(dfXEnd));
int iY = static_cast<int>(floor(dfY));
int iYEnd =
static_cast<int>(floor(dfYEnd - EPSILON_INTERSECT_ONLY));
if (iX < 0 || iX >= nRasterXSize)
continue;
double dfDeltaVariant = 0.0;
if (dfYEnd - dfY > 0.0)
dfDeltaVariant = (dfVariantEnd - dfVariant) /
(dfYEnd - dfY); // Per unit change in iY.
// Clip to the borders of the target region.
if (iY < 0)
iY = 0;
if (iYEnd >= nRasterYSize)
iYEnd = nRasterYSize - 1;
dfVariant += dfDeltaVariant * (iY - dfY);
if (padfVariant == nullptr)
{
for (; iY <= iYEnd; iY++)
{
if (bAvoidBurningSamePoints)
{
auto yx = std::pair<int, int>(iY, iX);
if (lastBurntPoints.find(yx) !=
lastBurntPoints.end())
{
continue;
}
newBurntPoints.insert(yx);
}
pfnPointFunc(pCBData, iY, iX, 0.0);
}
}
else
{
for (; iY <= iYEnd; iY++, dfVariant += dfDeltaVariant)
{
if (bAvoidBurningSamePoints)
{
auto yx = std::pair<int, int>(iY, iX);
if (lastBurntPoints.find(yx) !=
lastBurntPoints.end())
{
continue;
}
newBurntPoints.insert(yx);
}
pfnPointFunc(pCBData, iY, iX, dfVariant);
}
}
continue; // Next segment.
}
const double dfDeltaVariant =
(dfVariantEnd - dfVariant) /
(dfXEnd - dfX); // Per unit change in iX.
// Special case for horizontal lines.
if (floor(dfY) == floor(dfYEnd) || fabs(dfY - dfYEnd) < .01)
{
if (bIntersectOnly)
{
if (std::abs(dfY - std::round(dfY)) <
EPSILON_INTERSECT_ONLY &&
std::abs(dfYEnd - std::round(dfYEnd)) <
EPSILON_INTERSECT_ONLY)
continue;
}
if (dfXEnd < dfX)
{
std::swap(dfX, dfXEnd);
std::swap(dfVariant, dfVariantEnd);
}
int iX = static_cast<int>(floor(dfX));
const int iY = static_cast<int>(floor(dfY));
int iXEnd =
static_cast<int>(floor(dfXEnd - EPSILON_INTERSECT_ONLY));
if (iY < 0 || iY >= nRasterYSize)
continue;
// Clip to the borders of the target region.
if (iX < 0)
iX = 0;
if (iXEnd >= nRasterXSize)
iXEnd = nRasterXSize - 1;
dfVariant += dfDeltaVariant * (iX - dfX);
if (padfVariant == nullptr)
{
for (; iX <= iXEnd; iX++)
{
if (bAvoidBurningSamePoints)
{
auto yx = std::pair<int, int>(iY, iX);
if (lastBurntPoints.find(yx) !=
lastBurntPoints.end())
{
continue;
}
newBurntPoints.insert(yx);
}
pfnPointFunc(pCBData, iY, iX, 0.0);
}
}
else
{
for (; iX <= iXEnd; iX++, dfVariant += dfDeltaVariant)
{
if (bAvoidBurningSamePoints)
{
auto yx = std::pair<int, int>(iY, iX);
if (lastBurntPoints.find(yx) !=
lastBurntPoints.end())
{
continue;
}
newBurntPoints.insert(yx);
}
pfnPointFunc(pCBData, iY, iX, dfVariant);
}
}
continue; // Next segment.
}
/* --------------------------------------------------------------------
*/
/* General case - left to right sloped. */
/* --------------------------------------------------------------------
*/
const double dfSlope = (dfYEnd - dfY) / (dfXEnd - dfX);
// Clip segment in X.
if (dfXEnd > nRasterXSize)
{
dfYEnd -= (dfXEnd - nRasterXSize) * dfSlope;
dfXEnd = nRasterXSize;
}
if (dfX < 0.0)
{
dfY += (0.0 - dfX) * dfSlope;
dfVariant += dfDeltaVariant * (0.0 - dfX);
dfX = 0.0;
}
// Clip segment in Y.
if (dfYEnd > dfY)
{
if (dfY < 0.0)
{
const double dfDiffX = (0.0 - dfY) / dfSlope;
dfX += dfDiffX;
dfVariant += dfDeltaVariant * dfDiffX;
dfY = 0.0;
}
if (dfYEnd >= nRasterYSize)
{
dfXEnd += (dfYEnd - nRasterYSize) / dfSlope;
if (dfXEnd > nRasterXSize)
dfXEnd = nRasterXSize;
// dfYEnd is no longer used afterwards, but for
// consistency it should be:
// dfYEnd = nRasterXSize;
}
}
else
{
if (dfY >= nRasterYSize)
{
const double dfDiffX = (nRasterYSize - dfY) / dfSlope;
dfX += dfDiffX;
dfVariant += dfDeltaVariant * dfDiffX;
dfY = nRasterYSize;
}
if (dfYEnd < 0.0)
{
dfXEnd -= (dfYEnd - 0) / dfSlope;
// dfYEnd is no longer used afterwards, but for
// consistency it should be:
// dfYEnd = 0.0;
}
}
// Step from pixel to pixel.
while (dfX >= 0.0 && dfX < dfXEnd)
{
const int iX = static_cast<int>(floor(dfX));
const int iY = static_cast<int>(floor(dfY));
// Burn in the current point.
// We should be able to drop the Y check because we clipped
// in Y, but there may be some error with all the small steps.
if (iY >= 0 && iY < nRasterYSize)
{
if (bAvoidBurningSamePoints)
{
auto yx = std::pair<int, int>(iY, iX);
if (lastBurntPoints.find(yx) == lastBurntPoints.end() &&
newBurntPoints.find(yx) == newBurntPoints.end())
{
newBurntPoints.insert(yx);
pfnPointFunc(pCBData, iY, iX, dfVariant);
}
}
else
{
pfnPointFunc(pCBData, iY, iX, dfVariant);
}
}
double dfStepX = floor(dfX + 1.0) - dfX;
double dfStepY = dfStepX * dfSlope;
// Step to right pixel without changing scanline?
if (static_cast<int>(floor(dfY + dfStepY)) == iY)
{
dfX += dfStepX;
dfY += dfStepY;
dfVariant += dfDeltaVariant * dfStepX;
}
else if (dfSlope < 0)
{
dfStepY = iY - dfY;
if (dfStepY > -0.000000001)
dfStepY = -0.000000001;
dfStepX = dfStepY / dfSlope;
dfX += dfStepX;
dfY += dfStepY;
dfVariant += dfDeltaVariant * dfStepX;
}
else
{
dfStepY = (iY + 1) - dfY;
if (dfStepY < 0.000000001)
dfStepY = 0.000000001;
dfStepX = dfStepY / dfSlope;
dfX += dfStepX;
dfY += dfStepY;
dfVariant += dfDeltaVariant * dfStepX;
}
} // Next step along segment.
} // Next segment.
} // Next part.
}