I would like to add one more question.
What is going to happen if perimeter_inside(D, diff) ==0 ?
Here is a link to a TechNote I found on SupportNet:
The perimeter_inside(D, diff) should represent the length of the edge of the "D" pin that is strictly inside the "diff" layer. I think that may only make sense if the "diff" layer in this case is similar to the original drawn layer for diffusion, before the "gate region" area has been removed during layer derivations.
I wasn't able to find that example in documentation. Do you know where that original example came from?