Add 2-Opt TSP solver

This update introduces a new C++ 2-Opt TSP solver with Python bindings.

src/Mod/CAM/App/CMakeLists.txt:
- Add build and install rules for the new pybind11-based tsp_solver Python module

src/Mod/CAM/App/tsp_solver.cpp:
- Add new C++ implementation of a 2-Opt TSP solver with nearest-neighbor initialization

src/Mod/CAM/App/tsp_solver.h:
- Add TSPPoint struct and TSPSolver class with 2-Opt solve method

src/Mod/CAM/App/tsp_solver_pybind.cpp:
- Add pybind11 wrapper exposing the TSP solver to Python as tsp_solver.solve

src/Mod/CAM/PathScripts/PathUtils.py:
- Add sort_locations_tsp Python wrapper for the C++ TSP solver
- Use tsp_solver.solve for TSP-based
This commit is contained in:
Billy Huddleston
2025-10-17 14:08:31 -04:00
parent 442a36bea6
commit f9347c781b
5 changed files with 216 additions and 0 deletions

View File

@@ -2,6 +2,7 @@
# ***************************************************************************
# * Copyright (c) 2014 Dan Falck <ddfalck@gmail.com> *
# * Copyright (c) 2025 Billy Huddleston <billy@ivdc.com> *
# * *
# * This program is free software; you can redistribute it and/or modify *
# * it under the terms of the GNU Lesser General Public License (LGPL) *
@@ -29,6 +30,7 @@ import Path
import Path.Main.Job as PathJob
import math
from numpy import linspace
import tsp_solver
# lazily loaded modules
from lazy_loader.lazy_loader import LazyLoader
@@ -611,6 +613,17 @@ def sort_locations(locations, keys, attractors=None):
return out
def sort_locations_tsp(locations, keys, attractors=None):
"""
Python wrapper for the C++ TSP solver. Takes a list of dicts (locations),
a list of keys (e.g. ['x', 'y']), and optional attractors.
Returns the sorted list of locations in TSP order, starting at (0,0).
"""
points = [(loc[keys[0]], loc[keys[1]]) for loc in locations]
order = tsp_solver.solve(points)
return [locations[i] for i in order]
def guessDepths(objshape, subs=None):
"""
takes an object shape and optional list of subobjects and returns a depth_params