Optimize the performance of the mesh optimization

 - Instead of merging vertices when their distance is lower than the
   maximum distance, merge when the square of their distance is lower
   than the square of the maximum distance. This eliminates calls to
   sqrt().
 - Introduce functions with fixed parameter count for vertex
   calculations instead of doing a lot of list comprehensions.
 - Cache the return value of len(vertexdata).