Automatic Derivation of an Optimal Task Frame for Learning and Controlling Contact-Rich Tasks