A mathematical theory of optical illusions and figural aftereffects