AGG第四十九课 抗锯齿的算法理论

简介:

Explanation of the cl-aa algorithm (the same algorithm used in the AntiGrain project.)

Consider a rasterizer whose coordinates accuracy is 1/4 of the pixel size. The following picture show a random triangle whose coordinates are not aligned to the grid (like when using the line-f function with random floating point coordinates):

[image]

The algorithm first aligns each of these points to the nearest grid interesection, as shown in this picture:

[image]

Then it split each lines in the X axis at each grid boundary:

[image]

Then the same operation is done in the Y axis:

[image]

At this stage, we can see that there is a loss of precision. However, cl-aa use 1/256 subpixel accuracy by default, which is largely enough usually.

After this step, cl-aa look at each line segment (each line between 2 dots) to compute the (signed) area from the segment to the left border (storing in fact twicethe area for optimization purpose) and to compute "cover" which is simply the height of the segment. For a same line of pixels, cover must cancel when accumulating the value from the leftmost cell to the rightmost one. This happen when the polygon is closed, and is expected for the algorithm to work properly.

Scanning these segments for each cell gives the following:

[image]

(The algorithm really only keep area and cover values per cell, and discard segment coordinates.)

The red dot is the start of the segment (assuming the triangle was described in clockwise direction, starting from the point at the left.) The area is colored in blue when it is positive, and colored in red when it is negative. As said above, the area is in fact twice the real value.

Once sorted by Y then by X, we obtain:

[image]

As you can see, the sum of cover value cancels at the end of each line.

Then cells-sweep process each line of cells accumulating area for cells with the same coordinates, and accumulating cover along the whole line, and computing the effective area for each pixels with the following formula (where width is the accuracy of subpixel coordinate, which is 4 in our examples). The term area and cover are borrowed from the AntiGrain algorithm.

                                 area
effective-area = width x cover - ----
                                   2

The effective area is then scaled, usually to the range (0-256), and passed to the callback function provided to cells-sweep.

As an example, we consider the line 2 (the 3 cells in the bottom of the last picture.) The effective area for the cell at (2,2) is 4 x 1 - 6 / 2 = 1 (which can be verified visually.) Then we have 2 cells at (3,2). We sums their values, which gives us -2 for area and -1 for cover. Accumulating cover with the previous cells cancels (as expected since it is the last pixel on this line), which gives an effective area at (3,2) of 4 x 0 - (-2) / 2 = 1 (which is perhaps less obvious to check visually.)

If two consecutive cells have a hole (i.e. x coordinates difference is superior to 1), each intermediate cells have implicitly an area of 0 and a cover of 0, and thus an accumulated cover which is constant along this span of implicit cells.

FIXME: Update the example to have a hole between 2 cells.




     本文转自fengyuzaitu 51CTO博客,原文链接:http://blog.51cto.com/fengyuzaitu/1972931,如需转载请自行联系原作者

     
相关文章
|
2月前
|
机器学习/深度学习 PyTorch 算法框架/工具
【实操】涨点神器你还不会,快点进来学习Label Smooth
【实操】涨点神器你还不会,快点进来学习Label Smooth
37 1
|
6月前
|
数据可视化
好玩的DEM制图:等高线地形图入门与进阶
好玩的DEM制图:等高线地形图入门与进阶
53 0
|
8月前
|
数据可视化 Go
一行代码绘制高分SCI火山图
经过一段时间的文献阅读和资料查询,终于找到了一个好用而且简单的包——ggVolcano, 它是一个基于R语言和ggplot2绘图包开发的生物信息学数据可视化工具。它可以用于绘制火山图(Volcano plot),帮助研究者分析高通量实验数据,如基因表达谱或蛋白质组学数据,以识别差异表达或差异富集的基因或蛋白质。
187 0
|
存储 SQL 算法
第六课(二)|学习笔记
快速学习第六课(二)
92 0
第六课(二)|学习笔记
|
算法 大数据 开发者
变换编码(上)| 学习笔记
快速学习变换编码(上),介绍了变换编码(上)系统机制, 以及在实际应用过程中如何使用。
212 0
 变换编码(上)| 学习笔记
|
Web App开发 算法 计算机视觉
变换编码(下)| 学习笔记
快速学习变换编码(下),介绍了变换编码(下)系统机制, 以及在实际应用过程中如何使用。
143 0
变换编码(下)| 学习笔记
|
存储 Oracle 关系型数据库
第六课(一)|学习笔记
快速学习第六课(一)
89 0
第六课(一)|学习笔记
|
vr&ar 图形学
【Unity3D 灵巧小知识点】 ☀️ | 求解 两个向量的夹角度数
Unity 小科普 老规矩,先介绍一下 Unity 的科普小知识: Unity是 实时3D互动内容创作和运营平台 。 包括游戏开发、美术、建筑、汽车设计、影视在内的所有创作者,借助 Unity 将创意变成现实。 Unity 平台提供一整套完善的软件解决方案,可用于创作、运营和变现任何实时互动的2D和3D内容,支持平台包括手机、平板电脑、PC、游戏主机、增强现实和虚拟现实设备。
【Unity3D 灵巧小知识点】 ☀️ | 求解 两个向量的夹角度数