<commit_before>"use strict"

module.exports = triangulatePolyline

var poly2tri = require("poly2tri")

//poly2tri uses "cute" x/y accessors for point members
function PointWrapper(x, y, idx) {
  this.x = x
  this.y = y
  this.idx = idx
}

function triangulatePolyline(loops, positions) {
  //Converts a loop into poly2tri format
  function convertLoop(loop) {
    return loop.map(function(v) {
      var p = positions[v]
      return new PointWrapper(p[0], p[1], v)
    })
  }

  //Find outermost loop
  var numLoops = loops.length
  var outerLoop = 0
  var outerPoint = positions[loops[0][0]]
  for(var i=0; i<numLoops; ++i) {
    var loop = loops[i]
    var n = loop.length
    for(var j=0; j<n; ++j) {
      var p = positions[loop[j]]
      var d = p[1] - outerPoint[1]
      if(d === 0) {
        d = p[0] - outerPoint[0]
      }
      if(d < 0) {
        outerPoint = p
        outerLoop = i
      }
    }
  }

  //Pass geometry to poly2tri
  var ctx = new poly2tri.SweepContext(convertLoop(loops[outerLoop]))
  for(var i=0; i<numLoops; ++i) {
    if(i === outerLoop) {
      continue
    }
    ctx.addHole(convertLoop(loops[i]))
  }

  //Triangulate
  ctx.triangulate()
  var triangles = ctx.getTriangles()

  //Unbox triangles from poly2tri format to list of indices
  return triangles.map(function(tri) {
    return [
      tri.getPoint(0).idx,
      tri.getPoint(1).idx,
      tri.getPoint(2).idx
    ]
  })
}<commit_msg>catch error when poly2tri shits itself<commit_after>"use strict"

module.exports = triangulatePolyline

var poly2tri = require("poly2tri")

//poly2tri uses "cute" x/y accessors for point members
function PointWrapper(x, y, idx) {
  this.x = x
  this.y = y
  this.idx = idx
}

function tryTriangulation(ctx) {
  try {
    ctx.triangulate()
  } catch(e) {
    return false
  }
  return true
}

function triangulatePolyline(loops, positions) {
  //Converts a loop into poly2tri format
  function convertLoop(loop) {
    return loop.map(function(v) {
      var p = positions[v]
      return new PointWrapper(p[0], p[1], v)
    })
  }

  //Find outermost loop
  var numLoops = loops.length
  var outerLoop = 0
  var outerPoint = positions[loops[0][0]]
  for(var i=0; i<numLoops; ++i) {
    var loop = loops[i]
    var n = loop.length
    for(var j=0; j<n; ++j) {
      var p = positions[loop[j]]
      var d = p[1] - outerPoint[1]
      if(d === 0) {
        d = p[0] - outerPoint[0]
      }
      if(d < 0) {
        outerPoint = p
        outerLoop = i
      }
    }
  }

  //Pass geometry to poly2tri
  var ctx = new poly2tri.SweepContext(convertLoop(loops[outerLoop]))
  for(var i=0; i<numLoops; ++i) {
    if(i === outerLoop) {
      continue
    }
    ctx.addHole(convertLoop(loops[i]))
  }

  //Triangulate
  if(!tryTriangulation(ctx)) {
    return []
  }
  var triangles = ctx.getTriangles()

  //Unbox triangles from poly2tri format to list of indices
  return triangles.map(function(tri) {
    return [
      tri.getPoint(0).idx,
      tri.getPoint(1).idx,
      tri.getPoint(2).idx
    ]
  })
}
{'fraction_non_alphanumeric': 0.13432343234323432, 'fraction_numerical': 0.01617161716171617, 'mean_word_length': 2.5997624703087885, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6561863', 'n_tokens_mistral': 1136, 'n_tokens_neox': 1110, 'n_words': 267}