Fast direct solution algorithm for electromagnetic scattering from 3D planar and quasi-planar geometries