Question

I am getting quite a few spikes when offsetting Polygons with the clipper library, this is unfortunately not acceptable in my use case and I have no idea how to get rid of it. I have tried all type of join type settings but could not achieve anything. Any help would be greatly appreciated.

My application layers a model and calculates the outline polygons. It then also has to offset the outlines. Layers with a lot of curves in then tend to get one or more spikes each such as this: enter image description here

Now this does not seem to bad but once it happens to a lot of layers a model becomes like this: enter image description here

It is important to note that without offsetting the outlines I get none of these spikes.

Here is a file containing the input polygons: http://sdrv.ms/H7ysUC

Here is a file containing the output polygons: http://sdrv.ms/1fLoZjT

The parameters for the operation were an offset operation with jtRound JointType with default limit. The delta was -25000. I have also tried all the other JoinTypes with limits ranging from 0 to 1000 but they all created the exact same spike. The other JoinTypes though had some other added strange effects.

Was it helpful?

Solution

OK, I can confirm there's a bug. It happens when adjacent polygon edges are almost collinear.

Here's the fix (that hasn't been heavily tested yet) at about line 4220 in clipper.cs

      void OffsetPoint(JoinType jointype)
      {
          m_sinA = (normals[m_k].X * normals[m_j].Y - normals[m_j].X * normals[m_k].Y);
          if (Math.Abs(m_sinA) < 0.00005) return; //ADD THIS LINE (todo - check this!)
          else if (m_sinA > 1.0) m_sinA = 1.0;
          else if (m_sinA < -1.0) m_sinA = -1.0;

Note: 0.00005 is just a value that's close enough to zero to remove the spike in your supplied sample, but it may need to be readjusted with further testing.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top