Anisotropic mesh optimization for structured and unstructured meshes