title: Three.js polygon triangulation fails in pseudo duplicate points
body: <p>In <a href="https://github.com/mrdoob/three.js/blob/master/build/three.js" rel="nofollow noreferrer">three.js</a> there is a function <code>triangulateShape()</code>. Now I encountered a failure to triangulate polygons that are simplified using Javascript Clipper. Simplifying in Clipper is done using Unioning. <a href="http://en.wikipedia.org/wiki/Simple_polygon" rel="nofollow noreferrer">Wikipedia article</a> determines unioning as finding the simple polygon or polygons containing the area inside either of two simple polygons. The same article says that in simple polygon "exactly two edges meet at each vertex" and also determines a weakly simple polygon, where edges can meet, but says nothing about the edge case where edges doesn't meet, but some or many vertices meet. So it's a bit unclear if this like case is simple polygon or weakly simple polygon.</p>
  
  <p>Clipper has selected a permissive approach: simple polygon can have these like touching (or pseudo duplicate) vertices. <a href="https://sourceforge.net/p/polyclipping/discussion/1148419/thread/4e614a38/" rel="nofollow noreferrer">This Clipper style permissive approach</a> causes that the generated simple polygons are not simple in the meaning of what three.js:s <code>triangulateShape()</code> expects.</p>
  
  <p>The following image shows two examples of this edge case. The left polygon is one "simple" polygon, the red dot is a "duplicate". The right one is as well one "simple" polygon, but the red dot is a "duplicate".</p>
  
  <p><img src="https://i.stack.imgur.com/qIJOW.png" alt="enter image description here"></p>
  
  <p><code>triangulateShape()</code> fails in these cases, because it keeps track of points in array <code>allPointsMap</code> and checks from there if the point is duplicate. To remove these like duplicates I have two options:</p>
  
  <hr>
  
  <p><code>OPTION 1.</code></p>
  
  <p>Change Javascript Clipper internal code to handle these using extra parameter eg. <code>breakPolygonByWeakDuplicates</code> for <code>SimplifyPolygon()</code> and <code>SimplifyPolygons()</code>. As Angus Johnson described <a href="https://sourceforge.net/p/polyclipping/discussion/1148419/thread/4e614a38/" rel="nofollow noreferrer">in his post</a>, the change would be something like:</p>
  
  <p>In the IntersectEdges() method, change the follow from ...</p>
  
  <pre>
  if ( e1Contributing && e2contributing )
  {
    if ( e1stops || e2stops || 
      (e1Wc != 0 && e1Wc != 1) || (e2Wc != 0 && e2Wc != 1) ||
      (e1->polyType != e2->polyType && m_ClipType != ctXor) )
        AddLocalMaxPoly(e1, e2, pt); 
    else
        DoBothEdges( e1, e2, pt );
  }
  </pre>
  
  <p>to ...</p>
  
  <pre>
  
  if ( e1Contributing && e2contributing )
  {
      AddLocalMaxPoly(e1, e2, pt); 
      AddLocalMinPoly(e1, e2, pt);
  }
  </pre>
  
  <p>The change is very easy, but then original Angus Johnson Clipper and Javascript Clipper would not be any more so compatible. Of course if original Clipper would make the change, the Javascript Clipper will follow it.</p>
  
  <hr>
  
  <p><code>OPTION 2.</code></p>
  
  <p>To change three.js <code>triangulateShape()</code> source code to accept also pseudo duplicates.</p>
  
  <hr>
  
  <p>My question is: <strong>In which end this like extra simplification routine should be done? The first end is creation side (Clipper) and the other end is triangulation side (three.js).</strong></p>
  
  <p>I don't know polygon triangulation routines in various 3D libraries, so cannot imagine how permissive triangulation routines in general are. If someone knows this area, he/she could give more sophisticated answer.</p>
  
  <p>Also I don't know how other boolean libraries handles unioning or simplifying this like pseudo duplicates. There surely is a reason why Clipper is permissive in the means of simple polygon (eg. compatibility with other boolean libraries), but definitely this makes problems in triangulating polygons in three.js.</p>
  
  <p>For reference here is the triangulating code of three.js:</p>
  
  <pre><code>triangulateShape: function ( contour, holes ) {
  
      var shapeWithoutHoles = THREE.Shape.Utils.removeHoles( contour, holes );
  
      var shape = shapeWithoutHoles.shape,
          allpoints = shapeWithoutHoles.allpoints,
          isolatedPts = shapeWithoutHoles.isolatedPts;
  
      var triangles = THREE.FontUtils.Triangulate( shape, false ); // True returns indices for points of spooled shape
  
      // To maintain reference to old shape, one must match coordinates, or offset the indices from original arrays. It's probably easier to do the first.
  
      //console.log( "triangles",triangles, triangles.length );
      //console.log( "allpoints",allpoints, allpoints.length );
  
      var i, il, f, face,
          key, index,
          allPointsMap = {},
          isolatedPointsMap = {};
  
      // prepare all points map
  
      for ( i = 0, il = allpoints.length; i &lt; il; i ++ ) {
  
          key = allpoints[ i ].x + ":" + allpoints[ i ].y;
  
          if ( allPointsMap[ key ] !== undefined ) {
  
              console.log( "Duplicate point", key );
  
          }
  
          allPointsMap[ key ] = i;
  
      }
  
      // check all face vertices against all points map
  
      for ( i = 0, il = triangles.length; i &lt; il; i ++ ) {
  
          face = triangles[ i ];
  
          for ( f = 0; f &lt; 3; f ++ ) {
  
              key = face[ f ].x + ":" + face[ f ].y;
  
              index = allPointsMap[ key ];
  
              if ( index !== undefined ) {
  
                  face[ f ] = index;
  
              }
  
          }
  
      }
  
      // check isolated points vertices against all points map
  
      for ( i = 0, il = isolatedPts.length; i &lt; il; i ++ ) {
  
          face = isolatedPts[ i ];
  
          for ( f = 0; f &lt; 3; f ++ ) {
  
              key = face[ f ].x + ":" + face[ f ].y;
  
              index = allPointsMap[ key ];
  
              if ( index !== undefined ) {
  
                  face[ f ] = index;
  
              }
  
          }
  
      }
  
      return triangles.concat( isolatedPts );
  
  }, // end triangulate shapes
  </code></pre>
  
  <hr>
  
  <p>UPDATE: I made one SVG <a href="http://jsbin.com/ugimab/1" rel="nofollow noreferrer">http://jsbin.com/ugimab/1</a> where is an example of a polygon that has point (150,150) which is a weak duplicate or pseudo duplicate. The following show various ways to represent this polygon:</p>
  
  <pre>
  var weakDuplicate1 = [{"X":100,"Y":200},{"X":150,"Y":150},{"X":100,"Y":100},{"X":200,"Y":100},{"X":150,"Y":150},{"X":200,"Y":200}];
  
  var weakDuplicate2 = [100,200, 150,150, 100,100, 200,100, 150,150, 200,200];
  
  var weakDuplicate3 = "M100,200 L150,150 L100,100 L200,100 L150,150 L200,200Z";
  
  </pre>
  
  <hr>
  
  <p>UPDATE: If someone has managed to find a solution for triangulating also polygons which have this like weakly duplicate points, it would be very helpful if you would publish your findings.</p>
  
  <hr>
  
  <p>UPDATE: Tested Option 1, but it was not successful: <a href="http://jsbin.com/owivew/1" rel="nofollow noreferrer">http://jsbin.com/owivew/1</a>. The polygon remains as one piece, although it should be spitted into two parts. Maybe Angus Johnson (Clipper' creator) has better solution to provide.</p>
  
  <hr>
  
  <p>UPDATE: Here is a more complex "simple" polygon (after simplifying in Clipper). All the points that seems to be together are exactly identical. To divide this to truly simple polygons, would require it to be divided into pieces. My eyes say that here is 4 bottom polygons and one (bigger) upper polygon that has a hole, so as a total simplifying this would produce 5 outer polygons and 1 hole. Or alternatively one outer polygon that have 5 holes. Or possibly some other combination of outers and holes. It can be simplified in many different ways.</p>
  
  <p>The fiddle is in <a href="http://jsbin.com/ugimab/3" rel="nofollow noreferrer">http://jsbin.com/ugimab/3</a> (also JSON-version of the polygon).</p>
  
  <p><img src="https://i.stack.imgur.com/3Hhwx.png" alt="enter image description here"></p>
  
  <p>And here are the points numbered from 0 to 25:</p>
  
  <p><img src="https://i.stack.imgur.com/ouqVn.png" alt="enter image description here"></p>
  
  <p>In the image vertices 2,11,14,25 are the same coordinate, so it is a "pseudo-multiple-vertice". Vertex3 is not a duplicate, but it touches the edge 6-7.</p>
  
  <hr>
  
  <p>UPDATE:</p>
  
  <p><a href="https://stackoverflow.com/a/16183613/1691517">The suggested method</a> that is based on moving duplicate points seems to work. If the duplicate point is replaced by two points that are on certain distance of the duplicate coordinate, producing "broken pen nib" effect, the triangulation works, because the produced polygons are then true simple polygons, which is the requirement for triangulator. Also there are not allowed to be duplicates between contour and holes nor between holes and holes. The following image shows the effect of this method. The distance is here 10px to show the effect, but in reality eg. 0.001 is enough to make polygons simple. Also default triangulator in Three.js r58 is not working as expected, but if it is changed to Poly2tri, then all is well. The process is described in this rather long bug report: <a href="https://github.com/mrdoob/three.js/issues/3386" rel="nofollow noreferrer">https://github.com/mrdoob/three.js/issues/3386</a>.</p>
  
  <p><img src="https://i.stack.imgur.com/mr8U2.png" alt="enter image description here"></p>