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,如需转载请自行联系原作者

     
相关文章
|
存储 编解码 缓存
webgl系列之抗锯齿和深度缓存
前言 大家好我是Fly 哥, 这是今年webgl 系列的第三篇文章, 如果你之前的两篇文章没看的话,建议先看一下,然后再来看这一篇文章 Webgl 系列之buffer的使用 webgl系列之对光栅化的理解 上一篇文章,任何虚拟3维世界的转换到二维屏幕中通过「采样」 也就判断屏幕上的每个像素中心点是不是在三角形内部的得到了 下面这幅图: 图片 走样之前 这时候有同学问, 这不像三角形哇, 这个其实用个专业的词—— 「锯齿」 , 我的理解 一个三角形经过光栅化后, 得到屏幕上每一个像素点 组成的像素点的集合。那到底是经过什么样的处理得到下面这张图: 图片 final 反走样 其实出现上面
webgl系列之抗锯齿和深度缓存
|
存储 编解码 缓存
Qt开发技术:Qt绘图系统(一)绘图系统介绍
Qt开发技术:Qt绘图系统(一)绘图系统介绍
Qt开发技术:Qt绘图系统(一)绘图系统介绍
|
5月前
|
存储 JSON API
小红书获取笔记详情API接口的开发、应用与收益。
小红书笔记详情API采用Python与Django框架开发,使用MySQL数据库存储数据。接口通过HTTP GET请求获取笔记详情,返回JSON格式数据,包含笔记内容、作者信息、图片链接等。该API应用于小红书APP内笔记展示和互动功能,并支持第三方平台的内容整合与数据分析,提升用户体验与活跃度,促进品牌合作推广,优化平台运营效率,为平台带来显著收益。
385 1
|
12月前
|
存储 开发框架 JavaScript
程序与技术分享:CKEditor与CKFinder使用
程序与技术分享:CKEditor与CKFinder使用
311 0
|
10月前
【Qt 学习笔记】Qt常用控件 | 按钮类控件 | Radio Button的使用及说明
【Qt 学习笔记】Qt常用控件 | 按钮类控件 | Radio Button的使用及说明
1630 1
|
存储 Java 调度
Spring Batch轻量级批处理实战
Spring Batch轻量级批处理实战
225 0
|
12月前
|
算法 C++ 容器
C++之vector容器操作(构造、赋值、扩容、插入、删除、交换、预留空间、遍历)
C++之vector容器操作(构造、赋值、扩容、插入、删除、交换、预留空间、遍历)
652 0
|
网络协议 Serverless 网络安全
阿里云函数计算自定义域名的SSL证书免费全自动申请及部署
阿里云函数计算简化了开发部署,让用户专注业务逻辑,降低了运维复杂性。然而,SSL证书支持需额外付费:免费证书有效期缩短至3个月,1年证书及自动化部署均收费。为节省成本,小微企业和个人开发者可以采用一套全自动、全免费的SSL证书申请、续签和部署方案。该方案包括自动向Let's Encrypt申请证书、使用阿里云OpenAPI更新证书以及在证书到期前提前申请。通过Win-Acme(Windows)实现,详细步骤包括设置win-acme、自动DNS验证和更新函数计算证书。
1069 5
ReferenceError: self is not defined
ReferenceError: self is not defined
606 0
|
缓存 并行计算 算法
上帝视角看GPU(5):图形流水线里的不可编程单元
上帝视角看GPU(5):图形流水线里的不可编程单元
377 0