Twinning in hexagonal materials: application to zirconium and magnesium