An automatic and patient-specific algorithm to design the optimal insertion direction of pedicle screws for spine surgery templates