Refer to Periodic BC Problem in 3D - #6 by conpierce8.
It looks like you need to modify your inside
function so that it returns False
for the points marked 2 and 4 in my illustrations, and that you need to add a fourth condition to map
which handles all points that are not part of the periodic boundary.