Rework chain detection code (again) to further improve efficiency.
Rework chain detection code (again) to further improve efficiency.

Time has been saved by finding all the chain end lines first and storing their indices so
that later when we want to find the nearest chain end, we don't have to search through all
of the remaining lines to find the nearest.

Ultimaker/CuraEngine,Ultimaker/CuraEngine,ROBO3D/CuraEngine,ROBO3D/CuraEngine,ROBO3D/CuraEngine