A consensus-based filtering algorithm with state constraints