A scheme to deal accurately and efficiently with complex angular masks in galaxy surveys