NVlabs / nvdiffrast

Nvdiffrast - Modular Primitives for High-Performance Differentiable Rendering
Other
1.35k stars 144 forks source link

Why is abs() not applied to area calculation? #19

Closed takiyu closed 3 years ago

takiyu commented 3 years ago

I have a question about backward of rasterization.

It seems that the code contains area calculation to obtain barycentric coordinate. However there was no abs() function.

float a0 = p1x*p2y - p1y*p2x;
float a1 = p2x*p0y - p2y*p0x;
float a2 = p0x*p1y - p0y*p1x;

https://github.com/NVlabs/nvdiffrast/blob/main/nvdiffrast/common/rasterize.cu#L82

The case will not happen when 'a0', 'a1', and 'a2' are negative values?

s-laine commented 3 years ago

Sorry for the late reply — the unnormalized barycentrics, i.e., edge function evaluations, can indeed be negative. However, the sign cancels out when normalizing by area of triangle which would also be negative in this case. Thus it is simpler to consider all of these quantities as signed and avoid the abs().